LLVM API Documentation
00001 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains support for writing DWARF exception info into asm files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "DwarfException.h" 00015 #include "llvm/ADT/SmallString.h" 00016 #include "llvm/ADT/StringExtras.h" 00017 #include "llvm/ADT/Twine.h" 00018 #include "llvm/CodeGen/AsmPrinter.h" 00019 #include "llvm/CodeGen/MachineFrameInfo.h" 00020 #include "llvm/CodeGen/MachineFunction.h" 00021 #include "llvm/CodeGen/MachineModuleInfo.h" 00022 #include "llvm/IR/DataLayout.h" 00023 #include "llvm/IR/Module.h" 00024 #include "llvm/MC/MCAsmInfo.h" 00025 #include "llvm/MC/MCContext.h" 00026 #include "llvm/MC/MCExpr.h" 00027 #include "llvm/MC/MCSection.h" 00028 #include "llvm/MC/MCStreamer.h" 00029 #include "llvm/MC/MCSymbol.h" 00030 #include "llvm/Support/Dwarf.h" 00031 #include "llvm/Support/ErrorHandling.h" 00032 #include "llvm/Support/FormattedStream.h" 00033 #include "llvm/Target/Mangler.h" 00034 #include "llvm/Target/TargetFrameLowering.h" 00035 #include "llvm/Target/TargetLoweringObjectFile.h" 00036 #include "llvm/Target/TargetOptions.h" 00037 #include "llvm/Target/TargetRegisterInfo.h" 00038 using namespace llvm; 00039 00040 DwarfException::DwarfException(AsmPrinter *A) 00041 : Asm(A), MMI(Asm->MMI) {} 00042 00043 DwarfException::~DwarfException() {} 00044 00045 /// SharedTypeIds - How many leading type ids two landing pads have in common. 00046 unsigned DwarfException::SharedTypeIds(const LandingPadInfo *L, 00047 const LandingPadInfo *R) { 00048 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 00049 unsigned LSize = LIds.size(), RSize = RIds.size(); 00050 unsigned MinSize = LSize < RSize ? LSize : RSize; 00051 unsigned Count = 0; 00052 00053 for (; Count != MinSize; ++Count) 00054 if (LIds[Count] != RIds[Count]) 00055 return Count; 00056 00057 return Count; 00058 } 00059 00060 /// PadLT - Order landing pads lexicographically by type id. 00061 bool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { 00062 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 00063 unsigned LSize = LIds.size(), RSize = RIds.size(); 00064 unsigned MinSize = LSize < RSize ? LSize : RSize; 00065 00066 for (unsigned i = 0; i != MinSize; ++i) 00067 if (LIds[i] != RIds[i]) 00068 return LIds[i] < RIds[i]; 00069 00070 return LSize < RSize; 00071 } 00072 00073 /// ComputeActionsTable - Compute the actions table and gather the first action 00074 /// index for each landing pad site. 00075 unsigned DwarfException:: 00076 ComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, 00077 SmallVectorImpl<ActionEntry> &Actions, 00078 SmallVectorImpl<unsigned> &FirstActions) { 00079 00080 // The action table follows the call-site table in the LSDA. The individual 00081 // records are of two types: 00082 // 00083 // * Catch clause 00084 // * Exception specification 00085 // 00086 // The two record kinds have the same format, with only small differences. 00087 // They are distinguished by the "switch value" field: Catch clauses 00088 // (TypeInfos) have strictly positive switch values, and exception 00089 // specifications (FilterIds) have strictly negative switch values. Value 0 00090 // indicates a catch-all clause. 00091 // 00092 // Negative type IDs index into FilterIds. Positive type IDs index into 00093 // TypeInfos. The value written for a positive type ID is just the type ID 00094 // itself. For a negative type ID, however, the value written is the 00095 // (negative) byte offset of the corresponding FilterIds entry. The byte 00096 // offset is usually equal to the type ID (because the FilterIds entries are 00097 // written using a variable width encoding, which outputs one byte per entry 00098 // as long as the value written is not too large) but can differ. This kind 00099 // of complication does not occur for positive type IDs because type infos are 00100 // output using a fixed width encoding. FilterOffsets[i] holds the byte 00101 // offset corresponding to FilterIds[i]. 00102 00103 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 00104 SmallVector<int, 16> FilterOffsets; 00105 FilterOffsets.reserve(FilterIds.size()); 00106 int Offset = -1; 00107 00108 for (std::vector<unsigned>::const_iterator 00109 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { 00110 FilterOffsets.push_back(Offset); 00111 Offset -= MCAsmInfo::getULEB128Size(*I); 00112 } 00113 00114 FirstActions.reserve(LandingPads.size()); 00115 00116 int FirstAction = 0; 00117 unsigned SizeActions = 0; 00118 const LandingPadInfo *PrevLPI = 0; 00119 00120 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator 00121 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { 00122 const LandingPadInfo *LPI = *I; 00123 const std::vector<int> &TypeIds = LPI->TypeIds; 00124 unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0; 00125 unsigned SizeSiteActions = 0; 00126 00127 if (NumShared < TypeIds.size()) { 00128 unsigned SizeAction = 0; 00129 unsigned PrevAction = (unsigned)-1; 00130 00131 if (NumShared) { 00132 unsigned SizePrevIds = PrevLPI->TypeIds.size(); 00133 assert(Actions.size()); 00134 PrevAction = Actions.size() - 1; 00135 SizeAction = 00136 MCAsmInfo::getSLEB128Size(Actions[PrevAction].NextAction) + 00137 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID); 00138 00139 for (unsigned j = NumShared; j != SizePrevIds; ++j) { 00140 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); 00141 SizeAction -= 00142 MCAsmInfo::getSLEB128Size(Actions[PrevAction].ValueForTypeID); 00143 SizeAction += -Actions[PrevAction].NextAction; 00144 PrevAction = Actions[PrevAction].Previous; 00145 } 00146 } 00147 00148 // Compute the actions. 00149 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { 00150 int TypeID = TypeIds[J]; 00151 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 00152 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; 00153 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID); 00154 00155 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; 00156 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction); 00157 SizeSiteActions += SizeAction; 00158 00159 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; 00160 Actions.push_back(Action); 00161 PrevAction = Actions.size() - 1; 00162 } 00163 00164 // Record the first action of the landing pad site. 00165 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; 00166 } // else identical - re-use previous FirstAction 00167 00168 // Information used when created the call-site table. The action record 00169 // field of the call site record is the offset of the first associated 00170 // action record, relative to the start of the actions table. This value is 00171 // biased by 1 (1 indicating the start of the actions table), and 0 00172 // indicates that there are no actions. 00173 FirstActions.push_back(FirstAction); 00174 00175 // Compute this sites contribution to size. 00176 SizeActions += SizeSiteActions; 00177 00178 PrevLPI = LPI; 00179 } 00180 00181 return SizeActions; 00182 } 00183 00184 /// CallToNoUnwindFunction - Return `true' if this is a call to a function 00185 /// marked `nounwind'. Return `false' otherwise. 00186 bool DwarfException::CallToNoUnwindFunction(const MachineInstr *MI) { 00187 assert(MI->isCall() && "This should be a call instruction!"); 00188 00189 bool MarkedNoUnwind = false; 00190 bool SawFunc = false; 00191 00192 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { 00193 const MachineOperand &MO = MI->getOperand(I); 00194 00195 if (!MO.isGlobal()) continue; 00196 00197 const Function *F = dyn_cast<Function>(MO.getGlobal()); 00198 if (F == 0) continue; 00199 00200 if (SawFunc) { 00201 // Be conservative. If we have more than one function operand for this 00202 // call, then we can't make the assumption that it's the callee and 00203 // not a parameter to the call. 00204 // 00205 // FIXME: Determine if there's a way to say that `F' is the callee or 00206 // parameter. 00207 MarkedNoUnwind = false; 00208 break; 00209 } 00210 00211 MarkedNoUnwind = F->doesNotThrow(); 00212 SawFunc = true; 00213 } 00214 00215 return MarkedNoUnwind; 00216 } 00217 00218 /// ComputeCallSiteTable - Compute the call-site table. The entry for an invoke 00219 /// has a try-range containing the call, a non-zero landing pad, and an 00220 /// appropriate action. The entry for an ordinary call has a try-range 00221 /// containing the call and zero for the landing pad and the action. Calls 00222 /// marked 'nounwind' have no entry and must not be contained in the try-range 00223 /// of any entry - they form gaps in the table. Entries must be ordered by 00224 /// try-range address. 00225 void DwarfException:: 00226 ComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, 00227 const RangeMapType &PadMap, 00228 const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 00229 const SmallVectorImpl<unsigned> &FirstActions) { 00230 // The end label of the previous invoke or nounwind try-range. 00231 MCSymbol *LastLabel = 0; 00232 00233 // Whether there is a potentially throwing instruction (currently this means 00234 // an ordinary call) between the end of the previous try-range and now. 00235 bool SawPotentiallyThrowing = false; 00236 00237 // Whether the last CallSite entry was for an invoke. 00238 bool PreviousIsInvoke = false; 00239 00240 // Visit all instructions in order of address. 00241 for (MachineFunction::const_iterator I = Asm->MF->begin(), E = Asm->MF->end(); 00242 I != E; ++I) { 00243 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); 00244 MI != E; ++MI) { 00245 if (!MI->isLabel()) { 00246 if (MI->isCall()) 00247 SawPotentiallyThrowing |= !CallToNoUnwindFunction(MI); 00248 continue; 00249 } 00250 00251 // End of the previous try-range? 00252 MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol(); 00253 if (BeginLabel == LastLabel) 00254 SawPotentiallyThrowing = false; 00255 00256 // Beginning of a new try-range? 00257 RangeMapType::const_iterator L = PadMap.find(BeginLabel); 00258 if (L == PadMap.end()) 00259 // Nope, it was just some random label. 00260 continue; 00261 00262 const PadRange &P = L->second; 00263 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 00264 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 00265 "Inconsistent landing pad map!"); 00266 00267 // For Dwarf exception handling (SjLj handling doesn't use this). If some 00268 // instruction between the previous try-range and this one may throw, 00269 // create a call-site entry with no landing pad for the region between the 00270 // try-ranges. 00271 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) { 00272 CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 }; 00273 CallSites.push_back(Site); 00274 PreviousIsInvoke = false; 00275 } 00276 00277 LastLabel = LandingPad->EndLabels[P.RangeIndex]; 00278 assert(BeginLabel && LastLabel && "Invalid landing pad!"); 00279 00280 if (!LandingPad->LandingPadLabel) { 00281 // Create a gap. 00282 PreviousIsInvoke = false; 00283 } else { 00284 // This try-range is for an invoke. 00285 CallSiteEntry Site = { 00286 BeginLabel, 00287 LastLabel, 00288 LandingPad->LandingPadLabel, 00289 FirstActions[P.PadIndex] 00290 }; 00291 00292 // Try to merge with the previous call-site. SJLJ doesn't do this 00293 if (PreviousIsInvoke && Asm->MAI->isExceptionHandlingDwarf()) { 00294 CallSiteEntry &Prev = CallSites.back(); 00295 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { 00296 // Extend the range of the previous entry. 00297 Prev.EndLabel = Site.EndLabel; 00298 continue; 00299 } 00300 } 00301 00302 // Otherwise, create a new call-site. 00303 if (Asm->MAI->isExceptionHandlingDwarf()) 00304 CallSites.push_back(Site); 00305 else { 00306 // SjLj EH must maintain the call sites in the order assigned 00307 // to them by the SjLjPrepare pass. 00308 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel); 00309 if (CallSites.size() < SiteNo) 00310 CallSites.resize(SiteNo); 00311 CallSites[SiteNo - 1] = Site; 00312 } 00313 PreviousIsInvoke = true; 00314 } 00315 } 00316 } 00317 00318 // If some instruction between the previous try-range and the end of the 00319 // function may throw, create a call-site entry with no landing pad for the 00320 // region following the try-range. 00321 if (SawPotentiallyThrowing && Asm->MAI->isExceptionHandlingDwarf()) { 00322 CallSiteEntry Site = { LastLabel, 0, 0, 0 }; 00323 CallSites.push_back(Site); 00324 } 00325 } 00326 00327 /// EmitExceptionTable - Emit landing pads and actions. 00328 /// 00329 /// The general organization of the table is complex, but the basic concepts are 00330 /// easy. First there is a header which describes the location and organization 00331 /// of the three components that follow. 00332 /// 00333 /// 1. The landing pad site information describes the range of code covered by 00334 /// the try. In our case it's an accumulation of the ranges covered by the 00335 /// invokes in the try. There is also a reference to the landing pad that 00336 /// handles the exception once processed. Finally an index into the actions 00337 /// table. 00338 /// 2. The action table, in our case, is composed of pairs of type IDs and next 00339 /// action offset. Starting with the action index from the landing pad 00340 /// site, each type ID is checked for a match to the current exception. If 00341 /// it matches then the exception and type id are passed on to the landing 00342 /// pad. Otherwise the next action is looked up. This chain is terminated 00343 /// with a next action of zero. If no type id is found then the frame is 00344 /// unwound and handling continues. 00345 /// 3. Type ID table contains references to all the C++ typeinfo for all 00346 /// catches in the function. This tables is reverse indexed base 1. 00347 void DwarfException::EmitExceptionTable() { 00348 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); 00349 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 00350 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); 00351 00352 // Sort the landing pads in order of their type ids. This is used to fold 00353 // duplicate actions. 00354 SmallVector<const LandingPadInfo *, 64> LandingPads; 00355 LandingPads.reserve(PadInfos.size()); 00356 00357 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 00358 LandingPads.push_back(&PadInfos[i]); 00359 00360 std::sort(LandingPads.begin(), LandingPads.end(), PadLT); 00361 00362 // Compute the actions table and gather the first action index for each 00363 // landing pad site. 00364 SmallVector<ActionEntry, 32> Actions; 00365 SmallVector<unsigned, 64> FirstActions; 00366 unsigned SizeActions=ComputeActionsTable(LandingPads, Actions, FirstActions); 00367 00368 // Invokes and nounwind calls have entries in PadMap (due to being bracketed 00369 // by try-range labels when lowered). Ordinary calls do not, so appropriate 00370 // try-ranges for them need be deduced when using DWARF exception handling. 00371 RangeMapType PadMap; 00372 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 00373 const LandingPadInfo *LandingPad = LandingPads[i]; 00374 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 00375 MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; 00376 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 00377 PadRange P = { i, j }; 00378 PadMap[BeginLabel] = P; 00379 } 00380 } 00381 00382 // Compute the call-site table. 00383 SmallVector<CallSiteEntry, 64> CallSites; 00384 ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions); 00385 00386 // Final tallies. 00387 00388 // Call sites. 00389 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 00390 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true; 00391 00392 unsigned CallSiteTableLength; 00393 if (IsSJLJ) 00394 CallSiteTableLength = 0; 00395 else { 00396 unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4 00397 unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4 00398 unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4 00399 CallSiteTableLength = 00400 CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize); 00401 } 00402 00403 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { 00404 CallSiteTableLength += MCAsmInfo::getULEB128Size(CallSites[i].Action); 00405 if (IsSJLJ) 00406 CallSiteTableLength += MCAsmInfo::getULEB128Size(i); 00407 } 00408 00409 // Type infos. 00410 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); 00411 unsigned TTypeEncoding; 00412 unsigned TypeFormatSize; 00413 00414 if (!HaveTTData) { 00415 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say 00416 // that we're omitting that bit. 00417 TTypeEncoding = dwarf::DW_EH_PE_omit; 00418 // dwarf::DW_EH_PE_absptr 00419 TypeFormatSize = Asm->getDataLayout().getPointerSize(); 00420 } else { 00421 // Okay, we have actual filters or typeinfos to emit. As such, we need to 00422 // pick a type encoding for them. We're about to emit a list of pointers to 00423 // typeinfo objects at the end of the LSDA. However, unless we're in static 00424 // mode, this reference will require a relocation by the dynamic linker. 00425 // 00426 // Because of this, we have a couple of options: 00427 // 00428 // 1) If we are in -static mode, we can always use an absolute reference 00429 // from the LSDA, because the static linker will resolve it. 00430 // 00431 // 2) Otherwise, if the LSDA section is writable, we can output the direct 00432 // reference to the typeinfo and allow the dynamic linker to relocate 00433 // it. Since it is in a writable section, the dynamic linker won't 00434 // have a problem. 00435 // 00436 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 00437 // we need to use some form of indirection. For example, on Darwin, 00438 // we can output a statically-relocatable reference to a dyld stub. The 00439 // offset to the stub is constant, but the contents are in a section 00440 // that is updated by the dynamic linker. This is easy enough, but we 00441 // need to tell the personality function of the unwinder to indirect 00442 // through the dyld stub. 00443 // 00444 // FIXME: When (3) is actually implemented, we'll have to emit the stubs 00445 // somewhere. This predicate should be moved to a shared location that is 00446 // in target-independent code. 00447 // 00448 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); 00449 TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding); 00450 } 00451 00452 // Begin the exception table. 00453 // Sometimes we want not to emit the data into separate section (e.g. ARM 00454 // EHABI). In this case LSDASection will be NULL. 00455 if (LSDASection) 00456 Asm->OutStreamer.SwitchSection(LSDASection); 00457 Asm->EmitAlignment(2); 00458 00459 // Emit the LSDA. 00460 MCSymbol *GCCETSym = 00461 Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+ 00462 Twine(Asm->getFunctionNumber())); 00463 Asm->OutStreamer.EmitLabel(GCCETSym); 00464 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("exception", 00465 Asm->getFunctionNumber())); 00466 00467 if (IsSJLJ) 00468 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("_LSDA_", 00469 Asm->getFunctionNumber())); 00470 00471 // Emit the LSDA header. 00472 Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 00473 Asm->EmitEncodingByte(TTypeEncoding, "@TType"); 00474 00475 // The type infos need to be aligned. GCC does this by inserting padding just 00476 // before the type infos. However, this changes the size of the exception 00477 // table, so you need to take this into account when you output the exception 00478 // table size. However, the size is output using a variable length encoding. 00479 // So by increasing the size by inserting padding, you may increase the number 00480 // of bytes used for writing the size. If it increases, say by one byte, then 00481 // you now need to output one less byte of padding to get the type infos 00482 // aligned. However this decreases the size of the exception table. This 00483 // changes the value you have to output for the exception table size. Due to 00484 // the variable length encoding, the number of bytes used for writing the 00485 // length may decrease. If so, you then have to increase the amount of 00486 // padding. And so on. If you look carefully at the GCC code you will see that 00487 // it indeed does this in a loop, going on and on until the values stabilize. 00488 // We chose another solution: don't output padding inside the table like GCC 00489 // does, instead output it before the table. 00490 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize; 00491 unsigned CallSiteTableLengthSize = 00492 MCAsmInfo::getULEB128Size(CallSiteTableLength); 00493 unsigned TTypeBaseOffset = 00494 sizeof(int8_t) + // Call site format 00495 CallSiteTableLengthSize + // Call site table length size 00496 CallSiteTableLength + // Call site table length 00497 SizeActions + // Actions size 00498 SizeTypes; 00499 unsigned TTypeBaseOffsetSize = MCAsmInfo::getULEB128Size(TTypeBaseOffset); 00500 unsigned TotalSize = 00501 sizeof(int8_t) + // LPStart format 00502 sizeof(int8_t) + // TType format 00503 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size 00504 TTypeBaseOffset; // TType base offset 00505 unsigned SizeAlign = (4 - TotalSize) & 3; 00506 00507 if (HaveTTData) { 00508 // Account for any extra padding that will be added to the call site table 00509 // length. 00510 Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign); 00511 SizeAlign = 0; 00512 } 00513 00514 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm(); 00515 00516 // SjLj Exception handling 00517 if (IsSJLJ) { 00518 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); 00519 00520 // Add extra padding if it wasn't added to the TType base offset. 00521 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); 00522 00523 // Emit the landing pad site information. 00524 unsigned idx = 0; 00525 for (SmallVectorImpl<CallSiteEntry>::const_iterator 00526 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 00527 const CallSiteEntry &S = *I; 00528 00529 // Offset of the landing pad, counted in 16-byte bundles relative to the 00530 // @LPStart address. 00531 if (VerboseAsm) { 00532 Asm->OutStreamer.AddComment(">> Call Site " + Twine(idx) + " <<"); 00533 Asm->OutStreamer.AddComment(" On exception at call site "+Twine(idx)); 00534 } 00535 Asm->EmitULEB128(idx); 00536 00537 // Offset of the first associated action record, relative to the start of 00538 // the action table. This value is biased by 1 (1 indicates the start of 00539 // the action table), and 0 indicates that there are no actions. 00540 if (VerboseAsm) { 00541 if (S.Action == 0) 00542 Asm->OutStreamer.AddComment(" Action: cleanup"); 00543 else 00544 Asm->OutStreamer.AddComment(" Action: " + 00545 Twine((S.Action - 1) / 2 + 1)); 00546 } 00547 Asm->EmitULEB128(S.Action); 00548 } 00549 } else { 00550 // DWARF Exception handling 00551 assert(Asm->MAI->isExceptionHandlingDwarf()); 00552 00553 // The call-site table is a list of all call sites that may throw an 00554 // exception (including C++ 'throw' statements) in the procedure 00555 // fragment. It immediately follows the LSDA header. Each entry indicates, 00556 // for a given call, the first corresponding action record and corresponding 00557 // landing pad. 00558 // 00559 // The table begins with the number of bytes, stored as an LEB128 00560 // compressed, unsigned integer. The records immediately follow the record 00561 // count. They are sorted in increasing call-site address. Each record 00562 // indicates: 00563 // 00564 // * The position of the call-site. 00565 // * The position of the landing pad. 00566 // * The first action record for that call site. 00567 // 00568 // A missing entry in the call-site table indicates that a call is not 00569 // supposed to throw. 00570 00571 // Emit the landing pad call site table. 00572 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); 00573 00574 // Add extra padding if it wasn't added to the TType base offset. 00575 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); 00576 00577 unsigned Entry = 0; 00578 for (SmallVectorImpl<CallSiteEntry>::const_iterator 00579 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { 00580 const CallSiteEntry &S = *I; 00581 00582 MCSymbol *EHFuncBeginSym = 00583 Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber()); 00584 00585 MCSymbol *BeginLabel = S.BeginLabel; 00586 if (BeginLabel == 0) 00587 BeginLabel = EHFuncBeginSym; 00588 MCSymbol *EndLabel = S.EndLabel; 00589 if (EndLabel == 0) 00590 EndLabel = Asm->GetTempSymbol("eh_func_end", Asm->getFunctionNumber()); 00591 00592 00593 // Offset of the call site relative to the previous call site, counted in 00594 // number of 16-byte bundles. The first call site is counted relative to 00595 // the start of the procedure fragment. 00596 if (VerboseAsm) 00597 Asm->OutStreamer.AddComment(">> Call Site " + Twine(++Entry) + " <<"); 00598 Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4); 00599 if (VerboseAsm) 00600 Asm->OutStreamer.AddComment(Twine(" Call between ") + 00601 BeginLabel->getName() + " and " + 00602 EndLabel->getName()); 00603 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); 00604 00605 // Offset of the landing pad, counted in 16-byte bundles relative to the 00606 // @LPStart address. 00607 if (!S.PadLabel) { 00608 if (VerboseAsm) 00609 Asm->OutStreamer.AddComment(" has no landing pad"); 00610 Asm->OutStreamer.EmitIntValue(0, 4/*size*/); 00611 } else { 00612 if (VerboseAsm) 00613 Asm->OutStreamer.AddComment(Twine(" jumps to ") + 00614 S.PadLabel->getName()); 00615 Asm->EmitLabelDifference(S.PadLabel, EHFuncBeginSym, 4); 00616 } 00617 00618 // Offset of the first associated action record, relative to the start of 00619 // the action table. This value is biased by 1 (1 indicates the start of 00620 // the action table), and 0 indicates that there are no actions. 00621 if (VerboseAsm) { 00622 if (S.Action == 0) 00623 Asm->OutStreamer.AddComment(" On action: cleanup"); 00624 else 00625 Asm->OutStreamer.AddComment(" On action: " + 00626 Twine((S.Action - 1) / 2 + 1)); 00627 } 00628 Asm->EmitULEB128(S.Action); 00629 } 00630 } 00631 00632 // Emit the Action Table. 00633 int Entry = 0; 00634 for (SmallVectorImpl<ActionEntry>::const_iterator 00635 I = Actions.begin(), E = Actions.end(); I != E; ++I) { 00636 const ActionEntry &Action = *I; 00637 00638 if (VerboseAsm) { 00639 // Emit comments that decode the action table. 00640 Asm->OutStreamer.AddComment(">> Action Record " + Twine(++Entry) + " <<"); 00641 } 00642 00643 // Type Filter 00644 // 00645 // Used by the runtime to match the type of the thrown exception to the 00646 // type of the catch clauses or the types in the exception specification. 00647 if (VerboseAsm) { 00648 if (Action.ValueForTypeID > 0) 00649 Asm->OutStreamer.AddComment(" Catch TypeInfo " + 00650 Twine(Action.ValueForTypeID)); 00651 else if (Action.ValueForTypeID < 0) 00652 Asm->OutStreamer.AddComment(" Filter TypeInfo " + 00653 Twine(Action.ValueForTypeID)); 00654 else 00655 Asm->OutStreamer.AddComment(" Cleanup"); 00656 } 00657 Asm->EmitSLEB128(Action.ValueForTypeID); 00658 00659 // Action Record 00660 // 00661 // Self-relative signed displacement in bytes of the next action record, 00662 // or 0 if there is no next action record. 00663 if (VerboseAsm) { 00664 if (Action.NextAction == 0) { 00665 Asm->OutStreamer.AddComment(" No further actions"); 00666 } else { 00667 unsigned NextAction = Entry + (Action.NextAction + 1) / 2; 00668 Asm->OutStreamer.AddComment(" Continue to action "+Twine(NextAction)); 00669 } 00670 } 00671 Asm->EmitSLEB128(Action.NextAction); 00672 } 00673 00674 EmitTypeInfos(TTypeEncoding); 00675 00676 Asm->EmitAlignment(2); 00677 } 00678 00679 void DwarfException::EmitTypeInfos(unsigned TTypeEncoding) { 00680 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); 00681 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 00682 00683 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm(); 00684 00685 int Entry = 0; 00686 // Emit the Catch TypeInfos. 00687 if (VerboseAsm && !TypeInfos.empty()) { 00688 Asm->OutStreamer.AddComment(">> Catch TypeInfos <<"); 00689 Asm->OutStreamer.AddBlankLine(); 00690 Entry = TypeInfos.size(); 00691 } 00692 00693 for (std::vector<const GlobalVariable *>::const_reverse_iterator 00694 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { 00695 const GlobalVariable *GV = *I; 00696 if (VerboseAsm) 00697 Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--)); 00698 Asm->EmitTTypeReference(GV, TTypeEncoding); 00699 } 00700 00701 // Emit the Exception Specifications. 00702 if (VerboseAsm && !FilterIds.empty()) { 00703 Asm->OutStreamer.AddComment(">> Filter TypeInfos <<"); 00704 Asm->OutStreamer.AddBlankLine(); 00705 Entry = 0; 00706 } 00707 for (std::vector<unsigned>::const_iterator 00708 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 00709 unsigned TypeID = *I; 00710 if (VerboseAsm) { 00711 --Entry; 00712 if (TypeID != 0) 00713 Asm->OutStreamer.AddComment("FilterInfo " + Twine(Entry)); 00714 } 00715 00716 Asm->EmitULEB128(TypeID); 00717 } 00718 } 00719 00720 /// EndModule - Emit all exception information that should come after the 00721 /// content. 00722 void DwarfException::EndModule() { 00723 llvm_unreachable("Should be implemented"); 00724 } 00725 00726 /// BeginFunction - Gather pre-function exception information. Assumes it's 00727 /// being emitted immediately after the function entry point. 00728 void DwarfException::BeginFunction(const MachineFunction *MF) { 00729 llvm_unreachable("Should be implemented"); 00730 } 00731 00732 /// EndFunction - Gather and emit post-function exception information. 00733 /// 00734 void DwarfException::EndFunction() { 00735 llvm_unreachable("Should be implemented"); 00736 }