comparison gen/arrays.cpp @ 102:027b8d8b71ec trunk

[svn r106] Turns out the last commit wasn't enough, now the D->LLVM process is even more split up. Basically it tries to do the following in order: Resolve types, Declare symbols, Create constant initializers, Apply initializers, Generate functions bodies. ClassInfo is now has the most useful(biased?) members working. Probably other stuf...
author lindquist
date Sun, 18 Nov 2007 06:52:57 +0100
parents 5071469303d4
children 288fe1029e1f
comparison
equal deleted inserted replaced
101:169fda3a77d4 102:027b8d8b71ec
58 return arrty; 58 return arrty;
59 } 59 }
60 60
61 ////////////////////////////////////////////////////////////////////////////////////////// 61 //////////////////////////////////////////////////////////////////////////////////////////
62 62
63 void DtoNullArray(llvm::Value* v) 63 void DtoSetArrayToNull(llvm::Value* v)
64 { 64 {
65 assert(gIR);
66
67 llvm::Value* len = DtoGEPi(v,0,0,"tmp",gIR->scopebb()); 65 llvm::Value* len = DtoGEPi(v,0,0,"tmp",gIR->scopebb());
68 llvm::Value* zerolen = llvm::ConstantInt::get(len->getType()->getContainedType(0), 0, false); 66 llvm::Value* zerolen = llvm::ConstantInt::get(len->getType()->getContainedType(0), 0, false);
69 new llvm::StoreInst(zerolen, len, gIR->scopebb()); 67 new llvm::StoreInst(zerolen, len, gIR->scopebb());
70 68
71 llvm::Value* ptr = DtoGEPi(v,0,1,"tmp",gIR->scopebb()); 69 llvm::Value* ptr = DtoGEPi(v,0,1,"tmp",gIR->scopebb());
591 589
592 // pass element typeinfo ? 590 // pass element typeinfo ?
593 if (useti) { 591 if (useti) {
594 TypeInfoDeclaration* ti = DtoDType(l->getType())->next->getTypeInfoDeclaration(); 592 TypeInfoDeclaration* ti = DtoDType(l->getType())->next->getTypeInfoDeclaration();
595 if (!ti->llvmValue) { 593 if (!ti->llvmValue) {
596 ti->toObjFile(); 594 DtoForceConstInitDsymbol(ti);
597 } 595 }
598 Logger::cout() << "typeinfo decl: " << *ti->llvmValue << '\n'; 596 Logger::cout() << "typeinfo decl: " << *ti->llvmValue << '\n';
599 597
600 pt = fn->getFunctionType()->getParamType(2); 598 pt = fn->getFunctionType()->getParamType(2);
601 args.push_back(DtoBitCast(ti->llvmValue, pt)); 599 args.push_back(DtoBitCast(ti->llvmValue, pt));
784 } 782 }
785 assert(0); 783 assert(0);
786 return 0; 784 return 0;
787 } 785 }
788 786
787 //////////////////////////////////////////////////////////////////////////////////////////
788 DValue* DtoCastArray(DValue* u, Type* to)
789 {
790 const llvm::Type* tolltype = DtoType(to);
791
792 Type* totype = DtoDType(to);
793 Type* fromtype = DtoDType(u->getType());
794 assert(fromtype->ty == Tarray || fromtype->ty == Tsarray);
795
796 llvm::Value* rval;
797 llvm::Value* rval2;
798 bool isslice = false;
799
800 Logger::cout() << "from array or sarray" << '\n';
801 if (totype->ty == Tpointer) {
802 Logger::cout() << "to pointer" << '\n';
803 assert(fromtype->next == totype->next || totype->next->ty == Tvoid);
804 llvm::Value* ptr = DtoGEPi(u->getRVal(),0,1,"tmp",gIR->scopebb());
805 rval = new llvm::LoadInst(ptr, "tmp", gIR->scopebb());
806 if (fromtype->next != totype->next)
807 rval = gIR->ir->CreateBitCast(rval, llvm::PointerType::get(llvm::Type::Int8Ty), "tmp");
808 }
809 else if (totype->ty == Tarray) {
810 Logger::cout() << "to array" << '\n';
811 const llvm::Type* ptrty = DtoType(totype->next);
812 if (ptrty == llvm::Type::VoidTy)
813 ptrty = llvm::Type::Int8Ty;
814 ptrty = llvm::PointerType::get(ptrty);
815
816 const llvm::Type* ety = DtoType(fromtype->next);
817 if (ety == llvm::Type::VoidTy)
818 ety = llvm::Type::Int8Ty;
819
820 if (DSliceValue* usl = u->isSlice()) {
821 Logger::println("from slice");
822 Logger::cout() << "from: " << *usl->ptr << " to: " << *ptrty << '\n';
823 rval = new llvm::BitCastInst(usl->ptr, ptrty, "tmp", gIR->scopebb());
824 if (fromtype->next->size() == totype->next->size())
825 rval2 = usl->len;
826 else
827 rval2 = DtoArrayCastLength(usl->len, ety, ptrty->getContainedType(0));
828 }
829 else {
830 llvm::Value* uval = u->getRVal();
831 if (fromtype->ty == Tsarray) {
832 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
833 assert(isaPointer(uval->getType()));
834 const llvm::ArrayType* arrty = isaArray(uval->getType()->getContainedType(0));
835 rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false);
836 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
837 rval = new llvm::BitCastInst(uval, ptrty, "tmp", gIR->scopebb());
838 }
839 else {
840 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
841 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
842 rval2 = DtoGEP(uval,zero,zero,"tmp",gIR->scopebb());
843 rval2 = new llvm::LoadInst(rval2, "tmp", gIR->scopebb());
844 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
845
846 rval = DtoGEP(uval,zero,one,"tmp",gIR->scopebb());
847 rval = new llvm::LoadInst(rval, "tmp", gIR->scopebb());
848 //Logger::cout() << *e->mem->getType() << '|' << *ptrty << '\n';
849 rval = new llvm::BitCastInst(rval, ptrty, "tmp", gIR->scopebb());
850 }
851 }
852 isslice = true;
853 }
854 else if (totype->ty == Tsarray) {
855 Logger::cout() << "to sarray" << '\n';
856 assert(0);
857 }
858 else {
859 assert(0);
860 }
861
862 if (isslice) {
863 Logger::println("isslice");
864 return new DSliceValue(to, rval2, rval);
865 }
866
867 return new DImValue(to, rval);
868 }