comparison gen/arrays.cpp @ 99:a676a7743642 trunk

[svn r103] Array comparisons are now fully implemented, that is - to the extent that TypeInfo is.
author lindquist
date Thu, 15 Nov 2007 00:24:44 +0100
parents 6789050b5ad1
children 5071469303d4
comparison
equal deleted inserted replaced
98:6789050b5ad1 99:a676a7743642
544 mem = gIR->ir->CreateGEP(mem,len1,"tmp"); 544 mem = gIR->ir->CreateGEP(mem,len1,"tmp");
545 DtoMemCpy(mem,src2,len2); 545 DtoMemCpy(mem,src2,len2);
546 } 546 }
547 547
548 ////////////////////////////////////////////////////////////////////////////////////////// 548 //////////////////////////////////////////////////////////////////////////////////////////
549 549 // helper for eq and cmp
550 llvm::Value* DtoArrayEquals(TOK op, DValue* l, DValue* r) 550 static llvm::Value* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r)
551 { 551 {
552 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_adEq"); 552 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, func);
553 assert(fn); 553 assert(fn);
554 554
555 llvm::Value* lmem; 555 llvm::Value* lmem;
556 llvm::Value* rmem; 556 llvm::Value* rmem;
557 557
558 // cast static arrays to dynamic ones, this turns them into DSliceValues 558 // cast static arrays to dynamic ones, this turns them into DSliceValues
559 Type* l_ty = DtoDType(l->getType()); 559 Type* l_ty = DtoDType(l->getType());
560 Type* r_ty = DtoDType(r->getType()); 560 Type* r_ty = DtoDType(r->getType());
561 assert(l_ty->next == r_ty->next); 561 assert(l_ty->next == r_ty->next);
562 Type* a_ty = new Type(Tarray, l_ty->next); 562 if ((l_ty->ty == Tsarray) || (r_ty->ty == Tsarray)) {
563 if (l_ty->ty == Tsarray) 563 Type* a_ty = new Type(Tarray, l_ty->next);
564 l = DtoCastArray(l, a_ty); 564 if (l_ty->ty == Tsarray)
565 if (r_ty->ty == Tsarray) 565 l = DtoCastArray(l, a_ty);
566 r = DtoCastArray(r, a_ty); 566 if (r_ty->ty == Tsarray)
567 r = DtoCastArray(r, a_ty);
568 }
567 569
568 // we need to give slices storage 570 // we need to give slices storage
569 if (l->isSlice()) { 571 if (l->isSlice()) {
570 lmem = new llvm::AllocaInst(DtoType(l->getType()), "tmpparam", gIR->topallocapoint()); 572 lmem = new llvm::AllocaInst(DtoType(l->getType()), "tmpparam", gIR->topallocapoint());
571 DtoSetArray(lmem, DtoArrayLen(l), DtoArrayPtr(l)); 573 DtoSetArray(lmem, DtoArrayLen(l), DtoArrayPtr(l));
593 Logger::cout() << "typeinfo decl: " << *ti->llvmValue << '\n'; 595 Logger::cout() << "typeinfo decl: " << *ti->llvmValue << '\n';
594 596
595 pt = fn->getFunctionType()->getParamType(2); 597 pt = fn->getFunctionType()->getParamType(2);
596 args.push_back(DtoBitCast(ti->llvmValue, pt)); 598 args.push_back(DtoBitCast(ti->llvmValue, pt));
597 599
598 llvm::Value* res = gIR->ir->CreateCall(fn, args.begin(), args.end(), "tmp"); 600 return gIR->ir->CreateCall(fn, args.begin(), args.end(), "tmp");
601 }
602
603 //////////////////////////////////////////////////////////////////////////////////////////
604 llvm::Value* DtoArrayEquals(TOK op, DValue* l, DValue* r)
605 {
606 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_adEq");
607 assert(fn);
608
609 llvm::Value* res = DtoArrayEqCmp_impl("_adEq", l, r);
599 if (op == TOKnotequal) 610 if (op == TOKnotequal)
600 res = gIR->ir->CreateNot(res, "tmp"); 611 res = gIR->ir->CreateNot(res, "tmp");
601 612
613 return res;
614 }
615
616 //////////////////////////////////////////////////////////////////////////////////////////
617 llvm::Value* DtoArrayCompare(TOK op, DValue* l, DValue* r)
618 {
619 llvm::Value* res = 0;
620
621 llvm::ICmpInst::Predicate cmpop;
622 bool skip = false;
623
624 switch(op)
625 {
626 case TOKlt:
627 case TOKul:
628 cmpop = llvm::ICmpInst::ICMP_SLT;
629 break;
630 case TOKle:
631 case TOKule:
632 cmpop = llvm::ICmpInst::ICMP_SLE;
633 break;
634 case TOKgt:
635 case TOKug:
636 cmpop = llvm::ICmpInst::ICMP_SGT;
637 break;
638 case TOKge:
639 case TOKuge:
640 cmpop = llvm::ICmpInst::ICMP_SGE;
641 break;
642 case TOKue:
643 cmpop = llvm::ICmpInst::ICMP_EQ;
644 break;
645 case TOKlg:
646 cmpop = llvm::ICmpInst::ICMP_NE;
647 break;
648 case TOKleg:
649 skip = true;
650 res = llvm::ConstantInt::getTrue();
651 break;
652 case TOKunord:
653 skip = true;
654 res = llvm::ConstantInt::getFalse();
655 break;
656
657 default:
658 assert(0);
659 }
660
661 if (!skip)
662 {
663 res = DtoArrayEqCmp_impl("_adCmp", l, r);
664 res = new llvm::ICmpInst(cmpop, res, DtoConstInt(0), "tmp", gIR->scopebb());
665 }
666
667 assert(res);
602 return res; 668 return res;
603 } 669 }
604 670
605 ////////////////////////////////////////////////////////////////////////////////////////// 671 //////////////////////////////////////////////////////////////////////////////////////////
606 llvm::Value* DtoArrayCastLength(llvm::Value* len, const llvm::Type* elemty, const llvm::Type* newelemty) 672 llvm::Value* DtoArrayCastLength(llvm::Value* len, const llvm::Type* elemty, const llvm::Type* newelemty)