Eugene Zelenko | 149178d | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 1 | //===- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer ---===// |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 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 | // |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 10 | // This file contains support for writing exception info into assembly files. |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 14 | #include "EHStreamer.h" |
Eugene Zelenko | 149178d | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallVector.h" |
| 16 | #include "llvm/ADT/Twine.h" |
| 17 | #include "llvm/ADT/iterator_range.h" |
| 18 | #include "llvm/BinaryFormat/Dwarf.h" |
Chris Lattner | da790ea | 2010-04-05 05:28:23 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/AsmPrinter.h" |
David Greene | a5be1b1 | 2009-08-19 21:55:33 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 149178d | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineOperand.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
Chris Lattner | b866602 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | 149178d | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 4b7dadb | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | 555ceabe | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 149178d | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCTargetOptions.h" |
| 30 | #include "llvm/Support/Casting.h" |
Logan Chien | 5b776b7 | 2014-02-22 14:00:39 +0000 | [diff] [blame] | 31 | #include "llvm/Support/LEB128.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Eugene Zelenko | 149178d | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 33 | #include <algorithm> |
| 34 | #include <cassert> |
| 35 | #include <cstdint> |
| 36 | #include <vector> |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 37 | |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Rafael Espindola | dc4263c | 2015-03-17 13:57:48 +0000 | [diff] [blame] | 40 | EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} |
Bill Wendling | dd20b98 | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 41 | |
Eugene Zelenko | 149178d | 2017-10-10 22:33:29 +0000 | [diff] [blame] | 42 | EHStreamer::~EHStreamer() = default; |
Bill Wendling | dd20b98 | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 43 | |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 44 | /// How many leading type ids two landing pads have in common. |
| 45 | unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L, |
| 46 | const LandingPadInfo *R) { |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 47 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 48 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 49 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 50 | unsigned Count = 0; |
| 51 | |
| 52 | for (; Count != MinSize; ++Count) |
| 53 | if (LIds[Count] != RIds[Count]) |
| 54 | return Count; |
| 55 | |
| 56 | return Count; |
| 57 | } |
| 58 | |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 59 | /// Compute the actions table and gather the first action index for each landing |
| 60 | /// pad site. |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 61 | void EHStreamer::computeActionsTable( |
| 62 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, |
| 63 | SmallVectorImpl<ActionEntry> &Actions, |
| 64 | SmallVectorImpl<unsigned> &FirstActions) { |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 65 | // The action table follows the call-site table in the LSDA. The individual |
| 66 | // records are of two types: |
| 67 | // |
| 68 | // * Catch clause |
| 69 | // * Exception specification |
| 70 | // |
| 71 | // The two record kinds have the same format, with only small differences. |
| 72 | // They are distinguished by the "switch value" field: Catch clauses |
| 73 | // (TypeInfos) have strictly positive switch values, and exception |
| 74 | // specifications (FilterIds) have strictly negative switch values. Value 0 |
| 75 | // indicates a catch-all clause. |
| 76 | // |
Bill Wendling | 1fddd87 | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 77 | // Negative type IDs index into FilterIds. Positive type IDs index into |
| 78 | // TypeInfos. The value written for a positive type ID is just the type ID |
| 79 | // itself. For a negative type ID, however, the value written is the |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 80 | // (negative) byte offset of the corresponding FilterIds entry. The byte |
Bill Wendling | 1fddd87 | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 81 | // offset is usually equal to the type ID (because the FilterIds entries are |
| 82 | // written using a variable width encoding, which outputs one byte per entry |
| 83 | // as long as the value written is not too large) but can differ. This kind |
| 84 | // of complication does not occur for positive type IDs because type infos are |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 85 | // output using a fixed width encoding. FilterOffsets[i] holds the byte |
| 86 | // offset corresponding to FilterIds[i]. |
Bill Wendling | 4505734 | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 87 | |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 88 | const std::vector<unsigned> &FilterIds = Asm->MF->getFilterIds(); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 89 | SmallVector<int, 16> FilterOffsets; |
| 90 | FilterOffsets.reserve(FilterIds.size()); |
| 91 | int Offset = -1; |
Bill Wendling | 4505734 | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 92 | |
| 93 | for (std::vector<unsigned>::const_iterator |
| 94 | I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 95 | FilterOffsets.push_back(Offset); |
Logan Chien | 5b776b7 | 2014-02-22 14:00:39 +0000 | [diff] [blame] | 96 | Offset -= getULEB128Size(*I); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 99 | FirstActions.reserve(LandingPads.size()); |
| 100 | |
| 101 | int FirstAction = 0; |
| 102 | unsigned SizeActions = 0; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 103 | const LandingPadInfo *PrevLPI = nullptr; |
Bill Wendling | 4505734 | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 104 | |
Bill Wendling | 23b177e | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 105 | for (SmallVectorImpl<const LandingPadInfo *>::const_iterator |
Bill Wendling | 1fddd87 | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 106 | I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { |
| 107 | const LandingPadInfo *LPI = *I; |
| 108 | const std::vector<int> &TypeIds = LPI->TypeIds; |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 109 | unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 110 | unsigned SizeSiteActions = 0; |
| 111 | |
| 112 | if (NumShared < TypeIds.size()) { |
| 113 | unsigned SizeAction = 0; |
Bill Wendling | 7742b64 | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 114 | unsigned PrevAction = (unsigned)-1; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 115 | |
| 116 | if (NumShared) { |
Chris Lattner | 07c1b94 | 2010-04-04 20:10:41 +0000 | [diff] [blame] | 117 | unsigned SizePrevIds = PrevLPI->TypeIds.size(); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 118 | assert(Actions.size()); |
Bill Wendling | 7742b64 | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 119 | PrevAction = Actions.size() - 1; |
Logan Chien | 5b776b7 | 2014-02-22 14:00:39 +0000 | [diff] [blame] | 120 | SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) + |
| 121 | getSLEB128Size(Actions[PrevAction].ValueForTypeID); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 122 | |
| 123 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { |
Bill Wendling | 7742b64 | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 124 | assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); |
Logan Chien | 5b776b7 | 2014-02-22 14:00:39 +0000 | [diff] [blame] | 125 | SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID); |
Bill Wendling | 7742b64 | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 126 | SizeAction += -Actions[PrevAction].NextAction; |
| 127 | PrevAction = Actions[PrevAction].Previous; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | // Compute the actions. |
Bill Wendling | 1fddd87 | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 132 | for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { |
| 133 | int TypeID = TypeIds[J]; |
| 134 | assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 135 | int ValueForTypeID = |
| 136 | isFilterEHSelector(TypeID) ? FilterOffsets[-1 - TypeID] : TypeID; |
Logan Chien | 5b776b7 | 2014-02-22 14:00:39 +0000 | [diff] [blame] | 137 | unsigned SizeTypeID = getSLEB128Size(ValueForTypeID); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 138 | |
| 139 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; |
Logan Chien | 5b776b7 | 2014-02-22 14:00:39 +0000 | [diff] [blame] | 140 | SizeAction = SizeTypeID + getSLEB128Size(NextAction); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 141 | SizeSiteActions += SizeAction; |
| 142 | |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 143 | ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 144 | Actions.push_back(Action); |
Bill Wendling | 7742b64 | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 145 | PrevAction = Actions.size() - 1; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Record the first action of the landing pad site. |
| 149 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; |
| 150 | } // else identical - re-use previous FirstAction |
| 151 | |
Eric Christopher | d72f78e | 2018-01-09 23:25:38 +0000 | [diff] [blame] | 152 | // Information used when creating the call-site table. The action record |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 153 | // field of the call site record is the offset of the first associated |
| 154 | // action record, relative to the start of the actions table. This value is |
Bill Wendling | 7742b64 | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 155 | // biased by 1 (1 indicating the start of the actions table), and 0 |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 156 | // indicates that there are no actions. |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 157 | FirstActions.push_back(FirstAction); |
| 158 | |
| 159 | // Compute this sites contribution to size. |
| 160 | SizeActions += SizeSiteActions; |
Bill Wendling | 1fddd87 | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 161 | |
| 162 | PrevLPI = LPI; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 163 | } |
Bill Wendling | 1fddd87 | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 166 | /// Return `true' if this is a call to a function marked `nounwind'. Return |
| 167 | /// `false' otherwise. |
| 168 | bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) { |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 169 | assert(MI->isCall() && "This should be a call instruction!"); |
Bill Wendling | e412064 | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 170 | |
| 171 | bool MarkedNoUnwind = false; |
| 172 | bool SawFunc = false; |
| 173 | |
| 174 | for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { |
| 175 | const MachineOperand &MO = MI->getOperand(I); |
| 176 | |
Chris Lattner | 17d3859 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 177 | if (!MO.isGlobal()) continue; |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 178 | |
Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 179 | const Function *F = dyn_cast<Function>(MO.getGlobal()); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 180 | if (!F) continue; |
Bill Wendling | c781d7a | 2009-11-12 23:13:08 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 17d3859 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 182 | if (SawFunc) { |
| 183 | // Be conservative. If we have more than one function operand for this |
| 184 | // call, then we can't make the assumption that it's the callee and |
| 185 | // not a parameter to the call. |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 186 | // |
Chris Lattner | 17d3859 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 187 | // FIXME: Determine if there's a way to say that `F' is the callee or |
| 188 | // parameter. |
| 189 | MarkedNoUnwind = false; |
| 190 | break; |
Bill Wendling | e412064 | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 191 | } |
Chris Lattner | 17d3859 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 192 | |
| 193 | MarkedNoUnwind = F->doesNotThrow(); |
| 194 | SawFunc = true; |
Bill Wendling | e412064 | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return MarkedNoUnwind; |
| 198 | } |
| 199 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 200 | void EHStreamer::computePadMap( |
| 201 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, |
| 202 | RangeMapType &PadMap) { |
| 203 | // Invokes and nounwind calls have entries in PadMap (due to being bracketed |
| 204 | // by try-range labels when lowered). Ordinary calls do not, so appropriate |
| 205 | // try-ranges for them need be deduced so we can put them in the LSDA. |
| 206 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 207 | const LandingPadInfo *LandingPad = LandingPads[i]; |
| 208 | for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { |
| 209 | MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; |
| 210 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); |
| 211 | PadRange P = { i, j }; |
| 212 | PadMap[BeginLabel] = P; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 217 | /// Compute the call-site table. The entry for an invoke has a try-range |
| 218 | /// containing the call, a non-zero landing pad, and an appropriate action. The |
| 219 | /// entry for an ordinary call has a try-range containing the call and zero for |
| 220 | /// the landing pad and the action. Calls marked 'nounwind' have no entry and |
| 221 | /// must not be contained in the try-range of any entry - they form gaps in the |
| 222 | /// table. Entries must be ordered by try-range address. |
| 223 | void EHStreamer:: |
| 224 | computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, |
Bill Wendling | b67440e | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 225 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, |
| 226 | const SmallVectorImpl<unsigned> &FirstActions) { |
Reid Kleckner | f2acbba | 2014-12-19 22:30:08 +0000 | [diff] [blame] | 227 | RangeMapType PadMap; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 228 | computePadMap(LandingPads, PadMap); |
Reid Kleckner | f2acbba | 2014-12-19 22:30:08 +0000 | [diff] [blame] | 229 | |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 230 | // The end label of the previous invoke or nounwind try-range. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 231 | MCSymbol *LastLabel = nullptr; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 232 | |
| 233 | // Whether there is a potentially throwing instruction (currently this means |
| 234 | // an ordinary call) between the end of the previous try-range and now. |
| 235 | bool SawPotentiallyThrowing = false; |
| 236 | |
Bill Wendling | 23b177e | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 237 | // Whether the last CallSite entry was for an invoke. |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 238 | bool PreviousIsInvoke = false; |
| 239 | |
Reid Kleckner | 93acac6 | 2014-12-19 22:19:48 +0000 | [diff] [blame] | 240 | bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; |
| 241 | |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 242 | // Visit all instructions in order of address. |
Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 243 | for (const auto &MBB : *Asm->MF) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 244 | for (const auto &MI : MBB) { |
| 245 | if (!MI.isEHLabel()) { |
| 246 | if (MI.isCall()) |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 247 | SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 248 | continue; |
| 249 | } |
| 250 | |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 251 | // End of the previous try-range? |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 252 | MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 253 | if (BeginLabel == LastLabel) |
| 254 | SawPotentiallyThrowing = false; |
| 255 | |
| 256 | // Beginning of a new try-range? |
Jeffrey Yasskin | b40d3f7 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 257 | RangeMapType::const_iterator L = PadMap.find(BeginLabel); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 258 | if (L == PadMap.end()) |
| 259 | // Nope, it was just some random label. |
| 260 | continue; |
| 261 | |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 262 | const PadRange &P = L->second; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 263 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 264 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && |
| 265 | "Inconsistent landing pad map!"); |
| 266 | |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 267 | // For Dwarf exception handling (SjLj handling doesn't use this). If some |
| 268 | // instruction between the previous try-range and this one may throw, |
| 269 | // create a call-site entry with no landing pad for the region between the |
| 270 | // try-ranges. |
Reid Kleckner | 5cc1569 | 2015-01-23 18:49:01 +0000 | [diff] [blame] | 271 | if (SawPotentiallyThrowing && Asm->MAI->usesCFIForEH()) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 272 | CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 }; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 273 | CallSites.push_back(Site); |
| 274 | PreviousIsInvoke = false; |
| 275 | } |
| 276 | |
| 277 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; |
| 278 | assert(BeginLabel && LastLabel && "Invalid landing pad!"); |
| 279 | |
Chris Lattner | 17d3859 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 280 | if (!LandingPad->LandingPadLabel) { |
| 281 | // Create a gap. |
| 282 | PreviousIsInvoke = false; |
| 283 | } else { |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 284 | // This try-range is for an invoke. |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 285 | CallSiteEntry Site = { |
| 286 | BeginLabel, |
| 287 | LastLabel, |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 288 | LandingPad, |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 289 | FirstActions[P.PadIndex] |
| 290 | }; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 291 | |
Jim Grosbach | 5afbf2b | 2009-09-01 17:19:13 +0000 | [diff] [blame] | 292 | // Try to merge with the previous call-site. SJLJ doesn't do this |
Reid Kleckner | 93acac6 | 2014-12-19 22:19:48 +0000 | [diff] [blame] | 293 | if (PreviousIsInvoke && !IsSJLJ) { |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 294 | CallSiteEntry &Prev = CallSites.back(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 295 | if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) { |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 296 | // Extend the range of the previous entry. |
| 297 | Prev.EndLabel = Site.EndLabel; |
| 298 | continue; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // Otherwise, create a new call-site. |
Reid Kleckner | 93acac6 | 2014-12-19 22:19:48 +0000 | [diff] [blame] | 303 | if (!IsSJLJ) |
Jim Grosbach | 54c0530 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 304 | CallSites.push_back(Site); |
| 305 | else { |
| 306 | // SjLj EH must maintain the call sites in the order assigned |
| 307 | // to them by the SjLjPrepare pass. |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 308 | unsigned SiteNo = Asm->MF->getCallSiteBeginLabel(BeginLabel); |
Jim Grosbach | 54c0530 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 309 | if (CallSites.size() < SiteNo) |
| 310 | CallSites.resize(SiteNo); |
| 311 | CallSites[SiteNo - 1] = Site; |
| 312 | } |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 313 | PreviousIsInvoke = true; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // If some instruction between the previous try-range and the end of the |
| 319 | // function may throw, create a call-site entry with no landing pad for the |
| 320 | // region following the try-range. |
Reid Kleckner | c2f1bbf | 2017-05-31 22:18:49 +0000 | [diff] [blame] | 321 | if (SawPotentiallyThrowing && !IsSJLJ) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 322 | CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 }; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 323 | CallSites.push_back(Site); |
| 324 | } |
Bill Wendling | b67440e | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 327 | /// Emit landing pads and actions. |
Bill Wendling | 2cf0014 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 328 | /// |
| 329 | /// The general organization of the table is complex, but the basic concepts are |
| 330 | /// easy. First there is a header which describes the location and organization |
| 331 | /// of the three components that follow. |
Eric Christopher | d517ac0 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 332 | /// |
Bill Wendling | 2cf0014 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 333 | /// 1. The landing pad site information describes the range of code covered by |
| 334 | /// the try. In our case it's an accumulation of the ranges covered by the |
| 335 | /// invokes in the try. There is also a reference to the landing pad that |
| 336 | /// handles the exception once processed. Finally an index into the actions |
| 337 | /// table. |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 338 | /// 2. The action table, in our case, is composed of pairs of type IDs and next |
Bill Wendling | 2cf0014 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 339 | /// action offset. Starting with the action index from the landing pad |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 340 | /// site, each type ID is checked for a match to the current exception. If |
Bill Wendling | 2cf0014 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 341 | /// it matches then the exception and type id are passed on to the landing |
| 342 | /// pad. Otherwise the next action is looked up. This chain is terminated |
Bill Wendling | 023ed64 | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 343 | /// with a next action of zero. If no type id is found then the frame is |
Bill Wendling | 2cf0014 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 344 | /// unwound and handling continues. |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 345 | /// 3. Type ID table contains references to all the C++ typeinfo for all |
Bill Wendling | 023ed64 | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 346 | /// catches in the function. This tables is reverse indexed base 1. |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 347 | void EHStreamer::emitExceptionTable() { |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 348 | const MachineFunction *MF = Asm->MF; |
| 349 | const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); |
| 350 | const std::vector<unsigned> &FilterIds = MF->getFilterIds(); |
| 351 | const std::vector<LandingPadInfo> &PadInfos = MF->getLandingPads(); |
Bill Wendling | b67440e | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 352 | |
| 353 | // Sort the landing pads in order of their type ids. This is used to fold |
| 354 | // duplicate actions. |
| 355 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 356 | LandingPads.reserve(PadInfos.size()); |
| 357 | |
| 358 | for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) |
| 359 | LandingPads.push_back(&PadInfos[i]); |
| 360 | |
Benjamin Kramer | b0f74b2 | 2014-03-07 21:35:39 +0000 | [diff] [blame] | 361 | // Order landing pads lexicographically by type id. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame^] | 362 | llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) { |
| 363 | return L->TypeIds < R->TypeIds; |
| 364 | }); |
Bill Wendling | b67440e | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 365 | |
| 366 | // Compute the actions table and gather the first action index for each |
| 367 | // landing pad site. |
| 368 | SmallVector<ActionEntry, 32> Actions; |
| 369 | SmallVector<unsigned, 64> FirstActions; |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 370 | computeActionsTable(LandingPads, Actions, FirstActions); |
Bill Wendling | b67440e | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 371 | |
Bill Wendling | b67440e | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 372 | // Compute the call-site table. |
| 373 | SmallVector<CallSiteEntry, 64> CallSites; |
Reid Kleckner | f2acbba | 2014-12-19 22:30:08 +0000 | [diff] [blame] | 374 | computeCallSiteTable(CallSites, LandingPads, FirstActions); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 375 | |
Chris Lattner | fd79502 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 376 | bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; |
Rafael Espindola | c052fa0 | 2018-02-09 17:13:37 +0000 | [diff] [blame] | 377 | unsigned CallSiteEncoding = |
| 378 | IsSJLJ ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_uleb128; |
| 379 | bool HaveTTData = !TypeInfos.empty() || !FilterIds.empty(); |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 380 | |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 381 | // Type infos. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 382 | MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 383 | unsigned TTypeEncoding; |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 384 | |
Bill Wendling | bf56268 | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 385 | if (!HaveTTData) { |
Rafael Espindola | c052fa0 | 2018-02-09 17:13:37 +0000 | [diff] [blame] | 386 | // If there is no TypeInfo, then we just explicitly say that we're omitting |
| 387 | // that bit. |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 388 | TTypeEncoding = dwarf::DW_EH_PE_omit; |
Chris Lattner | 47fc235 | 2009-07-31 22:03:47 +0000 | [diff] [blame] | 389 | } else { |
Chris Lattner | 7005cd3 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 390 | // Okay, we have actual filters or typeinfos to emit. As such, we need to |
| 391 | // pick a type encoding for them. We're about to emit a list of pointers to |
| 392 | // typeinfo objects at the end of the LSDA. However, unless we're in static |
| 393 | // mode, this reference will require a relocation by the dynamic linker. |
Chris Lattner | f4b92a8 | 2009-07-31 22:18:14 +0000 | [diff] [blame] | 394 | // |
Chris Lattner | 7005cd3 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 395 | // Because of this, we have a couple of options: |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 396 | // |
Chris Lattner | 7005cd3 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 397 | // 1) If we are in -static mode, we can always use an absolute reference |
| 398 | // from the LSDA, because the static linker will resolve it. |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 399 | // |
Chris Lattner | 7005cd3 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 400 | // 2) Otherwise, if the LSDA section is writable, we can output the direct |
| 401 | // reference to the typeinfo and allow the dynamic linker to relocate |
| 402 | // it. Since it is in a writable section, the dynamic linker won't |
| 403 | // have a problem. |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 404 | // |
Chris Lattner | 7005cd3 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 405 | // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, |
| 406 | // we need to use some form of indirection. For example, on Darwin, |
| 407 | // we can output a statically-relocatable reference to a dyld stub. The |
| 408 | // offset to the stub is constant, but the contents are in a section |
| 409 | // that is updated by the dynamic linker. This is easy enough, but we |
| 410 | // need to tell the personality function of the unwinder to indirect |
| 411 | // through the dyld stub. |
| 412 | // |
Bill Wendling | bf56268 | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 413 | // FIXME: When (3) is actually implemented, we'll have to emit the stubs |
Chris Lattner | 7005cd3 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 414 | // somewhere. This predicate should be moved to a shared location that is |
| 415 | // in target-independent code. |
| 416 | // |
Anton Korobeynikov | ae4ccc1 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 417 | TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); |
Bill Wendling | 243ac56 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 418 | } |
Bill Wendling | bf56268 | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 419 | |
Bill Wendling | 243ac56 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 420 | // Begin the exception table. |
Anton Korobeynikov | fe3a6e0 | 2011-01-30 22:07:31 +0000 | [diff] [blame] | 421 | // Sometimes we want not to emit the data into separate section (e.g. ARM |
| 422 | // EHABI). In this case LSDASection will be NULL. |
Anton Korobeynikov | b15beb2 | 2011-01-24 22:38:40 +0000 | [diff] [blame] | 423 | if (LSDASection) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 424 | Asm->OutStreamer->SwitchSection(LSDASection); |
Chris Lattner | 9e06e53 | 2010-04-28 01:05:45 +0000 | [diff] [blame] | 425 | Asm->EmitAlignment(2); |
Bill Wendling | bf56268 | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 426 | |
Bill Wendling | 88fdcd3 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 427 | // Emit the LSDA. |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 428 | MCSymbol *GCCETSym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 429 | Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+ |
Chris Lattner | fd79502 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 430 | Twine(Asm->getFunctionNumber())); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 431 | Asm->OutStreamer->EmitLabel(GCCETSym); |
| 432 | Asm->OutStreamer->EmitLabel(Asm->getCurExceptionSym()); |
Bill Wendling | 243ac56 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 433 | |
Bill Wendling | 88fdcd3 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 434 | // Emit the LSDA header. |
Chris Lattner | b75af3c | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 435 | Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); |
| 436 | Asm->EmitEncodingByte(TTypeEncoding, "@TType"); |
Bill Wendling | 243ac56 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 437 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 438 | MCSymbol *TTBaseLabel = nullptr; |
Bill Wendling | 24c74f1 | 2010-02-25 23:52:44 +0000 | [diff] [blame] | 439 | if (HaveTTData) { |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 440 | // N.B.: There is a dependency loop between the size of the TTBase uleb128 |
| 441 | // here and the amount of padding before the aligned type table. The |
| 442 | // assembler must sometimes pad this uleb128 or insert extra padding before |
| 443 | // the type table. See PR35809 or GNU as bug 4029. |
| 444 | MCSymbol *TTBaseRefLabel = Asm->createTempSymbol("ttbaseref"); |
| 445 | TTBaseLabel = Asm->createTempSymbol("ttbase"); |
| 446 | Asm->EmitLabelDifferenceAsULEB128(TTBaseLabel, TTBaseRefLabel); |
| 447 | Asm->OutStreamer->EmitLabel(TTBaseRefLabel); |
Bill Wendling | 24c74f1 | 2010-02-25 23:52:44 +0000 | [diff] [blame] | 448 | } |
Bill Wendling | 15349f8 | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 449 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 450 | bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 451 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 452 | // Emit the landing pad call site table. |
| 453 | MCSymbol *CstBeginLabel = Asm->createTempSymbol("cst_begin"); |
| 454 | MCSymbol *CstEndLabel = Asm->createTempSymbol("cst_end"); |
Rafael Espindola | c052fa0 | 2018-02-09 17:13:37 +0000 | [diff] [blame] | 455 | Asm->EmitEncodingByte(CallSiteEncoding, "Call site"); |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 456 | Asm->EmitLabelDifferenceAsULEB128(CstEndLabel, CstBeginLabel); |
| 457 | Asm->OutStreamer->EmitLabel(CstBeginLabel); |
| 458 | |
Bill Wendling | 023ed64 | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 459 | // SjLj Exception handling |
Bill Wendling | a482ec8 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 460 | if (IsSJLJ) { |
Jim Grosbach | 486be66 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 461 | unsigned idx = 0; |
| 462 | for (SmallVectorImpl<CallSiteEntry>::const_iterator |
| 463 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { |
| 464 | const CallSiteEntry &S = *I; |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 465 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 466 | // Index of the call site entry. |
Bill Wendling | ddec683 | 2011-06-21 22:40:24 +0000 | [diff] [blame] | 467 | if (VerboseAsm) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 468 | Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<"); |
| 469 | Asm->OutStreamer->AddComment(" On exception at call site "+Twine(idx)); |
Bill Wendling | ddec683 | 2011-06-21 22:40:24 +0000 | [diff] [blame] | 470 | } |
Bill Wendling | ddec683 | 2011-06-21 22:40:24 +0000 | [diff] [blame] | 471 | Asm->EmitULEB128(idx); |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 472 | |
| 473 | // Offset of the first associated action record, relative to the start of |
| 474 | // the action table. This value is biased by 1 (1 indicates the start of |
| 475 | // the action table), and 0 indicates that there are no actions. |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 476 | if (VerboseAsm) { |
| 477 | if (S.Action == 0) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 478 | Asm->OutStreamer->AddComment(" Action: cleanup"); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 479 | else |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 480 | Asm->OutStreamer->AddComment(" Action: " + |
| 481 | Twine((S.Action - 1) / 2 + 1)); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 482 | } |
Bill Wendling | ddec683 | 2011-06-21 22:40:24 +0000 | [diff] [blame] | 483 | Asm->EmitULEB128(S.Action); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 484 | } |
Jim Grosbach | 693e36a | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 485 | } else { |
Reid Kleckner | 93acac6 | 2014-12-19 22:19:48 +0000 | [diff] [blame] | 486 | // Itanium LSDA exception handling |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 487 | |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 488 | // The call-site table is a list of all call sites that may throw an |
| 489 | // exception (including C++ 'throw' statements) in the procedure |
| 490 | // fragment. It immediately follows the LSDA header. Each entry indicates, |
| 491 | // for a given call, the first corresponding action record and corresponding |
| 492 | // landing pad. |
| 493 | // |
| 494 | // The table begins with the number of bytes, stored as an LEB128 |
| 495 | // compressed, unsigned integer. The records immediately follow the record |
| 496 | // count. They are sorted in increasing call-site address. Each record |
| 497 | // indicates: |
| 498 | // |
| 499 | // * The position of the call-site. |
| 500 | // * The position of the landing pad. |
| 501 | // * The first action record for that call site. |
| 502 | // |
| 503 | // A missing entry in the call-site table indicates that a call is not |
Bill Wendling | 023ed64 | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 504 | // supposed to throw. |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 505 | |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 506 | unsigned Entry = 0; |
Jim Grosbach | 693e36a | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 507 | for (SmallVectorImpl<CallSiteEntry>::const_iterator |
| 508 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { |
| 509 | const CallSiteEntry &S = *I; |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 510 | |
Rafael Espindola | 629cdba | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 511 | MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin(); |
Anton Korobeynikov | 61d167e | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 34adc8d | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 513 | MCSymbol *BeginLabel = S.BeginLabel; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 514 | if (!BeginLabel) |
Chris Lattner | 34adc8d | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 515 | BeginLabel = EHFuncBeginSym; |
| 516 | MCSymbol *EndLabel = S.EndLabel; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 517 | if (!EndLabel) |
Rafael Espindola | 629cdba | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 518 | EndLabel = Asm->getFunctionEnd(); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 519 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 520 | // Offset of the call site relative to the start of the procedure. |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 521 | if (VerboseAsm) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 522 | Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + " <<"); |
Rafael Espindola | c052fa0 | 2018-02-09 17:13:37 +0000 | [diff] [blame] | 523 | Asm->EmitLabelDifferenceAsULEB128(BeginLabel, EHFuncBeginSym); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 524 | if (VerboseAsm) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 525 | Asm->OutStreamer->AddComment(Twine(" Call between ") + |
| 526 | BeginLabel->getName() + " and " + |
| 527 | EndLabel->getName()); |
Rafael Espindola | c052fa0 | 2018-02-09 17:13:37 +0000 | [diff] [blame] | 528 | Asm->EmitLabelDifferenceAsULEB128(EndLabel, BeginLabel); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 529 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 530 | // Offset of the landing pad relative to the start of the procedure. |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 531 | if (!S.LPad) { |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 532 | if (VerboseAsm) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 533 | Asm->OutStreamer->AddComment(" has no landing pad"); |
Rafael Espindola | c052fa0 | 2018-02-09 17:13:37 +0000 | [diff] [blame] | 534 | Asm->EmitULEB128(0); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 535 | } else { |
| 536 | if (VerboseAsm) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 537 | Asm->OutStreamer->AddComment(Twine(" jumps to ") + |
| 538 | S.LPad->LandingPadLabel->getName()); |
Rafael Espindola | c052fa0 | 2018-02-09 17:13:37 +0000 | [diff] [blame] | 539 | Asm->EmitLabelDifferenceAsULEB128(S.LPad->LandingPadLabel, |
| 540 | EHFuncBeginSym); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 541 | } |
Jim Grosbach | 693e36a | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 542 | |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 543 | // Offset of the first associated action record, relative to the start of |
| 544 | // the action table. This value is biased by 1 (1 indicates the start of |
| 545 | // the action table), and 0 indicates that there are no actions. |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 546 | if (VerboseAsm) { |
| 547 | if (S.Action == 0) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 548 | Asm->OutStreamer->AddComment(" On action: cleanup"); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 549 | else |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 550 | Asm->OutStreamer->AddComment(" On action: " + |
| 551 | Twine((S.Action - 1) / 2 + 1)); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 552 | } |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 553 | Asm->EmitULEB128(S.Action); |
Jim Grosbach | 693e36a | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 554 | } |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 555 | } |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 556 | Asm->OutStreamer->EmitLabel(CstEndLabel); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 557 | |
Bill Wendling | 0e749fe | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 558 | // Emit the Action Table. |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 559 | int Entry = 0; |
Bill Wendling | 23b177e | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 560 | for (SmallVectorImpl<ActionEntry>::const_iterator |
| 561 | I = Actions.begin(), E = Actions.end(); I != E; ++I) { |
| 562 | const ActionEntry &Action = *I; |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 563 | |
| 564 | if (VerboseAsm) { |
| 565 | // Emit comments that decode the action table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 566 | Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<"); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | // Type Filter |
| 570 | // |
| 571 | // Used by the runtime to match the type of the thrown exception to the |
| 572 | // type of the catch clauses or the types in the exception specification. |
| 573 | if (VerboseAsm) { |
Duncan Sands | 2e67937 | 2011-09-28 09:13:02 +0000 | [diff] [blame] | 574 | if (Action.ValueForTypeID > 0) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 575 | Asm->OutStreamer->AddComment(" Catch TypeInfo " + |
| 576 | Twine(Action.ValueForTypeID)); |
Duncan Sands | 2e67937 | 2011-09-28 09:13:02 +0000 | [diff] [blame] | 577 | else if (Action.ValueForTypeID < 0) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 578 | Asm->OutStreamer->AddComment(" Filter TypeInfo " + |
| 579 | Twine(Action.ValueForTypeID)); |
Duncan Sands | 2e67937 | 2011-09-28 09:13:02 +0000 | [diff] [blame] | 580 | else |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 581 | Asm->OutStreamer->AddComment(" Cleanup"); |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 582 | } |
| 583 | Asm->EmitSLEB128(Action.ValueForTypeID); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 584 | |
Renato Golin | c8d4065 | 2011-08-18 23:43:14 +0000 | [diff] [blame] | 585 | // Action Record |
| 586 | // |
| 587 | // Self-relative signed displacement in bytes of the next action record, |
| 588 | // or 0 if there is no next action record. |
| 589 | if (VerboseAsm) { |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 590 | if (Action.NextAction == 0) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 591 | Asm->OutStreamer->AddComment(" No further actions"); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 592 | } else { |
| 593 | unsigned NextAction = Entry + (Action.NextAction + 1) / 2; |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 594 | Asm->OutStreamer->AddComment(" Continue to action "+Twine(NextAction)); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 595 | } |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 596 | } |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 597 | Asm->EmitSLEB128(Action.NextAction); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 600 | if (HaveTTData) { |
| 601 | Asm->EmitAlignment(2); |
| 602 | emitTypeInfos(TTypeEncoding, TTBaseLabel); |
| 603 | } |
Anton Korobeynikov | f65a638 | 2012-11-19 21:06:26 +0000 | [diff] [blame] | 604 | |
| 605 | Asm->EmitAlignment(2); |
| 606 | } |
| 607 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 608 | void EHStreamer::emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) { |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 609 | const MachineFunction *MF = Asm->MF; |
| 610 | const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); |
| 611 | const std::vector<unsigned> &FilterIds = MF->getFilterIds(); |
Anton Korobeynikov | f65a638 | 2012-11-19 21:06:26 +0000 | [diff] [blame] | 612 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 613 | bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); |
Anton Korobeynikov | f65a638 | 2012-11-19 21:06:26 +0000 | [diff] [blame] | 614 | |
| 615 | int Entry = 0; |
Bill Wendling | 4f0b8d7 | 2009-10-22 20:48:59 +0000 | [diff] [blame] | 616 | // Emit the Catch TypeInfos. |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 617 | if (VerboseAsm && !TypeInfos.empty()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 618 | Asm->OutStreamer->AddComment(">> Catch TypeInfos <<"); |
| 619 | Asm->OutStreamer->AddBlankLine(); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 620 | Entry = TypeInfos.size(); |
Chris Lattner | 566cae9 | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 621 | } |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 622 | |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 623 | for (const GlobalValue *GV : make_range(TypeInfos.rbegin(), |
| 624 | TypeInfos.rend())) { |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 625 | if (VerboseAsm) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 626 | Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--)); |
Anton Korobeynikov | 097b0e9 | 2012-11-19 21:17:20 +0000 | [diff] [blame] | 627 | Asm->EmitTTypeReference(GV, TTypeEncoding); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Rafael Espindola | d09b416 | 2018-02-09 17:00:25 +0000 | [diff] [blame] | 630 | Asm->OutStreamer->EmitLabel(TTBaseLabel); |
| 631 | |
Bill Wendling | 4f0b8d7 | 2009-10-22 20:48:59 +0000 | [diff] [blame] | 632 | // Emit the Exception Specifications. |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 633 | if (VerboseAsm && !FilterIds.empty()) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 634 | Asm->OutStreamer->AddComment(">> Filter TypeInfos <<"); |
| 635 | Asm->OutStreamer->AddBlankLine(); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 636 | Entry = 0; |
Chris Lattner | 566cae9 | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 637 | } |
Bill Wendling | 23b177e | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 638 | for (std::vector<unsigned>::const_iterator |
| 639 | I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { |
| 640 | unsigned TypeID = *I; |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 641 | if (VerboseAsm) { |
| 642 | --Entry; |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 643 | if (isFilterEHSelector(TypeID)) |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 644 | Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry)); |
Bill Wendling | a8339eb | 2011-06-21 22:30:20 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | Asm->EmitULEB128(TypeID); |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 648 | } |
Bill Wendling | d64cd2b | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 649 | } |