annotate python/ppci/ir2tree.py @ 397:5d03c10fe19d

Small changes
author Windel Bouwman
date Thu, 29 May 2014 10:47:28 +0200
parents 988f3fb861e4
children
rev   line source
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
1 from tree import Tree
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
2 from . import ir
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
3
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
4 """ Create a tree from ir code. """
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
5
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
6 f_map = {} # Mapping from types to tree creation functions
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
7
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
8 def register(tp):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
9 """ Register a function for type tp """
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
10 def reg_f(f):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
11 f_map[tp] = f
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
12 return f
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
13 return reg_f
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
14
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
15 @register(ir.Binop)
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 364
diff changeset
16 @register(ir.Add)
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
17 def binop_to_tree(e):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
18 names = {'+':'ADDI32', '-':'SUBI32', '|':'ORI32', '<<':'SHLI32',
356
52492b304adf Added newline to print
Windel Bouwman
parents: 355
diff changeset
19 '*':'MULI32', '&':'ANDI32', '>>':'SHRI32'}
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
20 op = names[e.operation]
397
5d03c10fe19d Small changes
Windel Bouwman
parents: 394
diff changeset
21 assert e.ty == ir.i32
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
22 return Tree(op, makeTree(e.a), makeTree(e.b))
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
23
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
24 @register(ir.Temp)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
25 def temp_to_tree(e):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
26 t = Tree('REGI32')
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
27 t.value = e
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
28 return t
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
29
364
c49459768aaa Work on globals
Windel Bouwman
parents: 356
diff changeset
30 @register(ir.GlobalVariable)
c49459768aaa Work on globals
Windel Bouwman
parents: 356
diff changeset
31 def global_address_to_tree(e):
c49459768aaa Work on globals
Windel Bouwman
parents: 356
diff changeset
32 t = Tree('GLOBALADDRESS')
c49459768aaa Work on globals
Windel Bouwman
parents: 356
diff changeset
33 t.value = ir.label_name(e)
c49459768aaa Work on globals
Windel Bouwman
parents: 356
diff changeset
34 return t
c49459768aaa Work on globals
Windel Bouwman
parents: 356
diff changeset
35
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
36 @register(ir.Const)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
37 def const_to_tree(e):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
38 if type(e.value) is bytes:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
39 t = Tree('CONSTDATA')
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
40 t.value = e.value
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
41 return t
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
42 elif type(e.value) is int:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
43 t = Tree('CONSTI32')
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
44 t.value = e.value
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
45 return t
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
46 else:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
47 raise Exception('{} not implemented'.format(type(e.value)))
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
48
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
49 @register(ir.Mem)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
50 def mem_to_tree(e):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
51 return Tree('MEMI32', makeTree(e.e))
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
52
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
53 @register(ir.Addr)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
54 def mem_to_tree(e):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
55 return Tree('ADR', makeTree(e.e))
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 323
diff changeset
56
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
57 @register(ir.Call)
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
58 def call_to_tree(e):
323
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
59 t = Tree('CALL')
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
60 t.value = e
e9fe6988497c Used burg for generating expressions
Windel Bouwman
parents: 322
diff changeset
61 return t
322
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
62
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
63 def makeTree(ir_node):
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
64 """ Transform an ir node into a tree usable for matching """
44f336460c2a Half of use of burg spec for arm
Windel Bouwman
parents:
diff changeset
65 return f_map[type(ir_node)](ir_node)