]> Git Repo - qemu.git/blob - scripts/qapi-commands.py
vl.c: extend -m option to support options for memory hotplug
[qemu.git] / scripts / qapi-commands.py
1 #
2 # QAPI command marshaller generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014 Red Hat, Inc.
6 #
7 # Authors:
8 #  Anthony Liguori <[email protected]>
9 #  Michael Roth    <[email protected]>
10 #  Markus Armbruster <[email protected]>
11 #
12 # This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
14
15 from ordereddict import OrderedDict
16 from qapi import *
17 import re
18 import sys
19 import os
20 import getopt
21 import errno
22
23 def type_visitor(name):
24     if type(name) == list:
25         return 'visit_type_%sList' % name[0]
26     else:
27         return 'visit_type_%s' % name
28
29 def generate_command_decl(name, args, ret_type):
30     arglist=""
31     for argname, argtype, optional, structured in parse_args(args):
32         argtype = c_type(argtype)
33         if argtype == "char *":
34             argtype = "const char *"
35         if optional:
36             arglist += "bool has_%s, " % c_var(argname)
37         arglist += "%s %s, " % (argtype, c_var(argname))
38     return mcgen('''
39 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
40 ''',
41                  ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
42
43 def gen_err_check(errvar):
44     if errvar:
45         return mcgen('''
46 if (local_err) {
47     goto out;
48 }
49 ''')
50     return ''
51
52 def gen_sync_call(name, args, ret_type, indent=0):
53     ret = ""
54     arglist=""
55     retval=""
56     if ret_type:
57         retval = "retval = "
58     for argname, argtype, optional, structured in parse_args(args):
59         if optional:
60             arglist += "has_%s, " % c_var(argname)
61         arglist += "%s, " % (c_var(argname))
62     push_indent(indent)
63     ret = mcgen('''
64 %(retval)sqmp_%(name)s(%(args)s&local_err);
65
66 ''',
67                 name=c_fun(name), args=arglist, retval=retval).rstrip()
68     if ret_type:
69         ret += "\n" + gen_err_check('local_err')
70         ret += "\n" + mcgen(''''
71 %(marshal_output_call)s
72 ''',
73                             marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
74     pop_indent(indent)
75     return ret.rstrip()
76
77
78 def gen_marshal_output_call(name, ret_type):
79     if not ret_type:
80         return ""
81     return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name)
82
83 def gen_visitor_input_containers_decl(args, obj):
84     ret = ""
85
86     push_indent()
87     if len(args) > 0:
88         ret += mcgen('''
89 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
90 QapiDeallocVisitor *md;
91 Visitor *v;
92 ''',
93                      obj=obj)
94     pop_indent()
95
96     return ret.rstrip()
97
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 = {0};
115 ''',
116                          argname=c_var(argname), argtype=c_type(argtype))
117
118     pop_indent()
119     return ret.rstrip()
120
121 def gen_visitor_input_block(args, dealloc=False):
122     ret = ""
123     errparg = '&local_err'
124     errarg = 'local_err'
125
126     if len(args) == 0:
127         return ret
128
129     push_indent()
130
131     if dealloc:
132         errparg = 'NULL'
133         errarg = None;
134         ret += mcgen('''
135 qmp_input_visitor_cleanup(mi);
136 md = qapi_dealloc_visitor_new();
137 v = qapi_dealloc_get_visitor(md);
138 ''')
139     else:
140         ret += mcgen('''
141 v = qmp_input_get_visitor(mi);
142 ''')
143
144     for argname, argtype, optional, structured in parse_args(args):
145         if optional:
146             ret += mcgen('''
147 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
148 ''',
149                          c_name=c_var(argname), name=argname, errp=errparg)
150             ret += gen_err_check(errarg)
151             ret += mcgen('''
152 if (has_%(c_name)s) {
153 ''',
154                          c_name=c_var(argname))
155             push_indent()
156         ret += mcgen('''
157 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
158 ''',
159                      c_name=c_var(argname), name=argname, argtype=argtype,
160                      visitor=type_visitor(argtype), errp=errparg)
161         ret += gen_err_check(errarg)
162         if optional:
163             pop_indent()
164             ret += mcgen('''
165 }
166 ''')
167
168     if dealloc:
169         ret += mcgen('''
170 qapi_dealloc_visitor_cleanup(md);
171 ''')
172     pop_indent()
173     return ret.rstrip()
174
175 def gen_marshal_output(name, args, ret_type, middle_mode):
176     if not ret_type:
177         return ""
178
179     ret = mcgen('''
180 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
181 {
182     Error *local_err = NULL;
183     QmpOutputVisitor *mo = qmp_output_visitor_new();
184     QapiDeallocVisitor *md;
185     Visitor *v;
186
187     v = qmp_output_get_visitor(mo);
188     %(visitor)s(v, &ret_in, "unused", &local_err);
189     if (local_err) {
190         goto out;
191     }
192     *ret_out = qmp_output_get_qobject(mo);
193
194 out:
195     error_propagate(errp, local_err);
196     qmp_output_visitor_cleanup(mo);
197     md = qapi_dealloc_visitor_new();
198     v = qapi_dealloc_get_visitor(md);
199     %(visitor)s(v, &ret_in, "unused", NULL);
200     qapi_dealloc_visitor_cleanup(md);
201 }
202 ''',
203                 c_ret_type=c_type(ret_type), c_name=c_fun(name),
204                 visitor=type_visitor(ret_type))
205
206     return ret
207
208 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
209     if middle_mode:
210         return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
211     else:
212         return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
213
214
215
216 def gen_marshal_input(name, args, ret_type, middle_mode):
217     hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
218
219     ret = mcgen('''
220 %(header)s
221 {
222     Error *local_err = NULL;
223 ''',
224                 header=hdr)
225
226     if middle_mode:
227         ret += mcgen('''
228     QDict *args = (QDict *)qdict;
229 ''')
230
231     if ret_type:
232         if c_type(ret_type).endswith("*"):
233             retval = "    %s retval = NULL;" % c_type(ret_type)
234         else:
235             retval = "    %s retval;" % c_type(ret_type)
236         ret += mcgen('''
237 %(retval)s
238 ''',
239                      retval=retval)
240
241     if len(args) > 0:
242         ret += mcgen('''
243 %(visitor_input_containers_decl)s
244 %(visitor_input_vars_decl)s
245
246 %(visitor_input_block)s
247
248 ''',
249                      visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
250                      visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
251                      visitor_input_block=gen_visitor_input_block(args))
252     else:
253         ret += mcgen('''
254
255     (void)args;
256 ''')
257
258     ret += mcgen('''
259 %(sync_call)s
260 ''',
261                  sync_call=gen_sync_call(name, args, ret_type, indent=4))
262     if re.search('^ *goto out\\;', ret, re.MULTILINE):
263         ret += mcgen('''
264
265 out:
266 ''')
267     if not middle_mode:
268         ret += mcgen('''
269     error_propagate(errp, local_err);
270 ''')
271     ret += mcgen('''
272 %(visitor_input_block_cleanup)s
273 ''',
274                  visitor_input_block_cleanup=gen_visitor_input_block(args,
275                                                                      dealloc=True))
276
277     if middle_mode:
278         ret += mcgen('''
279
280     if (local_err) {
281         qerror_report_err(local_err);
282         error_free(local_err);
283         return -1;
284     }
285     return 0;
286 ''')
287     else:
288         ret += mcgen('''
289     return;
290 ''')
291
292     ret += mcgen('''
293 }
294 ''')
295
296     return ret
297
298 def option_value_matches(opt, val, cmd):
299     if opt in cmd and cmd[opt] == val:
300         return True
301     return False
302
303 def gen_registry(commands):
304     registry=""
305     push_indent()
306     for cmd in commands:
307         options = 'QCO_NO_OPTIONS'
308         if option_value_matches('success-response', 'no', cmd):
309             options = 'QCO_NO_SUCCESS_RESP'
310
311         registry += mcgen('''
312 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
313 ''',
314                      name=cmd['command'], c_name=c_fun(cmd['command']),
315                      opts=options)
316     pop_indent()
317     ret = mcgen('''
318 static void qmp_init_marshal(void)
319 {
320 %(registry)s
321 }
322
323 qapi_init(qmp_init_marshal);
324 ''',
325                 registry=registry.rstrip())
326     return ret
327
328 def gen_command_decl_prologue(header, guard, prefix=""):
329     ret = mcgen('''
330 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
331
332 /*
333  * schema-defined QAPI function prototypes
334  *
335  * Copyright IBM, Corp. 2011
336  *
337  * Authors:
338  *  Anthony Liguori   <[email protected]>
339  *
340  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
341  * See the COPYING.LIB file in the top-level directory.
342  *
343  */
344
345 #ifndef %(guard)s
346 #define %(guard)s
347
348 #include "%(prefix)sqapi-types.h"
349 #include "qapi/qmp/qdict.h"
350 #include "qapi/error.h"
351
352 ''',
353                  header=basename(header), guard=guardname(header), prefix=prefix)
354     return ret
355
356 def gen_command_def_prologue(prefix="", proxy=False):
357     ret = mcgen('''
358 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
359
360 /*
361  * schema-defined QMP->QAPI command dispatch
362  *
363  * Copyright IBM, Corp. 2011
364  *
365  * Authors:
366  *  Anthony Liguori   <[email protected]>
367  *
368  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
369  * See the COPYING.LIB file in the top-level directory.
370  *
371  */
372
373 #include "qemu-common.h"
374 #include "qemu/module.h"
375 #include "qapi/qmp/qerror.h"
376 #include "qapi/qmp/types.h"
377 #include "qapi/qmp/dispatch.h"
378 #include "qapi/visitor.h"
379 #include "qapi/qmp-output-visitor.h"
380 #include "qapi/qmp-input-visitor.h"
381 #include "qapi/dealloc-visitor.h"
382 #include "%(prefix)sqapi-types.h"
383 #include "%(prefix)sqapi-visit.h"
384
385 ''',
386                 prefix=prefix)
387     if not proxy:
388         ret += '#include "%sqmp-commands.h"' % prefix
389     return ret + "\n\n"
390
391
392 try:
393     opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
394                                    ["source", "header", "prefix=",
395                                     "input-file=", "output-dir=",
396                                     "type=", "middle"])
397 except getopt.GetoptError, err:
398     print str(err)
399     sys.exit(1)
400
401 output_dir = ""
402 prefix = ""
403 dispatch_type = "sync"
404 c_file = 'qmp-marshal.c'
405 h_file = 'qmp-commands.h'
406 middle_mode = False
407
408 do_c = False
409 do_h = False
410
411 for o, a in opts:
412     if o in ("-p", "--prefix"):
413         prefix = a
414     elif o in ("-i", "--input-file"):
415         input_file = a
416     elif o in ("-o", "--output-dir"):
417         output_dir = a + "/"
418     elif o in ("-t", "--type"):
419         dispatch_type = a
420     elif o in ("-m", "--middle"):
421         middle_mode = True
422     elif o in ("-c", "--source"):
423         do_c = True
424     elif o in ("-h", "--header"):
425         do_h = True
426
427 if not do_c and not do_h:
428     do_c = True
429     do_h = True
430
431 c_file = output_dir + prefix + c_file
432 h_file = output_dir + prefix + h_file
433
434 def maybe_open(really, name, opt):
435     if really:
436         return open(name, opt)
437     else:
438         import StringIO
439         return StringIO.StringIO()
440
441 try:
442     os.makedirs(output_dir)
443 except os.error, e:
444     if e.errno != errno.EEXIST:
445         raise
446
447 exprs = parse_schema(input_file)
448 commands = filter(lambda expr: expr.has_key('command'), exprs)
449 commands = filter(lambda expr: not expr.has_key('gen'), commands)
450
451 if dispatch_type == "sync":
452     fdecl = maybe_open(do_h, h_file, 'w')
453     fdef = maybe_open(do_c, c_file, 'w')
454     ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
455     fdecl.write(ret)
456     ret = gen_command_def_prologue(prefix=prefix)
457     fdef.write(ret)
458
459     for cmd in commands:
460         arglist = []
461         ret_type = None
462         if cmd.has_key('data'):
463             arglist = cmd['data']
464         if cmd.has_key('returns'):
465             ret_type = cmd['returns']
466         ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
467         fdecl.write(ret)
468         if ret_type:
469             ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
470             fdef.write(ret)
471
472         if middle_mode:
473             fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
474
475         ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
476         fdef.write(ret)
477
478     fdecl.write("\n#endif\n");
479
480     if not middle_mode:
481         ret = gen_registry(commands)
482         fdef.write(ret)
483
484     fdef.flush()
485     fdef.close()
486     fdecl.flush()
487     fdecl.close()
This page took 0.05152 seconds and 4 git commands to generate.