1 //===-- EHStreamer.h - Exception Handling Directive Streamer ---*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing exception info into assembly files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_EHSTREAMER_H
17 #include "AsmPrinterHandler.h"
18 #include "llvm/ADT/DenseMap.h"
21 struct LandingPadInfo;
22 class MachineModuleInfo;
24 class MachineFunction;
27 class MCSymbolRefExpr;
30 class SmallVectorImpl;
32 /// Emits exception handling directives.
33 class EHStreamer : public AsmPrinterHandler {
34 MCSymbol *CurExceptionSym;
37 /// Target of directive emission.
40 /// Collected machine module information.
41 MachineModuleInfo *MMI;
43 /// How many leading type ids two landing pads have in common.
44 static unsigned sharedTypeIDs(const LandingPadInfo *L,
45 const LandingPadInfo *R);
47 /// Structure holding a try-range and the associated landing pad.
49 // The index of the landing pad.
51 // The index of the begin and end labels in the landing pad's label lists.
55 typedef DenseMap<MCSymbol *, PadRange> RangeMapType;
57 /// Structure describing an entry in the actions table.
59 int ValueForTypeID; // The value to write - may not be equal to the type id.
64 /// Structure describing an entry in the call-site table.
65 struct CallSiteEntry {
66 // The 'try-range' is BeginLabel .. EndLabel.
67 MCSymbol *BeginLabel; // Null indicates the start of the function.
68 MCSymbol *EndLabel; // Null indicates the end of the function.
70 // LPad contains the landing pad start labels.
71 const LandingPadInfo *LPad; // Null indicates that there is no landing pad.
75 /// Compute the actions table and gather the first action index for each
77 unsigned computeActionsTable(const SmallVectorImpl<const LandingPadInfo*>&LPs,
78 SmallVectorImpl<ActionEntry> &Actions,
79 SmallVectorImpl<unsigned> &FirstActions);
81 /// Return `true' if this is a call to a function marked `nounwind'. Return
82 /// `false' otherwise.
83 bool callToNoUnwindFunction(const MachineInstr *MI);
85 /// Compute the call-site table. The entry for an invoke has a try-range
86 /// containing the call, a non-zero landing pad and an appropriate action.
87 /// The entry for an ordinary call has a try-range containing the call and
88 /// zero for the landing pad and the action. Calls marked 'nounwind' have
89 /// no entry and must not be contained in the try-range of any entry - they
90 /// form gaps in the table. Entries must be ordered by try-range address.
92 void computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
93 const SmallVectorImpl<const LandingPadInfo *> &LPs,
94 const SmallVectorImpl<unsigned> &FirstActions);
96 /// Emit landing pads and actions.
98 /// The general organization of the table is complex, but the basic concepts
99 /// are easy. First there is a header which describes the location and
100 /// organization of the three components that follow.
101 /// 1. The landing pad site information describes the range of code covered
102 /// by the try. In our case it's an accumulation of the ranges covered
103 /// by the invokes in the try. There is also a reference to the landing
104 /// pad that handles the exception once processed. Finally an index into
105 /// the actions table.
106 /// 2. The action table, in our case, is composed of pairs of type ids
107 /// and next action offset. Starting with the action index from the
108 /// landing pad site, each type Id is checked for a match to the current
109 /// exception. If it matches then the exception and type id are passed
110 /// on to the landing pad. Otherwise the next action is looked up. This
111 /// chain is terminated with a next action of zero. If no type id is
112 /// found the frame is unwound and handling continues.
113 /// 3. Type id table contains references to all the C++ typeinfo for all
114 /// catches in the function. This tables is reversed indexed base 1.
115 void emitExceptionTable();
117 virtual void emitTypeInfos(unsigned TTypeEncoding);
119 // Helpers for for identifying what kind of clause an EH typeid or selector
120 // corresponds to. Negative selectors are for filter clauses, the zero
121 // selector is for cleanups, and positive selectors are for catch clauses.
122 static bool isFilterEHSelector(int Selector) { return Selector < 0; }
123 static bool isCleanupEHSelector(int Selector) { return Selector == 0; }
124 static bool isCatchEHSelector(int Selector) { return Selector > 0; }
127 EHStreamer(AsmPrinter *A);
128 virtual ~EHStreamer();
130 MCSymbol *getCurExceptionSym();
131 void beginFunction(const MachineFunction *MF) override;
134 void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {}
135 void beginInstruction(const MachineInstr *MI) override {}
136 void endInstruction() override {}