comparison python/c3/codegenerator.py @ 221:848c4b15fd0b

pointers
author Windel Bouwman
date Mon, 08 Jul 2013 22:21:44 +0200
parents 3f6c30a5d234
children c3f1ce8b638f
comparison
equal deleted inserted replaced
220:3f6c30a5d234 221:848c4b15fd0b
49 else: 49 else:
50 print(s) 50 print(s)
51 51
52 def genCode(self, code): 52 def genCode(self, code):
53 if type(code) is astnodes.CompoundStatement: 53 if type(code) is astnodes.CompoundStatement:
54 for s in code.statements: 54 for s in code.statements:
55 self.genCode(s) 55 self.genCode(s)
56 elif type(code) is astnodes.Assignment: 56 elif type(code) is astnodes.Assignment:
57 re = self.genExprCode(code.rval) 57 re = self.genExprCode(code.rval)
58 # TODO: Handle pointers 58 # TODO: Handle pointers
59 loc = self.varMap[code.lval.target] 59 loc = self.genExprCode(code.lval)
60 # determine location of variable
60 self.builder.addIns(ir.Store(loc, re)) 61 self.builder.addIns(ir.Store(loc, re))
61 elif type(code) is astnodes.IfStatement: 62 elif type(code) is astnodes.IfStatement:
62 bbtrue = self.builder.newBB() 63 bbtrue = self.builder.newBB()
63 bbfalse = self.builder.newBB() 64 bbfalse = self.builder.newBB()
64 te = self.builder.newBB() 65 te = self.builder.newBB()
68 self.builder.addIns(ir.Branch(te)) 69 self.builder.addIns(ir.Branch(te))
69 self.builder.setBB(bbfalse) 70 self.builder.setBB(bbfalse)
70 self.genCode(code.falsestatement) 71 self.genCode(code.falsestatement)
71 self.builder.addIns(ir.Branch(te)) 72 self.builder.addIns(ir.Branch(te))
72 self.builder.setBB(te) 73 self.builder.setBB(te)
73 elif type(code) is astnodes.FunctionCall:
74 print('TODO')
75 pass
76 elif type(code) is astnodes.EmptyStatement: 74 elif type(code) is astnodes.EmptyStatement:
77 pass 75 pass
78 elif type(code) is astnodes.ReturnStatement: 76 elif type(code) is astnodes.ReturnStatement:
79 if code.expr: 77 if code.expr:
80 re = self.genExprCode(code.expr) 78 re = self.genExprCode(code.expr)
124 print('Unknown cond', expr) 122 print('Unknown cond', expr)
125 def genExprCode(self, expr): 123 def genExprCode(self, expr):
126 if type(expr) is astnodes.Binop: 124 if type(expr) is astnodes.Binop:
127 ra = self.genExprCode(expr.a) 125 ra = self.genExprCode(expr.a)
128 rb = self.genExprCode(expr.b) 126 rb = self.genExprCode(expr.b)
129 ops = ['+', '-', '*', '/'] 127 ops = ['+', '-', '*', '/', '|', '&']
130 if expr.op in ops: 128 if expr.op in ops:
131 tmpnames = {'+':'addtmp', '-':'subtmp', '*': 'multmp', '/':'divtmp'} 129 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'}
132 tmp = self.builder.newTmp(tmpnames[expr.op]) 130 tmp = self.builder.newTmp(tmpnames[expr.op])
133 op = expr.op 131 op = expr.op
134 ins = ir.BinaryOperator(tmp, op, ra, rb) 132 ins = ir.BinaryOperator(tmp, op, ra, rb)
135 self.builder.addIns(ins) 133 self.builder.addIns(ins)
136 return tmp 134 return tmp