annotate python/ppci/ir.py @ 364:c49459768aaa

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