Mercurial > projects > ldc
comparison gen/arrays.cpp @ 96:ce7ed8f59b99 trunk
[svn r100] Moved test/ray.d to demos/ray.d.
Cleanups.
author | lindquist |
---|---|
date | Mon, 12 Nov 2007 07:58:44 +0100 |
parents | 61615fa85940 |
children | 6789050b5ad1 |
comparison
equal
deleted
inserted
replaced
95:71b8fecdae38 | 96:ce7ed8f59b99 |
---|---|
40 ////////////////////////////////////////////////////////////////////////////////////////// | 40 ////////////////////////////////////////////////////////////////////////////////////////// |
41 | 41 |
42 const llvm::ArrayType* DtoStaticArrayType(Type* t) | 42 const llvm::ArrayType* DtoStaticArrayType(Type* t) |
43 { | 43 { |
44 if (t->llvmType) | 44 if (t->llvmType) |
45 return llvm::cast<llvm::ArrayType>(t->llvmType); | 45 return isaArray(t->llvmType); |
46 | 46 |
47 assert(t->ty == Tsarray); | 47 assert(t->ty == Tsarray); |
48 assert(t->next); | 48 assert(t->next); |
49 | 49 |
50 const llvm::Type* at = DtoType(t->next); | 50 const llvm::Type* at = DtoType(t->next); |
66 llvm::Value* len = DtoGEPi(v,0,0,"tmp",gIR->scopebb()); | 66 llvm::Value* len = DtoGEPi(v,0,0,"tmp",gIR->scopebb()); |
67 llvm::Value* zerolen = llvm::ConstantInt::get(len->getType()->getContainedType(0), 0, false); | 67 llvm::Value* zerolen = llvm::ConstantInt::get(len->getType()->getContainedType(0), 0, false); |
68 new llvm::StoreInst(zerolen, len, gIR->scopebb()); | 68 new llvm::StoreInst(zerolen, len, gIR->scopebb()); |
69 | 69 |
70 llvm::Value* ptr = DtoGEPi(v,0,1,"tmp",gIR->scopebb()); | 70 llvm::Value* ptr = DtoGEPi(v,0,1,"tmp",gIR->scopebb()); |
71 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(ptr->getType()->getContainedType(0)); | 71 const llvm::PointerType* pty = isaPointer(ptr->getType()->getContainedType(0)); |
72 llvm::Value* nullptr = llvm::ConstantPointerNull::get(pty); | 72 llvm::Value* nullptr = llvm::ConstantPointerNull::get(pty); |
73 new llvm::StoreInst(nullptr, ptr, gIR->scopebb()); | 73 new llvm::StoreInst(nullptr, ptr, gIR->scopebb()); |
74 } | 74 } |
75 | 75 |
76 ////////////////////////////////////////////////////////////////////////////////////////// | 76 ////////////////////////////////////////////////////////////////////////////////////////// |
91 new llvm::StoreInst(val, ptr, gIR->scopebb()); | 91 new llvm::StoreInst(val, ptr, gIR->scopebb()); |
92 } | 92 } |
93 else | 93 else |
94 { | 94 { |
95 Logger::cout() << "array assignment type dont match: " << *dst->getType() << '\n' << *src->getType() << '\n'; | 95 Logger::cout() << "array assignment type dont match: " << *dst->getType() << '\n' << *src->getType() << '\n'; |
96 if (!llvm::isa<llvm::ArrayType>(src->getType()->getContainedType(0))) | 96 const llvm::ArrayType* arrty = isaArray(src->getType()->getContainedType(0)); |
97 if (!arrty) | |
97 { | 98 { |
98 Logger::cout() << "invalid: " << *src << '\n'; | 99 Logger::cout() << "invalid: " << *src << '\n'; |
99 assert(0); | 100 assert(0); |
100 } | 101 } |
101 const llvm::ArrayType* arrty = llvm::cast<llvm::ArrayType>(src->getType()->getContainedType(0)); | |
102 llvm::Type* dstty = llvm::PointerType::get(arrty->getElementType()); | 102 llvm::Type* dstty = llvm::PointerType::get(arrty->getElementType()); |
103 | 103 |
104 llvm::Value* dstlen = DtoGEPi(dst,0,0,"tmp",gIR->scopebb()); | 104 llvm::Value* dstlen = DtoGEPi(dst,0,0,"tmp",gIR->scopebb()); |
105 llvm::Value* srclen = DtoConstSize_t(arrty->getNumElements()); | 105 llvm::Value* srclen = DtoConstSize_t(arrty->getNumElements()); |
106 new llvm::StoreInst(srclen, dstlen, gIR->scopebb()); | 106 new llvm::StoreInst(srclen, dstlen, gIR->scopebb()); |
113 | 113 |
114 ////////////////////////////////////////////////////////////////////////////////////////// | 114 ////////////////////////////////////////////////////////////////////////////////////////// |
115 | 115 |
116 void DtoArrayInit(llvm::Value* l, llvm::Value* r) | 116 void DtoArrayInit(llvm::Value* l, llvm::Value* r) |
117 { | 117 { |
118 const llvm::PointerType* ptrty = llvm::cast<llvm::PointerType>(l->getType()); | 118 const llvm::PointerType* ptrty = isaPointer(l->getType()); |
119 const llvm::Type* t = ptrty->getContainedType(0); | 119 const llvm::Type* t = ptrty->getContainedType(0); |
120 const llvm::ArrayType* arrty = llvm::dyn_cast<llvm::ArrayType>(t); | 120 const llvm::ArrayType* arrty = isaArray(t); |
121 if (arrty) | 121 if (arrty) |
122 { | 122 { |
123 llvm::Value* ptr = DtoGEPi(l,0,0,"tmp",gIR->scopebb()); | 123 llvm::Value* ptr = DtoGEPi(l,0,0,"tmp",gIR->scopebb()); |
124 llvm::Value* dim = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false); | 124 llvm::Value* dim = llvm::ConstantInt::get(DtoSize_t(), arrty->getNumElements(), false); |
125 DtoArrayInit(ptr, dim, r); | 125 DtoArrayInit(ptr, dim, r); |
126 } | 126 } |
127 else if (llvm::isa<llvm::StructType>(t)) | 127 else if (isaStruct(t)) |
128 { | 128 { |
129 llvm::Value* dim = DtoLoad(DtoGEPi(l, 0,0, "tmp")); | 129 llvm::Value* dim = DtoLoad(DtoGEPi(l, 0,0, "tmp")); |
130 llvm::Value* ptr = DtoLoad(DtoGEPi(l, 0,1, "tmp")); | 130 llvm::Value* ptr = DtoLoad(DtoGEPi(l, 0,1, "tmp")); |
131 DtoArrayInit(ptr, dim, r); | 131 DtoArrayInit(ptr, dim, r); |
132 } | 132 } |
138 | 138 |
139 typedef const llvm::Type* constLLVMTypeP; | 139 typedef const llvm::Type* constLLVMTypeP; |
140 | 140 |
141 static size_t checkRectArrayInit(const llvm::Type* pt, constLLVMTypeP& finalty) | 141 static size_t checkRectArrayInit(const llvm::Type* pt, constLLVMTypeP& finalty) |
142 { | 142 { |
143 if (llvm::isa<llvm::ArrayType>(pt)) { | 143 if (const llvm::ArrayType* arrty = isaArray(pt)) { |
144 size_t n = checkRectArrayInit(pt->getContainedType(0), finalty); | 144 size_t n = checkRectArrayInit(arrty->getElementType(), finalty); |
145 size_t ne = llvm::cast<llvm::ArrayType>(pt)->getNumElements(); | 145 size_t ne = arrty->getNumElements(); |
146 if (n) return n * ne; | 146 if (n) return n * ne; |
147 return ne; | 147 return ne; |
148 } | 148 } |
149 finalty = pt; | 149 finalty = pt; |
150 return 0; | 150 return 0; |
157 const llvm::Type* pt = ptr->getType()->getContainedType(0); | 157 const llvm::Type* pt = ptr->getType()->getContainedType(0); |
158 const llvm::Type* t = val->getType(); | 158 const llvm::Type* t = val->getType(); |
159 const llvm::Type* finalTy; | 159 const llvm::Type* finalTy; |
160 if (size_t arrsz = checkRectArrayInit(pt, finalTy)) { | 160 if (size_t arrsz = checkRectArrayInit(pt, finalTy)) { |
161 assert(finalTy == t); | 161 assert(finalTy == t); |
162 llvm::Constant* c = llvm::cast_or_null<llvm::Constant>(dim); | 162 llvm::Constant* c = isaConstant(dim); |
163 assert(c); | 163 assert(c); |
164 dim = llvm::ConstantExpr::getMul(c, DtoConstSize_t(arrsz)); | 164 dim = llvm::ConstantExpr::getMul(c, DtoConstSize_t(arrsz)); |
165 ptr = gIR->ir->CreateBitCast(ptr, llvm::PointerType::get(finalTy), "tmp"); | 165 ptr = gIR->ir->CreateBitCast(ptr, llvm::PointerType::get(finalTy), "tmp"); |
166 } | 166 } |
167 else if (llvm::isa<llvm::StructType>(t)) { | 167 else if (isaStruct(t)) { |
168 assert(0); | 168 assert(0); |
169 } | 169 } |
170 else { | 170 else { |
171 assert(t == pt); | 171 assert(t == pt); |
172 } | 172 } |
176 args.push_back(dim); | 176 args.push_back(dim); |
177 args.push_back(val); | 177 args.push_back(val); |
178 | 178 |
179 const char* funcname = NULL; | 179 const char* funcname = NULL; |
180 | 180 |
181 if (llvm::isa<llvm::PointerType>(t)) { | 181 if (isaPointer(t)) { |
182 funcname = "_d_array_init_pointer"; | 182 funcname = "_d_array_init_pointer"; |
183 | 183 |
184 const llvm::Type* dstty = llvm::PointerType::get(llvm::PointerType::get(llvm::Type::Int8Ty)); | 184 const llvm::Type* dstty = llvm::PointerType::get(llvm::PointerType::get(llvm::Type::Int8Ty)); |
185 if (args[0]->getType() != dstty) | 185 if (args[0]->getType() != dstty) |
186 args[0] = new llvm::BitCastInst(args[0],dstty,"tmp",gIR->scopebb()); | 186 args[0] = new llvm::BitCastInst(args[0],dstty,"tmp",gIR->scopebb()); |
225 ////////////////////////////////////////////////////////////////////////////////////////// | 225 ////////////////////////////////////////////////////////////////////////////////////////// |
226 | 226 |
227 void DtoSetArray(llvm::Value* arr, llvm::Value* dim, llvm::Value* ptr) | 227 void DtoSetArray(llvm::Value* arr, llvm::Value* dim, llvm::Value* ptr) |
228 { | 228 { |
229 Logger::cout() << "DtoSetArray(" << *arr << ", " << *dim << ", " << *ptr << ")\n"; | 229 Logger::cout() << "DtoSetArray(" << *arr << ", " << *dim << ", " << *ptr << ")\n"; |
230 const llvm::StructType* st = llvm::cast<llvm::StructType>(arr->getType()->getContainedType(0)); | 230 const llvm::StructType* st = isaStruct(arr->getType()->getContainedType(0)); |
231 //const llvm::PointerType* pt = llvm::cast<llvm::PointerType>(r->getType()); | 231 |
232 | |
233 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); | 232 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); |
234 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); | 233 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); |
235 | 234 |
236 llvm::Value* arrdim = DtoGEP(arr,zero,zero,"tmp",gIR->scopebb()); | 235 llvm::Value* arrdim = DtoGEP(arr,zero,zero,"tmp",gIR->scopebb()); |
237 new llvm::StoreInst(dim, arrdim, gIR->scopebb()); | 236 new llvm::StoreInst(dim, arrdim, gIR->scopebb()); |
238 | 237 |
239 llvm::Value* arrptr = DtoGEP(arr,zero,one,"tmp",gIR->scopebb()); | 238 llvm::Value* arrptr = DtoGEP(arr,zero,one,"tmp",gIR->scopebb()); |
240 new llvm::StoreInst(ptr, arrptr, gIR->scopebb()); | 239 new llvm::StoreInst(ptr, arrptr, gIR->scopebb()); |
241 } | 240 } |
242 | 241 |
243 ////////////////////////////////////////////////////////////////////////////////////////// | 242 ////////////////////////////////////////////////////////////////////////////////////////// |
334 ret = e->ptr; | 333 ret = e->ptr; |
335 | 334 |
336 size_t elembsz = gTargetData->getTypeSize(ret->getType()); | 335 size_t elembsz = gTargetData->getTypeSize(ret->getType()); |
337 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); | 336 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); |
338 | 337 |
339 if (llvm::isa<llvm::ConstantInt>(e->len)) { | 338 if (isaConstantInt(e->len)) { |
340 sz = llvm::ConstantExpr::getMul(elemsz, llvm::cast<llvm::Constant>(e->len)); | 339 sz = llvm::ConstantExpr::getMul(elemsz, isaConstant(e->len)); |
341 } | 340 } |
342 else { | 341 else { |
343 sz = llvm::BinaryOperator::createMul(elemsz,e->len,"tmp",gIR->scopebb()); | 342 sz = llvm::BinaryOperator::createMul(elemsz,e->len,"tmp",gIR->scopebb()); |
344 } | 343 } |
345 } | 344 } |
346 else if (llvm::isa<llvm::ArrayType>(t)) { | 345 else if (isaArray(t)) { |
347 ret = DtoGEPi(e->ptr, 0, 0, "tmp", gIR->scopebb()); | 346 ret = DtoGEPi(e->ptr, 0, 0, "tmp", gIR->scopebb()); |
348 | 347 |
349 size_t elembsz = gTargetData->getTypeSize(ret->getType()->getContainedType(0)); | 348 size_t elembsz = gTargetData->getTypeSize(ret->getType()->getContainedType(0)); |
350 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); | 349 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); |
351 | 350 |
352 size_t numelements = llvm::cast<llvm::ArrayType>(t)->getNumElements(); | 351 size_t numelements = isaArray(t)->getNumElements(); |
353 llvm::ConstantInt* nelems = llvm::ConstantInt::get(DtoSize_t(), numelements, false); | 352 llvm::ConstantInt* nelems = llvm::ConstantInt::get(DtoSize_t(), numelements, false); |
354 | 353 |
355 sz = llvm::ConstantExpr::getMul(elemsz, nelems); | 354 sz = llvm::ConstantExpr::getMul(elemsz, nelems); |
356 } | 355 } |
357 else if (llvm::isa<llvm::StructType>(t)) { | 356 else if (isaStruct(t)) { |
358 ret = DtoGEPi(e->ptr, 0, 1, "tmp", gIR->scopebb()); | 357 ret = DtoGEPi(e->ptr, 0, 1, "tmp", gIR->scopebb()); |
359 ret = new llvm::LoadInst(ret, "tmp", gIR->scopebb()); | 358 ret = new llvm::LoadInst(ret, "tmp", gIR->scopebb()); |
360 | 359 |
361 size_t elembsz = gTargetData->getTypeSize(ret->getType()->getContainedType(0)); | 360 size_t elembsz = gTargetData->getTypeSize(ret->getType()->getContainedType(0)); |
362 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); | 361 llvm::ConstantInt* elemsz = llvm::ConstantInt::get(DtoSize_t(), elembsz, false); |
558 assert(0); | 557 assert(0); |
559 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname); | 558 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname); |
560 assert(fn); | 559 assert(fn); |
561 | 560 |
562 assert(l->getType() == r->getType()); | 561 assert(l->getType() == r->getType()); |
563 assert(llvm::isa<llvm::PointerType>(l->getType())); | 562 assert(isaPointer(l->getType())); |
564 const llvm::Type* arrty = l->getType()->getContainedType(0); | 563 const llvm::Type* arrty = l->getType()->getContainedType(0); |
565 assert(llvm::isa<llvm::ArrayType>(arrty)); | 564 assert(isaArray(arrty)); |
566 | 565 |
567 llvm::Value* ll = new llvm::BitCastInst(l, llvm::PointerType::get(llvm::Type::Int8Ty), "tmp", gIR->scopebb()); | 566 llvm::Value* ll = new llvm::BitCastInst(l, llvm::PointerType::get(llvm::Type::Int8Ty), "tmp", gIR->scopebb()); |
568 llvm::Value* rr = new llvm::BitCastInst(r, llvm::PointerType::get(llvm::Type::Int8Ty), "tmp", gIR->scopebb()); | 567 llvm::Value* rr = new llvm::BitCastInst(r, llvm::PointerType::get(llvm::Type::Int8Ty), "tmp", gIR->scopebb()); |
569 llvm::Value* n = llvm::ConstantInt::get(DtoSize_t(),gTargetData->getTypeSize(arrty),false); | 568 llvm::Value* n = llvm::ConstantInt::get(DtoSize_t(),gTargetData->getTypeSize(arrty),false); |
570 | 569 |
589 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname); | 588 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname); |
590 assert(fn); | 589 assert(fn); |
591 | 590 |
592 Logger::cout() << "lhsType:" << *l->getType() << "\nrhsType:" << *r->getType() << '\n'; | 591 Logger::cout() << "lhsType:" << *l->getType() << "\nrhsType:" << *r->getType() << '\n'; |
593 assert(l->getType() == r->getType()); | 592 assert(l->getType() == r->getType()); |
594 assert(llvm::isa<llvm::PointerType>(l->getType())); | 593 assert(isaPointer(l->getType())); |
595 const llvm::Type* arrty = l->getType()->getContainedType(0); | 594 const llvm::StructType* structType = isaStruct(l->getType()->getContainedType(0)); |
596 assert(llvm::isa<llvm::StructType>(arrty)); | 595 assert(structType); |
597 const llvm::StructType* structType = llvm::cast<llvm::StructType>(arrty); | |
598 const llvm::Type* elemType = structType->getElementType(1)->getContainedType(0); | 596 const llvm::Type* elemType = structType->getElementType(1)->getContainedType(0); |
599 | 597 |
600 std::vector<const llvm::Type*> arrTypes; | 598 std::vector<const llvm::Type*> arrTypes; |
601 arrTypes.push_back(DtoSize_t()); | 599 arrTypes.push_back(DtoSize_t()); |
602 arrTypes.push_back(llvm::PointerType::get(llvm::Type::Int8Ty)); | 600 arrTypes.push_back(llvm::PointerType::get(llvm::Type::Int8Ty)); |
603 const llvm::StructType* arrType = llvm::StructType::get(arrTypes); | 601 const llvm::StructType* arrType = llvm::StructType::get(arrTypes); |
604 | 602 |
605 llvm::Value* llmem = l; | 603 llvm::Value* llmem = l; |
606 llvm::Value* rrmem = r; | 604 llvm::Value* rrmem = r; |
607 | 605 |
608 if (arrty != arrType) { | 606 if (structType != arrType) { |
609 llmem= new llvm::AllocaInst(arrType,"tmparr",gIR->topallocapoint()); | 607 llmem= new llvm::AllocaInst(arrType,"tmparr",gIR->topallocapoint()); |
610 | 608 |
611 llvm::Value* ll = gIR->ir->CreateLoad(DtoGEPi(l, 0,0, "tmp"),"tmp"); | 609 llvm::Value* ll = gIR->ir->CreateLoad(DtoGEPi(l, 0,0, "tmp"),"tmp"); |
612 ll = DtoArrayCastLength(ll, elemType, llvm::Type::Int8Ty); | 610 ll = DtoArrayCastLength(ll, elemType, llvm::Type::Int8Ty); |
613 llvm::Value* lllen = DtoGEPi(llmem, 0,0, "tmp"); | 611 llvm::Value* lllen = DtoGEPi(llmem, 0,0, "tmp"); |
663 llvm::Value* ll = gIR->ir->CreateLoad(DtoGEPi(l, 0,0, "tmp"),"tmp"); | 661 llvm::Value* ll = gIR->ir->CreateLoad(DtoGEPi(l, 0,0, "tmp"),"tmp"); |
664 llvm::Value* rl = DtoConstSize_t(0); | 662 llvm::Value* rl = DtoConstSize_t(0); |
665 llvm::Value* b1 = gIR->ir->CreateICmp(pred,ll,rl,"tmp"); | 663 llvm::Value* b1 = gIR->ir->CreateICmp(pred,ll,rl,"tmp"); |
666 | 664 |
667 llvm::Value* lp = gIR->ir->CreateLoad(DtoGEPi(l, 0,1, "tmp"),"tmp"); | 665 llvm::Value* lp = gIR->ir->CreateLoad(DtoGEPi(l, 0,1, "tmp"),"tmp"); |
668 const llvm::PointerType* pty = llvm::cast<llvm::PointerType>(lp->getType()); | 666 const llvm::PointerType* pty = isaPointer(lp->getType()); |
669 llvm::Value* rp = llvm::ConstantPointerNull::get(pty); | 667 llvm::Value* rp = llvm::ConstantPointerNull::get(pty); |
670 llvm::Value* b2 = gIR->ir->CreateICmp(pred,lp,rp,"tmp"); | 668 llvm::Value* b2 = gIR->ir->CreateICmp(pred,lp,rp,"tmp"); |
671 | 669 |
672 llvm::Value* b = gIR->ir->CreateAnd(b1,b2,"tmp"); | 670 llvm::Value* b = gIR->ir->CreateAnd(b1,b2,"tmp"); |
673 return b; | 671 return b; |
689 } | 687 } |
690 | 688 |
691 ////////////////////////////////////////////////////////////////////////////////////////// | 689 ////////////////////////////////////////////////////////////////////////////////////////// |
692 llvm::Constant* DtoConstStaticArray(const llvm::Type* t, llvm::Constant* c) | 690 llvm::Constant* DtoConstStaticArray(const llvm::Type* t, llvm::Constant* c) |
693 { | 691 { |
694 assert(llvm::isa<llvm::ArrayType>(t)); | 692 const llvm::ArrayType* at = isaArray(t); |
695 const llvm::ArrayType* at = llvm::cast<llvm::ArrayType>(t); | 693 assert(at); |
696 | 694 |
697 if (llvm::isa<llvm::ArrayType>(at->getElementType())) | 695 if (isaArray(at->getElementType())) |
698 { | 696 { |
699 c = DtoConstStaticArray(at->getElementType(), c); | 697 c = DtoConstStaticArray(at->getElementType(), c); |
700 } | 698 } |
701 else { | 699 else { |
702 assert(at->getElementType() == c->getType()); | 700 assert(at->getElementType() == c->getType()); |
719 return DtoLoad(DtoGEPi(v->getRVal(), 0,0, "tmp")); | 717 return DtoLoad(DtoGEPi(v->getRVal(), 0,0, "tmp")); |
720 } | 718 } |
721 else if (t->ty == Tsarray) { | 719 else if (t->ty == Tsarray) { |
722 llvm::Value* rv = v->getRVal(); | 720 llvm::Value* rv = v->getRVal(); |
723 Logger::cout() << "casting: " << *rv << '\n'; | 721 Logger::cout() << "casting: " << *rv << '\n'; |
724 const llvm::ArrayType* t = llvm::cast<llvm::ArrayType>(rv->getType()->getContainedType(0)); | 722 const llvm::ArrayType* t = isaArray(rv->getType()->getContainedType(0)); |
725 return DtoConstSize_t(t->getNumElements()); | 723 return DtoConstSize_t(t->getNumElements()); |
726 } | 724 } |
727 assert(0); | 725 assert(0); |
728 return 0; | 726 return 0; |
729 } | 727 } |