-
Notifications
You must be signed in to change notification settings - Fork 0
/
x86_32_backend_generator.py
562 lines (477 loc) · 11.1 KB
/
x86_32_backend_generator.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import sys
from nasm import nasm
import re
"""
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 memset
48 memmove
52 compare
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", (), """
xchg eax, [ebx]
"""), ("*", (), """
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", (), """
xchg eax, [ebx]
xchg eax, [ebx-4]
"""), ("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
"""), ("fill", (), """
push edx
mov ecx, esp
and esp, ~15
push ecx
push dword [ebx]
push eax
push dword [ebx-4]
call [esi+44]
mov esp, [esp+12]
pop edx
sub ebx, 8
mov eax, [ebx]
sub ebx, 4
"""), ("2drop", (), """
sub ebx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("move", (), """
push edx
mov ecx, esp
and esp, ~15
push ecx
push eax
push dword [ebx-4]
push dword [ebx]
call [esi+48]
mov esp, [esp+12]
pop edx
sub ebx, 8
mov eax, [ebx]
sub ebx, 4
"""), ("execute", (), """
mov ecx, eax
mov eax, [ebx]
sub ebx, 4
call ecx
"""), ("align", (), """
add edx, 3
and edx, ~3
"""), (",", (), """
mov [edx], eax
add edx, 4
mov eax, [ebx]
sub ebx, 4
"""), ("compare", (), """
push edx
mov ecx, esp
and esp, ~15
push ecx
push eax
push dword [ebx]
push dword [ebx-4]
push dword [ebx-8]
call [esi+52]
mov esp, [esp+16]
pop edx
sub ebx, 8
sub ebx, 4
"""), ("<>", (), """
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
""")
)
ADD4 = 0
MVBA = 1
SUB4 = 2
MVAB = 3
CLOB = 4
def assemble(asm):
disasm = nasm(asm)
instr = tuple(bytes.fromhex(line.split(maxsplit=2)[1].decode())
for line in disasm.splitlines())
return instr
class Chore:
def __init__(self, normal, regex):
assert None is not re.fullmatch(regex, normal)
self.normal = normal
self.regex = regex
instr = assemble(normal)
assert len(instr) == 1
self.ins = instr[0]
def two_way_match(self, ins, a):
if self.ins == ins:
assert None is not re.fullmatch(self.regex, a)
return True
assert None is re.fullmatch(self.regex, a)
return False
flag_macros = (
"M4_X86_32_ADD4",
"M4_X86_32_MVBA",
"M4_X86_32_SUB4",
"M4_X86_32_MVAB",
"M4_X86_32_CLOB",
)
stack_chores = (
Chore(
"add ebx, 4",
r"^\s*add\s+ebx\s*,\s*4\s*(;.*|$)",
),
Chore(
"mov [ebx], eax",
r"^\s*mov\s*\[\s*ebx\s*\]\s*,\s*eax\s*(;.*|$)",
),
Chore(
"sub ebx, 4",
r"^\s*sub\s+ebx\s*,\s*4\s*(;.*|$)",
),
Chore(
"mov eax, [ebx]",
r"^\s*mov\s+eax\s*,\s*\[\s*ebx\s*\]\s*(;.*|$)",
),
)
def flag(x):
return 1 << x
def non_instr_lines(instr, asm):
ins_it = iter(instr)
a_it = iter(asm)
while True:
while True:
try:
a = next(a_it)
except StopIteration:
try:
next(ins_it)
except StopIteration:
return
assert False
spl = a.split()
if not spl or spl[0][0] in ";.":
yield b'', a
continue
break
ins = next(ins_it)
yield ins, a
def prune_instr_and_get_flags(instr, asm, opts):
lines = [list(pair) for pair in non_instr_lines(instr, asm)]
flags = 0
for i in range(len(lines)):
ins, a = lines[i]
if not ins:
continue
if stack_chores[ADD4].two_way_match(ins, a):
flags |= flag(ADD4)
lines[i][0] = b''
i += 1
if i < len(lines) and stack_chores[MVBA].two_way_match(*lines[i]):
lines[i][0] = b''
flags |= flag(MVBA)
break
for i in reversed(range(len(lines))):
ins, a = lines[i]
if not ins:
continue
if stack_chores[SUB4].two_way_match(ins, a):
flags |= flag(SUB4)
lines[i][0] = b''
i -= 1
if i >= 0 and stack_chores[MVAB].two_way_match(*lines[i]):
lines[i][0] = b''
flags |= flag(MVAB)
break
for opt in opts:
assert opt == "clobber" and (flags & flag(MVBA)) and not (flags & flag(CLOB))
flags |= flag(CLOB)
return tuple(ins for ins, a in lines), tuple(a for ins, a in lines), flags
def fmt_machine_code_inner(ins, a, s1, ipad, apad, f):
if s1 is None:
s1 = (', '.join("0x" + hex(b)[2:].zfill(2) for b in ins) + ("," if ins else '')).ljust(ipad if ipad is not None else 0)
s2 = a.ljust(apad if apad is not None else 0)
print(f' {s1} /* {s2} */', file=f)
def fmt_machine_code(num, name, instr, asm, flags, igmax, agmax, f):
if flags & flag(CLOB):
print(f"/* {name} */ /* clobber */", file=f)
else:
print(f"/* {name} */", file=f)
s_omitted = "/* omitted */ "
ipad = max(igmax * 6, len(s_omitted))
apad = agmax
for ins, a in zip(instr, asm):
if not ins and any(None is not re.fullmatch(ch.regex, a) for ch in stack_chores):
s1 = s_omitted.ljust(ipad)
else:
s1 = None
fmt_machine_code_inner(ins, a, s1, ipad, apad, f)
def table(results, f):
cumulative = 0
for i, bi_name, instr, asm, flags in results:
n_bytes = sum(map(len, instr))
pos = (str(cumulative) + ",").ljust(6)
length = (str(n_bytes) + ",").ljust(6)
s_flags = " | ".join(macro for i, macro in enumerate(flag_macros) if flag(i) & flags)
print(f" {{{pos} {length}{s_flags}}},", file=f)
cumulative += n_bytes
def main():
results = []
for i, (bi_name, opts, asm) in enumerate(builtins):
instr = assemble(asm)
asm = tuple(asm.splitlines())
instr, asm2, flags = prune_instr_and_get_flags(instr, asm, opts)
assert asm == asm2
results.append((i, bi_name, instr, asm, flags))
total_machine_code_bytes = sum(sum(map(len, instr)) for _, _, instr, _, _ in results)
with open("x86-32_backend_generated.c", "w") as f:
print(f"""/* generated by {sys.argv[0]} DO NOT EDIT */
#include "x86-32_backend_generated.h"
const uint8_t m4_x86_32_backend_code[{total_machine_code_bytes}] = {{""", file=f)
igmax = max(max(map(len, instr)) for _, _, instr, _, _ in results)
agmax = max(max(map(len, asm)) for _, _, _, asm, _ in results)
for i, bi_name, instr, asm, flags in results:
fmt_machine_code(i, bi_name, instr, asm, flags, igmax, agmax, f)
print("""};
""", file=f)
for i, macro in zip(range(4), flag_macros):
print(f"""const uint8_t {macro.lower()}[{len(stack_chores[i].ins)}] = {{""", file=f)
fmt_machine_code_inner(stack_chores[i].ins, stack_chores[i].normal, None, None, None, f)
print("};", file=f)
print(f"""
const struct m4_x86_32_backend_table_entry m4_x86_32_backend_table[{len(builtins)}] = {{""", file=f)
table(results, f)
print("};", file=f)
newline = "\n"
with open("x86-32_backend_generated.h", "w") as f:
print(f"""/* generated by {sys.argv[0]} DO NOT EDIT */
#pragma once
#include <stdint.h>
{newline.join(f'#define {macro} {hex(flag(i))}' for i, macro in enumerate(flag_macros))}
{newline.join(f"extern const uint8_t {macro.lower()}[{len(stack_chores[i].ins)}];" for i, macro in zip(range(4), flag_macros))}
extern const uint8_t m4_x86_32_backend_code[{total_machine_code_bytes}];
struct m4_x86_32_backend_table_entry {{uint16_t pos; uint8_t len; uint8_t flags;}};
extern const struct m4_x86_32_backend_table_entry m4_x86_32_backend_table[{len(builtins)}];""", file=f)
if __name__ == "__main__":
main()