Mercurial > lcfOS
comparison python/c3/codegenerator.py @ 220:3f6c30a5d234
Major change in expression parsing to enable pointers and structs
author | Windel Bouwman |
---|---|
date | Sat, 06 Jul 2013 21:32:20 +0200 |
parents | 8b2e5f3cd579 |
children | 848c4b15fd0b |
comparison
equal
deleted
inserted
replaced
219:1fa3e0050b49 | 220:3f6c30a5d234 |
---|---|
1 import ir | 1 import ir |
2 from . import astnodes | 2 from . import astnodes |
3 from .scope import boolType | 3 from .scope import boolType |
4 from ppci import CompilerError | |
4 | 5 |
5 class CodeGenerator: | 6 class CodeGenerator: |
6 """ Generates intermediate code from a package """ | 7 """ Generates intermediate code from a package """ |
7 def gencode(self, pkg): | 8 def gencode(self, pkg): |
8 assert type(pkg) is astnodes.Package | 9 assert type(pkg) is astnodes.Package |
52 if type(code) is astnodes.CompoundStatement: | 53 if type(code) is astnodes.CompoundStatement: |
53 for s in code.statements: | 54 for s in code.statements: |
54 self.genCode(s) | 55 self.genCode(s) |
55 elif type(code) is astnodes.Assignment: | 56 elif type(code) is astnodes.Assignment: |
56 re = self.genExprCode(code.rval) | 57 re = self.genExprCode(code.rval) |
58 # TODO: Handle pointers | |
57 loc = self.varMap[code.lval.target] | 59 loc = self.varMap[code.lval.target] |
58 self.builder.addIns(ir.Store(loc, re)) | 60 self.builder.addIns(ir.Store(loc, re)) |
59 elif type(code) is astnodes.IfStatement: | 61 elif type(code) is astnodes.IfStatement: |
60 bbtrue = self.builder.newBB() | 62 bbtrue = self.builder.newBB() |
61 bbfalse = self.builder.newBB() | 63 bbfalse = self.builder.newBB() |
135 else: | 137 else: |
136 print('Unknown {0}'.format(expr)) | 138 print('Unknown {0}'.format(expr)) |
137 tmp = self.builder.newTmp() | 139 tmp = self.builder.newTmp() |
138 # TODO | 140 # TODO |
139 return tmp | 141 return tmp |
142 elif type(expr) is astnodes.Unop: | |
143 if expr.op == '&': | |
144 # Address of operator | |
145 ra = self.genExprCode(expr.a) | |
146 tmp = self.builder.newTmp('addr') | |
147 return tmp | |
148 #self.builder.addIns(ins) | |
149 else: | |
150 print('Unknown {0}'.format(expr)) | |
140 elif type(expr) is astnodes.Constant: | 151 elif type(expr) is astnodes.Constant: |
141 tmp = self.builder.newTmp() | 152 tmp = self.builder.newTmp() |
142 # TODO | 153 # TODO |
143 return tmp | 154 return tmp |
144 elif type(expr) is astnodes.VariableUse: | 155 elif type(expr) is astnodes.VariableUse: |
161 fn = self.funcMap[expr.proc] | 172 fn = self.funcMap[expr.proc] |
162 ins = ir.Call(fn, args, tmp) | 173 ins = ir.Call(fn, args, tmp) |
163 self.builder.addIns(ins) | 174 self.builder.addIns(ins) |
164 return tmp | 175 return tmp |
165 else: | 176 else: |
166 print('Unknown expr:', code) | 177 raise CompilerError('Unknown expr {}'.format(expr)) |
167 | 178 |