-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate.py
273 lines (230 loc) · 7.73 KB
/
generate.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import binascii
import json
from array import array
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-j", "--json", dest="jsonfile", default="dyncalls.json",
help="read JSON from FILE", metavar="FILE")
parser.add_argument("-o", "--output", dest="output",
help="write generated prototypes to FILE", metavar="FILE")
parser.add_argument("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="print status messages to stdout")
parser.add_argument("-P", "--print-sources",
action="store_true", dest="print_sources", default=False,
help="print generated sources to stdout")
parser.add_argument('--dyncall', dest='dyncall', action='store', default=504,
help='set the dyncall system call number')
parser.add_argument('--cpp', dest='cpp', action='store_true', default=False,
help='generate a .cpp file in addition to the .c file')
args = parser.parse_args()
# this is the plain CRC32 polynomial
poly = 0xEDB88320
# we need to be able to calculate CRC32 using any poly
table = array('L')
for byte in range(256):
crc = 0
for bit in range(8):
if (byte ^ crc) & 1:
crc = (crc >> 1) ^ poly
else:
crc >>= 1
byte >>= 1
table.append(crc)
def crc32(string):
value = 0xffffffff
for ch in string:
value = table[(ord(ch) ^ value) & 0xff] ^ (value >> 8)
return -1 - value
def is_type(string):
keywords = ["unsigned", "char", "short", "int", "long", "float", "double", "size_t", "int8_t", "uint8_t", "int16_t", "uint16_t", "int32_t", "uint32_t", "int64_t", "uint64_t"]
conventions = ("_callback", "_t")
return ("*" in string) or string in keywords or string.endswith(conventions)
def is_simple_return(string):
keywords = ["*", "void", "unsigned", "char", "short", "int", "long", "size_t", "int8_t", "uint8_t", "int16_t", "uint16_t", "int32_t", "uint32_t", "int64_t", "uint64_t"]
return any(substring in string for substring in keywords)
def find_arguments(string):
sfront = string.split('(', 1)
retval = [sfront[0].split(' ')[0]]
strargs = sfront[1].split(')')[0]
# [retval, arg0, arg1, '']
fargs = retval + strargs.split(", ")
# Remove parameter names
for (idx, arg) in enumerate(fargs):
symbols = arg.split(" ")
if len(symbols) > 1:
last = symbols[-1]
if not is_type(last):
symbols.pop()
fargs[idx] = " ".join(symbols)
# Remove empty argument lists
if fargs[-1] == "":
fargs.pop()
return fargs
def emit_inline_assembly(header, asmdef, index, fargs):
retval = fargs[0]
fargs.pop(0)
if not is_simple_return(retval):
return (header, False)
has_output = (retval != "void")
inputs = []
output = []
argno = 0
areg = 0
freg = 0
asm_regs = ""
asm_in = []
asm_out = []
asm_clob = []
if has_output:
asm_regs += "register " + retval + " ra0 __asm__(\"a0\");\n"
asm_out = ["\"=r\"(ra0)"]
asm_clob += []
for arg in fargs:
if "float" in arg or "double" in arg:
# floating-point registers
reg = "fa" + str(freg)
freg += 1
else:
# integral registers
reg = "a" + str(areg)
areg += 1
asm_regs += "register " + arg + " " + reg + " __asm__(\"" + reg + "\") = arg" + str(argno) + ";\n"
# strings
if "*" in arg:
asm_in += ["\"r\"(" + reg + ")"]
if "void" in arg:
asm_in += ["\"m\"(*(char *)arg" + str(argno) + ")"]
else:
asm_in += ["\"m\"(*arg" + str(argno) + ")"]
# floats
elif "float" in arg or "double" in arg:
asm_in += ["\"f\"(" + reg + ")"]
# integrals
else:
asm_in += ["\"r\"(" + reg + ")"]
fargs[argno] = arg + " arg" + str(argno)
argno += 1
asm_in += []
header += "static inline __attribute__((always_inline, optimize(\"O2\"))) " + retval + " i" + asmdef + " (" + ','.join(fargs) + ') {\n'
header += asm_regs
header += '__asm__ volatile(\".insn i 0b1011011, 0, x0, x0, ' + str(index) + "\"" \
+ " : " + ",".join(asm_out) + " : " + ",".join(asm_in) + " : " + ",".join(asm_clob) + ");\n"
if has_output:
header += "return ra0;\n"
header += "}" + '\n'
return (header, True)
# load JSON
j = {}
with open(args.jsonfile) as f:
j = json.load(f)
# List of client-side only dyncalls
client_side = []
if "clientside" in j:
for key in j["clientside"]:
client_side.append(key)
# List of server-side only dyncalls
server_side = []
if "serverside" in j:
for key in j["serverside"]:
server_side.append(key)
# List of initialization-only dyncalls
initialization = []
if "initialization" in j:
for key in j["initialization"]:
initialization.append(key)
header = """
#pragma once
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
""";
# iterate typedefs first
for key in j:
if key == "typedef":
for typedef in j[key]:
header += typedef + ";\n"
header += "\n"
source = '__asm__(".section .text\\n");\n\n'
dyncallindex = 0
dyncall = ""
# create dyncall prototypes and assembly
for key in j:
if key == "typedef" or key == "clientside" or key == "serverside" or key == "initialization":
continue
else:
asmdef = " ".join(j[key].split())
asmname = asmdef.split(' ')[1]
fargs = find_arguments(asmdef)
crcval = crc32(asmdef) & 0xffffffff
crc = '%08x' % crcval
header += "// " + key + ": 0x" + crc + "\n"
header += "extern " + asmdef + ";\n"
## Given the parsed arguments, starting with
## the return value, we can produce perfect
## inline assembly that allows the compiler
## room to optimize better.
(header, inlined) = emit_inline_assembly(header, asmname, dyncallindex, fargs)
if args.verbose:
print("Dynamic call: " + key + ", hash 0x" + crc + (" (inlined)" if inlined else ""))
# Each dynamic call has a table index where the name and hash is stored
dyncall += ' .long 0x' + crc + '\\n\\\n'
dyncall += ' .long ' + str(0) + '\\n\\\n'
dyncall += ' .long ' + asmname + '_str\\n\\\n'
# Flags (one byte each for client-side, server-side, initialization, and padding)
is_client_side = key in client_side
is_server_side = key in server_side
is_initialization = key in initialization
dyncall += ' .byte ' + str(int(is_initialization)) + '\\n\\\n'
dyncall += ' .byte ' + str(int(is_client_side)) + '\\n\\\n'
dyncall += ' .byte ' + str(int(is_server_side)) + '\\n\\\n'
dyncall += ' .byte 0\\n\\\n'
# These dynamic calls use the table indexed variant
# Each dynamic call has a table index where the name and hash is stored
# and at run-time this value is lazily resolved
source += '__asm__("\\n\\\n'
source += '.global ' + asmname + '\\n\\\n'
source += '.func ' + asmname + '\\n\\\n'
source += asmname + ':\\n\\\n'
source += ' .insn i 0b1011011, 0, x0, x0, ' + str(dyncallindex) + '\\n\\\n'
source += ' ret\\n\\\n'
source += '.endfunc\\n\\\n'
source += '.pushsection .rodata\\n\\\n'
source += asmname + '_str:\\n\\\n'
source += '.asciz \\\"' + key + '\\\"\\n\\\n'
source += '.popsection\\n\\\n'
source += '");\n\n'
dyncallindex += 1
header += """
#ifdef __cplusplus
}
#endif
"""
dyncall_header = '__asm__("\\n\\\n'
dyncall_header += '.pushsection .rodata\\n\\\n'
dyncall_header += '.align 8\\n\\\n'
dyncall_header += '.global dyncall_table\\n\\\n'
dyncall_header += 'dyncall_table:\\n\\\n'
dyncall_header += ' .long ' + str(dyncallindex) + '\\n\\\n'
dyncall = dyncall_header + dyncall
dyncall += '.popsection\\n\\\n'
dyncall += '");\n\n'
source += dyncall
if (args.verbose):
print("* There are " + str(dyncallindex) + " dynamic calls")
if (args.print_sources):
print(header)
print(source)
if (args.output):
with open(args.output + ".h", "w") as hdrfile:
hdrfile.write(header)
with open(args.output + ".c", "w") as srcfile:
srcfile.write(source)
if args.cpp:
with open(args.output + ".cpp", "w") as srcfile:
srcfile.write(source)
exit(0)
else:
exit(1)