LLVM API Documentation

WinCodeViewLineTables.h
Go to the documentation of this file.
00001 //===-- llvm/lib/CodeGen/AsmPrinter/WinCodeViewLineTables.h ----*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains support for writing line tables info into COFF files.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_WINCODEVIEWLINETABLES_H
00015 #define LLVM_LIB_CODEGEN_ASMPRINTER_WINCODEVIEWLINETABLES_H
00016 
00017 #include "AsmPrinterHandler.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/StringMap.h"
00020 #include "llvm/ADT/StringRef.h"
00021 #include "llvm/CodeGen/AsmPrinter.h"
00022 #include "llvm/CodeGen/LexicalScopes.h"
00023 #include "llvm/CodeGen/MachineFunction.h"
00024 #include "llvm/CodeGen/MachineModuleInfo.h"
00025 #include "llvm/IR/DebugInfo.h"
00026 #include "llvm/IR/DebugLoc.h"
00027 #include "llvm/MC/MCStreamer.h"
00028 #include "llvm/Target/TargetLoweringObjectFile.h"
00029 
00030 namespace llvm {
00031 /// \brief Collects and handles line tables information in a CodeView format.
00032 class WinCodeViewLineTables : public AsmPrinterHandler {
00033   AsmPrinter *Asm;
00034   DebugLoc PrevInstLoc;
00035 
00036   // For each function, store a vector of labels to its instructions, as well as
00037   // to the end of the function.
00038   struct FunctionInfo {
00039     SmallVector<MCSymbol *, 10> Instrs;
00040     MCSymbol *End;
00041     FunctionInfo() : End(nullptr) {}
00042   } *CurFn;
00043 
00044   typedef DenseMap<const Function *, FunctionInfo> FnDebugInfoTy;
00045   FnDebugInfoTy FnDebugInfo;
00046   // Store the functions we've visited in a vector so we can maintain a stable
00047   // order while emitting subsections.
00048   SmallVector<const Function *, 10> VisitedFunctions;
00049 
00050   // InstrInfoTy - Holds the Filename:LineNumber information for every
00051   // instruction with a unique debug location.
00052   struct InstrInfoTy {
00053     StringRef Filename;
00054     unsigned LineNumber;
00055 
00056     InstrInfoTy() : LineNumber(0) {}
00057 
00058     InstrInfoTy(StringRef Filename, unsigned LineNumber)
00059         : Filename(Filename), LineNumber(LineNumber) {}
00060   };
00061   DenseMap<MCSymbol *, InstrInfoTy> InstrInfo;
00062 
00063   // FileNameRegistry - Manages filenames observed while generating debug info
00064   // by filtering out duplicates and bookkeeping the offsets in the string
00065   // table to be generated.
00066   struct FileNameRegistryTy {
00067     SmallVector<StringRef, 10> Filenames;
00068     struct PerFileInfo {
00069       size_t FilenameID, StartOffset;
00070     };
00071     StringMap<PerFileInfo> Infos;
00072 
00073     // The offset in the string table where we'll write the next unique
00074     // filename.
00075     size_t LastOffset;
00076 
00077     FileNameRegistryTy() {
00078       clear();
00079     }
00080 
00081     // Add Filename to the registry, if it was not observed before.
00082     void add(StringRef Filename) {
00083       if (Infos.count(Filename))
00084         return;
00085       size_t OldSize = Infos.size();
00086       Infos[Filename].FilenameID = OldSize;
00087       Infos[Filename].StartOffset = LastOffset;
00088       LastOffset += Filename.size() + 1;
00089       Filenames.push_back(Filename);
00090     }
00091 
00092     void clear() {
00093       LastOffset = 1;
00094       Infos.clear();
00095       Filenames.clear();
00096     }
00097   } FileNameRegistry;
00098 
00099   typedef std::map<std::pair<StringRef, StringRef>, char *>
00100       DirAndFilenameToFilepathMapTy;
00101   DirAndFilenameToFilepathMapTy DirAndFilenameToFilepathMap;
00102   StringRef getFullFilepath(const MDNode *S);
00103 
00104   void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
00105 
00106   void clear() {
00107     assert(CurFn == nullptr);
00108     FileNameRegistry.clear();
00109     InstrInfo.clear();
00110   }
00111 
00112   void emitDebugInfoForFunction(const Function *GV);
00113 
00114 public:
00115   WinCodeViewLineTables(AsmPrinter *Asm);
00116 
00117   ~WinCodeViewLineTables() {
00118     for (DirAndFilenameToFilepathMapTy::iterator
00119              I = DirAndFilenameToFilepathMap.begin(),
00120              E = DirAndFilenameToFilepathMap.end();
00121          I != E; ++I)
00122       free(I->second);
00123   }
00124 
00125   void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
00126 
00127   /// \brief Emit the COFF section that holds the line table information.
00128   void endModule() override;
00129 
00130   /// \brief Gather pre-function debug information.
00131   void beginFunction(const MachineFunction *MF) override;
00132 
00133   /// \brief Gather post-function debug information.
00134   void endFunction(const MachineFunction *) override;
00135 
00136   /// \brief Process beginning of an instruction.
00137   void beginInstruction(const MachineInstr *MI) override;
00138 
00139   /// \brief Process end of an instruction.
00140   void endInstruction() override {}
00141 };
00142 } // End of namespace llvm
00143 
00144 #endif