[SEH] Update SEH codegen tests to use the new IR
[oota-llvm.git] / lib / CodeGen / AsmPrinter / WinException.cpp
index 4f1d5cb864a7a55b350a8ddd5cd033df424c3fbe..63be0daa708e77a47b6f3e2cde970fa6ef637fb0 100644 (file)
@@ -38,6 +38,7 @@
 #include "llvm/Target/TargetLoweringObjectFile.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Target/TargetRegisterInfo.h"
+#include "llvm/Target/TargetSubtargetInfo.h"
 using namespace llvm;
 
 WinException::WinException(AsmPrinter *A) : EHStreamer(A) {
@@ -292,6 +293,14 @@ const MCExpr *WinException::getLabelPlusOne(MCSymbol *Label) {
                                  Asm->OutContext);
 }
 
+int WinException::getFrameIndexOffset(int FrameIndex) {
+  const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering();
+  unsigned UnusedReg;
+  if (Asm->MAI->usesWindowsCFI())
+    return TFI.getFrameIndexReferenceFromSP(*Asm->MF, FrameIndex, UnusedReg);
+  return TFI.getFrameIndexReference(*Asm->MF, FrameIndex, UnusedReg);
+}
+
 namespace {
 /// Information describing an invoke range.
 struct InvokeRange {
@@ -409,7 +418,7 @@ invoke_ranges(WinEHFuncInfo &EHInfo, const MachineBasicBlock &MBB) {
 ///       imagerel32 LabelStart;
 ///       imagerel32 LabelEnd;
 ///       imagerel32 FilterOrFinally;  // One means catch-all.
-///       imagerel32 LabelLPad;        // Zero means __finally.
+///       imagerel32 ExceptOrNull;     // Zero means __finally.
 ///     } Entries[NumEntries];
 ///   };
 void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
@@ -449,6 +458,11 @@ void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
     // the actions that would be taken in that state. This means our tables are
     // slightly bigger, which is OK.
     for (const auto &MBB : *MF) {
+      // Break out before we enter into a finally funclet.
+      // FIXME: We need to emit separate EH tables for cleanups.
+      if (MBB.isEHFuncletEntry() && &MBB != MF->begin())
+        break;
+
       for (InvokeRange &I : invoke_ranges(FuncInfo, MBB)) {
         // If this invoke is in the same state as the last invoke and there were
         // no non-throwing calls between it, extend the range to include both
@@ -460,38 +474,20 @@ void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
 
         // If this invoke ends a previous one, emit all the actions for this
         // state.
-        if (LastEHState != -1) {
-          assert(LastBeginLabel && LastEndLabel);
-          for (int State = LastEHState; State != -1;) {
-            SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
-            const MCExpr *FilterOrFinally;
-            const MCExpr *ExceptOrNull;
-            auto *Handler = UME.Handler.get<MachineBasicBlock *>();
-            if (UME.IsFinally) {
-              FilterOrFinally = create32bitRef(Handler->getSymbol());
-              ExceptOrNull = MCConstantExpr::create(0, Ctx);
-            } else {
-              // For an except, the filter can be 1 (catch-all) or a function
-              // label.
-              FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
-                                           : MCConstantExpr::create(1, Ctx);
-              ExceptOrNull = create32bitRef(Handler->getSymbol());
-            }
-
-            OS.EmitValue(getLabelPlusOne(LastBeginLabel), 4);
-            OS.EmitValue(getLabelPlusOne(LastEndLabel), 4);
-            OS.EmitValue(FilterOrFinally, 4);
-            OS.EmitValue(ExceptOrNull, 4);
-
-            State = UME.ToState;
-          }
-        }
+        if (LastEHState != -1)
+          emitSEHActionsForRange(FuncInfo, LastBeginLabel, LastEndLabel,
+                                 LastEHState);
 
         LastBeginLabel = I.BeginLabel;
         LastEndLabel = I.EndLabel;
         LastEHState = I.State;
       }
     }
+
+    if (LastEndLabel)
+      emitSEHActionsForRange(FuncInfo, LastBeginLabel, LastEndLabel,
+                             LastEHState);
+
     OS.EmitLabel(TableEnd);
     return;
   }
@@ -579,6 +575,45 @@ void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
   }
 }
 
