debuggers.hg

view tools/libxl/gentypes.py @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents e363e0a285f5
children
line source
1 #!/usr/bin/python
3 import sys
4 import re
6 import libxltypes
8 def format_comment(level, comment):
9 indent = reduce(lambda x,y: x + " ", range(level), "")
10 s = "%s/*\n" % indent
11 s += "%s * " % indent
12 comment = comment.replace("\n", "\n%s * " % indent)
13 x = re.compile(r'^%s \* $' % indent, re.MULTILINE)
14 comment = x.sub("%s *" % indent, comment)
15 s += comment
16 s += "\n"
17 s += "%s */" % indent
18 s += "\n"
19 return s
21 def libxl_C_type_of(ty):
22 return ty.typename
24 def libxl_C_instance_of(ty, instancename):
25 if isinstance(ty, libxltypes.BitField):
26 return libxl_C_type_of(ty) + " " + instancename + ":%d" % ty.width
27 elif isinstance(ty, libxltypes.Aggregate) and ty.typename is None:
28 if instancename is None:
29 return libxl_C_type_define(ty)
30 else:
31 return libxl_C_type_define(ty) + " " + instancename
32 else:
33 return libxl_C_type_of(ty) + " " + instancename
35 def libxl_C_type_define(ty, indent = ""):
36 s = ""
37 if isinstance(ty, libxltypes.Aggregate):
38 if ty.comment is not None:
39 s += format_comment(0, ty.comment)
41 if ty.typename is None:
42 s += "%s {\n" % ty.kind
43 else:
44 s += "typedef %s {\n" % ty.kind
46 for f in ty.fields:
47 if f.comment is not None:
48 s += format_comment(4, f.comment)
49 x = libxl_C_instance_of(f.type, f.name)
50 if f.const:
51 x = "const " + x
52 x = x.replace("\n", "\n ")
53 s += " " + x + ";\n"
54 if ty.typename is None:
55 s += "}"
56 else:
57 s += "} %s" % ty.typename
58 else:
59 raise NotImplementedError("%s" % type(ty))
60 return s.replace("\n", "\n%s" % indent)
62 def libxl_C_type_destroy(ty, v, reference, indent = " ", parent = None):
63 if reference:
64 deref = v + "->"
65 else:
66 deref = v + "."
68 if ty.passby == libxltypes.PASS_BY_REFERENCE and not reference:
69 makeref = "&"
70 else:
71 makeref = ""
73 s = ""
74 if isinstance(ty, libxltypes.KeyedUnion):
75 if parent is None:
76 raise Exception("KeyedUnion type must have a parent")
77 for f in ty.fields:
78 keyvar_expr = f.keyvar_expr % (parent + ty.keyvar_name)
79 s += "if (" + keyvar_expr + ") {\n"
80 s += libxl_C_type_destroy(f.type, deref + f.name, False, indent + " ", deref)
81 s += "}\n"
82 elif isinstance(ty, libxltypes.Reference):
83 s += libxl_C_type_destroy(ty.ref_type, v, True, indent, v)
84 if ty.destructor_fn is not None:
85 s += "%s(%s);\n" % (ty.destructor_fn, makeref + v)
86 elif isinstance(ty, libxltypes.Struct) and (parent is None or ty.destructor_fn is None):
87 for f in [f for f in ty.fields if not f.const]:
89 if f.name is None: # Anonynous struct
90 s += libxl_C_type_destroy(f.type, deref, False, "", deref)
91 else:
92 s += libxl_C_type_destroy(f.type, deref + f.name, False, "", deref)
93 else:
94 if ty.destructor_fn is not None:
95 s += "%s(%s);\n" % (ty.destructor_fn, makeref + v)
97 if s != "":
98 s = indent + s
99 return s.replace("\n", "\n%s" % indent).rstrip(indent)
101 if __name__ == '__main__':
102 if len(sys.argv) < 4:
103 print >>sys.stderr, "Usage: gentypes.py <idl> <header> <implementation>"
104 sys.exit(1)
106 idl = sys.argv[1]
107 (_,types) = libxltypes.parse(idl)
109 header = sys.argv[2]
110 print "outputting libxl type definitions to %s" % header
112 f = open(header, "w")
114 f.write("""#ifndef __LIBXL_TYPES_H
115 #define __LIBXL_TYPES_H
117 /*
118 * DO NOT EDIT.
119 *
120 * This file is autogenerated by
121 * "%s"
122 */
124 """ % " ".join(sys.argv))
126 for ty in types:
127 f.write(libxl_C_type_define(ty) + ";\n")
128 if ty.destructor_fn is not None:
129 f.write("void %s(%s *p);\n" % (ty.destructor_fn, ty.typename))
130 f.write("\n")
132 f.write("""#endif /* __LIBXL_TYPES_H */\n""")
133 f.close()
135 impl = sys.argv[3]
136 print "outputting libxl type implementations to %s" % impl
138 f = open(impl, "w")
139 f.write("""
140 /* DO NOT EDIT.
141 *
142 * This file is autogenerated by
143 * "%s"
144 */
146 #include "libxl_osdeps.h"
148 #include <stdint.h>
149 #include <stdlib.h>
150 #include <string.h>
152 #include "libxl.h"
154 #define LIBXL_DTOR_POISON 0xa5
156 """ % " ".join(sys.argv))
158 for ty in [t for t in types if t.destructor_fn is not None and t.autogenerate_destructor]:
159 f.write("void %s(%s *p)\n" % (ty.destructor_fn, ty.typename))
160 f.write("{\n")
161 f.write(libxl_C_type_destroy(ty, "p", True))
162 f.write(" memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n")
163 f.write("}\n")
164 f.write("\n")
165 f.close()