-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx86_32_backend_generator.py
More file actions
397 lines (323 loc) · 5.72 KB
/
Copy pathx86_32_backend_generator.py
File metadata and controls
397 lines (323 loc) · 5.72 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import subprocess
import os
from backend_generator import generate_builtins
"""
eax holds the top stack element
ebx points to the stack element second from the top.
the stack grows upwards.
edi shall point to a struct of:
...
variable 2 address
variable 1 address
-> variable 0 address
runtime word 0 m4_runtime_cb_pair_t ptr
runtime word 1 m4_runtime_cb_pair_t ptr
runtime word 2 m4_runtime_cb_pair_t ptr
...
esi shall point to a struct of:
m4_stack_t
data
4 max
8 len
12 memory
16 data
20 callback info (uint8_t *)
24 callback_word_locations
28 a number that when subtracted from ebx and divided
by 4 is the stack depth
32 edi value
36 runtime word function
40 callback targets
44 special_runtime_cbs
48 total_extra_memory_size
edx holds the data-space pointer
"""
builtins = (
("dup", (), """
add ebx, 4
mov [ebx], eax
"""), ("-", (), """
neg eax
add eax, [ebx]
sub ebx, 4
"""), (">", (), """
cmp [ebx], eax
setng al
movzx eax, al
dec eax
sub ebx, 4
"""), ("+", (), """
add eax, [ebx]
sub ebx, 4
"""), ("=", (), """
cmp [ebx], eax
setne al
movzx eax, al
dec eax
sub ebx, 4
"""), ("over", ("clobber", ), """
add ebx, 4
mov [ebx], eax
mov eax, [ebx-4]
"""), ("mod", (), """
push edx
mov ecx, eax
mov eax, [ebx]
cdq
idiv ecx
mov eax, edx
pop edx
sub ebx, 4
"""), ("drop", (), """
mov eax, [ebx]
sub ebx, 4
"""), ("swap", (), """
mov ecx, [ebx]
mov [ebx], eax
mov eax, ecx
"""), ("*", (), """
imul eax, [ebx]
sub ebx, 4
"""), ("allot", (), """
add edx, eax
mov eax, [ebx]
sub ebx, 4
"""), ("1+", (), """
inc eax
"""), ("invert", (), """
not eax
"""), ("0=", (), """
cmp eax, 1 ; from C dissasembly of
sbb eax, eax ; x == 0 ? -1 : 0
"""), ("i", ("clobber", ), """
add ebx, 4
mov [ebx], eax
mov eax, [esp]
"""), ("j", ("clobber", ), """
add ebx, 4
mov [ebx], eax
mov eax, [esp+8]
"""), ("1-", (), """
dec eax
"""), ("rot", (), """
mov ecx, [ebx]
mov [ebx], eax
mov eax, [ebx-4]
mov [ebx-4], ecx
"""), ("pick", (), """
neg eax ; maybe flip the stack growth direction?
mov eax, [ebx+eax*4]
"""), ("xor", (), """
xor eax, [ebx]
sub ebx, 4
"""), ("lshift", (), """
mov ecx, eax
mov eax, [ebx]
shl eax, cl
sub ebx, 4
"""), ("rshift", (), """
mov ecx, eax
mov eax, [ebx]
shr eax, cl
sub ebx, 4
"""), ("<", (), """
cmp [ebx], eax
setnl al
movzx eax, al
dec eax
sub ebx, 4
"""), (">=", (), """
cmp [ebx], eax
setnge al
movzx eax, al
dec eax
sub ebx, 4
"""), ("<=", (), """
cmp [ebx], eax
setnle al
movzx eax, al
dec eax
sub ebx, 4
"""), ("or", (), """
or eax, [ebx]
sub ebx, 4
"""), ("and", (), """
and eax, [ebx]
sub ebx, 4
"""), ("depth", ("clobber", ), """
add ebx, 4
mov [ebx], eax
mov eax, ebx
sub eax, [esi+28]
shr eax, 2
"""), ("c!", (), """
mov ecx, [ebx]
mov [eax], cl
sub ebx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("c@", (), """
movzx eax, byte [eax]
"""), ("0<", (), """
sar eax, 31
"""), ("!", (), """
mov ecx, [ebx]
mov [eax], ecx
sub ebx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("@", (), """
mov eax, [eax]
"""), ("c,", (), """
mov [edx], al
inc edx
mov eax, [ebx]
sub ebx, 4
"""), ("here", ("clobber", ), """
add ebx, 4
mov [ebx], eax
mov eax, edx
"""), ("max", (), """
mov ecx, [ebx]
cmp eax, ecx
cmovl eax, ecx
sub ebx, 4
"""), ("2*", (), """
add eax, eax
"""), ("2drop", (), """
sub ebx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("tuck", (), """
add ebx, 4
mov ecx, [ebx-4]
mov [ebx], ecx
mov [ebx-4], eax
"""), ("cell+", (), """
add eax, 4
"""), ("align", (), """
add edx, 3
and edx, ~3
"""), (",", (), """
mov [edx], eax
add edx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("?dup", (), """
add ebx, 4
mov [ebx], eax
lea ecx, [ebx-4]
test eax, eax
cmovz ebx, ecx
"""), ("<>", (), """
cmp [ebx], eax
sete al
movzx eax, al
dec eax
sub ebx, 4
"""), (">r", (), """
push eax
mov eax, [ebx]
sub ebx, 4
"""), ("r>", ("clobber", ), """
add ebx, 4
mov [ebx], eax
pop eax
"""), ("2>r", (), """
push dword [ebx]
push eax
sub ebx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("2r>", ("clobber", ), """
add ebx, 4
mov [ebx], eax
add ebx, 4
pop eax
pop dword [ebx]
"""), ("2/", (), """
sar eax, 1
"""), ("2dup", (), """
add ebx, 4
mov [ebx], eax
mov ecx, [ebx-4]
add ebx, 4
mov [ebx], ecx
"""), ("0<>", (), """
neg eax ; from C dissasembly of
sbb eax, eax ; x != 0 ? -1 : 0
"""), ("0>", (), """
test eax, eax
setng al
movzx eax, al
dec eax
"""), ("w@", (), """
movzx eax, word [eax]
"""), ("w!", (), """
mov ecx, [ebx]
mov [eax], cx
sub ebx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("+!", (), """
mov ecx, [ebx]
add [eax], ecx
sub ebx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("/", (), """
push edx
mov ecx, eax
mov eax, [ebx]
cdq
idiv ecx
pop edx
sub ebx, 4
"""), ("cells", (), """
sal eax, 2
"""), ("nip", (), """
sub ebx, 4
"""), ("min", (), """
mov ecx, [ebx]
cmp ecx, eax
cmovl eax, ecx
sub ebx, 4
"""), ("abs", (), """
mov ecx, eax
neg ecx
cmovns eax, ecx
"""), ("r@", ("clobber", ), """
add ebx, 4
mov [ebx], eax
mov eax, [esp]
"""), ("w,", (), """
mov [edx], ax
add edx, 2
mov eax, [ebx]
sub ebx, 4
"""), ("negate", (), """
neg eax
""")
)
elidables = (
("add ebx, 4", r" *add +ebx *, *4 *"),
("mov [ebx], eax", r" *mov *\[ *ebx *\] *, *eax *"),
("mov eax, [ebx]", r" *mov +eax *, *\[ *ebx *\] *"),
("sub ebx, 4", r" *sub +ebx *, *4 *"),
)
def src_to_code(src):
src_path = "tmp.s"
bin_path = "tmp.bin"
with open(src_path, "w") as f:
f.write("[BITS 32]\n")
f.write(src)
subprocess.check_call(("nasm", "-f", "bin", src_path, "-o", bin_path))
with open(bin_path, "rb") as f:
code = f.read()
os.unlink(src_path)
os.unlink(bin_path)
return code
def main():
generate_builtins("x86_32", builtins, elidables, src_to_code)
if __name__ == "__main__":
main()