[svn r40] Cleaned up some of the array routines to use gep/load/store instead of memcpy/memset.

Resizing arrays did not allocate enough memory for types bigger than 1 byte.
This commit is contained in:
Tomas Lindquist Olsen
2007-10-09 07:51:13 +02:00
parent e251fc42b2
commit 4fdad2c750
5 changed files with 59 additions and 55 deletions

View File

@@ -25,13 +25,13 @@ DOXYFILE_ENCODING = UTF-8
# The PROJECT_NAME tag is a single word (or a sequence of words surrounded
# by quotes) that should identify the project.
PROJECT_NAME = DMDFE
PROJECT_NAME = DMD
# The PROJECT_NUMBER tag can be used to enter a project or revision number.
# This could be handy for archiving the generated documentation or
# if some version control system is used.
PROJECT_NUMBER = 1.020
PROJECT_NUMBER = 1.022
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
# base path where the generated documentation will be put.

View File

@@ -71,29 +71,18 @@ const llvm::ArrayType* LLVM_DtoStaticArrayType(Type* t)
//////////////////////////////////////////////////////////////////////////////////////////
llvm::Value* LLVM_DtoNullArray(llvm::Value* v)
void LLVM_DtoNullArray(llvm::Value* v)
{
assert(gIR);
d_uns64 n = (global.params.is64bit) ? 16 : 8;
llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty);
llvm::Value* len = LLVM_DtoGEPi(v,0,0,"tmp",gIR->scopebb());
llvm::Value* zerolen = llvm::ConstantInt::get(len->getType()->getContainedType(0), 0, false);
new llvm::StoreInst(zerolen, len, gIR->scopebb());
llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb());
llvm::Function* fn = LLVM_DeclareMemSet32();
std::vector<llvm::Value*> llargs;
llargs.resize(4);
llargs[0] = arr;
llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false);
llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false);
llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
//Logger::cout() << *fn << '|' << *fn->getType() << '\n';
//Logger::cout() << "to null array call: " << *llargs[0] << '|' << *llargs[1] << '|' << *llargs[2] << '|' << *llargs[3] << '\n';
llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
return ret;
llvm::Value* ptr = LLVM_DtoGEPi(v,0,1,"tmp",gIR->scopebb());
const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(ptr->getType()->getContainedType(0));
llvm::Value* nullptr = llvm::ConstantPointerNull::get(pty);
new llvm::StoreInst(nullptr, ptr, gIR->scopebb());
}
//////////////////////////////////////////////////////////////////////////////////////////
@@ -103,22 +92,15 @@ void LLVM_DtoArrayAssign(llvm::Value* dst, llvm::Value* src)
assert(gIR);
if (dst->getType() == src->getType())
{
d_uns64 n = (global.params.is64bit) ? 16 : 8;
llvm::Value* ptr = LLVM_DtoGEPi(src,0,0,"tmp",gIR->scopebb());
llvm::Value* val = new llvm::LoadInst(ptr,"tmp",gIR->scopebb());
ptr = LLVM_DtoGEPi(dst,0,0,"tmp",gIR->scopebb());
new llvm::StoreInst(val, ptr, gIR->scopebb());
llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty);
llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb());
llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb());
llvm::Function* fn = LLVM_DeclareMemCpy32();
std::vector<llvm::Value*> llargs;
llargs.resize(4);
llargs[0] = dstarr;
llargs[1] = srcarr;
llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false);
llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
ptr = LLVM_DtoGEPi(src,0,1,"tmp",gIR->scopebb());
val = new llvm::LoadInst(ptr,"tmp",gIR->scopebb());
ptr = LLVM_DtoGEPi(dst,0,1,"tmp",gIR->scopebb());
new llvm::StoreInst(val, ptr, gIR->scopebb());
}
else
{
@@ -347,7 +329,6 @@ static llvm::Value* get_slice_ptr(elem* e, llvm::Value*& sz)
return ret;
}
//////////////////////////////////////////////////////////////////////////////////////////
void LLVM_DtoArrayCopy(elem* dst, elem* src)
{
Logger::cout() << "Array copy ((((" << *src->mem << ")))) into ((((" << *dst->mem << "))))\n";
@@ -358,8 +339,9 @@ void LLVM_DtoArrayCopy(elem* dst, elem* src)
llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty);
llvm::Value* sz1;
llvm::Value* sz2;
llvm::Value* dstarr = new llvm::BitCastInst(get_slice_ptr(dst,sz1),arrty,"tmp",gIR->scopebb());
llvm::Value* sz2;
llvm::Value* srcarr = new llvm::BitCastInst(get_slice_ptr(src,sz2),arrty,"tmp",gIR->scopebb());
llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32();
@@ -408,8 +390,14 @@ void LLVM_DtoResizeDynArray(llvm::Value* arr, llvm::Value* sz)
{
llvm::Value* ptr = LLVM_DtoGEPi(arr, 0, 1, "tmp", gIR->scopebb());
llvm::Value* ptrld = new llvm::LoadInst(ptr,"tmp",gIR->scopebb());
llvm::Value* newptr = LLVM_DtoRealloc(ptrld, sz);
size_t isz = gTargetData->getTypeSize(ptrld->getType()->getContainedType(0));
llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), isz, false);
llvm::Value* bytesz = llvm::BinaryOperator::createMul(n,sz,"tmp",gIR->scopebb());
llvm::Value* newptr = LLVM_DtoRealloc(ptrld, bytesz);
new llvm::StoreInst(newptr,ptr,gIR->scopebb());
llvm::Value* len = LLVM_DtoGEPi(arr, 0, 0, "tmp", gIR->scopebb());
new llvm::StoreInst(sz,len,gIR->scopebb());
}

