blob: d9f38f3eaf8e3f24e6d68e8c3c7ad8eceede7680 [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 Kleckner1fcd6102016-02-02 17:41:18 +000051 const MCSymbol *Begin = nullptr;
52 const MCSymbol *End = nullptr;
Reid Kleckner2214ed82016-01-29 00:49:42 +000053 unsigned FuncId = 0;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000054 unsigned LastFileId = 0;
Reid Kleckner2214ed82016-01-29 00:49:42 +000055 bool HaveLineInfo = false;
Reid Kleckner9533af42016-01-16 00:09:09 +000056 };
57 FunctionInfo *CurFn;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000058
Reid Kleckner2214ed82016-01-29 00:49:42 +000059 unsigned NextFuncId = 0;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000060
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000061 InlineSite &getInlineSite(const DILocation *Loc);
62
Reid Kleckner1fcd6102016-02-02 17:41:18 +000063 static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
64 const FunctionInfo &FI,
65 const InlineSite &Site);
66
Reid Kleckner2214ed82016-01-29 00:49:42 +000067 /// Remember some debug info about each function. Keep it in a stable order to
68 /// emit at the end of the TU.
69 MapVector<const Function *, FunctionInfo> FnDebugInfo;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000070
Reid Kleckner2214ed82016-01-29 00:49:42 +000071 /// Map from DIFile to .cv_file id.
72 DenseMap<const DIFile *, unsigned> FileIdMap;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000073
Reid Kleckner1fcd6102016-02-02 17:41:18 +000074 SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
75
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000076 DenseMap<const DISubprogram *, codeview::TypeIndex> SubprogramToFuncId;
77
78 unsigned TypeCount = 0;
79
80 /// Gets the next type index and increments the count of types streamed so
81 /// far.
82 codeview::TypeIndex getNextTypeIndex() {
83 return codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex + TypeCount++);
84 }
85
Reid Kleckner9533af42016-01-16 00:09:09 +000086 typedef std::map<const DIFile *, std::string> FileToFilepathMapTy;
87 FileToFilepathMapTy FileToFilepathMap;
88 StringRef getFullFilepath(const DIFile *S);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000089
Reid Kleckner2214ed82016-01-29 00:49:42 +000090 unsigned maybeRecordFile(const DIFile *F);
91
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000092 void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
93
94 void clear() {
Craig Toppere73658d2014-04-28 04:05:08 +000095 assert(CurFn == nullptr);
Reid Kleckner2214ed82016-01-29 00:49:42 +000096 FileIdMap.clear();
97 FnDebugInfo.clear();
98 FileToFilepathMap.clear();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000099 }
100
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000101 void emitTypeInformation();
102
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000103 void emitInlineeLinesSubsection();
104
Reid Kleckner2214ed82016-01-29 00:49:42 +0000105 void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000106
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000107 void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
108 const InlineSite &Site);
109
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000110public:
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000111 CodeViewDebug(AsmPrinter *Asm);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000112
Craig Topper7b883b32014-03-08 06:31:39 +0000113 void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000114
115 /// \brief Emit the COFF section that holds the line table information.
Craig Topper7b883b32014-03-08 06:31:39 +0000116 void endModule() override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000117
118 /// \brief Gather pre-function debug information.
Craig Topper7b883b32014-03-08 06:31:39 +0000119 void beginFunction(const MachineFunction *MF) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000120
121 /// \brief Gather post-function debug information.
Craig Topper7b883b32014-03-08 06:31:39 +0000122 void endFunction(const MachineFunction *) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000123
124 /// \brief Process beginning of an instruction.
Craig Topper7b883b32014-03-08 06:31:39 +0000125 void beginInstruction(const MachineInstr *MI) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000126
127 /// \brief Process end of an instruction.
Craig Topper7b883b32014-03-08 06:31:39 +0000128 void endInstruction() override {}
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000129};
130} // End of namespace llvm
131
132#endif