Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 1 | //===-- CodeGen/AsmPrinter/WinException.cpp - Dwarf Exception Impl ------===// |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for writing Win64 exception info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 14 | #include "WinException.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/ADT/Twine.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Module.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCAsmInfo.h" |
| 27 | #include "llvm/MC/MCContext.h" |
| 28 | #include "llvm/MC/MCExpr.h" |
| 29 | #include "llvm/MC/MCSection.h" |
| 30 | #include "llvm/MC/MCStreamer.h" |
| 31 | #include "llvm/MC/MCSymbol.h" |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCWin64EH.h" |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 33 | #include "llvm/Support/COFF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Dwarf.h" |
| 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/FormattedStream.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetFrameLowering.h" |
| 38 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetOptions.h" |
| 40 | #include "llvm/Target/TargetRegisterInfo.h" |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame^] | 41 | #include "llvm/Target/TargetSubtargetInfo.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 44 | WinException::WinException(AsmPrinter *A) : EHStreamer(A) { |
| 45 | // MSVC's EH tables are always composed of 32-bit words. All known 64-bit |
| 46 | // platforms use an imagerel32 relocation to refer to symbols. |
| 47 | useImageRel32 = (A->getDataLayout().getPointerSizeInBits() == 64); |
| 48 | } |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 49 | |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 50 | WinException::~WinException() {} |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 51 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 52 | /// endModule - Emit all exception information that should come after the |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 53 | /// content. |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 54 | void WinException::endModule() { |
Reid Kleckner | 2bc93ca | 2015-06-10 01:02:30 +0000 | [diff] [blame] | 55 | auto &OS = *Asm->OutStreamer; |
| 56 | const Module *M = MMI->getModule(); |
Reid Kleckner | ca6ef66 | 2015-06-10 01:13:44 +0000 | [diff] [blame] | 57 | for (const Function &F : *M) |
| 58 | if (F.hasFnAttribute("safeseh")) |
Reid Kleckner | 2bc93ca | 2015-06-10 01:02:30 +0000 | [diff] [blame] | 59 | OS.EmitCOFFSafeSEH(Asm->getSymbol(&F)); |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 62 | void WinException::beginFunction(const MachineFunction *MF) { |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 63 | shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false; |
| 64 | |
| 65 | // If any landing pads survive, we need an EH table. |
| 66 | bool hasLandingPads = !MMI->getLandingPads().empty(); |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 67 | bool hasEHFunclets = MMI->hasEHFunclets(); |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 68 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 69 | const Function *F = MF->getFunction(); |
| 70 | const Function *ParentF = MMI->getWinEHParent(F); |
| 71 | |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 72 | shouldEmitMoves = Asm->needsSEHMoves(); |
| 73 | |
| 74 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 75 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
Reid Kleckner | 9a1a919 | 2015-07-13 20:41:46 +0000 | [diff] [blame] | 76 | const Function *Per = nullptr; |
| 77 | if (F->hasPersonalityFn()) |
| 78 | Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 79 | |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 80 | bool forceEmitPersonality = |
| 81 | F->hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) && |
| 82 | F->needsUnwindTableEntry(); |
| 83 | |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 84 | shouldEmitPersonality = |
| 85 | forceEmitPersonality || ((hasLandingPads || hasEHFunclets) && |
| 86 | PerEncoding != dwarf::DW_EH_PE_omit && Per); |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 87 | |
| 88 | unsigned LSDAEncoding = TLOF.getLSDAEncoding(); |
| 89 | shouldEmitLSDA = shouldEmitPersonality && |
| 90 | LSDAEncoding != dwarf::DW_EH_PE_omit; |
| 91 | |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 92 | // If we're not using CFI, we don't want the CFI or the personality, but we |
| 93 | // might want EH tables if we had EH pads. |
| 94 | // FIXME: If WinEHPrepare outlined something, we should emit the LSDA. Remove |
| 95 | // this once WinEHPrepare stops doing that. |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 96 | if (!Asm->MAI->usesWindowsCFI()) { |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 97 | shouldEmitLSDA = |
| 98 | hasEHFunclets || (F->hasFnAttribute("wineh-parent") && F == ParentF); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 99 | shouldEmitPersonality = false; |
| 100 | return; |
| 101 | } |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 102 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 103 | beginFunclet(MF->front(), Asm->CurrentFnSym); |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 106 | /// endFunction - Gather and emit post-function exception information. |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 107 | /// |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 108 | void WinException::endFunction(const MachineFunction *MF) { |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 109 | if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA) |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 110 | return; |
| 111 | |
Reid Kleckner | 9a1a919 | 2015-07-13 20:41:46 +0000 | [diff] [blame] | 112 | const Function *F = MF->getFunction(); |
| 113 | EHPersonality Per = EHPersonality::Unknown; |
| 114 | if (F->hasPersonalityFn()) |
| 115 | Per = classifyEHPersonality(F->getPersonalityFn()); |
Reid Kleckner | 3e9fadf | 2015-04-15 18:48:15 +0000 | [diff] [blame] | 116 | |
Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 117 | // Get rid of any dead landing pads if we're not using funclets. In funclet |
| 118 | // schemes, the landing pad is not actually reachable. It only exists so |
| 119 | // that we can emit the right table data. |
| 120 | if (!isFuncletEHPersonality(Per)) |
Reid Kleckner | 3e9fadf | 2015-04-15 18:48:15 +0000 | [diff] [blame] | 121 | MMI->TidyLandingPads(); |
Charles Davis | a575226 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 122 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 123 | endFunclet(); |
| 124 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 125 | // endFunclet will emit the necessary .xdata tables for x64 SEH. |
| 126 | if (Per == EHPersonality::MSVC_Win64SEH && MMI->hasEHFunclets()) |
| 127 | return; |
| 128 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 129 | if (shouldEmitPersonality || shouldEmitLSDA) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 130 | Asm->OutStreamer->PushSection(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 131 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 132 | // Just switch sections to the right xdata section. This use of CurrentFnSym |
| 133 | // assumes that we only emit the LSDA when ending the parent function. |
| 134 | MCSection *XData = WinEH::UnwindEmitter::getXDataSection(Asm->CurrentFnSym, |
| 135 | Asm->OutContext); |
| 136 | Asm->OutStreamer->SwitchSection(XData); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 137 | |
Reid Kleckner | 9b5eaf0 | 2015-01-14 18:50:10 +0000 | [diff] [blame] | 138 | // Emit the tables appropriate to the personality function in use. If we |
| 139 | // don't recognize the personality, assume it uses an Itanium-style LSDA. |
Reid Kleckner | 2d5fb68 | 2015-02-14 00:21:02 +0000 | [diff] [blame] | 140 | if (Per == EHPersonality::MSVC_Win64SEH) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 141 | emitCSpecificHandlerTable(MF); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 142 | else if (Per == EHPersonality::MSVC_X86SEH) |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 143 | emitExceptHandlerTable(MF); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 144 | else if (Per == EHPersonality::MSVC_CXX) |
| 145 | emitCXXFrameHandler3Table(MF); |
Reid Kleckner | 9b5eaf0 | 2015-01-14 18:50:10 +0000 | [diff] [blame] | 146 | else |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 147 | emitExceptionTable(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 148 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 149 | Asm->OutStreamer->PopSection(); |
Charles Davis | a575226 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 150 | } |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 151 | } |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 152 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 153 | /// Retreive the MCSymbol for a GlobalValue or MachineBasicBlock. GlobalValues |
| 154 | /// are used in the old WinEH scheme, and they will be removed eventually. |
| 155 | static MCSymbol *getMCSymbolForMBBOrGV(AsmPrinter *Asm, ValueOrMBB Handler) { |
| 156 | if (!Handler) |
| 157 | return nullptr; |
| 158 | if (Handler.is<const MachineBasicBlock *>()) { |
| 159 | auto *MBB = Handler.get<const MachineBasicBlock *>(); |
| 160 | assert(MBB->isEHFuncletEntry()); |
| 161 | |
| 162 | // Give catches and cleanups a name based off of their parent function and |
| 163 | // their funclet entry block's number. |
| 164 | const MachineFunction *MF = MBB->getParent(); |
| 165 | const Function *F = MF->getFunction(); |
| 166 | StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
| 167 | MCContext &Ctx = MF->getContext(); |
| 168 | StringRef HandlerPrefix = MBB->isCleanupFuncletEntry() ? "dtor" : "catch"; |
| 169 | return Ctx.getOrCreateSymbol("?" + HandlerPrefix + "$" + |
| 170 | Twine(MBB->getNumber()) + "@?0?" + |
| 171 | FuncLinkageName + "@4HA"); |
| 172 | } |
| 173 | return Asm->getSymbol(cast<GlobalValue>(Handler.get<const Value *>())); |
| 174 | } |
| 175 | |
| 176 | void WinException::beginFunclet(const MachineBasicBlock &MBB, |
| 177 | MCSymbol *Sym) { |
| 178 | CurrentFuncletEntry = &MBB; |
| 179 | |
| 180 | const Function *F = Asm->MF->getFunction(); |
| 181 | // If a symbol was not provided for the funclet, invent one. |
| 182 | if (!Sym) { |
| 183 | Sym = getMCSymbolForMBBOrGV(Asm, &MBB); |
| 184 | |
| 185 | // Describe our funclet symbol as a function with internal linkage. |
| 186 | Asm->OutStreamer->BeginCOFFSymbolDef(Sym); |
| 187 | Asm->OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC); |
| 188 | Asm->OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION |
| 189 | << COFF::SCT_COMPLEX_TYPE_SHIFT); |
| 190 | Asm->OutStreamer->EndCOFFSymbolDef(); |
| 191 | |
| 192 | // We want our funclet's entry point to be aligned such that no nops will be |
| 193 | // present after the label. |
| 194 | Asm->EmitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()), |
| 195 | F); |
| 196 | |
| 197 | // Now that we've emitted the alignment directive, point at our funclet. |
| 198 | Asm->OutStreamer->EmitLabel(Sym); |
| 199 | } |
| 200 | |
| 201 | // Mark 'Sym' as starting our funclet. |
David Majnemer | 0e70598 | 2015-09-11 17:34:34 +0000 | [diff] [blame] | 202 | if (shouldEmitMoves || shouldEmitPersonality) |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 203 | Asm->OutStreamer->EmitWinCFIStartProc(Sym); |
| 204 | |
| 205 | if (shouldEmitPersonality) { |
| 206 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 207 | const Function *PerFn = nullptr; |
| 208 | |
| 209 | // Determine which personality routine we are using for this funclet. |
| 210 | if (F->hasPersonalityFn()) |
| 211 | PerFn = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
| 212 | const MCSymbol *PersHandlerSym = |
| 213 | TLOF.getCFIPersonalitySymbol(PerFn, *Asm->Mang, Asm->TM, MMI); |
| 214 | |
| 215 | // Classify the personality routine so that we may reason about it. |
| 216 | EHPersonality Per = EHPersonality::Unknown; |
| 217 | if (F->hasPersonalityFn()) |
| 218 | Per = classifyEHPersonality(F->getPersonalityFn()); |
| 219 | |
| 220 | // Do not emit a .seh_handler directive if it is a C++ cleanup funclet. |
| 221 | if (Per != EHPersonality::MSVC_CXX || |
| 222 | !CurrentFuncletEntry->isCleanupFuncletEntry()) |
| 223 | Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void WinException::endFunclet() { |
| 228 | // No funclet to process? Great, we have nothing to do. |
| 229 | if (!CurrentFuncletEntry) |
| 230 | return; |
| 231 | |
| 232 | if (shouldEmitMoves || shouldEmitPersonality) { |
| 233 | const Function *F = Asm->MF->getFunction(); |
| 234 | EHPersonality Per = EHPersonality::Unknown; |
| 235 | if (F->hasPersonalityFn()) |
| 236 | Per = classifyEHPersonality(F->getPersonalityFn()); |
| 237 | |
| 238 | // The .seh_handlerdata directive implicitly switches section, push the |
| 239 | // current section so that we may return to it. |
| 240 | Asm->OutStreamer->PushSection(); |
| 241 | |
| 242 | // Emit an UNWIND_INFO struct describing the prologue. |
| 243 | Asm->OutStreamer->EmitWinEHHandlerData(); |
| 244 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 245 | if (Per == EHPersonality::MSVC_CXX && shouldEmitPersonality && |
| 246 | !CurrentFuncletEntry->isCleanupFuncletEntry()) { |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 247 | // If this is a C++ catch funclet (or the parent function), |
| 248 | // emit a reference to the LSDA for the parent function. |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 249 | StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
| 250 | MCSymbol *FuncInfoXData = Asm->OutContext.getOrCreateSymbol( |
| 251 | Twine("$cppxdata$", FuncLinkageName)); |
| 252 | Asm->OutStreamer->EmitValue(create32bitRef(FuncInfoXData), 4); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 253 | } else if (Per == EHPersonality::MSVC_Win64SEH && MMI->hasEHFunclets() && |
| 254 | !CurrentFuncletEntry->isEHFuncletEntry()) { |
| 255 | // If this is the parent function in Win64 SEH, emit the LSDA immediately |
| 256 | // following .seh_handlerdata. |
| 257 | emitCSpecificHandlerTable(Asm->MF); |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Switch back to the previous section now that we are done writing to |
| 261 | // .xdata. |
| 262 | Asm->OutStreamer->PopSection(); |
| 263 | |
| 264 | // Emit a .seh_endproc directive to mark the end of the function. |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 265 | Asm->OutStreamer->EmitWinCFIEndProc(); |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Let's make sure we don't try to end the same funclet twice. |
| 269 | CurrentFuncletEntry = nullptr; |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 270 | } |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 271 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 272 | const MCExpr *WinException::create32bitRef(const MCSymbol *Value) { |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 273 | if (!Value) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 274 | return MCConstantExpr::create(0, Asm->OutContext); |
| 275 | return MCSymbolRefExpr::create(Value, useImageRel32 |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 276 | ? MCSymbolRefExpr::VK_COFF_IMGREL32 |
| 277 | : MCSymbolRefExpr::VK_None, |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 278 | Asm->OutContext); |
| 279 | } |
| 280 | |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 281 | const MCExpr *WinException::create32bitRef(const Value *V) { |
| 282 | if (!V) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 283 | return MCConstantExpr::create(0, Asm->OutContext); |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 284 | // FIXME: Delete the GlobalValue case once the new IR is fully functional. |
| 285 | if (const auto *GV = dyn_cast<GlobalValue>(V)) |
| 286 | return create32bitRef(Asm->getSymbol(GV)); |
| 287 | return create32bitRef(MMI->getAddrLabelSymbol(cast<BasicBlock>(V))); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 290 | const MCExpr *WinException::getLabelPlusOne(MCSymbol *Label) { |
| 291 | return MCBinaryExpr::createAdd(create32bitRef(Label), |
| 292 | MCConstantExpr::create(1, Asm->OutContext), |
| 293 | Asm->OutContext); |
| 294 | } |
| 295 | |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame^] | 296 | int WinException::getFrameIndexOffset(int FrameIndex) { |
| 297 | const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering(); |
| 298 | unsigned UnusedReg; |
| 299 | if (Asm->MAI->usesWindowsCFI()) |
| 300 | return TFI.getFrameIndexReferenceFromSP(*Asm->MF, FrameIndex, UnusedReg); |
| 301 | return TFI.getFrameIndexReference(*Asm->MF, FrameIndex, UnusedReg); |
| 302 | } |
| 303 | |
Benjamin Kramer | 808d2a0 | 2015-10-05 21:20:26 +0000 | [diff] [blame] | 304 | namespace { |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 305 | /// Information describing an invoke range. |
| 306 | struct InvokeRange { |
| 307 | MCSymbol *BeginLabel = nullptr; |
| 308 | MCSymbol *EndLabel = nullptr; |
| 309 | int State = -1; |
| 310 | |
| 311 | /// If we saw a potentially throwing call between this range and the last |
| 312 | /// range. |
| 313 | bool SawPotentiallyThrowing = false; |
| 314 | }; |
| 315 | |
| 316 | /// Iterator over the begin/end label pairs of invokes within a basic block. |
| 317 | class InvokeLabelIterator { |
| 318 | public: |
| 319 | InvokeLabelIterator(WinEHFuncInfo &EHInfo, |
| 320 | MachineBasicBlock::const_iterator MBBI, |
| 321 | MachineBasicBlock::const_iterator MBBIEnd) |
| 322 | : EHInfo(EHInfo), MBBI(MBBI), MBBIEnd(MBBIEnd) { |
| 323 | scan(); |
| 324 | } |
| 325 | |
| 326 | // Iterator methods. |
| 327 | bool operator==(const InvokeLabelIterator &o) const { return MBBI == o.MBBI; } |
| 328 | bool operator!=(const InvokeLabelIterator &o) const { return MBBI != o.MBBI; } |
| 329 | InvokeRange &operator*() { return CurRange; } |
| 330 | InvokeRange *operator->() { return &CurRange; } |
| 331 | InvokeLabelIterator &operator++() { return scan(); } |
| 332 | |
| 333 | private: |
| 334 | // Scan forward to find the next invoke range, or hit the end iterator. |
| 335 | InvokeLabelIterator &scan(); |
| 336 | |
| 337 | WinEHFuncInfo &EHInfo; |
| 338 | MachineBasicBlock::const_iterator MBBI; |
| 339 | MachineBasicBlock::const_iterator MBBIEnd; |
| 340 | InvokeRange CurRange; |
| 341 | }; |
Benjamin Kramer | 808d2a0 | 2015-10-05 21:20:26 +0000 | [diff] [blame] | 342 | } // end anonymous namespace |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 343 | |
| 344 | /// Invoke label range iteration logic. Increment MBBI until we find the next |
| 345 | /// EH_LABEL pair, and then update MBBI to point after the end label. |
| 346 | InvokeLabelIterator &InvokeLabelIterator::scan() { |
| 347 | // Reset our state. |
| 348 | CurRange = InvokeRange{}; |
| 349 | |
| 350 | for (const MachineInstr &MI : make_range(MBBI, MBBIEnd)) { |
| 351 | // Remember if we had to cross a potentially throwing call instruction that |
| 352 | // must unwind to caller. |
| 353 | if (MI.isCall()) { |
| 354 | CurRange.SawPotentiallyThrowing |= |
| 355 | !EHStreamer::callToNoUnwindFunction(&MI); |
| 356 | continue; |
| 357 | } |
| 358 | // Find the next EH_LABEL instruction. |
| 359 | if (!MI.isEHLabel()) |
| 360 | continue; |
| 361 | |
| 362 | // If this is a begin label, break out with the state and end label. |
| 363 | // Otherwise this is probably a CFI EH_LABEL that we should continue past. |
| 364 | MCSymbol *Label = MI.getOperand(0).getMCSymbol(); |
| 365 | auto StateAndEnd = EHInfo.InvokeToStateMap.find(Label); |
| 366 | if (StateAndEnd == EHInfo.InvokeToStateMap.end()) |
| 367 | continue; |
| 368 | MBBI = MachineBasicBlock::const_iterator(&MI); |
| 369 | CurRange.BeginLabel = Label; |
| 370 | CurRange.EndLabel = StateAndEnd->second.second; |
| 371 | CurRange.State = StateAndEnd->second.first; |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | // If we didn't find a begin label, we are done, return the end iterator. |
| 376 | if (!CurRange.BeginLabel) { |
| 377 | MBBI = MBBIEnd; |
| 378 | return *this; |
| 379 | } |
| 380 | |
| 381 | // If this is a begin label, update MBBI to point past the end label. |
| 382 | for (; MBBI != MBBIEnd; ++MBBI) |
| 383 | if (MBBI->isEHLabel() && |
| 384 | MBBI->getOperand(0).getMCSymbol() == CurRange.EndLabel) |
| 385 | break; |
| 386 | return *this; |
| 387 | } |
| 388 | |
| 389 | /// Utility for making a range for all the invoke ranges. |
| 390 | static iterator_range<InvokeLabelIterator> |
| 391 | invoke_ranges(WinEHFuncInfo &EHInfo, const MachineBasicBlock &MBB) { |
| 392 | return make_range(InvokeLabelIterator(EHInfo, MBB.begin(), MBB.end()), |
| 393 | InvokeLabelIterator(EHInfo, MBB.end(), MBB.end())); |
| 394 | } |
| 395 | |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 396 | /// Emit the language-specific data that __C_specific_handler expects. This |
| 397 | /// handler lives in the x64 Microsoft C runtime and allows catching or cleaning |
| 398 | /// up after faults with __try, __except, and __finally. The typeinfo values |
| 399 | /// are not really RTTI data, but pointers to filter functions that return an |
| 400 | /// integer (1, 0, or -1) indicating how to handle the exception. For __finally |
| 401 | /// blocks and other cleanups, the landing pad label is zero, and the filter |
| 402 | /// function is actually a cleanup handler with the same prototype. A catch-all |
| 403 | /// entry is modeled with a null filter function field and a non-zero landing |
| 404 | /// pad label. |
| 405 | /// |
| 406 | /// Possible filter function return values: |
| 407 | /// EXCEPTION_EXECUTE_HANDLER (1): |
| 408 | /// Jump to the landing pad label after cleanups. |
| 409 | /// EXCEPTION_CONTINUE_SEARCH (0): |
| 410 | /// Continue searching this table or continue unwinding. |
| 411 | /// EXCEPTION_CONTINUE_EXECUTION (-1): |
| 412 | /// Resume execution at the trapping PC. |
| 413 | /// |
| 414 | /// Inferred table structure: |
| 415 | /// struct Table { |
| 416 | /// int NumEntries; |
| 417 | /// struct Entry { |
| 418 | /// imagerel32 LabelStart; |
| 419 | /// imagerel32 LabelEnd; |
Reid Kleckner | f690f50 | 2015-01-22 02:27:44 +0000 | [diff] [blame] | 420 | /// imagerel32 FilterOrFinally; // One means catch-all. |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 421 | /// imagerel32 LabelLPad; // Zero means __finally. |
| 422 | /// } Entries[NumEntries]; |
| 423 | /// }; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 424 | void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) { |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 425 | auto &OS = *Asm->OutStreamer; |
| 426 | MCContext &Ctx = Asm->OutContext; |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 427 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 428 | WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(MF->getFunction()); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 429 | if (!FuncInfo.SEHUnwindMap.empty()) { |
| 430 | // Remember what state we were in the last time we found a begin try label. |
| 431 | // This allows us to coalesce many nearby invokes with the same state into |
| 432 | // one entry. |
| 433 | int LastEHState = -1; |
| 434 | MCSymbol *LastBeginLabel = nullptr; |
| 435 | MCSymbol *LastEndLabel = nullptr; |
| 436 | |
| 437 | // Use the assembler to compute the number of table entries through label |
| 438 | // difference and division. |
Richard Trieu | e0129e4 | 2015-10-02 20:52:14 +0000 | [diff] [blame] | 439 | MCSymbol *TableBegin = |
| 440 | Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true); |
| 441 | MCSymbol *TableEnd = |
| 442 | Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 443 | const MCExpr *LabelDiff = |
| 444 | MCBinaryExpr::createSub(MCSymbolRefExpr::create(TableEnd, Ctx), |
| 445 | MCSymbolRefExpr::create(TableBegin, Ctx), Ctx); |
| 446 | const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx); |
| 447 | const MCExpr *EntryCount = |
| 448 | MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx); |
| 449 | OS.EmitValue(EntryCount, 4); |
| 450 | |
| 451 | OS.EmitLabel(TableBegin); |
| 452 | |
| 453 | // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only |
| 454 | // models exceptions from invokes. LLVM also allows arbitrary reordering of |
| 455 | // the code, so our tables end up looking a bit different. Rather than |
| 456 | // trying to match MSVC's tables exactly, we emit a denormalized table. For |
| 457 | // each range of invokes in the same state, we emit table entries for all |
| 458 | // the actions that would be taken in that state. This means our tables are |
| 459 | // slightly bigger, which is OK. |
| 460 | for (const auto &MBB : *MF) { |
| 461 | for (InvokeRange &I : invoke_ranges(FuncInfo, MBB)) { |
| 462 | // If this invoke is in the same state as the last invoke and there were |
| 463 | // no non-throwing calls between it, extend the range to include both |
| 464 | // and continue. |
| 465 | if (!I.SawPotentiallyThrowing && I.State == LastEHState) { |
| 466 | LastEndLabel = I.EndLabel; |
| 467 | continue; |
| 468 | } |
| 469 | |
| 470 | // If this invoke ends a previous one, emit all the actions for this |
| 471 | // state. |
| 472 | if (LastEHState != -1) { |
| 473 | assert(LastBeginLabel && LastEndLabel); |
| 474 | for (int State = LastEHState; State != -1;) { |
| 475 | SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State]; |
| 476 | const MCExpr *FilterOrFinally; |
| 477 | const MCExpr *ExceptOrNull; |
| 478 | auto *Handler = UME.Handler.get<MachineBasicBlock *>(); |
| 479 | if (UME.IsFinally) { |
| 480 | FilterOrFinally = create32bitRef(Handler->getSymbol()); |
| 481 | ExceptOrNull = MCConstantExpr::create(0, Ctx); |
| 482 | } else { |
| 483 | // For an except, the filter can be 1 (catch-all) or a function |
| 484 | // label. |
| 485 | FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter) |
| 486 | : MCConstantExpr::create(1, Ctx); |
| 487 | ExceptOrNull = create32bitRef(Handler->getSymbol()); |
| 488 | } |
| 489 | |
| 490 | OS.EmitValue(getLabelPlusOne(LastBeginLabel), 4); |
| 491 | OS.EmitValue(getLabelPlusOne(LastEndLabel), 4); |
| 492 | OS.EmitValue(FilterOrFinally, 4); |
| 493 | OS.EmitValue(ExceptOrNull, 4); |
| 494 | |
| 495 | State = UME.ToState; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | LastBeginLabel = I.BeginLabel; |
| 500 | LastEndLabel = I.EndLabel; |
| 501 | LastEHState = I.State; |
| 502 | } |
| 503 | } |
| 504 | OS.EmitLabel(TableEnd); |
| 505 | return; |
| 506 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 507 | |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 508 | // Simplifying assumptions for first implementation: |
| 509 | // - Cleanups are not implemented. |
| 510 | // - Filters are not implemented. |
| 511 | |
| 512 | // The Itanium LSDA table sorts similar landing pads together to simplify the |
| 513 | // actions table, but we don't need that. |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 514 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 515 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 516 | LandingPads.reserve(PadInfos.size()); |
| 517 | for (const auto &LP : PadInfos) |
| 518 | LandingPads.push_back(&LP); |
| 519 | |
| 520 | // Compute label ranges for call sites as we would for the Itanium LSDA, but |
| 521 | // use an all zero action table because we aren't using these actions. |
| 522 | SmallVector<unsigned, 64> FirstActions; |
| 523 | FirstActions.resize(LandingPads.size()); |
| 524 | SmallVector<CallSiteEntry, 64> CallSites; |
| 525 | computeCallSiteTable(CallSites, LandingPads, FirstActions); |
| 526 | |
Rafael Espindola | 629cdba | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 527 | MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin(); |
| 528 | MCSymbol *EHFuncEndSym = Asm->getFunctionEnd(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 529 | |
| 530 | // Emit the number of table entries. |
| 531 | unsigned NumEntries = 0; |
| 532 | for (const CallSiteEntry &CSE : CallSites) { |
| 533 | if (!CSE.LPad) |
| 534 | continue; // Ignore gaps. |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 535 | NumEntries += CSE.LPad->SEHHandlers.size(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 536 | } |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 537 | OS.EmitIntValue(NumEntries, 4); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 538 | |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 539 | // If there are no actions, we don't need to iterate again. |
| 540 | if (NumEntries == 0) |
| 541 | return; |
| 542 | |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 543 | // Emit the four-label records for each call site entry. The table has to be |
| 544 | // sorted in layout order, and the call sites should already be sorted. |
| 545 | for (const CallSiteEntry &CSE : CallSites) { |
| 546 | // Ignore gaps. Unlike the Itanium model, unwinding through a frame without |
| 547 | // an EH table entry will propagate the exception rather than terminating |
| 548 | // the program. |
| 549 | if (!CSE.LPad) |
| 550 | continue; |
| 551 | const LandingPadInfo *LPad = CSE.LPad; |
| 552 | |
| 553 | // Compute the label range. We may reuse the function begin and end labels |
| 554 | // rather than forming new ones. |
| 555 | const MCExpr *Begin = |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 556 | create32bitRef(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 557 | const MCExpr *End; |
| 558 | if (CSE.EndLabel) { |
| 559 | // The interval is half-open, so we have to add one to include the return |
| 560 | // address of the last invoke in the range. |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 561 | End = getLabelPlusOne(CSE.EndLabel); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 562 | } else { |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 563 | End = create32bitRef(EHFuncEndSym); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 566 | // Emit an entry for each action. |
| 567 | for (SEHHandler Handler : LPad->SEHHandlers) { |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 568 | OS.EmitValue(Begin, 4); |
| 569 | OS.EmitValue(End, 4); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 570 | |
| 571 | // Emit the filter or finally function pointer, if present. Otherwise, |
| 572 | // emit '1' to indicate a catch-all. |
| 573 | const Function *F = Handler.FilterOrFinally; |
| 574 | if (F) |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 575 | OS.EmitValue(create32bitRef(Asm->getSymbol(F)), 4); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 576 | else |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 577 | OS.EmitIntValue(1, 4); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 578 | |
| 579 | // Emit the recovery address, if present. Otherwise, this must be a |
| 580 | // finally. |
| 581 | const BlockAddress *BA = Handler.RecoverBA; |
| 582 | if (BA) |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 583 | OS.EmitValue( |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 584 | create32bitRef(Asm->GetBlockAddressSymbol(BA)), 4); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 585 | else |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 586 | OS.EmitIntValue(0, 4); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 587 | } |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 590 | |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 591 | void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) { |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 592 | const Function *F = MF->getFunction(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 593 | auto &OS = *Asm->OutStreamer; |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 594 | WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(F); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 595 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 596 | StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 597 | |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 598 | SmallVector<std::pair<const MCExpr *, int>, 4> IPToStateTable; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 599 | MCSymbol *FuncInfoXData = nullptr; |
| 600 | if (shouldEmitPersonality) { |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 601 | // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from |
| 602 | // IPs to state numbers. |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 603 | FuncInfoXData = |
| 604 | Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName)); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 605 | computeIP2StateTable(MF, FuncInfo, IPToStateTable); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 606 | } else { |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 607 | FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName); |
| 608 | emitEHRegistrationOffsetLabel(FuncInfo, FuncLinkageName); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame^] | 611 | int UnwindHelpOffset = 0; |
| 612 | if (Asm->MAI->usesWindowsCFI()) |
| 613 | UnwindHelpOffset = getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx); |
| 614 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 615 | MCSymbol *UnwindMapXData = nullptr; |
| 616 | MCSymbol *TryBlockMapXData = nullptr; |
| 617 | MCSymbol *IPToStateXData = nullptr; |
| 618 | if (!FuncInfo.UnwindMap.empty()) |
| 619 | UnwindMapXData = Asm->OutContext.getOrCreateSymbol( |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 620 | Twine("$stateUnwindMap$", FuncLinkageName)); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 621 | if (!FuncInfo.TryBlockMap.empty()) |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 622 | TryBlockMapXData = |
| 623 | Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName)); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 624 | if (!IPToStateTable.empty()) |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 625 | IPToStateXData = |
| 626 | Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName)); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 627 | |
| 628 | // FuncInfo { |
| 629 | // uint32_t MagicNumber |
| 630 | // int32_t MaxState; |
| 631 | // UnwindMapEntry *UnwindMap; |
| 632 | // uint32_t NumTryBlocks; |
| 633 | // TryBlockMapEntry *TryBlockMap; |
| 634 | // uint32_t IPMapEntries; // always 0 for x86 |
| 635 | // IPToStateMapEntry *IPToStateMap; // always 0 for x86 |
| 636 | // uint32_t UnwindHelp; // non-x86 only |
| 637 | // ESTypeList *ESTypeList; |
| 638 | // int32_t EHFlags; |
| 639 | // } |
| 640 | // EHFlags & 1 -> Synchronous exceptions only, no async exceptions. |
| 641 | // EHFlags & 2 -> ??? |
| 642 | // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue. |
Reid Kleckner | 85a2450 | 2015-07-10 00:08:49 +0000 | [diff] [blame] | 643 | OS.EmitValueToAlignment(4); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 644 | OS.EmitLabel(FuncInfoXData); |
| 645 | OS.EmitIntValue(0x19930522, 4); // MagicNumber |
| 646 | OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState |
| 647 | OS.EmitValue(create32bitRef(UnwindMapXData), 4); // UnwindMap |
| 648 | OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks |
| 649 | OS.EmitValue(create32bitRef(TryBlockMapXData), 4); // TryBlockMap |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 650 | OS.EmitIntValue(IPToStateTable.size(), 4); // IPMapEntries |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 651 | OS.EmitValue(create32bitRef(IPToStateXData), 4); // IPToStateMap |
| 652 | if (Asm->MAI->usesWindowsCFI()) |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame^] | 653 | OS.EmitIntValue(UnwindHelpOffset, 4); // UnwindHelp |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 654 | OS.EmitIntValue(0, 4); // ESTypeList |
| 655 | OS.EmitIntValue(1, 4); // EHFlags |
| 656 | |
| 657 | // UnwindMapEntry { |
| 658 | // int32_t ToState; |
| 659 | // void (*Action)(); |
| 660 | // }; |
| 661 | if (UnwindMapXData) { |
| 662 | OS.EmitLabel(UnwindMapXData); |
| 663 | for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) { |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 664 | MCSymbol *CleanupSym = getMCSymbolForMBBOrGV(Asm, UME.Cleanup); |
| 665 | OS.EmitIntValue(UME.ToState, 4); // ToState |
| 666 | OS.EmitValue(create32bitRef(CleanupSym), 4); // Action |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | // TryBlockMap { |
| 671 | // int32_t TryLow; |
| 672 | // int32_t TryHigh; |
| 673 | // int32_t CatchHigh; |
| 674 | // int32_t NumCatches; |
| 675 | // HandlerType *HandlerArray; |
| 676 | // }; |
| 677 | if (TryBlockMapXData) { |
| 678 | OS.EmitLabel(TryBlockMapXData); |
| 679 | SmallVector<MCSymbol *, 1> HandlerMaps; |
| 680 | for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) { |
| 681 | WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I]; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 682 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 683 | MCSymbol *HandlerMapXData = nullptr; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 684 | if (!TBME.HandlerArray.empty()) |
| 685 | HandlerMapXData = |
| 686 | Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$") |
| 687 | .concat(Twine(I)) |
| 688 | .concat("$") |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 689 | .concat(FuncLinkageName)); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 690 | HandlerMaps.push_back(HandlerMapXData); |
| 691 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 692 | // TBMEs should form intervals. |
| 693 | assert(0 <= TBME.TryLow && "bad trymap interval"); |
| 694 | assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval"); |
| 695 | assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval"); |
| 696 | assert(TBME.CatchHigh < int(FuncInfo.UnwindMap.size()) && |
| 697 | "bad trymap interval"); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 698 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 699 | OS.EmitIntValue(TBME.TryLow, 4); // TryLow |
| 700 | OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 701 | OS.EmitIntValue(TBME.CatchHigh, 4); // CatchHigh |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 702 | OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches |
| 703 | OS.EmitValue(create32bitRef(HandlerMapXData), 4); // HandlerArray |
| 704 | } |
| 705 | |
| 706 | for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) { |
| 707 | WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I]; |
| 708 | MCSymbol *HandlerMapXData = HandlerMaps[I]; |
| 709 | if (!HandlerMapXData) |
| 710 | continue; |
| 711 | // HandlerType { |
| 712 | // int32_t Adjectives; |
| 713 | // TypeDescriptor *Type; |
| 714 | // int32_t CatchObjOffset; |
| 715 | // void (*Handler)(); |
| 716 | // int32_t ParentFrameOffset; // x64 only |
| 717 | // }; |
| 718 | OS.EmitLabel(HandlerMapXData); |
| 719 | for (const WinEHHandlerType &HT : TBME.HandlerArray) { |
| 720 | // Get the frame escape label with the offset of the catch object. If |
| 721 | // the index is -1, then there is no catch object, and we should emit an |
| 722 | // offset of zero, indicating that no copy will occur. |
| 723 | const MCExpr *FrameAllocOffsetRef = nullptr; |
| 724 | if (HT.CatchObjRecoverIdx >= 0) { |
| 725 | MCSymbol *FrameAllocOffset = |
| 726 | Asm->OutContext.getOrCreateFrameAllocSymbol( |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 727 | FuncLinkageName, HT.CatchObjRecoverIdx); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 728 | FrameAllocOffsetRef = MCSymbolRefExpr::create( |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 729 | FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext); |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame^] | 730 | } else if (HT.CatchObj.FrameIndex != INT_MAX) { |
| 731 | int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex); |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 732 | // For 32-bit, the catch object offset is relative to the end of the |
| 733 | // EH registration node. For 64-bit, it's relative to SP at the end of |
| 734 | // the prologue. |
| 735 | if (!shouldEmitPersonality) { |
| 736 | assert(FuncInfo.EHRegNodeEndOffset != INT_MAX); |
| 737 | Offset += FuncInfo.EHRegNodeEndOffset; |
| 738 | } |
| 739 | FrameAllocOffsetRef = MCConstantExpr::create(Offset, Asm->OutContext); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 740 | } else { |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 741 | FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 744 | MCSymbol *HandlerSym = getMCSymbolForMBBOrGV(Asm, HT.Handler); |
| 745 | |
| 746 | OS.EmitIntValue(HT.Adjectives, 4); // Adjectives |
| 747 | OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4); // Type |
| 748 | OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset |
| 749 | OS.EmitValue(create32bitRef(HandlerSym), 4); // Handler |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 750 | |
| 751 | if (shouldEmitPersonality) { |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 752 | // With the new IR, this is always 16 + 8 + getMaxCallFrameSize(). |
| 753 | // Keep this in sync with X86FrameLowering::emitPrologue. |
| 754 | int ParentFrameOffset = |
| 755 | 16 + 8 + MF->getFrameInfo()->getMaxCallFrameSize(); |
| 756 | OS.EmitIntValue(ParentFrameOffset, 4); // ParentFrameOffset |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | // IPToStateMapEntry { |
| 763 | // void *IP; |
| 764 | // int32_t State; |
| 765 | // }; |
| 766 | if (IPToStateXData) { |
| 767 | OS.EmitLabel(IPToStateXData); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 768 | for (auto &IPStatePair : IPToStateTable) { |
| 769 | OS.EmitValue(IPStatePair.first, 4); // IP |
| 770 | OS.EmitIntValue(IPStatePair.second, 4); // State |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 775 | void WinException::computeIP2StateTable( |
| 776 | const MachineFunction *MF, WinEHFuncInfo &FuncInfo, |
| 777 | SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) { |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 778 | // Remember what state we were in the last time we found a begin try label. |
| 779 | // This allows us to coalesce many nearby invokes with the same state into one |
| 780 | // entry. |
| 781 | int LastEHState = -1; |
| 782 | MCSymbol *LastEndLabel = Asm->getFunctionBegin(); |
| 783 | assert(LastEndLabel && "need local function start label"); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 784 | |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 785 | // Indicate that all calls from the prologue to the first invoke unwind to |
| 786 | // caller. We handle this as a special case since other ranges starting at end |
| 787 | // labels need to use LtmpN+1. |
| 788 | IPToStateTable.push_back(std::make_pair(create32bitRef(LastEndLabel), -1)); |
Andrew Kaylor | 762a6be | 2015-05-11 19:41:19 +0000 | [diff] [blame] | 789 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 790 | for (const auto &MBB : *MF) { |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 791 | // FIXME: Do we need to emit entries for funclet base states? |
| 792 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 793 | for (InvokeRange &I : invoke_ranges(FuncInfo, MBB)) { |
| 794 | assert(I.BeginLabel && I.EndLabel); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 795 | // If there was a potentially throwing call between this begin label and |
| 796 | // the last end label, we need an extra base state entry to indicate that |
| 797 | // those calls unwind directly to the caller. |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 798 | if (I.SawPotentiallyThrowing && LastEHState != -1) { |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 799 | IPToStateTable.push_back( |
| 800 | std::make_pair(getLabelPlusOne(LastEndLabel), -1)); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 801 | LastEHState = -1; |
| 802 | } |
| 803 | |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 804 | // Emit an entry indicating that PCs after 'Label' have this EH state. |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 805 | if (I.State != LastEHState) |
| 806 | IPToStateTable.push_back( |
Reid Kleckner | 33bd2d9 | 2015-10-07 17:49:32 +0000 | [diff] [blame] | 807 | std::make_pair(getLabelPlusOne(I.BeginLabel), I.State)); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 808 | LastEHState = I.State; |
| 809 | LastEndLabel = I.EndLabel; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 812 | |
| 813 | if (LastEndLabel != Asm->getFunctionBegin()) { |
| 814 | // Indicate that all calls from the last invoke until the epilogue unwind to |
| 815 | // caller. This also ensures that we have at least one ip2state entry, if |
| 816 | // somehow all invokes were deleted during CodeGen. |
| 817 | IPToStateTable.push_back(std::make_pair(getLabelPlusOne(LastEndLabel), -1)); |
| 818 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 819 | } |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 820 | |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 821 | void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo, |
| 822 | StringRef FLinkageName) { |
| 823 | // Outlined helpers called by the EH runtime need to know the offset of the EH |
| 824 | // registration in order to recover the parent frame pointer. Now that we know |
| 825 | // we've code generated the parent, we can emit the label assignment that |
| 826 | // those helpers use to get the offset of the registration node. |
| 827 | assert(FuncInfo.EHRegNodeEscapeIndex != INT_MAX && |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 828 | "no EH reg node localescape index"); |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 829 | MCSymbol *ParentFrameOffset = |
| 830 | Asm->OutContext.getOrCreateParentFrameOffsetSymbol(FLinkageName); |
| 831 | MCSymbol *RegistrationOffsetSym = Asm->OutContext.getOrCreateFrameAllocSymbol( |
| 832 | FLinkageName, FuncInfo.EHRegNodeEscapeIndex); |
| 833 | const MCExpr *RegistrationOffsetSymRef = |
| 834 | MCSymbolRefExpr::create(RegistrationOffsetSym, Asm->OutContext); |
| 835 | Asm->OutStreamer->EmitAssignment(ParentFrameOffset, RegistrationOffsetSymRef); |
| 836 | } |
| 837 | |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 838 | /// Emit the language-specific data that _except_handler3 and 4 expect. This is |
| 839 | /// functionally equivalent to the __C_specific_handler table, except it is |
| 840 | /// indexed by state number instead of IP. |
| 841 | void WinException::emitExceptHandlerTable(const MachineFunction *MF) { |
Reid Kleckner | a9d6253 | 2015-06-11 22:32:23 +0000 | [diff] [blame] | 842 | MCStreamer &OS = *Asm->OutStreamer; |
Reid Kleckner | a9d6253 | 2015-06-11 22:32:23 +0000 | [diff] [blame] | 843 | const Function *F = MF->getFunction(); |
Reid Kleckner | a9d6253 | 2015-06-11 22:32:23 +0000 | [diff] [blame] | 844 | StringRef FLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 845 | |
| 846 | WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(F); |
| 847 | emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 848 | |
| 849 | // Emit the __ehtable label that we use for llvm.x86.seh.lsda. |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 850 | MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName); |
Reid Kleckner | 85a2450 | 2015-07-10 00:08:49 +0000 | [diff] [blame] | 851 | OS.EmitValueToAlignment(4); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 852 | OS.EmitLabel(LSDALabel); |
| 853 | |
Reid Kleckner | 9a1a919 | 2015-07-13 20:41:46 +0000 | [diff] [blame] | 854 | const Function *Per = |
| 855 | dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 856 | StringRef PerName = Per->getName(); |
| 857 | int BaseState = -1; |
| 858 | if (PerName == "_except_handler4") { |
| 859 | // The LSDA for _except_handler4 starts with this struct, followed by the |
| 860 | // scope table: |
| 861 | // |
| 862 | // struct EH4ScopeTable { |
| 863 | // int32_t GSCookieOffset; |
| 864 | // int32_t GSCookieXOROffset; |
| 865 | // int32_t EHCookieOffset; |
| 866 | // int32_t EHCookieXOROffset; |
| 867 | // ScopeTableEntry ScopeRecord[]; |
| 868 | // }; |
| 869 | // |
| 870 | // Only the EHCookieOffset field appears to vary, and it appears to be the |
| 871 | // offset from the final saved SP value to the retaddr. |
| 872 | OS.EmitIntValue(-2, 4); |
| 873 | OS.EmitIntValue(0, 4); |
| 874 | // FIXME: Calculate. |
| 875 | OS.EmitIntValue(9999, 4); |
| 876 | OS.EmitIntValue(0, 4); |
| 877 | BaseState = -2; |
| 878 | } |
| 879 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 880 | if (!FuncInfo.SEHUnwindMap.empty()) { |
| 881 | for (SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) { |
| 882 | MCSymbol *ExceptOrFinally = |
| 883 | UME.Handler.get<MachineBasicBlock *>()->getSymbol(); |
| 884 | OS.EmitIntValue(UME.ToState, 4); // ToState |
| 885 | OS.EmitValue(create32bitRef(UME.Filter), 4); // Filter |
| 886 | OS.EmitValue(create32bitRef(ExceptOrFinally), 4); // Except/Finally |
| 887 | } |
| 888 | return; |
| 889 | } |
| 890 | // FIXME: The following code is for the old landingpad-based SEH |
| 891 | // implementation. Remove it when possible. |
| 892 | |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 893 | // Build a list of pointers to LandingPadInfos and then sort by WinEHState. |
| 894 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 895 | SmallVector<const LandingPadInfo *, 4> LPads; |
| 896 | LPads.reserve((PadInfos.size())); |
| 897 | for (const LandingPadInfo &LPInfo : PadInfos) |
| 898 | LPads.push_back(&LPInfo); |
| 899 | std::sort(LPads.begin(), LPads.end(), |
| 900 | [](const LandingPadInfo *L, const LandingPadInfo *R) { |
| 901 | return L->WinEHState < R->WinEHState; |
| 902 | }); |
| 903 | |
| 904 | // For each action in each lpad, emit one of these: |
| 905 | // struct ScopeTableEntry { |
| 906 | // int32_t EnclosingLevel; |
Reid Kleckner | 81d1cc0 | 2015-06-11 23:37:18 +0000 | [diff] [blame] | 907 | // int32_t (__cdecl *Filter)(); |
| 908 | // void *HandlerOrFinally; |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 909 | // }; |
| 910 | // |
| 911 | // The "outermost" action will use BaseState as its enclosing level. Each |
| 912 | // other action will refer to the previous state as its enclosing level. |
| 913 | int CurState = 0; |
| 914 | for (const LandingPadInfo *LPInfo : LPads) { |
| 915 | int EnclosingLevel = BaseState; |
Reid Kleckner | 7912d9b | 2015-06-10 00:04:53 +0000 | [diff] [blame] | 916 | assert(CurState + int(LPInfo->SEHHandlers.size()) - 1 == |
| 917 | LPInfo->WinEHState && |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 918 | "gaps in the SEH scope table"); |
Reid Kleckner | 81d1cc0 | 2015-06-11 23:37:18 +0000 | [diff] [blame] | 919 | for (auto I = LPInfo->SEHHandlers.rbegin(), E = LPInfo->SEHHandlers.rend(); |
| 920 | I != E; ++I) { |
| 921 | const SEHHandler &Handler = *I; |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 922 | const BlockAddress *BA = Handler.RecoverBA; |
Reid Kleckner | 81d1cc0 | 2015-06-11 23:37:18 +0000 | [diff] [blame] | 923 | const Function *F = Handler.FilterOrFinally; |
| 924 | assert(F && "cannot catch all in 32-bit SEH without filter function"); |
| 925 | const MCExpr *FilterOrNull = |
| 926 | create32bitRef(BA ? Asm->getSymbol(F) : nullptr); |
| 927 | const MCExpr *ExceptOrFinally = create32bitRef( |
| 928 | BA ? Asm->GetBlockAddressSymbol(BA) : Asm->getSymbol(F)); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 929 | |
| 930 | OS.EmitIntValue(EnclosingLevel, 4); |
Reid Kleckner | 81d1cc0 | 2015-06-11 23:37:18 +0000 | [diff] [blame] | 931 | OS.EmitValue(FilterOrNull, 4); |
| 932 | OS.EmitValue(ExceptOrFinally, 4); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 933 | |
| 934 | // The next state unwinds to this state. |
| 935 | EnclosingLevel = CurState; |
| 936 | CurState++; |
| 937 | } |
| 938 | } |
| 939 | } |