comparison gen/llvmhelpers.cpp @ 778:4adf0f742896

Get rid of DtoBoolean - use DtoCast(... Type::tbool) instead. Fixed some casts to bool that were using truncation.
author Christian Kamm <kamm incasoftware de>
date Sat, 22 Nov 2008 18:35:52 +0100
parents 5696a7167b21
children a45179dfaa43
comparison
equal deleted inserted replaced
777:961e249eb2aa 778:4adf0f742896
567 LLValue* rval = val->getRVal(); 567 LLValue* rval = val->getRVal();
568 if (rval->getType() == tolltype) { 568 if (rval->getType() == tolltype) {
569 return new DImValue(_to, rval); 569 return new DImValue(_to, rval);
570 } 570 }
571 571
572 if (to->isintegral()) { 572 if (to->ty == Tbool) {
573 LLValue* zero = LLConstantInt::get(rval->getType(), 0, false);
574 rval = gIR->ir->CreateICmpNE(rval, zero, "tmp");
575 }
576 else if (to->isintegral()) {
573 if (fromsz < tosz || from->ty == Tbool) { 577 if (fromsz < tosz || from->ty == Tbool) {
574 if (Logger::enabled()) 578 if (Logger::enabled())
575 Logger::cout() << "cast to: " << *tolltype << '\n'; 579 Logger::cout() << "cast to: " << *tolltype << '\n';
576 if (from->isunsigned() || from->ty == Tbool) { 580 if (from->isunsigned() || from->ty == Tbool) {
577 rval = new llvm::ZExtInst(rval, tolltype, "tmp", gIR->scopebb()); 581 rval = new llvm::ZExtInst(rval, tolltype, "tmp", gIR->scopebb());
578 } else { 582 } else {
579 rval = new llvm::SExtInst(rval, tolltype, "tmp", gIR->scopebb()); 583 rval = new llvm::SExtInst(rval, tolltype, "tmp", gIR->scopebb());
580 } 584 }
581 } 585 }
582 else if (fromsz > tosz || to->ty == Tbool) { 586 else if (fromsz > tosz) {
583 rval = new llvm::TruncInst(rval, tolltype, "tmp", gIR->scopebb()); 587 rval = new llvm::TruncInst(rval, tolltype, "tmp", gIR->scopebb());
584 } 588 }
585 else { 589 else {
586 rval = DtoBitCast(rval, tolltype); 590 rval = DtoBitCast(rval, tolltype);
587 } 591 }
624 LLValue* src = val->getRVal(); 628 LLValue* src = val->getRVal();
625 if (Logger::enabled()) 629 if (Logger::enabled())
626 Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n'; 630 Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n';
627 rval = DtoBitCast(src, tolltype); 631 rval = DtoBitCast(src, tolltype);
628 } 632 }
633 else if (totype->ty == Tbool) {
634 LLValue* src = val->getRVal();
635 LLValue* zero = LLConstant::getNullValue(src->getType());
636 rval = gIR->ir->CreateICmpNE(src, zero, "tmp");
637 }
629 else if (totype->isintegral()) { 638 else if (totype->isintegral()) {
630 rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); 639 rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
631 } 640 }
632 else { 641 else {
633 error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars()); 642 error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars());
651 size_t fromsz = fromtype->size(); 660 size_t fromsz = fromtype->size();
652 size_t tosz = totype->size(); 661 size_t tosz = totype->size();
653 662
654 LLValue* rval; 663 LLValue* rval;
655 664
656 if (totype->iscomplex()) { 665 if (totype->ty == Tbool) {
666 rval = val->getRVal();
667 LLValue* zero = LLConstant::getNullValue(rval->getType());
668 rval = gIR->ir->CreateFCmpONE(rval, zero, "tmp");
669 }
670 else if (totype->iscomplex()) {
657 return DtoComplex(loc, to, val); 671 return DtoComplex(loc, to, val);
658 } 672 }
659 else if (totype->isfloating()) { 673 else if (totype->isfloating()) {
660 if (fromsz == tosz) { 674 if (fromsz == tosz) {
661 rval = val->getRVal(); 675 rval = val->getRVal();
691 DValue* DtoCastDelegate(Loc& loc, DValue* val, Type* to) 705 DValue* DtoCastDelegate(Loc& loc, DValue* val, Type* to)
692 { 706 {
693 if (to->toBasetype()->ty == Tdelegate) 707 if (to->toBasetype()->ty == Tdelegate)
694 { 708 {
695 return DtoPaintType(loc, val, to); 709 return DtoPaintType(loc, val, to);
710 }
711 else if (to->toBasetype()->ty == Tbool)
712 {
713 return new DImValue(to, DtoDelegateEquals(TOKnotequal, val->getRVal(), NULL));
696 } 714 }
697 else 715 else
698 { 716 {
699 error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars()); 717 error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars());
700 fatal(); 718 fatal();
1612 return c; 1630 return c;
1613 } 1631 }
1614 1632
1615 ////////////////////////////////////////////////////////////////////////////////////////// 1633 //////////////////////////////////////////////////////////////////////////////////////////
1616 1634
1617 LLValue* DtoBoolean(Loc& loc, DValue* dval)
1618 {
1619 Type* dtype = dval->getType()->toBasetype();
1620 TY ty = dtype->ty;
1621
1622 // integer
1623 if (dtype->isintegral())
1624 {
1625 LLValue* val = dval->getRVal();
1626 if (val->getType() == LLType::Int1Ty)
1627 return val;
1628 else {
1629 LLValue* zero = LLConstantInt::get(val->getType(), 0, false);
1630 return gIR->ir->CreateICmpNE(val, zero, "tmp");
1631 }
1632 }
1633 // complex
1634 else if (dtype->iscomplex())
1635 {
1636 return DtoComplexEquals(loc, TOKnotequal, dval, DtoNullValue(dtype));
1637 }
1638 // floating point
1639 else if (dtype->isfloating())
1640 {
1641 LLValue* val = dval->getRVal();
1642 LLValue* zero = LLConstant::getNullValue(val->getType());
1643 return gIR->ir->CreateFCmpONE(val, zero, "tmp");
1644 }
1645 // pointer/class
1646 else if (ty == Tpointer || ty == Tclass) {
1647 LLValue* val = dval->getRVal();
1648 LLValue* zero = LLConstant::getNullValue(val->getType());
1649 if (Logger::enabled())
1650 {
1651 Logger::cout() << "val: " << *val << '\n';
1652 Logger::cout() << "zero: " << *zero << '\n';
1653 }
1654 return gIR->ir->CreateICmpNE(val, zero, "tmp");
1655 }
1656 // dynamic array
1657 else if (ty == Tarray)
1658 {
1659 // return (arr.ptr !is null)
1660 LLValue* ptr = DtoArrayPtr(dval);
1661 LLConstant* nul = getNullPtr(ptr->getType());
1662 return gIR->ir->CreateICmpNE(ptr, nul, "tmp");
1663 }
1664 // delegate
1665 else if (ty == Tdelegate)
1666 {
1667 // return (dg !is null)
1668 return DtoDelegateEquals(TOKnotequal, dval->getRVal(), NULL);
1669 }
1670 // unknown
1671 std::cout << "unsupported -> bool : " << dtype->toChars() << '\n';
1672 assert(0);
1673 return 0;
1674 }
1675
1676 //////////////////////////////////////////////////////////////////////////////////////////
1677
1678 void DtoOverloadedIntrinsicName(TemplateInstance* ti, TemplateDeclaration* td, std::string& name) 1635 void DtoOverloadedIntrinsicName(TemplateInstance* ti, TemplateDeclaration* td, std::string& name)
1679 { 1636 {
1680 Logger::println("DtoOverloadedIntrinsicName"); 1637 Logger::println("DtoOverloadedIntrinsicName");
1681 LOG_SCOPE; 1638 LOG_SCOPE;
1682 1639