aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/gentypes.py
blob: 23406fceac141efff7036bf86e0a614fec1942aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/python

import sys
import re

import libxltypes

def format_comment(level, comment):
    indent = reduce(lambda x,y: x + " ", range(level), "")
    s  = "%s/*\n" % indent
    s += "%s * " % indent
    comment = comment.replace("\n", "\n%s * " % indent)
    x = re.compile(r'^%s \* $' % indent, re.MULTILINE)
    comment = x.sub("%s *" % indent, comment)
    s += comment
    s += "\n"
    s += "%s */" % indent
    s += "\n"
    return s
    
def libxl_C_instance_of(ty, instancename):
    if isinstance(ty, libxltypes.Aggregate) and ty.typename is None:
        if instancename is None:
            return libxl_C_type_define(ty)
        else:
            return libxl_C_type_define(ty) + " " + instancename
    else:
        return ty.typename + " " + instancename

def libxl_C_type_define(ty, indent = ""):
    s = ""

    if isinstance(ty, libxltypes.Enumeration):
        if ty.comment is not None:
            s += format_comment(0, ty.comment)
        
        if ty.typename is None:
            s += "enum {\n"
        else:
            s += "typedef enum %s {\n" % ty.typename

        for v in ty.values:
            if v.comment is not None:
                s += format_comment(4, v.comment)
            x = "%s = %d" % (v.name, v.value)
            x = x.replace("\n", "\n    ")
            s += "    " + x + ",\n"
        if ty.typename is None:
            s += "}"
        else:
            s += "} %s" % ty.typename

    elif isinstance(ty, libxltypes.Aggregate):
        if ty.comment is not None:
            s += format_comment(0, ty.comment)

        if ty.typename is None:
            s += "%s {\n" % ty.kind
        else:
            s += "typedef %s {\n" % ty.kind

        for f in ty.fields:
            if f.comment is not None:
                s += format_comment(4, f.comment)
            x = libxl_C_instance_of(f.type, f.name)
            if f.const:
                x = "const " + x
            x = x.replace("\n", "\n    ")
            s += "    " + x + ";\n"
        if ty.typename is None:
            s += "}"
        else:
            s += "} %s" % ty.typename
    else:
        raise NotImplementedError("%s" % type(ty))
    return s.replace("\n", "\n%s" % indent)

def libxl_C_type_destroy(ty, v, indent = "    ", parent = None):
        
    s = ""
    if isinstance(ty, libxltypes.KeyedUnion):
        if parent is None:
            raise Exception("KeyedUnion type must have a parent")
        s += "switch (%s) {\n" % (parent + ty.keyvar_name)        
        for f in ty.fields:
            (nparent,fexpr) = ty.member(v, f, parent is None)
            s += "case %s:\n" % f.enumname
            s += libxl_C_type_destroy(f.type, fexpr, indent + "    ", nparent)
            s += "    break;\n"
        s += "}\n"
    elif isinstance(ty, libxltypes.Struct) and (parent is None or ty.destructor_fn is None):
        for f in [f for f in ty.fields if not f.const]:
            (nparent,fexpr) = ty.member(v, f, parent is None)
            s += libxl_C_type_destroy(f.type, fexpr, "", nparent)
    else:
        if ty.destructor_fn is not None:
            s += "%s(%s);\n" % (ty.destructor_fn, ty.pass_arg(v, parent is None))
            
    if s != "":
        s = indent + s
    return s.replace("\n", "\n%s" % indent).rstrip(indent)

def libxl_C_enum_to_string(ty, e, indent = "    "):
    s = ""
    s += "switch(%s) {\n" % e
    for v in ty.values:
        s += "    case %s:\n" % (v.name)
        s += "        return \"%s\";\n" % (v.valuename.lower())
    s += "    default:\n "
    s += "        return NULL;\n"
    s += "}\n"

    if s != "":
        s = indent + s
    return s.replace("\n", "\n%s" % indent).rstrip(indent)

def libxl_C_enum_strings(ty, indent=""):
    s = ""
    s += "libxl_enum_string_table %s_string_table[] = {\n" % (ty.typename)
    for v in ty.values:
        s += "    { .s = \"%s\", .v = %s },\n" % (v.valuename.lower(), v.name)
    s += "    { NULL, -1 },\n"
    s += "};\n"
    s += "\n"
    
    if s != "":
        s = indent + s
    return s.replace("\n", "\n%s" % indent).rstrip(indent)

def libxl_C_enum_from_string(ty, str, e, indent = "    "):
    s = ""
    s += "return libxl__enum_from_string(%s_string_table,\n" % ty.typename
    s += "                               %s, (int *)%s);\n" % (str, e)
    
    if s != "":
        s = indent + s
    return s.replace("\n", "\n%s" % indent).rstrip(indent)
    

if __name__ == '__main__':
    if len(sys.argv) != 4:
        print >>sys.stderr, "Usage: gentypes.py <idl> <header> <implementation>"
        sys.exit(1)

    (_, idl, header, impl) = sys.argv

    (_,types) = libxltypes.parse(idl)
                    
    print "outputting libxl type definitions to %s" % header

    f = open(header, "w")
    
    f.write("""#ifndef __LIBXL_TYPES_H
#define __LIBXL_TYPES_H

/*
 * DO NOT EDIT.
 *
 * This file is autogenerated by
 * "%s"
 */
 
""" % " ".join(sys.argv))
        
    for ty in types:
        f.write(libxl_C_type_define(ty) + ";\n")
        if ty.destructor_fn is not None:
            f.write("void %s(%s);\n" % (ty.destructor_fn, ty.make_arg("p")))
        if isinstance(ty, libxltypes.Enumeration):
            f.write("const char *%s_to_string(%s);\n" % (ty.typename, ty.make_arg("p")))
            f.write("int %s_from_string(const char *s, %s);\n" % (ty.typename, ty.make_arg("e", passby=libxltypes.PASS_BY_REFERENCE)))
            f.write("extern libxl_enum_string_table %s_string_table[];\n" % (ty.typename))
        f.write("\n")

    f.write("""#endif /* __LIBXL_TYPES_H */\n""")
    f.close()
    
    print "outputting libxl type implementations to %s" % impl

    f = open(impl, "w")
    f.write("""
/* DO NOT EDIT.
 *
 * This file is autogenerated by
 * "%s"
 */

#include "libxl_osdeps.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "libxl.h"
#include "libxl_internal.h"

#define LIBXL_DTOR_POISON 0xa5

""" % " ".join(sys.argv))

    for ty in [t for t in types if t.destructor_fn is not None and t.autogenerate_destructor]:
        f.write("void %s(%s)\n" % (ty.destructor_fn, ty.make_arg("p")))
        f.write("{\n")
        f.write(libxl_C_type_destroy(ty, "p"))
        f.write("    memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n")
        f.write("}\n")
        f.write("\n")

    for ty in [t for t in types if isinstance(t,libxltypes.Enumeration)]:
        f.write("const char *%s_to_string(%s e)\n" % (ty.typename, ty.typename))
        f.write("{\n")
        f.write(libxl_C_enum_to_string(ty, "e"))
        f.write("}\n")
        f.write("\n")

        f.write(libxl_C_enum_strings(ty))               

        f.write("int %s_from_string(const char *s, %s *e)\n" % (ty.typename, ty.typename))
        f.write("{\n")
        f.write(libxl_C_enum_from_string(ty, "s", "e"))               
        f.write("}\n")
        f.write("\n")


    f.close()