1 //===-- llvm/CodeGen/WinEHFuncInfo.h ----------------------------*- 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 // Data structures and associated state for Windows exception handling schemes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_WINEHFUNCINFO_H 15 #define LLVM_CODEGEN_WINEHFUNCINFO_H 16 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/PointerUnion.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/TinyPtrVector.h" 21 #include "llvm/IR/Instructions.h" 22 23 namespace llvm { 24 class AllocaInst; 25 class BasicBlock; 26 class CatchReturnInst; 27 class Constant; 28 class Function; 29 class GlobalVariable; 30 class InvokeInst; 31 class IntrinsicInst; 32 class LandingPadInst; 33 class MCExpr; 34 class MCSymbol; 35 class MachineBasicBlock; 36 class Value; 37 38 // The following structs respresent the .xdata tables for various 39 // Windows-related EH personalities. 40 41 typedef PointerUnion<const BasicBlock *, MachineBasicBlock *> MBBOrBasicBlock; 42 43 struct CxxUnwindMapEntry { 44 int ToState; 45 MBBOrBasicBlock Cleanup; 46 }; 47 48 /// Similar to CxxUnwindMapEntry, but supports SEH filters. 49 struct SEHUnwindMapEntry { 50 /// If unwinding continues through this handler, transition to the handler at 51 /// this state. This indexes into SEHUnwindMap. 52 int ToState = -1; 53 54 bool IsFinally = false; 55 56 /// Holds the filter expression function. 57 const Function *Filter = nullptr; 58 59 /// Holds the __except or __finally basic block. 60 MBBOrBasicBlock Handler; 61 }; 62 63 struct WinEHHandlerType { 64 int Adjectives; 65 /// The CatchObj starts out life as an LLVM alloca and is eventually turned 66 /// frame index. 67 union { 68 const AllocaInst *Alloca; 69 int FrameIndex; 70 } CatchObj = {}; 71 GlobalVariable *TypeDescriptor; 72 MBBOrBasicBlock Handler; 73 }; 74 75 struct WinEHTryBlockMapEntry { 76 int TryLow = -1; 77 int TryHigh = -1; 78 int CatchHigh = -1; 79 SmallVector<WinEHHandlerType, 1> HandlerArray; 80 }; 81 82 enum class ClrHandlerType { Catch, Finally, Fault, Filter }; 83 84 struct ClrEHUnwindMapEntry { 85 MBBOrBasicBlock Handler; 86 uint32_t TypeToken; 87 int HandlerParentState; ///< Outer handler enclosing this entry's handler 88 int TryParentState; ///< Outer try region enclosing this entry's try region, 89 ///< treating later catches on same try as "outer" 90 ClrHandlerType HandlerType; 91 }; 92 93 struct WinEHFuncInfo { 94 DenseMap<const Instruction *, int> EHPadStateMap; 95 DenseMap<const FuncletPadInst *, int> FuncletBaseStateMap; 96 DenseMap<const InvokeInst *, int> InvokeStateMap; 97 DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> LabelToStateMap; 98 SmallVector<CxxUnwindMapEntry, 4> CxxUnwindMap; 99 SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap; 100 SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap; 101 SmallVector<ClrEHUnwindMapEntry, 4> ClrEHUnwindMap; 102 int UnwindHelpFrameIdx = INT_MAX; 103 int PSPSymFrameIdx = INT_MAX; 104 getLastStateNumberWinEHFuncInfo105 int getLastStateNumber() const { return CxxUnwindMap.size() - 1; } 106 107 void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, 108 MCSymbol *InvokeEnd); 109 110 int EHRegNodeFrameIndex = INT_MAX; 111 int EHRegNodeEndOffset = INT_MAX; 112 int EHGuardFrameIndex = INT_MAX; 113 int SEHSetFrameOffset = INT_MAX; 114 115 WinEHFuncInfo(); 116 }; 117 118 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which 119 /// describes the state numbers and tables used by __CxxFrameHandler3. This 120 /// analysis assumes that WinEHPrepare has already been run. 121 void calculateWinCXXEHStateNumbers(const Function *ParentFn, 122 WinEHFuncInfo &FuncInfo); 123 124 void calculateSEHStateNumbers(const Function *ParentFn, 125 WinEHFuncInfo &FuncInfo); 126 127 void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo); 128 } 129 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H 130