comparison python/c3/codegenerator.py @ 274:ea93e0a7a31e

Move docs
author Windel Bouwman
date Wed, 04 Sep 2013 17:35:06 +0200
parents e64bae57cda8
children 6f2423df0675
comparison
equal deleted inserted replaced
273:6b3a874edd6e 274:ea93e0a7a31e
5 from ppci import CompilerError 5 from ppci import CompilerError
6 from .typecheck import theType 6 from .typecheck import theType
7 7
8 8
9 class CodeGenerator(ir.Builder): 9 class CodeGenerator(ir.Builder):
10 """ Generates intermediate code from a package """ 10 """
11 Generates intermediate (IR) code from a package. The entry function is
12 'genModule'. The main task of this part is to rewrite complex control
13 structures, such as while and for loops into simple conditional
14 jump statements. Also complex conditional statements are simplified.
15 Such as 'and' and 'or' statements are rewritten in conditional jumps.
16 And structured datatypes are rewritten.
17 """
11 def __init__(self): 18 def __init__(self):
12 self.logger = logging.getLogger('c3cgen') 19 self.logger = logging.getLogger('c3cgen')
13 20
14 def gencode(self, pkg): 21 def gencode(self, pkg):
15 self.prepare() 22 self.prepare()
46 53
47 for sym in fn.innerScope: 54 for sym in fn.innerScope:
48 # TODO: handle parameters different 55 # TODO: handle parameters different
49 if sym.isParameter: 56 if sym.isParameter:
50 print('param', sym) 57 print('param', sym)
51 ir.Parameter(sym.name) 58 v = ir.Parameter(sym.name)
52 if sym.isLocal: 59 f.addParameter(v)
60 elif sym.isLocal:
53 print('local', sym) 61 print('local', sym)
54 62 v = self.newTemp()
55 v = self.newTemp() 63 else:
64 v = self.newTemp()
65 #raise NotImplementedError('{}'.format(sym))
56 # TODO: make this ssa here?? 66 # TODO: make this ssa here??
57 self.varMap[sym] = v 67 self.varMap[sym] = v
58 68
59 self.genCode(fn.body) 69 self.genCode(fn.body)
60 # Set the default return value to zero: 70 # Set the default return value to zero:
91 elif type(code) is astnodes.ReturnStatement: 101 elif type(code) is astnodes.ReturnStatement:
92 if code.expr: 102 if code.expr:
93 re = self.genExprCode(code.expr) 103 re = self.genExprCode(code.expr)
94 self.emit(ir.Move(self.fn.return_value, re)) 104 self.emit(ir.Move(self.fn.return_value, re))
95 self.emit(ir.Jump(self.fn.epiloog)) 105 self.emit(ir.Jump(self.fn.epiloog))
106 b = self.newBlock()
107 self.setBlock(b)
96 else: 108 else:
97 self.builder.addIns(ir.Return()) 109 self.builder.addIns(ir.Return())
98 elif type(code) is astnodes.WhileStatement: 110 elif type(code) is astnodes.WhileStatement:
99 bbdo = self.newBlock() 111 bbdo = self.newBlock()
100 bbtest = self.newBlock() 112 bbtest = self.newBlock()