comparison python/c3/codegenerator.py @ 261:444b9df2ed99

try to split up code generation
author Windel Bouwman
date Fri, 09 Aug 2013 09:05:13 +0200
parents ac603eb66b63
children 5ec7580976d9
comparison
equal deleted inserted replaced
260:b2f94b4951f1 261:444b9df2ed99
7 7
8 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', \ 8 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', \
9 '&':'and', '>>':'shl', '<<':'shr'} 9 '&':'and', '>>':'shl', '<<':'shr'}
10 10
11 class CodeGenerator: 11 class CodeGenerator:
12 """ Generates intermediate code from a package """
12 def __init__(self): 13 def __init__(self):
13 self.logger = logging.getLogger('c3') 14 self.logger = logging.getLogger('c3cgen')
14 """ Generates intermediate code from a package """ 15
15 def gencode(self, pkg): 16 def gencode(self, pkg):
16 assert type(pkg) is astnodes.Package 17 assert type(pkg) is astnodes.Package
17 self.logger.info('Generating ir-code for {}'.format(pkg.name)) 18 self.logger.info('Generating ir-code for {}'.format(pkg.name))
18 self.varMap = {} # Maps variables to storage locations. 19 self.varMap = {} # Maps variables to storage locations.
19 self.funcMap = {} 20 self.funcMap = {}
99 self.genCode(code.statement) 100 self.genCode(code.statement)
100 self.builder.addIns(ir.Branch(bbtest)) 101 self.builder.addIns(ir.Branch(bbtest))
101 self.builder.setBB(te) 102 self.builder.setBB(te)
102 else: 103 else:
103 print('Unknown stmt:', code) 104 print('Unknown stmt:', code)
105 raise NotImplementedError()
104 106
105 def genCondCode(self, expr, bbtrue, bbfalse): 107 def genCondCode(self, expr, bbtrue, bbfalse):
106 # Implement sequential logical operators 108 # Implement sequential logical operators
107 assert expr.typ == boolType 109 assert expr.typ == boolType
108 if type(expr) is astnodes.Binop: 110 if type(expr) is astnodes.Binop:
128 self.builder.addIns(ir.Branch(bbtrue)) 130 self.builder.addIns(ir.Branch(bbtrue))
129 else: 131 else:
130 self.builder.addIns(ir.Branch(bbfalse)) 132 self.builder.addIns(ir.Branch(bbfalse))
131 else: 133 else:
132 print('Unknown cond', expr) 134 print('Unknown cond', expr)
135 raise NotImplementedError()
133 136
134 def cast_to_rvalue(self, expr, loc): 137 def cast_to_rvalue(self, expr, loc):
135 """ Cast lvalue to rvalue if required """ 138 """ Cast lvalue to rvalue if required """
136 if hasattr(expr, 'expect_rvalue'): 139 if hasattr(expr, 'expect_rvalue'):
137 # Load value from memory location: 140 # Load value from memory location: