aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/gentypes.py
blob: 20d403079a53572152f5711dd316b417362f85b4 (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
#!/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_type_of(ty):
    return ty.typename

def libxl_C_instance_of(ty, instancename):
    if isinstance(ty, libxltypes.BitField):
        return libxl_C_type_of(ty) + " " + instancename + ":%d" % ty.width
    elif 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 libxl_C_type_of(ty) + " " + instancename

def libxl_C_type_define(ty, indent = ""):
    s = ""
    if 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, reference, indent = "    ", parent = None):
    if reference:
        deref = v + "->"
    else:
        deref = v + "."
        
    if ty.passby == libxltypes.PASS_BY_REFERENCE and not reference:
        makeref = "&"
    else:
        makeref = ""

    s = ""
    if isinstance(ty, libxltypes.KeyedUnion):
        if parent is None:
            raise Exception("KeyedUnion type must have a parent")
        for f in ty.fields:
            keyvar_expr = f.keyvar_expr % (parent + ty.keyvar_name)
            s += "if (" + keyvar_expr + ") {\n"
            s += libxl_C_type_destroy(f.type, deref + f.name, False, indent + "    ", deref)
            s += "}\n"
    elif isinstance(ty, libxltypes.Reference):
        s += libxl_C_type_destroy(ty.ref_type, v, True, indent, v)
        if ty.destructor_fn is not None:
            s += "%s(%s);\n" % (ty.destructor_fn, makeref + v)
    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]:

            if f.name is None: # Anonynous struct
                s += libxl_C_type_destroy(f.type, deref, False, "", deref)
            else:
                s += libxl_C_type_destroy(f.type, deref + f.name, False, "", deref)
    else:
        if ty.destructor_fn is not None:
            s += "%s(%s);\n" % (ty.destructor_fn, makeref + v)
            
    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 = sys.argv[1]
    (_,types) = libxltypes.parse(idl)
                    
    header = sys.argv[2]
    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 *p);\n" % (ty.destructor_fn, ty.typename))
        f.write("\n")

    f.write("""#endif /* __LIBXL_TYPES_H */\n""")
    f.close()
    
    impl = sys.argv[3]
    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"

#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 *p)\n" % (ty.destructor_fn, ty.typename))
        f.write("{\n")
        f.write(libxl_C_type_destroy(ty, "p", True))
        f.write("    memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n")
        f.write("}\n")
        f.write("\n")
    f.close()