# HG changeset patch # User Windel Bouwman # Date 1365091117 -7200 # Node ID 5a7d37d615ee28449535a98fd1177799f438e664 # Parent 3eb9b9e2958d7c4e22a92d9f2905d57e0326c2e1 Added function to IR diff -r 3eb9b9e2958d -r 5a7d37d615ee python/c3/codegenerator.py --- a/python/c3/codegenerator.py Wed Apr 03 22:20:20 2013 +0200 +++ b/python/c3/codegenerator.py Thu Apr 04 17:58:37 2013 +0200 @@ -20,11 +20,15 @@ pass elif type(s) is astnodes.Function: # TODO: handle arguments - # TODO handle return? + f = self.builder.newFunc(s.name) + self.builder.setFunc(f) bb = self.builder.newBB() + f.entry = bb self.builder.setBB(bb) self.genCode(s.body) + # TODO handle return? self.builder.addIns(ir.Return()) + self.builder.setFunc(None) else: print(s) diff -r 3eb9b9e2958d -r 5a7d37d615ee python/ir/__init__.py --- a/python/ir/__init__.py Wed Apr 03 22:20:20 2013 +0200 +++ b/python/ir/__init__.py Thu Apr 04 17:58:37 2013 +0200 @@ -1,4 +1,6 @@ from .module import * from .instruction import * +from .function import Function from .builder import Builder +from .basicblock import BasicBlock diff -r 3eb9b9e2958d -r 5a7d37d615ee python/ir/builder.py --- a/python/ir/builder.py Wed Apr 03 22:20:20 2013 +0200 +++ b/python/ir/builder.py Thu Apr 04 17:58:37 2013 +0200 @@ -1,4 +1,4 @@ -from . import Value, BasicBlock +from . import Value, BasicBlock, Function class NameGenerator: def __init__(self, prefix): @@ -32,14 +32,21 @@ self.newBBint = BBGenerator().gen self.bb = None self.m = None + self.fn = None # Helpers: def newBB(self): bb = self.newBBint() - self.m.addBB(bb) + self.fn.addBB(bb) return bb def setModule(self, m): self.m = m + def newFunc(self, name): + f = Function(name) + self.m.addFunc(f) + return f + def setFunc(self, f): + self.fn = f def setBB(self, bb): self.bb = bb def addIns(self, i): diff -r 3eb9b9e2958d -r 5a7d37d615ee python/ir/function.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/ir/function.py Thu Apr 04 17:58:37 2013 +0200 @@ -0,0 +1,16 @@ +from .basicblock import BasicBlock + +class Function: + def __init__(self, name): + self.name = name + self.bbs = [] + self.entry = None + def __repr__(self): + return 'FUNC {0}, entry:{1}'.format(self.name, self.entry) + def addBB(self, bb): + self.bbs.append(bb) + def getBBs(self): + return self.bbs + BasicBlocks = property(getBBs) + + diff -r 3eb9b9e2958d -r 5a7d37d615ee python/ir/module.py --- a/python/ir/module.py Wed Apr 03 22:20:20 2013 +0200 +++ b/python/ir/module.py Thu Apr 04 17:58:37 2013 +0200 @@ -6,24 +6,34 @@ """ Main container for a piece of code. """ def __init__(self, name): self.name = name - self.bbs = [] + self.funcs = [] def __repr__(self): return 'IR-module [{0}]'.format(self.name) def getInstructions(self): ins = [] - for bb in self.bbs: + for bb in self.BasicBlocks: ins += bb.Instructions return ins Instructions = property(getInstructions) - def addBB(self, bb): - self.bbs.append(bb) def getBBs(self): - return self.bbs + bbs = [] + for f in self.Functions: + bbs += f.BasicBlocks + return bbs BasicBlocks = property(getBBs) + def addFunc(self, f): + self.funcs.append(f) + def getFuncs(self): + return self.funcs + Functions = property(getFuncs) def dump(self): print(self) - for i in self.Instructions: - print(i, 'live vars:', list(i.live_in), 'uses', list(i.uses), 'defs', list(i.defs)) + for fn in self.Functions: + print(fn) + for bb in fn.BasicBlocks: + print(' ', bb) + for ins in bb.Instructions: + print(' ', ins) print('END') def dumpgv(self, outf): outf.write('digraph G \n{\n') @@ -46,7 +56,7 @@ def link(a, b): a.succ.add(b) b.pred.add(a) - for bb in self.bbs: + for bb in self.BasicBlocks: if not bb.Empty: if len(bb.Instructions) > 1: for i1, i2 in zip(bb.Instructions[:-1], bb.Instructions[1:]): diff -r 3eb9b9e2958d -r 5a7d37d615ee python/ppci/errors.py --- a/python/ppci/errors.py Wed Apr 03 22:20:20 2013 +0200 +++ b/python/ppci/errors.py Thu Apr 04 17:58:37 2013 +0200 @@ -44,4 +44,9 @@ self.addDiag(CompilerError(msg, loc)) def clear(self): self.diags.clear() + def printErrors(self, src): + if len(self.diags) > 0: + print('{0} Errors'.format(len(self.diags))) + for d in self.diags: + printError(src, d) diff -r 3eb9b9e2958d -r 5a7d37d615ee python/testir.py --- a/python/testir.py Wed Apr 03 22:20:20 2013 +0200 +++ b/python/testir.py Thu Apr 04 17:58:37 2013 +0200 @@ -24,6 +24,13 @@ y = a - b * 53; } +function int add2(int x, int y) +{ + var int res; + res = x + y; + return res; +} + """ if __name__ == '__main__': @@ -31,6 +38,7 @@ builder = c3.Builder(diag) cgenx86 = x86.X86CodeGen(diag) ir = builder.build(testsrc) + diag.printErrors(testsrc) ir.check() ir.analyze() #ir.constantProp()