comparison gen/arrays.c @ 58:2c3cd3596187 trunk

[svn r62] Added support for TypeInfo _Array, _Function, _Pointer, _Delegate, _Enum Added initial support for CatExp aka 'a ~ b' Fixed global constant static arrays initialized with string literals Fixed casting any dynamic array to void* Fixed new expression with temporary storage Fixed alias declarations in function scope Fixed relational comparisons of pointers
author lindquist
date Thu, 25 Oct 2007 09:02:55 +0200
parents 28e99b04a132
children b688ad419f8c
comparison
equal deleted inserted replaced
57:a9d29e9f1fed 58:2c3cd3596187
190 assert(0); 190 assert(0);
191 } 191 }
192 192
193 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, funcname); 193 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, funcname);
194 assert(fn); 194 assert(fn);
195 Logger::cout() << "calling array init function: " << *fn <<'\n';
195 llvm::CallInst* call = new llvm::CallInst(fn, args.begin(), args.end(), "", gIR->scopebb()); 196 llvm::CallInst* call = new llvm::CallInst(fn, args.begin(), args.end(), "", gIR->scopebb());
196 call->setCallingConv(llvm::CallingConv::C); 197 call->setCallingConv(llvm::CallingConv::C);
197 } 198 }
198 199
199 ////////////////////////////////////////////////////////////////////////////////////////// 200 //////////////////////////////////////////////////////////////////////////////////////////
389 values.push_back(ptr); 390 values.push_back(ptr);
390 return llvm::ConstantStruct::get(type,values); 391 return llvm::ConstantStruct::get(type,values);
391 } 392 }
392 393
393 ////////////////////////////////////////////////////////////////////////////////////////// 394 //////////////////////////////////////////////////////////////////////////////////////////
394 void LLVM_DtoNewDynArray(llvm::Value* dst, llvm::Value* dim, Type* dty, bool doinit) 395 llvm::Value* LLVM_DtoNewDynArray(llvm::Value* dst, llvm::Value* dim, Type* dty, bool doinit)
395 { 396 {
396 const llvm::Type* ty = LLVM_DtoType(dty); 397 const llvm::Type* ty = LLVM_DtoType(dty);
398 assert(ty != llvm::Type::VoidTy);
397 size_t sz = gTargetData->getTypeSize(ty); 399 size_t sz = gTargetData->getTypeSize(ty);
398 llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), sz, false); 400 llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), sz, false);
399 llvm::Value* bytesize = llvm::BinaryOperator::createMul(n,dim,"tmp",gIR->scopebb()); 401 llvm::Value* bytesize = (sz == 1) ? dim : llvm::BinaryOperator::createMul(n,dim,"tmp",gIR->scopebb());
400 402
401 llvm::Value* nullptr = llvm::ConstantPointerNull::get(llvm::PointerType::get(ty)); 403 llvm::Value* nullptr = llvm::ConstantPointerNull::get(llvm::PointerType::get(ty));
402 404
403 llvm::Value* newptr = LLVM_DtoRealloc(nullptr, bytesize); 405 llvm::Value* newptr = LLVM_DtoRealloc(nullptr, bytesize);
404 406
405 if (doinit) { 407 if (doinit) {
406 elem* e = dty->defaultInit()->toElem(gIR); 408 elem* e = dty->defaultInit()->toElem(gIR);
407 LLVM_DtoArrayInit(newptr,dim,e->getValue()); 409 LLVM_DtoArrayInit(newptr,dim,e->getValue());
408 delete e; 410 delete e;
409 } 411 }
410 412
411 llvm::Value* lenptr = LLVM_DtoGEPi(dst,0,0,"tmp",gIR->scopebb()); 413 llvm::Value* lenptr = LLVM_DtoGEPi(dst,0,0,"tmp",gIR->scopebb());
412 new llvm::StoreInst(dim,lenptr,gIR->scopebb()); 414 new llvm::StoreInst(dim,lenptr,gIR->scopebb());
413 llvm::Value* ptrptr = LLVM_DtoGEPi(dst,0,1,"tmp",gIR->scopebb()); 415 llvm::Value* ptrptr = LLVM_DtoGEPi(dst,0,1,"tmp",gIR->scopebb());
414 new llvm::StoreInst(newptr,ptrptr,gIR->scopebb()); 416 new llvm::StoreInst(newptr,ptrptr,gIR->scopebb());
417
418 return newptr;
415 } 419 }
416 420
417 ////////////////////////////////////////////////////////////////////////////////////////// 421 //////////////////////////////////////////////////////////////////////////////////////////
418 void LLVM_DtoResizeDynArray(llvm::Value* arr, llvm::Value* sz) 422 void LLVM_DtoResizeDynArray(llvm::Value* arr, llvm::Value* sz)
419 { 423 {
420 llvm::Value* ptr = LLVM_DtoGEPi(arr, 0, 1, "tmp", gIR->scopebb()); 424 llvm::Value* ptr = LLVM_DtoGEPi(arr, 0, 1, "tmp", gIR->scopebb());
421 llvm::Value* ptrld = new llvm::LoadInst(ptr,"tmp",gIR->scopebb()); 425 llvm::Value* ptrld = new llvm::LoadInst(ptr,"tmp",gIR->scopebb());
422 426
423 size_t isz = gTargetData->getTypeSize(ptrld->getType()->getContainedType(0)); 427 size_t isz = gTargetData->getTypeSize(ptrld->getType()->getContainedType(0));
424 llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), isz, false); 428 llvm::ConstantInt* n = llvm::ConstantInt::get(LLVM_DtoSize_t(), isz, false);
425 llvm::Value* bytesz = llvm::BinaryOperator::createMul(n,sz,"tmp",gIR->scopebb()); 429 llvm::Value* bytesz = (isz == 1) ? sz : llvm::BinaryOperator::createMul(n,sz,"tmp",gIR->scopebb());
426 430
427 llvm::Value* newptr = LLVM_DtoRealloc(ptrld, bytesz); 431 llvm::Value* newptr = LLVM_DtoRealloc(ptrld, bytesz);
428 new llvm::StoreInst(newptr,ptr,gIR->scopebb()); 432 new llvm::StoreInst(newptr,ptr,gIR->scopebb());
429 433
430 llvm::Value* len = LLVM_DtoGEPi(arr, 0, 0, "tmp", gIR->scopebb()); 434 llvm::Value* len = LLVM_DtoGEPi(arr, 0, 0, "tmp", gIR->scopebb());
431 new llvm::StoreInst(sz,len,gIR->scopebb()); 435 new llvm::StoreInst(sz,len,gIR->scopebb());
432 } 436 }
433 437
434 ////////////////////////////////////////////////////////////////////////////////////////// 438 //////////////////////////////////////////////////////////////////////////////////////////
435 void LLVM_DtoCatArrayElement(llvm::Value* arr, Expression* exp) 439 void LLVM_DtoCatAssignElement(llvm::Value* arr, Expression* exp)
436 { 440 {
437 llvm::Value* ptr = LLVM_DtoGEPi(arr, 0, 0, "tmp", gIR->scopebb()); 441 llvm::Value* ptr = LLVM_DtoGEPi(arr, 0, 0, "tmp", gIR->scopebb());
438 llvm::Value* idx = new llvm::LoadInst(ptr, "tmp", gIR->scopebb()); 442 llvm::Value* idx = new llvm::LoadInst(ptr, "tmp", gIR->scopebb());
439 llvm::Value* one = llvm::ConstantInt::get(idx->getType(),1,false); 443 llvm::Value* one = llvm::ConstantInt::get(idx->getType(),1,false);
440 llvm::Value* len = llvm::BinaryOperator::createAdd(idx, one, "tmp", gIR->scopebb()); 444 llvm::Value* len = llvm::BinaryOperator::createAdd(idx, one, "tmp", gIR->scopebb());
446 450
447 elem* e = exp->toElem(gIR); 451 elem* e = exp->toElem(gIR);
448 Type* et = LLVM_DtoDType(exp->type); 452 Type* et = LLVM_DtoDType(exp->type);
449 LLVM_DtoAssign(et, ptr, e->getValue()); 453 LLVM_DtoAssign(et, ptr, e->getValue());
450 delete e; 454 delete e;
455 }
456
457 //////////////////////////////////////////////////////////////////////////////////////////
458 void LLVM_DtoCatArrays(llvm::Value* arr, Expression* exp1, Expression* exp2)
459 {
460 Type* t1 = LLVM_DtoDType(exp1->type);
461 Type* t2 = LLVM_DtoDType(exp2->type);
462
463 assert(t1->ty == Tarray);
464 assert(t1->ty == t2->ty);
465
466 elem* e1 = exp1->toElem(gIR);
467 llvm::Value* a = e1->getValue();
468 delete e1;
469
470 elem* e2 = exp2->toElem(gIR);
471 llvm::Value* b = e2->getValue();
472 delete e2;
473
474 llvm::Value *len1, *len2, *src1, *src2, *res;
475 len1 = gIR->ir->CreateLoad(LLVM_DtoGEPi(a,0,0,"tmp"),"tmp");
476 len2 = gIR->ir->CreateLoad(LLVM_DtoGEPi(b,0,0,"tmp"),"tmp");
477 res = gIR->ir->CreateAdd(len1,len2,"tmp");
478
479 llvm::Value* mem = LLVM_DtoNewDynArray(arr, res, LLVM_DtoDType(t1->next), false);
480
481 src1 = gIR->ir->CreateLoad(LLVM_DtoGEPi(a,0,1,"tmp"),"tmp");
482 src2 = gIR->ir->CreateLoad(LLVM_DtoGEPi(b,0,1,"tmp"),"tmp");
483
484 LLVM_DtoMemCpy(mem,src1,len1);
485 mem = gIR->ir->CreateGEP(mem,len1,"tmp");
486 LLVM_DtoMemCpy(mem,src2,len2);
451 } 487 }
452 488
453 ////////////////////////////////////////////////////////////////////////////////////////// 489 //////////////////////////////////////////////////////////////////////////////////////////
454 llvm::Value* LLVM_DtoStaticArrayCompare(TOK op, llvm::Value* l, llvm::Value* r) 490 llvm::Value* LLVM_DtoStaticArrayCompare(TOK op, llvm::Value* l, llvm::Value* r)
455 { 491 {
567 llvm::Value* lp = gIR->ir->CreateLoad(LLVM_DtoGEPi(l, 0,1, "tmp"),"tmp"); 603 llvm::Value* lp = gIR->ir->CreateLoad(LLVM_DtoGEPi(l, 0,1, "tmp"),"tmp");
568 llvm::Value* rp = gIR->ir->CreateLoad(LLVM_DtoGEPi(r, 0,1, "tmp"),"tmp"); 604 llvm::Value* rp = gIR->ir->CreateLoad(LLVM_DtoGEPi(r, 0,1, "tmp"),"tmp");
569 llvm::Value* b2 = gIR->ir->CreateICmp(pred,lp,rp,"tmp"); 605 llvm::Value* b2 = gIR->ir->CreateICmp(pred,lp,rp,"tmp");
570 606
571 llvm::Value* b = gIR->ir->CreateAnd(b1,b2,"tmp"); 607 llvm::Value* b = gIR->ir->CreateAnd(b1,b2,"tmp");
572 if (op == TOKnotidentity) 608 return b;
573 return gIR->ir->CreateNot(b,"tmp"); 609 }
574 else
575 return b;
576 }