changeset 234:9760f54af0b7 trunk

[svn r250] Fixed the warning about dropping arguments to _Dmain when optimizing. Did a few cleanups in inline asm code.
author lindquist
date Sun, 08 Jun 2008 08:03:19 +0200
parents 76ee1bbe487e
children c603041da866
files gen/arrays.cpp gen/arrays.h gen/asmstmt.cpp gen/d-asm-i386.h gen/functions.cpp gen/functions.h gen/statements.cpp gen/toobj.cpp
diffstat 8 files changed, 45 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- a/gen/arrays.cpp	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/arrays.cpp	Sun Jun 08 08:03:19 2008 +0200
@@ -15,15 +15,20 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
-const llvm::StructType* DtoArrayType(Type* t)
+const llvm::StructType* DtoArrayType(Type* arrayTy)
 {
-    assert(t->next);
-    const LLType* elemty = DtoType(t->next);
+    assert(arrayTy->next);
+    const LLType* elemty = DtoType(arrayTy->next);
     if (elemty == llvm::Type::VoidTy)
         elemty = llvm::Type::Int8Ty;
     return llvm::StructType::get(DtoSize_t(), getPtrToType(elemty), 0);
 }
 
+const llvm::StructType* DtoArrayType(const LLType* t)
+{
+    return llvm::StructType::get(DtoSize_t(), getPtrToType(t), 0);
+}
+
 //////////////////////////////////////////////////////////////////////////////////////////
 
 const llvm::ArrayType* DtoStaticArrayType(Type* t)
--- a/gen/arrays.h	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/arrays.h	Sun Jun 08 08:03:19 2008 +0200
@@ -3,8 +3,9 @@
 
 struct DSliceValue;
 
-const llvm::StructType* DtoArrayType(Type* t);
-const llvm::ArrayType* DtoStaticArrayType(Type* t);
+const llvm::StructType* DtoArrayType(Type* arrayTy);
+const llvm::StructType* DtoArrayType(const LLType* elemTy);
+const llvm::ArrayType* DtoStaticArrayType(Type* sarrayTy);
 
 LLConstant* DtoConstArrayInitializer(ArrayInitializer* si);
 LLConstant* DtoConstSlice(LLConstant* dim, LLConstant* ptr);
--- a/gen/asmstmt.cpp	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/asmstmt.cpp	Sun Jun 08 08:03:19 2008 +0200
@@ -157,6 +157,7 @@
 static void d_format_priv_asm_label(char * buf, unsigned n)
 {
     //ASM_GENERATE_INTERNAL_LABEL(buf, "LDASM", n);//inserts a '*' for use with assemble_name
+    assert(0);
     sprintf(buf, ".LDASM%u", n);
 }
 
@@ -270,42 +271,13 @@
 	    cns = i_cns;
 	    break;
 	case Arg_Pointer:
-// FIXME
-std::cout << "asm fixme Arg_Pointer" << std::endl;
-        if (arg->expr->op == TOKdsymbol)
-        {
-            assert(0);
-            DsymbolExp* dse = (DsymbolExp*)arg->expr;
-            LabelDsymbol* lbl = dse->s->isLabel();
-            assert(lbl);
-            arg_val = lbl->statement->llvmBB;
-            if (!arg_val)
-            {
-                arg_val = lbl->statement->llvmBB = llvm::BasicBlock::Create("label", irs->topfunc());
-            }
-            cns = l_cns;
-        }
-        else
-        {
-            arg_val = arg->expr->toElem(irs)->getRVal();
-            cns = p_cns;
-        }
-        /*if (arg->expr->op == TOKvar)
-        arg_val = arg->expr->toElem(irs);
-        else if (arg->expr->op == TOKdsymbol)
-        arg_val = arg->expr->toElem(irs);
-        else
-        assert(0);*/
+        assert(arg->expr->op == TOKvar);
+        arg_val = arg->expr->toElem(irs)->getRVal();
+        cns = p_cns;
 
 	    break;
 	case Arg_Memory:
-// FIXME
-std::cout << "asm fixme Arg_Memory" << std::endl;
         arg_val = arg->expr->toElem(irs)->getRVal();
