-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
324 lines (241 loc) · 12.8 KB
/
main.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/python3
import argparse
from contextlib import redirect_stdout
import os
import sys
import toml
def get_toml_from_file(filename):
"""Returns the TOML structure for the given filename."""
with open(filename, 'r') as f:
parsed_toml = toml.load(f)
if len(parsed_toml) != 0:
return parsed_toml
return None
def generate_c_code(filename, output):
"""Create header file for joint allocated structures in the TOML file."""
parsed_toml = get_toml_from_file(filename)
if parsed_toml is None:
return
if output:
with open(output, 'w') as f:
with redirect_stdout(f):
print_data_structs(parsed_toml, f.name)
else:
print_data_structs(parsed_toml)
def print_data_structs(parsed_toml, output='joint_memory.h'):
"""Write header file with function declarations and library imports"""
filename = os.path.basename(output)
header_name = filename.upper().replace('.', '_')
# Single include pragma prologue
print("/* Code generated by \"ordnat\". DO NOT EDIT. */")
print("#ifndef __{0}__".format(header_name))
print("#define __{0}__\n".format(header_name))
# Create library imports
print("#include <stdlib.h>")
print("#include <stdint.h>")
print("#include <string.h>")
print("#include <assert.h>")
print("#include \"ordnat.h\"")
if parsed_toml['header']:
for library in parsed_toml['header']['lib']:
print("#include {0}".format(library))
# Generate data structure with joint memory
for (struct_name, struct_members) in parsed_toml['data'].items():
field_count = len(struct_members)
if field_count == 0:
print("[WARN] Struct {0} has no data members".format(struct_name), file=sys.stderr)
continue
print("\n/* {0} structure with joint memory allocation */".format(struct_name))
print("struct joint_{0} {{".format(struct_name))
for (i, field) in enumerate(struct_members):
print(" {0} *{1};".format(field['type'], field['name']))
print("\n void *memory_block;")
print(" uint32_t offset[{0}];".format(field_count))
print("};\n")
# Generate free method
print_free_method(struct_name, field_count, struct_members)
# Generate allocation methods
print_alloc_method(struct_name, struct_members)
print_alloc_soa_method(struct_name, struct_members)
# Generate realloc method
print_realloc_method(struct_name, struct_members)
# Generate copy method
print_copy_method(struct_name, field_count, struct_members)
# Generate foreach macros
print_foreach_macros(struct_name, struct_members)
# Header file epilogue
print("#endif /* __{0}__ */".format(header_name))
def print_foreach_macros(struct_name, struct_members):
"""Generate macros to safely iterate through range of each field in joint data structure."""
print("/* Macros for joint_{0} data iteration. */\n".format(struct_name))
for (i, field) in enumerate(struct_members):
print("/* Macros for {0} field iteration. */\n".format(field['name']))
print("#define foreach_joint_{0}_{1}(joint_name, item_name, loop) \\".format(struct_name, field['name']))
print(" foreach_joint(joint_name, item_name, {0}, {1}, loop)\n".format(field['name'], i))
print("#define foreach_range_joint_{0}_{1}(joint_name, item_name, itr, loop) \\".format(
struct_name, field['name']
))
print(" foreach_range_joint(joint_name, item_name, {0}, itr, {1}, loop)\n".format(field['name'], i))
print("#define for_joint_{0}_{1}(joint_name, item_name, start, end, itr, loop) \\".format(
struct_name, field['name']
))
print(" for_range_joint(joint_name, item_name, {0}, start, end, step, itr, {1}, loop)\n".format(field['name'], i))
print("#define for_range_joint_{0}_{1}(joint_name, item_name, start, end, itr, loop) \\".format(
struct_name, field['name']
))
print(" for_range_joint(joint_name, item_name, {0}, start, end, 1, itr, {1}, loop)\n".format(field['name'], i))
print("#define for_down_joint_{0}_{1}(joint_name, item_name, start, end, itr, loop) \\".format(
struct_name, field['name']
))
print(" for_range_down_joint(joint_name, item_name, {0}, start, end, step, itr, loop)\n".format(field['name']))
print("#define for_range_down_joint_{0}_{1}(joint_name, item_name, start, end, itr, loop) \\".format(
struct_name, field['name']
))
print(" for_range_down_joint(joint_name, item_name, {0}, start, end, 1, itr, loop)\n".format(field['name']))
def print_free_method(struct_name, field_count, struct_members):
"""Generate function to deallocate joint data structure."""
print("static inline void free_joint_{0}(void (*mem_dealloc)(void *), struct joint_{0} *m_joint) {{".format(struct_name))
print(" assert(((void) \"m_joint cannot be null\", m_joint != NULL));")
print(" mem_dealloc(m_joint->memory_block);")
if field_count:
print()
for field in struct_members:
print(" m_joint->{0} = NULL;".format(field['name']))
print("\n for (int i = 0; i < {0}; i++) {{".format(field_count))
print(" m_joint->offset[i] = 0;")
print(" }")
print("}\n")
def print_alloc_method(struct_name, struct_members):
"""Generate function to allocate joint data structure."""
field_params = [('n_' + field['name']) for field in struct_members]
param_str = ', '.join(["size_t {0}".format(i) for i in field_params])
print("static inline struct joint_{0} alloc_joint_{0}(void *(*mem_alloc)(size_t), {1}) {{".format(struct_name, param_str))
for i in range(len(struct_members)):
print(" assert(((void) \"{0} cannot be 0\", {0} > 0));".format(field_params[i]))
print("\n uint32_t current_offset = 0;")
print(" struct joint_{0} data = {{ 0 }};\n".format(struct_name))
for (i, field) in enumerate(struct_members):
if i > 0:
print(" data.{0} = (void *) (uintptr_t)current_offset;".format(field['name']))
print(" current_offset += ((sizeof({0})) * ({1}));".format(field['type'], field_params[i]))
print(" data.offset[{0}] = current_offset;".format(i))
print()
print(" data.memory_block = mem_alloc(current_offset);")
print(" assert(((void) \"memory_block cannot be NULL\", data.memory_block != NULL));\n")
for (i, field) in enumerate(struct_members):
if i > 0:
print(" data.{0} = (void *)(((uintptr_t) data.{0}) + ((uintptr_t) data.memory_block));".format(field['name']))
else:
print(" data.{0} = ({1} *)(data.memory_block);".format(field['name'], field['type']))
print("\n return data;")
print("}\n")
def print_alloc_soa_method(struct_name, struct_members):
"""Generate function to allocate uniform SOA joint data structure."""
field_param = "n_{0}s".format(struct_name)
print("static inline struct joint_{0} alloc_joint_{0}_soa(void *(*mem_alloc)(size_t), size_t {1}) {{".format(struct_name, field_param))
print(" assert(((void) \"{0} cannot be 0\", {0} > 0));".format(field_param))
print("\n uint32_t current_offset = 0;")
print(" struct joint_{0} data_soa = {{ 0 }};\n".format(struct_name))
for (i, field) in enumerate(struct_members):
if i > 0:
print(" data_soa.{0} = (void *) (uintptr_t)current_offset;".format(field['name']))
print(" current_offset += ((sizeof({0})) * ({1}));".format(field['type'], field_param))
print(" data_soa.offset[{0}] = current_offset;".format(i))
print()
print(" data_soa.memory_block = mem_alloc(current_offset);")
print(" assert(((void) \"memory_block cannot be NULL\", data_soa.memory_block != NULL));\n")
for (i, field) in enumerate(struct_members):
if i > 0:
print(" data_soa.{0} = (void *)(((uintptr_t) data_soa.{0}) + ((uintptr_t) data_soa.memory_block));".format(field['name']))
else:
print(" data_soa.{0} = ({1} *)(data_soa.memory_block);".format(field['name'], field['type']))
print("\n return data_soa;")
print("}\n")
def print_realloc_method(struct_name, struct_members):
"""Generate function to reallocate joint data structure."""
field_params = [('n_' + field['name']) for field in struct_members]
param_str = ', '.join(["size_t {0}".format(i) for i in field_params])
compound_field_check = " && ".join(["{0} <= 0".format('n_' + field['name']) for field in struct_members])
print("static inline struct joint_{0} realloc_joint_{0}(void *(*mem_alloc)(size_t), void (*mem_dealloc)(void *), struct joint_{0} *m_joint, {1}) {{".format(struct_name, param_str))
print(" if ({0}) {{".format(compound_field_check))
print(" free_joint_{0}(mem_dealloc, m_joint);".format(struct_name))
print(" return *m_joint;")
print(" }\n")
for i in range(len(struct_members)):
print(" assert(((void) \"{0} cannot be 0\", {0} > 0));".format(field_params[i]))
print("\n uint32_t current_offset = 0;")
print(" struct joint_{0} data = {{ 0 }};\n".format(struct_name))
for (i, field) in enumerate(struct_members):
if i > 0:
print(" data.{0} = ({1} *) (uintptr_t)current_offset;".format(field['name'], field['type']))
print(" current_offset += ((sizeof({0})) * ({1}));".format(field['type'], field_params[i]))
print(" data.offset[{0}] = current_offset;".format(i))
print()
print(" if (current_offset < m_joint->offset[{0}]) {{".format(len(struct_members) - 1))
print(" return *m_joint;")
print(" }\n")
print(" data.memory_block = mem_alloc(current_offset);")
print(" if (data.memory_block == NULL) {")
print(" return *m_joint;")
print(" }\n")
for (i, field) in enumerate(struct_members):
if i > 0:
print(" data.{0} = (void *)(((uintptr_t) data.{0}) + ((uintptr_t) data.memory_block));".format(field['name']))
else:
print(" data.{0} = ({1} *)(data.memory_block);".format(field['name'], field['type']))
for (i, field) in enumerate(struct_members):
if i > 0:
data_size = "data.offset[{0}] - data.offset[{0} - 1]".format(i)
m_joint_size = "m_joint->offset[{0}] - m_joint->offset[{0} - 1]".format(i)
else:
print()
data_size = "data.offset[0]"
m_joint_size = "m_joint->offset[0]"
print(" memcpy(data.{0}, m_joint->{0}, min({1}, {2}));".format(field['name'], data_size, m_joint_size))
print("\n free_joint_{0}(mem_dealloc, m_joint);".format(struct_name))
print("\n return data;")
print("}\n")
def print_copy_method(struct_name, field_count, struct_members):
"""Generate copy function for joint data structure."""
print("struct joint_{0} *copy_joint_{0}(void *(*mem_alloc)(size_t), void (*mem_dealloc)(void *), struct joint_{0} *lhs, struct joint_{0} *rhs) {{".format(struct_name))
print(" if (lhs == rhs) {")
print(" return rhs;")
print(" }\n")
print(" assert(((void) \"lhs cannot be null\", lhs != NULL));")
print(" assert(((void) \"rhs cannot be null\", rhs != NULL));")
# Copy memory_block from lhs to rhs
print("\n if (lhs->memory_block != rhs->memory_block) {")
print(" mem_dealloc(rhs->memory_block);")
print("\n rhs->memory_block = mem_alloc(lhs->offset[{0}]);".format(field_count - 1))
print(" if (rhs->memory_block == NULL) {")
print(" return NULL;")
print(" }")
print("\n memcpy(rhs->memory_block, lhs->memory_block, lhs->offset[{0}]);".format(field_count - 1))
print(" }\n")
# Copy offsets from lhs to rhs
for (i, field) in enumerate(struct_members):
if i > 0:
print(" rhs->{0} = ({1} *) (uintptr_t)lhs->offset[{2}];".format(field['name'], field['type'], i-1))
print(" rhs->offset[{0}] = lhs->offset[{0}];\n".format(i))
for (i, field) in enumerate(struct_members):
if i > 0:
print(" rhs->{0} = (void *)(((uintptr_t) rhs->{0}) + ((uintptr_t) rhs->memory_block));".format(field['name']))
else:
print(" rhs->{0} = ({1} *)(rhs->memory_block);".format(field['name'], field['type']))
print("\n return rhs;")
print("}\n")
def parse_args():
argparser = argparse.ArgumentParser('Create C code for joint memory allocation')
argparser.add_argument('filename',
default='',
help='name of file to parse')
argparser.add_argument('-o',
'--output',
default='',
help='output file')
return argparser.parse_args()
def main():
args = parse_args()
generate_c_code(args.filename, args.output)
if __name__ == "__main__":
main()