comparison python/ir/instruction.py @ 173:c1d2b6b9f9a7

Rework into passes
author Windel Bouwman
date Fri, 19 Apr 2013 12:42:21 +0200
parents 3eb9b9e2958d
children 3eb06f5fb987
comparison
equal deleted inserted replaced
172:5a7d37d615ee 173:c1d2b6b9f9a7
15 return '{0}'.format(n) 15 return '{0}'.format(n)
16 16
17 class Instruction: 17 class Instruction:
18 """ Base class for all instructions. """ 18 """ Base class for all instructions. """
19 def __init__(self): 19 def __init__(self):
20 # successors:
21 self.succ = set()
22 # predecessors:
23 self.pred = set()
24 # live variables at this node: 20 # live variables at this node:
25 self.live_in = set() 21 self.live_in = set()
26 self.live_out = set() 22 self.live_out = set()
27 # What variables this instruction uses and defines: 23 # What variables this instruction uses and defines:
28 self.defs = set() 24 self.defs = set()
29 self.uses = set() 25 self.uses = set()
26 def getParent(self):
27 return self.parent
28 Parent = property(getParent)
29 @property
30 def Targets(self):
31 return self.getTargets()
30 32
31 # Function calling: 33 # Function calling:
32 class Call(Instruction): 34 class Call(Instruction):
33 def __init__(self, callee, arguments): 35 def __init__(self, callee, arguments):
34 super().__init__() 36 super().__init__()
38 return 'CALL {0}'.format(self.callee) 40 return 'CALL {0}'.format(self.callee)
39 41
40 class Return(Instruction): 42 class Return(Instruction):
41 def __repr__(self): 43 def __repr__(self):
42 return 'RET' 44 return 'RET'
45 def getTargets(self):
46 return []
43 47
44 class ImmLoad(Instruction): 48 class ImmLoad(Instruction):
45 def __init__(self, target, value): 49 def __init__(self, target, value):
46 super().__init__() 50 super().__init__()
47 self.target = target 51 self.target = target
91 super().__init__() 95 super().__init__()
92 assert type(target) is BasicBlock 96 assert type(target) is BasicBlock
93 self.target = target 97 self.target = target
94 def __repr__(self): 98 def __repr__(self):
95 return 'BRANCH {0}'.format(self.target) 99 return 'BRANCH {0}'.format(self.target)
100 def getTargets(self):
101 return [self.target]
96 102
97 class ConditionalBranch(Instruction): 103 class ConditionalBranch(Instruction):
98 def __init__(self, a, cond, b, lab1, lab2): 104 def __init__(self, a, cond, b, lab1, lab2):
99 super().__init__() 105 super().__init__()
100 self.a = a 106 self.a = a
108 self.lab1 = lab1 114 self.lab1 = lab1
109 assert type(lab2) is BasicBlock 115 assert type(lab2) is BasicBlock
110 self.lab2 = lab2 116 self.lab2 = lab2
111 def __repr__(self): 117 def __repr__(self):
112 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2) 118 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
119 def getTargets(self):
120 return [self.lab1, self.lab2]
113 121
114 class PhiNode(Instruction): 122 class PhiNode(Instruction):
115 def __init__(self): 123 def __init__(self):
116 super().__init__() 124 super().__init__()
117 self.incBB = [] 125 self.incBB = []