+void WinException::emitSEHActionsForRange(WinEHFuncInfo &FuncInfo,
+                                          MCSymbol *BeginLabel,
+                                          MCSymbol *EndLabel, int State) {
+  auto &OS = *Asm->OutStreamer;
+  MCContext &Ctx = Asm->OutContext;
+
+  assert(BeginLabel && EndLabel);
+  while (State != -1) {
+    // struct Entry {
+    //   imagerel32 LabelStart;
+    //   imagerel32 LabelEnd;
+    //   imagerel32 FilterOrFinally;  // One means catch-all.
+    //   imagerel32 ExceptOrNull;     // Zero means __finally.
+    // };
+    SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
+    const MCExpr *FilterOrFinally;
+    const MCExpr *ExceptOrNull;
+    auto *Handler = UME.Handler.get<MachineBasicBlock *>();
+    if (UME.IsFinally) {
+      FilterOrFinally = create32bitRef(getMCSymbolForMBBOrGV(Asm, Handler));
+      ExceptOrNull = MCConstantExpr::create(0, Ctx);
+    } else {
+      // For an except, the filter can be 1 (catch-all) or a function
+      // label.
+      FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
+                                   : MCConstantExpr::create(1, Ctx);
+      ExceptOrNull = create32bitRef(Handler->getSymbol());
+    }
+
+    OS.EmitValue(getLabelPlusOne(BeginLabel), 4);
+    OS.EmitValue(getLabelPlusOne(EndLabel), 4);
+    OS.EmitValue(FilterOrFinally, 4);
+    OS.EmitValue(ExceptOrNull, 4);
+
+    assert(UME.ToState < State && "states should decrease");
+    State = UME.ToState;
+  }
+}
+
 void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
   const Function *F = MF->getFunction();
   auto &OS = *Asm->OutStreamer;
@@ -599,6 +634,10 @@ void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
     emitEHRegistrationOffsetLabel(FuncInfo, FuncLinkageName);
   }
 
+  int UnwindHelpOffset = 0;
+  if (Asm->MAI->usesWindowsCFI())
+    UnwindHelpOffset = getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx);
+
   MCSymbol *UnwindMapXData = nullptr;
   MCSymbol *TryBlockMapXData = nullptr;
   MCSymbol *IPToStateXData = nullptr;
@@ -637,7 +676,7 @@ void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
   OS.EmitIntValue(IPToStateTable.size(), 4);           // IPMapEntries
   OS.EmitValue(create32bitRef(IPToStateXData), 4);     // IPToStateMap
   if (Asm->MAI->usesWindowsCFI())
-    OS.EmitIntValue(FuncInfo.UnwindHelpFrameOffset, 4); // UnwindHelp
+    OS.EmitIntValue(UnwindHelpOffset, 4);              // UnwindHelp
   OS.EmitIntValue(0, 4);                               // ESTypeList
   OS.EmitIntValue(1, 4);                               // EHFlags
 
@@ -714,8 +753,8 @@ void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
                   FuncLinkageName, HT.CatchObjRecoverIdx);
           FrameAllocOffsetRef = MCSymbolRefExpr::create(
               FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext);
-        } else if (HT.CatchObj.FrameOffset != INT_MAX) {
-          int Offset = HT.CatchObj.FrameOffset;
+        } else if (HT.CatchObj.FrameIndex != INT_MAX) {
+          int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex);
           // For 32-bit, the catch object offset is relative to the end of the
           // EH registration node. For 64-bit, it's relative to SP at the end of
           // the prologue.
@@ -791,7 +830,7 @@ void WinException::computeIP2StateTable(
       // Emit an entry indicating that PCs after 'Label' have this EH state.
       if (I.State != LastEHState)
         IPToStateTable.push_back(
-            std::make_pair(create32bitRef(I.BeginLabel), I.State));
+            std::make_pair(getLabelPlusOne(I.BeginLabel), I.State));
       LastEHState = I.State;
       LastEndLabel = I.EndLabel;
     }
@@ -868,7 +907,10 @@ void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
     for (SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
       MCSymbol *ExceptOrFinally =
           UME.Handler.get<MachineBasicBlock *>()->getSymbol();
-      OS.EmitIntValue(UME.ToState, 4);                  // ToState
+      // -1 is usually the base state for "unwind to caller", but for
+      // _except_handler4 it's -2. Do that replacement here if necessary.
+      int ToState = UME.ToState == -1 ? BaseState : UME.ToState;
+      OS.EmitIntValue(ToState, 4);                      // ToState
       OS.EmitValue(create32bitRef(UME.Filter), 4);      // Filter
       OS.EmitValue(create32bitRef(ExceptOrFinally), 4); // Except/Finally
     }