changeset 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 961e249eb2aa
children ef7fe998f9b3
files gen/arrays.cpp gen/classes.cpp gen/complex.cpp gen/llvmhelpers.cpp gen/llvmhelpers.h gen/statements.cpp gen/toir.cpp
diffstat 7 files changed, 50 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/gen/arrays.cpp	Sat Nov 22 13:41:36 2008 +0100
+++ b/gen/arrays.cpp	Sat Nov 22 18:35:52 2008 +0100
@@ -945,6 +945,12 @@
             Logger::cout() << "to sarray" << '\n';
         assert(0);
     }
+    else if (totype->ty == Tbool) {
+        // return (arr.ptr !is null)
+        LLValue* ptr = DtoArrayPtr(u);
+        LLConstant* nul = getNullPtr(ptr->getType());
+        rval = gIR->ir->CreateICmpNE(ptr, nul, "tmp");
+    }
     else {
         assert(0);
     }
--- a/gen/classes.cpp	Sat Nov 22 13:41:36 2008 +0100
+++ b/gen/classes.cpp	Sat Nov 22 18:35:52 2008 +0100
@@ -924,10 +924,17 @@
 
     Type* to = _to->toBasetype();
     if (to->ty == Tpointer) {
+        Logger::println("to pointer");
         const LLType* tolltype = DtoType(_to);
         LLValue* rval = DtoBitCast(val->getRVal(), tolltype);
         return new DImValue(_to, rval);
     }
+    else if (to->ty == Tbool) {
+        Logger::println("to bool");
+        LLValue* llval = val->getRVal();
+        LLValue* zero = LLConstant::getNullValue(llval->getType());
+        return new DImValue(_to, gIR->ir->CreateICmpNE(llval, zero, "tmp"));
+    }
 
     assert(to->ty == Tclass);
     TypeClass* tc = (TypeClass*)to;
@@ -947,7 +954,7 @@
         }
     }
     else {
-        Logger::println("to object");
+        Logger::println("to class");
         int poffset;
         if (fc->sym->isInterfaceDeclaration()) {
             Logger::println("interface cast");
--- a/gen/complex.cpp	Sat Nov 22 13:41:36 2008 +0100
+++ b/gen/complex.cpp	Sat Nov 22 18:35:52 2008 +0100
@@ -454,6 +454,9 @@
         DImValue* re = new DImValue(to, repart);
         return DtoCastFloat(loc, re, to);
     }
+    else if (to->ty == Tbool) {
+        return new DImValue(_to, DtoComplexEquals(loc, TOKnotequal, val, DtoNullValue(vty)));
+    }
     else
     assert(0);
 }
--- a/gen/llvmhelpers.cpp	Sat Nov 22 13:41:36 2008 +0100
+++ b/gen/llvmhelpers.cpp	Sat Nov 22 18:35:52 2008 +0100
@@ -569,7 +569,11 @@
         return new DImValue(_to, rval);
     }
 
