blob: 4883905e90713a77ecd0a04bf5b545348de6023d [file] [log] [blame]
Reid Kleckner70f5bc92016-01-14 19:25:04 +00001//===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h ----*- C++ -*--===//
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00002//
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//
Reid Kleckner70f5bc92016-01-14 19:25:04 +000010// This file contains support for writing Microsoft CodeView debug info.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Kleckner70f5bc92016-01-14 19:25:04 +000014#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
15#define LLVM_LIB_CODEGEN_ASMPRINTER_CODEVIEWDEBUG_H
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000016
17#include "AsmPrinterHandler.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000021#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000022#include "llvm/CodeGen/LexicalScopes.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000025#include "llvm/DebugInfo/CodeView/TypeIndex.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000026#include "llvm/IR/DebugInfo.h"
Chandler Carruth92051402014-03-05 10:30:38 +000027#include "llvm/IR/DebugLoc.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000028#include "llvm/MC/MCStreamer.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
30
31namespace llvm {
32/// \brief Collects and handles line tables information in a CodeView format.
Reid Kleckner70f5bc92016-01-14 19:25:04 +000033class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public AsmPrinterHandler {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000034 AsmPrinter *Asm;
35 DebugLoc PrevInstLoc;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000036
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000037 struct InlineSite {
38 TinyPtrVector<const DILocation *> ChildSites;
39 const DISubprogram *Inlinee = nullptr;
40 unsigned SiteFuncId = 0;
41 };
42
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000043 // For each function, store a vector of labels to its instructions, as well as
44 // to the end of the function.
45 struct FunctionInfo {
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000046 /// Map from inlined call site to inlined instructions and child inlined
47 /// call sites. Listed in program order.
48 MapVector<const DILocation *, InlineSite> InlineSites;
49
Reid Kleckner9533af42016-01-16 00:09:09 +000050 DebugLoc LastLoc;
Reid Kleckner2214ed82016-01-29 00:49:42 +000051 MCSymbol *End = nullptr;
52 unsigned FuncId = 0;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000053 unsigned LastFileId = 0;
Reid Kleckner2214ed82016-01-29 00:49:42 +000054 bool HaveLineInfo = false;
Reid Kleckner9533af42016-01-16 00:09:09 +000055 };
56 FunctionInfo *CurFn;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000057
Reid Kleckner2214ed82016-01-29 00:49:42 +000058 unsigned NextFuncId = 0;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000059
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000060 InlineSite &getInlineSite(const DILocation *Loc);
61
Reid Kleckner2214ed82016-01-29 00:49:42 +000062 /// Remember some debug info about each function. Keep it in a stable order to
63 /// emit at the end of the TU.
64 MapVector<const Function *, FunctionInfo> FnDebugInfo;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000065
Reid Kleckner2214ed82016-01-29 00:49:42 +000066 /// Map from DIFile to .cv_file id.
67 DenseMap<const DIFile *, unsigned> FileIdMap;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000068
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000069 DenseMap<const DISubprogram *, codeview::TypeIndex> SubprogramToFuncId;
70
71 unsigned TypeCount = 0;
72
73 /// Gets the next type index and increments the count of types streamed so
74 /// far.
75 codeview::TypeIndex getNextTypeIndex() {
76 return codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex + TypeCount++);
77 }
78
Reid Kleckner9533af42016-01-16 00:09:09 +000079 typedef std::map<const DIFile *, std::string> FileToFilepathMapTy;
80 FileToFilepathMapTy FileToFilepathMap;
81 StringRef getFullFilepath(const DIFile *S);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000082
Reid Kleckner2214ed82016-01-29 00:49:42 +000083 unsigned maybeRecordFile(const DIFile *F);
84
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000085 void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
86
87 void clear() {
Craig Toppere73658d2014-04-28 04:05:08 +000088 assert(CurFn == nullptr);
Reid Kleckner2214ed82016-01-29 00:49:42 +000089 FileIdMap.clear();
90 FnDebugInfo.clear();
91 FileToFilepathMap.clear();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000092 }
93
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000094 void emitTypeInformation();
95
Reid Kleckner2214ed82016-01-29 00:49:42 +000096 void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000097
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000098 void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
99 const InlineSite &Site);
100
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000101public:
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000102 CodeViewDebug(AsmPrinter *Asm);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000103
Craig Topper7b883b32014-03-08 06:31:39 +0000104 void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000105
106 /// \brief Emit the COFF section that holds the line table information.
Craig Topper7b883b32014-03-08 06:31:39 +0000107 void endModule() override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000108
109 /// \brief Gather pre-function debug information.
Craig Topper7b883b32014-03-08 06:31:39 +0000110 void beginFunction(const MachineFunction *MF) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000111
112 /// \brief Gather post-function debug information.
Craig Topper7b883b32014-03-08 06:31:39 +0000113 void endFunction(const MachineFunction *) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000114
115 /// \brief Process beginning of an instruction.
Craig Topper7b883b32014-03-08 06:31:39 +0000116 void beginInstruction(const MachineInstr *MI) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000117
118 /// \brief Process end of an instruction.
Craig Topper7b883b32014-03-08 06:31:39 +0000119 void endInstruction() override {}
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000120};
121} // End of namespace llvm
122
123#endif