blob: 080fdd14b467071712aebf86e4ded20765b6eef1 [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;
Reid Kleckner0a57f652015-01-14 01:05:27 +000025class MCSymbol;
26class MCSymbolRefExpr;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000027
28template <typename T>
29class SmallVectorImpl;
30
31/// Emits exception handling directives.
Benjamin Kramer286d4662015-07-01 16:18:16 +000032class LLVM_LIBRARY_VISIBILITY EHStreamer : public AsmPrinterHandler {
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000033protected:
34 /// Target of directive emission.
35 AsmPrinter *Asm;
36
37 /// Collected machine module information.
38 MachineModuleInfo *MMI;
39
40 /// How many leading type ids two landing pads have in common.
41 static unsigned sharedTypeIDs(const LandingPadInfo *L,
42 const LandingPadInfo *R);
43
44 /// Structure holding a try-range and the associated landing pad.
45 struct PadRange {
46 // The index of the landing pad.
47 unsigned PadIndex;
48 // The index of the begin and end labels in the landing pad's label lists.
49 unsigned RangeIndex;
50 };
51
52 typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
53
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.
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.
74 unsigned computeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
75 SmallVectorImpl<ActionEntry> &Actions,
76 SmallVectorImpl<unsigned> &FirstActions);
77
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.
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000087 void computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000088 const SmallVectorImpl<const LandingPadInfo *> &LPs,
89 const SmallVectorImpl<unsigned> &FirstActions);
90
91 /// Emit landing pads and actions.
92 ///
93 /// The general organization of the table is complex, but the basic concepts
94 /// are easy. First there is a header which describes the location and
95 /// organization of the three components that follow.
96 /// 1. The landing pad site information describes the range of code covered
97 /// by the try. In our case it's an accumulation of the ranges covered
98 /// by the invokes in the try. There is also a reference to the landing
99 /// pad that handles the exception once processed. Finally an index into
100 /// the actions table.
101 /// 2. The action table, in our case, is composed of pairs of type ids
102 /// and next action offset. Starting with the action index from the
103 /// landing pad site, each type Id is checked for a match to the current
104 /// exception. If it matches then the exception and type id are passed
105 /// on to the landing pad. Otherwise the next action is looked up. This
106 /// chain is terminated with a next action of zero. If no type id is
107 /// found the frame is unwound and handling continues.
108 /// 3. Type id table contains references to all the C++ typeinfo for all
109 /// catches in the function. This tables is reversed indexed base 1.
110 void emitExceptionTable();
111
112 virtual void emitTypeInfos(unsigned TTypeEncoding);
113
Reid Kleckner0a57f652015-01-14 01:05:27 +0000114 // Helpers for for identifying what kind of clause an EH typeid or selector
115 // corresponds to. Negative selectors are for filter clauses, the zero
116 // selector is for cleanups, and positive selectors are for catch clauses.
117 static bool isFilterEHSelector(int Selector) { return Selector < 0; }
118 static bool isCleanupEHSelector(int Selector) { return Selector == 0; }
119 static bool isCatchEHSelector(int Selector) { return Selector > 0; }
120
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000121public:
122 EHStreamer(AsmPrinter *A);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000123 ~EHStreamer() override;
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000124
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000125 // Unused.
126 void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
127 void beginInstruction(const MachineInstr *MI) override {}
128 void endInstruction() override {}
Reid Klecknerfc64fae2015-10-01 21:38:24 +0000129
130 /// Return `true' if this is a call to a function marked `nounwind'. Return
131 /// `false' otherwise.
132 static bool callToNoUnwindFunction(const MachineInstr *MI);
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000133};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000134}
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000135
136#endif
137