Mercurial > projects > ldc
comparison gen/arrays.cpp @ 308:6b62e8cdf970 trunk
[svn r329] Cleaned up a bunch of array code for handling special slice cases no
longer relevant.
author | lindquist |
---|---|
date | Sat, 28 Jun 2008 05:57:16 +0200 |
parents | 5de180867c46 |
children | a9697749e898 |
comparison
equal
deleted
inserted
replaced
307:7ade5e035beb | 308:6b62e8cdf970 |
---|---|
239 ////////////////////////////////////////////////////////////////////////////////////////// | 239 ////////////////////////////////////////////////////////////////////////////////////////// |
240 | 240 |
241 void DtoSetArray(LLValue* arr, LLValue* dim, LLValue* ptr) | 241 void DtoSetArray(LLValue* arr, LLValue* dim, LLValue* ptr) |
242 { | 242 { |
243 Logger::println("SetArray"); | 243 Logger::println("SetArray"); |
244 assert(isaStruct(arr->getType()->getContainedType(0))); | |
244 DtoStore(dim, DtoGEPi(arr,0,0)); | 245 DtoStore(dim, DtoGEPi(arr,0,0)); |
245 DtoStore(ptr, DtoGEPi(arr,0,1)); | 246 DtoStore(ptr, DtoGEPi(arr,0,1)); |
246 } | 247 } |
247 | 248 |
248 ////////////////////////////////////////////////////////////////////////////////////////// | 249 ////////////////////////////////////////////////////////////////////////////////////////// |
347 } | 348 } |
348 | 349 |
349 ////////////////////////////////////////////////////////////////////////////////////////// | 350 ////////////////////////////////////////////////////////////////////////////////////////// |
350 static LLValue* get_slice_ptr(DSliceValue* e, LLValue*& sz) | 351 static LLValue* get_slice_ptr(DSliceValue* e, LLValue*& sz) |
351 { | 352 { |
353 assert(e->len != 0); | |
352 const LLType* t = e->ptr->getType()->getContainedType(0); | 354 const LLType* t = e->ptr->getType()->getContainedType(0); |
353 LLValue* ret = 0; | 355 sz = gIR->ir->CreateMul(DtoConstSize_t(getABITypeSize(t)), e->len, "tmp"); |
354 if (e->len != 0) { | 356 return e->ptr; |
355 // this means it's a real slice | |
356 ret = e->ptr; | |
357 | |
358 size_t elembsz = getABITypeSize(ret->getType()->getContainedType(0)); | |
359 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); | |
360 | |
361 if (isaConstantInt(e->len)) { | |
362 sz = llvm::ConstantExpr::getMul(elemsz, isaConstant(e->len)); | |
363 } | |
364 else { | |
365 sz = llvm::BinaryOperator::createMul(elemsz,e->len,"tmp",gIR->scopebb()); | |
366 } | |
367 } | |
368 else if (isaArray(t)) { | |
369 ret = DtoGEPi(e->ptr, 0, 0); | |
370 | |
371 size_t elembsz = getABITypeSize(ret->getType()->getContainedType(0)); | |
372 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); | |
373 | |
374 size_t numelements = isaArray(t)->getNumElements(); | |
375 llvm::ConstantInt* nelems = llvm::ConstantInt::get(DtoSize_t(), numelements, false); | |
376 | |
377 sz = llvm::ConstantExpr::getMul(elemsz, nelems); | |
378 } | |
379 else if (isaStruct(t)) { | |
380 ret = DtoGEPi(e->ptr, 0, 1); | |
381 ret = DtoLoad(ret); | |
382 | |
383 size_t elembsz = getABITypeSize(ret->getType()->getContainedType(0)); | |
384 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); | |
385 | |
386 LLValue* len = DtoGEPi(e->ptr, 0, 0); | |
387 len = DtoLoad(len); | |
388 sz = llvm::BinaryOperator::createMul(len,elemsz,"tmp",gIR->scopebb()); | |
389 } | |
390 else { | |
391 assert(0); | |
392 } | |
393 return ret; | |
394 } | 357 } |
395 | 358 |
396 void DtoArrayCopySlices(DSliceValue* dst, DSliceValue* src) | 359 void DtoArrayCopySlices(DSliceValue* dst, DSliceValue* src) |
397 { | 360 { |
398 Logger::println("ArrayCopySlices"); | 361 Logger::println("ArrayCopySlices"); |
893 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_array_cast_len"); | 856 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_array_cast_len"); |
894 return llvm::CallInst::Create(fn, args.begin(), args.end(), "tmp", gIR->scopebb()); | 857 return llvm::CallInst::Create(fn, args.begin(), args.end(), "tmp", gIR->scopebb()); |
895 } | 858 } |
896 | 859 |
897 ////////////////////////////////////////////////////////////////////////////////////////// | 860 ////////////////////////////////////////////////////////////////////////////////////////// |
898 LLValue* DtoDynArrayIs(TOK op, LLValue* l, LLValue* r) | 861 LLValue* DtoDynArrayIs(TOK op, DValue* l, DValue* r) |
899 { | 862 { |
900 llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; | 863 LLValue *len1, *ptr1, *len2, *ptr2; |
901 | 864 |
902 if (r == NULL) { | 865 assert(l); |
903 LLValue* ll = gIR->ir->CreateLoad(DtoGEPi(l, 0,0),"tmp"); | 866 assert(r); |
904 LLValue* rl = llvm::Constant::getNullValue(ll->getType());//DtoConstSize_t(0); | 867 |
905 LLValue* b1 = gIR->ir->CreateICmp(pred,ll,rl,"tmp"); | 868 // compare lengths |
906 | 869 len1 = DtoArrayLen(l); |
907 LLValue* lp = gIR->ir->CreateLoad(DtoGEPi(l, 0,1),"tmp"); | 870 len2 = DtoArrayLen(r); |
908 const LLPointerType* pty = isaPointer(lp->getType()); | 871 LLValue* b1 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,len1,len2,"tmp"); |
909 LLValue* rp = llvm::ConstantPointerNull::get(pty); | 872 |
910 LLValue* b2 = gIR->ir->CreateICmp(pred,lp,rp,"tmp"); | 873 // compare pointers |
911 | 874 ptr1 = DtoArrayPtr(l); |
912 LLValue* b = gIR->ir->CreateAnd(b1,b2,"tmp"); | 875 ptr2 = DtoArrayPtr(r); |
913 return b; | 876 LLValue* b2 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,ptr1,ptr2,"tmp"); |
914 } | 877 |
915 else { | 878 // combine |
916 assert(l->getType() == r->getType()); | 879 LLValue* res = gIR->ir->CreateAnd(b1,b2,"tmp"); |
917 | 880 |
918 LLValue* ll = gIR->ir->CreateLoad(DtoGEPi(l, 0,0),"tmp"); | 881 // return result |
919 LLValue* rl = gIR->ir->CreateLoad(DtoGEPi(r, 0,0),"tmp"); | 882 return (op == TOKnotidentity) ? gIR->ir->CreateNot(res) : res; |
920 LLValue* b1 = gIR->ir->CreateICmp(pred,ll,rl,"tmp"); | |
921 | |
922 LLValue* lp = gIR->ir->CreateLoad(DtoGEPi(l, 0,1),"tmp"); | |
923 LLValue* rp = gIR->ir->CreateLoad(DtoGEPi(r, 0,1),"tmp"); | |
924 LLValue* b2 = gIR->ir->CreateICmp(pred,lp,rp,"tmp"); | |
925 | |
926 LLValue* b = gIR->ir->CreateAnd(b1,b2,"tmp"); | |
927 return b; | |
928 } | |
929 } | 883 } |
930 | 884 |
931 ////////////////////////////////////////////////////////////////////////////////////////// | 885 ////////////////////////////////////////////////////////////////////////////////////////// |
932 LLConstant* DtoConstStaticArray(const LLType* t, LLConstant* c) | 886 LLConstant* DtoConstStaticArray(const LLType* t, LLConstant* c) |
933 { | 887 { |
952 Logger::println("DtoArrayLen"); | 906 Logger::println("DtoArrayLen"); |
953 LOG_SCOPE; | 907 LOG_SCOPE; |
954 | 908 |
955 Type* t = DtoDType(v->getType()); | 909 Type* t = DtoDType(v->getType()); |
956 if (t->ty == Tarray) { | 910 if (t->ty == Tarray) { |
957 if (DSliceValue* s = v->isSlice()) { | 911 if (DSliceValue* s = v->isSlice()) |
958 if (s->len) { | 912 return s->len; |
959 return s->len; | 913 else if (v->isNull()) |
960 } | 914 return DtoConstSize_t(0); |
961 const LLArrayType* arrTy = isaArray(s->ptr->getType()->getContainedType(0)); | |
962 if (arrTy) | |
963 return DtoConstSize_t(arrTy->getNumElements()); | |
964 else | |
965 return DtoLoad(DtoGEPi(s->ptr, 0,0)); | |
966 } | |
967 return DtoLoad(DtoGEPi(v->getRVal(), 0,0)); | 915 return DtoLoad(DtoGEPi(v->getRVal(), 0,0)); |
968 } | 916 } |
969 else if (t->ty == Tsarray) { | 917 else if (t->ty == Tsarray) { |
970 assert(!v->isSlice()); | 918 assert(!v->isSlice()); |
919 assert(!v->isNull()); | |
971 LLValue* rv = v->getRVal(); | 920 LLValue* rv = v->getRVal(); |
972 Logger::cout() << "casting: " << *rv << '\n'; | |
973 const LLArrayType* t = isaArray(rv->getType()->getContainedType(0)); | 921 const LLArrayType* t = isaArray(rv->getType()->getContainedType(0)); |
922 assert(t); | |
974 return DtoConstSize_t(t->getNumElements()); | 923 return DtoConstSize_t(t->getNumElements()); |
975 } | 924 } |
976 assert(0); | 925 assert(0 && "unsupported array for len"); |
977 return 0; | 926 return 0; |
978 } | 927 } |
979 | 928 |
980 ////////////////////////////////////////////////////////////////////////////////////////// | 929 ////////////////////////////////////////////////////////////////////////////////////////// |
981 LLValue* DtoArrayPtr(DValue* v) | 930 LLValue* DtoArrayPtr(DValue* v) |
983 Logger::println("DtoArrayPtr"); | 932 Logger::println("DtoArrayPtr"); |
984 LOG_SCOPE; | 933 LOG_SCOPE; |
985 | 934 |
986 Type* t = DtoDType(v->getType()); | 935 Type* t = DtoDType(v->getType()); |
987 if (t->ty == Tarray) { | 936 if (t->ty == Tarray) { |
988 if (DSliceValue* s = v->isSlice()) { | 937 if (DSliceValue* s = v->isSlice()) |
989 if (s->len) return s->ptr; | 938 return s->ptr; |
990 const LLType* t = s->ptr->getType()->getContainedType(0); | 939 else if (v->isNull()) |
991 Logger::cout() << "ptr of full slice: " << *s->ptr << '\n'; | 940 return getNullPtr(getPtrToType(DtoType(t->next))); |
992 const LLArrayType* arrTy = isaArray(s->ptr->getType()->getContainedType(0)); | |
993 if (arrTy) | |
994 return DtoGEPi(s->ptr, 0,0); | |
995 else | |
996 return DtoLoad(DtoGEPi(s->ptr, 0,1)); | |
997 } | |
998 return DtoLoad(DtoGEPi(v->getRVal(), 0,1)); | 941 return DtoLoad(DtoGEPi(v->getRVal(), 0,1)); |
999 } | 942 } |
1000 else if (t->ty == Tsarray) { | 943 else if (t->ty == Tsarray) { |
944 assert(!v->isSlice()); | |
945 assert(!v->isNull()); | |
1001 return DtoGEPi(v->getRVal(), 0,0); | 946 return DtoGEPi(v->getRVal(), 0,0); |
1002 } | 947 } |
1003 assert(0); | 948 assert(0); |
1004 return 0; | 949 return 0; |
1005 } | 950 } |