comparison gen/arrays.cpp @ 758:f04dde6e882c

Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:38:48 +0100
parents 30b42a283c8e
children 4adf0f742896
comparison
equal deleted inserted replaced
757:2c730d530c98 758:f04dde6e882c
18 18
19 ////////////////////////////////////////////////////////////////////////////////////////// 19 //////////////////////////////////////////////////////////////////////////////////////////
20 20
21 const LLStructType* DtoArrayType(Type* arrayTy) 21 const LLStructType* DtoArrayType(Type* arrayTy)
22 { 22 {
23 assert(arrayTy->next); 23 assert(arrayTy->nextOf());
24 const LLType* elemty = DtoType(arrayTy->next); 24 const LLType* elemty = DtoType(arrayTy->nextOf());
25 if (elemty == LLType::VoidTy) 25 if (elemty == LLType::VoidTy)
26 elemty = LLType::Int8Ty; 26 elemty = LLType::Int8Ty;
27 return LLStructType::get(DtoSize_t(), getPtrToType(elemty), 0); 27 return LLStructType::get(DtoSize_t(), getPtrToType(elemty), 0);
28 } 28 }
29 29
37 const LLArrayType* DtoStaticArrayType(Type* t) 37 const LLArrayType* DtoStaticArrayType(Type* t)
38 { 38 {
39 t = t->toBasetype(); 39 t = t->toBasetype();
40 assert(t->ty == Tsarray); 40 assert(t->ty == Tsarray);
41 TypeSArray* tsa = (TypeSArray*)t; 41 TypeSArray* tsa = (TypeSArray*)t;
42 Type* tnext = tsa->next; 42 Type* tnext = tsa->nextOf();
43 43
44 const LLType* elemty = DtoType(tnext); 44 const LLType* elemty = DtoType(tnext);
45 if (elemty == LLType::VoidTy) 45 if (elemty == LLType::VoidTy)
46 elemty = LLType::Int8Ty; 46 elemty = LLType::Int8Ty;
47 47
236 236
237 Logger::println("dim = %u", tdim); 237 Logger::println("dim = %u", tdim);
238 238
239 std::vector<LLConstant*> inits(tdim, NULL); 239 std::vector<LLConstant*> inits(tdim, NULL);
240 240
241 Type* arrnext = arrinittype->next; 241 Type* arrnext = arrinittype->nextOf();
242 const LLType* elemty = DtoType(arrinittype->next); 242 const LLType* elemty = DtoType(arrinittype->nextOf());
243 243
244 assert(arrinit->index.dim == arrinit->value.dim); 244 assert(arrinit->index.dim == arrinit->value.dim);
245 for (unsigned i=0,j=0; i < tdim; ++i) 245 for (unsigned i=0,j=0; i < tdim; ++i)
246 { 246 {
247 Initializer* init = 0; 247 Initializer* init = 0;
287 else 287 else
288 v = arrnext->defaultInit()->toConstElem(gIR); 288 v = arrnext->defaultInit()->toConstElem(gIR);
289 } 289 }
290 290
291 if (!v) 291 if (!v)
292 v = DtoConstInitializer(t->next, init); 292 v = DtoConstInitializer(t->nextOf(), init);
293 assert(v); 293 assert(v);
294 294
295 inits[i] = v; 295 inits[i] = v;
296 if (Logger::enabled()) 296 if (Logger::enabled())
297 Logger::cout() << "llval: " << *v << '\n'; 297 Logger::cout() << "llval: " << *v << '\n';
445 assert(newdim); 445 assert(newdim);
446 assert(arrayType); 446 assert(arrayType);
447 assert(arrayType->toBasetype()->ty == Tarray); 447 assert(arrayType->toBasetype()->ty == Tarray);
448 448
449 // decide on what runtime function to call based on whether the type is zero initialized 449 // decide on what runtime function to call based on whether the type is zero initialized
450 bool zeroInit = arrayType->toBasetype()->next->isZeroInit(); 450 bool zeroInit = arrayType->toBasetype()->nextOf()->isZeroInit();
451 451
452 // call runtime 452 // call runtime
453 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, zeroInit ? "_d_arraysetlengthT" : "_d_arraysetlengthiT" ); 453 LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, zeroInit ? "_d_arraysetlengthT" : "_d_arraysetlengthiT" );
454 454
455 LLSmallVector<LLValue*,4> args; 455 LLSmallVector<LLValue*,4> args;
581 DValue* e2 = exp2->toElem(gIR); 581 DValue* e2 = exp2->toElem(gIR);
582 582
583 llvm::Value *len1, *src1, *res; 583 llvm::Value *len1, *src1, *res;
584 584
585 // handle prefix case, eg. int~int[] 585 // handle prefix case, eg. int~int[]
586 if (t2->next && t1 == t2->next->toBasetype()) 586 if (t2->nextOf() && t1 == t2->nextOf()->toBasetype())
587 { 587 {
588 len1 = DtoArrayLen(e2); 588 len1 = DtoArrayLen(e2);
589 res = gIR->ir->CreateAdd(len1,DtoConstSize_t(1),"tmp"); 589 res = gIR->ir->CreateAdd(len1,DtoConstSize_t(1),"tmp");
590 590
591 DValue* lenval = new DImValue(Type::tsize_t, res); 591 DValue* lenval = new DImValue(Type::tsize_t, res);
731 assert(0); 731 assert(0);
732 } 732 }
733 733
734 if (!skip) 734 if (!skip)
735 { 735 {
736 Type* t = l->getType()->toBasetype()->next->toBasetype(); 736 Type* t = l->getType()->toBasetype()->nextOf()->toBasetype();
737 if (t->ty == Tchar) 737 if (t->ty == Tchar)
738 res = DtoArrayEqCmp_impl(loc, "_adCmpChar", l, r, false); 738 res = DtoArrayEqCmp_impl(loc, "_adCmpChar", l, r, false);
739 else 739 else
740 res = DtoArrayEqCmp_impl(loc, "_adCmp", l, r, true); 740 res = DtoArrayEqCmp_impl(loc, "_adCmp", l, r, true);
741 res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp"); 741 res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp");
849 Type* t = v->getType()->toBasetype(); 849 Type* t = v->getType()->toBasetype();
850 if (t->ty == Tarray) { 850 if (t->ty == Tarray) {
851 if (DSliceValue* s = v->isSlice()) 851 if (DSliceValue* s = v->isSlice())
852 return s->ptr; 852 return s->ptr;
853 else if (v->isNull()) 853 else if (v->isNull())
854 return getNullPtr(getPtrToType(DtoType(t->next))); 854 return getNullPtr(getPtrToType(DtoType(t->nextOf())));
855 else if (v->isLVal()) 855 else if (v->isLVal())
856 return DtoLoad(DtoGEPi(v->getLVal(), 0,1), ".ptr"); 856 return DtoLoad(DtoGEPi(v->getLVal(), 0,1), ".ptr");
857 return gIR->ir->CreateExtractValue(v->getRVal(), 1, ".ptr"); 857 return gIR->ir->CreateExtractValue(v->getRVal(), 1, ".ptr");
858 } 858 }
859 else if (t->ty == Tsarray) { 859 else if (t->ty == Tsarray) {
894 else if (totype->ty == Tarray) { 894 else if (totype->ty == Tarray) {
895 if (Logger::enabled()) 895 if (Logger::enabled())
896 Logger::cout() << "to array" << '\n'; 896 Logger::cout() << "to array" << '\n';
897 897
898 const LLType* ptrty = DtoArrayType(totype)->getContainedType(1); 898 const LLType* ptrty = DtoArrayType(totype)->getContainedType(1);
899 const LLType* ety = DtoTypeNotVoid(fromtype->next); 899 const LLType* ety = DtoTypeNotVoid(fromtype->nextOf());
900 900
901 if (DSliceValue* usl = u->isSlice()) { 901 if (DSliceValue* usl = u->isSlice()) {
902 if (Logger::enabled()) 902 if (Logger::enabled())
903 { 903 {
904 Logger::println("from slice"); 904 Logger::println("from slice");
905 Logger::cout() << "from: " << *usl->ptr << " to: " << *ptrty << '\n'; 905 Logger::cout() << "from: " << *usl->ptr << " to: " << *ptrty << '\n';
906 } 906 }
907 rval = DtoBitCast(usl->ptr, ptrty); 907 rval = DtoBitCast(usl->ptr, ptrty);
908 if (fromtype->next->size() == totype->next->size()) 908 if (fromtype->nextOf()->size() == totype->nextOf()->size())
909 rval2 = DtoArrayLen(usl); 909 rval2 = DtoArrayLen(usl);
910 else 910 else
911 rval2 = DtoArrayCastLength(DtoArrayLen(usl), ety, ptrty->getContainedType(0)); 911 rval2 = DtoArrayCastLength(DtoArrayLen(usl), ety, ptrty->getContainedType(0));
912 } 912 }
913 else { 913 else {
918 Logger::cout() << "uvalTy = " << *uval->getType() << '\n'; 918 Logger::cout() << "uvalTy = " << *uval->getType() << '\n';
919 919
920 assert(isaPointer(uval->getType())); 920 assert(isaPointer(uval->getType()));
921 const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0)); 921 const LLArrayType* arrty = isaArray(uval->getType()->getContainedType(0));
922 922
923 if(arrty->getNumElements()*fromtype->next->size() % totype->next->size() != 0) 923 if(arrty->getNumElements()*fromtype->nextOf()->size() % totype->nextOf()->size() != 0)
924 { 924 {
925 error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars()); 925 error(loc, "invalid cast from '%s' to '%s', the element sizes don't line up", fromtype->toChars(), totype->toChars());
926 fatal(); 926 fatal();
927 } 927 }
928 928