blob: e3a6f8e9d587268e821f716a0ef2871ec04c5745 [file] [log] [blame]
Eugene Zelenko149178d2017-10-10 22:33:29 +00001//===- EHStreamer.h - Exception Handling Directive Streamer -----*- C++ -*-===//
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +00002//
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"
Eugene Zelenko149178d2017-10-10 22:33:29 +000019#include "llvm/Support/Compiler.h"
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000020
21namespace llvm {
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000022
Eugene Zelenko149178d2017-10-10 22:33:29 +000023class AsmPrinter;
24struct LandingPadInfo;
25class MachineInstr;
26class MachineModuleInfo;
27class MCSymbol;
28template <typename T> class SmallVectorImpl;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000029
30/// Emits exception handling directives.
Benjamin Kramer286d4662015-07-01 16:18:16 +000031class LLVM_LIBRARY_VISIBILITY EHStreamer : public AsmPrinterHandler {
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000032protected:
33 /// Target of directive emission.
34 AsmPrinter *Asm;
35
36 /// Collected machine module information.
37 MachineModuleInfo *MMI;
38
39 /// How many leading type ids two landing pads have in common.
40 static unsigned sharedTypeIDs(const LandingPadInfo *L,
41 const LandingPadInfo *R);
42
43 /// Structure holding a try-range and the associated landing pad.
44 struct PadRange {
45 // The index of the landing pad.
46 unsigned PadIndex;
Eugene Zelenko149178d2017-10-10 22:33:29 +000047
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000048 // The index of the begin and end labels in the landing pad's label lists.
49 unsigned RangeIndex;
50 };
51
Eugene Zelenko149178d2017-10-10 22:33:29 +000052 using RangeMapType = DenseMap<MCSymbol *, PadRange>;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000053
54 /// Structure describing an entry in the actions table.
55 struct ActionEntry {
56 int ValueForTypeID; // The value to write - may not be equal to the type id.
57 int NextAction;
58 unsigned Previous;
59 };
60
61 /// Structure describing an entry in the call-site table.
62 struct CallSiteEntry {
63 // The 'try-range' is BeginLabel .. EndLabel.
Reid Kleckner0a57f652015-01-14 01:05:27 +000064 MCSymbol *BeginLabel; // Null indicates the start of the function.
65 MCSymbol *EndLabel; // Null indicates the end of the function.
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000066
Reid Kleckner0a57f652015-01-14 01:05:27 +000067 // LPad contains the landing pad start labels.
68 const LandingPadInfo *LPad; // Null indicates that there is no landing pad.
Eugene Zelenko149178d2017-10-10 22:33:29 +000069
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.
Fangrui Songcb0bab82018-07-16 18:51:40 +000075 void computeActionsTable(const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
Rafael Espindolad09b4162018-02-09 17:00:25 +000076 SmallVectorImpl<ActionEntry> &Actions,
77 SmallVectorImpl<unsigned> &FirstActions);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000078
David Majnemercde33032015-03-30 22:58:10 +000079 void computePadMap(const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
80 RangeMapType &PadMap);
81
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000082 /// Compute the call-site table. The entry for an invoke has a try-range
83 /// containing the call, a non-zero landing pad and an appropriate action.
84 /// The entry for an ordinary call has a try-range containing the call and
85 /// zero for the landing pad and the action. Calls marked 'nounwind' have
86 /// no entry and must not be contained in the try-range of any entry - they
87 /// form gaps in the table. Entries must be ordered by try-range address.
Heejin Ahn24faf852018-10-25 23:55:10 +000088 virtual void computeCallSiteTable(
89 SmallVectorImpl<CallSiteEntry> &CallSites,
90 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
91 const SmallVectorImpl<unsigned> &FirstActions);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000092
93 /// Emit landing pads and actions.
94 ///
95 /// The general organization of the table is complex, but the basic concepts
96 /// are easy. First there is a header which describes the location and
97 /// organization of the three components that follow.
98 /// 1. The landing pad site information describes the range of code covered
99 /// by the try. In our case it's an accumulation of the ranges covered
100 /// by the invokes in the try. There is also a reference to the landing
101 /// pad that handles the exception once processed. Finally an index into
102 /// the actions table.
103 /// 2. The action table, in our case, is composed of pairs of type ids
104 /// and next action offset. Starting with the action index from the
105 /// landing pad site, each type Id is checked for a match to the current
106 /// exception. If it matches then the exception and type id are passed
107 /// on to the landing pad. Otherwise the next action is looked up. This
108 /// chain is terminated with a next action of zero. If no type id is
109 /// found the frame is unwound and handling continues.
110 /// 3. Type id table contains references to all the C++ typeinfo for all
111 /// catches in the function. This tables is reversed indexed base 1.
Heejin Ahn24faf852018-10-25 23:55:10 +0000112 ///
113 /// Returns the starting symbol of an exception table.
114 MCSymbol *emitExceptionTable();
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000115
Rafael Espindolad09b4162018-02-09 17:00:25 +0000116 virtual void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000117
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000118 // Helpers for identifying what kind of clause an EH typeid or selector
Reid Kleckner0a57f652015-01-14 01:05:27 +0000119 // corresponds to. Negative selectors are for filter clauses, the zero
120 // selector is for cleanups, and positive selectors are for catch clauses.
121 static bool isFilterEHSelector(int Selector) { return Selector < 0; }
122 static bool isCleanupEHSelector(int Selector) { return Selector == 0; }
123 static bool isCatchEHSelector(int Selector) { return Selector > 0; }
124
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000125public:
126 EHStreamer(AsmPrinter *A);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000127 ~EHStreamer() override;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000128
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000129 // Unused.
130 void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
131 void beginInstruction(const MachineInstr *MI) override {}
132 void endInstruction() override {}
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000133
134 /// Return `true' if this is a call to a function marked `nounwind'. Return
135 /// `false' otherwise.
136 static bool callToNoUnwindFunction(const MachineInstr *MI);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000137};
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000138
Eugene Zelenko149178d2017-10-10 22:33:29 +0000139} // end namespace llvm
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000140
Eugene Zelenko149178d2017-10-10 22:33:29 +0000141#endif // LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H