target-arm: Store SPSR_EL1 state in banked_spsr[1] (SPSR_svc)
[qemu.git] / scripts / qapi-event.py
blob601e3076abafe679d4acf13414ba2bd6d9aa1065
2 # QAPI event generator
4 # Copyright (c) 2014 Wenchao Xia
6 # Authors:
7 # Wenchao Xia <wenchaoqemu@gmail.com>
9 # This work is licensed under the terms of the GNU GPL, version 2.
10 # See the COPYING file in the top-level directory.
12 from ordereddict import OrderedDict
13 from qapi import *
14 import sys
15 import os
16 import getopt
17 import errno
19 def _generate_event_api_name(event_name, params):
20 api_name = "void qapi_event_send_%s(" % c_fun(event_name).lower();
21 l = len(api_name)
23 if params:
24 for argname, argentry, optional, structured in parse_args(params):
25 if optional:
26 api_name += "bool has_%s,\n" % c_var(argname)
27 api_name += "".ljust(l)
29 api_name += "%s %s,\n" % (c_type(argentry, is_param=True),
30 c_var(argname))
31 api_name += "".ljust(l)
33 api_name += "Error **errp)"
34 return api_name;
37 # Following are the core functions that generate C APIs to emit event.
39 def generate_event_declaration(api_name):
40 return mcgen('''
42 %(api_name)s;
43 ''',
44 api_name = api_name)
46 def generate_event_implement(api_name, event_name, params):
47 # step 1: declare any variables
48 ret = mcgen("""
50 %(api_name)s
52 QDict *qmp;
53 Error *local_err = NULL;
54 QMPEventFuncEmit emit;
55 """,
56 api_name = api_name)
58 if params:
59 ret += mcgen("""
60 QmpOutputVisitor *qov;
61 Visitor *v;
62 QObject *obj;
64 """)
66 # step 2: check emit function, create a dict
67 ret += mcgen("""
68 emit = qmp_event_get_func_emit();
69 if (!emit) {
70 return;
73 qmp = qmp_event_build_dict("%(event_name)s");
75 """,
76 event_name = event_name)
78 # step 3: visit the params if params != None
79 if params:
80 ret += mcgen("""
81 qov = qmp_output_visitor_new();
82 g_assert(qov);
84 v = qmp_output_get_visitor(qov);
85 g_assert(v);
87 /* Fake visit, as if all members are under a structure */
88 visit_start_struct(v, NULL, "", "%(event_name)s", 0, &local_err);
89 if (local_err) {
90 goto clean;
93 """,
94 event_name = event_name)
96 for argname, argentry, optional, structured in parse_args(params):
97 if optional:
98 ret += mcgen("""
99 if (has_%(var)s) {
100 """,
101 var = c_var(argname))
102 push_indent()
104 if argentry == "str":
105 var_type = "(char **)"
106 else:
107 var_type = ""
109 ret += mcgen("""
110 visit_type_%(type)s(v, %(var_type)s&%(var)s, "%(name)s", &local_err);
111 if (local_err) {
112 goto clean;
114 """,
115 var_type = var_type,
116 var = c_var(argname),
117 type = type_name(argentry),
118 name = argname)
120 if optional:
121 pop_indent()
122 ret += mcgen("""
124 """)
126 ret += mcgen("""
128 visit_end_struct(v, &local_err);
129 if (local_err) {
130 goto clean;
133 obj = qmp_output_get_qobject(qov);
134 g_assert(obj != NULL);
136 qdict_put_obj(qmp, "data", obj);
137 """)
139 # step 4: call qmp event api
140 ret += mcgen("""
141 emit(%(event_enum_value)s, qmp, &local_err);
143 """,
144 event_enum_value = event_enum_value)
146 # step 5: clean up
147 if params:
148 ret += mcgen("""
149 clean:
150 qmp_output_visitor_cleanup(qov);
151 """)
152 ret += mcgen("""
153 error_propagate(errp, local_err);
154 QDECREF(qmp);
156 """)
158 return ret
161 # Following are the functions that generate an enum type for all defined
162 # events, similar to qapi-types.py. Here we already have enum name and
163 # values which were generated before and recorded in event_enum_*. It also
164 # works around the issue that "import qapi-types" can't work.
166 def generate_event_enum_decl(event_enum_name, event_enum_values):
167 lookup_decl = mcgen('''
169 extern const char *%(event_enum_name)s_lookup[];
170 ''',
171 event_enum_name = event_enum_name)
173 enum_decl = mcgen('''
174 typedef enum %(event_enum_name)s
176 ''',
177 event_enum_name = event_enum_name)
179 # append automatically generated _MAX value
180 enum_max_value = generate_enum_full_value(event_enum_name, "MAX")
181 enum_values = event_enum_values + [ enum_max_value ]
183 i = 0
184 for value in enum_values:
185 enum_decl += mcgen('''
186 %(value)s = %(i)d,
187 ''',
188 value = value,
189 i = i)
190 i += 1
192 enum_decl += mcgen('''
193 } %(event_enum_name)s;
194 ''',
195 event_enum_name = event_enum_name)
197 return lookup_decl + enum_decl
199 def generate_event_enum_lookup(event_enum_name, event_enum_strings):
200 ret = mcgen('''
202 const char *%(event_enum_name)s_lookup[] = {
203 ''',
204 event_enum_name = event_enum_name)
206 i = 0
207 for string in event_enum_strings:
208 ret += mcgen('''
209 "%(string)s",
210 ''',
211 string = string)
213 ret += mcgen('''
214 NULL,
216 ''')
217 return ret
220 # Start the real job
222 try:
223 opts, args = getopt.gnu_getopt(sys.argv[1:], "chbp:i:o:",
224 ["source", "header", "builtins", "prefix=",
225 "input-file=", "output-dir="])
226 except getopt.GetoptError, err:
227 print str(err)
228 sys.exit(1)
230 input_file = ""
231 output_dir = ""
232 prefix = ""
233 c_file = 'qapi-event.c'
234 h_file = 'qapi-event.h'
236 do_c = False
237 do_h = False
238 do_builtins = False
240 for o, a in opts:
241 if o in ("-p", "--prefix"):
242 prefix = a
243 elif o in ("-i", "--input-file"):
244 input_file = a
245 elif o in ("-o", "--output-dir"):
246 output_dir = a + "/"
247 elif o in ("-c", "--source"):
248 do_c = True
249 elif o in ("-h", "--header"):
250 do_h = True
251 elif o in ("-b", "--builtins"):
252 do_builtins = True
254 if not do_c and not do_h:
255 do_c = True
256 do_h = True
258 c_file = output_dir + prefix + c_file
259 h_file = output_dir + prefix + h_file
261 try:
262 os.makedirs(output_dir)
263 except os.error, e:
264 if e.errno != errno.EEXIST:
265 raise
267 def maybe_open(really, name, opt):
268 if really:
269 return open(name, opt)
270 else:
271 import StringIO
272 return StringIO.StringIO()
274 fdef = maybe_open(do_c, c_file, 'w')
275 fdecl = maybe_open(do_h, h_file, 'w')
277 fdef.write(mcgen('''
278 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
281 * schema-defined QAPI event functions
283 * Copyright (c) 2014 Wenchao Xia
285 * Authors:
286 * Wenchao Xia <wenchaoqemu@gmail.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 #include "qemu-common.h"
294 #include "%(header)s"
295 #include "%(prefix)sqapi-visit.h"
296 #include "qapi/qmp-output-visitor.h"
297 #include "qapi/qmp-event.h"
299 ''',
300 prefix=prefix, header=basename(h_file)))
302 fdecl.write(mcgen('''
303 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
306 * schema-defined QAPI event functions
308 * Copyright (c) 2014 Wenchao Xia
310 * Authors:
311 * Wenchao Xia <wenchaoqemu@gmail.com>
313 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
314 * See the COPYING.LIB file in the top-level directory.
318 #ifndef %(guard)s
319 #define %(guard)s
321 #include "qapi/error.h"
322 #include "qapi/qmp/qdict.h"
323 #include "%(prefix)sqapi-types.h"
325 ''',
326 prefix=prefix, guard=guardname(h_file)))
328 exprs = parse_schema(input_file)
330 event_enum_name = prefix.upper().replace('-', '_') + "QAPIEvent"
331 event_enum_values = []
332 event_enum_strings = []
334 for expr in exprs:
335 if expr.has_key('event'):
336 event_name = expr['event']
337 params = expr.get('data')
338 if params and len(params) == 0:
339 params = None
341 api_name = _generate_event_api_name(event_name, params)
342 ret = generate_event_declaration(api_name)
343 fdecl.write(ret)
345 # We need an enum value per event
346 event_enum_value = generate_enum_full_value(event_enum_name,
347 event_name)
348 ret = generate_event_implement(api_name, event_name, params)
349 fdef.write(ret)
351 # Record it, and generate enum later
352 event_enum_values.append(event_enum_value)
353 event_enum_strings.append(event_name)
355 ret = generate_event_enum_decl(event_enum_name, event_enum_values)
356 fdecl.write(ret)
357 ret = generate_event_enum_lookup(event_enum_name, event_enum_strings)
358 fdef.write(ret)
360 fdecl.write('''
361 #endif
362 ''')
364 fdecl.flush()
365 fdecl.close()
367 fdef.flush()
368 fdef.close()