-    if (to->isintegral()) {
+    if (to->ty == Tbool) {
+        LLValue* zero = LLConstantInt::get(rval->getType(), 0, false);
+        rval = gIR->ir->CreateICmpNE(rval, zero, "tmp");
+    }
+    else if (to->isintegral()) {
         if (fromsz < tosz || from->ty == Tbool) {
             if (Logger::enabled())
                 Logger::cout() << "cast to: " << *tolltype << '\n';
@@ -579,7 +583,7 @@
                 rval = new llvm::SExtInst(rval, tolltype, "tmp", gIR->scopebb());
             }
         }
-        else if (fromsz > tosz || to->ty == Tbool) {
+        else if (fromsz > tosz) {
             rval = new llvm::TruncInst(rval, tolltype, "tmp", gIR->scopebb());
         }
         else {
@@ -626,6 +630,11 @@
             Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n';
         rval = DtoBitCast(src, tolltype);
     }
+    else if (totype->ty == Tbool) {
+        LLValue* src = val->getRVal();
+        LLValue* zero = LLConstant::getNullValue(src->getType());
+        rval = gIR->ir->CreateICmpNE(src, zero, "tmp");
+    }
     else if (totype->isintegral()) {
         rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
     }
@@ -653,7 +662,12 @@
 
     LLValue* rval;
 
-    if (totype->iscomplex()) {
+    if (totype->ty == Tbool) {
+        rval = val->getRVal();
+        LLValue* zero = LLConstant::getNullValue(rval->getType());
+        rval = gIR->ir->CreateFCmpONE(rval, zero, "tmp");
+    }
+    else if (totype->iscomplex()) {
         return DtoComplex(loc, to, val);
     }
     else if (totype->isfloating()) {
@@ -694,6 +708,10 @@
     {
         return DtoPaintType(loc, val, to);
     }
+    else if (to->toBasetype()->ty == Tbool)
+    {
+        return new DImValue(to, DtoDelegateEquals(TOKnotequal, val->getRVal(), NULL));
+    }
     else
     {
         error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars());
@@ -1614,67 +1632,6 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
-LLValue* DtoBoolean(Loc& loc, DValue* dval)
-{
-    Type* dtype = dval->getType()->toBasetype();
-    TY ty = dtype->ty;
-
-    // integer
-    if (dtype->isintegral())
-    {
-        LLValue* val = dval->getRVal();
-        if (val->getType() == LLType::Int1Ty)
-            return val;
-        else {
-            LLValue* zero = LLConstantInt::get(val->getType(), 0, false);
-            return gIR->ir->CreateICmpNE(val, zero, "tmp");
-        }
-    }
-    // complex
-    else if (dtype->iscomplex())
-    {
-        return DtoComplexEquals(loc, TOKnotequal, dval, DtoNullValue(dtype));
-    }
-    // floating point
-    else if (dtype->isfloating())
-    {
-        LLValue* val = dval->getRVal();
-        LLValue* zero = LLConstant::getNullValue(val->getType());
-        return gIR->ir->CreateFCmpONE(val, zero, "tmp");
-    }
-    // pointer/class
-    else if (ty == Tpointer || ty == Tclass) {
-        LLValue* val = dval->getRVal();
-        LLValue* zero = LLConstant::getNullValue(val->getType());
-        if (Logger::enabled())
-        {
-            Logger::cout() << "val:  " << *val << '\n';
-            Logger::cout() << "zero: " << *zero << '\n';
-        }
-        return gIR->ir->CreateICmpNE(val, zero, "tmp");
-    }
-    // dynamic array
-    else if (ty == Tarray)
-    {
-        // return (arr.ptr !is null)
-        LLValue* ptr = DtoArrayPtr(dval);
-        LLConstant* nul = getNullPtr(ptr->getType());
-        return gIR->ir->CreateICmpNE(ptr, nul, "tmp");
-    }
-    // delegate
-    else if (ty == Tdelegate)
-    {
-        // return (dg !is null)
-        return DtoDelegateEquals(TOKnotequal, dval->getRVal(), NULL);
-    }
-    // unknown
-    std::cout << "unsupported -> bool : " << dtype->toChars() << '\n';
-    assert(0);
-    return 0;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////
-
 void DtoOverloadedIntrinsicName(TemplateInstance* ti, TemplateDeclaration* td, std::string& name)
 {
     Logger::println("DtoOverloadedIntrinsicName");
--- a/gen/llvmhelpers.h	Sat Nov 22 13:41:36 2008 +0100
+++ b/gen/llvmhelpers.h	Sat Nov 22 18:35:52 2008 +0100
@@ -108,9 +108,6 @@
 // target stuff
 void findDefaultTarget();
 
-/// Converts any value to a boolean (llvm i1)
-LLValue* DtoBoolean(Loc& loc, DValue* dval);
-
 /// get the default initializer of the type
 LLConstant* DtoDefaultInit(Type* t);
 
--- a/gen/statements.cpp	Sat Nov 22 13:41:36 2008 +0100
+++ b/gen/statements.cpp	Sat Nov 22 18:35:52 2008 +0100
@@ -168,7 +168,7 @@
     if (cond_val->getType() != LLType::Int1Ty) {
         if (Logger::enabled())
             Logger::cout() << "if conditional: " << *cond_val << '\n';
-        cond_val = DtoBoolean(loc, cond_e);
+        cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal();
     }
     LLValue* ifgoback = llvm::BranchInst::Create(ifbb, elsebb, cond_val, gIR->scopebb());
 
@@ -256,7 +256,7 @@
 
     // create the condition
     DValue* cond_e = condition->toElem(p);
-    LLValue* cond_val = DtoBoolean(loc, cond_e);
+    LLValue* cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal();
     delete cond_e;
 
     // conditional branch
@@ -312,7 +312,7 @@
 
     // create the condition
     DValue* cond_e = condition->toElem(p);
-    LLValue* cond_val = DtoBoolean(loc, cond_e);
+    LLValue* cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal();
     delete cond_e;
 
     // conditional branch
@@ -357,7 +357,7 @@
     if (condition)
     {
         DValue* cond_e = condition->toElem(p);
-        cond_val = DtoBoolean(loc, cond_e);
+        cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal();
         delete cond_e;
     }
     else
--- a/gen/toir.cpp	Sat Nov 22 13:41:36 2008 +0100
+++ b/gen/toir.cpp	Sat Nov 22 18:35:52 2008 +0100
@@ -1649,7 +1649,7 @@
         llvm::BasicBlock* endbb = llvm::BasicBlock::Create("noassert", p->topfunc(), oldend);
 
         // test condition
-        LLValue* condval = DtoBoolean(loc, cond);
+        LLValue* condval = DtoCast(loc, cond, Type::tbool)->getRVal();
 
         // branch
         llvm::BranchInst::Create(endbb, assertbb, condval, p->scopebb());
@@ -1675,7 +1675,7 @@
 
     DValue* u = e1->toElem(p);
 
-    LLValue* b = DtoBoolean(loc, u);
+    LLValue* b = DtoCast(loc, u, Type::tbool)->getRVal();
 
     LLConstant* zero = DtoConstBool(false);
     b = p->ir->CreateICmpEQ(b,zero);
@@ -1701,14 +1701,14 @@
     llvm::BasicBlock* andand = llvm::BasicBlock::Create("andand", gIR->topfunc(), oldend);
     llvm::BasicBlock* andandend = llvm::BasicBlock::Create("andandend", gIR->topfunc(), oldend);
 
-    LLValue* ubool = DtoBoolean(loc, u);
+    LLValue* ubool = DtoCast(loc, u, Type::tbool)->getRVal();
     DtoStore(ubool,resval);
     llvm::BranchInst::Create(andand,andandend,ubool,p->scopebb());
 
     p->scope() = IRScope(andand, andandend);
     DValue* v = e2->toElem(p);
 
-    LLValue* vbool = DtoBoolean(loc, v);
+    LLValue* vbool = DtoCast(loc, v, Type::tbool)->getRVal();
     LLValue* uandvbool = llvm::BinaryOperator::Create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb());
     DtoStore(uandvbool,resval);
     llvm::BranchInst::Create(andandend,p->scopebb());
@@ -1737,14 +1737,14 @@
     llvm::BasicBlock* oror = llvm::BasicBlock::Create("oror", gIR->topfunc(), oldend);
     llvm::BasicBlock* ororend = llvm::BasicBlock::Create("ororend", gIR->topfunc(), oldend);
 
-    LLValue* ubool = DtoBoolean(loc, u);
+    LLValue* ubool = DtoCast(loc, u, Type::tbool)->getRVal();
     DtoStore(ubool,resval);
     llvm::BranchInst::Create(ororend,oror,ubool,p->scopebb());
 
     p->scope() = IRScope(oror, ororend);
     DValue* v = e2->toElem(p);
 
-    LLValue* vbool = DtoBoolean(loc, v);
+    LLValue* vbool = DtoCast(loc, v, Type::tbool)->getRVal();
     DtoStore(vbool,resval);
     llvm::BranchInst::Create(ororend,p->scopebb());
 
@@ -1999,7 +1999,7 @@
     llvm::BasicBlock* condend = llvm::BasicBlock::Create("condend", gIR->topfunc(), oldend);
 
     DValue* c = econd->toElem(p);
-    LLValue* cond_val = DtoBoolean(loc, c);
+    LLValue* cond_val = DtoCast(loc, c, Type::tbool)->getRVal();
     llvm::BranchInst::Create(condtrue,condfalse,cond_val,p->scopebb());
 
     p->scope() = IRScope(condtrue, condfalse);
@@ -2420,7 +2420,7 @@
 
 DValue* BoolExp::toElem(IRState* p)
 {
-    return new DImValue(type, DtoBoolean(loc, e1->toElem(p)));
+    return new DImValue(type, DtoCast(loc, e1->toElem(p), Type::tbool)->getRVal());
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////