From patchwork Thu Oct 4 17:33:39 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 189291 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4C3C32C03EE for ; Fri, 5 Oct 2012 06:05:48 +1000 (EST) Received: from localhost ([::1]:34772 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJrg2-0007jU-8f for incoming@patchwork.ozlabs.org; Thu, 04 Oct 2012 16:05:46 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49550) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJpJZ-0007cR-UO for qemu-devel@nongnu.org; Thu, 04 Oct 2012 13:34:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TJpJY-00010x-6G for qemu-devel@nongnu.org; Thu, 04 Oct 2012 13:34:25 -0400 Received: from mail-ob0-f173.google.com ([209.85.214.173]:43670) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TJpJY-0000gj-1B for qemu-devel@nongnu.org; Thu, 04 Oct 2012 13:34:24 -0400 Received: by mail-ob0-f173.google.com with SMTP id wc18so694637obb.4 for ; Thu, 04 Oct 2012 10:34:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=BjFdtV1EDAGrWkCyeqJRXMWrNfTwdk7mz/xN01Nws28=; b=w4zlxkCMfn0FP13rcnO8A5cPA75YOb0alOgyb7uU52c/dc/o5xtQMuvh9dTF+wnfOx M0JnDdn3CwAf3v6Zsf011i9puDFmlmnegm5d8I8XcaFMQDJKwhT0jZjmdhr+r7ANJFme itkdnSho2wWFmDVKWVypfEqbK7695ncB3ifARqNpxCpmuaWis1HBV1rAPsXhuc7Xfzq5 OL/lFPDpzc0NnkkpL0YkUF4LKDeojsK52cbDiWoySJIyENDwBf9JKmlkWuEfo/r6WJGr RZf8HDFY/RDLnydAReq9KpBp43Zp068BWnHp3Xpo82mWYMLnw89WoiyDG2W2Cd055Lmz ssPA== Received: by 10.60.14.165 with SMTP id q5mr5127607oec.28.1349372063735; Thu, 04 Oct 2012 10:34:23 -0700 (PDT) Received: from loki.austin.ibm.com ([32.97.110.59]) by mx.google.com with ESMTPS id h2sm7131923obn.11.2012.10.04.10.34.21 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 04 Oct 2012 10:34:22 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Thu, 4 Oct 2012 12:33:39 -0500 Message-Id: <1349372021-31212-21-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1349372021-31212-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1349372021-31212-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.214.173 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 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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 +# Michael Roth +# +# 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[''] = '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[''] = 'true' + description[''] = '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))