Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 1 | # |
| 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 | # |
| 10 | # This work is licensed under the terms of the GNU GPLv2. |
| 11 | # See the COPYING.LIB file in the top-level directory. |
| 12 | |
| 13 | from ordereddict import OrderedDict |
| 14 | from qapi import * |
| 15 | import sys |
| 16 | import os |
| 17 | import getopt |
| 18 | import errno |
| 19 | |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 20 | def 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 Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 26 | def generate_decl_enum(name, members, genlist=True): |
| 27 | return mcgen(''' |
| 28 | |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 29 | void %(visitor)s(Visitor *m, %(name)s * obj, const char *name, Error **errp); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 30 | ''', |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 31 | visitor=type_visitor(name)) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 32 | |
| 33 | def generate_command_decl(name, args, ret_type): |
| 34 | arglist="" |
| 35 | for argname, argtype, optional, structured in parse_args(args): |
| 36 | argtype = c_type(argtype) |
| 37 | if argtype == "char *": |
| 38 | argtype = "const char *" |
| 39 | if optional: |
| 40 | arglist += "bool has_%s, " % c_var(argname) |
| 41 | arglist += "%s %s, " % (argtype, c_var(argname)) |
| 42 | return mcgen(''' |
| 43 | %(ret_type)s qmp_%(name)s(%(args)sError **errp); |
| 44 | ''', |
| 45 | ret_type=c_type(ret_type), name=c_var(name), args=arglist).strip() |
| 46 | |
| 47 | def gen_sync_call(name, args, ret_type, indent=0): |
| 48 | ret = "" |
| 49 | arglist="" |
| 50 | retval="" |
| 51 | if ret_type: |
| 52 | retval = "retval = " |
| 53 | for argname, argtype, optional, structured in parse_args(args): |
| 54 | if optional: |
| 55 | arglist += "has_%s, " % c_var(argname) |
| 56 | arglist += "%s, " % (c_var(argname)) |
| 57 | push_indent(indent) |
| 58 | ret = mcgen(''' |
| 59 | %(retval)sqmp_%(name)s(%(args)serrp); |
| 60 | |
| 61 | ''', |
| 62 | name=c_var(name), args=arglist, retval=retval).rstrip() |
| 63 | if ret_type: |
| 64 | ret += "\n" + mcgen('''' |
| 65 | %(marshal_output_call)s |
| 66 | ''', |
| 67 | marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip() |
| 68 | pop_indent(indent) |
| 69 | return ret.rstrip() |
| 70 | |
| 71 | |
| 72 | def gen_marshal_output_call(name, ret_type): |
| 73 | if not ret_type: |
| 74 | return "" |
| 75 | return "qmp_marshal_output_%s(retval, ret, errp);" % c_var(name) |
| 76 | |
| 77 | def gen_visitor_output_containers_decl(ret_type): |
| 78 | ret = "" |
| 79 | push_indent() |
| 80 | if ret_type: |
| 81 | ret += mcgen(''' |
| 82 | QmpOutputVisitor *mo; |
| 83 | QapiDeallocVisitor *md; |
| 84 | Visitor *v; |
| 85 | ''') |
| 86 | pop_indent() |
| 87 | |
| 88 | return ret |
| 89 | |
| 90 | def gen_visitor_input_containers_decl(args): |
| 91 | ret = "" |
| 92 | |
| 93 | push_indent() |
| 94 | if len(args) > 0: |
| 95 | ret += mcgen(''' |
| 96 | QmpInputVisitor *mi; |
| 97 | QapiDeallocVisitor *md; |
| 98 | Visitor *v; |
| 99 | ''') |
| 100 | pop_indent() |
| 101 | |
| 102 | return ret.rstrip() |
| 103 | |
| 104 | def gen_visitor_input_vars_decl(args): |
| 105 | ret = "" |
| 106 | push_indent() |
| 107 | for argname, argtype, optional, structured in parse_args(args): |
| 108 | if optional: |
| 109 | ret += mcgen(''' |
| 110 | bool has_%(argname)s = false; |
| 111 | ''', |
| 112 | argname=c_var(argname)) |
| 113 | if c_type(argtype).endswith("*"): |
| 114 | ret += mcgen(''' |
| 115 | %(argtype)s %(argname)s = NULL; |
| 116 | ''', |
| 117 | argname=c_var(argname), argtype=c_type(argtype)) |
| 118 | else: |
| 119 | ret += mcgen(''' |
| 120 | %(argtype)s %(argname)s; |
| 121 | ''', |
| 122 | argname=c_var(argname), argtype=c_type(argtype)) |
| 123 | |
| 124 | pop_indent() |
| 125 | return ret.rstrip() |
| 126 | |
| 127 | def gen_visitor_input_block(args, obj, dealloc=False): |
| 128 | ret = "" |
| 129 | if len(args) == 0: |
| 130 | return ret |
| 131 | |
| 132 | push_indent() |
| 133 | |
| 134 | if dealloc: |
| 135 | ret += mcgen(''' |
| 136 | md = qapi_dealloc_visitor_new(); |
| 137 | v = qapi_dealloc_get_visitor(md); |
| 138 | ''') |
| 139 | else: |
| 140 | ret += mcgen(''' |
| 141 | mi = qmp_input_visitor_new(%(obj)s); |
| 142 | v = qmp_input_get_visitor(mi); |
| 143 | ''', |
| 144 | obj=obj) |
| 145 | |
| 146 | for argname, argtype, optional, structured in parse_args(args): |
| 147 | if optional: |
| 148 | ret += mcgen(''' |
| 149 | visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp); |
| 150 | if (has_%(c_name)s) { |
| 151 | ''', |
| 152 | c_name=c_var(argname), name=argname) |
| 153 | push_indent() |
| 154 | ret += mcgen(''' |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 155 | %(visitor)s(v, &%(c_name)s, "%(name)s", errp); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 156 | ''', |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 157 | c_name=c_var(argname), name=argname, argtype=argtype, |
| 158 | visitor=type_visitor(argtype)) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 159 | if optional: |
| 160 | pop_indent() |
| 161 | ret += mcgen(''' |
| 162 | } |
| 163 | visit_end_optional(v, errp); |
| 164 | ''') |
| 165 | |
| 166 | if dealloc: |
| 167 | ret += mcgen(''' |
| 168 | qapi_dealloc_visitor_cleanup(md); |
| 169 | ''') |
| 170 | else: |
| 171 | ret += mcgen(''' |
| 172 | qmp_input_visitor_cleanup(mi); |
| 173 | ''') |
| 174 | pop_indent() |
| 175 | return ret.rstrip() |
| 176 | |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 177 | def gen_marshal_output(name, args, ret_type, middle_mode): |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 178 | if not ret_type: |
| 179 | return "" |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 180 | |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 181 | ret = mcgen(''' |
| 182 | static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp) |
| 183 | { |
| 184 | QapiDeallocVisitor *md = qapi_dealloc_visitor_new(); |
| 185 | QmpOutputVisitor *mo = qmp_output_visitor_new(); |
| 186 | Visitor *v; |
| 187 | |
| 188 | v = qmp_output_get_visitor(mo); |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 189 | %(visitor)s(v, &ret_in, "unused", errp); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 190 | if (!error_is_set(errp)) { |
| 191 | *ret_out = qmp_output_get_qobject(mo); |
| 192 | } |
| 193 | qmp_output_visitor_cleanup(mo); |
| 194 | v = qapi_dealloc_get_visitor(md); |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 195 | %(visitor)s(v, &ret_in, "unused", errp); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 196 | qapi_dealloc_visitor_cleanup(md); |
| 197 | } |
| 198 | ''', |
Anthony Liguori | 15e43e6 | 2011-09-14 14:30:00 -0500 | [diff] [blame^] | 199 | c_ret_type=c_type(ret_type), c_name=c_var(name), |
| 200 | visitor=type_visitor(ret_type)) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 201 | |
| 202 | return ret |
| 203 | |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 204 | def gen_marshal_input_decl(name, args, ret_type, middle_mode): |
| 205 | if middle_mode: |
| 206 | return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_var(name) |
| 207 | else: |
| 208 | return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_var(name) |
| 209 | |
| 210 | |
| 211 | |
| 212 | def gen_marshal_input(name, args, ret_type, middle_mode): |
| 213 | hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode) |
| 214 | |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 215 | ret = mcgen(''' |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 216 | %(header)s |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 217 | { |
| 218 | ''', |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 219 | header=hdr) |
| 220 | |
| 221 | if middle_mode: |
| 222 | ret += mcgen(''' |
| 223 | Error *local_err = NULL; |
| 224 | Error **errp = &local_err; |
| 225 | QDict *args = (QDict *)qdict; |
| 226 | ''') |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 227 | |
| 228 | if ret_type: |
| 229 | if c_type(ret_type).endswith("*"): |
| 230 | retval = " %s retval = NULL;" % c_type(ret_type) |
| 231 | else: |
| 232 | retval = " %s retval;" % c_type(ret_type) |
| 233 | ret += mcgen(''' |
| 234 | %(retval)s |
| 235 | ''', |
| 236 | retval=retval) |
| 237 | |
| 238 | if len(args) > 0: |
| 239 | ret += mcgen(''' |
| 240 | %(visitor_input_containers_decl)s |
| 241 | %(visitor_input_vars_decl)s |
| 242 | |
| 243 | %(visitor_input_block)s |
| 244 | |
| 245 | ''', |
| 246 | visitor_input_containers_decl=gen_visitor_input_containers_decl(args), |
| 247 | visitor_input_vars_decl=gen_visitor_input_vars_decl(args), |
| 248 | visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)")) |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 249 | else: |
| 250 | ret += mcgen(''' |
| 251 | (void)args; |
| 252 | ''') |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 253 | |
| 254 | ret += mcgen(''' |
| 255 | if (error_is_set(errp)) { |
| 256 | goto out; |
| 257 | } |
| 258 | %(sync_call)s |
| 259 | ''', |
| 260 | sync_call=gen_sync_call(name, args, ret_type, indent=4)) |
| 261 | ret += mcgen(''' |
| 262 | |
| 263 | out: |
| 264 | ''') |
| 265 | ret += mcgen(''' |
| 266 | %(visitor_input_block_cleanup)s |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 267 | ''', |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 268 | visitor_input_block_cleanup=gen_visitor_input_block(args, None, |
| 269 | dealloc=True)) |
| 270 | |
| 271 | if middle_mode: |
| 272 | ret += mcgen(''' |
| 273 | |
| 274 | if (local_err) { |
| 275 | qerror_report_err(local_err); |
| 276 | error_free(local_err); |
| 277 | return -1; |
| 278 | } |
| 279 | return 0; |
| 280 | ''') |
| 281 | else: |
| 282 | ret += mcgen(''' |
| 283 | return; |
| 284 | ''') |
| 285 | |
| 286 | ret += mcgen(''' |
| 287 | } |
| 288 | ''') |
| 289 | |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 290 | return ret |
| 291 | |
| 292 | def gen_registry(commands): |
| 293 | registry="" |
| 294 | push_indent() |
| 295 | for cmd in commands: |
| 296 | registry += mcgen(''' |
| 297 | qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s); |
| 298 | ''', |
| 299 | name=cmd['command'], c_name=c_var(cmd['command'])) |
| 300 | pop_indent() |
| 301 | ret = mcgen(''' |
| 302 | static void qmp_init_marshal(void) |
| 303 | { |
| 304 | %(registry)s |
| 305 | } |
| 306 | |
| 307 | qapi_init(qmp_init_marshal); |
| 308 | ''', |
| 309 | registry=registry.rstrip()) |
| 310 | return ret |
| 311 | |
| 312 | def gen_command_decl_prologue(header, guard, prefix=""): |
| 313 | ret = mcgen(''' |
| 314 | /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ |
| 315 | |
| 316 | /* |
| 317 | * schema-defined QAPI function prototypes |
| 318 | * |
| 319 | * Copyright IBM, Corp. 2011 |
| 320 | * |
| 321 | * Authors: |
| 322 | * Anthony Liguori <aliguori@us.ibm.com> |
| 323 | * |
| 324 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 325 | * See the COPYING.LIB file in the top-level directory. |
| 326 | * |
| 327 | */ |
| 328 | |
| 329 | #ifndef %(guard)s |
| 330 | #define %(guard)s |
| 331 | |
| 332 | #include "%(prefix)sqapi-types.h" |
| 333 | #include "error.h" |
| 334 | |
| 335 | ''', |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 336 | header=basename(header), guard=guardname(header), prefix=prefix) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 337 | return ret |
| 338 | |
| 339 | def gen_command_def_prologue(prefix="", proxy=False): |
| 340 | ret = mcgen(''' |
| 341 | /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */ |
| 342 | |
| 343 | /* |
| 344 | * schema-defined QMP->QAPI command dispatch |
| 345 | * |
| 346 | * Copyright IBM, Corp. 2011 |
| 347 | * |
| 348 | * Authors: |
| 349 | * Anthony Liguori <aliguori@us.ibm.com> |
| 350 | * |
| 351 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 352 | * See the COPYING.LIB file in the top-level directory. |
| 353 | * |
| 354 | */ |
| 355 | |
| 356 | #include "qemu-objects.h" |
| 357 | #include "qapi/qmp-core.h" |
| 358 | #include "qapi/qapi-visit-core.h" |
| 359 | #include "qapi/qmp-output-visitor.h" |
| 360 | #include "qapi/qmp-input-visitor.h" |
| 361 | #include "qapi/qapi-dealloc-visitor.h" |
| 362 | #include "%(prefix)sqapi-types.h" |
| 363 | #include "%(prefix)sqapi-visit.h" |
| 364 | |
| 365 | ''', |
| 366 | prefix=prefix) |
| 367 | if not proxy: |
| 368 | ret += '#include "%sqmp-commands.h"' % prefix |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 369 | return ret + "\n\n" |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 370 | |
| 371 | |
| 372 | try: |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 373 | opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:m", ["prefix=", "output-dir=", "type=", "middle"]) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 374 | except getopt.GetoptError, err: |
| 375 | print str(err) |
| 376 | sys.exit(1) |
| 377 | |
| 378 | output_dir = "" |
| 379 | prefix = "" |
| 380 | dispatch_type = "sync" |
| 381 | c_file = 'qmp-marshal.c' |
| 382 | h_file = 'qmp-commands.h' |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 383 | middle_mode = False |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 384 | |
| 385 | for o, a in opts: |
| 386 | if o in ("-p", "--prefix"): |
| 387 | prefix = a |
| 388 | elif o in ("-o", "--output-dir"): |
| 389 | output_dir = a + "/" |
| 390 | elif o in ("-t", "--type"): |
| 391 | dispatch_type = a |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 392 | elif o in ("-m", "--middle"): |
| 393 | middle_mode = True |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 394 | |
| 395 | c_file = output_dir + prefix + c_file |
| 396 | h_file = output_dir + prefix + h_file |
| 397 | |
| 398 | try: |
| 399 | os.makedirs(output_dir) |
| 400 | except os.error, e: |
| 401 | if e.errno != errno.EEXIST: |
| 402 | raise |
| 403 | |
| 404 | exprs = parse_schema(sys.stdin) |
| 405 | commands = filter(lambda expr: expr.has_key('command'), exprs) |
| 406 | |
| 407 | if dispatch_type == "sync": |
| 408 | fdecl = open(h_file, 'w') |
| 409 | fdef = open(c_file, 'w') |
| 410 | ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix) |
| 411 | fdecl.write(ret) |
| 412 | ret = gen_command_def_prologue(prefix=prefix) |
| 413 | fdef.write(ret) |
| 414 | |
| 415 | for cmd in commands: |
| 416 | arglist = [] |
| 417 | ret_type = None |
| 418 | if cmd.has_key('data'): |
| 419 | arglist = cmd['data'] |
| 420 | if cmd.has_key('returns'): |
| 421 | ret_type = cmd['returns'] |
| 422 | ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n" |
| 423 | fdecl.write(ret) |
| 424 | if ret_type: |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 425 | ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n" |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 426 | fdef.write(ret) |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 427 | |
| 428 | if middle_mode: |
| 429 | fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode)) |
| 430 | |
| 431 | ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n" |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 432 | fdef.write(ret) |
| 433 | |
Michael Roth | 7534ba0 | 2011-08-10 13:10:51 -0500 | [diff] [blame] | 434 | fdecl.write("\n#endif\n"); |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 435 | |
| 436 | if not middle_mode: |
| 437 | ret = gen_registry(commands) |
| 438 | fdef.write(ret) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 439 | |
| 440 | fdef.flush() |
| 441 | fdef.close() |
| 442 | fdecl.flush() |
| 443 | fdecl.close() |