blob: 9734ab0a53a39ec868d629ab1ca79567d32f4601 [file] [log] [blame]
Michael Rothc17d9902011-07-19 14:50:42 -05001#
2# QAPI command marshaller generator
3#
4# Copyright IBM, Corp. 2011
5#
6# Authors:
7# Anthony Liguori <aliguori@us.ibm.com>
8# Michael Roth <mdroth@linux.vnet.ibm.com>
9#
Markus Armbruster678e48a2014-03-01 08:40:34 +010010# This work is licensed under the terms of the GNU GPL, version 2.
11# See the COPYING file in the top-level directory.
Michael Rothc17d9902011-07-19 14:50:42 -050012
13from ordereddict import OrderedDict
14from qapi import *
15import sys
16import os
17import getopt
18import errno
19
Anthony Liguori15e43e62011-09-14 14:30:00 -050020def type_visitor(name):
21 if type(name) == list:
22 return 'visit_type_%sList' % name[0]
23 else:
24 return 'visit_type_%s' % name
25
Michael Rothc17d9902011-07-19 14:50:42 -050026def generate_command_decl(name, args, ret_type):
27 arglist=""
28 for argname, argtype, optional, structured in parse_args(args):
29 argtype = c_type(argtype)
30 if argtype == "char *":
31 argtype = "const char *"
32 if optional:
33 arglist += "bool has_%s, " % c_var(argname)
34 arglist += "%s %s, " % (argtype, c_var(argname))
35 return mcgen('''
36%(ret_type)s qmp_%(name)s(%(args)sError **errp);
37''',
Federico Simoncellic9da2282012-03-20 13:54:35 +000038 ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
Michael Rothc17d9902011-07-19 14:50:42 -050039
40def gen_sync_call(name, args, ret_type, indent=0):
41 ret = ""
42 arglist=""
43 retval=""
44 if ret_type:
45 retval = "retval = "
46 for argname, argtype, optional, structured in parse_args(args):
47 if optional:
48 arglist += "has_%s, " % c_var(argname)
49 arglist += "%s, " % (c_var(argname))
50 push_indent(indent)
51 ret = mcgen('''
52%(retval)sqmp_%(name)s(%(args)serrp);
53
54''',
Federico Simoncellic9da2282012-03-20 13:54:35 +000055 name=c_fun(name), args=arglist, retval=retval).rstrip()
Michael Rothc17d9902011-07-19 14:50:42 -050056 if ret_type:
57 ret += "\n" + mcgen(''''
Luiz Capitulino694a0992011-10-19 14:51:14 -020058if (!error_is_set(errp)) {
59 %(marshal_output_call)s
60}
Michael Rothc17d9902011-07-19 14:50:42 -050061''',
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
63 pop_indent(indent)
64 return ret.rstrip()
65
66
67def gen_marshal_output_call(name, ret_type):
68 if not ret_type:
69 return ""
Federico Simoncellic9da2282012-03-20 13:54:35 +000070 return "qmp_marshal_output_%s(retval, ret, errp);" % c_fun(name)
Michael Rothc17d9902011-07-19 14:50:42 -050071
Michael Rothc17d9902011-07-19 14:50:42 -050072def gen_visitor_input_containers_decl(args):
73 ret = ""
74
75 push_indent()
76 if len(args) > 0:
77 ret += mcgen('''
78QmpInputVisitor *mi;
79QapiDeallocVisitor *md;
80Visitor *v;
81''')
82 pop_indent()
83
84 return ret.rstrip()
85
86def gen_visitor_input_vars_decl(args):
87 ret = ""
88 push_indent()
89 for argname, argtype, optional, structured in parse_args(args):
90 if optional:
91 ret += mcgen('''
92bool has_%(argname)s = false;
93''',
94 argname=c_var(argname))
95 if c_type(argtype).endswith("*"):
96 ret += mcgen('''
97%(argtype)s %(argname)s = NULL;
98''',
99 argname=c_var(argname), argtype=c_type(argtype))
100 else:
101 ret += mcgen('''
102%(argtype)s %(argname)s;
103''',
104 argname=c_var(argname), argtype=c_type(argtype))
105
106 pop_indent()
107 return ret.rstrip()
108
109def gen_visitor_input_block(args, obj, dealloc=False):
110 ret = ""
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400111 errparg = 'errp'
112
Michael Rothc17d9902011-07-19 14:50:42 -0500113 if len(args) == 0:
114 return ret
115
116 push_indent()
117
118 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400119 errparg = 'NULL'
Michael Rothc17d9902011-07-19 14:50:42 -0500120 ret += mcgen('''
121md = qapi_dealloc_visitor_new();
122v = qapi_dealloc_get_visitor(md);
123''')
124 else:
125 ret += mcgen('''
Paolo Bonzini6d36d7d2012-03-22 12:51:12 +0100126mi = qmp_input_visitor_new_strict(%(obj)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500127v = qmp_input_get_visitor(mi);
128''',
129 obj=obj)
130
131 for argname, argtype, optional, structured in parse_args(args):
132 if optional:
133 ret += mcgen('''
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400134visit_start_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500135if (has_%(c_name)s) {
136''',
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400137 c_name=c_var(argname), name=argname, errp=errparg)
Michael Rothc17d9902011-07-19 14:50:42 -0500138 push_indent()
139 ret += mcgen('''
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400140%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500141''',
Anthony Liguori15e43e62011-09-14 14:30:00 -0500142 c_name=c_var(argname), name=argname, argtype=argtype,
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400143 visitor=type_visitor(argtype), errp=errparg)
Michael Rothc17d9902011-07-19 14:50:42 -0500144 if optional:
145 pop_indent()
146 ret += mcgen('''
147}
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400148visit_end_optional(v, %(errp)s);
149''', errp=errparg)
Michael Rothc17d9902011-07-19 14:50:42 -0500150
151 if dealloc:
152 ret += mcgen('''
153qapi_dealloc_visitor_cleanup(md);
154''')
155 else:
156 ret += mcgen('''
157qmp_input_visitor_cleanup(mi);
158''')
159 pop_indent()
160 return ret.rstrip()
161
Anthony Liguori776574d2011-09-02 12:34:46 -0500162def gen_marshal_output(name, args, ret_type, middle_mode):
Michael Rothc17d9902011-07-19 14:50:42 -0500163 if not ret_type:
164 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500165
Michael Rothc17d9902011-07-19 14:50:42 -0500166 ret = mcgen('''
167static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
168{
169 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
170 QmpOutputVisitor *mo = qmp_output_visitor_new();
171 Visitor *v;
172
173 v = qmp_output_get_visitor(mo);
Anthony Liguori15e43e62011-09-14 14:30:00 -0500174 %(visitor)s(v, &ret_in, "unused", errp);
Michael Rothc17d9902011-07-19 14:50:42 -0500175 if (!error_is_set(errp)) {
176 *ret_out = qmp_output_get_qobject(mo);
177 }
178 qmp_output_visitor_cleanup(mo);
179 v = qapi_dealloc_get_visitor(md);
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400180 %(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500181 qapi_dealloc_visitor_cleanup(md);
182}
183''',
Federico Simoncellic9da2282012-03-20 13:54:35 +0000184 c_ret_type=c_type(ret_type), c_name=c_fun(name),
Anthony Liguori15e43e62011-09-14 14:30:00 -0500185 visitor=type_visitor(ret_type))
Michael Rothc17d9902011-07-19 14:50:42 -0500186
187 return ret
188
Anthony Liguori776574d2011-09-02 12:34:46 -0500189def gen_marshal_input_decl(name, args, ret_type, middle_mode):
190 if middle_mode:
Federico Simoncellic9da2282012-03-20 13:54:35 +0000191 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500192 else:
Federico Simoncellic9da2282012-03-20 13:54:35 +0000193 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500194
195
196
197def gen_marshal_input(name, args, ret_type, middle_mode):
198 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
199
Michael Rothc17d9902011-07-19 14:50:42 -0500200 ret = mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500201%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500202{
203''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500204 header=hdr)
205
206 if middle_mode:
207 ret += mcgen('''
208 Error *local_err = NULL;
209 Error **errp = &local_err;
210 QDict *args = (QDict *)qdict;
211''')
Michael Rothc17d9902011-07-19 14:50:42 -0500212
213 if ret_type:
214 if c_type(ret_type).endswith("*"):
215 retval = " %s retval = NULL;" % c_type(ret_type)
216 else:
217 retval = " %s retval;" % c_type(ret_type)
218 ret += mcgen('''
219%(retval)s
220''',
221 retval=retval)
222
223 if len(args) > 0:
224 ret += mcgen('''
225%(visitor_input_containers_decl)s
226%(visitor_input_vars_decl)s
227
228%(visitor_input_block)s
229
230''',
231 visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
232 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
233 visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
Anthony Liguori776574d2011-09-02 12:34:46 -0500234 else:
235 ret += mcgen('''
236 (void)args;
237''')
Michael Rothc17d9902011-07-19 14:50:42 -0500238
239 ret += mcgen('''
240 if (error_is_set(errp)) {
241 goto out;
242 }
243%(sync_call)s
244''',
245 sync_call=gen_sync_call(name, args, ret_type, indent=4))
246 ret += mcgen('''
247
248out:
249''')
250 ret += mcgen('''
251%(visitor_input_block_cleanup)s
Michael Rothc17d9902011-07-19 14:50:42 -0500252''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500253 visitor_input_block_cleanup=gen_visitor_input_block(args, None,
254 dealloc=True))
255
256 if middle_mode:
257 ret += mcgen('''
258
259 if (local_err) {
260 qerror_report_err(local_err);
261 error_free(local_err);
262 return -1;
263 }
264 return 0;
265''')
266 else:
267 ret += mcgen('''
268 return;
269''')
270
271 ret += mcgen('''
272}
273''')
274
Michael Rothc17d9902011-07-19 14:50:42 -0500275 return ret
276
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300277def option_value_matches(opt, val, cmd):
278 if opt in cmd and cmd[opt] == val:
279 return True
280 return False
281
Michael Rothc17d9902011-07-19 14:50:42 -0500282def gen_registry(commands):
283 registry=""
284 push_indent()
285 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300286 options = 'QCO_NO_OPTIONS'
287 if option_value_matches('success-response', 'no', cmd):
288 options = 'QCO_NO_SUCCESS_RESP'
289
Michael Rothc17d9902011-07-19 14:50:42 -0500290 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300291qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500292''',
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300293 name=cmd['command'], c_name=c_fun(cmd['command']),
294 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500295 pop_indent()
296 ret = mcgen('''
297static void qmp_init_marshal(void)
298{
299%(registry)s
300}
301
302qapi_init(qmp_init_marshal);
303''',
304 registry=registry.rstrip())
305 return ret
306
307def gen_command_decl_prologue(header, guard, prefix=""):
308 ret = mcgen('''
309/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
310
311/*
312 * schema-defined QAPI function prototypes
313 *
314 * Copyright IBM, Corp. 2011
315 *
316 * Authors:
317 * Anthony Liguori <aliguori@us.ibm.com>
318 *
319 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
320 * See the COPYING.LIB file in the top-level directory.
321 *
322 */
323
324#ifndef %(guard)s
325#define %(guard)s
326
327#include "%(prefix)sqapi-types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100328#include "qapi/qmp/qdict.h"
329#include "qapi/error.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500330
331''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500332 header=basename(header), guard=guardname(header), prefix=prefix)
Michael Rothc17d9902011-07-19 14:50:42 -0500333 return ret
334
335def gen_command_def_prologue(prefix="", proxy=False):
336 ret = mcgen('''
337/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
338
339/*
340 * schema-defined QMP->QAPI command dispatch
341 *
342 * Copyright IBM, Corp. 2011
343 *
344 * Authors:
345 * Anthony Liguori <aliguori@us.ibm.com>
346 *
347 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
348 * See the COPYING.LIB file in the top-level directory.
349 *
350 */
351
Paolo Bonzini79ee7df2012-12-06 11:22:34 +0100352#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100353#include "qemu/module.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100354#include "qapi/qmp/qerror.h"
355#include "qapi/qmp/types.h"
356#include "qapi/qmp/dispatch.h"
357#include "qapi/visitor.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500358#include "qapi/qmp-output-visitor.h"
359#include "qapi/qmp-input-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100360#include "qapi/dealloc-visitor.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500361#include "%(prefix)sqapi-types.h"
362#include "%(prefix)sqapi-visit.h"
363
364''',
365 prefix=prefix)
366 if not proxy:
367 ret += '#include "%sqmp-commands.h"' % prefix
Anthony Liguori776574d2011-09-02 12:34:46 -0500368 return ret + "\n\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500369
370
371try:
Avi Kivity8d3bc512011-12-27 16:02:16 +0200372 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:m",
373 ["source", "header", "prefix=",
374 "output-dir=", "type=", "middle"])
Michael Rothc17d9902011-07-19 14:50:42 -0500375except getopt.GetoptError, err:
376 print str(err)
377 sys.exit(1)
378
379output_dir = ""
380prefix = ""
381dispatch_type = "sync"
382c_file = 'qmp-marshal.c'
383h_file = 'qmp-commands.h'
Anthony Liguori776574d2011-09-02 12:34:46 -0500384middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500385
Avi Kivity8d3bc512011-12-27 16:02:16 +0200386do_c = False
387do_h = False
388
Michael Rothc17d9902011-07-19 14:50:42 -0500389for o, a in opts:
390 if o in ("-p", "--prefix"):
391 prefix = a
392 elif o in ("-o", "--output-dir"):
393 output_dir = a + "/"
394 elif o in ("-t", "--type"):
395 dispatch_type = a
Anthony Liguori776574d2011-09-02 12:34:46 -0500396 elif o in ("-m", "--middle"):
397 middle_mode = True
Avi Kivity8d3bc512011-12-27 16:02:16 +0200398 elif o in ("-c", "--source"):
Avi Kivity8d3bc512011-12-27 16:02:16 +0200399 do_c = True
Avi Kivity19bf7c82011-12-28 12:26:58 +0200400 elif o in ("-h", "--header"):
401 do_h = True
Avi Kivity8d3bc512011-12-27 16:02:16 +0200402
403if not do_c and not do_h:
404 do_c = True
405 do_h = True
Michael Rothc17d9902011-07-19 14:50:42 -0500406
407c_file = output_dir + prefix + c_file
408h_file = output_dir + prefix + h_file
409
Avi Kivity8d3bc512011-12-27 16:02:16 +0200410def maybe_open(really, name, opt):
Avi Kivity8d3bc512011-12-27 16:02:16 +0200411 if really:
412 return open(name, opt)
413 else:
Avi Kivity19bf7c82011-12-28 12:26:58 +0200414 import StringIO
415 return StringIO.StringIO()
Avi Kivity8d3bc512011-12-27 16:02:16 +0200416
Michael Rothc17d9902011-07-19 14:50:42 -0500417try:
418 os.makedirs(output_dir)
419except os.error, e:
420 if e.errno != errno.EEXIST:
421 raise
422
423exprs = parse_schema(sys.stdin)
424commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600425commands = filter(lambda expr: not expr.has_key('gen'), commands)
Michael Rothc17d9902011-07-19 14:50:42 -0500426
427if dispatch_type == "sync":
Avi Kivity8d3bc512011-12-27 16:02:16 +0200428 fdecl = maybe_open(do_h, h_file, 'w')
429 fdef = maybe_open(do_c, c_file, 'w')
Michael Rothc17d9902011-07-19 14:50:42 -0500430 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
431 fdecl.write(ret)
432 ret = gen_command_def_prologue(prefix=prefix)
433 fdef.write(ret)
434
435 for cmd in commands:
436 arglist = []
437 ret_type = None
438 if cmd.has_key('data'):
439 arglist = cmd['data']
440 if cmd.has_key('returns'):
441 ret_type = cmd['returns']
442 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
443 fdecl.write(ret)
444 if ret_type:
Anthony Liguori776574d2011-09-02 12:34:46 -0500445 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500446 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500447
448 if middle_mode:
449 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
450
451 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500452 fdef.write(ret)
453
Michael Roth7534ba02011-08-10 13:10:51 -0500454 fdecl.write("\n#endif\n");
Anthony Liguori776574d2011-09-02 12:34:46 -0500455
456 if not middle_mode:
457 ret = gen_registry(commands)
458 fdef.write(ret)
Michael Rothc17d9902011-07-19 14:50:42 -0500459
460 fdef.flush()
461 fdef.close()
462 fdecl.flush()
463 fdecl.close()