-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreepass.py
508 lines (459 loc) · 15 KB
/
threepass.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @File Name: threepass.py
# @Author: Copyright (c) 2017-01-28 01:03:16 gilletthernandez
# @Date: 2017-01-28 01:03:16
# @Last Modified by: Gillett Hernandez
# @Last Modified time: 2017-02-09 12:58:51
# language syntax:
#
#
# function ::= '[' arg-list ']' expression
#
# arg-list ::= /* nothing */
# | arg-list variable
#
# expression ::= term
# | expression '+' term
# | expression '-' term
#
# term ::= factor
# | term '*' factor
# | term '/' factor
#
# factor ::= number
# | variable
# | '(' expression ')'
# variable ::= [a-zA-Z]+
# number ::= [0-9]+
# numbers and variables are marked by the tokenize function
# asm opcodes:
# IM = immediate n
# AR = argument n
# SW = swap r0 and r1
# PU = push r0 to stack
# PO = pop value from stack to r0
# AD = add
# SU = subtract
# MU = multiply
# DI = divide
# example
# [ a b ] a*a + b*b
# {
# 'op':'+',
# 'a': {
# 'op':'*',
# 'a': {
# 'op':'arg',
# 'n': 'a'
# },
# 'b': {
# 'op':'arg',
# 'n': 'a'
# }
# },
# 'b': {
# 'op':'*',
# 'a': {
# 'op':'arg',
# 'n': 'b'
# },
# 'b': {
# 'op':'arg',
# 'n': 'b'
# }
# }
# }
# no optimizations, # instructions = 23
# ['AR0', 'PU', 'AR0', 'PU', 'PO', 'SW', 'PO', 'MU', 'PU', 'AR1', 'PU', 'AR1', 'PU', 'PO' 'SW', 'PO', 'MU', 'PU', 'PO', 'SW', 'PO', 'AD', 'PU']
# 2 optimizations types, # instructions = 17
# ['AR0', 'PU', 'AR0', 'SW', 'PO', 'MU', 'PU', 'AR1', 'PU', 'AR1', 'SW', 'PO', 'MU', 'SW', 'PO', 'AD', 'PU']
# 3 optimizations types, # instructions = 15
# ['AR0', 'SW', 'AR0', 'SW', 'MU', 'PU', 'AR1', 'SW', 'AR1', 'SW', 'MU', 'SW', 'PO', 'AD', 'PU']
# hand written, # instructions = 13
# ['AR0', 'SW', 'AR0', 'MU', 'PU', 'AR1', 'SW', 'AR1', 'MU', 'SW', 'PO', 'AD', 'PU']
import re
global debug
debug = True
OP = "OP"
CP = "CP"
IMM = "imm"
ARG = "arg"
OPR = "op"
opcodes = {"+": "AD", "-": "SU", "*": "MU", "/": "DI", "imm": "IM", "arg": "AR"}
class Node:
__slots__ = ["op", "token", "ti"]
def __init__(self, optype):
self.op = optype
self.token = ""
self.ti = 0
def __repr__(self):
if hasattr(self, "n"):
string2 = ", 'n': {}".format(self.n)
elif hasattr(self, "a") and hasattr(self, "b"):
string2 = ", 'a': {0!r}, 'b': {1!r}".format(self.a, self.b)
else:
string2 = ""
# print("op = ",self.op)
# print("string2 = ", string2)
rstring = "'op': '{0}'{1}".format(self.op, string2)
rstring = "{" + rstring + "}"
return rstring
def __str__(self):
return str(self.token)
class Const(Node):
__slots__ = ["n"]
def __init__(self, n):
super(Const, self).__init__(IMM)
self.n = n
class Arg(Node):
__slots__ = ["n"]
def __init__(self, n):
super(Arg, self).__init__(ARG)
self.n = n
class Op(Node):
__slots__ = ["a", "b"]
def __init__(self, optype, a, b):
super(Op, self).__init__(optype)
self.a = a
self.b = b
class Compiler:
def __repr__(self):
return f"<Compiler instance at 0x{id(self)}>"
def compile(self, code):
return self.pass4(self.pass3(self.pass2(self.pass1(code))))
def tokenizer(self, code):
"""Turn a code string into an array of tokens. Each token
is either '[', ']', '(', ')', '+', '-', '*', '/', a variable
name or a number (as a string)"""
token_iter = (
m.group(0) for m in re.finditer(r"[-+*/()[\]]|[A-Za-z]+|\d+", code)
)
tokens = []
parens = []
self.ptable = {}
for i, token in enumerate(
[int(tok) if tok.isdigit() else tok for tok in token_iter]
):
if isinstance(token, int):
node = Const(token)
elif token.isalpha():
node = Arg(token)
elif token in "+-*/":
optype = OPR
node = Op(token, None, None)
elif token == "(":
node = Node(OP)
parens.append(i)
elif token == ")":
node = Node(CP)
self.ptable[parens.pop()] = i
else:
node = Node(None)
node.token = str(token)
node.ti = i
tokens.append(node)
if debug:
print(self.ptable)
if debug:
print(parens)
self.rptable = {b: a for a, b in self.ptable.items()}
return tokens
def pass1(self, code):
"""Returns an un-optimized AST"""
if debug:
print("tokenizer starting")
tokens = self.tokenizer(code)
if debug:
print("tokenizer done, arglist parsing")
self.args = self.parse_arglist(tokens)
if debug:
print("arglist done, ast parsing")
ast = self.parse_expression(tokens)
if debug:
print("ast parsing done")
print(ast)
return ast
def parse_arglist(self, tokens):
if debug:
print("parse arglist", [str(t) for t in tokens])
ii = 1
args = []
tokens[0].token == "["
while tokens[ii].token != "]":
args.append(tokens[ii].token)
ii += 1
# ii+1 to include ']' in deletion
del tokens[: ii + 1]
return args
def parse_expression(self, tokens):
# expression ::= term
# | expression '+' term
# | expression '-' term
if debug:
print("parse expression tokens ", tokens)
ii = len(tokens) - 1
token = tokens[ii]
while token.token not in "+-":
if debug:
print("searching for + or -", ii)
if token.token == ")":
start = self.rptable[token.ti]
if token.ti != ii:
if debug:
print(
"if branch, token.ti = {}, ii = {}, start = {}".format(
token.ti, ii, start
)
)
ii -= token.ti - start
if debug:
print("after, ii = {}".format(ii))
if debug:
print([str(t) for t in tokens])
else:
if debug:
print(
"else branch, token.ti = {}, ii = {}, start = {}".format(
token.ti, ii, start
)
)
ii = start
if debug:
print([str(t) for t in tokens])
# if it was a parenth, now at start so skip backward past '('
# else, normally 'advance'
ii -= 1
if ii < 0:
break
try:
token = tokens[ii]
except:
if debug:
print(locals())
raise
else:
assert tokens[ii].token in "+-"
node = tokens[ii]
node.a = self.parse_expression(tokens[:ii])
node.b = self.parse_term(tokens[ii + 1 :])
if debug:
print("expression a b path", locals())
return node
assert ii <= 0, locals()
if debug:
print("subparse into term path", locals())
# no +- on within tokens that are outside of parens
node = self.parse_term(tokens)
return node
def parse_term(self, tokens):
# term ::= factor
# | term '*' factor
# | term '/' factor
if debug:
print("parse term tokens ", tokens)
ii = len(tokens) - 1
token = tokens[ii]
while token.token not in "*/":
if debug:
print("searching for * or /", ii)
if token.token == ")":
start = self.rptable[token.ti]
# subtraction to offset the offset induced by a slice
if token.ti != ii:
ii -= token.ti - start
else:
ii = start
# if it was a parenth, now at end so skip past ')'
# else, normally advance
ii -= 1
if ii < 0:
break
token = tokens[ii]
else:
assert tokens[ii].token in "*/"
node = tokens[ii]
node.a = self.parse_term(tokens[:ii])
node.b = self.parse_factor(tokens[ii + 1 :])
if debug:
print("term a b path", locals())
return node
assert ii < 0, locals()
# no +- on within tokens that are outside of parens
if debug:
print("subparse into factor path", locals())
node = self.parse_factor(tokens)
return node
def parse_factor(self, tokens):
# factor ::= number
# | variable
# | '(' expression ')'
if debug:
print("parse factor", tokens)
if tokens[0].op == OP:
end = self.ptable[tokens[0].ti]
# end is offsetted because of previous token consumption
end -= tokens[0].ti
assert tokens[end].op == CP, locals()
del tokens[0]
node = self.parse_expression(tokens[0 : end - 1])
# because of token[0] deletion, this next call includes the ending ']' token
del tokens[:end]
return node
elif tokens[0].op in [IMM, ARG]:
if tokens[0].op == ARG:
# tee hee.
# ast manipulation, converting 'n': 'a' to 'n': 0
tokens[0].n = self.args.index(tokens[0].token)
return tokens[0]
else:
return tokens[0]
else:
raise RuntimeError("parse error in parse factor")
def pass2(self, ast):
"""Returns an AST with constant expressions reduced"""
# post order tree traversal
# ast is root node of AST and any operators with only constants on both sides need to be reduced
# CTFE/CTE
# Compile Time Evaluation
if debug:
print("subcall")
if ast.op not in [IMM, ARG]:
# subcalls in case of nested constants
ast.a = self.pass2(ast.a)
ast.b = self.pass2(ast.b)
# transformations in case they were resolved
ast.a = self.transformer(ast.a)
ast.b = self.transformer(ast.b)
# additional transformation in case self was resolved
ast = self.transformer(ast)
return ast
@staticmethod
def transformer(node):
if hasattr(node, "a") and hasattr(node, "b"):
if node.a.op == IMM and node.b.op == IMM:
node = Const(eval("{}{}{}".format(node.a.n, node.op, node.b.n)))
return node
def pass3(self, ast):
"""Returns assembly instructions"""
# post order tree traversal
instructions = []
# entire call either loads and pushes on to stack
# or subcalls, pops, swaps, pops, ops, then pushes on to stack
if ast.op in [IMM, ARG]:
# load number into r0
instructions.append(opcodes[ast.op] + str(ast.n))
# push on to stack
instructions.append("PU")
elif ast.op in "+-*/":
# subcall and push result on to stack
instructions.extend(self.pass3(ast.a))
# subcall and push result on to stack
instructions.extend(self.pass3(ast.b))
# pop both off stack
instructions.append("PO")
instructions.append("SW")
instructions.append("PO")
# compute
instructions.append(opcodes[ast.op])
# push
instructions.append("PU")
return instructions
def pass4(self, asm):
"""optimizes assembly instructions"""
# discards PU PO combinations and PO PU combinations
# discards SW SW combinations
def mutator1(asm):
i = 0
while i + 1 < len(asm):
if (asm[i], asm[i + 1]) in [("PO", "PU"), ("PU", "PO")]:
del asm[i : i + 2]
i += 1
return asm
def mutator2(asm):
i = 0
while i + 1 < len(asm):
if (asm[i], asm[i + 1]) in ("SW", "SW"):
del asm[i : i + 2]
i += 1
return asm
def mutator3(asm):
i = 0
while i + 3 < len(asm):
if (
asm[i] == "PU"
and asm[i + 1][:2] in ["AR", "IM"]
and asm[i + 2] == "SW"
and asm[i + 3] == "PO"
):
asm[i : i + 4] = ["SW", asm[i + 1], "SW"]
i += 1
return asm
L = [mutator1, mutator2, mutator3]
num_mutators = len(L)
orig_len = len(asm)
print(asm)
last_asm = asm[:]
asm = L[0](asm)
i = 1
while i != 0 or last_asm != asm:
if i == 0:
last_asm = asm[:]
asm = L[i](asm)
i += 1
i %= num_mutators
print("optimizations cut {} instructions".format(orig_len - len(asm)))
return asm
def simulate(asm, argv):
r0, r1 = None, None
stack = []
for ins in asm:
if debug:
print(ins)
if ins[:2] == "IM" or ins[:2] == "AR":
ins, n = ins[:2], int(ins[2:])
if ins == "IM":
r0 = n
elif ins == "AR":
r0 = argv[n]
elif ins == "SW":
r0, r1 = r1, r0
elif ins == "PU":
stack.append(r0)
elif ins == "PO":
r0 = stack.pop()
elif ins == "AD":
r0 += r1
elif ins == "SU":
r0 -= r1
elif ins == "MU":
r0 *= r1
elif ins == "DI":
r0 /= r1
return r0
def main():
testcode1 = "[ a b ] a * a + b * b"
# testcode2 = "[ a b ] (a + b) / 2"
# testcode3 = "[ a b c d ] (a-b)*(c-d)/(2-a)"
# testcode4 = "[ a b c d ] a*b*c/d"
# testcode5 = "[ a b c d ] a/b*c/d"
# testcode6 = "[ a ] a / (2 * 5)"
# testcode7 = "[ a b c ] a*b*c"
# asm = Compiler().compile(testcode1)
# asm = Compiler().compile(testcode2)
# asm = Compiler().compile(testcode3)
# asm = Compiler().compile(testcode4)
# asm = Compiler().compile(testcode5)
# asm = Compiler().compile(testcode6)
comp = Compiler()
ast = comp.pass1(testcode1)
if debug:
print(ast)
asm = comp.compile(testcode1)
if debug:
print(asm)
# print(simulate(asm, [5, 7]))
# print(simulate(asm, [3, 4]))
print(simulate(asm, [2, 2, 2]))
if __name__ == "__main__":
main()