annotate python/ir/instruction.py @ 262:ed14e077124c

Added conditional branch instructions
author Windel Bouwman
date Fri, 09 Aug 2013 11:30:11 +0200
parents 444b9df2ed99
children f8b5da5784b8
rev   line source
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
1 from .basicblock import BasicBlock
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
2 from .function import Function
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
3
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
4
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
5 class Value:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
6 """ Temporary SSA value (value that is assigned only once! """
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
7 def __init__(self, name):
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
8 # TODO: add typing? for now only handle integers
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
9 self.name = name
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
10 self.used_by = []
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
11 self.Setter = None
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
12
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
13 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
14 return '{0}'.format(self.name) # + str(self.IsUsed)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
15
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
16 @property
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
17 def UsedInBlocks(self):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
18 bbs = [i.parent for i in self.used_by + [self.Setter]]
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
19 assert all(isinstance(bb, BasicBlock) for bb in bbs)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
20 return set(bbs)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
21
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
22 @property
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
23 def IsUsed(self):
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
24 return len(self.used_by) > 0
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
25
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
26 Used = IsUsed
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
27
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
28 def onlyUsedInBlock(self, bb):
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
29 return all(use.Block is bb for use in self.used_by)
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
30
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
31 def lastUse(self, ins):
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
32 assert ins in self.used_by
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
33 return all(not ins.precedes(ub) for ub in self.used_by)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
34
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
35 def replaceby(self, v2):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
36 for use_ins in self.used_by:
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
37 use_ins.replaceValue(self, v2.value)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
38 assert not self.Used
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
39
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
40
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
41 class Variable(Value):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
42 pass
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
43
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
44
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
45 class Use:
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
46 def __init__(self, user, val):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
47 self.user = user
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
48 assert isinstance(val, Value)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
49 self.val = val
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
50 self.val.used_by.append(self.user)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
51
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
52 def delete(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
53 self.val.used_by.remove(self.user)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
54
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
55
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
56 class Instruction:
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
57 """ Base class for all instructions. """
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
58 def __init__(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
59 # live variables at this node:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
60 self.live_in = set()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
61 self.live_out = set()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
62 # What variables this instruction uses and defines:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
63 self.defs = []
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
64 self.uses = []
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
65
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
66 def delete(self):
209
07bfea4c1ed7 Added codegen test
Windel Bouwman
parents: 205
diff changeset
67 while self.uses:
07bfea4c1ed7 Added codegen test
Windel Bouwman
parents: 205
diff changeset
68 use = self.uses.pop()
07bfea4c1ed7 Added codegen test
Windel Bouwman
parents: 205
diff changeset
69 use.delete()
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
70 while self.defs:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
71 d = self.defs.pop()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
72 d.Setter = None
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
73
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
74 def addUse(self, val):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
75 self.uses.append(Use(self, val))
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
76
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
77 def removeUse(self, val):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
78 for u in self.uses:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
79 if u.val is val:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
80 theUse = u
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
81 theUse.delete()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
82 self.uses.remove(theUse)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
83
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
84 def addDef(self, v):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
85 self.defs.append(v)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
86 assert v.Setter == None
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
87 v.Setter = self
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
88
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
89 def removeDef(self, v):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
90 assert v.Setter is self
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
91 v.Setter = None
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
92 self.defs.remove(v)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
93
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
94 def getParent(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
95 return self.parent
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
96
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
97 def setParent(self, p):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
98 self.parent = p
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
99 Parent = property(getParent, setParent)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
100
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
101 def replaceValue(self, old, new):
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
102 # TODO: make this a generic function
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
103 raise NotImplementedError('{}'.format(type(self)))
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
104
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
105 @property
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
106 def Position(self):
258
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
107 return self.Block.Instructions.index(self)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
108
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
109 def precedes(self, other):
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
110 assert isinstance(other, Instruction)
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
111 if self.Block is other.Block:
04c19282a5aa Added register allocator
Windel Bouwman
parents: 252
diff changeset
112 return other.Position > self.Position
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
113 else:
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
114 return self.Block.precedes(other.Block)
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
115
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
116 def follows(self, other):
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
117 pass
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
118
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
119 @property
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
120 def Function(self):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
121 return self.Block.parent
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
122
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
123 @property
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
124 def Block(self):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
125 return self.Parent
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
126
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
127 def check(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
128 # Check that the variables defined by this instruction
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
129 # are only used in the same block
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
130 for v in self.defs:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
131 assert v.Setter is self
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
132 for ub in v.used_by:
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
133 assert ub.Function == self.Function
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
134
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
135 # Check that variables used are defined earlier:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
136 for u in self.uses:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
137 v = u.val
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
138 #assert self.Position > v.Setter.Position
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
139
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
140
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
141
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
142
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
143
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
144
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
145 # Function calling:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
146 class Call(Instruction):
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
147 def __init__(self, callee, arguments, result=None):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
148 super().__init__()
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
149 self.callee = callee
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
150 assert type(callee) is Function
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
151 self.arguments = arguments
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
152 for arg in arguments:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
153 assert type(arg) is Value
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
154 self.addUse(arg)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
155 self.result = result
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
156 if result:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
157 assert type(result) is Value
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
158 self.addDef(result)
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
159
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
160 def __repr__(self):
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
161 if self.result:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
162 pfx = '{0} = '.format(self.result)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
163 else:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
164 pfx = ''
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
165 args = ','.join([str(arg) for arg in self.arguments])
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
166 return pfx + '{0}({1})'.format(self.callee.name, args)
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
167
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
168
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
169 class Alloc(Instruction):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
170 """ Allocates space on the stack """
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
171 def __init__(self, value):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
172 super().__init__()
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
173 assert isinstance(value, Value)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
174 self.value = value
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
175 self.addDef(value)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
176
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
177 def __repr__(self):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
178 return '{0} = alloc'.format(self.value)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
179
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
180
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
181 class ImmLoad(Instruction):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
182 def __init__(self, target, value):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
183 super().__init__()
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
184 assert type(target) is Value
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
185 self.target = target
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
186 self.value = value
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
187 self.addDef(target)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
188
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
189 def __repr__(self):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
190 return '{} = {}'.format(self.target, self.value)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
191
262
ed14e077124c Added conditional branch instructions
Windel Bouwman
parents: 261
diff changeset
192
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
193 # Data operations
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
194 class BinaryOperator(Instruction):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
195 def __init__(self, result, operation, value1, value2):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
196 super().__init__()
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
197 assert type(value1) is Value, str(value1) + str(type(value1))
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
198 assert type(value2) is Value, value2
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
199 self.result = result
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
200 self.addDef(result)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
201 self.value1 = value1
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
202 self.value2 = value2
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
203 self.addUse(value1)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
204 self.addUse(value2)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
205 self.operation = operation
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
206
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
207 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
208 a, b = self.value1, self.value2
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
209 return '{} = {} {} {}'.format(self.result, a, self.operation, b)
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
210
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
211 def replaceValue(self, old, new):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
212 if old is self.value1:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
213 self.value1 = new
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
214 elif old is self.value2:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
215 self.value2 = new
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
216 elif old is self.result:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
217 self.result = new
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
218 else:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
219 raise Exception()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
220 self.removeUse(old)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
221 self.addUse(new)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
222
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
223
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
224 # Memory functions:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
225 class Load(Instruction):
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
226 def __init__(self, location, value):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
227 super().__init__()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
228 assert type(value) is Value
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
229 assert isinstance(location, Value), "Location must be a value"
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
230 self.value = value
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
231 self.addDef(value)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
232 self.location = location
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
233 self.addUse(self.location)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
234
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
235 def __repr__(self):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
236 return '{} = [{}]'.format(self.value, self.location)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
237
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
238
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
239 class Store(Instruction):
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
240 def __init__(self, location, value):
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
241 super().__init__()
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
242 assert type(value) is Value, value
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
243 assert isinstance(location, Value), "Location must be a value"
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
244 self.location = location
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
245 self.value = value
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
246 self.addUse(value)
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
247 self.addUse(location)
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
248
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
249 def __repr__(self):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
250 return '[{}] = {}'.format(self.location, self.value)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
251
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
252 def replaceValue(self, old, new):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
253 if old is self.value:
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
254 self.value = new
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
255 elif old is self.location:
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
256 self.location = new
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
257 else:
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
258 raise Exception()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
259 self.removeUse(old)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
260 self.addUse(new)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
261
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
262
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
263 # Branching:
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
264 class Terminator(Instruction):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
265 @property
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
266 def Targets(self):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
267 return self.getTargets()
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
268
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
269 def changeTarget(self, tfrom, tto):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
270 pass
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
271
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
272
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
273 class Branch(Terminator):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
274 def __init__(self, target):
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
275 super().__init__()
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
276 assert type(target) is BasicBlock
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
277 self.target = target
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
278
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
279 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
280 return 'BRANCH {0}'.format(self.target)
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
281
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
282 def getTargets(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
283 return [self.target]
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
284
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
285 def changeTarget(self, tfrom, tto):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
286 assert tfrom is self.target
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
287 self.target = tto
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
288
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
289
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
290 class ConditionalBranch(Terminator):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
291 def __init__(self, a, cond, b, lab1, lab2):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
292 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
293 self.a = a
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
294 assert type(a) is Value
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
295 self.cond = cond
221
848c4b15fd0b pointers
Windel Bouwman
parents: 219
diff changeset
296 assert cond in ['==', '<', '>']
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
297 self.b = b
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
298 self.addUse(a)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
299 self.addUse(b)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
300 assert type(b) is Value
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
301 assert type(lab1) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
302 self.lab1 = lab1
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
303 assert type(lab2) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
304 self.lab2 = lab2
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
305
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
306 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
307 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
308 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
309 return [self.lab1, self.lab2]
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
310
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
311 def changeTarget(self, tfrom, tto):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
312 assert tfrom is self.lab1 or tfrom is self.lab2
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
313 if tfrom is self.lab1:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
314 self.lab1 = tto
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
315 elif tfrom is self.lab2:
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
316 self.lab2 = tto
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
317
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
318
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
319 class Return(Terminator):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
320 def __init__(self, value=None):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
321 super().__init__()
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
322 self.value = value
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
323 if value:
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
324 self.addUse(value)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
325
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
326 def __repr__(self):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
327 if self.value:
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
328 return 'ret {0}'.format(self.value)
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
329 else:
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
330 return 'ret'
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
331
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
332 def getTargets(self):
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
333 return []
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
334
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 259
diff changeset
335
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
336 class PhiNode(Instruction):
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
337 def __init__(self):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
338 super().__init__()
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
339 self.incBB = []
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
340
252
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
341 def addIncoming(self, bb):
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
342 self.incBB.append(bb)
c4370696ccc7 added optimize function
Windel Bouwman
parents: 239
diff changeset
343