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(); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 70 | |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 71 | shouldEmitMoves = Asm->needsSEHMoves(); |
| 72 | |
| 73 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 74 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
Reid Kleckner | 9a1a919 | 2015-07-13 20:41:46 +0000 | [diff] [blame] | 75 | const Function *Per = nullptr; |
| 76 | if (F->hasPersonalityFn()) |
| 77 | Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 78 | |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 79 | bool forceEmitPersonality = |
| 80 | F->hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) && |
| 81 | F->needsUnwindTableEntry(); |
| 82 | |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 83 | shouldEmitPersonality = |
| 84 | forceEmitPersonality || ((hasLandingPads || hasEHFunclets) && |
| 85 | PerEncoding != dwarf::DW_EH_PE_omit && Per); |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 86 | |
| 87 | unsigned LSDAEncoding = TLOF.getLSDAEncoding(); |
| 88 | shouldEmitLSDA = shouldEmitPersonality && |
| 89 | LSDAEncoding != dwarf::DW_EH_PE_omit; |
| 90 | |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 91 | // If we're not using CFI, we don't want the CFI or the personality, but we |
| 92 | // might want EH tables if we had EH pads. |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 93 | if (!Asm->MAI->usesWindowsCFI()) { |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 94 | shouldEmitLSDA = hasEHFunclets; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 95 | shouldEmitPersonality = false; |
| 96 | return; |
| 97 | } |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 98 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 99 | beginFunclet(MF->front(), Asm->CurrentFnSym); |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 102 | /// endFunction - Gather and emit post-function exception information. |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 103 | /// |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 104 | void WinException::endFunction(const MachineFunction *MF) { |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 105 | if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA) |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 106 | return; |
| 107 | |
Reid Kleckner | 9a1a919 | 2015-07-13 20:41:46 +0000 | [diff] [blame] | 108 | const Function *F = MF->getFunction(); |
| 109 | EHPersonality Per = EHPersonality::Unknown; |
| 110 | if (F->hasPersonalityFn()) |
| 111 | Per = classifyEHPersonality(F->getPersonalityFn()); |
Reid Kleckner | 3e9fadf | 2015-04-15 18:48:15 +0000 | [diff] [blame] | 112 | |
Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 113 | // Get rid of any dead landing pads if we're not using funclets. In funclet |
| 114 | // schemes, the landing pad is not actually reachable. It only exists so |
| 115 | // that we can emit the right table data. |
| 116 | if (!isFuncletEHPersonality(Per)) |
Reid Kleckner | 3e9fadf | 2015-04-15 18:48:15 +0000 | [diff] [blame] | 117 | MMI->TidyLandingPads(); |
Charles Davis | a575226 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 118 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 119 | endFunclet(); |
| 120 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 121 | // endFunclet will emit the necessary .xdata tables for x64 SEH. |
| 122 | if (Per == EHPersonality::MSVC_Win64SEH && MMI->hasEHFunclets()) |
| 123 | return; |
| 124 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 125 | if (shouldEmitPersonality || shouldEmitLSDA) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 126 | Asm->OutStreamer->PushSection(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 127 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 128 | // Just switch sections to the right xdata section. This use of CurrentFnSym |
| 129 | // assumes that we only emit the LSDA when ending the parent function. |
| 130 | MCSection *XData = WinEH::UnwindEmitter::getXDataSection(Asm->CurrentFnSym, |
| 131 | Asm->OutContext); |
| 132 | Asm->OutStreamer->SwitchSection(XData); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 133 | |
Reid Kleckner | 9b5eaf0 | 2015-01-14 18:50:10 +0000 | [diff] [blame] | 134 | // Emit the tables appropriate to the personality function in use. If we |
| 135 | // don't recognize the personality, assume it uses an Itanium-style LSDA. |
Reid Kleckner | 2d5fb68 | 2015-02-14 00:21:02 +0000 | [diff] [blame] | 136 | if (Per == EHPersonality::MSVC_Win64SEH) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 137 | emitCSpecificHandlerTable(MF); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 138 | else if (Per == EHPersonality::MSVC_X86SEH) |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 139 | emitExceptHandlerTable(MF); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 140 | else if (Per == EHPersonality::MSVC_CXX) |
| 141 | emitCXXFrameHandler3Table(MF); |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 142 | else if (Per == EHPersonality::CoreCLR) |
| 143 | emitCLRExceptionTable(MF); |
Reid Kleckner | 9b5eaf0 | 2015-01-14 18:50:10 +0000 | [diff] [blame] | 144 | else |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 145 | emitExceptionTable(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 146 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 147 | Asm->OutStreamer->PopSection(); |
Charles Davis | a575226 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 148 | } |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 149 | } |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 150 | |
David Majnemer | 99c1d13 | 2015-10-12 16:44:22 +0000 | [diff] [blame] | 151 | /// Retreive the MCSymbol for a GlobalValue or MachineBasicBlock. |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 152 | static MCSymbol *getMCSymbolForMBB(AsmPrinter *Asm, |
| 153 | const MachineBasicBlock *MBB) { |
| 154 | if (!MBB) |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 155 | return nullptr; |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 156 | |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 157 | assert(MBB->isEHFuncletEntry()); |
| 158 | |
| 159 | // Give catches and cleanups a name based off of their parent function and |
| 160 | // their funclet entry block's number. |
| 161 | const MachineFunction *MF = MBB->getParent(); |
| 162 | const Function *F = MF->getFunction(); |
| 163 | StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
| 164 | MCContext &Ctx = MF->getContext(); |
| 165 | StringRef HandlerPrefix = MBB->isCleanupFuncletEntry() ? "dtor" : "catch"; |
| 166 | return Ctx.getOrCreateSymbol("?" + HandlerPrefix + "$" + |
| 167 | Twine(MBB->getNumber()) + "@?0?" + |
| 168 | FuncLinkageName + "@4HA"); |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void WinException::beginFunclet(const MachineBasicBlock &MBB, |
| 172 | MCSymbol *Sym) { |
| 173 | CurrentFuncletEntry = &MBB; |
| 174 | |
| 175 | const Function *F = Asm->MF->getFunction(); |
| 176 | // If a symbol was not provided for the funclet, invent one. |
| 177 | if (!Sym) { |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 178 | Sym = getMCSymbolForMBB(Asm, &MBB); |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 179 | |
| 180 | // Describe our funclet symbol as a function with internal linkage. |
| 181 | Asm->OutStreamer->BeginCOFFSymbolDef(Sym); |
| 182 | Asm->OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC); |
| 183 | Asm->OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION |
| 184 | << COFF::SCT_COMPLEX_TYPE_SHIFT); |
| 185 | Asm->OutStreamer->EndCOFFSymbolDef(); |
| 186 | |
| 187 | // We want our funclet's entry point to be aligned such that no nops will be |
| 188 | // present after the label. |
| 189 | Asm->EmitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()), |
| 190 | F); |
| 191 | |
| 192 | // Now that we've emitted the alignment directive, point at our funclet. |
| 193 | Asm->OutStreamer->EmitLabel(Sym); |
| 194 | } |
| 195 | |
| 196 | // Mark 'Sym' as starting our funclet. |
David Majnemer | 0e70598 | 2015-09-11 17:34:34 +0000 | [diff] [blame] | 197 | if (shouldEmitMoves || shouldEmitPersonality) |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 198 | Asm->OutStreamer->EmitWinCFIStartProc(Sym); |
| 199 | |
| 200 | if (shouldEmitPersonality) { |
| 201 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 202 | const Function *PerFn = nullptr; |
| 203 | |
| 204 | // Determine which personality routine we are using for this funclet. |
| 205 | if (F->hasPersonalityFn()) |
| 206 | PerFn = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
| 207 | const MCSymbol *PersHandlerSym = |
| 208 | TLOF.getCFIPersonalitySymbol(PerFn, *Asm->Mang, Asm->TM, MMI); |
| 209 | |
| 210 | // Classify the personality routine so that we may reason about it. |
| 211 | EHPersonality Per = EHPersonality::Unknown; |
| 212 | if (F->hasPersonalityFn()) |
| 213 | Per = classifyEHPersonality(F->getPersonalityFn()); |
| 214 | |
| 215 | // Do not emit a .seh_handler directive if it is a C++ cleanup funclet. |
| 216 | if (Per != EHPersonality::MSVC_CXX || |
| 217 | !CurrentFuncletEntry->isCleanupFuncletEntry()) |
| 218 | Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void WinException::endFunclet() { |
| 223 | // No funclet to process? Great, we have nothing to do. |
| 224 | if (!CurrentFuncletEntry) |
| 225 | return; |
| 226 | |
| 227 | if (shouldEmitMoves || shouldEmitPersonality) { |
| 228 | const Function *F = Asm->MF->getFunction(); |
| 229 | EHPersonality Per = EHPersonality::Unknown; |
| 230 | if (F->hasPersonalityFn()) |
| 231 | Per = classifyEHPersonality(F->getPersonalityFn()); |
| 232 | |
| 233 | // The .seh_handlerdata directive implicitly switches section, push the |
| 234 | // current section so that we may return to it. |
| 235 | Asm->OutStreamer->PushSection(); |
| 236 | |
| 237 | // Emit an UNWIND_INFO struct describing the prologue. |
| 238 | Asm->OutStreamer->EmitWinEHHandlerData(); |
| 239 | |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 240 | if (Per == EHPersonality::MSVC_CXX && shouldEmitPersonality && |
| 241 | !CurrentFuncletEntry->isCleanupFuncletEntry()) { |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 242 | // If this is a C++ catch funclet (or the parent function), |
| 243 | // emit a reference to the LSDA for the parent function. |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 244 | StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
| 245 | MCSymbol *FuncInfoXData = Asm->OutContext.getOrCreateSymbol( |
| 246 | Twine("$cppxdata$", FuncLinkageName)); |
| 247 | Asm->OutStreamer->EmitValue(create32bitRef(FuncInfoXData), 4); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 248 | } else if (Per == EHPersonality::MSVC_Win64SEH && MMI->hasEHFunclets() && |
| 249 | !CurrentFuncletEntry->isEHFuncletEntry()) { |
| 250 | // If this is the parent function in Win64 SEH, emit the LSDA immediately |
| 251 | // following .seh_handlerdata. |
| 252 | emitCSpecificHandlerTable(Asm->MF); |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Switch back to the previous section now that we are done writing to |
| 256 | // .xdata. |
| 257 | Asm->OutStreamer->PopSection(); |
| 258 | |
| 259 | // Emit a .seh_endproc directive to mark the end of the function. |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 260 | Asm->OutStreamer->EmitWinCFIEndProc(); |
David Majnemer | a80c151 | 2015-09-29 20:12:33 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // Let's make sure we don't try to end the same funclet twice. |
| 264 | CurrentFuncletEntry = nullptr; |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 265 | } |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 266 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 267 | const MCExpr *WinException::create32bitRef(const MCSymbol *Value) { |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 268 | if (!Value) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 269 | return MCConstantExpr::create(0, Asm->OutContext); |
| 270 | return MCSymbolRefExpr::create(Value, useImageRel32 |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 271 | ? MCSymbolRefExpr::VK_COFF_IMGREL32 |
| 272 | : MCSymbolRefExpr::VK_None, |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 273 | Asm->OutContext); |
| 274 | } |
| 275 | |
Joseph Tremoulet | 3d0fbf1 | 2015-10-23 15:06:05 +0000 | [diff] [blame] | 276 | const MCExpr *WinException::create32bitRef(const GlobalValue *GV) { |
| 277 | if (!GV) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 278 | return MCConstantExpr::create(0, Asm->OutContext); |
Joseph Tremoulet | 3d0fbf1 | 2015-10-23 15:06:05 +0000 | [diff] [blame] | 279 | return create32bitRef(Asm->getSymbol(GV)); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 282 | const MCExpr *WinException::getLabelPlusOne(const MCSymbol *Label) { |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 283 | return MCBinaryExpr::createAdd(create32bitRef(Label), |
| 284 | MCConstantExpr::create(1, Asm->OutContext), |
| 285 | Asm->OutContext); |
| 286 | } |
| 287 | |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 288 | const MCExpr *WinException::getOffset(const MCSymbol *OffsetOf, |
| 289 | const MCSymbol *OffsetFrom) { |
| 290 | return MCBinaryExpr::createSub( |
| 291 | MCSymbolRefExpr::create(OffsetOf, Asm->OutContext), |
| 292 | MCSymbolRefExpr::create(OffsetFrom, Asm->OutContext), Asm->OutContext); |
| 293 | } |
| 294 | |
| 295 | const MCExpr *WinException::getOffsetPlusOne(const MCSymbol *OffsetOf, |
| 296 | const MCSymbol *OffsetFrom) { |
| 297 | return MCBinaryExpr::createAdd(getOffset(OffsetOf, OffsetFrom), |
| 298 | MCConstantExpr::create(1, Asm->OutContext), |
| 299 | Asm->OutContext); |
| 300 | } |
| 301 | |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 302 | int WinException::getFrameIndexOffset(int FrameIndex, |
| 303 | const WinEHFuncInfo &FuncInfo) { |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame] | 304 | const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering(); |
| 305 | unsigned UnusedReg; |
| 306 | if (Asm->MAI->usesWindowsCFI()) |
| 307 | return TFI.getFrameIndexReferenceFromSP(*Asm->MF, FrameIndex, UnusedReg); |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 308 | // For 32-bit, offsets should be relative to the end of the EH registration |
| 309 | // node. For 64-bit, it's relative to SP at the end of the prologue. |
| 310 | assert(FuncInfo.EHRegNodeEndOffset != INT_MAX); |
| 311 | int Offset = TFI.getFrameIndexReference(*Asm->MF, FrameIndex, UnusedReg); |
| 312 | Offset += FuncInfo.EHRegNodeEndOffset; |
| 313 | return Offset; |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Benjamin Kramer | 808d2a0 | 2015-10-05 21:20:26 +0000 | [diff] [blame] | 316 | namespace { |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 317 | |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 318 | /// Top-level state used to represent unwind to caller |
| 319 | const int NullState = -1; |
| 320 | |
| 321 | struct InvokeStateChange { |
| 322 | /// EH Label immediately after the last invoke in the previous state, or |
| 323 | /// nullptr if the previous state was the null state. |
| 324 | const MCSymbol *PreviousEndLabel; |
| 325 | |
| 326 | /// EH label immediately before the first invoke in the new state, or nullptr |
| 327 | /// if the new state is the null state. |
| 328 | const MCSymbol *NewStartLabel; |
| 329 | |
| 330 | /// State of the invoke following NewStartLabel, or NullState to indicate |
| 331 | /// the presence of calls which may unwind to caller. |
| 332 | int NewState; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 333 | }; |
| 334 | |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 335 | /// Iterator that reports all the invoke state changes in a range of machine |
| 336 | /// basic blocks. Changes to the null state are reported whenever a call that |
| 337 | /// may unwind to caller is encountered. The MBB range is expected to be an |
| 338 | /// entire function or funclet, and the start and end of the range are treated |
| 339 | /// as being in the NullState even if there's not an unwind-to-caller call |
| 340 | /// before the first invoke or after the last one (i.e., the first state change |
| 341 | /// reported is the first change to something other than NullState, and a |
| 342 | /// change back to NullState is always reported at the end of iteration). |
| 343 | class InvokeStateChangeIterator { |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 344 | InvokeStateChangeIterator(const WinEHFuncInfo &EHInfo, |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 345 | MachineFunction::const_iterator MFI, |
| 346 | MachineFunction::const_iterator MFE, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 347 | MachineBasicBlock::const_iterator MBBI, |
| 348 | int BaseState) |
| 349 | : EHInfo(EHInfo), MFI(MFI), MFE(MFE), MBBI(MBBI), BaseState(BaseState) { |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 350 | LastStateChange.PreviousEndLabel = nullptr; |
| 351 | LastStateChange.NewStartLabel = nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 352 | LastStateChange.NewState = BaseState; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 353 | scan(); |
| 354 | } |
| 355 | |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 356 | public: |
| 357 | static iterator_range<InvokeStateChangeIterator> |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 358 | range(const WinEHFuncInfo &EHInfo, MachineFunction::const_iterator Begin, |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 359 | MachineFunction::const_iterator End, int BaseState = NullState) { |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 360 | // Reject empty ranges to simplify bookkeeping by ensuring that we can get |
| 361 | // the end of the last block. |
| 362 | assert(Begin != End); |
| 363 | auto BlockBegin = Begin->begin(); |
| 364 | auto BlockEnd = std::prev(End)->end(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 365 | return make_range( |
| 366 | InvokeStateChangeIterator(EHInfo, Begin, End, BlockBegin, BaseState), |
| 367 | InvokeStateChangeIterator(EHInfo, End, End, BlockEnd, BaseState)); |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 370 | // Iterator methods. |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 371 | bool operator==(const InvokeStateChangeIterator &O) const { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 372 | assert(BaseState == O.BaseState); |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 373 | // Must be visiting same block. |
| 374 | if (MFI != O.MFI) |
| 375 | return false; |
| 376 | // Must be visiting same isntr. |
| 377 | if (MBBI != O.MBBI) |
| 378 | return false; |
| 379 | // At end of block/instr iteration, we can still have two distinct states: |
| 380 | // one to report the final EndLabel, and another indicating the end of the |
| 381 | // state change iteration. Check for CurrentEndLabel equality to |
| 382 | // distinguish these. |
| 383 | return CurrentEndLabel == O.CurrentEndLabel; |
| 384 | } |
| 385 | |
| 386 | bool operator!=(const InvokeStateChangeIterator &O) const { |
| 387 | return !operator==(O); |
| 388 | } |
| 389 | InvokeStateChange &operator*() { return LastStateChange; } |
| 390 | InvokeStateChange *operator->() { return &LastStateChange; } |
| 391 | InvokeStateChangeIterator &operator++() { return scan(); } |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 392 | |
| 393 | private: |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 394 | InvokeStateChangeIterator &scan(); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 395 | |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 396 | const WinEHFuncInfo &EHInfo; |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 397 | const MCSymbol *CurrentEndLabel = nullptr; |
| 398 | MachineFunction::const_iterator MFI; |
| 399 | MachineFunction::const_iterator MFE; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 400 | MachineBasicBlock::const_iterator MBBI; |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 401 | InvokeStateChange LastStateChange; |
| 402 | bool VisitingInvoke = false; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 403 | int BaseState; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 404 | }; |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 405 | |
Benjamin Kramer | 808d2a0 | 2015-10-05 21:20:26 +0000 | [diff] [blame] | 406 | } // end anonymous namespace |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 407 | |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 408 | InvokeStateChangeIterator &InvokeStateChangeIterator::scan() { |
| 409 | bool IsNewBlock = false; |
| 410 | for (; MFI != MFE; ++MFI, IsNewBlock = true) { |
| 411 | if (IsNewBlock) |
| 412 | MBBI = MFI->begin(); |
| 413 | for (auto MBBE = MFI->end(); MBBI != MBBE; ++MBBI) { |
| 414 | const MachineInstr &MI = *MBBI; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 415 | if (!VisitingInvoke && LastStateChange.NewState != BaseState && |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 416 | MI.isCall() && !EHStreamer::callToNoUnwindFunction(&MI)) { |
| 417 | // Indicate a change of state to the null state. We don't have |
| 418 | // start/end EH labels handy but the caller won't expect them for |
| 419 | // null state regions. |
| 420 | LastStateChange.PreviousEndLabel = CurrentEndLabel; |
| 421 | LastStateChange.NewStartLabel = nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 422 | LastStateChange.NewState = BaseState; |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 423 | CurrentEndLabel = nullptr; |
| 424 | // Don't re-visit this instr on the next scan |
| 425 | ++MBBI; |
| 426 | return *this; |
| 427 | } |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 428 | |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 429 | // All other state changes are at EH labels before/after invokes. |
| 430 | if (!MI.isEHLabel()) |
| 431 | continue; |
| 432 | MCSymbol *Label = MI.getOperand(0).getMCSymbol(); |
| 433 | if (Label == CurrentEndLabel) { |
| 434 | VisitingInvoke = false; |
| 435 | continue; |
| 436 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 437 | auto InvokeMapIter = EHInfo.LabelToStateMap.find(Label); |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 438 | // Ignore EH labels that aren't the ones inserted before an invoke |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 439 | if (InvokeMapIter == EHInfo.LabelToStateMap.end()) |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 440 | continue; |
| 441 | auto &StateAndEnd = InvokeMapIter->second; |
| 442 | int NewState = StateAndEnd.first; |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 443 | // Keep track of the fact that we're between EH start/end labels so |
| 444 | // we know not to treat the inoke we'll see as unwinding to caller. |
| 445 | VisitingInvoke = true; |
| 446 | if (NewState == LastStateChange.NewState) { |
| 447 | // The state isn't actually changing here. Record the new end and |
| 448 | // keep going. |
| 449 | CurrentEndLabel = StateAndEnd.second; |
| 450 | continue; |
| 451 | } |
| 452 | // Found a state change to report |
| 453 | LastStateChange.PreviousEndLabel = CurrentEndLabel; |
| 454 | LastStateChange.NewStartLabel = Label; |
| 455 | LastStateChange.NewState = NewState; |
| 456 | // Start keeping track of the new current end |
| 457 | CurrentEndLabel = StateAndEnd.second; |
| 458 | // Don't re-visit this instr on the next scan |
| 459 | ++MBBI; |
| 460 | return *this; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 461 | } |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 462 | } |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 463 | // Iteration hit the end of the block range. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 464 | if (LastStateChange.NewState != BaseState) { |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 465 | // Report the end of the last new state |
| 466 | LastStateChange.PreviousEndLabel = CurrentEndLabel; |
| 467 | LastStateChange.NewStartLabel = nullptr; |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 468 | LastStateChange.NewState = BaseState; |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 469 | // Leave CurrentEndLabel non-null to distinguish this state from end. |
| 470 | assert(CurrentEndLabel != nullptr); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 471 | return *this; |
| 472 | } |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 473 | // We've reported all state changes and hit the end state. |
| 474 | CurrentEndLabel = nullptr; |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 475 | return *this; |
| 476 | } |
| 477 | |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 478 | /// Emit the language-specific data that __C_specific_handler expects. This |
| 479 | /// handler lives in the x64 Microsoft C runtime and allows catching or cleaning |
| 480 | /// up after faults with __try, __except, and __finally. The typeinfo values |
| 481 | /// are not really RTTI data, but pointers to filter functions that return an |
| 482 | /// integer (1, 0, or -1) indicating how to handle the exception. For __finally |
| 483 | /// blocks and other cleanups, the landing pad label is zero, and the filter |
| 484 | /// function is actually a cleanup handler with the same prototype. A catch-all |
| 485 | /// entry is modeled with a null filter function field and a non-zero landing |
| 486 | /// pad label. |
| 487 | /// |
| 488 | /// Possible filter function return values: |
| 489 | /// EXCEPTION_EXECUTE_HANDLER (1): |
| 490 | /// Jump to the landing pad label after cleanups. |
| 491 | /// EXCEPTION_CONTINUE_SEARCH (0): |
| 492 | /// Continue searching this table or continue unwinding. |
| 493 | /// EXCEPTION_CONTINUE_EXECUTION (-1): |
| 494 | /// Resume execution at the trapping PC. |
| 495 | /// |
| 496 | /// Inferred table structure: |
| 497 | /// struct Table { |
| 498 | /// int NumEntries; |
| 499 | /// struct Entry { |
| 500 | /// imagerel32 LabelStart; |
| 501 | /// imagerel32 LabelEnd; |
Reid Kleckner | f690f50 | 2015-01-22 02:27:44 +0000 | [diff] [blame] | 502 | /// imagerel32 FilterOrFinally; // One means catch-all. |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 503 | /// imagerel32 LabelLPad; // Zero means __finally. |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 504 | /// } Entries[NumEntries]; |
| 505 | /// }; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 506 | void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) { |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 507 | auto &OS = *Asm->OutStreamer; |
| 508 | MCContext &Ctx = Asm->OutContext; |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 509 | const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo(); |
Reid Kleckner | 7850c9f | 2015-12-15 23:40:58 +0000 | [diff] [blame] | 510 | |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 511 | bool VerboseAsm = OS.isVerboseAsm(); |
| 512 | auto AddComment = [&](const Twine &Comment) { |
| 513 | if (VerboseAsm) |
| 514 | OS.AddComment(Comment); |
| 515 | }; |
| 516 | |
Reid Kleckner | 7850c9f | 2015-12-15 23:40:58 +0000 | [diff] [blame] | 517 | // Emit a label assignment with the SEH frame offset so we can use it for |
| 518 | // llvm.x86.seh.recoverfp. |
| 519 | StringRef FLinkageName = |
| 520 | GlobalValue::getRealLinkageName(MF->getFunction()->getName()); |
| 521 | MCSymbol *ParentFrameOffset = |
| 522 | Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName); |
| 523 | const MCExpr *MCOffset = |
| 524 | MCConstantExpr::create(FuncInfo.SEHSetFrameOffset, Ctx); |
| 525 | Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset); |
| 526 | |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 527 | // Use the assembler to compute the number of table entries through label |
| 528 | // difference and division. |
| 529 | MCSymbol *TableBegin = |
| 530 | Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true); |
| 531 | MCSymbol *TableEnd = |
| 532 | Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true); |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 533 | const MCExpr *LabelDiff = getOffset(TableEnd, TableBegin); |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 534 | const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx); |
| 535 | const MCExpr *EntryCount = MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 536 | AddComment("Number of call sites"); |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 537 | OS.EmitValue(EntryCount, 4); |
Reid Kleckner | fc64fae | 2015-10-01 21:38:24 +0000 | [diff] [blame] | 538 | |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 539 | OS.EmitLabel(TableBegin); |
Reid Kleckner | eb7cd6c | 2015-10-09 23:05:54 +0000 | [diff] [blame] | 540 | |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 541 | // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only |
| 542 | // models exceptions from invokes. LLVM also allows arbitrary reordering of |
| 543 | // the code, so our tables end up looking a bit different. Rather than |
| 544 | // trying to match MSVC's tables exactly, we emit a denormalized table. For |
| 545 | // each range of invokes in the same state, we emit table entries for all |
| 546 | // the actions that would be taken in that state. This means our tables are |
| 547 | // slightly bigger, which is OK. |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 548 | const MCSymbol *LastStartLabel = nullptr; |
| 549 | int LastEHState = -1; |
| 550 | // Break out before we enter into a finally funclet. |
| 551 | // FIXME: We need to emit separate EH tables for cleanups. |
| 552 | MachineFunction::const_iterator End = MF->end(); |
| 553 | MachineFunction::const_iterator Stop = std::next(MF->begin()); |
| 554 | while (Stop != End && !Stop->isEHFuncletEntry()) |
| 555 | ++Stop; |
| 556 | for (const auto &StateChange : |
| 557 | InvokeStateChangeIterator::range(FuncInfo, MF->begin(), Stop)) { |
| 558 | // Emit all the actions for the state we just transitioned out of |
| 559 | // if it was not the null state |
| 560 | if (LastEHState != -1) |
| 561 | emitSEHActionsForRange(FuncInfo, LastStartLabel, |
| 562 | StateChange.PreviousEndLabel, LastEHState); |
| 563 | LastStartLabel = StateChange.NewStartLabel; |
| 564 | LastEHState = StateChange.NewState; |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 565 | } |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 566 | |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 567 | OS.EmitLabel(TableEnd); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 568 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 569 | |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 570 | void WinException::emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo, |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 571 | const MCSymbol *BeginLabel, |
| 572 | const MCSymbol *EndLabel, int State) { |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 573 | auto &OS = *Asm->OutStreamer; |
| 574 | MCContext &Ctx = Asm->OutContext; |
| 575 | |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 576 | bool VerboseAsm = OS.isVerboseAsm(); |
| 577 | auto AddComment = [&](const Twine &Comment) { |
| 578 | if (VerboseAsm) |
| 579 | OS.AddComment(Comment); |
| 580 | }; |
| 581 | |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 582 | assert(BeginLabel && EndLabel); |
| 583 | while (State != -1) { |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 584 | const SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State]; |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 585 | const MCExpr *FilterOrFinally; |
| 586 | const MCExpr *ExceptOrNull; |
| 587 | auto *Handler = UME.Handler.get<MachineBasicBlock *>(); |
| 588 | if (UME.IsFinally) { |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 589 | FilterOrFinally = create32bitRef(getMCSymbolForMBB(Asm, Handler)); |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 590 | ExceptOrNull = MCConstantExpr::create(0, Ctx); |
| 591 | } else { |
| 592 | // For an except, the filter can be 1 (catch-all) or a function |
| 593 | // label. |
| 594 | FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter) |
| 595 | : MCConstantExpr::create(1, Ctx); |
| 596 | ExceptOrNull = create32bitRef(Handler->getSymbol()); |
| 597 | } |
| 598 | |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 599 | AddComment("LabelStart"); |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 600 | OS.EmitValue(getLabelPlusOne(BeginLabel), 4); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 601 | AddComment("LabelEnd"); |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 602 | OS.EmitValue(getLabelPlusOne(EndLabel), 4); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 603 | AddComment(UME.IsFinally ? "FinallyFunclet" : UME.Filter ? "FilterFunction" |
| 604 | : "CatchAll"); |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 605 | OS.EmitValue(FilterOrFinally, 4); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 606 | AddComment(UME.IsFinally ? "Null" : "ExceptionHandler"); |
Reid Kleckner | d880dc7 | 2015-10-09 20:39:39 +0000 | [diff] [blame] | 607 | OS.EmitValue(ExceptOrNull, 4); |
| 608 | |
| 609 | assert(UME.ToState < State && "states should decrease"); |
| 610 | State = UME.ToState; |
| 611 | } |
| 612 | } |
| 613 | |
Reid Kleckner | 60b640b | 2015-05-28 22:47:01 +0000 | [diff] [blame] | 614 | void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) { |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 615 | const Function *F = MF->getFunction(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 616 | auto &OS = *Asm->OutStreamer; |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 617 | const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo(); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 618 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 619 | StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 620 | |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 621 | SmallVector<std::pair<const MCExpr *, int>, 4> IPToStateTable; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 622 | MCSymbol *FuncInfoXData = nullptr; |
| 623 | if (shouldEmitPersonality) { |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 624 | // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from |
| 625 | // IPs to state numbers. |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 626 | FuncInfoXData = |
| 627 | Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName)); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 628 | computeIP2StateTable(MF, FuncInfo, IPToStateTable); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 629 | } else { |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 630 | FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame] | 633 | int UnwindHelpOffset = 0; |
| 634 | if (Asm->MAI->usesWindowsCFI()) |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 635 | UnwindHelpOffset = |
| 636 | getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx, FuncInfo); |
Reid Kleckner | 70bf6bb | 2015-10-07 21:13:15 +0000 | [diff] [blame] | 637 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 638 | MCSymbol *UnwindMapXData = nullptr; |
| 639 | MCSymbol *TryBlockMapXData = nullptr; |
| 640 | MCSymbol *IPToStateXData = nullptr; |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 641 | if (!FuncInfo.CxxUnwindMap.empty()) |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 642 | UnwindMapXData = Asm->OutContext.getOrCreateSymbol( |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 643 | Twine("$stateUnwindMap$", FuncLinkageName)); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 644 | if (!FuncInfo.TryBlockMap.empty()) |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 645 | TryBlockMapXData = |
| 646 | Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName)); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 647 | if (!IPToStateTable.empty()) |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 648 | IPToStateXData = |
| 649 | Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName)); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 650 | |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 651 | bool VerboseAsm = OS.isVerboseAsm(); |
| 652 | auto AddComment = [&](const Twine &Comment) { |
| 653 | if (VerboseAsm) |
| 654 | OS.AddComment(Comment); |
| 655 | }; |
| 656 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 657 | // FuncInfo { |
| 658 | // uint32_t MagicNumber |
| 659 | // int32_t MaxState; |
| 660 | // UnwindMapEntry *UnwindMap; |
| 661 | // uint32_t NumTryBlocks; |
| 662 | // TryBlockMapEntry *TryBlockMap; |
| 663 | // uint32_t IPMapEntries; // always 0 for x86 |
| 664 | // IPToStateMapEntry *IPToStateMap; // always 0 for x86 |
| 665 | // uint32_t UnwindHelp; // non-x86 only |
| 666 | // ESTypeList *ESTypeList; |
| 667 | // int32_t EHFlags; |
| 668 | // } |
| 669 | // EHFlags & 1 -> Synchronous exceptions only, no async exceptions. |
| 670 | // EHFlags & 2 -> ??? |
| 671 | // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue. |
Reid Kleckner | 85a2450 | 2015-07-10 00:08:49 +0000 | [diff] [blame] | 672 | OS.EmitValueToAlignment(4); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 673 | OS.EmitLabel(FuncInfoXData); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 674 | |
| 675 | AddComment("MagicNumber"); |
| 676 | OS.EmitIntValue(0x19930522, 4); |
| 677 | |
| 678 | AddComment("MaxState"); |
| 679 | OS.EmitIntValue(FuncInfo.CxxUnwindMap.size(), 4); |
| 680 | |
| 681 | AddComment("UnwindMap"); |
| 682 | OS.EmitValue(create32bitRef(UnwindMapXData), 4); |
| 683 | |
| 684 | AddComment("NumTryBlocks"); |
| 685 | OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); |
| 686 | |
| 687 | AddComment("TryBlockMap"); |
| 688 | OS.EmitValue(create32bitRef(TryBlockMapXData), 4); |
| 689 | |
| 690 | AddComment("IPMapEntries"); |
| 691 | OS.EmitIntValue(IPToStateTable.size(), 4); |
| 692 | |
| 693 | AddComment("IPToStateXData"); |
| 694 | OS.EmitValue(create32bitRef(IPToStateXData), 4); |
| 695 | |
| 696 | if (Asm->MAI->usesWindowsCFI()) { |
| 697 | AddComment("UnwindHelp"); |
| 698 | OS.EmitIntValue(UnwindHelpOffset, 4); |
| 699 | } |
| 700 | |
| 701 | AddComment("ESTypeList"); |
| 702 | OS.EmitIntValue(0, 4); |
| 703 | |
| 704 | AddComment("EHFlags"); |
| 705 | OS.EmitIntValue(1, 4); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 706 | |
| 707 | // UnwindMapEntry { |
| 708 | // int32_t ToState; |
| 709 | // void (*Action)(); |
| 710 | // }; |
| 711 | if (UnwindMapXData) { |
| 712 | OS.EmitLabel(UnwindMapXData); |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 713 | for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) { |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 714 | MCSymbol *CleanupSym = |
| 715 | getMCSymbolForMBB(Asm, UME.Cleanup.dyn_cast<MachineBasicBlock *>()); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 716 | AddComment("ToState"); |
| 717 | OS.EmitIntValue(UME.ToState, 4); |
| 718 | |
| 719 | AddComment("Action"); |
| 720 | OS.EmitValue(create32bitRef(CleanupSym), 4); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | // TryBlockMap { |
| 725 | // int32_t TryLow; |
| 726 | // int32_t TryHigh; |
| 727 | // int32_t CatchHigh; |
| 728 | // int32_t NumCatches; |
| 729 | // HandlerType *HandlerArray; |
| 730 | // }; |
| 731 | if (TryBlockMapXData) { |
| 732 | OS.EmitLabel(TryBlockMapXData); |
| 733 | SmallVector<MCSymbol *, 1> HandlerMaps; |
| 734 | for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) { |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 735 | const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I]; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 736 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 737 | MCSymbol *HandlerMapXData = nullptr; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 738 | if (!TBME.HandlerArray.empty()) |
| 739 | HandlerMapXData = |
| 740 | Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$") |
| 741 | .concat(Twine(I)) |
| 742 | .concat("$") |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 743 | .concat(FuncLinkageName)); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 744 | HandlerMaps.push_back(HandlerMapXData); |
| 745 | |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 746 | // TBMEs should form intervals. |
| 747 | assert(0 <= TBME.TryLow && "bad trymap interval"); |
| 748 | assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval"); |
| 749 | assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval"); |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 750 | assert(TBME.CatchHigh < int(FuncInfo.CxxUnwindMap.size()) && |
Reid Kleckner | 813f1b6 | 2015-09-16 22:14:46 +0000 | [diff] [blame] | 751 | "bad trymap interval"); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 752 | |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 753 | AddComment("TryLow"); |
| 754 | OS.EmitIntValue(TBME.TryLow, 4); |
| 755 | |
| 756 | AddComment("TryHigh"); |
| 757 | OS.EmitIntValue(TBME.TryHigh, 4); |
| 758 | |
| 759 | AddComment("CatchHigh"); |
| 760 | OS.EmitIntValue(TBME.CatchHigh, 4); |
| 761 | |
| 762 | AddComment("NumCatches"); |
| 763 | OS.EmitIntValue(TBME.HandlerArray.size(), 4); |
| 764 | |
| 765 | AddComment("HandlerArray"); |
| 766 | OS.EmitValue(create32bitRef(HandlerMapXData), 4); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Reid Kleckner | 28e4903 | 2015-10-16 23:43:27 +0000 | [diff] [blame] | 769 | // All funclets use the same parent frame offset currently. |
| 770 | unsigned ParentFrameOffset = 0; |
| 771 | if (shouldEmitPersonality) { |
| 772 | const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering(); |
| 773 | ParentFrameOffset = TFI->getWinEHParentFrameOffset(*MF); |
| 774 | } |
| 775 | |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 776 | for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) { |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 777 | const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I]; |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 778 | MCSymbol *HandlerMapXData = HandlerMaps[I]; |
| 779 | if (!HandlerMapXData) |
| 780 | continue; |
| 781 | // HandlerType { |
| 782 | // int32_t Adjectives; |
| 783 | // TypeDescriptor *Type; |
| 784 | // int32_t CatchObjOffset; |
| 785 | // void (*Handler)(); |
| 786 | // int32_t ParentFrameOffset; // x64 only |
| 787 | // }; |
| 788 | OS.EmitLabel(HandlerMapXData); |
| 789 | for (const WinEHHandlerType &HT : TBME.HandlerArray) { |
| 790 | // Get the frame escape label with the offset of the catch object. If |
David Majnemer | 99c1d13 | 2015-10-12 16:44:22 +0000 | [diff] [blame] | 791 | // the index is INT_MAX, then there is no catch object, and we should |
| 792 | // emit an offset of zero, indicating that no copy will occur. |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 793 | const MCExpr *FrameAllocOffsetRef = nullptr; |
David Majnemer | 99c1d13 | 2015-10-12 16:44:22 +0000 | [diff] [blame] | 794 | if (HT.CatchObj.FrameIndex != INT_MAX) { |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 795 | int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex, FuncInfo); |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 796 | FrameAllocOffsetRef = MCConstantExpr::create(Offset, Asm->OutContext); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 797 | } else { |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 798 | FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 799 | } |
| 800 | |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 801 | MCSymbol *HandlerSym = |
| 802 | getMCSymbolForMBB(Asm, HT.Handler.dyn_cast<MachineBasicBlock *>()); |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 803 | |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 804 | AddComment("Adjectives"); |
| 805 | OS.EmitIntValue(HT.Adjectives, 4); |
| 806 | |
| 807 | AddComment("Type"); |
| 808 | OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4); |
| 809 | |
| 810 | AddComment("CatchObjOffset"); |
| 811 | OS.EmitValue(FrameAllocOffsetRef, 4); |
| 812 | |
| 813 | AddComment("Handler"); |
| 814 | OS.EmitValue(create32bitRef(HandlerSym), 4); |
| 815 | |
| 816 | if (shouldEmitPersonality) { |
| 817 | AddComment("ParentFrameOffset"); |
| 818 | OS.EmitIntValue(ParentFrameOffset, 4); |
| 819 | } |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | // IPToStateMapEntry { |
| 825 | // void *IP; |
| 826 | // int32_t State; |
| 827 | // }; |
| 828 | if (IPToStateXData) { |
| 829 | OS.EmitLabel(IPToStateXData); |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 830 | for (auto &IPStatePair : IPToStateTable) { |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 831 | AddComment("IP"); |
| 832 | OS.EmitValue(IPStatePair.first, 4); |
| 833 | AddComment("ToState"); |
| 834 | OS.EmitIntValue(IPStatePair.second, 4); |
Reid Kleckner | 1d3d4ad | 2015-05-29 17:00:57 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 839 | void WinException::computeIP2StateTable( |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 840 | const MachineFunction *MF, const WinEHFuncInfo &FuncInfo, |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 841 | SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) { |
Andrew Kaylor | 762a6be | 2015-05-11 19:41:19 +0000 | [diff] [blame] | 842 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 843 | for (MachineFunction::const_iterator FuncletStart = MF->begin(), |
| 844 | FuncletEnd = MF->begin(), |
| 845 | End = MF->end(); |
| 846 | FuncletStart != End; FuncletStart = FuncletEnd) { |
| 847 | // Find the end of the funclet |
| 848 | while (++FuncletEnd != End) { |
| 849 | if (FuncletEnd->isEHFuncletEntry()) { |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | // Don't emit ip2state entries for cleanup funclets. Any interesting |
| 855 | // exceptional actions in cleanups must be handled in a separate IR |
| 856 | // function. |
| 857 | if (FuncletStart->isCleanupFuncletEntry()) |
| 858 | continue; |
| 859 | |
| 860 | MCSymbol *StartLabel; |
| 861 | int BaseState; |
| 862 | if (FuncletStart == MF->begin()) { |
| 863 | BaseState = NullState; |
| 864 | StartLabel = Asm->getFunctionBegin(); |
| 865 | } else { |
| 866 | auto *FuncletPad = |
| 867 | cast<FuncletPadInst>(FuncletStart->getBasicBlock()->getFirstNonPHI()); |
| 868 | assert(FuncInfo.FuncletBaseStateMap.count(FuncletPad) != 0); |
| 869 | BaseState = FuncInfo.FuncletBaseStateMap.find(FuncletPad)->second; |
| 870 | StartLabel = getMCSymbolForMBB(Asm, &*FuncletStart); |
| 871 | } |
| 872 | assert(StartLabel && "need local function start label"); |
Joseph Tremoulet | 1e2f062 | 2015-10-13 16:44:30 +0000 | [diff] [blame] | 873 | IPToStateTable.push_back( |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 874 | std::make_pair(create32bitRef(StartLabel), BaseState)); |
| 875 | |
| 876 | for (const auto &StateChange : InvokeStateChangeIterator::range( |
| 877 | FuncInfo, FuncletStart, FuncletEnd, BaseState)) { |
| 878 | // Compute the label to report as the start of this entry; use the EH |
| 879 | // start label for the invoke if we have one, otherwise (this is a call |
| 880 | // which may unwind to our caller and does not have an EH start label, so) |
| 881 | // use the previous end label. |
| 882 | const MCSymbol *ChangeLabel = StateChange.NewStartLabel; |
| 883 | if (!ChangeLabel) |
| 884 | ChangeLabel = StateChange.PreviousEndLabel; |
| 885 | // Emit an entry indicating that PCs after 'Label' have this EH state. |
| 886 | IPToStateTable.push_back( |
| 887 | std::make_pair(getLabelPlusOne(ChangeLabel), StateChange.NewState)); |
| 888 | // FIXME: assert that NewState is between CatchLow and CatchHigh. |
| 889 | } |
Reid Kleckner | c71d627 | 2015-09-28 23:56:30 +0000 | [diff] [blame] | 890 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 891 | } |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 892 | |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 893 | void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo, |
| 894 | StringRef FLinkageName) { |
| 895 | // Outlined helpers called by the EH runtime need to know the offset of the EH |
| 896 | // registration in order to recover the parent frame pointer. Now that we know |
| 897 | // we've code generated the parent, we can emit the label assignment that |
| 898 | // those helpers use to get the offset of the registration node. |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 899 | MCContext &Ctx = Asm->OutContext; |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 900 | MCSymbol *ParentFrameOffset = |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 901 | Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName); |
| 902 | unsigned UnusedReg; |
| 903 | const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); |
| 904 | int64_t Offset = TFI->getFrameIndexReference( |
| 905 | *Asm->MF, FuncInfo.EHRegNodeFrameIndex, UnusedReg); |
| 906 | const MCExpr *MCOffset = MCConstantExpr::create(Offset, Ctx); |
| 907 | Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset); |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 910 | /// Emit the language-specific data that _except_handler3 and 4 expect. This is |
| 911 | /// functionally equivalent to the __C_specific_handler table, except it is |
| 912 | /// indexed by state number instead of IP. |
| 913 | void WinException::emitExceptHandlerTable(const MachineFunction *MF) { |
Reid Kleckner | a9d6253 | 2015-06-11 22:32:23 +0000 | [diff] [blame] | 914 | MCStreamer &OS = *Asm->OutStreamer; |
Reid Kleckner | a9d6253 | 2015-06-11 22:32:23 +0000 | [diff] [blame] | 915 | const Function *F = MF->getFunction(); |
Reid Kleckner | a9d6253 | 2015-06-11 22:32:23 +0000 | [diff] [blame] | 916 | StringRef FLinkageName = GlobalValue::getRealLinkageName(F->getName()); |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 917 | |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 918 | bool VerboseAsm = OS.isVerboseAsm(); |
| 919 | auto AddComment = [&](const Twine &Comment) { |
| 920 | if (VerboseAsm) |
| 921 | OS.AddComment(Comment); |
| 922 | }; |
| 923 | |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 924 | const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo(); |
Reid Kleckner | 399a2fe | 2015-06-30 22:46:59 +0000 | [diff] [blame] | 925 | emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 926 | |
| 927 | // Emit the __ehtable label that we use for llvm.x86.seh.lsda. |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 928 | MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName); |
Reid Kleckner | 85a2450 | 2015-07-10 00:08:49 +0000 | [diff] [blame] | 929 | OS.EmitValueToAlignment(4); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 930 | OS.EmitLabel(LSDALabel); |
| 931 | |
Reid Kleckner | 9a1a919 | 2015-07-13 20:41:46 +0000 | [diff] [blame] | 932 | const Function *Per = |
| 933 | dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 934 | StringRef PerName = Per->getName(); |
| 935 | int BaseState = -1; |
| 936 | if (PerName == "_except_handler4") { |
| 937 | // The LSDA for _except_handler4 starts with this struct, followed by the |
| 938 | // scope table: |
| 939 | // |
| 940 | // struct EH4ScopeTable { |
| 941 | // int32_t GSCookieOffset; |
| 942 | // int32_t GSCookieXOROffset; |
| 943 | // int32_t EHCookieOffset; |
| 944 | // int32_t EHCookieXOROffset; |
| 945 | // ScopeTableEntry ScopeRecord[]; |
| 946 | // }; |
| 947 | // |
| 948 | // Only the EHCookieOffset field appears to vary, and it appears to be the |
| 949 | // offset from the final saved SP value to the retaddr. |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 950 | AddComment("GSCookieOffset"); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 951 | OS.EmitIntValue(-2, 4); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 952 | AddComment("GSCookieXOROffset"); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 953 | OS.EmitIntValue(0, 4); |
| 954 | // FIXME: Calculate. |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 955 | AddComment("EHCookieOffset"); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 956 | OS.EmitIntValue(9999, 4); |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 957 | AddComment("EHCookieXOROffset"); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 958 | OS.EmitIntValue(0, 4); |
| 959 | BaseState = -2; |
| 960 | } |
| 961 | |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 962 | assert(!FuncInfo.SEHUnwindMap.empty()); |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 963 | for (const SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) { |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 964 | auto *Handler = UME.Handler.get<MachineBasicBlock *>(); |
| 965 | const MCSymbol *ExceptOrFinally = |
| 966 | UME.IsFinally ? getMCSymbolForMBB(Asm, Handler) : Handler->getSymbol(); |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 967 | // -1 is usually the base state for "unwind to caller", but for |
| 968 | // _except_handler4 it's -2. Do that replacement here if necessary. |
| 969 | int ToState = UME.ToState == -1 ? BaseState : UME.ToState; |
David Majnemer | 081e8fe | 2015-12-27 06:07:12 +0000 | [diff] [blame] | 970 | AddComment("ToState"); |
| 971 | OS.EmitIntValue(ToState, 4); |
| 972 | AddComment(UME.IsFinally ? "Null" : "FilterFunction"); |
| 973 | OS.EmitValue(create32bitRef(UME.Filter), 4); |
| 974 | AddComment(UME.IsFinally ? "FinallyFunclet" : "ExceptionHandler"); |
| 975 | OS.EmitValue(create32bitRef(ExceptOrFinally), 4); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 978 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 979 | static int getTryRank(const WinEHFuncInfo &FuncInfo, int State) { |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 980 | int Rank = 0; |
| 981 | while (State != -1) { |
| 982 | ++Rank; |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 983 | State = FuncInfo.ClrEHUnwindMap[State].TryParentState; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 984 | } |
| 985 | return Rank; |
| 986 | } |
| 987 | |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 988 | static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) { |
| 989 | int LeftRank = getTryRank(FuncInfo, Left); |
| 990 | int RightRank = getTryRank(FuncInfo, Right); |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 991 | |
| 992 | while (LeftRank < RightRank) { |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 993 | Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 994 | --RightRank; |
| 995 | } |
| 996 | |
| 997 | while (RightRank < LeftRank) { |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 998 | Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 999 | --LeftRank; |
| 1000 | } |
| 1001 | |
| 1002 | while (Left != Right) { |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 1003 | Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState; |
| 1004 | Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | return Left; |
| 1008 | } |
| 1009 | |
| 1010 | void WinException::emitCLRExceptionTable(const MachineFunction *MF) { |
| 1011 | // CLR EH "states" are really just IDs that identify handlers/funclets; |
| 1012 | // states, handlers, and funclets all have 1:1 mappings between them, and a |
| 1013 | // handler/funclet's "state" is its index in the ClrEHUnwindMap. |
| 1014 | MCStreamer &OS = *Asm->OutStreamer; |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 1015 | const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo(); |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1016 | MCSymbol *FuncBeginSym = Asm->getFunctionBegin(); |
| 1017 | MCSymbol *FuncEndSym = Asm->getFunctionEnd(); |
| 1018 | |
| 1019 | // A ClrClause describes a protected region. |
| 1020 | struct ClrClause { |
| 1021 | const MCSymbol *StartLabel; // Start of protected region |
| 1022 | const MCSymbol *EndLabel; // End of protected region |
| 1023 | int State; // Index of handler protecting the protected region |
| 1024 | int EnclosingState; // Index of funclet enclosing the protected region |
| 1025 | }; |
| 1026 | SmallVector<ClrClause, 8> Clauses; |
| 1027 | |
| 1028 | // Build a map from handler MBBs to their corresponding states (i.e. their |
| 1029 | // indices in the ClrEHUnwindMap). |
| 1030 | int NumStates = FuncInfo.ClrEHUnwindMap.size(); |
| 1031 | assert(NumStates > 0 && "Don't need exception table!"); |
| 1032 | DenseMap<const MachineBasicBlock *, int> HandlerStates; |
| 1033 | for (int State = 0; State < NumStates; ++State) { |
| 1034 | MachineBasicBlock *HandlerBlock = |
| 1035 | FuncInfo.ClrEHUnwindMap[State].Handler.get<MachineBasicBlock *>(); |
| 1036 | HandlerStates[HandlerBlock] = State; |
| 1037 | // Use this loop through all handlers to verify our assumption (used in |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 1038 | // the MinEnclosingState computation) that enclosing funclets have lower |
| 1039 | // state numbers than their enclosed funclets. |
| 1040 | assert(FuncInfo.ClrEHUnwindMap[State].HandlerParentState < State && |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1041 | "ill-formed state numbering"); |
| 1042 | } |
| 1043 | // Map the main function to the NullState. |
Duncan P. N. Exon Smith | a25ad06 | 2015-10-20 00:36:08 +0000 | [diff] [blame] | 1044 | HandlerStates[&MF->front()] = NullState; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Write out a sentinel indicating the end of the standard (Windows) xdata |
| 1047 | // and the start of the additional (CLR) info. |
| 1048 | OS.EmitIntValue(0xffffffff, 4); |
| 1049 | // Write out the number of funclets |
| 1050 | OS.EmitIntValue(NumStates, 4); |
| 1051 | |
| 1052 | // Walk the machine blocks/instrs, computing and emitting a few things: |
| 1053 | // 1. Emit a list of the offsets to each handler entry, in lexical order. |
| 1054 | // 2. Compute a map (EndSymbolMap) from each funclet to the symbol at its end. |
| 1055 | // 3. Compute the list of ClrClauses, in the required order (inner before |
| 1056 | // outer, earlier before later; the order by which a forward scan with |
| 1057 | // early termination will find the innermost enclosing clause covering |
| 1058 | // a given address). |
| 1059 | // 4. A map (MinClauseMap) from each handler index to the index of the |
| 1060 | // outermost funclet/function which contains a try clause targeting the |
| 1061 | // key handler. This will be used to determine IsDuplicate-ness when |
| 1062 | // emitting ClrClauses. The NullState value is used to indicate that the |
| 1063 | // top-level function contains a try clause targeting the key handler. |
| 1064 | // HandlerStack is a stack of (PendingStartLabel, PendingState) pairs for |
| 1065 | // try regions we entered before entering the PendingState try but which |
| 1066 | // we haven't yet exited. |
| 1067 | SmallVector<std::pair<const MCSymbol *, int>, 4> HandlerStack; |
| 1068 | // EndSymbolMap and MinClauseMap are maps described above. |
| 1069 | std::unique_ptr<MCSymbol *[]> EndSymbolMap(new MCSymbol *[NumStates]); |
| 1070 | SmallVector<int, 4> MinClauseMap((size_t)NumStates, NumStates); |
| 1071 | |
| 1072 | // Visit the root function and each funclet. |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1073 | for (MachineFunction::const_iterator FuncletStart = MF->begin(), |
| 1074 | FuncletEnd = MF->begin(), |
| 1075 | End = MF->end(); |
| 1076 | FuncletStart != End; FuncletStart = FuncletEnd) { |
Duncan P. N. Exon Smith | a25ad06 | 2015-10-20 00:36:08 +0000 | [diff] [blame] | 1077 | int FuncletState = HandlerStates[&*FuncletStart]; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1078 | // Find the end of the funclet |
| 1079 | MCSymbol *EndSymbol = FuncEndSym; |
| 1080 | while (++FuncletEnd != End) { |
| 1081 | if (FuncletEnd->isEHFuncletEntry()) { |
Duncan P. N. Exon Smith | a25ad06 | 2015-10-20 00:36:08 +0000 | [diff] [blame] | 1082 | EndSymbol = getMCSymbolForMBB(Asm, &*FuncletEnd); |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | // Emit the function/funclet end and, if this is a funclet (and not the |
| 1087 | // root function), record it in the EndSymbolMap. |
| 1088 | OS.EmitValue(getOffset(EndSymbol, FuncBeginSym), 4); |
| 1089 | if (FuncletState != NullState) { |
| 1090 | // Record the end of the handler. |
| 1091 | EndSymbolMap[FuncletState] = EndSymbol; |
| 1092 | } |
| 1093 | |
| 1094 | // Walk the state changes in this function/funclet and compute its clauses. |
| 1095 | // Funclets always start in the null state. |
| 1096 | const MCSymbol *CurrentStartLabel = nullptr; |
| 1097 | int CurrentState = NullState; |
| 1098 | assert(HandlerStack.empty()); |
| 1099 | for (const auto &StateChange : |
| 1100 | InvokeStateChangeIterator::range(FuncInfo, FuncletStart, FuncletEnd)) { |
| 1101 | // Close any try regions we're not still under |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 1102 | int StillPendingState = |
| 1103 | getTryAncestor(FuncInfo, CurrentState, StateChange.NewState); |
| 1104 | while (CurrentState != StillPendingState) { |
| 1105 | assert(CurrentState != NullState && |
| 1106 | "Failed to find still-pending state!"); |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1107 | // Close the pending clause |
| 1108 | Clauses.push_back({CurrentStartLabel, StateChange.PreviousEndLabel, |
| 1109 | CurrentState, FuncletState}); |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 1110 | // Now the next-outer try region is current |
| 1111 | CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].TryParentState; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1112 | // Pop the new start label from the handler stack if we've exited all |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 1113 | // inner try regions of the corresponding try region. |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1114 | if (HandlerStack.back().second == CurrentState) |
| 1115 | CurrentStartLabel = HandlerStack.pop_back_val().first; |
| 1116 | } |
| 1117 | |
| 1118 | if (StateChange.NewState != CurrentState) { |
| 1119 | // For each clause we're starting, update the MinClauseMap so we can |
| 1120 | // know which is the topmost funclet containing a clause targeting |
| 1121 | // it. |
| 1122 | for (int EnteredState = StateChange.NewState; |
| 1123 | EnteredState != CurrentState; |
Joseph Tremoulet | 52f729a | 2016-01-04 16:16:01 +0000 | [diff] [blame^] | 1124 | EnteredState = |
| 1125 | FuncInfo.ClrEHUnwindMap[EnteredState].TryParentState) { |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1126 | int &MinEnclosingState = MinClauseMap[EnteredState]; |
| 1127 | if (FuncletState < MinEnclosingState) |
| 1128 | MinEnclosingState = FuncletState; |
| 1129 | } |
| 1130 | // Save the previous current start/label on the stack and update to |
| 1131 | // the newly-current start/state. |
| 1132 | HandlerStack.emplace_back(CurrentStartLabel, CurrentState); |
| 1133 | CurrentStartLabel = StateChange.NewStartLabel; |
| 1134 | CurrentState = StateChange.NewState; |
| 1135 | } |
| 1136 | } |
| 1137 | assert(HandlerStack.empty()); |
| 1138 | } |
| 1139 | |
| 1140 | // Now emit the clause info, starting with the number of clauses. |
| 1141 | OS.EmitIntValue(Clauses.size(), 4); |
| 1142 | for (ClrClause &Clause : Clauses) { |
| 1143 | // Emit a CORINFO_EH_CLAUSE : |
| 1144 | /* |
| 1145 | struct CORINFO_EH_CLAUSE |
| 1146 | { |
| 1147 | CORINFO_EH_CLAUSE_FLAGS Flags; // actually a CorExceptionFlag |
| 1148 | DWORD TryOffset; |
| 1149 | DWORD TryLength; // actually TryEndOffset |
| 1150 | DWORD HandlerOffset; |
| 1151 | DWORD HandlerLength; // actually HandlerEndOffset |
| 1152 | union |
| 1153 | { |
| 1154 | DWORD ClassToken; // use for catch clauses |
| 1155 | DWORD FilterOffset; // use for filter clauses |
| 1156 | }; |
| 1157 | }; |
| 1158 | |
| 1159 | enum CORINFO_EH_CLAUSE_FLAGS |
| 1160 | { |
| 1161 | CORINFO_EH_CLAUSE_NONE = 0, |
| 1162 | CORINFO_EH_CLAUSE_FILTER = 0x0001, // This clause is for a filter |
| 1163 | CORINFO_EH_CLAUSE_FINALLY = 0x0002, // This clause is a finally clause |
| 1164 | CORINFO_EH_CLAUSE_FAULT = 0x0004, // This clause is a fault clause |
| 1165 | }; |
| 1166 | typedef enum CorExceptionFlag |
| 1167 | { |
| 1168 | COR_ILEXCEPTION_CLAUSE_NONE, |
| 1169 | COR_ILEXCEPTION_CLAUSE_FILTER = 0x0001, // This is a filter clause |
| 1170 | COR_ILEXCEPTION_CLAUSE_FINALLY = 0x0002, // This is a finally clause |
| 1171 | COR_ILEXCEPTION_CLAUSE_FAULT = 0x0004, // This is a fault clause |
| 1172 | COR_ILEXCEPTION_CLAUSE_DUPLICATED = 0x0008, // duplicated clause. This |
| 1173 | // clause was duplicated |
| 1174 | // to a funclet which was |
| 1175 | // pulled out of line |
| 1176 | } CorExceptionFlag; |
| 1177 | */ |
| 1178 | // Add 1 to the start/end of the EH clause; the IP associated with a |
| 1179 | // call when the runtime does its scan is the IP of the next instruction |
| 1180 | // (the one to which control will return after the call), so we need |
| 1181 | // to add 1 to the end of the clause to cover that offset. We also add |
| 1182 | // 1 to the start of the clause to make sure that the ranges reported |
| 1183 | // for all clauses are disjoint. Note that we'll need some additional |
| 1184 | // logic when machine traps are supported, since in that case the IP |
| 1185 | // that the runtime uses is the offset of the faulting instruction |
| 1186 | // itself; if such an instruction immediately follows a call but the |
| 1187 | // two belong to different clauses, we'll need to insert a nop between |
| 1188 | // them so the runtime can distinguish the point to which the call will |
| 1189 | // return from the point at which the fault occurs. |
| 1190 | |
| 1191 | const MCExpr *ClauseBegin = |
| 1192 | getOffsetPlusOne(Clause.StartLabel, FuncBeginSym); |
| 1193 | const MCExpr *ClauseEnd = getOffsetPlusOne(Clause.EndLabel, FuncBeginSym); |
| 1194 | |
Reid Kleckner | c20276d | 2015-11-17 21:10:25 +0000 | [diff] [blame] | 1195 | const ClrEHUnwindMapEntry &Entry = FuncInfo.ClrEHUnwindMap[Clause.State]; |
Joseph Tremoulet | 28c89bb | 2015-10-13 20:18:27 +0000 | [diff] [blame] | 1196 | MachineBasicBlock *HandlerBlock = Entry.Handler.get<MachineBasicBlock *>(); |
| 1197 | MCSymbol *BeginSym = getMCSymbolForMBB(Asm, HandlerBlock); |
| 1198 | const MCExpr *HandlerBegin = getOffset(BeginSym, FuncBeginSym); |
| 1199 | MCSymbol *EndSym = EndSymbolMap[Clause.State]; |
| 1200 | const MCExpr *HandlerEnd = getOffset(EndSym, FuncBeginSym); |
| 1201 | |
| 1202 | uint32_t Flags = 0; |
| 1203 | switch (Entry.HandlerType) { |
| 1204 | case ClrHandlerType::Catch: |
| 1205 | // Leaving bits 0-2 clear indicates catch. |
| 1206 | break; |
| 1207 | case ClrHandlerType::Filter: |
| 1208 | Flags |= 1; |
| 1209 | break; |
| 1210 | case ClrHandlerType::Finally: |
| 1211 | Flags |= 2; |
| 1212 | break; |
| 1213 | case ClrHandlerType::Fault: |
| 1214 | Flags |= 4; |
| 1215 | break; |
| 1216 | } |
| 1217 | if (Clause.EnclosingState != MinClauseMap[Clause.State]) { |
| 1218 | // This is a "duplicate" clause; the handler needs to be entered from a |
| 1219 | // frame above the one holding the invoke. |
| 1220 | assert(Clause.EnclosingState > MinClauseMap[Clause.State]); |
| 1221 | Flags |= 8; |
| 1222 | } |
| 1223 | OS.EmitIntValue(Flags, 4); |
| 1224 | |
| 1225 | // Write the clause start/end |
| 1226 | OS.EmitValue(ClauseBegin, 4); |
| 1227 | OS.EmitValue(ClauseEnd, 4); |
| 1228 | |
| 1229 | // Write out the handler start/end |
| 1230 | OS.EmitValue(HandlerBegin, 4); |
| 1231 | OS.EmitValue(HandlerEnd, 4); |
| 1232 | |
| 1233 | // Write out the type token or filter offset |
| 1234 | assert(Entry.HandlerType != ClrHandlerType::Filter && "NYI: filters"); |
| 1235 | OS.EmitIntValue(Entry.TypeToken, 4); |
| 1236 | } |
| 1237 | } |