Add support for the unwind instruction
authorChris Lattner <sabre@nondot.org>
Mon, 8 Sep 2003 18:54:55 +0000 (18:54 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 8 Sep 2003 18:54:55 +0000 (18:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8408 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AsmParser/Lexer.l
lib/AsmParser/llvmAsmParser.y
lib/Bytecode/Reader/InstructionReader.cpp
lib/Target/CBackend/CBackend.cpp
lib/Target/CBackend/Writer.cpp
lib/Target/X86/InstSelectSimple.cpp
lib/Target/X86/X86ISelSimple.cpp
lib/Transforms/IPO/MutateStructTypes.cpp
lib/Transforms/Scalar/SCCP.cpp

index a1566290a90ead88f3b35e73a322ab2b34e46d36..6e69262c5f27310ca60bf62acbda207511ad0ab0 100644 (file)
@@ -229,6 +229,7 @@ ret             { RET_TOK(TermOpVal, Ret, RET); }
 br              { RET_TOK(TermOpVal, Br, BR); }
 switch          { RET_TOK(TermOpVal, Switch, SWITCH); }
 invoke          { RET_TOK(TermOpVal, Invoke, INVOKE); }
+unwind          { RET_TOK(TermOpVal, Unwind, UNWIND); }
 
 
 malloc          { RET_TOK(MemOpVal, Malloc, MALLOC); }
index 6971ebe2f303b9a69e949b4a3702db6829972fe1..930f1aa4526c1136a447033eaa2f6878feedd8f9 100644 (file)
@@ -701,7 +701,7 @@ Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
 %token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
 
 // Basic Block Terminating Operators 
-%token <TermOpVal> RET BR SWITCH
+%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
 
 // Binary Operators 
 %type  <BinaryOpVal> BinaryOps  // all the binary operators
@@ -714,7 +714,7 @@ Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
 
 // Other Operators
 %type  <OtherOpVal> ShiftOps
-%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR VA_ARG
+%token <OtherOpVal> PHI CALL CAST SHL SHR VA_ARG
 
 %start Module
 %%
@@ -1535,6 +1535,9 @@ BBTerminatorInst : RET ResolvedVal {              // Return with a result...
       $$ = new InvokeInst(V, Normal, Except, *$5);
     }
     delete $5;
+  }
+  | UNWIND {
+    $$ = new UnwindInst();
   };
 
 
index 603c20564caa0b7aeea5a1fab00cebc296af31a0..8f256e4220ea458ebf103436f651b6e8234fc551 100644 (file)
@@ -421,6 +421,9 @@ bool BytecodeParser::ParseInstruction(const unsigned char *&Buf,
     Res = new StoreInst(getValue(ValTy, Raw.Arg1), Ptr, Raw.Opcode == 63);
     return false;
   }
+  case Instruction::Unwind:
+    if (Raw.NumOperands != 0) return true;
+    return new UnwindInst();
   }  // end switch(Raw.Opcode) 
 
   std::cerr << "Unrecognized instruction! " << Raw.Opcode 
