blob: e299417241544c27ec424b76fc908d1e21a5df8d [file] [log] [blame]
Reid Kleckner60b640b2015-05-28 22:47:01 +00001//===-- CodeGen/AsmPrinter/WinException.cpp - Dwarf Exception Impl ------===//
Charles Davis91ed7992011-05-27 23:47:32 +00002//
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 Kleckner60b640b2015-05-28 22:47:01 +000014#include "WinException.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Charles Davis91ed7992011-05-27 23:47:32 +000018#include "llvm/CodeGen/AsmPrinter.h"
Charles Davis91ed7992011-05-27 23:47:32 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
David Majnemercde33032015-03-30 22:58:10 +000022#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000024#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Module.h"
Charles Davis91ed7992011-05-27 23:47:32 +000026#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 Kleckner1d3d4ad2015-05-29 17:00:57 +000032#include "llvm/MC/MCWin64EH.h"
David Majnemera80c1512015-09-29 20:12:33 +000033#include "llvm/Support/COFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/Dwarf.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
Charles Davis91ed7992011-05-27 23:47:32 +000037#include "llvm/Target/TargetFrameLowering.h"
38#include "llvm/Target/TargetLoweringObjectFile.h"
Charles Davis91ed7992011-05-27 23:47:32 +000039#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegisterInfo.h"
Reid Kleckner70bf6bb2015-10-07 21:13:15 +000041#include "llvm/Target/TargetSubtargetInfo.h"
Charles Davis91ed7992011-05-27 23:47:32 +000042using namespace llvm;
43
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000044WinException::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 Davis91ed7992011-05-27 23:47:32 +000049
Reid Kleckner60b640b2015-05-28 22:47:01 +000050WinException::~WinException() {}
Charles Davis91ed7992011-05-27 23:47:32 +000051
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000052/// endModule - Emit all exception information that should come after the
Charles Davis91ed7992011-05-27 23:47:32 +000053/// content.
Reid Kleckner60b640b2015-05-28 22:47:01 +000054void WinException::endModule() {
Reid Kleckner2bc93ca2015-06-10 01:02:30 +000055 auto &OS = *Asm->OutStreamer;
56 const Module *M = MMI->getModule();
Reid Klecknerca6ef662015-06-10 01:13:44 +000057 for (const Function &F : *M)
58 if (F.hasFnAttribute("safeseh"))
Reid Kleckner2bc93ca2015-06-10 01:02:30 +000059 OS.EmitCOFFSafeSEH(Asm->getSymbol(&F));
Charles Davis91ed7992011-05-27 23:47:32 +000060}
61
Reid Kleckner60b640b2015-05-28 22:47:01 +000062void WinException::beginFunction(const MachineFunction *MF) {
Charles Davis5638b9f2011-05-28 04:21:04 +000063 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
64
65 // If any landing pads survive, we need an EH table.
66 bool hasLandingPads = !MMI->getLandingPads().empty();
Reid Kleckner0e288232015-08-27 23:27:47 +000067 bool hasEHFunclets = MMI->hasEHFunclets();
Charles Davis5638b9f2011-05-28 04:21:04 +000068
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000069 const Function *F = MF->getFunction();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000070
Charles Davis5638b9f2011-05-28 04:21:04 +000071 shouldEmitMoves = Asm->needsSEHMoves();
72
73 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
74 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Reid Kleckner9a1a9192015-07-13 20:41:46 +000075 const Function *Per = nullptr;
76 if (F->hasPersonalityFn())
77 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Charles Davis5638b9f2011-05-28 04:21:04 +000078
Keno Fischeraff703a2015-07-14 19:22:51 +000079 bool forceEmitPersonality =
80 F->hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
81 F->needsUnwindTableEntry();
82
Reid Kleckner0e288232015-08-27 23:27:47 +000083 shouldEmitPersonality =
84 forceEmitPersonality || ((hasLandingPads || hasEHFunclets) &&
85 PerEncoding != dwarf::DW_EH_PE_omit && Per);
Charles Davis5638b9f2011-05-28 04:21:04 +000086
87 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
88 shouldEmitLSDA = shouldEmitPersonality &&
89 LSDAEncoding != dwarf::DW_EH_PE_omit;
90
Reid Kleckner0e288232015-08-27 23:27:47 +000091 // 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 Kleckner1d3d4ad2015-05-29 17:00:57 +000093 if (!Asm->MAI->usesWindowsCFI()) {
David Majnemerbfa5b982015-10-10 00:04:29 +000094 shouldEmitLSDA = hasEHFunclets;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000095 shouldEmitPersonality = false;
96 return;
97 }
David Majnemera225a192015-03-31 22:35:44 +000098
David Majnemera80c1512015-09-29 20:12:33 +000099 beginFunclet(MF->front(), Asm->CurrentFnSym);
Charles Davis91ed7992011-05-27 23:47:32 +0000100}
101
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000102/// endFunction - Gather and emit post-function exception information.
Charles Davis91ed7992011-05-27 23:47:32 +0000103///
Reid Kleckner60b640b2015-05-28 22:47:01 +0000104void WinException::endFunction(const MachineFunction *MF) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000105 if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
Charles Davis5638b9f2011-05-28 04:21:04 +0000106 return;
107
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000108 const Function *F = MF->getFunction();
109 EHPersonality Per = EHPersonality::Unknown;
110 if (F->hasPersonalityFn())
111 Per = classifyEHPersonality(F->getPersonalityFn());
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000112
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000113 // 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 Kleckner3e9fadf2015-04-15 18:48:15 +0000117 MMI->TidyLandingPads();
Charles Davisa5752262011-05-30 00:13:34 +0000118
David Majnemera80c1512015-09-29 20:12:33 +0000119 endFunclet();
120
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000121 // endFunclet will emit the necessary .xdata tables for x64 SEH.
122 if (Per == EHPersonality::MSVC_Win64SEH && MMI->hasEHFunclets())
123 return;
124
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000125 if (shouldEmitPersonality || shouldEmitLSDA) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000126 Asm->OutStreamer->PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000127
David Majnemera80c1512015-09-29 20:12:33 +0000128 // 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 Kleckner0a57f652015-01-14 01:05:27 +0000133
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000134 // 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 Kleckner2d5fb682015-02-14 00:21:02 +0000136 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner94b704c2015-09-09 21:10:03 +0000137 emitCSpecificHandlerTable(MF);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000138 else if (Per == EHPersonality::MSVC_X86SEH)
Reid Klecknerf12c0302015-06-09 21:42:19 +0000139 emitExceptHandlerTable(MF);
David Majnemercde33032015-03-30 22:58:10 +0000140 else if (Per == EHPersonality::MSVC_CXX)
141 emitCXXFrameHandler3Table(MF);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000142 else if (Per == EHPersonality::CoreCLR)
143 emitCLRExceptionTable(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000144 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000145 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000146
Lang Hames9ff69c82015-04-24 19:11:51 +0000147 Asm->OutStreamer->PopSection();
Charles Davisa5752262011-05-30 00:13:34 +0000148 }
David Majnemera80c1512015-09-29 20:12:33 +0000149}
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000150
David Majnemer99c1d132015-10-12 16:44:22 +0000151/// Retreive the MCSymbol for a GlobalValue or MachineBasicBlock.
David Majnemerbfa5b982015-10-10 00:04:29 +0000152static MCSymbol *getMCSymbolForMBB(AsmPrinter *Asm,
153 const MachineBasicBlock *MBB) {
154 if (!MBB)
David Majnemera80c1512015-09-29 20:12:33 +0000155 return nullptr;
David Majnemera80c1512015-09-29 20:12:33 +0000156
David Majnemerbfa5b982015-10-10 00:04:29 +0000157 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 Majnemera80c1512015-09-29 20:12:33 +0000169}
170
171void 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 Majnemerbfa5b982015-10-10 00:04:29 +0000178 Sym = getMCSymbolForMBB(Asm, &MBB);
David Majnemera80c1512015-09-29 20:12:33 +0000179
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 Majnemer0e705982015-09-11 17:34:34 +0000197 if (shouldEmitMoves || shouldEmitPersonality)
David Majnemera80c1512015-09-29 20:12:33 +0000198 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
222void 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 Majnemera80c1512015-09-29 20:12:33 +0000240 if (Per == EHPersonality::MSVC_CXX && shouldEmitPersonality &&
241 !CurrentFuncletEntry->isCleanupFuncletEntry()) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000242 // If this is a C++ catch funclet (or the parent function),
243 // emit a reference to the LSDA for the parent function.
David Majnemera80c1512015-09-29 20:12:33 +0000244 StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName());
245 MCSymbol *FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
246 Twine("$cppxdata$", FuncLinkageName));
247 Asm->OutStreamer->EmitValue(create32bitRef(FuncInfoXData), 4);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000248 } 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 Majnemera80c1512015-09-29 20:12:33 +0000253 }
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 Kleckner1d3d4ad2015-05-29 17:00:57 +0000260 Asm->OutStreamer->EmitWinCFIEndProc();
David Majnemera80c1512015-09-29 20:12:33 +0000261 }
262
263 // Let's make sure we don't try to end the same funclet twice.
264 CurrentFuncletEntry = nullptr;
Charles Davis91ed7992011-05-27 23:47:32 +0000265}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000266
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000267const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
David Majnemercde33032015-03-30 22:58:10 +0000268 if (!Value)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000269 return MCConstantExpr::create(0, Asm->OutContext);
270 return MCSymbolRefExpr::create(Value, useImageRel32
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000271 ? MCSymbolRefExpr::VK_COFF_IMGREL32
272 : MCSymbolRefExpr::VK_None,
Reid Kleckner0a57f652015-01-14 01:05:27 +0000273 Asm->OutContext);
274}
275
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000276const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
277 if (!GV)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000278 return MCConstantExpr::create(0, Asm->OutContext);
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000279 return create32bitRef(Asm->getSymbol(GV));
David Majnemercde33032015-03-30 22:58:10 +0000280}
281
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000282const MCExpr *WinException::getLabelPlusOne(const MCSymbol *Label) {
Reid Klecknerc71d6272015-09-28 23:56:30 +0000283 return MCBinaryExpr::createAdd(create32bitRef(Label),
284 MCConstantExpr::create(1, Asm->OutContext),
285 Asm->OutContext);
286}
287
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000288const 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
295const 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 Klecknerc20276d2015-11-17 21:10:25 +0000302int WinException::getFrameIndexOffset(int FrameIndex,
303 const WinEHFuncInfo &FuncInfo) {
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000304 const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering();
305 unsigned UnusedReg;
306 if (Asm->MAI->usesWindowsCFI())
307 return TFI.getFrameIndexReferenceFromSP(*Asm->MF, FrameIndex, UnusedReg);
Reid Kleckner6ddae312015-11-05 21:09:49 +0000308 // 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 Kleckner70bf6bb2015-10-07 21:13:15 +0000314}
315
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000316namespace {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000317
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000318/// Top-level state used to represent unwind to caller
319const int NullState = -1;
320
321struct 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 Klecknerfc64fae2015-10-01 21:38:24 +0000333};
334
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000335/// 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).
343class InvokeStateChangeIterator {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000344 InvokeStateChangeIterator(const WinEHFuncInfo &EHInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000345 MachineFunction::const_iterator MFI,
346 MachineFunction::const_iterator MFE,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000347 MachineBasicBlock::const_iterator MBBI,
348 int BaseState)
349 : EHInfo(EHInfo), MFI(MFI), MFE(MFE), MBBI(MBBI), BaseState(BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000350 LastStateChange.PreviousEndLabel = nullptr;
351 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000352 LastStateChange.NewState = BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000353 scan();
354 }
355
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000356public:
357 static iterator_range<InvokeStateChangeIterator>
Reid Klecknerc20276d2015-11-17 21:10:25 +0000358 range(const WinEHFuncInfo &EHInfo, MachineFunction::const_iterator Begin,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000359 MachineFunction::const_iterator End, int BaseState = NullState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000360 // 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 Majnemer8a1c45d2015-12-12 05:38:55 +0000365 return make_range(
366 InvokeStateChangeIterator(EHInfo, Begin, End, BlockBegin, BaseState),
367 InvokeStateChangeIterator(EHInfo, End, End, BlockEnd, BaseState));
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000368 }
369
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000370 // Iterator methods.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000371 bool operator==(const InvokeStateChangeIterator &O) const {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000372 assert(BaseState == O.BaseState);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000373 // 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 Klecknerfc64fae2015-10-01 21:38:24 +0000392
393private:
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000394 InvokeStateChangeIterator &scan();
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000395
Reid Klecknerc20276d2015-11-17 21:10:25 +0000396 const WinEHFuncInfo &EHInfo;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000397 const MCSymbol *CurrentEndLabel = nullptr;
398 MachineFunction::const_iterator MFI;
399 MachineFunction::const_iterator MFE;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000400 MachineBasicBlock::const_iterator MBBI;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000401 InvokeStateChange LastStateChange;
402 bool VisitingInvoke = false;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000403 int BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000404};
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000405
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000406} // end anonymous namespace
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000407
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000408InvokeStateChangeIterator &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 Majnemer8a1c45d2015-12-12 05:38:55 +0000415 if (!VisitingInvoke && LastStateChange.NewState != BaseState &&
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000416 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 Majnemer8a1c45d2015-12-12 05:38:55 +0000422 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000423 CurrentEndLabel = nullptr;
424 // Don't re-visit this instr on the next scan
425 ++MBBI;
426 return *this;
427 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000428
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000429 // 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 Majnemer8a1c45d2015-12-12 05:38:55 +0000437 auto InvokeMapIter = EHInfo.LabelToStateMap.find(Label);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000438 // Ignore EH labels that aren't the ones inserted before an invoke
David Majnemer8a1c45d2015-12-12 05:38:55 +0000439 if (InvokeMapIter == EHInfo.LabelToStateMap.end())
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000440 continue;
441 auto &StateAndEnd = InvokeMapIter->second;
442 int NewState = StateAndEnd.first;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000443 // 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 Klecknerfc64fae2015-10-01 21:38:24 +0000461 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000462 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000463 // Iteration hit the end of the block range.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000464 if (LastStateChange.NewState != BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000465 // Report the end of the last new state
466 LastStateChange.PreviousEndLabel = CurrentEndLabel;
467 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000468 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000469 // Leave CurrentEndLabel non-null to distinguish this state from end.
470 assert(CurrentEndLabel != nullptr);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000471 return *this;
472 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000473 // We've reported all state changes and hit the end state.
474 CurrentEndLabel = nullptr;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000475 return *this;
476}
477
Reid Kleckner0a57f652015-01-14 01:05:27 +0000478/// 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 Klecknerf690f502015-01-22 02:27:44 +0000502/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner14e77352015-10-09 23:34:53 +0000503/// imagerel32 LabelLPad; // Zero means __finally.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000504/// } Entries[NumEntries];
505/// };
Reid Kleckner94b704c2015-09-09 21:10:03 +0000506void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000507 auto &OS = *Asm->OutStreamer;
508 MCContext &Ctx = Asm->OutContext;
Reid Kleckner0a57f652015-01-14 01:05:27 +0000509
Reid Klecknerc20276d2015-11-17 21:10:25 +0000510 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner14e77352015-10-09 23:34:53 +0000511 // Use the assembler to compute the number of table entries through label
512 // difference and division.
513 MCSymbol *TableBegin =
514 Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
515 MCSymbol *TableEnd =
516 Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000517 const MCExpr *LabelDiff = getOffset(TableEnd, TableBegin);
Reid Kleckner14e77352015-10-09 23:34:53 +0000518 const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx);
519 const MCExpr *EntryCount = MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx);
520 OS.EmitValue(EntryCount, 4);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000521
Reid Kleckner14e77352015-10-09 23:34:53 +0000522 OS.EmitLabel(TableBegin);
Reid Klecknereb7cd6c2015-10-09 23:05:54 +0000523
Reid Kleckner14e77352015-10-09 23:34:53 +0000524 // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
525 // models exceptions from invokes. LLVM also allows arbitrary reordering of
526 // the code, so our tables end up looking a bit different. Rather than
527 // trying to match MSVC's tables exactly, we emit a denormalized table. For
528 // each range of invokes in the same state, we emit table entries for all
529 // the actions that would be taken in that state. This means our tables are
530 // slightly bigger, which is OK.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000531 const MCSymbol *LastStartLabel = nullptr;
532 int LastEHState = -1;
533 // Break out before we enter into a finally funclet.
534 // FIXME: We need to emit separate EH tables for cleanups.
535 MachineFunction::const_iterator End = MF->end();
536 MachineFunction::const_iterator Stop = std::next(MF->begin());
537 while (Stop != End && !Stop->isEHFuncletEntry())
538 ++Stop;
539 for (const auto &StateChange :
540 InvokeStateChangeIterator::range(FuncInfo, MF->begin(), Stop)) {
541 // Emit all the actions for the state we just transitioned out of
542 // if it was not the null state
543 if (LastEHState != -1)
544 emitSEHActionsForRange(FuncInfo, LastStartLabel,
545 StateChange.PreviousEndLabel, LastEHState);
546 LastStartLabel = StateChange.NewStartLabel;
547 LastEHState = StateChange.NewState;
Reid Kleckner0a57f652015-01-14 01:05:27 +0000548 }
Reid Kleckner14e77352015-10-09 23:34:53 +0000549
Reid Kleckner14e77352015-10-09 23:34:53 +0000550 OS.EmitLabel(TableEnd);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000551}
David Majnemercde33032015-03-30 22:58:10 +0000552
Reid Klecknerc20276d2015-11-17 21:10:25 +0000553void WinException::emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000554 const MCSymbol *BeginLabel,
555 const MCSymbol *EndLabel, int State) {
Reid Klecknerd880dc72015-10-09 20:39:39 +0000556 auto &OS = *Asm->OutStreamer;
557 MCContext &Ctx = Asm->OutContext;
558
559 assert(BeginLabel && EndLabel);
560 while (State != -1) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000561 const SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
Reid Klecknerd880dc72015-10-09 20:39:39 +0000562 const MCExpr *FilterOrFinally;
563 const MCExpr *ExceptOrNull;
564 auto *Handler = UME.Handler.get<MachineBasicBlock *>();
565 if (UME.IsFinally) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000566 FilterOrFinally = create32bitRef(getMCSymbolForMBB(Asm, Handler));
Reid Klecknerd880dc72015-10-09 20:39:39 +0000567 ExceptOrNull = MCConstantExpr::create(0, Ctx);
568 } else {
569 // For an except, the filter can be 1 (catch-all) or a function
570 // label.
571 FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
572 : MCConstantExpr::create(1, Ctx);
573 ExceptOrNull = create32bitRef(Handler->getSymbol());
574 }
575
576 OS.EmitValue(getLabelPlusOne(BeginLabel), 4);
577 OS.EmitValue(getLabelPlusOne(EndLabel), 4);
578 OS.EmitValue(FilterOrFinally, 4);
579 OS.EmitValue(ExceptOrNull, 4);
580
581 assert(UME.ToState < State && "states should decrease");
582 State = UME.ToState;
583 }
584}
585
Reid Kleckner60b640b2015-05-28 22:47:01 +0000586void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
David Majnemercde33032015-03-30 22:58:10 +0000587 const Function *F = MF->getFunction();
Lang Hames9ff69c82015-04-24 19:11:51 +0000588 auto &OS = *Asm->OutStreamer;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000589 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
David Majnemercde33032015-03-30 22:58:10 +0000590
Reid Kleckner813f1b62015-09-16 22:14:46 +0000591 StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName());
David Majnemercde33032015-03-30 22:58:10 +0000592
Reid Klecknerc71d6272015-09-28 23:56:30 +0000593 SmallVector<std::pair<const MCExpr *, int>, 4> IPToStateTable;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000594 MCSymbol *FuncInfoXData = nullptr;
595 if (shouldEmitPersonality) {
Reid Klecknerc71d6272015-09-28 23:56:30 +0000596 // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from
597 // IPs to state numbers.
Reid Kleckner813f1b62015-09-16 22:14:46 +0000598 FuncInfoXData =
599 Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000600 computeIP2StateTable(MF, FuncInfo, IPToStateTable);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000601 } else {
Reid Kleckner813f1b62015-09-16 22:14:46 +0000602 FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000603 }
604
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000605 int UnwindHelpOffset = 0;
606 if (Asm->MAI->usesWindowsCFI())
Reid Kleckner6ddae312015-11-05 21:09:49 +0000607 UnwindHelpOffset =
608 getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx, FuncInfo);
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000609
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000610 MCSymbol *UnwindMapXData = nullptr;
611 MCSymbol *TryBlockMapXData = nullptr;
612 MCSymbol *IPToStateXData = nullptr;
Reid Kleckner14e77352015-10-09 23:34:53 +0000613 if (!FuncInfo.CxxUnwindMap.empty())
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000614 UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
Reid Kleckner813f1b62015-09-16 22:14:46 +0000615 Twine("$stateUnwindMap$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000616 if (!FuncInfo.TryBlockMap.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000617 TryBlockMapXData =
618 Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000619 if (!IPToStateTable.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000620 IPToStateXData =
621 Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000622
623 // FuncInfo {
624 // uint32_t MagicNumber
625 // int32_t MaxState;
626 // UnwindMapEntry *UnwindMap;
627 // uint32_t NumTryBlocks;
628 // TryBlockMapEntry *TryBlockMap;
629 // uint32_t IPMapEntries; // always 0 for x86
630 // IPToStateMapEntry *IPToStateMap; // always 0 for x86
631 // uint32_t UnwindHelp; // non-x86 only
632 // ESTypeList *ESTypeList;
633 // int32_t EHFlags;
634 // }
635 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
636 // EHFlags & 2 -> ???
637 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
Reid Kleckner85a24502015-07-10 00:08:49 +0000638 OS.EmitValueToAlignment(4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000639 OS.EmitLabel(FuncInfoXData);
640 OS.EmitIntValue(0x19930522, 4); // MagicNumber
Reid Kleckner14e77352015-10-09 23:34:53 +0000641 OS.EmitIntValue(FuncInfo.CxxUnwindMap.size(), 4); // MaxState
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000642 OS.EmitValue(create32bitRef(UnwindMapXData), 4); // UnwindMap
643 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks
644 OS.EmitValue(create32bitRef(TryBlockMapXData), 4); // TryBlockMap
Reid Klecknerc71d6272015-09-28 23:56:30 +0000645 OS.EmitIntValue(IPToStateTable.size(), 4); // IPMapEntries
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000646 OS.EmitValue(create32bitRef(IPToStateXData), 4); // IPToStateMap
647 if (Asm->MAI->usesWindowsCFI())
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000648 OS.EmitIntValue(UnwindHelpOffset, 4); // UnwindHelp
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000649 OS.EmitIntValue(0, 4); // ESTypeList
650 OS.EmitIntValue(1, 4); // EHFlags
651
652 // UnwindMapEntry {
653 // int32_t ToState;
654 // void (*Action)();
655 // };
656 if (UnwindMapXData) {
657 OS.EmitLabel(UnwindMapXData);
Reid Kleckner14e77352015-10-09 23:34:53 +0000658 for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000659 MCSymbol *CleanupSym =
660 getMCSymbolForMBB(Asm, UME.Cleanup.dyn_cast<MachineBasicBlock *>());
Reid Kleckner78783912015-09-10 00:25:23 +0000661 OS.EmitIntValue(UME.ToState, 4); // ToState
662 OS.EmitValue(create32bitRef(CleanupSym), 4); // Action
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000663 }
664 }
665
666 // TryBlockMap {
667 // int32_t TryLow;
668 // int32_t TryHigh;
669 // int32_t CatchHigh;
670 // int32_t NumCatches;
671 // HandlerType *HandlerArray;
672 // };
673 if (TryBlockMapXData) {
674 OS.EmitLabel(TryBlockMapXData);
675 SmallVector<MCSymbol *, 1> HandlerMaps;
676 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000677 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000678
Reid Kleckner813f1b62015-09-16 22:14:46 +0000679 MCSymbol *HandlerMapXData = nullptr;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000680 if (!TBME.HandlerArray.empty())
681 HandlerMapXData =
682 Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
683 .concat(Twine(I))
684 .concat("$")
Reid Kleckner813f1b62015-09-16 22:14:46 +0000685 .concat(FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000686 HandlerMaps.push_back(HandlerMapXData);
687
Reid Kleckner813f1b62015-09-16 22:14:46 +0000688 // TBMEs should form intervals.
689 assert(0 <= TBME.TryLow && "bad trymap interval");
690 assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval");
691 assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval");
Reid Kleckner14e77352015-10-09 23:34:53 +0000692 assert(TBME.CatchHigh < int(FuncInfo.CxxUnwindMap.size()) &&
Reid Kleckner813f1b62015-09-16 22:14:46 +0000693 "bad trymap interval");
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000694
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000695 OS.EmitIntValue(TBME.TryLow, 4); // TryLow
696 OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh
Reid Kleckner813f1b62015-09-16 22:14:46 +0000697 OS.EmitIntValue(TBME.CatchHigh, 4); // CatchHigh
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000698 OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches
699 OS.EmitValue(create32bitRef(HandlerMapXData), 4); // HandlerArray
700 }
701
Reid Kleckner28e49032015-10-16 23:43:27 +0000702 // All funclets use the same parent frame offset currently.
703 unsigned ParentFrameOffset = 0;
704 if (shouldEmitPersonality) {
705 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
706 ParentFrameOffset = TFI->getWinEHParentFrameOffset(*MF);
707 }
708
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000709 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000710 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000711 MCSymbol *HandlerMapXData = HandlerMaps[I];
712 if (!HandlerMapXData)
713 continue;
714 // HandlerType {
715 // int32_t Adjectives;
716 // TypeDescriptor *Type;
717 // int32_t CatchObjOffset;
718 // void (*Handler)();
719 // int32_t ParentFrameOffset; // x64 only
720 // };
721 OS.EmitLabel(HandlerMapXData);
722 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
723 // Get the frame escape label with the offset of the catch object. If
David Majnemer99c1d132015-10-12 16:44:22 +0000724 // the index is INT_MAX, then there is no catch object, and we should
725 // emit an offset of zero, indicating that no copy will occur.
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000726 const MCExpr *FrameAllocOffsetRef = nullptr;
David Majnemer99c1d132015-10-12 16:44:22 +0000727 if (HT.CatchObj.FrameIndex != INT_MAX) {
Reid Kleckner6ddae312015-11-05 21:09:49 +0000728 int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex, FuncInfo);
Reid Klecknerb005d282015-09-16 20:16:27 +0000729 FrameAllocOffsetRef = MCConstantExpr::create(Offset, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000730 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000731 FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000732 }
733
David Majnemerbfa5b982015-10-10 00:04:29 +0000734 MCSymbol *HandlerSym =
735 getMCSymbolForMBB(Asm, HT.Handler.dyn_cast<MachineBasicBlock *>());
Reid Kleckner94b704c2015-09-09 21:10:03 +0000736
737 OS.EmitIntValue(HT.Adjectives, 4); // Adjectives
738 OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4); // Type
739 OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset
740 OS.EmitValue(create32bitRef(HandlerSym), 4); // Handler
Reid Kleckner28e49032015-10-16 23:43:27 +0000741 if (shouldEmitPersonality)
Reid Kleckner813f1b62015-09-16 22:14:46 +0000742 OS.EmitIntValue(ParentFrameOffset, 4); // ParentFrameOffset
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000743 }
744 }
745 }
746
747 // IPToStateMapEntry {
748 // void *IP;
749 // int32_t State;
750 // };
751 if (IPToStateXData) {
752 OS.EmitLabel(IPToStateXData);
Reid Klecknerc71d6272015-09-28 23:56:30 +0000753 for (auto &IPStatePair : IPToStateTable) {
754 OS.EmitValue(IPStatePair.first, 4); // IP
755 OS.EmitIntValue(IPStatePair.second, 4); // State
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000756 }
757 }
758}
759
Reid Klecknerc71d6272015-09-28 23:56:30 +0000760void WinException::computeIP2StateTable(
Reid Klecknerc20276d2015-11-17 21:10:25 +0000761 const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
Reid Klecknerc71d6272015-09-28 23:56:30 +0000762 SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) {
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000763
David Majnemer8a1c45d2015-12-12 05:38:55 +0000764 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
765 FuncletEnd = MF->begin(),
766 End = MF->end();
767 FuncletStart != End; FuncletStart = FuncletEnd) {
768 // Find the end of the funclet
769 while (++FuncletEnd != End) {
770 if (FuncletEnd->isEHFuncletEntry()) {
771 break;
772 }
773 }
774
775 // Don't emit ip2state entries for cleanup funclets. Any interesting
776 // exceptional actions in cleanups must be handled in a separate IR
777 // function.
778 if (FuncletStart->isCleanupFuncletEntry())
779 continue;
780
781 MCSymbol *StartLabel;
782 int BaseState;
783 if (FuncletStart == MF->begin()) {
784 BaseState = NullState;
785 StartLabel = Asm->getFunctionBegin();
786 } else {
787 auto *FuncletPad =
788 cast<FuncletPadInst>(FuncletStart->getBasicBlock()->getFirstNonPHI());
789 assert(FuncInfo.FuncletBaseStateMap.count(FuncletPad) != 0);
790 BaseState = FuncInfo.FuncletBaseStateMap.find(FuncletPad)->second;
791 StartLabel = getMCSymbolForMBB(Asm, &*FuncletStart);
792 }
793 assert(StartLabel && "need local function start label");
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000794 IPToStateTable.push_back(
David Majnemer8a1c45d2015-12-12 05:38:55 +0000795 std::make_pair(create32bitRef(StartLabel), BaseState));
796
797 for (const auto &StateChange : InvokeStateChangeIterator::range(
798 FuncInfo, FuncletStart, FuncletEnd, BaseState)) {
799 // Compute the label to report as the start of this entry; use the EH
800 // start label for the invoke if we have one, otherwise (this is a call
801 // which may unwind to our caller and does not have an EH start label, so)
802 // use the previous end label.
803 const MCSymbol *ChangeLabel = StateChange.NewStartLabel;
804 if (!ChangeLabel)
805 ChangeLabel = StateChange.PreviousEndLabel;
806 // Emit an entry indicating that PCs after 'Label' have this EH state.
807 IPToStateTable.push_back(
808 std::make_pair(getLabelPlusOne(ChangeLabel), StateChange.NewState));
809 // FIXME: assert that NewState is between CatchLow and CatchHigh.
810 }
Reid Klecknerc71d6272015-09-28 23:56:30 +0000811 }
David Majnemercde33032015-03-30 22:58:10 +0000812}
Reid Klecknerf12c0302015-06-09 21:42:19 +0000813
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000814void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
815 StringRef FLinkageName) {
816 // Outlined helpers called by the EH runtime need to know the offset of the EH
817 // registration in order to recover the parent frame pointer. Now that we know
818 // we've code generated the parent, we can emit the label assignment that
819 // those helpers use to get the offset of the registration node.
Reid Klecknerc20276d2015-11-17 21:10:25 +0000820 MCContext &Ctx = Asm->OutContext;
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000821 MCSymbol *ParentFrameOffset =
Reid Klecknerc20276d2015-11-17 21:10:25 +0000822 Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
823 unsigned UnusedReg;
824 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
825 int64_t Offset = TFI->getFrameIndexReference(
826 *Asm->MF, FuncInfo.EHRegNodeFrameIndex, UnusedReg);
827 const MCExpr *MCOffset = MCConstantExpr::create(Offset, Ctx);
828 Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset);
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000829}
830
Reid Klecknerf12c0302015-06-09 21:42:19 +0000831/// Emit the language-specific data that _except_handler3 and 4 expect. This is
832/// functionally equivalent to the __C_specific_handler table, except it is
833/// indexed by state number instead of IP.
834void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
Reid Klecknera9d62532015-06-11 22:32:23 +0000835 MCStreamer &OS = *Asm->OutStreamer;
Reid Klecknera9d62532015-06-11 22:32:23 +0000836 const Function *F = MF->getFunction();
Reid Klecknera9d62532015-06-11 22:32:23 +0000837 StringRef FLinkageName = GlobalValue::getRealLinkageName(F->getName());
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000838
Reid Klecknerc20276d2015-11-17 21:10:25 +0000839 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000840 emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000841
842 // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
Reid Klecknerf12c0302015-06-09 21:42:19 +0000843 MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
Reid Kleckner85a24502015-07-10 00:08:49 +0000844 OS.EmitValueToAlignment(4);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000845 OS.EmitLabel(LSDALabel);
846
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000847 const Function *Per =
848 dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Reid Klecknerf12c0302015-06-09 21:42:19 +0000849 StringRef PerName = Per->getName();
850 int BaseState = -1;
851 if (PerName == "_except_handler4") {
852 // The LSDA for _except_handler4 starts with this struct, followed by the
853 // scope table:
854 //
855 // struct EH4ScopeTable {
856 // int32_t GSCookieOffset;
857 // int32_t GSCookieXOROffset;
858 // int32_t EHCookieOffset;
859 // int32_t EHCookieXOROffset;
860 // ScopeTableEntry ScopeRecord[];
861 // };
862 //
863 // Only the EHCookieOffset field appears to vary, and it appears to be the
864 // offset from the final saved SP value to the retaddr.
865 OS.EmitIntValue(-2, 4);
866 OS.EmitIntValue(0, 4);
867 // FIXME: Calculate.
868 OS.EmitIntValue(9999, 4);
869 OS.EmitIntValue(0, 4);
870 BaseState = -2;
871 }
872
Reid Kleckner14e77352015-10-09 23:34:53 +0000873 assert(!FuncInfo.SEHUnwindMap.empty());
Reid Klecknerc20276d2015-11-17 21:10:25 +0000874 for (const SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
Reid Kleckner14e77352015-10-09 23:34:53 +0000875 MCSymbol *ExceptOrFinally =
876 UME.Handler.get<MachineBasicBlock *>()->getSymbol();
877 // -1 is usually the base state for "unwind to caller", but for
878 // _except_handler4 it's -2. Do that replacement here if necessary.
879 int ToState = UME.ToState == -1 ? BaseState : UME.ToState;
880 OS.EmitIntValue(ToState, 4); // ToState
881 OS.EmitValue(create32bitRef(UME.Filter), 4); // Filter
882 OS.EmitValue(create32bitRef(ExceptOrFinally), 4); // Except/Finally
Reid Klecknerf12c0302015-06-09 21:42:19 +0000883 }
884}
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000885
Reid Klecknerc20276d2015-11-17 21:10:25 +0000886static int getRank(const WinEHFuncInfo &FuncInfo, int State) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000887 int Rank = 0;
888 while (State != -1) {
889 ++Rank;
890 State = FuncInfo.ClrEHUnwindMap[State].Parent;
891 }
892 return Rank;
893}
894
Reid Klecknerc20276d2015-11-17 21:10:25 +0000895static int getAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000896 int LeftRank = getRank(FuncInfo, Left);
897 int RightRank = getRank(FuncInfo, Right);
898
899 while (LeftRank < RightRank) {
900 Right = FuncInfo.ClrEHUnwindMap[Right].Parent;
901 --RightRank;
902 }
903
904 while (RightRank < LeftRank) {
905 Left = FuncInfo.ClrEHUnwindMap[Left].Parent;
906 --LeftRank;
907 }
908
909 while (Left != Right) {
910 Left = FuncInfo.ClrEHUnwindMap[Left].Parent;
911 Right = FuncInfo.ClrEHUnwindMap[Right].Parent;
912 }
913
914 return Left;
915}
916
917void WinException::emitCLRExceptionTable(const MachineFunction *MF) {
918 // CLR EH "states" are really just IDs that identify handlers/funclets;
919 // states, handlers, and funclets all have 1:1 mappings between them, and a
920 // handler/funclet's "state" is its index in the ClrEHUnwindMap.
921 MCStreamer &OS = *Asm->OutStreamer;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000922 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000923 MCSymbol *FuncBeginSym = Asm->getFunctionBegin();
924 MCSymbol *FuncEndSym = Asm->getFunctionEnd();
925
926 // A ClrClause describes a protected region.
927 struct ClrClause {
928 const MCSymbol *StartLabel; // Start of protected region
929 const MCSymbol *EndLabel; // End of protected region
930 int State; // Index of handler protecting the protected region
931 int EnclosingState; // Index of funclet enclosing the protected region
932 };
933 SmallVector<ClrClause, 8> Clauses;
934
935 // Build a map from handler MBBs to their corresponding states (i.e. their
936 // indices in the ClrEHUnwindMap).
937 int NumStates = FuncInfo.ClrEHUnwindMap.size();
938 assert(NumStates > 0 && "Don't need exception table!");
939 DenseMap<const MachineBasicBlock *, int> HandlerStates;
940 for (int State = 0; State < NumStates; ++State) {
941 MachineBasicBlock *HandlerBlock =
942 FuncInfo.ClrEHUnwindMap[State].Handler.get<MachineBasicBlock *>();
943 HandlerStates[HandlerBlock] = State;
944 // Use this loop through all handlers to verify our assumption (used in
945 // the MinEnclosingState computation) that ancestors have lower state
946 // numbers than their descendants.
947 assert(FuncInfo.ClrEHUnwindMap[State].Parent < State &&
948 "ill-formed state numbering");
949 }
950 // Map the main function to the NullState.
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +0000951 HandlerStates[&MF->front()] = NullState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000952
953 // Write out a sentinel indicating the end of the standard (Windows) xdata
954 // and the start of the additional (CLR) info.
955 OS.EmitIntValue(0xffffffff, 4);
956 // Write out the number of funclets
957 OS.EmitIntValue(NumStates, 4);
958
959 // Walk the machine blocks/instrs, computing and emitting a few things:
960 // 1. Emit a list of the offsets to each handler entry, in lexical order.
961 // 2. Compute a map (EndSymbolMap) from each funclet to the symbol at its end.
962 // 3. Compute the list of ClrClauses, in the required order (inner before
963 // outer, earlier before later; the order by which a forward scan with
964 // early termination will find the innermost enclosing clause covering
965 // a given address).
966 // 4. A map (MinClauseMap) from each handler index to the index of the
967 // outermost funclet/function which contains a try clause targeting the
968 // key handler. This will be used to determine IsDuplicate-ness when
969 // emitting ClrClauses. The NullState value is used to indicate that the
970 // top-level function contains a try clause targeting the key handler.
971 // HandlerStack is a stack of (PendingStartLabel, PendingState) pairs for
972 // try regions we entered before entering the PendingState try but which
973 // we haven't yet exited.
974 SmallVector<std::pair<const MCSymbol *, int>, 4> HandlerStack;
975 // EndSymbolMap and MinClauseMap are maps described above.
976 std::unique_ptr<MCSymbol *[]> EndSymbolMap(new MCSymbol *[NumStates]);
977 SmallVector<int, 4> MinClauseMap((size_t)NumStates, NumStates);
978
979 // Visit the root function and each funclet.
980
981 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
982 FuncletEnd = MF->begin(),
983 End = MF->end();
984 FuncletStart != End; FuncletStart = FuncletEnd) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +0000985 int FuncletState = HandlerStates[&*FuncletStart];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000986 // Find the end of the funclet
987 MCSymbol *EndSymbol = FuncEndSym;
988 while (++FuncletEnd != End) {
989 if (FuncletEnd->isEHFuncletEntry()) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +0000990 EndSymbol = getMCSymbolForMBB(Asm, &*FuncletEnd);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000991 break;
992 }
993 }
994 // Emit the function/funclet end and, if this is a funclet (and not the
995 // root function), record it in the EndSymbolMap.
996 OS.EmitValue(getOffset(EndSymbol, FuncBeginSym), 4);
997 if (FuncletState != NullState) {
998 // Record the end of the handler.
999 EndSymbolMap[FuncletState] = EndSymbol;
1000 }
1001
1002 // Walk the state changes in this function/funclet and compute its clauses.
1003 // Funclets always start in the null state.
1004 const MCSymbol *CurrentStartLabel = nullptr;
1005 int CurrentState = NullState;
1006 assert(HandlerStack.empty());
1007 for (const auto &StateChange :
1008 InvokeStateChangeIterator::range(FuncInfo, FuncletStart, FuncletEnd)) {
1009 // Close any try regions we're not still under
1010 int AncestorState =
1011 getAncestor(FuncInfo, CurrentState, StateChange.NewState);
1012 while (CurrentState != AncestorState) {
1013 assert(CurrentState != NullState && "Failed to find ancestor!");
1014 // Close the pending clause
1015 Clauses.push_back({CurrentStartLabel, StateChange.PreviousEndLabel,
1016 CurrentState, FuncletState});
1017 // Now the parent handler is current
1018 CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].Parent;
1019 // Pop the new start label from the handler stack if we've exited all
1020 // descendants of the corresponding handler.
1021 if (HandlerStack.back().second == CurrentState)
1022 CurrentStartLabel = HandlerStack.pop_back_val().first;
1023 }
1024
1025 if (StateChange.NewState != CurrentState) {
1026 // For each clause we're starting, update the MinClauseMap so we can
1027 // know which is the topmost funclet containing a clause targeting
1028 // it.
1029 for (int EnteredState = StateChange.NewState;
1030 EnteredState != CurrentState;
1031 EnteredState = FuncInfo.ClrEHUnwindMap[EnteredState].Parent) {
1032 int &MinEnclosingState = MinClauseMap[EnteredState];
1033 if (FuncletState < MinEnclosingState)
1034 MinEnclosingState = FuncletState;
1035 }
1036 // Save the previous current start/label on the stack and update to
1037 // the newly-current start/state.
1038 HandlerStack.emplace_back(CurrentStartLabel, CurrentState);
1039 CurrentStartLabel = StateChange.NewStartLabel;
1040 CurrentState = StateChange.NewState;
1041 }
1042 }
1043 assert(HandlerStack.empty());
1044 }
1045
1046 // Now emit the clause info, starting with the number of clauses.
1047 OS.EmitIntValue(Clauses.size(), 4);
1048 for (ClrClause &Clause : Clauses) {
1049 // Emit a CORINFO_EH_CLAUSE :
1050 /*
1051 struct CORINFO_EH_CLAUSE
1052 {
1053 CORINFO_EH_CLAUSE_FLAGS Flags; // actually a CorExceptionFlag
1054 DWORD TryOffset;
1055 DWORD TryLength; // actually TryEndOffset
1056 DWORD HandlerOffset;
1057 DWORD HandlerLength; // actually HandlerEndOffset
1058 union
1059 {
1060 DWORD ClassToken; // use for catch clauses
1061 DWORD FilterOffset; // use for filter clauses
1062 };
1063 };
1064
1065 enum CORINFO_EH_CLAUSE_FLAGS
1066 {
1067 CORINFO_EH_CLAUSE_NONE = 0,
1068 CORINFO_EH_CLAUSE_FILTER = 0x0001, // This clause is for a filter
1069 CORINFO_EH_CLAUSE_FINALLY = 0x0002, // This clause is a finally clause
1070 CORINFO_EH_CLAUSE_FAULT = 0x0004, // This clause is a fault clause
1071 };
1072 typedef enum CorExceptionFlag
1073 {
1074 COR_ILEXCEPTION_CLAUSE_NONE,
1075 COR_ILEXCEPTION_CLAUSE_FILTER = 0x0001, // This is a filter clause
1076 COR_ILEXCEPTION_CLAUSE_FINALLY = 0x0002, // This is a finally clause
1077 COR_ILEXCEPTION_CLAUSE_FAULT = 0x0004, // This is a fault clause
1078 COR_ILEXCEPTION_CLAUSE_DUPLICATED = 0x0008, // duplicated clause. This
1079 // clause was duplicated
1080 // to a funclet which was
1081 // pulled out of line
1082 } CorExceptionFlag;
1083 */
1084 // Add 1 to the start/end of the EH clause; the IP associated with a
1085 // call when the runtime does its scan is the IP of the next instruction
1086 // (the one to which control will return after the call), so we need
1087 // to add 1 to the end of the clause to cover that offset. We also add
1088 // 1 to the start of the clause to make sure that the ranges reported
1089 // for all clauses are disjoint. Note that we'll need some additional
1090 // logic when machine traps are supported, since in that case the IP
1091 // that the runtime uses is the offset of the faulting instruction
1092 // itself; if such an instruction immediately follows a call but the
1093 // two belong to different clauses, we'll need to insert a nop between
1094 // them so the runtime can distinguish the point to which the call will
1095 // return from the point at which the fault occurs.
1096
1097 const MCExpr *ClauseBegin =
1098 getOffsetPlusOne(Clause.StartLabel, FuncBeginSym);
1099 const MCExpr *ClauseEnd = getOffsetPlusOne(Clause.EndLabel, FuncBeginSym);
1100
Reid Klecknerc20276d2015-11-17 21:10:25 +00001101 const ClrEHUnwindMapEntry &Entry = FuncInfo.ClrEHUnwindMap[Clause.State];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001102 MachineBasicBlock *HandlerBlock = Entry.Handler.get<MachineBasicBlock *>();
1103 MCSymbol *BeginSym = getMCSymbolForMBB(Asm, HandlerBlock);
1104 const MCExpr *HandlerBegin = getOffset(BeginSym, FuncBeginSym);
1105 MCSymbol *EndSym = EndSymbolMap[Clause.State];
1106 const MCExpr *HandlerEnd = getOffset(EndSym, FuncBeginSym);
1107
1108 uint32_t Flags = 0;
1109 switch (Entry.HandlerType) {
1110 case ClrHandlerType::Catch:
1111 // Leaving bits 0-2 clear indicates catch.
1112 break;
1113 case ClrHandlerType::Filter:
1114 Flags |= 1;
1115 break;
1116 case ClrHandlerType::Finally:
1117 Flags |= 2;
1118 break;
1119 case ClrHandlerType::Fault:
1120 Flags |= 4;
1121 break;
1122 }
1123 if (Clause.EnclosingState != MinClauseMap[Clause.State]) {
1124 // This is a "duplicate" clause; the handler needs to be entered from a
1125 // frame above the one holding the invoke.
1126 assert(Clause.EnclosingState > MinClauseMap[Clause.State]);
1127 Flags |= 8;
1128 }
1129 OS.EmitIntValue(Flags, 4);
1130
1131 // Write the clause start/end
1132 OS.EmitValue(ClauseBegin, 4);
1133 OS.EmitValue(ClauseEnd, 4);
1134
1135 // Write out the handler start/end
1136 OS.EmitValue(HandlerBegin, 4);
1137 OS.EmitValue(HandlerEnd, 4);
1138
1139 // Write out the type token or filter offset
1140 assert(Entry.HandlerType != ClrHandlerType::Filter && "NYI: filters");
1141 OS.EmitIntValue(Entry.TypeToken, 4);
1142 }
1143}