annotate python/ir/instruction.py @ 174:3eb06f5fb987

Added memory alloc for locals
author Windel Bouwman
date Fri, 19 Apr 2013 19:22:52 +0200
parents c1d2b6b9f9a7
children a51b3c956386
rev   line source
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
1 from .basicblock import BasicBlock
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
2
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
3 class Value:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
4 """ Temporary SSA value (value that is assigned only once! """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
5 def __init__(self, name):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
6 # TODO: add typing? for now only handle integers
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
7 self.name = name
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
8 self.interferes = set()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
9 self.reg = None
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
10 self.used_by = []
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
11 def __repr__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
12 if self.reg:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
13 n = self.reg
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
14 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
15 n = self.name
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
16 return '{0}'.format(n) # + str(self.IsUsed)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
17 @property
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
18 def IsUsed(self):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
19 return len(self.used_by) > 0
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
20
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
21 class Use:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
22 def __init__(self, user, val):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
23 self.user = user
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
24 self.val = val
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
25 self.val.used_by.append(self.user)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
26 def delete(self):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
27 self.val.used_by.remove(self.user)
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
28
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
29 class Instruction:
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
30 """ Base class for all instructions. """
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
31 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
32 # live variables at this node:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
33 self.live_in = set()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
34 self.live_out = set()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
35 # What variables this instruction uses and defines:
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
36 self.defs = []
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
37 self.uses = []
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
38 def delete(self):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
39 for use in self.uses:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
40 use.delete()
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
41 self.uses.clear()
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
42 def addUse(self, val):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
43 self.uses.append(Use(self, val))
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
44 def addDef(self, v):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
45 self.defs.append(v)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
46 def getParent(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
47 return self.parent
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
48 def setParent(self, p):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
49 self.parent = p
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
50 Parent = property(getParent, setParent)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
51 @property
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
52 def Targets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
53 return self.getTargets()
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
54
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
55 # Function calling:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
56 class Call(Instruction):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
57 def __init__(self, callee, arguments):
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
58 super().__init__()
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
59 self.callee = callee
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
60 self.arguments = arguments
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
61 def __repr__(self):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
62 return 'CALL {0}'.format(self.callee)
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
63
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
64 class Return(Instruction):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
65 def __init__(self, value=None):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
66 super().__init__()
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
67 self.value = value
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
68 if value:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
69 self.addUse(value)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
70 def __repr__(self):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
71 if self.value:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
72 return 'Return {0}'.format(self.value)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
73 else:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
74 return 'Return'
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
75 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
76 return []
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
77
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
78 class Alloc(Instruction):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
79 """ Allocates space on the stack """
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
80 def __init__(self, value):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
81 super().__init__()
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
82 self.value = value
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
83 self.addDef(value)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
84 def __repr__(self):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
85 return '{0} = alloc'.format(self.value)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
86
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
87 class ImmLoad(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
88 def __init__(self, target, value):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
89 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
90 self.target = target
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
91 self.value = value
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
92 self.addDef(target)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
93 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
94 return '{0} = {1}'.format(self.target, self.value)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
95
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
96 # Data operations
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
97 class BinaryOperator(Instruction):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
98 def __init__(self, result, operation, value1, value2):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
99 super().__init__()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
100 #print('operation is in binops:', operation in BinOps)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
101 # Check types of the two operands:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
102 self.result = result
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
103 self.addDef(result)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
104 self.value1 = value1
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
105 self.value2 = value2
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
106 self.addUse(value1)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
107 self.addUse(value2)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
108 self.operation = operation
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
109 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
110 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
111
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
112 # Memory functions:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
113 class Load(Instruction):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
114 def __init__(self, location, value):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
115 super().__init__()
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
116 assert type(value) is Value
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
117 assert type(location) is Value, "Location must be a value"
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
118 self.value = value
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
119 self.addDef(value)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
120 self.location = location
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
121 self.addUse(self.location)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
122 def __repr__(self):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
123 return '{1} = [{0}]'.format(self.location, self.value)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
124
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
125 class Store(Instruction):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
126 def __init__(self, location, value):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
127 super().__init__()
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
128 assert type(value) is Value
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
129 assert type(location) is Value, "Location must be a value"
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
130 self.location = location
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
131 self.value = value
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
132 self.addUse(value)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
133 def __repr__(self):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
134 return '[{0}] = {1}'.format(self.location, self.value)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
135
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
136 # Branching:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
137 class Branch(Instruction):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
138 def __init__(self, target):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
139 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
140 assert type(target) is BasicBlock
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
141 self.target = target
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
142 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
143 return 'BRANCH {0}'.format(self.target)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
144 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
145 return [self.target]
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
146
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
147 class ConditionalBranch(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
148 def __init__(self, a, cond, b, lab1, lab2):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
149 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
150 self.a = a
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
151 assert type(a) is Value
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
152 self.cond = cond
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
153 self.b = b
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
154 self.addUse(a)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
155 self.addUse(b)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
156 assert type(b) is Value
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
157 assert type(lab1) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
158 self.lab1 = lab1
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
159 assert type(lab2) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
160 self.lab2 = lab2
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
161 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
162 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
163 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
164 return [self.lab1, self.lab2]
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
165
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
166 class PhiNode(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
167 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
168 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
169 self.incBB = []
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
170 def addIncoming(self, bb):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
171 self.incBB.append(bb)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
172