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"
17fe6060f1SDimitry 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 
42349cc55cSDimitry Andric   /// True if we are generating exception handling on Windows for ARM (Thumb).
43349cc55cSDimitry Andric   bool isThumb = false;
44349cc55cSDimitry Andric 
450b57cec5SDimitry Andric   /// Pointer to the current funclet entry BB.
460b57cec5SDimitry Andric   const MachineBasicBlock *CurrentFuncletEntry = nullptr;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   /// The section of the last funclet start.
490b57cec5SDimitry Andric   MCSection *CurrentFuncletTextSection = nullptr;
500b57cec5SDimitry Andric 
51fe6060f1SDimitry Andric   /// The list of symbols to add to the ehcont section
52fe6060f1SDimitry Andric   std::vector<const MCSymbol *> EHContTargets;
53fe6060f1SDimitry Andric 
540b57cec5SDimitry Andric   void emitCSpecificHandlerTable(const MachineFunction *MF);
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   void emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,
570b57cec5SDimitry Andric                               const MCSymbol *BeginLabel,
580b57cec5SDimitry Andric                               const MCSymbol *EndLabel, int State);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   /// Emit the EH table data for 32-bit and 64-bit functions using
610b57cec5SDimitry Andric   /// the __CxxFrameHandler3 personality.
620b57cec5SDimitry Andric   void emitCXXFrameHandler3Table(const MachineFunction *MF);
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   /// Emit the EH table data for _except_handler3 and _except_handler4
650b57cec5SDimitry Andric   /// personality functions. These are only used on 32-bit and do not use CFI
660b57cec5SDimitry Andric   /// tables.
670b57cec5SDimitry Andric   void emitExceptHandlerTable(const MachineFunction *MF);
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   void emitCLRExceptionTable(const MachineFunction *MF);
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   void computeIP2StateTable(
720b57cec5SDimitry Andric       const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
730b57cec5SDimitry Andric       SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable);
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   /// Emits the label used with llvm.eh.recoverfp, which is used by
760b57cec5SDimitry Andric   /// outlined funclets.
770b57cec5SDimitry Andric   void emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
780b57cec5SDimitry Andric                                      StringRef FLinkageName);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   const MCExpr *create32bitRef(const MCSymbol *Value);
810b57cec5SDimitry Andric   const MCExpr *create32bitRef(const GlobalValue *GV);
820b57cec5SDimitry Andric   const MCExpr *getLabel(const MCSymbol *Label);
83349cc55cSDimitry Andric   const MCExpr *getLabelPlusOne(const MCSymbol *Label);
840b57cec5SDimitry Andric   const MCExpr *getOffset(const MCSymbol *OffsetOf, const MCSymbol *OffsetFrom);
850b57cec5SDimitry Andric   const MCExpr *getOffsetPlusOne(const MCSymbol *OffsetOf,
860b57cec5SDimitry Andric                                  const MCSymbol *OffsetFrom);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   /// Gets the offset that we should use in a table for a stack object with the
890b57cec5SDimitry Andric   /// given index. For targets using CFI (Win64, etc), this is relative to the
900b57cec5SDimitry Andric   /// established SP at the end of the prologue. For targets without CFI (Win32
910b57cec5SDimitry Andric   /// only), it is relative to the frame pointer.
920b57cec5SDimitry Andric   int getFrameIndexOffset(int FrameIndex, const WinEHFuncInfo &FuncInfo);
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   void endFuncletImpl();
950b57cec5SDimitry Andric public:
960b57cec5SDimitry Andric   //===--------------------------------------------------------------------===//
970b57cec5SDimitry Andric   // Main entry points.
980b57cec5SDimitry Andric   //
990b57cec5SDimitry Andric   WinException(AsmPrinter *A);
1000b57cec5SDimitry Andric   ~WinException() override;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   /// Emit all exception information that should come after the content.
1030b57cec5SDimitry Andric   void endModule() override;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   /// Gather pre-function exception information.  Assumes being emitted
1060b57cec5SDimitry Andric   /// immediately after the function entry point.
1070b57cec5SDimitry Andric   void beginFunction(const MachineFunction *MF) override;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   void markFunctionEnd() override;
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   /// Gather and emit post-function exception information.
1120b57cec5SDimitry Andric   void endFunction(const MachineFunction *) override;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   /// Emit target-specific EH funclet machinery.
1150b57cec5SDimitry Andric   void beginFunclet(const MachineBasicBlock &MBB, MCSymbol *Sym) override;
1160b57cec5SDimitry Andric   void endFunclet() override;
1170b57cec5SDimitry Andric };
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric #endif
1210b57cec5SDimitry Andric 
122