~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to scripts/qapi.py

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# QAPI helper library
3
3
#
4
4
# Copyright IBM, Corp. 2011
 
5
# Copyright (c) 2013 Red Hat Inc.
5
6
#
6
7
# Authors:
7
8
#  Anthony Liguori <aliguori@us.ibm.com>
 
9
#  Markus Armbruster <armbru@redhat.com>
8
10
#
9
11
# This work is licensed under the terms of the GNU GPLv2.
10
12
# See the COPYING.LIB file in the top-level directory.
11
13
 
12
14
from ordereddict import OrderedDict
13
 
 
14
 
def tokenize(data):
15
 
    while len(data):
16
 
        ch = data[0]
17
 
        data = data[1:]
18
 
        if ch in ['{', '}', ':', ',', '[', ']']:
19
 
            yield ch
20
 
        elif ch in ' \n':
21
 
            None
22
 
        elif ch == "'":
23
 
            string = ''
24
 
            esc = False
25
 
            while True:
26
 
                if (data == ''):
27
 
                    raise Exception("Mismatched quotes")
28
 
                ch = data[0]
29
 
                data = data[1:]
30
 
                if esc:
31
 
                    string += ch
32
 
                    esc = False
33
 
                elif ch == "\\":
34
 
                    esc = True
35
 
                elif ch == "'":
36
 
                    break
37
 
                else:
38
 
                    string += ch
39
 
            yield string
40
 
 
41
 
def parse(tokens):
42
 
    if tokens[0] == '{':
43
 
        ret = OrderedDict()
44
 
        tokens = tokens[1:]
45
 
        while tokens[0] != '}':
46
 
            key = tokens[0]
47
 
            tokens = tokens[1:]
48
 
 
49
 
            tokens = tokens[1:] # :
50
 
 
51
 
            value, tokens = parse(tokens)
52
 
 
53
 
            if tokens[0] == ',':
54
 
                tokens = tokens[1:]
55
 
 
56
 
            ret[key] = value
57
 
        tokens = tokens[1:]
58
 
        return ret, tokens
59
 
    elif tokens[0] == '[':
60
 
        ret = []
61
 
        tokens = tokens[1:]
62
 
        while tokens[0] != ']':
63
 
            value, tokens = parse(tokens)
64
 
            if tokens[0] == ',':
65
 
                tokens = tokens[1:]
66
 
            ret.append(value)
67
 
        tokens = tokens[1:]
68
 
        return ret, tokens
69
 
    else:
70
 
        return tokens[0], tokens[1:]
71
 
 
72
 
def evaluate(string):
73
 
    return parse(map(lambda x: x, tokenize(string)))[0]
 
15
import sys
 
16
 
 
17
builtin_types = [
 
18
    'str', 'int', 'number', 'bool',
 
19
    'int8', 'int16', 'int32', 'int64',
 
20
    'uint8', 'uint16', 'uint32', 'uint64'
 
21
]
 
22
 
 
23
builtin_type_qtypes = {
 
24
    'str':      'QTYPE_QSTRING',
 
25
    'int':      'QTYPE_QINT',
 
26
    'number':   'QTYPE_QFLOAT',
 
27
    'bool':     'QTYPE_QBOOL',
 
28
    'int8':     'QTYPE_QINT',
 
29
    'int16':    'QTYPE_QINT',
 
30
    'int32':    'QTYPE_QINT',
 
31
    'int64':    'QTYPE_QINT',
 
32
    'uint8':    'QTYPE_QINT',
 
33
    'uint16':   'QTYPE_QINT',
 
34
    'uint32':   'QTYPE_QINT',
 
35
    'uint64':   'QTYPE_QINT',
 
36
}
 
37
 
 
38
class QAPISchemaError(Exception):
 
39
    def __init__(self, schema, msg):
 
40
        self.fp = schema.fp
 
41
        self.msg = msg
 
42
        self.line = self.col = 1
 
