Mercurial > projects > ldc
comparison gen/arrays.cpp @ 116:fd7ad91fd713 trunk
[svn r120] ModuleInfo implementation is now almost complete.
Fixed some nasty static array-initializer bugs.
Fixed bug in DtoArrayLen and DtoArrayPtr for full slices of static arrays.
author | lindquist |
---|---|
date | Sun, 25 Nov 2007 18:55:52 +0100 |
parents | 5ab8e92611f9 |
children | d580b95cce2b |
comparison
equal
deleted
inserted
replaced
115:5ba6d286c941 | 116:fd7ad91fd713 |
---|---|
283 | 283 |
284 Logger::println("dim = %u", tdim); | 284 Logger::println("dim = %u", tdim); |
285 | 285 |
286 std::vector<llvm::Constant*> inits(tdim, NULL); | 286 std::vector<llvm::Constant*> inits(tdim, NULL); |
287 | 287 |
288 Type* arrnext = arrinittype->next; | |
288 const llvm::Type* elemty = DtoType(arrinittype->next); | 289 const llvm::Type* elemty = DtoType(arrinittype->next); |
289 | 290 |
290 assert(arrinit->index.dim == arrinit->value.dim); | 291 assert(arrinit->index.dim == arrinit->value.dim); |
291 for (unsigned i=0,j=0; i < tdim; ++i) | 292 for (unsigned i=0,j=0; i < tdim; ++i) |
292 { | 293 { |
293 Initializer* init = 0; | 294 Initializer* init = 0; |
294 Expression* idx = (Expression*)arrinit->index.data[j]; | 295 Expression* idx; |
296 | |
297 if (j < arrinit->index.dim) | |
298 idx = (Expression*)arrinit->index.data[j]; | |
299 else | |
300 idx = NULL; | |
301 | |
302 llvm::Constant* v = NULL; | |
295 | 303 |
296 if (idx) | 304 if (idx) |
297 { | 305 { |
306 Logger::println("%d has idx", i); | |
298 // this is pretty weird :/ idx->type turned out NULL for the initializer: | 307 // this is pretty weird :/ idx->type turned out NULL for the initializer: |
299 // const in6_addr IN6ADDR_ANY = { s6_addr8: [0] }; | 308 // const in6_addr IN6ADDR_ANY = { s6_addr8: [0] }; |
300 // in std.c.linux.socket | 309 // in std.c.linux.socket |
301 if (idx->type) { | 310 if (idx->type) { |
311 Logger::println("has idx->type", i); | |
302 //integer_t k = idx->toInteger(); | 312 //integer_t k = idx->toInteger(); |
303 Logger::println("getting value for exp: %s | %s", idx->toChars(), idx->type->toChars()); | 313 //Logger::println("getting value for exp: %s | %s", idx->toChars(), arrnext->toChars()); |
304 llvm::Constant* cc = idx->toConstElem(gIR); | 314 llvm::Constant* cc = idx->toConstElem(gIR); |
305 Logger::println("value gotten"); | 315 Logger::println("value gotten"); |
306 assert(cc != NULL); | 316 assert(cc != NULL); |
307 llvm::ConstantInt* ci = llvm::dyn_cast<llvm::ConstantInt>(cc); | 317 llvm::ConstantInt* ci = llvm::dyn_cast<llvm::ConstantInt>(cc); |
308 assert(ci != NULL); | 318 assert(ci != NULL); |
315 } | 325 } |
316 } | 326 } |
317 } | 327 } |
318 else | 328 else |
319 { | 329 { |
320 init = (Initializer*)arrinit->value.data[j]; | 330 if (j < arrinit->value.dim) { |
321 ++j; | 331 init = (Initializer*)arrinit->value.data[j]; |
322 } | 332 ++j; |
323 | 333 } |
324 llvm::Constant* v = DtoConstInitializer(t->next, init); | 334 else |
335 v = arrnext->defaultInit()->toConstElem(gIR); | |
336 } | |
337 | |
338 if (!v) | |
339 v = DtoConstInitializer(t->next, init); | |
325 assert(v); | 340 assert(v); |
326 | 341 |
327 inits[i] = v; | 342 inits[i] = v; |
328 } | 343 } |
329 | 344 |
782 } | 797 } |
783 | 798 |
784 ////////////////////////////////////////////////////////////////////////////////////////// | 799 ////////////////////////////////////////////////////////////////////////////////////////// |
785 llvm::Value* DtoArrayLen(DValue* v) | 800 llvm::Value* DtoArrayLen(DValue* v) |
786 { | 801 { |
802 Logger::println("DtoArrayLen"); | |
803 LOG_SCOPE; | |
787 Type* t = DtoDType(v->getType()); | 804 Type* t = DtoDType(v->getType()); |
788 if (t->ty == Tarray) { | 805 if (t->ty == Tarray) { |
789 if (DSliceValue* s = v->isSlice()) { | 806 if (DSliceValue* s = v->isSlice()) { |
790 if (s->len) return s->len; | 807 if (s->len) { |
791 DValue* next = new DVarValue(t,s->ptr,true); | 808 return s->len; |
792 return DtoArrayLen(next); | 809 } |
810 const llvm::ArrayType* arrTy = isaArray(s->ptr->getType()->getContainedType(0)); | |
811 assert(arrTy); | |
812 return DtoConstSize_t(arrTy->getNumElements()); | |
793 } | 813 } |
794 return DtoLoad(DtoGEPi(v->getRVal(), 0,0, "tmp")); | 814 return DtoLoad(DtoGEPi(v->getRVal(), 0,0, "tmp")); |
795 } | 815 } |
796 else if (t->ty == Tsarray) { | 816 else if (t->ty == Tsarray) { |
817 assert(!v->isSlice()); | |
797 llvm::Value* rv = v->getRVal(); | 818 llvm::Value* rv = v->getRVal(); |
798 Logger::cout() << "casting: " << *rv << '\n'; | 819 Logger::cout() << "casting: " << *rv << '\n'; |
799 const llvm::ArrayType* t = isaArray(rv->getType()->getContainedType(0)); | 820 const llvm::ArrayType* t = isaArray(rv->getType()->getContainedType(0)); |
800 return DtoConstSize_t(t->getNumElements()); | 821 return DtoConstSize_t(t->getNumElements()); |
801 } | 822 } |
808 { | 829 { |
809 Type* t = DtoDType(v->getType()); | 830 Type* t = DtoDType(v->getType()); |
810 if (t->ty == Tarray) { | 831 if (t->ty == Tarray) { |
811 if (DSliceValue* s = v->isSlice()) { | 832 if (DSliceValue* s = v->isSlice()) { |
812 if (s->len) return s->ptr; | 833 if (s->len) return s->ptr; |
813 DValue* next = new DVarValue(t,s->ptr,true); | 834 const llvm::ArrayType* arrTy = isaArray(s->ptr->getType()->getContainedType(0)); |
814 return DtoArrayPtr(next); | 835 assert(arrTy); |
836 return DtoGEPi(s->ptr, 0,0, "tmp"); | |
815 } | 837 } |
816 return DtoLoad(DtoGEPi(v->getRVal(), 0,1, "tmp")); | 838 return DtoLoad(DtoGEPi(v->getRVal(), 0,1, "tmp")); |
817 } | 839 } |
818 else if (t->ty == Tsarray) { | 840 else if (t->ty == Tsarray) { |
819 return DtoGEPi(v->getRVal(), 0,0, "tmp"); | 841 return DtoGEPi(v->getRVal(), 0,0, "tmp"); |