Mercurial > projects > ldc
diff 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 |
line wrap: on
line diff
--- a/gen/tollvm.cpp Mon May 12 23:49:07 2008 +0200 +++ b/gen/tollvm.cpp Tue May 13 14:42:09 2008 +0200 @@ -655,41 +655,20 @@ ////////////////////////////////////////////////////////////////////////////////////////// -llvm::Value* DtoRealloc(llvm::Value* ptr, const llvm::Type* ty) -{ - /*size_t sz = gTargetData->getTypeSize(ty); - llvm::ConstantInt* n = llvm::ConstantInt::get(DtoSize_t(), sz, false); - if (ptr == 0) { - llvm::PointerType* i8pty = getPtrToType(llvm::Type::Int8Ty); - ptr = llvm::ConstantPointerNull::get(i8pty); - } - return DtoRealloc(ptr, n);*/ - return NULL; -} - -////////////////////////////////////////////////////////////////////////////////////////// - -llvm::Value* DtoRealloc(llvm::Value* ptr, llvm::Value* n) +llvm::Value* DtoNew(Type* newtype) { - assert(ptr); - assert(n); - - llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_realloc"); - assert(fn); - - llvm::Value* newptr = ptr; - - const llvm::PointerType* i8pty = getPtrToType(llvm::Type::Int8Ty); - if (ptr->getType() != i8pty) { - newptr = new llvm::BitCastInst(ptr,i8pty,"tmp",gIR->scopebb()); - } - - std::vector<llvm::Value*> args; - args.push_back(newptr); - args.push_back(n); - llvm::Value* ret = new llvm::CallInst(fn, args.begin(), args.end(), "tmprealloc", gIR->scopebb()); - - return ret->getType() == ptr->getType() ? ret : new llvm::BitCastInst(ret,ptr->getType(),"tmp",gIR->scopebb()); + // get runtime function + llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_allocmemoryT"); + // get type info + llvm::Constant* ti = DtoTypeInfoOf(newtype); + assert(isaPointer(ti)); + // call runtime + llvm::SmallVector<llvm::Value*,1> arg; + arg.push_back(ti); + // allocate + llvm::Value* mem = gIR->ir->CreateCall(fn, arg.begin(), arg.end(), ".gc_mem"); + // cast + return DtoBitCast(mem, getPtrToType(DtoType(newtype)), ".gc_mem"); } ////////////////////////////////////////////////////////////////////////////////////////// @@ -707,7 +686,12 @@ // file param c = DtoConstString(loc->filename); - llvm::AllocaInst* alloc = new llvm::AllocaInst(c->getType(), "srcfile", gIR->topallocapoint()); + llvm::AllocaInst* alloc = gIR->func()->srcfileArg; + if (!alloc) + { + alloc = new llvm::AllocaInst(c->getType(), "srcfile", gIR->topallocapoint()); + gIR->func()->srcfileArg = alloc; + } llvm::Value* ptr = DtoGEPi(alloc, 0,0, "tmp"); DtoStore(c->getOperand(0), ptr); ptr = DtoGEPi(alloc, 0,1, "tmp"); @@ -938,6 +922,7 @@ } // rhs is slice else if (DSliceValue* s = rhs->isSlice()) { + assert(s->getType()->toBasetype() == lhs->getType()->toBasetype()); DtoSetArray(lhs->getLVal(),s->len,s->ptr); } // null @@ -1282,15 +1267,6 @@ ////////////////////////////////////////////////////////////////////////////////////////// -llvm::Constant* DtoConstNullPtr(const llvm::Type* t) -{ - return llvm::ConstantPointerNull::get( - getPtrToType(t) - ); -} - -////////////////////////////////////////////////////////////////////////////////////////// - void DtoMemSetZero(llvm::Value* dst, llvm::Value* nbytes) { const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty); @@ -1319,20 +1295,19 @@ void DtoMemCpy(llvm::Value* dst, llvm::Value* src, llvm::Value* nbytes) { - assert(dst->getType() == src->getType()); + const llvm::Type* arrty = getVoidPtrType(); - const llvm::Type* arrty = getPtrToType(llvm::Type::Int8Ty); - llvm::Value *dstarr, *srcarr; + llvm::Value* dstarr; if (dst->getType() == arrty) - { dstarr = dst; - srcarr = src; - } else - { - dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); - srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); - } + dstarr = DtoBitCast(dst, arrty, "tmp"); + + llvm::Value* srcarr; + if (src->getType() == arrty) + srcarr = src; + else + srcarr = DtoBitCast(src, arrty, "tmp"); llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32(); std::vector<llvm::Value*> llargs; @@ -1807,6 +1782,8 @@ gIR->ir->CreateAnd(DtoConstSize_t(0),DtoConstSize_t(0),s.c_str()); } +////////////////////////////////////////////////////////////////////////////////////////// + const llvm::StructType* DtoInterfaceInfoType() { if (gIR->interfaceInfoType) @@ -1831,3 +1808,28 @@ return gIR->interfaceInfoType; } + +////////////////////////////////////////////////////////////////////////////////////////// + +llvm::Constant* DtoTypeInfoOf(Type* type) +{ + const llvm::Type* typeinfotype = DtoType(Type::typeinfo->type); + TypeInfoDeclaration* tidecl = type->getTypeInfoDeclaration(); + DtoForceDeclareDsymbol(tidecl); + assert(tidecl->ir.irGlobal != NULL); + llvm::Constant* c = isaConstant(tidecl->ir.irGlobal->value); + assert(c != NULL); + return llvm::ConstantExpr::getBitCast(c, typeinfotype); +} + + + + + + + + + + + +