comparison gen/CodeGen.d @ 143:d76cc5cad4fc

Added partial support for switches. Added support for extern(C) in CodeGen.
author Anders Johnsen <skabet@gmail.com>
date Mon, 21 Jul 2008 01:05:20 +0200
parents b61de188cd0d
children 2149f4a7b48d
comparison
equal deleted inserted replaced
142:1e48315c36fc 143:d76cc5cad4fc
10 ast.Stmt, 10 ast.Stmt,
11 ast.Exp, 11 ast.Exp,
12 ast.Module : DModule = Module; 12 ast.Module : DModule = Module;
13 13
14 import basic.SmallArray, 14 import basic.SmallArray,
15 basic.Attribute,
15 basic.LiteralParsing; 16 basic.LiteralParsing;
16 17
17 import lexer.Token; 18 import lexer.Token;
18 19
19 import sema.Scope, 20 import sema.Scope,
114 if(auto st = cast(DStruct)ret_t) 115 if(auto st = cast(DStruct)ret_t)
115 ret_t = DType.Void; 116 ret_t = DType.Void;
116 else if(auto f = cast(DFunction)ret_t) 117 else if(auto f = cast(DFunction)ret_t)
117 ret_t = f.returnType; 118 ret_t = f.returnType;
118 auto func_t = FunctionType.Get(llvm(ret_t), param_types); 119 auto func_t = FunctionType.Get(llvm(ret_t), param_types);
119 auto llfunc = m.addFunction(func_t, fd.sym.getMangledFQN()); 120 auto llfunc = m.addFunction(func_t, symbolName(fd));
120 121
121 foreach (i, p; fd.funcArgs) 122 foreach (i, p; fd.funcArgs)
122 { 123 {
123 if(i == 0 && fd.sret) 124 if(i == 0 && fd.sret)
124 llfunc.addParamAttr(0, ParamAttr.StructRet); 125 llfunc.addParamAttr(0, ParamAttr.StructRet);
171 // Empty function - declare; 172 // Empty function - declare;
172 if(funcDecl.emptyFunction) 173 if(funcDecl.emptyFunction)
173 return; 174 return;
174 175
175 llvm(funcDecl.type); 176 llvm(funcDecl.type);
176 auto llfunc = m.getNamedFunction(funcDecl.sym.getMangledFQN()); 177 auto llfunc = m.getNamedFunction(symbolName(funcDecl));
177 auto func_tp = cast(PointerType)llfunc.type; 178 auto func_tp = cast(PointerType)llfunc.type;
178 auto func_t = cast(FunctionType)func_tp.elementType(); 179 auto func_t = cast(FunctionType)func_tp.elementType();
179 auto ret_t = func_t.returnType(); 180 auto ret_t = func_t.returnType();
180 181
181 182
325 auto sym = callExp.exp.getSymbol(); 326 auto sym = callExp.exp.getSymbol();
326 scope args = new Value[callExp.args.length]; 327 scope args = new Value[callExp.args.length];
327 foreach (i, arg; callExp.args) 328 foreach (i, arg; callExp.args)
328 args[i] = genExpression(arg).value; 329 args[i] = genExpression(arg).value;
329 llvm(type); 330 llvm(type);
330 auto f = m.getNamedFunction(sym.getMangledFQN()); 331 auto f = m.getNamedFunction(symbolName(callExp.exp));
331 DFunction f_type = type.asFunction(); 332 DFunction f_type = type.asFunction();
332 bool isVoid = f_type.returnType is DType.Void; 333 bool isVoid = f_type.returnType is DType.Void;
333 // BUG: doesn't do implicit type-conversion on args 334 // BUG: doesn't do implicit type-conversion on args
334 auto r = b.buildCall(f, args, isVoid? "" : "call"); 335 auto r = b.buildCall(f, args, isVoid? "" : "call");
335 return RValue(r); 336 return RValue(r);
535 break; 536 break;
536 case StmtType.Switch: 537 case StmtType.Switch:
537 auto sw = cast(SwitchStmt)stmt; 538 auto sw = cast(SwitchStmt)stmt;
538 Value cond = genExpression(sw.cond).value; 539 Value cond = genExpression(sw.cond).value;
539 540
540 auto func_name = stmt.env.parentFunction().identifier.get; 541 auto fc = stmt.env.parentFunction();
541 Function func = m.getNamedFunction(func_name); 542 Function func = m.getNamedFunction(symbolName(fc));
542 543
543 BasicBlock oldBB = b.getInsertBlock(); 544 BasicBlock oldBB = b.getInsertBlock();
544 BasicBlock defBB; 545 BasicBlock defBB;
545 BasicBlock endBB = func.appendBasicBlock("sw.end"); 546 BasicBlock endBB = func.appendBasicBlock("sw.end");
546 if (sw.defaultBlock) 547 if (sw.defaultBlock)
745 } 746 }
746 747
747 /// Get the mangled name of a function 748 /// Get the mangled name of a function
748 char[] symbolName(FuncDecl f) 749 char[] symbolName(FuncDecl f)
749 { 750 {
750 return f.sym.getMangledFQN(); 751 if (f.att.getExtern == Extern.D)
752 return f.sym.getMangledFQN();
753 return f.sym.getName;
754 }
755
756 char[] symbolName(Exp f)
757 {
758 if (f.getSymbol.decl.att.getExtern == Extern.D)
759 return f.getSymbol.getMangledFQN();
760 return f.getSymbol.getName;
751 } 761 }
752 762
753 /** 763 /**
754 Get the LLVM Type corresponding to a DType. 764 Get the LLVM Type corresponding to a DType.
755 765