comparison gen/arrays.c @ 36:c0967c4b2a74 trunk

[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.
author lindquist
date Tue, 09 Oct 2007 07:51:13 +0200
parents 4648206ca213
children 77cdca8c210f
comparison
equal deleted inserted replaced
35:3cfcb944304e 36:c0967c4b2a74
69 return arrty; 69 return arrty;
70 } 70 }
71 71
72 ////////////////////////////////////////////////////////////////////////////////////////// 72 //////////////////////////////////////////////////////////////////////////////////////////
73 73
74 llvm::Value* LLVM_DtoNullArray(llvm::Value* v) 74 void LLVM_DtoNullArray(llvm::Value* v)
75 { 75 {
76 assert(gIR); 76 assert(gIR);
77 d_uns64 n = (global.params.is64bit) ? 16 : 8; 77
78 78 llvm::Value* len = LLVM_DtoGEPi(v,0,0,"tmp",gIR->scopebb());
79 llvm::Type* i8p_ty = llvm::PointerType::get(llvm::Type::Int8Ty); 79 llvm::Value* zerolen = llvm::ConstantInt::get(len->getType()->getContainedType(0), 0, false);
80 80 new llvm::StoreInst(zerolen, len, gIR->scopebb());
81 llvm::Value* arr = new llvm::BitCastInst(v,i8p_ty,"tmp",gIR->scopebb()); 81
82 82 llvm::Value* ptr = LLVM_DtoGEPi(v,0,1,"tmp",gIR->scopebb());
83 llvm::Function* fn = LLVM_DeclareMemSet32(); 83 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(ptr->getType()->getContainedType(0));
84 std::vector<llvm::Value*> llargs; 84 llvm::Value* nullptr = llvm::ConstantPointerNull::get(pty);
85 llargs.resize(4); 85 new llvm::StoreInst(nullptr, ptr, gIR->scopebb());
86 llargs[0] = arr;
87 llargs[1] = llvm::ConstantInt::get(llvm::Type::Int8Ty, 0, false);
88 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false);
89 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
90
91 //Logger::cout() << *fn << '|' << *fn->getType() << '\n';
92 //Logger::cout() << "to null array call: " << *llargs[0] << '|' << *llargs[1] << '|' << *llargs[2] << '|' << *llargs[3] << '\n';
93
94 llvm::Value* ret = new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
95
96 return ret;
97 } 86 }
98 87
99 ////////////////////////////////////////////////////////////////////////////////////////// 88 //////////////////////////////////////////////////////////////////////////////////////////
100 89
101 void LLVM_DtoArrayAssign(llvm::Value* dst, llvm::Value* src) 90 void LLVM_DtoArrayAssign(llvm::Value* dst, llvm::Value* src)
102 { 91 {
103 assert(gIR); 92 assert(gIR);
104 if (dst->getType() == src->getType()) 93 if (dst->getType() == src->getType())
105 { 94 {
106 d_uns64 n = (global.params.is64bit) ? 16 : 8; 95 llvm::Value* ptr = LLVM_DtoGEPi(src,0,0,"tmp",gIR->scopebb());
107 96 llvm::Value* val = new llvm::LoadInst(ptr,"tmp",gIR->scopebb());
108 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); 97 ptr = LLVM_DtoGEPi(dst,0,0,"tmp",gIR->scopebb());
109 98 new llvm::StoreInst(val, ptr, gIR->scopebb());
110 llvm::Value* dstarr = new llvm::BitCastInst(dst,arrty,"tmp",gIR->scopebb()); 99
111 llvm::Value* srcarr = new llvm::BitCastInst(src,arrty,"tmp",gIR->scopebb()); 100 ptr = LLVM_DtoGEPi(src,0,1,"tmp",gIR->scopebb());
112 101 val = new llvm::LoadInst(ptr,"tmp",gIR->scopebb());
113 llvm::Function* fn = LLVM_DeclareMemCpy32(); 102 ptr = LLVM_DtoGEPi(dst,0,1,"tmp",gIR->scopebb());
114 std::vector<llvm::Value*> llargs; 103 new llvm::StoreInst(val, ptr, gIR->scopebb());
115 llargs.resize(4);
116 llargs[0] = dstarr;
117 llargs[1] = srcarr;
118 llargs[2] = llvm::ConstantInt::get(llvm::Type::Int32Ty, n, false);
119 llargs[3] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
120
121 new llvm::CallInst(fn, llargs.begin(), llargs.end(), "", gIR->scopebb());
122 } 104 }
123 else 105 else
124 { 106 {
125 if (!llvm::isa<llvm::ArrayType>(src->getType()->getContainedType(0))) 107 if (!llvm::isa<llvm::ArrayType>(src->getType()->getContainedType(0)))
126 { 108 {
345 assert(0); 327 assert(0);
346 } 328 }
347 return ret; 329 return ret;
348 } 330 }
349 331
350 //////////////////////////////////////////////////////////////////////////////////////////
351 void LLVM_DtoArrayCopy(elem* dst, elem* src) 332 void LLVM_DtoArrayCopy(elem* dst, elem* src)
352 { 333 {
353 Logger::cout() << "Array copy ((((" << *src->mem << ")))) into ((((" << *dst->mem << "))))\n"; 334 Logger::cout() << "Array copy ((((" << *src->mem << ")))) into ((((" << *dst->mem << "))))\n";
354 335
355 assert(dst->type == elem::SLICE); 336 assert(dst->type == elem::SLICE);
356 assert(src->type == elem::SLICE); 337 assert(src->type == elem::SLICE);
357 338
358 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty); 339 llvm::Type* arrty = llvm::PointerType::get(llvm::Type::Int8Ty);
359 340
360 llvm::Value* sz1; 341 llvm::Value* sz1;
342 llvm::Value* dstarr = new llvm::BitCastInst(get_slice_ptr(dst,sz1),arrty,"tmp",gIR->scopebb());
343
361 llvm::Value* sz2; 344 llvm::Value* sz2;
362 llvm::Value* dstarr = new llvm::BitCastInst(get_slice_ptr(dst,sz1),arrty,"tmp",gIR->scopebb());
363 llvm::Value* srcarr = new llvm::BitCastInst(get_slice_ptr(src,sz2),arrty,"tmp",gIR->scopebb()); 345 llvm::Value* srcarr = new llvm::BitCastInst(get_slice_ptr(src,sz2),arrty,"tmp",gIR->scopebb());
364 346
365 llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32(); 347 llvm::Function* fn = (global.params.is64bit) ? LLVM_DeclareMemCpy64() : LLVM_DeclareMemCpy32();
366 std::vector<llvm::Value*> llargs; 348 std::vector<llvm::Value*> llargs;
367 llargs.resize(4); 349 llargs.resize(4);
406 ////////////////////////////////////////////////////////////////////////////////////////// 388 //////////////////////////////////////////////////////////////////////////////////////////
407 void LLVM_DtoResizeDynArray(llvm::Value* arr, llvm::Value* sz) 389 void LLVM_DtoResizeDynArray(llvm::Value* arr, llvm::Value* sz)
408 { 390 {
409 llvm::Value* ptr = LLVM_DtoGEPi(arr, 0, 1, "tmp", gIR->scopebb()); 391 llvm::Value* ptr = LLVM_DtoGEPi(arr, 0, 1, "tmp", gIR->scopebb());
410 llvm::Value* ptrld = new llvm::LoadInst(ptr,"tmp",gIR->scopebb()); 392 llvm::Value* ptrld = new llvm::LoadInst(ptr,"tmp",gIR->scopebb());
411 llvm::Value* newptr = LLVM_DtoRealloc(ptrld, sz); 393
394 size_t isz = gTargetData->getTypeSize(ptrld->getType()->getContainedType(0));
395 llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), isz, false);
396 llvm::Value* bytesz = llvm::BinaryOperator::createMul(n,sz,"tmp",gIR->scopebb());
397
398 llvm::Value* newptr = LLVM_DtoRealloc(ptrld, bytesz);
412 new llvm::StoreInst(newptr,ptr,gIR->scopebb()); 399 new llvm::StoreInst(newptr,ptr,gIR->scopebb());
400
413 llvm::Value* len = LLVM_DtoGEPi(arr, 0, 0, "tmp", gIR->scopebb()); 401 llvm::Value* len = LLVM_DtoGEPi(arr, 0, 0, "tmp", gIR->scopebb());
414 new llvm::StoreInst(sz,len,gIR->scopebb()); 402 new llvm::StoreInst(sz,len,gIR->scopebb());
415 } 403 }
416 404
417 405