Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:38

0001 //===- llvm/CodeGen/WinEHFuncInfo.h -----------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Data structures and associated state for Windows exception handling schemes.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CODEGEN_WINEHFUNCINFO_H
0014 #define LLVM_CODEGEN_WINEHFUNCINFO_H
0015 
0016 #include "llvm/ADT/DenseMap.h"
0017 #include "llvm/ADT/PointerUnion.h"
0018 #include "llvm/ADT/SmallVector.h"
0019 #include <cstdint>
0020 #include <limits>
0021 #include <utility>
0022 
0023 namespace llvm {
0024 
0025 class AllocaInst;
0026 class BasicBlock;
0027 class FuncletPadInst;
0028 class Function;
0029 class GlobalVariable;
0030 class Instruction;
0031 class InvokeInst;
0032 class MachineBasicBlock;
0033 class MCSymbol;
0034 
0035 // The following structs respresent the .xdata tables for various
0036 // Windows-related EH personalities.
0037 
0038 using MBBOrBasicBlock = PointerUnion<const BasicBlock *, MachineBasicBlock *>;
0039 
0040 struct CxxUnwindMapEntry {
0041   int ToState;
0042   MBBOrBasicBlock Cleanup;
0043 };
0044 
0045 /// Similar to CxxUnwindMapEntry, but supports SEH filters.
0046 struct SEHUnwindMapEntry {
0047   /// If unwinding continues through this handler, transition to the handler at
0048   /// this state. This indexes into SEHUnwindMap.
0049   int ToState = -1;
0050 
0051   bool IsFinally = false;
0052 
0053   /// Holds the filter expression function.
0054   const Function *Filter = nullptr;
0055 
0056   /// Holds the __except or __finally basic block.
0057   MBBOrBasicBlock Handler;
0058 };
0059 
0060 struct WinEHHandlerType {
0061   int Adjectives;
0062   /// The CatchObj starts out life as an LLVM alloca and is eventually turned
0063   /// frame index.
0064   union {
0065     const AllocaInst *Alloca;
0066     int FrameIndex;
0067   } CatchObj = {};
0068   GlobalVariable *TypeDescriptor;
0069   MBBOrBasicBlock Handler;
0070 };
0071 
0072 struct WinEHTryBlockMapEntry {
0073   int TryLow = -1;
0074   int TryHigh = -1;
0075   int CatchHigh = -1;
0076   SmallVector<WinEHHandlerType, 1> HandlerArray;
0077 };
0078 
0079 enum class ClrHandlerType { Catch, Finally, Fault, Filter };
0080 
0081 struct ClrEHUnwindMapEntry {
0082   MBBOrBasicBlock Handler;
0083   uint32_t TypeToken;
0084   int HandlerParentState; ///< Outer handler enclosing this entry's handler
0085   int TryParentState; ///< Outer try region enclosing this entry's try region,
0086                       ///< treating later catches on same try as "outer"
0087   ClrHandlerType HandlerType;
0088 };
0089 
0090 struct WinEHFuncInfo {
0091   DenseMap<const Instruction *, int> EHPadStateMap;
0092   DenseMap<const FuncletPadInst *, int> FuncletBaseStateMap;
0093   DenseMap<const InvokeInst *, int> InvokeStateMap;
0094   DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> LabelToStateMap;
0095   DenseMap<const BasicBlock *, int> BlockToStateMap; // for AsynchEH
0096   SmallVector<CxxUnwindMapEntry, 4> CxxUnwindMap;
0097   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
0098   SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
0099   SmallVector<ClrEHUnwindMapEntry, 4> ClrEHUnwindMap;
0100   int UnwindHelpFrameIdx = std::numeric_limits<int>::max();
0101   int PSPSymFrameIdx = std::numeric_limits<int>::max();
0102 
0103   int getLastStateNumber() const { return CxxUnwindMap.size() - 1; }
0104 
0105   void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin,
0106                          MCSymbol *InvokeEnd);
0107 
0108   void addIPToStateRange(int State, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd);
0109 
0110   int EHRegNodeFrameIndex = std::numeric_limits<int>::max();
0111   int EHRegNodeEndOffset = std::numeric_limits<int>::max();
0112   int EHGuardFrameIndex = std::numeric_limits<int>::max();
0113   int SEHSetFrameOffset = std::numeric_limits<int>::max();
0114 
0115   WinEHFuncInfo();
0116 };
0117 
0118 /// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
0119 /// describes the state numbers and tables used by __CxxFrameHandler3. This
0120 /// analysis assumes that WinEHPrepare has already been run.
0121 void calculateWinCXXEHStateNumbers(const Function *ParentFn,
0122                                    WinEHFuncInfo &FuncInfo);
0123 
0124 void calculateSEHStateNumbers(const Function *ParentFn,
0125                               WinEHFuncInfo &FuncInfo);
0126 
0127 void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo);
0128 
0129 // For AsynchEH (VC++ option -EHa)
0130 void calculateCXXStateForAsynchEH(const BasicBlock *BB, int State,
0131                                   WinEHFuncInfo &FuncInfo);
0132 void calculateSEHStateForAsynchEH(const BasicBlock *BB, int State,
0133                                   WinEHFuncInfo &FuncInfo);
0134 
0135 } // end namespace llvm
0136 
0137 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H