changeset 355:d8357f7004ca trunk

[svn r376] Fix bug with finally blocks and labels. The labels would get emitted multiple times and conflict. It is now possible to add label scopes in IrFunction and all labels names will be prefixed accordingly. Also disallow goto into finally blocks. Fixes nocompile/finally_02 and others.
author ChristianK
date Mon, 14 Jul 2008 11:48:55 +0200
parents ac654d4cb935
children 44daf304421c
files gen/llvmhelpers.cpp gen/statements.cpp ir/irfunction.cpp ir/irfunction.h ir/irlandingpad.cpp
diffstat 5 files changed, 71 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/gen/llvmhelpers.cpp	Mon Jul 14 11:47:03 2008 +0200
+++ b/gen/llvmhelpers.cpp	Mon Jul 14 11:48:55 2008 +0200
@@ -184,10 +184,10 @@
 
     // if the target label is inside inline asm, error
     if(lblstmt->asmLabel)
-        error("cannot goto into inline asm block", loc->toChars());
+        error(*loc, "cannot goto into inline asm block");
 
     // find target basic block
-    std::string labelname = target->toChars();
+    std::string labelname = gIR->func()->getScopedLabelName(target->toChars());
     llvm::BasicBlock*& targetBB = gIR->func()->labelToBB[labelname];
     if (targetBB == NULL)
         targetBB = llvm::BasicBlock::Create("label", gIR->topfunc());
@@ -200,7 +200,12 @@
 
     // error if didn't find tf statement of label
     if(endfinally != lblstmt->enclosinghandler)
-        error("cannot goto into try block", loc->toChars());
+        error(*loc, "cannot goto into try block");
+
+    // goto into finally blocks is forbidden by the spec
+    // though it should not be problematic to implement
+    if(lblstmt->tf)
+        error(*loc, "spec disallows goto into finally block");
 
     // emit code for finallys between goto and label
     DtoEnclosingHandlers(enclosinghandler, endfinally);
@@ -263,12 +268,20 @@
     }
     assert(endfinally == end);
 
+
+    //
     // emit code for finallys between start and end
+    //
+
+    // since the labelstatements possibly inside are private
+    // and might already exist push a label scope
+    gIR->func()->pushUniqueLabelScope("enclosing");
     EnclosingHandler* tf = start;
     while(tf != end) {
         tf->emitCode(gIR);
         tf = tf->getEnclosing();
     }
+    gIR->func()->popLabelScope();
 }
 
 /****************************************************************************************/
--- a/gen/statements.cpp	Mon Jul 14 11:47:03 2008 +0200
+++ b/gen/statements.cpp	Mon Jul 14 11:48:55 2008 +0200
@@ -1033,7 +1033,7 @@
     }
     else
     {
-        std::string labelname = ident->toChars();
+        std::string labelname = p->func()->getScopedLabelName(ident->toChars());
         llvm::BasicBlock*& labelBB = p->func()->labelToBB[labelname];
 
         llvm::BasicBlock* oldend = gIR->scopeend();
--- a/ir/irfunction.cpp	Mon Jul 14 11:47:03 2008 +0200
+++ b/ir/irfunction.cpp	Mon Jul 14 11:48:55 2008 +0200
@@ -3,6 +3,8 @@
 #include "gen/tollvm.h"
 #include "ir/irfunction.h"
 
+#include <sstream>
+
 //////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////
@@ -29,4 +31,31 @@
 
     srcfileArg = NULL;
     msgArg = NULL;
+
+    nextUnique.push(0);
 }
+
+std::string IrFunction::getScopedLabelName(const char* ident)
+{
+    if(labelScopes.empty())
+        return std::string(ident);
+
+    std::string result = "__";
+    for(unsigned int i = 0; i < labelScopes.size(); ++i)
+        result += labelScopes[i] + "_";
+    return result + ident;
+}
+
+void IrFunction::pushUniqueLabelScope(const char* name)
+{
+    std::ostringstream uniquename;
+    uniquename << name << nextUnique.top()++;
+    nextUnique.push(0);
+    labelScopes.push_back(uniquename.str());
+}
+
+void IrFunction::popLabelScope()
+{
+    labelScopes.pop_back();
+    nextUnique.pop();
+}
--- a/ir/irfunction.h	Mon Jul 14 11:47:03 2008 +0200
+++ b/ir/irfunction.h	Mon Jul 14 11:48:55 2008 +0200
@@ -5,6 +5,8 @@
 #include "ir/irlandingpad.h"
 
 #include <vector>
+#include <stack>
+#include <map>
 
 // represents a function
 struct IrFunction : IrBase
@@ -26,6 +28,16 @@
     llvm::AllocaInst* srcfileArg;
     llvm::AllocaInst* msgArg;
 
+    // pushes a unique label scope of the given name
+    void pushUniqueLabelScope(const char* name);
+    // pops a label scope
+    void popLabelScope();
+
+    // gets the string under which the label's BB
+    // is stored in the labelToBB map.
+    // essentially prefixes ident by the strings in labelScopes
+    std::string getScopedLabelName(const char* ident);
+
     // label to basic block lookup
     typedef std::map<std::string, llvm::BasicBlock*> LabelToBBMap;
     LabelToBBMap labelToBB;
@@ -34,6 +46,14 @@
     IRLandingPad landingPad;
 
     IrFunction(FuncDeclaration* fd);
+
+private:
+    // prefix for labels and gotos
+    // used for allowing labels to be emitted twice
+    std::vector<std::string> labelScopes;
+
+    // next unique id stack
+    std::stack<int> nextUnique;
 };
 
 #endif
--- a/ir/irlandingpad.cpp	Mon Jul 14 11:47:03 2008 +0200
+++ b/ir/irlandingpad.cpp	Mon Jul 14 11:48:55 2008 +0200
@@ -150,7 +150,12 @@
         {
             if(switchinst)
                 switchinst = NULL;
+
+            // since this may be emitted multiple times
+            // give the labels a new scope
+            gIR->func()->pushUniqueLabelScope("finally");
             it->finallyBody->toIR(gIR);
+            gIR->func()->popLabelScope();
         }
         // otherwise it's a catch and we'll add a switch case
         else