comparison gen/tollvm.cpp @ 203:e881c9b1c738 trunk

[svn r219] Fixed: the tango/lib/gc/basic garbage collector now compiles and links into an executable (change in tango/lib/llvmdc-posix.mak), closes #5 . Changed: removed the crappy realloc based dynamic memory runtime and started moving over to DMD style runtime support, part of moving to real GC. Fixed: dynamic arrays now use GC runtime for allocating memory. Fixed: new expression now use GC for allocating memory. Changed: revamped the dynamic array support routines related to dynamic memory. Fixed: assertions no longer create exsessive allocas. Changed: misc. minor cleanups.
author lindquist
date Tue, 13 May 2008 14:42:09 +0200
parents 8f9191180c7a
children 9d44ec83acd1
comparison
equal deleted inserted replaced
202:56e0c5b1d428 203:e881c9b1c738
653 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb()); 653 return new llvm::GetElementPtrInst(ptr, v.begin(), v.end(), var, bb?bb:gIR->scopebb());
654 } 654 }
655 655
656 ////////////////////////////////////////////////////////////////////////////////////////// 656 //////////////////////////////////////////////////////////////////////////////////////////
657 657
658 llvm::Value* DtoRealloc(llvm::Value* ptr, const llvm::Type* ty) 658 llvm::Value* DtoNew(Type* newtype)
659 { 659 {
660 /*size_t sz = gTargetData->getTypeSize(ty); 660 // get runtime function
661 llvm::ConstantInt* n = llvm::ConstantInt::get(DtoSize_t(), sz, false); 661 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_allocmemoryT");
662 if (ptr == 0) { 662 // get type info
663 llvm::PointerType* i8pty = getPtrToType(llvm::Type::Int8Ty); 663 llvm::Constant* ti = DtoTypeInfoOf(newtype);
664 ptr = llvm::ConstantPointerNull::get(i8pty); 664 assert(isaPointer(ti));
665 } 665 // call runtime
666 return DtoRealloc(ptr, n);*/ 666 llvm::SmallVector<llvm::Value*,1> arg;
667 return NULL; 667 arg.push_back(ti);
668 } 668 // allocate
669 669 llvm::Value* mem = gIR->ir->CreateCall(fn, arg.begin(), arg.end(), ".gc_mem");
670 ////////////////////////////////////////////////////////////////////////////////////////// 670 // cast
671 671 return DtoBitCast(mem, getPtrToType(DtoType(newtype)), ".gc_mem");
672 llvm::Value* DtoRealloc(llvm::Value* ptr, llvm::Value* n)
673 {
674 assert(ptr);
675 assert(n);
676
677 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_realloc");
678 assert(fn);
679
680 llvm::Value* newptr = ptr;
681
682 const llvm::PointerType* i8pty = getPtrToType(llvm::Type::Int8Ty);
683 if (ptr->getType() != i8pty) {
684 newptr = new llvm::BitCastInst(ptr,i8pty,"tmp",gIR->scopebb());
685 }
686
687 std::vector<llvm::Value*> args;
688 args.push_back(newptr);
689 args.push_back(n);
690 llvm::Value* ret = new llvm::CallInst(fn, args.begin(), args.end(), "tmprealloc", gIR->scopebb());
691
692 return ret->getType() == ptr->getType() ? ret : new llvm::BitCastInst(ret,ptr->getType(),"tmp",gIR->scopebb());
693 } 672 }
694 673
695 ////////////////////////////////////////////////////////////////////////////////////////// 674 //////////////////////////////////////////////////////////////////////////////////////////
696 675
697 void DtoAssert(Loc* loc, DValue* msg) 676 void DtoAssert(Loc* loc, DValue* msg)
705 // msg param 684 // msg param
706 if (msg) args.push_back(msg->getRVal()); 685 if (msg) args.push_back(msg->getRVal());
707 686
708 // file param 687 // file param
709 c = DtoConstString(loc->filename); 688 c = DtoConstString(loc->filename);
710 llvm::AllocaInst* alloc = new llvm::AllocaInst(c->getType(), "srcfile", gIR->topallocapoint()); 689 llvm::AllocaInst* alloc = gIR->func()->srcfileArg;
690 if (!alloc)
691 {
692 alloc = new llvm::AllocaInst(c->getType(), "srcfile", gIR->topallocapoint());
693 gIR->func()->srcfileArg = alloc;
694 }
711 llvm::Value* ptr = DtoGEPi(alloc, 0,0, "tmp"); 695 llvm::Value* ptr = DtoGEPi(alloc, 0,0, "tmp");
712 DtoStore(c->getOperand(0), ptr); 696 DtoStore(c->getOperand(0), ptr);
713 ptr = DtoGEPi(alloc, 0,1, "tmp"); 697 ptr = DtoGEPi(alloc, 0,1, "tmp");
714 DtoStore(c->getOperand(1), ptr); 698 DtoStore(c->getOperand(1), ptr);
715 args.push_back(alloc); 699 args.push_back(alloc);
936 DtoArrayCopyToSlice(s, rhs); 920 DtoArrayCopyToSlice(s, rhs);
937 } 921 }
938 } 922 }
939 // rhs is slice 923 // rhs is slice
940 else if (DSliceValue* s = rhs->isSlice()) { 924 else if (DSliceValue* s = rhs->isSlice()) {
925 assert(s->getType()->toBasetype() == lhs->getType()->toBasetype());
941 DtoSetArray(lhs->getLVal(),s->len,s->ptr); 926 DtoSetArray(lhs->getLVal(),s->len,s->ptr);
942 } 927 }
943 // null 928 // null
944 else if (rhs->isNull()) { 929 else if (rhs->isNull()) {
945 DtoSetArrayToNull(lhs->getLVal()); 930 DtoSetArrayToNull(lhs->getLVal());
1280 return llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2); 1265 return llvm::ConstantExpr::getGetElementPtr(gvar,idxs,2);
1281 } 1266 }
1282 1267
1283 ////////////////////////////////////////////////////////////////////////////////////////// 1268 //////////////////////////////////////////////////////////////////////////////////////////
1284 1269
1285 llvm::Constant* DtoConstNullPtr(const llvm::Type* t)
1286 {
1287 return llvm::ConstantPointerNull::get(
1288 getPtrToType(t)
1289 );
1290 }
1291
1292 //////////////////////////////////////////////////////////////////////////////////////////
1293
1294 void DtoMemSetZero(llvm::Value* dst, llvm::Value* nbytes) 1270 void DtoMemSetZero(llvm::Value* dst, llvm::Value* nbytes)
1295 { 1271 {
1296 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty); 1272 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty);
1297 llvm::Value *dstarr; 1273 llvm::Value *dstarr;
1298 if (dst->getType() == arrty) 1274 if (dst->getType() == arrty)
1317 1293
1318 ////////////////////////////////////////////////////////////////////////////////////////// 1294 //////////////////////////////////////////////////////////////////////////////////////////
1319 1295
1320 void DtoMemCpy(llvm::Value* dst, llvm::Value* src, llvm::Value* nbytes) 1296 void DtoMemCpy(llvm::Value* dst, llvm::Value* src, llvm::Value* nbytes)
1321 { 1297 {
1322 assert(dst->getType() == src->getType()); 1298 const llvm::Type* arrty = getVoidPtrType();
1323 1299
1324 const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty); 1300 llvm::Value* dstarr;
1325 llvm::Value *dstarr, *srcarr;
1326 if (dst->getType() == arrty) 1301 if (dst->getType() == arrty)
1327 {
1328 dstarr = dst; 1302 dstarr = dst;
1303 else
1304 dstarr = DtoBitCast(dst, arrty, "tmp");
1305
1306 llvm::Value* srcarr;
1307 if (src->getType() == arrty)
1329 srcarr = src; 1308 srcarr = src;
1330 }
1331 else 1309 else
1332 { 1310 srcarr = DtoBitCast(src, arrty, "tmp");
1333 dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb());
1334 srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb());
1335 }
1336 1311
1337 llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32(); 1312 llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32();
1338 std::vector<llvm::Value*> llargs; 1313 std::vector<llvm::Value*> llargs;
1339 llargs.resize(4); 1314 llargs.resize(4);
1340 llargs[0] = dstarr; 1315 llargs[0] = dstarr;
1804 ++p; 1779 ++p;
1805 } 1780 }
1806 // create a noop with the code as the result name! 1781 // create a noop with the code as the result name!
1807 gIR->ir->CreateAnd(DtoConstSize_t(0),DtoConstSize_t(0),s.c_str()); 1782 gIR->ir->CreateAnd(DtoConstSize_t(0),DtoConstSize_t(0),s.c_str());
1808 } 1783 }
1784
1785 //////////////////////////////////////////////////////////////////////////////////////////
1809 1786
1810 const llvm::StructType* DtoInterfaceInfoType() 1787 const llvm::StructType* DtoInterfaceInfoType()
1811 { 1788 {
1812 if (gIR->interfaceInfoType) 1789 if (gIR->interfaceInfoType)
1813 return gIR->interfaceInfoType; 1790 return gIR->interfaceInfoType;
1829 // create type 1806 // create type
1830 gIR->interfaceInfoType = llvm::StructType::get(types); 1807 gIR->interfaceInfoType = llvm::StructType::get(types);
1831 1808
1832 return gIR->interfaceInfoType; 1809 return gIR->interfaceInfoType;
1833 } 1810 }
1811
1812 //////////////////////////////////////////////////////////////////////////////////////////
1813
1814 llvm::Constant* DtoTypeInfoOf(Type* type)
1815 {
1816 const llvm::Type* typeinfotype = DtoType(Type::typeinfo->type);
1817 TypeInfoDeclaration* tidecl = type->getTypeInfoDeclaration();
1818 DtoForceDeclareDsymbol(tidecl);
1819 assert(tidecl->ir.irGlobal != NULL);
1820 llvm::Constant* c = isaConstant(tidecl->ir.irGlobal->value);
1821 assert(c != NULL);
1822 return llvm::ConstantExpr::getBitCast(c, typeinfotype);
1823 }
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835