LLVM API Documentation
00001 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===// 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 implements the helper classes used to build and interpret debug 00011 // information in LLVM IR form. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Analysis/DebugInfo.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/DerivedTypes.h" 00018 #include "llvm/Intrinsics.h" 00019 #include "llvm/IntrinsicInst.h" 00020 #include "llvm/Instructions.h" 00021 #include "llvm/Module.h" 00022 #include "llvm/Analysis/ValueTracking.h" 00023 #include "llvm/Support/Streams.h" 00024 using namespace llvm; 00025 00026 //===----------------------------------------------------------------------===// 00027 // DIDescriptor 00028 //===----------------------------------------------------------------------===// 00029 00030 DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) { 00031 GV = gv; 00032 00033 // If this is non-null, check to see if the Tag matches. If not, set to null. 00034 if (GV && getTag() != RequiredTag) 00035 GV = 0; 00036 } 00037 00038 00039 std::string DIDescriptor::getStringField(unsigned Elt) const { 00040 if (GV == 0) return ""; 00041 Constant *C = GV->getInitializer(); 00042 if (C == 0 || Elt >= C->getNumOperands()) 00043 return ""; 00044 00045 std::string Result; 00046 // Fills in the string if it succeeds 00047 if (!GetConstantStringInfo(C->getOperand(Elt), Result)) 00048 Result.clear(); 00049 return Result; 00050 } 00051 00052 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { 00053 if (GV == 0) return 0; 00054 Constant *C = GV->getInitializer(); 00055 if (C == 0 || Elt >= C->getNumOperands()) 00056 return 0; 00057 if (ConstantInt *CI = dyn_cast<ConstantInt>(C->getOperand(Elt))) 00058 return CI->getZExtValue(); 00059 return 0; 00060 } 00061 00062 00063 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { 00064 if (GV == 0) return DIDescriptor(); 00065 Constant *C = GV->getInitializer(); 00066 if (C == 0 || Elt >= C->getNumOperands()) 00067 return DIDescriptor(); 00068 C = C->getOperand(Elt); 00069 return DIDescriptor(dyn_cast<GlobalVariable>(C->stripPointerCasts())); 00070 } 00071 00072 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { 00073 if (GV == 0) return 0; 00074 Constant *C = GV->getInitializer(); 00075 if (C == 0 || Elt >= C->getNumOperands()) 00076 return 0; 00077 C = C->getOperand(Elt); 00078 00079 return dyn_cast<GlobalVariable>(C->stripPointerCasts()); 00080 } 00081 00082 00083 00084 //===----------------------------------------------------------------------===// 00085 // Simple Descriptor Constructors and other Methods 00086 //===----------------------------------------------------------------------===// 00087 00088 DIAnchor::DIAnchor(GlobalVariable *GV) 00089 : DIDescriptor(GV, dwarf::DW_TAG_anchor) {} 00090 DIEnumerator::DIEnumerator(GlobalVariable *GV) 00091 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {} 00092 DISubrange::DISubrange(GlobalVariable *GV) 00093 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {} 00094 DICompileUnit::DICompileUnit(GlobalVariable *GV) 00095 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {} 00096 DIBasicType::DIBasicType(GlobalVariable *GV) 00097 : DIType(GV, dwarf::DW_TAG_base_type) {} 00098 DISubprogram::DISubprogram(GlobalVariable *GV) 00099 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {} 00100 DIGlobalVariable::DIGlobalVariable(GlobalVariable *GV) 00101 : DIGlobal(GV, dwarf::DW_TAG_variable) {} 00102 DIBlock::DIBlock(GlobalVariable *GV) 00103 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {} 00104 // needed by DIVariable::getType() 00105 DIType::DIType(GlobalVariable *gv) : DIDescriptor(gv) { 00106 if (!gv) return; 00107 unsigned tag = getTag(); 00108 if (tag != dwarf::DW_TAG_base_type && !DIDerivedType::isDerivedType(tag) && 00109 !DICompositeType::isCompositeType(tag)) 00110 GV = 0; 00111 } 00112 00113 /// isDerivedType - Return true if the specified tag is legal for 00114 /// DIDerivedType. 00115 bool DIType::isDerivedType(unsigned Tag) { 00116 switch (Tag) { 00117 case dwarf::DW_TAG_typedef: 00118 case dwarf::DW_TAG_pointer_type: 00119 case dwarf::DW_TAG_reference_type: 00120 case dwarf::DW_TAG_const_type: 00121 case dwarf::DW_TAG_volatile_type: 00122 case dwarf::DW_TAG_restrict_type: 00123 case dwarf::DW_TAG_member: 00124 case dwarf::DW_TAG_inheritance: 00125 return true; 00126 default: 00127 // FIXME: Even though it doesn't make sense, CompositeTypes are current 00128 // modelled as DerivedTypes, this should return true for them as well. 00129 return false; 00130 } 00131 } 00132 00133 DIDerivedType::DIDerivedType(GlobalVariable *GV) : DIType(GV, true, true) { 00134 if (GV && !isDerivedType(getTag())) 00135 GV = 0; 00136 } 00137 00138 /// isCompositeType - Return true if the specified tag is legal for 00139 /// DICompositeType. 00140 bool DIType::isCompositeType(unsigned TAG) { 00141 switch (TAG) { 00142 case dwarf::DW_TAG_array_type: 00143 case dwarf::DW_TAG_structure_type: 00144 case dwarf::DW_TAG_union_type: 00145 case dwarf::DW_TAG_enumeration_type: 00146 case dwarf::DW_TAG_vector_type: 00147 case dwarf::DW_TAG_subroutine_type: 00148 return true; 00149 default: 00150 return false; 00151 } 00152 } 00153 00154 DICompositeType::DICompositeType(GlobalVariable *GV) 00155 : DIDerivedType(GV, true, true) { 00156 if (GV && !isCompositeType(getTag())) 00157 GV = 0; 00158 } 00159 00160 /// isVariable - Return true if the specified tag is legal for DIVariable. 00161 bool DIVariable::isVariable(unsigned Tag) { 00162 switch (Tag) { 00163 case dwarf::DW_TAG_auto_variable: 00164 case dwarf::DW_TAG_arg_variable: 00165 case dwarf::DW_TAG_return_variable: 00166 return true; 00167 default: 00168 return false; 00169 } 00170 } 00171 00172 DIVariable::DIVariable(GlobalVariable *GV) : DIDescriptor(GV) { 00173 if (GV && !isVariable(getTag())) 00174 GV = 0; 00175 } 00176 00177 unsigned DIArray::getNumElements() const { 00178 assert (GV && "Invalid DIArray"); 00179 Constant *C = GV->getInitializer(); 00180 assert (C && "Invalid DIArray initializer"); 00181 return C->getNumOperands(); 00182 } 00183 00184 /// Verify - Verify that a compile unit is well formed. 00185 bool DICompileUnit::Verify() const { 00186 if (isNull()) 00187 return false; 00188 if (getFilename().empty()) 00189 return false; 00190 // It is possible that directory and produce string is empty. 00191 return true; 00192 } 00193 00194 /// Verify - Verify that a type descriptor is well formed. 00195 bool DIType::Verify() const { 00196 if (isNull()) 00197 return false; 00198 if (getContext().isNull()) 00199 return false; 00200 00201 DICompileUnit CU = getCompileUnit(); 00202 if (!CU.isNull() && !CU.Verify()) 00203 return false; 00204 return true; 00205 } 00206 00207 /// Verify - Verify that a composite type descriptor is well formed. 00208 bool DICompositeType::Verify() const { 00209 if (isNull()) 00210 return false; 00211 if (getContext().isNull()) 00212 return false; 00213 00214 DICompileUnit CU = getCompileUnit(); 00215 if (!CU.isNull() && !CU.Verify()) 00216 return false; 00217 return true; 00218 } 00219 00220 /// Verify - Verify that a subprogram descriptor is well formed. 00221 bool DISubprogram::Verify() const { 00222 if (isNull()) 00223 return false; 00224 00225 if (getContext().isNull()) 00226 return false; 00227 00228 DICompileUnit CU = getCompileUnit(); 00229 if (!CU.Verify()) 00230 return false; 00231 00232 DICompositeType Ty = getType(); 00233 if (!Ty.isNull() && !Ty.Verify()) 00234 return false; 00235 return true; 00236 } 00237 00238 /// Verify - Verify that a global variable descriptor is well formed. 00239 bool DIGlobalVariable::Verify() const { 00240 if (isNull()) 00241 return false; 00242 00243 if (getContext().isNull()) 00244 return false; 00245 00246 DICompileUnit CU = getCompileUnit(); 00247 if (!CU.Verify()) 00248 return false; 00249 00250 DIType Ty = getType(); 00251 if (!Ty.Verify()) 00252 return false; 00253 00254 if (!getGlobal()) 00255 return false; 00256 00257 return true; 00258 } 00259 00260 /// Verify - Verify that a variable descriptor is well formed. 00261 bool DIVariable::Verify() const { 00262 if (isNull()) 00263 return false; 00264 00265 if (getContext().isNull()) 00266 return false; 00267 00268 DIType Ty = getType(); 00269 if (!Ty.Verify()) 00270 return false; 00271 00272 00273 return true; 00274 } 00275 00276 00277 00278 //===----------------------------------------------------------------------===// 00279 // DIFactory: Basic Helpers 00280 //===----------------------------------------------------------------------===// 00281 00282 DIFactory::DIFactory(Module &m) : M(m) { 00283 StopPointFn = FuncStartFn = RegionStartFn = RegionEndFn = DeclareFn = 0; 00284 EmptyStructPtr = PointerType::getUnqual(StructType::get(NULL, NULL)); 00285 } 00286 00287 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'. 00288 /// This is only valid when the descriptor is non-null. 00289 Constant *DIFactory::getCastToEmpty(DIDescriptor D) { 00290 if (D.isNull()) return Constant::getNullValue(EmptyStructPtr); 00291 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr); 00292 } 00293 00294 Constant *DIFactory::GetTagConstant(unsigned TAG) { 00295 assert((TAG & LLVMDebugVersionMask) == 0 && 00296 "Tag too large for debug encoding!"); 00297 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion); 00298 } 00299 00300 Constant *DIFactory::GetStringConstant(const std::string &String) { 00301 // Check string cache for previous edition. 00302 Constant *&Slot = StringCache[String]; 00303 00304 // Return Constant if previously defined. 00305 if (Slot) return Slot; 00306 00307 const PointerType *DestTy = PointerType::getUnqual(Type::Int8Ty); 00308 00309 // If empty string then use a sbyte* null instead. 00310 if (String.empty()) 00311 return Slot = ConstantPointerNull::get(DestTy); 00312 00313 // Construct string as an llvm constant. 00314 Constant *ConstStr = ConstantArray::get(String); 00315 00316 // Otherwise create and return a new string global. 00317 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, 00318 GlobalVariable::InternalLinkage, 00319 ConstStr, ".str", &M); 00320 StrGV->setSection("llvm.metadata"); 00321 return Slot = ConstantExpr::getBitCast(StrGV, DestTy); 00322 } 00323 00324 /// GetOrCreateAnchor - Look up an anchor for the specified tag and name. If it 00325 /// already exists, return it. If not, create a new one and return it. 00326 DIAnchor DIFactory::GetOrCreateAnchor(unsigned TAG, const char *Name) { 00327 const Type *EltTy = StructType::get(Type::Int32Ty, Type::Int32Ty, NULL); 00328 00329 // Otherwise, create the global or return it if already in the module. 00330 Constant *C = M.getOrInsertGlobal(Name, EltTy); 00331 assert(isa<GlobalVariable>(C) && "Incorrectly typed anchor?"); 00332 GlobalVariable *GV = cast<GlobalVariable>(C); 00333 00334 // If it has an initializer, it is already in the module. 00335 if (GV->hasInitializer()) 00336 return SubProgramAnchor = DIAnchor(GV); 00337 00338 GV->setLinkage(GlobalValue::LinkOnceLinkage); 00339 GV->setSection("llvm.metadata"); 00340 GV->setConstant(true); 00341 M.addTypeName("llvm.dbg.anchor.type", EltTy); 00342 00343 // Otherwise, set the initializer. 00344 Constant *Elts[] = { 00345 GetTagConstant(dwarf::DW_TAG_anchor), 00346 ConstantInt::get(Type::Int32Ty, TAG) 00347 }; 00348 00349 GV->setInitializer(ConstantStruct::get(Elts, 2)); 00350 return DIAnchor(GV); 00351 } 00352 00353 00354 00355 //===----------------------------------------------------------------------===// 00356 // DIFactory: Primary Constructors 00357 //===----------------------------------------------------------------------===// 00358 00359 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units, 00360 /// creating a new one if there isn't already one in the module. 00361 DIAnchor DIFactory::GetOrCreateCompileUnitAnchor() { 00362 // If we already created one, just return it. 00363 if (!CompileUnitAnchor.isNull()) 00364 return CompileUnitAnchor; 00365 return CompileUnitAnchor = GetOrCreateAnchor(dwarf::DW_TAG_compile_unit, 00366 "llvm.dbg.compile_units"); 00367 } 00368 00369 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms, 00370 /// creating a new one if there isn't already one in the module. 00371 DIAnchor DIFactory::GetOrCreateSubprogramAnchor() { 00372 // If we already created one, just return it. 00373 if (!SubProgramAnchor.isNull()) 00374 return SubProgramAnchor; 00375 return SubProgramAnchor = GetOrCreateAnchor(dwarf::DW_TAG_subprogram, 00376 "llvm.dbg.subprograms"); 00377 } 00378 00379 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals, 00380 /// creating a new one if there isn't already one in the module. 00381 DIAnchor DIFactory::GetOrCreateGlobalVariableAnchor() { 00382 // If we already created one, just return it. 00383 if (!GlobalVariableAnchor.isNull()) 00384 return GlobalVariableAnchor; 00385 return GlobalVariableAnchor = GetOrCreateAnchor(dwarf::DW_TAG_variable, 00386 "llvm.dbg.global_variables"); 00387 } 00388 00389 /// GetOrCreateArray - Create an descriptor for an array of descriptors. 00390 /// This implicitly uniques the arrays created. 00391 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) { 00392 SmallVector<Constant*, 16> Elts; 00393 00394 for (unsigned i = 0; i != NumTys; ++i) 00395 Elts.push_back(getCastToEmpty(Tys[i])); 00396 00397 Constant *Init = ConstantArray::get(ArrayType::get(EmptyStructPtr, 00398 Elts.size()), 00399 &Elts[0], Elts.size()); 00400 // If we already have this array, just return the uniqued version. 00401 DIDescriptor &Entry = SimpleConstantCache[Init]; 00402 if (!Entry.isNull()) return DIArray(Entry.getGV()); 00403 00404 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00405 GlobalValue::InternalLinkage, 00406 Init, "llvm.dbg.array", &M); 00407 GV->setSection("llvm.metadata"); 00408 Entry = DIDescriptor(GV); 00409 return DIArray(GV); 00410 } 00411 00412 /// GetOrCreateSubrange - Create a descriptor for a value range. This 00413 /// implicitly uniques the values returned. 00414 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) { 00415 Constant *Elts[] = { 00416 GetTagConstant(dwarf::DW_TAG_subrange_type), 00417 ConstantInt::get(Type::Int64Ty, Lo), 00418 ConstantInt::get(Type::Int64Ty, Hi) 00419 }; 00420 00421 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00422 00423 // If we already have this range, just return the uniqued version. 00424 DIDescriptor &Entry = SimpleConstantCache[Init]; 00425 if (!Entry.isNull()) return DISubrange(Entry.getGV()); 00426 00427 M.addTypeName("llvm.dbg.subrange.type", Init->getType()); 00428 00429 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00430 GlobalValue::InternalLinkage, 00431 Init, "llvm.dbg.subrange", &M); 00432 GV->setSection("llvm.metadata"); 00433 Entry = DIDescriptor(GV); 00434 return DISubrange(GV); 00435 } 00436 00437 00438 00439 /// CreateCompileUnit - Create a new descriptor for the specified compile 00440 /// unit. Note that this does not unique compile units within the module. 00441 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID, 00442 const std::string &Filename, 00443 const std::string &Directory, 00444 const std::string &Producer, 00445 bool isMain, 00446 bool isOptimized, 00447 const char *Flags) { 00448 Constant *Elts[] = { 00449 GetTagConstant(dwarf::DW_TAG_compile_unit), 00450 getCastToEmpty(GetOrCreateCompileUnitAnchor()), 00451 ConstantInt::get(Type::Int32Ty, LangID), 00452 GetStringConstant(Filename), 00453 GetStringConstant(Directory), 00454 GetStringConstant(Producer), 00455 ConstantInt::get(Type::Int1Ty, isMain), 00456 ConstantInt::get(Type::Int1Ty, isOptimized), 00457 GetStringConstant(Flags) 00458 }; 00459 00460 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00461 00462 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType()); 00463 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00464 GlobalValue::InternalLinkage, 00465 Init, "llvm.dbg.compile_unit", &M); 00466 GV->setSection("llvm.metadata"); 00467 return DICompileUnit(GV); 00468 } 00469 00470 /// CreateEnumerator - Create a single enumerator value. 00471 DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){ 00472 Constant *Elts[] = { 00473 GetTagConstant(dwarf::DW_TAG_enumerator), 00474 GetStringConstant(Name), 00475 ConstantInt::get(Type::Int64Ty, Val) 00476 }; 00477 00478 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00479 00480 M.addTypeName("llvm.dbg.enumerator.type", Init->getType()); 00481 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00482 GlobalValue::InternalLinkage, 00483 Init, "llvm.dbg.enumerator", &M); 00484 GV->setSection("llvm.metadata"); 00485 return DIEnumerator(GV); 00486 } 00487 00488 00489 /// CreateBasicType - Create a basic type like int, float, etc. 00490 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context, 00491 const std::string &Name, 00492 DICompileUnit CompileUnit, 00493 unsigned LineNumber, 00494 uint64_t SizeInBits, 00495 uint64_t AlignInBits, 00496 uint64_t OffsetInBits, unsigned Flags, 00497 unsigned Encoding) { 00498 Constant *Elts[] = { 00499 GetTagConstant(dwarf::DW_TAG_base_type), 00500 getCastToEmpty(Context), 00501 GetStringConstant(Name), 00502 getCastToEmpty(CompileUnit), 00503 ConstantInt::get(Type::Int32Ty, LineNumber), 00504 ConstantInt::get(Type::Int64Ty, SizeInBits), 00505 ConstantInt::get(Type::Int64Ty, AlignInBits), 00506 ConstantInt::get(Type::Int64Ty, OffsetInBits), 00507 ConstantInt::get(Type::Int32Ty, Flags), 00508 ConstantInt::get(Type::Int32Ty, Encoding) 00509 }; 00510 00511 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00512 00513 M.addTypeName("llvm.dbg.basictype.type", Init->getType()); 00514 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00515 GlobalValue::InternalLinkage, 00516 Init, "llvm.dbg.basictype", &M); 00517 GV->setSection("llvm.metadata"); 00518 return DIBasicType(GV); 00519 } 00520 00521 /// CreateDerivedType - Create a derived type like const qualified type, 00522 /// pointer, typedef, etc. 00523 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag, 00524 DIDescriptor Context, 00525 const std::string &Name, 00526 DICompileUnit CompileUnit, 00527 unsigned LineNumber, 00528 uint64_t SizeInBits, 00529 uint64_t AlignInBits, 00530 uint64_t OffsetInBits, 00531 unsigned Flags, 00532 DIType DerivedFrom) { 00533 Constant *Elts[] = { 00534 GetTagConstant(Tag), 00535 getCastToEmpty(Context), 00536 GetStringConstant(Name), 00537 getCastToEmpty(CompileUnit), 00538 ConstantInt::get(Type::Int32Ty, LineNumber), 00539 ConstantInt::get(Type::Int64Ty, SizeInBits), 00540 ConstantInt::get(Type::Int64Ty, AlignInBits), 00541 ConstantInt::get(Type::Int64Ty, OffsetInBits), 00542 ConstantInt::get(Type::Int32Ty, Flags), 00543 getCastToEmpty(DerivedFrom) 00544 }; 00545 00546 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00547 00548 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType()); 00549 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00550 GlobalValue::InternalLinkage, 00551 Init, "llvm.dbg.derivedtype", &M); 00552 GV->setSection("llvm.metadata"); 00553 return DIDerivedType(GV); 00554 } 00555 00556 /// CreateCompositeType - Create a composite type like array, struct, etc. 00557 DICompositeType DIFactory::CreateCompositeType(unsigned Tag, 00558 DIDescriptor Context, 00559 const std::string &Name, 00560 DICompileUnit CompileUnit, 00561 unsigned LineNumber, 00562 uint64_t SizeInBits, 00563 uint64_t AlignInBits, 00564 uint64_t OffsetInBits, 00565 unsigned Flags, 00566 DIType DerivedFrom, 00567 DIArray Elements) { 00568 00569 Constant *Elts[] = { 00570 GetTagConstant(Tag), 00571 getCastToEmpty(Context), 00572 GetStringConstant(Name), 00573 getCastToEmpty(CompileUnit), 00574 ConstantInt::get(Type::Int32Ty, LineNumber), 00575 ConstantInt::get(Type::Int64Ty, SizeInBits), 00576 ConstantInt::get(Type::Int64Ty, AlignInBits), 00577 ConstantInt::get(Type::Int64Ty, OffsetInBits), 00578 ConstantInt::get(Type::Int32Ty, Flags), 00579 getCastToEmpty(DerivedFrom), 00580 getCastToEmpty(Elements) 00581 }; 00582 00583 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00584 00585 M.addTypeName("llvm.dbg.composite.type", Init->getType()); 00586 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00587 GlobalValue::InternalLinkage, 00588 Init, "llvm.dbg.composite", &M); 00589 GV->setSection("llvm.metadata"); 00590 return DICompositeType(GV); 00591 } 00592 00593 00594 /// CreateSubprogram - Create a new descriptor for the specified subprogram. 00595 /// See comments in DISubprogram for descriptions of these fields. This 00596 /// method does not unique the generated descriptors. 00597 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context, 00598 const std::string &Name, 00599 const std::string &DisplayName, 00600 const std::string &LinkageName, 00601 DICompileUnit CompileUnit, 00602 unsigned LineNo, DIType Type, 00603 bool isLocalToUnit, 00604 bool isDefinition) { 00605 00606 Constant *Elts[] = { 00607 GetTagConstant(dwarf::DW_TAG_subprogram), 00608 getCastToEmpty(GetOrCreateSubprogramAnchor()), 00609 getCastToEmpty(Context), 00610 GetStringConstant(Name), 00611 GetStringConstant(DisplayName), 00612 GetStringConstant(LinkageName), 00613 getCastToEmpty(CompileUnit), 00614 ConstantInt::get(Type::Int32Ty, LineNo), 00615 getCastToEmpty(Type), 00616 ConstantInt::get(Type::Int1Ty, isLocalToUnit), 00617 ConstantInt::get(Type::Int1Ty, isDefinition) 00618 }; 00619 00620 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00621 00622 M.addTypeName("llvm.dbg.subprogram.type", Init->getType()); 00623 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00624 GlobalValue::InternalLinkage, 00625 Init, "llvm.dbg.subprogram", &M); 00626 GV->setSection("llvm.metadata"); 00627 return DISubprogram(GV); 00628 } 00629 00630 /// CreateGlobalVariable - Create a new descriptor for the specified global. 00631 DIGlobalVariable 00632 DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name, 00633 const std::string &DisplayName, 00634 const std::string &LinkageName, 00635 DICompileUnit CompileUnit, 00636 unsigned LineNo, DIType Type,bool isLocalToUnit, 00637 bool isDefinition, llvm::GlobalVariable *Val) { 00638 Constant *Elts[] = { 00639 GetTagConstant(dwarf::DW_TAG_variable), 00640 getCastToEmpty(GetOrCreateGlobalVariableAnchor()), 00641 getCastToEmpty(Context), 00642 GetStringConstant(Name), 00643 GetStringConstant(DisplayName), 00644 GetStringConstant(LinkageName), 00645 getCastToEmpty(CompileUnit), 00646 ConstantInt::get(Type::Int32Ty, LineNo), 00647 getCastToEmpty(Type), 00648 ConstantInt::get(Type::Int1Ty, isLocalToUnit), 00649 ConstantInt::get(Type::Int1Ty, isDefinition), 00650 ConstantExpr::getBitCast(Val, EmptyStructPtr) 00651 }; 00652 00653 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00654 00655 M.addTypeName("llvm.dbg.global_variable.type", Init->getType()); 00656 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00657 GlobalValue::InternalLinkage, 00658 Init, "llvm.dbg.global_variable", &M); 00659 GV->setSection("llvm.metadata"); 00660 return DIGlobalVariable(GV); 00661 } 00662 00663 00664 /// CreateVariable - Create a new descriptor for the specified variable. 00665 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context, 00666 const std::string &Name, 00667 DICompileUnit CompileUnit, unsigned LineNo, 00668 DIType Type) { 00669 Constant *Elts[] = { 00670 GetTagConstant(Tag), 00671 getCastToEmpty(Context), 00672 GetStringConstant(Name), 00673 getCastToEmpty(CompileUnit), 00674 ConstantInt::get(Type::Int32Ty, LineNo), 00675 getCastToEmpty(Type) 00676 }; 00677 00678 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00679 00680 M.addTypeName("llvm.dbg.variable.type", Init->getType()); 00681 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00682 GlobalValue::InternalLinkage, 00683 Init, "llvm.dbg.variable", &M); 00684 GV->setSection("llvm.metadata"); 00685 return DIVariable(GV); 00686 } 00687 00688 00689 /// CreateBlock - This creates a descriptor for a lexical block with the 00690 /// specified parent context. 00691 DIBlock DIFactory::CreateBlock(DIDescriptor Context) { 00692 Constant *Elts[] = { 00693 GetTagConstant(dwarf::DW_TAG_lexical_block), 00694 getCastToEmpty(Context) 00695 }; 00696 00697 Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0])); 00698 00699 M.addTypeName("llvm.dbg.block.type", Init->getType()); 00700 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, 00701 GlobalValue::InternalLinkage, 00702 Init, "llvm.dbg.block", &M); 00703 GV->setSection("llvm.metadata"); 00704 return DIBlock(GV); 00705 } 00706 00707 00708 //===----------------------------------------------------------------------===// 00709 // DIFactory: Routines for inserting code into a function 00710 //===----------------------------------------------------------------------===// 00711 00712 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation, 00713 /// inserting it at the end of the specified basic block. 00714 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo, 00715 unsigned ColNo, BasicBlock *BB) { 00716 00717 // Lazily construct llvm.dbg.stoppoint function. 00718 if (!StopPointFn) 00719 StopPointFn = llvm::Intrinsic::getDeclaration(&M, 00720 llvm::Intrinsic::dbg_stoppoint); 00721 00722 // Invoke llvm.dbg.stoppoint 00723 Value *Args[] = { 00724 llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo), 00725 llvm::ConstantInt::get(llvm::Type::Int32Ty, ColNo), 00726 getCastToEmpty(CU) 00727 }; 00728 CallInst::Create(StopPointFn, Args, Args+3, "", BB); 00729 } 00730 00731 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to 00732 /// mark the start of the specified subprogram. 00733 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) { 00734 // Lazily construct llvm.dbg.func.start. 00735 if (!FuncStartFn) 00736 FuncStartFn = llvm::Intrinsic::getDeclaration(&M, 00737 llvm::Intrinsic::dbg_func_start); 00738 00739 // Call llvm.dbg.func.start which also implicitly sets a stoppoint. 00740 CallInst::Create(FuncStartFn, getCastToEmpty(SP), "", BB); 00741 } 00742 00743 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to 00744 /// mark the start of a region for the specified scoping descriptor. 00745 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) { 00746 // Lazily construct llvm.dbg.region.start function. 00747 if (!RegionStartFn) 00748 RegionStartFn = llvm::Intrinsic::getDeclaration(&M, 00749 llvm::Intrinsic::dbg_region_start); 00750 // Call llvm.dbg.func.start. 00751 CallInst::Create(RegionStartFn, getCastToEmpty(D), "", BB); 00752 } 00753 00754 00755 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to 00756 /// mark the end of a region for the specified scoping descriptor. 00757 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) { 00758 // Lazily construct llvm.dbg.region.end function. 00759 if (!RegionEndFn) 00760 RegionEndFn = llvm::Intrinsic::getDeclaration(&M, 00761 llvm::Intrinsic::dbg_region_end); 00762 00763 CallInst::Create(RegionEndFn, getCastToEmpty(D), "", BB); 00764 } 00765 00766 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call. 00767 void DIFactory::InsertDeclare(llvm::Value *Storage, DIVariable D, 00768 BasicBlock *BB) { 00769 // Cast the storage to a {}* for the call to llvm.dbg.declare. 00770 Storage = new llvm::BitCastInst(Storage, EmptyStructPtr, "", BB); 00771 00772 if (!DeclareFn) 00773 DeclareFn = llvm::Intrinsic::getDeclaration(&M, 00774 llvm::Intrinsic::dbg_declare); 00775 Value *Args[] = { Storage, getCastToEmpty(D) }; 00776 CallInst::Create(DeclareFn, Args, Args+2, "", BB); 00777 } 00778 00779 namespace llvm { 00780 /// Finds the stoppoint coressponding to this instruction, that is the 00781 /// stoppoint that dominates this instruction 00782 const DbgStopPointInst *findStopPoint(const Instruction *Inst) 00783 { 00784 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst)) 00785 return DSI; 00786 00787 const BasicBlock *BB = Inst->getParent(); 00788 BasicBlock::const_iterator I = Inst, B; 00789 do { 00790 B = BB->begin(); 00791 // A BB consisting only of a terminator can't have a stoppoint. 00792 if (I != B) { 00793 do { 00794 --I; 00795 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I)) 00796 return DSI; 00797 } while (I != B); 00798 } 00799 // This BB didn't have a stoppoint: if there is only one 00800 // predecessor, look for a stoppoint there. 00801 // We could use getIDom(), but that would require dominator info. 00802 BB = I->getParent()->getUniquePredecessor(); 00803 if (BB) 00804 I = BB->getTerminator(); 00805 } while (BB != 0); 00806 return 0; 00807 } 00808 00809 /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 00810 /// instruction in this Basic Block, and returns the stoppoint for it. 00811 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) 00812 { 00813 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 00814 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I)) 00815 return DSI; 00816 } 00817 // Fallback to looking for stoppoint of unique predecessor. 00818 // Useful if this BB contains no stoppoints, but unique predecessor does. 00819 BB = BB->getUniquePredecessor(); 00820 if (BB) 00821 return findStopPoint(BB->getTerminator()); 00822 return 0; 00823 } 00824 00825 /// Finds the dbg.declare intrinsic corresponding to this value if any. 00826 /// It looks through pointer casts too. 00827 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) 00828 { 00829 if (stripCasts) { 00830 V = V->stripPointerCasts(); 00831 // Look for the bitcast. 00832 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end(); 00833 I != E; ++I) { 00834 if (isa<BitCastInst>(I)) 00835 return findDbgDeclare(*I, false); 00836 } 00837 return 0; 00838 } 00839 00840 // Find dbg.declare among uses of the instruction. 00841 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end(); 00842 I != E; ++I) { 00843 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) 00844 return DDI; 00845 } 00846 return 0; 00847 } 00848 } 00849 00850 /// dump - print compile unit. 00851 void DICompileUnit::dump() const { 00852 cerr << " [" << dwarf::LanguageString(getLanguage()) << "] "; 00853 cerr << " [" << getDirectory() << "/" << getFilename() << " ]"; 00854 } 00855 00856 /// dump - print type. 00857 void DIType::dump() const { 00858 if (isNull()) return; 00859 if (!getName().empty()) 00860 cerr << " [" << getName() << "] "; 00861 unsigned Tag = getTag(); 00862 cerr << " [" << dwarf::TagString(Tag) << "] "; 00863 // TODO : Print context 00864 getCompileUnit().dump(); 00865 cerr << " [" 00866 << getLineNumber() << ", " 00867 << getSizeInBits() << ", " 00868 << getAlignInBits() << ", " 00869 << getOffsetInBits() 00870 << "] "; 00871 if (isPrivate()) 00872 cerr << " [private] "; 00873 else if (isProtected()) 00874 cerr << " [protected] "; 00875 if (isForwardDecl()) 00876 cerr << " [fwd] "; 00877 00878 if (isBasicType(Tag)) 00879 DIBasicType(GV).dump(); 00880 else if (isDerivedType(Tag)) 00881 DIDerivedType(GV).dump(); 00882 else if (isCompositeType(Tag)) 00883 DICompositeType(GV).dump(); 00884 else { 00885 cerr << "Invalid DIType\n"; 00886 return; 00887 } 00888 cerr << "\n"; 00889 } 00890 00891 /// dump - print basic type. 00892 void DIBasicType::dump() const { 00893 cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] "; 00894 00895 } 00896 00897 /// dump - print derived type. 00898 void DIDerivedType::dump() const { 00899 cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump(); 00900 } 00901 00902 /// dump - print composite type. 00903 void DICompositeType::dump() const { 00904 DIArray A = getTypeArray(); 00905 if (A.isNull()) 00906 return; 00907 cerr << " [" << A.getNumElements() << " elements]"; 00908 } 00909 00910 /// dump - print global. 00911 void DIGlobal::dump() const { 00912 00913 if (!getName().empty()) 00914 cerr << " [" << getName() << "] "; 00915 unsigned Tag = getTag(); 00916 cerr << " [" << dwarf::TagString(Tag) << "] "; 00917 // TODO : Print context 00918 getCompileUnit().dump(); 00919 cerr << " [" << getLineNumber() << "] "; 00920 if (isLocalToUnit()) 00921 cerr << " [local] "; 00922 if (isDefinition()) 00923 cerr << " [def] "; 00924 00925 if (isGlobalVariable(Tag)) 00926 DIGlobalVariable(GV).dump(); 00927 00928 cerr << "\n"; 00929 } 00930 00931 /// dump - print subprogram. 00932 void DISubprogram::dump() const { 00933 DIGlobal::dump(); 00934 } 00935 00936 /// dump - print global variable. 00937 void DIGlobalVariable::dump() const { 00938 cerr << " ["; getGlobal()->dump(); cerr << "] "; 00939 } 00940 00941 /// dump - print variable. 00942 void DIVariable::dump() const { 00943 if (!getName().empty()) 00944 cerr << " [" << getName() << "] "; 00945 getCompileUnit().dump(); 00946 cerr << " [" << getLineNumber() << "] "; 00947 getType().dump(); 00948 cerr << "\n"; 00949 }