View File

@@ -11,7 +11,7 @@ void LLVM_DtoArrayCopy(elem* dst, elem* src);
void LLVM_DtoArrayInit(llvm::Value* l, llvm::Value* r);
void LLVM_DtoArrayAssign(llvm::Value* l, llvm::Value* r);
void LLVM_DtoSetArray(llvm::Value* arr, llvm::Value* dim, llvm::Value* ptr);
llvm::Value* LLVM_DtoNullArray(llvm::Value* v);
void LLVM_DtoNullArray(llvm::Value* v);
void LLVM_DtoNewDynArray(llvm::Value* dst, llvm::Value* dim, const llvm::Type* ty);
void LLVM_DtoResizeDynArray(llvm::Value* arr, llvm::Value* sz);

View File

@@ -2158,21 +2158,10 @@ elem* HaltExp::toElem(IRState* p)
Logger::print("HaltExp::toElem: %s | %s\n", toChars(), type->toChars());
LOG_SCOPE;
std::vector<llvm::Value*> llargs;
llargs.resize(3);
llargs[0] = llvm::ConstantInt::get(llvm::Type::Int1Ty, 0, false);
llargs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false);
llargs[2] = llvm::ConstantPointerNull::get(llvm::PointerType::get(llvm::Type::Int8Ty));
//Logger::cout() << *llargs[0] << '|' << *llargs[1] << '\n';
llvm::Function* fn = LLVM_D_GetRuntimeFunction(p->module, "_d_assert");
assert(fn);
llvm::CallInst* call = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", p->scopebb());
call->setCallingConv(llvm::CallingConv::C);
//new llvm::UnreachableInst(p->scopebb());
llvm::Value* loca = llvm::ConstantInt::get(llvm::Type::Int32Ty, loc.linnum, false);
LLVM_DtoAssert(llvm::ConstantInt::getFalse(), loca, NULL);
//new llvm::UnreachableInst(p->scopebb());
return 0;
}
@@ -2348,6 +2337,26 @@ elem* NegExp::toElem(IRState* p)
//////////////////////////////////////////////////////////////////////////////////////////
elem* CatExp::toElem(IRState* p)
{
Logger::print("CatExp::toElem: %s | %s\n", toChars(), type->toChars());
LOG_SCOPE;
assert(0 && "array concatenation is not yet implemented");
elem* lhs = e1->toElem(p);
elem* rhs = e2->toElem(p);
// determine new size
delete lhs;
delete rhs;
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////
#define STUB(x) elem *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; }
//STUB(IdentityExp);
//STUB(CondExp);
@@ -2374,7 +2383,7 @@ STUB(InExp);
//STUB(MulAssignExp);
//STUB(ModExp);
//STUB(ModAssignExp);
STUB(CatExp);
//STUB(CatExp);
STUB(CatAssignExp);
//STUB(AddExp);
//STUB(AddAssignExp);

7
test/arrays4.d Normal file
View File

@@ -0,0 +1,7 @@
module arrays4;
void main()
{
auto arr = new int[4];
{auto arrcat = arr ~ arr;}
}