annotate python/ppci/ir.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents e30a77ae359b
children d1ecc493384e
rev   line source
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
1 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
2 Intermediate representation (IR) code classes.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
3 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
4
310
e95e5572cd6d Added utils doc page
Windel Bouwman
parents: 309
diff changeset
5
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
6 class Module:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
7 """ Container unit for variables and functions. """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
8 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
9 self.name = name
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
10 self.functions = []
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
11 self.variables = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
12
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
13 def __repr__(self):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
14 return 'module {0}'.format(self.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
15
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
16 def add_function(self, f):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
17 """ Add a function to this module """
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
18 self.functions.append(f)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
19
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
20 def addVariable(self, v):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
21 self.variables.append(v)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
22
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
23 def getVariables(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
24 return self.variables
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
25
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
26 Variables = property(getVariables)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
27
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
28 def getFunctions(self):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
29 return self.functions
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
30
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
31 Functions = property(getFunctions)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
32
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
33 def findFunction(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
34 for f in self.funcs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
35 if f.name == name:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
36 return f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
37 raise KeyError(name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
38
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
39 getFunction = findFunction
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
40
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
41
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
42 class Function:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
43 """ Represents a function. """
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
44 def __init__(self, name, module=None):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
45 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
46 self.entry = Block('{}_entry'.format(name))
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
47 self.entry.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
48 self.epiloog = Block('{}_epilog'.format(name))
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
49 self.epiloog.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
50 self.epiloog.addInstruction(Terminator())
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
51 self.return_value = Temp('{}_retval'.format(name))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
52 self.arguments = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
53 self.localvars = []
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
54 if module:
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
55 module.add_function(self)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
56
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
57 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
58 args = ','.join(str(a) for a in self.arguments)
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
59 return 'function i32 {}({})'.format(self.name, args)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
60
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
61 def add_block(self, bb):
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
62 #self.bbs.append(bb)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
63 bb.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
64
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
65 def removeBlock(self, bb):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
66 #self.bbs.remove(bb)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
67 bb.function = None
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
68
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
69 def getBlocks(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
70 bbs = [self.entry]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
71 worklist = [self.entry]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
72 while worklist:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
73 b = worklist.pop()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
74 for sb in b.Successors:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
75 if sb not in bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
76 bbs.append(sb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
77 worklist.append(sb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
78 bbs.remove(self.entry)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
79 if self.epiloog in bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
80 bbs.remove(self.epiloog)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
81 bbs.insert(0, self.entry)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
82 bbs.append(self.epiloog)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
83 return bbs
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
84
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
85 def findBasicBlock(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
86 for bb in self.bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
87 if bb.name == name:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
88 return bb
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
89 raise KeyError(name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
90
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
91 Blocks = property(getBlocks)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
92
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
93 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
94 def Entry(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
95 return self.entry
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
96
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
97 def check(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
98 for b in self.Blocks:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
99 b.check()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
100
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
101 def addParameter(self, p):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
102 assert type(p) is Parameter
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
103 p.num = len(self.arguments)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
104 self.arguments.append(p)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
105
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
106 def addLocal(self, l):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
107 assert type(l) is LocalVariable
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
108 self.localvars.append(l)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
109
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
110
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
111 class Block:
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
112 """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
113 Uninterrupted sequence of instructions with a label at the start.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
114 """
317
e30a77ae359b Added glue blocks
Windel Bouwman
parents: 312
diff changeset
115 def __init__(self, name, function=None):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
116 self.name = name
317
e30a77ae359b Added glue blocks
Windel Bouwman
parents: 312
diff changeset
117 self.function = function
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
118 self.instructions = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
119
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
120 parent = property(lambda s: s.function)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
121
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
122 def __repr__(self):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
123 return '{0}:'.format(self.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
124
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
125 def addInstruction(self, i):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
126 i.parent = self
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
127 assert not isinstance(self.LastInstruction, LastStatement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
128 self.instructions.append(i)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
129
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
130 def replaceInstruction(self, i1, i2):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
131 idx = self.instructions.index(i1)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
132 i1.parent = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
133 i1.delete()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
134 i2.parent = self
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
135 self.instructions[idx] = i2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
136
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
137 def removeInstruction(self, i):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
138 i.parent = None
317
e30a77ae359b Added glue blocks
Windel Bouwman
parents: 312
diff changeset
139 #i.delete()
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
140 self.instructions.remove(i)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
141
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
142 @property
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
143 def Instructions(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
144 return self.instructions
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
145
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
146 @property
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
147 def LastInstruction(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
148 if not self.Empty:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
149 return self.instructions[-1]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
150
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
151 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
152 def Empty(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
153 return len(self.instructions) == 0
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
154
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
155 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
156 def FirstInstruction(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
157 return self.instructions[0]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
158
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
159 def getSuccessors(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
160 if not self.Empty:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
161 return self.LastInstruction.Targets
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
162 return []
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
163
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
164 Successors = property(getSuccessors)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
165
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
166 def getPredecessors(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
167 preds = []
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
168 for bb in self.parent.Blocks:
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
169 if self in bb.Successors:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
170 preds.append(bb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
171 return preds
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
172
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
173 Predecessors = property(getPredecessors)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
174
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
175 def precedes(self, other):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
176 raise NotImplementedError()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
177
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
178
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
179 # Instructions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
180
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
181 class Expression:
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
182 """ Base class for an expression """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
183 pass
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
184
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
185
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
186 class Const(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
187 """ Represents a constant value """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
188 def __init__(self, value):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
189 self.value = value
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
190
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
191 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
192 return 'Const {}'.format(self.value)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
193
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
194
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
195 class Call(Expression):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
196 """ Call a function with some arguments """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
197 def __init__(self, f, arguments):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
198 self.f = f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
199 self.arguments = arguments
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
200
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
201 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
202 args = ', '.join([str(arg) for arg in self.arguments])
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
203 return '{}({})'.format(self.f, args)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
204
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
205
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
206 # Data operations
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
207 class Binop(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
208 """ Generic binary operation """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
209 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
210
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
211 def __init__(self, value1, operation, value2):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
212 assert operation in Binop.ops
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
213 self.a = value1
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
214 self.b = value2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
215 self.operation = operation
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
216
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
217 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
218 a, b = self.a, self.b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
219 return '({} {} {})'.format(a, self.operation, b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
220
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
221
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
222 def Add(a, b):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
223 """ Add a and b """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
224 return Binop(a, '+', b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
225
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
226
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
227 def Sub(a, b):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
228 """ Substract b from a """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
229 return Binop(a, '-', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
230
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
231
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
232 def Mul(a, b):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
233 """ Multiply a by b """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
234 return Binop(a, '*', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
235
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
236
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
237 def Div(a, b):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
238 """ Divide a in b pieces """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
239 return Binop(a, '/', b)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
240
303
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
241
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
242 class Eseq(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
243 """ Sequence of instructions where the last is an expression """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
244 def __init__(self, stmt, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
245 self.stmt = stmt
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
246 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
247
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
248 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
249 return '({}, {})'.format(self.stmt, self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
250
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
251
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
252 class Alloc(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
253 """ Allocates space on the stack """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
254 def __init__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
255 super().__init__()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
256
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
257 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
258 return 'Alloc'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
259
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
260
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
261 class Variable(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
262 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
263 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
264
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
265 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
266 return 'Var {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
267
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
268
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
269 class LocalVariable(Variable):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
270 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
271 return 'Local {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
272
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
273
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
274 class Parameter(Variable):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
275 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
276 return 'Param {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
278
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
279 class Temp(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
280 """ Temporary storage, same as register """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
281 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
282 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
283
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
284 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
285 return 'TMP_{}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
286
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
287
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
288 class Mem(Expression):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
289 """ Memory access """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
290 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
291 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
292
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
293 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
294 return '[{}]'.format(self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
295
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
296
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
297 class Statement:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
298 """ Base class for all instructions. """
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
299 @property
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
300 def IsTerminator(self):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
301 return isinstance(self, LastStatement)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
302
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
303
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
304 class Move(Statement):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
305 """ Move source to destination """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
306 def __init__(self, dst, src):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
307 self.dst = dst
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
308 self.src = src
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
309
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
310 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
311 return '{} = {}'.format(self.dst, self.src)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
312
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
313
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
314 class Exp(Statement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
315 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
316 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
317
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
318 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
319 return '{}'.format(self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
320
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
321
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
322 # Branching:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
323 class LastStatement(Statement):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
324 def changeTarget(self, old, new):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
325 idx = self.Targets.index(old)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
326 self.Targets[idx] = new
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
327
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
328
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
329 class Terminator(LastStatement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
330 """ Instruction that terminates the terminal block """
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
331 def __init__(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
332 self.Targets = []
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
333
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
334 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
335 return 'Terminator'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
336
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
337
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
338 class Jump(LastStatement):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
339 """ Jump statement to some target location """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
340 def __init__(self, target):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
341 self.Targets = [target]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
342
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
343 def setTarget(self, t):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
344 self.Targets[0] = t
310
e95e5572cd6d Added utils doc page
Windel Bouwman
parents: 309
diff changeset
345
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
346 target = property(lambda s: s.Targets[0], setTarget)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
347
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
348 def __repr__(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
349 return 'JUMP {}'.format(self.target.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
350
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
351
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
352 class CJump(LastStatement):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
353 """ Conditional jump to true or false labels. """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
354 conditions = ['==', '<', '>', '>=', '<=', '!=']
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
355
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
356 def __init__(self, a, cond, b, lab_yes, lab_no):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
357 assert cond in CJump.conditions
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
358 self.a = a
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
359 self.cond = cond
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
360 self.b = b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
361 self.Targets = [lab_yes, lab_no]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
362
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
363 lab_yes = property(lambda s: s.Targets[0])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
364 lab_no = property(lambda s: s.Targets[1])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
365
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
366 def __repr__(self):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
367 return 'IF {} {} {} THEN {} ELSE {}'\
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
368 .format(self.a, self.cond, self.b, self.lab_yes, self.lab_no)