blob: 9b316ff00e9bb6364f182f3e2beb43f52dcdd30e [file] [log] [blame]
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +00001//===-- EHStreamer.h - Exception Handling Directive Streamer ---*- 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 exception info into assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
15#define LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000016
17#include "AsmPrinterHandler.h"
18#include "llvm/ADT/DenseMap.h"
19
20namespace llvm {
21struct LandingPadInfo;
22class MachineModuleInfo;
23class MachineInstr;
24class MachineFunction;
25class AsmPrinter;
Reid Kleckner0a57f652015-01-14 01:05:27 +000026class MCSymbol;
27class MCSymbolRefExpr;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000028
29template <typename T>
30class SmallVectorImpl;
31
32/// Emits exception handling directives.
33class EHStreamer : public AsmPrinterHandler {
34protected:
35 /// Target of directive emission.
36 AsmPrinter *Asm;
37
38 /// Collected machine module information.
39 MachineModuleInfo *MMI;
40
41 /// How many leading type ids two landing pads have in common.
42 static unsigned sharedTypeIDs(const LandingPadInfo *L,
43 const LandingPadInfo *R);
44
45 /// Structure holding a try-range and the associated landing pad.
46 struct PadRange {
47 // The index of the landing pad.
48 unsigned PadIndex;
49 // The index of the begin and end labels in the landing pad's label lists.
50 unsigned RangeIndex;
51 };
52
53 typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
54
55 /// Structure describing an entry in the actions table.
56 struct ActionEntry {
57 int ValueForTypeID; // The value to write - may not be equal to the type id.
58 int NextAction;
59 unsigned Previous;
60 };
61
62 /// Structure describing an entry in the call-site table.
63 struct CallSiteEntry {
64 // The 'try-range' is BeginLabel .. EndLabel.
Reid Kleckner0a57f652015-01-14 01:05:27 +000065 MCSymbol *BeginLabel; // Null indicates the start of the function.
66 MCSymbol *EndLabel; // Null indicates the end of the function.
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000067
Reid Kleckner0a57f652015-01-14 01:05:27 +000068 // LPad contains the landing pad start labels.
69 const LandingPadInfo *LPad; // Null indicates that there is no landing pad.
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000070 unsigned Action;
71 };
72
73 /// Compute the actions table and gather the first action index for each
74 /// landing pad site.
75 unsigned computeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
76 SmallVectorImpl<ActionEntry> &Actions,
77 SmallVectorImpl<unsigned> &FirstActions);
78
79 /// Return `true' if this is a call to a function marked `nounwind'. Return
80 /// `false' otherwise.
81 bool callToNoUnwindFunction(const MachineInstr *MI);
82
83 /// Compute the call-site table. The entry for an invoke has a try-range
84 /// containing the call, a non-zero landing pad and an appropriate action.
85 /// The entry for an ordinary call has a try-range containing the call and
86 /// zero for the landing pad and the action. Calls marked 'nounwind' have
87 /// no entry and must not be contained in the try-range of any entry - they
88 /// form gaps in the table. Entries must be ordered by try-range address.
89
90 void computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000091 const SmallVectorImpl<const LandingPadInfo *> &LPs,
92 const SmallVectorImpl<unsigned> &FirstActions);
93
94 /// Emit landing pads and actions.
95 ///
96 /// The general organization of the table is complex, but the basic concepts
97 /// are easy. First there is a header which describes the location and
98 /// organization of the three components that follow.
99 /// 1. The landing pad site information describes the range of code covered
100 /// by the try. In our case it's an accumulation of the ranges covered
101 /// by the invokes in the try. There is also a reference to the landing
102 /// pad that handles the exception once processed. Finally an index into
103 /// the actions table.
104 /// 2. The action table, in our case, is composed of pairs of type ids
105 /// and next action offset. Starting with the action index from the
106 /// landing pad site, each type Id is checked for a match to the current
107 /// exception. If it matches then the exception and type id are passed
108 /// on to the landing pad. Otherwise the next action is looked up. This
109 /// chain is terminated with a next action of zero. If no type id is
110 /// found the frame is unwound and handling continues.
111 /// 3. Type id table contains references to all the C++ typeinfo for all
112 /// catches in the function. This tables is reversed indexed base 1.
113 void emitExceptionTable();
114
115 virtual void emitTypeInfos(unsigned TTypeEncoding);
116
Reid Kleckner0a57f652015-01-14 01:05:27 +0000117 // Helpers for for identifying what kind of clause an EH typeid or selector
118 // corresponds to. Negative selectors are for filter clauses, the zero
119 // selector is for cleanups, and positive selectors are for catch clauses.
120 static bool isFilterEHSelector(int Selector) { return Selector < 0; }
121 static bool isCleanupEHSelector(int Selector) { return Selector == 0; }
122 static bool isCatchEHSelector(int Selector) { return Selector > 0; }
123
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000124public:
125 EHStreamer(AsmPrinter *A);
126 virtual ~EHStreamer();
127
128 /// Emit all exception information that should come after the content.
129 void endModule() override;
130
131 /// Gather pre-function exception information. Assumes being emitted
132 /// immediately after the function entry point.
133 void beginFunction(const MachineFunction *MF) override;
134
135 /// Gather and emit post-function exception information.
136 void endFunction(const MachineFunction *) override;
137
138 // Unused.
139 void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
140 void beginInstruction(const MachineInstr *MI) override {}
141 void endInstruction() override {}
142};
143}
144
145#endif
146