43
        for ch in schema.src[0:schema.pos]:
 
44
            if ch == '\n':
 
45
                self.line += 1
 
46
                self.col = 1
 
47
            elif ch == '\t':
 
48
                self.col = (self.col + 7) % 8 + 1
 
49
            else:
 
50
                self.col += 1
 
51
 
 
52
    def __str__(self):
 
53
        return "%s:%s:%s: %s" % (self.fp.name, self.line, self.col, self.msg)
 
54
 
 
55
class QAPISchema:
 
56
 
 
57
    def __init__(self, fp):
 
58
        self.fp = fp
 
59
        self.src = fp.read()
 
60
        if self.src == '' or self.src[-1] != '\n':
 
61
            self.src += '\n'
 
62
        self.cursor = 0
 
63
        self.exprs = []
 
64
        self.accept()
 
65
 
 
66
        while self.tok != None:
 
67
            self.exprs.append(self.get_expr(False))
 
68
 
 
69
    def accept(self):
 
70
        while True:
 
71
            self.tok = self.src[self.cursor]
 
72
            self.pos = self.cursor
 
73
            self.cursor += 1
 
74
            self.val = None
 
75
 
 
76
            if self.tok == '#':
 
77
                self.cursor = self.src.find('\n', self.cursor)
 
78
            elif self.tok in ['{', '}', ':', ',', '[', ']']:
 
79
                return
 
80
            elif self.tok == "'":
 
81
                string = ''
 
82
                esc = False
 
83
                while True:
 
84
                    ch = self.src[self.cursor]
 
85
                    self.cursor += 1
 
86
                    if ch == '\n':
 
87
                        raise QAPISchemaError(self,
 
88
                                              'Missing terminating "\'"')
 
89
                    if esc:
 
90
                        string += ch
 
91
                        esc = False
 
92
                    elif ch == "\\":
 
93
                        esc = True
 
94
                    elif ch == "'":
 
95
                        self.val = string
 
96
                        return
 
97
                    else:
 
98
                        string += ch
 
99
            elif self.tok == '\n':
 
100
                if self.cursor == len(self.src):
 
101
                    self.tok = None
 
102
                    return
 
103
            elif not self.tok.isspace():
 
104
                raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
 
105
 
 
106
    def get_members(self):
 
107
        expr = OrderedDict()
 
108
        if self.tok == '}':
 
109
            self.accept()
 
110
            return expr
 
111
        if self.tok != "'":
 
112
            raise QAPISchemaError(self, 'Expected string or "}"')
 
113
        while True:
 
114
            key = self.val
 
115
            self.accept()
 
116
            if self.tok != ':':
 
117
                raise QAPISchemaError(self, 'Expected ":"')
 
118
            self.accept()
 
119
            expr[key] = self.get_expr(True)
 
120
            if self.tok == '}':
 
121
                self.accept()
 
122
                return expr
 
123
            if self.tok != ',':
 
124
                raise QAPISchemaError(self, 'Expected "," or "}"')
 
125
            self.accept()
 
126
            if self.tok != "'":
 
127
                raise QAPISchemaError(self, 'Expected string')
 
128
 
 
129
    def get_values(self):
 
130
        expr = []
 
131
        if self.tok == ']':
 
132
            self.accept()
 
133
            return expr
 
134
        if not self.tok in [ '{', '[', "'" ]:
 
135
            raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
 
136
        while True:
 
137
            expr.append(self.get_expr(True))
 
138
            if self.tok == ']':
 
139
                self.accept()
 
140
                return expr
 
141
            if self.tok != ',':
 
142
                raise QAPISchemaError(self, 'Expected "," or "]"')
 
143
            self.accept()
 
144
 
 
145
    def get_expr(self, nested):
 
146
        if self.tok != '{' and not nested:
 
147
            raise QAPISchemaError(self, 'Expected "{"')
 
148
        if self.tok == '{':
 
