target-arm: Handle always condition codes within arm_test_cc
[qemu/ar7.git] / scripts / qapi-commands.py
blob890ce5db9243a77f3f41ac44ae82f1a01dd2cd32
2 # QAPI command marshaller 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 def generate_command_decl(name, args, ret_type):
20 arglist=""
21 for argname, argtype, optional in parse_args(args):
22 argtype = c_type(argtype, is_param=True)
23 if optional:
24 arglist += "bool has_%s, " % c_name(argname)
25 arglist += "%s %s, " % (argtype, c_name(argname))
26 return mcgen('''
27 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
28 ''',
29 ret_type=c_type(ret_type), name=c_name(name),
30 args=arglist)
32 def gen_err_check(err):
33 if not err:
34 return ''
35 return mcgen('''
36 if (%(err)s) {
37 goto out;
39 ''',
40 err=err)
42 def gen_sync_call(name, args, ret_type):
43 ret = ""
44 arglist=""
45 retval=""
46 if ret_type:
47 retval = "retval = "
48 for argname, argtype, optional in parse_args(args):
49 if optional:
50 arglist += "has_%s, " % c_name(argname)
51 arglist += "%s, " % (c_name(argname))
52 push_indent()
53 ret = mcgen('''
54 %(retval)sqmp_%(name)s(%(args)s&local_err);
55 ''',
56 name=c_name(name), args=arglist, retval=retval)
57 if ret_type:
58 ret += gen_err_check('local_err')
59 ret += mcgen('''
61 qmp_marshal_output_%(c_name)s(retval, ret, &local_err);
62 ''',
63 c_name=c_name(name))
64 pop_indent()
65 return ret
67 def gen_visitor_input_containers_decl(args):
68 ret = ""
70 push_indent()
71 if len(args) > 0:
72 ret += mcgen('''
73 QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
74 QapiDeallocVisitor *md;
75 Visitor *v;
76 ''')
77 pop_indent()
79 return ret
81 def gen_visitor_input_vars_decl(args):
82 ret = ""
83 push_indent()
84 for argname, argtype, optional in parse_args(args):
85 if optional:
86 ret += mcgen('''
87 bool has_%(argname)s = false;
88 ''',
89 argname=c_name(argname))
90 if is_c_ptr(argtype):
91 ret += mcgen('''
92 %(argtype)s %(argname)s = NULL;
93 ''',
94 argname=c_name(argname), argtype=c_type(argtype))
95 else:
96 ret += mcgen('''
97 %(argtype)s %(argname)s = {0};
98 ''',
99 argname=c_name(argname), argtype=c_type(argtype))
101 pop_indent()
102 return ret
104 def gen_visitor_input_block(args, dealloc=False):
105 ret = ""
106 errparg = '&local_err'
107 errarg = 'local_err'
109 if len(args) == 0:
110 return ret
112 push_indent()
114 if dealloc:
115 errparg = 'NULL'
116 errarg = None;
117 ret += mcgen('''
118 qmp_input_visitor_cleanup(mi);
119 md = qapi_dealloc_visitor_new();
120 v = qapi_dealloc_get_visitor(md);
121 ''')
122 else:
123 ret += mcgen('''
124 v = qmp_input_get_visitor(mi);
125 ''')
127 for argname, argtype, optional in parse_args(args):
128 if optional:
129 ret += mcgen('''
130 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
131 ''',
132 c_name=c_name(argname), name=argname, errp=errparg)
133 ret += gen_err_check(errarg)
134 ret += mcgen('''
135 if (has_%(c_name)s) {
136 ''',
137 c_name=c_name(argname))
138 push_indent()
139 ret += mcgen('''
140 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
141 ''',
142 c_name=c_name(argname), name=argname, argtype=argtype,
143 visitor=type_name(argtype), errp=errparg)
144 ret += gen_err_check(errarg)
145 if optional:
146 pop_indent()
147 ret += mcgen('''
149 ''')
151 if dealloc:
152 ret += mcgen('''
153 qapi_dealloc_visitor_cleanup(md);
154 ''')
155 pop_indent()
156 return ret
158 def gen_marshal_output(name, ret_type):
159 if not ret_type:
160 return ""
162 ret = mcgen('''
163 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
165 Error *local_err = NULL;
166 QmpOutputVisitor *mo = qmp_output_visitor_new();
167 QapiDeallocVisitor *md;
168 Visitor *v;
170 v = qmp_output_get_visitor(mo);
171 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
172 if (local_err) {
173 goto out;
175 *ret_out = qmp_output_get_qobject(mo);
177 out:
178 error_propagate(errp, local_err);
179 qmp_output_visitor_cleanup(mo);
180 md = qapi_dealloc_visitor_new();
181 v = qapi_dealloc_get_visitor(md);
182 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
183 qapi_dealloc_visitor_cleanup(md);
185 ''',
186 c_ret_type=c_type(ret_type), c_name=c_name(name),
187 visitor=type_name(ret_type))
189 return ret
191 def gen_marshal_input_decl(name, middle_mode):
192 ret = 'void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
193 if not middle_mode:
194 ret = "static " + ret
195 return ret
197 def gen_marshal_input(name, args, ret_type, middle_mode):
198 hdr = gen_marshal_input_decl(name, middle_mode)
200 ret = mcgen('''
201 %(header)s
203 Error *local_err = NULL;
204 ''',
205 header=hdr)
207 if ret_type:
208 ret += mcgen('''
209 %(c_type)s retval;
210 ''',
211 c_type=c_type(ret_type))
213 if len(args) > 0:
214 ret += gen_visitor_input_containers_decl(args)
215 ret += gen_visitor_input_vars_decl(args) + '\n'
216 ret += gen_visitor_input_block(args) + '\n'
217 else:
218 ret += mcgen('''
220 (void)args;
222 ''')
224 ret += gen_sync_call(name, args, ret_type)
226 if re.search('^ *goto out\\;', ret, re.MULTILINE):
227 ret += mcgen('''
229 out:
230 ''')
231 ret += mcgen('''
232 error_propagate(errp, local_err);
233 ''')
234 ret += gen_visitor_input_block(args, dealloc=True)
235 ret += mcgen('''
237 ''')
238 return ret
240 def gen_registry(commands):
241 registry=""
242 push_indent()
243 for cmd in commands:
244 options = 'QCO_NO_OPTIONS'
245 if not cmd.get('success-response', True):
246 options = 'QCO_NO_SUCCESS_RESP'
248 registry += mcgen('''
249 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
250 ''',
251 name=cmd['command'], c_name=c_name(cmd['command']),
252 opts=options)
253 pop_indent()
254 ret = mcgen('''
255 static void qmp_init_marshal(void)
257 ''')
258 ret += registry
259 ret += mcgen('''
262 qapi_init(qmp_init_marshal);
263 ''')
264 return ret
266 middle_mode = False
268 (input_file, output_dir, do_c, do_h, prefix, opts) = \
269 parse_command_line("m", ["middle"])
271 for o, a in opts:
272 if o in ("-m", "--middle"):
273 middle_mode = True
275 exprs = parse_schema(input_file)
276 commands = filter(lambda expr: expr.has_key('command'), exprs)
277 commands = filter(lambda expr: not expr.has_key('gen'), commands)
279 c_comment = '''
281 * schema-defined QMP->QAPI command dispatch
283 * Copyright IBM, Corp. 2011
285 * Authors:
286 * Anthony Liguori <aliguori@us.ibm.com>
288 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
289 * See the COPYING.LIB file in the top-level directory.
293 h_comment = '''
295 * schema-defined QAPI function prototypes
297 * Copyright IBM, Corp. 2011
299 * Authors:
300 * Anthony Liguori <aliguori@us.ibm.com>
302 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
303 * See the COPYING.LIB file in the top-level directory.
308 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
309 'qmp-marshal.c', 'qmp-commands.h',
310 c_comment, h_comment)
312 fdef.write(mcgen('''
313 #include "qemu-common.h"
314 #include "qemu/module.h"
315 #include "qapi/qmp/types.h"
316 #include "qapi/qmp/dispatch.h"
317 #include "qapi/visitor.h"
318 #include "qapi/qmp-output-visitor.h"
319 #include "qapi/qmp-input-visitor.h"
320 #include "qapi/dealloc-visitor.h"
321 #include "%(prefix)sqapi-types.h"
322 #include "%(prefix)sqapi-visit.h"
323 #include "%(prefix)sqmp-commands.h"
325 ''',
326 prefix=prefix))
328 fdecl.write(mcgen('''
329 #include "%(prefix)sqapi-types.h"
330 #include "qapi/qmp/qdict.h"
331 #include "qapi/error.h"
333 ''',
334 prefix=prefix))
336 for cmd in commands:
337 arglist = []
338 ret_type = None
339 if cmd.has_key('data'):
340 arglist = cmd['data']
341 if cmd.has_key('returns'):
342 ret_type = cmd['returns']
343 ret = generate_command_decl(cmd['command'], arglist, ret_type)
344 fdecl.write(ret)
345 if ret_type:
346 ret = gen_marshal_output(cmd['command'], ret_type) + "\n"
347 fdef.write(ret)
349 if middle_mode:
350 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], middle_mode))
352 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
353 fdef.write(ret)
355 if not middle_mode:
356 ret = gen_registry(commands)
357 fdef.write(ret)
359 close_output(fdef, fdecl)