comparison python/c3/codegenerator.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents 5f8c04a8d26b
children ea93e0a7a31e
comparison
equal deleted inserted replaced
271:cf7d5fb7d9c8 272:e64bae57cda8
25 def genModule(self, pkg): 25 def genModule(self, pkg):
26 # Take care of forward declarations: 26 # Take care of forward declarations:
27 for s in pkg.innerScope.Functions: 27 for s in pkg.innerScope.Functions:
28 f = self.newFunction(s.name) 28 f = self.newFunction(s.name)
29 self.funcMap[s] = f 29 self.funcMap[s] = f
30 for s in pkg.innerScope: 30 for v in pkg.innerScope.Variables:
31 if type(s) is astnodes.Variable: 31 #print(v)
32 #v = self.builder.newTmp(s.name) 32 self.varMap[v] = self.newTemp()
33 self.varMap[s] = self.newTemp() 33 for s in pkg.innerScope.Functions:
34 pass 34 self.genFunction(s)
35 elif type(s) is astnodes.Function:
36 self.genFunction(s)
37 else:
38 raise NotImplementedError(str(s))
39 35
40 def genFunction(self, fn): 36 def genFunction(self, fn):
41 # TODO: handle arguments 37 # TODO: handle arguments
42 f = self.funcMap[fn] 38 f = self.funcMap[fn]
39 f.return_value = self.newTemp()
43 # TODO reserve room for stack, this can be done at later point? 40 # TODO reserve room for stack, this can be done at later point?
44 self.setFunction(f) 41 self.setFunction(f)
45 l2 = self.newBlock() 42 l2 = self.newBlock()
46 self.emit(ir.Jump(l2)) 43 self.emit(ir.Jump(l2))
47 self.setBlock(l2) 44 self.setBlock(l2)
48 # generate room for locals: 45 # generate room for locals:
49 46
50 for sym in fn.innerScope: 47 for sym in fn.innerScope:
51 # TODO: handle parameters different 48 # TODO: handle parameters different
49 if sym.isParameter:
50 print('param', sym)
51 ir.Parameter(sym.name)
52 if sym.isLocal:
53 print('local', sym)
54
52 v = self.newTemp() 55 v = self.newTemp()
53 # TODO: make this ssa here?? 56 # TODO: make this ssa here??
54 self.varMap[sym] = v 57 self.varMap[sym] = v
55 58
56 self.genCode(fn.body) 59 self.genCode(fn.body)
57 # TODO handle return? 60 # Set the default return value to zero:
61 # TBD: this may not be required?
62 self.emit(ir.Move(f.return_value, ir.Const(0)))
58 self.emit(ir.Jump(f.epiloog)) 63 self.emit(ir.Jump(f.epiloog))
59 #self.emit(ir.Return(ir.Const(0)))
60 self.setFunction(None) 64 self.setFunction(None)
61 65
62 def genCode(self, code): 66 def genCode(self, code):
63 assert isinstance(code, astnodes.Statement) 67 assert isinstance(code, astnodes.Statement)
64 self.setLoc(code.loc) 68 self.setLoc(code.loc)
85 self.emit(ir.Jump(te)) 89 self.emit(ir.Jump(te))
86 self.setBlock(te) 90 self.setBlock(te)
87 elif type(code) is astnodes.ReturnStatement: 91 elif type(code) is astnodes.ReturnStatement:
88 if code.expr: 92 if code.expr:
89 re = self.genExprCode(code.expr) 93 re = self.genExprCode(code.expr)
90 self.emit(ir.Return(re)) 94 self.emit(ir.Move(self.fn.return_value, re))
95 self.emit(ir.Jump(self.fn.epiloog))
91 else: 96 else:
92 self.builder.addIns(ir.Return()) 97 self.builder.addIns(ir.Return())
93 elif type(code) is astnodes.WhileStatement: 98 elif type(code) is astnodes.WhileStatement:
94 bbdo = self.newBlock() 99 bbdo = self.newBlock()
95 bbtest = self.newBlock() 100 bbtest = self.newBlock()