-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
WinEHFuncInfo.h
214 lines (174 loc) · 6.92 KB
/
WinEHFuncInfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//===-- llvm/CodeGen/WinEHFuncInfo.h ----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Data structures and associated state for Windows exception handling schemes.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CODEGEN_WINEHFUNCINFO_H
#define LLVM_CODEGEN_WINEHFUNCINFO_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TinyPtrVector.h"
namespace llvm {
class AllocaInst;
class BasicBlock;
class Constant;
class Function;
class GlobalVariable;
class InvokeInst;
class IntrinsicInst;
class LandingPadInst;
class MCExpr;
class MCSymbol;
class MachineBasicBlock;
class Value;
enum ActionType { Catch, Cleanup };
class ActionHandler {
public:
ActionHandler(BasicBlock *BB, ActionType Type)
: StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
ActionType getType() const { return Type; }
BasicBlock *getStartBlock() const { return StartBB; }
bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
void setEHState(int State) { EHState = State; }
int getEHState() const { return EHState; }
private:
BasicBlock *StartBB;
ActionType Type;
int EHState;
// Can be either a BlockAddress or a Function depending on the EH personality.
Constant *HandlerBlockOrFunc;
};
class CatchHandler : public ActionHandler {
public:
CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
: ActionHandler(BB, ActionType::Catch), Selector(Selector),
NextBB(NextBB), ExceptionObjectVar(nullptr),
ExceptionObjectIndex(-1) {}
// Method for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const ActionHandler *H) {
return H->getType() == ActionType::Catch;
}
Constant *getSelector() const { return Selector; }
BasicBlock *getNextBB() const { return NextBB; }
const Value *getExceptionVar() { return ExceptionObjectVar; }
TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
void setExceptionVarIndex(int Index) { ExceptionObjectIndex = Index; }
int getExceptionVarIndex() const { return ExceptionObjectIndex; }
void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
ReturnTargets = Targets;
}
private:
Constant *Selector;
BasicBlock *NextBB;
// While catch handlers are being outlined the ExceptionObjectVar field will
// be populated with the instruction in the parent frame that corresponds
// to the exception object (or nullptr if the catch does not use an
// exception object) and the ExceptionObjectIndex field will be -1.
// When the parseEHActions function is called to populate a vector of
// instances of this class, the ExceptionObjectVar field will be nullptr
// and the ExceptionObjectIndex will be the index of the exception object in
// the parent function's localescape block.
const Value *ExceptionObjectVar;
int ExceptionObjectIndex;
TinyPtrVector<BasicBlock *> ReturnTargets;
};
class CleanupHandler : public ActionHandler {
public:
CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
// Method for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const ActionHandler *H) {
return H->getType() == ActionType::Cleanup;
}
};
void parseEHActions(const IntrinsicInst *II,
SmallVectorImpl<std::unique_ptr<ActionHandler>> &Actions);
// The following structs respresent the .xdata for functions using C++
// exceptions on Windows.
typedef PointerUnion<const BasicBlock *, MachineBasicBlock *> MBBOrBasicBlock;
typedef PointerUnion<const Value *, const MachineBasicBlock *> ValueOrMBB;
struct WinEHUnwindMapEntry {
int ToState;
ValueOrMBB Cleanup;
};
/// Similar to WinEHUnwindMapEntry, but supports SEH filters.
struct SEHUnwindMapEntry {
/// If unwinding continues through this handler, transition to the handler at
/// this state. This indexes into SEHUnwindMap.
int ToState = -1;
bool IsFinally = false;
/// Holds the filter expression function.
const Function *Filter = nullptr;
/// Holds the __except or __finally basic block.
MBBOrBasicBlock Handler;
};
struct WinEHHandlerType {
int Adjectives;
int CatchObjRecoverIdx;
/// The CatchObj starts out life as an LLVM alloca, is turned into a frame
/// index, and after PEI, becomes a raw offset.
union {
const AllocaInst *Alloca;
int FrameOffset;
int FrameIndex;
} CatchObj = {};
GlobalVariable *TypeDescriptor;
ValueOrMBB Handler;
};
struct WinEHTryBlockMapEntry {
int TryLow = -1;
int TryHigh = -1;
int CatchHigh = -1;
SmallVector<WinEHHandlerType, 1> HandlerArray;
};
enum class ClrHandlerType { Catch, Finally, Fault, Filter };
struct ClrEHUnwindMapEntry {
MBBOrBasicBlock Handler;
uint32_t TypeToken;
int Parent;
ClrHandlerType HandlerType;
};
struct WinEHFuncInfo {
DenseMap<const Instruction *, int> EHPadStateMap;
DenseMap<const CatchReturnInst *, const BasicBlock *>
CatchRetSuccessorColorMap;
DenseMap<MCSymbol *, std::pair<int, MCSymbol *>> InvokeToStateMap;
SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
SmallVector<SEHUnwindMapEntry, 4> SEHUnwindMap;
SmallVector<ClrEHUnwindMapEntry, 4> ClrEHUnwindMap;
int UnwindHelpFrameIdx = INT_MAX;
int UnwindHelpFrameOffset = -1;
int getLastStateNumber() const { return UnwindMap.size() - 1; }
void addIPToStateRange(const BasicBlock *PadBB, MCSymbol *InvokeBegin,
MCSymbol *InvokeEnd);
/// localescape index of the 32-bit EH registration node. Set by
/// WinEHStatePass and used indirectly by SEH filter functions of the parent.
int EHRegNodeEscapeIndex = INT_MAX;
const AllocaInst *EHRegNode = nullptr;
int EHRegNodeFrameIndex = INT_MAX;
int EHRegNodeEndOffset = INT_MAX;
WinEHFuncInfo() {}
};
/// Analyze the IR in ParentFn and it's handlers to build WinEHFuncInfo, which
/// describes the state numbers and tables used by __CxxFrameHandler3. This
/// analysis assumes that WinEHPrepare has already been run.
void calculateWinCXXEHStateNumbers(const Function *ParentFn,
WinEHFuncInfo &FuncInfo);
void calculateSEHStateNumbers(const Function *ParentFn,
WinEHFuncInfo &FuncInfo);
void calculateClrEHStateNumbers(const Function *Fn, WinEHFuncInfo &FuncInfo);
void calculateCatchReturnSuccessorColors(const Function *Fn,
WinEHFuncInfo &FuncInfo);
}
#endif // LLVM_CODEGEN_WINEHFUNCINFO_H