MachineModuleInfo.h revision 10fff6078a7bc42f142c536bd55e9569253b280b
1//===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Collect meta information for a module. This information should be in a 11// neutral form that can be used by different debugging and exception handling 12// schemes. 13// 14// The organization of information is primarily clustered around the source 15// compile units. The main exception is source line correspondence where 16// inlining may interleave code from various compile units. 17// 18// The following information can be retrieved from the MachineModuleInfo. 19// 20// -- Source directories - Directories are uniqued based on their canonical 21// string and assigned a sequential numeric ID (base 1.) 22// -- Source files - Files are also uniqued based on their name and directory 23// ID. A file ID is sequential number (base 1.) 24// -- Source line correspondence - A vector of file ID, line#, column# triples. 25// A DEBUG_LOCATION instruction is generated by the DAG Legalizer 26// corresponding to each entry in the source line list. This allows a debug 27// emitter to generate labels referenced by debug information tables. 28// 29//===----------------------------------------------------------------------===// 30 31#ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H 32#define LLVM_CODEGEN_MACHINEMODULEINFO_H 33 34#include "llvm/Support/Dwarf.h" 35#include "llvm/Support/DataTypes.h" 36#include "llvm/ADT/SmallVector.h" 37#include "llvm/ADT/DenseMap.h" 38#include "llvm/ADT/UniqueVector.h" 39#include "llvm/ADT/SmallPtrSet.h" 40#include "llvm/GlobalValue.h" 41#include "llvm/Pass.h" 42 43namespace llvm { 44 45//===----------------------------------------------------------------------===// 46// Forward declarations. 47class Constant; 48class DebugInfoDesc; 49class GlobalVariable; 50class MachineBasicBlock; 51class MachineFunction; 52class MachineMove; 53class Module; 54class PointerType; 55class StructType; 56 57//===----------------------------------------------------------------------===// 58// Debug info constants. 59 60enum { 61 LLVMDebugVersion = (6 << 16), // Current version of debug information. 62 LLVMDebugVersion5 = (5 << 16), // Constant for version 5. 63 LLVMDebugVersion4 = (4 << 16), // Constant for version 4. 64 LLVMDebugVersionMask = 0xffff0000 // Mask for version number. 65}; 66 67//===----------------------------------------------------------------------===// 68/// DIVisitor - Subclasses of this class apply steps to each of the fields in 69/// the supplied DebugInfoDesc. 70class DIVisitor { 71public: 72 DIVisitor() {} 73 virtual ~DIVisitor() {} 74 75 /// ApplyToFields - Target the visitor to each field of the debug information 76 /// descriptor. 77 void ApplyToFields(DebugInfoDesc *DD); 78 79 /// Apply - Subclasses override each of these methods to perform the 80 /// appropriate action for the type of field. 81 virtual void Apply(int &Field) = 0; 82 virtual void Apply(unsigned &Field) = 0; 83 virtual void Apply(int64_t &Field) = 0; 84 virtual void Apply(uint64_t &Field) = 0; 85 virtual void Apply(bool &Field) = 0; 86 virtual void Apply(std::string &Field) = 0; 87 virtual void Apply(DebugInfoDesc *&Field) = 0; 88 virtual void Apply(GlobalVariable *&Field) = 0; 89 virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0; 90}; 91 92//===----------------------------------------------------------------------===// 93/// DebugInfoDesc - This class is the base class for debug info descriptors. 94/// 95class DebugInfoDesc { 96private: 97 unsigned Tag; // Content indicator. Dwarf values are 98 // used but that does not limit use to 99 // Dwarf writers. 100 101protected: 102 explicit DebugInfoDesc(unsigned T) : Tag(T | LLVMDebugVersion) {} 103 104public: 105 virtual ~DebugInfoDesc() {} 106 107 // Accessors 108 unsigned getTag() const { return Tag & ~LLVMDebugVersionMask; } 109 unsigned getVersion() const { return Tag & LLVMDebugVersionMask; } 110 void setTag(unsigned T) { Tag = T | LLVMDebugVersion; } 111 112 /// TagFromGlobal - Returns the tag number from a debug info descriptor 113 /// GlobalVariable. Return DIIValid if operand is not an unsigned int. 114 static unsigned TagFromGlobal(GlobalVariable *GV); 115 116 /// VersionFromGlobal - Returns the version number from a debug info 117 /// descriptor GlobalVariable. Return DIIValid if operand is not an unsigned 118 /// int. 119 static unsigned VersionFromGlobal(GlobalVariable *GV); 120 121 /// DescFactory - Create an instance of debug info descriptor based on Tag. 122 /// Return NULL if not a recognized Tag. 123 static DebugInfoDesc *DescFactory(unsigned Tag); 124 125 /// getLinkage - get linkage appropriate for this type of descriptor. 126 /// 127 virtual GlobalValue::LinkageTypes getLinkage() const; 128 129 //===--------------------------------------------------------------------===// 130 // Subclasses should supply the following static methods. 131 132 // Implement isa/cast/dyncast. 133 static bool classof(const DebugInfoDesc *) { return true; } 134 135 //===--------------------------------------------------------------------===// 136 // Subclasses should supply the following virtual methods. 137 138 /// ApplyToFields - Target the vistor to the fields of the descriptor. 139 /// 140 virtual void ApplyToFields(DIVisitor *Visitor); 141 142 /// getDescString - Return a string used to compose global names and labels. 143 /// 144 virtual const char *getDescString() const = 0; 145 146 /// getTypeString - Return a string used to label this descriptor's type. 147 /// 148 virtual const char *getTypeString() const = 0; 149 150#ifndef NDEBUG 151 virtual void dump() = 0; 152#endif 153}; 154 155//===----------------------------------------------------------------------===// 156/// AnchorDesc - Descriptors of this class act as markers for identifying 157/// descriptors of certain groups. 158class AnchoredDesc; 159class AnchorDesc : public DebugInfoDesc { 160private: 161 unsigned AnchorTag; // Tag number of descriptors anchored 162 // by this object. 163 164public: 165 AnchorDesc(); 166 explicit AnchorDesc(AnchoredDesc *D); 167 168 // Accessors 169 unsigned getAnchorTag() const { return AnchorTag; } 170 171 // Implement isa/cast/dyncast. 172 static bool classof(const AnchorDesc *) { return true; } 173 static bool classof(const DebugInfoDesc *D); 174 175 /// getLinkage - get linkage appropriate for this type of descriptor. 176 /// 177 virtual GlobalValue::LinkageTypes getLinkage() const; 178 179 /// ApplyToFields - Target the visitor to the fields of the AnchorDesc. 180 /// 181 virtual void ApplyToFields(DIVisitor *Visitor); 182 183 /// getDescString - Return a string used to compose global names and labels. 184 /// 185 virtual const char *getDescString() const; 186 187 /// getTypeString - Return a string used to label this descriptor's type. 188 /// 189 virtual const char *getTypeString() const; 190 191#ifndef NDEBUG 192 virtual void dump(); 193#endif 194}; 195 196//===----------------------------------------------------------------------===// 197/// AnchoredDesc - This class manages anchors for a variety of top level 198/// descriptors. 199class AnchoredDesc : public DebugInfoDesc { 200private: 201 DebugInfoDesc *Anchor; // Anchor for all descriptors of the 202 // same type. 203 204protected: 205 206 explicit AnchoredDesc(unsigned T); 207 208public: 209 // Accessors. 210 AnchorDesc *getAnchor() const { return static_cast<AnchorDesc *>(Anchor); } 211 void setAnchor(AnchorDesc *A) { Anchor = static_cast<DebugInfoDesc *>(A); } 212 213 //===--------------------------------------------------------------------===// 214 // Subclasses should supply the following virtual methods. 215 216 /// getAnchorString - Return a string used to label descriptor's anchor. 217 /// 218 virtual const char *getAnchorString() const = 0; 219 220 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc. 221 /// 222 virtual void ApplyToFields(DIVisitor *Visitor); 223}; 224 225//===----------------------------------------------------------------------===// 226/// CompileUnitDesc - This class packages debug information associated with a 227/// source/header file. 228class CompileUnitDesc : public AnchoredDesc { 229private: 230 unsigned Language; // Language number (ex. DW_LANG_C89.) 231 std::string FileName; // Source file name. 232 std::string Directory; // Source file directory. 233 std::string Producer; // Compiler string. 234 235public: 236 CompileUnitDesc(); 237 238 239 // Accessors 240 unsigned getLanguage() const { return Language; } 241 const std::string &getFileName() const { return FileName; } 242 const std::string &getDirectory() const { return Directory; } 243 const std::string &getProducer() const { return Producer; } 244 void setLanguage(unsigned L) { Language = L; } 245 void setFileName(const std::string &FN) { FileName = FN; } 246 void setDirectory(const std::string &D) { Directory = D; } 247 void setProducer(const std::string &P) { Producer = P; } 248 249 // FIXME - Need translation unit getter/setter. 250 251 // Implement isa/cast/dyncast. 252 static bool classof(const CompileUnitDesc *) { return true; } 253 static bool classof(const DebugInfoDesc *D); 254 255 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc. 256 /// 257 virtual void ApplyToFields(DIVisitor *Visitor); 258 259 /// getDescString - Return a string used to compose global names and labels. 260 /// 261 virtual const char *getDescString() const; 262 263 /// getTypeString - Return a string used to label this descriptor's type. 264 /// 265 virtual const char *getTypeString() const; 266 267 /// getAnchorString - Return a string used to label this descriptor's anchor. 268 /// 269 static const char *const AnchorString; 270 virtual const char *getAnchorString() const; 271 272#ifndef NDEBUG 273 virtual void dump(); 274#endif 275}; 276 277//===----------------------------------------------------------------------===// 278/// TypeDesc - This class packages debug information associated with a type. 279/// 280class TypeDesc : public DebugInfoDesc { 281private: 282 enum { 283 FlagPrivate = 1 << 0, 284 FlagProtected = 1 << 1 285 }; 286 DebugInfoDesc *Context; // Context debug descriptor. 287 std::string Name; // Type name (may be empty.) 288 DebugInfoDesc *File; // Defined compile unit (may be NULL.) 289 unsigned Line; // Defined line# (may be zero.) 290 uint64_t Size; // Type bit size (may be zero.) 291 uint64_t Align; // Type bit alignment (may be zero.) 292 uint64_t Offset; // Type bit offset (may be zero.) 293 unsigned Flags; // Miscellaneous flags. 294 295public: 296 explicit TypeDesc(unsigned T); 297 298 // Accessors 299 DebugInfoDesc *getContext() const { return Context; } 300 const std::string &getName() const { return Name; } 301 CompileUnitDesc *getFile() const { 302 return static_cast<CompileUnitDesc *>(File); 303 } 304 unsigned getLine() const { return Line; } 305 uint64_t getSize() const { return Size; } 306 uint64_t getAlign() const { return Align; } 307 uint64_t getOffset() const { return Offset; } 308 bool isPrivate() const { 309 return (Flags & FlagPrivate) != 0; 310 } 311 bool isProtected() const { 312 return (Flags & FlagProtected) != 0; 313 } 314 void setContext(DebugInfoDesc *C) { Context = C; } 315 void setName(const std::string &N) { Name = N; } 316 void setFile(CompileUnitDesc *U) { 317 File = static_cast<DebugInfoDesc *>(U); 318 } 319 void setLine(unsigned L) { Line = L; } 320 void setSize(uint64_t S) { Size = S; } 321 void setAlign(uint64_t A) { Align = A; } 322 void setOffset(uint64_t O) { Offset = O; } 323 void setIsPrivate() { Flags |= FlagPrivate; } 324 void setIsProtected() { Flags |= FlagProtected; } 325 326 /// ApplyToFields - Target the visitor to the fields of the TypeDesc. 327 /// 328 virtual void ApplyToFields(DIVisitor *Visitor); 329 330 /// getDescString - Return a string used to compose global names and labels. 331 /// 332 virtual const char *getDescString() const; 333 334 /// getTypeString - Return a string used to label this descriptor's type. 335 /// 336 virtual const char *getTypeString() const; 337 338#ifndef NDEBUG 339 virtual void dump(); 340#endif 341}; 342 343//===----------------------------------------------------------------------===// 344/// BasicTypeDesc - This class packages debug information associated with a 345/// basic type (eg. int, bool, double.) 346class BasicTypeDesc : public TypeDesc { 347private: 348 unsigned Encoding; // Type encoding. 349 350public: 351 BasicTypeDesc(); 352 353 // Accessors 354 unsigned getEncoding() const { return Encoding; } 355 void setEncoding(unsigned E) { Encoding = E; } 356 357 // Implement isa/cast/dyncast. 358 static bool classof(const BasicTypeDesc *) { return true; } 359 static bool classof(const DebugInfoDesc *D); 360 361 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc. 362 /// 363 virtual void ApplyToFields(DIVisitor *Visitor); 364 365 /// getDescString - Return a string used to compose global names and labels. 366 /// 367 virtual const char *getDescString() const; 368 369 /// getTypeString - Return a string used to label this descriptor's type. 370 /// 371 virtual const char *getTypeString() const; 372 373#ifndef NDEBUG 374 virtual void dump(); 375#endif 376}; 377 378 379//===----------------------------------------------------------------------===// 380/// DerivedTypeDesc - This class packages debug information associated with a 381/// derived types (eg., typedef, pointer, reference.) 382class DerivedTypeDesc : public TypeDesc { 383private: 384 DebugInfoDesc *FromType; // Type derived from. 385 386public: 387 explicit DerivedTypeDesc(unsigned T); 388 389 // Accessors 390 TypeDesc *getFromType() const { 391 return static_cast<TypeDesc *>(FromType); 392 } 393 void setFromType(TypeDesc *F) { 394 FromType = static_cast<DebugInfoDesc *>(F); 395 } 396 397 // Implement isa/cast/dyncast. 398 static bool classof(const DerivedTypeDesc *) { return true; } 399 static bool classof(const DebugInfoDesc *D); 400 401 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc. 402 /// 403 virtual void ApplyToFields(DIVisitor *Visitor); 404 405 /// getDescString - Return a string used to compose global names and labels. 406 /// 407 virtual const char *getDescString() const; 408 409 /// getTypeString - Return a string used to label this descriptor's type. 410 /// 411 virtual const char *getTypeString() const; 412 413#ifndef NDEBUG 414 virtual void dump(); 415#endif 416}; 417 418//===----------------------------------------------------------------------===// 419/// CompositeTypeDesc - This class packages debug information associated with a 420/// array/struct types (eg., arrays, struct, union, enums.) 421class CompositeTypeDesc : public DerivedTypeDesc { 422private: 423 std::vector<DebugInfoDesc *> Elements;// Information used to compose type. 424 425public: 426 explicit CompositeTypeDesc(unsigned T); 427 428 // Accessors 429 std::vector<DebugInfoDesc *> &getElements() { return Elements; } 430 431 // Implement isa/cast/dyncast. 432 static bool classof(const CompositeTypeDesc *) { return true; } 433 static bool classof(const DebugInfoDesc *D); 434 435 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc. 436 /// 437 virtual void ApplyToFields(DIVisitor *Visitor); 438 439 /// getDescString - Return a string used to compose global names and labels. 440 /// 441 virtual const char *getDescString() const; 442 443 /// getTypeString - Return a string used to label this descriptor's type. 444 /// 445 virtual const char *getTypeString() const; 446 447#ifndef NDEBUG 448 virtual void dump(); 449#endif 450}; 451 452//===----------------------------------------------------------------------===// 453/// SubrangeDesc - This class packages debug information associated with integer 454/// value ranges. 455class SubrangeDesc : public DebugInfoDesc { 456private: 457 int64_t Lo; // Low value of range. 458 int64_t Hi; // High value of range. 459 460public: 461 SubrangeDesc(); 462 463 // Accessors 464 int64_t getLo() const { return Lo; } 465 int64_t getHi() const { return Hi; } 466 void setLo(int64_t L) { Lo = L; } 467 void setHi(int64_t H) { Hi = H; } 468 469 // Implement isa/cast/dyncast. 470 static bool classof(const SubrangeDesc *) { return true; } 471 static bool classof(const DebugInfoDesc *D); 472 473 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc. 474 /// 475 virtual void ApplyToFields(DIVisitor *Visitor); 476 477 /// getDescString - Return a string used to compose global names and labels. 478 /// 479 virtual const char *getDescString() const; 480 481 /// getTypeString - Return a string used to label this descriptor's type. 482 /// 483 virtual const char *getTypeString() const; 484 485#ifndef NDEBUG 486 virtual void dump(); 487#endif 488}; 489 490//===----------------------------------------------------------------------===// 491/// EnumeratorDesc - This class packages debug information associated with 492/// named integer constants. 493class EnumeratorDesc : public DebugInfoDesc { 494private: 495 std::string Name; // Enumerator name. 496 int64_t Value; // Enumerator value. 497 498public: 499 EnumeratorDesc(); 500 501 // Accessors 502 const std::string &getName() const { return Name; } 503 int64_t getValue() const { return Value; } 504 void setName(const std::string &N) { Name = N; } 505 void setValue(int64_t V) { Value = V; } 506 507 // Implement isa/cast/dyncast. 508 static bool classof(const EnumeratorDesc *) { return true; } 509 static bool classof(const DebugInfoDesc *D); 510 511 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc. 512 /// 513 virtual void ApplyToFields(DIVisitor *Visitor); 514 515 /// getDescString - Return a string used to compose global names and labels. 516 /// 517 virtual const char *getDescString() const; 518 519 /// getTypeString - Return a string used to label this descriptor's type. 520 /// 521 virtual const char *getTypeString() const; 522 523#ifndef NDEBUG 524 virtual void dump(); 525#endif 526}; 527 528//===----------------------------------------------------------------------===// 529/// VariableDesc - This class packages debug information associated with a 530/// subprogram variable. 531/// 532class VariableDesc : public DebugInfoDesc { 533private: 534 DebugInfoDesc *Context; // Context debug descriptor. 535 std::string Name; // Type name (may be empty.) 536 DebugInfoDesc *File; // Defined compile unit (may be NULL.) 537 unsigned Line; // Defined line# (may be zero.) 538 DebugInfoDesc *TyDesc; // Type of variable. 539 540public: 541 explicit VariableDesc(unsigned T); 542 543 // Accessors 544 DebugInfoDesc *getContext() const { return Context; } 545 const std::string &getName() const { return Name; } 546 CompileUnitDesc *getFile() const { 547 return static_cast<CompileUnitDesc *>(File); 548 } 549 unsigned getLine() const { return Line; } 550 TypeDesc *getType() const { 551 return static_cast<TypeDesc *>(TyDesc); 552 } 553 void setContext(DebugInfoDesc *C) { Context = C; } 554 void setName(const std::string &N) { Name = N; } 555 void setFile(CompileUnitDesc *U) { 556 File = static_cast<DebugInfoDesc *>(U); 557 } 558 void setLine(unsigned L) { Line = L; } 559 void setType(TypeDesc *T) { 560 TyDesc = static_cast<DebugInfoDesc *>(T); 561 } 562 563 // Implement isa/cast/dyncast. 564 static bool classof(const VariableDesc *) { return true; } 565 static bool classof(const DebugInfoDesc *D); 566 567 /// ApplyToFields - Target the visitor to the fields of the VariableDesc. 568 /// 569 virtual void ApplyToFields(DIVisitor *Visitor); 570 571 /// getDescString - Return a string used to compose global names and labels. 572 /// 573 virtual const char *getDescString() const; 574 575 /// getTypeString - Return a string used to label this descriptor's type. 576 /// 577 virtual const char *getTypeString() const; 578 579#ifndef NDEBUG 580 virtual void dump(); 581#endif 582}; 583 584//===----------------------------------------------------------------------===// 585/// GlobalDesc - This class is the base descriptor for global functions and 586/// variables. 587class GlobalDesc : public AnchoredDesc { 588private: 589 DebugInfoDesc *Context; // Context debug descriptor. 590 std::string Name; // Global name. 591 std::string FullName; // Fully qualified name. 592 std::string LinkageName; // Name for binding to MIPS linkage. 593 DebugInfoDesc *File; // Defined compile unit (may be NULL.) 594 unsigned Line; // Defined line# (may be zero.) 595 DebugInfoDesc *TyDesc; // Type debug descriptor. 596 bool IsStatic; // Is the global a static. 597 bool IsDefinition; // Is the global defined in context. 598 599protected: 600 explicit GlobalDesc(unsigned T); 601 602public: 603 // Accessors 604 DebugInfoDesc *getContext() const { return Context; } 605 const std::string &getName() const { return Name; } 606 const std::string &getFullName() const { return FullName; } 607 const std::string &getLinkageName() const { return LinkageName; } 608 CompileUnitDesc *getFile() const { 609 return static_cast<CompileUnitDesc *>(File); 610 } 611 unsigned getLine() const { return Line; } 612 TypeDesc *getType() const { 613 return static_cast<TypeDesc *>(TyDesc); 614 } 615 bool isStatic() const { return IsStatic; } 616 bool isDefinition() const { return IsDefinition; } 617 void setContext(DebugInfoDesc *C) { Context = C; } 618 void setName(const std::string &N) { Name = N; } 619 void setFullName(const std::string &N) { FullName = N; } 620 void setLinkageName(const std::string &N) { LinkageName = N; } 621 void setFile(CompileUnitDesc *U) { 622 File = static_cast<DebugInfoDesc *>(U); 623 } 624 void setLine(unsigned L) { Line = L; } 625 void setType(TypeDesc *T) { 626 TyDesc = static_cast<DebugInfoDesc *>(T); 627 } 628 void setIsStatic(bool IS) { IsStatic = IS; } 629 void setIsDefinition(bool ID) { IsDefinition = ID; } 630 631 /// ApplyToFields - Target the visitor to the fields of the GlobalDesc. 632 /// 633 virtual void ApplyToFields(DIVisitor *Visitor); 634}; 635 636//===----------------------------------------------------------------------===// 637/// GlobalVariableDesc - This class packages debug information associated with a 638/// GlobalVariable. 639class GlobalVariableDesc : public GlobalDesc { 640private: 641 GlobalVariable *Global; // llvm global. 642 643public: 644 GlobalVariableDesc(); 645 646 // Accessors. 647 GlobalVariable *getGlobalVariable() const { return Global; } 648 void setGlobalVariable(GlobalVariable *GV) { Global = GV; } 649 650 // Implement isa/cast/dyncast. 651 static bool classof(const GlobalVariableDesc *) { return true; } 652 static bool classof(const DebugInfoDesc *D); 653 654 /// ApplyToFields - Target the visitor to the fields of the 655 /// GlobalVariableDesc. 656 virtual void ApplyToFields(DIVisitor *Visitor); 657 658 /// getDescString - Return a string used to compose global names and labels. 659 /// 660 virtual const char *getDescString() const; 661 662 /// getTypeString - Return a string used to label this descriptor's type. 663 /// 664 virtual const char *getTypeString() const; 665 666 /// getAnchorString - Return a string used to label this descriptor's anchor. 667 /// 668 static const char *const AnchorString; 669 virtual const char *getAnchorString() const; 670 671#ifndef NDEBUG 672 virtual void dump(); 673#endif 674}; 675 676//===----------------------------------------------------------------------===// 677/// SubprogramDesc - This class packages debug information associated with a 678/// subprogram/function. 679class SubprogramDesc : public GlobalDesc { 680private: 681 682public: 683 SubprogramDesc(); 684 685 // Accessors 686 687 // Implement isa/cast/dyncast. 688 static bool classof(const SubprogramDesc *) { return true; } 689 static bool classof(const DebugInfoDesc *D); 690 691 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc. 692 /// 693 virtual void ApplyToFields(DIVisitor *Visitor); 694 695 /// getDescString - Return a string used to compose global names and labels. 696 /// 697 virtual const char *getDescString() const; 698 699 /// getTypeString - Return a string used to label this descriptor's type. 700 /// 701 virtual const char *getTypeString() const; 702 703 /// getAnchorString - Return a string used to label this descriptor's anchor. 704 /// 705 static const char *const AnchorString; 706 virtual const char *getAnchorString() const; 707 708#ifndef NDEBUG 709 virtual void dump(); 710#endif 711}; 712 713//===----------------------------------------------------------------------===// 714/// BlockDesc - This descriptor groups variables and blocks nested in a block. 715/// 716class BlockDesc : public DebugInfoDesc { 717private: 718 DebugInfoDesc *Context; // Context debug descriptor. 719 720public: 721 BlockDesc(); 722 723 // Accessors 724 DebugInfoDesc *getContext() const { return Context; } 725 void setContext(DebugInfoDesc *C) { Context = C; } 726 727 // Implement isa/cast/dyncast. 728 static bool classof(const BlockDesc *) { return true; } 729 static bool classof(const DebugInfoDesc *D); 730 731 /// ApplyToFields - Target the visitor to the fields of the BlockDesc. 732 /// 733 virtual void ApplyToFields(DIVisitor *Visitor); 734 735 /// getDescString - Return a string used to compose global names and labels. 736 /// 737 virtual const char *getDescString() const; 738 739 /// getTypeString - Return a string used to label this descriptor's type. 740 /// 741 virtual const char *getTypeString() const; 742 743#ifndef NDEBUG 744 virtual void dump(); 745#endif 746}; 747 748//===----------------------------------------------------------------------===// 749/// DIDeserializer - This class is responsible for casting GlobalVariables 750/// into DebugInfoDesc objects. 751class DIDeserializer { 752 // Previously defined gloabls. 753 std::map<GlobalVariable*, DebugInfoDesc*> GlobalDescs; 754public: 755 const std::map<GlobalVariable *, DebugInfoDesc *> &getGlobalDescs() const { 756 return GlobalDescs; 757 } 758 759 /// Deserialize - Reconstitute a GlobalVariable into it's component 760 /// DebugInfoDesc objects. 761 DebugInfoDesc *Deserialize(Value *V); 762 DebugInfoDesc *Deserialize(GlobalVariable *GV); 763}; 764 765//===----------------------------------------------------------------------===// 766/// DISerializer - This class is responsible for casting DebugInfoDesc objects 767/// into GlobalVariables. 768class DISerializer { 769 Module *M; // Definition space module. 770 PointerType *StrPtrTy; // A "i8*" type. Created lazily. 771 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily. 772 773 // Types per Tag. Created lazily. 774 std::map<unsigned, StructType *> TagTypes; 775 776 // Previously defined descriptors. 777 DenseMap<DebugInfoDesc *, GlobalVariable *> DescGlobals; 778 779 // Previously defined strings. 780 DenseMap<const char *, Constant*> StringCache; 781public: 782 DISerializer() 783 : M(NULL), StrPtrTy(NULL), EmptyStructPtrTy(NULL), TagTypes(), 784 DescGlobals(), StringCache() 785 {} 786 787 // Accessors 788 Module *getModule() const { return M; }; 789 void setModule(Module *module) { M = module; } 790 791 /// getStrPtrType - Return a "i8*" type. 792 /// 793 const PointerType *getStrPtrType(); 794 795 /// getEmptyStructPtrType - Return a "{ }*" type. 796 /// 797 const PointerType *getEmptyStructPtrType(); 798 799 /// getTagType - Return the type describing the specified descriptor (via 800 /// tag.) 801 const StructType *getTagType(DebugInfoDesc *DD); 802 803 /// getString - Construct the string as constant string global. 804 /// 805 Constant *getString(const std::string &String); 806 807 /// Serialize - Recursively cast the specified descriptor into a 808 /// GlobalVariable so that it can be serialized to a .bc or .ll file. 809 GlobalVariable *Serialize(DebugInfoDesc *DD); 810 811 /// addDescriptor - Directly connect DD with existing GV. 812 void addDescriptor(DebugInfoDesc *DD, GlobalVariable *GV); 813}; 814 815//===----------------------------------------------------------------------===// 816/// DIVerifier - This class is responsible for verifying the given network of 817/// GlobalVariables are valid as DebugInfoDesc objects. 818class DIVerifier { 819 enum { 820 Unknown = 0, 821 Invalid, 822 Valid 823 }; 824 DenseMap<GlobalVariable *, unsigned> Validity; // Tracks prior results. 825 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type. 826public: 827 DIVerifier() 828 : Validity(), Counts() 829 {} 830 831 /// Verify - Return true if the GlobalVariable appears to be a valid 832 /// serialization of a DebugInfoDesc. 833 bool Verify(Value *V); 834 bool Verify(GlobalVariable *GV); 835 836 /// isVerified - Return true if the specified GV has already been 837 /// verified as a debug information descriptor. 838 bool isVerified(GlobalVariable *GV); 839}; 840 841//===----------------------------------------------------------------------===// 842/// SourceLineInfo - This class is used to record source line correspondence. 843/// 844class SourceLineInfo { 845 unsigned Line; // Source line number. 846 unsigned Column; // Source column. 847 unsigned SourceID; // Source ID number. 848 unsigned LabelID; // Label in code ID number. 849public: 850 SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I) 851 : Line(L), Column(C), SourceID(S), LabelID(I) {} 852 853 // Accessors 854 unsigned getLine() const { return Line; } 855 unsigned getColumn() const { return Column; } 856 unsigned getSourceID() const { return SourceID; } 857 unsigned getLabelID() const { return LabelID; } 858}; 859 860//===----------------------------------------------------------------------===// 861/// SourceFileInfo - This class is used to track source information. 862/// 863class SourceFileInfo { 864 unsigned DirectoryID; // Directory ID number. 865 std::string Name; // File name (not including directory.) 866public: 867 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {} 868 869 // Accessors 870 unsigned getDirectoryID() const { return DirectoryID; } 871 const std::string &getName() const { return Name; } 872 873 /// operator== - Used by UniqueVector to locate entry. 874 /// 875 bool operator==(const SourceFileInfo &SI) const { 876 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName(); 877 } 878 879 /// operator< - Used by UniqueVector to locate entry. 880 /// 881 bool operator<(const SourceFileInfo &SI) const { 882 return getDirectoryID() < SI.getDirectoryID() || 883 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName()); 884 } 885}; 886 887//===----------------------------------------------------------------------===// 888/// DebugVariable - This class is used to track local variable information. 889/// 890class DebugVariable { 891private: 892 VariableDesc *Desc; // Variable Descriptor. 893 unsigned FrameIndex; // Variable frame index. 894 895public: 896 DebugVariable(VariableDesc *D, unsigned I) 897 : Desc(D) 898 , FrameIndex(I) 899 {} 900 901 // Accessors. 902 VariableDesc *getDesc() const { return Desc; } 903 unsigned getFrameIndex() const { return FrameIndex; } 904}; 905 906//===----------------------------------------------------------------------===// 907/// DebugScope - This class is used to track scope information. 908/// 909class DebugScope { 910private: 911 DebugScope *Parent; // Parent to this scope. 912 DebugInfoDesc *Desc; // Debug info descriptor for scope. 913 // Either subprogram or block. 914 unsigned StartLabelID; // Label ID of the beginning of scope. 915 unsigned EndLabelID; // Label ID of the end of scope. 916 std::vector<DebugScope *> Scopes; // Scopes defined in scope. 917 std::vector<DebugVariable *> Variables;// Variables declared in scope. 918 919public: 920 DebugScope(DebugScope *P, DebugInfoDesc *D) 921 : Parent(P) 922 , Desc(D) 923 , StartLabelID(0) 924 , EndLabelID(0) 925 , Scopes() 926 , Variables() 927 {} 928 ~DebugScope(); 929 930 // Accessors. 931 DebugScope *getParent() const { return Parent; } 932 DebugInfoDesc *getDesc() const { return Desc; } 933 unsigned getStartLabelID() const { return StartLabelID; } 934 unsigned getEndLabelID() const { return EndLabelID; } 935 std::vector<DebugScope *> &getScopes() { return Scopes; } 936 std::vector<DebugVariable *> &getVariables() { return Variables; } 937 void setStartLabelID(unsigned S) { StartLabelID = S; } 938 void setEndLabelID(unsigned E) { EndLabelID = E; } 939 940 /// AddScope - Add a scope to the scope. 941 /// 942 void AddScope(DebugScope *S) { Scopes.push_back(S); } 943 944 /// AddVariable - Add a variable to the scope. 945 /// 946 void AddVariable(DebugVariable *V) { Variables.push_back(V); } 947}; 948 949//===----------------------------------------------------------------------===// 950/// LandingPadInfo - This structure is used to retain landing pad info for 951/// the current function. 952/// 953struct LandingPadInfo { 954 MachineBasicBlock *LandingPadBlock; // Landing pad block. 955 SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke. 956 SmallVector<unsigned, 1> EndLabels; // Labels after invoke. 957 unsigned LandingPadLabel; // Label at beginning of landing pad. 958 Function *Personality; // Personality function. 959 std::vector<int> TypeIds; // List of type ids (filters negative) 960 961 explicit LandingPadInfo(MachineBasicBlock *MBB) 962 : LandingPadBlock(MBB) 963 , LandingPadLabel(0) 964 , Personality(NULL) 965 {} 966}; 967 968//===----------------------------------------------------------------------===// 969/// MachineModuleInfo - This class contains meta information specific to a 970/// module. Queries can be made by different debugging and exception handling 971/// schemes and reformated for specific use. 972/// 973class MachineModuleInfo : public ImmutablePass { 974private: 975 // Use the same deserializer/verifier for the module. 976 DIDeserializer DR; 977 DIVerifier VR; 978 979 // CompileUnits - Uniquing vector for compile units. 980 UniqueVector<CompileUnitDesc *> CompileUnits; 981 982 // Directories - Uniquing vector for directories. 983 UniqueVector<std::string> Directories; 984 985 // SourceFiles - Uniquing vector for source files. 986 UniqueVector<SourceFileInfo> SourceFiles; 987 988 // Lines - List of of source line correspondence. 989 std::vector<SourceLineInfo> Lines; 990 991 // LabelIDList - One entry per assigned label. Normally the entry is equal to 992 // the list index(+1). If the entry is zero then the label has been deleted. 993 // Any other value indicates the label has been deleted by is mapped to 994 // another label. 995 std::vector<unsigned> LabelIDList; 996 997 // ScopeMap - Tracks the scopes in the current function. 998 std::map<DebugInfoDesc *, DebugScope *> ScopeMap; 999 1000 // RootScope - Top level scope for the current function. 1001 // 1002 DebugScope *RootScope; 1003 1004 // FrameMoves - List of moves done by a function's prolog. Used to construct 1005 // frame maps by debug and exception handling consumers. 1006 std::vector<MachineMove> FrameMoves; 1007 1008 // LandingPads - List of LandingPadInfo describing the landing pad information 1009 // in the current function. 1010 std::vector<LandingPadInfo> LandingPads; 1011 1012 // TypeInfos - List of C++ TypeInfo used in the current function. 1013 // 1014 std::vector<GlobalVariable *> TypeInfos; 1015 1016 // FilterIds - List of typeids encoding filters used in the current function. 1017 // 1018 std::vector<unsigned> FilterIds; 1019 1020 // FilterEnds - List of the indices in FilterIds corresponding to filter 1021 // terminators. 1022 // 1023 std::vector<unsigned> FilterEnds; 1024 1025 // Personalities - Vector of all personality functions ever seen. Used to emit 1026 // common EH frames. 1027 std::vector<Function *> Personalities; 1028 1029 // UsedFunctions - the functions in the llvm.used list in a more easily 1030 // searchable format. 1031 SmallPtrSet<const Function *, 32> UsedFunctions; 1032 1033 bool CallsEHReturn; 1034 bool CallsUnwindInit; 1035public: 1036 static char ID; // Pass identification, replacement for typeid 1037 1038 MachineModuleInfo(); 1039 ~MachineModuleInfo(); 1040 1041 /// doInitialization - Initialize the state for a new module. 1042 /// 1043 bool doInitialization(); 1044 1045 /// doFinalization - Tear down the state after completion of a module. 1046 /// 1047 bool doFinalization(); 1048 1049 /// BeginFunction - Begin gathering function meta information. 1050 /// 1051 void BeginFunction(MachineFunction *MF); 1052 1053 /// EndFunction - Discard function meta information. 1054 /// 1055 void EndFunction(); 1056 1057 /// getDescFor - Convert a Value to a debug information descriptor. 1058 /// 1059 // FIXME - use new Value type when available. 1060 DebugInfoDesc *getDescFor(Value *V); 1061 1062 /// Verify - Verify that a Value is debug information descriptor. 1063 /// 1064 bool Verify(Value *V) { return VR.Verify(V); } 1065 1066 /// isVerified - Return true if the specified GV has already been 1067 /// verified as a debug information descriptor. 1068 bool isVerified(GlobalVariable *GV) { return VR.isVerified(GV); } 1069 1070 /// AnalyzeModule - Scan the module for global debug information. 1071 /// 1072 void AnalyzeModule(Module &M); 1073 1074 /// hasDebugInfo - Returns true if valid debug info is present. 1075 /// 1076 bool hasDebugInfo() const { return !CompileUnits.empty(); } 1077 1078 bool callsEHReturn() const { return CallsEHReturn; } 1079 void setCallsEHReturn(bool b) { CallsEHReturn = b; } 1080 1081 bool callsUnwindInit() const { return CallsUnwindInit; } 1082 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; } 1083 1084 /// NextLabelID - Return the next unique label id. 1085 /// 1086 unsigned NextLabelID() { 1087 unsigned ID = (unsigned)LabelIDList.size() + 1; 1088 LabelIDList.push_back(ID); 1089 return ID; 1090 } 1091 1092 /// RecordSourceLine - Records location information and associates it with a 1093 /// label. Returns a unique label ID used to generate a label and 1094 /// provide correspondence to the source line list. 1095 unsigned RecordSourceLine(unsigned Line, unsigned Column, unsigned Source); 1096 1097 /// InvalidateLabel - Inhibit use of the specified label # from 1098 /// MachineModuleInfo, for example because the code was deleted. 1099 void InvalidateLabel(unsigned LabelID) { 1100 // Remap to zero to indicate deletion. 1101 RemapLabel(LabelID, 0); 1102 } 1103 1104 /// RemapLabel - Indicate that a label has been merged into another. 1105 /// 1106 void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) { 1107 assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() && 1108 "Old label ID out of range."); 1109 assert(NewLabelID <= LabelIDList.size() && 1110 "New label ID out of range."); 1111 LabelIDList[OldLabelID - 1] = NewLabelID; 1112 } 1113 1114 /// MappedLabel - Find out the label's final ID. Zero indicates deletion. 1115 /// ID != Mapped ID indicates that the label was folded into another label. 1116 unsigned MappedLabel(unsigned LabelID) const { 1117 assert(LabelID <= LabelIDList.size() && "Debug label ID out of range."); 1118 return LabelID ? LabelIDList[LabelID - 1] : 0; 1119 } 1120 1121 /// RecordSource - Register a source file with debug info. Returns an source 1122 /// ID. 1123 unsigned RecordSource(const std::string &Directory, 1124 const std::string &Source); 1125 unsigned RecordSource(const CompileUnitDesc *CompileUnit); 1126 1127 /// getDirectories - Return the UniqueVector of std::string representing 1128 /// directories. 1129 const UniqueVector<std::string> &getDirectories() const { 1130 return Directories; 1131 } 1132 1133 /// getSourceFiles - Return the UniqueVector of source files. 1134 /// 1135 const UniqueVector<SourceFileInfo> &getSourceFiles() const { 1136 return SourceFiles; 1137 } 1138 1139 /// getSourceLines - Return a vector of source lines. 1140 /// 1141 const std::vector<SourceLineInfo> &getSourceLines() const { 1142 return Lines; 1143 } 1144 1145 /// SetupCompileUnits - Set up the unique vector of compile units. 1146 /// 1147 void SetupCompileUnits(Module &M); 1148 1149 /// getCompileUnits - Return a vector of debug compile units. 1150 /// 1151 const UniqueVector<CompileUnitDesc *> getCompileUnits() const; 1152 1153 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the 1154 /// named GlobalVariable. 1155 std::vector<GlobalVariable*> 1156 getGlobalVariablesUsing(Module &M, const std::string &RootName); 1157 1158 /// getAnchoredDescriptors - Return a vector of anchored debug descriptors. 1159 /// 1160 template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) { 1161 T Desc; 1162 std::vector<GlobalVariable *> Globals = 1163 getGlobalVariablesUsing(M, Desc.getAnchorString()); 1164 std::vector<T *> AnchoredDescs; 1165 for (unsigned i = 0, N = Globals.size(); i < N; ++i) { 1166 GlobalVariable *GV = Globals[i]; 1167 1168 // FIXME - In the short term, changes are too drastic to continue. 1169 if (DebugInfoDesc::TagFromGlobal(GV) == Desc.getTag() && 1170 DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) { 1171 AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV))); 1172 } 1173 } 1174 1175 return AnchoredDescs; 1176 } 1177 1178 /// RecordRegionStart - Indicate the start of a region. 1179 /// 1180 unsigned RecordRegionStart(Value *V); 1181 1182 /// RecordRegionEnd - Indicate the end of a region. 1183 /// 1184 unsigned RecordRegionEnd(Value *V); 1185 1186 /// RecordVariable - Indicate the declaration of a local variable. 1187 /// 1188 void RecordVariable(GlobalValue *GV, unsigned FrameIndex); 1189 1190 /// getRootScope - Return current functions root scope. 1191 /// 1192 DebugScope *getRootScope() { return RootScope; } 1193 1194 /// getOrCreateScope - Returns the scope associated with the given descriptor. 1195 /// 1196 DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc); 1197 1198 /// getFrameMoves - Returns a reference to a list of moves done in the current 1199 /// function's prologue. Used to construct frame maps for debug and exception 1200 /// handling comsumers. 1201 std::vector<MachineMove> &getFrameMoves() { return FrameMoves; } 1202 1203 //===-EH-----------------------------------------------------------------===// 1204 1205 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the 1206 /// specified MachineBasicBlock. 1207 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad); 1208 1209 /// addInvoke - Provide the begin and end labels of an invoke style call and 1210 /// associate it with a try landing pad block. 1211 void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel, 1212 unsigned EndLabel); 1213 1214 /// addLandingPad - Add a new panding pad. Returns the label ID for the 1215 /// landing pad entry. 1216 unsigned addLandingPad(MachineBasicBlock *LandingPad); 1217 1218 /// addPersonality - Provide the personality function for the exception 1219 /// information. 1220 void addPersonality(MachineBasicBlock *LandingPad, Function *Personality); 1221 1222 /// getPersonalityIndex - Get index of the current personality function inside 1223 /// Personalitites array 1224 unsigned getPersonalityIndex() const; 1225 1226 /// getPersonalities - Return array of personality functions ever seen. 1227 const std::vector<Function *>& getPersonalities() const { 1228 return Personalities; 1229 } 1230 1231 // UsedFunctions - Return set of the functions in the llvm.used list. 1232 const SmallPtrSet<const Function *, 32>& getUsedFunctions() const { 1233 return UsedFunctions; 1234 } 1235 1236 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. 1237 /// 1238 void addCatchTypeInfo(MachineBasicBlock *LandingPad, 1239 std::vector<GlobalVariable *> &TyInfo); 1240 1241 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. 1242 /// 1243 void addFilterTypeInfo(MachineBasicBlock *LandingPad, 1244 std::vector<GlobalVariable *> &TyInfo); 1245 1246 /// addCleanup - Add a cleanup action for a landing pad. 1247 /// 1248 void addCleanup(MachineBasicBlock *LandingPad); 1249 1250 /// getTypeIDFor - Return the type id for the specified typeinfo. This is 1251 /// function wide. 1252 unsigned getTypeIDFor(GlobalVariable *TI); 1253 1254 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is 1255 /// function wide. 1256 int getFilterIDFor(std::vector<unsigned> &TyIds); 1257 1258 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing 1259 /// pads. 1260 void TidyLandingPads(); 1261 1262 /// getLandingPads - Return a reference to the landing pad info for the 1263 /// current function. 1264 const std::vector<LandingPadInfo> &getLandingPads() const { 1265 return LandingPads; 1266 } 1267 1268 /// getTypeInfos - Return a reference to the C++ typeinfo for the current 1269 /// function. 1270 const std::vector<GlobalVariable *> &getTypeInfos() const { 1271 return TypeInfos; 1272 } 1273 1274 /// getFilterIds - Return a reference to the typeids encoding filters used in 1275 /// the current function. 1276 const std::vector<unsigned> &getFilterIds() const { 1277 return FilterIds; 1278 } 1279 1280 /// getPersonality - Return a personality function if available. The presence 1281 /// of one is required to emit exception handling info. 1282 Function *getPersonality() const; 1283 1284 DIDeserializer *getDIDeserializer() { return &DR; } 1285}; // End class MachineModuleInfo 1286 1287} // End llvm namespace 1288 1289#endif 1290