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" |
| 18 | #include "llvm/CodeGen/MachineLocation.h" |
| 19 | #include "llvm/Support/Dwarf.h" |
| 20 | #include "llvm/Support/Timer.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/Target/TargetAsmInfo.h" |
| 23 | #include "llvm/Target/TargetRegisterInfo.h" |
| 24 | #include "llvm/Target/TargetData.h" |
| 25 | #include "llvm/Target/TargetFrameInfo.h" |
| 26 | #include "llvm/Target/TargetOptions.h" |
| 27 | #include "llvm/ADT/StringExtras.h" |
| 28 | using namespace llvm; |
| 29 | |
| 30 | static TimerGroup &getDwarfTimerGroup() { |
| 31 | static TimerGroup DwarfTimerGroup("Dwarf Exception"); |
| 32 | return DwarfTimerGroup; |
| 33 | } |
| 34 | |
Bill Wendling | bc0d23a | 2009-05-15 01:18:50 +0000 | [diff] [blame] | 35 | DwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A, |
| 36 | const TargetAsmInfo *T) |
| 37 | : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false), |
| 38 | shouldEmitTableModule(false), shouldEmitMovesModule(false), |
| 39 | ExceptionTimer(0) { |
| 40 | if (TimePassesIsEnabled) |
| 41 | ExceptionTimer = new Timer("Dwarf Exception Writer", |
| 42 | getDwarfTimerGroup()); |
| 43 | } |
| 44 | |
| 45 | DwarfException::~DwarfException() { |
| 46 | delete ExceptionTimer; |
| 47 | } |
| 48 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 49 | void DwarfException::EmitCommonEHFrame(const Function *Personality, |
| 50 | unsigned Index) { |
| 51 | // Size and sign of stack growth. |
| 52 | int stackGrowth = |
| 53 | Asm->TM.getFrameInfo()->getStackGrowthDirection() == |
| 54 | TargetFrameInfo::StackGrowsUp ? |
| 55 | TD->getPointerSize() : -TD->getPointerSize(); |
| 56 | |
| 57 | // Begin eh frame section. |
| 58 | Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection()); |
| 59 | |
Chris Lattner | e2cf37b | 2009-07-17 20:46:40 +0000 | [diff] [blame] | 60 | if (TAI->is_EHSymbolPrivate()) |
| 61 | O << TAI->getPrivateGlobalPrefix(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 62 | |
| 63 | O << "EH_frame" << Index << ":\n"; |
| 64 | EmitLabel("section_eh_frame", Index); |
| 65 | |
| 66 | // Define base labels. |
| 67 | EmitLabel("eh_frame_common", Index); |
| 68 | |
| 69 | // Define the eh frame length. |
| 70 | EmitDifference("eh_frame_common_end", Index, |
| 71 | "eh_frame_common_begin", Index, true); |
| 72 | Asm->EOL("Length of Common Information Entry"); |
| 73 | |
| 74 | // EH frame header. |
| 75 | EmitLabel("eh_frame_common_begin", Index); |
| 76 | Asm->EmitInt32((int)0); |
| 77 | Asm->EOL("CIE Identifier Tag"); |
| 78 | Asm->EmitInt8(dwarf::DW_CIE_VERSION); |
| 79 | Asm->EOL("CIE Version"); |
| 80 | |
| 81 | // The personality presence indicates that language specific information will |
| 82 | // show up in the eh frame. |
| 83 | Asm->EmitString(Personality ? "zPLR" : "zR"); |
| 84 | Asm->EOL("CIE Augmentation"); |
| 85 | |
| 86 | // Round out reader. |
| 87 | Asm->EmitULEB128Bytes(1); |
| 88 | Asm->EOL("CIE Code Alignment Factor"); |
| 89 | Asm->EmitSLEB128Bytes(stackGrowth); |
| 90 | Asm->EOL("CIE Data Alignment Factor"); |
| 91 | Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true)); |
| 92 | Asm->EOL("CIE Return Address Column"); |
| 93 | |
| 94 | // If there is a personality, we need to indicate the functions location. |
| 95 | if (Personality) { |
| 96 | Asm->EmitULEB128Bytes(7); |
| 97 | Asm->EOL("Augmentation Size"); |
| 98 | |
| 99 | if (TAI->getNeedsIndirectEncoding()) { |
| 100 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 | |
| 101 | dwarf::DW_EH_PE_indirect); |
| 102 | Asm->EOL("Personality (pcrel sdata4 indirect)"); |
| 103 | } else { |
| 104 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 105 | Asm->EOL("Personality (pcrel sdata4)"); |
| 106 | } |
| 107 | |
| 108 | PrintRelDirective(true); |
| 109 | O << TAI->getPersonalityPrefix(); |
| 110 | Asm->EmitExternalGlobal((const GlobalVariable *)(Personality)); |
| 111 | O << TAI->getPersonalitySuffix(); |
| 112 | if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL")) |
| 113 | O << "-" << TAI->getPCSymbol(); |
| 114 | Asm->EOL("Personality"); |
| 115 | |
| 116 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 117 | Asm->EOL("LSDA Encoding (pcrel sdata4)"); |
| 118 | |
| 119 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 120 | Asm->EOL("FDE Encoding (pcrel sdata4)"); |
| 121 | } else { |
| 122 | Asm->EmitULEB128Bytes(1); |
| 123 | Asm->EOL("Augmentation Size"); |
| 124 | |
| 125 | Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 126 | Asm->EOL("FDE Encoding (pcrel sdata4)"); |
| 127 | } |
| 128 | |
| 129 | // Indicate locations of general callee saved registers in frame. |
| 130 | std::vector<MachineMove> Moves; |
| 131 | RI->getInitialFrameState(Moves); |
| 132 | EmitFrameMoves(NULL, 0, Moves, true); |
| 133 | |
| 134 | // On Darwin the linker honors the alignment of eh_frame, which means it must |
| 135 | // be 8-byte on 64-bit targets to match what gcc does. Otherwise you get |
| 136 | // holes which confuse readers of eh_frame. |
| 137 | Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, |
| 138 | 0, 0, false); |
| 139 | EmitLabel("eh_frame_common_end", Index); |
| 140 | |
| 141 | Asm->EOL(); |
| 142 | } |
| 143 | |
| 144 | /// EmitEHFrame - Emit function exception frame information. |
| 145 | /// |
| 146 | void DwarfException::EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) { |
| 147 | assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() && |
| 148 | "Should not emit 'available externally' functions at all"); |
| 149 | |
Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 150 | const Function *TheFunc = EHFrameInfo.function; |
| 151 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 152 | Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection()); |
| 153 | |
| 154 | // Externally visible entry into the functions eh frame info. If the |
| 155 | // corresponding function is static, this should not be externally visible. |
Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 156 | if (!TheFunc->hasLocalLinkage()) |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 157 | if (const char *GlobalEHDirective = TAI->getGlobalEHDirective()) |
| 158 | O << GlobalEHDirective << EHFrameInfo.FnName << "\n"; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 159 | |
| 160 | // If corresponding function is weak definition, this should be too. |
Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 161 | if (TheFunc->isWeakForLinker() && TAI->getWeakDefDirective()) |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 162 | O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n"; |
| 163 | |
| 164 | // If there are no calls then you can't unwind. This may mean we can omit the |
| 165 | // EH Frame, but some environments do not handle weak absolute symbols. If |
| 166 | // UnwindTablesMandatory is set we cannot do this optimization; the unwind |
| 167 | // info is to be available for non-EH uses. |
Chris Lattner | 3e0f60b | 2009-07-17 21:00:50 +0000 | [diff] [blame] | 168 | if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory && |
| 169 | (!TheFunc->isWeakForLinker() || |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 170 | !TAI->getWeakDefDirective() || |
| 171 | TAI->getSupportsWeakOmittedEHFrame())) { |
| 172 | O << EHFrameInfo.FnName << " = 0\n"; |
| 173 | // This name has no connection to the function, so it might get |
| 174 | // dead-stripped when the function is not, erroneously. Prohibit |
| 175 | // dead-stripping unconditionally. |
| 176 | if (const char *UsedDirective = TAI->getUsedDirective()) |
| 177 | O << UsedDirective << EHFrameInfo.FnName << "\n\n"; |
| 178 | } else { |
| 179 | O << EHFrameInfo.FnName << ":\n"; |
| 180 | |
| 181 | // EH frame header. |
| 182 | EmitDifference("eh_frame_end", EHFrameInfo.Number, |
| 183 | "eh_frame_begin", EHFrameInfo.Number, true); |
| 184 | Asm->EOL("Length of Frame Information Entry"); |
| 185 | |
| 186 | EmitLabel("eh_frame_begin", EHFrameInfo.Number); |
| 187 | |
Chris Lattner | a4ff5e4 | 2009-07-17 20:53:51 +0000 | [diff] [blame] | 188 | EmitSectionOffset("eh_frame_begin", "eh_frame_common", |
| 189 | EHFrameInfo.Number, EHFrameInfo.PersonalityIndex, |
| 190 | true, true, false); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 191 | |
| 192 | Asm->EOL("FDE CIE offset"); |
| 193 | |
| 194 | EmitReference("eh_func_begin", EHFrameInfo.Number, true, true); |
| 195 | Asm->EOL("FDE initial location"); |
| 196 | EmitDifference("eh_func_end", EHFrameInfo.Number, |
| 197 | "eh_func_begin", EHFrameInfo.Number, true); |
| 198 | Asm->EOL("FDE address range"); |
| 199 | |
| 200 | // If there is a personality and landing pads then point to the language |
| 201 | // specific data area in the exception table. |
| 202 | if (EHFrameInfo.PersonalityIndex) { |
| 203 | Asm->EmitULEB128Bytes(4); |
| 204 | Asm->EOL("Augmentation size"); |
| 205 | |
| 206 | if (EHFrameInfo.hasLandingPads) |
| 207 | EmitReference("exception", EHFrameInfo.Number, true, true); |
| 208 | else |
| 209 | Asm->EmitInt32((int)0); |
| 210 | Asm->EOL("Language Specific Data Area"); |
| 211 | } else { |
| 212 | Asm->EmitULEB128Bytes(0); |
| 213 | Asm->EOL("Augmentation size"); |
| 214 | } |
| 215 | |
| 216 | // Indicate locations of function specific callee saved registers in frame. |
| 217 | EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves, |
| 218 | true); |
| 219 | |
| 220 | // On Darwin the linker honors the alignment of eh_frame, which means it |
| 221 | // must be 8-byte on 64-bit targets to match what gcc does. Otherwise you |
| 222 | // get holes which confuse readers of eh_frame. |
| 223 | Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3, |
| 224 | 0, 0, false); |
| 225 | EmitLabel("eh_frame_end", EHFrameInfo.Number); |
| 226 | |
| 227 | // If the function is marked used, this table should be also. We cannot |
| 228 | // make the mark unconditional in this case, since retaining the table also |
| 229 | // retains the function in this case, and there is code around that depends |
| 230 | // on unused functions (calling undefined externals) being dead-stripped to |
| 231 | // link correctly. Yes, there really is. |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 232 | if (MMI->isUsedFunction(EHFrameInfo.function)) |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 233 | if (const char *UsedDirective = TAI->getUsedDirective()) |
| 234 | O << UsedDirective << EHFrameInfo.FnName << "\n\n"; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /// EmitExceptionTable - Emit landing pads and actions. |
| 239 | /// |
| 240 | /// The general organization of the table is complex, but the basic concepts are |
| 241 | /// easy. First there is a header which describes the location and organization |
| 242 | /// of the three components that follow. |
| 243 | /// |
| 244 | /// 1. The landing pad site information describes the range of code covered by |
| 245 | /// the try. In our case it's an accumulation of the ranges covered by the |
| 246 | /// invokes in the try. There is also a reference to the landing pad that |
| 247 | /// handles the exception once processed. Finally an index into the actions |
| 248 | /// table. |
| 249 | /// 2. The action table, in our case, is composed of pairs of type ids and next |
| 250 | /// action offset. Starting with the action index from the landing pad |
| 251 | /// site, each type Id is checked for a match to the current exception. If |
| 252 | /// it matches then the exception and type id are passed on to the landing |
| 253 | /// pad. Otherwise the next action is looked up. This chain is terminated |
| 254 | /// with a next action of zero. If no type id is found the the frame is |
| 255 | /// unwound and handling continues. |
| 256 | /// 3. Type id table contains references to all the C++ typeinfo for all |
| 257 | /// catches in the function. This tables is reversed indexed base 1. |
| 258 | |
| 259 | /// SharedTypeIds - How many leading type ids two landing pads have in common. |
| 260 | unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L, |
| 261 | const LandingPadInfo *R) { |
| 262 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 263 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 264 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 265 | unsigned Count = 0; |
| 266 | |
| 267 | for (; Count != MinSize; ++Count) |
| 268 | if (LIds[Count] != RIds[Count]) |
| 269 | return Count; |
| 270 | |
| 271 | return Count; |
| 272 | } |
| 273 | |
| 274 | /// PadLT - Order landing pads lexicographically by type id. |
| 275 | bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { |
| 276 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 277 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 278 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 279 | |
| 280 | for (unsigned i = 0; i != MinSize; ++i) |
| 281 | if (LIds[i] != RIds[i]) |
| 282 | return LIds[i] < RIds[i]; |
| 283 | |
| 284 | return LSize < RSize; |
| 285 | } |
| 286 | |
Bill Wendling | d460962 | 2009-07-28 23:23:00 +0000 | [diff] [blame] | 287 | /// ComputeActionsTable - Compute the actions table and gather the first action |
| 288 | /// index for each landing pad site. |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 289 | unsigned |
| 290 | DwarfException::ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> |
| 291 | &LandingPads, |
| 292 | SmallVectorImpl<ActionEntry> &Actions, |
| 293 | SmallVectorImpl<unsigned> &FirstActions) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 294 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 295 | |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 296 | // Negative type IDs index into FilterIds. Positive type IDs index into |
| 297 | // TypeInfos. The value written for a positive type ID is just the type ID |
| 298 | // itself. For a negative type ID, however, the value written is the |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 299 | // (negative) byte offset of the corresponding FilterIds entry. The byte |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 300 | // offset is usually equal to the type ID (because the FilterIds entries are |
| 301 | // written using a variable width encoding, which outputs one byte per entry |
| 302 | // as long as the value written is not too large) but can differ. This kind |
| 303 | // 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] | 304 | // output using a fixed width encoding. FilterOffsets[i] holds the byte |
| 305 | // offset corresponding to FilterIds[i]. |
| 306 | SmallVector<int, 16> FilterOffsets; |
| 307 | FilterOffsets.reserve(FilterIds.size()); |
| 308 | int Offset = -1; |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 309 | for(std::vector<unsigned>::const_iterator |
| 310 | I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 311 | FilterOffsets.push_back(Offset); |
| 312 | Offset -= TargetAsmInfo::getULEB128Size(*I); |
| 313 | } |
| 314 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 315 | FirstActions.reserve(LandingPads.size()); |
| 316 | |
| 317 | int FirstAction = 0; |
| 318 | unsigned SizeActions = 0; |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 319 | const LandingPadInfo *PrevLPI = 0; |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 320 | for (SmallVectorImpl<const LandingPadInfo *>::const_iterator |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 321 | I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { |
| 322 | const LandingPadInfo *LPI = *I; |
| 323 | const std::vector<int> &TypeIds = LPI->TypeIds; |
| 324 | const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 325 | unsigned SizeSiteActions = 0; |
| 326 | |
| 327 | if (NumShared < TypeIds.size()) { |
| 328 | unsigned SizeAction = 0; |
| 329 | ActionEntry *PrevAction = 0; |
| 330 | |
| 331 | if (NumShared) { |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 332 | const unsigned SizePrevIds = PrevLPI->TypeIds.size(); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 333 | assert(Actions.size()); |
| 334 | PrevAction = &Actions.back(); |
| 335 | SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) + |
| 336 | TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
| 337 | |
| 338 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { |
| 339 | SizeAction -= |
| 340 | TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
| 341 | SizeAction += -PrevAction->NextAction; |
| 342 | PrevAction = PrevAction->Previous; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // Compute the actions. |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 347 | for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { |
| 348 | int TypeID = TypeIds[J]; |
| 349 | assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 350 | int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; |
| 351 | unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID); |
| 352 | |
| 353 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; |
| 354 | SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction); |
| 355 | SizeSiteActions += SizeAction; |
| 356 | |
| 357 | ActionEntry Action = {ValueForTypeID, NextAction, PrevAction}; |
| 358 | Actions.push_back(Action); |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 359 | PrevAction = &Actions.back(); |
| 360 | } |
| 361 | |
| 362 | // Record the first action of the landing pad site. |
| 363 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; |
| 364 | } // else identical - re-use previous FirstAction |
| 365 | |
| 366 | FirstActions.push_back(FirstAction); |
| 367 | |
| 368 | // Compute this sites contribution to size. |
| 369 | SizeActions += SizeSiteActions; |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 370 | |
| 371 | PrevLPI = LPI; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 374 | return SizeActions; |
| 375 | } |
| 376 | |
| 377 | void DwarfException::EmitExceptionTable() { |
| 378 | const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); |
| 379 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
| 380 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 381 | if (PadInfos.empty()) return; |
| 382 | |
| 383 | // Sort the landing pads in order of their type ids. This is used to fold |
| 384 | // duplicate actions. |
| 385 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 386 | LandingPads.reserve(PadInfos.size()); |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 387 | |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 388 | for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) |
| 389 | LandingPads.push_back(&PadInfos[i]); |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 390 | |
Bill Wendling | 5e953dd | 2009-07-28 23:22:13 +0000 | [diff] [blame] | 391 | std::sort(LandingPads.begin(), LandingPads.end(), PadLT); |
| 392 | |
| 393 | // Compute the actions table and gather the first action index for each |
| 394 | // landing pad site. |
| 395 | SmallVector<ActionEntry, 32> Actions; |
| 396 | SmallVector<unsigned, 64> FirstActions; |
| 397 | unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions); |
| 398 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 399 | // Invokes and nounwind calls have entries in PadMap (due to being bracketed |
| 400 | // by try-range labels when lowered). Ordinary calls do not, so appropriate |
| 401 | // try-ranges for them need be deduced. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 402 | RangeMapType PadMap; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 403 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 404 | const LandingPadInfo *LandingPad = LandingPads[i]; |
| 405 | for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { |
| 406 | unsigned BeginLabel = LandingPad->BeginLabels[j]; |
| 407 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); |
| 408 | PadRange P = { i, j }; |
| 409 | PadMap[BeginLabel] = P; |
| 410 | } |
| 411 | } |
| 412 | |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 413 | // Compute the call-site table. The entry for an invoke has a try-range |
| 414 | // containing the call, a non-zero landing pad and an appropriate action. The |
| 415 | // entry for an ordinary call has a try-range containing the call and zero for |
| 416 | // the landing pad and the action. Calls marked 'nounwind' have no entry and |
| 417 | // must not be contained in the try-range of any entry - they form gaps in the |
| 418 | // table. Entries must be ordered by try-range address. |
| 419 | SmallVector<CallSiteEntry, 64> CallSites; |
| 420 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 421 | // The end label of the previous invoke or nounwind try-range. |
| 422 | unsigned LastLabel = 0; |
| 423 | |
| 424 | // Whether there is a potentially throwing instruction (currently this means |
| 425 | // an ordinary call) between the end of the previous try-range and now. |
| 426 | bool SawPotentiallyThrowing = false; |
| 427 | |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 428 | // Whether the last CallSite entry was for an invoke. |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 429 | bool PreviousIsInvoke = false; |
| 430 | |
| 431 | // Visit all instructions in order of address. |
| 432 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 433 | I != E; ++I) { |
| 434 | for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); |
| 435 | MI != E; ++MI) { |
| 436 | if (!MI->isLabel()) { |
| 437 | SawPotentiallyThrowing |= MI->getDesc().isCall(); |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | unsigned BeginLabel = MI->getOperand(0).getImm(); |
| 442 | assert(BeginLabel && "Invalid label!"); |
| 443 | |
| 444 | // End of the previous try-range? |
| 445 | if (BeginLabel == LastLabel) |
| 446 | SawPotentiallyThrowing = false; |
| 447 | |
| 448 | // Beginning of a new try-range? |
| 449 | RangeMapType::iterator L = PadMap.find(BeginLabel); |
| 450 | if (L == PadMap.end()) |
| 451 | // Nope, it was just some random label. |
| 452 | continue; |
| 453 | |
| 454 | PadRange P = L->second; |
| 455 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 456 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && |
| 457 | "Inconsistent landing pad map!"); |
| 458 | |
| 459 | // If some instruction between the previous try-range and this one may |
| 460 | // throw, create a call-site entry with no landing pad for the region |
| 461 | // between the try-ranges. |
| 462 | if (SawPotentiallyThrowing) { |
| 463 | CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0}; |
| 464 | CallSites.push_back(Site); |
| 465 | PreviousIsInvoke = false; |
| 466 | } |
| 467 | |
| 468 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; |
| 469 | assert(BeginLabel && LastLabel && "Invalid landing pad!"); |
| 470 | |
| 471 | if (LandingPad->LandingPadLabel) { |
| 472 | // This try-range is for an invoke. |
| 473 | CallSiteEntry Site = {BeginLabel, LastLabel, |
| 474 | LandingPad->LandingPadLabel, |
| 475 | FirstActions[P.PadIndex]}; |
| 476 | |
| 477 | // Try to merge with the previous call-site. |
| 478 | if (PreviousIsInvoke) { |
| 479 | CallSiteEntry &Prev = CallSites.back(); |
| 480 | if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { |
| 481 | // Extend the range of the previous entry. |
| 482 | Prev.EndLabel = Site.EndLabel; |
| 483 | continue; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Otherwise, create a new call-site. |
| 488 | CallSites.push_back(Site); |
| 489 | PreviousIsInvoke = true; |
| 490 | } else { |
| 491 | // Create a gap. |
| 492 | PreviousIsInvoke = false; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // If some instruction between the previous try-range and the end of the |
| 498 | // function may throw, create a call-site entry with no landing pad for the |
| 499 | // region following the try-range. |
| 500 | if (SawPotentiallyThrowing) { |
| 501 | CallSiteEntry Site = {LastLabel, 0, 0, 0}; |
| 502 | CallSites.push_back(Site); |
| 503 | } |
| 504 | |
| 505 | // Final tallies. |
| 506 | |
| 507 | // Call sites. |
| 508 | const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4 |
| 509 | const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4 |
| 510 | const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4 |
| 511 | unsigned SizeSites = CallSites.size() * (SiteStartSize + |
| 512 | SiteLengthSize + |
| 513 | LandingPadSize); |
| 514 | for (unsigned i = 0, e = CallSites.size(); i < e; ++i) |
| 515 | SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action); |
| 516 | |
| 517 | // Type infos. |
| 518 | const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr |
| 519 | unsigned SizeTypes = TypeInfos.size() * TypeInfoSize; |
| 520 | |
| 521 | unsigned TypeOffset = sizeof(int8_t) + // Call site format |
| 522 | TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length |
| 523 | SizeSites + SizeActions + SizeTypes; |
| 524 | |
| 525 | unsigned TotalSize = sizeof(int8_t) + // LPStart format |
| 526 | sizeof(int8_t) + // TType format |
| 527 | TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset |
| 528 | TypeOffset; |
| 529 | |
| 530 | unsigned SizeAlign = (4 - TotalSize) & 3; |
| 531 | |
| 532 | // Begin the exception table. |
| 533 | Asm->SwitchToDataSection(TAI->getDwarfExceptionSection()); |
| 534 | Asm->EmitAlignment(2, 0, 0, false); |
| 535 | O << "GCC_except_table" << SubprogramCount << ":\n"; |
| 536 | |
| 537 | for (unsigned i = 0; i != SizeAlign; ++i) { |
| 538 | Asm->EmitInt8(0); |
| 539 | Asm->EOL("Padding"); |
Bill Wendling | c5800a8 | 2009-07-28 21:54:03 +0000 | [diff] [blame] | 540 | } |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 541 | |
| 542 | EmitLabel("exception", SubprogramCount); |
| 543 | |
| 544 | // Emit the header. |
| 545 | Asm->EmitInt8(dwarf::DW_EH_PE_omit); |
| 546 | Asm->EOL("LPStart format (DW_EH_PE_omit)"); |
Bill Wendling | b0d9c3e | 2009-07-28 22:23:45 +0000 | [diff] [blame] | 547 | |
| 548 | if (!TypeInfos.empty() || !FilterIds.empty()) { |
| 549 | Asm->EmitInt8(TAI->PreferredEHDataFormat(DwarfEncoding::Data, true)); |
| 550 | // FIXME: The comment here should correspond with what PreferredEHDataFormat |
| 551 | // returned. |
| 552 | Asm->EOL("TType format (DW_EH_PE_xxxxx)"); |
| 553 | Asm->EmitULEB128Bytes(TypeOffset); |
| 554 | Asm->EOL("TType base offset"); |
| 555 | } else { |
| 556 | Asm->EmitInt8(dwarf::DW_EH_PE_omit); |
| 557 | Asm->EOL("TType format (DW_EH_PE_omit)"); |
| 558 | } |
| 559 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 560 | Asm->EmitInt8(dwarf::DW_EH_PE_udata4); |
| 561 | Asm->EOL("Call site format (DW_EH_PE_udata4)"); |
| 562 | Asm->EmitULEB128Bytes(SizeSites); |
| 563 | Asm->EOL("Call-site table length"); |
| 564 | |
| 565 | // Emit the landing pad site information. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 566 | for (SmallVectorImpl<CallSiteEntry>::const_iterator |
| 567 | I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { |
| 568 | const CallSiteEntry &S = *I; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 569 | const char *BeginTag; |
| 570 | unsigned BeginNumber; |
| 571 | |
| 572 | if (!S.BeginLabel) { |
| 573 | BeginTag = "eh_func_begin"; |
| 574 | BeginNumber = SubprogramCount; |
| 575 | } else { |
| 576 | BeginTag = "label"; |
| 577 | BeginNumber = S.BeginLabel; |
| 578 | } |
| 579 | |
| 580 | EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount, |
| 581 | true, true); |
| 582 | Asm->EOL("Region start"); |
| 583 | |
| 584 | if (!S.EndLabel) |
| 585 | EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber, |
| 586 | true); |
| 587 | else |
| 588 | EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true); |
| 589 | |
| 590 | Asm->EOL("Region length"); |
| 591 | |
| 592 | if (!S.PadLabel) |
| 593 | Asm->EmitInt32(0); |
| 594 | else |
| 595 | EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount, |
| 596 | true, true); |
| 597 | |
| 598 | Asm->EOL("Landing pad"); |
| 599 | |
| 600 | Asm->EmitULEB128Bytes(S.Action); |
| 601 | Asm->EOL("Action"); |
| 602 | } |
| 603 | |
| 604 | // Emit the actions. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 605 | for (SmallVectorImpl<ActionEntry>::const_iterator |
| 606 | I = Actions.begin(), E = Actions.end(); I != E; ++I) { |
| 607 | const ActionEntry &Action = *I; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 608 | Asm->EmitSLEB128Bytes(Action.ValueForTypeID); |
| 609 | Asm->EOL("TypeInfo index"); |
| 610 | Asm->EmitSLEB128Bytes(Action.NextAction); |
| 611 | Asm->EOL("Next action"); |
| 612 | } |
| 613 | |
| 614 | // Emit the type ids. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 615 | for (std::vector<GlobalVariable *>::const_reverse_iterator |
| 616 | I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { |
| 617 | GlobalVariable *GV = *I; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 618 | PrintRelDirective(); |
| 619 | |
| 620 | if (GV) { |
| 621 | std::string GLN; |
| 622 | O << Asm->getGlobalLinkName(GV, GLN); |
| 623 | } else { |
| 624 | O << "0"; |
| 625 | } |
| 626 | |
| 627 | Asm->EOL("TypeInfo"); |
| 628 | } |
| 629 | |
| 630 | // Emit the filter typeids. |
Bill Wendling | 5cff487 | 2009-07-28 23:44:43 +0000 | [diff] [blame^] | 631 | for (std::vector<unsigned>::const_iterator |
| 632 | I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { |
| 633 | unsigned TypeID = *I; |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 634 | Asm->EmitULEB128Bytes(TypeID); |
| 635 | Asm->EOL("Filter TypeInfo index"); |
| 636 | } |
| 637 | |
| 638 | Asm->EmitAlignment(2, 0, 0, false); |
| 639 | } |
| 640 | |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 641 | /// EndModule - Emit all exception information that should come after the |
| 642 | /// content. |
| 643 | void DwarfException::EndModule() { |
| 644 | if (TimePassesIsEnabled) |
| 645 | ExceptionTimer->startTimer(); |
| 646 | |
| 647 | if (shouldEmitMovesModule || shouldEmitTableModule) { |
| 648 | const std::vector<Function *> Personalities = MMI->getPersonalities(); |
| 649 | for (unsigned i = 0; i < Personalities.size(); ++i) |
| 650 | EmitCommonEHFrame(Personalities[i], i); |
| 651 | |
| 652 | for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(), |
| 653 | E = EHFrames.end(); I != E; ++I) |
| 654 | EmitEHFrame(*I); |
| 655 | } |
| 656 | |
| 657 | if (TimePassesIsEnabled) |
| 658 | ExceptionTimer->stopTimer(); |
| 659 | } |
| 660 | |
| 661 | /// BeginFunction - Gather pre-function exception information. Assumes being |
| 662 | /// emitted immediately after the function entry point. |
| 663 | void DwarfException::BeginFunction(MachineFunction *MF) { |
| 664 | if (TimePassesIsEnabled) |
| 665 | ExceptionTimer->startTimer(); |
| 666 | |
| 667 | this->MF = MF; |
| 668 | shouldEmitTable = shouldEmitMoves = false; |
| 669 | |
| 670 | if (MMI && TAI->doesSupportExceptionHandling()) { |
| 671 | // Map all labels and get rid of any dead landing pads. |
| 672 | MMI->TidyLandingPads(); |
| 673 | |
| 674 | // If any landing pads survive, we need an EH table. |
| 675 | if (MMI->getLandingPads().size()) |
| 676 | shouldEmitTable = true; |
| 677 | |
| 678 | // See if we need frame move info. |
| 679 | if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory) |
| 680 | shouldEmitMoves = true; |
| 681 | |
| 682 | if (shouldEmitMoves || shouldEmitTable) |
| 683 | // Assumes in correct section after the entry point. |
| 684 | EmitLabel("eh_func_begin", ++SubprogramCount); |
| 685 | } |
| 686 | |
| 687 | shouldEmitTableModule |= shouldEmitTable; |
| 688 | shouldEmitMovesModule |= shouldEmitMoves; |
| 689 | |
| 690 | if (TimePassesIsEnabled) |
| 691 | ExceptionTimer->stopTimer(); |
| 692 | } |
| 693 | |
| 694 | /// EndFunction - Gather and emit post-function exception information. |
| 695 | /// |
| 696 | void DwarfException::EndFunction() { |
| 697 | if (TimePassesIsEnabled) |
| 698 | ExceptionTimer->startTimer(); |
| 699 | |
| 700 | if (shouldEmitMoves || shouldEmitTable) { |
| 701 | EmitLabel("eh_func_end", SubprogramCount); |
| 702 | EmitExceptionTable(); |
| 703 | |
| 704 | // Save EH frame information |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 705 | EHFrames.push_back( |
Chris Lattner | e2cf37b | 2009-07-17 20:46:40 +0000 | [diff] [blame] | 706 | FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF), |
Bill Wendling | eb90721 | 2009-05-15 01:12:28 +0000 | [diff] [blame] | 707 | SubprogramCount, |
| 708 | MMI->getPersonalityIndex(), |
| 709 | MF->getFrameInfo()->hasCalls(), |
| 710 | !MMI->getLandingPads().empty(), |
| 711 | MMI->getFrameMoves(), |
| 712 | MF->getFunction())); |
| 713 | } |
| 714 | |
| 715 | if (TimePassesIsEnabled) |
| 716 | ExceptionTimer->stopTimer(); |
| 717 | } |