1 | #!/usr/bin/python |
---|
2 | |
---|
3 | import sys, re; |
---|
4 | from structs import structs, defines; |
---|
5 | |
---|
6 | # command line arguments |
---|
7 | arch = sys.argv[1]; |
---|
8 | outfile = sys.argv[2]; |
---|
9 | infiles = sys.argv[3:]; |
---|
10 | |
---|
11 | |
---|
12 | ########################################################################### |
---|
13 | # configuration #2: architecture information |
---|
14 | |
---|
15 | inttypes = {}; |
---|
16 | header = {}; |
---|
17 | footer = {}; |
---|
18 | |
---|
19 | # x86_32 |
---|
20 | inttypes["x86_32"] = { |
---|
21 | "unsigned long" : "uint32_t", |
---|
22 | "long" : "uint32_t", |
---|
23 | "xen_pfn_t" : "uint32_t", |
---|
24 | }; |
---|
25 | header["x86_32"] = """ |
---|
26 | #define __i386___X86_32 1 |
---|
27 | #pragma pack(4) |
---|
28 | """; |
---|
29 | footer["x86_32"] = """ |
---|
30 | #pragma pack() |
---|
31 | """; |
---|
32 | |
---|
33 | # x86_64 |
---|
34 | inttypes["x86_64"] = { |
---|
35 | "unsigned long" : "__align8__ uint64_t", |
---|
36 | "long" : "__align8__ uint64_t", |
---|
37 | "xen_pfn_t" : "__align8__ uint64_t", |
---|
38 | }; |
---|
39 | header["x86_64"] = """ |
---|
40 | #ifdef __GNUC__ |
---|
41 | # define __DECL_REG(name) union { uint64_t r ## name, e ## name; } |
---|
42 | # define __align8__ __attribute__((aligned (8))) |
---|
43 | #else |
---|
44 | # define __DECL_REG(name) uint64_t r ## name |
---|
45 | # define __align8__ FIXME |
---|
46 | #endif |
---|
47 | #define __x86_64___X86_64 1 |
---|
48 | """; |
---|
49 | |
---|
50 | # ia64 |
---|
51 | inttypes["ia64"] = { |
---|
52 | "unsigned long" : "__align8__ uint64_t", |
---|
53 | "long" : "__align8__ uint64_t", |
---|
54 | "xen_pfn_t" : "__align8__ uint64_t", |
---|
55 | "long double" : "__align16__ ldouble_t", |
---|
56 | }; |
---|
57 | header["ia64"] = """ |
---|
58 | #define __align8__ __attribute__((aligned (8))) |
---|
59 | #define __align16__ __attribute__((aligned (16))) |
---|
60 | typedef unsigned char ldouble_t[16]; |
---|
61 | """; |
---|
62 | |
---|
63 | |
---|
64 | ########################################################################### |
---|
65 | # main |
---|
66 | |
---|
67 | input = ""; |
---|
68 | output = ""; |
---|
69 | fileid = re.sub("[-.]", "_", "__FOREIGN_%s__" % outfile.upper()); |
---|
70 | |
---|
71 | # read input header files |
---|
72 | for name in infiles: |
---|
73 | f = open(name, "r"); |
---|
74 | input += f.read(); |
---|
75 | f.close(); |
---|
76 | |
---|
77 | # add header |
---|
78 | output += """ |
---|
79 | /* |
---|
80 | * public xen defines and struct for %s |
---|
81 | * generated by %s -- DO NOT EDIT |
---|
82 | */ |
---|
83 | |
---|
84 | #ifndef %s |
---|
85 | #define %s 1 |
---|
86 | |
---|
87 | """ % (arch, sys.argv[0], fileid, fileid) |
---|
88 | |
---|
89 | if arch in header: |
---|
90 | output += header[arch]; |
---|
91 | output += "\n"; |
---|
92 | |
---|
93 | # add defines to output |
---|
94 | for line in re.findall("#define[^\n]+", input): |
---|
95 | for define in defines: |
---|
96 | regex = "#define\s+%s\\b" % define; |
---|
97 | match = re.search(regex, line); |
---|
98 | if None == match: |
---|
99 | continue; |
---|
100 | if define.upper()[0] == define[0]: |
---|
101 | replace = define + "_" + arch.upper(); |
---|
102 | else: |
---|
103 | replace = define + "_" + arch; |
---|
104 | regex = "\\b%s\\b" % define; |
---|
105 | output += re.sub(regex, replace, line) + "\n"; |
---|
106 | output += "\n"; |
---|
107 | |
---|
108 | # delete defines, comments, empty lines |
---|
109 | input = re.sub("#define[^\n]+\n", "", input); |
---|
110 | input = re.compile("/\*(.*?)\*/", re.S).sub("", input) |
---|
111 | input = re.compile("\n\s*\n", re.S).sub("\n", input); |
---|
112 | |
---|
113 | # add structs to output |
---|
114 | for struct in structs: |
---|
115 | regex = "struct\s+%s\s*\{(.*?)\n\};" % struct; |
---|
116 | match = re.search(regex, input, re.S) |
---|
117 | if None == match: |
---|
118 | output += "#define %s_has_no_%s 1\n" % (arch, struct); |
---|
119 | else: |
---|
120 | output += "struct %s_%s {%s\n};\n" % (struct, arch, match.group(1)); |
---|
121 | output += "typedef struct %s_%s %s_%s_t;\n" % (struct, arch, struct, arch); |
---|
122 | output += "\n"; |
---|
123 | |
---|
124 | # add footer |
---|
125 | if arch in footer: |
---|
126 | output += footer[arch]; |
---|
127 | output += "\n"; |
---|
128 | output += "#endif /* %s */\n" % fileid; |
---|
129 | |
---|
130 | # replace: defines |
---|
131 | for define in defines: |
---|
132 | if define.upper()[0] == define[0]: |
---|
133 | replace = define + "_" + arch.upper(); |
---|
134 | else: |
---|
135 | replace = define + "_" + arch; |
---|
136 | output = re.sub("\\b%s\\b" % define, replace, output); |
---|
137 | |
---|
138 | # replace: structs + struct typedefs |
---|
139 | for struct in structs: |
---|
140 | output = re.sub("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output); |
---|
141 | output = re.sub("\\b(%s)_t\\b" % struct, "\\1_%s_t" % arch, output); |
---|
142 | |
---|
143 | # replace: integer types |
---|
144 | integers = inttypes[arch].keys(); |
---|
145 | integers.sort(lambda a, b: cmp(len(b),len(a))); |
---|
146 | for type in integers: |
---|
147 | output = re.sub("\\b%s\\b" % type, inttypes[arch][type], output); |
---|
148 | |
---|
149 | # print results |
---|
150 | f = open(outfile, "w"); |
---|
151 | f.write(output); |
---|
152 | f.close; |
---|
153 | |
---|