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