149
            self.accept()
 
150
            expr = self.get_members()
 
151
        elif self.tok == '[':
 
152
            self.accept()
 
153
            expr = self.get_values()
 
154
        elif self.tok == "'":
 
155
            expr = self.val
 
156
            self.accept()
 
157
        else:
 
158
            raise QAPISchemaError(self, 'Expected "{", "[" or string')
 
159
        return expr
74
160
 
75
161
def parse_schema(fp):
 
162
    try:
 
163
        schema = QAPISchema(fp)
 
164
    except QAPISchemaError, e:
 
165
        print >>sys.stderr, e
 
166
        exit(1)
 
167
 
76
168
    exprs = []
77
 
    expr = ''
78
 
    expr_eval = None
79
 
 
80
 
    for line in fp:
81
 
        if line.startswith('#') or line == '\n':
82
 
            continue
83
 
 
84
 
        if line.startswith(' '):
85
 
            expr += line
86
 
        elif expr:
87
 
            expr_eval = evaluate(expr)
88
 
            if expr_eval.has_key('enum'):
89
 
                add_enum(expr_eval['enum'])
90
 
            elif expr_eval.has_key('union'):
91
 
                add_enum('%sKind' % expr_eval['union'])
92
 
            exprs.append(expr_eval)
93
 
            expr = line
94
 
        else:
95
 
            expr += line
96
 
 
97
 
    if expr:
98
 
        expr_eval = evaluate(expr)
99
 
        if expr_eval.has_key('enum'):
100
 
            add_enum(expr_eval['enum'])
101
 
        elif expr_eval.has_key('union'):
102
 
            add_enum('%sKind' % expr_eval['union'])
103
 
        exprs.append(expr_eval)
 
169
 
 
170
    for expr in schema.exprs:
 
171
        if expr.has_key('enum'):
 
172
            add_enum(expr['enum'])
 
173
        elif expr.has_key('union'):
 
174
            add_union(expr)
 
175
            add_enum('%sKind' % expr['union'])
 
176
        elif expr.has_key('type'):
 
177
            add_struct(expr)
 
178
        exprs.append(expr)
104
179
 
105
180
    return exprs
106
181
 
107
182
def parse_args(typeinfo):
 
183
    if isinstance(typeinfo, basestring):
 
184
        struct = find_struct(typeinfo)
 
185
        assert struct != None
 
186
        typeinfo = struct['data']
 
187
 
108
188
    for member in typeinfo:
109
189
        argname = member
110
190
        argentry = typeinfo[member]
174
254
    return name
175
255
 
176
256
enum_types = []
 
257
struct_types = []
 
258
union_types = []
 
259
 
 
260
def add_struct(definition):
 
261
    global struct_types
 
262
    struct_types.append(definition)
 
263
 
 
264
def find_struct(name):
 
265
    global struct_types
 
266
    for struct in struct_types:
 
267
        if struct['type'] == name:
 
268
            return struct
 
269
    return None
 
270
 
 
271
def add_union(definition):
 
272
    global union_types
 
273
    union_types.append(definition)
 
274
 
 
275
def find_union(name):
 
276
    global union_types
 
277
    for union in union_types:
 
278
        if union['union'] == name:
 
279
            return union
 
280
    return None
177
281
 
178
282
def add_enum(name):
179
283
    global enum_types
242
346
    for substr in [".", " ", "-"]:
243
347
        guard = guard.replace(substr, "_")
244
348
    return guard.upper() + '_H'
 
349
 
 
350
def guardstart(name):
 
351
    return mcgen('''
 
352
 
 
353
#ifndef %(name)s
 
354
#define %(name)s
 
355
 
 
356
''',
 
357
                 name=guardname(name))
 
358
 
 
359
def guardend(name):
 
360
    return mcgen('''
 
361
 
 
362
#endif /* %(name)s */
 
363
 
 
364
''',
 
365
                 name=guardname(name))