2
0

qapi-commands.py 7.0 KB

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