Mercurial > projects > dang
changeset 65:932bb3c6b80b new_gen
Merge
author | Anders Halager <halager@gmail.com> |
---|---|
date | Tue, 29 Apr 2008 18:17:09 +0200 |
parents | 91f10c34cd7b (diff) 78a6808b2e0f (current diff) |
children | c62c32e92fde |
files | ast/Exp.d gen/CodeGen.d |
diffstat | 10 files changed, 128 insertions(+), 115 deletions(-) [+] |
line wrap: on
line diff
--- a/ast/Decl.d Tue Apr 29 16:34:11 2008 +0200 +++ b/ast/Decl.d Tue Apr 29 18:17:09 2008 +0200 @@ -7,7 +7,9 @@ import tango.io.Stdout; -import sema.SymbolTable; +import sema.SymbolTable, + sema.DType, + basic.SmallArray; enum DeclType { @@ -27,6 +29,8 @@ { } + DType type() { return null; } + DeclType declType; Scope env; } @@ -37,7 +41,7 @@ Exp e = null) { super(DeclType.VarDecl); - this.type = type; + this.varType = type; this.identifier = identifier; this.init = e; } @@ -46,7 +50,12 @@ { } - Identifier type, identifier; + override DType type() + { + return env.find(identifier).type; + } + + Identifier varType, identifier; Exp init; } @@ -55,7 +64,7 @@ this(Identifier type, Identifier identifier) { super(DeclType.FuncDecl); - this.type = type; + this.returnType = type; this.identifier = identifier; } @@ -73,22 +82,24 @@ { if(auto t = cast(DFunction)env.find(identifier).type) { - if(auto s = cast(DStruct)t.return_type) + if(auto s = cast(DStruct)t.returnType) { VarDecl[] funcArgs; auto i = new Identifier("ret.val"); i.env = env; i.env.add(i); i.env.find(i).type = s; - auto var = new VarDecl(type, i); + auto var = new VarDecl(returnType, i); var.env = env; funcArgs ~= var; funcArgs ~= this.funcArgs; this.funcArgs = funcArgs; - t.return_type = DType.Void; - this.type = new Identifier("void"); + t.returnType = DType.Void; + this.returnType = new Identifier("void"); env.find(identifier).type = t; sret = true; + + myType = null; } } @@ -98,10 +109,27 @@ stmt.simplify(); } - Identifier type, identifier; + override DFunction type() + { + if (myType !is null) + return myType; + + auto t = new DFunction(identifier); + t.returnType = env.findType(returnType); + SmallArray!(DType) array; + foreach (a; funcArgs) + array ~= a.type(); + t.params = array.safe(); + t.firstParamIsReturnValue = this.sret; + myType = t; + return myType; + } + + Identifier returnType, identifier; VarDecl[] funcArgs; Stmt[] statements; bool sret = false; + private DFunction myType; } class StructDecl : Decl @@ -122,7 +150,13 @@ { } + override DType type() + { + return env.findType(identifier); + } + Identifier identifier; VarDecl[] vars; + private DType myType; }
--- a/ast/Exp.d Tue Apr 29 16:34:11 2008 +0200 +++ b/ast/Exp.d Tue Apr 29 18:17:09 2008 +0200 @@ -55,7 +55,7 @@ { DFunction f = cast(DFunction)exp.type(); assert(f !is null, "Can only call functions"); - return f.return_type; + return f.returnType; } Exp exp; @@ -69,7 +69,7 @@ { DFunction func_t = cast(DFunction)exp.type(); assert(func_t !is null, "Calling on something that isn't a function"); - if (cast(DStruct)func_t.return_type is null) + if (cast(DStruct)func_t.returnType is null) return this; auto call = cast(Identifier)exp; @@ -85,9 +85,7 @@ args ~= this.args; auto callExp = new CallExp(exp, args); callExp.env = f.env; - // auto ass = new AssignExp(i, callExp); var.env = f.env; - // ass.env = f.env; auto stmtVar = new DeclStmt(var); auto stmtCall = new ExpStmt(callExp); Stmt[] stmts;
--- a/ast/Stmt.d Tue Apr 29 16:34:11 2008 +0200 +++ b/ast/Stmt.d Tue Apr 29 18:17:09 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 16:34:11 2008 +0200 +++ b/gen/CodeGen.d Tue Apr 29 18:17:09 2008 +0200 @@ -69,45 +69,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.return_type; - 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 @@ -135,7 +96,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(); @@ -184,18 +146,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: @@ -292,7 +244,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); @@ -316,8 +268,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) { @@ -484,7 +436,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); @@ -584,7 +536,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) @@ -592,17 +544,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.return_type); 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 16:34:11 2008 +0200 +++ b/sema/DType.d Tue Apr 29 18:17:09 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 return_type; + DType returnType; + bool firstParamIsReturnValue = false; }
--- a/sema/Declarations.d Tue Apr 29 16:34:11 2008 +0200 +++ b/sema/Declarations.d Tue Apr 29 18:17:09 2008 +0200 @@ -28,10 +28,10 @@ override void visitVarDecl(VarDecl d) { - if(!d.env.findType(d.type)) + if(!d.env.findType(d.varType)) throw error(__LINE__, "Undefined type: '%0'") - .arg(d.type.get) - .loc(d.type.token.location); + .arg(d.varType.get) + .loc(d.varType.token.location); visitExp(d.identifier); if (d.init) @@ -65,11 +65,9 @@ } } - - bool isType(char[] s) + private bool isType(char[] s) { return (s in types? true : false); } - }
--- a/sema/SymbolTableBuilder.d Tue Apr 29 16:34:11 2008 +0200 +++ b/sema/SymbolTableBuilder.d Tue Apr 29 18:17:09 2008 +0200 @@ -21,23 +21,15 @@ override void visitFuncDecl(FuncDecl d) { - visitExp(d.type); + 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.return_type = d.env.findType(d.type); - 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) @@ -45,8 +37,8 @@ if (d.init) visitExp(d.init); - d.env.find(d.identifier).type = typeOf(d.type, d.env); - visitExp(d.type); + d.env.find(d.identifier).type = typeOf(d.varType, d.env); + visitExp(d.varType); visitExp(d.identifier); } @@ -54,11 +46,9 @@ { 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.type, varDecl.env), varDecl.identifier.get); - } + st.addMember(typeOf(varDecl.varType, varDecl.env), varDecl.identifier.get); super.visitStructDecl(s); } @@ -66,13 +56,6 @@ DType typeOf(Identifier id, Scope sc) { return sc.findType(id); - - /* - if (auto type = id.get in types) - return *type; - DType res = new DType(id); - types[id.get] = res; - return res;*/ } } @@ -124,7 +107,7 @@ auto sym = current().add(d.identifier); auto sc = push(); - visitExp(d.type); + visitExp(d.returnType); visitExp(d.identifier); d.env = current(); sc.parentFunction = d; @@ -151,7 +134,7 @@ auto sc = current(); auto sym = sc.add(d.identifier); d.env = sc; - visitExp(d.type); + visitExp(d.varType); visitExp(d.identifier); } @@ -161,7 +144,6 @@ auto sym = sc.add(s.identifier); s.env = sc; auto type = new DStruct(s.identifier); - sc.types[s.identifier.get] = type;
--- a/sema/Visitor.d Tue Apr 29 16:34:11 2008 +0200 +++ b/sema/Visitor.d Tue Apr 29 18:17:09 2008 +0200 @@ -86,7 +86,7 @@ // Declarations: DeclT visitVarDecl(VarDecl d) { - visitExp(d.type); + visitExp(d.varType); visitExp(d.identifier); if (d.init) visitExp(d.init); @@ -99,7 +99,7 @@ DeclT visitFuncDecl(FuncDecl f) { - visitExp(f.type); + visitExp(f.returnType); visitExp(f.identifier); foreach (arg; f.funcArgs) visitDecl(arg);
--- a/tools/AstPrinter.d Tue Apr 29 16:34:11 2008 +0200 +++ b/tools/AstPrinter.d Tue Apr 29 18:17:09 2008 +0200 @@ -33,7 +33,7 @@ case DeclType.FuncDecl: auto funcDecl = cast(FuncDecl)decl; printBeginLine(); - printIdentifier(funcDecl.type); + printIdentifier(funcDecl.returnType); space; printIdentifier(funcDecl.identifier); printFuncArgs(funcDecl); @@ -46,7 +46,7 @@ case DeclType.VarDecl: auto varDecl = cast(VarDecl)decl; printBeginLine(); - printIdentifier(varDecl.type); + printIdentifier(varDecl.varType); space; printIdentifier(varDecl.identifier); if(varDecl.init) @@ -156,7 +156,7 @@ foreach(i, d; decl.funcArgs) { - printIdentifier(d.type); + printIdentifier(d.varType); if(i == 0 && decl.sret) print("*"); space;
--- a/tools/DotPrinter.d Tue Apr 29 16:34:11 2008 +0200 +++ b/tools/DotPrinter.d Tue Apr 29 18:17:09 2008 +0200 @@ -40,7 +40,7 @@ //printFuncArgs(funcDecl.funcArgs); Stdout(dotId(decl))(` [label="function`); Stdout(`\n name: `)(text(funcDecl.identifier)); - Stdout(`\n return type: `)(text(funcDecl.type)); + Stdout(`\n return type: `)(text(funcDecl.returnType)); Stdout(`", shape=box, fillcolor=lightblue, style=filled]`); Stdout.newline; //Stdout(`"`); @@ -55,7 +55,7 @@ //printFuncArgs(funcDecl.funcArgs); Stdout(dotId(decl))(` [label="var`); Stdout(`\n name: `)(text(varDecl.identifier)); - Stdout(`\n type: `)(text(varDecl.type)); + Stdout(`\n type: `)(text(varDecl.varType)); Stdout(`"]`).newline; if (varDecl.init !is null)