# HG changeset patch # User lindquist # Date 1195067881 -3600 # Node ID c4e161556a21d000753987b7e1559fdb2b405d90 # Parent ce7ed8f59b992a79269f9cc4125d3dd96ea35ae7 [svn r101] Split up CastExp into several smaller utility functions. diff -r ce7ed8f59b99 -r c4e161556a21 gen/toir.cpp --- a/gen/toir.cpp Mon Nov 12 07:58:44 2007 +0100 +++ b/gen/toir.cpp Wed Nov 14 20:18:01 2007 +0100 @@ -95,7 +95,7 @@ // unsupported declaration else { - error("Only Var/Struct-Declaration is supported for DeclarationExp"); + error("Unimplemented DeclarationExp type"); assert(0); } return 0; @@ -1341,175 +1341,20 @@ LOG_SCOPE; DValue* u = e1->toElem(p); - - const llvm::Type* tolltype = DtoType(to); - Type* fromtype = DtoDType(e1->type); - Type* totype = DtoDType(to); - int lsz = fromtype->size(); - int rsz = totype->size(); - - // this makes sure the strange lvalue casts don't screw things up - llvm::Value* rval = 0; - llvm::Value* rval2 = 0; - bool isslice = false; - - if (fromtype->isintegral()) { - if (totype->isintegral()) { - if (lsz < rsz) { - Logger::cout() << "cast to: " << *tolltype << '\n'; - if (fromtype->isunsigned() || fromtype->ty == Tbool) { - rval = new llvm::ZExtInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } else { - rval = new llvm::SExtInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - } - else if (lsz > rsz) { - rval = new llvm::TruncInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - else { - rval = new llvm::BitCastInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - } - else if (totype->isfloating()) { - if (fromtype->isunsigned()) { - rval = new llvm::UIToFPInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - else { - rval = new llvm::SIToFPInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - } - else if (totype->ty == Tpointer) { - rval = p->ir->CreateIntToPtr(u->getRVal(), tolltype, "tmp"); - } - else { - assert(0); - } - } - else if (fromtype->isfloating()) { - if (totype->isfloating()) { - if ((fromtype->ty == Tfloat80 || fromtype->ty == Tfloat64) && (totype->ty == Tfloat80 || totype->ty == Tfloat64)) { - rval = u->getRVal(); - } - else if (lsz < rsz) { - rval = new llvm::FPExtInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - else if (lsz > rsz) { - rval = new llvm::FPTruncInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - else { - assert(0); - } - } - else if (totype->isintegral()) { - if (totype->isunsigned()) { - rval = new llvm::FPToUIInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - else { - rval = new llvm::FPToSIInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - } - else { - assert(0); - } - } - else if (fromtype->ty == Tclass) { - //assert(to->ty == Tclass); - rval = new llvm::BitCastInst(u->getRVal(), tolltype, "tmp", p->scopebb()); + DValue* v = DtoCast(u, to); + + if (v->isSlice()) + return v; + else if (u->isLValueCast() || (u->isVar() && u->isVar()->lval)) + return new DLValueCast(to, u->getLVal(), v->getRVal()); + else if (gIR->topexp() && gIR->topexp()->e1 == this) { + llvm::Value* lval = u->getLVal(); + llvm::Value* rval = v->getRVal(); + Logger::cout() << "lval: " << *lval << "rval: " << *rval << '\n'; + return new DLValueCast(to, lval, rval); } - else if (fromtype->ty == Tarray || fromtype->ty == Tsarray) { - Logger::cout() << "from array or sarray" << '\n'; - if (totype->ty == Tpointer) { - Logger::cout() << "to pointer" << '\n'; - assert(fromtype->next == totype->next || totype->next->ty == Tvoid); - llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); - llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); - llvm::Value* ptr = DtoGEP(u->getRVal(),zero,one,"tmp",p->scopebb()); - rval = new llvm::LoadInst(ptr, "tmp", p->scopebb()); - if (fromtype->next != totype->next) - rval = p->ir->CreateBitCast(rval, llvm::PointerType::get(llvm::Type::Int8Ty), "tmp"); - } - else if (totype->ty == Tarray) { - Logger::cout() << "to array" << '\n'; - const llvm::Type* ptrty = DtoType(totype->next); - if (ptrty == llvm::Type::VoidTy) - ptrty = llvm::Type::Int8Ty; - ptrty = llvm::PointerType::get(ptrty); - - const llvm::Type* ety = DtoType(fromtype->next); - if (ety == llvm::Type::VoidTy) - ety = llvm::Type::Int8Ty; - - if (DSliceValue* usl = u->isSlice()) { - rval = new llvm::BitCastInst(usl->ptr, ptrty, "tmp", p->scopebb()); - if (fromtype->next->size() == totype->next->size()) - rval2 = usl->len; - else - rval2 = DtoArrayCastLength(usl->len, ety, ptrty->getContainedType(0)); - } - else { - llvm::Value* uval = u->getRVal(); - if (fromtype->ty == Tsarray) { - Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; - assert(isaPointer(uval->getType())); - const llvm::ArrayType* arrty = isaArray(uval->getType()->getContainedType(0)); - rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false); - rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0)); - rval = new llvm::BitCastInst(uval, ptrty, "tmp", p->scopebb()); - } - else { - llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); - llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); - rval2 = DtoGEP(uval,zero,zero,"tmp",p->scopebb()); - rval2 = new llvm::LoadInst(rval2, "tmp", p->scopebb()); - rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0)); - - rval = DtoGEP(uval,zero,one,"tmp",p->scopebb()); - rval = new llvm::LoadInst(rval, "tmp", p->scopebb()); - //Logger::cout() << *e->mem->getType() << '|' << *ptrty << '\n'; - rval = new llvm::BitCastInst(rval, ptrty, "tmp", p->scopebb()); - } - } - isslice = true; - } - else if (totype->ty == Tsarray) { - Logger::cout() << "to sarray" << '\n'; - assert(0); - } - else { - assert(0); - } - } - else if (fromtype->ty == Tpointer) { - if (totype->ty == Tpointer || totype->ty == Tclass) { - llvm::Value* src = u->getRVal(); - Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n'; - rval = new llvm::BitCastInst(src, tolltype, "tmp", p->scopebb()); - } - else if (totype->isintegral()) { - rval = new llvm::PtrToIntInst(u->getRVal(), tolltype, "tmp", p->scopebb()); - } - else - assert(0); - } - else { - assert(0); - } - - if (isslice) { - return new DSliceValue(type, rval2, rval); - } - else if (u->isLValueCast() || (u->isVar() && u->isVar()->lval)) { - return new DLValueCast(type, u->getLVal(), rval); - } - else if (p->topexp() && p->topexp()->e1 == this) { - llvm::Value* lval = u->getLVal(); - Logger::cout() << "lval: " << *lval << "rval: " << *rval << '\n'; - return new DLValueCast(type, lval, rval); - } - else { - Logger::cout() << "im rval: " << *rval << '\n'; - return new DImValue(type, rval); - } + + return v; } ////////////////////////////////////////////////////////////////////////////////////////// diff -r ce7ed8f59b99 -r c4e161556a21 gen/tollvm.cpp --- a/gen/tollvm.cpp Mon Nov 12 07:58:44 2007 +0100 +++ b/gen/tollvm.cpp Wed Nov 14 20:18:01 2007 +0100 @@ -1305,6 +1305,234 @@ } ////////////////////////////////////////////////////////////////////////////////////////// +DValue* DtoCastInt(DValue* val, Type* _to) +{ + const llvm::Type* tolltype = DtoType(_to); + + Type* to = DtoDType(_to); + Type* from = DtoDType(val->getType()); + assert(from->isintegral()); + + size_t fromsz = from->size(); + size_t tosz = to->size(); + + llvm::Value* rval; + + if (to->isintegral()) { + if (fromsz < tosz) { + Logger::cout() << "cast to: " << *tolltype << '\n'; + if (from->isunsigned() || from->ty == Tbool) { + rval = new llvm::ZExtInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } else { + rval = new llvm::SExtInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + } + else if (fromsz > tosz) { + rval = new llvm::TruncInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + else { + rval = new llvm::BitCastInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + } + else if (to->isfloating()) { + if (from->isunsigned()) { + rval = new llvm::UIToFPInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + else { + rval = new llvm::SIToFPInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + } + else if (to->ty == Tpointer) { + rval = gIR->ir->CreateIntToPtr(val->getRVal(), tolltype, "tmp"); + } + else { + assert(0 && "bad int cast"); + } + + return new DImValue(_to, rval); +} + +DValue* DtoCastPtr(DValue* val, Type* to) +{ + const llvm::Type* tolltype = DtoType(to); + + Type* totype = DtoDType(to); + Type* fromtype = DtoDType(val->getType()); + assert(fromtype->ty == Tpointer); + + llvm::Value* rval; + + if (totype->ty == Tpointer || totype->ty == Tclass) { + llvm::Value* src = val->getRVal(); + Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n'; + rval = new llvm::BitCastInst(src, tolltype, "tmp", gIR->scopebb()); + } + else if (totype->isintegral()) { + rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + else { + assert(0); + } + + return new DImValue(to, rval); +} + +DValue* DtoCastFloat(DValue* val, Type* to) +{ + const llvm::Type* tolltype = DtoType(to); + + Type* totype = DtoDType(to); + Type* fromtype = DtoDType(val->getType()); + assert(fromtype->isfloating()); + + size_t fromsz = fromtype->size(); + size_t tosz = totype->size(); + + llvm::Value* rval; + + if (totype->isfloating()) { + if ((fromtype->ty == Tfloat80 || fromtype->ty == Tfloat64) && (totype->ty == Tfloat80 || totype->ty == Tfloat64)) { + rval = val->getRVal(); + } + else if (fromsz < tosz) { + rval = new llvm::FPExtInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + else if (fromsz > tosz) { + rval = new llvm::FPTruncInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + else { + assert(0 && "bad float cast"); + } + } + else if (totype->isintegral()) { + if (totype->isunsigned()) { + rval = new llvm::FPToUIInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + else { + rval = new llvm::FPToSIInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + } + } + else { + assert(0 && "bad float cast"); + } + + return new DImValue(to, rval); +} + +DValue* DtoCastClass(DValue* val, Type* _to) +{ + const llvm::Type* tolltype = DtoType(_to); + Type* to = DtoDType(_to); + assert(to->ty == Tclass); + llvm::Value* rval = new llvm::BitCastInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); + return new DImValue(_to, rval); +} + +DValue* DtoCastArray(DValue* u, Type* to) +{ + const llvm::Type* tolltype = DtoType(to); + + Type* totype = DtoDType(to); + Type* fromtype = DtoDType(u->getType()); + assert(fromtype->ty == Tarray || fromtype->ty == Tsarray); + + llvm::Value* rval; + llvm::Value* rval2; + bool isslice = false; + + Logger::cout() << "from array or sarray" << '\n'; + if (totype->ty == Tpointer) { + Logger::cout() << "to pointer" << '\n'; + assert(fromtype->next == totype->next || totype->next->ty == Tvoid); + llvm::Value* ptr = DtoGEPi(u->getRVal(),0,1,"tmp",gIR->scopebb()); + rval = new llvm::LoadInst(ptr, "tmp", gIR->scopebb()); + if (fromtype->next != totype->next) + rval = gIR->ir->CreateBitCast(rval, llvm::PointerType::get(llvm::Type::Int8Ty), "tmp"); + } + else if (totype->ty == Tarray) { + Logger::cout() << "to array" << '\n'; + const llvm::Type* ptrty = DtoType(totype->next); + if (ptrty == llvm::Type::VoidTy) + ptrty = llvm::Type::Int8Ty; + ptrty = llvm::PointerType::get(ptrty); + + const llvm::Type* ety = DtoType(fromtype->next); + if (ety == llvm::Type::VoidTy) + ety = llvm::Type::Int8Ty; + + if (DSliceValue* usl = u->isSlice()) { + Logger::println("from slice"); + rval = new llvm::BitCastInst(usl->ptr, ptrty, "tmp", gIR->scopebb()); + if (fromtype->next->size() == totype->next->size()) + rval2 = usl->len; + else + rval2 = DtoArrayCastLength(usl->len, ety, ptrty->getContainedType(0)); + } + else { + llvm::Value* uval = u->getRVal(); + if (fromtype->ty == Tsarray) { + Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; + assert(isaPointer(uval->getType())); + const llvm::ArrayType* arrty = isaArray(uval->getType()->getContainedType(0)); + rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false); + rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0)); + rval = new llvm::BitCastInst(uval, ptrty, "tmp", gIR->scopebb()); + } + else { + llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); + llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); + rval2 = DtoGEP(uval,zero,zero,"tmp",gIR->scopebb()); + rval2 = new llvm::LoadInst(rval2, "tmp", gIR->scopebb()); + rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0)); + + rval = DtoGEP(uval,zero,one,"tmp",gIR->scopebb()); + rval = new llvm::LoadInst(rval, "tmp", gIR->scopebb()); + //Logger::cout() << *e->mem->getType() << '|' << *ptrty << '\n'; + rval = new llvm::BitCastInst(rval, ptrty, "tmp", gIR->scopebb()); + } + } + isslice = true; + } + else if (totype->ty == Tsarray) { + Logger::cout() << "to sarray" << '\n'; + assert(0); + } + else { + assert(0); + } + + if (isslice) { + Logger::println("isslice"); + return new DSliceValue(to, rval2, rval); + } + + return new DImValue(to, rval); +} + +DValue* DtoCast(DValue* val, Type* to) +{ + Type* fromtype = DtoDType(val->getType()); + if (fromtype->isintegral()) { + return DtoCastInt(val, to); + } + else if (fromtype->isfloating()) { + return DtoCastFloat(val, to); + } + else if (fromtype->ty == Tclass) { + return DtoCastClass(val, to); + } + else if (fromtype->ty == Tarray || fromtype->ty == Tsarray) { + return DtoCastArray(val, to); + } + else if (fromtype->ty == Tpointer) { + return DtoCastPtr(val, to); + } + else { + assert(0); + } +} + +////////////////////////////////////////////////////////////////////////////////////////// llvm::ConstantInt* DtoConstSize_t(size_t i) { diff -r ce7ed8f59b99 -r c4e161556a21 gen/tollvm.h --- a/gen/tollvm.h Mon Nov 12 07:58:44 2007 +0100 +++ b/gen/tollvm.h Wed Nov 14 20:18:01 2007 +0100 @@ -89,6 +89,14 @@ // basic operations void DtoAssign(DValue* lhs, DValue* rhs); +// casts +DValue* DtoCastInt(DValue* val, Type* to); +DValue* DtoCastPtr(DValue* val, Type* to); +DValue* DtoCastFloat(DValue* val, Type* to); +DValue* DtoCastArray(DValue* val, Type* to); +DValue* DtoCastClass(DValue* val, Type* to); +DValue* DtoCast(DValue* val, Type* to); + // binary operations DValue* DtoBinAdd(DValue* lhs, DValue* rhs); DValue* DtoBinSub(DValue* lhs, DValue* rhs); diff -r ce7ed8f59b99 -r c4e161556a21 llvmdc.kdevelop --- a/llvmdc.kdevelop Mon Nov 12 07:58:44 2007 +0100 +++ b/llvmdc.kdevelop Wed Nov 14 20:18:01 2007 +0100 @@ -14,8 +14,8 @@ llvmdc . false - - + + @@ -156,7 +156,7 @@ .; - + set m_,_ theValue @@ -174,8 +174,8 @@ executable /home/tomas/kdevprojects/llvmdc - - + + /home/tomas/kdevprojects/llvmdc false false @@ -324,16 +324,86 @@ dmdorig/phobos/internal/deh.c dmdorig/phobos/internal/mars.h dmdorig/phobos/internal/monitor.c + obj/Debug + obj/Debug/access.d + obj/Debug/array.d + obj/Debug/arrays.d + obj/Debug/attrib.d + obj/Debug/binops.d + obj/Debug/cast.d + obj/Debug/class.d + obj/Debug/cond.d + obj/Debug/constfold.d + obj/Debug/dchar.d + obj/Debug/declaration.d + obj/Debug/delegatize.d + obj/Debug/doc.d + obj/Debug/dsymbol.d + obj/Debug/dump.d + obj/Debug/dvalue.d + obj/Debug/dwarftypes.d + obj/Debug/elem.d + obj/Debug/entity.d + obj/Debug/enum.d + obj/Debug/expression.d + obj/Debug/func.d + obj/Debug/gnuc.d + obj/Debug/hdrgen.d + obj/Debug/html.d + obj/Debug/id.d + obj/Debug/identifier.d + obj/Debug/idgen.d + obj/Debug/impcnvgen.d + obj/Debug/impcnvtab.d + obj/Debug/import.d + obj/Debug/inifile.d + obj/Debug/init.d + obj/Debug/inline.d + obj/Debug/interpret.d + obj/Debug/irstate.d + obj/Debug/lexer.d + obj/Debug/link.d + obj/Debug/logger.d + obj/Debug/lstring.d + obj/Debug/macro.d + obj/Debug/mangle.d + obj/Debug/mars.d + obj/Debug/mem.d + obj/Debug/module.d + obj/Debug/mtype.d + obj/Debug/opover.d + obj/Debug/optimize.d + obj/Debug/parse.d + obj/Debug/root.d + obj/Debug/runtime.d + obj/Debug/scope.d + obj/Debug/statement.d + obj/Debug/statements.d + obj/Debug/staticassert.d + obj/Debug/stringtable.d + obj/Debug/struct.d + obj/Debug/structs.d + obj/Debug/template.d + obj/Debug/tocsym.d + obj/Debug/todebug.d + obj/Debug/todt.d + obj/Debug/toir.d + obj/Debug/tollvm.d + obj/Debug/toobj.d + obj/Debug/typinf.d + obj/Debug/unialpha.d + obj/Debug/utf.d + obj/Debug/version.d make - + 0 - - - + + + default @@ -344,9 +414,9 @@ 0 0 false - - - + + + default @@ -361,11 +431,11 @@ - - - - - + + + + + true false false diff -r ce7ed8f59b99 -r c4e161556a21 llvmdc.kdevelop.filelist --- a/llvmdc.kdevelop.filelist Mon Nov 12 07:58:44 2007 +0100 +++ b/llvmdc.kdevelop.filelist Wed Nov 14 20:18:01 2007 +0100 @@ -106,8 +106,6 @@ gen/dvalue.cpp gen/dvalue.h gen/dwarftypes.cpp -gen/elem.cpp -gen/elem.h gen/enums.h gen/irstate.cpp gen/irstate.h @@ -119,7 +117,6 @@ gen/statements.cpp gen/structs.cpp gen/structs.h -gen/symbol.h gen/tocsym.cpp gen/todebug.cpp gen/todebug.h @@ -292,9 +289,7 @@ test/bug60.d test/bug61.d test/bug62.d -test/bug63.d test/bug64.d -test/bug65.d test/bug7.d test/bug8.d test/bug9.d @@ -361,7 +356,6 @@ test/pointers.d test/pt.d test/ptrarith.d -test/ray.d test/scope1.d test/scope2.d test/scope3.d diff -r ce7ed8f59b99 -r c4e161556a21 lphobos/internal/adi.d --- a/lphobos/internal/adi.d Mon Nov 12 07:58:44 2007 +0100 +++ b/lphobos/internal/adi.d Wed Nov 14 20:18:01 2007 +0100 @@ -506,7 +506,7 @@ } /*************************************** - * Support for array equality test for bit arrays. + * Support for bit array equality test for bit arrays. */ version (none) @@ -602,7 +602,7 @@ } /*************************************** - * Support for array compare test. + * Support for char array compare test. */ extern (C) int _adCmpChar(Array a1, Array a2) @@ -748,7 +748,7 @@ } /*************************************** - * Support for array compare test. + * Support for bit array compare test. */ version (none)