2
0

qapi-commands.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. from qapi import *
  15. import re
  16. def gen_command_decl(name, arg_type, ret_type):
  17. return mcgen('''
  18. %(c_type)s qmp_%(c_name)s(%(params)s);
  19. ''',
  20. c_type=(ret_type and ret_type.c_type()) or 'void',
  21. c_name=c_name(name),
  22. params=gen_params(arg_type, 'Error **errp'))
  23. def gen_call(name, arg_type, ret_type):
  24. ret = ''
  25. argstr = ''
  26. if arg_type:
  27. assert not arg_type.variants
  28. for memb in arg_type.members:
  29. if memb.optional:
  30. argstr += 'arg.has_%s, ' % c_name(memb.name)
  31. argstr += 'arg.%s, ' % c_name(memb.name)
  32. lhs = ''
  33. if ret_type:
  34. lhs = 'retval = '
  35. ret = mcgen('''
  36. %(lhs)sqmp_%(c_name)s(%(args)s&err);
  37. ''',
  38. c_name=c_name(name), args=argstr, lhs=lhs)
  39. if ret_type:
  40. ret += mcgen('''
  41. if (err) {
  42. goto out;
  43. }
  44. qmp_marshal_output_%(c_name)s(retval, ret, &err);
  45. ''',
  46. c_name=ret_type.c_name())
  47. return ret
  48. def gen_marshal_output(ret_type):
  49. return mcgen('''
  50. static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
  51. {
  52. Error *err = NULL;
  53. Visitor *v;
  54. v = qmp_output_visitor_new(ret_out);
  55. visit_type_%(c_name)s(v, "unused", &ret_in, &err);
  56. if (!err) {
  57. visit_complete(v, ret_out);
  58. }
  59. error_propagate(errp, err);
  60. visit_free(v);
  61. v = qapi_dealloc_visitor_new();
  62. visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
  63. visit_free(v);
  64. }
  65. ''',
  66. c_type=ret_type.c_type(), c_name=ret_type.c_name())
  67. def gen_marshal_proto(name):
  68. ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
  69. if not middle_mode:
  70. ret = 'static ' + ret
  71. return ret
  72. def gen_marshal_decl(name):
  73. return mcgen('''
  74. %(proto)s;
  75. ''',
  76. proto=gen_marshal_proto(name))
  77. def gen_marshal(name, arg_type, ret_type):
  78. ret = mcgen('''
  79. %(proto)s
  80. {
  81. Error *err = NULL;
  82. ''',
  83. proto=gen_marshal_proto(name))
  84. if ret_type:
  85. ret += mcgen('''
  86. %(c_type)s retval;
  87. ''',
  88. c_type=ret_type.c_type())
  89. if arg_type and not arg_type.is_empty():
  90. ret += mcgen('''
  91. Visitor *v;
  92. %(c_name)s arg = {0};
  93. v = qmp_input_visitor_new(QOBJECT(args), true);
  94. visit_start_struct(v, NULL, NULL, 0, &err);
  95. if (err) {
  96. goto out;
  97. }
  98. visit_type_%(c_name)s_members(v, &arg, &err);
  99. if (!err) {
  100. visit_check_struct(v, &err);
  101. }
  102. visit_end_struct(v, NULL);
  103. if (err) {
  104. goto out;
  105. }
  106. ''',
  107. c_name=arg_type.c_name())
  108. else:
  109. ret += mcgen('''
  110. (void)args;
  111. ''')
  112. ret += gen_call(name, arg_type, ret_type)
  113. # 'goto out' produced above for arg_type, and by gen_call() for ret_type
  114. if (arg_type and not arg_type.is_empty()) or ret_type:
  115. ret += mcgen('''
  116. out:
  117. ''')
  118. ret += mcgen('''
  119. error_propagate(errp, err);
  120. ''')
  121. if arg_type and not arg_type.is_empty():
  122. ret += mcgen('''
  123. visit_free(v);
  124. v = qapi_dealloc_visitor_new();
  125. visit_start_struct(v, NULL, NULL, 0, NULL);
  126. visit_type_%(c_name)s_members(v, &arg, NULL);
  127. visit_end_struct(v, NULL);
  128. visit_free(v);
  129. ''',
  130. c_name=arg_type.c_name())
  131. ret += mcgen('''
  132. }
  133. ''')
  134. return ret
  135. def gen_register_command(name, success_response):
  136. options = 'QCO_NO_OPTIONS'
  137. if not success_response:
  138. options = 'QCO_NO_SUCCESS_RESP'
  139. ret = mcgen('''
  140. qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
  141. ''',
  142. name=name, c_name=c_name(name),
  143. opts=options)
  144. return ret
  145. def gen_registry(registry):
  146. ret = mcgen('''
  147. static void qmp_init_marshal(void)
  148. {
  149. ''')
  150. ret += registry
  151. ret += mcgen('''
  152. }
  153. qapi_init(qmp_init_marshal);
  154. ''')
  155. return ret
  156. class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
  157. def __init__(self):
  158. self.decl = None
  159. self.defn = None
  160. self._regy = None
  161. self._visited_ret_types = None
  162. def visit_begin(self, schema):
  163. self.decl = ''
  164. self.defn = ''
  165. self._regy = ''
  166. self._visited_ret_types = set()
  167. def visit_end(self):
  168. if not middle_mode:
  169. self.defn += gen_registry(self._regy)
  170. self._regy = None
  171. self._visited_ret_types = None
  172. def visit_command(self, name, info, arg_type, ret_type,
  173. gen, success_response):
  174. if not gen:
  175. return
  176. self.decl += gen_command_decl(name, arg_type, ret_type)
  177. if ret_type and ret_type not in self._visited_ret_types:
  178. self._visited_ret_types.add(ret_type)
  179. self.defn += gen_marshal_output(ret_type)
  180. if middle_mode:
  181. self.decl += gen_marshal_decl(name)
  182. self.defn += gen_marshal(name, arg_type, ret_type)
  183. if not middle_mode:
  184. self._regy += gen_register_command(name, success_response)
  185. middle_mode = False
  186. (input_file, output_dir, do_c, do_h, prefix, opts) = \
  187. parse_command_line("m", ["middle"])
  188. for o, a in opts:
  189. if o in ("-m", "--middle"):
  190. middle_mode = True
  191. c_comment = '''
  192. /*
  193. * schema-defined QMP->QAPI command dispatch
  194. *
  195. * Copyright IBM, Corp. 2011
  196. *
  197. * Authors:
  198. * Anthony Liguori <aliguori@us.ibm.com>
  199. *
  200. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  201. * See the COPYING.LIB file in the top-level directory.
  202. *
  203. */
  204. '''
  205. h_comment = '''
  206. /*
  207. * schema-defined QAPI function prototypes
  208. *
  209. * Copyright IBM, Corp. 2011
  210. *
  211. * Authors:
  212. * Anthony Liguori <aliguori@us.ibm.com>
  213. *
  214. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  215. * See the COPYING.LIB file in the top-level directory.
  216. *
  217. */
  218. '''
  219. (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
  220. 'qmp-marshal.c', 'qmp-commands.h',
  221. c_comment, h_comment)
  222. fdef.write(mcgen('''
  223. #include "qemu/osdep.h"
  224. #include "qemu-common.h"
  225. #include "qemu/module.h"
  226. #include "qapi/qmp/types.h"
  227. #include "qapi/qmp/dispatch.h"
  228. #include "qapi/visitor.h"
  229. #include "qapi/qmp-output-visitor.h"
  230. #include "qapi/qmp-input-visitor.h"
  231. #include "qapi/dealloc-visitor.h"
  232. #include "%(prefix)sqapi-types.h"
  233. #include "%(prefix)sqapi-visit.h"
  234. #include "%(prefix)sqmp-commands.h"
  235. ''',
  236. prefix=prefix))
  237. fdecl.write(mcgen('''
  238. #include "%(prefix)sqapi-types.h"
  239. #include "qapi/qmp/qdict.h"
  240. #include "qapi/error.h"
  241. ''',
  242. prefix=prefix))
  243. schema = QAPISchema(input_file)
  244. gen = QAPISchemaGenCommandVisitor()
  245. schema.visit(gen)
  246. fdef.write(gen.defn)
  247. fdecl.write(gen.decl)
  248. close_output(fdef, fdecl)