annotate python/ppci/ir.py @ 394:988f3fb861e4

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