10b57cec5SDimitry Andric //===-- WinException.h - Windows Exception Handling ----------*- C++ -*--===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains support for writing windows exception info into asm files. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_WIN64EXCEPTION_H 140b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_ASMPRINTER_WIN64EXCEPTION_H 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "EHStreamer.h" 17*fe6060f1SDimitry Andric #include <vector> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace llvm { 200b57cec5SDimitry Andric class GlobalValue; 210b57cec5SDimitry Andric class MachineFunction; 220b57cec5SDimitry Andric class MCExpr; 230b57cec5SDimitry Andric class MCSection; 240b57cec5SDimitry Andric struct WinEHFuncInfo; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY WinException : public EHStreamer { 270b57cec5SDimitry Andric /// Per-function flag to indicate if personality info should be emitted. 280b57cec5SDimitry Andric bool shouldEmitPersonality = false; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric /// Per-function flag to indicate if the LSDA should be emitted. 310b57cec5SDimitry Andric bool shouldEmitLSDA = false; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric /// Per-function flag to indicate if frame moves info should be emitted. 340b57cec5SDimitry Andric bool shouldEmitMoves = false; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric /// True if this is a 64-bit target and we should use image relative offsets. 370b57cec5SDimitry Andric bool useImageRel32 = false; 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric /// True if we are generating exception handling on Windows for ARM64. 400b57cec5SDimitry Andric bool isAArch64 = false; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric /// Pointer to the current funclet entry BB. 430b57cec5SDimitry Andric const MachineBasicBlock *CurrentFuncletEntry = nullptr; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric /// The section of the last funclet start. 460b57cec5SDimitry Andric MCSection *CurrentFuncletTextSection = nullptr; 470b57cec5SDimitry Andric 48*fe6060f1SDimitry Andric /// The list of symbols to add to the ehcont section 49*fe6060f1SDimitry Andric std::vector<const MCSymbol *> EHContTargets; 50*fe6060f1SDimitry Andric 510b57cec5SDimitry Andric void emitCSpecificHandlerTable(const MachineFunction *MF); 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric void emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo, 540b57cec5SDimitry Andric const MCSymbol *BeginLabel, 550b57cec5SDimitry Andric const MCSymbol *EndLabel, int State); 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric /// Emit the EH table data for 32-bit and 64-bit functions using 580b57cec5SDimitry Andric /// the __CxxFrameHandler3 personality. 590b57cec5SDimitry Andric void emitCXXFrameHandler3Table(const MachineFunction *MF); 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric /// Emit the EH table data for _except_handler3 and _except_handler4 620b57cec5SDimitry Andric /// personality functions. These are only used on 32-bit and do not use CFI 630b57cec5SDimitry Andric /// tables. 640b57cec5SDimitry Andric void emitExceptHandlerTable(const MachineFunction *MF); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric void emitCLRExceptionTable(const MachineFunction *MF); 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric void computeIP2StateTable( 690b57cec5SDimitry Andric const MachineFunction *MF, const WinEHFuncInfo &FuncInfo, 700b57cec5SDimitry Andric SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable); 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric /// Emits the label used with llvm.eh.recoverfp, which is used by 730b57cec5SDimitry Andric /// outlined funclets. 740b57cec5SDimitry Andric void emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo, 750b57cec5SDimitry Andric StringRef FLinkageName); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric const MCExpr *create32bitRef(const MCSymbol *Value); 780b57cec5SDimitry Andric const MCExpr *create32bitRef(const GlobalValue *GV); 790b57cec5SDimitry Andric const MCExpr *getLabel(const MCSymbol *Label); 800b57cec5SDimitry Andric const MCExpr *getOffset(const MCSymbol *OffsetOf, const MCSymbol *OffsetFrom); 810b57cec5SDimitry Andric const MCExpr *getOffsetPlusOne(const MCSymbol *OffsetOf, 820b57cec5SDimitry Andric const MCSymbol *OffsetFrom); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric /// Gets the offset that we should use in a table for a stack object with the 850b57cec5SDimitry Andric /// given index. For targets using CFI (Win64, etc), this is relative to the 860b57cec5SDimitry Andric /// established SP at the end of the prologue. For targets without CFI (Win32 870b57cec5SDimitry Andric /// only), it is relative to the frame pointer. 880b57cec5SDimitry Andric int getFrameIndexOffset(int FrameIndex, const WinEHFuncInfo &FuncInfo); 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric void endFuncletImpl(); 910b57cec5SDimitry Andric public: 920b57cec5SDimitry Andric //===--------------------------------------------------------------------===// 930b57cec5SDimitry Andric // Main entry points. 940b57cec5SDimitry Andric // 950b57cec5SDimitry Andric WinException(AsmPrinter *A); 960b57cec5SDimitry Andric ~WinException() override; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric /// Emit all exception information that should come after the content. 990b57cec5SDimitry Andric void endModule() override; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric /// Gather pre-function exception information. Assumes being emitted 1020b57cec5SDimitry Andric /// immediately after the function entry point. 1030b57cec5SDimitry Andric void beginFunction(const MachineFunction *MF) override; 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric void markFunctionEnd() override; 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric /// Gather and emit post-function exception information. 1080b57cec5SDimitry Andric void endFunction(const MachineFunction *) override; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// Emit target-specific EH funclet machinery. 1110b57cec5SDimitry Andric void beginFunclet(const MachineBasicBlock &MBB, MCSymbol *Sym) override; 1120b57cec5SDimitry Andric void endFunclet() override; 1130b57cec5SDimitry Andric }; 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric #endif 1170b57cec5SDimitry Andric 118