annotate python/ppci/ir.py @ 312:2c9768114877

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