blob: e189b0244546529723d0ebac893faf2f24bc655f [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
16#if defined(vl_msg_id)||defined(vl_union_id) \\
17 || defined(vl_printfun) ||defined(vl_endianfun) \\
18 || defined(vl_api_version)||defined(vl_typedefs) \\
19 || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
20 || defined(vl_api_version_tuple)
21/* ok, something was selected */
22#else
23#warning no content included from {input_filename}
24#endif
25
26#define VL_API_PACKED(x) x __attribute__ ((packed))
27'''
28
29bottom_boilerplate = '''\
30/****** API CRC (whole file) *****/
31
32#ifdef vl_api_version
33vl_api_version({input_filename}, {file_crc:#08x})
34
35#endif
36'''
37
38
39def msg_ids(s):
40 output = '''\
41
42/****** Message ID / handler enum ******/
43
44#ifdef vl_msg_id
45'''
46
Ole Troan2c2feab2018-04-24 00:02:37 -040047 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +020048 output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
49 (t.name.upper(), t.name)
50 output += "#endif"
51
52 return output
53
54
55def msg_names(s):
56 output = '''\
57
58/****** Message names ******/
59
60#ifdef vl_msg_name
61'''
62
Ole Troan2c2feab2018-04-24 00:02:37 -040063 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +020064 dont_trace = 0 if t.dont_trace else 1
65 output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
66 output += "#endif"
67
68 return output
69
70
71def msg_name_crc_list(s, suffix):
72 output = '''\
73
74/****** Message name, crc list ******/
75
76#ifdef vl_msg_name_crc_list
77'''
78 output += "#define foreach_vl_msg_name_crc_%s " % suffix
79
Ole Troan2c2feab2018-04-24 00:02:37 -040080 for t in s['Define']:
Ole Troan9d420872017-10-12 13:06:35 +020081 output += "\\\n_(VL_API_%s, %s, %08x) " % \
82 (t.name.upper(), t.name, t.crc)
83 output += "\n#endif"
84
85 return output
86
87
88def duplicate_wrapper_head(name):
Ole Troan02143df2018-11-30 08:34:11 +010089 s = "#ifndef _vl_api_defined_%s\n" % name
90 s += "#define _vl_api_defined_%s\n" % name
Ole Troan9d420872017-10-12 13:06:35 +020091 return s
92
93
94def duplicate_wrapper_tail():
95 return '#endif\n\n'
96
97
Ole Troan413f4a52018-11-28 11:36:05 +010098def api2c(fieldtype):
99 mappingtable = {'string': 'vl_api_string_t', }
100 if fieldtype in mappingtable:
101 return mappingtable[fieldtype]
102 return fieldtype
103
104
Ole Troan53fffa12018-11-13 12:36:56 +0100105def typedefs(objs, aliases, filename):
Ole Troan9d420872017-10-12 13:06:35 +0200106 name = filename.replace('.', '_')
107 output = '''\
108
109
110/****** Typedefs ******/
111
112#ifdef vl_typedefs
113#ifndef included_{module}
114#define included_{module}
115'''
116 output = output.format(module=name)
Ole Troan53fffa12018-11-13 12:36:56 +0100117
118 for k, v in aliases.items():
119 output += duplicate_wrapper_head(k)
120 if 'length' in v:
121 output += 'typedef %s vl_api_%s_t[%s];\n' % (v['type'], k, v['length'])
122 else:
123 output += 'typedef %s vl_api_%s_t;\n' % (v['type'], k)
124 output += duplicate_wrapper_tail()
125
Ole Troan2c2feab2018-04-24 00:02:37 -0400126 for o in objs:
127 tname = o.__class__.__name__
128 output += duplicate_wrapper_head(o.name)
129 if tname == 'Enum':
130 output += "typedef enum {\n"
131 for b in o.block:
132 output += " %s = %s,\n" % (b[0], b[1])
133 output += '} vl_api_%s_t;\n' % o.name
134 else:
135 if tname == 'Union':
136 output += "typedef VL_API_PACKED(union _vl_api_%s {\n" % o.name
Ole Troan9d420872017-10-12 13:06:35 +0200137 else:
Ole Troan2c2feab2018-04-24 00:02:37 -0400138 output += "typedef VL_API_PACKED(struct _vl_api_%s {\n" % o.name
139 for b in o.block:
140 if b.type == 'Field':
Ole Troan413f4a52018-11-28 11:36:05 +0100141 output += " %s %s;\n" % (api2c(b.fieldtype), b.fieldname)
Ole Troan2c2feab2018-04-24 00:02:37 -0400142 elif b.type == 'Array':
143 if b.lengthfield:
Ole Troan413f4a52018-11-28 11:36:05 +0100144 output += " %s %s[0];\n" % (api2c(b.fieldtype), b.fieldname)
Ole Troan2c2feab2018-04-24 00:02:37 -0400145 else:
Ole Troan413f4a52018-11-28 11:36:05 +0100146 output += " %s %s[%s];\n" % (api2c(b.fieldtype), b.fieldname,
Ole Troan2c2feab2018-04-24 00:02:37 -0400147 b.length)
148 else:
149 raise ValueError("Error in processing array type %s" % b)
Ole Troan9d420872017-10-12 13:06:35 +0200150
Ole Troan2c2feab2018-04-24 00:02:37 -0400151 output += '}) vl_api_%s_t;\n' % o.name
Ole Troan9d420872017-10-12 13:06:35 +0200152 output += duplicate_wrapper_tail()
153
154 output += "\n#endif"
155 output += "\n#endif\n\n"
156
157 return output
158
159
160format_strings = {'u8': '%u',
161 'i8': '%d',
162 'u16': '%u',
163 'i16': '%d',
164 'u32': '%u',
165 'i32': '%ld',
166 'u64': '%llu',
167 'i64': '%llu',
168 'f64': '%.2f', }
169
170
Ole Troan2c2feab2018-04-24 00:02:37 -0400171def printfun(objs):
Ole Troan9d420872017-10-12 13:06:35 +0200172 output = '''\
173/****** Print functions *****/
174#ifdef vl_printfun
175
176#ifdef LP64
177#define _uword_fmt \"%lld\"
178#define _uword_cast (long long)
179#else
180#define _uword_fmt \"%ld\"
181#define _uword_cast long
182#endif
183
184'''
Ole Troan2c2feab2018-04-24 00:02:37 -0400185 for t in objs:
186 if t.__class__.__name__ == 'Enum':
187 continue
Ole Troan9d420872017-10-12 13:06:35 +0200188 if t.manual_print:
189 output += "/***** manual: vl_api_%s_t_print *****/\n\n" % t.name
190 continue
191 output += duplicate_wrapper_head(t.name + '_t_print')
192 output += "static inline void *vl_api_%s_t_print (vl_api_%s_t *a," % \
193 (t.name, t.name)
194 output += "void *handle)\n{\n"
195 output += " vl_print(handle, \"vl_api_%s_t:\\n\");\n" % t.name
196
197 for o in t.block:
198 if o.type != 'Field':
199 continue
200 if o.fieldtype in format_strings:
201 output += " vl_print(handle, \"%s: %s\\n\", a->%s);\n" % \
202 (o.fieldname, format_strings[o.fieldtype],
203 o.fieldname)
204
205 output += ' return handle;\n'
206 output += '}\n\n'
207 output += duplicate_wrapper_tail()
208
209 output += "\n#endif /* vl_printfun */\n"
210
211 return output
212
213
214endian_strings = {
215 'u16': 'clib_net_to_host_u16',
216 'u32': 'clib_net_to_host_u32',
217 'u64': 'clib_net_to_host_u64',
218 'i16': 'clib_net_to_host_u16',
219 'i32': 'clib_net_to_host_u32',
220 'i64': 'clib_net_to_host_u64',
221}
222
223
Ole Troan2c2feab2018-04-24 00:02:37 -0400224def endianfun(objs):
Ole Troan9d420872017-10-12 13:06:35 +0200225 output = '''\
226
227/****** Endian swap functions *****/\n\
228#ifdef vl_endianfun
229
230#undef clib_net_to_host_uword
231#ifdef LP64
232#define clib_net_to_host_uword clib_net_to_host_u64
233#else
234#define clib_net_to_host_uword clib_net_to_host_u32
235#endif
236
237'''
238
Ole Troan2c2feab2018-04-24 00:02:37 -0400239 for t in objs:
240 if t.__class__.__name__ == 'Enum':
241 continue
Ole Troan9d420872017-10-12 13:06:35 +0200242 if t.manual_endian:
243 output += "/***** manual: vl_api_%s_t_endian *****/\n\n" % t.name
244 continue
245 output += duplicate_wrapper_head(t.name + '_t_endian')
246 output += "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)" % \
247 (t.name, t.name)
248 output += "\n{\n"
249
250 for o in t.block:
251 if o.type != 'Field':
252 continue
253 if o.fieldtype in endian_strings:
254 output += " a->%s = %s(a->%s);\n" % \
255 (o.fieldname, endian_strings[o.fieldtype], o.fieldname)
256 else:
257 output += " /* a->%s = a->%s (no-op) */\n" % \
258 (o.fieldname, o.fieldname)
259
260 output += '}\n\n'
261 output += duplicate_wrapper_tail()
262 output += "\n#endif /* vl_endianfun */\n\n"
263
264 return output
265
266
267def version_tuple(s, module):
268 output = '''\
269/****** Version tuple *****/
270
271#ifdef vl_api_version_tuple
272
273'''
Ole Troan2c2feab2018-04-24 00:02:37 -0400274 if 'version' in s['Option']:
275 v = s['Option']['version']
Ole Troan9d420872017-10-12 13:06:35 +0200276 (major, minor, patch) = v.split('.')
277 output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
278 (module, major, minor, patch)
279
280 output += "\n#endif /* vl_api_version_tuple */\n\n"
281
282 return output
283
284
285#
286# Plugin entry point
287#
288def run(input_filename, s, file_crc):
289 basename = os.path.basename(input_filename)
290 filename, file_extension = os.path.splitext(basename)
291 output = top_boilerplate.format(datestring=datestring,
292 input_filename=basename)
293 output += msg_ids(s)
294 output += msg_names(s)
295 output += msg_name_crc_list(s, filename)
Ole Troan53fffa12018-11-13 12:36:56 +0100296 output += typedefs(s['types'] + s['Define'], s['Alias'], filename + file_extension)
Ole Troan2c2feab2018-04-24 00:02:37 -0400297 output += printfun(s['types'] + s['Define'])
298 output += endianfun(s['types'] + s['Define'])
Ole Troan9d420872017-10-12 13:06:35 +0200299 output += version_tuple(s, basename)
300 output += bottom_boilerplate.format(input_filename=basename,
301 file_crc=file_crc)
302
303 return output