comparison gen/arrays.cpp @ 424:c8d98ccad0cc

Error if static array is cast to an array such that oldarraysize % newelemsize != 0.
author Christian Kamm <kamm incasoftware de>
date Tue, 29 Jul 2008 12:32:01 +0200
parents e6e972c5cc17
children 944d43f3779f
comparison
equal deleted inserted replaced
423:3424f0fab7a9 424:c8d98ccad0cc
670 } 670 }
671 } 671 }
672 672
673 ////////////////////////////////////////////////////////////////////////////////////////// 673 //////////////////////////////////////////////////////////////////////////////////////////
674 // helper for eq and cmp 674 // helper for eq and cmp
675 static LLValue* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r, bool useti) 675 static LLValue* DtoArrayEqCmp_impl(Loc& loc, const char* func, DValue* l, DValue* r, bool useti)
676 { 676 {
677 Logger::println("comparing arrays"); 677 Logger::println("comparing arrays");
678 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, func); 678 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, func);
679 assert(fn); 679 assert(fn);
680 680
687 Type* r_ty = DtoDType(r->getType()); 687 Type* r_ty = DtoDType(r->getType());
688 assert(l_ty->next == r_ty->next); 688 assert(l_ty->next == r_ty->next);
689 if ((l_ty->ty == Tsarray) || (r_ty->ty == Tsarray)) { 689 if ((l_ty->ty == Tsarray) || (r_ty->ty == Tsarray)) {
690 Type* a_ty = l_ty->next->arrayOf(); 690 Type* a_ty = l_ty->next->arrayOf();
691 if (l_ty->ty == Tsarray) 691 if (l_ty->ty == Tsarray)
692 l = DtoCastArray(l, a_ty); 692 l = DtoCastArray(loc, l, a_ty);
693 if (r_ty->ty == Tsarray) 693 if (r_ty->ty == Tsarray)
694 r = DtoCastArray(r, a_ty); 694 r = DtoCastArray(loc, r, a_ty);
695 } 695 }
696 696
697 Logger::println("giving storage"); 697 Logger::println("giving storage");
698 698
699 // we need to give slices storage 699 // we need to give slices storage
756 756
757 return call->get(); 757 return call->get();
758 } 758 }
759 759
760 ////////////////////////////////////////////////////////////////////////////////////////// 760 //////////////////////////////////////////////////////////////////////////////////////////
761 LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r) 761 LLValue* DtoArrayEquals(Loc& loc, TOK op, DValue* l, DValue* r)
762 { 762 {
763 LLValue* res = DtoArrayEqCmp_impl("_adEq", l, r, true); 763 LLValue* res = DtoArrayEqCmp_impl(loc, "_adEq", l, r, true);
764 res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp"); 764 res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp");
765 if (op == TOKnotequal) 765 if (op == TOKnotequal)
766 res = gIR->ir->CreateNot(res, "tmp"); 766 res = gIR->ir->CreateNot(res, "tmp");
767 767
768 return res; 768 return res;
769 } 769 }
770 770
771 ////////////////////////////////////////////////////////////////////////////////////////// 771 //////////////////////////////////////////////////////////////////////////////////////////
772 LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r) 772 LLValue* DtoArrayCompare(Loc& loc, TOK op, DValue* l, DValue* r)
773 { 773 {
774 LLValue* res = 0; 774 LLValue* res = 0;
775 775
776 llvm::ICmpInst::Predicate cmpop; 776 llvm::ICmpInst::Predicate cmpop;
777 bool skip = false; 777 bool skip = false;
815 815
816 if (!skip) 816 if (!skip)
817 { 817 {
818 Type* t = DtoDType(DtoDType(l->getType())->next); 818 Type* t = DtoDType(DtoDType(l->getType())->next);
819 if (t->ty == Tchar) 819 if (t->ty == Tchar)
820 res = DtoArrayEqCmp_impl("_adCmpChar", l, r, false); 820 res = DtoArrayEqCmp_impl(loc, "_adCmpChar", l, r, false);
821 else 821 else
822 res = DtoArrayEqCmp_impl("_adCmp", l, r, true); 822 res = DtoArrayEqCmp_impl(loc, "_adCmp", l, r, true);
823 res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp"); 823 res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp");
824 } 824 }
825 825
826 assert(res); 826 assert(res);
827 return res; 827 return res;
942 assert(0); 942 assert(0);
943 return 0; 943 return 0;
944 } 944 }
945 945
946 ////////////////////////////////////////////////////////////////////////////////////////// 946 //////////////////////////////////////////////////////////////////////////////////////////
947 DValue* DtoCastArray(DValue* u, Type* to) 947 DValue* DtoCastArray(Loc& loc, DValue* u, Type* to)
948 { 948 {
949 Logger::println("DtoCastArray"); 949 Logger::println("DtoCastArray");
950 LOG_SCOPE; 950 LOG_SCOPE;
951 951
952 const LLType* tolltype = DtoType(to); 952 const LLType* tolltype = DtoType(to);
966 if (rval->getType() != tolltype) 966 if (rval->getType() != tolltype)
967 rval = gIR->ir->CreateBitCast(rval, tolltype, "tmp"); 967 rval = gIR->ir->CreateBitCast(rval, tolltype, "tmp");
968 } 968 }
969 else if (totype->ty == Tarray) { 969 else if (totype->ty == Tarray) {
970 Logger::cout() << "to array" << '\n'; 970 Logger::cout() << "to array" << '\n';
971
971 const LLType* ptrty = DtoArrayType(totype)->getContainedType(1); 972 const LLType* ptrty = DtoArrayType(totype)->getContainedType(1);
972 const LLType* ety = DtoTypeNotVoid(fromtype->next); 973 const LLType* ety = DtoTypeNotVoid(fromtype->next);
973 974
974 if (DSliceValue* usl = u->isSlice()) { 975 if (DSliceValue* usl = u->isSlice()) {
975 Logger::println("from slice"); 976 Logger::println("from slice");
984 LLValue* uval = u->getRVal(); 985 LLValue* uval = u->getRVal();
985 if (fromtype->ty == Tsarray) { 986 if (fromtype->ty == Tsarray) {
986 Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; 987 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
987 assert(isaPointer(uval->getType())); 988 assert(isaPointer(uval->getType()));
988 const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0)); 989 const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0));
990
991 if(arrty->getNumElements() % totype->next->size() != 0)
992 {
993 error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars());
994 fatal();
995 }
996
989 rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false); 997 rval2 = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false);
990 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0)); 998 rval2 = DtoArrayCastLength(rval2, ety, ptrty->getContainedType(0));
991 rval = DtoBitCast(uval, ptrty); 999 rval = DtoBitCast(uval, ptrty);
992 } 1000 }
993 else { 1001 else {