comparison gen/statements.cpp @ 263:2be09ee06bc7 trunk

[svn r284] Fixed some problems with cases, code is not as pretty, but it doesn't crash llvm!
author lindquist
date Sat, 14 Jun 2008 06:13:35 +0200
parents 88252a1af660
children c43911baea21
comparison
equal deleted inserted replaced
262:88252a1af660 263:2be09ee06bc7
648 { 648 {
649 Logger::println("SwitchStatement::toIR(): %s", loc.toChars()); 649 Logger::println("SwitchStatement::toIR(): %s", loc.toChars());
650 LOG_SCOPE; 650 LOG_SCOPE;
651 651
652 llvm::BasicBlock* oldend = gIR->scopeend(); 652 llvm::BasicBlock* oldend = gIR->scopeend();
653
654 // clear data from previous passes... :/
655 for (int i=0; i<cases->dim; ++i)
656 {
657 CaseStatement* cs = (CaseStatement*)cases->data[i];
658 cs->bodyBB = NULL;
659 cs->llvmIdx = NULL;
660 }
653 661
654 // string switch? 662 // string switch?
655 llvm::GlobalVariable* switchTable = 0; 663 llvm::GlobalVariable* switchTable = 0;
656 Array caseArray; 664 Array caseArray;
657 if (!condition->type->isintegral()) 665 if (!condition->type->isintegral())
750 void CaseStatement::toIR(IRState* p) 758 void CaseStatement::toIR(IRState* p)
751 { 759 {
752 Logger::println("CaseStatement::toIR(): %s", loc.toChars()); 760 Logger::println("CaseStatement::toIR(): %s", loc.toChars());
753 LOG_SCOPE; 761 LOG_SCOPE;
754 762
755 if (!bodyBB) 763 llvm::BasicBlock* nbb = llvm::BasicBlock::Create("case", p->topfunc(), p->scopeend());
756 { 764
757 bodyBB = llvm::BasicBlock::Create("case", p->topfunc(), p->scopeend()); 765 if (bodyBB && !bodyBB->getTerminator())
758 } 766 {
759 else 767 llvm::BranchInst::Create(nbb, bodyBB);
760 { 768 }
761 bodyBB->moveAfter(p->scopebb()); 769 bodyBB = nbb;
762 }
763 770
764 if (exp->type->isintegral()) { 771 if (exp->type->isintegral()) {
765 assert(!llvmIdx);
766 LLConstant* c = exp->toConstElem(p); 772 LLConstant* c = exp->toConstElem(p);
767 llvmIdx = isaConstantInt(c); 773 llvmIdx = isaConstantInt(c);
768 } 774 }
769 else { 775 else {
770 assert(llvmIdx); 776 assert(llvmIdx);
785 Logger::println("DefaultStatement::toIR(): %s", loc.toChars()); 791 Logger::println("DefaultStatement::toIR(): %s", loc.toChars());
786 LOG_SCOPE; 792 LOG_SCOPE;
787 793
788 assert(bodyBB); 794 assert(bodyBB);
789 795
790 bodyBB->moveAfter(p->scopebb()); 796 llvm::BasicBlock* nbb = llvm::BasicBlock::Create("default", p->topfunc(), p->scopeend());
797
798 if (!bodyBB->getTerminator())
799 {
800 llvm::BranchInst::Create(nbb, bodyBB);
801 }
802 bodyBB = nbb;
791 803
792 if (!p->scopereturned()) 804 if (!p->scopereturned())
793 llvm::BranchInst::Create(bodyBB, p->scopebb()); 805 llvm::BranchInst::Create(bodyBB, p->scopebb());
794 806
795 p->scope() = IRScope(bodyBB, p->scopeend()); 807 p->scope() = IRScope(bodyBB, p->scopeend());
1086 llvm::BasicBlock* bb = llvm::BasicBlock::Create("aftergotocase", p->topfunc(), oldend); 1098 llvm::BasicBlock* bb = llvm::BasicBlock::Create("aftergotocase", p->topfunc(), oldend);
1087 1099
1088 assert(!p->scopereturned()); 1100 assert(!p->scopereturned());
1089 if (!cs->bodyBB) 1101 if (!cs->bodyBB)
1090 { 1102 {
1091 cs->bodyBB = llvm::BasicBlock::Create("case", p->topfunc(), p->scopeend()); 1103 cs->bodyBB = llvm::BasicBlock::Create("goto_case", p->topfunc(), p->scopeend());
1092 } 1104 }
1093 1105
1094 emit_finallyblocks(p, enclosingtryfinally, sw->enclosingtryfinally); 1106 emit_finallyblocks(p, enclosingtryfinally, sw->enclosingtryfinally);
1095 1107
1096 llvm::BranchInst::Create(cs->bodyBB, p->scopebb()); 1108 llvm::BranchInst::Create(cs->bodyBB, p->scopebb());