comparison gen/arrays.cpp @ 593:7042d912767e

Undid some of the previous changes: DtoArrayInit has issues with arrays similar to T[n][].
author Christian Kamm <kamm incasoftware de>
date Sun, 14 Sep 2008 13:47:38 +0200
parents e6bcc4d9e5ff
children 48f079b4fe0f
comparison
equal deleted inserted replaced
592:5fb7ed0ac580 593:7042d912767e
134 args.push_back(dim); 134 args.push_back(dim);
135 args.push_back(val); 135 args.push_back(val);
136 136
137 // determine the right runtime function to call 137 // determine the right runtime function to call
138 const char* funcname = NULL; 138 const char* funcname = NULL;
139 Type* t = value->getType()->toBasetype(); 139 Type* arrayelemty = array->getType()->nextOf()->toBasetype();
140 Type* valuety = value->getType()->toBasetype();
140 141
141 // lets first optimize all zero initializations down to a memset. 142 // lets first optimize all zero initializations down to a memset.
142 // this simplifies codegen later on as llvm null's have no address! 143 // this simplifies codegen later on as llvm null's have no address!
143 if (isaConstant(val) && isaConstant(val)->isNullValue()) 144 if (isaConstant(val) && isaConstant(val)->isNullValue())
144 { 145 {
147 DtoMemSetZero(ptr, nbytes); 148 DtoMemSetZero(ptr, nbytes);
148 return; 149 return;
149 } 150 }
150 151
151 // if not a zero initializer, call the appropriate runtime function! 152 // if not a zero initializer, call the appropriate runtime function!
152 switch (t->ty) 153 switch (arrayelemty->ty)
153 { 154 {
154 case Tbool: 155 case Tbool:
155 funcname = "_d_array_init_i1"; 156 funcname = "_d_array_init_i1";
156 break; 157 break;
157 158
218 case Tstruct: 219 case Tstruct:
219 case Tdelegate: 220 case Tdelegate:
220 case Tarray: 221 case Tarray:
221 case Tsarray: 222 case Tsarray:
222 funcname = "_d_array_init_mem"; 223 funcname = "_d_array_init_mem";
224 assert(arrayelemty == valuety && "ArrayInit doesn't work on elem-initialized static arrays");
223 args[0] = DtoBitCast(args[0], getVoidPtrType()); 225 args[0] = DtoBitCast(args[0], getVoidPtrType());
224 args[2] = DtoBitCast(args[2], getVoidPtrType()); 226 args[2] = DtoBitCast(args[2], getVoidPtrType());
225 args.push_back(DtoConstSize_t(getABITypeSize(DtoType(t)))); 227 args.push_back(DtoConstSize_t(getABITypeSize(DtoType(arrayelemty))));
226 break; 228 break;
227 229
228 default: 230 default:
229 error("unhandled array init: %s = %s", array->getType()->toChars(), value->getType()->toChars()); 231 error("unhandled array init: %s = %s", array->getType()->toChars(), value->getType()->toChars());
230 assert(0 && "unhandled array init"); 232 assert(0 && "unhandled array init");
410 // dim arg 412 // dim arg
411 assert(DtoType(dim->getType()) == DtoSize_t()); 413 assert(DtoType(dim->getType()) == DtoSize_t());
412 LLValue* arrayLen = dim->getRVal(); 414 LLValue* arrayLen = dim->getRVal();
413 415
414 // get runtime function 416 // get runtime function
415 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_newarrayvT"); 417 bool zeroInit = arrayType->toBasetype()->nextOf()->isZeroInit();
418 const char* fnname = defaultInit ? (zeroInit ? "_d_newarrayT" : "_d_newarrayiT") : "_d_newarrayvT";
419 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);
416 420
417 // call allocator 421 // call allocator
418 LLValue* newptr = gIR->CreateCallOrInvoke2(fn, arrayTypeInfo, arrayLen, ".gc_mem")->get(); 422 LLValue* newptr = gIR->CreateCallOrInvoke2(fn, arrayTypeInfo, arrayLen, ".gc_mem")->get();
419 423
420 // cast to wanted type 424 // cast to wanted type
422 if (newptr->getType() != dstType) 426 if (newptr->getType() != dstType)
423 newptr = DtoBitCast(newptr, dstType, ".gc_mem"); 427 newptr = DtoBitCast(newptr, dstType, ".gc_mem");
424 428
425 Logger::cout() << "final ptr = " << *newptr << '\n'; 429 Logger::cout() << "final ptr = " << *newptr << '\n';
426 430
427 DSliceValue* ret = new DSliceValue(arrayType, arrayLen, newptr); 431 return new DSliceValue(arrayType, arrayLen, newptr);
428
429 if (defaultInit) {
430 DValue* e = arrayType->nextOf()->defaultInit()->toElem(gIR);
431 DtoArrayInit(loc, ret, e);
432 }
433
434 return ret;
435 } 432 }
436 433
437 ////////////////////////////////////////////////////////////////////////////////////////// 434 //////////////////////////////////////////////////////////////////////////////////////////
438 DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size_t ndims, bool defaultInit) 435 DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size_t ndims, bool defaultInit)
439 { 436 {
448 for (size_t i=0; i<ndims; ++i) 445 for (size_t i=0; i<ndims; ++i)
449 vtype = vtype->nextOf(); 446 vtype = vtype->nextOf();
450 447
451 // get runtime function 448 // get runtime function
452 bool zeroInit = vtype->isZeroInit(); 449 bool zeroInit = vtype->isZeroInit();
453 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, zeroInit ? "_d_newarraymT" : "_d_newarraymiT" ); 450 const char* fnname = defaultInit ? (zeroInit ? "_d_newarraymT" : "_d_newarraymiT") : "_d_newarraymvT";
451 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname);
454 452
455 // build dims 453 // build dims
456 LLValue* dimsArg = DtoAlloca(DtoSize_t(), DtoConstUint(ndims), ".newdims"); 454 LLValue* dimsArg = DtoAlloca(DtoSize_t(), DtoConstUint(ndims), ".newdims");
457 LLValue* firstDim = NULL; 455 LLValue* firstDim = NULL;
458 for (size_t i=0; i<ndims; ++i) 456 for (size_t i=0; i<ndims; ++i)
469 const LLType* dstType = DtoType(arrayType)->getContainedType(1); 467 const LLType* dstType = DtoType(arrayType)->getContainedType(1);
470 if (newptr->getType() != dstType) 468 if (newptr->getType() != dstType)
471 newptr = DtoBitCast(newptr, dstType, ".gc_mem"); 469 newptr = DtoBitCast(newptr, dstType, ".gc_mem");
472 470
473 Logger::cout() << "final ptr = " << *newptr << '\n'; 471 Logger::cout() << "final ptr = " << *newptr << '\n';
474
475 #if 0
476 // using this would reinit all arrays beyond the first level to []
477 if (defaultInit) {
478 DValue* e = dty->defaultInit()->toElem(gIR);
479 DtoArrayInit(newptr,dim,e->getRVal());
480 }
481 #endif
482 472
483 assert(firstDim); 473 assert(firstDim);
484 return new DSliceValue(arrayType, firstDim, newptr); 474 return new DSliceValue(arrayType, firstDim, newptr);
485 } 475 }
486 476