-
Notifications
You must be signed in to change notification settings - Fork 1
/
micropython-ffigen
executable file
·211 lines (183 loc) · 6.43 KB
/
micropython-ffigen
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
#!/usr/bin/env python3
import sys
import json
import re
comment_level = 0
FFI_TYPE_MAP = {
":void": "v",
":int": "i",
":signed-char": "b",
":unsigned-char": "B",
":short": "h",
":unsigned-short": "H",
":int": "i",
":unsigned-int": "I",
":long": "l",
":unsigned-long": "L",
":long-long": "q",
":unsigned-long-long": "Q",
":enum": "i",
":float": "f",
":double": "d",
"uint8_t": "B",
"uint16_t": "H",
"uint32_t": "I",
"ssize_t": "l",
}
UCTYPES_TYPE_MAP = {
":void": "uctypes.VOID",
":char": "uctypes.INT8",
":signed-char": "uctypes.INT8",
":unsigned-char": "uctypes.UINT8",
":short": "uctypes.INT16",
":unsigned-short": "uctypes.UINT16",
":int": "uctypes.INT32",
":unsigned-int": "uctypes.UINT32",
":long": "uctypes.INT32",
":unsigned-long": "uctypes.UINT32",
":long-long": "uctypes.INT64",
":unsigned-long-long": "uctypes.UINT64",
":enum": "uctypes.INT32",
":float": "uctypes.FLOAT",
":double": "uctypes.DOUBLE",
"uint8_t": "uctypes.UINT8",
"uint16_t": "uctypes.UINT16",
"uint32_t": "uctypes.UINT32",
}
def matches_filters(loc, proj):
process = False
if "include" in proj:
for pat in proj["include"]:
if re.search(pat, loc):
process = True
break
return process
def conv_ffi_type(rec):
if rec["tag"] == ":pointer":
return "p"
if rec["tag"] == ":array":
return "p"
if rec["tag"] in FFI_TYPE_MAP:
return FFI_TYPE_MAP[rec["tag"]]
assert 0, rec["tag"]
def conv_uctypes_type(rec, offset):
if rec["tag"] in UCTYPES_TYPE_MAP:
v = UCTYPES_TYPE_MAP[rec["tag"]]
if "%d" in v:
return v % offset
else:
return "%s | %d" % (v, offset)
if rec["tag"] == ":pointer":
if rec["type"]["tag"] == ":struct":
return "(uctypes.PTR | %d, %s)" % (offset, rec["type"]["name"])
elif rec["type"]["tag"] == ":pointer":
return "(uctypes.PTR | %d, %s)" % (offset, "uctypes.VOID")
else:
if rec["type"]["tag"] not in UCTYPES_TYPE_MAP:
assert 0, rec
return "(uctypes.PTR | %d, %s)" % (offset, UCTYPES_TYPE_MAP[rec["type"]["tag"]])
if rec["tag"] == ":array":
if rec["type"]["tag"] == ":function-pointer":
return "(uctypes.ARRAY | %d, %d, (uctypes.PTR,)) # TODO: array of func-ptr" % \
(offset, rec["size"])
return "(uctypes.ARRAY | %d, %d, %s)" % (offset, rec["size"], UCTYPES_TYPE_MAP[rec["type"]["tag"]])
if rec["tag"] == ":function-pointer":
return "(uctypes.PTR | %d, %s) # TODO: func-ptr" % (offset, "uctypes.VOID")
if rec["tag"] == "union":
return "# TODO: union"
assert 0, rec["tag"]
def handle_typedef(desc, should_output=False):
if desc["type"]["tag"] in (":struct", "struct"):
FFI_TYPE_MAP[desc["name"]] = desc["type"]["name"]
UCTYPES_TYPE_MAP[desc["name"]] = desc["type"]["name"]
if comment_level > 1:
print("# typedef struct %s" % desc["name"])
elif desc["type"]["tag"] in (":union", "union"):
FFI_TYPE_MAP[desc["name"]] = desc["type"]["name"]
UCTYPES_TYPE_MAP[desc["name"]] = desc["type"]["name"]
if comment_level > 1:
print("# typedef union %s" % desc["name"])
elif desc["type"]["tag"] == ":pointer":
FFI_TYPE_MAP[desc["name"]] = "p"
# TODO
UCTYPES_TYPE_MAP[desc["name"]] = "(uctypes.PTR | %d, uctypes.VOID)"
if comment_level > 1:
print("# typedef ptr %s" % desc["name"])
elif desc["type"]["tag"] == ":function-pointer":
FFI_TYPE_MAP[desc["name"]] = "C"
UCTYPES_TYPE_MAP[desc["name"]] = "(uctypes.PTR | %d, uctypes.VOID)"
if comment_level > 1:
print("# typedef func-ptr %s" % desc["name"])
else:
FFI_TYPE_MAP[desc["name"]] = FFI_TYPE_MAP[desc["type"]["tag"]]
UCTYPES_TYPE_MAP[desc["name"]] = UCTYPES_TYPE_MAP[desc["type"]["tag"]]
if comment_level > 1:
print("# typedef %s" % desc["name"])
if len(sys.argv) < 3 or len(sys.argv) > 4:
print("usage: %s <proj>.json <c2ffi>.json [<c2ffi-macro>.h]" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1]) as f:
proj = json.load(f)
with open(sys.argv[2]) as f:
data = json.load(f)
print("import ffi")
print("import uctypes")
print()
print('l = ffi.open("%s")' % proj["lib"])
print()
for desc in data:
loc = desc["location"]
process = matches_filters(loc, proj)
# We need to collect all types for future references
if desc["tag"] == "typedef":
# print("Processing %s" % loc)
handle_typedef(desc, process)
continue
if not process:
if comment_level > 0:
print("# Ignoring %s" % loc)
continue
if comment_level > 0:
print("# Processing %s" % loc)
if desc["tag"] == "function":
params = [conv_ffi_type(x["type"]) for x in desc["parameters"]]
s = '%s = l.func("%s", "%s", "%s")' % (desc["name"], conv_ffi_type(desc["return-type"]), desc["name"], "".join(params))
if desc.get("inline", False):
print("#%s" % s)
else:
print(s)
elif desc["tag"] == "enum":
print("# %s" % desc["name"])
for f in desc["fields"]:
print("%s = %s" % (f["name"], f["value"]))
elif desc["tag"] == "struct":
print("%s = {" % desc["name"])
for f in desc["fields"]:
print(' "%s": %s,' % (f["name"], conv_uctypes_type(f["type"], f["bit-offset"] / 8)))
# print(f)
if desc["bit-size"] != 0:
# This doesn't have to be true due to alignment
#assert desc["bit-size"] == f["bit-offset"] + f["bit-size"]
pass
# print(desc)
print("}")
else:
print(desc)
assert 0, desc["tag"]
pass
if len(sys.argv) > 3:
with open(sys.argv[3]) as f:
print("\n# Constants\n")
items = []
for l in f:
if l.startswith("/* "):
loc = l[3:-4]
elif l.startswith("#define "):
if matches_filters(loc, proj):
_, name, val = l.rstrip().split(" ", 2)
loc = loc.split(":")
items.append((loc[0], int(loc[1]), name, val))
items.sort()
for fname, line, name, val in items:
#print("#%s:%s" % (fname, line))
print("%s = %s" % (name, val))