blob: 478396556d2757bf65c514b23505ced5a307a801 [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/StringExtras.h"
16#include "llvm/ADT/Twine.h"
Charles Davis91ed7992011-05-27 23:47:32 +000017#include "llvm/CodeGen/AsmPrinter.h"
Charles Davis91ed7992011-05-27 23:47:32 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
David Majnemercde33032015-03-30 22:58:10 +000021#include "llvm/CodeGen/WinEHFuncInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000023#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Charles Davis91ed7992011-05-27 23:47:32 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCExpr.h"
28#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000031#include "llvm/MC/MCWin64EH.h"
David Majnemera80c1512015-09-29 20:12:33 +000032#include "llvm/Support/COFF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/Dwarf.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/FormattedStream.h"
Charles Davis91ed7992011-05-27 23:47:32 +000036#include "llvm/Target/TargetFrameLowering.h"
37#include "llvm/Target/TargetLoweringObjectFile.h"
Charles Davis91ed7992011-05-27 23:47:32 +000038#include "llvm/Target/TargetOptions.h"
39#include "llvm/Target/TargetRegisterInfo.h"
Reid Kleckner70bf6bb2015-10-07 21:13:15 +000040#include "llvm/Target/TargetSubtargetInfo.h"
Charles Davis91ed7992011-05-27 23:47:32 +000041using namespace llvm;
42
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000043WinException::WinException(AsmPrinter *A) : EHStreamer(A) {
44 // MSVC's EH tables are always composed of 32-bit words. All known 64-bit
45 // platforms use an imagerel32 relocation to refer to symbols.
46 useImageRel32 = (A->getDataLayout().getPointerSizeInBits() == 64);
47}
Charles Davis91ed7992011-05-27 23:47:32 +000048
Reid Kleckner60b640b2015-05-28 22:47:01 +000049WinException::~WinException() {}
Charles Davis91ed7992011-05-27 23:47:32 +000050
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000051/// endModule - Emit all exception information that should come after the
Charles Davis91ed7992011-05-27 23:47:32 +000052/// content.
Reid Kleckner60b640b2015-05-28 22:47:01 +000053void WinException::endModule() {
Reid Kleckner2bc93ca2015-06-10 01:02:30 +000054 auto &OS = *Asm->OutStreamer;
55 const Module *M = MMI->getModule();
Reid Klecknerca6ef662015-06-10 01:13:44 +000056 for (const Function &F : *M)
57 if (F.hasFnAttribute("safeseh"))
Reid Kleckner2bc93ca2015-06-10 01:02:30 +000058 OS.EmitCOFFSafeSEH(Asm->getSymbol(&F));
Charles Davis91ed7992011-05-27 23:47:32 +000059}
60
Reid Kleckner60b640b2015-05-28 22:47:01 +000061void WinException::beginFunction(const MachineFunction *MF) {
Charles Davis5638b9f2011-05-28 04:21:04 +000062 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
63
64 // If any landing pads survive, we need an EH table.
65 bool hasLandingPads = !MMI->getLandingPads().empty();
Reid Kleckner0e288232015-08-27 23:27:47 +000066 bool hasEHFunclets = MMI->hasEHFunclets();
Charles Davis5638b9f2011-05-28 04:21:04 +000067
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000068 const Function *F = MF->getFunction();
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000069
Charles Davis5638b9f2011-05-28 04:21:04 +000070 shouldEmitMoves = Asm->needsSEHMoves();
71
72 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
73 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Reid Kleckner9a1a9192015-07-13 20:41:46 +000074 const Function *Per = nullptr;
75 if (F->hasPersonalityFn())
76 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Charles Davis5638b9f2011-05-28 04:21:04 +000077
Keno Fischeraff703a2015-07-14 19:22:51 +000078 bool forceEmitPersonality =
79 F->hasPersonalityFn() && !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
80 F->needsUnwindTableEntry();
81
Reid Kleckner0e288232015-08-27 23:27:47 +000082 shouldEmitPersonality =
83 forceEmitPersonality || ((hasLandingPads || hasEHFunclets) &&
84 PerEncoding != dwarf::DW_EH_PE_omit && Per);
Charles Davis5638b9f2011-05-28 04:21:04 +000085
86 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
87 shouldEmitLSDA = shouldEmitPersonality &&
88 LSDAEncoding != dwarf::DW_EH_PE_omit;
89
Reid Kleckner0e288232015-08-27 23:27:47 +000090 // If we're not using CFI, we don't want the CFI or the personality, but we
91 // might want EH tables if we had EH pads.
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000092 if (!Asm->MAI->usesWindowsCFI()) {
David Majnemerbfa5b982015-10-10 00:04:29 +000093 shouldEmitLSDA = hasEHFunclets;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +000094 shouldEmitPersonality = false;
95 return;
96 }
David Majnemera225a192015-03-31 22:35:44 +000097
David Majnemera80c1512015-09-29 20:12:33 +000098 beginFunclet(MF->front(), Asm->CurrentFnSym);
Charles Davis91ed7992011-05-27 23:47:32 +000099}
100
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000101/// endFunction - Gather and emit post-function exception information.
Charles Davis91ed7992011-05-27 23:47:32 +0000102///
Reid Kleckner60b640b2015-05-28 22:47:01 +0000103void WinException::endFunction(const MachineFunction *MF) {
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000104 if (!shouldEmitPersonality && !shouldEmitMoves && !shouldEmitLSDA)
Charles Davis5638b9f2011-05-28 04:21:04 +0000105 return;
106
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000107 const Function *F = MF->getFunction();
108 EHPersonality Per = EHPersonality::Unknown;
109 if (F->hasPersonalityFn())
110 Per = classifyEHPersonality(F->getPersonalityFn());
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000111
Joseph Tremoulet2afea542015-10-06 20:28:16 +0000112 // Get rid of any dead landing pads if we're not using funclets. In funclet
113 // schemes, the landing pad is not actually reachable. It only exists so
114 // that we can emit the right table data.
115 if (!isFuncletEHPersonality(Per))
Reid Kleckner3e9fadf2015-04-15 18:48:15 +0000116 MMI->TidyLandingPads();
Charles Davisa5752262011-05-30 00:13:34 +0000117
David Majnemera80c1512015-09-29 20:12:33 +0000118 endFunclet();
119
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000120 // endFunclet will emit the necessary .xdata tables for x64 SEH.
121 if (Per == EHPersonality::MSVC_Win64SEH && MMI->hasEHFunclets())
122 return;
123
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000124 if (shouldEmitPersonality || shouldEmitLSDA) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000125 Asm->OutStreamer->PushSection();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000126
Reid Kleckner97837b72016-05-02 23:22:18 +0000127 // Just switch sections to the right xdata section.
128 MCSection *XData = Asm->OutStreamer->getAssociatedXDataSection(
129 Asm->OutStreamer->getCurrentSectionOnly());
David Majnemera80c1512015-09-29 20:12:33 +0000130 Asm->OutStreamer->SwitchSection(XData);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000131
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000132 // Emit the tables appropriate to the personality function in use. If we
133 // don't recognize the personality, assume it uses an Itanium-style LSDA.
Reid Kleckner2d5fb682015-02-14 00:21:02 +0000134 if (Per == EHPersonality::MSVC_Win64SEH)
Reid Kleckner94b704c2015-09-09 21:10:03 +0000135 emitCSpecificHandlerTable(MF);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000136 else if (Per == EHPersonality::MSVC_X86SEH)
Reid Klecknerf12c0302015-06-09 21:42:19 +0000137 emitExceptHandlerTable(MF);
David Majnemercde33032015-03-30 22:58:10 +0000138 else if (Per == EHPersonality::MSVC_CXX)
139 emitCXXFrameHandler3Table(MF);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000140 else if (Per == EHPersonality::CoreCLR)
141 emitCLRExceptionTable(MF);
Reid Kleckner9b5eaf02015-01-14 18:50:10 +0000142 else
Reid Kleckner0a57f652015-01-14 01:05:27 +0000143 emitExceptionTable();
Reid Kleckner0a57f652015-01-14 01:05:27 +0000144
Lang Hames9ff69c82015-04-24 19:11:51 +0000145 Asm->OutStreamer->PopSection();
Charles Davisa5752262011-05-30 00:13:34 +0000146 }
David Majnemera80c1512015-09-29 20:12:33 +0000147}
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000148
David Majnemer99c1d132015-10-12 16:44:22 +0000149/// Retreive the MCSymbol for a GlobalValue or MachineBasicBlock.
David Majnemerbfa5b982015-10-10 00:04:29 +0000150static MCSymbol *getMCSymbolForMBB(AsmPrinter *Asm,
151 const MachineBasicBlock *MBB) {
152 if (!MBB)
David Majnemera80c1512015-09-29 20:12:33 +0000153 return nullptr;
David Majnemera80c1512015-09-29 20:12:33 +0000154
David Majnemerbfa5b982015-10-10 00:04:29 +0000155 assert(MBB->isEHFuncletEntry());
156
157 // Give catches and cleanups a name based off of their parent function and
158 // their funclet entry block's number.
159 const MachineFunction *MF = MBB->getParent();
160 const Function *F = MF->getFunction();
161 StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName());
162 MCContext &Ctx = MF->getContext();
163 StringRef HandlerPrefix = MBB->isCleanupFuncletEntry() ? "dtor" : "catch";
164 return Ctx.getOrCreateSymbol("?" + HandlerPrefix + "$" +
165 Twine(MBB->getNumber()) + "@?0?" +
166 FuncLinkageName + "@4HA");
David Majnemera80c1512015-09-29 20:12:33 +0000167}
168
169void WinException::beginFunclet(const MachineBasicBlock &MBB,
170 MCSymbol *Sym) {
171 CurrentFuncletEntry = &MBB;
172
173 const Function *F = Asm->MF->getFunction();
174 // If a symbol was not provided for the funclet, invent one.
175 if (!Sym) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000176 Sym = getMCSymbolForMBB(Asm, &MBB);
David Majnemera80c1512015-09-29 20:12:33 +0000177
178 // Describe our funclet symbol as a function with internal linkage.
179 Asm->OutStreamer->BeginCOFFSymbolDef(Sym);
180 Asm->OutStreamer->EmitCOFFSymbolStorageClass(COFF::IMAGE_SYM_CLASS_STATIC);
181 Asm->OutStreamer->EmitCOFFSymbolType(COFF::IMAGE_SYM_DTYPE_FUNCTION
182 << COFF::SCT_COMPLEX_TYPE_SHIFT);
183 Asm->OutStreamer->EndCOFFSymbolDef();
184
185 // We want our funclet's entry point to be aligned such that no nops will be
186 // present after the label.
187 Asm->EmitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()),
188 F);
189
190 // Now that we've emitted the alignment directive, point at our funclet.
191 Asm->OutStreamer->EmitLabel(Sym);
192 }
193
194 // Mark 'Sym' as starting our funclet.
David Majnemer0e705982015-09-11 17:34:34 +0000195 if (shouldEmitMoves || shouldEmitPersonality)
David Majnemera80c1512015-09-29 20:12:33 +0000196 Asm->OutStreamer->EmitWinCFIStartProc(Sym);
197
198 if (shouldEmitPersonality) {
199 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
200 const Function *PerFn = nullptr;
201
202 // Determine which personality routine we are using for this funclet.
203 if (F->hasPersonalityFn())
204 PerFn = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
205 const MCSymbol *PersHandlerSym =
206 TLOF.getCFIPersonalitySymbol(PerFn, *Asm->Mang, Asm->TM, MMI);
207
208 // Classify the personality routine so that we may reason about it.
209 EHPersonality Per = EHPersonality::Unknown;
210 if (F->hasPersonalityFn())
211 Per = classifyEHPersonality(F->getPersonalityFn());
212
213 // Do not emit a .seh_handler directive if it is a C++ cleanup funclet.
214 if (Per != EHPersonality::MSVC_CXX ||
215 !CurrentFuncletEntry->isCleanupFuncletEntry())
216 Asm->OutStreamer->EmitWinEHHandler(PersHandlerSym, true, true);
217 }
218}
219
220void WinException::endFunclet() {
221 // No funclet to process? Great, we have nothing to do.
222 if (!CurrentFuncletEntry)
223 return;
224
225 if (shouldEmitMoves || shouldEmitPersonality) {
226 const Function *F = Asm->MF->getFunction();
227 EHPersonality Per = EHPersonality::Unknown;
228 if (F->hasPersonalityFn())
229 Per = classifyEHPersonality(F->getPersonalityFn());
230
231 // The .seh_handlerdata directive implicitly switches section, push the
232 // current section so that we may return to it.
233 Asm->OutStreamer->PushSection();
234
235 // Emit an UNWIND_INFO struct describing the prologue.
236 Asm->OutStreamer->EmitWinEHHandlerData();
237
David Majnemera80c1512015-09-29 20:12:33 +0000238 if (Per == EHPersonality::MSVC_CXX && shouldEmitPersonality &&
239 !CurrentFuncletEntry->isCleanupFuncletEntry()) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000240 // If this is a C++ catch funclet (or the parent function),
241 // emit a reference to the LSDA for the parent function.
David Majnemera80c1512015-09-29 20:12:33 +0000242 StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName());
243 MCSymbol *FuncInfoXData = Asm->OutContext.getOrCreateSymbol(
244 Twine("$cppxdata$", FuncLinkageName));
245 Asm->OutStreamer->EmitValue(create32bitRef(FuncInfoXData), 4);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000246 } else if (Per == EHPersonality::MSVC_Win64SEH && MMI->hasEHFunclets() &&
247 !CurrentFuncletEntry->isEHFuncletEntry()) {
248 // If this is the parent function in Win64 SEH, emit the LSDA immediately
249 // following .seh_handlerdata.
250 emitCSpecificHandlerTable(Asm->MF);
David Majnemera80c1512015-09-29 20:12:33 +0000251 }
252
253 // Switch back to the previous section now that we are done writing to
254 // .xdata.
255 Asm->OutStreamer->PopSection();
256
257 // Emit a .seh_endproc directive to mark the end of the function.
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000258 Asm->OutStreamer->EmitWinCFIEndProc();
David Majnemera80c1512015-09-29 20:12:33 +0000259 }
260
261 // Let's make sure we don't try to end the same funclet twice.
262 CurrentFuncletEntry = nullptr;
Charles Davis91ed7992011-05-27 23:47:32 +0000263}
Reid Kleckner0a57f652015-01-14 01:05:27 +0000264
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000265const MCExpr *WinException::create32bitRef(const MCSymbol *Value) {
David Majnemercde33032015-03-30 22:58:10 +0000266 if (!Value)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000267 return MCConstantExpr::create(0, Asm->OutContext);
268 return MCSymbolRefExpr::create(Value, useImageRel32
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000269 ? MCSymbolRefExpr::VK_COFF_IMGREL32
270 : MCSymbolRefExpr::VK_None,
Reid Kleckner0a57f652015-01-14 01:05:27 +0000271 Asm->OutContext);
272}
273
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000274const MCExpr *WinException::create32bitRef(const GlobalValue *GV) {
275 if (!GV)
Jim Grosbach13760bd2015-05-30 01:25:56 +0000276 return MCConstantExpr::create(0, Asm->OutContext);
Joseph Tremoulet3d0fbf12015-10-23 15:06:05 +0000277 return create32bitRef(Asm->getSymbol(GV));
David Majnemercde33032015-03-30 22:58:10 +0000278}
279
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000280const MCExpr *WinException::getLabelPlusOne(const MCSymbol *Label) {
Reid Klecknerc71d6272015-09-28 23:56:30 +0000281 return MCBinaryExpr::createAdd(create32bitRef(Label),
282 MCConstantExpr::create(1, Asm->OutContext),
283 Asm->OutContext);
284}
285
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000286const MCExpr *WinException::getOffset(const MCSymbol *OffsetOf,
287 const MCSymbol *OffsetFrom) {
288 return MCBinaryExpr::createSub(
289 MCSymbolRefExpr::create(OffsetOf, Asm->OutContext),
290 MCSymbolRefExpr::create(OffsetFrom, Asm->OutContext), Asm->OutContext);
291}
292
293const MCExpr *WinException::getOffsetPlusOne(const MCSymbol *OffsetOf,
294 const MCSymbol *OffsetFrom) {
295 return MCBinaryExpr::createAdd(getOffset(OffsetOf, OffsetFrom),
296 MCConstantExpr::create(1, Asm->OutContext),
297 Asm->OutContext);
298}
299
Reid Klecknerc20276d2015-11-17 21:10:25 +0000300int WinException::getFrameIndexOffset(int FrameIndex,
301 const WinEHFuncInfo &FuncInfo) {
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000302 const TargetFrameLowering &TFI = *Asm->MF->getSubtarget().getFrameLowering();
303 unsigned UnusedReg;
304 if (Asm->MAI->usesWindowsCFI())
305 return TFI.getFrameIndexReferenceFromSP(*Asm->MF, FrameIndex, UnusedReg);
Reid Kleckner6ddae312015-11-05 21:09:49 +0000306 // For 32-bit, offsets should be relative to the end of the EH registration
307 // node. For 64-bit, it's relative to SP at the end of the prologue.
308 assert(FuncInfo.EHRegNodeEndOffset != INT_MAX);
309 int Offset = TFI.getFrameIndexReference(*Asm->MF, FrameIndex, UnusedReg);
310 Offset += FuncInfo.EHRegNodeEndOffset;
311 return Offset;
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000312}
313
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000314namespace {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000315
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000316/// Top-level state used to represent unwind to caller
317const int NullState = -1;
318
319struct InvokeStateChange {
320 /// EH Label immediately after the last invoke in the previous state, or
321 /// nullptr if the previous state was the null state.
322 const MCSymbol *PreviousEndLabel;
323
324 /// EH label immediately before the first invoke in the new state, or nullptr
325 /// if the new state is the null state.
326 const MCSymbol *NewStartLabel;
327
328 /// State of the invoke following NewStartLabel, or NullState to indicate
329 /// the presence of calls which may unwind to caller.
330 int NewState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000331};
332
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000333/// Iterator that reports all the invoke state changes in a range of machine
334/// basic blocks. Changes to the null state are reported whenever a call that
335/// may unwind to caller is encountered. The MBB range is expected to be an
336/// entire function or funclet, and the start and end of the range are treated
337/// as being in the NullState even if there's not an unwind-to-caller call
338/// before the first invoke or after the last one (i.e., the first state change
339/// reported is the first change to something other than NullState, and a
340/// change back to NullState is always reported at the end of iteration).
341class InvokeStateChangeIterator {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000342 InvokeStateChangeIterator(const WinEHFuncInfo &EHInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000343 MachineFunction::const_iterator MFI,
344 MachineFunction::const_iterator MFE,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000345 MachineBasicBlock::const_iterator MBBI,
346 int BaseState)
347 : EHInfo(EHInfo), MFI(MFI), MFE(MFE), MBBI(MBBI), BaseState(BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000348 LastStateChange.PreviousEndLabel = nullptr;
349 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000350 LastStateChange.NewState = BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000351 scan();
352 }
353
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000354public:
355 static iterator_range<InvokeStateChangeIterator>
Reid Klecknerc20276d2015-11-17 21:10:25 +0000356 range(const WinEHFuncInfo &EHInfo, MachineFunction::const_iterator Begin,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000357 MachineFunction::const_iterator End, int BaseState = NullState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000358 // Reject empty ranges to simplify bookkeeping by ensuring that we can get
359 // the end of the last block.
360 assert(Begin != End);
361 auto BlockBegin = Begin->begin();
362 auto BlockEnd = std::prev(End)->end();
David Majnemer8a1c45d2015-12-12 05:38:55 +0000363 return make_range(
364 InvokeStateChangeIterator(EHInfo, Begin, End, BlockBegin, BaseState),
365 InvokeStateChangeIterator(EHInfo, End, End, BlockEnd, BaseState));
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000366 }
367
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000368 // Iterator methods.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000369 bool operator==(const InvokeStateChangeIterator &O) const {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000370 assert(BaseState == O.BaseState);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000371 // Must be visiting same block.
372 if (MFI != O.MFI)
373 return false;
374 // Must be visiting same isntr.
375 if (MBBI != O.MBBI)
376 return false;
377 // At end of block/instr iteration, we can still have two distinct states:
378 // one to report the final EndLabel, and another indicating the end of the
379 // state change iteration. Check for CurrentEndLabel equality to
380 // distinguish these.
381 return CurrentEndLabel == O.CurrentEndLabel;
382 }
383
384 bool operator!=(const InvokeStateChangeIterator &O) const {
385 return !operator==(O);
386 }
387 InvokeStateChange &operator*() { return LastStateChange; }
388 InvokeStateChange *operator->() { return &LastStateChange; }
389 InvokeStateChangeIterator &operator++() { return scan(); }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000390
391private:
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000392 InvokeStateChangeIterator &scan();
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000393
Reid Klecknerc20276d2015-11-17 21:10:25 +0000394 const WinEHFuncInfo &EHInfo;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000395 const MCSymbol *CurrentEndLabel = nullptr;
396 MachineFunction::const_iterator MFI;
397 MachineFunction::const_iterator MFE;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000398 MachineBasicBlock::const_iterator MBBI;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000399 InvokeStateChange LastStateChange;
400 bool VisitingInvoke = false;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000401 int BaseState;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000402};
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000403
Benjamin Kramer808d2a02015-10-05 21:20:26 +0000404} // end anonymous namespace
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000405
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000406InvokeStateChangeIterator &InvokeStateChangeIterator::scan() {
407 bool IsNewBlock = false;
408 for (; MFI != MFE; ++MFI, IsNewBlock = true) {
409 if (IsNewBlock)
410 MBBI = MFI->begin();
411 for (auto MBBE = MFI->end(); MBBI != MBBE; ++MBBI) {
412 const MachineInstr &MI = *MBBI;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000413 if (!VisitingInvoke && LastStateChange.NewState != BaseState &&
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000414 MI.isCall() && !EHStreamer::callToNoUnwindFunction(&MI)) {
415 // Indicate a change of state to the null state. We don't have
416 // start/end EH labels handy but the caller won't expect them for
417 // null state regions.
418 LastStateChange.PreviousEndLabel = CurrentEndLabel;
419 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000420 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000421 CurrentEndLabel = nullptr;
422 // Don't re-visit this instr on the next scan
423 ++MBBI;
424 return *this;
425 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000426
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000427 // All other state changes are at EH labels before/after invokes.
428 if (!MI.isEHLabel())
429 continue;
430 MCSymbol *Label = MI.getOperand(0).getMCSymbol();
431 if (Label == CurrentEndLabel) {
432 VisitingInvoke = false;
433 continue;
434 }
David Majnemer8a1c45d2015-12-12 05:38:55 +0000435 auto InvokeMapIter = EHInfo.LabelToStateMap.find(Label);
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000436 // Ignore EH labels that aren't the ones inserted before an invoke
David Majnemer8a1c45d2015-12-12 05:38:55 +0000437 if (InvokeMapIter == EHInfo.LabelToStateMap.end())
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000438 continue;
439 auto &StateAndEnd = InvokeMapIter->second;
440 int NewState = StateAndEnd.first;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000441 // Keep track of the fact that we're between EH start/end labels so
442 // we know not to treat the inoke we'll see as unwinding to caller.
443 VisitingInvoke = true;
444 if (NewState == LastStateChange.NewState) {
445 // The state isn't actually changing here. Record the new end and
446 // keep going.
447 CurrentEndLabel = StateAndEnd.second;
448 continue;
449 }
450 // Found a state change to report
451 LastStateChange.PreviousEndLabel = CurrentEndLabel;
452 LastStateChange.NewStartLabel = Label;
453 LastStateChange.NewState = NewState;
454 // Start keeping track of the new current end
455 CurrentEndLabel = StateAndEnd.second;
456 // Don't re-visit this instr on the next scan
457 ++MBBI;
458 return *this;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000459 }
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000460 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000461 // Iteration hit the end of the block range.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000462 if (LastStateChange.NewState != BaseState) {
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000463 // Report the end of the last new state
464 LastStateChange.PreviousEndLabel = CurrentEndLabel;
465 LastStateChange.NewStartLabel = nullptr;
David Majnemer8a1c45d2015-12-12 05:38:55 +0000466 LastStateChange.NewState = BaseState;
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000467 // Leave CurrentEndLabel non-null to distinguish this state from end.
468 assert(CurrentEndLabel != nullptr);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000469 return *this;
470 }
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000471 // We've reported all state changes and hit the end state.
472 CurrentEndLabel = nullptr;
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000473 return *this;
474}
475
Reid Kleckner0a57f652015-01-14 01:05:27 +0000476/// Emit the language-specific data that __C_specific_handler expects. This
477/// handler lives in the x64 Microsoft C runtime and allows catching or cleaning
478/// up after faults with __try, __except, and __finally. The typeinfo values
479/// are not really RTTI data, but pointers to filter functions that return an
480/// integer (1, 0, or -1) indicating how to handle the exception. For __finally
481/// blocks and other cleanups, the landing pad label is zero, and the filter
482/// function is actually a cleanup handler with the same prototype. A catch-all
483/// entry is modeled with a null filter function field and a non-zero landing
484/// pad label.
485///
486/// Possible filter function return values:
487/// EXCEPTION_EXECUTE_HANDLER (1):
488/// Jump to the landing pad label after cleanups.
489/// EXCEPTION_CONTINUE_SEARCH (0):
490/// Continue searching this table or continue unwinding.
491/// EXCEPTION_CONTINUE_EXECUTION (-1):
492/// Resume execution at the trapping PC.
493///
494/// Inferred table structure:
495/// struct Table {
496/// int NumEntries;
497/// struct Entry {
498/// imagerel32 LabelStart;
499/// imagerel32 LabelEnd;
Reid Klecknerf690f502015-01-22 02:27:44 +0000500/// imagerel32 FilterOrFinally; // One means catch-all.
Reid Kleckner14e77352015-10-09 23:34:53 +0000501/// imagerel32 LabelLPad; // Zero means __finally.
Reid Kleckner0a57f652015-01-14 01:05:27 +0000502/// } Entries[NumEntries];
503/// };
Reid Kleckner94b704c2015-09-09 21:10:03 +0000504void WinException::emitCSpecificHandlerTable(const MachineFunction *MF) {
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000505 auto &OS = *Asm->OutStreamer;
506 MCContext &Ctx = Asm->OutContext;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000507 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner7850c9f2015-12-15 23:40:58 +0000508
David Majnemer081e8fe2015-12-27 06:07:12 +0000509 bool VerboseAsm = OS.isVerboseAsm();
510 auto AddComment = [&](const Twine &Comment) {
511 if (VerboseAsm)
512 OS.AddComment(Comment);
513 };
514
Reid Kleckner7850c9f2015-12-15 23:40:58 +0000515 // Emit a label assignment with the SEH frame offset so we can use it for
516 // llvm.x86.seh.recoverfp.
517 StringRef FLinkageName =
518 GlobalValue::getRealLinkageName(MF->getFunction()->getName());
519 MCSymbol *ParentFrameOffset =
520 Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
521 const MCExpr *MCOffset =
522 MCConstantExpr::create(FuncInfo.SEHSetFrameOffset, Ctx);
523 Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset);
524
Reid Kleckner14e77352015-10-09 23:34:53 +0000525 // Use the assembler to compute the number of table entries through label
526 // difference and division.
527 MCSymbol *TableBegin =
528 Ctx.createTempSymbol("lsda_begin", /*AlwaysAddSuffix=*/true);
529 MCSymbol *TableEnd =
530 Ctx.createTempSymbol("lsda_end", /*AlwaysAddSuffix=*/true);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000531 const MCExpr *LabelDiff = getOffset(TableEnd, TableBegin);
Reid Kleckner14e77352015-10-09 23:34:53 +0000532 const MCExpr *EntrySize = MCConstantExpr::create(16, Ctx);
533 const MCExpr *EntryCount = MCBinaryExpr::createDiv(LabelDiff, EntrySize, Ctx);
David Majnemer081e8fe2015-12-27 06:07:12 +0000534 AddComment("Number of call sites");
Reid Kleckner14e77352015-10-09 23:34:53 +0000535 OS.EmitValue(EntryCount, 4);
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000536
Reid Kleckner14e77352015-10-09 23:34:53 +0000537 OS.EmitLabel(TableBegin);
Reid Klecknereb7cd6c2015-10-09 23:05:54 +0000538
Reid Kleckner14e77352015-10-09 23:34:53 +0000539 // Iterate over all the invoke try ranges. Unlike MSVC, LLVM currently only
540 // models exceptions from invokes. LLVM also allows arbitrary reordering of
541 // the code, so our tables end up looking a bit different. Rather than
542 // trying to match MSVC's tables exactly, we emit a denormalized table. For
543 // each range of invokes in the same state, we emit table entries for all
544 // the actions that would be taken in that state. This means our tables are
545 // slightly bigger, which is OK.
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000546 const MCSymbol *LastStartLabel = nullptr;
547 int LastEHState = -1;
548 // Break out before we enter into a finally funclet.
549 // FIXME: We need to emit separate EH tables for cleanups.
550 MachineFunction::const_iterator End = MF->end();
551 MachineFunction::const_iterator Stop = std::next(MF->begin());
552 while (Stop != End && !Stop->isEHFuncletEntry())
553 ++Stop;
554 for (const auto &StateChange :
555 InvokeStateChangeIterator::range(FuncInfo, MF->begin(), Stop)) {
556 // Emit all the actions for the state we just transitioned out of
557 // if it was not the null state
558 if (LastEHState != -1)
559 emitSEHActionsForRange(FuncInfo, LastStartLabel,
560 StateChange.PreviousEndLabel, LastEHState);
561 LastStartLabel = StateChange.NewStartLabel;
562 LastEHState = StateChange.NewState;
Reid Kleckner0a57f652015-01-14 01:05:27 +0000563 }
Reid Kleckner14e77352015-10-09 23:34:53 +0000564
Reid Kleckner14e77352015-10-09 23:34:53 +0000565 OS.EmitLabel(TableEnd);
Reid Kleckner0a57f652015-01-14 01:05:27 +0000566}
David Majnemercde33032015-03-30 22:58:10 +0000567
Reid Klecknerc20276d2015-11-17 21:10:25 +0000568void WinException::emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000569 const MCSymbol *BeginLabel,
570 const MCSymbol *EndLabel, int State) {
Reid Klecknerd880dc72015-10-09 20:39:39 +0000571 auto &OS = *Asm->OutStreamer;
572 MCContext &Ctx = Asm->OutContext;
573
David Majnemer081e8fe2015-12-27 06:07:12 +0000574 bool VerboseAsm = OS.isVerboseAsm();
575 auto AddComment = [&](const Twine &Comment) {
576 if (VerboseAsm)
577 OS.AddComment(Comment);
578 };
579
Reid Klecknerd880dc72015-10-09 20:39:39 +0000580 assert(BeginLabel && EndLabel);
581 while (State != -1) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000582 const SEHUnwindMapEntry &UME = FuncInfo.SEHUnwindMap[State];
Reid Klecknerd880dc72015-10-09 20:39:39 +0000583 const MCExpr *FilterOrFinally;
584 const MCExpr *ExceptOrNull;
585 auto *Handler = UME.Handler.get<MachineBasicBlock *>();
586 if (UME.IsFinally) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000587 FilterOrFinally = create32bitRef(getMCSymbolForMBB(Asm, Handler));
Reid Klecknerd880dc72015-10-09 20:39:39 +0000588 ExceptOrNull = MCConstantExpr::create(0, Ctx);
589 } else {
590 // For an except, the filter can be 1 (catch-all) or a function
591 // label.
592 FilterOrFinally = UME.Filter ? create32bitRef(UME.Filter)
593 : MCConstantExpr::create(1, Ctx);
594 ExceptOrNull = create32bitRef(Handler->getSymbol());
595 }
596
David Majnemer081e8fe2015-12-27 06:07:12 +0000597 AddComment("LabelStart");
Reid Klecknerd880dc72015-10-09 20:39:39 +0000598 OS.EmitValue(getLabelPlusOne(BeginLabel), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000599 AddComment("LabelEnd");
Reid Klecknerd880dc72015-10-09 20:39:39 +0000600 OS.EmitValue(getLabelPlusOne(EndLabel), 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000601 AddComment(UME.IsFinally ? "FinallyFunclet" : UME.Filter ? "FilterFunction"
602 : "CatchAll");
Reid Klecknerd880dc72015-10-09 20:39:39 +0000603 OS.EmitValue(FilterOrFinally, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000604 AddComment(UME.IsFinally ? "Null" : "ExceptionHandler");
Reid Klecknerd880dc72015-10-09 20:39:39 +0000605 OS.EmitValue(ExceptOrNull, 4);
606
607 assert(UME.ToState < State && "states should decrease");
608 State = UME.ToState;
609 }
610}
611
Reid Kleckner60b640b2015-05-28 22:47:01 +0000612void WinException::emitCXXFrameHandler3Table(const MachineFunction *MF) {
David Majnemercde33032015-03-30 22:58:10 +0000613 const Function *F = MF->getFunction();
Lang Hames9ff69c82015-04-24 19:11:51 +0000614 auto &OS = *Asm->OutStreamer;
Reid Klecknerc20276d2015-11-17 21:10:25 +0000615 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
David Majnemercde33032015-03-30 22:58:10 +0000616
Reid Kleckner813f1b62015-09-16 22:14:46 +0000617 StringRef FuncLinkageName = GlobalValue::getRealLinkageName(F->getName());
David Majnemercde33032015-03-30 22:58:10 +0000618
Reid Klecknerc71d6272015-09-28 23:56:30 +0000619 SmallVector<std::pair<const MCExpr *, int>, 4> IPToStateTable;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000620 MCSymbol *FuncInfoXData = nullptr;
621 if (shouldEmitPersonality) {
Reid Klecknerc71d6272015-09-28 23:56:30 +0000622 // If we're 64-bit, emit a pointer to the C++ EH data, and build a map from
623 // IPs to state numbers.
Reid Kleckner813f1b62015-09-16 22:14:46 +0000624 FuncInfoXData =
625 Asm->OutContext.getOrCreateSymbol(Twine("$cppxdata$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000626 computeIP2StateTable(MF, FuncInfo, IPToStateTable);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000627 } else {
Reid Kleckner813f1b62015-09-16 22:14:46 +0000628 FuncInfoXData = Asm->OutContext.getOrCreateLSDASymbol(FuncLinkageName);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000629 }
630
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000631 int UnwindHelpOffset = 0;
632 if (Asm->MAI->usesWindowsCFI())
Reid Kleckner6ddae312015-11-05 21:09:49 +0000633 UnwindHelpOffset =
634 getFrameIndexOffset(FuncInfo.UnwindHelpFrameIdx, FuncInfo);
Reid Kleckner70bf6bb2015-10-07 21:13:15 +0000635
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000636 MCSymbol *UnwindMapXData = nullptr;
637 MCSymbol *TryBlockMapXData = nullptr;
638 MCSymbol *IPToStateXData = nullptr;
Reid Kleckner14e77352015-10-09 23:34:53 +0000639 if (!FuncInfo.CxxUnwindMap.empty())
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000640 UnwindMapXData = Asm->OutContext.getOrCreateSymbol(
Reid Kleckner813f1b62015-09-16 22:14:46 +0000641 Twine("$stateUnwindMap$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000642 if (!FuncInfo.TryBlockMap.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000643 TryBlockMapXData =
644 Asm->OutContext.getOrCreateSymbol(Twine("$tryMap$", FuncLinkageName));
Reid Klecknerc71d6272015-09-28 23:56:30 +0000645 if (!IPToStateTable.empty())
Reid Kleckner813f1b62015-09-16 22:14:46 +0000646 IPToStateXData =
647 Asm->OutContext.getOrCreateSymbol(Twine("$ip2state$", FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000648
David Majnemer081e8fe2015-12-27 06:07:12 +0000649 bool VerboseAsm = OS.isVerboseAsm();
650 auto AddComment = [&](const Twine &Comment) {
651 if (VerboseAsm)
652 OS.AddComment(Comment);
653 };
654
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000655 // FuncInfo {
656 // uint32_t MagicNumber
657 // int32_t MaxState;
658 // UnwindMapEntry *UnwindMap;
659 // uint32_t NumTryBlocks;
660 // TryBlockMapEntry *TryBlockMap;
661 // uint32_t IPMapEntries; // always 0 for x86
662 // IPToStateMapEntry *IPToStateMap; // always 0 for x86
663 // uint32_t UnwindHelp; // non-x86 only
664 // ESTypeList *ESTypeList;
665 // int32_t EHFlags;
666 // }
667 // EHFlags & 1 -> Synchronous exceptions only, no async exceptions.
668 // EHFlags & 2 -> ???
669 // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue.
Reid Kleckner85a24502015-07-10 00:08:49 +0000670 OS.EmitValueToAlignment(4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000671 OS.EmitLabel(FuncInfoXData);
David Majnemer081e8fe2015-12-27 06:07:12 +0000672
673 AddComment("MagicNumber");
674 OS.EmitIntValue(0x19930522, 4);
675
676 AddComment("MaxState");
677 OS.EmitIntValue(FuncInfo.CxxUnwindMap.size(), 4);
678
679 AddComment("UnwindMap");
680 OS.EmitValue(create32bitRef(UnwindMapXData), 4);
681
682 AddComment("NumTryBlocks");
683 OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4);
684
685 AddComment("TryBlockMap");
686 OS.EmitValue(create32bitRef(TryBlockMapXData), 4);
687
688 AddComment("IPMapEntries");
689 OS.EmitIntValue(IPToStateTable.size(), 4);
690
691 AddComment("IPToStateXData");
692 OS.EmitValue(create32bitRef(IPToStateXData), 4);
693
694 if (Asm->MAI->usesWindowsCFI()) {
695 AddComment("UnwindHelp");
696 OS.EmitIntValue(UnwindHelpOffset, 4);
697 }
698
699 AddComment("ESTypeList");
700 OS.EmitIntValue(0, 4);
701
702 AddComment("EHFlags");
703 OS.EmitIntValue(1, 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000704
705 // UnwindMapEntry {
706 // int32_t ToState;
707 // void (*Action)();
708 // };
709 if (UnwindMapXData) {
710 OS.EmitLabel(UnwindMapXData);
Reid Kleckner14e77352015-10-09 23:34:53 +0000711 for (const CxxUnwindMapEntry &UME : FuncInfo.CxxUnwindMap) {
David Majnemerbfa5b982015-10-10 00:04:29 +0000712 MCSymbol *CleanupSym =
713 getMCSymbolForMBB(Asm, UME.Cleanup.dyn_cast<MachineBasicBlock *>());
David Majnemer081e8fe2015-12-27 06:07:12 +0000714 AddComment("ToState");
715 OS.EmitIntValue(UME.ToState, 4);
716
717 AddComment("Action");
718 OS.EmitValue(create32bitRef(CleanupSym), 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000719 }
720 }
721
722 // TryBlockMap {
723 // int32_t TryLow;
724 // int32_t TryHigh;
725 // int32_t CatchHigh;
726 // int32_t NumCatches;
727 // HandlerType *HandlerArray;
728 // };
729 if (TryBlockMapXData) {
730 OS.EmitLabel(TryBlockMapXData);
731 SmallVector<MCSymbol *, 1> HandlerMaps;
732 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000733 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000734
Reid Kleckner813f1b62015-09-16 22:14:46 +0000735 MCSymbol *HandlerMapXData = nullptr;
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000736 if (!TBME.HandlerArray.empty())
737 HandlerMapXData =
738 Asm->OutContext.getOrCreateSymbol(Twine("$handlerMap$")
739 .concat(Twine(I))
740 .concat("$")
Reid Kleckner813f1b62015-09-16 22:14:46 +0000741 .concat(FuncLinkageName));
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000742 HandlerMaps.push_back(HandlerMapXData);
743
Reid Kleckner813f1b62015-09-16 22:14:46 +0000744 // TBMEs should form intervals.
745 assert(0 <= TBME.TryLow && "bad trymap interval");
746 assert(TBME.TryLow <= TBME.TryHigh && "bad trymap interval");
747 assert(TBME.TryHigh < TBME.CatchHigh && "bad trymap interval");
Reid Kleckner14e77352015-10-09 23:34:53 +0000748 assert(TBME.CatchHigh < int(FuncInfo.CxxUnwindMap.size()) &&
Reid Kleckner813f1b62015-09-16 22:14:46 +0000749 "bad trymap interval");
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000750
David Majnemer081e8fe2015-12-27 06:07:12 +0000751 AddComment("TryLow");
752 OS.EmitIntValue(TBME.TryLow, 4);
753
754 AddComment("TryHigh");
755 OS.EmitIntValue(TBME.TryHigh, 4);
756
757 AddComment("CatchHigh");
758 OS.EmitIntValue(TBME.CatchHigh, 4);
759
760 AddComment("NumCatches");
761 OS.EmitIntValue(TBME.HandlerArray.size(), 4);
762
763 AddComment("HandlerArray");
764 OS.EmitValue(create32bitRef(HandlerMapXData), 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000765 }
766
Reid Kleckner28e49032015-10-16 23:43:27 +0000767 // All funclets use the same parent frame offset currently.
768 unsigned ParentFrameOffset = 0;
769 if (shouldEmitPersonality) {
770 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
771 ParentFrameOffset = TFI->getWinEHParentFrameOffset(*MF);
772 }
773
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000774 for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) {
Reid Klecknerc20276d2015-11-17 21:10:25 +0000775 const WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I];
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000776 MCSymbol *HandlerMapXData = HandlerMaps[I];
777 if (!HandlerMapXData)
778 continue;
779 // HandlerType {
780 // int32_t Adjectives;
781 // TypeDescriptor *Type;
782 // int32_t CatchObjOffset;
783 // void (*Handler)();
784 // int32_t ParentFrameOffset; // x64 only
785 // };
786 OS.EmitLabel(HandlerMapXData);
787 for (const WinEHHandlerType &HT : TBME.HandlerArray) {
788 // Get the frame escape label with the offset of the catch object. If
David Majnemer99c1d132015-10-12 16:44:22 +0000789 // the index is INT_MAX, then there is no catch object, and we should
790 // emit an offset of zero, indicating that no copy will occur.
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000791 const MCExpr *FrameAllocOffsetRef = nullptr;
David Majnemer99c1d132015-10-12 16:44:22 +0000792 if (HT.CatchObj.FrameIndex != INT_MAX) {
Reid Kleckner6ddae312015-11-05 21:09:49 +0000793 int Offset = getFrameIndexOffset(HT.CatchObj.FrameIndex, FuncInfo);
David Majnemercb305de2016-03-01 04:30:16 +0000794 assert(Offset != 0 && "Illegal offset for catch object!");
Reid Klecknerb005d282015-09-16 20:16:27 +0000795 FrameAllocOffsetRef = MCConstantExpr::create(Offset, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000796 } else {
Jim Grosbach13760bd2015-05-30 01:25:56 +0000797 FrameAllocOffsetRef = MCConstantExpr::create(0, Asm->OutContext);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000798 }
799
David Majnemerbfa5b982015-10-10 00:04:29 +0000800 MCSymbol *HandlerSym =
801 getMCSymbolForMBB(Asm, HT.Handler.dyn_cast<MachineBasicBlock *>());
Reid Kleckner94b704c2015-09-09 21:10:03 +0000802
David Majnemer081e8fe2015-12-27 06:07:12 +0000803 AddComment("Adjectives");
804 OS.EmitIntValue(HT.Adjectives, 4);
805
806 AddComment("Type");
807 OS.EmitValue(create32bitRef(HT.TypeDescriptor), 4);
808
809 AddComment("CatchObjOffset");
810 OS.EmitValue(FrameAllocOffsetRef, 4);
811
812 AddComment("Handler");
813 OS.EmitValue(create32bitRef(HandlerSym), 4);
814
815 if (shouldEmitPersonality) {
816 AddComment("ParentFrameOffset");
817 OS.EmitIntValue(ParentFrameOffset, 4);
818 }
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000819 }
820 }
821 }
822
823 // IPToStateMapEntry {
824 // void *IP;
825 // int32_t State;
826 // };
827 if (IPToStateXData) {
828 OS.EmitLabel(IPToStateXData);
Reid Klecknerc71d6272015-09-28 23:56:30 +0000829 for (auto &IPStatePair : IPToStateTable) {
David Majnemer081e8fe2015-12-27 06:07:12 +0000830 AddComment("IP");
831 OS.EmitValue(IPStatePair.first, 4);
832 AddComment("ToState");
833 OS.EmitIntValue(IPStatePair.second, 4);
Reid Kleckner1d3d4ad2015-05-29 17:00:57 +0000834 }
835 }
836}
837
Reid Klecknerc71d6272015-09-28 23:56:30 +0000838void WinException::computeIP2StateTable(
Reid Klecknerc20276d2015-11-17 21:10:25 +0000839 const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
Reid Klecknerc71d6272015-09-28 23:56:30 +0000840 SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable) {
Andrew Kaylor762a6be2015-05-11 19:41:19 +0000841
David Majnemer8a1c45d2015-12-12 05:38:55 +0000842 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
843 FuncletEnd = MF->begin(),
844 End = MF->end();
845 FuncletStart != End; FuncletStart = FuncletEnd) {
846 // Find the end of the funclet
847 while (++FuncletEnd != End) {
848 if (FuncletEnd->isEHFuncletEntry()) {
849 break;
850 }
851 }
852
853 // Don't emit ip2state entries for cleanup funclets. Any interesting
854 // exceptional actions in cleanups must be handled in a separate IR
855 // function.
856 if (FuncletStart->isCleanupFuncletEntry())
857 continue;
858
859 MCSymbol *StartLabel;
860 int BaseState;
861 if (FuncletStart == MF->begin()) {
862 BaseState = NullState;
863 StartLabel = Asm->getFunctionBegin();
864 } else {
865 auto *FuncletPad =
866 cast<FuncletPadInst>(FuncletStart->getBasicBlock()->getFirstNonPHI());
867 assert(FuncInfo.FuncletBaseStateMap.count(FuncletPad) != 0);
868 BaseState = FuncInfo.FuncletBaseStateMap.find(FuncletPad)->second;
869 StartLabel = getMCSymbolForMBB(Asm, &*FuncletStart);
870 }
871 assert(StartLabel && "need local function start label");
Joseph Tremoulet1e2f0622015-10-13 16:44:30 +0000872 IPToStateTable.push_back(
David Majnemer8a1c45d2015-12-12 05:38:55 +0000873 std::make_pair(create32bitRef(StartLabel), BaseState));
874
875 for (const auto &StateChange : InvokeStateChangeIterator::range(
876 FuncInfo, FuncletStart, FuncletEnd, BaseState)) {
877 // Compute the label to report as the start of this entry; use the EH
878 // start label for the invoke if we have one, otherwise (this is a call
879 // which may unwind to our caller and does not have an EH start label, so)
880 // use the previous end label.
881 const MCSymbol *ChangeLabel = StateChange.NewStartLabel;
882 if (!ChangeLabel)
883 ChangeLabel = StateChange.PreviousEndLabel;
884 // Emit an entry indicating that PCs after 'Label' have this EH state.
885 IPToStateTable.push_back(
886 std::make_pair(getLabelPlusOne(ChangeLabel), StateChange.NewState));
887 // FIXME: assert that NewState is between CatchLow and CatchHigh.
888 }
Reid Klecknerc71d6272015-09-28 23:56:30 +0000889 }
David Majnemercde33032015-03-30 22:58:10 +0000890}
Reid Klecknerf12c0302015-06-09 21:42:19 +0000891
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000892void WinException::emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
893 StringRef FLinkageName) {
894 // Outlined helpers called by the EH runtime need to know the offset of the EH
895 // registration in order to recover the parent frame pointer. Now that we know
896 // we've code generated the parent, we can emit the label assignment that
897 // those helpers use to get the offset of the registration node.
Reid Klecknerc20276d2015-11-17 21:10:25 +0000898 MCContext &Ctx = Asm->OutContext;
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000899 MCSymbol *ParentFrameOffset =
Reid Klecknerc20276d2015-11-17 21:10:25 +0000900 Ctx.getOrCreateParentFrameOffsetSymbol(FLinkageName);
901 unsigned UnusedReg;
902 const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering();
903 int64_t Offset = TFI->getFrameIndexReference(
904 *Asm->MF, FuncInfo.EHRegNodeFrameIndex, UnusedReg);
905 const MCExpr *MCOffset = MCConstantExpr::create(Offset, Ctx);
906 Asm->OutStreamer->EmitAssignment(ParentFrameOffset, MCOffset);
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000907}
908
Reid Klecknerf12c0302015-06-09 21:42:19 +0000909/// Emit the language-specific data that _except_handler3 and 4 expect. This is
910/// functionally equivalent to the __C_specific_handler table, except it is
911/// indexed by state number instead of IP.
912void WinException::emitExceptHandlerTable(const MachineFunction *MF) {
Reid Klecknera9d62532015-06-11 22:32:23 +0000913 MCStreamer &OS = *Asm->OutStreamer;
Reid Klecknera9d62532015-06-11 22:32:23 +0000914 const Function *F = MF->getFunction();
Reid Klecknera9d62532015-06-11 22:32:23 +0000915 StringRef FLinkageName = GlobalValue::getRealLinkageName(F->getName());
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000916
David Majnemer081e8fe2015-12-27 06:07:12 +0000917 bool VerboseAsm = OS.isVerboseAsm();
918 auto AddComment = [&](const Twine &Comment) {
919 if (VerboseAsm)
920 OS.AddComment(Comment);
921 };
922
Reid Klecknerc20276d2015-11-17 21:10:25 +0000923 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Reid Kleckner399a2fe2015-06-30 22:46:59 +0000924 emitEHRegistrationOffsetLabel(FuncInfo, FLinkageName);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000925
926 // Emit the __ehtable label that we use for llvm.x86.seh.lsda.
Reid Klecknerf12c0302015-06-09 21:42:19 +0000927 MCSymbol *LSDALabel = Asm->OutContext.getOrCreateLSDASymbol(FLinkageName);
Reid Kleckner85a24502015-07-10 00:08:49 +0000928 OS.EmitValueToAlignment(4);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000929 OS.EmitLabel(LSDALabel);
930
Reid Kleckner9a1a9192015-07-13 20:41:46 +0000931 const Function *Per =
932 dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
Reid Klecknerf12c0302015-06-09 21:42:19 +0000933 StringRef PerName = Per->getName();
934 int BaseState = -1;
935 if (PerName == "_except_handler4") {
936 // The LSDA for _except_handler4 starts with this struct, followed by the
937 // scope table:
938 //
939 // struct EH4ScopeTable {
940 // int32_t GSCookieOffset;
941 // int32_t GSCookieXOROffset;
942 // int32_t EHCookieOffset;
943 // int32_t EHCookieXOROffset;
944 // ScopeTableEntry ScopeRecord[];
945 // };
946 //
947 // Only the EHCookieOffset field appears to vary, and it appears to be the
948 // offset from the final saved SP value to the retaddr.
David Majnemer081e8fe2015-12-27 06:07:12 +0000949 AddComment("GSCookieOffset");
Reid Klecknerf12c0302015-06-09 21:42:19 +0000950 OS.EmitIntValue(-2, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000951 AddComment("GSCookieXOROffset");
Reid Klecknerf12c0302015-06-09 21:42:19 +0000952 OS.EmitIntValue(0, 4);
953 // FIXME: Calculate.
David Majnemer081e8fe2015-12-27 06:07:12 +0000954 AddComment("EHCookieOffset");
Reid Klecknerf12c0302015-06-09 21:42:19 +0000955 OS.EmitIntValue(9999, 4);
David Majnemer081e8fe2015-12-27 06:07:12 +0000956 AddComment("EHCookieXOROffset");
Reid Klecknerf12c0302015-06-09 21:42:19 +0000957 OS.EmitIntValue(0, 4);
958 BaseState = -2;
959 }
960
Reid Kleckner14e77352015-10-09 23:34:53 +0000961 assert(!FuncInfo.SEHUnwindMap.empty());
Reid Klecknerc20276d2015-11-17 21:10:25 +0000962 for (const SEHUnwindMapEntry &UME : FuncInfo.SEHUnwindMap) {
David Majnemer081e8fe2015-12-27 06:07:12 +0000963 auto *Handler = UME.Handler.get<MachineBasicBlock *>();
964 const MCSymbol *ExceptOrFinally =
965 UME.IsFinally ? getMCSymbolForMBB(Asm, Handler) : Handler->getSymbol();
Reid Kleckner14e77352015-10-09 23:34:53 +0000966 // -1 is usually the base state for "unwind to caller", but for
967 // _except_handler4 it's -2. Do that replacement here if necessary.
968 int ToState = UME.ToState == -1 ? BaseState : UME.ToState;
David Majnemer081e8fe2015-12-27 06:07:12 +0000969 AddComment("ToState");
970 OS.EmitIntValue(ToState, 4);
971 AddComment(UME.IsFinally ? "Null" : "FilterFunction");
972 OS.EmitValue(create32bitRef(UME.Filter), 4);
973 AddComment(UME.IsFinally ? "FinallyFunclet" : "ExceptionHandler");
974 OS.EmitValue(create32bitRef(ExceptOrFinally), 4);
Reid Klecknerf12c0302015-06-09 21:42:19 +0000975 }
976}
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000977
Joseph Tremoulet52f729a2016-01-04 16:16:01 +0000978static int getTryRank(const WinEHFuncInfo &FuncInfo, int State) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000979 int Rank = 0;
980 while (State != -1) {
981 ++Rank;
Joseph Tremoulet52f729a2016-01-04 16:16:01 +0000982 State = FuncInfo.ClrEHUnwindMap[State].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000983 }
984 return Rank;
985}
986
Joseph Tremoulet52f729a2016-01-04 16:16:01 +0000987static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) {
988 int LeftRank = getTryRank(FuncInfo, Left);
989 int RightRank = getTryRank(FuncInfo, Right);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000990
991 while (LeftRank < RightRank) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +0000992 Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000993 --RightRank;
994 }
995
996 while (RightRank < LeftRank) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +0000997 Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +0000998 --LeftRank;
999 }
1000
1001 while (Left != Right) {
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001002 Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;
1003 Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001004 }
1005
1006 return Left;
1007}
1008
1009void WinException::emitCLRExceptionTable(const MachineFunction *MF) {
1010 // CLR EH "states" are really just IDs that identify handlers/funclets;
1011 // states, handlers, and funclets all have 1:1 mappings between them, and a
1012 // handler/funclet's "state" is its index in the ClrEHUnwindMap.
1013 MCStreamer &OS = *Asm->OutStreamer;
Reid Klecknerc20276d2015-11-17 21:10:25 +00001014 const WinEHFuncInfo &FuncInfo = *MF->getWinEHFuncInfo();
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001015 MCSymbol *FuncBeginSym = Asm->getFunctionBegin();
1016 MCSymbol *FuncEndSym = Asm->getFunctionEnd();
1017
1018 // A ClrClause describes a protected region.
1019 struct ClrClause {
1020 const MCSymbol *StartLabel; // Start of protected region
1021 const MCSymbol *EndLabel; // End of protected region
1022 int State; // Index of handler protecting the protected region
1023 int EnclosingState; // Index of funclet enclosing the protected region
1024 };
1025 SmallVector<ClrClause, 8> Clauses;
1026
1027 // Build a map from handler MBBs to their corresponding states (i.e. their
1028 // indices in the ClrEHUnwindMap).
1029 int NumStates = FuncInfo.ClrEHUnwindMap.size();
1030 assert(NumStates > 0 && "Don't need exception table!");
1031 DenseMap<const MachineBasicBlock *, int> HandlerStates;
1032 for (int State = 0; State < NumStates; ++State) {
1033 MachineBasicBlock *HandlerBlock =
1034 FuncInfo.ClrEHUnwindMap[State].Handler.get<MachineBasicBlock *>();
1035 HandlerStates[HandlerBlock] = State;
1036 // Use this loop through all handlers to verify our assumption (used in
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001037 // the MinEnclosingState computation) that enclosing funclets have lower
1038 // state numbers than their enclosed funclets.
1039 assert(FuncInfo.ClrEHUnwindMap[State].HandlerParentState < State &&
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001040 "ill-formed state numbering");
1041 }
1042 // Map the main function to the NullState.
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001043 HandlerStates[&MF->front()] = NullState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001044
1045 // Write out a sentinel indicating the end of the standard (Windows) xdata
1046 // and the start of the additional (CLR) info.
1047 OS.EmitIntValue(0xffffffff, 4);
1048 // Write out the number of funclets
1049 OS.EmitIntValue(NumStates, 4);
1050
1051 // Walk the machine blocks/instrs, computing and emitting a few things:
1052 // 1. Emit a list of the offsets to each handler entry, in lexical order.
1053 // 2. Compute a map (EndSymbolMap) from each funclet to the symbol at its end.
1054 // 3. Compute the list of ClrClauses, in the required order (inner before
1055 // outer, earlier before later; the order by which a forward scan with
1056 // early termination will find the innermost enclosing clause covering
1057 // a given address).
1058 // 4. A map (MinClauseMap) from each handler index to the index of the
1059 // outermost funclet/function which contains a try clause targeting the
1060 // key handler. This will be used to determine IsDuplicate-ness when
1061 // emitting ClrClauses. The NullState value is used to indicate that the
1062 // top-level function contains a try clause targeting the key handler.
1063 // HandlerStack is a stack of (PendingStartLabel, PendingState) pairs for
1064 // try regions we entered before entering the PendingState try but which
1065 // we haven't yet exited.
1066 SmallVector<std::pair<const MCSymbol *, int>, 4> HandlerStack;
1067 // EndSymbolMap and MinClauseMap are maps described above.
1068 std::unique_ptr<MCSymbol *[]> EndSymbolMap(new MCSymbol *[NumStates]);
1069 SmallVector<int, 4> MinClauseMap((size_t)NumStates, NumStates);
1070
1071 // Visit the root function and each funclet.
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001072 for (MachineFunction::const_iterator FuncletStart = MF->begin(),
1073 FuncletEnd = MF->begin(),
1074 End = MF->end();
1075 FuncletStart != End; FuncletStart = FuncletEnd) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001076 int FuncletState = HandlerStates[&*FuncletStart];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001077 // Find the end of the funclet
1078 MCSymbol *EndSymbol = FuncEndSym;
1079 while (++FuncletEnd != End) {
1080 if (FuncletEnd->isEHFuncletEntry()) {
Duncan P. N. Exon Smitha25ad062015-10-20 00:36:08 +00001081 EndSymbol = getMCSymbolForMBB(Asm, &*FuncletEnd);
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001082 break;
1083 }
1084 }
1085 // Emit the function/funclet end and, if this is a funclet (and not the
1086 // root function), record it in the EndSymbolMap.
1087 OS.EmitValue(getOffset(EndSymbol, FuncBeginSym), 4);
1088 if (FuncletState != NullState) {
1089 // Record the end of the handler.
1090 EndSymbolMap[FuncletState] = EndSymbol;
1091 }
1092
1093 // Walk the state changes in this function/funclet and compute its clauses.
1094 // Funclets always start in the null state.
1095 const MCSymbol *CurrentStartLabel = nullptr;
1096 int CurrentState = NullState;
1097 assert(HandlerStack.empty());
1098 for (const auto &StateChange :
1099 InvokeStateChangeIterator::range(FuncInfo, FuncletStart, FuncletEnd)) {
1100 // Close any try regions we're not still under
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001101 int StillPendingState =
1102 getTryAncestor(FuncInfo, CurrentState, StateChange.NewState);
1103 while (CurrentState != StillPendingState) {
1104 assert(CurrentState != NullState &&
1105 "Failed to find still-pending state!");
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001106 // Close the pending clause
1107 Clauses.push_back({CurrentStartLabel, StateChange.PreviousEndLabel,
1108 CurrentState, FuncletState});
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001109 // Now the next-outer try region is current
1110 CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].TryParentState;
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001111 // Pop the new start label from the handler stack if we've exited all
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001112 // inner try regions of the corresponding try region.
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001113 if (HandlerStack.back().second == CurrentState)
1114 CurrentStartLabel = HandlerStack.pop_back_val().first;
1115 }
1116
1117 if (StateChange.NewState != CurrentState) {
1118 // For each clause we're starting, update the MinClauseMap so we can
1119 // know which is the topmost funclet containing a clause targeting
1120 // it.
1121 for (int EnteredState = StateChange.NewState;
1122 EnteredState != CurrentState;
Joseph Tremoulet52f729a2016-01-04 16:16:01 +00001123 EnteredState =
1124 FuncInfo.ClrEHUnwindMap[EnteredState].TryParentState) {
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001125 int &MinEnclosingState = MinClauseMap[EnteredState];
1126 if (FuncletState < MinEnclosingState)
1127 MinEnclosingState = FuncletState;
1128 }
1129 // Save the previous current start/label on the stack and update to
1130 // the newly-current start/state.
1131 HandlerStack.emplace_back(CurrentStartLabel, CurrentState);
1132 CurrentStartLabel = StateChange.NewStartLabel;
1133 CurrentState = StateChange.NewState;
1134 }
1135 }
1136 assert(HandlerStack.empty());
1137 }
1138
1139 // Now emit the clause info, starting with the number of clauses.
1140 OS.EmitIntValue(Clauses.size(), 4);
1141 for (ClrClause &Clause : Clauses) {
1142 // Emit a CORINFO_EH_CLAUSE :
1143 /*
1144 struct CORINFO_EH_CLAUSE
1145 {
1146 CORINFO_EH_CLAUSE_FLAGS Flags; // actually a CorExceptionFlag
1147 DWORD TryOffset;
1148 DWORD TryLength; // actually TryEndOffset
1149 DWORD HandlerOffset;
1150 DWORD HandlerLength; // actually HandlerEndOffset
1151 union
1152 {
1153 DWORD ClassToken; // use for catch clauses
1154 DWORD FilterOffset; // use for filter clauses
1155 };
1156 };
1157
1158 enum CORINFO_EH_CLAUSE_FLAGS
1159 {
1160 CORINFO_EH_CLAUSE_NONE = 0,
1161 CORINFO_EH_CLAUSE_FILTER = 0x0001, // This clause is for a filter
1162 CORINFO_EH_CLAUSE_FINALLY = 0x0002, // This clause is a finally clause
1163 CORINFO_EH_CLAUSE_FAULT = 0x0004, // This clause is a fault clause
1164 };
1165 typedef enum CorExceptionFlag
1166 {
1167 COR_ILEXCEPTION_CLAUSE_NONE,
1168 COR_ILEXCEPTION_CLAUSE_FILTER = 0x0001, // This is a filter clause
1169 COR_ILEXCEPTION_CLAUSE_FINALLY = 0x0002, // This is a finally clause
1170 COR_ILEXCEPTION_CLAUSE_FAULT = 0x0004, // This is a fault clause
1171 COR_ILEXCEPTION_CLAUSE_DUPLICATED = 0x0008, // duplicated clause. This
1172 // clause was duplicated
1173 // to a funclet which was
1174 // pulled out of line
1175 } CorExceptionFlag;
1176 */
1177 // Add 1 to the start/end of the EH clause; the IP associated with a
1178 // call when the runtime does its scan is the IP of the next instruction
1179 // (the one to which control will return after the call), so we need
1180 // to add 1 to the end of the clause to cover that offset. We also add
1181 // 1 to the start of the clause to make sure that the ranges reported
1182 // for all clauses are disjoint. Note that we'll need some additional
1183 // logic when machine traps are supported, since in that case the IP
1184 // that the runtime uses is the offset of the faulting instruction
1185 // itself; if such an instruction immediately follows a call but the
1186 // two belong to different clauses, we'll need to insert a nop between
1187 // them so the runtime can distinguish the point to which the call will
1188 // return from the point at which the fault occurs.
1189
1190 const MCExpr *ClauseBegin =
1191 getOffsetPlusOne(Clause.StartLabel, FuncBeginSym);
1192 const MCExpr *ClauseEnd = getOffsetPlusOne(Clause.EndLabel, FuncBeginSym);
1193
Reid Klecknerc20276d2015-11-17 21:10:25 +00001194 const ClrEHUnwindMapEntry &Entry = FuncInfo.ClrEHUnwindMap[Clause.State];
Joseph Tremoulet28c89bb2015-10-13 20:18:27 +00001195 MachineBasicBlock *HandlerBlock = Entry.Handler.get<MachineBasicBlock *>();
1196 MCSymbol *BeginSym = getMCSymbolForMBB(Asm, HandlerBlock);
1197 const MCExpr *HandlerBegin = getOffset(BeginSym, FuncBeginSym);
1198 MCSymbol *EndSym = EndSymbolMap[Clause.State];
1199 const MCExpr *HandlerEnd = getOffset(EndSym, FuncBeginSym);
1200
1201 uint32_t Flags = 0;
1202 switch (Entry.HandlerType) {
1203 case ClrHandlerType::Catch:
1204 // Leaving bits 0-2 clear indicates catch.
1205 break;
1206 case ClrHandlerType::Filter:
1207 Flags |= 1;
1208 break;
1209 case ClrHandlerType::Finally:
1210 Flags |= 2;
1211 break;
1212 case ClrHandlerType::Fault:
1213 Flags |= 4;
1214 break;
1215 }
1216 if (Clause.EnclosingState != MinClauseMap[Clause.State]) {
1217 // This is a "duplicate" clause; the handler needs to be entered from a
1218 // frame above the one holding the invoke.
1219 assert(Clause.EnclosingState > MinClauseMap[Clause.State]);
1220 Flags |= 8;
1221 }
1222 OS.EmitIntValue(Flags, 4);
1223
1224 // Write the clause start/end
1225 OS.EmitValue(ClauseBegin, 4);
1226 OS.EmitValue(ClauseEnd, 4);
1227
1228 // Write out the handler start/end
1229 OS.EmitValue(HandlerBegin, 4);
1230 OS.EmitValue(HandlerEnd, 4);
1231
1232 // Write out the type token or filter offset
1233 assert(Entry.HandlerType != ClrHandlerType::Filter && "NYI: filters");
1234 OS.EmitIntValue(Entry.TypeToken, 4);
1235 }
1236}