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