comparison gen/arrays.cpp @ 1205:e45984519be7

Simplify array casts and implement dyn array -> static array.
author Christian Kamm <kamm incasoftware de>
date Sun, 12 Apr 2009 19:56:03 +0200
parents d51b5fa41657
children 0686701178d3
comparison
equal deleted inserted replaced
1204:498c484f3a03 1205:e45984519be7
892 Logger::cout() << "to array" << '\n'; 892 Logger::cout() << "to array" << '\n';
893 893
894 const LLType* ptrty = DtoArrayType(totype)->getContainedType(1); 894 const LLType* ptrty = DtoArrayType(totype)->getContainedType(1);
895 const LLType* ety = DtoTypeNotVoid(fromtype->nextOf()); 895 const LLType* ety = DtoTypeNotVoid(fromtype->nextOf());
896 896
897 if (DSliceValue* usl = u->isSlice()) {
898 if (Logger::enabled())
899 {
900 Logger::println("from slice");
901 Logger::cout() << "from: " << *usl->ptr << " to: " << *ptrty << '\n';
902 }
903 rval = DtoBitCast(usl->ptr, ptrty);
904 if (fromtype->nextOf()->size() == totype->nextOf()->size())
905 rval2 = DtoArrayLen(usl);
906 else
907 rval2 = DtoArrayCastLength(DtoArrayLen(usl), ety, ptrty->getContainedType(0));
908 }
909 else {
910 if (fromtype->ty == Tsarray) {
911 LLValue* uval = u->getRVal();
912
913 if (Logger::enabled())
914 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
915
916 assert(isaPointer(uval->getType()));
917 const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0));
918
919 if(arrty->getNumElements()*fromtype->nextOf()->size() % totype->nextOf()->size() != 0)
920 {
921 error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars());
922 fatal();
923 }
924
925 rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false);
926 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
927 rval = DtoBitCast(uval, ptrty);
928 }
929 else {
930 rval2 = DtoArrayLen(u);
931 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
932
933 rval = DtoArrayPtr(u);
934 rval = DtoBitCast(rval, ptrty);
935 }
936 }
937 isslice = true;
938 }
939 else if (totype->ty == Tsarray) {
940 if (Logger::enabled())
941 Logger::cout() << "to sarray" << '\n';
942
943 if (fromtype->ty == Tsarray) { 897 if (fromtype->ty == Tsarray) {
944 LLValue* uval = u->getRVal(); 898 LLValue* uval = u->getRVal();
945 899
946 if (Logger::enabled()) 900 if (Logger::enabled())
947 Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; 901 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
953 { 907 {
954 error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars()); 908 error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars());
955 fatal(); 909 fatal();
956 } 910 }
957 911
912 rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false);
913 if (fromtype->nextOf()->size() != totype->nextOf()->size())
914 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
915 rval = DtoBitCast(uval, ptrty);
916 }
917 else {
918 rval2 = DtoArrayLen(u);
919 if (fromtype->nextOf()->size() != totype->nextOf()->size())
920 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
921
922 rval = DtoArrayPtr(u);
923 rval = DtoBitCast(rval, ptrty);
924 }
925 isslice = true;
926 }
927 else if (totype->ty == Tsarray) {
928 if (Logger::enabled())
929 Logger::cout() << "to sarray" << '\n';
930
931 size_t tosize = ((TypeSArray*)totype)->dim->toInteger();
932
933 if (fromtype->ty == Tsarray) {
934 LLValue* uval = u->getRVal();
935
936 if (Logger::enabled())
937 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
938
939 assert(isaPointer(uval->getType()));
940 const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0));
941
942 /*if(arrty->getNumElements()*fromtype->nextOf()->size() != tosize*totype->nextOf()->size())
943 {
944 error(loc, "invalid cast from '%s' to '%s', the sizes are not the same", fromtype->toChars(), totype->toChars());
945 fatal();
946 }*/
947
958 rval = DtoBitCast(uval, getPtrToType(tolltype)); 948 rval = DtoBitCast(uval, getPtrToType(tolltype));
959 } 949 }
960 else 950 else {
961 assert(0 && "Cast to static array not implemented!"); 951 size_t i = (tosize * totype->nextOf()->size() - 1) / fromtype->nextOf()->size();
952 DConstValue index(Type::tsize_t, DtoConstSize_t(i));
953 DtoArrayBoundsCheck(loc, u, &index, false);
954
955 rval = DtoArrayPtr(u);
956 rval = DtoBitCast(rval, getPtrToType(tolltype));
957 }
962 } 958 }
963 else if (totype->ty == Tbool) { 959 else if (totype->ty == Tbool) {
964 // return (arr.ptr !is null) 960 // return (arr.ptr !is null)
965 LLValue* ptr = DtoArrayPtr(u); 961 LLValue* ptr = DtoArrayPtr(u);
966 LLConstant* nul = getNullPtr(ptr->getType()); 962 LLConstant* nul = getNullPtr(ptr->getType());