xlnx-zynqmp: Connect the sysbus AHCI to ZynqMP
[qemu/cris-port.git] / scripts / qapi-types.py
blobf2428f3807a88a87e00f702a7269ffc63c8213d5
2 # QAPI types generator
4 # Copyright IBM, Corp. 2011
6 # Authors:
7 # Anthony Liguori <aliguori@us.ibm.com>
9 # This work is licensed under the terms of the GNU GPL, version 2.
10 # See the COPYING file in the top-level directory.
12 from ordereddict import OrderedDict
13 from qapi import *
15 def generate_fwd_builtin(name):
16 return mcgen('''
18 typedef struct %(name)sList {
19 union {
20 %(type)s value;
21 uint64_t padding;
23 struct %(name)sList *next;
24 } %(name)sList;
25 ''',
26 type=c_type(name),
27 name=name)
29 def generate_fwd_struct(name):
30 return mcgen('''
32 typedef struct %(name)s %(name)s;
34 typedef struct %(name)sList {
35 union {
36 %(name)s *value;
37 uint64_t padding;
39 struct %(name)sList *next;
40 } %(name)sList;
41 ''',
42 name=c_name(name))
44 def generate_fwd_enum_struct(name):
45 return mcgen('''
47 typedef struct %(name)sList {
48 union {
49 %(name)s value;
50 uint64_t padding;
52 struct %(name)sList *next;
53 } %(name)sList;
54 ''',
55 name=c_name(name))
57 def generate_struct_fields(members):
58 ret = ''
60 for argname, argentry, optional in parse_args(members):
61 if optional:
62 ret += mcgen('''
63 bool has_%(c_name)s;
64 ''',
65 c_name=c_name(argname))
66 ret += mcgen('''
67 %(c_type)s %(c_name)s;
68 ''',
69 c_type=c_type(argentry), c_name=c_name(argname))
71 return ret
73 def generate_struct(expr):
75 structname = expr.get('struct', "")
76 members = expr['data']
77 base = expr.get('base')
79 ret = mcgen('''
81 struct %(name)s {
82 ''',
83 name=c_name(structname))
85 if base:
86 ret += generate_struct_fields({'base': base})
88 ret += generate_struct_fields(members)
90 # Make sure that all structs have at least one field; this avoids
91 # potential issues with attempting to malloc space for zero-length structs
92 # in C, and also incompatibility with C++ (where an empty struct is size 1).
93 if not base and not members:
94 ret += mcgen('''
95 char qapi_dummy_field_for_empty_struct;
96 ''')
98 ret += mcgen('''
100 ''')
102 return ret
104 def generate_enum_lookup(name, values):
105 ret = mcgen('''
107 const char *const %(name)s_lookup[] = {
108 ''',
109 name=c_name(name))
110 for value in values:
111 index = c_enum_const(name, value)
112 ret += mcgen('''
113 [%(index)s] = "%(value)s",
114 ''',
115 index = index, value = value)
117 max_index = c_enum_const(name, 'MAX')
118 ret += mcgen('''
119 [%(max_index)s] = NULL,
121 ''',
122 max_index=max_index)
123 return ret
125 def generate_enum(name, values):
126 name = c_name(name)
127 lookup_decl = mcgen('''
129 extern const char *const %(name)s_lookup[];
130 ''',
131 name=name)
133 enum_decl = mcgen('''
135 typedef enum %(name)s {
136 ''',
137 name=name)
139 # append automatically generated _MAX value
140 enum_values = values + [ 'MAX' ]
142 i = 0
143 for value in enum_values:
144 enum_full_value = c_enum_const(name, value)
145 enum_decl += mcgen('''
146 %(enum_full_value)s = %(i)d,
147 ''',
148 enum_full_value = enum_full_value,
149 i=i)
150 i += 1
152 enum_decl += mcgen('''
153 } %(name)s;
154 ''',
155 name=name)
157 return enum_decl + lookup_decl
159 def generate_alternate_qtypes(expr):
161 name = expr['alternate']
162 members = expr['data']
164 ret = mcgen('''
166 const int %(name)s_qtypes[QTYPE_MAX] = {
167 ''',
168 name=c_name(name))
170 for key in members:
171 qtype = find_alternate_member_qtype(members[key])
172 assert qtype, "Invalid alternate member"
174 ret += mcgen('''
175 [%(qtype)s] = %(enum_const)s,
176 ''',
177 qtype = qtype,
178 enum_const = c_enum_const(name + 'Kind', key))
180 ret += mcgen('''
182 ''')
183 return ret
186 def generate_union(expr, meta):
188 name = c_name(expr[meta])
189 typeinfo = expr['data']
191 base = expr.get('base')
192 discriminator = expr.get('discriminator')
194 enum_define = discriminator_find_enum_define(expr)
195 if enum_define:
196 discriminator_type_name = enum_define['enum_name']
197 else:
198 discriminator_type_name = '%sKind' % (name)
200 ret = mcgen('''
202 struct %(name)s {
203 ''',
204 name=name)
205 if base:
206 ret += mcgen('''
207 /* Members inherited from %(c_name)s: */
208 ''',
209 c_name=c_name(base))
210 base_fields = find_struct(base)['data']
211 ret += generate_struct_fields(base_fields)
212 ret += mcgen('''
213 /* Own members: */
214 ''')
215 else:
216 assert not discriminator
217 ret += mcgen('''
218 %(discriminator_type_name)s kind;
219 ''',
220 discriminator_type_name=c_name(discriminator_type_name))
222 # FIXME: What purpose does data serve, besides preventing a union that
223 # has a branch named 'data'? We use it in qapi-visit.py to decide
224 # whether to bypass the switch statement if visiting the discriminator
225 # failed; but since we 0-initialize structs, and cannot tell what
226 # branch of the union is in use if the discriminator is invalid, there
227 # should not be any data leaks even without a data pointer. Or, if
228 # 'data' is merely added to guarantee we don't have an empty union,
229 # shouldn't we enforce that at .json parse time?
230 ret += mcgen('''
231 union { /* union tag is @%(c_name)s */
232 void *data;
233 ''',
234 c_name=c_name(discriminator or 'kind'))
236 for key in typeinfo:
237 ret += mcgen('''
238 %(c_type)s %(c_name)s;
239 ''',
240 c_type=c_type(typeinfo[key]),
241 c_name=c_name(key))
243 ret += mcgen('''
246 ''')
247 if meta == 'alternate':
248 ret += mcgen('''
249 extern const int %(name)s_qtypes[];
250 ''',
251 name=name)
254 return ret
256 def generate_type_cleanup_decl(name):
257 ret = mcgen('''
258 void qapi_free_%(name)s(%(c_type)s obj);
259 ''',
260 c_type=c_type(name), name=c_name(name))
261 return ret
263 def generate_type_cleanup(name):
264 ret = mcgen('''
266 void qapi_free_%(name)s(%(c_type)s obj)
268 QapiDeallocVisitor *md;
269 Visitor *v;
271 if (!obj) {
272 return;
275 md = qapi_dealloc_visitor_new();
276 v = qapi_dealloc_get_visitor(md);
277 visit_type_%(name)s(v, &obj, NULL, NULL);
278 qapi_dealloc_visitor_cleanup(md);
280 ''',
281 c_type=c_type(name), name=c_name(name))
282 return ret
284 do_builtins = False
286 (input_file, output_dir, do_c, do_h, prefix, opts) = \
287 parse_command_line("b", ["builtins"])
289 for o, a in opts:
290 if o in ("-b", "--builtins"):
291 do_builtins = True
293 c_comment = '''
295 * deallocation functions for schema-defined QAPI types
297 * Copyright IBM, Corp. 2011
299 * Authors:
300 * Anthony Liguori <aliguori@us.ibm.com>
301 * Michael Roth <mdroth@linux.vnet.ibm.com>
303 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
304 * See the COPYING.LIB file in the top-level directory.
308 h_comment = '''
310 * schema-defined QAPI types
312 * Copyright IBM, Corp. 2011
314 * Authors:
315 * Anthony Liguori <aliguori@us.ibm.com>
317 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
318 * See the COPYING.LIB file in the top-level directory.
323 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
324 'qapi-types.c', 'qapi-types.h',
325 c_comment, h_comment)
327 fdef.write(mcgen('''
328 #include "qapi/dealloc-visitor.h"
329 #include "%(prefix)sqapi-types.h"
330 #include "%(prefix)sqapi-visit.h"
331 ''',
332 prefix=prefix))
334 fdecl.write(mcgen('''
335 #include <stdbool.h>
336 #include <stdint.h>
337 '''))
339 exprs = parse_schema(input_file)
341 fdecl.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
342 for typename in builtin_types.keys():
343 fdecl.write(generate_fwd_builtin(typename))
344 fdecl.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
346 for expr in exprs:
347 ret = ""
348 if expr.has_key('struct'):
349 ret += generate_fwd_struct(expr['struct'])
350 elif expr.has_key('enum'):
351 ret += generate_enum(expr['enum'], expr['data'])
352 ret += generate_fwd_enum_struct(expr['enum'])
353 fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
354 elif expr.has_key('union'):
355 ret += generate_fwd_struct(expr['union'])
356 enum_define = discriminator_find_enum_define(expr)
357 if not enum_define:
358 ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
359 fdef.write(generate_enum_lookup('%sKind' % expr['union'],
360 expr['data'].keys()))
361 elif expr.has_key('alternate'):
362 ret += generate_fwd_struct(expr['alternate'])
363 ret += generate_enum('%sKind' % expr['alternate'], expr['data'].keys())
364 fdef.write(generate_enum_lookup('%sKind' % expr['alternate'],
365 expr['data'].keys()))
366 fdef.write(generate_alternate_qtypes(expr))
367 else:
368 continue
369 fdecl.write(ret)
371 # to avoid header dependency hell, we always generate declarations
372 # for built-in types in our header files and simply guard them
373 fdecl.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
374 for typename in builtin_types.keys():
375 fdecl.write(generate_type_cleanup_decl(typename + "List"))
376 fdecl.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
378 # ...this doesn't work for cases where we link in multiple objects that
379 # have the functions defined, so we use -b option to provide control
380 # over these cases
381 if do_builtins:
382 for typename in builtin_types.keys():
383 fdef.write(generate_type_cleanup(typename + "List"))
385 for expr in exprs:
386 ret = ""
387 if expr.has_key('struct'):
388 ret += generate_struct(expr) + "\n"
389 ret += generate_type_cleanup_decl(expr['struct'] + "List")
390 fdef.write(generate_type_cleanup(expr['struct'] + "List"))
391 ret += generate_type_cleanup_decl(expr['struct'])
392 fdef.write(generate_type_cleanup(expr['struct']))
393 elif expr.has_key('union'):
394 ret += generate_union(expr, 'union') + "\n"
395 ret += generate_type_cleanup_decl(expr['union'] + "List")
396 fdef.write(generate_type_cleanup(expr['union'] + "List"))
397 ret += generate_type_cleanup_decl(expr['union'])
398 fdef.write(generate_type_cleanup(expr['union']))
399 elif expr.has_key('alternate'):
400 ret += generate_union(expr, 'alternate') + "\n"
401 ret += generate_type_cleanup_decl(expr['alternate'] + "List")
402 fdef.write(generate_type_cleanup(expr['alternate'] + "List"))
403 ret += generate_type_cleanup_decl(expr['alternate'])
404 fdef.write(generate_type_cleanup(expr['alternate']))
405 elif expr.has_key('enum'):
406 ret += "\n" + generate_type_cleanup_decl(expr['enum'] + "List")
407 fdef.write(generate_type_cleanup(expr['enum'] + "List"))
408 else:
409 continue
410 fdecl.write(ret)
412 close_output(fdef, fdecl)