# HG changeset patch # User Windel Bouwman # Date 1362322234 -3600 # Node ID 1b4a85bdd99c68230e950cff4df7a75958ee19db # Parent b28a11c01dbe24dd904e551cd382a13fcb5c7abb change types diff -r b28a11c01dbe -r 1b4a85bdd99c python/c3/codegenerator.py --- a/python/c3/codegenerator.py Sun Mar 03 13:20:03 2013 +0100 +++ b/python/c3/codegenerator.py Sun Mar 03 15:50:34 2013 +0100 @@ -4,9 +4,12 @@ def genModule(pkg): m = ir.Module(pkg.name) for s in pkg.scope: - print(s) if type(s) is astnodes.Variable: genGlobal(m, s) + elif type(s) is astnodes.Function: + genFunction(m, s) + else: + print(s) return m def genGlobal(m, var): @@ -14,6 +17,10 @@ v.name = var.name m.Globals.append(v) +def genFunction(m, fnc): + f = ir.Function() + m.Globals.append(f) + class CodeGenerator: """ Generates intermediate code """ def gencode(self, ast): diff -r b28a11c01dbe -r 1b4a85bdd99c python/ir/__init__.py --- a/python/ir/__init__.py Sun Mar 03 13:20:03 2013 +0100 +++ b/python/ir/__init__.py Sun Mar 03 15:50:34 2013 +0100 @@ -1,3 +1,4 @@ from .module import Module, Function, BasicBlock from .value import Value +from .types import Type, i8, i16, i32, void diff -r b28a11c01dbe -r 1b4a85bdd99c python/ir/llvmtype.py --- a/python/ir/llvmtype.py Sun Mar 03 13:20:03 2013 +0100 +++ b/python/ir/llvmtype.py Sun Mar 03 15:50:34 2013 +0100 @@ -1,30 +1,27 @@ -def Enum(**enums): - return type('Enum', (), enums) - -typeID = Enum(Void=0, Double=3, Integer=10, Function=11, Struct=12, Array=13, Pointer=14, Vector=15) - -class llvmType: - def __init__(self, tid): - self.tid = tid +class Type: + def __init__(self): + pass -class IntegerType(llvmType): +class IntegerType(Type): def __init__(self, bits): - super().__init__(typeID.Integer) + super().__init__() self.bits = bits -class FunctionType(llvmType): +class VoidType(Type): + pass + +class FunctionType(Type): def __init__(self, resultType, parameterTypes): - super().__init__(typeID.Function) + super().__init__() assert type(parameterTypes) is list self.resultType = resultType - self.returnType = resultType self.parameterTypes = parameterTypes # Default types: i8 = IntegerType(8) i16 = IntegerType(16) i32 = IntegerType(32) -void = llvmType(typeID.Void) +void = VoidType() diff -r b28a11c01dbe -r 1b4a85bdd99c python/ir/module.py --- a/python/ir/module.py Sun Mar 03 13:20:03 2013 +0100 +++ b/python/ir/module.py Sun Mar 03 15:50:34 2013 +0100 @@ -7,7 +7,6 @@ self.functions = [] # Do functions come out of symbol table? self.globals_ = [] # TODO: are globals in symbol table? self.symtable = SymbolTable() - Globals = property(lambda self: self.globals_) Functions = property(lambda self: self.functions) Identifier = property(lambda self: self.identifier) @@ -19,17 +18,15 @@ self.function = function class Function: - def __init__(self, functiontype, name, module): + def __init__(self, name, functiontype): super().__init__() + self.name = name self.functiontype = functiontype - self.name = name - self.module = module self.module.Functions.append(self) self.basicblocks = [] self.arguments = [] # Construct formal arguments depending on function type - BasicBlocks = property(lambda self: self.basicblocks) Arguments = property(lambda self: self.arguments) ReturnType = property(lambda self: self.functiontype.returnType)