annotate python/ir/instruction.py @ 239:63bb40758066

added check
author Windel Bouwman
date Mon, 22 Jul 2013 17:57:25 +0200
parents 88a1e0baef65
children c4370696ccc7
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
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
17 def IsUsed(self):
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
18 return len(self.used_by) > 0
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
19
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
20 def onlyUsedInBlock(self, bb):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
21 for use in self.used_by:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
22 ins = use
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
23 if ins.parent != bb:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
24 return False
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
25 return True
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
26
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
27
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
28 class Variable(Value):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
29 pass
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
30
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
31
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
32 class Use:
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
33 def __init__(self, user, val):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
34 self.user = user
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
35 assert isinstance(val, Value)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
36 self.val = val
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
37 self.val.used_by.append(self.user)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
38
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
39 def delete(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
40 self.val.used_by.remove(self.user)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
41
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
42
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
43 class Instruction:
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
44 """ Base class for all instructions. """
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
45 def __init__(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
46 # live variables at this node:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
47 self.live_in = set()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
48 self.live_out = set()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
49 # What variables this instruction uses and defines:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
50 self.defs = []
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
51 self.uses = []
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
52 def delete(self):
209
07bfea4c1ed7 Added codegen test
Windel Bouwman
parents: 205
diff changeset
53 while self.uses:
07bfea4c1ed7 Added codegen test
Windel Bouwman
parents: 205
diff changeset
54 use = self.uses.pop()
07bfea4c1ed7 Added codegen test
Windel Bouwman
parents: 205
diff changeset
55 use.delete()
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
56 while self.defs:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
57 d = self.defs.pop()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
58 d.Setter = None
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
59
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
60 def addUse(self, val):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
61 self.uses.append(Use(self, val))
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
62
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
63 def removeUse(self, val):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
64 for u in self.uses:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
65 if u.val is val:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
66 theUse = u
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
67 theUse.delete()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
68 self.uses.remove(theUse)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
69
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
70 def addDef(self, v):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
71 self.defs.append(v)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
72 assert v.Setter == None
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
73 v.Setter = self
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
74
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
75 def removeDef(self, v):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
76 assert v.Setter is self
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
77 v.Setter = None
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
78 self.defs.remove(v)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
79
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
80 def getParent(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
81 return self.parent
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
82
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
83 def setParent(self, p):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
84 self.parent = p
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
85 Parent = property(getParent, setParent)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
86
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
87 def replaceValue(self, old, new):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
88 raise NotImplementedError()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
89
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
90 @property
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
91 def Position(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
92 return self.parent.Instructions.index(self)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
93
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
94 def check(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
95 # Check that the variables defined by this instruction
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
96 # are only used in the same block
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
97 for v in self.defs:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
98 assert v.Setter is self
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
99 for ub in v.used_by:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
100 assert ub.parent == self.parent
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
101
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
102 # Check that variables used are defined earlier:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
103 for u in self.uses:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
104 v = u.val
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
105 assert self.Position > v.Setter.Position
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
106
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
107
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
108
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
109
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
110 class Terminator(Instruction):
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
111 @property
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
112 def Targets(self):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
113 return self.getTargets()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
114
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
115 def changeTarget(self, tfrom, tto):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
116 pass
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
117
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
118
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
119 # Function calling:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
120 class Call(Instruction):
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
121 def __init__(self, callee, arguments, result=None):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
122 super().__init__()
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
123 self.callee = callee
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
124 assert type(callee) is Function
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
125 self.arguments = arguments
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
126 for arg in arguments:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
127 assert type(arg) is Value
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
128 self.addUse(arg)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
129 self.result = result
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
130 if result:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
131 assert type(result) is Value
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
132 self.addDef(result)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
133 def __repr__(self):
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
134 if self.result:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
135 pfx = '{0} = '.format(self.result)
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
136 else:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
137 pfx = ''
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
138 args = ','.join([str(arg) for arg in self.arguments])
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
139 return pfx + '{0}({1})'.format(self.callee.name, args)
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
140
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
141 class Return(Terminator):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
142 def __init__(self, value=None):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
143 super().__init__()
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
144 self.value = value
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
145 if value:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
146 self.addUse(value)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
147 def __repr__(self):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
148 if self.value:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
149 return 'ret {0}'.format(self.value)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
150 else:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
151 return 'ret'
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
152 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
153 return []
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
154
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
155 class Alloc(Instruction):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
156 """ Allocates space on the stack """
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
157 def __init__(self, value):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
158 super().__init__()
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
159 assert isinstance(value, Value)
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
160 self.value = value
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
161 self.addDef(value)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
162 def __repr__(self):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
163 return '{0} = alloc'.format(self.value)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
164
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
165 class ImmLoad(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
166 def __init__(self, target, value):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
167 super().__init__()
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 177
diff changeset
168 assert type(target) is Value
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
169 self.target = target
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
170 self.value = value
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
171 self.addDef(target)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
172 def __repr__(self):
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
173 return '{} = {}'.format(self.target, self.value)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
174
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
175 # Data operations
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
176 class BinaryOperator(Instruction):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
177 def __init__(self, result, operation, value1, value2):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
178 super().__init__()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
179 #print('operation is in binops:', operation in BinOps)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
180 # Check types of the two operands:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
181 assert type(value1) is Value, str(value1) + str(type(value1))
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
182 assert type(value2) is Value, value2
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
183 self.result = result
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
184 self.addDef(result)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
185 self.value1 = value1
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
186 self.value2 = value2
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
187 self.addUse(value1)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
188 self.addUse(value2)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
189 self.operation = operation
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
190
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
191 def __repr__(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
192 a, b = self.value1, self.value2
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
193 return '{} = {} {} {}'.format(self.result, a, self.operation, b)
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
194
239
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
195 def replaceValue(self, old, new):
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
196 if old is self.value1:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
197 self.value1 = new
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
198 elif old is self.value2:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
199 self.value2 = new
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
200 elif old is self.result:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
201 self.result = new
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
202 else:
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
203 raise Exception()
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
204 self.removeUse(old)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
205 self.addUse(new)
63bb40758066 added check
Windel Bouwman
parents: 230
diff changeset
206
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
207 # Memory functions:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
208 class Load(Instruction):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
209 def __init__(self, location, value):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
210 super().__init__()
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
211 assert type(value) is Value
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
212 assert isinstance(location, Value), "Location must be a value"
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
213 self.value = value
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
214 self.addDef(value)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
215 self.location = location
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
216 self.addUse(self.location)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
217 def __repr__(self):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
218 return '{} = [{}]'.format(self.value, self.location)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
219
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
220 class Store(Instruction):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
221 def __init__(self, location, value):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
222 super().__init__()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
223 assert type(value) is Value, value
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
224 assert isinstance(location, Value), "Location must be a value"
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
225 self.location = location
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
226 self.value = value
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
227 self.addUse(value)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
228 self.addUse(location)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
229 def __repr__(self):
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 222
diff changeset
230 return '[{}] = {}'.format(self.location, self.value)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
231
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
232 # Branching:
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
233 class Branch(Terminator):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
234 def __init__(self, target):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
235 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
236 assert type(target) is BasicBlock
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
237 self.target = target
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
238 def __repr__(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
239 return 'BRANCH {0}'.format(self.target)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
240 def getTargets(self):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
241 return [self.target]
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
242 def changeTarget(self, tfrom, tto):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
243 assert tfrom is self.target
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
244 self.target = tto
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
245
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
246 class ConditionalBranch(Terminator):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
247 def __init__(self, a, cond, b, lab1, lab2):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
248 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
249 self.a = a
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
250 assert type(a) is Value
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
251 self.cond = cond
221
848c4b15fd0b pointers
Windel Bouwman
parents: 219
diff changeset
252 assert cond in ['==', '<', '>']
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
253 self.b = b
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
254 self.addUse(a)
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
255 self.addUse(b)
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
256 assert type(b) is Value
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
257 assert type(lab1) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
258 self.lab1 = lab1
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
259 assert type(lab2) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
260 self.lab2 = lab2
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
261 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
262 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
263 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
264 return [self.lab1, self.lab2]
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
265 def changeTarget(self, tfrom, tto):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
266 assert tfrom is self.lab1 or tfrom is self.lab2
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
267 if tfrom is self.lab1:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
268 self.lab1 = tto
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 209
diff changeset
269 elif tfrom is self.lab2:
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
270 self.lab2 = tto
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
271
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
272 class PhiNode(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
273 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
274 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
275 self.incBB = []
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
276 def addIncoming(self, bb):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
277 self.incBB.append(bb)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
278