comparison gen/functions.cpp @ 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 74701ba40398
children 0db62b770a49
comparison
equal deleted inserted replaced
233:76ee1bbe487e 234:9760f54af0b7
42 const LLType* actualRettype; 42 const LLType* actualRettype;
43 Type* rt = f->next; 43 Type* rt = f->next;
44 bool retinptr = false; 44 bool retinptr = false;
45 bool usesthis = false; 45 bool usesthis = false;
46 46
47 if (ismain) { 47 // parameter types
48 std::vector<const LLType*> paramvec;
49
50 if (ismain)
51 {
48 rettype = llvm::Type::Int32Ty; 52 rettype = llvm::Type::Int32Ty;
49 actualRettype = rettype; 53 actualRettype = rettype;
50 } 54 if (Argument::dim(f->parameters) == 0)
51 else { 55 {
56 const LLType* arrTy = DtoArrayType(LLType::Int8Ty);
57 const LLType* arrArrTy = DtoArrayType(arrTy);
58 paramvec.push_back(getPtrToType(arrArrTy));
59 }
60 }
61 else{
52 assert(rt); 62 assert(rt);
53 Type* rtfin = DtoDType(rt); 63 Type* rtfin = DtoDType(rt);
54 if (DtoIsReturnedInArg(rt)) { 64 if (DtoIsReturnedInArg(rt)) {
55 rettype = getPtrToType(DtoType(rt)); 65 rettype = getPtrToType(DtoType(rt));
56 actualRettype = llvm::Type::VoidTy; 66 actualRettype = llvm::Type::VoidTy;
59 else { 69 else {
60 rettype = DtoType(rt); 70 rettype = DtoType(rt);
61 actualRettype = rettype; 71 actualRettype = rettype;
62 } 72 }
63 } 73 }
64
65 // parameter types
66 std::vector<const LLType*> paramvec;
67 74
68 if (retinptr) { 75 if (retinptr) {
69 //Logger::cout() << "returning through pointer parameter: " << *rettype << '\n'; 76 //Logger::cout() << "returning through pointer parameter: " << *rettype << '\n';
70 paramvec.push_back(rettype); 77 paramvec.push_back(rettype);
71 } 78 }
400 int funcNumArgs = func->getArgumentList().size(); 407 int funcNumArgs = func->getArgumentList().size();
401 std::vector<llvm::ParamAttrsWithIndex> attrs; 408 std::vector<llvm::ParamAttrsWithIndex> attrs;
402 int k = 0; 409 int k = 0;
403 410
404 int nbyval = 0; 411 int nbyval = 0;
412
413 if (fdecl->isMain() && Argument::dim(f->parameters) == 0)
414 {
415 llvm::ParamAttrsWithIndex PAWI;
416 PAWI.Index = llidx;
417 PAWI.Attrs = llvm::ParamAttr::ByVal;
418 attrs.push_back(PAWI);
419 llidx++;
420 nbyval++;
421 }
405 422
406 for (; llidx <= funcNumArgs && f->parameters->dim > k; ++llidx,++k) 423 for (; llidx <= funcNumArgs && f->parameters->dim > k; ++llidx,++k)
407 { 424 {
408 Argument* fnarg = (Argument*)f->parameters->data[k]; 425 Argument* fnarg = (Argument*)f->parameters->data[k];
409 assert(fnarg); 426 assert(fnarg);
712 } 729 }
713 } 730 }
714 731
715 ////////////////////////////////////////////////////////////////////////////////////////// 732 //////////////////////////////////////////////////////////////////////////////////////////
716 733
717 void DtoMain()
718 {
719 // emit main function llvm style
720 // int main(int argc, char**argv, char**env);
721
722 assert(gIR != 0);
723 IRState& ir = *gIR;
724
725 assert(ir.emitMain && ir.mainFunc);
726
727 // parameter types
728 std::vector<const LLType*> pvec;
729 pvec.push_back((const LLType*)llvm::Type::Int32Ty);
730 const LLType* chPtrType = (const LLType*)getPtrToType(llvm::Type::Int8Ty);
731 pvec.push_back((const LLType*)getPtrToType(chPtrType));
732 pvec.push_back((const LLType*)getPtrToType(chPtrType));
733 const LLType* rettype = (const LLType*)llvm::Type::Int32Ty;
734
735 llvm::FunctionType* functype = llvm::FunctionType::get(rettype, pvec, false);
736 llvm::Function* func = llvm::Function::Create(functype,llvm::GlobalValue::ExternalLinkage,"main",ir.module);
737
738 llvm::BasicBlock* bb = llvm::BasicBlock::Create("entry",func);
739
740 // call static ctors
741 llvm::Function* fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleCtor");
742 llvm::Instruction* apt = llvm::CallInst::Create(fn,"",bb);
743
744 // run unit tests if -unittest is provided
745 if (global.params.useUnitTests) {
746 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleUnitTests");
747 llvm::Instruction* apt = llvm::CallInst::Create(fn,"",bb);
748 }
749
750 // call user main function
751 const llvm::FunctionType* mainty = ir.mainFunc->getFunctionType();
752 llvm::CallInst* call;
753 if (mainty->getNumParams() > 0)
754 {
755 // main with arguments
756 assert(mainty->getNumParams() == 1);
757 std::vector<LLValue*> args;
758 llvm::Function* mfn = LLVM_D_GetRuntimeFunction(ir.module,"_d_main_args");
759
760 llvm::Function::arg_iterator argi = func->arg_begin();
761 args.push_back(argi++);
762 args.push_back(argi++);
763
764 const LLType* at = mainty->getParamType(0)->getContainedType(0);
765 LLValue* arr = new llvm::AllocaInst(at->getContainedType(1)->getContainedType(0), func->arg_begin(), "argstorage", apt);
766 LLValue* a = new llvm::AllocaInst(at, "argarray", apt);
767 LLValue* ptr = DtoGEPi(a,0,0,"tmp",bb);
768 LLValue* v = args[0];
769 if (v->getType() != DtoSize_t())
770 v = new llvm::ZExtInst(v, DtoSize_t(), "tmp", bb);
771 new llvm::StoreInst(v,ptr,bb);
772 ptr = DtoGEPi(a,0,1,"tmp",bb);
773 new llvm::StoreInst(arr,ptr,bb);
774 args.push_back(a);
775 llvm::CallInst::Create(mfn, args.begin(), args.end(), "", bb);
776 call = llvm::CallInst::Create(ir.mainFunc,a,"ret",bb);
777 }
778 else
779 {
780 // main with no arguments
781 call = llvm::CallInst::Create(ir.mainFunc,"ret",bb);
782 }
783 call->setCallingConv(ir.mainFunc->getCallingConv());
784
785 // call static dtors
786 fn = LLVM_D_GetRuntimeFunction(ir.module,"_moduleDtor");
787 llvm::CallInst::Create(fn,"",bb);
788
789 // return
790 llvm::ReturnInst::Create(call,bb);
791 }
792
793 //////////////////////////////////////////////////////////////////////////////////////////
794
795 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl) 734 const llvm::FunctionType* DtoBaseFunctionType(FuncDeclaration* fdecl)
796 { 735 {
797 Dsymbol* parent = fdecl->toParent(); 736 Dsymbol* parent = fdecl->toParent();
798 ClassDeclaration* cd = parent->isClassDeclaration(); 737 ClassDeclaration* cd = parent->isClassDeclaration();
799 assert(cd); 738 assert(cd);