comparison python/ir/instruction.py @ 171:3eb9b9e2958d

Improved IR code
author Windel Bouwman
date Wed, 03 Apr 2013 22:20:20 +0200
parents 4348da5ca307
children c1d2b6b9f9a7
comparison
equal deleted inserted replaced
170:4348da5ca307 171:3eb9b9e2958d
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)
1 16
2 class Instruction: 17 class Instruction:
3 """ Base class for all instructions. """ 18 """ Base class for all instructions. """
4 pass 19 def __init__(self):
5 20 # successors:
6 # Label: 21 self.succ = set()
7 class LabelInstruction(Instruction): 22 # predecessors:
8 def __init__(self, labid): 23 self.pred = set()
9 self.labid = labid 24 # live variables at this node:
10 def __repr__(self): 25 self.live_in = set()
11 return '{0}:'.format(self.labid) 26 self.live_out = set()
27 # What variables this instruction uses and defines:
28 self.defs = set()
29 self.uses = set()
12 30
13 # Function calling: 31 # Function calling:
14 class CallInstruction(Instruction): 32 class Call(Instruction):
15 def __init__(self, callee, arguments): 33 def __init__(self, callee, arguments):
16 super().__init__() 34 super().__init__()
17 self.callee = callee 35 self.callee = callee
18 self.arguments = arguments 36 self.arguments = arguments
19 def __repr__(self): 37 def __repr__(self):
20 return 'CALL {0}'.format(self.callee) 38 return 'CALL {0}'.format(self.callee)
21 39
22 class RetInstruction(Instruction): 40 class Return(Instruction):
23 def __repr__(self): 41 def __repr__(self):
24 return 'RET' 42 return 'RET'
25 43
26 class MoveInstruction(Instruction): 44 class ImmLoad(Instruction):
27 def __init__(self, name, value): 45 def __init__(self, target, value):
28 self.name = name 46 super().__init__()
47 self.target = target
29 self.value = value 48 self.value = value
49 self.defs.add(target)
30 def __repr__(self): 50 def __repr__(self):
31 return '{0} = {1}'.format(self.name, self.value) 51 return '{0} = {1}'.format(self.target, self.value)
32 52
53 # Data operations
33 class BinaryOperator(Instruction): 54 class BinaryOperator(Instruction):
34 def __init__(self, name, operation, value1, value2): 55 def __init__(self, result, operation, value1, value2):
56 super().__init__()
35 #print('operation is in binops:', operation in BinOps) 57 #print('operation is in binops:', operation in BinOps)
36 # Check types of the two operands: 58 # Check types of the two operands:
37 self.name = name 59 self.result = result
60 self.defs.add(result)
38 self.value1 = value1 61 self.value1 = value1
39 self.value2 = value2 62 self.value2 = value2
63 self.uses.add(value1)
64 self.uses.add(value2)
40 self.operation = operation 65 self.operation = operation
41 def __repr__(self): 66 def __repr__(self):
42 return '{0} = {2} {1} {3}'.format(self.name, self.operation, self.value1, self.value2) 67 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
43 68
44 # Memory functions: 69 # Memory functions:
45 class LoadInstruction(Instruction): 70 class Load(Instruction):
46 def __init__(self, name, value): 71 def __init__(self, name, value):
72 super().__init__()
47 self.value = value 73 self.value = value
74 self.defs.add(value)
48 self.name = name 75 self.name = name
49 def __repr__(self): 76 def __repr__(self):
50 return '{1} = [{0}]'.format(self.name, self.value) 77 return '{1} = [{0}]'.format(self.name, self.value)
51 78
52 class StoreInstruction(Instruction): 79 class Store(Instruction):
53 def __init__(self, name, value): 80 def __init__(self, name, value):
81 super().__init__()
54 self.name = name 82 self.name = name
55 self.value = value 83 self.value = value
84 self.uses.add(value)
56 def __repr__(self): 85 def __repr__(self):
57 return '[{0}] = {1}'.format(self.name, self.value) 86 return '[{0}] = {1}'.format(self.name, self.value)
58 87
59 class BranchInstruction(Instruction): 88 # Branching:
89 class Branch(Instruction):
60 def __init__(self, target): 90 def __init__(self, target):
61 self.t1 = target 91 super().__init__()
92 assert type(target) is BasicBlock
93 self.target = target
62 def __repr__(self): 94 def __repr__(self):
63 return 'BRANCH {0}'.format(self.t1) 95 return 'BRANCH {0}'.format(self.target)
64 96
65 class IfInstruction(Instruction): 97 class ConditionalBranch(Instruction):
66 def __init__(self, cond, lab1, lab2): 98 def __init__(self, a, cond, b, lab1, lab2):
99 super().__init__()
100 self.a = a
101 assert type(a) is Value
67 self.cond = cond 102 self.cond = cond
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
68 self.lab1 = lab1 108 self.lab1 = lab1
109 assert type(lab2) is BasicBlock
69 self.lab2 = lab2 110 self.lab2 = lab2
70 def __repr__(self): 111 def __repr__(self):
71 return 'IF {0} THEN {1} ELSE {2}'.format(self.cond, self.lab1, self.lab2) 112 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
72 113
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