microblaze: boot: Use cpu_set_pc()
[qemu/cris-port.git] / scripts / qapi-visit.py
blob5b993364885e4aaf09e2e8d6481ed5fa8b3c99c7
2 # QAPI visitor generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 Red Hat, Inc.
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
12 # This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
15 from ordereddict import OrderedDict
16 from qapi import *
17 import re
19 implicit_structs = []
21 def generate_visit_implicit_struct(type):
22 global implicit_structs
23 if type in implicit_structs:
24 return ''
25 implicit_structs.append(type)
26 return mcgen('''
28 static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error **errp)
30 Error *err = NULL;
32 visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err);
33 if (!err) {
34 visit_type_%(c_type)s_fields(m, obj, errp);
35 visit_end_implicit_struct(m, &err);
37 error_propagate(errp, err);
39 ''',
40 c_type=type_name(type))
42 def generate_visit_struct_fields(name, members, base = None):
43 substructs = []
44 ret = ''
46 if base:
47 ret += generate_visit_implicit_struct(base)
49 ret += mcgen('''
51 static void visit_type_%(name)s_fields(Visitor *m, %(name)s **obj, Error **errp)
53 Error *err = NULL;
54 ''',
55 name=c_name(name))
56 push_indent()
58 if base:
59 ret += mcgen('''
60 visit_type_implicit_%(type)s(m, &(*obj)->%(c_name)s, &err);
61 if (err) {
62 goto out;
64 ''',
65 type=type_name(base), c_name=c_name('base'))
67 for argname, argentry, optional in parse_args(members):
68 if optional:
69 ret += mcgen('''
70 visit_optional(m, &(*obj)->has_%(c_name)s, "%(name)s", &err);
71 if (!err && (*obj)->has_%(c_name)s) {
72 ''',
73 c_name=c_name(argname), name=argname)
74 push_indent()
76 ret += mcgen('''
77 visit_type_%(type)s(m, &(*obj)->%(c_name)s, "%(name)s", &err);
78 ''',
79 type=type_name(argentry), c_name=c_name(argname),
80 name=argname)
82 if optional:
83 pop_indent()
84 ret += mcgen('''
86 ''')
87 ret += mcgen('''
88 if (err) {
89 goto out;
91 ''')
93 pop_indent()
94 if re.search('^ *goto out\\;', ret, re.MULTILINE):
95 ret += mcgen('''
97 out:
98 ''')
99 ret += mcgen('''
100 error_propagate(errp, err);
102 ''')
103 return ret
106 def generate_visit_struct_body(name, members):
107 ret = mcgen('''
108 Error *err = NULL;
110 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
111 if (!err) {
112 if (*obj) {
113 visit_type_%(c_name)s_fields(m, obj, errp);
115 visit_end_struct(m, &err);
117 error_propagate(errp, err);
118 ''',
119 name=name, c_name=c_name(name))
121 return ret
123 def generate_visit_struct(expr):
125 name = expr['struct']
126 members = expr['data']
127 base = expr.get('base')
129 ret = generate_visit_struct_fields(name, members, base)
131 ret += mcgen('''
133 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
135 ''',
136 name=c_name(name))
138 ret += generate_visit_struct_body(name, members)
140 ret += mcgen('''
142 ''')
143 return ret
145 def generate_visit_list(name, members):
146 return mcgen('''
148 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp)
150 Error *err = NULL;
151 GenericList *i, **prev;
153 visit_start_list(m, name, &err);
154 if (err) {
155 goto out;
158 for (prev = (GenericList **)obj;
159 !err && (i = visit_next_list(m, prev, &err)) != NULL;
160 prev = &i) {
161 %(name)sList *native_i = (%(name)sList *)i;
162 visit_type_%(name)s(m, &native_i->value, NULL, &err);
165 error_propagate(errp, err);
166 err = NULL;
167 visit_end_list(m, &err);
168 out:
169 error_propagate(errp, err);
171 ''',
172 name=type_name(name))
174 def generate_visit_enum(name, members):
175 return mcgen('''
177 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp)
179 visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
181 ''',
182 name=c_name(name))
184 def generate_visit_alternate(name, members):
185 ret = mcgen('''
187 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
189 Error *err = NULL;
191 visit_start_implicit_struct(m, (void**) obj, sizeof(%(name)s), &err);
192 if (err) {
193 goto out;
195 visit_get_next_type(m, (int*) &(*obj)->kind, %(name)s_qtypes, name, &err);
196 if (err) {
197 goto out_end;
199 switch ((*obj)->kind) {
200 ''',
201 name=c_name(name))
203 # For alternate, always use the default enum type automatically generated
204 # as name + 'Kind'
205 disc_type = c_name(name) + 'Kind'
207 for key in members:
208 assert (members[key] in builtin_types.keys()
209 or find_struct(members[key])
210 or find_union(members[key])
211 or find_enum(members[key])), "Invalid alternate member"
213 enum_full_value = c_enum_const(disc_type, key)
214 ret += mcgen('''
215 case %(enum_full_value)s:
216 visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, name, &err);
217 break;
218 ''',
219 enum_full_value = enum_full_value,
220 c_type = type_name(members[key]),
221 c_name = c_name(key))
223 ret += mcgen('''
224 default:
225 abort();
227 out_end:
228 error_propagate(errp, err);
229 err = NULL;
230 visit_end_implicit_struct(m, &err);
231 out:
232 error_propagate(errp, err);
234 ''')
236 return ret
239 def generate_visit_union(expr):
241 name = expr['union']
242 members = expr['data']
244 base = expr.get('base')
245 discriminator = expr.get('discriminator')
247 enum_define = discriminator_find_enum_define(expr)
248 if enum_define:
249 # Use the enum type as discriminator
250 ret = ""
251 disc_type = c_name(enum_define['enum_name'])
252 else:
253 # There will always be a discriminator in the C switch code, by default
254 # it is an enum type generated silently
255 ret = generate_visit_enum(name + 'Kind', members.keys())
256 disc_type = c_name(name) + 'Kind'
258 if base:
259 assert discriminator
260 base_fields = find_struct(base)['data'].copy()
261 del base_fields[discriminator]
262 ret += generate_visit_struct_fields(name, base_fields)
264 if discriminator:
265 for key in members:
266 ret += generate_visit_implicit_struct(members[key])
268 ret += mcgen('''
270 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp)
272 Error *err = NULL;
274 visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), &err);
275 if (err) {
276 goto out;
278 if (*obj) {
279 ''',
280 name=c_name(name))
282 if base:
283 ret += mcgen('''
284 visit_type_%(name)s_fields(m, obj, &err);
285 if (err) {
286 goto out_obj;
288 ''',
289 name=c_name(name))
291 if not discriminator:
292 disc_key = "type"
293 else:
294 disc_key = discriminator
295 ret += mcgen('''
296 visit_type_%(disc_type)s(m, &(*obj)->kind, "%(disc_key)s", &err);
297 if (err) {
298 goto out_obj;
300 if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
301 goto out_obj;
303 switch ((*obj)->kind) {
304 ''',
305 disc_type = disc_type,
306 disc_key = disc_key)
308 for key in members:
309 if not discriminator:
310 fmt = 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
311 else:
312 fmt = 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
314 enum_full_value = c_enum_const(disc_type, key)
315 ret += mcgen('''
316 case %(enum_full_value)s:
317 ''' + fmt + '''
318 break;
319 ''',
320 enum_full_value = enum_full_value,
321 c_type=type_name(members[key]),
322 c_name=c_name(key))
324 ret += mcgen('''
325 default:
326 abort();
328 out_obj:
329 error_propagate(errp, err);
330 err = NULL;
331 visit_end_union(m, !!(*obj)->data, &err);
332 error_propagate(errp, err);
333 err = NULL;
335 visit_end_struct(m, &err);
336 out:
337 error_propagate(errp, err);
339 ''')
341 return ret
343 def generate_declaration(name, members, builtin_type=False):
344 ret = ""
345 if not builtin_type:
346 name = c_name(name)
347 ret += mcgen('''
349 void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **errp);
350 ''',
351 name=name)
353 ret += mcgen('''
354 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
355 ''',
356 name=name)
358 return ret
360 def generate_enum_declaration(name, members):
361 ret = mcgen('''
362 void visit_type_%(name)sList(Visitor *m, %(name)sList **obj, const char *name, Error **errp);
363 ''',
364 name=c_name(name))
366 return ret
368 def generate_decl_enum(name, members):
369 return mcgen('''
371 void visit_type_%(name)s(Visitor *m, %(name)s *obj, const char *name, Error **errp);
372 ''',
373 name=c_name(name))
375 do_builtins = False
377 (input_file, output_dir, do_c, do_h, prefix, opts) = \
378 parse_command_line("b", ["builtins"])
380 for o, a in opts:
381 if o in ("-b", "--builtins"):
382 do_builtins = True
384 c_comment = '''
386 * schema-defined QAPI visitor functions
388 * Copyright IBM, Corp. 2011
390 * Authors:
391 * Anthony Liguori <aliguori@us.ibm.com>
393 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
394 * See the COPYING.LIB file in the top-level directory.
398 h_comment = '''
400 * schema-defined QAPI visitor functions
402 * Copyright IBM, Corp. 2011
404 * Authors:
405 * Anthony Liguori <aliguori@us.ibm.com>
407 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
408 * See the COPYING.LIB file in the top-level directory.
413 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
414 'qapi-visit.c', 'qapi-visit.h',
415 c_comment, h_comment)
417 fdef.write(mcgen('''
418 #include "qemu-common.h"
419 #include "%(prefix)sqapi-visit.h"
420 ''',
421 prefix = prefix))
423 fdecl.write(mcgen('''
424 #include "qapi/visitor.h"
425 #include "%(prefix)sqapi-types.h"
427 ''',
428 prefix=prefix))
430 exprs = parse_schema(input_file)
432 # to avoid header dependency hell, we always generate declarations
433 # for built-in types in our header files and simply guard them
434 fdecl.write(guardstart("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
435 for typename in builtin_types.keys():
436 fdecl.write(generate_declaration(typename, None, builtin_type=True))
437 fdecl.write(guardend("QAPI_VISIT_BUILTIN_VISITOR_DECL"))
439 # ...this doesn't work for cases where we link in multiple objects that
440 # have the functions defined, so we use -b option to provide control
441 # over these cases
442 if do_builtins:
443 for typename in builtin_types.keys():
444 fdef.write(generate_visit_list(typename, None))
446 for expr in exprs:
447 if expr.has_key('struct'):
448 ret = generate_visit_struct(expr)
449 ret += generate_visit_list(expr['struct'], expr['data'])
450 fdef.write(ret)
452 ret = generate_declaration(expr['struct'], expr['data'])
453 fdecl.write(ret)
454 elif expr.has_key('union'):
455 ret = generate_visit_union(expr)
456 ret += generate_visit_list(expr['union'], expr['data'])
457 fdef.write(ret)
459 enum_define = discriminator_find_enum_define(expr)
460 ret = ""
461 if not enum_define:
462 ret = generate_decl_enum('%sKind' % expr['union'],
463 expr['data'].keys())
464 ret += generate_declaration(expr['union'], expr['data'])
465 fdecl.write(ret)
466 elif expr.has_key('alternate'):
467 ret = generate_visit_alternate(expr['alternate'], expr['data'])
468 ret += generate_visit_list(expr['alternate'], expr['data'])
469 fdef.write(ret)
471 ret = generate_decl_enum('%sKind' % expr['alternate'],
472 expr['data'].keys())
473 ret += generate_declaration(expr['alternate'], expr['data'])
474 fdecl.write(ret)
475 elif expr.has_key('enum'):
476 ret = generate_visit_list(expr['enum'], expr['data'])
477 ret += generate_visit_enum(expr['enum'], expr['data'])
478 fdef.write(ret)
480 ret = generate_decl_enum(expr['enum'], expr['data'])
481 ret += generate_enum_declaration(expr['enum'], expr['data'])
482 fdecl.write(ret)
484 close_output(fdef, fdecl)