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