index 721ead091c2916eaaf26891c5fe8fd01b6fa7f75..073ce19189f035d761e0a87ca6ef45913abcf98d 100644 (file)
@@ -117,6 +117,7 @@ namespace {
     void visitBranchInst(BranchInst &I);
     void visitSwitchInst(SwitchInst &I);
     void visitInvokeInst(InvokeInst &I);
+    void visitUnwindInst(UnwindInst &I);
 
     void visitPHINode(PHINode &I);
     void visitBinaryOperator(Instruction &I);
@@ -980,6 +981,19 @@ void CWriter::visitInvokeInst(InvokeInst &II) {
 }
 
 
+void CWriter::visitUnwindInst(UnwindInst &I) {
+  // The unwind instructions causes a control flow transfer out of the current
+  // function, unwinding the stack until a caller who used the invoke
+  // instruction is found.  In this context, we code generated the invoke
+  // instruction to add an entry to the top of the jmpbuf_list.  Thus, here we
+  // just have to longjmp to the specified handler.
+  Out << "  if (__llvm_jmpbuf_list == 0) {  /* llvm.unwind */\n"
+      << "    printf(\"throw found with no handler!\\n\"); abort();\n"
+      << "  }\n"
+      << "  longjmp(__llvm_jmpbuf_list->buf, 1);\n";
+  emittedInvoke = true;
+}
+
 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
   // If PHI nodes need copies, we need the copy code...
   if (isa<PHINode>(To->front()) ||
index 721ead091c2916eaaf26891c5fe8fd01b6fa7f75..073ce19189f035d761e0a87ca6ef45913abcf98d 100644 (file)
@@ -117,6 +117,7 @@ namespace {
     void visitBranchInst(BranchInst &I);
     void visitSwitchInst(SwitchInst &I);
     void visitInvokeInst(InvokeInst &I);
+    void visitUnwindInst(UnwindInst &I);
 
     void visitPHINode(PHINode &I);
     void visitBinaryOperator(Instruction &I);
@@ -980,6 +981,19 @@ void CWriter::visitInvokeInst(InvokeInst &II) {
 }
 
 
+void CWriter::visitUnwindInst(UnwindInst &I) {
+  // The unwind instructions causes a control flow transfer out of the current
+  // function, unwinding the stack until a caller who used the invoke
+  // instruction is found.  In this context, we code generated the invoke
+  // instruction to add an entry to the top of the jmpbuf_list.  Thus, here we
+  // just have to longjmp to the specified handler.
+  Out << "  if (__llvm_jmpbuf_list == 0) {  /* llvm.unwind */\n"
+      << "    printf(\"throw found with no handler!\\n\"); abort();\n"
+      << "  }\n"
+      << "  longjmp(__llvm_jmpbuf_list->buf, 1);\n";
+  emittedInvoke = true;
+}
+
 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
   // If PHI nodes need copies, we need the copy code...
   if (isa<PHINode>(To->front()) ||
index f754d11066f4493695c3e82e9dff3473cc617262..6712d94f32460b9bbbf14daea15f5ad6d73ad72b 100644 (file)
@@ -132,6 +132,7 @@ namespace {
                const std::vector<ValueRecord> &Args);
     void visitCallInst(CallInst &I);
     void visitInvokeInst(InvokeInst &II);
+    void visitUnwindInst(UnwindInst &UI);
     void visitIntrinsicCall(LLVMIntrinsic::ID ID, CallInst &I);
 
     // Arithmetic operators
@@ -1020,6 +1021,10 @@ void ISel::visitInvokeInst(InvokeInst &II) {
     BuildMI(BB, X86::JMP, 1).addPCDisp(II.getNormalDest());
 }
 
+void ISel::visitUnwindInst(UnwindInst &UI) {
+  // unwind is not supported yet!  Just abort when the unwind inst is executed!
+  BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("abort", true); 
+}
 
 void ISel::visitIntrinsicCall(LLVMIntrinsic::ID ID, CallInst &CI) {
   unsigned TmpReg1, TmpReg2;
index f754d11066f4493695c3e82e9dff3473cc617262..6712d94f32460b9bbbf14daea15f5ad6d73ad72b 100644 (file)
@@ -132,6 +132,7 @@ namespace {
                const std::vector<ValueRecord> &Args);
     void visitCallInst(CallInst &I);
     void visitInvokeInst(InvokeInst &II);
+    void visitUnwindInst(UnwindInst &UI);
     void visitIntrinsicCall(LLVMIntrinsic::ID ID, CallInst &I);
 
     // Arithmetic operators
@@ -1020,6 +1021,10 @@ void ISel::visitInvokeInst(InvokeInst &II) {
     BuildMI(BB, X86::JMP, 1).addPCDisp(II.getNormalDest());
 }
 
+void ISel::visitUnwindInst(UnwindInst &UI) {
+  // unwind is not supported yet!  Just abort when the unwind inst is executed!
+  BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("abort", true); 
+}
 
 void ISel::visitIntrinsicCall(LLVMIntrinsic::ID ID, CallInst &CI) {
   unsigned TmpReg1, TmpReg2;
index 370b6be8940cae0c928b9acea93b4245c07dd08d..2ea6938be25647638f8b8ae30955b4fe3dfaadf2 100644 (file)
@@ -352,6 +352,7 @@ void MutateStructTypes::transformFunction(Function *m) {
       }
       case Instruction::Switch:
       case Instruction::Invoke:
+      case Instruction::Unwind:
         assert(0 && "Insn not implemented!");
 
         // Binary Instructions
index 5938c2a5851cb7586a7f42921a6c31241ecb581e..b9aba13915d236b1f9bb0f1bb574f8c362fda7d0 100644 (file)
@@ -196,6 +196,7 @@ private:
     markOverdefined(&I);
     visitTerminatorInst(I);
   }
+  void visitUnwindInst    (TerminatorInst &I) { /*returns void*/ }
   void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
   void visitVarArgInst    (Instruction &I) { markOverdefined(&I); }
   void visitFreeInst      (Instruction &I) { /*returns void*/ }