ditto hw/xilinx_uartlite.c
[qemu/aliguori.git] / scripts / qapi-commands.py
blobbf617401ad2651b0ed1079100558eb1691b94973
2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
6 # Authors:
7 # Anthony Liguori <aliguori@us.ibm.com>
8 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # This work is licensed under the terms of the GNU GPLv2.
11 # See the COPYING.LIB file in the top-level directory.
13 from ordereddict import OrderedDict
14 from qapi import *
15 import sys
16 import os
17 import getopt
18 import errno
20 def generate_decl_enum(name, members, genlist=True):
21 return mcgen('''
23 void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
24 ''',
25 name=name)
27 def generate_command_decl(name, args, ret_type):
28 arglist=""
29 for argname, argtype, optional, structured in parse_args(args):
30 argtype = c_type(argtype)
31 if argtype == "char *":
32 argtype = "const char *"
33 if optional:
34 arglist += "bool has_%s, " % c_var(argname)
35 arglist += "%s %s, " % (argtype, c_var(argname))
36 return mcgen('''
37 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
38 ''',
39 ret_type=c_type(ret_type), name=c_var(name), args=arglist).strip()
41 def gen_sync_call(name, args, ret_type, indent=0):
42 ret = ""
43 arglist=""
44 retval=""
45 if ret_type:
46 retval = "retval = "
47 for argname, argtype, optional, structured in parse_args(args):
48 if optional:
49 arglist += "has_%s, " % c_var(argname)
50 arglist += "%s, " % (c_var(argname))
51 push_indent(indent)
52 ret = mcgen('''
53 %(retval)sqmp_%(name)s(%(args)serrp);
55 ''',
56 name=c_var(name), args=arglist, retval=retval).rstrip()
57 if ret_type:
58 ret += "\n" + mcgen(''''
59 %(marshal_output_call)s
60 ''',
61 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
62 pop_indent(indent)
63 return ret.rstrip()
66 def gen_marshal_output_call(name, ret_type):
67 if not ret_type:
68 return ""
69 return "qmp_marshal_output_%s(retval, ret, errp);" % c_var(name)
71 def gen_visitor_output_containers_decl(ret_type):
72 ret = ""
73 push_indent()
74 if ret_type:
75 ret += mcgen('''
76 QmpOutputVisitor *mo;
77 QapiDeallocVisitor *md;
78 Visitor *v;
79 ''')
80 pop_indent()
82 return ret
84 def gen_visitor_input_containers_decl(args):
85 ret = ""
87 push_indent()
88 if len(args) > 0:
89 ret += mcgen('''
90 QmpInputVisitor *mi;
91 QapiDeallocVisitor *md;
92 Visitor *v;
93 ''')
94 pop_indent()
96 return ret.rstrip()
98 def gen_visitor_input_vars_decl(args):
99 ret = ""
100 push_indent()
101 for argname, argtype, optional, structured in parse_args(args):
102 if optional:
103 ret += mcgen('''
104 bool has_%(argname)s = false;
105 ''',
106 argname=c_var(argname))
107 if c_type(argtype).endswith("*"):
108 ret += mcgen('''
109 %(argtype)s %(argname)s = NULL;
110 ''',
111 argname=c_var(argname), argtype=c_type(argtype))
112 else:
113 ret += mcgen('''
114 %(argtype)s %(argname)s;
115 ''',
116 argname=c_var(argname), argtype=c_type(argtype))
118 pop_indent()
119 return ret.rstrip()
121 def gen_visitor_input_block(args, obj, dealloc=False):
122 ret = ""
123 if len(args) == 0:
124 return ret
126 push_indent()
128 if dealloc:
129 ret += mcgen('''
130 md = qapi_dealloc_visitor_new();
131 v = qapi_dealloc_get_visitor(md);
132 ''')
133 else:
134 ret += mcgen('''
135 mi = qmp_input_visitor_new(%(obj)s);
136 v = qmp_input_get_visitor(mi);
137 ''',
138 obj=obj)
140 for argname, argtype, optional, structured in parse_args(args):
141 if optional:
142 ret += mcgen('''
143 visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp);
144 if (has_%(c_name)s) {
145 ''',
146 c_name=c_var(argname), name=argname)
147 push_indent()
148 ret += mcgen('''
149 visit_type_%(argtype)s(v, &%(c_name)s, "%(name)s", errp);
150 ''',
151 c_name=c_var(argname), name=argname, argtype=argtype)
152 if optional:
153 pop_indent()
154 ret += mcgen('''
156 visit_end_optional(v, errp);
157 ''')
159 if dealloc:
160 ret += mcgen('''
161 qapi_dealloc_visitor_cleanup(md);
162 ''')
163 else:
164 ret += mcgen('''
165 qmp_input_visitor_cleanup(mi);
166 ''')
167 pop_indent()
168 return ret.rstrip()
170 def gen_marshal_output(name, args, ret_type):
171 if not ret_type:
172 return ""
173 ret = mcgen('''
174 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
176 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
177 QmpOutputVisitor *mo = qmp_output_visitor_new();
178 Visitor *v;
180 v = qmp_output_get_visitor(mo);
181 visit_type_%(ret_type)s(v, &ret_in, "unused", errp);
182 if (!error_is_set(errp)) {
183 *ret_out = qmp_output_get_qobject(mo);
185 qmp_output_visitor_cleanup(mo);
186 v = qapi_dealloc_get_visitor(md);
187 visit_type_%(ret_type)s(v, &ret_in, "unused", errp);
188 qapi_dealloc_visitor_cleanup(md);
190 ''',
191 c_ret_type=c_type(ret_type), c_name=c_var(name), ret_type=ret_type)
193 return ret
195 def gen_marshal_input(name, args, ret_type):
196 ret = mcgen('''
197 static void qmp_marshal_input_%(c_name)s(QDict *args, QObject **ret, Error **errp)
199 ''',
200 c_name=c_var(name))
202 if ret_type:
203 if c_type(ret_type).endswith("*"):
204 retval = " %s retval = NULL;" % c_type(ret_type)
205 else:
206 retval = " %s retval;" % c_type(ret_type)
207 ret += mcgen('''
208 %(retval)s
209 ''',
210 retval=retval)
212 if len(args) > 0:
213 ret += mcgen('''
214 %(visitor_input_containers_decl)s
215 %(visitor_input_vars_decl)s
217 %(visitor_input_block)s
219 ''',
220 visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
221 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
222 visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
224 ret += mcgen('''
225 if (error_is_set(errp)) {
226 goto out;
228 %(sync_call)s
229 ''',
230 sync_call=gen_sync_call(name, args, ret_type, indent=4))
231 ret += mcgen('''
233 out:
234 ''')
235 ret += mcgen('''
236 %(visitor_input_block_cleanup)s
237 return;
239 ''',
240 visitor_input_block_cleanup=gen_visitor_input_block(args, None, dealloc=True))
241 return ret
243 def gen_registry(commands):
244 registry=""
245 push_indent()
246 for cmd in commands:
247 registry += mcgen('''
248 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s);
249 ''',
250 name=cmd['command'], c_name=c_var(cmd['command']))
251 pop_indent()
252 ret = mcgen('''
253 static void qmp_init_marshal(void)
255 %(registry)s
258 qapi_init(qmp_init_marshal);
259 ''',
260 registry=registry.rstrip())
261 return ret
263 def gen_command_decl_prologue(header, guard, prefix=""):
264 ret = mcgen('''
265 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
268 * schema-defined QAPI function prototypes
270 * Copyright IBM, Corp. 2011
272 * Authors:
273 * Anthony Liguori <aliguori@us.ibm.com>
275 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
276 * See the COPYING.LIB file in the top-level directory.
280 #ifndef %(guard)s
281 #define %(guard)s
283 #include "%(prefix)sqapi-types.h"
284 #include "error.h"
286 ''',
287 header=basename(h_file), guard=guardname(h_file), prefix=prefix)
288 return ret
290 def gen_command_def_prologue(prefix="", proxy=False):
291 ret = mcgen('''
292 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
295 * schema-defined QMP->QAPI command dispatch
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.
307 #include "qemu-objects.h"
308 #include "qapi/qmp-core.h"
309 #include "qapi/qapi-visit-core.h"
310 #include "qapi/qmp-output-visitor.h"
311 #include "qapi/qmp-input-visitor.h"
312 #include "qapi/qapi-dealloc-visitor.h"
313 #include "%(prefix)sqapi-types.h"
314 #include "%(prefix)sqapi-visit.h"
316 ''',
317 prefix=prefix)
318 if not proxy:
319 ret += '#include "%sqmp-commands.h"' % prefix
320 return ret + "\n"
323 try:
324 opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:", ["prefix=", "output-dir=", "type="])
325 except getopt.GetoptError, err:
326 print str(err)
327 sys.exit(1)
329 output_dir = ""
330 prefix = ""
331 dispatch_type = "sync"
332 c_file = 'qmp-marshal.c'
333 h_file = 'qmp-commands.h'
335 for o, a in opts:
336 if o in ("-p", "--prefix"):
337 prefix = a
338 elif o in ("-o", "--output-dir"):
339 output_dir = a + "/"
340 elif o in ("-t", "--type"):
341 dispatch_type = a
343 c_file = output_dir + prefix + c_file
344 h_file = output_dir + prefix + h_file
346 try:
347 os.makedirs(output_dir)
348 except os.error, e:
349 if e.errno != errno.EEXIST:
350 raise
352 exprs = parse_schema(sys.stdin)
353 commands = filter(lambda expr: expr.has_key('command'), exprs)
355 if dispatch_type == "sync":
356 fdecl = open(h_file, 'w')
357 fdef = open(c_file, 'w')
358 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
359 fdecl.write(ret)
360 ret = gen_command_def_prologue(prefix=prefix)
361 fdef.write(ret)
363 for cmd in commands:
364 arglist = []
365 ret_type = None
366 if cmd.has_key('data'):
367 arglist = cmd['data']
368 if cmd.has_key('returns'):
369 ret_type = cmd['returns']
370 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
371 fdecl.write(ret)
372 if ret_type:
373 ret = gen_marshal_output(cmd['command'], arglist, ret_type) + "\n"
374 fdef.write(ret)
375 ret = gen_marshal_input(cmd['command'], arglist, ret_type) + "\n"
376 fdef.write(ret)
378 fdecl.write("\n#endif\n");
379 ret = gen_registry(commands)
380 fdef.write(ret)
382 fdef.flush()
383 fdef.close()
384 fdecl.flush()
385 fdecl.close()