Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 1 | //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===// |
| 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 | // |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 10 | // This file contains support for writing DWARF exception info into asm files. |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "DwarfException.h" |
| 15 | #include "llvm/Module.h" |
Chris Lattner | 6d73378 | 2010-04-05 05:28:23 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/AsmPrinter.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
David Greene | fc4da0c | 2009-08-19 21:55:33 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineLocation.h" |
Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
| 22 | #include "llvm/MC/MCContext.h" |
| 23 | #include "llvm/MC/MCExpr.h" |
Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSection.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | 7a2ba94 | 2010-01-16 18:37:32 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame] | 27 | #include "llvm/Target/Mangler.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetFrameLowering.h" |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chris Lattner | 9d1c1ad | 2010-04-04 18:06:11 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Dwarf.h" |
Chris Lattner | 0ad9c91 | 2010-01-22 22:09:00 +0000 | [diff] [blame] | 35 | #include "llvm/Support/FormattedStream.h" |
Jim Grosbach | c40d9f9 | 2009-09-01 18:49:12 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallString.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Bill Wendling | 1f8075d | 2010-02-09 22:49:16 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/Twine.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chris Lattner | 75f5072 | 2010-04-04 07:48:20 +0000 | [diff] [blame] | 41 | DwarfException::DwarfException(AsmPrinter *A) |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 42 | : Asm(A), MMI(Asm->MMI) {} |
Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 43 | |
Bill Wendling | 5f017e8 | 2010-04-07 09:28:04 +0000 | [diff] [blame] | 44 | DwarfException::~DwarfException() {} |
Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 45 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 46 | /// SharedTypeIds - How many leading type ids two landing pads have in common. |
| 47 | unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L, |
| 48 | const LandingPadInfo *R) { |
| 49 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 50 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 51 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 52 | unsigned Count = 0; |
| 53 | |
| 54 | for (; Count != MinSize; ++Count) |
| 55 | if (LIds[Count] != RIds[Count]) |
| 56 | return Count; |
| 57 | |
| 58 | return Count; |
| 59 | } |
| 60 | |
| 61 | /// PadLT - Order landing pads lexicographically by type id. |
| 62 | bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { |
| 63 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 64 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 65 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 66 | |
| 67 | for (unsigned i = 0; i != MinSize; ++i) |
| 68 | if (LIds[i] != RIds[i]) |
| 69 | return LIds[i] < RIds[i]; |
| 70 | |
| 71 | return LSize < RSize; |
| 72 | } |
| 73 | |
Bill Wendling | d460962 | 2009-07-28 23:23:00 +0000 | [diff] [blame] | 74 | /// ComputeActionsTable - Compute the actions table and gather the first action |
| 75 | /// index for each landing pad site. |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 76 | unsigned DwarfException:: |
| 77 | ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, |
| 78 | SmallVectorImpl<ActionEntry> &Actions, |
| 79 | SmallVectorImpl<unsigned> &FirstActions) { |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 80 | |
| 81 | // The action table follows the call-site table in the LSDA. The individual |
| 82 | // records are of two types: |
| 83 | // |
| 84 | // * Catch clause |
| 85 | // * Exception specification |
| 86 | // |
| 87 | // The two record kinds have the same format, with only small differences. |
| 88 | // They are distinguished by the "switch value" field: Catch clauses |
| 89 | // (TypeInfos) have strictly positive switch values, and exception |
| 90 | // specifications (FilterIds) have strictly negative switch values. Value 0 |
| 91 | // indicates a catch-all clause. |
| 92 | // |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 93 | // Negative type IDs index into FilterIds. Positive type IDs index into |
| 94 | // TypeInfos. The value written for a positive type ID is just the type ID |
| 95 | // itself. For a negative type ID, however, the value written is the |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 96 | // (negative) byte offset of the corresponding FilterIds entry. The byte |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 97 | // offset is usually equal to the type ID (because the FilterIds entries are |
| 98 | // written using a variable width encoding, which outputs one byte per entry |
| 99 | // as long as the value written is not too large) but can differ. This kind |
| 100 | // of complication does not occur for positive type IDs because type infos are |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 101 | // output using a fixed width encoding. FilterOffsets[i] holds the byte |
| 102 | // offset corresponding to FilterIds[i]. |
Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 103 | |
| 104 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 105 | SmallVector<int, 16> FilterOffsets; |
| 106 | FilterOffsets.reserve(FilterIds.size()); |
| 107 | int Offset = -1; |
Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 108 | |
| 109 | for (std::vector<unsigned>::const_iterator |
| 110 | I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 111 | FilterOffsets.push_back(Offset); |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 112 | Offset -= MCAsmInfo::getULEB128Size(*I); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 115 | FirstActions.reserve(LandingPads.size()); |
| 116 | |
| 117 | int FirstAction = 0; |
| 118 | unsigned SizeActions = 0; |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 119 | const LandingPadInfo *PrevLPI = 0; |
Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 120 | |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 121 | for (SmallVectorImpl<const LandingPadInfo *>::const_iterator |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 122 | I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { |
| 123 | const LandingPadInfo *LPI = *I; |
| 124 | const std::vector<int> &TypeIds = LPI->TypeIds; |
Chris Lattner | 9be4913 | 2010-04-04 20:10:41 +0000 | [diff] [blame] | 125 | unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 126 | unsigned SizeSiteActions = 0; |
| 127 | |
| 128 | if (NumShared < TypeIds.size()) { |
| 129 | unsigned SizeAction = 0; |
Bill Wendling | 0a9abcb | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 130 | unsigned PrevAction = (unsigned)-1; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 131 | |
| 132 | if (NumShared) { |
Chris Lattner | 9be4913 | 2010-04-04 20:10:41 +0000 | [diff] [blame] | 133 | unsigned SizePrevIds = PrevLPI->TypeIds.size(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 134 | assert(Actions.size()); |
Bill Wendling | 0a9abcb | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 135 | PrevAction = Actions.size() - 1; |
| 136 | SizeAction = |
| 137 | MCAsmInfo::getSLEB128Size(Actions[PrevAction].NextAction) + |
| 138 | MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 139 | |
| 140 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { |
Bill Wendling | 0a9abcb | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 141 | assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 142 | SizeAction -= |
Bill Wendling | 0a9abcb | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 143 | MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID); |
| 144 | SizeAction += -Actions[PrevAction].NextAction; |
| 145 | PrevAction = Actions[PrevAction].Previous; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | // Compute the actions. |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 150 | for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { |
| 151 | int TypeID = TypeIds[J]; |
| 152 | assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 153 | int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 154 | unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 155 | |
| 156 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 157 | SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 158 | SizeSiteActions += SizeAction; |
| 159 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 160 | ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 161 | Actions.push_back(Action); |
Bill Wendling | 0a9abcb | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 162 | PrevAction = Actions.size() - 1; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Record the first action of the landing pad site. |
| 166 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; |
| 167 | } // else identical - re-use previous FirstAction |
| 168 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 169 | // Information used when created the call-site table. The action record |
| 170 | // field of the call site record is the offset of the first associated |
| 171 | // action record, relative to the start of the actions table. This value is |
Bill Wendling | 0a9abcb | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 172 | // biased by 1 (1 indicating the start of the actions table), and 0 |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 173 | // indicates that there are no actions. |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 174 | FirstActions.push_back(FirstAction); |
| 175 | |
| 176 | // Compute this sites contribution to size. |
| 177 | SizeActions += SizeSiteActions; |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 178 | |
| 179 | PrevLPI = LPI; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 182 | return SizeActions; |
| 183 | } |
| 184 | |
Bill Wendling | ed060dc | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 185 | /// CallToNoUnwindFunction - Return `true' if this is a call to a function |
| 186 | /// marked `nounwind'. Return `false' otherwise. |
| 187 | bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) { |
| 188 | assert(MI->getDesc().isCall() && "This should be a call instruction!"); |
| 189 | |
| 190 | bool MarkedNoUnwind = false; |
| 191 | bool SawFunc = false; |
| 192 | |
| 193 | for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { |
| 194 | const MachineOperand &MO = MI->getOperand(I); |
| 195 | |
Chris Lattner | cfd3188 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 196 | if (!MO.isGlobal()) continue; |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 197 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 198 | const Function *F = dyn_cast<Function>(MO.getGlobal()); |
Chris Lattner | cfd3188 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 199 | if (F == 0) continue; |
Bill Wendling | ecc260e | 2009-11-12 23:13:08 +0000 | [diff] [blame] | 200 | |
Chris Lattner | cfd3188 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 201 | if (SawFunc) { |
| 202 | // Be conservative. If we have more than one function operand for this |
| 203 | // call, then we can't make the assumption that it's the callee and |
| 204 | // not a parameter to the call. |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 205 | // |
Chris Lattner | cfd3188 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 206 | // FIXME: Determine if there's a way to say that `F' is the callee or |
| 207 | // parameter. |
| 208 | MarkedNoUnwind = false; |
| 209 | break; |
Bill Wendling | ed060dc | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | cfd3188 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 211 | |
| 212 | MarkedNoUnwind = F->doesNotThrow(); |
| 213 | SawFunc = true; |
Bill Wendling | ed060dc | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return MarkedNoUnwind; |
| 217 | } |
| 218 | |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 219 | /// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 220 | /// has a try-range containing the call, a non-zero landing pad, and an |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 221 | /// appropriate action. The entry for an ordinary call has a try-range |
| 222 | /// containing the call and zero for the landing pad and the action. Calls |
| 223 | /// marked 'nounwind' have no entry and must not be contained in the try-range |
| 224 | /// of any entry - they form gaps in the table. Entries must be ordered by |
| 225 | /// try-range address. |
| 226 | void DwarfException:: |
| 227 | ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, |
| 228 | const RangeMapType &PadMap, |
| 229 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, |
| 230 | const SmallVectorImpl<unsigned> &FirstActions) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 231 | // The end label of the previous invoke or nounwind try-range. |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 232 | MCSymbol *LastLabel = 0; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 233 | |
| 234 | // Whether there is a potentially throwing instruction (currently this means |
| 235 | // an ordinary call) between the end of the previous try-range and now. |
| 236 | bool SawPotentiallyThrowing = false; |
| 237 | |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 238 | // Whether the last CallSite entry was for an invoke. |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 239 | bool PreviousIsInvoke = false; |
| 240 | |
| 241 | // Visit all instructions in order of address. |
Chris Lattner | 84ac8b7 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 242 | for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 243 | I != E; ++I) { |
| 244 | for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); |
| 245 | MI != E; ++MI) { |
| 246 | if (!MI->isLabel()) { |
Bill Wendling | ed060dc | 2009-11-12 21:59:20 +0000 | [diff] [blame] | 247 | if (MI->getDesc().isCall()) |
| 248 | SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 249 | continue; |
| 250 | } |
| 251 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 252 | // End of the previous try-range? |
Chris Lattner | 0397ada | 2010-03-14 08:17:53 +0000 | [diff] [blame] | 253 | MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 254 | if (BeginLabel == LastLabel) |
| 255 | SawPotentiallyThrowing = false; |
| 256 | |
| 257 | // Beginning of a new try-range? |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 258 | RangeMapType::const_iterator L = PadMap.find(BeginLabel); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 259 | if (L == PadMap.end()) |
| 260 | // Nope, it was just some random label. |
| 261 | continue; |
| 262 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 263 | const PadRange &P = L->second; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 264 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 265 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && |
| 266 | "Inconsistent landing pad map!"); |
| 267 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 268 | // For Dwarf exception handling (SjLj handling doesn't use this). If some |
| 269 | // instruction between the previous try-range and this one may throw, |
| 270 | // create a call-site entry with no landing pad for the region between the |
| 271 | // try-ranges. |
Anton Korobeynikov | 3965b5e | 2011-01-14 21:58:08 +0000 | [diff] [blame^] | 272 | if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) { |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 273 | CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 274 | CallSites.push_back(Site); |
| 275 | PreviousIsInvoke = false; |
| 276 | } |
| 277 | |
| 278 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; |
| 279 | assert(BeginLabel && LastLabel && "Invalid landing pad!"); |
| 280 | |
Chris Lattner | cfd3188 | 2010-03-31 06:09:04 +0000 | [diff] [blame] | 281 | if (!LandingPad->LandingPadLabel) { |
| 282 | // Create a gap. |
| 283 | PreviousIsInvoke = false; |
| 284 | } else { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 285 | // This try-range is for an invoke. |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 286 | CallSiteEntry Site = { |
| 287 | BeginLabel, |
| 288 | LastLabel, |
| 289 | LandingPad->LandingPadLabel, |
| 290 | FirstActions[P.PadIndex] |
| 291 | }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 292 | |
Jim Grosbach | 33668c0 | 2009-09-01 17:19:13 +0000 | [diff] [blame] | 293 | // Try to merge with the previous call-site. SJLJ doesn't do this |
Anton Korobeynikov | 3965b5e | 2011-01-14 21:58:08 +0000 | [diff] [blame^] | 294 | if (PreviousIsInvoke && Asm->MAI->isExceptionHandlingDwarf()) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 295 | CallSiteEntry &Prev = CallSites.back(); |
| 296 | if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { |
| 297 | // Extend the range of the previous entry. |
| 298 | Prev.EndLabel = Site.EndLabel; |
| 299 | continue; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Otherwise, create a new call-site. |
Anton Korobeynikov | 3965b5e | 2011-01-14 21:58:08 +0000 | [diff] [blame^] | 304 | if (Asm->MAI->isExceptionHandlingDwarf()) |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 305 | CallSites.push_back(Site); |
| 306 | else { |
| 307 | // SjLj EH must maintain the call sites in the order assigned |
| 308 | // to them by the SjLjPrepare pass. |
| 309 | unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel); |
| 310 | if (CallSites.size() < SiteNo) |
| 311 | CallSites.resize(SiteNo); |
| 312 | CallSites[SiteNo - 1] = Site; |
| 313 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 314 | PreviousIsInvoke = true; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // If some instruction between the previous try-range and the end of the |
| 320 | // function may throw, create a call-site entry with no landing pad for the |
| 321 | // region following the try-range. |
Anton Korobeynikov | 3965b5e | 2011-01-14 21:58:08 +0000 | [diff] [blame^] | 322 | if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) { |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 323 | CallSiteEntry Site = { LastLabel, 0, 0, 0 }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 324 | CallSites.push_back(Site); |
| 325 | } |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 328 | /// EmitExceptionTable - Emit landing pads and actions. |
| 329 | /// |
| 330 | /// The general organization of the table is complex, but the basic concepts are |
| 331 | /// easy. First there is a header which describes the location and organization |
| 332 | /// of the three components that follow. |
Eric Christopher | dbfcdb9 | 2009-08-28 22:33:43 +0000 | [diff] [blame] | 333 | /// |
Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 334 | /// 1. The landing pad site information describes the range of code covered by |
| 335 | /// the try. In our case it's an accumulation of the ranges covered by the |
| 336 | /// invokes in the try. There is also a reference to the landing pad that |
| 337 | /// handles the exception once processed. Finally an index into the actions |
| 338 | /// table. |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 339 | /// 2. The action table, in our case, is composed of pairs of type IDs and next |
Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 340 | /// action offset. Starting with the action index from the landing pad |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 341 | /// site, each type ID is checked for a match to the current exception. If |
Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 342 | /// it matches then the exception and type id are passed on to the landing |
| 343 | /// pad. Otherwise the next action is looked up. This chain is terminated |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 344 | /// with a next action of zero. If no type id is found then the frame is |
Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 345 | /// unwound and handling continues. |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 346 | /// 3. Type ID table contains references to all the C++ typeinfo for all |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 347 | /// catches in the function. This tables is reverse indexed base 1. |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 348 | void DwarfException::EmitExceptionTable() { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 349 | const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 350 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
| 351 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
Bill Wendling | ade025c | 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 | |
| 361 | std::sort(LandingPads.begin(), LandingPads.end(), PadLT); |
| 362 | |
| 363 | // Compute the actions table and gather the first action index for each |
| 364 | // landing pad site. |
| 365 | SmallVector<ActionEntry, 32> Actions; |
| 366 | SmallVector<unsigned, 64> FirstActions; |
Bill Wendling | 0a9abcb | 2010-02-10 21:41:57 +0000 | [diff] [blame] | 367 | unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions); |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 368 | |
| 369 | // Invokes and nounwind calls have entries in PadMap (due to being bracketed |
| 370 | // by try-range labels when lowered). Ordinary calls do not, so appropriate |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 371 | // try-ranges for them need be deduced when using DWARF exception handling. |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 372 | RangeMapType PadMap; |
| 373 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 374 | const LandingPadInfo *LandingPad = LandingPads[i]; |
| 375 | for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 376 | MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 377 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); |
| 378 | PadRange P = { i, j }; |
| 379 | PadMap[BeginLabel] = P; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Compute the call-site table. |
| 384 | SmallVector<CallSiteEntry, 64> CallSites; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 385 | ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 386 | |
| 387 | // Final tallies. |
| 388 | |
| 389 | // Call sites. |
Chris Lattner | 84ac8b7 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 390 | bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; |
Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 391 | bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true; |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 392 | |
Bill Wendling | 3dc9b48 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 393 | unsigned CallSiteTableLength; |
Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 394 | if (IsSJLJ) |
Bill Wendling | 3dc9b48 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 395 | CallSiteTableLength = 0; |
Chris Lattner | 9be4913 | 2010-04-04 20:10:41 +0000 | [diff] [blame] | 396 | else { |
| 397 | unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4 |
| 398 | unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4 |
| 399 | unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4 |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 400 | CallSiteTableLength = |
Chris Lattner | 9be4913 | 2010-04-04 20:10:41 +0000 | [diff] [blame] | 401 | CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize); |
| 402 | } |
Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 403 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 404 | for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { |
Bill Wendling | 3dc9b48 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 405 | CallSiteTableLength += MCAsmInfo::getULEB128Size(CallSites[i].Action); |
Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 406 | if (IsSJLJ) |
Bill Wendling | 3dc9b48 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 407 | CallSiteTableLength += MCAsmInfo::getULEB128Size(i); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 408 | } |
Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 409 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 410 | // Type infos. |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 411 | const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 412 | unsigned TTypeEncoding; |
Bill Wendling | a2f6449 | 2009-09-10 06:27:16 +0000 | [diff] [blame] | 413 | unsigned TypeFormatSize; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 414 | |
Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 415 | if (!HaveTTData) { |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 416 | // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say |
| 417 | // that we're omitting that bit. |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 418 | TTypeEncoding = dwarf::DW_EH_PE_omit; |
Chris Lattner | 84ac8b7 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 419 | // dwarf::DW_EH_PE_absptr |
| 420 | TypeFormatSize = Asm->getTargetData().getPointerSize(); |
Chris Lattner | 81c9a06 | 2009-07-31 22:03:47 +0000 | [diff] [blame] | 421 | } else { |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 422 | // Okay, we have actual filters or typeinfos to emit. As such, we need to |
| 423 | // pick a type encoding for them. We're about to emit a list of pointers to |
| 424 | // typeinfo objects at the end of the LSDA. However, unless we're in static |
| 425 | // mode, this reference will require a relocation by the dynamic linker. |
Chris Lattner | 46b754c | 2009-07-31 22:18:14 +0000 | [diff] [blame] | 426 | // |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 427 | // Because of this, we have a couple of options: |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 428 | // |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 429 | // 1) If we are in -static mode, we can always use an absolute reference |
| 430 | // from the LSDA, because the static linker will resolve it. |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 431 | // |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 432 | // 2) Otherwise, if the LSDA section is writable, we can output the direct |
| 433 | // reference to the typeinfo and allow the dynamic linker to relocate |
| 434 | // it. Since it is in a writable section, the dynamic linker won't |
| 435 | // have a problem. |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 436 | // |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 437 | // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, |
| 438 | // we need to use some form of indirection. For example, on Darwin, |
| 439 | // we can output a statically-relocatable reference to a dyld stub. The |
| 440 | // offset to the stub is constant, but the contents are in a section |
| 441 | // that is updated by the dynamic linker. This is easy enough, but we |
| 442 | // need to tell the personality function of the unwinder to indirect |
| 443 | // through the dyld stub. |
| 444 | // |
Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 445 | // FIXME: When (3) is actually implemented, we'll have to emit the stubs |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 446 | // somewhere. This predicate should be moved to a shared location that is |
| 447 | // in target-independent code. |
| 448 | // |
Anton Korobeynikov | 9184b25 | 2010-02-15 22:35:59 +0000 | [diff] [blame] | 449 | TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 450 | TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding); |
Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 451 | } |
Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 452 | |
Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 453 | // Begin the exception table. |
| 454 | Asm->OutStreamer.SwitchSection(LSDASection); |
Chris Lattner | 059ea13 | 2010-04-28 01:05:45 +0000 | [diff] [blame] | 455 | Asm->EmitAlignment(2); |
Bill Wendling | 43e484f | 2009-09-10 01:12:47 +0000 | [diff] [blame] | 456 | |
Bill Wendling | 3dc9b48 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 457 | // Emit the LSDA. |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 458 | MCSymbol *GCCETSym = |
Chris Lattner | 974c0fb | 2010-03-10 02:48:06 +0000 | [diff] [blame] | 459 | Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+ |
Chris Lattner | 84ac8b7 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 460 | Twine(Asm->getFunctionNumber())); |
Chris Lattner | 974c0fb | 2010-03-10 02:48:06 +0000 | [diff] [blame] | 461 | Asm->OutStreamer.EmitLabel(GCCETSym); |
Chris Lattner | 84ac8b7 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 462 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("exception", |
| 463 | Asm->getFunctionNumber())); |
Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 974c0fb | 2010-03-10 02:48:06 +0000 | [diff] [blame] | 465 | if (IsSJLJ) |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 466 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("_LSDA_", |
| 467 | Asm->getFunctionNumber())); |
Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 468 | |
Bill Wendling | 3dc9b48 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 469 | // Emit the LSDA header. |
Chris Lattner | ca6190b | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 470 | Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); |
| 471 | Asm->EmitEncodingByte(TTypeEncoding, "@TType"); |
Bill Wendling | fe22028 | 2009-09-10 02:07:37 +0000 | [diff] [blame] | 472 | |
Bill Wendling | 3dc9b48 | 2010-02-24 23:34:35 +0000 | [diff] [blame] | 473 | // The type infos need to be aligned. GCC does this by inserting padding just |
| 474 | // before the type infos. However, this changes the size of the exception |
| 475 | // table, so you need to take this into account when you output the exception |
| 476 | // table size. However, the size is output using a variable length encoding. |
| 477 | // So by increasing the size by inserting padding, you may increase the number |
| 478 | // of bytes used for writing the size. If it increases, say by one byte, then |
| 479 | // you now need to output one less byte of padding to get the type infos |
| 480 | // aligned. However this decreases the size of the exception table. This |
| 481 | // changes the value you have to output for the exception table size. Due to |
| 482 | // the variable length encoding, the number of bytes used for writing the |
| 483 | // length may decrease. If so, you then have to increase the amount of |
| 484 | // padding. And so on. If you look carefully at the GCC code you will see that |
| 485 | // it indeed does this in a loop, going on and on until the values stabilize. |
| 486 | // We chose another solution: don't output padding inside the table like GCC |
| 487 | // does, instead output it before the table. |
| 488 | unsigned SizeTypes = TypeInfos.size() * TypeFormatSize; |
| 489 | unsigned CallSiteTableLengthSize = |
| 490 | MCAsmInfo::getULEB128Size(CallSiteTableLength); |
| 491 | unsigned TTypeBaseOffset = |
| 492 | sizeof(int8_t) + // Call site format |
| 493 | CallSiteTableLengthSize + // Call site table length size |
| 494 | CallSiteTableLength + // Call site table length |
| 495 | SizeActions + // Actions size |
| 496 | SizeTypes; |
| 497 | unsigned TTypeBaseOffsetSize = MCAsmInfo::getULEB128Size(TTypeBaseOffset); |
| 498 | unsigned TotalSize = |
| 499 | sizeof(int8_t) + // LPStart format |
| 500 | sizeof(int8_t) + // TType format |
| 501 | (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size |
| 502 | TTypeBaseOffset; // TType base offset |
| 503 | unsigned SizeAlign = (4 - TotalSize) & 3; |
| 504 | |
Bill Wendling | 86f0d33 | 2010-02-25 23:52:44 +0000 | [diff] [blame] | 505 | if (HaveTTData) { |
Bill Wendling | 6507eca | 2010-02-26 21:31:01 +0000 | [diff] [blame] | 506 | // Account for any extra padding that will be added to the call site table |
Bill Wendling | f7e90ae | 2010-02-25 21:19:47 +0000 | [diff] [blame] | 507 | // length. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 508 | Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign); |
Bill Wendling | 1869ac8 | 2010-02-26 22:17:52 +0000 | [diff] [blame] | 509 | SizeAlign = 0; |
Bill Wendling | 86f0d33 | 2010-02-25 23:52:44 +0000 | [diff] [blame] | 510 | } |
Bill Wendling | b0d9c3e | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 511 | |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 512 | // SjLj Exception handling |
Bill Wendling | d1a5b37 | 2009-09-10 00:17:04 +0000 | [diff] [blame] | 513 | if (IsSJLJ) { |
Chris Lattner | ca6190b | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 514 | Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); |
Bill Wendling | 1869ac8 | 2010-02-26 22:17:52 +0000 | [diff] [blame] | 515 | |
| 516 | // Add extra padding if it wasn't added to the TType base offset. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 517 | Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 518 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 519 | // Emit the landing pad site information. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 520 | unsigned idx = 0; |
| 521 | for (SmallVectorImpl<CallSiteEntry>::const_iterator |
| 522 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { |
| 523 | const CallSiteEntry &S = *I; |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 524 | |
| 525 | // Offset of the landing pad, counted in 16-byte bundles relative to the |
| 526 | // @LPStart address. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 527 | Asm->EmitULEB128(idx, "Landing pad"); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 528 | |
| 529 | // Offset of the first associated action record, relative to the start of |
| 530 | // the action table. This value is biased by 1 (1 indicates the start of |
| 531 | // the action table), and 0 indicates that there are no actions. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 532 | Asm->EmitULEB128(S.Action, "Action"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 533 | } |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 534 | } else { |
| 535 | // DWARF Exception handling |
Anton Korobeynikov | 3965b5e | 2011-01-14 21:58:08 +0000 | [diff] [blame^] | 536 | assert(Asm->MAI->isExceptionHandlingDwarf()); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 537 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 538 | // The call-site table is a list of all call sites that may throw an |
| 539 | // exception (including C++ 'throw' statements) in the procedure |
| 540 | // fragment. It immediately follows the LSDA header. Each entry indicates, |
| 541 | // for a given call, the first corresponding action record and corresponding |
| 542 | // landing pad. |
| 543 | // |
| 544 | // The table begins with the number of bytes, stored as an LEB128 |
| 545 | // compressed, unsigned integer. The records immediately follow the record |
| 546 | // count. They are sorted in increasing call-site address. Each record |
| 547 | // indicates: |
| 548 | // |
| 549 | // * The position of the call-site. |
| 550 | // * The position of the landing pad. |
| 551 | // * The first action record for that call site. |
| 552 | // |
| 553 | // A missing entry in the call-site table indicates that a call is not |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 554 | // supposed to throw. |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 555 | |
| 556 | // Emit the landing pad call site table. |
Chris Lattner | ca6190b | 2010-04-04 20:04:21 +0000 | [diff] [blame] | 557 | Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); |
Bill Wendling | 1869ac8 | 2010-02-26 22:17:52 +0000 | [diff] [blame] | 558 | |
| 559 | // Add extra padding if it wasn't added to the TType base offset. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 560 | Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 561 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 562 | for (SmallVectorImpl<CallSiteEntry>::const_iterator |
| 563 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { |
| 564 | const CallSiteEntry &S = *I; |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 565 | |
Chris Lattner | c021572 | 2010-04-04 19:25:43 +0000 | [diff] [blame] | 566 | MCSymbol *EHFuncBeginSym = |
Chris Lattner | 84ac8b7 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 567 | Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber()); |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 569 | MCSymbol *BeginLabel = S.BeginLabel; |
| 570 | if (BeginLabel == 0) |
| 571 | BeginLabel = EHFuncBeginSym; |
| 572 | MCSymbol *EndLabel = S.EndLabel; |
| 573 | if (EndLabel == 0) |
Chris Lattner | 84ac8b7 | 2010-04-05 00:26:50 +0000 | [diff] [blame] | 574 | EndLabel = Asm->GetTempSymbol("eh_func_end", Asm->getFunctionNumber()); |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 575 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 576 | // Offset of the call site relative to the previous call site, counted in |
| 577 | // number of 16-byte bundles. The first call site is counted relative to |
| 578 | // the start of the procedure fragment. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 579 | Asm->OutStreamer.AddComment("Region start"); |
Chris Lattner | f88dce1 | 2010-04-04 21:31:54 +0000 | [diff] [blame] | 580 | Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4); |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 582 | Asm->OutStreamer.AddComment("Region length"); |
Chris Lattner | a643718 | 2010-04-04 19:58:12 +0000 | [diff] [blame] | 583 | Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 584 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 585 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 586 | // Offset of the landing pad, counted in 16-byte bundles relative to the |
| 587 | // @LPStart address. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 588 | Asm->OutStreamer.AddComment("Landing pad"); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 589 | if (!S.PadLabel) |
Chris Lattner | bcb83e5 | 2010-01-20 07:41:15 +0000 | [diff] [blame] | 590 | Asm->OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 591 | else |
Chris Lattner | f88dce1 | 2010-04-04 21:31:54 +0000 | [diff] [blame] | 592 | Asm->EmitLabelDifference(S.PadLabel, EHFuncBeginSym, 4); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 593 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 594 | // Offset of the first associated action record, relative to the start of |
| 595 | // the action table. This value is biased by 1 (1 indicates the start of |
| 596 | // the action table), and 0 indicates that there are no actions. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 597 | Asm->EmitULEB128(S.Action, "Action"); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 598 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 601 | // Emit the Action Table. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 602 | if (Actions.size() != 0) { |
| 603 | Asm->OutStreamer.AddComment("-- Action Record Table --"); |
| 604 | Asm->OutStreamer.AddBlankLine(); |
| 605 | } |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 606 | |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 607 | for (SmallVectorImpl<ActionEntry>::const_iterator |
| 608 | I = Actions.begin(), E = Actions.end(); I != E; ++I) { |
| 609 | const ActionEntry &Action = *I; |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 610 | Asm->OutStreamer.AddComment("Action Record"); |
| 611 | Asm->OutStreamer.AddBlankLine(); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 612 | |
| 613 | // Type Filter |
| 614 | // |
| 615 | // Used by the runtime to match the type of the thrown exception to the |
| 616 | // type of the catch clauses or the types in the exception specification. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 617 | Asm->EmitSLEB128(Action.ValueForTypeID, " TypeInfo index"); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 618 | |
| 619 | // Action Record |
| 620 | // |
| 621 | // Self-relative signed displacement in bytes of the next action record, |
| 622 | // or 0 if there is no next action record. |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 623 | Asm->EmitSLEB128(Action.NextAction, " Next action"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Bill Wendling | 48dc29e | 2009-10-22 20:48:59 +0000 | [diff] [blame] | 626 | // Emit the Catch TypeInfos. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 627 | if (!TypeInfos.empty()) { |
| 628 | Asm->OutStreamer.AddComment("-- Catch TypeInfos --"); |
| 629 | Asm->OutStreamer.AddBlankLine(); |
| 630 | } |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 631 | for (std::vector<const GlobalVariable *>::const_reverse_iterator |
Bill Wendling | 01c6937 | 2009-11-19 00:09:14 +0000 | [diff] [blame] | 632 | I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { |
Bill Wendling | fb7634f | 2009-11-19 19:21:09 +0000 | [diff] [blame] | 633 | const GlobalVariable *GV = *I; |
Bill Wendling | ec04458 | 2009-11-18 23:18:46 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 90e4af7 | 2010-03-10 00:09:21 +0000 | [diff] [blame] | 635 | Asm->OutStreamer.AddComment("TypeInfo"); |
| 636 | if (GV) |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 637 | Asm->EmitReference(GV, TTypeEncoding); |
Chris Lattner | 90e4af7 | 2010-03-10 00:09:21 +0000 | [diff] [blame] | 638 | else |
Chris Lattner | d2af785 | 2010-04-04 20:20:50 +0000 | [diff] [blame] | 639 | Asm->OutStreamer.EmitIntValue(0,Asm->GetSizeOfEncodedValue(TTypeEncoding), |
| 640 | 0); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Bill Wendling | 48dc29e | 2009-10-22 20:48:59 +0000 | [diff] [blame] | 643 | // Emit the Exception Specifications. |
Chris Lattner | 233f52b | 2010-03-09 23:52:58 +0000 | [diff] [blame] | 644 | if (!FilterIds.empty()) { |
| 645 | Asm->OutStreamer.AddComment("-- Filter IDs --"); |
| 646 | Asm->OutStreamer.AddBlankLine(); |
| 647 | } |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 648 | for (std::vector<unsigned>::const_iterator |
| 649 | I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { |
| 650 | unsigned TypeID = *I; |
Chris Lattner | 7e1a8f8 | 2010-04-04 19:09:29 +0000 | [diff] [blame] | 651 | Asm->EmitULEB128(TypeID, TypeID != 0 ? "Exception specification" : 0); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Chris Lattner | 059ea13 | 2010-04-28 01:05:45 +0000 | [diff] [blame] | 654 | Asm->EmitAlignment(2); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 657 | /// EndModule - Emit all exception information that should come after the |
| 658 | /// content. |
| 659 | void DwarfException::EndModule() { |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 660 | assert(0 && "Should be implemented"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Bill Wendling | 28275fd | 2009-09-10 06:50:01 +0000 | [diff] [blame] | 663 | /// BeginFunction - Gather pre-function exception information. Assumes it's |
| 664 | /// being emitted immediately after the function entry point. |
Chris Lattner | eec791a | 2010-01-26 23:18:02 +0000 | [diff] [blame] | 665 | void DwarfException::BeginFunction(const MachineFunction *MF) { |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 666 | assert(0 && "Should be implemented"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /// EndFunction - Gather and emit post-function exception information. |
| 670 | /// |
| 671 | void DwarfException::EndFunction() { |
Anton Korobeynikov | d7e8ddc | 2011-01-14 21:57:45 +0000 | [diff] [blame] | 672 | assert(0 && "Should be implemented"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 673 | } |