[WinEH] Generate .xdata for catch handlers
[oota-llvm.git] / include / llvm / CodeGen / WinEHFuncInfo.h
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/SmallVector.h"
18 #include "llvm/ADT/TinyPtrVector.h"
19 #include "llvm/ADT/DenseMap.h"
20
21 namespace llvm {
22 class BasicBlock;
23 class Constant;
24 class Function;
25 class GlobalValue;
26 class LandingPadInst;
27 class MCSymbol;
28 class Value;
29
30 enum ActionType { Catch, Cleanup };
31
32 class ActionHandler {
33 public:
34   ActionHandler(BasicBlock *BB, ActionType Type)
35       : StartBB(BB), Type(Type), EHState(-1), HandlerBlockOrFunc(nullptr) {}
36
37   ActionType getType() const { return Type; }
38   BasicBlock *getStartBlock() const { return StartBB; }
39
40   bool hasBeenProcessed() { return HandlerBlockOrFunc != nullptr; }
41
42   void setHandlerBlockOrFunc(Constant *F) { HandlerBlockOrFunc = F; }
43   Constant *getHandlerBlockOrFunc() { return HandlerBlockOrFunc; }
44
45   void setEHState(int State) { EHState = State; }
46   int getEHState() const { return EHState; }
47
48 private:
49   BasicBlock *StartBB;
50   ActionType Type;
51   int EHState;
52
53   // Can be either a BlockAddress or a Function depending on the EH personality.
54   Constant *HandlerBlockOrFunc;
55 };
56
57 class CatchHandler : public ActionHandler {
58 public:
59   CatchHandler(BasicBlock *BB, Constant *Selector, BasicBlock *NextBB)
60       : ActionHandler(BB, ActionType::Catch), Selector(Selector),
61         NextBB(NextBB), ExceptionObjectVar(nullptr) {}
62
63   // Method for support type inquiry through isa, cast, and dyn_cast:
64   static inline bool classof(const ActionHandler *H) {
65     return H->getType() == ActionType::Catch;
66   }
67
68   Constant *getSelector() const { return Selector; }
69   BasicBlock *getNextBB() const { return NextBB; }
70
71   const Value *getExceptionVar() { return ExceptionObjectVar; }
72   TinyPtrVector<BasicBlock *> &getReturnTargets() { return ReturnTargets; }
73
74   void setExceptionVar(const Value *Val) { ExceptionObjectVar = Val; }
75   void setReturnTargets(TinyPtrVector<BasicBlock *> &Targets) {
76     ReturnTargets = Targets;
77   }
78
79 private:
80   Constant *Selector;
81   BasicBlock *NextBB;
82   const Value *ExceptionObjectVar;
83   TinyPtrVector<BasicBlock *> ReturnTargets;
84 };
85
86 class CleanupHandler : public ActionHandler {
87 public:
88   CleanupHandler(BasicBlock *BB) : ActionHandler(BB, ActionType::Cleanup) {}
89
90   // Method for support type inquiry through isa, cast, and dyn_cast:
91   static inline bool classof(const ActionHandler *H) {
92     return H->getType() == ActionType::Cleanup;
93   }
94 };
95
96 // The following structs respresent the .xdata for functions using C++
97 // exceptions on Windows.
98
99 struct WinEHUnwindMapEntry {
100   int ToState;
101   Function *Cleanup;
102 };
103
104 struct WinEHHandlerType {
105   int Adjectives;
106   GlobalVariable *TypeDescriptor;
107   int CatchObjIdx;
108   int CatchObjOffset;
109   Function *Handler;
110 };
111
112 struct WinEHTryBlockMapEntry {
113   int TryLow;
114   int TryHigh;
115   int CatchHigh;
116   SmallVector<WinEHHandlerType, 1> HandlerArray;
117 };
118
119 struct WinEHFuncInfo {
120   DenseMap<const LandingPadInst *, int> LandingPadStateMap;
121   DenseMap<const Function *, int> CatchHandlerParentFrameObjIdx;
122   DenseMap<const Function *, int> CatchHandlerParentFrameObjOffset;
123   SmallVector<WinEHUnwindMapEntry, 4> UnwindMap;
124   SmallVector<WinEHTryBlockMapEntry, 4> TryBlockMap;
125   SmallVector<std::pair<MCSymbol *, int>, 4> IPToStateList;
126   int UnwindHelpFrameIdx;
127   int UnwindHelpFrameOffset;
128
129   WinEHFuncInfo() : UnwindHelpFrameIdx(INT_MAX), UnwindHelpFrameOffset(-1) {}
130 };
131
132 }
133 #endif // LLVM_CODEGEN_WINEHFUNCINFO_H