1 //===-- WinException.h - Windows Exception Handling ----------*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing windows exception info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_WIN64EXCEPTION_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_WIN64EXCEPTION_H
16 
17 #include "EHStreamer.h"
18 
19 namespace llvm {
20 class Function;
21 class GlobalValue;
22 class MachineFunction;
23 class MCExpr;
24 class MCSection;
25 class Value;
26 struct WinEHFuncInfo;
27 
28 class LLVM_LIBRARY_VISIBILITY WinException : public EHStreamer {
29   /// Per-function flag to indicate if personality info should be emitted.
30   bool shouldEmitPersonality = false;
31 
32   /// Per-function flag to indicate if the LSDA should be emitted.
33   bool shouldEmitLSDA = false;
34 
35   /// Per-function flag to indicate if frame moves info should be emitted.
36   bool shouldEmitMoves = false;
37 
38   /// True if this is a 64-bit target and we should use image relative offsets.
39   bool useImageRel32 = false;
40 
41   /// True if we are generating exception handling on Windows for ARM64.
42   bool isAArch64 = false;
43 
44   /// Pointer to the current funclet entry BB.
45   const MachineBasicBlock *CurrentFuncletEntry = nullptr;
46 
47   /// The section of the last funclet start.
48   MCSection *CurrentFuncletTextSection = nullptr;
49 
50   void emitCSpecificHandlerTable(const MachineFunction *MF);
51 
52   void emitSEHActionsForRange(const WinEHFuncInfo &FuncInfo,
53                               const MCSymbol *BeginLabel,
54                               const MCSymbol *EndLabel, int State);
55 
56   /// Emit the EH table data for 32-bit and 64-bit functions using
57   /// the __CxxFrameHandler3 personality.
58   void emitCXXFrameHandler3Table(const MachineFunction *MF);
59 
60   /// Emit the EH table data for _except_handler3 and _except_handler4
61   /// personality functions. These are only used on 32-bit and do not use CFI
62   /// tables.
63   void emitExceptHandlerTable(const MachineFunction *MF);
64 
65   void emitCLRExceptionTable(const MachineFunction *MF);
66 
67   void computeIP2StateTable(
68       const MachineFunction *MF, const WinEHFuncInfo &FuncInfo,
69       SmallVectorImpl<std::pair<const MCExpr *, int>> &IPToStateTable);
70 
71   /// Emits the label used with llvm.eh.recoverfp, which is used by
72   /// outlined funclets.
73   void emitEHRegistrationOffsetLabel(const WinEHFuncInfo &FuncInfo,
74                                      StringRef FLinkageName);
75 
76   const MCExpr *create32bitRef(const MCSymbol *Value);
77   const MCExpr *create32bitRef(const GlobalValue *GV);
78   const MCExpr *getLabel(const MCSymbol *Label);
79   const MCExpr *getOffset(const MCSymbol *OffsetOf, const MCSymbol *OffsetFrom);
80   const MCExpr *getOffsetPlusOne(const MCSymbol *OffsetOf,
81                                  const MCSymbol *OffsetFrom);
82 
83   /// Gets the offset that we should use in a table for a stack object with the
84   /// given index. For targets using CFI (Win64, etc), this is relative to the
85   /// established SP at the end of the prologue. For targets without CFI (Win32
86   /// only), it is relative to the frame pointer.
87   int getFrameIndexOffset(int FrameIndex, const WinEHFuncInfo &FuncInfo);
88 
89 public:
90   //===--------------------------------------------------------------------===//
91   // Main entry points.
92   //
93   WinException(AsmPrinter *A);
94   ~WinException() override;
95 
96   /// Emit all exception information that should come after the content.
97   void endModule() override;
98 
99   /// Gather pre-function exception information.  Assumes being emitted
100   /// immediately after the function entry point.
101   void beginFunction(const MachineFunction *MF) override;
102 
103   /// Gather and emit post-function exception information.
104   void endFunction(const MachineFunction *) override;
105 
106   /// Emit target-specific EH funclet machinery.
107   void beginFunclet(const MachineBasicBlock &MBB, MCSymbol *Sym) override;
108   void endFunclet() override;
109 };
110 }
111 
112 #endif
113 
114