Mercurial > lcfOS
diff python/c3/codegenerator.py @ 175:a51b3c956386
Added function call in expressions
author | Windel Bouwman |
---|---|
date | Fri, 19 Apr 2013 22:15:54 +0200 |
parents | 3eb06f5fb987 |
children | 5fd02aa38b42 |
line wrap: on
line diff
--- a/python/c3/codegenerator.py Fri Apr 19 19:22:52 2013 +0200 +++ b/python/c3/codegenerator.py Fri Apr 19 22:15:54 2013 +0200 @@ -7,6 +7,7 @@ def gencode(self, pkg): assert type(pkg) is astnodes.Package self.varMap = {} # Maps variables to storage locations. + self.funcMap = {} self.builder = ir.Builder() m = ir.Module(pkg.name) self.builder.setModule(m) @@ -15,13 +16,18 @@ # inner helpers: def genModule(self, pkg): + # Take care of forward declarations: + for s in pkg.scope: + if type(s) is astnodes.Function: + f = self.builder.newFunc(s.name) + self.funcMap[s] = f for s in pkg.scope: if type(s) is astnodes.Variable: # TODO pass elif type(s) is astnodes.Function: # TODO: handle arguments - f = self.builder.newFunc(s.name) + f = self.funcMap[s] self.builder.setFunc(f) bb = self.builder.newBB() f.entry = bb @@ -29,6 +35,8 @@ # generate room for locals: for sym in s.scope: + #print(sym, sym.isParameter) + # TODO: handle parameters different v = self.builder.newTmp(sym.name) self.builder.addIns(ir.Alloc(v)) self.varMap[sym] = v @@ -46,7 +54,6 @@ self.genCode(s) elif type(code) is astnodes.Assignment: re = self.genExprCode(code.rval) - print(code.lval) loc = self.varMap[code.lval.target] self.builder.addIns(ir.Store(loc, re)) elif type(code) is astnodes.IfStatement: @@ -131,6 +138,16 @@ ins = ir.ImmLoad(tmp, expr.val) self.builder.addIns(ins) return tmp + elif type(expr) is astnodes.FunctionCall: + tmp = self.builder.newTmp("res") + args = [] + for arg in expr.args: + ar = self.genExprCode(arg) + args.append(ar) + fn = self.funcMap[expr.proc] + ins = ir.Call(fn, args, tmp) + self.builder.addIns(ins) + return tmp else: print('Unknown expr:', code)