ppc/pnv: add a PnvICPState object
[qemu.git] / scripts / qapi-commands.py
1 #
2 # QAPI command marshaller generator
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2016 Red Hat, Inc.
6 #
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
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 qapi import *
16
17
18 def gen_command_decl(name, arg_type, boxed, ret_type):
19 return mcgen('''
20 %(c_type)s qmp_%(c_name)s(%(params)s);
21 ''',
22 c_type=(ret_type and ret_type.c_type()) or 'void',
23 c_name=c_name(name),
24 params=gen_params(arg_type, boxed, 'Error **errp'))
25
26
27 def gen_call(name, arg_type, boxed, ret_type):
28 ret = ''
29
30 argstr = ''
31 if boxed:
32 assert arg_type and not arg_type.is_empty()
33 argstr = '&arg, '
34 elif arg_type:
35 assert not arg_type.variants
36 for memb in arg_type.members:
37 if memb.optional:
38 argstr += 'arg.has_%s, ' % c_name(memb.name)
39 argstr += 'arg.%s, ' % c_name(memb.name)
40
41 lhs = ''
42 if ret_type:
43 lhs = 'retval = '
44
45 ret = mcgen('''
46
47 %(lhs)sqmp_%(c_name)s(%(args)s&err);
48 ''',
49 c_name=c_name(name), args=argstr, lhs=lhs)
50 if ret_type:
51 ret += mcgen('''
52 if (err) {
53 goto out;
54 }
55
56 qmp_marshal_output_%(c_name)s(retval, ret, &err);
57 ''',
58 c_name=ret_type.c_name())
59 return ret
60
61
62 def gen_marshal_output(ret_type):
63 return mcgen('''
64
65 static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
66 {
67 Error *err = NULL;
68 Visitor *v;
69
70 v = qobject_output_visitor_new(ret_out);
71 visit_type_%(c_name)s(v, "unused", &ret_in, &err);
72 if (!err) {
73 visit_complete(v, ret_out);
74 }
75 error_propagate(errp, err);
76 visit_free(v);
77 v = qapi_dealloc_visitor_new();
78 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
79 visit_free(v);
80 }
81 ''',
82 c_type=ret_type.c_type(), c_name=ret_type.c_name())
83
84
85 def gen_marshal_proto(name):
86 return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
87 % c_name(name))
88
89
90 def gen_marshal_decl(name):
91 return mcgen('''
92 %(proto)s;
93 ''',
94 proto=gen_marshal_proto(name))
95
96
97 def gen_marshal(name, arg_type, boxed, ret_type):
98 have_args = arg_type and not arg_type.is_empty()
99
100 ret = mcgen('''
101
102 %(proto)s
103 {
104 Error *err = NULL;
105 ''',
106 proto=gen_marshal_proto(name))
107
108 if ret_type:
109 ret += mcgen('''
110 %(c_type)s retval;
111 ''',
112 c_type=ret_type.c_type())
113
114 if have_args:
115 visit_members = ('visit_type_%s_members(v, &arg, &err);'
116 % arg_type.c_name())
117 ret += mcgen('''
118 Visitor *v;
119 %(c_name)s arg = {0};
120
121 ''',
122 c_name=arg_type.c_name())
123 else:
124 visit_members = ''
125 ret += mcgen('''
126 Visitor *v = NULL;
127
128 if (args) {
129 ''')
130 push_indent()
131
132 ret += mcgen('''
133 v = qobject_input_visitor_new(QOBJECT(args));
134 visit_start_struct(v, NULL, NULL, 0, &err);
135 if (err) {
136 goto out;
137 }
138 %(visit_members)s
139 if (!err) {
140 visit_check_struct(v, &err);
141 }
142 visit_end_struct(v, NULL);
143 if (err) {
144 goto out;
145 }
146 ''',
147 visit_members=visit_members)
148
149 if not have_args:
150 pop_indent()
151 ret += mcgen('''
152 }
153 ''')
154
155 ret += gen_call(name, arg_type, boxed, ret_type)
156
157 ret += mcgen('''
158
159 out:
160 error_propagate(errp, err);
161 visit_free(v);
162 ''')
163
164 if have_args:
165 visit_members = ('visit_type_%s_members(v, &arg, NULL);'
166 % arg_type.c_name())
167 else:
168 visit_members = ''
169 ret += mcgen('''
170 if (args) {
171 ''')
172 push_indent()
173
174 ret += mcgen('''
175 v = qapi_dealloc_visitor_new();
176 visit_start_struct(v, NULL, NULL, 0, NULL);
177 %(visit_members)s
178 visit_end_struct(v, NULL);
179 visit_free(v);
180 ''',
181 visit_members=visit_members)
182
183 if not have_args:
184 pop_indent()
185 ret += mcgen('''
186 }
187 ''')
188
189 ret += mcgen('''
190 }
191 ''')
192 return ret
193
194
195 def gen_register_command(name, success_response):
196 options = 'QCO_NO_OPTIONS'
197 if not success_response:
198 options = 'QCO_NO_SUCCESS_RESP'
199
200 ret = mcgen('''
201 qmp_register_command(cmds, "%(name)s",
202 qmp_marshal_%(c_name)s, %(opts)s);
203 ''',
204 name=name, c_name=c_name(name),
205 opts=options)
206 return ret
207
208
209 def gen_registry(registry):
210 ret = mcgen('''
211
212 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
213 {
214 QTAILQ_INIT(cmds);
215
216 ''',
217 c_prefix=c_name(prefix, protect=False))
218 ret += registry
219 ret += mcgen('''
220 }
221 ''')
222 return ret
223
224
225 class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
226 def __init__(self):
227 self.decl = None
228 self.defn = None
229 self._regy = None
230 self._visited_ret_types = None
231
232 def visit_begin(self, schema):
233 self.decl = ''
234 self.defn = ''
235 self._regy = ''
236 self._visited_ret_types = set()
237
238 def visit_end(self):
239 self.defn += gen_registry(self._regy)
240 self._regy = None
241 self._visited_ret_types = None
242
243 def visit_command(self, name, info, arg_type, ret_type,
244 gen, success_response, boxed):
245 if not gen:
246 return
247 self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
248 if ret_type and ret_type not in self._visited_ret_types:
249 self._visited_ret_types.add(ret_type)
250 self.defn += gen_marshal_output(ret_type)
251 self.decl += gen_marshal_decl(name)
252 self.defn += gen_marshal(name, arg_type, boxed, ret_type)
253 self._regy += gen_register_command(name, success_response)
254
255
256 (input_file, output_dir, do_c, do_h, prefix, opts) = parse_command_line()
257
258 c_comment = '''
259 /*
260 * schema-defined QMP->QAPI command dispatch
261 *
262 * Copyright IBM, Corp. 2011
263 *
264 * Authors:
265 * Anthony Liguori <aliguori@us.ibm.com>
266 *
267 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
268 * See the COPYING.LIB file in the top-level directory.
269 *
270 */
271 '''
272 h_comment = '''
273 /*
274 * schema-defined QAPI function prototypes
275 *
276 * Copyright IBM, Corp. 2011
277 *
278 * Authors:
279 * Anthony Liguori <aliguori@us.ibm.com>
280 *
281 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
282 * See the COPYING.LIB file in the top-level directory.
283 *
284 */
285 '''
286
287 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
288 'qmp-marshal.c', 'qmp-commands.h',
289 c_comment, h_comment)
290
291 fdef.write(mcgen('''
292 #include "qemu/osdep.h"
293 #include "qemu-common.h"
294 #include "qemu/module.h"
295 #include "qapi/qmp/types.h"
296 #include "qapi/visitor.h"
297 #include "qapi/qobject-output-visitor.h"
298 #include "qapi/qobject-input-visitor.h"
299 #include "qapi/dealloc-visitor.h"
300 #include "%(prefix)sqapi-types.h"
301 #include "%(prefix)sqapi-visit.h"
302 #include "%(prefix)sqmp-commands.h"
303
304 ''',
305 prefix=prefix))
306
307 fdecl.write(mcgen('''
308 #include "%(prefix)sqapi-types.h"
309 #include "qapi/qmp/qdict.h"
310 #include "qapi/qmp/dispatch.h"
311 #include "qapi/error.h"
312
313 void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
314 ''',
315 prefix=prefix, c_prefix=c_name(prefix, protect=False)))
316
317 schema = QAPISchema(input_file)
318 gen = QAPISchemaGenCommandVisitor()
319 schema.visit(gen)
320 fdef.write(gen.defn)
321 fdecl.write(gen.decl)
322
323 close_output(fdef, fdecl)