322
|
1 from tree import Tree
|
|
2 from . import ir
|
|
3
|
|
4 f_map = {} # Mapping from types to tree creation functions
|
|
5
|
|
6 def register(tp):
|
|
7 """ Register a function for type tp """
|
|
8 def reg_f(f):
|
|
9 f_map[tp] = f
|
|
10 return f
|
|
11 return reg_f
|
|
12
|
|
13 @register(ir.Binop)
|
|
14 def binop_to_tree(e):
|
|
15 names = {'+':'ADDI32', '-':'SUBI32', '|':'ORI32', '<<':'SHLI32',
|
|
16 '*':'MULI32'}
|
|
17 op = names[e.operation]
|
|
18 return Tree(op, makeTree(e.a), makeTree(e.b))
|
|
19
|
|
20 @register(ir.Temp)
|
|
21 def temp_to_tree(e):
|
|
22 t = Tree('REGI32')
|
|
23 t.value = e
|
|
24 return t
|
|
25
|
|
26 @register(ir.Const)
|
|
27 def const_to_tree(e):
|
|
28 t = Tree('CONSTI32')
|
|
29 t.value = e.value
|
|
30 return t
|
|
31
|
|
32 @register(ir.Mem)
|
|
33 def mem_to_tree(e):
|
|
34 return Tree('MEMI32', makeTree(e.e))
|
|
35
|
|
36 @register(ir.Call)
|
|
37 def call_to_tree(e):
|
|
38 return Tree('CALL')
|
|
39
|
|
40 def makeTree(ir_node):
|
|
41 """ Transform an ir node into a tree usable for matching """
|
|
42 return f_map[type(ir_node)](ir_node)
|