-//         if (arg->expr->op == TOKvar)
-//         arg_val = arg->expr->toElem(irs);
-//         else
-//         arg_val = arg->expr->toElem(irs);
 
 	    switch (arg->mode) {
 	    case Mode_Input:  cns = m_cns; break;
@@ -471,8 +443,8 @@
     };
     assert(nargs <= 10);
 
-    static const std::string prefix("<<<out");
-    static const std::string suffix(">>>");
+    static const std::string prefix("<<out");
+    static const std::string suffix(">>");
     std::string argnum;
     std::string needle;
     char buf[10];
@@ -493,8 +465,8 @@
     };
     assert(nargs <= 10);
 
-    static const std::string prefix("<<<in");
-    static const std::string suffix(">>>");
+    static const std::string prefix("<<in");
+    static const std::string suffix(">>");
     std::string argnum;
     std::string needle;
     char buf[10];
--- a/gen/d-asm-i386.h	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/d-asm-i386.h	Sun Jun 08 08:03:19 2008 +0200
@@ -1408,12 +1408,12 @@
 
     void addOperand(const char * fmt, AsmArgType type, Expression * e, AsmCode * asmcode, AsmArgMode mode = Mode_Input) {
 	insnTemplate->writestring((char*) fmt);
-	insnTemplate->printf("<<<%s%d>>>", (mode==Mode_Input)?"in":"out", asmcode->args.dim);
+	insnTemplate->printf("<<%s%d>>", (mode==Mode_Input)?"in":"out", asmcode->args.dim);
 	asmcode->args.push( new AsmArg(type, e, mode) );
     }
     void addOperand2(const char * fmtpre, const char * fmtpost, AsmArgType type, Expression * e, AsmCode * asmcode, AsmArgMode mode = Mode_Input) {
     insnTemplate->writestring((char*) fmtpre);
-    insnTemplate->printf("<<<%s%d>>>", (mode==Mode_Input)?"in":"out", asmcode->args.dim);
+    insnTemplate->printf("<<%s%d>>", (mode==Mode_Input)?"in":"out", asmcode->args.dim);
     insnTemplate->writestring((char*) fmtpost);
     asmcode->args.push( new AsmArg(type, e, mode) );
     }
@@ -1427,7 +1427,7 @@
     }
 
     void addLabel(char* id) {
-    insnTemplate->writestring(".LDASM");
+    insnTemplate->writestring(".LDASM_");
     insnTemplate->writestring(id);
     }
 
--- a/gen/functions.cpp	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/functions.cpp	Sun Jun 08 08:03:19 2008 +0200
@@ -44,11 +44,21 @@
     bool retinptr = false;
     bool usesthis = false;
 
