blob: 0e14797717acfc6d9bbb31331f42368bf3000101 [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;
Reid Klecknerdac21b42016-02-03 21:15:48 +000035 MCStreamer &OS;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000036 DebugLoc PrevInstLoc;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000037
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000038 struct InlineSite {
39 TinyPtrVector<const DILocation *> ChildSites;
40 const DISubprogram *Inlinee = nullptr;
41 unsigned SiteFuncId = 0;
42 };
43
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000044 // For each function, store a vector of labels to its instructions, as well as
45 // to the end of the function.
46 struct FunctionInfo {
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000047 /// Map from inlined call site to inlined instructions and child inlined
48 /// call sites. Listed in program order.
49 MapVector<const DILocation *, InlineSite> InlineSites;
50
Reid Kleckner9533af42016-01-16 00:09:09 +000051 DebugLoc LastLoc;
Reid Kleckner1fcd6102016-02-02 17:41:18 +000052 const MCSymbol *Begin = nullptr;
53 const MCSymbol *End = nullptr;
Reid Kleckner2214ed82016-01-29 00:49:42 +000054 unsigned FuncId = 0;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000055 unsigned LastFileId = 0;
Reid Kleckner2214ed82016-01-29 00:49:42 +000056 bool HaveLineInfo = false;
Reid Kleckner9533af42016-01-16 00:09:09 +000057 };
58 FunctionInfo *CurFn;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000059
Reid Kleckner2214ed82016-01-29 00:49:42 +000060 unsigned NextFuncId = 0;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000061
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000062 InlineSite &getInlineSite(const DILocation *Loc);
63
Reid Kleckner1fcd6102016-02-02 17:41:18 +000064 static void collectInlineSiteChildren(SmallVectorImpl<unsigned> &Children,
65 const FunctionInfo &FI,
66 const InlineSite &Site);
67
Reid Kleckner2214ed82016-01-29 00:49:42 +000068 /// Remember some debug info about each function. Keep it in a stable order to
69 /// emit at the end of the TU.
70 MapVector<const Function *, FunctionInfo> FnDebugInfo;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000071
Reid Kleckner2214ed82016-01-29 00:49:42 +000072 /// Map from DIFile to .cv_file id.
73 DenseMap<const DIFile *, unsigned> FileIdMap;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000074
Reid Kleckner1fcd6102016-02-02 17:41:18 +000075 SmallSetVector<const DISubprogram *, 4> InlinedSubprograms;
76
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000077 DenseMap<const DISubprogram *, codeview::TypeIndex> SubprogramToFuncId;
78
79 unsigned TypeCount = 0;
80
81 /// Gets the next type index and increments the count of types streamed so
82 /// far.
83 codeview::TypeIndex getNextTypeIndex() {
84 return codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex + TypeCount++);
85 }
86
Reid Kleckner9533af42016-01-16 00:09:09 +000087 typedef std::map<const DIFile *, std::string> FileToFilepathMapTy;
88 FileToFilepathMapTy FileToFilepathMap;
89 StringRef getFullFilepath(const DIFile *S);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000090
Reid Kleckner2214ed82016-01-29 00:49:42 +000091 unsigned maybeRecordFile(const DIFile *F);
92
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000093 void maybeRecordLocation(DebugLoc DL, const MachineFunction *MF);
94
95 void clear() {
Craig Toppere73658d2014-04-28 04:05:08 +000096 assert(CurFn == nullptr);
Reid Kleckner2214ed82016-01-29 00:49:42 +000097 FileIdMap.clear();
98 FnDebugInfo.clear();
99 FileToFilepathMap.clear();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000100 }
101
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000102 void emitTypeInformation();
103
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000104 void emitInlineeLinesSubsection();
105
Reid Kleckner2214ed82016-01-29 00:49:42 +0000106 void emitDebugInfoForFunction(const Function *GV, FunctionInfo &FI);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000107
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000108 void emitInlinedCallSite(const FunctionInfo &FI, const DILocation *InlinedAt,
109 const InlineSite &Site);
110
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000111public:
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000112 CodeViewDebug(AsmPrinter *Asm);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000113
Craig Topper7b883b32014-03-08 06:31:39 +0000114 void setSymbolSize(const llvm::MCSymbol *, uint64_t) override {}
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000115
116 /// \brief Emit the COFF section that holds the line table information.
Craig Topper7b883b32014-03-08 06:31:39 +0000117 void endModule() override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000118
119 /// \brief Gather pre-function debug information.
Craig Topper7b883b32014-03-08 06:31:39 +0000120 void beginFunction(const MachineFunction *MF) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000121
122 /// \brief Gather post-function debug information.
Craig Topper7b883b32014-03-08 06:31:39 +0000123 void endFunction(const MachineFunction *) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000124
125 /// \brief Process beginning of an instruction.
Craig Topper7b883b32014-03-08 06:31:39 +0000126 void beginInstruction(const MachineInstr *MI) override;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000127
128 /// \brief Process end of an instruction.
Craig Topper7b883b32014-03-08 06:31:39 +0000129 void endInstruction() override {}
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000130};
131} // End of namespace llvm
132
133#endif