All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, peter.maydell@linaro.org, aliguori@us.ibm.com,
	blauwirbel@gmail.com, pbonzini@redhat.com, eblake@redhat.com
Subject: [Qemu-devel] [PATCH v3 20/22] qidl: add QAPI-based code generator
Date: Thu,  4 Oct 2012 12:33:39 -0500	[thread overview]
Message-ID: <1349372021-31212-21-git-send-email-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <1349372021-31212-1-git-send-email-mdroth@linux.vnet.ibm.com>

This takes the declarations generated by the QIDL parser and converts
them to QAPI schemas to generate the visitor routines and other
supporting code for QIDL.

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 scripts/qidl.py |  281 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 281 insertions(+)
 create mode 100644 scripts/qidl.py

diff --git a/scripts/qidl.py b/scripts/qidl.py
new file mode 100644
index 0000000..5726d98
--- /dev/null
+++ b/scripts/qidl.py
@@ -0,0 +1,281 @@
+#
+# QIDL Code Generator
+#
+# Copyright IBM, Corp. 2012
+#
+# Authors:
+#  Anthony Liguori <aliguori@us.ibm.com>
+#  Michael Roth    <mdroth@linux.vnet.ibm.com>
+#
+# This work is licensed under the terms of the GNU GPLv2.
+# See the COPYING file in the top-level directory.
+
+from ordereddict import OrderedDict
+from qidl_parser import parse_file
+from qapi import parse_schema, mcgen, camel_case, de_camel_case
+from qapi_visit import generate_visit_struct, push_indent, pop_indent
+import sys
+import json
+import getopt
+import os
+import errno
+
+def qapi_schema(node):
+    schema = OrderedDict()
+    data = OrderedDict()
+    fields = None
+    if node.has_key('typedef'):
+        schema['type'] = node['typedef']
+        fields = node['type']['fields']
+    elif node.has_key('struct'):
+        schema['type'] = node['struct']
+        fields = node['fields']
+    else:
+        raise Exception("top-level neither typedef nor struct")
+
+    for field in fields:
+        if filter(lambda x: field.has_key(x), \
+                ['is_derived', 'is_immutable', 'is_broken', 'is_function', \
+                 'is_nested_decl', 'is_elsewhere']):
+            continue
+
+        description = {}
+
+        if field['type'].endswith('_t'):
+            typename = field['type'][:-2]
+        elif field['type'].startswith('struct '):
+            typename = field['type'].split(" ")[1]
+        elif field['type'].startswith('enum '):
+            typename = 'int'
+        elif field['type'] == "_Bool":
+            typename = 'bool'
+        elif field['type'].endswith("char") and field.has_key('is_pointer'):
+            typename = 'str';
+        elif field['type'] == 'int':
+            typename = 'int32'
+        elif field['type'] == 'unsigned int':
+            typename = 'uint32'
+        elif field['type'] == 'char':
+            typename = 'uint8'
+        else:
+            typename = field['type']
+
+        if field.has_key('is_array') and field['is_array']:
+            description['type'] = [typename]
+            description['<annotated>'] = 'true'
+            if field.has_key('array_size'):
+                description['array_size'] = field['array_size']
+            if field.has_key('array_capacity'):
+                description['array_capacity'] = field['array_capacity']
+        elif camel_case(de_camel_case(typename)) == typename and \
+            (not field.has_key('is_pointer') or not field['is_pointer']):
+            description['<annotated>'] = 'true'
+            description['<embedded struct>'] = 'true'
+            description['type'] = typename
+        else:
+            description = typename
+
+        if field.has_key('is_optional') and field['is_optional']:
+            data["*" + field['variable']] = description
+        else:
+            data[field['variable']] = description
+
+    schema['data'] = data
+    return schema
+
+def parse_schema_file(filename):
+    return parse_schema(open(filename, 'r'))
+
+def write_file(output, filename):
+    if filename:
+        output_file = open(filename, 'w')
+    else:
+        output_file = sys.stdout
+    output_file.write(output)
+    if filename:
+        output_file.close()
+
+def property_list(node):
+    prop_list = []
+    fields = None
+    if node.has_key('typedef'):
+        state = node['typedef']
+        fields = node['type']['fields']
+    elif node.has_key('struct'):
+        state = node['struct']
+        fields = node['fields']
+    else:
+        raise Exception("top-level neither typedef nor struct")
+
+    for field in fields:
+        if not field.has_key('is_property'):
+            continue
+
+        for arglist in field['property_fields']:
+            if field['variable'] == 'devfn':
+                typename = 'pci_devfn'
+            elif field['type'].endswith('_t'):
+                typename = field['type'][:-2]
+            elif field['type'] == "_Bool":
+                typename = 'bool'
+            elif field.has_key('is_pointer'):
+                if field['type'] in ("char", "const char"):
+                    typename = "string"
+                elif field['type'] == "void":
+                    typename = "ptr"
+            else:
+                typename = field['type']
+
+            prop = {}
+            prop['name'] = arglist[0]
+            prop['state'] = state
+            prop['type'] = typename
+            prop['field'] = field['variable']
+            if len(arglist) == 2:
+                prop['default'] = arglist[1]
+            elif len(arglist) == 3:
+                prop['type'] = 'bit'
+                prop['bit'] = arglist[1]
+                prop['default'] = arglist[2]
+
+            prop_list.append(prop)
+
+    return prop_list
+
+def generate_include(include_path):
+    return mcgen('''
+#include "%(include)s"
+''',
+                       include=include_path)
+
+def generate_property_bit(type_name, prop):
+    if prop.has_key('default'):
+        return mcgen('''
+        DEFINE_PROP_BIT(%(name)s, %(state)s, %(field)s, %(bit)s, %(default)s),
+''',
+                        name=prop['name'], state=prop['state'],
+                        field=prop['field'], bit=prop['bit'],
+                        default=prop['default'])
+    return mcgen('''
+        DEFINE_PROP_BIT(%(name)s, %(state)s, %(field)s, %(bit)s),
+''',
+                 name=prop['name'], state=prop['state'],
+                 field=prop['field'], bit=prop['bit'])
+
+def generate_property(type_name, prop):
+    if prop.has_key('default'):
+        return mcgen('''
+        DEFINE_PROP_%(type)s(%(name)s, %(state)s, %(field)s, %(default)s),
+''',
+                        type=prop['type'].upper(),  name=prop['name'],
+                        state=prop['state'], field=prop['field'],
+                        default=prop['default'])
+    return mcgen('''
+        DEFINE_PROP_%(type)s(%(name)s, %(state)s, %(field)s),
+''',
+                 type=prop['type'].upper(),  name=prop['name'],
+                 state=prop['state'], field=prop['field'])
+
+def generate_properties(type_name, prop_list=[]):
+    output = ""
+
+    for prop in prop_list:
+        if prop['type'] == 'bit':
+            output += generate_property_bit(type_name, prop)
+        else:
+            output += generate_property(type_name, prop)
+
+    output += mcgen('''
+        DEFINE_PROP_END_OF_LIST()
+''')
+
+    return output
+
+def generate_qidl_registration(type_name, schema, do_state, prop_list=[]):
+    schema_text = json.dumps("%s" % json.dumps(schema))
+    visitor = "NULL"
+    if do_state:
+        visitor = "visit_type_%s" % type_name
+
+    return mcgen('''
+static char *%(type_name)s_get_schema(Object *obj, Error **errp)
+{
+    return g_strdup(qidl_data_%(type_name)s.schema_json_text);
+}
+
+static void %(type_name)s_register_qidl(void)
+{
+    static Property properties[] = {
+%(properties)s
+    };
+    ObjectProperty *schema_link;
+
+    qidl_data_%(type_name)s.properties = properties;
+    qidl_data_%(type_name)s.visitor = %(visitor)s;
+    qidl_data_%(type_name)s.schema_json_text = %(schema_text)s;
+
+    schema_link = object_property_find(container_get(object_get_root(), "/qidl/schemas"),
+                                       "%(type_name)s", NULL);
+    qidl_data_%(type_name)s.schema_obj = container_get(object_get_root(), "/qidl/schemas/%(type_name)s");
+    if (!schema_link) {
+        object_property_add_str(qidl_data_%(type_name)s.schema_obj, "json_text",
+                                %(type_name)s_get_schema, NULL, NULL);
+    }
+}
+
+qidl_init(%(type_name)s_register_qidl)
+''',
+                 type_name=type_name, schema_text=schema_text, visitor=visitor,
+                 properties=generate_properties(type_name, prop_list))
+
+def main(argv=[]):
+    try:
+        opts, args = getopt.gnu_getopt(argv[1:], "o:cd:I:",
+                                       ["output-filepath=", "include="])
+    except getopt.GetoptError, err:
+        print >> sys.stderr, err
+        return 1
+
+    output_filepath = None
+    includes = []
+    for o, a in opts:
+        if o in ("-f", "--output-filepath"):
+            output_filepath = a
+        elif o in ("-I", "--include"):
+            includes.append(a)
+
+    nodes = parse_file(sys.stdin)
+    if not nodes:
+        return 2
+
+    if os.path.dirname(output_filepath) != "":
+        try:
+            os.makedirs(os.path.dirname(output_filepath))
+        except os.error, e:
+            if e.errno != errno.EEXIST:
+                raise
+    output = ""
+    for include in includes:
+        output += generate_include(include)
+    for node in nodes:
+        do_state = False
+        schema = qapi_schema(node)
+        prop_list = []
+        # qapi parser expects iteration to be line-by-line
+        schema_text = json.dumps(schema, indent=4).replace("\"", "'").split("\n")
+        expr = parse_schema(schema_text)[0]
+
+        if node.has_key('do_state') and node['do_state']:
+            do_state = True
+            output += generate_visit_struct(expr['type'], expr['data'], True)
+        if node.has_key('do_properties') and node['do_properties']:
+            prop_list = property_list(node)
+
+        output += generate_qidl_registration(expr['type'], schema, do_state, prop_list)
+
+    write_file(output, output_filepath)
+
+    return 0
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))
-- 
1.7.9.5

  parent reply	other threads:[~2012-10-04 17:34 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-04 17:33 [Qemu-devel] [PATCH v3] Add infrastructure for QIDL-based device serialization Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 01/22] qapi: qapi-visit.py -> qapi_visit.py so we can import Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 02/22] qapi: qapi-types.py -> qapi_types.py Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 03/22] qapi: qapi-commands.py -> qapi_commands.py Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 04/22] qapi: qapi_visit.py, make code useable as module Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 05/22] qapi: qapi_visit.py, support arrays and complex qapi definitions Michael Roth
