LLVM API Documentation
00001 //===----- JITDwarfEmitter.cpp - Write dwarf tables into memory -----------===// 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 defines a JITDwarfEmitter object that is used by the JIT to 00011 // write dwarf tables to memory. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "JIT.h" 00016 #include "JITDwarfEmitter.h" 00017 #include "llvm/Function.h" 00018 #include "llvm/ADT/DenseMap.h" 00019 #include "llvm/CodeGen/JITCodeEmitter.h" 00020 #include "llvm/CodeGen/MachineFunction.h" 00021 #include "llvm/CodeGen/MachineLocation.h" 00022 #include "llvm/CodeGen/MachineModuleInfo.h" 00023 #include "llvm/ExecutionEngine/JITMemoryManager.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Target/TargetAsmInfo.h" 00026 #include "llvm/Target/TargetData.h" 00027 #include "llvm/Target/TargetInstrInfo.h" 00028 #include "llvm/Target/TargetFrameInfo.h" 00029 #include "llvm/Target/TargetMachine.h" 00030 #include "llvm/Target/TargetRegisterInfo.h" 00031 00032 using namespace llvm; 00033 00034 JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : Jit(theJit) {} 00035 00036 00037 unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F, 00038 JITCodeEmitter& jce, 00039 unsigned char* StartFunction, 00040 unsigned char* EndFunction) { 00041 const TargetMachine& TM = F.getTarget(); 00042 TD = TM.getTargetData(); 00043 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding(); 00044 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection(); 00045 RI = TM.getRegisterInfo(); 00046 JCE = &jce; 00047 00048 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction, 00049 EndFunction); 00050 00051 unsigned char* Result = 0; 00052 unsigned char* EHFramePtr = 0; 00053 00054 const std::vector<Function *> Personalities = MMI->getPersonalities(); 00055 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]); 00056 00057 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr, 00058 StartFunction, EndFunction, ExceptionTable); 00059 00060 return Result; 00061 } 00062 00063 00064 void 00065 JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr, 00066 const std::vector<MachineMove> &Moves) const { 00067 unsigned PointerSize = TD->getPointerSize(); 00068 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? 00069 PointerSize : -PointerSize; 00070 bool IsLocal = false; 00071 unsigned BaseLabelID = 0; 00072 00073 for (unsigned i = 0, N = Moves.size(); i < N; ++i) { 00074 const MachineMove &Move = Moves[i]; 00075 unsigned LabelID = Move.getLabelID(); 00076 00077 if (LabelID) { 00078 LabelID = MMI->MappedLabel(LabelID); 00079 00080 // Throw out move if the label is invalid. 00081 if (!LabelID) continue; 00082 } 00083 00084 intptr_t LabelPtr = 0; 00085 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID); 00086 00087 const MachineLocation &Dst = Move.getDestination(); 00088 const MachineLocation &Src = Move.getSource(); 00089 00090 // Advance row if new location. 00091 if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) { 00092 JCE->emitByte(dwarf::DW_CFA_advance_loc4); 00093 JCE->emitInt32(LabelPtr - BaseLabelPtr); 00094 00095 BaseLabelID = LabelID; 00096 BaseLabelPtr = LabelPtr; 00097 IsLocal = true; 00098 } 00099 00100 // If advancing cfa. 00101 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { 00102 if (!Src.isReg()) { 00103 if (Src.getReg() == MachineLocation::VirtualFP) { 00104 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset); 00105 } else { 00106 JCE->emitByte(dwarf::DW_CFA_def_cfa); 00107 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true)); 00108 } 00109 00110 int Offset = -Src.getOffset(); 00111 00112 JCE->emitULEB128Bytes(Offset); 00113 } else { 00114 llvm_unreachable("Machine move no supported yet."); 00115 } 00116 } else if (Src.isReg() && 00117 Src.getReg() == MachineLocation::VirtualFP) { 00118 if (Dst.isReg()) { 00119 JCE->emitByte(dwarf::DW_CFA_def_cfa_register); 00120 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true)); 00121 } else { 00122 llvm_unreachable("Machine move no supported yet."); 00123 } 00124 } else { 00125 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true); 00126 int Offset = Dst.getOffset() / stackGrowth; 00127 00128 if (Offset < 0) { 00129 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf); 00130 JCE->emitULEB128Bytes(Reg); 00131 JCE->emitSLEB128Bytes(Offset); 00132 } else if (Reg < 64) { 00133 JCE->emitByte(dwarf::DW_CFA_offset + Reg); 00134 JCE->emitULEB128Bytes(Offset); 00135 } else { 00136 JCE->emitByte(dwarf::DW_CFA_offset_extended); 00137 JCE->emitULEB128Bytes(Reg); 00138 JCE->emitULEB128Bytes(Offset); 00139 } 00140 } 00141 } 00142 } 00143 00144 /// SharedTypeIds - How many leading type ids two landing pads have in common. 00145 static unsigned SharedTypeIds(const LandingPadInfo *L, 00146 const LandingPadInfo *R) { 00147 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 00148 unsigned LSize = LIds.size(), RSize = RIds.size(); 00149 unsigned MinSize = LSize < RSize ? LSize : RSize; 00150 unsigned Count = 0; 00151 00152 for (; Count != MinSize; ++Count) 00153 if (LIds[Count] != RIds[Count]) 00154 return Count; 00155 00156 return Count; 00157 } 00158 00159 00160 /// PadLT - Order landing pads lexicographically by type id. 00161 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { 00162 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 00163 unsigned LSize = LIds.size(), RSize = RIds.size(); 00164 unsigned MinSize = LSize < RSize ? LSize : RSize; 00165 00166 for (unsigned i = 0; i != MinSize; ++i) 00167 if (LIds[i] != RIds[i]) 00168 return LIds[i] < RIds[i]; 00169 00170 return LSize < RSize; 00171 } 00172 00173 namespace { 00174 00175 struct KeyInfo { 00176 static inline unsigned getEmptyKey() { return -1U; } 00177 static inline unsigned getTombstoneKey() { return -2U; } 00178 static unsigned getHashValue(const unsigned &Key) { return Key; } 00179 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; } 00180 static bool isPod() { return true; } 00181 }; 00182 00183 /// ActionEntry - Structure describing an entry in the actions table. 00184 struct ActionEntry { 00185 int ValueForTypeID; // The value to write - may not be equal to the type id. 00186 int NextAction; 00187 struct ActionEntry *Previous; 00188 }; 00189 00190 /// PadRange - Structure holding a try-range and the associated landing pad. 00191 struct PadRange { 00192 // The index of the landing pad. 00193 unsigned PadIndex; 00194 // The index of the begin and end labels in the landing pad's label lists. 00195 unsigned RangeIndex; 00196 }; 00197 00198 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType; 00199 00200 /// CallSiteEntry - Structure describing an entry in the call-site table. 00201 struct CallSiteEntry { 00202 unsigned BeginLabel; // zero indicates the start of the function. 00203 unsigned EndLabel; // zero indicates the end of the function. 00204 unsigned PadLabel; // zero indicates that there is no landing pad. 00205 unsigned Action; 00206 }; 00207 00208 } 00209 00210 unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF, 00211 unsigned char* StartFunction, 00212 unsigned char* EndFunction) const { 00213 // Map all labels and get rid of any dead landing pads. 00214 MMI->TidyLandingPads(); 00215 00216 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); 00217 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 00218 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); 00219 if (PadInfos.empty()) return 0; 00220 00221 // Sort the landing pads in order of their type ids. This is used to fold 00222 // duplicate actions. 00223 SmallVector<const LandingPadInfo *, 64> LandingPads; 00224 LandingPads.reserve(PadInfos.size()); 00225 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 00226 LandingPads.push_back(&PadInfos[i]); 00227 std::sort(LandingPads.begin(), LandingPads.end(), PadLT); 00228 00229 // Negative type ids index into FilterIds, positive type ids index into 00230 // TypeInfos. The value written for a positive type id is just the type 00231 // id itself. For a negative type id, however, the value written is the 00232 // (negative) byte offset of the corresponding FilterIds entry. The byte 00233 // offset is usually equal to the type id, because the FilterIds entries 00234 // are written using a variable width encoding which outputs one byte per 00235 // entry as long as the value written is not too large, but can differ. 00236 // This kind of complication does not occur for positive type ids because 00237 // type infos are output using a fixed width encoding. 00238 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i]. 00239 SmallVector<int, 16> FilterOffsets; 00240 FilterOffsets.reserve(FilterIds.size()); 00241 int Offset = -1; 00242 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(), 00243 E = FilterIds.end(); I != E; ++I) { 00244 FilterOffsets.push_back(Offset); 00245 Offset -= TargetAsmInfo::getULEB128Size(*I); 00246 } 00247 00248 // Compute the actions table and gather the first action index for each 00249 // landing pad site. 00250 SmallVector<ActionEntry, 32> Actions; 00251 SmallVector<unsigned, 64> FirstActions; 00252 FirstActions.reserve(LandingPads.size()); 00253 00254 int FirstAction = 0; 00255 unsigned SizeActions = 0; 00256 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 00257 const LandingPadInfo *LP = LandingPads[i]; 00258 const std::vector<int> &TypeIds = LP->TypeIds; 00259 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0; 00260 unsigned SizeSiteActions = 0; 00261 00262 if (NumShared < TypeIds.size()) { 00263 unsigned SizeAction = 0; 00264 ActionEntry *PrevAction = 0; 00265 00266 if (NumShared) { 00267 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size(); 00268 assert(Actions.size()); 00269 PrevAction = &Actions.back(); 00270 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) + 00271 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 00272 for (unsigned j = NumShared; j != SizePrevIds; ++j) { 00273 SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 00274 SizeAction += -PrevAction->NextAction; 00275 PrevAction = PrevAction->Previous; 00276 } 00277 } 00278 00279 // Compute the actions. 00280 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) { 00281 int TypeID = TypeIds[I]; 00282 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 00283 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; 00284 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID); 00285 00286 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; 00287 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction); 00288 SizeSiteActions += SizeAction; 00289 00290 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction}; 00291 Actions.push_back(Action); 00292 00293 PrevAction = &Actions.back(); 00294 } 00295 00296 // Record the first action of the landing pad site. 00297 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; 00298 } // else identical - re-use previous FirstAction 00299 00300 FirstActions.push_back(FirstAction); 00301 00302 // Compute this sites contribution to size. 00303 SizeActions += SizeSiteActions; 00304 } 00305 00306 // Compute the call-site table. Entries must be ordered by address. 00307 SmallVector<CallSiteEntry, 64> CallSites; 00308 00309 RangeMapType PadMap; 00310 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 00311 const LandingPadInfo *LandingPad = LandingPads[i]; 00312 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 00313 unsigned BeginLabel = LandingPad->BeginLabels[j]; 00314 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 00315 PadRange P = { i, j }; 00316 PadMap[BeginLabel] = P; 00317 } 00318 } 00319 00320 bool MayThrow = false; 00321 unsigned LastLabel = 0; 00322 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 00323 I != E; ++I) { 00324 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); 00325 MI != E; ++MI) { 00326 if (!MI->isLabel()) { 00327 MayThrow |= MI->getDesc().isCall(); 00328 continue; 00329 } 00330 00331 unsigned BeginLabel = MI->getOperand(0).getImm(); 00332 assert(BeginLabel && "Invalid label!"); 00333 00334 if (BeginLabel == LastLabel) 00335 MayThrow = false; 00336 00337 RangeMapType::iterator L = PadMap.find(BeginLabel); 00338 00339 if (L == PadMap.end()) 00340 continue; 00341 00342 PadRange P = L->second; 00343 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 00344 00345 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 00346 "Inconsistent landing pad map!"); 00347 00348 // If some instruction between the previous try-range and this one may 00349 // throw, create a call-site entry with no landing pad for the region 00350 // between the try-ranges. 00351 if (MayThrow) { 00352 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0}; 00353 CallSites.push_back(Site); 00354 } 00355 00356 LastLabel = LandingPad->EndLabels[P.RangeIndex]; 00357 CallSiteEntry Site = {BeginLabel, LastLabel, 00358 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]}; 00359 00360 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel && 00361 "Invalid landing pad!"); 00362 00363 // Try to merge with the previous call-site. 00364 if (CallSites.size()) { 00365 CallSiteEntry &Prev = CallSites.back(); 00366 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { 00367 // Extend the range of the previous entry. 00368 Prev.EndLabel = Site.EndLabel; 00369 continue; 00370 } 00371 } 00372 00373 // Otherwise, create a new call-site. 00374 CallSites.push_back(Site); 00375 } 00376 } 00377 // If some instruction between the previous try-range and the end of the 00378 // function may throw, create a call-site entry with no landing pad for the 00379 // region following the try-range. 00380 if (MayThrow) { 00381 CallSiteEntry Site = {LastLabel, 0, 0, 0}; 00382 CallSites.push_back(Site); 00383 } 00384 00385 // Final tallies. 00386 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start. 00387 sizeof(int32_t) + // Site length. 00388 sizeof(int32_t)); // Landing pad. 00389 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) 00390 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action); 00391 00392 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize(); 00393 00394 unsigned TypeOffset = sizeof(int8_t) + // Call site format 00395 // Call-site table length 00396 TargetAsmInfo::getULEB128Size(SizeSites) + 00397 SizeSites + SizeActions + SizeTypes; 00398 00399 // Begin the exception table. 00400 JCE->emitAlignmentWithFill(4, 0); 00401 // Asm->EOL("Padding"); 00402 00403 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue(); 00404 00405 // Emit the header. 00406 JCE->emitByte(dwarf::DW_EH_PE_omit); 00407 // Asm->EOL("LPStart format (DW_EH_PE_omit)"); 00408 JCE->emitByte(dwarf::DW_EH_PE_absptr); 00409 // Asm->EOL("TType format (DW_EH_PE_absptr)"); 00410 JCE->emitULEB128Bytes(TypeOffset); 00411 // Asm->EOL("TType base offset"); 00412 JCE->emitByte(dwarf::DW_EH_PE_udata4); 00413 // Asm->EOL("Call site format (DW_EH_PE_udata4)"); 00414 JCE->emitULEB128Bytes(SizeSites); 00415 // Asm->EOL("Call-site table length"); 00416 00417 // Emit the landing pad site information. 00418 for (unsigned i = 0; i < CallSites.size(); ++i) { 00419 CallSiteEntry &S = CallSites[i]; 00420 intptr_t BeginLabelPtr = 0; 00421 intptr_t EndLabelPtr = 0; 00422 00423 if (!S.BeginLabel) { 00424 BeginLabelPtr = (intptr_t)StartFunction; 00425 JCE->emitInt32(0); 00426 } else { 00427 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel); 00428 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction); 00429 } 00430 00431 // Asm->EOL("Region start"); 00432 00433 if (!S.EndLabel) { 00434 EndLabelPtr = (intptr_t)EndFunction; 00435 JCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr); 00436 } else { 00437 EndLabelPtr = JCE->getLabelAddress(S.EndLabel); 00438 JCE->emitInt32(EndLabelPtr - BeginLabelPtr); 00439 } 00440 //Asm->EOL("Region length"); 00441 00442 if (!S.PadLabel) { 00443 JCE->emitInt32(0); 00444 } else { 00445 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel); 00446 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction); 00447 } 00448 // Asm->EOL("Landing pad"); 00449 00450 JCE->emitULEB128Bytes(S.Action); 00451 // Asm->EOL("Action"); 00452 } 00453 00454 // Emit the actions. 00455 for (unsigned I = 0, N = Actions.size(); I != N; ++I) { 00456 ActionEntry &Action = Actions[I]; 00457 00458 JCE->emitSLEB128Bytes(Action.ValueForTypeID); 00459 //Asm->EOL("TypeInfo index"); 00460 JCE->emitSLEB128Bytes(Action.NextAction); 00461 //Asm->EOL("Next action"); 00462 } 00463 00464 // Emit the type ids. 00465 for (unsigned M = TypeInfos.size(); M; --M) { 00466 GlobalVariable *GV = TypeInfos[M - 1]; 00467 00468 if (GV) { 00469 if (TD->getPointerSize() == sizeof(int32_t)) { 00470 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV)); 00471 } else { 00472 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV)); 00473 } 00474 } else { 00475 if (TD->getPointerSize() == sizeof(int32_t)) 00476 JCE->emitInt32(0); 00477 else 00478 JCE->emitInt64(0); 00479 } 00480 // Asm->EOL("TypeInfo"); 00481 } 00482 00483 // Emit the filter typeids. 00484 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) { 00485 unsigned TypeID = FilterIds[j]; 00486 JCE->emitULEB128Bytes(TypeID); 00487 //Asm->EOL("Filter TypeInfo index"); 00488 } 00489 00490 JCE->emitAlignmentWithFill(4, 0); 00491 00492 return DwarfExceptionTable; 00493 } 00494 00495 unsigned char* 00496 JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const { 00497 unsigned PointerSize = TD->getPointerSize(); 00498 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? 00499 PointerSize : -PointerSize; 00500 00501 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue(); 00502 // EH Common Frame header 00503 JCE->allocateSpace(4, 0); 00504 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue(); 00505 JCE->emitInt32((int)0); 00506 JCE->emitByte(dwarf::DW_CIE_VERSION); 00507 JCE->emitString(Personality ? "zPLR" : "zR"); 00508 JCE->emitULEB128Bytes(1); 00509 JCE->emitSLEB128Bytes(stackGrowth); 00510 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true)); 00511 00512 if (Personality) { 00513 // Augmentation Size: 3 small ULEBs of one byte each, and the personality 00514 // function which size is PointerSize. 00515 JCE->emitULEB128Bytes(3 + PointerSize); 00516 00517 // We set the encoding of the personality as direct encoding because we use 00518 // the function pointer. The encoding is not relative because the current 00519 // PC value may be bigger than the personality function pointer. 00520 if (PointerSize == 4) { 00521 JCE->emitByte(dwarf::DW_EH_PE_sdata4); 00522 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality))); 00523 } else { 00524 JCE->emitByte(dwarf::DW_EH_PE_sdata8); 00525 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality))); 00526 } 00527 00528 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); 00529 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); 00530 00531 } else { 00532 JCE->emitULEB128Bytes(1); 00533 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); 00534 } 00535 00536 std::vector<MachineMove> Moves; 00537 RI->getInitialFrameState(Moves); 00538 EmitFrameMoves(0, Moves); 00539 00540 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop); 00541 00542 JCE->emitInt32At((uintptr_t*)StartCommonPtr, 00543 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() - 00544 FrameCommonBeginPtr)); 00545 00546 return StartCommonPtr; 00547 } 00548 00549 00550 unsigned char* 00551 JITDwarfEmitter::EmitEHFrame(const Function* Personality, 00552 unsigned char* StartCommonPtr, 00553 unsigned char* StartFunction, 00554 unsigned char* EndFunction, 00555 unsigned char* ExceptionTable) const { 00556 unsigned PointerSize = TD->getPointerSize(); 00557 00558 // EH frame header. 00559 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue(); 00560 JCE->allocateSpace(4, 0); 00561 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue(); 00562 // FDE CIE Offset 00563 JCE->emitInt32(FrameBeginPtr - StartCommonPtr); 00564 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue()); 00565 JCE->emitInt32(EndFunction - StartFunction); 00566 00567 // If there is a personality and landing pads then point to the language 00568 // specific data area in the exception table. 00569 if (MMI->getPersonalityIndex()) { 00570 JCE->emitULEB128Bytes(4); 00571 00572 if (!MMI->getLandingPads().empty()) { 00573 JCE->emitInt32(ExceptionTable - (unsigned char*)JCE->getCurrentPCValue()); 00574 } else { 00575 JCE->emitInt32((int)0); 00576 } 00577 } else { 00578 JCE->emitULEB128Bytes(0); 00579 } 00580 00581 // Indicate locations of function specific callee saved registers in 00582 // frame. 00583 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves()); 00584 00585 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop); 00586 00587 // Indicate the size of the table 00588 JCE->emitInt32At((uintptr_t*)StartEHPtr, 00589 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() - 00590 StartEHPtr)); 00591 00592 // Double zeroes for the unwind runtime 00593 if (PointerSize == 8) { 00594 JCE->emitInt64(0); 00595 JCE->emitInt64(0); 00596 } else { 00597 JCE->emitInt32(0); 00598 JCE->emitInt32(0); 00599 } 00600 00601 return StartEHPtr; 00602 } 00603 00604 unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F, 00605 JITCodeEmitter& jce, 00606 unsigned char* StartFunction, 00607 unsigned char* EndFunction) { 00608 const TargetMachine& TM = F.getTarget(); 00609 TD = TM.getTargetData(); 00610 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding(); 00611 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection(); 00612 RI = TM.getRegisterInfo(); 00613 JCE = &jce; 00614 unsigned FinalSize = 0; 00615 00616 FinalSize += GetExceptionTableSizeInBytes(&F); 00617 00618 const std::vector<Function *> Personalities = MMI->getPersonalities(); 00619 FinalSize += 00620 GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]); 00621 00622 FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()], 00623 StartFunction); 00624 00625 return FinalSize; 00626 } 00627 00628 /// RoundUpToAlign - Add the specified alignment to FinalSize and returns 00629 /// the new value. 00630 static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) { 00631 if (Alignment == 0) Alignment = 1; 00632 // Since we do not know where the buffer will be allocated, be pessimistic. 00633 return FinalSize + Alignment; 00634 } 00635 00636 unsigned 00637 JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality, 00638 unsigned char* StartFunction) const { 00639 unsigned PointerSize = TD->getPointerSize(); 00640 unsigned FinalSize = 0; 00641 // EH frame header. 00642 FinalSize += PointerSize; 00643 // FDE CIE Offset 00644 FinalSize += 3 * PointerSize; 00645 // If there is a personality and landing pads then point to the language 00646 // specific data area in the exception table. 00647 if (MMI->getPersonalityIndex()) { 00648 FinalSize += TargetAsmInfo::getULEB128Size(4); 00649 FinalSize += PointerSize; 00650 } else { 00651 FinalSize += TargetAsmInfo::getULEB128Size(0); 00652 } 00653 00654 // Indicate locations of function specific callee saved registers in 00655 // frame. 00656 FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction, 00657 MMI->getFrameMoves()); 00658 00659 FinalSize = RoundUpToAlign(FinalSize, 4); 00660 00661 // Double zeroes for the unwind runtime 00662 FinalSize += 2 * PointerSize; 00663 00664 return FinalSize; 00665 } 00666 00667 unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality) 00668 const { 00669 00670 unsigned PointerSize = TD->getPointerSize(); 00671 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? 00672 PointerSize : -PointerSize; 00673 unsigned FinalSize = 0; 00674 // EH Common Frame header 00675 FinalSize += PointerSize; 00676 FinalSize += 4; 00677 FinalSize += 1; 00678 FinalSize += Personality ? 5 : 3; // "zPLR" or "zR" 00679 FinalSize += TargetAsmInfo::getULEB128Size(1); 00680 FinalSize += TargetAsmInfo::getSLEB128Size(stackGrowth); 00681 FinalSize += 1; 00682 00683 if (Personality) { 00684 FinalSize += TargetAsmInfo::getULEB128Size(7); 00685 00686 // Encoding 00687 FinalSize+= 1; 00688 //Personality 00689 FinalSize += PointerSize; 00690 00691 FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel); 00692 FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel); 00693 00694 } else { 00695 FinalSize += TargetAsmInfo::getULEB128Size(1); 00696 FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel); 00697 } 00698 00699 std::vector<MachineMove> Moves; 00700 RI->getInitialFrameState(Moves); 00701 FinalSize += GetFrameMovesSizeInBytes(0, Moves); 00702 FinalSize = RoundUpToAlign(FinalSize, 4); 00703 return FinalSize; 00704 } 00705 00706 unsigned 00707 JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr, 00708 const std::vector<MachineMove> &Moves) const { 00709 unsigned PointerSize = TD->getPointerSize(); 00710 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? 00711 PointerSize : -PointerSize; 00712 bool IsLocal = BaseLabelPtr; 00713 unsigned FinalSize = 0; 00714 00715 for (unsigned i = 0, N = Moves.size(); i < N; ++i) { 00716 const MachineMove &Move = Moves[i]; 00717 unsigned LabelID = Move.getLabelID(); 00718 00719 if (LabelID) { 00720 LabelID = MMI->MappedLabel(LabelID); 00721 00722 // Throw out move if the label is invalid. 00723 if (!LabelID) continue; 00724 } 00725 00726 intptr_t LabelPtr = 0; 00727 if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID); 00728 00729 const MachineLocation &Dst = Move.getDestination(); 00730 const MachineLocation &Src = Move.getSource(); 00731 00732 // Advance row if new location. 00733 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) { 00734 FinalSize++; 00735 FinalSize += PointerSize; 00736 BaseLabelPtr = LabelPtr; 00737 IsLocal = true; 00738 } 00739 00740 // If advancing cfa. 00741 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { 00742 if (!Src.isReg()) { 00743 if (Src.getReg() == MachineLocation::VirtualFP) { 00744 ++FinalSize; 00745 } else { 00746 ++FinalSize; 00747 unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true); 00748 FinalSize += TargetAsmInfo::getULEB128Size(RegNum); 00749 } 00750 00751 int Offset = -Src.getOffset(); 00752 00753 FinalSize += TargetAsmInfo::getULEB128Size(Offset); 00754 } else { 00755 llvm_unreachable("Machine move no supported yet."); 00756 } 00757 } else if (Src.isReg() && 00758 Src.getReg() == MachineLocation::VirtualFP) { 00759 if (Dst.isReg()) { 00760 ++FinalSize; 00761 unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true); 00762 FinalSize += TargetAsmInfo::getULEB128Size(RegNum); 00763 } else { 00764 llvm_unreachable("Machine move no supported yet."); 00765 } 00766 } else { 00767 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true); 00768 int Offset = Dst.getOffset() / stackGrowth; 00769 00770 if (Offset < 0) { 00771 ++FinalSize; 00772 FinalSize += TargetAsmInfo::getULEB128Size(Reg); 00773 FinalSize += TargetAsmInfo::getSLEB128Size(Offset); 00774 } else if (Reg < 64) { 00775 ++FinalSize; 00776 FinalSize += TargetAsmInfo::getULEB128Size(Offset); 00777 } else { 00778 ++FinalSize; 00779 FinalSize += TargetAsmInfo::getULEB128Size(Reg); 00780 FinalSize += TargetAsmInfo::getULEB128Size(Offset); 00781 } 00782 } 00783 } 00784 return FinalSize; 00785 } 00786 00787 unsigned 00788 JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const { 00789 unsigned FinalSize = 0; 00790 00791 // Map all labels and get rid of any dead landing pads. 00792 MMI->TidyLandingPads(); 00793 00794 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); 00795 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 00796 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); 00797 if (PadInfos.empty()) return 0; 00798 00799 // Sort the landing pads in order of their type ids. This is used to fold 00800 // duplicate actions. 00801 SmallVector<const LandingPadInfo *, 64> LandingPads; 00802 LandingPads.reserve(PadInfos.size()); 00803 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 00804 LandingPads.push_back(&PadInfos[i]); 00805 std::sort(LandingPads.begin(), LandingPads.end(), PadLT); 00806 00807 // Negative type ids index into FilterIds, positive type ids index into 00808 // TypeInfos. The value written for a positive type id is just the type 00809 // id itself. For a negative type id, however, the value written is the 00810 // (negative) byte offset of the corresponding FilterIds entry. The byte 00811 // offset is usually equal to the type id, because the FilterIds entries 00812 // are written using a variable width encoding which outputs one byte per 00813 // entry as long as the value written is not too large, but can differ. 00814 // This kind of complication does not occur for positive type ids because 00815 // type infos are output using a fixed width encoding. 00816 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i]. 00817 SmallVector<int, 16> FilterOffsets; 00818 FilterOffsets.reserve(FilterIds.size()); 00819 int Offset = -1; 00820 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(), 00821 E = FilterIds.end(); I != E; ++I) { 00822 FilterOffsets.push_back(Offset); 00823 Offset -= TargetAsmInfo::getULEB128Size(*I); 00824 } 00825 00826 // Compute the actions table and gather the first action index for each 00827 // landing pad site. 00828 SmallVector<ActionEntry, 32> Actions; 00829 SmallVector<unsigned, 64> FirstActions; 00830 FirstActions.reserve(LandingPads.size()); 00831 00832 int FirstAction = 0; 00833 unsigned SizeActions = 0; 00834 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 00835 const LandingPadInfo *LP = LandingPads[i]; 00836 const std::vector<int> &TypeIds = LP->TypeIds; 00837 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0; 00838 unsigned SizeSiteActions = 0; 00839 00840 if (NumShared < TypeIds.size()) { 00841 unsigned SizeAction = 0; 00842 ActionEntry *PrevAction = 0; 00843 00844 if (NumShared) { 00845 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size(); 00846 assert(Actions.size()); 00847 PrevAction = &Actions.back(); 00848 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) + 00849 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 00850 for (unsigned j = NumShared; j != SizePrevIds; ++j) { 00851 SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); 00852 SizeAction += -PrevAction->NextAction; 00853 PrevAction = PrevAction->Previous; 00854 } 00855 } 00856 00857 // Compute the actions. 00858 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) { 00859 int TypeID = TypeIds[I]; 00860 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 00861 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; 00862 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID); 00863 00864 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; 00865 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction); 00866 SizeSiteActions += SizeAction; 00867 00868 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction}; 00869 Actions.push_back(Action); 00870 00871 PrevAction = &Actions.back(); 00872 } 00873 00874 // Record the first action of the landing pad site. 00875 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; 00876 } // else identical - re-use previous FirstAction 00877 00878 FirstActions.push_back(FirstAction); 00879 00880 // Compute this sites contribution to size. 00881 SizeActions += SizeSiteActions; 00882 } 00883 00884 // Compute the call-site table. Entries must be ordered by address. 00885 SmallVector<CallSiteEntry, 64> CallSites; 00886 00887 RangeMapType PadMap; 00888 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 00889 const LandingPadInfo *LandingPad = LandingPads[i]; 00890 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 00891 unsigned BeginLabel = LandingPad->BeginLabels[j]; 00892 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 00893 PadRange P = { i, j }; 00894 PadMap[BeginLabel] = P; 00895 } 00896 } 00897 00898 bool MayThrow = false; 00899 unsigned LastLabel = 0; 00900 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 00901 I != E; ++I) { 00902 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); 00903 MI != E; ++MI) { 00904 if (!MI->isLabel()) { 00905 MayThrow |= MI->getDesc().isCall(); 00906 continue; 00907 } 00908 00909 unsigned BeginLabel = MI->getOperand(0).getImm(); 00910 assert(BeginLabel && "Invalid label!"); 00911 00912 if (BeginLabel == LastLabel) 00913 MayThrow = false; 00914 00915 RangeMapType::iterator L = PadMap.find(BeginLabel); 00916 00917 if (L == PadMap.end()) 00918 continue; 00919 00920 PadRange P = L->second; 00921 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 00922 00923 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 00924 "Inconsistent landing pad map!"); 00925 00926 // If some instruction between the previous try-range and this one may 00927 // throw, create a call-site entry with no landing pad for the region 00928 // between the try-ranges. 00929 if (MayThrow) { 00930 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0}; 00931 CallSites.push_back(Site); 00932 } 00933 00934 LastLabel = LandingPad->EndLabels[P.RangeIndex]; 00935 CallSiteEntry Site = {BeginLabel, LastLabel, 00936 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]}; 00937 00938 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel && 00939 "Invalid landing pad!"); 00940 00941 // Try to merge with the previous call-site. 00942 if (CallSites.size()) { 00943 CallSiteEntry &Prev = CallSites.back(); 00944 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { 00945 // Extend the range of the previous entry. 00946 Prev.EndLabel = Site.EndLabel; 00947 continue; 00948 } 00949 } 00950 00951 // Otherwise, create a new call-site. 00952 CallSites.push_back(Site); 00953 } 00954 } 00955 // If some instruction between the previous try-range and the end of the 00956 // function may throw, create a call-site entry with no landing pad for the 00957 // region following the try-range. 00958 if (MayThrow) { 00959 CallSiteEntry Site = {LastLabel, 0, 0, 0}; 00960 CallSites.push_back(Site); 00961 } 00962 00963 // Final tallies. 00964 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start. 00965 sizeof(int32_t) + // Site length. 00966 sizeof(int32_t)); // Landing pad. 00967 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) 00968 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action); 00969 00970 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize(); 00971 00972 unsigned TypeOffset = sizeof(int8_t) + // Call site format 00973 // Call-site table length 00974 TargetAsmInfo::getULEB128Size(SizeSites) + 00975 SizeSites + SizeActions + SizeTypes; 00976 00977 unsigned TotalSize = sizeof(int8_t) + // LPStart format 00978 sizeof(int8_t) + // TType format 00979 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset 00980 TypeOffset; 00981 00982 unsigned SizeAlign = (4 - TotalSize) & 3; 00983 00984 // Begin the exception table. 00985 FinalSize = RoundUpToAlign(FinalSize, 4); 00986 for (unsigned i = 0; i != SizeAlign; ++i) { 00987 ++FinalSize; 00988 } 00989 00990 unsigned PointerSize = TD->getPointerSize(); 00991 00992 // Emit the header. 00993 ++FinalSize; 00994 // Asm->EOL("LPStart format (DW_EH_PE_omit)"); 00995 ++FinalSize; 00996 // Asm->EOL("TType format (DW_EH_PE_absptr)"); 00997 ++FinalSize; 00998 // Asm->EOL("TType base offset"); 00999 ++FinalSize; 01000 // Asm->EOL("Call site format (DW_EH_PE_udata4)"); 01001 ++FinalSize; 01002 // Asm->EOL("Call-site table length"); 01003 01004 // Emit the landing pad site information. 01005 for (unsigned i = 0; i < CallSites.size(); ++i) { 01006 CallSiteEntry &S = CallSites[i]; 01007 01008 // Asm->EOL("Region start"); 01009 FinalSize += PointerSize; 01010 01011 //Asm->EOL("Region length"); 01012 FinalSize += PointerSize; 01013 01014 // Asm->EOL("Landing pad"); 01015 FinalSize += PointerSize; 01016 01017 FinalSize += TargetAsmInfo::getULEB128Size(S.Action); 01018 // Asm->EOL("Action"); 01019 } 01020 01021 // Emit the actions. 01022 for (unsigned I = 0, N = Actions.size(); I != N; ++I) { 01023 ActionEntry &Action = Actions[I]; 01024 01025 //Asm->EOL("TypeInfo index"); 01026 FinalSize += TargetAsmInfo::getSLEB128Size(Action.ValueForTypeID); 01027 //Asm->EOL("Next action"); 01028 FinalSize += TargetAsmInfo::getSLEB128Size(Action.NextAction); 01029 } 01030 01031 // Emit the type ids. 01032 for (unsigned M = TypeInfos.size(); M; --M) { 01033 // Asm->EOL("TypeInfo"); 01034 FinalSize += PointerSize; 01035 } 01036 01037 // Emit the filter typeids. 01038 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) { 01039 unsigned TypeID = FilterIds[j]; 01040 FinalSize += TargetAsmInfo::getULEB128Size(TypeID); 01041 //Asm->EOL("Filter TypeInfo index"); 01042 } 01043 01044 FinalSize = RoundUpToAlign(FinalSize, 4); 01045 01046 return FinalSize; 01047 }