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
226
227
228
229
230
231
232
233
|
#!/usr/bin/python
from __future__ import print_function
import sys, re;
from structs import unions, structs, defines;
# command line arguments
arch = sys.argv[1];
outfile = sys.argv[2];
infiles = sys.argv[3:];
###########################################################################
# configuration #2: architecture information
inttypes = {};
header = {};
footer = {};
#arm
inttypes["arm32"] = [
("unsigned long", "__danger_unsigned_long_on_arm32"),
("long", "__danger_long_on_arm32"),
("xen_pfn_t", "uint64_t"),
("xen_ulong_t", "uint64_t"),
("uint64_t", "__align8__ uint64_t"),
]
header["arm32"] = """
#define __arm___ARM32 1
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# define __DECL_REG(n64, n32) union { uint64_t n64; uint32_t n32; }
# define __align8__ __attribute__((aligned (8)))
#else
# define __DECL_REG(n64, n32) uint64_t n64
# define __align8__ FIXME
#endif
""";
footer["arm32"] = """
#undef __DECL_REG
"""
inttypes["arm64"] = [
("unsigned long", "__danger_unsigned_long_on_arm64"),
("long", "__danger_long_on_arm64"),
("xen_pfn_t", "uint64_t"),
("xen_ulong_t", "uint64_t"),
("uint64_t", "__align8__ uint64_t"),
]
header["arm64"] = """
#define __aarch64___ARM64 1
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# define __DECL_REG(n64, n32) union { uint64_t n64; uint32_t n32; }
# define __align8__ __attribute__((aligned (8)))
#else
# define __DECL_REG(n64, n32) uint64_t n64
# define __align8__ FIXME
#endif
""";
footer["arm64"] = """
#undef __DECL_REG
"""
# x86_32
inttypes["x86_32"] = [
("unsigned long", "uint32_t"),
("long", "uint32_t"),
("xen_pfn_t", "uint32_t"),
("xen_ulong_t", "uint32_t"),
]
header["x86_32"] = """
#define __DECL_REG_LO8(which) uint32_t e ## which ## x
#define __DECL_REG_LO16(name) uint32_t e ## name
#define __i386___X86_32 1
#pragma pack(4)
""";
footer["x86_32"] = """
#undef __DECL_REG_LO8
#undef __DECL_REG_LO16
#pragma pack()
""";
# x86_64
inttypes["x86_64"] = [
("unsigned long", "__align8__ uint64_t"),
("long", "__align8__ uint64_t"),
("xen_pfn_t", "__align8__ uint64_t"),
("xen_ulong_t", "__align8__ uint64_t"),
]
header["x86_64"] = """
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# define __DECL_REG(name) union { uint64_t r ## name, e ## name; }
# define __align8__ __attribute__((aligned (8)))
#else
# define __DECL_REG(name) uint64_t r ## name
# define __align8__ FIXME
#endif
#define __DECL_REG_LOHI(name) __DECL_REG(name ## x)
#define __DECL_REG_LO8 __DECL_REG
#define __DECL_REG_LO16 __DECL_REG
#define __DECL_REG_HI __DECL_REG
#define __x86_64___X86_64 1
""";
footer["x86_64"] = """
#undef __DECL_REG
#undef __DECL_REG_LOHI
#undef __DECL_REG_LO8
#undef __DECL_REG_LO16
#undef __DECL_REG_HI
"""
###########################################################################
# main
input = "";
output = "";
fileid = re.sub("[-.]", "_", "__FOREIGN_%s__" % outfile.upper());
for name in infiles:
f = open(name, "r");
# Sanity check the licence of the input file(s)
line = f.readline()
if line != "/* SPDX-License-Identifier: MIT */\n":
print("Error: %s %s Missing or unexpected SPDX tag '%s'" %
(sys.argv[0], name, line.strip()), file=sys.stderr)
exit(1)
input += f.read();
f.close();
# replace path in "infiles" by path in '/usr/include' to avoid exposing the
# build directory path in the generated headers.
headers_name_list = ""
public_headers_location = 'xen/include/public/'
for name in infiles:
i = name.rindex(public_headers_location)
i += len(public_headers_location)
headers_name_list += " xen/%s" % (name[i:])
# add header
output += """/* SPDX-License-Identifier: MIT */
/*
* public xen defines and struct for %s
* generated from%s by %s -- DO NOT EDIT
*/
#ifndef %s
#define %s 1
""" % (arch, headers_name_list, sys.argv[0], fileid, fileid)
if arch in header:
output += header[arch];
output += "\n";
defined = {}
# add defines to output
for line in re.findall("#define[^\n]+", input):
for define in defines:
regex = "#define\s+%s\\b" % define;
match = re.search(regex, line);
if None == match:
continue;
defined[define] = 1
if define.upper()[0] == define[0]:
replace = define + "_" + arch.upper();
else:
replace = define + "_" + arch;
regex = "\\b%s\\b" % define;
output += re.sub(regex, replace, line) + "\n";
output += "\n";
# delete defines, comments, empty lines
input = re.sub("#define[^\n]+\n", "", input);
input = re.compile("/\*(.*?)\*/", re.S).sub("", input)
input = re.compile("\n\s*\n", re.S).sub("\n", input);
# add unions to output
for union in unions:
regex = "union\s+%s\s*\{(.*?)\n\};" % union;
match = re.search(regex, input, re.S)
if None == match:
output += "#define %s_has_no_%s 1\n" % (arch, union);
else:
output += "union %s_%s {%s\n};\n" % (union, arch, match.group(1));
output += "\n";
# add structs to output
for struct in structs:
regex = "(?:#ifdef ([A-Z_]+))?\nstruct\s+%s\s*\{(.*?)\n\};" % struct;
match = re.search(regex, input, re.S)
if None == match or \
(match.group(1) is not None and match.group(1) not in defined):
output += "#define %s_has_no_%s 1\n" % (arch, struct);
else:
output += "struct %s_%s {%s\n};\n" % (struct, arch, match.group(2));
output += "typedef struct %s_%s %s_%s_t;\n" % (struct, arch, struct, arch);
output += "\n";
# add footer
if arch in footer:
output += footer[arch];
output += "\n";
output += "#endif /* %s */\n" % fileid;
# replace: defines
for define in defines:
if define.upper()[0] == define[0]:
replace = define + "_" + arch.upper();
else:
replace = define + "_" + arch;
output = re.sub("\\b%s\\b" % define, replace, output);
# replace: unions
for union in unions:
output = re.sub("\\b(union\s+%s)\\b" % union, "\\1_%s" % arch, output);
# replace: structs + struct typedefs
for struct in structs:
output = re.sub("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output);
output = re.sub("\\b(%s)_t\\b" % struct, "\\1_%s_t" % arch, output);
# replace: integer types
for old, new in inttypes[arch]:
output = re.sub("\\b%s\\b" % old, new, output)
# print results
f = open(outfile, "w");
f.write(output);
f.close;
|