annotate python/ppci/ir.py @ 304:fa99f36fabb5

Fix docs
author Windel Bouwman
date Fri, 06 Dec 2013 12:45:02 +0100
parents be7f60545368
children 0615b5308710
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
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
5
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
6 def dumpgv(m, outf):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
7 print('digraph G ', file=outf)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
8 print('{', file=outf)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
9 for f in m.Functions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
10 print('{} [label="{}" shape=box3d]'.format(id(f), f),file=outf)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
11 for bb in f.Blocks:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
12 contents = str(bb) + '\n'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
13 contents += '\n'.join([str(i) for i in bb.Instructions])
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
14 outf.write('{0} [shape=note label="{1}"];\n'.format(id(bb), contents))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
15 for successor in bb.Successors:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
16 print('"{}" -> "{}"\n'.format(id(bb), id(successor)), file=outf)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
17
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
18 outf.write('"{}" -> "{}" [label="entry"]\n'.format(id(f), id(f.entry)))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
19 print('}', file=outf)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
20
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
21
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
22 class Module:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
23 """ Main container of variables and functions. """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
24 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
25 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
26 self.funcs = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
27 self.variables = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
28
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
29 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
30 return 'IR-module [{0}]'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
31
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
32 def addFunc(self, f):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
33 self.funcs.append(f)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
34
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
35 addFunction = addFunc
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
36
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
37 def addVariable(self, v):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
38 self.variables.append(v)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
39
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
40 def getVariables(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
41 return self.variables
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
42
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
43 Variables = property(getVariables)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
44
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
45 def getFunctions(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
46 return self.funcs
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
47
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
48 Functions = property(getFunctions)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
49
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
50 def findFunction(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
51 for f in self.funcs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
52 if f.name == name:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
53 return f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
54 raise KeyError(name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
55
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
56 getFunction = findFunction
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
57
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
58 def dump(self, indent=' '):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
59 print(self)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
60 for v in self.Variables:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
61 print(indent, v)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
62 for fn in self.Functions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
63 fn.dump(indent=indent+' ')
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
64
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
65 # Analysis functions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
66 def check(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
67 """ Perform sanity check on module """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
68 for f in self.Functions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
69 f.check()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
70
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
71
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
72 class Function:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
73 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
74 Function definition. Contains an entry block.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
75 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
76 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
77 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
78 self.entry = Block('{}_entry'.format(name))
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
79 self.entry.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
80 self.epiloog = Block('{}_epilog'.format(name))
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
81 self.epiloog.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
82 self.epiloog.addInstruction(Terminator())
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
83 self.return_value = Temp('{}_retval'.format(name))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
84 self.arguments = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
85 self.localvars = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
86
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
87 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
88 args = ','.join(str(a) for a in self.arguments)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
89 return 'Function {}({})'.format(self.name, args)
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 addBlock(self, bb):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
92 self.bbs.append(bb)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
93 bb.function = self
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
94
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
95 def removeBlock(self, bb):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
96 #self.bbs.remove(bb)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
97 bb.function = None
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
98
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
99 def getBlocks(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
100 bbs = [self.entry]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
101 worklist = [self.entry]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
102 while worklist:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
103 b = worklist.pop()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
104 for sb in b.Successors:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
105 if sb not in bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
106 bbs.append(sb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
107 worklist.append(sb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
108 bbs.remove(self.entry)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
109 if self.epiloog in bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
110 bbs.remove(self.epiloog)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
111 bbs.insert(0, self.entry)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
112 bbs.append(self.epiloog)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
113 return bbs
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
114
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
115 def findBasicBlock(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
116 for bb in self.bbs:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
117 if bb.name == name:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
118 return bb
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
119 raise KeyError(name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
120
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
121 Blocks = property(getBlocks)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
122
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
123 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
124 def Entry(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
125 return self.entry
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
126
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
127 def check(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
128 for b in self.Blocks:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
129 b.check()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
130
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
131 def addParameter(self, p):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
132 assert type(p) is Parameter
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
133 p.num = len(self.arguments)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
134 self.arguments.append(p)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
135
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
136 def addLocal(self, l):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
137 assert type(l) is LocalVariable
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
138 self.localvars.append(l)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
139
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
140 def dump(self, indent=''):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
141 print(indent+str(self))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
142 for bb in self.Blocks:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
143 print(indent+' '+str(bb))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
144 for ins in bb.Instructions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
145 print(indent +' '*2 + str(ins))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
146
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
147
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
148 class Block:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
149 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
150 Uninterrupted sequence of instructions with a label at the start.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
151 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
152 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
153 self.name = name
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
154 self.function = None
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
155 self.instructions = []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
156
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
157 parent = property(lambda s: s.function)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
158
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
159 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
160 return 'Block {0}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
161
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
162 def addInstruction(self, i):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
163 i.parent = self
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
164 assert not isinstance(self.LastInstruction, LastStatement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
165 self.instructions.append(i)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
166
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
167 def replaceInstruction(self, i1, i2):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
168 idx = self.instructions.index(i1)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
169 i1.parent = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
170 i1.delete()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
171 i2.parent = self
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
172 self.instructions[idx] = i2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
173
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
174 def removeInstruction(self, i):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
175 i.parent = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
176 i.delete()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
177 self.instructions.remove(i)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
178
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
179 @property
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
180 def Instructions(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
181 return self.instructions
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
182
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
183 @property
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
184 def LastInstruction(self):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
185 if not self.Empty:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
186 return self.instructions[-1]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
187
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
188 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
189 def Empty(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
190 return len(self.instructions) == 0
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
191
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
192 @property
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
193 def FirstInstruction(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
194 return self.instructions[0]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
195
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
196 def getSuccessors(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
197 if not self.Empty:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
198 return self.LastInstruction.Targets
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
199 return []
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
200 Successors = property(getSuccessors)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
201
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
202 def getPredecessors(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
203 preds = []
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
204 for bb in self.parent.Blocks:
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
205 if self in bb.Successors:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
206 preds.append(bb)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
207 return preds
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
208 Predecessors = property(getPredecessors)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
209
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
210 def precedes(self, other):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
211 raise NotImplementedError()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
212
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
213 def check(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
214 assert isinstance(self.LastInstruction, LastStatement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
215 for i in self.instructions[:-1]:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
216 assert not isinstance(i, LastStatement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
217
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
218
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
219 # Instructions:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
220 class Term:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
221 def __init__(self, x):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
222 self.x = x
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
223
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
224 def match_tree(tree, pattern):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
225 if type(pattern) is Term:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
226 return True, {pattern: tree}
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
227 elif type(pattern) is Binop and type(tree) is Binop and tree.operation == pattern.operation:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
228 res_a, mp_a = match_tree(tree.a, pattern.a)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
229 res_b, mp_b = match_tree(tree.b, pattern.b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
230 assert not (mp_a.keys() & mp_b.keys())
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
231 mp_a.update(mp_b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
232 return res_a and res_b, mp_a
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
233 elif type(pattern) is Const and type(tree) is Const and pattern.value == tree.value:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
234 return True, {}
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
235 else:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
236 return False, {}
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
237
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
238
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
239 class Expression:
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
240 """ Base class for an expression """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
241 pass
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
242
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
243
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
244 class Const(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
245 """ Represents a constant value """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
246 def __init__(self, value):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
247 self.value = value
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
248
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
249 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
250 return 'Const {}'.format(self.value)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
251
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
252
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
253 # Function calling:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
254 class Call(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
255 def __init__(self, f, arguments):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
256 self.f = f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
257 assert type(f) is Function
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
258 self.arguments = arguments
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
259
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
260 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
261 args = ', '.join([str(arg) for arg in self.arguments])
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
262 return '{}({})'.format(self.f.name, args)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
263
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
264
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
265 # Data operations
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
266 class Binop(Expression):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
267 """ Generic binary operation """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
268 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
269 def __init__(self, value1, operation, value2):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
270 assert operation in Binop.ops
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
271 self.a = value1
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
272 self.b = value2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
273 self.operation = operation
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
274
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
275 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
276 a, b = self.a, self.b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
277 return '({} {} {})'.format(a, self.operation, b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
278
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
279
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
280 def Add(a, b):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
281 """ Convenience call """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
282 return Binop(a, '+', b)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
283
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
284
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
285 def Sub(a, b):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
286 return Binop(a, '-', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
287
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
288
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
289 def Mul(a, b):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
290 return Binop(a, '*', b)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
291
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 280
diff changeset
292
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
293 def Div(a, b):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
294 return Binop(a, '/', b)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
295
303
be7f60545368 Final fixups
Windel Bouwman
parents: 301
diff changeset
296
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
297 class Eseq(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
298 """ Sequence of instructions where the last is an expression """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
299 def __init__(self, stmt, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
300 self.stmt = stmt
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
301 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
302
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
303 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
304 return '({}, {})'.format(self.stmt, self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
305
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
306
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
307 class Alloc(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
308 """ Allocates space on the stack """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
309 def __init__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
310 super().__init__()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
311
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
312 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
313 return 'Alloc'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
314
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
315
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
316 class Variable(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
317 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
318 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
319
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
320 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
321 return 'Var {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
322
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
323
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
324 class LocalVariable(Variable):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
325 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
326 return 'Local {}'.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 Parameter(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 'Param {}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
332
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
333
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
334 class Temp(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
335 """ Temporary storage, same as register """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
336 def __init__(self, name):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
337 self.name = name
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
338
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
339 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
340 return 'TMP_{}'.format(self.name)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
341
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
342
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
343 class Mem(Expression):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
344 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
345 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
346
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
347 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
348 return '[{}]'.format(self.e)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
349
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
350
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
351 class Statement:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
352 """ Base class for all instructions. """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
353 pass
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
354
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
355
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
356 class Move(Statement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
357 def __init__(self, dst, src):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
358 self.dst = dst
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
359 self.src = src
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
360
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
361 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
362 return '{} = {}'.format(self.dst, self.src)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
363
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
364
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
365 class Exp(Statement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
366 def __init__(self, e):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
367 self.e = e
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
368
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
369 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
370 return '{}'.format(self.e)
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 # Branching:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
374 class LastStatement(Statement):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
375 def changeTarget(self, old, new):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
376 idx = self.Targets.index(old)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
377 self.Targets[idx] = new
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
378
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
379
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
380 class Terminator(LastStatement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
381 """ Instruction that terminates the terminal block """
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
382 def __init__(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
383 self.Targets = []
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
384
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
385 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
386 return 'Terminator'
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
387
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
388
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
389 class Jump(LastStatement):
304
fa99f36fabb5 Fix docs
Windel Bouwman
parents: 303
diff changeset
390 """ Jump statement to some target location """
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
391 def __init__(self, target):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
392 self.Targets = [target]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
393
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
394 def setTarget(self, t):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
395 self.Targets[0] = t
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
396 target = property(lambda s: s.Targets[0], setTarget)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
397
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
398 def __repr__(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
399 return 'JUMP {}'.format(self.target.name)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
400
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
401
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
402 class CJump(LastStatement):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
403 conditions = ['==', '<', '>', '>=', '<=', '!=']
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
404 def __init__(self, a, cond, b, lab_yes, lab_no):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
405 assert cond in CJump.conditions
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
406 self.a = a
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
407 self.cond = cond
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
408 self.b = b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
409 self.Targets = [lab_yes, lab_no]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
410
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
411 lab_yes = property(lambda s: s.Targets[0])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
412 lab_no = property(lambda s: s.Targets[1])
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
413
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
414 def __repr__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
415 return 'IF {} {} {} THEN {} ELSE {}'.format(self.a, self.cond, self.b, self.lab_yes, self.lab_no)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
416
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
417
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
418 # Constructing IR:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 277
diff changeset
419
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
420 class NamedClassGenerator:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
421 def __init__(self, prefix, cls):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
422 self.prefix = prefix
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
423 self.cls = cls
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
424 def NumGen():
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
425 a = 0
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
426 while True:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
427 yield a
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
428 a = a + 1
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
429 self.nums = NumGen()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
430
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
431 def gen(self, prefix=None):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
432 if not prefix:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
433 prefix = self.prefix
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
434 return self.cls('{0}{1}'.format(prefix, self.nums.__next__()))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
435
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
436
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
437 class Builder:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
438 """ Base class for ir code generators """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
439 def __init__(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
440 self.prepare()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
441
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
442 def prepare(self):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
443 self.newTemp = NamedClassGenerator('reg', Temp).gen
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
444 self.newBlock2 = NamedClassGenerator('block', Block).gen
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
445 self.bb = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
446 self.m = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
447 self.fn = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
448 self.loc = None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
449
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
450 # Helpers:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
451 def setModule(self, m):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
452 self.m = m
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
453
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
454 def newFunction(self, name):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
455 f = Function(name)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
456 self.m.addFunc(f)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
457 return f
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
458
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
459 def newBlock(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
460 assert self.fn
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
461 b = self.newBlock2()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
462 b.function = self.fn
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 279
diff changeset
463 return b
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
464
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
465 def setFunction(self, f):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
466 self.fn = f
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
467 self.bb = f.entry if f else None
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
468
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
469 def setBlock(self, b):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
470 self.bb = b
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
471
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
472 def setLoc(self, l):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
473 self.loc = l
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
474
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
475 def emit(self, i):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
476 assert isinstance(i, Statement)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
477 i.debugLoc = self.loc
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
478 if not self.bb:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
479 raise Exception('No basic block')
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
480 self.bb.addInstruction(i)