api: autogenerate api trace print/endian
[vpp.git] / src / tools / vppapigen / vppapigen_c.py
1 # C generation
2 import datetime
3 import os
4 import time
5 import sys
6 from io import StringIO
7
8 datestring = datetime.datetime.utcfromtimestamp(
9     int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
10 input_filename = 'inputfil'
11 top_boilerplate = '''\
12 /*
13  * VLIB API definitions {datestring}
14  * Input file: {input_filename}
15  * Automatically generated: please edit the input file NOT this file!
16  */
17
18 #include <stdbool.h>
19 #if defined(vl_msg_id)||defined(vl_union_id) \\
20     || defined(vl_printfun) ||defined(vl_endianfun) \\
21     || defined(vl_api_version)||defined(vl_typedefs) \\
22     || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
23     || defined(vl_api_version_tuple)
24 /* ok, something was selected */
25 #else
26 #warning no content included from {input_filename}
27 #endif
28
29 #define VL_API_PACKED(x) x __attribute__ ((packed))
30 '''
31
32 bottom_boilerplate = '''\
33 /****** API CRC (whole file) *****/
34
35 #ifdef vl_api_version
36 vl_api_version({input_filename}, {file_crc:#08x})
37
38 #endif
39 '''
40
41
42 def msg_ids(s):
43     output = '''\
44
45 /****** Message ID / handler enum ******/
46
47 #ifdef vl_msg_id
48 '''
49
50     for t in s['Define']:
51         output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
52                   (t.name.upper(), t.name)
53     output += "#endif"
54
55     return output
56
57
58 def msg_names(s):
59     output = '''\
60
61 /****** Message names ******/
62
63 #ifdef vl_msg_name
64 '''
65
66     for t in s['Define']:
67         dont_trace = 0 if t.dont_trace else 1
68         output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
69     output += "#endif"
70
71     return output
72
73
74 def msg_name_crc_list(s, suffix):
75     output = '''\
76
77 /****** Message name, crc list ******/
78
79 #ifdef vl_msg_name_crc_list
80 '''
81     output += "#define foreach_vl_msg_name_crc_%s " % suffix
82
83     for t in s['Define']:
84         output += "\\\n_(VL_API_%s, %s, %08x) " % \
85                    (t.name.upper(), t.name, t.crc)
86     output += "\n#endif"
87
88     return output
89
90
91 def api2c(fieldtype):
92     mappingtable = {'string': 'vl_api_string_t', }
93     if fieldtype in mappingtable:
94         return mappingtable[fieldtype]
95     return fieldtype
96
97
98 def typedefs(objs, aliases, filename):
99     name = filename.replace('.', '_')
100     output = '''\
101
102
103 /****** Typedefs ******/
104
105 #ifdef vl_typedefs
106 #ifndef included_{module}_typedef
107 #define included_{module}_typedef
108 '''
109     output = output.format(module=name)
110
111     for k, v in aliases.items():
112         if 'length' in v.alias:
113             output += ('typedef %s vl_api_%s_t[%s];\n'
114                        % (v.alias['type'], k, v.alias['length']))
115         else:
116             output += 'typedef %s vl_api_%s_t;\n' % (v.alias['type'], k)
117
118     for o in objs:
119         tname = o.__class__.__name__
120         if tname == 'Enum':
121             if o.enumtype == 'u32':
122                 output += "typedef enum {\n"
123             else:
124                 output += "typedef enum __attribute__((__packed__)) {\n"
125
126             for b in o.block:
127                 output += "    %s = %s,\n" % (b[0], b[1])
128             output += '} vl_api_%s_t;\n' % o.name
129             if o.enumtype != 'u32':
130                 size1 = 'sizeof(vl_api_%s_t)' % o.name
131                 size2 = 'sizeof(%s)' % o.enumtype
132                 err_str = 'size of API enum %s is wrong' % o.name
133                 output += ('STATIC_ASSERT(%s == %s, "%s");\n'
134                            % (size1, size2, err_str))
135         else:
136             if tname == 'Union':
137                 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
138             else:
139                 output += ("typedef VL_API_PACKED(struct _vl_api_%s {\n"
140                            % o.name)
141             for b in o.block:
142                 if b.type == 'Option':
143                     continue
144                 if b.type == 'Field':
145                     output += "    %s %s;\n" % (api2c(b.fieldtype),
146                                                 b.fieldname)
147                 elif b.type == 'Array':
148                     if b.lengthfield:
149                         output += "    %s %s[0];\n" % (api2c(b.fieldtype),
150                                                        b.fieldname)
151                     else:
152                         # Fixed length strings decay to nul terminated u8
153                         if b.fieldtype == 'string':
154                             if b.modern_vla:
155                                 output += ('    {} {};\n'
156                                            .format(api2c(b.fieldtype),
157                                                    b.fieldname))
158                             else:
159                                 output += ('    u8 {}[{}];\n'
160                                            .format(b.fieldname, b.length))
161                         else:
162                             output += ("    %s %s[%s];\n" %
163                                        (api2c(b.fieldtype), b.fieldname,
164                                         b.length))
165                 else:
166                     raise ValueError("Error in processing type {} for {}"
167                                      .format(b, o.name))
168
169             output += '}) vl_api_%s_t;\n' % o.name
170
171     output += "\n#endif"
172     output += "\n#endif\n\n"
173
174     return output
175
176
177 format_strings = {'u8': '%u',
178                   'bool': '%u',
179                   'i8': '%d',
180                   'u16': '%u',
181                   'i16': '%d',
182                   'u32': '%u',
183                   'i32': '%ld',
184                   'u64': '%llu',
185                   'i64': '%llu',
186                   'f64': '%.2f'}
187
188 noprint_fields = {'_vl_msg_id': None,
189                   'client_index': None,
190                   'context': None}
191
192
193 class Printfun():
194     _dispatch = {}
195
196     def __init__(self, stream):
197         self.stream = stream
198
199     def print_string(self, o, stream):
200         write = stream.write
201         if o.modern_vla:
202             write('    if (vl_api_string_len(&a->{f}) > 0) {{\n'
203                   .format(f=o.fieldname))
204             write('        s = format(s, "\\n%U{f}: %.*s", '
205                   'format_white_space, indent, '
206                   'vl_api_string_len(&a->{f}) - 1, '
207                   'vl_api_from_api_string(&a->{f}));\n'.format(f=o.fieldname))
208             write('    } else {\n')
209             write('        s = format(s, "\\n%U{f}:", '
210                   'format_white_space, indent);\n'.format(f=o.fieldname))
211             write('    }\n')
212         else:
213             write('    s = format(s, "\\n%U{f}: %s", '
214                   'format_white_space, indent, a->{f});\n'
215                   .format(f=o.fieldname))
216
217     def print_field(self, o, stream):
218         write = stream.write
219         if o.fieldname in noprint_fields:
220             return
221         if o.fieldtype in format_strings:
222             f = format_strings[o.fieldtype]
223             write('    s = format(s, "\\n%U{n}: {f}", '
224                   'format_white_space, indent, a->{n});\n'
225                   .format(n=o.fieldname, f=f))
226         else:
227             write('    s = format(s, "\\n%U{n}: %U", '
228                   'format_white_space, indent, '
229                   'format_{t}, &a->{n}, indent);\n'
230                   .format(n=o.fieldname, t=o.fieldtype))
231
232     _dispatch['Field'] = print_field
233
234     def print_array(self, o, stream):
235         write = stream.write
236
237         forloop = '''\
238     for (i = 0; i < {lfield}; i++) {{
239         s = format(s, "\\n%U{n}: %U",
240                    format_white_space, indent, format_{t}, &a->{n}[i], indent);
241     }}
242 '''
243
244         forloop_format = '''\
245     for (i = 0; i < {lfield}; i++) {{
246         s = format(s, "\\n%U{n}: {t}",
247                    format_white_space, indent, a->{n}[i]);
248     }}
249 '''
250
251         if o.fieldtype == 'string':
252             return self.print_string(o, stream)
253
254         if o.fieldtype == 'u8':
255             if o.lengthfield:
256                 write('    s = format(s, "\\n%U{n}: %U", format_white_space, '
257                       'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
258                       .format(n=o.fieldname, lfield=o.lengthfield))
259             else:
260                 write('    s = format(s, "\\n%U{n}: %U", format_white_space, '
261                       'indent, format_hex_bytes, a, {lfield});\n'
262                       .format(n=o.fieldname, lfield=o.length))
263             return
264
265         lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
266         if o.fieldtype in format_strings:
267             write(forloop_format.format(lfield=lfield,
268                                         t=format_strings[o.fieldtype],
269                                         n=o.fieldname))
270         else:
271             write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))
272
273     _dispatch['Array'] = print_array
274
275     def print_alias(self, k, v, stream):
276         write = stream.write
277         if ('length' in v.alias and v.alias['length'] and
278                 v.alias['type'] == 'u8'):
279             write('    return format(s, "%U", format_hex_bytes, a, {});\n'
280                   .format(v.alias['length']))
281         elif v.alias['type'] in format_strings:
282             write('    return format(s, "{}", *a);\n'
283                   .format(format_strings[v.alias['type']]))
284         else:
285             write('    return format(s, "{} (print not implemented)"'
286                   .format(k))
287
288     def print_enum(self, o, stream):
289         write = stream.write
290         write("    switch(*a) {\n")
291         for b in o:
292             write("    case %s:\n" % b[1])
293             write('        return format(s, "{}");\n'.format(b[0]))
294         write('    }\n')
295
296     _dispatch['Enum'] = print_enum
297
298     def print_obj(self, o, stream):
299         write = stream.write
300
301         if o.type in self._dispatch:
302             self._dispatch[o.type](self, o, stream)
303         else:
304             write('    s = format(s, "\\n{} {} {} (print not implemented");\n'
305                   .format(o.type, o.fieldtype, o.fieldname))
306
307
308 def printfun(objs, stream, modulename):
309     write = stream.write
310
311     h = '''\
312 /****** Print functions *****/
313 #ifdef vl_printfun
314 #ifndef included_{module}_printfun
315 #define included_{module}_printfun
316
317 #ifdef LP64
318 #define _uword_fmt \"%lld\"
319 #define _uword_cast (long long)
320 #else
321 #define _uword_fmt \"%ld\"
322 #define _uword_cast long
323 #endif
324
325 '''
326
327     signature = '''\
328 static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle)
329 {{
330     u8 *s = 0;
331     u32 indent __attribute__((unused)) = 2;
332     int i __attribute__((unused));
333 '''
334
335     h = h.format(module=modulename)
336     write(h)
337
338     pp = Printfun(stream)
339     for t in objs:
340         if t.manual_print:
341             write("/***** manual: vl_api_%s_t_print  *****/\n\n" % t.name)
342             continue
343         write(signature.format(name=t.name))
344         write('    /* Message definition: vl_api_{}_t: */\n'.format(t.name))
345         write("    s = format(s, \"vl_api_%s_t:\");\n" % t.name)
346         for o in t.block:
347             pp.print_obj(o, stream)
348         write('    vec_add1(s, 0);\n')
349         write('    vl_print (handle, (char *)s);\n')
350         write('    vec_free (s);\n')
351         write('    return handle;\n')
352         write('}\n\n')
353
354     write("\n#endif")
355     write("\n#endif /* vl_printfun */\n")
356
357     return ''
358
359
360 def printfun_types(objs, aliases, stream, modulename):
361     write = stream.write
362     pp = Printfun(stream)
363
364     h = '''\
365 /****** Print functions *****/
366 #ifdef vl_printfun
367 #ifndef included_{module}_printfun_types
368 #define included_{module}_printfun_types
369
370 '''
371     h = h.format(module=modulename)
372     write(h)
373
374     signature = '''\
375 static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
376 {{
377     vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
378     u32 indent __attribute__((unused)) = va_arg (*args, u32);
379     int i __attribute__((unused));
380     indent += 2;
381 '''
382
383     for k, v in aliases.items():
384         if v.manual_print:
385             write("/***** manual: vl_api_%s_t_print  *****/\n\n" % k)
386             continue
387
388         write(signature.format(name=k))
389         pp.print_alias(k, v, stream)
390         write('}\n\n')
391
392     for t in objs:
393         if t.__class__.__name__ == 'Enum':
394             write(signature.format(name=t.name))
395             pp.print_enum(t.block, stream)
396             write('    return s;\n')
397             write('}\n\n')
398             continue
399
400         if t.manual_print:
401             write("/***** manual: vl_api_%s_t_print  *****/\n\n" % t.name)
402             continue
403
404         write(signature.format(name=t.name))
405         for o in t.block:
406             pp.print_obj(o, stream)
407
408         write('    return s;\n')
409         write('}\n\n')
410
411     write("\n#endif")
412     write("\n#endif /* vl_printfun_types */\n")
413
414
415 def imports(imports):
416     output = '/* Imported API files */\n'
417     output += '#ifndef vl_api_version\n'
418
419     for i in imports:
420         s = i.filename.replace('plugins/', '')
421         output += '#include <{}.h>\n'.format(s)
422     output += '#endif\n'
423     return output
424
425
426 endian_strings = {
427     'u16': 'clib_net_to_host_u16',
428     'u32': 'clib_net_to_host_u32',
429     'u64': 'clib_net_to_host_u64',
430     'i16': 'clib_net_to_host_u16',
431     'i32': 'clib_net_to_host_u32',
432     'i64': 'clib_net_to_host_u64',
433     'f64': 'clib_net_to_host_u64',
434 }
435
436
437 def endianfun_array(o):
438     forloop = '''\
439     for (i = 0; i < {length}; i++) {{
440         a->{name}[i] = {format}(a->{name}[i]);
441     }}
442 '''
443
444     forloop_format = '''\
445     for (i = 0; i < {length}; i++) {{
446         {type}_endian(&a->{name}[i]);
447     }}
448 '''
449
450     output = ''
451     if o.fieldtype == 'u8' or o.fieldtype == 'string':
452         output += '    /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
453     else:
454         lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
455         if o.fieldtype in endian_strings:
456             output += (forloop
457                        .format(length=lfield,
458                                format=endian_strings[o.fieldtype],
459                                name=o.fieldname))
460         else:
461             output += (forloop_format
462                        .format(length=lfield, type=o.fieldtype,
463                                name=o.fieldname))
464     return output
465
466
467 def endianfun_obj(o):
468     output = ''
469     if o.type == 'Array':
470         return endianfun_array(o)
471     elif o.type != 'Field':
472         output += ('    s = format(s, "\\n{} {} {} (print not implemented");\n'
473                    .format(o.type, o.fieldtype, o.fieldname))
474         return output
475     if o.fieldtype in endian_strings:
476         output += ('    a->{name} = {format}(a->{name});\n'
477                    .format(name=o.fieldname,
478                            format=endian_strings[o.fieldtype]))
479     elif o.fieldtype.startswith('vl_api_'):
480         output += ('    {type}_endian(&a->{name});\n'
481                    .format(type=o.fieldtype, name=o.fieldname))
482     else:
483         output += '    /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
484
485     return output
486
487
488 def endianfun(objs, aliases, modulename):
489     output = '''\
490
491 /****** Endian swap functions *****/\n\
492 #ifdef vl_endianfun
493 #ifndef included_{module}_endianfun
494 #define included_{module}_endianfun
495
496 #undef clib_net_to_host_uword
497 #ifdef LP64
498 #define clib_net_to_host_uword clib_net_to_host_u64
499 #else
500 #define clib_net_to_host_uword clib_net_to_host_u32
501 #endif
502
503 '''
504     output = output.format(module=modulename)
505
506     signature = '''\
507 static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
508 {{
509     int i __attribute__((unused));
510 '''
511
512     for k, v in aliases.items():
513         if v.manual_endian:
514             output += "/***** manual: vl_api_%s_t_endian  *****/\n\n" % k
515             continue
516
517         output += signature.format(name=k)
518         if ('length' in v.alias and v.alias['length'] and
519                 v.alias['type'] == 'u8'):
520             output += ('    /* a->{name} = a->{name} (no-op) */\n'
521                        .format(name=k))
522         elif v.alias['type'] in format_strings:
523             output += ('    *a = {}(*a);\n'
524                        .format(endian_strings[v.alias['type']]))
525         else:
526             output += '    /* Not Implemented yet {} */'.format(k)
527         output += '}\n\n'
528
529     for t in objs:
530         if t.__class__.__name__ == 'Enum':
531             output += signature.format(name=t.name)
532             if t.enumtype in endian_strings:
533                 output += ('    *a = {}(*a);\n'
534                            .format(endian_strings[t.enumtype]))
535             else:
536                 output += ('    /* a->{name} = a->{name} (no-op) */\n'
537                            .format(name=t.name))
538
539             output += '}\n\n'
540             continue
541
542         if t.manual_endian:
543             output += "/***** manual: vl_api_%s_t_endian  *****/\n\n" % t.name
544             continue
545
546         output += signature.format(name=t.name)
547
548         for o in t.block:
549             output += endianfun_obj(o)
550         output += '}\n\n'
551
552     output += "\n#endif"
553     output += "\n#endif /* vl_endianfun */\n\n"
554
555     return output
556
557
558 def version_tuple(s, module):
559     output = '''\
560 /****** Version tuple *****/
561
562 #ifdef vl_api_version_tuple
563
564 '''
565     if 'version' in s['Option']:
566         v = s['Option']['version']
567         (major, minor, patch) = v.split('.')
568         output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
569                   (module, major, minor, patch)
570
571     output += "\n#endif /* vl_api_version_tuple */\n\n"
572
573     return output
574
575
576 #
577 # Plugin entry point
578 #
579 def run(input_filename, s):
580     stream = StringIO()
581     basename = os.path.basename(input_filename)
582     filename, file_extension = os.path.splitext(basename)
583     modulename = filename.replace('.', '_')
584
585     output = top_boilerplate.format(datestring=datestring,
586                                     input_filename=basename)
587     output += imports(s['Import'])
588     output += msg_ids(s)
589     output += msg_names(s)
590     output += msg_name_crc_list(s, filename)
591     output += typedefs(s['types'] + s['Define'], s['Alias'],
592                        filename + file_extension)
593     printfun_types(s['types'], s['Alias'], stream, modulename)
594     printfun(s['Define'], stream, modulename)
595     output += stream.getvalue()
596     output += endianfun(s['types'] + s['Define'], s['Alias'],  modulename)
597     output += version_tuple(s, basename)
598     output += bottom_boilerplate.format(input_filename=basename,
599                                         file_crc=s['file_crc'])
600
601     return output