-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_function_pointer_table.py
More file actions
191 lines (173 loc) · 5.54 KB
/
Copy pathgenerate_function_pointer_table.py
File metadata and controls
191 lines (173 loc) · 5.54 KB
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
import sys
void = 0
u8 = 1
u16 = 2
u32 = 3
u64 = 4
i8 = 5
i16 = 6
i32 = 7
i64 = 8
f32 = 9
f64 = 10
def generate_function(f, arity):
global void, u8, u16, u32, u64, i8, i16, i32, i64, f32, f64
f.write(f"pub fn cast_and_call_arity_{arity}(function_pointer: *const (), args: &[Value], return_type: Type) -> Value {{\n")
f.write(" match (args, return_type) {\n")
types = [u8 for _ in range(0, arity + 1)]
return_type = void
while True:
while return_type < f64 + 1:
output = " ([Value::U64(x)"
for i, ty in enumerate(types):
if ty == u8:
output += f", Value::U8(x{i})"
elif ty == u16:
output += f", Value::U16(x{i})"
elif ty == u32:
output += f", Value::U32(x{i})"
elif ty == u64:
output += f", Value::U64(x{i})"
elif ty == i8:
output += f", Value::I8(x{i})"
elif ty == i16:
output += f", Value::I16(x{i})"
elif ty == i32:
output += f", Value::I32(x{i})"
elif ty == i64:
output += f", Value::I64(x{i})"
elif ty == f32:
output += f", Value::F32(x{i})"
elif ty == f64:
output += f", Value::F64(x{i})"
output += "], "
if return_type == void:
output += "Type::Void) => {\n"
elif return_type == u8:
output += "Type::U8) => {\n"
elif return_type == u16:
output += "Type::U16) => {\n"
elif return_type == u32:
output += "Type::U32) => {\n"
elif return_type == u64:
output += "Type::64) => {\n"
elif return_type == i8:
output += "Type::I8) => {\n"
elif return_type == i16:
output += "Type::I16) => {\n"
elif return_type == i32:
output += "Type::I32) => {\n"
elif return_type == i64:
output += "Type::I64) => {\n"
elif return_type == f32:
output += "Type::F32) => {\n"
elif return_type == f64:
output += "Type::F64) => {\n"
output += generate_cast(types, return_type)
output += "\n }\n"
f.write(output)
return_type += 1
if increment_types(types):
break
return_type = void
f.write("}")
def generate_cast(types, return_type) -> str:
global void, u8, u16, u32, u64, i8, i16, i32, i64, f32, f64
call = ' let out = a(x'
output = ' let a = unsafe { std::mem::transmute::<*const (), extern "C" fn(u64'
for i, ty in enumerate(types):
if ty == u8:
output += ', u8'
call += f', x{i}'
elif ty == u16:
output += ', u16'
call += f', x{i}'
elif ty == u32:
output += ', u32'
call += f', x{i}'
elif ty == u64:
output += ', u64'
call += f', x{i}'
elif ty == i8:
output += ', i8'
call += f', x{i}'
elif ty == i16:
output += ', i16'
call += f', x{i}'
elif ty == i32:
output += ', i32'
call += f', x{i}'
elif ty == i64:
output += ', i64'
call += f', x{i}'
elif ty == f32:
output += ', f32'
call += f', x{i}'
elif ty == f64:
output += ', f64'
call += f', x{i}'
output += ') -> '
call += ');\n'
return_value = ' Value::'
if return_type == void:
output += "()"
return_value += 'Unit(a)\n'
elif return_type == u8:
output += "u8"
return_value += 'U8(a)\n'
elif return_type == u16:
output += "u16"
return_value += 'U16(a)\n'
elif return_type == u32:
output += "u32"
return_value += 'U32(a)\n'
elif return_type == u64:
output += "u64"
return_value += 'U164(a)\n'
elif return_type == i8:
output += "i8"
return_value += 'I8(a)\n'
elif return_type == i16:
output += "i16"
return_value += 'I16(a)\n'
elif return_type == i32:
output += "i32"
return_value += 'I32(a)\n'
elif return_type == i64:
output += "i64"
return_value += 'I64(a)\n'
elif return_type == f32:
output += "f32"
return_value += 'F32(a)\n'
elif return_type == f64:
output += "f64"
return_value += 'F64(a)\n'
output += '>(function_pointer) };\n'
return output + call + return_value
def increment_types(types):
global void, u8, u16, u32, u64, i8, i16, i32, i64, f32, f64
index = 0
carry = 1
while index < len(types) and carry == 1:
types[index] += carry
if types[index] == f64 + 1:
types[index] = u8
carry = 1
else:
carry = 0
index += 1
if carry == 1:
return True
"""
all_f64 = True
for ty in types:
all_f64 = all_f64 and (True if ty == f64 else False)
if not all_f64:
return False
return all_f64"""
return False
if __name__ == '__main__':
arity = int(sys.argv[1])
path = f'{sys.argv[2]}/arity{arity}.rs'
with open(path, 'w+') as f:
generate_function(f, arity)