blob: 19efc99dae032997f45c6a54771b03eea46b39ce [file] [log] [blame]
Ole Troan9d420872017-10-12 13:06:35 +02001# C generation
2import datetime
3import os
Nirmoy Dasc9f40222018-06-28 10:18:43 +02004import time
Ole Troan9d420872017-10-12 13:06:35 +02005
Ole Troan413f4a52018-11-28 11:36:05 +01006datestring = datetime.datetime.utcfromtimestamp(
7 int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
Ole Troan9d420872017-10-12 13:06:35 +02008input_filename = 'inputfil'
9top_boilerplate = '''\
10/*
11 * VLIB API definitions {datestring}
12 * Input file: {input_filename}
13 * Automatically generated: please edit the input file NOT this file!
14 */
15
Ole Troan288e0932019-05-29 12:30:05 +020016#include <stdbool.h>
Ole Troan9d420872017-10-12 13:06:35 +020017#if defined(vl_msg_id)||defined(vl_union_id) \\
18 || defined(vl_printfun) ||defined(vl_endianfun) \\
19 || defined(vl_api_version)||defined(vl_typedefs) \\
20 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
21 || defined(vl_api_version_tuple)
22/* ok, something was selected */
23#else
24#warning no content included from {input_filename}
25#endif
26
27#define VL_API_PACKED(x) x __attribute__ ((packed))
28'''
29
30bottom_boilerplate = '''\
31/****** API CRC (whole file) *****/
32
33#ifdef vl_api_version
34vl_api_version({input_filename}, {file_crc:#08x})
35
36#endif
37'''
38
39
40def msg_ids(s):
41 output = '''\
42
43/****** Message ID / handler enum ******/
44
45#ifdef vl_msg_id
46'''
47
Ole Troan2c2feab2018-04-24 00:02:37 -040048 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +020049 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
50 (t.name.upper(), t.name)
51 output += "#endif"
52
53 return output
54
55
56def msg_names(s):
57 output = '''\
58
59/****** Message names ******/
60
61#ifdef vl_msg_name
62'''
63
Ole Troan2c2feab2018-04-24 00:02:37 -040064 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +020065 dont_trace = 0 if t.dont_trace else 1
66 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
67 output += "#endif"
68
69 return output
70
71
72def msg_name_crc_list(s, suffix):
73 output = '''\
74
75/****** Message name, crc list ******/
76
77#ifdef vl_msg_name_crc_list
78'''
79 output += "#define foreach_vl_msg_name_crc_%s " % suffix
80
Ole Troan2c2feab2018-04-24 00:02:37 -040081 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +020082 output += "\\\n_(VL_API_%s, %s, %08x) " % \
83 (t.name.upper(), t.name, t.crc)
84 output += "\n#endif"
85
86 return output
87
88
89def duplicate_wrapper_head(name):
Ole Troan02143df2018-11-30 08:34:11 +010090 s = "#ifndef _vl_api_defined_%s\n" % name
91 s += "#define _vl_api_defined_%s\n" % name
Ole Troan9d420872017-10-12 13:06:35 +020092 return s
93
94
95def duplicate_wrapper_tail():
96 return '#endif\n\n'
97
98
Ole Troan413f4a52018-11-28 11:36:05 +010099def api2c(fieldtype):
100 mappingtable = {'string': 'vl_api_string_t', }
101 if fieldtype in mappingtable:
102 return mappingtable[fieldtype]
103 return fieldtype
104
105
Ole Troan53fffa12018-11-13 12:36:56 +0100106def typedefs(objs, aliases, filename):
Ole Troan9d420872017-10-12 13:06:35 +0200107 name = filename.replace('.', '_')
108 output = '''\
109
110
111/****** Typedefs ******/
112
113#ifdef vl_typedefs
114#ifndef included_{module}
115#define included_{module}
116'''
117 output = output.format(module=name)
Ole Troan53fffa12018-11-13 12:36:56 +0100118
119 for k, v in aliases.items():
120 output += duplicate_wrapper_head(k)
121 if 'length' in v:
122 output += 'typedef %s vl_api_%s_t[%s];\n' % (v['type'], k, v['length'])
123 else:
124 output += 'typedef %s vl_api_%s_t;\n' % (v['type'], k)
125 output += duplicate_wrapper_tail()
126
Ole Troan2c2feab2018-04-24 00:02:37 -0400127 for o in objs:
128 tname = o.__class__.__name__
129 output += duplicate_wrapper_head(o.name)
130 if tname == 'Enum':
Andrew Yourtchenko5c4b9f12019-03-19 14:52:29 +0100131 if o.enumtype == 'u32':
132 output += "typedef enum {\n"
133 else:
134 output += "typedef enum __attribute__((__packed__)) {\n"
135
Ole Troan2c2feab2018-04-24 00:02:37 -0400136 for b in o.block:
137 output += " %s = %s,\n" % (b[0], b[1])
138 output += '} vl_api_%s_t;\n' % o.name
Andrew Yourtchenko5c4b9f12019-03-19 14:52:29 +0100139 if o.enumtype != 'u32':
140 size1 = 'sizeof(vl_api_%s_t)' % o.name
141 size2 = 'sizeof(%s)' % o.enumtype
142 err_str = 'size of API enum %s is wrong' % o.name
143 output += 'STATIC_ASSERT(%s == %s, "%s");\n' % (size1, size2, err_str)
Ole Troan2c2feab2018-04-24 00:02:37 -0400144 else:
145 if tname == 'Union':
146 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
Ole Troan9d420872017-10-12 13:06:35 +0200147 else:
Ole Troan2c2feab2018-04-24 00:02:37 -0400148 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
149 for b in o.block:
150 if b.type == 'Field':
Ole Troan413f4a52018-11-28 11:36:05 +0100151 output += " %s %s;\n" % (api2c(b.fieldtype), b.fieldname)
Ole Troan2c2feab2018-04-24 00:02:37 -0400152 elif b.type == 'Array':
153 if b.lengthfield:
Ole Troan413f4a52018-11-28 11:36:05 +0100154 output += " %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname)
Ole Troan2c2feab2018-04-24 00:02:37 -0400155 else:
Ole Troan413f4a52018-11-28 11:36:05 +0100156 output += " %s %s[%s];\n" % (api2c(b.fieldtype), b.fieldname,
Ole Troan2c2feab2018-04-24 00:02:37 -0400157 b.length)
158 else:
159 raise ValueError("Error in processing array type %s" % b)
Ole Troan9d420872017-10-12 13:06:35 +0200160
Ole Troan2c2feab2018-04-24 00:02:37 -0400161 output += '}) vl_api_%s_t;\n' % o.name
Ole Troan9d420872017-10-12 13:06:35 +0200162 output += duplicate_wrapper_tail()
163
164 output += "\n#endif"
165 output += "\n#endif\n\n"
166
167 return output
168
169
170format_strings = {'u8': '%u',
171 'i8': '%d',
172 'u16': '%u',
173 'i16': '%d',
174 'u32': '%u',
175 'i32': '%ld',
176 'u64': '%llu',
177 'i64': '%llu',
178 'f64': '%.2f', }
179
180
Ole Troan2c2feab2018-04-24 00:02:37 -0400181def printfun(objs):
Ole Troan9d420872017-10-12 13:06:35 +0200182 output = '''\
183/****** Print functions *****/
184#ifdef vl_printfun
185
186#ifdef LP64
187#define _uword_fmt \"%lld\"
188#define _uword_cast (long long)
189#else
190#define _uword_fmt \"%ld\"
191#define _uword_cast long
192#endif
193
194'''
Ole Troan2c2feab2018-04-24 00:02:37 -0400195 for t in objs:
196 if t.__class__.__name__ == 'Enum':
197 continue
Ole Troan9d420872017-10-12 13:06:35 +0200198 if t.manual_print:
199 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
200 continue
201 output += duplicate_wrapper_head(t.name + '_t_print')
202 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
203 (t.name, t.name)
204 output += "void *handle)\n{\n"
205 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
206
207 for o in t.block:
208 if o.type != 'Field':
209 continue
210 if o.fieldtype in format_strings:
211 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
212 (o.fieldname, format_strings[o.fieldtype],
213 o.fieldname)
214
215 output += ' return handle;\n'
216 output += '}\n\n'
217 output += duplicate_wrapper_tail()
218
219 output += "\n#endif /* vl_printfun */\n"
220
221 return output
222
223
224endian_strings = {
225 'u16': 'clib_net_to_host_u16',
226 'u32': 'clib_net_to_host_u32',
227 'u64': 'clib_net_to_host_u64',
228 'i16': 'clib_net_to_host_u16',
229 'i32': 'clib_net_to_host_u32',
230 'i64': 'clib_net_to_host_u64',
Paul Vinciguerra07f87542019-07-30 18:24:41 -0400231 'f64': 'clib_net_to_host_u64',
Ole Troan9d420872017-10-12 13:06:35 +0200232}
233
234
Ole Troan2c2feab2018-04-24 00:02:37 -0400235def endianfun(objs):
Ole Troan9d420872017-10-12 13:06:35 +0200236 output = '''\
237
238/****** Endian swap functions *****/\n\
239#ifdef vl_endianfun
240
241#undef clib_net_to_host_uword
242#ifdef LP64
243#define clib_net_to_host_uword clib_net_to_host_u64
244#else
245#define clib_net_to_host_uword clib_net_to_host_u32
246#endif
247
248'''
249
Ole Troan2c2feab2018-04-24 00:02:37 -0400250 for t in objs:
251 if t.__class__.__name__ == 'Enum':
252 continue
Ole Troan9d420872017-10-12 13:06:35 +0200253 if t.manual_endian:
254 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
255 continue
256 output += duplicate_wrapper_head(t.name + '_t_endian')
257 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
258 (t.name, t.name)
259 output += "\n{\n"
260
261 for o in t.block:
262 if o.type != 'Field':
263 continue
264 if o.fieldtype in endian_strings:
265 output += " a->%s = %s(a->%s);\n" % \
266 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
267 else:
268 output += " /* a->%s = a->%s (no-op) */\n" % \
269 (o.fieldname, o.fieldname)
270
271 output += '}\n\n'
272 output += duplicate_wrapper_tail()
273 output += "\n#endif /* vl_endianfun */\n\n"
274
275 return output
276
277
278def version_tuple(s, module):
279 output = '''\
280/****** Version tuple *****/
281
282#ifdef vl_api_version_tuple
283
284'''
Ole Troan2c2feab2018-04-24 00:02:37 -0400285 if 'version' in s['Option']:
286 v = s['Option']['version']
Ole Troan9d420872017-10-12 13:06:35 +0200287 (major, minor, patch) = v.split('.')
288 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
289 (module, major, minor, patch)
290
291 output += "\n#endif /* vl_api_version_tuple */\n\n"
292
293 return output
294
295
296#
297# Plugin entry point
298#
Ole Troan8dbfb432019-04-24 14:31:18 +0200299def run(input_filename, s):
Ole Troan9d420872017-10-12 13:06:35 +0200300 basename = os.path.basename(input_filename)
301 filename, file_extension = os.path.splitext(basename)
302 output = top_boilerplate.format(datestring=datestring,
303 input_filename=basename)
304 output += msg_ids(s)
305 output += msg_names(s)
306 output += msg_name_crc_list(s, filename)
Ole Troan53fffa12018-11-13 12:36:56 +0100307 output += typedefs(s['types'] + s['Define'], s['Alias'], filename + file_extension)
Ole Troan2c2feab2018-04-24 00:02:37 -0400308 output += printfun(s['types'] + s['Define'])
309 output += endianfun(s['types'] + s['Define'])
Ole Troan9d420872017-10-12 13:06:35 +0200310 output += version_tuple(s, basename)
311 output += bottom_boilerplate.format(input_filename=basename,
Ole Troan8dbfb432019-04-24 14:31:18 +0200312 file_crc=s['file_crc'])
Ole Troan9d420872017-10-12 13:06:35 +0200313
314 return output