2
0

WinCodeViewLineTables.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===-- llvm/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.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. // This file contains support for writing line tables info into COFF files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_WINCODEVIEWLINETABLES_H
  14. #define LLVM_LIB_CODEGEN_ASMPRINTER_WINCODEVIEWLINETABLES_H
  15. #include "AsmPrinterHandler.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/CodeGen/AsmPrinter.h"
  20. #include "llvm/CodeGen/LexicalScopes.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineModuleInfo.h"
  23. #include "llvm/IR/DebugInfo.h"
  24. #include "llvm/IR/DebugLoc.h"
  25. #include "llvm/MC/MCStreamer.h"
  26. #include "llvm/Target/TargetLoweringObjectFile.h"
  27. namespace llvm {
  28. /// \brief Collects and handles line tables information in a CodeView format.
  29. class LLVM_LIBRARY_VISIBILITY WinCodeViewLineTables : public AsmPrinterHandler {
  30. AsmPrinter *Asm;
  31. DebugLoc PrevInstLoc;
  32. // For each function, store a vector of labels to its instructions, as well as
  33. // to the end of the function.
  34. struct FunctionInfo {
  35. SmallVector<MCSymbol *, 10> Instrs;
  36. MCSymbol *End;
  37. FunctionInfo() : End(nullptr) {}
  38. } *CurFn;
  39. typedef DenseMap<const Function *, FunctionInfo> FnDebugInfoTy;
  40. FnDebugInfoTy FnDebugInfo;
  41. // Store the functions we've visited in a vector so we can maintain a stable
  42. // order while emitting subsections.
  43. SmallVector<const Function *, 10> VisitedFunctions;
  44. // InstrInfoTy - Holds the Filename:LineNumber information for every
  45. // instruction with a unique debug location.
  46. struct InstrInfoTy {
  47. StringRef Filename;
  48. unsigned LineNumber;
  49. unsigned ColumnNumber;
  50. InstrInfoTy() : LineNumber(0), ColumnNumber(0) {}
  51. InstrInfoTy(StringRef Filename, unsigned LineNumber, unsigned ColumnNumber)
  52. : Filename(Filename), LineNumber(LineNumber),
  53. ColumnNumber(ColumnNumber) {}
  54. };
  55. DenseMap<MCSymbol *, InstrInfoTy> InstrInfo;
  56. // FileNameRegistry - Manages filenames observed while generating debug info
  57. // by filtering out duplicates and bookkeeping the offsets in the string
  58. // table to be generated.
  59. struct FileNameRegistryTy {
  60. SmallVector<StringRef, 10> Filenames;
  61. struct PerFileInfo {
  62. size_t FilenameID, StartOffset;
  63. };
  64. StringMap<PerFileInfo> Infos;
  65. // The offset in the string table where we'll write the next unique
  66. // filename.
  67. size_t LastOffset;
  68. FileNameRegistryTy() {
  69. clear();
  70. }
  71. // Add Filename to the registry, if it was not observed before.
  72. void add(StringRef Filename) {
  73. if (Infos.count(Filename))
  74. return;
  75. size_t OldSize = Infos.size();
  76. Infos[Filename].FilenameID = OldSize;
  77. Infos[Filename].StartOffset = LastOffset;
  78. LastOffset += Filename.size() + 1;
  79. Filenames.push_back(Filename);
  80. }
  81. void clear() {
  82. LastOffset = 1;
  83. Infos.clear();
  84. Filenames.clear();
  85. }
  86. } FileNameRegistry;
  87. typedef std::map<std::pair<StringRef, StringRef>, char *>
  88. DirAndFilenameToFilepathMapTy;
  89. DirAndFilenameToFilepathMapTy DirAndFilenameToFilepathMap;
  90. StringRef getFullFilepath(const MDNode *S);
  91. void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
  92. void clear() {
  93. assert(CurFn == nullptr);
  94. FileNameRegistry.clear();
  95. InstrInfo.clear();
  96. }
  97. void emitDebugInfoForFunction(const Function *GV);
  98. public:
  99. WinCodeViewLineTables(AsmPrinter *Asm);
  100. ~WinCodeViewLineTables() override {
  101. for (DirAndFilenameToFilepathMapTy::iterator
  102. I = DirAndFilenameToFilepathMap.begin(),
  103. E = DirAndFilenameToFilepathMap.end();
  104. I != E; ++I)
  105. free(I->second);
  106. }
  107. void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
  108. /// \brief Emit the COFF section that holds the line table information.
  109. void endModule() override;
  110. /// \brief Gather pre-function debug information.
  111. void beginFunction(const MachineFunction *MF) override;
  112. /// \brief Gather post-function debug information.
  113. void endFunction(const MachineFunction *) override;
  114. /// \brief Process beginning of an instruction.
  115. void beginInstruction(const MachineInstr *MI) override;
  116. /// \brief Process end of an instruction.
  117. void endInstruction() override {}
  118. };
  119. } // End of namespace llvm
  120. #endif