# HG changeset patch # User Windel Bouwman # Date 1357056965 -3600 # Node ID 1544e7a4aa9819fe8befed2fbda1739ddaae7b7d # Parent f2d980eef50904c817d2b70319b3715583d91e06 Improvements diff -r f2d980eef509 -r 1544e7a4aa98 python/ppci/core/asmwriter.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/ppci/core/asmwriter.py Tue Jan 01 17:16:05 2013 +0100 @@ -0,0 +1,40 @@ + +from . import llvmtype +#typeNames[VoidType] = 'void' + +class AsmWriter: + def __init__(self): + self.typeNames = {} + self.typeNames[llvmtype.typeID.Void] = 'void' + def printModule(self, module): + if module.Identifier: + print('; ModuleID = {0}'.format(module.Identifier)) + # + # Print functions: + for f in module.Functions: + self.printFunction(f) + def printFunction(self, f): + # TODO: if definition: + + t = self.strType(f.ReturnType.tid) + args = '()' + print('define {0} {1}{2}'.format(t, f.name, args)) + print('{') + + for bb in f.BasicBlocks: + print(bb) + + print('}') + + def strType(self, t): + return self.typeNames[t] + + def printBasicBlock(self, bb): + if bb.Name: + # print label + print('{0}:'.format(bb.Name)) + for instr in bb.Instructions: + self.printInstruction(instr) + def printInstruction(self, i): + pass + diff -r f2d980eef509 -r 1544e7a4aa98 python/ppci/core/llvmtype.py --- a/python/ppci/core/llvmtype.py Mon Dec 31 18:26:56 2012 +0100 +++ b/python/ppci/core/llvmtype.py Tue Jan 01 17:16:05 2013 +0100 @@ -7,6 +7,7 @@ class llvmType: def __init__(self, context, tid): self.context = context + self.tid = tid class IntegerType(llvmType): def __init__(self, context, bits): diff -r f2d980eef509 -r 1544e7a4aa98 python/ppci/core/module.py --- a/python/ppci/core/module.py Mon Dec 31 18:26:56 2012 +0100 +++ b/python/ppci/core/module.py Tue Jan 01 17:16:05 2013 +0100 @@ -1,4 +1,3 @@ - from .value import Value from .symboltable import SymbolTable @@ -6,8 +5,8 @@ """ Main container for a piece of code. Contains globals and functions. """ - def __init__(self): - self.identifier = None + def __init__(self, identifier=None): + self.identifier = identifier self.functions = [] # Do functions come out of symbol table? self.globals_ = [] # TODO: are globals in symbol table? self.symtable = SymbolTable() diff -r f2d980eef509 -r 1544e7a4aa98 python/ppci/core/symboltable.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/ppci/core/symboltable.py Tue Jan 01 17:16:05 2013 +0100 @@ -0,0 +1,7 @@ + +class SymbolTable: + """ Holds a table of symbols for a module or function """ + def __init__(self): + self.symbols = {} + + diff -r f2d980eef509 -r 1544e7a4aa98 python/ppci/frontends/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/ppci/frontends/__init__.py Tue Jan 01 17:16:05 2013 +0100 @@ -0,0 +1,4 @@ +# File to make this directory a package. + +from .ks import KsFrontend + diff -r f2d980eef509 -r 1544e7a4aa98 python/ppci/frontends/ks/irgenerator.py --- a/python/ppci/frontends/ks/irgenerator.py Mon Dec 31 18:26:56 2012 +0100 +++ b/python/ppci/frontends/ks/irgenerator.py Tue Jan 01 17:16:05 2013 +0100 @@ -75,23 +75,8 @@ # Get a register to store the integer value else: Error('Cannot load variable type {0}'.format(node.typ)) - - elif isinstance(node, Relop): - # Create a boolean from operands - # TODO create an alternative for expressions used as conditions. - self.genexprcode(node.a) - self.genexprcode(node.b) - - if node.a.typ.isType(integer): - self.freereg(node.b) - else: - Error('Relop not implemented for {0}'.format(node.a.typ)) - elif type(node) is Constant: print('TODO: constant') - - elif type(node) is ProcedureCall: - Error('TODO: proc call') else: Error('Cannot generate expression code for: {0}'.format(node)) @@ -99,8 +84,16 @@ """ Code generation function for AST nodes """ if isinstance(node, Module): # TODO: recurse! + + print(node.symtable) + node.symtable.printTable() + funcs = node.symtable.getAllLocal(Procedure) + for f in funcs: + print('function', f) + ftype = f.typ.coreType() + ftype = core.FunctionType(retType) - self.mod = core.Module() + self.mod = core.Module(node.name) # Create a function called init for this module: ftype = core.FunctionType(self.context.VoidType, []) func = core.Function(ftype, "init", self.mod) diff -r f2d980eef509 -r 1544e7a4aa98 python/ppci/frontends/ks/nodes.py --- a/python/ppci/frontends/ks/nodes.py Mon Dec 31 18:26:56 2012 +0100 +++ b/python/ppci/frontends/ks/nodes.py Tue Jan 01 17:16:05 2013 +0100 @@ -129,6 +129,8 @@ self.returntype = returntype def __repr__(self): return '[PROCTYPE {0} RET {1}]'.format(self.parameters, self.returntype) + def coreType(self): + return None class DefinedType(Type): def __init__(self, name, typ): diff -r f2d980eef509 -r 1544e7a4aa98 python/zcc.py --- a/python/zcc.py Mon Dec 31 18:26:56 2012 +0100 +++ b/python/zcc.py Tue Jan 01 17:16:05 2013 +0100 @@ -42,5 +42,3 @@ with open(args.source + '.bc', 'wb') as f: bitcodeWriter.WriteModuleToFile(module, f) - -