Ole Troan | 9d42087 | 2017-10-12 13:06:35 +0200 | [diff] [blame] | 1 | # C generation |
| 2 | import datetime |
| 3 | import os |
| 4 | |
| 5 | datestring = datetime.datetime.now() |
| 6 | input_filename = 'inputfil' |
| 7 | top_boilerplate = '''\ |
| 8 | /* |
| 9 | * VLIB API definitions {datestring} |
| 10 | * Input file: {input_filename} |
| 11 | * Automatically generated: please edit the input file NOT this file! |
| 12 | */ |
| 13 | |
| 14 | #if defined(vl_msg_id)||defined(vl_union_id) \\ |
| 15 | || defined(vl_printfun) ||defined(vl_endianfun) \\ |
| 16 | || defined(vl_api_version)||defined(vl_typedefs) \\ |
| 17 | || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\ |
| 18 | || defined(vl_api_version_tuple) |
| 19 | /* ok, something was selected */ |
| 20 | #else |
| 21 | #warning no content included from {input_filename} |
| 22 | #endif |
| 23 | |
| 24 | #define VL_API_PACKED(x) x __attribute__ ((packed)) |
| 25 | ''' |
| 26 | |
| 27 | bottom_boilerplate = '''\ |
| 28 | /****** API CRC (whole file) *****/ |
| 29 | |
| 30 | #ifdef vl_api_version |
| 31 | vl_api_version({input_filename}, {file_crc:#08x}) |
| 32 | |
| 33 | #endif |
| 34 | ''' |
| 35 | |
| 36 | |
| 37 | def msg_ids(s): |
| 38 | output = '''\ |
| 39 | |
| 40 | /****** Message ID / handler enum ******/ |
| 41 | |
| 42 | #ifdef vl_msg_id |
| 43 | ''' |
| 44 | |
| 45 | for t in s['defines']: |
| 46 | output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \ |
| 47 | (t.name.upper(), t.name) |
| 48 | output += "#endif" |
| 49 | |
| 50 | return output |
| 51 | |
| 52 | |
| 53 | def msg_names(s): |
| 54 | output = '''\ |
| 55 | |
| 56 | /****** Message names ******/ |
| 57 | |
| 58 | #ifdef vl_msg_name |
| 59 | ''' |
| 60 | |
| 61 | for t in s['defines']: |
| 62 | dont_trace = 0 if t.dont_trace else 1 |
| 63 | output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace) |
| 64 | output += "#endif" |
| 65 | |
| 66 | return output |
| 67 | |
| 68 | |
| 69 | def msg_name_crc_list(s, suffix): |
| 70 | output = '''\ |
| 71 | |
| 72 | /****** Message name, crc list ******/ |
| 73 | |
| 74 | #ifdef vl_msg_name_crc_list |
| 75 | ''' |
| 76 | output += "#define foreach_vl_msg_name_crc_%s " % suffix |
| 77 | |
| 78 | for t in s['defines']: |
| 79 | output += "\\\n_(VL_API_%s, %s, %08x) " % \ |
| 80 | (t.name.upper(), t.name, t.crc) |
| 81 | output += "\n#endif" |
| 82 | |
| 83 | return output |
| 84 | |
| 85 | |
| 86 | def duplicate_wrapper_head(name): |
| 87 | s = "#ifndef defined_%s\n" % name |
| 88 | s += "#define defined_%s\n" % name |
| 89 | return s |
| 90 | |
| 91 | |
| 92 | def duplicate_wrapper_tail(): |
| 93 | return '#endif\n\n' |
| 94 | |
| 95 | |
| 96 | def typedefs(s, filename): |
| 97 | name = filename.replace('.', '_') |
| 98 | output = '''\ |
| 99 | |
| 100 | |
| 101 | /****** Typedefs ******/ |
| 102 | |
| 103 | #ifdef vl_typedefs |
| 104 | #ifndef included_{module} |
| 105 | #define included_{module} |
| 106 | ''' |
| 107 | output = output.format(module=name) |
| 108 | for e in s['enums']: |
| 109 | output += duplicate_wrapper_head(e.name) |
| 110 | output += "typedef enum {\n" |
| 111 | for b in e.block: |
| 112 | output += " %s = %s,\n" % (b[0], b[1]) |
| 113 | output += '} vl_api_%s_t;\n' % e.name |
| 114 | output += duplicate_wrapper_tail() |
| 115 | |
| 116 | for t in s['typedefs'] + s['defines']: |
| 117 | output += duplicate_wrapper_head(t.name) |
| 118 | output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % t.name |
| 119 | for b in t.block: |
| 120 | if b.type == 'Field': |
| 121 | output += " %s %s;\n" % (b.fieldtype, b.fieldname) |
| 122 | elif b.type == 'Array': |
| 123 | if b.lengthfield: |
| 124 | output += " %s %s[0];\n" % (b.fieldtype, b.fieldname) |
| 125 | else: |
| 126 | output += " %s %s[%s];\n" % (b.fieldtype, b.fieldname, |
| 127 | b.length) |
| 128 | else: |
| 129 | raise ValueError("Error in processing array type %s" % b) |
| 130 | |
| 131 | output += '}) vl_api_%s_t;\n' % t.name |
| 132 | output += duplicate_wrapper_tail() |
| 133 | |
| 134 | output += "\n#endif" |
| 135 | output += "\n#endif\n\n" |
| 136 | |
| 137 | return output |
| 138 | |
| 139 | |
| 140 | format_strings = {'u8': '%u', |
| 141 | 'i8': '%d', |
| 142 | 'u16': '%u', |
| 143 | 'i16': '%d', |
| 144 | 'u32': '%u', |
| 145 | 'i32': '%ld', |
| 146 | 'u64': '%llu', |
| 147 | 'i64': '%llu', |
| 148 | 'f64': '%.2f', } |
| 149 | |
| 150 | |
| 151 | def printfun(s): |
| 152 | output = '''\ |
| 153 | /****** Print functions *****/ |
| 154 | #ifdef vl_printfun |
| 155 | |
| 156 | #ifdef LP64 |
| 157 | #define _uword_fmt \"%lld\" |
| 158 | #define _uword_cast (long long) |
| 159 | #else |
| 160 | #define _uword_fmt \"%ld\" |
| 161 | #define _uword_cast long |
| 162 | #endif |
| 163 | |
| 164 | ''' |
| 165 | for t in s['typedefs'] + s['defines']: |
| 166 | if t.manual_print: |
| 167 | output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name |
| 168 | continue |
| 169 | output += duplicate_wrapper_head(t.name + '_t_print') |
| 170 | output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \ |
| 171 | (t.name, t.name) |
| 172 | output += "void *handle)\n{\n" |
| 173 | output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name |
| 174 | |
| 175 | for o in t.block: |
| 176 | if o.type != 'Field': |
| 177 | continue |
| 178 | if o.fieldtype in format_strings: |
| 179 | output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \ |
| 180 | (o.fieldname, format_strings[o.fieldtype], |
| 181 | o.fieldname) |
| 182 | |
| 183 | output += ' return handle;\n' |
| 184 | output += '}\n\n' |
| 185 | output += duplicate_wrapper_tail() |
| 186 | |
| 187 | output += "\n#endif /* vl_printfun */\n" |
| 188 | |
| 189 | return output |
| 190 | |
| 191 | |
| 192 | endian_strings = { |
| 193 | 'u16': 'clib_net_to_host_u16', |
| 194 | 'u32': 'clib_net_to_host_u32', |
| 195 | 'u64': 'clib_net_to_host_u64', |
| 196 | 'i16': 'clib_net_to_host_u16', |
| 197 | 'i32': 'clib_net_to_host_u32', |
| 198 | 'i64': 'clib_net_to_host_u64', |
| 199 | } |
| 200 | |
| 201 | |
| 202 | def endianfun(s): |
| 203 | output = '''\ |
| 204 | |
| 205 | /****** Endian swap functions *****/\n\ |
| 206 | #ifdef vl_endianfun |
| 207 | |
| 208 | #undef clib_net_to_host_uword |
| 209 | #ifdef LP64 |
| 210 | #define clib_net_to_host_uword clib_net_to_host_u64 |
| 211 | #else |
| 212 | #define clib_net_to_host_uword clib_net_to_host_u32 |
| 213 | #endif |
| 214 | |
| 215 | ''' |
| 216 | |
| 217 | for t in s['typedefs'] + s['defines']: |
| 218 | if t.manual_endian: |
| 219 | output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name |
| 220 | continue |
| 221 | output += duplicate_wrapper_head(t.name + '_t_endian') |
| 222 | output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \ |
| 223 | (t.name, t.name) |
| 224 | output += "\n{\n" |
| 225 | |
| 226 | for o in t.block: |
| 227 | if o.type != 'Field': |
| 228 | continue |
| 229 | if o.fieldtype in endian_strings: |
| 230 | output += " a->%s = %s(a->%s);\n" % \ |
| 231 | (o.fieldname, endian_strings[o.fieldtype], o.fieldname) |
| 232 | else: |
| 233 | output += " /* a->%s = a->%s (no-op) */\n" % \ |
| 234 | (o.fieldname, o.fieldname) |
| 235 | |
| 236 | output += '}\n\n' |
| 237 | output += duplicate_wrapper_tail() |
| 238 | output += "\n#endif /* vl_endianfun */\n\n" |
| 239 | |
| 240 | return output |
| 241 | |
| 242 | |
| 243 | def version_tuple(s, module): |
| 244 | output = '''\ |
| 245 | /****** Version tuple *****/ |
| 246 | |
| 247 | #ifdef vl_api_version_tuple |
| 248 | |
| 249 | ''' |
| 250 | if 'version' in s['options']: |
| 251 | v = s['options']['version'] |
| 252 | (major, minor, patch) = v.split('.') |
| 253 | output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \ |
| 254 | (module, major, minor, patch) |
| 255 | |
| 256 | output += "\n#endif /* vl_api_version_tuple */\n\n" |
| 257 | |
| 258 | return output |
| 259 | |
| 260 | |
| 261 | # |
| 262 | # Plugin entry point |
| 263 | # |
| 264 | def run(input_filename, s, file_crc): |
| 265 | basename = os.path.basename(input_filename) |
| 266 | filename, file_extension = os.path.splitext(basename) |
| 267 | output = top_boilerplate.format(datestring=datestring, |
| 268 | input_filename=basename) |
| 269 | output += msg_ids(s) |
| 270 | output += msg_names(s) |
| 271 | output += msg_name_crc_list(s, filename) |
| 272 | output += typedefs(s, filename + file_extension) |
| 273 | output += printfun(s) |
| 274 | output += endianfun(s) |
| 275 | output += version_tuple(s, basename) |
| 276 | output += bottom_boilerplate.format(input_filename=basename, |
| 277 | file_crc=file_crc) |
| 278 | |
| 279 | return output |