171
|
1 from .basicblock import BasicBlock
|
|
2
|
|
3 class Value:
|
|
4 """ Temporary SSA value (value that is assigned only once! """
|
|
5 def __init__(self, name):
|
|
6 # TODO: add typing? for now only handle integers
|
|
7 self.name = name
|
|
8 self.interferes = set()
|
|
9 self.reg = None
|
|
10 def __repr__(self):
|
|
11 if self.reg:
|
|
12 n = self.reg
|
|
13 else:
|
|
14 n = self.name
|
|
15 return '{0}'.format(n)
|
110
|
16
|
155
|
17 class Instruction:
|
104
|
18 """ Base class for all instructions. """
|
171
|
19 def __init__(self):
|
|
20 # successors:
|
|
21 self.succ = set()
|
|
22 # predecessors:
|
|
23 self.pred = set()
|
|
24 # live variables at this node:
|
|
25 self.live_in = set()
|
|
26 self.live_out = set()
|
|
27 # What variables this instruction uses and defines:
|
|
28 self.defs = set()
|
|
29 self.uses = set()
|
170
|
30
|
158
|
31 # Function calling:
|
171
|
32 class Call(Instruction):
|
110
|
33 def __init__(self, callee, arguments):
|
|
34 super().__init__()
|
|
35 self.callee = callee
|
|
36 self.arguments = arguments
|
158
|
37 def __repr__(self):
|
|
38 return 'CALL {0}'.format(self.callee)
|
110
|
39
|
171
|
40 class Return(Instruction):
|
158
|
41 def __repr__(self):
|
|
42 return 'RET'
|
70
|
43
|
171
|
44 class ImmLoad(Instruction):
|
|
45 def __init__(self, target, value):
|
|
46 super().__init__()
|
|
47 self.target = target
|
170
|
48 self.value = value
|
171
|
49 self.defs.add(target)
|
170
|
50 def __repr__(self):
|
171
|
51 return '{0} = {1}'.format(self.target, self.value)
|
170
|
52
|
171
|
53 # Data operations
|
70
|
54 class BinaryOperator(Instruction):
|
171
|
55 def __init__(self, result, operation, value1, value2):
|
|
56 super().__init__()
|
157
|
57 #print('operation is in binops:', operation in BinOps)
|
104
|
58 # Check types of the two operands:
|
171
|
59 self.result = result
|
|
60 self.defs.add(result)
|
104
|
61 self.value1 = value1
|
|
62 self.value2 = value2
|
171
|
63 self.uses.add(value1)
|
|
64 self.uses.add(value2)
|
104
|
65 self.operation = operation
|
158
|
66 def __repr__(self):
|
171
|
67 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
|
70
|
68
|
170
|
69 # Memory functions:
|
171
|
70 class Load(Instruction):
|
158
|
71 def __init__(self, name, value):
|
171
|
72 super().__init__()
|
157
|
73 self.value = value
|
171
|
74 self.defs.add(value)
|
158
|
75 self.name = name
|
|
76 def __repr__(self):
|
170
|
77 return '{1} = [{0}]'.format(self.name, self.value)
|
157
|
78
|
171
|
79 class Store(Instruction):
|
160
|
80 def __init__(self, name, value):
|
171
|
81 super().__init__()
|
160
|
82 self.name = name
|
|
83 self.value = value
|
171
|
84 self.uses.add(value)
|
160
|
85 def __repr__(self):
|
170
|
86 return '[{0}] = {1}'.format(self.name, self.value)
|
160
|
87
|
171
|
88 # Branching:
|
|
89 class Branch(Instruction):
|
170
|
90 def __init__(self, target):
|
171
|
91 super().__init__()
|
|
92 assert type(target) is BasicBlock
|
|
93 self.target = target
|
158
|
94 def __repr__(self):
|
171
|
95 return 'BRANCH {0}'.format(self.target)
|
170
|
96
|
171
|
97 class ConditionalBranch(Instruction):
|
|
98 def __init__(self, a, cond, b, lab1, lab2):
|
|
99 super().__init__()
|
|
100 self.a = a
|
|
101 assert type(a) is Value
|
170
|
102 self.cond = cond
|
171
|
103 self.b = b
|
|
104 self.uses.add(a)
|
|
105 self.uses.add(b)
|
|
106 assert type(b) is Value
|
|
107 assert type(lab1) is BasicBlock
|
170
|
108 self.lab1 = lab1
|
171
|
109 assert type(lab2) is BasicBlock
|
170
|
110 self.lab2 = lab2
|
|
111 def __repr__(self):
|
171
|
112 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
|
170
|
113
|
171
|
114 class PhiNode(Instruction):
|
|
115 def __init__(self):
|
|
116 super().__init__()
|
|
117 self.incBB = []
|
|
118 def addIncoming(self, bb):
|
|
119 self.incBB.append(bb)
|
|
120
|