comparison gen/statements.c @ 45:ff359b65fa62 trunk

[svn r49] foreach on dynamic arrays
author lindquist
date Fri, 19 Oct 2007 16:37:15 +0200
parents 8b0e809563df
children 61bc1b4ad3c4
comparison
equal deleted inserted replaced
44:ea65e12b0dd0 45:ff359b65fa62
649 if (key) key->llvmValue = keyvar; 649 if (key) key->llvmValue = keyvar;
650 650
651 const llvm::Type* valtype = LLVM_DtoType(value->type); 651 const llvm::Type* valtype = LLVM_DtoType(value->type);
652 llvm::Value* valvar = new llvm::AllocaInst(keytype, "foreachval", p->topallocapoint()); 652 llvm::Value* valvar = new llvm::AllocaInst(keytype, "foreachval", p->topallocapoint());
653 653
654 if (LLVM_DtoDType(aggr->type)->ty == Tsarray) 654 Type* aggrtype = LLVM_DtoDType(aggr->type);
655 if (aggrtype->ty == Tsarray)
655 { 656 {
656 assert(llvm::isa<llvm::PointerType>(val->getType())); 657 assert(llvm::isa<llvm::PointerType>(val->getType()));
657 assert(llvm::isa<llvm::ArrayType>(val->getType()->getContainedType(0))); 658 assert(llvm::isa<llvm::ArrayType>(val->getType()->getContainedType(0)));
658 size_t n = llvm::cast<llvm::ArrayType>(val->getType()->getContainedType(0))->getNumElements(); 659 size_t n = llvm::cast<llvm::ArrayType>(val->getType()->getContainedType(0))->getNumElements();
659 assert(n > 0); 660 assert(n > 0);
660 numiters = llvm::ConstantInt::get(keytype,n,false); 661 numiters = llvm::ConstantInt::get(keytype,n,false);
661 } 662 }
663 else if (aggrtype->ty == Tarray)
664 {
665 numiters = p->ir->CreateLoad(LLVM_DtoGEPi(val,0,0,"tmp",p->scopebb()));
666 val = p->ir->CreateLoad(LLVM_DtoGEPi(val,0,1,"tmp",p->scopebb()));
667 }
662 else 668 else
663 { 669 {
664 assert(0); 670 assert(0 && "aggregate type is not Tarray or Tsarray");
665 } 671 }
666 672
667 if (op == TOKforeach) { 673 if (op == TOKforeach) {
668 new llvm::StoreInst(llvm::ConstantInt::get(keytype,0,false), keyvar, p->scopebb()); 674 new llvm::StoreInst(llvm::ConstantInt::get(keytype,0,false), keyvar, p->scopebb());
669 } 675 }
699 705
700 // begin 706 // begin
701 p->scope() = IRScope(begbb,nexbb); 707 p->scope() = IRScope(begbb,nexbb);
702 708
703 // get value for this iteration 709 // get value for this iteration
704 value->llvmValue = LLVM_DtoGEP(val,llvm::ConstantInt::get(keytype,0,false),new llvm::LoadInst(keyvar,"tmp",p->scopebb()),"tmp",p->scopebb()); 710 llvm::Constant* zero = llvm::ConstantInt::get(keytype,0,false);
711 if (aggrtype->ty == Tsarray)
712 value->llvmValue = LLVM_DtoGEP(val,zero,new llvm::LoadInst(keyvar,"tmp",p->scopebb()),"tmp",p->scopebb());
713 else if (aggrtype->ty == Tarray)
714 value->llvmValue = new llvm::GetElementPtrInst(val,new llvm::LoadInst(keyvar,"tmp",p->scopebb()),"tmp",p->scopebb());
705 715
706 // body 716 // body
707 p->scope() = IRScope(p->scopebb(),endbb); 717 p->scope() = IRScope(p->scopebb(),endbb);
708 p->loopbbs.push_back(IRScope(nexbb,endbb)); 718 p->loopbbs.push_back(IRScope(nexbb,endbb));
709 body->toIR(p); 719 body->toIR(p);