blob: e62cf17a05d4b95060c9cc8454844c2bfa9d8b81 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing exception info into assembly files.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
14#define LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000015
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000016#include "llvm/ADT/DenseMap.h"
Yonghong Song61b189e2018-12-18 23:10:17 +000017#include "llvm/CodeGen/AsmPrinterHandler.h"
Eugene Zelenko149178d2017-10-10 22:33:29 +000018#include "llvm/Support/Compiler.h"
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000019
20namespace llvm {
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000021
Eugene Zelenko149178d2017-10-10 22:33:29 +000022class AsmPrinter;
23struct LandingPadInfo;
24class MachineInstr;
25class MachineModuleInfo;
26class MCSymbol;
27template <typename T> class SmallVectorImpl;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000028
29/// Emits exception handling directives.
Benjamin Kramer286d4662015-07-01 16:18:16 +000030class LLVM_LIBRARY_VISIBILITY EHStreamer : public AsmPrinterHandler {
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000031protected:
32 /// Target of directive emission.
33 AsmPrinter *Asm;
34
35 /// Collected machine module information.
36 MachineModuleInfo *MMI;
37
38 /// How many leading type ids two landing pads have in common.
39 static unsigned sharedTypeIDs(const LandingPadInfo *L,
40 const LandingPadInfo *R);
41
42 /// Structure holding a try-range and the associated landing pad.
43 struct PadRange {
44 // The index of the landing pad.
45 unsigned PadIndex;
Eugene Zelenko149178d2017-10-10 22:33:29 +000046
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000047 // The index of the begin and end labels in the landing pad's label lists.
48 unsigned RangeIndex;
49 };
50
Eugene Zelenko149178d2017-10-10 22:33:29 +000051 using RangeMapType = DenseMap<MCSymbol *, PadRange>;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000052
53 /// Structure describing an entry in the actions table.
54 struct ActionEntry {
55 int ValueForTypeID; // The value to write - may not be equal to the type id.
56 int NextAction;
57 unsigned Previous;
58 };
59
60 /// Structure describing an entry in the call-site table.
61 struct CallSiteEntry {
62 // The 'try-range' is BeginLabel .. EndLabel.
Reid Kleckner0a57f652015-01-14 01:05:27 +000063 MCSymbol *BeginLabel; // Null indicates the start of the function.
64 MCSymbol *EndLabel; // Null indicates the end of the function.
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000065
Reid Kleckner0a57f652015-01-14 01:05:27 +000066 // LPad contains the landing pad start labels.
67 const LandingPadInfo *LPad; // Null indicates that there is no landing pad.
Eugene Zelenko149178d2017-10-10 22:33:29 +000068
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000069 unsigned Action;
70 };
71
72 /// Compute the actions table and gather the first action index for each
73 /// landing pad site.
Fangrui Songcb0bab82018-07-16 18:51:40 +000074 void computeActionsTable(const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
Rafael Espindolad09b4162018-02-09 17:00:25 +000075 SmallVectorImpl<ActionEntry> &Actions,
76 SmallVectorImpl<unsigned> &FirstActions);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000077
David Majnemercde33032015-03-30 22:58:10 +000078 void computePadMap(const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
79 RangeMapType &PadMap);
80
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000081 /// Compute the call-site table. The entry for an invoke has a try-range
82 /// containing the call, a non-zero landing pad and an appropriate action.
83 /// The entry for an ordinary call has a try-range containing the call and
84 /// zero for the landing pad and the action. Calls marked 'nounwind' have
85 /// no entry and must not be contained in the try-range of any entry - they
86 /// form gaps in the table. Entries must be ordered by try-range address.
Heejin Ahn24faf852018-10-25 23:55:10 +000087 virtual void computeCallSiteTable(
88 SmallVectorImpl<CallSiteEntry> &CallSites,
89 const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
90 const SmallVectorImpl<unsigned> &FirstActions);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000091
92 /// Emit landing pads and actions.
93 ///
94 /// The general organization of the table is complex, but the basic concepts
95 /// are easy. First there is a header which describes the location and
96 /// organization of the three components that follow.
97 /// 1. The landing pad site information describes the range of code covered
98 /// by the try. In our case it's an accumulation of the ranges covered
99 /// by the invokes in the try. There is also a reference to the landing
100 /// pad that handles the exception once processed. Finally an index into
101 /// the actions table.
102 /// 2. The action table, in our case, is composed of pairs of type ids
103 /// and next action offset. Starting with the action index from the
104 /// landing pad site, each type Id is checked for a match to the current
105 /// exception. If it matches then the exception and type id are passed
106 /// on to the landing pad. Otherwise the next action is looked up. This
107 /// chain is terminated with a next action of zero. If no type id is
108 /// found the frame is unwound and handling continues.
109 /// 3. Type id table contains references to all the C++ typeinfo for all
110 /// catches in the function. This tables is reversed indexed base 1.
Heejin Ahn24faf852018-10-25 23:55:10 +0000111 ///
112 /// Returns the starting symbol of an exception table.
113 MCSymbol *emitExceptionTable();
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000114
Rafael Espindolad09b4162018-02-09 17:00:25 +0000115 virtual void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000116
Hiroshi Inoue0909ca12018-01-26 08:15:29 +0000117 // Helpers for identifying what kind of clause an EH typeid or selector
Reid Kleckner0a57f652015-01-14 01:05:27 +0000118 // 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);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000126 ~EHStreamer() override;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000127
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000128 // Unused.
129 void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
130 void beginInstruction(const MachineInstr *MI) override {}
131 void endInstruction() override {}
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000132
133 /// Return `true' if this is a call to a function marked `nounwind'. Return
134 /// `false' otherwise.
135 static bool callToNoUnwindFunction(const MachineInstr *MI);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000136};
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000137
Eugene Zelenko149178d2017-10-10 22:33:29 +0000138} // end namespace llvm
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000139
Eugene Zelenko149178d2017-10-10 22:33:29 +0000140#endif // LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H