1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by James M. Laskey and is distributed under 6 // the University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/CodeGen/MachineModuleInfo.h" 11 12 #include "llvm/Constants.h" 13 #include "llvm/CodeGen/MachineFunctionPass.h" 14 #include "llvm/CodeGen/MachineFunction.h" 15 #include "llvm/CodeGen/MachineLocation.h" 16 #include "llvm/Target/TargetInstrInfo.h" 17 #include "llvm/Target/TargetMachine.h" 18 #include "llvm/Target/TargetOptions.h" 19 #include "llvm/DerivedTypes.h" 20 #include "llvm/GlobalVariable.h" 21 #include "llvm/Intrinsics.h" 22 #include "llvm/Instructions.h" 23 #include "llvm/Module.h" 24 #include "llvm/Support/Dwarf.h" 25 #include "llvm/Support/Streams.h" 26 using namespace llvm; 27 using namespace llvm::dwarf; 28 29 // Handle the Pass registration stuff necessary to use TargetData's. 30 namespace { 31 RegisterPass<MachineModuleInfo> X("machinemoduleinfo", "Module Information"); 32 } 33 34 //===----------------------------------------------------------------------===// 35 36 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the 37 /// specified value in their initializer somewhere. 38 static void 39 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) { 40 // Scan though value users. 41 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 42 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) { 43 // If the user is a GlobalVariable then add to result. 44 Result.push_back(GV); 45 } else if (Constant *C = dyn_cast<Constant>(*I)) { 46 // If the user is a constant variable then scan its users 47 getGlobalVariablesUsing(C, Result); 48 } 49 } 50 } 51 52 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the 53 /// named GlobalVariable. 54 static std::vector<GlobalVariable*> 55 getGlobalVariablesUsing(Module &M, const std::string &RootName) { 56 std::vector<GlobalVariable*> Result; // GlobalVariables matching criteria. 57 58 std::vector<const Type*> FieldTypes; 59 FieldTypes.push_back(Type::Int32Ty); 60 FieldTypes.push_back(Type::Int32Ty); 61 62 // Get the GlobalVariable root. 63 GlobalVariable *UseRoot = M.getGlobalVariable(RootName, 64 StructType::get(FieldTypes)); 65 66 // If present and linkonce then scan for users. 67 if (UseRoot && UseRoot->hasLinkOnceLinkage()) { 68 getGlobalVariablesUsing(UseRoot, Result); 69 } 70 71 return Result; 72 } 73 74 /// isStringValue - Return true if the given value can be coerced to a string. 75 /// 76 static bool isStringValue(Value *V) { 77 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { 78 if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { 79 ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); 80 return Init->isString(); 81 } 82 } else if (Constant *C = dyn_cast<Constant>(V)) { 83 if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) 84 return isStringValue(GV); 85 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 86 if (CE->getOpcode() == Instruction::GetElementPtr) { 87 if (CE->getNumOperands() == 3 && 88 cast<Constant>(CE->getOperand(1))->isNullValue() && 89 isa<ConstantInt>(CE->getOperand(2))) { 90 return isStringValue(CE->getOperand(0)); 91 } 92 } 93 } 94 } 95 return false; 96 } 97 98 /// getGlobalVariable - Return either a direct or cast Global value. 99 /// 100 static GlobalVariable *getGlobalVariable(Value *V) { 101 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { 102 return GV; 103 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 104 if (CE->getOpcode() == Instruction::BitCast) { 105 return dyn_cast<GlobalVariable>(CE->getOperand(0)); 106 } 107 } 108 return NULL; 109 } 110 111 /// isGlobalVariable - Return true if the given value can be coerced to a 112 /// GlobalVariable. 113 static bool isGlobalVariable(Value *V) { 114 if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) { 115 return true; 116 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 117 if (CE->getOpcode() == Instruction::BitCast) { 118 return isa<GlobalVariable>(CE->getOperand(0)); 119 } 120 } 121 return false; 122 } 123 124 /// getUIntOperand - Return ith operand if it is an unsigned integer. 125 /// 126 static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) { 127 // Make sure the GlobalVariable has an initializer. 128 if (!GV->hasInitializer()) return NULL; 129 130 // Get the initializer constant. 131 ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer()); 132 if (!CI) return NULL; 133 134 // Check if there is at least i + 1 operands. 135 unsigned N = CI->getNumOperands(); 136 if (i >= N) return NULL; 137 138 // Check constant. 139 return dyn_cast<ConstantInt>(CI->getOperand(i)); 140 } 141 142 //===----------------------------------------------------------------------===// 143 144 /// ApplyToFields - Target the visitor to each field of the debug information 145 /// descriptor. 146 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) { 147 DD->ApplyToFields(this); 148 } 149 150 //===----------------------------------------------------------------------===// 151 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug 152 /// the supplied DebugInfoDesc. 153 class DICountVisitor : public DIVisitor { 154 private: 155 unsigned Count; // Running count of fields. 156 157 public: 158 DICountVisitor() : DIVisitor(), Count(0) {} 159 160 // Accessors. 161 unsigned getCount() const { return Count; } 162 163 /// Apply - Count each of the fields. 164 /// 165 virtual void Apply(int &Field) { ++Count; } 166 virtual void Apply(unsigned &Field) { ++Count; } 167 virtual void Apply(int64_t &Field) { ++Count; } 168 virtual void Apply(uint64_t &Field) { ++Count; } 169 virtual void Apply(bool &Field) { ++Count; } 170 virtual void Apply(std::string &Field) { ++Count; } 171 virtual void Apply(DebugInfoDesc *&Field) { ++Count; } 172 virtual void Apply(GlobalVariable *&Field) { ++Count; } 173 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 174 ++Count; 175 } 176 }; 177 178 //===----------------------------------------------------------------------===// 179 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the 180 /// supplied DebugInfoDesc. 181 class DIDeserializeVisitor : public DIVisitor { 182 private: 183 DIDeserializer &DR; // Active deserializer. 184 unsigned I; // Current operand index. 185 ConstantStruct *CI; // GlobalVariable constant initializer. 186 187 public: 188 DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV) 189 : DIVisitor() 190 , DR(D) 191 , I(0) 192 , CI(cast<ConstantStruct>(GV->getInitializer())) 193 {} 194 195 /// Apply - Set the value of each of the fields. 196 /// 197 virtual void Apply(int &Field) { 198 Constant *C = CI->getOperand(I++); 199 Field = cast<ConstantInt>(C)->getSExtValue(); 200 } 201 virtual void Apply(unsigned &Field) { 202 Constant *C = CI->getOperand(I++); 203 Field = cast<ConstantInt>(C)->getZExtValue(); 204 } 205 virtual void Apply(int64_t &Field) { 206 Constant *C = CI->getOperand(I++); 207 Field = cast<ConstantInt>(C)->getSExtValue(); 208 } 209 virtual void Apply(uint64_t &Field) { 210 Constant *C = CI->getOperand(I++); 211 Field = cast<ConstantInt>(C)->getZExtValue(); 212 } 213 virtual void Apply(bool &Field) { 214 Constant *C = CI->getOperand(I++); 215 Field = cast<ConstantInt>(C)->getZExtValue(); 216 } 217 virtual void Apply(std::string &Field) { 218 Constant *C = CI->getOperand(I++); 219 Field = C->getStringValue(); 220 } 221 virtual void Apply(DebugInfoDesc *&Field) { 222 Constant *C = CI->getOperand(I++); 223 Field = DR.Deserialize(C); 224 } 225 virtual void Apply(GlobalVariable *&Field) { 226 Constant *C = CI->getOperand(I++); 227 Field = getGlobalVariable(C); 228 } 229 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 230 Field.resize(0); 231 Constant *C = CI->getOperand(I++); 232 GlobalVariable *GV = getGlobalVariable(C); 233 if (GV->hasInitializer()) { 234 if (ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer())) { 235 for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) { 236 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); 237 DebugInfoDesc *DE = DR.Deserialize(GVE); 238 Field.push_back(DE); 239 } 240 } else if (GV->getInitializer()->isNullValue()) { 241 if (const ArrayType *T = 242 dyn_cast<ArrayType>(GV->getType()->getElementType())) { 243 Field.resize(T->getNumElements()); 244 } 245 } 246 } 247 } 248 }; 249 250 //===----------------------------------------------------------------------===// 251 /// DISerializeVisitor - This DIVisitor serializes all the fields in 252 /// the supplied DebugInfoDesc. 253 class DISerializeVisitor : public DIVisitor { 254 private: 255 DISerializer &SR; // Active serializer. 256 std::vector<Constant*> &Elements; // Element accumulator. 257 258 public: 259 DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E) 260 : DIVisitor() 261 , SR(S) 262 , Elements(E) 263 {} 264 265 /// Apply - Set the value of each of the fields. 266 /// 267 virtual void Apply(int &Field) { 268 Elements.push_back(ConstantInt::get(Type::Int32Ty, int32_t(Field))); 269 } 270 virtual void Apply(unsigned &Field) { 271 Elements.push_back(ConstantInt::get(Type::Int32Ty, uint32_t(Field))); 272 } 273 virtual void Apply(int64_t &Field) { 274 Elements.push_back(ConstantInt::get(Type::Int64Ty, int64_t(Field))); 275 } 276 virtual void Apply(uint64_t &Field) { 277 Elements.push_back(ConstantInt::get(Type::Int64Ty, uint64_t(Field))); 278 } 279 virtual void Apply(bool &Field) { 280 Elements.push_back(ConstantInt::get(Type::Int1Ty, Field)); 281 } 282 virtual void Apply(std::string &Field) { 283 Elements.push_back(SR.getString(Field)); 284 } 285 virtual void Apply(DebugInfoDesc *&Field) { 286 GlobalVariable *GV = NULL; 287 288 // If non-NULL then convert to global. 289 if (Field) GV = SR.Serialize(Field); 290 291 // FIXME - At some point should use specific type. 292 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 293 294 if (GV) { 295 // Set to pointer to global. 296 Elements.push_back(ConstantExpr::getBitCast(GV, EmptyTy)); 297 } else { 298 // Use NULL. 299 Elements.push_back(ConstantPointerNull::get(EmptyTy)); 300 } 301 } 302 virtual void Apply(GlobalVariable *&Field) { 303 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 304 if (Field) { 305 Elements.push_back(ConstantExpr::getBitCast(Field, EmptyTy)); 306 } else { 307 Elements.push_back(ConstantPointerNull::get(EmptyTy)); 308 } 309 } 310 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 311 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 312 unsigned N = Field.size(); 313 ArrayType *AT = ArrayType::get(EmptyTy, N); 314 std::vector<Constant *> ArrayElements; 315 316 for (unsigned i = 0, N = Field.size(); i < N; ++i) { 317 if (DebugInfoDesc *Element = Field[i]) { 318 GlobalVariable *GVE = SR.Serialize(Element); 319 Constant *CE = ConstantExpr::getBitCast(GVE, EmptyTy); 320 ArrayElements.push_back(cast<Constant>(CE)); 321 } else { 322 ArrayElements.push_back(ConstantPointerNull::get(EmptyTy)); 323 } 324 } 325 326 Constant *CA = ConstantArray::get(AT, ArrayElements); 327 GlobalVariable *CAGV = new GlobalVariable(AT, true, 328 GlobalValue::InternalLinkage, 329 CA, "llvm.dbg.array", 330 SR.getModule()); 331 CAGV->setSection("llvm.metadata"); 332 Constant *CAE = ConstantExpr::getBitCast(CAGV, EmptyTy); 333 Elements.push_back(CAE); 334 } 335 }; 336 337 //===----------------------------------------------------------------------===// 338 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in 339 /// the supplied DebugInfoDesc. 340 class DIGetTypesVisitor : public DIVisitor { 341 private: 342 DISerializer &SR; // Active serializer. 343 std::vector<const Type*> &Fields; // Type accumulator. 344 345 public: 346 DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F) 347 : DIVisitor() 348 , SR(S) 349 , Fields(F) 350 {} 351 352 /// Apply - Set the value of each of the fields. 353 /// 354 virtual void Apply(int &Field) { 355 Fields.push_back(Type::Int32Ty); 356 } 357 virtual void Apply(unsigned &Field) { 358 Fields.push_back(Type::Int32Ty); 359 } 360 virtual void Apply(int64_t &Field) { 361 Fields.push_back(Type::Int64Ty); 362 } 363 virtual void Apply(uint64_t &Field) { 364 Fields.push_back(Type::Int64Ty); 365 } 366 virtual void Apply(bool &Field) { 367 Fields.push_back(Type::Int1Ty); 368 } 369 virtual void Apply(std::string &Field) { 370 Fields.push_back(SR.getStrPtrType()); 371 } 372 virtual void Apply(DebugInfoDesc *&Field) { 373 // FIXME - At some point should use specific type. 374 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 375 Fields.push_back(EmptyTy); 376 } 377 virtual void Apply(GlobalVariable *&Field) { 378 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 379 Fields.push_back(EmptyTy); 380 } 381 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 382 const PointerType *EmptyTy = SR.getEmptyStructPtrType(); 383 Fields.push_back(EmptyTy); 384 } 385 }; 386 387 //===----------------------------------------------------------------------===// 388 /// DIVerifyVisitor - This DIVisitor verifies all the field types against 389 /// a constant initializer. 390 class DIVerifyVisitor : public DIVisitor { 391 private: 392 DIVerifier &VR; // Active verifier. 393 bool IsValid; // Validity status. 394 unsigned I; // Current operand index. 395 ConstantStruct *CI; // GlobalVariable constant initializer. 396 397 public: 398 DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV) 399 : DIVisitor() 400 , VR(V) 401 , IsValid(true) 402 , I(0) 403 , CI(cast<ConstantStruct>(GV->getInitializer())) 404 { 405 } 406 407 // Accessors. 408 bool isValid() const { return IsValid; } 409 410 /// Apply - Set the value of each of the fields. 411 /// 412 virtual void Apply(int &Field) { 413 Constant *C = CI->getOperand(I++); 414 IsValid = IsValid && isa<ConstantInt>(C); 415 } 416 virtual void Apply(unsigned &Field) { 417 Constant *C = CI->getOperand(I++); 418 IsValid = IsValid && isa<ConstantInt>(C); 419 } 420 virtual void Apply(int64_t &Field) { 421 Constant *C = CI->getOperand(I++); 422 IsValid = IsValid && isa<ConstantInt>(C); 423 } 424 virtual void Apply(uint64_t &Field) { 425 Constant *C = CI->getOperand(I++); 426 IsValid = IsValid && isa<ConstantInt>(C); 427 } 428 virtual void Apply(bool &Field) { 429 Constant *C = CI->getOperand(I++); 430 IsValid = IsValid && isa<ConstantInt>(C) && C->getType() == Type::Int1Ty; 431 } 432 virtual void Apply(std::string &Field) { 433 Constant *C = CI->getOperand(I++); 434 IsValid = IsValid && 435 (!C || isStringValue(C) || C->isNullValue()); 436 } 437 virtual void Apply(DebugInfoDesc *&Field) { 438 // FIXME - Prepare the correct descriptor. 439 Constant *C = CI->getOperand(I++); 440 IsValid = IsValid && isGlobalVariable(C); 441 } 442 virtual void Apply(GlobalVariable *&Field) { 443 Constant *C = CI->getOperand(I++); 444 IsValid = IsValid && isGlobalVariable(C); 445 } 446 virtual void Apply(std::vector<DebugInfoDesc *> &Field) { 447 Constant *C = CI->getOperand(I++); 448 IsValid = IsValid && isGlobalVariable(C); 449 if (!IsValid) return; 450 451 GlobalVariable *GV = getGlobalVariable(C); 452 IsValid = IsValid && GV && GV->hasInitializer(); 453 if (!IsValid) return; 454 455 ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer()); 456 IsValid = IsValid && CA; 457 if (!IsValid) return; 458 459 for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) { 460 IsValid = IsValid && isGlobalVariable(CA->getOperand(i)); 461 if (!IsValid) return; 462 463 GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i)); 464 VR.Verify(GVE); 465 } 466 } 467 }; 468 469 470 //===----------------------------------------------------------------------===// 471 472 /// TagFromGlobal - Returns the tag number from a debug info descriptor 473 /// GlobalVariable. Return DIIValid if operand is not an unsigned int. 474 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) { 475 ConstantInt *C = getUIntOperand(GV, 0); 476 return C ? ((unsigned)C->getZExtValue() & ~LLVMDebugVersionMask) : 477 (unsigned)DW_TAG_invalid; 478 } 479 480 /// VersionFromGlobal - Returns the version number from a debug info 481 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned 482 /// int. 483 unsigned DebugInfoDesc::VersionFromGlobal(GlobalVariable *GV) { 484 ConstantInt *C = getUIntOperand(GV, 0); 485 return C ? ((unsigned)C->getZExtValue() & LLVMDebugVersionMask) : 486 (unsigned)DW_TAG_invalid; 487 } 488 489 /// DescFactory - Create an instance of debug info descriptor based on Tag. 490 /// Return NULL if not a recognized Tag. 491 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) { 492 switch (Tag) { 493 case DW_TAG_anchor: return new AnchorDesc(); 494 case DW_TAG_compile_unit: return new CompileUnitDesc(); 495 case DW_TAG_variable: return new GlobalVariableDesc(); 496 case DW_TAG_subprogram: return new SubprogramDesc(); 497 case DW_TAG_lexical_block: return new BlockDesc(); 498 case DW_TAG_base_type: return new BasicTypeDesc(); 499 case DW_TAG_typedef: 500 case DW_TAG_pointer_type: 501 case DW_TAG_reference_type: 502 case DW_TAG_const_type: 503 case DW_TAG_volatile_type: 504 case DW_TAG_restrict_type: 505 case DW_TAG_member: 506 case DW_TAG_inheritance: return new DerivedTypeDesc(Tag); 507 case DW_TAG_array_type: 508 case DW_TAG_structure_type: 509 case DW_TAG_union_type: 510 case DW_TAG_enumeration_type: 511 case DW_TAG_vector_type: 512 case DW_TAG_subroutine_type: return new CompositeTypeDesc(Tag); 513 case DW_TAG_subrange_type: return new SubrangeDesc(); 514 case DW_TAG_enumerator: return new EnumeratorDesc(); 515 case DW_TAG_return_variable: 516 case DW_TAG_arg_variable: 517 case DW_TAG_auto_variable: return new VariableDesc(Tag); 518 default: break; 519 } 520 return NULL; 521 } 522 523 /// getLinkage - get linkage appropriate for this type of descriptor. 524 /// 525 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const { 526 return GlobalValue::InternalLinkage; 527 } 528 529 /// ApplyToFields - Target the vistor to the fields of the descriptor. 530 /// 531 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) { 532 Visitor->Apply(Tag); 533 } 534 535 //===----------------------------------------------------------------------===// 536 537 AnchorDesc::AnchorDesc() 538 : DebugInfoDesc(DW_TAG_anchor) 539 , AnchorTag(0) 540 {} 541 AnchorDesc::AnchorDesc(AnchoredDesc *D) 542 : DebugInfoDesc(DW_TAG_anchor) 543 , AnchorTag(D->getTag()) 544 {} 545 546 // Implement isa/cast/dyncast. 547 bool AnchorDesc::classof(const DebugInfoDesc *D) { 548 return D->getTag() == DW_TAG_anchor; 549 } 550 551 /// getLinkage - get linkage appropriate for this type of descriptor. 552 /// 553 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const { 554 return GlobalValue::LinkOnceLinkage; 555 } 556 557 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc. 558 /// 559 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) { 560 DebugInfoDesc::ApplyToFields(Visitor); 561 562 Visitor->Apply(AnchorTag); 563 } 564 565 /// getDescString - Return a string used to compose global names and labels. A 566 /// A global variable name needs to be defined for each debug descriptor that is 567 /// anchored. NOTE: that each global variable named here also needs to be added 568 /// to the list of names left external in the internalizer. 569 /// ExternalNames.insert("llvm.dbg.compile_units"); 570 /// ExternalNames.insert("llvm.dbg.global_variables"); 571 /// ExternalNames.insert("llvm.dbg.subprograms"); 572 const char *AnchorDesc::getDescString() const { 573 switch (AnchorTag) { 574 case DW_TAG_compile_unit: return CompileUnitDesc::AnchorString; 575 case DW_TAG_variable: return GlobalVariableDesc::AnchorString; 576 case DW_TAG_subprogram: return SubprogramDesc::AnchorString; 577 default: break; 578 } 579 580 assert(0 && "Tag does not have a case for anchor string"); 581 return ""; 582 } 583 584 /// getTypeString - Return a string used to label this descriptors type. 585 /// 586 const char *AnchorDesc::getTypeString() const { 587 return "llvm.dbg.anchor.type"; 588 } 589 590 #ifndef NDEBUG 591 void AnchorDesc::dump() { 592 cerr << getDescString() << " " 593 << "Version(" << getVersion() << "), " 594 << "Tag(" << getTag() << "), " 595 << "AnchorTag(" << AnchorTag << ")\n"; 596 } 597 #endif 598 599 //===----------------------------------------------------------------------===// 600 601 AnchoredDesc::AnchoredDesc(unsigned T) 602 : DebugInfoDesc(T) 603 , Anchor(NULL) 604 {} 605 606 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc. 607 /// 608 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) { 609 DebugInfoDesc::ApplyToFields(Visitor); 610 611 Visitor->Apply(Anchor); 612 } 613 614 //===----------------------------------------------------------------------===// 615 616 CompileUnitDesc::CompileUnitDesc() 617 : AnchoredDesc(DW_TAG_compile_unit) 618 , Language(0) 619 , FileName("") 620 , Directory("") 621 , Producer("") 622 {} 623 624 // Implement isa/cast/dyncast. 625 bool CompileUnitDesc::classof(const DebugInfoDesc *D) { 626 return D->getTag() == DW_TAG_compile_unit; 627 } 628 629 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc. 630 /// 631 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) { 632 AnchoredDesc::ApplyToFields(Visitor); 633 634 // Handle cases out of sync with compiler. 635 if (getVersion() == 0) { 636 unsigned DebugVersion; 637 Visitor->Apply(DebugVersion); 638 } 639 640 Visitor->Apply(Language); 641 Visitor->Apply(FileName); 642 Visitor->Apply(Directory); 643 Visitor->Apply(Producer); 644 } 645 646 /// getDescString - Return a string used to compose global names and labels. 647 /// 648 const char *CompileUnitDesc::getDescString() const { 649 return "llvm.dbg.compile_unit"; 650 } 651 652 /// getTypeString - Return a string used to label this descriptors type. 653 /// 654 const char *CompileUnitDesc::getTypeString() const { 655 return "llvm.dbg.compile_unit.type"; 656 } 657 658 /// getAnchorString - Return a string used to label this descriptor's anchor. 659 /// 660 const char *CompileUnitDesc::AnchorString = "llvm.dbg.compile_units"; 661 const char *CompileUnitDesc::getAnchorString() const { 662 return AnchorString; 663 } 664 665 #ifndef NDEBUG 666 void CompileUnitDesc::dump() { 667 cerr << getDescString() << " " 668 << "Version(" << getVersion() << "), " 669 << "Tag(" << getTag() << "), " 670 << "Anchor(" << getAnchor() << "), " 671 << "Language(" << Language << "), " 672 << "FileName(\"" << FileName << "\"), " 673 << "Directory(\"" << Directory << "\"), " 674 << "Producer(\"" << Producer << "\")\n"; 675 } 676 #endif 677 678 //===----------------------------------------------------------------------===// 679 680 TypeDesc::TypeDesc(unsigned T) 681 : DebugInfoDesc(T) 682 , Context(NULL) 683 , Name("") 684 , File(NULL) 685 , Line(0) 686 , Size(0) 687 , Align(0) 688 , Offset(0) 689 , Flags(0) 690 {} 691 692 /// ApplyToFields - Target the visitor to the fields of the TypeDesc. 693 /// 694 void TypeDesc::ApplyToFields(DIVisitor *Visitor) { 695 DebugInfoDesc::ApplyToFields(Visitor); 696 697 Visitor->Apply(Context); 698 Visitor->Apply(Name); 699 Visitor->Apply(File); 700 Visitor->Apply(Line); 701 Visitor->Apply(Size); 702 Visitor->Apply(Align); 703 Visitor->Apply(Offset); 704 if (getVersion() > LLVMDebugVersion4) Visitor->Apply(Flags); 705 } 706 707 /// getDescString - Return a string used to compose global names and labels. 708 /// 709 const char *TypeDesc::getDescString() const { 710 return "llvm.dbg.type"; 711 } 712 713 /// getTypeString - Return a string used to label this descriptor's type. 714 /// 715 const char *TypeDesc::getTypeString() const { 716 return "llvm.dbg.type.type"; 717 } 718 719 #ifndef NDEBUG 720 void TypeDesc::dump() { 721 cerr << getDescString() << " " 722 << "Version(" << getVersion() << "), " 723 << "Tag(" << getTag() << "), " 724 << "Context(" << Context << "), " 725 << "Name(\"" << Name << "\"), " 726 << "File(" << File << "), " 727 << "Line(" << Line << "), " 728 << "Size(" << Size << "), " 729 << "Align(" << Align << "), " 730 << "Offset(" << Offset << "), " 731 << "Flags(" << Flags << ")\n"; 732 } 733 #endif 734 735 //===----------------------------------------------------------------------===// 736 737 BasicTypeDesc::BasicTypeDesc() 738 : TypeDesc(DW_TAG_base_type) 739 , Encoding(0) 740 {} 741 742 // Implement isa/cast/dyncast. 743 bool BasicTypeDesc::classof(const DebugInfoDesc *D) { 744 return D->getTag() == DW_TAG_base_type; 745 } 746 747 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc. 748 /// 749 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) { 750 TypeDesc::ApplyToFields(Visitor); 751 752 Visitor->Apply(Encoding); 753 } 754 755 /// getDescString - Return a string used to compose global names and labels. 756 /// 757 const char *BasicTypeDesc::getDescString() const { 758 return "llvm.dbg.basictype"; 759 } 760 761 /// getTypeString - Return a string used to label this descriptor's type. 762 /// 763 const char *BasicTypeDesc::getTypeString() const { 764 return "llvm.dbg.basictype.type"; 765 } 766 767 #ifndef NDEBUG 768 void BasicTypeDesc::dump() { 769 cerr << getDescString() << " " 770 << "Version(" << getVersion() << "), " 771 << "Tag(" << getTag() << "), " 772 << "Context(" << getContext() << "), " 773 << "Name(\"" << getName() << "\"), " 774 << "Size(" << getSize() << "), " 775 << "Encoding(" << Encoding << ")\n"; 776 } 777 #endif 778 779 //===----------------------------------------------------------------------===// 780 781 DerivedTypeDesc::DerivedTypeDesc(unsigned T) 782 : TypeDesc(T) 783 , FromType(NULL) 784 {} 785 786 // Implement isa/cast/dyncast. 787 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) { 788 unsigned T = D->getTag(); 789 switch (T) { 790 case DW_TAG_typedef: 791 case DW_TAG_pointer_type: 792 case DW_TAG_reference_type: 793 case DW_TAG_const_type: 794 case DW_TAG_volatile_type: 795 case DW_TAG_restrict_type: 796 case DW_TAG_member: 797 case DW_TAG_inheritance: 798 return true; 799 default: break; 800 } 801 return false; 802 } 803 804 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc. 805 /// 806 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) { 807 TypeDesc::ApplyToFields(Visitor); 808 809 Visitor->Apply(FromType); 810 } 811 812 /// getDescString - Return a string used to compose global names and labels. 813 /// 814 const char *DerivedTypeDesc::getDescString() const { 815 return "llvm.dbg.derivedtype"; 816 } 817 818 /// getTypeString - Return a string used to label this descriptor's type. 819 /// 820 const char *DerivedTypeDesc::getTypeString() const { 821 return "llvm.dbg.derivedtype.type"; 822 } 823 824 #ifndef NDEBUG 825 void DerivedTypeDesc::dump() { 826 cerr << getDescString() << " " 827 << "Version(" << getVersion() << "), " 828 << "Tag(" << getTag() << "), " 829 << "Context(" << getContext() << "), " 830 << "Name(\"" << getName() << "\"), " 831 << "Size(" << getSize() << "), " 832 << "File(" << getFile() << "), " 833 << "Line(" << getLine() << "), " 834 << "FromType(" << FromType << ")\n"; 835 } 836 #endif 837 838 //===----------------------------------------------------------------------===// 839 840 CompositeTypeDesc::CompositeTypeDesc(unsigned T) 841 : DerivedTypeDesc(T) 842 , Elements() 843 {} 844 845 // Implement isa/cast/dyncast. 846 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) { 847 unsigned T = D->getTag(); 848 switch (T) { 849 case DW_TAG_array_type: 850 case DW_TAG_structure_type: 851 case DW_TAG_union_type: 852 case DW_TAG_enumeration_type: 853 case DW_TAG_vector_type: 854 case DW_TAG_subroutine_type: 855 return true; 856 default: break; 857 } 858 return false; 859 } 860 861 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc. 862 /// 863 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) { 864 DerivedTypeDesc::ApplyToFields(Visitor); 865 866 Visitor->Apply(Elements); 867 } 868 869 /// getDescString - Return a string used to compose global names and labels. 870 /// 871 const char *CompositeTypeDesc::getDescString() const { 872 return "llvm.dbg.compositetype"; 873 } 874 875 /// getTypeString - Return a string used to label this descriptor's type. 876 /// 877 const char *CompositeTypeDesc::getTypeString() const { 878 return "llvm.dbg.compositetype.type"; 879 } 880 881 #ifndef NDEBUG 882 void CompositeTypeDesc::dump() { 883 cerr << getDescString() << " " 884 << "Version(" << getVersion() << "), " 885 << "Tag(" << getTag() << "), " 886 << "Context(" << getContext() << "), " 887 << "Name(\"" << getName() << "\"), " 888 << "Size(" << getSize() << "), " 889 << "File(" << getFile() << "), " 890 << "Line(" << getLine() << "), " 891 << "FromType(" << getFromType() << "), " 892 << "Elements.size(" << Elements.size() << ")\n"; 893 } 894 #endif 895 896 //===----------------------------------------------------------------------===// 897 898 SubrangeDesc::SubrangeDesc() 899 : DebugInfoDesc(DW_TAG_subrange_type) 900 , Lo(0) 901 , Hi(0) 902 {} 903 904 // Implement isa/cast/dyncast. 905 bool SubrangeDesc::classof(const DebugInfoDesc *D) { 906 return D->getTag() == DW_TAG_subrange_type; 907 } 908 909 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc. 910 /// 911 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) { 912 DebugInfoDesc::ApplyToFields(Visitor); 913 914 Visitor->Apply(Lo); 915 Visitor->Apply(Hi); 916 } 917 918 /// getDescString - Return a string used to compose global names and labels. 919 /// 920 const char *SubrangeDesc::getDescString() const { 921 return "llvm.dbg.subrange"; 922 } 923 924 /// getTypeString - Return a string used to label this descriptor's type. 925 /// 926 const char *SubrangeDesc::getTypeString() const { 927 return "llvm.dbg.subrange.type"; 928 } 929 930 #ifndef NDEBUG 931 void SubrangeDesc::dump() { 932 cerr << getDescString() << " " 933 << "Version(" << getVersion() << "), " 934 << "Tag(" << getTag() << "), " 935 << "Lo(" << Lo << "), " 936 << "Hi(" << Hi << ")\n"; 937 } 938 #endif 939 940 //===----------------------------------------------------------------------===// 941 942 EnumeratorDesc::EnumeratorDesc() 943 : DebugInfoDesc(DW_TAG_enumerator) 944 , Name("") 945 , Value(0) 946 {} 947 948 // Implement isa/cast/dyncast. 949 bool EnumeratorDesc::classof(const DebugInfoDesc *D) { 950 return D->getTag() == DW_TAG_enumerator; 951 } 952 953 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc. 954 /// 955 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) { 956 DebugInfoDesc::ApplyToFields(Visitor); 957 958 Visitor->Apply(Name); 959 Visitor->Apply(Value); 960 } 961 962 /// getDescString - Return a string used to compose global names and labels. 963 /// 964 const char *EnumeratorDesc::getDescString() const { 965 return "llvm.dbg.enumerator"; 966 } 967 968 /// getTypeString - Return a string used to label this descriptor's type. 969 /// 970 const char *EnumeratorDesc::getTypeString() const { 971 return "llvm.dbg.enumerator.type"; 972 } 973 974 #ifndef NDEBUG 975 void EnumeratorDesc::dump() { 976 cerr << getDescString() << " " 977 << "Version(" << getVersion() << "), " 978 << "Tag(" << getTag() << "), " 979 << "Name(" << Name << "), " 980 << "Value(" << Value << ")\n"; 981 } 982 #endif 983 984 //===----------------------------------------------------------------------===// 985 986 VariableDesc::VariableDesc(unsigned T) 987 : DebugInfoDesc(T) 988 , Context(NULL) 989 , Name("") 990 , File(NULL) 991 , Line(0) 992 , TyDesc(0) 993 {} 994 995 // Implement isa/cast/dyncast. 996 bool VariableDesc::classof(const DebugInfoDesc *D) { 997 unsigned T = D->getTag(); 998 switch (T) { 999 case DW_TAG_auto_variable: 1000 case DW_TAG_arg_variable: 1001 case DW_TAG_return_variable: 1002 return true; 1003 default: break; 1004 } 1005 return false; 1006 } 1007 1008 /// ApplyToFields - Target the visitor to the fields of the VariableDesc. 1009 /// 1010 void VariableDesc::ApplyToFields(DIVisitor *Visitor) { 1011 DebugInfoDesc::ApplyToFields(Visitor); 1012 1013 Visitor->Apply(Context); 1014 Visitor->Apply(Name); 1015 Visitor->Apply(File); 1016 Visitor->Apply(Line); 1017 Visitor->Apply(TyDesc); 1018 } 1019 1020 /// getDescString - Return a string used to compose global names and labels. 1021 /// 1022 const char *VariableDesc::getDescString() const { 1023 return "llvm.dbg.variable"; 1024 } 1025 1026 /// getTypeString - Return a string used to label this descriptor's type. 1027 /// 1028 const char *VariableDesc::getTypeString() const { 1029 return "llvm.dbg.variable.type"; 1030 } 1031 1032 #ifndef NDEBUG 1033 void VariableDesc::dump() { 1034 cerr << getDescString() << " " 1035 << "Version(" << getVersion() << "), " 1036 << "Tag(" << getTag() << "), " 1037 << "Context(" << Context << "), " 1038 << "Name(\"" << Name << "\"), " 1039 << "File(" << File << "), " 1040 << "Line(" << Line << "), " 1041 << "TyDesc(" << TyDesc << ")\n"; 1042 } 1043 #endif 1044 1045 //===----------------------------------------------------------------------===// 1046 1047 GlobalDesc::GlobalDesc(unsigned T) 1048 : AnchoredDesc(T) 1049 , Context(0) 1050 , Name("") 1051 , FullName("") 1052 , LinkageName("") 1053 , File(NULL) 1054 , Line(0) 1055 , TyDesc(NULL) 1056 , IsStatic(false) 1057 , IsDefinition(false) 1058 {} 1059 1060 /// ApplyToFields - Target the visitor to the fields of the global. 1061 /// 1062 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) { 1063 AnchoredDesc::ApplyToFields(Visitor); 1064 1065 Visitor->Apply(Context); 1066 Visitor->Apply(Name); 1067 Visitor->Apply(FullName); 1068 Visitor->Apply(LinkageName); 1069 Visitor->Apply(File); 1070 Visitor->Apply(Line); 1071 Visitor->Apply(TyDesc); 1072 Visitor->Apply(IsStatic); 1073 Visitor->Apply(IsDefinition); 1074 } 1075 1076 //===----------------------------------------------------------------------===// 1077 1078 GlobalVariableDesc::GlobalVariableDesc() 1079 : GlobalDesc(DW_TAG_variable) 1080 , Global(NULL) 1081 {} 1082 1083 // Implement isa/cast/dyncast. 1084 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) { 1085 return D->getTag() == DW_TAG_variable; 1086 } 1087 1088 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc. 1089 /// 1090 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) { 1091 GlobalDesc::ApplyToFields(Visitor); 1092 1093 Visitor->Apply(Global); 1094 } 1095 1096 /// getDescString - Return a string used to compose global names and labels. 1097 /// 1098 const char *GlobalVariableDesc::getDescString() const { 1099 return "llvm.dbg.global_variable"; 1100 } 1101 1102 /// getTypeString - Return a string used to label this descriptors type. 1103 /// 1104 const char *GlobalVariableDesc::getTypeString() const { 1105 return "llvm.dbg.global_variable.type"; 1106 } 1107 1108 /// getAnchorString - Return a string used to label this descriptor's anchor. 1109 /// 1110 const char *GlobalVariableDesc::AnchorString = "llvm.dbg.global_variables"; 1111 const char *GlobalVariableDesc::getAnchorString() const { 1112 return AnchorString; 1113 } 1114 1115 #ifndef NDEBUG 1116 void GlobalVariableDesc::dump() { 1117 cerr << getDescString() << " " 1118 << "Version(" << getVersion() << "), " 1119 << "Tag(" << getTag() << "), " 1120 << "Anchor(" << getAnchor() << "), " 1121 << "Name(\"" << getName() << "\"), " 1122 << "FullName(\"" << getFullName() << "\"), " 1123 << "LinkageName(\"" << getLinkageName() << "\"), " 1124 << "File(" << getFile() << ")," 1125 << "Line(" << getLine() << ")," 1126 << "Type(" << getType() << "), " 1127 << "IsStatic(" << (isStatic() ? "true" : "false") << "), " 1128 << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), " 1129 << "Global(" << Global << ")\n"; 1130 } 1131 #endif 1132 1133 //===----------------------------------------------------------------------===// 1134 1135 SubprogramDesc::SubprogramDesc() 1136 : GlobalDesc(DW_TAG_subprogram) 1137 {} 1138 1139 // Implement isa/cast/dyncast. 1140 bool SubprogramDesc::classof(const DebugInfoDesc *D) { 1141 return D->getTag() == DW_TAG_subprogram; 1142 } 1143 1144 /// ApplyToFields - Target the visitor to the fields of the 1145 /// SubprogramDesc. 1146 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) { 1147 GlobalDesc::ApplyToFields(Visitor); 1148 } 1149 1150 /// getDescString - Return a string used to compose global names and labels. 1151 /// 1152 const char *SubprogramDesc::getDescString() const { 1153 return "llvm.dbg.subprogram"; 1154 } 1155 1156 /// getTypeString - Return a string used to label this descriptors type. 1157 /// 1158 const char *SubprogramDesc::getTypeString() const { 1159 return "llvm.dbg.subprogram.type"; 1160 } 1161 1162 /// getAnchorString - Return a string used to label this descriptor's anchor. 1163 /// 1164 const char *SubprogramDesc::AnchorString = "llvm.dbg.subprograms"; 1165 const char *SubprogramDesc::getAnchorString() const { 1166 return AnchorString; 1167 } 1168 1169 #ifndef NDEBUG 1170 void SubprogramDesc::dump() { 1171 cerr << getDescString() << " " 1172 << "Version(" << getVersion() << "), " 1173 << "Tag(" << getTag() << "), " 1174 << "Anchor(" << getAnchor() << "), " 1175 << "Name(\"" << getName() << "\"), " 1176 << "FullName(\"" << getFullName() << "\"), " 1177 << "LinkageName(\"" << getLinkageName() << "\"), " 1178 << "File(" << getFile() << ")," 1179 << "Line(" << getLine() << ")," 1180 << "Type(" << getType() << "), " 1181 << "IsStatic(" << (isStatic() ? "true" : "false") << "), " 1182 << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n"; 1183 } 1184 #endif 1185 1186 //===----------------------------------------------------------------------===// 1187 1188 BlockDesc::BlockDesc() 1189 : DebugInfoDesc(DW_TAG_lexical_block) 1190 , Context(NULL) 1191 {} 1192 1193 // Implement isa/cast/dyncast. 1194 bool BlockDesc::classof(const DebugInfoDesc *D) { 1195 return D->getTag() == DW_TAG_lexical_block; 1196 } 1197 1198 /// ApplyToFields - Target the visitor to the fields of the BlockDesc. 1199 /// 1200 void BlockDesc::ApplyToFields(DIVisitor *Visitor) { 1201 DebugInfoDesc::ApplyToFields(Visitor); 1202 1203 Visitor->Apply(Context); 1204 } 1205 1206 /// getDescString - Return a string used to compose global names and labels. 1207 /// 1208 const char *BlockDesc::getDescString() const { 1209 return "llvm.dbg.block"; 1210 } 1211 1212 /// getTypeString - Return a string used to label this descriptors type. 1213 /// 1214 const char *BlockDesc::getTypeString() const { 1215 return "llvm.dbg.block.type"; 1216 } 1217 1218 #ifndef NDEBUG 1219 void BlockDesc::dump() { 1220 cerr << getDescString() << " " 1221 << "Version(" << getVersion() << "), " 1222 << "Tag(" << getTag() << ")," 1223 << "Context(" << Context << ")\n"; 1224 } 1225 #endif 1226 1227 //===----------------------------------------------------------------------===// 1228 1229 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) { 1230 return Deserialize(getGlobalVariable(V)); 1231 } 1232 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) { 1233 // Handle NULL. 1234 if (!GV) return NULL; 1235 1236 // Check to see if it has been already deserialized. 1237 DebugInfoDesc *&Slot = GlobalDescs[GV]; 1238 if (Slot) return Slot; 1239 1240 // Get the Tag from the global. 1241 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); 1242 1243 // Create an empty instance of the correct sort. 1244 Slot = DebugInfoDesc::DescFactory(Tag); 1245 1246 // If not a user defined descriptor. 1247 if (Slot) { 1248 // Deserialize the fields. 1249 DIDeserializeVisitor DRAM(*this, GV); 1250 DRAM.ApplyToFields(Slot); 1251 } 1252 1253 return Slot; 1254 } 1255 1256 //===----------------------------------------------------------------------===// 1257 1258 /// getStrPtrType - Return a "sbyte *" type. 1259 /// 1260 const PointerType *DISerializer::getStrPtrType() { 1261 // If not already defined. 1262 if (!StrPtrTy) { 1263 // Construct the pointer to signed bytes. 1264 StrPtrTy = PointerType::get(Type::Int8Ty); 1265 } 1266 1267 return StrPtrTy; 1268 } 1269 1270 /// getEmptyStructPtrType - Return a "{ }*" type. 1271 /// 1272 const PointerType *DISerializer::getEmptyStructPtrType() { 1273 // If not already defined. 1274 if (!EmptyStructPtrTy) { 1275 // Construct the empty structure type. 1276 const StructType *EmptyStructTy = 1277 StructType::get(std::vector<const Type*>()); 1278 // Construct the pointer to empty structure type. 1279 EmptyStructPtrTy = PointerType::get(EmptyStructTy); 1280 } 1281 1282 return EmptyStructPtrTy; 1283 } 1284 1285 /// getTagType - Return the type describing the specified descriptor (via tag.) 1286 /// 1287 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) { 1288 // Attempt to get the previously defined type. 1289 StructType *&Ty = TagTypes[DD->getTag()]; 1290 1291 // If not already defined. 1292 if (!Ty) { 1293 // Set up fields vector. 1294 std::vector<const Type*> Fields; 1295 // Get types of fields. 1296 DIGetTypesVisitor GTAM(*this, Fields); 1297 GTAM.ApplyToFields(DD); 1298 1299 // Construct structured type. 1300 Ty = StructType::get(Fields); 1301 1302 // Register type name with module. 1303 M->addTypeName(DD->getTypeString(), Ty); 1304 } 1305 1306 return Ty; 1307 } 1308 1309 /// getString - Construct the string as constant string global. 1310 /// 1311 Constant *DISerializer::getString(const std::string &String) { 1312 // Check string cache for previous edition. 1313 Constant *&Slot = StringCache[String]; 1314 // Return Constant if previously defined. 1315 if (Slot) return Slot; 1316 // If empty string then use a sbyte* null instead. 1317 if (String.empty()) { 1318 Slot = ConstantPointerNull::get(getStrPtrType()); 1319 } else { 1320 // Construct string as an llvm constant. 1321 Constant *ConstStr = ConstantArray::get(String); 1322 // Otherwise create and return a new string global. 1323 GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, 1324 GlobalVariable::InternalLinkage, 1325 ConstStr, "str", M); 1326 StrGV->setSection("llvm.metadata"); 1327 // Convert to generic string pointer. 1328 Slot = ConstantExpr::getBitCast(StrGV, getStrPtrType()); 1329 } 1330 return Slot; 1331 1332 } 1333 1334 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable 1335 /// so that it can be serialized to a .bc or .ll file. 1336 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) { 1337 // Check if the DebugInfoDesc is already in the map. 1338 GlobalVariable *&Slot = DescGlobals[DD]; 1339 1340 // See if DebugInfoDesc exists, if so return prior GlobalVariable. 1341 if (Slot) return Slot; 1342 1343 // Get the type associated with the Tag. 1344 const StructType *Ty = getTagType(DD); 1345 1346 // Create the GlobalVariable early to prevent infinite recursion. 1347 GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(), 1348 NULL, DD->getDescString(), M); 1349 GV->setSection("llvm.metadata"); 1350 1351 // Insert new GlobalVariable in DescGlobals map. 1352 Slot = GV; 1353 1354 // Set up elements vector 1355 std::vector<Constant*> Elements; 1356 // Add fields. 1357 DISerializeVisitor SRAM(*this, Elements); 1358 SRAM.ApplyToFields(DD); 1359 1360 // Set the globals initializer. 1361 GV->setInitializer(ConstantStruct::get(Ty, Elements)); 1362 1363 return GV; 1364 } 1365 1366 //===----------------------------------------------------------------------===// 1367 1368 /// Verify - Return true if the GlobalVariable appears to be a valid 1369 /// serialization of a DebugInfoDesc. 1370 bool DIVerifier::Verify(Value *V) { 1371 return !V || Verify(getGlobalVariable(V)); 1372 } 1373 bool DIVerifier::Verify(GlobalVariable *GV) { 1374 // NULLs are valid. 1375 if (!GV) return true; 1376 1377 // Check prior validity. 1378 unsigned &ValiditySlot = Validity[GV]; 1379 1380 // If visited before then use old state. 1381 if (ValiditySlot) return ValiditySlot == Valid; 1382 1383 // Assume validity for the time being (recursion.) 1384 ValiditySlot = Valid; 1385 1386 // Make sure the global is internal or link once (anchor.) 1387 if (GV->getLinkage() != GlobalValue::InternalLinkage && 1388 GV->getLinkage() != GlobalValue::LinkOnceLinkage) { 1389 ValiditySlot = Invalid; 1390 return false; 1391 } 1392 1393 // Get the Tag. 1394 unsigned Tag = DebugInfoDesc::TagFromGlobal(GV); 1395 1396 // Check for user defined descriptors. 1397 if (Tag == DW_TAG_invalid) { 1398 ValiditySlot = Valid; 1399 return true; 1400 } 1401 1402 // Get the Version. 1403 unsigned Version = DebugInfoDesc::VersionFromGlobal(GV); 1404 1405 // Check for version mismatch. 1406 if (Version != LLVMDebugVersion) { 1407 ValiditySlot = Invalid; 1408 return false; 1409 } 1410 1411 // Construct an empty DebugInfoDesc. 1412 DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag); 1413 1414 // Allow for user defined descriptors. 1415 if (!DD) return true; 1416 1417 // Get the initializer constant. 1418 ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer()); 1419 1420 // Get the operand count. 1421 unsigned N = CI->getNumOperands(); 1422 1423 // Get the field count. 1424 unsigned &CountSlot = Counts[Tag]; 1425 if (!CountSlot) { 1426 // Check the operand count to the field count 1427 DICountVisitor CTAM; 1428 CTAM.ApplyToFields(DD); 1429 CountSlot = CTAM.getCount(); 1430 } 1431 1432 // Field count must be at most equal operand count. 1433 if (CountSlot > N) { 1434 delete DD; 1435 ValiditySlot = Invalid; 1436 return false; 1437 } 1438 1439 // Check each field for valid type. 1440 DIVerifyVisitor VRAM(*this, GV); 1441 VRAM.ApplyToFields(DD); 1442 1443 // Release empty DebugInfoDesc. 1444 delete DD; 1445 1446 // If fields are not valid. 1447 if (!VRAM.isValid()) { 1448 ValiditySlot = Invalid; 1449 return false; 1450 } 1451 1452 return true; 1453 } 1454 1455 //===----------------------------------------------------------------------===// 1456 1457 DebugScope::~DebugScope() { 1458 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i]; 1459 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j]; 1460 } 1461 1462 //===----------------------------------------------------------------------===// 1463 1464 MachineModuleInfo::MachineModuleInfo() 1465 : DR() 1466 , VR() 1467 , CompileUnits() 1468 , Directories() 1469 , SourceFiles() 1470 , Lines() 1471 , LabelIDList() 1472 , ScopeMap() 1473 , RootScope(NULL) 1474 , FrameMoves() 1475 {} 1476 MachineModuleInfo::~MachineModuleInfo() { 1477 1478 } 1479 1480 /// doInitialization - Initialize the state for a new module. 1481 /// 1482 bool MachineModuleInfo::doInitialization() { 1483 return false; 1484 } 1485 1486 /// doFinalization - Tear down the state after completion of a module. 1487 /// 1488 bool MachineModuleInfo::doFinalization() { 1489 return false; 1490 } 1491 1492 /// BeginFunction - Begin gathering function meta information. 1493 /// 1494 void MachineModuleInfo::BeginFunction(MachineFunction *MF) { 1495 // Coming soon. 1496 } 1497 1498 /// EndFunction - Discard function meta information. 1499 /// 1500 void MachineModuleInfo::EndFunction() { 1501 // Clean up scope information. 1502 if (RootScope) { 1503 delete RootScope; 1504 ScopeMap.clear(); 1505 RootScope = NULL; 1506 } 1507 1508 // Clean up frame info. 1509 FrameMoves.clear(); 1510 } 1511 1512 /// getDescFor - Convert a Value to a debug information descriptor. 1513 /// 1514 // FIXME - use new Value type when available. 1515 DebugInfoDesc *MachineModuleInfo::getDescFor(Value *V) { 1516 return DR.Deserialize(V); 1517 } 1518 1519 /// Verify - Verify that a Value is debug information descriptor. 1520 /// 1521 bool MachineModuleInfo::Verify(Value *V) { 1522 return VR.Verify(V); 1523 } 1524 1525 /// AnalyzeModule - Scan the module for global debug information. 1526 /// 1527 void MachineModuleInfo::AnalyzeModule(Module &M) { 1528 SetupCompileUnits(M); 1529 } 1530 1531 /// needsFrameInfo - Returns true if we need to gather callee-saved register 1532 /// move info for the frame. 1533 bool MachineModuleInfo::needsFrameInfo() const { 1534 return hasDebugInfo() || ExceptionHandling; 1535 } 1536 1537 /// SetupCompileUnits - Set up the unique vector of compile units. 1538 /// 1539 void MachineModuleInfo::SetupCompileUnits(Module &M) { 1540 std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M); 1541 1542 for (unsigned i = 0, N = CU.size(); i < N; i++) { 1543 CompileUnits.insert(CU[i]); 1544 } 1545 } 1546 1547 /// getCompileUnits - Return a vector of debug compile units. 1548 /// 1549 const UniqueVector<CompileUnitDesc *> MachineModuleInfo::getCompileUnits()const{ 1550 return CompileUnits; 1551 } 1552 1553 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the 1554 /// named GlobalVariable. 1555 std::vector<GlobalVariable*> 1556 MachineModuleInfo::getGlobalVariablesUsing(Module &M, 1557 const std::string &RootName) { 1558 return ::getGlobalVariablesUsing(M, RootName); 1559 } 1560 1561 /// RecordLabel - Records location information and associates it with a 1562 /// debug label. Returns a unique label ID used to generate a label and 1563 /// provide correspondence to the source line list. 1564 unsigned MachineModuleInfo::RecordLabel(unsigned Line, unsigned Column, 1565 unsigned Source) { 1566 unsigned ID = NextLabelID(); 1567 Lines.push_back(SourceLineInfo(Line, Column, Source, ID)); 1568 return ID; 1569 } 1570 1571 /// RecordSource - Register a source file with debug info. Returns an source 1572 /// ID. 1573 unsigned MachineModuleInfo::RecordSource(const std::string &Directory, 1574 const std::string &Source) { 1575 unsigned DirectoryID = Directories.insert(Directory); 1576 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source)); 1577 } 1578 unsigned MachineModuleInfo::RecordSource(const CompileUnitDesc *CompileUnit) { 1579 return RecordSource(CompileUnit->getDirectory(), 1580 CompileUnit->getFileName()); 1581 } 1582 1583 /// RecordRegionStart - Indicate the start of a region. 1584 /// 1585 unsigned MachineModuleInfo::RecordRegionStart(Value *V) { 1586 // FIXME - need to be able to handle split scopes because of bb cloning. 1587 DebugInfoDesc *ScopeDesc = DR.Deserialize(V); 1588 DebugScope *Scope = getOrCreateScope(ScopeDesc); 1589 unsigned ID = NextLabelID(); 1590 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID); 1591 return ID; 1592 } 1593 1594 /// RecordRegionEnd - Indicate the end of a region. 1595 /// 1596 unsigned MachineModuleInfo::RecordRegionEnd(Value *V) { 1597 // FIXME - need to be able to handle split scopes because of bb cloning. 1598 DebugInfoDesc *ScopeDesc = DR.Deserialize(V); 1599 DebugScope *Scope = getOrCreateScope(ScopeDesc); 1600 unsigned ID = NextLabelID(); 1601 Scope->setEndLabelID(ID); 1602 return ID; 1603 } 1604 1605 /// RecordVariable - Indicate the declaration of a local variable. 1606 /// 1607 void MachineModuleInfo::RecordVariable(Value *V, unsigned FrameIndex) { 1608 VariableDesc *VD = cast<VariableDesc>(DR.Deserialize(V)); 1609 DebugScope *Scope = getOrCreateScope(VD->getContext()); 1610 DebugVariable *DV = new DebugVariable(VD, FrameIndex); 1611 Scope->AddVariable(DV); 1612 } 1613 1614 /// getOrCreateScope - Returns the scope associated with the given descriptor. 1615 /// 1616 DebugScope *MachineModuleInfo::getOrCreateScope(DebugInfoDesc *ScopeDesc) { 1617 DebugScope *&Slot = ScopeMap[ScopeDesc]; 1618 if (!Slot) { 1619 // FIXME - breaks down when the context is an inlined function. 1620 DebugInfoDesc *ParentDesc = NULL; 1621 if (BlockDesc *Block = dyn_cast<BlockDesc>(ScopeDesc)) { 1622 ParentDesc = Block->getContext(); 1623 } 1624 DebugScope *Parent = ParentDesc ? getOrCreateScope(ParentDesc) : NULL; 1625 Slot = new DebugScope(Parent, ScopeDesc); 1626 if (Parent) { 1627 Parent->AddScope(Slot); 1628 } else if (RootScope) { 1629 // FIXME - Add inlined function scopes to the root so we can delete 1630 // them later. Long term, handle inlined functions properly. 1631 RootScope->AddScope(Slot); 1632 } else { 1633 // First function is top level function. 1634 RootScope = Slot; 1635 } 1636 } 1637 return Slot; 1638 } 1639 1640 //===----------------------------------------------------------------------===// 1641 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows 1642 /// a info consumer to determine if the range of two labels is empty, by seeing 1643 /// if the labels map to the same reduced label. 1644 1645 namespace llvm { 1646 1647 struct DebugLabelFolder : public MachineFunctionPass { 1648 virtual bool runOnMachineFunction(MachineFunction &MF); 1649 virtual const char *getPassName() const { return "Label Folder"; } 1650 }; 1651 1652 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) { 1653 // Get machine module info. 1654 MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>(); 1655 if (!MMI) return false; 1656 // Get target instruction info. 1657 const TargetInstrInfo *TII = MF.getTarget().getInstrInfo(); 1658 if (!TII) return false; 1659 1660 // Track if change is made. 1661 bool MadeChange = false; 1662 // No prior label to begin. 1663 unsigned PriorLabel = 0; 1664 1665 // Iterate through basic blocks. 1666 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); 1667 BB != E; ++BB) { 1668 // Iterate through instructions. 1669 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 1670 // Is it a label. 1671 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) { 1672 // The label ID # is always operand #0, an immediate. 1673 unsigned NextLabel = I->getOperand(0).getImm(); 1674 1675 // If there was an immediate prior label. 1676 if (PriorLabel) { 1677 // Remap the current label to prior label. 1678 MMI->RemapLabel(NextLabel, PriorLabel); 1679 // Delete the current label. 1680 I = BB->erase(I); 1681 // Indicate a change has been made. 1682 MadeChange = true; 1683 continue; 1684 } else { 1685 // Start a new round. 1686 PriorLabel = NextLabel; 1687 } 1688 } else { 1689 // No consecutive labels. 1690 PriorLabel = 0; 1691 } 1692 1693 ++I; 1694 } 1695 } 1696 1697 return MadeChange; 1698 } 1699 1700 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); } 1701 1702 } 1703 1704