Mercurial > projects > dang
changeset 64:91f10c34cd7b new_gen
Fixed some bugs, removed the function gathering pass in codegen and types are
created when first referenced
author | Anders Halager <halager@gmail.com> |
---|---|
date | Tue, 29 Apr 2008 17:56:52 +0200 |
parents | 9f8131676242 |
children | 932bb3c6b80b |
files | ast/Decl.d ast/Stmt.d gen/CodeGen.d sema/DType.d sema/SymbolTableBuilder.d |
diffstat | 5 files changed, 71 insertions(+), 76 deletions(-) [+] |
line wrap: on
line diff
--- a/ast/Decl.d Tue Apr 29 15:13:38 2008 +0200 +++ b/ast/Decl.d Tue Apr 29 17:56:52 2008 +0200 @@ -98,6 +98,8 @@ this.returnType = new Identifier("void"); env.find(identifier).type = t; sret = true; + + myType = null; } } @@ -107,7 +109,7 @@ stmt.simplify(); } - override DType type() + override DFunction type() { if (myType !is null) return myType; @@ -118,6 +120,7 @@ foreach (a; funcArgs) array ~= a.type(); t.params = array.safe(); + t.firstParamIsReturnValue = this.sret; myType = t; return myType; } @@ -126,7 +129,7 @@ VarDecl[] funcArgs; Stmt[] statements; bool sret = false; - private DType myType; + private DFunction myType; } class StructDecl : Decl @@ -149,7 +152,7 @@ override DType type() { - return env.find(identifier).type; + return env.findType(identifier); } Identifier identifier;
--- a/ast/Stmt.d Tue Apr 29 15:13:38 2008 +0200 +++ b/ast/Stmt.d Tue Apr 29 17:56:52 2008 +0200 @@ -65,6 +65,8 @@ void simplify() { FuncDecl f = env.parentFunction; + if(exp) + exp.simplify; if(f !is null && f.sret) { auto i = new Identifier("ret.val"); @@ -86,8 +88,6 @@ exp = null; } - if(exp) - exp.simplify; } public Exp exp;
--- a/gen/CodeGen.d Tue Apr 29 15:13:38 2008 +0200 +++ b/gen/CodeGen.d Tue Apr 29 17:56:52 2008 +0200 @@ -68,45 +68,6 @@ BytePtr = PointerType.Get(Type.Int8); auto temp = FunctionType.Get(Type.Void, [BytePtr, BytePtr, Type.Int32, Type.Int32]); llvm_memcpy = m.addFunction(temp, "llvm.memcpy.i32"); - auto registerFunc = - (FuncDecl fd) - { - Type[] param_types; - foreach (i, p; fd.funcArgs) - { - DType t = p.env.find(p.identifier).type; - if(auto st = cast(DStruct)t) - { - Type pointer = PointerType.Get(llvm(st)); - param_types ~= pointer; - } - else - param_types ~= llvm(t); - } - auto ret_t = fd.env.find(fd.identifier).type; - if(auto st = cast(DStruct)ret_t) - ret_t = DType.Void; - else if(auto f = cast(DFunction)ret_t) - ret_t = f.returnType; - auto func_t = FunctionType.Get(llvm(ret_t), param_types); - auto llfunc = m.addFunction(func_t, fd.identifier.get); - - foreach (i, p; fd.funcArgs) - { - if(i == 0 && fd.sret) - llfunc.addParamAttr(0, ParamAttr.StructRet); - - DType t = p.env.find(p.identifier).type; - if(auto st = cast(DStruct)t) - { - if(i == 0 && fd.sret) - continue; - llfunc.addParamAttr(i,ParamAttr.ByVal); - } - } - }; - auto visitor = new VisitFuncDecls(registerFunc); - visitor.visit(decls); // Before beginning we move all top level var-decls to the start // and then we generate the var-decls first @@ -134,7 +95,8 @@ case DeclType.FuncDecl: FuncDecl funcDecl = cast(FuncDecl)decl; - auto llfunc = m.getNamedFunction(funcDecl.identifier.get); + llvm(funcDecl.type); + auto llfunc = m.getNamedFunction(funcDecl.type.name); auto func_tp = cast(PointerType)llfunc.type; auto func_t = cast(FunctionType)func_tp.elementType(); auto ret_t = func_t.returnType(); @@ -183,18 +145,8 @@ case DeclType.StructDecl: auto structDecl = cast(StructDecl)decl; - Type[] types; - foreach(varDecl ; structDecl.vars) - { - auto sym = varDecl.env.find(varDecl.identifier); - Type t = llvm(sym.type); - types ~= t; - } - - StructType t = StructType.Get(types); - m.addTypeName(structDecl.identifier.get, t); -// table[structDecl.identifier.get] = g; - + llvm(structDecl.type); + //m.addTypeName(structDecl.identifier.get, llvm(structDecl.type)); break; default: @@ -291,7 +243,7 @@ case ExpType.Identifier: auto identifier = cast(Identifier)exp; auto sym = exp.env.find(identifier); - if(cast(DStruct)sym.type) + if(sym.type.isStruct) return table.find(sym.id.get); else return b.buildLoad(table.find(sym.id.get), sym.id.get); @@ -315,8 +267,8 @@ break; case StmtType.Return: auto ret = cast(ReturnStmt)stmt; - auto sym = stmt.env.parentFunction(); - Type t = llvm(sym.env.find(sym.identifier).type); + DFunction type = stmt.env.parentFunction().type(); + Type t = llvm(type.returnType); if (ret.exp is null) if (t is Type.Void) { @@ -483,7 +435,7 @@ auto symChild = child.env.find(child); Value v = table.find(sym.id.get); DType t = sym.type; - auto st = cast(DStruct)t; + auto st = t.asStruct; int i = st.indexOf(child.get); @@ -583,7 +535,7 @@ type_map[t] = res; return res; } - else if (auto s = cast(DStruct)t) + else if (auto s = t.asStruct) { SmallArray!(Type, 8) members; foreach(m; s.members) @@ -591,17 +543,37 @@ Type res = StructType.Get(members.unsafe()); type_map[t] = res; + m.addTypeName(s.name, res); return res; } - else if (auto f = cast(DFunction)t) + else if (auto f = t.asFunction) { + // We should never have a function returning structs, because of + // the simplify step + assert(f.returnType.isStruct() == false, "Can't return structs"); + Type ret_t = llvm(f.returnType); + SmallArray!(Type, 8) params; foreach(param; f.params) - params ~= llvm(param); + if (param.isStruct) + params ~= PointerType.Get(llvm(param)); + else + params ~= llvm(param); - Type ret_t = llvm(f.returnType); Type res = FunctionType.Get(ret_t, params.unsafe()); type_map[t] = res; + auto llfunc = m.addFunction(res, f.name); + + foreach (i, param; f.params) + if (param.isStruct) + llfunc.addParamAttr(i, ParamAttr.ByVal); + + if (f.firstParamIsReturnValue) + { + llfunc.removeParamAttr(0, ParamAttr.ByVal); + llfunc.addParamAttr(0, ParamAttr.StructRet); + } + return res; } assert(0, "Only integers, structs and functions are supported");
--- a/sema/DType.d Tue Apr 29 15:13:38 2008 +0200 +++ b/sema/DType.d Tue Apr 29 17:56:52 2008 +0200 @@ -25,6 +25,25 @@ this.actual = actual is null? this : actual; } + /// Is this type a DStruct + bool isStruct() { return false; } + /// Return a DStruct if this is one, otherwise return null + DStruct asStruct() { return null; } + + /// Is this type a DFunction + bool isFunction() { return false; } + /// Return a DFunction if this is one, otherwise return null + DFunction asFunction() { return null; } + + /// Is this type a DInteger + bool isInteger() { return false; } + /// Return a DInteger if this is one, otherwise return null + DInteger asInteger() { return null; } + + // Is this type a DPointer + //bool isPointer() { return false; } + // DPointer asPointer() { return null; } + int opEquals(Object o) { if (auto t = cast(DType)o) @@ -105,6 +124,9 @@ return false; } + override bool isInteger() { return true; } + override DInteger asInteger() { return this; } + int bits; bool unsigned; } @@ -118,6 +140,9 @@ int byteSize() { return bytes_total; } + override bool isStruct() { return true; } + override DStruct asStruct() { return this; } + void addMember(DType type, char[] name) { auto s = DStructMember(type, members.length); @@ -158,7 +183,11 @@ super(id, actual); } + override bool isFunction() { return true; } + override DFunction asFunction() { return this; } + DType[] params; DType returnType; + bool firstParamIsReturnValue = false; }
--- a/sema/SymbolTableBuilder.d Tue Apr 29 15:13:38 2008 +0200 +++ b/sema/SymbolTableBuilder.d Tue Apr 29 17:56:52 2008 +0200 @@ -23,21 +23,13 @@ { visitExp(d.returnType); visitExp(d.identifier); - SmallArray!(DType, 8) arg_types; foreach (arg; d.funcArgs) - { visitDecl(arg); - arg_types ~= d.env.find(arg.identifier).type; - } foreach (stmt; d.statements) visitStmt(stmt); - auto func_t = new DFunction(d.identifier); - func_t.returnType = d.env.findType(d.returnType); - func_t.params = arg_types.safe(); - auto sym = d.env.find(d.identifier); - sym.type = func_t; + sym.type = d.type; } override void visitVarDecl(VarDecl d) @@ -54,7 +46,7 @@ { DType[char[]] types; - auto st = (cast(DStruct)s.env.types[s.identifier.get]); + auto st = s.env.types[s.identifier.get].asStruct; foreach(varDecl ; s.vars) st.addMember(typeOf(varDecl.varType, varDecl.env), varDecl.identifier.get); @@ -152,7 +144,6 @@ auto sym = sc.add(s.identifier); s.env = sc; auto type = new DStruct(s.identifier); - sc.types[s.identifier.get] = type;