comparison python/c3/codegenerator.py @ 232:e621e3ba78d2

Added left shift instruction
author Windel Bouwman
date Sun, 14 Jul 2013 11:50:58 +0200
parents 521567d17388
children d3dccf12ca88
comparison
equal deleted inserted replaced
231:521567d17388 232:e621e3ba78d2
2 from . import astnodes 2 from . import astnodes
3 from .scope import boolType, intType 3 from .scope import boolType, intType
4 from ppci import CompilerError 4 from ppci import CompilerError
5 from .typecheck import theType 5 from .typecheck import theType
6 6
7 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'} 7 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', \
8 '&':'and', '>>':'shl', '<<':'shr'}
8 9
9 class CodeGenerator: 10 class CodeGenerator:
10 """ Generates intermediate code from a package """ 11 """ Generates intermediate code from a package """
11 def gencode(self, pkg): 12 def gencode(self, pkg):
12 assert type(pkg) is astnodes.Package 13 assert type(pkg) is astnodes.Package
139 def genExprCode(self, expr): 140 def genExprCode(self, expr):
140 assert isinstance(expr, astnodes.Expression) 141 assert isinstance(expr, astnodes.Expression)
141 if type(expr) is astnodes.Binop: 142 if type(expr) is astnodes.Binop:
142 ra = self.genExprCode(expr.a) 143 ra = self.genExprCode(expr.a)
143 rb = self.genExprCode(expr.b) 144 rb = self.genExprCode(expr.b)
144 ops = ['+', '-', '*', '/', '|', '&'] 145 ops = ['+', '-', '*', '/', '|', '&', '<<', '>>']
145 if expr.op in ops: 146 if expr.op in ops:
146 tmp = self.builder.newTmp(tmpnames[expr.op]) 147 tmp = self.builder.newTmp(tmpnames[expr.op])
147 op = expr.op 148 op = expr.op
148 ins = ir.BinaryOperator(tmp, op, ra, rb) 149 ins = ir.BinaryOperator(tmp, op, ra, rb)
149 self.builder.addIns(ins) 150 self.builder.addIns(ins)