2012-10-05  8:11   ` Paolo Bonzini
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 06/22] qapi: qapi_visit.py, support generating static functions Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 07/22] qapi: qapi_visit.py, support for visiting non-pointer/embedded structs Michael Roth
2012-10-05  8:09   ` Paolo Bonzini
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 08/22] qapi: add visitor interfaces for C arrays Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 09/22] qapi: QmpOutputVisitor, implement array handling Michael Roth
2012-10-05  8:05   ` Paolo Bonzini
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 10/22] qapi: QmpInputVisitor, " Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 11/22] qapi: qapi.py, make json parser more robust Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 12/22] qapi: add open-coded visitor for struct tm types Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 13/22] qom-fuse: force single-threaded mode to avoid QMP races Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 14/22] qom-fuse: workaround for truncated properties > 4096 Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 15/22] module additions for schema registration Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 16/22] qdev: move Property-related declarations to qdev-properties.h Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 17/22] qidl: add documentation Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 18/22] qidl: add lexer library (based on QC parser) Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 19/22] qidl: add C parser " Michael Roth
2012-10-04 17:33 ` Michael Roth [this message]
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 21/22] qidl: qidl.h, definitions for qidl annotations Michael Roth
2012-10-05  8:14   ` Paolo Bonzini
2012-10-05 14:50     ` Michael Roth
2012-10-05 15:07   ` Paolo Bonzini
2012-10-05 15:41     ` Michael Roth
2012-10-05 15:53       ` Paolo Bonzini
2012-10-05 16:47         ` Michael Roth
2012-10-15 13:37           ` Paolo Bonzini
2012-10-15 15:50             ` Michael Roth
2012-10-04 17:33 ` [Qemu-devel] [PATCH v3 22/22] qidl: unit tests and build infrastructure Michael Roth
2012-10-05  8:24   ` Paolo Bonzini
2012-10-12 21:39     ` Michael Roth
2012-10-13  7:12       ` Paolo Bonzini
2012-10-15  8:52       ` Kevin Wolf
2012-10-15 14:48         ` Michael Roth
2012-10-05  8:26 ` [Qemu-devel] [PATCH v3] Add infrastructure for QIDL-based device serialization Paolo Bonzini
2012-10-05 14:52   ` Michael Roth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1349372021-31212-21-git-send-email-mdroth@linux.vnet.ibm.com \
    --to=mdroth@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=blauwirbel@gmail.com \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.