annotate python/ppci/ir.py @ 386:2a970e7270e2

Added repeat assembler macro
author Windel Bouwman
date Thu, 01 May 2014 17:40:59 +0200
parents 9667d78ba79e
children 988f3fb861e4
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
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 364
diff changeset
44 def get_functions(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
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 364
diff changeset
47 Functions = property(get_functions)
277
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
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 364
diff changeset
197 class Value:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 364
diff changeset
198 pass
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 364
diff changeset
199
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
200 class Expression:
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
201 """ Base class for an expression """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
202 pass
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
203
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
204
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
205 class Const(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
206 """ Represents a constant value """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
207 def __init__(self, value):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
208 self.value = value
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
209
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
210 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
211 return 'Const {}'.format(self.value)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
212
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
213
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
214 class Call(Expression):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
215 """ Call a function with some arguments """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
216 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
217 assert type(f) is str
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
218 self.f = f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
219 self.arguments = arguments
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
220
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
221 def __repr__(self):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
222 args = ', '.join(str(arg) for arg in self.arguments)
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
223 return '{}({})'.format(self.f, args)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
224
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
225
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
226 # Data operations
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
227 class Binop(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
228 """ Generic binary operation """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
229 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
230
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
231 def __init__(self, value1, operation, value2):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
232 assert operation in Binop.ops
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 364
diff changeset
233 #assert type(value1) is type(value2)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
234 self.a = value1
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
235 self.b = value2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
236 self.operation = operation
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
237
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
238 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
239 a, b = self.a, self.b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
240 return '({} {} {})'.format(a, self.operation, b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
241
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
242
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
243 def Add(a, b):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
244 """ Add a and b """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
245 return Binop(a, '+', b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
246
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
247
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
248 def Sub(a, b):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
249 """ Substract b from a """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
250 return Binop(a, '-', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
251
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
252
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
253 def Mul(a, b):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
254 """ Multiply a by b """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
255 return Binop(a, '*', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
256
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
257
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
258 def Div(a, b):
309
68b01c8abf8a Added start of ir read and write
Windel Bouwman
parents: 307
diff changeset
259 """ Divide a in b pieces """
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
260 return Binop(a, '/', b)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
261
303
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
262
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
263 class Eseq(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
264 """ Sequence of instructions where the last is an expression """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
265 def __init__(self, stmt, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
266 self.stmt = stmt
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
267 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
268
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
269 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
270 return '({}, {})'.format(self.stmt, self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
271
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
272
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
273 class Alloc(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
274 """ Allocates space on the stack """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
275 def __init__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
276 super().__init__()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
278 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
279 return 'Alloc'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
280
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
281
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
282 class Variable(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
283 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
284 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
285
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
286 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
287 return 'Var {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
288
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
289
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
290 class LocalVariable(Variable):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
291 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
292 return 'Local {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
293
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
294
363
396e5cefba13 Removed debuginfo instruction
Windel Bouwman
parents: 354
diff changeset
295 class GlobalVariable(Variable):
396e5cefba13 Removed debuginfo instruction
Windel Bouwman
parents: 354
diff changeset
296 def __repr__(self):
396e5cefba13 Removed debuginfo instruction
Windel Bouwman
parents: 354
diff changeset
297 return 'Global {}'.format(self.name)
396e5cefba13 Removed debuginfo instruction
Windel Bouwman
parents: 354
diff changeset
298
396e5cefba13 Removed debuginfo instruction
Windel Bouwman
parents: 354
diff changeset
299
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
300 class Parameter(Variable):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
301 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
302 return 'Param {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
303
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
304
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
305 class Temp(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
306 """ Temporary storage, same as register """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
307 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
308 self.name = name
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 'TMP_{}'.format(self.name)
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 Mem(Expression):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
315 """ Memory access """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
316 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
317 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
318
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
319 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
320 return '[{}]'.format(self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
321
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
322
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
323 class Addr(Expression):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
324 """ Address of label """
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
325 def __init__(self, e):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
326 self.e = e
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
327
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
328 def __repr__(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
329 return '&{}'.format(self.e)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
330
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
331
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
332 class Statement:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
333 """ Base class for all instructions. """
312
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
334 @property
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
335 def IsTerminator(self):
2c9768114877 Added cool logging formatter
Windel Bouwman
parents: 310
diff changeset
336 return isinstance(self, LastStatement)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
337
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
338
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
339 class Move(Statement):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
340 """ Move source to destination """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
341 def __init__(self, dst, src):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
342 self.dst = dst
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
343 self.src = src
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
344
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
345 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
346 return '{} = {}'.format(self.dst, self.src)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
347
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
348
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
349 class Exp(Statement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
350 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
351 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
352
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
353 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
354 return '{}'.format(self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
355
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
356
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
357 # Branching:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
358 class LastStatement(Statement):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
359 def changeTarget(self, old, new):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
360 idx = self.Targets.index(old)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
361 self.Targets[idx] = new
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
362
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
363
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
364 class Terminator(LastStatement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
365 """ Instruction that terminates the terminal block """
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
366 def __init__(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
367 self.Targets = []
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
368
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
369 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
370 return 'Terminator'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
371
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
372
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
373 class Jump(LastStatement):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
374 """ Jump statement to some target location """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
375 def __init__(self, target):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
376 self.Targets = [target]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
377
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
378 def setTarget(self, t):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
379 self.Targets[0] = t
310
e95e5572cd6d Added utils doc page
Windel Bouwman
parents: 309
diff changeset
380
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
381 target = property(lambda s: s.Targets[0], setTarget)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
382
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
383 def __repr__(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
384 return 'JUMP {}'.format(self.target.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
385
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
386
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
387 class CJump(LastStatement):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
388 """ Conditional jump to true or false labels. """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
389 conditions = ['==', '<', '>', '>=', '<=', '!=']
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
390
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
391 def __init__(self, a, cond, b, lab_yes, lab_no):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
392 assert cond in CJump.conditions
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
393 self.a = a
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
394 self.cond = cond
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
395 self.b = b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
396 self.Targets = [lab_yes, lab_no]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
397
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
398 lab_yes = property(lambda s: s.Targets[0])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
399 lab_no = property(lambda s: s.Targets[1])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
400
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
401 def __repr__(self):
305
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
402 return 'IF {} {} {} THEN {} ELSE {}'\
0615b5308710 Updated docs
Windel Bouwman
parents: 304
diff changeset
403 .format(self.a, self.cond, self.b, self.lab_yes, self.lab_no)