comparison 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
comparison
equal deleted inserted replaced
393:6ae782a085e0 394:988f3fb861e4
2 Intermediate representation (IR) code classes. 2 Intermediate representation (IR) code classes.
3 """ 3 """
4 4
5 5
6 def label_name(dut): 6 def label_name(dut):
7 """ Function that returns the assembly code label name """ 7 """ Returns the assembly code label name """
8 if isinstance(dut, Block): 8 if isinstance(dut, Block):
9 f = dut.function 9 f = dut.function
10 return label_name(f) + '_' + dut.name 10 return label_name(f) + '_' + dut.name
11 elif isinstance(dut, Function) or isinstance(dut, GlobalVariable): 11 elif isinstance(dut, Function) or isinstance(dut, GlobalVariable):
12 return label_name(dut.module) + '_' + dut.name 12 return label_name(dut.module) + '_' + dut.name
14 return dut.name 14 return dut.name
15 else: 15 else:
16 raise NotImplementedError(str(dut)) 16 raise NotImplementedError(str(dut))
17 17
18 18
19 class Typ:
20 def __init__(self):
21 pass
22
23
24 i32 = Typ()
25 i8 = Typ()
26
19 class Module: 27 class Module:
20 """ Container unit for variables and functions. """ 28 """ Container unit for variables and functions. """
21 def __init__(self, name): 29 def __init__(self, name):
22 self.name = name 30 self.name = name
23 self.functions = [] 31 self.functions = []
44 def get_functions(self): 52 def get_functions(self):
45 return self.functions 53 return self.functions
46 54
47 Functions = property(get_functions) 55 Functions = property(get_functions)
48 56
49 def findFunction(self, name): 57 def find_function(self, name):
50 for f in self.funcs: 58 for f in self.funcs:
51 if f.name == name: 59 if f.name == name:
52 return f 60 return f
53 raise KeyError(name) 61 raise KeyError(name)
54
55 getFunction = findFunction
56 62
57 63
58 class Function: 64 class Function:
59 """ Represents a function. """ 65 """ Represents a function. """
60 def __init__(self, name, module=None): 66 def __init__(self, name, module=None):
193 199
194 200
195 # Instructions: 201 # Instructions:
196 202
197 class Value: 203 class Value:
198 pass 204 """ A value has a type and a name """
199 205 def __init__(self, name, ty):
200 class Expression: 206 assert isinstance(ty, Typ)
207 self.name = name
208 self.ty = ty
209
210
211 class User(Value):
212 """ Value that uses other values """
213 def __init__(self, name, ty):
214 super().__init__(name, ty)
215 # Create a collection to store the values this value uses.
216 # TODO: think of better naming..
217 self.uses = set()
218
219 def add_use(self, v):
220 assert isinstance(v, Value)
221 self.uses.add(v)
222
223
224 class Expression(User):
201 """ Base class for an expression """ 225 """ Base class for an expression """
202 pass 226 pass
203 227
204 228
205 class Const(Expression): 229 class Const(Expression):
226 # Data operations 250 # Data operations
227 class Binop(Expression): 251 class Binop(Expression):
228 """ Generic binary operation """ 252 """ Generic binary operation """
229 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>'] 253 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
230 254
231 def __init__(self, value1, operation, value2): 255 def __init__(self, a, operation, b, name, ty):
256 super().__init__(name, ty)
232 assert operation in Binop.ops 257 assert operation in Binop.ops
233 #assert type(value1) is type(value2) 258 #assert type(value1) is type(value2)
234 self.a = value1 259 assert isinstance(a, Value), str(a)
235 self.b = value2 260 assert isinstance(b, Value), str(b)
261 self.a = a
262 self.b = b
236 self.operation = operation 263 self.operation = operation
237 264
238 def __repr__(self): 265 def __repr__(self):
239 a, b = self.a, self.b 266 a, b = self.a, self.b
240 return '({} {} {})'.format(a, self.operation, b) 267 return '({} {} {})'.format(a, self.operation, b)
241 268
242 269
243 def Add(a, b): 270 class Add(Binop):
244 """ Add a and b """ 271 """ Add a and b """
245 return Binop(a, '+', b) 272 def __init__(self, a, b, name, ty):
273 super().__init__(a, '+', b, name, ty)
246 274
247 275
248 def Sub(a, b): 276 def Sub(a, b):
249 """ Substract b from a """ 277 """ Substract b from a """
250 return Binop(a, '-', b) 278 return Binop(a, '-', b)
251 279
252 280
253 def Mul(a, b): 281 def Mul(a, b, name, ty):
254 """ Multiply a by b """ 282 """ Multiply a by b """
255 return Binop(a, '*', b) 283 return Binop(a, '*', b, name, ty)
256 284
257 285
258 def Div(a, b): 286 def Div(a, b):
259 """ Divide a in b pieces """ 287 """ Divide a in b pieces """
260 return Binop(a, '/', b) 288 return Binop(a, '/', b)
289
290
291 def Phi(User):
292 """ Imaginary phi instruction to make SSA possible. """
293 def __init__(self, name, ty):
294 super().__init__(name, ty)
295 self.inputs = []
296
297 def add_input(self, value, block):
298 self.inputs.append((value, block))
261 299
262 300
263 class Eseq(Expression): 301 class Eseq(Expression):
264 """ Sequence of instructions where the last is an expression """ 302 """ Sequence of instructions where the last is an expression """
265 def __init__(self, stmt, e): 303 def __init__(self, stmt, e):
278 def __repr__(self): 316 def __repr__(self):
279 return 'Alloc' 317 return 'Alloc'
280 318
281 319
282 class Variable(Expression): 320 class Variable(Expression):
283 def __init__(self, name): 321 def __init__(self, name, ty):
322 super().__init__(name, ty)
284 self.name = name 323 self.name = name
285 324
286 def __repr__(self): 325 def __repr__(self):
287 return 'Var {}'.format(self.name) 326 return 'Var {}'.format(self.name)
288 327
316 def __init__(self, e): 355 def __init__(self, e):
317 self.e = e 356 self.e = e
318 357
319 def __repr__(self): 358 def __repr__(self):
320 return '[{}]'.format(self.e) 359 return '[{}]'.format(self.e)
360
361
362 class Load(Value):
363 """ Load a value from memory """
364 def __init__(self, address, name, ty):
365 super().__init__(name, ty)
366 assert isinstance(address, Value)
367 self.address = address
368
369 def __repr__(self):
370 return 'load {}'.format(self.address)
371
372
373 class Store:
374 """ Store a value into memory """
375 def __init__(self, address, value):
376 self.address = address
321 377
322 378
323 class Addr(Expression): 379 class Addr(Expression):
324 """ Address of label """ 380 """ Address of label """
325 def __init__(self, e): 381 def __init__(self, e):