changeset 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 a9dae3da4e87
files gen/statements.cpp
diffstat 1 files changed, 21 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/gen/statements.cpp	Sat Jun 14 05:13:49 2008 +0200
+++ b/gen/statements.cpp	Sat Jun 14 06:13:35 2008 +0200
@@ -651,6 +651,14 @@
 
     llvm::BasicBlock* oldend = gIR->scopeend();
 
+    // clear data from previous passes... :/
+    for (int i=0; i<cases->dim; ++i)
+    {
+        CaseStatement* cs = (CaseStatement*)cases->data[i];
+        cs->bodyBB = NULL;
+        cs->llvmIdx = NULL;
+    }
+
     // string switch?
     llvm::GlobalVariable* switchTable = 0;
     Array caseArray;
@@ -752,17 +760,15 @@
     Logger::println("CaseStatement::toIR(): %s", loc.toChars());
     LOG_SCOPE;
 
-    if (!bodyBB)
+    llvm::BasicBlock* nbb = llvm::BasicBlock::Create("case", p->topfunc(), p->scopeend());
+
+    if (bodyBB && !bodyBB->getTerminator())
     {
-        bodyBB = llvm::BasicBlock::Create("case", p->topfunc(), p->scopeend());
+        llvm::BranchInst::Create(nbb, bodyBB);
     }
-    else
-    {
-        bodyBB->moveAfter(p->scopebb());
-    }
+    bodyBB = nbb;
 
     if (exp->type->isintegral()) {
-        assert(!llvmIdx);
         LLConstant* c = exp->toConstElem(p);
         llvmIdx = isaConstantInt(c);
     }
@@ -787,7 +793,13 @@
 
     assert(bodyBB);
 
-    bodyBB->moveAfter(p->scopebb());
+    llvm::BasicBlock* nbb = llvm::BasicBlock::Create("default", p->topfunc(), p->scopeend());
+
+    if (!bodyBB->getTerminator())
+    {
+        llvm::BranchInst::Create(nbb, bodyBB);
+    }
+    bodyBB = nbb;
 
     if (!p->scopereturned())
         llvm::BranchInst::Create(bodyBB, p->scopebb());
@@ -1088,7 +1100,7 @@
     assert(!p->scopereturned());
     if (!cs->bodyBB)
     {
-        cs->bodyBB = llvm::BasicBlock::Create("case", p->topfunc(), p->scopeend());
+        cs->bodyBB = llvm::BasicBlock::Create("goto_case", p->topfunc(), p->scopeend());
     }
 
     emit_finallyblocks(p, enclosingtryfinally, sw->enclosingtryfinally);