WinCodeViewLineTables.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 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. InstrInfoTy() : LineNumber(0) {}
  50. InstrInfoTy(StringRef Filename, unsigned LineNumber)
  51. : Filename(Filename), LineNumber(LineNumber) {}
  52. };
  53. DenseMap<MCSymbol *, InstrInfoTy> InstrInfo;
  54. // FileNameRegistry - Manages filenames observed while generating debug info
  55. // by filtering out duplicates and bookkeeping the offsets in the string
  56. // table to be generated.
  57. struct FileNameRegistryTy {
  58. SmallVector<StringRef, 10> Filenames;
  59. struct PerFileInfo {
  60. size_t FilenameID, StartOffset;
  61. };
  62. StringMap<PerFileInfo> Infos;
  63. // The offset in the string table where we'll write the next unique
  64. // filename.
  65. size_t LastOffset;
  66. FileNameRegistryTy() {
  67. clear();
  68. }
  69. // Add Filename to the registry, if it was not observed before.
  70. void add(StringRef Filename) {
  71. if (Infos.count(Filename))
  72. return;
  73. size_t OldSize = Infos.size();
  74. Infos[Filename].FilenameID = OldSize;
  75. Infos[Filename].StartOffset = LastOffset;
  76. LastOffset += Filename.size() + 1;
  77. Filenames.push_back(Filename);
  78. }
  79. void clear() {
  80. LastOffset = 1;
  81. Infos.clear();
  82. Filenames.clear();
  83. }
  84. } FileNameRegistry;
  85. typedef std::map<std::pair<StringRef, StringRef>, char *>
  86. DirAndFilenameToFilepathMapTy;
  87. DirAndFilenameToFilepathMapTy DirAndFilenameToFilepathMap;
  88. StringRef getFullFilepath(const MDNode *S);
  89. void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
  90. void clear() {
  91. assert(CurFn == nullptr);
  92. FileNameRegistry.clear();
  93. InstrInfo.clear();
  94. }
  95. void emitDebugInfoForFunction(const Function *GV);
  96. public:
  97. WinCodeViewLineTables(AsmPrinter *Asm);
  98. ~WinCodeViewLineTables() override {
  99. for (DirAndFilenameToFilepathMapTy::iterator
  100. I = DirAndFilenameToFilepathMap.begin(),
  101. E = DirAndFilenameToFilepathMap.end();
  102. I != E; ++I)
  103. free(I->second);
  104. }
  105. void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
  106. /// \brief Emit the COFF section that holds the line table information.
  107. void endModule() override;
  108. /// \brief Gather pre-function debug information.
  109. void beginFunction(const MachineFunction *MF) override;
  110. /// \brief Gather post-function debug information.
  111. void endFunction(const MachineFunction *) override;
  112. /// \brief Process beginning of an instruction.
  113. void beginInstruction(const MachineInstr *MI) override;
  114. /// \brief Process end of an instruction.
  115. void endInstruction() override {}
  116. };
  117. } // End of namespace llvm
  118. #endif