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 | // |
| 10 | // This file contains support for writing dwarf exception info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "DwarfException.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
David Greene | fc4da0c | 2009-08-19 21:55:33 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineLocation.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
| 23 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Dwarf.h" |
| 28 | #include "llvm/Support/Timer.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | using namespace llvm; |
| 32 | |
| 33 | static TimerGroup &getDwarfTimerGroup() { |
| 34 | static TimerGroup DwarfTimerGroup("Dwarf Exception"); |
| 35 | return DwarfTimerGroup; |
| 36 | } |
| 37 | |
Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 38 | DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A, |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 39 | const MCAsmInfo *T) |
Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 40 | : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false), |
| 41 | shouldEmitTableModule(false), shouldEmitMovesModule(false), |
| 42 | ExceptionTimer(0) { |
| 43 | if (TimePassesIsEnabled) |
| 44 | ExceptionTimer = new Timer("Dwarf Exception Writer", |
| 45 | getDwarfTimerGroup()); |
| 46 | } |
| 47 | |
| 48 | DwarfException::~DwarfException() { |
| 49 | delete ExceptionTimer; |
| 50 | } |
| 51 | |
Bill Wendling | a6f91da | 2009-08-25 02:27:42 +0000 | [diff] [blame] | 52 | /// EmitCommonInformationEntry - Emit a Common Information Entry (CIE). This |
| 53 | /// holds information that is shared among many Frame Description Entries. |
| 54 | /// There is at least one CIE in every non-empty .debug_frame section. |
| 55 | void DwarfException::EmitCommonInformationEntry(const Function *Personality, |
| 56 | unsigned Index) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 57 | // Size and sign of stack growth. |
| 58 | int stackGrowth = |
| 59 | Asm->TM.getFrameInfo()->getStackGrowthDirection() == |
| 60 | TargetFrameInfo::StackGrowsUp ? |
| 61 | TD->getPointerSize() : -TD->getPointerSize(); |
| 62 | |
| 63 | // Begin eh frame section. |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 64 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection()); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 66 | if (MAI->is_EHSymbolPrivate()) |
| 67 | O << MAI->getPrivateGlobalPrefix(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 68 | |
| 69 | O << "EH_frame" << Index << ":\n"; |
| 70 | EmitLabel("section_eh_frame", Index); |
| 71 | |
| 72 | // Define base labels. |
| 73 | EmitLabel("eh_frame_common", Index); |
| 74 | |
| 75 | // Define the eh frame length. |
| 76 | EmitDifference("eh_frame_common_end", Index, |
| 77 | "eh_frame_common_begin", Index, true); |
| 78 | Asm->EOL("Length of Common Information Entry"); |
| 79 | |
| 80 | // EH frame header. |
| 81 | EmitLabel("eh_frame_common_begin", Index); |
| 82 | Asm->EmitInt32((int)0); |
| 83 | Asm->EOL("CIE Identifier Tag"); |
| 84 | Asm->EmitInt8(dwarf::DW_CIE_VERSION); |
| 85 | Asm->EOL("CIE Version"); |
| 86 | |
| 87 | // The personality presence indicates that language specific information will |
| 88 | // show up in the eh frame. |
| 89 | Asm->EmitString(Personality ? "zPLR" : "zR"); |
| 90 | Asm->EOL("CIE Augmentation"); |
| 91 | |
| 92 | // Round out reader. |
| 93 | Asm->EmitULEB128Bytes(1); |
| 94 | Asm->EOL("CIE Code Alignment Factor"); |
| 95 | Asm->EmitSLEB128Bytes(stackGrowth); |
| 96 | Asm->EOL("CIE Data Alignment Factor"); |
| 97 | Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true)); |
| 98 | Asm->EOL("CIE Return Address Column"); |
| 99 | |
Bill Wendling | 4bda11f | 2009-08-25 02:32:05 +0000 | [diff] [blame^] | 100 | // If there is a personality, we need to indicate the function's location. |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 101 | if (Personality) { |
| 102 | Asm->EmitULEB128Bytes(7); |
| 103 | Asm->EOL("Augmentation Size"); |
| 104 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 105 | if (MAI->getNeedsIndirectEncoding()) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 106 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 | |
| 107 | dwarf::DW_EH_PE_indirect); |
| 108 | Asm->EOL("Personality (pcrel sdata4 indirect)"); |
| 109 | } else { |
| 110 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 111 | Asm->EOL("Personality (pcrel sdata4)"); |
| 112 | } |
| 113 | |
| 114 | PrintRelDirective(true); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 115 | O << MAI->getPersonalityPrefix(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 116 | Asm->EmitExternalGlobal((const GlobalVariable *)(Personality)); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 117 | O << MAI->getPersonalitySuffix(); |
| 118 | if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL")) |
| 119 | O << "-" << MAI->getPCSymbol(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 120 | Asm->EOL("Personality"); |
| 121 | |
| 122 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 123 | Asm->EOL("LSDA Encoding (pcrel sdata4)"); |
| 124 | |
| 125 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 126 | Asm->EOL("FDE Encoding (pcrel sdata4)"); |
| 127 | } else { |
| 128 | Asm->EmitULEB128Bytes(1); |
| 129 | Asm->EOL("Augmentation Size"); |
| 130 | |
| 131 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 132 | Asm->EOL("FDE Encoding (pcrel sdata4)"); |
| 133 | } |
| 134 | |
| 135 | // Indicate locations of general callee saved registers in frame. |
| 136 | std::vector<MachineMove> Moves; |
| 137 | RI->getInitialFrameState(Moves); |
| 138 | EmitFrameMoves(NULL, 0, Moves, true); |
| 139 | |
| 140 | // On Darwin the linker honors the alignment of eh_frame, which means it must |
| 141 | // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get |
| 142 | // holes which confuse readers of eh_frame. |
| 143 | Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, |
| 144 | 0, 0, false); |
| 145 | EmitLabel("eh_frame_common_end", Index); |
| 146 | |
| 147 | Asm->EOL(); |
| 148 | } |
| 149 | |
Bill Wendling | a6f91da | 2009-08-25 02:27:42 +0000 | [diff] [blame] | 150 | /// EmitFrameDescriptionEntry - Emit the Frame Description Entry (FDE) for the |
| 151 | /// function. |
| 152 | void DwarfException:: |
| 153 | EmitFrameDescriptionEntry(const FunctionEHFrameInfo &EHFrameInfo) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 154 | assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() && |
| 155 | "Should not emit 'available externally' functions at all"); |
| 156 | |
Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 157 | const Function *TheFunc = EHFrameInfo.function; |
| 158 | |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 159 | Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection()); |
Chris Lattner | 6a66e58 | 2009-08-18 06:13:03 +0000 | [diff] [blame] | 160 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 161 | // Externally visible entry into the functions eh frame info. If the |
| 162 | // corresponding function is static, this should not be externally visible. |
Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 163 | if (!TheFunc->hasLocalLinkage()) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 164 | if (const char *GlobalEHDirective = MAI->getGlobalEHDirective()) |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 165 | O << GlobalEHDirective << EHFrameInfo.FnName << "\n"; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 166 | |
| 167 | // If corresponding function is weak definition, this should be too. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 168 | if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective()) |
| 169 | O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n"; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 170 | |
| 171 | // If there are no calls then you can't unwind. This may mean we can omit the |
| 172 | // EH Frame, but some environments do not handle weak absolute symbols. If |
| 173 | // UnwindTablesMandatory is set we cannot do this optimization; the unwind |
| 174 | // info is to be available for non-EH uses. |
Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 175 | if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory && |
| 176 | (!TheFunc->isWeakForLinker() || |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 177 | !MAI->getWeakDefDirective() || |
| 178 | MAI->getSupportsWeakOmittedEHFrame())) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 179 | O << EHFrameInfo.FnName << " = 0\n"; |
| 180 | // This name has no connection to the function, so it might get |
| 181 | // dead-stripped when the function is not, erroneously. Prohibit |
| 182 | // dead-stripping unconditionally. |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 183 | if (const char *UsedDirective = MAI->getUsedDirective()) |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 184 | O << UsedDirective << EHFrameInfo.FnName << "\n\n"; |
| 185 | } else { |
| 186 | O << EHFrameInfo.FnName << ":\n"; |
| 187 | |
| 188 | // EH frame header. |
| 189 | EmitDifference("eh_frame_end", EHFrameInfo.Number, |
| 190 | "eh_frame_begin", EHFrameInfo.Number, true); |
| 191 | Asm->EOL("Length of Frame Information Entry"); |
| 192 | |
| 193 | EmitLabel("eh_frame_begin", EHFrameInfo.Number); |
| 194 | |
Chris Lattner | a4ff5e4 | 2009-07-17 20:53:51 +0000 | [diff] [blame] | 195 | EmitSectionOffset("eh_frame_begin", "eh_frame_common", |
| 196 | EHFrameInfo.Number, EHFrameInfo.PersonalityIndex, |
| 197 | true, true, false); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 198 | |
| 199 | Asm->EOL("FDE CIE offset"); |
| 200 | |
| 201 | EmitReference("eh_func_begin", EHFrameInfo.Number, true, true); |
| 202 | Asm->EOL("FDE initial location"); |
| 203 | EmitDifference("eh_func_end", EHFrameInfo.Number, |
| 204 | "eh_func_begin", EHFrameInfo.Number, true); |
| 205 | Asm->EOL("FDE address range"); |
| 206 | |
| 207 | // If there is a personality and landing pads then point to the language |
| 208 | // specific data area in the exception table. |
| 209 | if (EHFrameInfo.PersonalityIndex) { |
| 210 | Asm->EmitULEB128Bytes(4); |
| 211 | Asm->EOL("Augmentation size"); |
| 212 | |
| 213 | if (EHFrameInfo.hasLandingPads) |
| 214 | EmitReference("exception", EHFrameInfo.Number, true, true); |
| 215 | else |
| 216 | Asm->EmitInt32((int)0); |
| 217 | Asm->EOL("Language Specific Data Area"); |
| 218 | } else { |
| 219 | Asm->EmitULEB128Bytes(0); |
| 220 | Asm->EOL("Augmentation size"); |
| 221 | } |
| 222 | |
| 223 | // Indicate locations of function specific callee saved registers in frame. |
| 224 | EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, |
| 225 | true); |
| 226 | |
| 227 | // On Darwin the linker honors the alignment of eh_frame, which means it |
| 228 | // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you |
| 229 | // get holes which confuse readers of eh_frame. |
| 230 | Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, |
| 231 | 0, 0, false); |
| 232 | EmitLabel("eh_frame_end", EHFrameInfo.Number); |
| 233 | |
| 234 | // If the function is marked used, this table should be also. We cannot |
| 235 | // make the mark unconditional in this case, since retaining the table also |
| 236 | // retains the function in this case, and there is code around that depends |
| 237 | // on unused functions (calling undefined externals) being dead-stripped to |
| 238 | // link correctly. Yes, there really is. |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 239 | if (MMI->isUsedFunction(EHFrameInfo.function)) |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 240 | if (const char *UsedDirective = MAI->getUsedDirective()) |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 241 | O << UsedDirective << EHFrameInfo.FnName << "\n\n"; |
| 242 | } |
Bill Wendling | 4bda11f | 2009-08-25 02:32:05 +0000 | [diff] [blame^] | 243 | |
| 244 | Asm->EOL(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 247 | /// SharedTypeIds - How many leading type ids two landing pads have in common. |
| 248 | unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L, |
| 249 | const LandingPadInfo *R) { |
| 250 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 251 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 252 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 253 | unsigned Count = 0; |
| 254 | |
| 255 | for (; Count != MinSize; ++Count) |
| 256 | if (LIds[Count] != RIds[Count]) |
| 257 | return Count; |
| 258 | |
| 259 | return Count; |
| 260 | } |
| 261 | |
| 262 | /// PadLT - Order landing pads lexicographically by type id. |
| 263 | bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { |
| 264 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 265 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 266 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 267 | |
| 268 | for (unsigned i = 0; i != MinSize; ++i) |
| 269 | if (LIds[i] != RIds[i]) |
| 270 | return LIds[i] < RIds[i]; |
| 271 | |
| 272 | return LSize < RSize; |
| 273 | } |
| 274 | |
Bill Wendling | d460962 | 2009-07-28 23:23:00 +0000 | [diff] [blame] | 275 | /// ComputeActionsTable - Compute the actions table and gather the first action |
| 276 | /// index for each landing pad site. |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 277 | unsigned DwarfException:: |
| 278 | ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, |
| 279 | SmallVectorImpl<ActionEntry> &Actions, |
| 280 | SmallVectorImpl<unsigned> &FirstActions) { |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 281 | |
| 282 | // The action table follows the call-site table in the LSDA. The individual |
| 283 | // records are of two types: |
| 284 | // |
| 285 | // * Catch clause |
| 286 | // * Exception specification |
| 287 | // |
| 288 | // The two record kinds have the same format, with only small differences. |
| 289 | // They are distinguished by the "switch value" field: Catch clauses |
| 290 | // (TypeInfos) have strictly positive switch values, and exception |
| 291 | // specifications (FilterIds) have strictly negative switch values. Value 0 |
| 292 | // indicates a catch-all clause. |
| 293 | // |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 294 | // Negative type IDs index into FilterIds. Positive type IDs index into |
| 295 | // TypeInfos. The value written for a positive type ID is just the type ID |
| 296 | // itself. For a negative type ID, however, the value written is the |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 297 | // (negative) byte offset of the corresponding FilterIds entry. The byte |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 298 | // offset is usually equal to the type ID (because the FilterIds entries are |
| 299 | // written using a variable width encoding, which outputs one byte per entry |
| 300 | // as long as the value written is not too large) but can differ. This kind |
| 301 | // 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] | 302 | // output using a fixed width encoding. FilterOffsets[i] holds the byte |
| 303 | // offset corresponding to FilterIds[i]. |
Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 304 | |
| 305 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 306 | SmallVector<int, 16> FilterOffsets; |
| 307 | FilterOffsets.reserve(FilterIds.size()); |
| 308 | int Offset = -1; |
Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 309 | |
| 310 | for (std::vector<unsigned>::const_iterator |
| 311 | I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 312 | FilterOffsets.push_back(Offset); |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 313 | Offset -= MCAsmInfo::getULEB128Size(*I); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 316 | FirstActions.reserve(LandingPads.size()); |
| 317 | |
| 318 | int FirstAction = 0; |
| 319 | unsigned SizeActions = 0; |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 320 | const LandingPadInfo *PrevLPI = 0; |
Bill Wendling | 409914b | 2009-07-29 21:19:44 +0000 | [diff] [blame] | 321 | |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 322 | for (SmallVectorImpl<const LandingPadInfo *>::const_iterator |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 323 | I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { |
| 324 | const LandingPadInfo *LPI = *I; |
| 325 | const std::vector<int> &TypeIds = LPI->TypeIds; |
| 326 | const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 327 | unsigned SizeSiteActions = 0; |
| 328 | |
| 329 | if (NumShared < TypeIds.size()) { |
| 330 | unsigned SizeAction = 0; |
| 331 | ActionEntry *PrevAction = 0; |
| 332 | |
| 333 | if (NumShared) { |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 334 | const unsigned SizePrevIds = PrevLPI->TypeIds.size(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 335 | assert(Actions.size()); |
| 336 | PrevAction = &Actions.back(); |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 337 | SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) + |
| 338 | MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 339 | |
| 340 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { |
| 341 | SizeAction -= |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 342 | MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 343 | SizeAction += -PrevAction->NextAction; |
| 344 | PrevAction = PrevAction->Previous; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // Compute the actions. |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 349 | for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { |
| 350 | int TypeID = TypeIds[J]; |
| 351 | assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 352 | int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 353 | unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 354 | |
| 355 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 356 | SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 357 | SizeSiteActions += SizeAction; |
| 358 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 359 | ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 360 | Actions.push_back(Action); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 361 | PrevAction = &Actions.back(); |
| 362 | } |
| 363 | |
| 364 | // Record the first action of the landing pad site. |
| 365 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; |
| 366 | } // else identical - re-use previous FirstAction |
| 367 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 368 | // Information used when created the call-site table. The action record |
| 369 | // field of the call site record is the offset of the first associated |
| 370 | // action record, relative to the start of the actions table. This value is |
| 371 | // biased by 1 (1 in dicating the start of the actions table), and 0 |
| 372 | // indicates that there are no actions. |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 373 | FirstActions.push_back(FirstAction); |
| 374 | |
| 375 | // Compute this sites contribution to size. |
| 376 | SizeActions += SizeSiteActions; |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 377 | |
| 378 | PrevLPI = LPI; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 381 | return SizeActions; |
| 382 | } |
| 383 | |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 384 | /// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 385 | /// 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] | 386 | /// appropriate action. The entry for an ordinary call has a try-range |
| 387 | /// containing the call and zero for the landing pad and the action. Calls |
| 388 | /// marked 'nounwind' have no entry and must not be contained in the try-range |
| 389 | /// of any entry - they form gaps in the table. Entries must be ordered by |
| 390 | /// try-range address. |
| 391 | void DwarfException:: |
| 392 | ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, |
| 393 | const RangeMapType &PadMap, |
| 394 | const SmallVectorImpl<const LandingPadInfo *> &LandingPads, |
| 395 | const SmallVectorImpl<unsigned> &FirstActions) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 396 | // The end label of the previous invoke or nounwind try-range. |
| 397 | unsigned LastLabel = 0; |
| 398 | |
| 399 | // Whether there is a potentially throwing instruction (currently this means |
| 400 | // an ordinary call) between the end of the previous try-range and now. |
| 401 | bool SawPotentiallyThrowing = false; |
| 402 | |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 403 | // Whether the last CallSite entry was for an invoke. |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 404 | bool PreviousIsInvoke = false; |
| 405 | |
| 406 | // Visit all instructions in order of address. |
| 407 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 408 | I != E; ++I) { |
| 409 | for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); |
| 410 | MI != E; ++MI) { |
| 411 | if (!MI->isLabel()) { |
| 412 | SawPotentiallyThrowing |= MI->getDesc().isCall(); |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | unsigned BeginLabel = MI->getOperand(0).getImm(); |
| 417 | assert(BeginLabel && "Invalid label!"); |
| 418 | |
| 419 | // End of the previous try-range? |
| 420 | if (BeginLabel == LastLabel) |
| 421 | SawPotentiallyThrowing = false; |
| 422 | |
| 423 | // Beginning of a new try-range? |
| 424 | RangeMapType::iterator L = PadMap.find(BeginLabel); |
| 425 | if (L == PadMap.end()) |
| 426 | // Nope, it was just some random label. |
| 427 | continue; |
| 428 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 429 | const PadRange &P = L->second; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 430 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 431 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && |
| 432 | "Inconsistent landing pad map!"); |
| 433 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 434 | // For Dwarf exception handling (SjLj handling doesn't use this). If some |
| 435 | // instruction between the previous try-range and this one may throw, |
| 436 | // create a call-site entry with no landing pad for the region between the |
| 437 | // try-ranges. |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 438 | if (SawPotentiallyThrowing && |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 439 | MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 440 | CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 441 | CallSites.push_back(Site); |
| 442 | PreviousIsInvoke = false; |
| 443 | } |
| 444 | |
| 445 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; |
| 446 | assert(BeginLabel && LastLabel && "Invalid landing pad!"); |
| 447 | |
| 448 | if (LandingPad->LandingPadLabel) { |
| 449 | // This try-range is for an invoke. |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 450 | CallSiteEntry Site = { |
| 451 | BeginLabel, |
| 452 | LastLabel, |
| 453 | LandingPad->LandingPadLabel, |
| 454 | FirstActions[P.PadIndex] |
| 455 | }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 456 | |
| 457 | // Try to merge with the previous call-site. |
| 458 | if (PreviousIsInvoke) { |
| 459 | CallSiteEntry &Prev = CallSites.back(); |
| 460 | if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { |
| 461 | // Extend the range of the previous entry. |
| 462 | Prev.EndLabel = Site.EndLabel; |
| 463 | continue; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Otherwise, create a new call-site. |
| 468 | CallSites.push_back(Site); |
| 469 | PreviousIsInvoke = true; |
| 470 | } else { |
| 471 | // Create a gap. |
| 472 | PreviousIsInvoke = false; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // If some instruction between the previous try-range and the end of the |
| 478 | // function may throw, create a call-site entry with no landing pad for the |
| 479 | // region following the try-range. |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 480 | if (SawPotentiallyThrowing && |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 481 | MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) { |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 482 | CallSiteEntry Site = { LastLabel, 0, 0, 0 }; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 483 | CallSites.push_back(Site); |
| 484 | } |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 487 | /// EmitExceptionTable - Emit landing pads and actions. |
| 488 | /// |
| 489 | /// The general organization of the table is complex, but the basic concepts are |
| 490 | /// easy. First there is a header which describes the location and organization |
| 491 | /// of the three components that follow. |
| 492 | /// |
| 493 | /// 1. The landing pad site information describes the range of code covered by |
| 494 | /// the try. In our case it's an accumulation of the ranges covered by the |
| 495 | /// invokes in the try. There is also a reference to the landing pad that |
| 496 | /// handles the exception once processed. Finally an index into the actions |
| 497 | /// table. |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 498 | /// 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] | 499 | /// action offset. Starting with the action index from the landing pad |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 500 | /// 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] | 501 | /// it matches then the exception and type id are passed on to the landing |
| 502 | /// pad. Otherwise the next action is looked up. This chain is terminated |
| 503 | /// with a next action of zero. If no type id is found the the frame is |
| 504 | /// unwound and handling continues. |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 505 | /// 3. Type ID table contains references to all the C++ typeinfo for all |
Bill Wendling | 0dafca9 | 2009-07-29 00:50:05 +0000 | [diff] [blame] | 506 | /// catches in the function. This tables is reversed indexed base 1. |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 507 | void DwarfException::EmitExceptionTable() { |
| 508 | const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); |
| 509 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
| 510 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 511 | if (PadInfos.empty()) return; |
| 512 | |
| 513 | // Sort the landing pads in order of their type ids. This is used to fold |
| 514 | // duplicate actions. |
| 515 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 516 | LandingPads.reserve(PadInfos.size()); |
| 517 | |
| 518 | for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) |
| 519 | LandingPads.push_back(&PadInfos[i]); |
| 520 | |
| 521 | std::sort(LandingPads.begin(), LandingPads.end(), PadLT); |
| 522 | |
| 523 | // Compute the actions table and gather the first action index for each |
| 524 | // landing pad site. |
| 525 | SmallVector<ActionEntry, 32> Actions; |
| 526 | SmallVector<unsigned, 64> FirstActions; |
| 527 | unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions); |
| 528 | |
| 529 | // Invokes and nounwind calls have entries in PadMap (due to being bracketed |
| 530 | // by try-range labels when lowered). Ordinary calls do not, so appropriate |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 531 | // try-ranges for them need be deduced when using Dwarf exception handling. |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 532 | RangeMapType PadMap; |
| 533 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 534 | const LandingPadInfo *LandingPad = LandingPads[i]; |
| 535 | for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { |
| 536 | unsigned BeginLabel = LandingPad->BeginLabels[j]; |
| 537 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); |
| 538 | PadRange P = { i, j }; |
| 539 | PadMap[BeginLabel] = P; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // Compute the call-site table. |
| 544 | SmallVector<CallSiteEntry, 64> CallSites; |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 545 | ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 546 | |
| 547 | // Final tallies. |
| 548 | |
| 549 | // Call sites. |
| 550 | const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4 |
| 551 | const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4 |
| 552 | const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4 |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 553 | unsigned SizeSites; |
Jim Grosbach | bff3923 | 2009-08-12 17:38:44 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 555 | bool HaveTTData = (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) |
Jim Grosbach | bff3923 | 2009-08-12 17:38:44 +0000 | [diff] [blame] | 556 | ? (!TypeInfos.empty() || !FilterIds.empty()) : true; |
| 557 | |
| 558 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 559 | if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) { |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 560 | SizeSites = 0; |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 561 | } else |
| 562 | SizeSites = CallSites.size() * |
| 563 | (SiteStartSize + SiteLengthSize + LandingPadSize); |
| 564 | for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 565 | SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 566 | if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 567 | SizeSites += MCAsmInfo::getULEB128Size(i); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 568 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 569 | // Type infos. |
| 570 | const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr |
| 571 | unsigned SizeTypes = TypeInfos.size() * TypeInfoSize; |
| 572 | |
| 573 | unsigned TypeOffset = sizeof(int8_t) + // Call site format |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 574 | MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 575 | SizeSites + SizeActions + SizeTypes; |
| 576 | |
| 577 | unsigned TotalSize = sizeof(int8_t) + // LPStart format |
| 578 | sizeof(int8_t) + // TType format |
Jim Grosbach | bff3923 | 2009-08-12 17:38:44 +0000 | [diff] [blame] | 579 | (HaveTTData ? |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 580 | MCAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 581 | TypeOffset; |
| 582 | |
| 583 | unsigned SizeAlign = (4 - TotalSize) & 3; |
| 584 | |
| 585 | // Begin the exception table. |
Chris Lattner | d5bbb07 | 2009-08-02 01:34:32 +0000 | [diff] [blame] | 586 | const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); |
Chris Lattner | 6c2f9e1 | 2009-08-19 05:49:37 +0000 | [diff] [blame] | 587 | Asm->OutStreamer.SwitchSection(LSDASection); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 588 | Asm->EmitAlignment(2, 0, 0, false); |
| 589 | O << "GCC_except_table" << SubprogramCount << ":\n"; |
| 590 | |
| 591 | for (unsigned i = 0; i != SizeAlign; ++i) { |
| 592 | Asm->EmitInt8(0); |
| 593 | Asm->EOL("Padding"); |
Bill Wendling | c5800a8 | 2009-07-28 21:54:03 +0000 | [diff] [blame] | 594 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 595 | |
| 596 | EmitLabel("exception", SubprogramCount); |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 597 | if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) { |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 598 | std::string SjLjName = "_lsda_"; |
| 599 | SjLjName += MF->getFunction()->getName().str(); |
| 600 | EmitLabel(SjLjName.c_str(), 0); |
| 601 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 602 | |
| 603 | // Emit the header. |
| 604 | Asm->EmitInt8(dwarf::DW_EH_PE_omit); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 605 | Asm->EOL("@LPStart format (DW_EH_PE_omit)"); |
Bill Wendling | b0d9c3e | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 606 | |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 607 | #if 0 |
Chris Lattner | 81c9a06 | 2009-07-31 22:03:47 +0000 | [diff] [blame] | 608 | if (TypeInfos.empty() && FilterIds.empty()) { |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 609 | // If there are no typeinfos or filters, there is nothing to emit, optimize |
| 610 | // by specifying the "omit" encoding. |
Chris Lattner | 81c9a06 | 2009-07-31 22:03:47 +0000 | [diff] [blame] | 611 | Asm->EmitInt8(dwarf::DW_EH_PE_omit); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 612 | Asm->EOL("@TType format (DW_EH_PE_omit)"); |
Chris Lattner | 81c9a06 | 2009-07-31 22:03:47 +0000 | [diff] [blame] | 613 | } else { |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 614 | // Okay, we have actual filters or typeinfos to emit. As such, we need to |
| 615 | // pick a type encoding for them. We're about to emit a list of pointers to |
| 616 | // typeinfo objects at the end of the LSDA. However, unless we're in static |
| 617 | // mode, this reference will require a relocation by the dynamic linker. |
Chris Lattner | 46b754c | 2009-07-31 22:18:14 +0000 | [diff] [blame] | 618 | // |
Chris Lattner | ad88bc4 | 2009-08-02 03:59:56 +0000 | [diff] [blame] | 619 | // Because of this, we have a couple of options: |
| 620 | // 1) If we are in -static mode, we can always use an absolute reference |
| 621 | // from the LSDA, because the static linker will resolve it. |
| 622 | // 2) Otherwise, if the LSDA section is writable, we can output the direct |
| 623 | // reference to the typeinfo and allow the dynamic linker to relocate |
| 624 | // it. Since it is in a writable section, the dynamic linker won't |
| 625 | // have a problem. |
| 626 | // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, |
| 627 | // we need to use some form of indirection. For example, on Darwin, |
| 628 | // we can output a statically-relocatable reference to a dyld stub. The |
| 629 | // offset to the stub is constant, but the contents are in a section |
| 630 | // that is updated by the dynamic linker. This is easy enough, but we |
| 631 | // need to tell the personality function of the unwinder to indirect |
| 632 | // through the dyld stub. |
| 633 | // |
| 634 | // FIXME: When this is actually implemented, we'll have to emit the stubs |
| 635 | // somewhere. This predicate should be moved to a shared location that is |
| 636 | // in target-independent code. |
| 637 | // |
| 638 | if (LSDASection->isWritable() || |
| 639 | Asm->TM.getRelocationModel() == Reloc::Static) { |
| 640 | Asm->EmitInt8(DW_EH_PE_absptr); |
| 641 | Asm->EOL("TType format (DW_EH_PE_absptr)"); |
| 642 | } else { |
| 643 | Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4); |
| 644 | Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect" |
| 645 | " | DW_EH_PE_sdata4)"); |
| 646 | } |
Bill Wendling | b0d9c3e | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 647 | Asm->EmitULEB128Bytes(TypeOffset); |
| 648 | Asm->EOL("TType base offset"); |
Bill Wendling | b0d9c3e | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 649 | } |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 650 | #else |
Jim Grosbach | bff3923 | 2009-08-12 17:38:44 +0000 | [diff] [blame] | 651 | // For SjLj exceptions, if there is no TypeInfo, then we just explicitly |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 652 | // say that we're omitting that bit. |
| 653 | // FIXME: does this apply to Dwarf also? The above #if 0 implies yes? |
Jim Grosbach | bff3923 | 2009-08-12 17:38:44 +0000 | [diff] [blame] | 654 | if (!HaveTTData) { |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 655 | Asm->EmitInt8(dwarf::DW_EH_PE_omit); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 656 | Asm->EOL("@TType format (DW_EH_PE_omit)"); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 657 | } else { |
| 658 | Asm->EmitInt8(dwarf::DW_EH_PE_absptr); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 659 | Asm->EOL("@TType format (DW_EH_PE_absptr)"); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 660 | Asm->EmitULEB128Bytes(TypeOffset); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 661 | Asm->EOL("@TType base offset"); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 662 | } |
Bill Wendling | ade025c | 2009-07-29 00:31:35 +0000 | [diff] [blame] | 663 | #endif |
Bill Wendling | b0d9c3e | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 664 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 665 | // SjLj Exception handilng |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 666 | if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) { |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 667 | Asm->EmitInt8(dwarf::DW_EH_PE_udata4); |
| 668 | Asm->EOL("Call site format (DW_EH_PE_udata4)"); |
| 669 | Asm->EmitULEB128Bytes(SizeSites); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 670 | Asm->EOL("Call site table length"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 671 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 672 | // Emit the landing pad site information. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 673 | unsigned idx = 0; |
| 674 | for (SmallVectorImpl<CallSiteEntry>::const_iterator |
| 675 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { |
| 676 | const CallSiteEntry &S = *I; |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 677 | |
| 678 | // Offset of the landing pad, counted in 16-byte bundles relative to the |
| 679 | // @LPStart address. |
Jim Grosbach | 8b818d7 | 2009-08-17 16:41:22 +0000 | [diff] [blame] | 680 | Asm->EmitULEB128Bytes(idx); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 681 | Asm->EOL("Landing pad"); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 682 | |
| 683 | // Offset of the first associated action record, relative to the start of |
| 684 | // the action table. This value is biased by 1 (1 indicates the start of |
| 685 | // the action table), and 0 indicates that there are no actions. |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 686 | Asm->EmitULEB128Bytes(S.Action); |
| 687 | Asm->EOL("Action"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 688 | } |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 689 | } else { |
| 690 | // DWARF Exception handling |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 691 | assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 692 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 693 | // The call-site table is a list of all call sites that may throw an |
| 694 | // exception (including C++ 'throw' statements) in the procedure |
| 695 | // fragment. It immediately follows the LSDA header. Each entry indicates, |
| 696 | // for a given call, the first corresponding action record and corresponding |
| 697 | // landing pad. |
| 698 | // |
| 699 | // The table begins with the number of bytes, stored as an LEB128 |
| 700 | // compressed, unsigned integer. The records immediately follow the record |
| 701 | // count. They are sorted in increasing call-site address. Each record |
| 702 | // indicates: |
| 703 | // |
| 704 | // * The position of the call-site. |
| 705 | // * The position of the landing pad. |
| 706 | // * The first action record for that call site. |
| 707 | // |
| 708 | // A missing entry in the call-site table indicates that a call is not |
| 709 | // supposed to throw. Such calls include: |
| 710 | // |
| 711 | // * Calls to destructors within cleanup code. C++ semantics forbids these |
| 712 | // calls to throw. |
| 713 | // * Calls to intrinsic routines in the standard library which are known |
| 714 | // not to throw (sin, memcpy, et al). |
| 715 | // |
| 716 | // If the runtime does not find the call-site entry for a given call, it |
| 717 | // will call `terminate()'. |
| 718 | |
| 719 | // Emit the landing pad call site table. |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 720 | Asm->EmitInt8(dwarf::DW_EH_PE_udata4); |
| 721 | Asm->EOL("Call site format (DW_EH_PE_udata4)"); |
| 722 | Asm->EmitULEB128Bytes(SizeSites); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 723 | Asm->EOL("Call site table size"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 724 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 725 | for (SmallVectorImpl<CallSiteEntry>::const_iterator |
| 726 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { |
| 727 | const CallSiteEntry &S = *I; |
| 728 | const char *BeginTag; |
| 729 | unsigned BeginNumber; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 730 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 731 | if (!S.BeginLabel) { |
| 732 | BeginTag = "eh_func_begin"; |
| 733 | BeginNumber = SubprogramCount; |
| 734 | } else { |
| 735 | BeginTag = "label"; |
| 736 | BeginNumber = S.BeginLabel; |
| 737 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 738 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 739 | // Offset of the call site relative to the previous call site, counted in |
| 740 | // number of 16-byte bundles. The first call site is counted relative to |
| 741 | // the start of the procedure fragment. |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 742 | EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount, |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 743 | true, true); |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 744 | Asm->EOL("Region start"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 745 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 746 | if (!S.EndLabel) |
| 747 | EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber, |
| 748 | true); |
| 749 | else |
| 750 | EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 751 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 752 | Asm->EOL("Region length"); |
| 753 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 754 | // Offset of the landing pad, counted in 16-byte bundles relative to the |
| 755 | // @LPStart address. |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 756 | if (!S.PadLabel) |
| 757 | Asm->EmitInt32(0); |
| 758 | else |
| 759 | EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount, |
| 760 | true, true); |
| 761 | |
| 762 | Asm->EOL("Landing pad"); |
| 763 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 764 | // Offset of the first associated action record, relative to the start of |
| 765 | // the action table. This value is biased by 1 (1 indicates the start of |
| 766 | // the action table), and 0 indicates that there are no actions. |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 767 | Asm->EmitULEB128Bytes(S.Action); |
| 768 | Asm->EOL("Action"); |
| 769 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 772 | // Emit the Action Table. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 773 | for (SmallVectorImpl<ActionEntry>::const_iterator |
| 774 | I = Actions.begin(), E = Actions.end(); I != E; ++I) { |
| 775 | const ActionEntry &Action = *I; |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 776 | |
| 777 | // Type Filter |
| 778 | // |
| 779 | // Used by the runtime to match the type of the thrown exception to the |
| 780 | // type of the catch clauses or the types in the exception specification. |
| 781 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 782 | Asm->EmitSLEB128Bytes(Action.ValueForTypeID); |
| 783 | Asm->EOL("TypeInfo index"); |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 784 | |
| 785 | // Action Record |
| 786 | // |
| 787 | // Self-relative signed displacement in bytes of the next action record, |
| 788 | // or 0 if there is no next action record. |
| 789 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 790 | Asm->EmitSLEB128Bytes(Action.NextAction); |
| 791 | Asm->EOL("Next action"); |
| 792 | } |
| 793 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 794 | // Emit the Catch Clauses. The code for the catch clauses following the same |
| 795 | // try is similar to a switch statement. The catch clause action record |
| 796 | // informs the runtime about the type of a catch clause and about the |
| 797 | // associated switch value. |
| 798 | // |
| 799 | // Action Record Fields: |
| 800 | // |
| 801 | // * Filter Value |
| 802 | // Positive value, starting at 1. Index in the types table of the |
| 803 | // __typeinfo for the catch-clause type. 1 is the first word preceding |
| 804 | // TTBase, 2 is the second word, and so on. Used by the runtime to check |
| 805 | // if the thrown exception type matches the catch-clause type. Back-end |
| 806 | // generated switch statements check against this value. |
| 807 | // |
| 808 | // * Next |
| 809 | // Signed offset, in bytes from the start of this field, to the next |
| 810 | // chained action record, or zero if none. |
| 811 | // |
| 812 | // The order of the action records determined by the next field is the order |
| 813 | // of the catch clauses as they appear in the source code, and must be kept in |
| 814 | // the same order. As a result, changing the order of the catch clause would |
| 815 | // change the semantics of the program. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 816 | for (std::vector<GlobalVariable *>::const_reverse_iterator |
| 817 | I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 818 | const GlobalVariable *GV = *I; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 819 | PrintRelDirective(); |
| 820 | |
| 821 | if (GV) { |
| 822 | std::string GLN; |
| 823 | O << Asm->getGlobalLinkName(GV, GLN); |
| 824 | } else { |
| 825 | O << "0"; |
| 826 | } |
| 827 | |
| 828 | Asm->EOL("TypeInfo"); |
| 829 | } |
| 830 | |
Bill Wendling | a583c55 | 2009-08-20 22:02:24 +0000 | [diff] [blame] | 831 | // Emit the Type Table. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame] | 832 | for (std::vector<unsigned>::const_iterator |
| 833 | I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { |
| 834 | unsigned TypeID = *I; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 835 | Asm->EmitULEB128Bytes(TypeID); |
| 836 | Asm->EOL("Filter TypeInfo index"); |
| 837 | } |
| 838 | |
| 839 | Asm->EmitAlignment(2, 0, 0, false); |
| 840 | } |
| 841 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 842 | /// EndModule - Emit all exception information that should come after the |
| 843 | /// content. |
| 844 | void DwarfException::EndModule() { |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 845 | if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf) |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 846 | return; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 847 | if (TimePassesIsEnabled) |
| 848 | ExceptionTimer->startTimer(); |
| 849 | |
| 850 | if (shouldEmitMovesModule || shouldEmitTableModule) { |
| 851 | const std::vector<Function *> Personalities = MMI->getPersonalities(); |
| 852 | for (unsigned i = 0; i < Personalities.size(); ++i) |
Bill Wendling | a6f91da | 2009-08-25 02:27:42 +0000 | [diff] [blame] | 853 | EmitCommonInformationEntry(Personalities[i], i); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 854 | |
| 855 | for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(), |
| 856 | E = EHFrames.end(); I != E; ++I) |
Bill Wendling | a6f91da | 2009-08-25 02:27:42 +0000 | [diff] [blame] | 857 | EmitFrameDescriptionEntry(*I); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | if (TimePassesIsEnabled) |
| 861 | ExceptionTimer->stopTimer(); |
| 862 | } |
| 863 | |
| 864 | /// BeginFunction - Gather pre-function exception information. Assumes being |
| 865 | /// emitted immediately after the function entry point. |
| 866 | void DwarfException::BeginFunction(MachineFunction *MF) { |
| 867 | if (TimePassesIsEnabled) |
| 868 | ExceptionTimer->startTimer(); |
| 869 | |
| 870 | this->MF = MF; |
| 871 | shouldEmitTable = shouldEmitMoves = false; |
| 872 | |
Chris Lattner | 33adcfb | 2009-08-22 21:43:10 +0000 | [diff] [blame] | 873 | if (MMI && MAI->doesSupportExceptionHandling()) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 874 | // Map all labels and get rid of any dead landing pads. |
| 875 | MMI->TidyLandingPads(); |
| 876 | |
| 877 | // If any landing pads survive, we need an EH table. |
| 878 | if (MMI->getLandingPads().size()) |
| 879 | shouldEmitTable = true; |
| 880 | |
| 881 | // See if we need frame move info. |
| 882 | if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory) |
| 883 | shouldEmitMoves = true; |
| 884 | |
| 885 | if (shouldEmitMoves || shouldEmitTable) |
| 886 | // Assumes in correct section after the entry point. |
| 887 | EmitLabel("eh_func_begin", ++SubprogramCount); |
| 888 | } |
| 889 | |
| 890 | shouldEmitTableModule |= shouldEmitTable; |
| 891 | shouldEmitMovesModule |= shouldEmitMoves; |
| 892 | |
| 893 | if (TimePassesIsEnabled) |
| 894 | ExceptionTimer->stopTimer(); |
| 895 | } |
| 896 | |
| 897 | /// EndFunction - Gather and emit post-function exception information. |
| 898 | /// |
| 899 | void DwarfException::EndFunction() { |
| 900 | if (TimePassesIsEnabled) |
| 901 | ExceptionTimer->startTimer(); |
| 902 | |
| 903 | if (shouldEmitMoves || shouldEmitTable) { |
| 904 | EmitLabel("eh_func_end", SubprogramCount); |
| 905 | EmitExceptionTable(); |
| 906 | |
| 907 | // Save EH frame information |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 908 | EHFrames.push_back( |
Chris Lattner | e2cf37b | 2009-07-17 20:46:40 +0000 | [diff] [blame] | 909 | FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF), |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 910 | SubprogramCount, |
| 911 | MMI->getPersonalityIndex(), |
| 912 | MF->getFrameInfo()->hasCalls(), |
| 913 | !MMI->getLandingPads().empty(), |
| 914 | MMI->getFrameMoves(), |
| 915 | MF->getFunction())); |
| 916 | } |
| 917 | |
| 918 | if (TimePassesIsEnabled) |
| 919 | ExceptionTimer->stopTimer(); |
| 920 | } |