-    if (ismain) {
+    // parameter types
+    std::vector<const LLType*> paramvec;
+
+    if (ismain)
+    {
         rettype = llvm::Type::Int32Ty;
         actualRettype = rettype;
+        if (Argument::dim(f->parameters) == 0)
+        {
+        const LLType* arrTy = DtoArrayType(LLType::Int8Ty);
+        const LLType* arrArrTy = DtoArrayType(arrTy);
+        paramvec.push_back(getPtrToType(arrArrTy));
+        }
     }
-    else {
+    else{
         assert(rt);
         Type* rtfin = DtoDType(rt);
         if (DtoIsReturnedInArg(rt)) {
@@ -62,9 +72,6 @@
         }
     }
 
-    // parameter types
-    std::vector<const LLType*> paramvec;
-
     if (retinptr) {
         //Logger::cout() << "returning through pointer parameter: " << *rettype << '\n';
         paramvec.push_back(rettype);
@@ -403,6 +410,16 @@
 
         int nbyval = 0;
 
+        if (fdecl->isMain() && Argument::dim(f->parameters) == 0)
+        {
+            llvm::ParamAttrsWithIndex PAWI;
+            PAWI.Index = llidx;
+            PAWI.Attrs = llvm::ParamAttr::ByVal;
+            attrs.push_back(PAWI);
+            llidx++;
+            nbyval++;
+        }
+
         for (; llidx <= funcNumArgs && f->parameters->dim > k; ++llidx,++k)
         {
             Argument* fnarg = (Argument*)f->parameters->data[k];
@@ -714,84 +731,6 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
-void DtoMain()
-{
-    // emit main function llvm style
-    // int main(int argc, char**argv, char**env);
-
-    assert(gIR != 0);
-    IRState& ir = *gIR;
-
-    assert(ir.emitMain && ir.mainFunc);
-
-    // parameter types
-    std::vector<const LLType*> pvec;
-    pvec.push_back((const LLType*)llvm::Type::Int32Ty);
-    const LLType* chPtrType = (const LLType*)getPtrToType(llvm::Type::Int8Ty);
-    pvec.push_back((const LLType*)getPtrToType(chPtrType));
-    pvec.push_back((const LLType*)getPtrToType(chPtrType));
-    const LLType* rettype = (const LLType*)llvm::Type::Int32Ty;
-
-    llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false);
-    llvm::Function* func = llvm::Function::Create(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module);
-
-    llvm::BasicBlock* bb = llvm::BasicBlock::Create("entry",func);
-
-    // call static ctors
-    llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor");
-    llvm::Instruction* apt = llvm::CallInst::Create(fn,"",bb);
-
-    // run unit tests if -unittest is provided
-    if (global.params.useUnitTests) {
-        fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleUnitTests");
-        llvm::Instruction* apt = llvm::CallInst::Create(fn,"",bb);
-    }
-
-    // call user main function
-    const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType();
-    llvm::CallInst* call;
-    if (mainty->getNumParams() > 0)
-    {
-        // main with arguments
-        assert(mainty->getNumParams() == 1);
-        std::vector<LLValue*> args;
-        llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args");
-
-        llvm::Function::arg_iterator argi = func->arg_begin();
-        args.push_back(argi++);
-        args.push_back(argi++);
-
-        const LLType* at = mainty->getParamType(0)->getContainedType(0);
-        LLValue* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt);
-        LLValue* a = new llvm::AllocaInst(at, "argarray", apt);
-        LLValue* ptr = DtoGEPi(a,0,0,"tmp",bb);
-        LLValue* v = args[0];
-        if (v->getType() != DtoSize_t())
-            v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb);
-        new llvm::StoreInst(v,ptr,bb);
-        ptr = DtoGEPi(a,0,1,"tmp",bb);
-        new llvm::StoreInst(arr,ptr,bb);
-        args.push_back(a);
-        llvm::CallInst::Create(mfn, args.begin(), args.end(), "", bb);
-        call = llvm::CallInst::Create(ir.mainFunc,a,"ret",bb);
-    }
-    else
-    {
-        // main with no arguments
-        call = llvm::CallInst::Create(ir.mainFunc,"ret",bb);
-    }
-    call->setCallingConv(ir.mainFunc->getCallingConv());
-
-    // call static dtors
-    fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor");
-    llvm::CallInst::Create(fn,"",bb);
-
-    // return
-    llvm::ReturnInst::Create(call,bb);
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////
-
 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl)
 {
     Dsymbol* parent = fdecl->toParent();
--- a/gen/functions.h	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/functions.h	Sun Jun 08 08:03:19 2008 +0200
@@ -13,6 +13,4 @@
 DValue* DtoArgument(Argument* fnarg, Expression* argexp);
 void DtoVariadicArgument(Expression* argexp, LLValue* dst);
 
-void DtoMain();
-
 #endif
--- a/gen/statements.cpp	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/statements.cpp	Sun Jun 08 08:03:19 2008 +0200
@@ -996,7 +996,7 @@
     if (p->asmBlock)
     {
         IRAsmStmt* a = new IRAsmStmt;
-        a->code = ".LDASM";
+        a->code = ".LDASM_";
         a->code += ident->toChars();
         a->code += ":";
         p->asmBlock->s.push_back(a);
--- a/gen/toobj.cpp	Sun Jun 08 06:45:54 2008 +0200
+++ b/gen/toobj.cpp	Sun Jun 08 08:03:19 2008 +0200
@@ -129,11 +129,6 @@
     // do this again as moduleinfo might have pulled something in!
     DtoEmptyAllLists();
 
-    // emit the llvm main function if necessary
-    if (ir.emitMain) {
-        //DtoMain();
-    }
-
     // verify the llvm
     if (!global.params.novalidate) {
         std::string verifyErr;