Mercurial > projects > dang
diff gen/CodeGen.d @ 129:ed815b31479b
Added a Symbol
author | Anders Halager <halager@gmail.com> |
---|---|
date | Sat, 21 Jun 2008 20:41:18 +0200 |
parents | d604152de1eb |
children | 2be29b296081 |
line wrap: on
line diff
--- a/gen/CodeGen.d Sat Jun 21 17:32:27 2008 +0200 +++ b/gen/CodeGen.d Sat Jun 21 20:41:18 2008 +0200 @@ -96,7 +96,7 @@ Type[] param_types; foreach (i, p; fd.funcArgs) { - DType t = p.env.find(p.identifier).type; + DType t = p.identifier.type; if (auto st = t.asStruct()) { Type pointer = PointerType.Get(llvm(st)); @@ -110,20 +110,20 @@ else param_types ~= llvm(t); } - auto ret_t = fd.env.find(fd.identifier).type; + auto ret_t = 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.getMangled); + auto llfunc = m.addFunction(func_t, fd.sym.getMangledFQN()); foreach (i, p; fd.funcArgs) { if(i == 0 && fd.sret) llfunc.addParamAttr(0, ParamAttr.StructRet); - DType t = p.env.find(p.identifier).type; + DType t = p.identifier.type; if (auto st = t.asStruct) { if (i == 0 && fd.sret) @@ -173,7 +173,7 @@ return; llvm(funcDecl.type); - auto llfunc = m.getNamedFunction(funcDecl.identifier.getMangled); + auto llfunc = m.getNamedFunction(funcDecl.sym.getMangledFQN()); auto func_tp = cast(PointerType)llfunc.type; auto func_t = cast(FunctionType)func_tp.elementType(); auto ret_t = func_t.returnType(); @@ -214,7 +214,7 @@ case DeclType.VarDecl: auto varDecl = cast(VarDecl)decl; - auto id = varDecl.env.find(varDecl.identifier); + auto id = varDecl.identifier; Type t = llvm(id.type); GlobalVariable g = m.addGlobal(t, id.get); g.initializer = ConstantInt.GetS(t, 0); @@ -241,17 +241,21 @@ { case DeclType.VarDecl: auto varDecl = cast(VarDecl)decl; - auto name = varDecl.identifier.get; - auto sym = varDecl.env.find(varDecl.identifier); - auto AI = b.buildAlloca(llvm(sym.type), name); + auto id = varDecl.identifier; + auto name = id.get; + auto AI = b.buildAlloca(llvm(id.type), name); table[name] = AI; if (varDecl.init) { LValue dst = genLValue(varDecl.identifier); RValue src = genExpression(varDecl.init); - storeThroughLValue(dst, src, sym.type); + storeThroughLValue(dst, src, id.type); } break; + + case DeclType.FuncDecl: + genRootDecl(decl); + break; default: } @@ -344,13 +348,20 @@ auto callExp = cast(CallExp)exp; // BUG: Might not be a simple identifier, a.foo(x) is also a // valid call - or foo(x)(y) for that matter. - auto id = exp.env.find(cast(Identifier)callExp.exp); + + // if type of exp is DFunction - safe to call getSymbol and FQN + // if function ptr: do something else + // if delegate do a third thing + // if struct/class check for opCall + DType type = callExp.exp.type; + assert (type.isFunction(), "Can only call functions"); + auto sym = callExp.exp.getSymbol(); scope args = new Value[callExp.args.length]; foreach (i, arg; callExp.args) args[i] = genExpression(arg).value; - llvm(id.type); - auto f = m.getNamedFunction(id.getMangled); - DFunction f_type = cast(DFunction)id.type; + llvm(type); + auto f = m.getNamedFunction(sym.getMangledFQN()); + DFunction f_type = type.asFunction(); bool isVoid = f_type.returnType is DType.Void; // BUG: doesn't do implicit type-conversion on args auto r = b.buildCall(f, args, isVoid? "" : "call"); @@ -366,8 +377,7 @@ return genTypeCast(value, exp.type, castExp.type); case ExpType.Identifier: - auto identifier = cast(Identifier)exp; - auto id = exp.env.find(identifier); + auto id = cast(Identifier)exp; if(id.type.isStruct() || id.type.isArray()) return RValue(table.find(id.get)); else @@ -518,7 +528,7 @@ Value False = ConstantInt.GetS(cond.type, 0); cond = b.buildICmp(IntPredicate.NE, cond, False, ".cond"); } - auto func_name = stmt.env.parentFunction().identifier.getMangled; + auto func_name = symbolName(stmt.env.parentFunction()); Function func = m.getNamedFunction(func_name); bool has_else = (ifStmt.else_body !is null); @@ -546,8 +556,8 @@ break; case StmtType.While: auto wStmt = cast(WhileStmt)stmt; - auto func_name = stmt.env.parentFunction().identifier.get; - Function func = m.getNamedFunction(func_name); + auto fd = stmt.env.parentFunction(); + Function func = m.getNamedFunction(symbolName(fd)); auto condBB = func.appendBasicBlock("cond"); auto bodyBB = func.appendBasicBlock("body"); @@ -632,8 +642,7 @@ switch(exp.expType) { case ExpType.Identifier: - auto identifier = cast(Identifier)exp; - auto id = exp.env.find(identifier); + auto id = cast(Identifier)exp; return LValue(table.find(id.get)); case ExpType.Deref: // LValue(*x): load(x) @@ -663,9 +672,8 @@ switch (mem.target.expType) { case ExpType.Identifier: - auto identifier = cast(Identifier)mem.target; + auto id = cast(Identifier)mem.target; auto child = mem.child; - auto id = exp.env.find(identifier); Value v = table.find(id.get); DType t = id.type; auto st = t.asStruct; @@ -682,11 +690,9 @@ case ExpType.MemberReference: auto addr = genLValue(mem.target).getAddress(); auto child = mem.child; - auto symChild = child.env.find(child); - DType t = mem.target.type; - auto st = t.asStruct; + DStruct t = mem.target.type.asStruct(); - int i = st.indexOf(child.get); + int i = t.indexOf(child.get); Value[2] vals; vals[0] = ZeroIndex; @@ -785,6 +791,12 @@ return RValue(res); } + /// Get the mangled name of a function + char[] symbolName(FuncDecl f) + { + return f.sym.getMangledFQN(); + } + /** Get the LLVM Type corresponding to a DType. @@ -846,13 +858,17 @@ Type res = FunctionType.Get(ret_t, params.unsafe()); type_map[t] = res; + /* + //TODO: create own DType -> FunctionType mapping without names auto id = new Identifier(f.name); id.setType(f); - auto f_t = m.getNamedFunction(id.getMangled); + auto f_name = symbolName(id); + auto f_t = m.getNamedFunction(f_name); if(f_t is null) { - auto llfunc = m.addFunction(res, id.getMangled); + Stdout("oh noes").newline; + auto llfunc = m.addFunction(res, f_name); foreach (i, param; f.params) if (param.isStruct) @@ -864,6 +880,7 @@ llfunc.addParamAttr(0, ParamAttr.StructRet); } } + */ return res; } else if (auto f = t.asPointer) @@ -901,8 +918,8 @@ type_map[DType.Real] = Type.X86_FP80; type_map[DType.Char] = Type.Int8; - type_map[DType.WChar] = Type.Int16; - type_map[DType.DChar] = Type.Int32; + type_map[DType.WChar] = Type.Int16; + type_map[DType.DChar] = Type.Int32; } private: @@ -916,8 +933,6 @@ Type BytePtr; Type[DType] type_map; - FuncDecl[char[]] functions; - SimpleSymbolTable table; } @@ -1009,11 +1024,9 @@ this.dg = dg; } - override void visitModule(DModule m) + override void visitFuncDecl(FuncDecl fd) { - foreach (decl; m.decls) - if (auto f = cast(FuncDecl)decl) - dg(f); + dg(fd); } }