LLVM  13.0.0git
MCCodeView.h
Go to the documentation of this file.
1 //===- MCCodeView.h - Machine Code CodeView support -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Holds state from .cv_file and .cv_loc directives for later emission.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_MC_MCCODEVIEW_H
14 #define LLVM_MC_MCCODEVIEW_H
15 
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/MC/MCFragment.h"
20 #include <map>
21 #include <vector>
22 
23 namespace llvm {
24 class MCContext;
25 class MCObjectStreamer;
26 class MCStreamer;
27 class CodeViewContext;
28 
29 /// Instances of this class represent the information from a
30 /// .cv_loc directive.
31 class MCCVLoc {
32  const MCSymbol *Label = nullptr;
33  uint32_t FunctionId;
34  uint32_t FileNum;
35  uint32_t Line;
36  uint16_t Column;
37  uint16_t PrologueEnd : 1;
38  uint16_t IsStmt : 1;
39 
40 private: // CodeViewContext manages these
41  friend class CodeViewContext;
42  MCCVLoc(const MCSymbol *Label, unsigned functionid, unsigned fileNum,
43  unsigned line, unsigned column, bool prologueend, bool isstmt)
44  : Label(Label), FunctionId(functionid), FileNum(fileNum), Line(line),
45  Column(column), PrologueEnd(prologueend), IsStmt(isstmt) {}
46 
47  // Allow the default copy constructor and assignment operator to be used
48  // for an MCCVLoc object.
49 
50 public:
51  const MCSymbol *getLabel() const { return Label; }
52 
53  unsigned getFunctionId() const { return FunctionId; }
54 
55  /// Get the FileNum of this MCCVLoc.
56  unsigned getFileNum() const { return FileNum; }
57 
58  /// Get the Line of this MCCVLoc.
59  unsigned getLine() const { return Line; }
60 
61  /// Get the Column of this MCCVLoc.
62  unsigned getColumn() const { return Column; }
63 
64  bool isPrologueEnd() const { return PrologueEnd; }
65  bool isStmt() const { return IsStmt; }
66 
67  void setLabel(const MCSymbol *L) { Label = L; }
68 
69  void setFunctionId(unsigned FID) { FunctionId = FID; }
70 
71  /// Set the FileNum of this MCCVLoc.
72  void setFileNum(unsigned fileNum) { FileNum = fileNum; }
73 
74  /// Set the Line of this MCCVLoc.
75  void setLine(unsigned line) { Line = line; }
76 
77  /// Set the Column of this MCCVLoc.
78  void setColumn(unsigned column) {
79  assert(column <= UINT16_MAX);
80  Column = column;
81  }
82 
83  void setPrologueEnd(bool PE) { PrologueEnd = PE; }
84  void setIsStmt(bool IS) { IsStmt = IS; }
85 };
86 
87 /// Information describing a function or inlined call site introduced by
88 /// .cv_func_id or .cv_inline_site_id. Accumulates information from .cv_loc
89 /// directives used with this function's id or the id of an inlined call site
90 /// within this function or inlined call site.
92  /// If this represents an inlined call site, then ParentFuncIdPlusOne will be
93  /// the parent function id plus one. If this represents a normal function,
94  /// then there is no parent, and ParentFuncIdPlusOne will be FunctionSentinel.
95  /// If this struct is an unallocated slot in the function info vector, then
96  /// ParentFuncIdPlusOne will be zero.
97  unsigned ParentFuncIdPlusOne = 0;
98 
99  enum : unsigned { FunctionSentinel = ~0U };
100 
101  struct LineInfo {
102  unsigned File;
103  unsigned Line;
104  unsigned Col;
105  };
106 
108 
109  /// The section of the first .cv_loc directive used for this function, or null
110  /// if none has been seen yet.
111  MCSection *Section = nullptr;
112 
113  /// Map from inlined call site id to the inlined at location to use for that
114  /// call site. Call chains are collapsed, so for the call chain 'f -> g -> h',
115  /// the InlinedAtMap of 'f' will contain entries for 'g' and 'h' that both
116  /// list the line info for the 'g' call site.
118 
119  /// Returns true if this is function info has not yet been used in a
120  /// .cv_func_id or .cv_inline_site_id directive.
121  bool isUnallocatedFunctionInfo() const { return ParentFuncIdPlusOne == 0; }
122 
123  /// Returns true if this represents an inlined call site, meaning
124  /// ParentFuncIdPlusOne is neither zero nor ~0U.
125  bool isInlinedCallSite() const {
126  return !isUnallocatedFunctionInfo() &&
128  }
129 
130  unsigned getParentFuncId() const {
132  return ParentFuncIdPlusOne - 1;
133  }
134 };
135 
136 /// Holds state from .cv_file and .cv_loc directives for later emission.
138 public:
139  CodeViewContext();
141 
142  bool isValidFileNumber(unsigned FileNumber) const;
143  bool addFile(MCStreamer &OS, unsigned FileNumber, StringRef Filename,
144  ArrayRef<uint8_t> ChecksumBytes, uint8_t ChecksumKind);
145 
146  /// Records the function id of a normal function. Returns false if the
147  /// function id has already been used, and true otherwise.
148  bool recordFunctionId(unsigned FuncId);
149 
150  /// Records the function id of an inlined call site. Records the "inlined at"
151  /// location info of the call site, including what function or inlined call
152  /// site it was inlined into. Returns false if the function id has already
153  /// been used, and true otherwise.
154  bool recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc,
155  unsigned IAFile, unsigned IALine,
156  unsigned IACol);
157 
158  /// Retreive the function info if this is a valid function id, or nullptr.
160 
161  /// Saves the information from the currently parsed .cv_loc directive
162  /// and sets CVLocSeen. When the next instruction is assembled an entry
163  /// in the line number table with this information and the address of the
164  /// instruction will be created.
165  void recordCVLoc(MCContext &Ctx, const MCSymbol *Label, unsigned FunctionId,
166  unsigned FileNo, unsigned Line, unsigned Column,
167  bool PrologueEnd, bool IsStmt);
168 
169  /// Add a line entry.
170  void addLineEntry(const MCCVLoc &LineEntry);
171 
172  std::vector<MCCVLoc> getFunctionLineEntries(unsigned FuncId);
173 
174  std::pair<size_t, size_t> getLineExtent(unsigned FuncId);
175 
176  ArrayRef<MCCVLoc> getLinesForExtent(size_t L, size_t R);
177 
178  /// Emits a line table substream.
180  const MCSymbol *FuncBegin,
181  const MCSymbol *FuncEnd);
182 
184  unsigned PrimaryFunctionId,
185  unsigned SourceFileId,
186  unsigned SourceLineNum,
187  const MCSymbol *FnStartSym,
188  const MCSymbol *FnEndSym);
189 
190  /// Encodes the binary annotations once we have a layout.
191  void encodeInlineLineTable(MCAsmLayout &Layout,
193 
194  MCFragment *
196  ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
197  StringRef FixedSizePortion);
198 
200 
201  /// Emits the string table substream.
203 
204  /// Emits the file checksum substream.
206 
207  /// Emits the offset into the checksum table of the given file number.
208  void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo);
209 
210  /// Add something to the string table. Returns the final string as well as
211  /// offset into the string table.
212  std::pair<StringRef, unsigned> addToStringTable(StringRef S);
213 
214 private:
215  /// Map from string to string table offset.
216  StringMap<unsigned> StringTable;
217 
218  /// The fragment that ultimately holds our strings.
219  MCDataFragment *StrTabFragment = nullptr;
220  bool InsertedStrTabFragment = false;
221 
222  MCDataFragment *getStringTableFragment();
223 
224  /// Get a string table offset.
225  unsigned getStringTableOffset(StringRef S);
226 
227  struct FileInfo {
228  unsigned StringTableOffset;
229 
230  // Indicates if this FileInfo corresponds to an actual file, or hasn't been
231  // set yet.
232  bool Assigned = false;
233 
234  uint8_t ChecksumKind;
235 
236  ArrayRef<uint8_t> Checksum;
237 
238  // Checksum offset stored as a symbol because it might be requested
239  // before it has been calculated, so a fixup may be needed.
240  MCSymbol *ChecksumTableOffset;
241  };
242 
243  /// Array storing added file information.
245 
246  /// The offset of the first and last .cv_loc directive for a given function
247  /// id.
248  std::map<unsigned, std::pair<size_t, size_t>> MCCVLineStartStop;
249 
250  /// A collection of MCCVLoc for each section.
251  std::vector<MCCVLoc> MCCVLines;
252 
253  /// All known functions and inlined call sites, indexed by function id.
254  std::vector<MCCVFunctionInfo> Functions;
255 
256  /// Indicate whether we have already laid out the checksum table addresses or
257  /// not.
258  bool ChecksumOffsetsAssigned = false;
259 };
260 
261 } // end namespace llvm
262 #endif
llvm::MCCVLoc::getColumn
unsigned getColumn() const
Get the Column of this MCCVLoc.
Definition: MCCodeView.h:62
llvm
Definition: AllocatorList.h:23
llvm::MCCVFunctionInfo::getParentFuncId
unsigned getParentFuncId() const
Definition: MCCodeView.h:130
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::CodeViewContext::addToStringTable
std::pair< StringRef, unsigned > addToStringTable(StringRef S)
Add something to the string table.
Definition: MCCodeView.cpp:147
llvm::MCCVInlineLineTableFragment
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition: MCFragment.h:503
llvm::CodeViewContext::getLineExtent
std::pair< size_t, size_t > getLineExtent(unsigned FuncId)
Definition: MCCodeView.cpp:310
llvm::MCCVFunctionInfo::ParentFuncIdPlusOne
unsigned ParentFuncIdPlusOne
If this represents an inlined call site, then ParentFuncIdPlusOne will be the parent function id plus...
Definition: MCCodeView.h:97
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:71
llvm::MCCVFunctionInfo::LineInfo::File
unsigned File
Definition: MCCodeView.h:102
StringRef.h
llvm::CodeViewContext::recordCVLoc
void recordCVLoc(MCContext &Ctx, const MCSymbol *Label, unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt)
Saves the information from the currently parsed .cv_loc directive and sets CVLocSeen.
Definition: MCCodeView.cpp:130
llvm::SmallVector< FileInfo, 4 >
llvm::CodeViewContext::addFile
bool addFile(MCStreamer &OS, unsigned FileNumber, StringRef Filename, ArrayRef< uint8_t > ChecksumBytes, uint8_t ChecksumKind)
Definition: MCCodeView.cpp:46
MCFragment.h
llvm::CodeViewContext::~CodeViewContext
~CodeViewContext()
Definition: MCCodeView.cpp:30
llvm::MCCVLoc::setFunctionId
void setFunctionId(unsigned FID)
Definition: MCCodeView.h:69
llvm::CodeViewContext::emitLineTableForFunction
void emitLineTableForFunction(MCObjectStreamer &OS, unsigned FuncId, const MCSymbol *FuncBegin, const MCSymbol *FuncEnd)
Emits a line table substream.
Definition: MCCodeView.cpp:326
llvm::MCCVFunctionInfo::LineInfo::Line
unsigned Line
Definition: MCCodeView.h:103
F
#define F(x, y, z)
Definition: MD5.cpp:56
MCObjectStreamer.h
llvm::MCCVLoc::setColumn
void setColumn(unsigned column)
Set the Column of this MCCVLoc.
Definition: MCCodeView.h:78
llvm::CodeViewContext::getCVFunctionInfo
MCCVFunctionInfo * getCVFunctionInfo(unsigned FuncId)
Retreive the function info if this is a valid function id, or nullptr.
Definition: MCCodeView.cpp:78
llvm::MCFragment
Definition: MCFragment.h:31
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:197
llvm::CodeViewContext::CodeViewContext
CodeViewContext()
Definition: MCCodeView.cpp:28
llvm::MCCVLoc::setIsStmt
void setIsStmt(bool IS)
Definition: MCCodeView.h:84
llvm::CodeViewContext::encodeDefRange
void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F)
Definition: MCCodeView.cpp:605
llvm::CodeViewContext::addLineEntry
void addLineEntry(const MCCVLoc &LineEntry)
Add a line entry.
Definition: MCCodeView.cpp:265
llvm::CodeViewContext
Holds state from .cv_file and .cv_loc directives for later emission.
Definition: MCCodeView.h:137
llvm::MCCVLoc::setLabel
void setLabel(const MCSymbol *L)
Definition: MCCodeView.h:67
llvm::MCCVLoc::getFileNum
unsigned getFileNum() const
Get the FileNum of this MCCVLoc.
Definition: MCCodeView.h:56
llvm::MCObjectStreamer
Streaming object file generation interface.
Definition: MCObjectStreamer.h:36
llvm::MCCVFunctionInfo::InlinedAt
LineInfo InlinedAt
Definition: MCCodeView.h:107
llvm::MCCVFunctionInfo::LineInfo::Col
unsigned Col
Definition: MCCodeView.h:104
llvm::MCCVFunctionInfo::InlinedAtMap
DenseMap< unsigned, LineInfo > InlinedAtMap
Map from inlined call site id to the inlined at location to use for that call site.
Definition: MCCodeView.h:117
llvm::CodeViewContext::getLinesForExtent
ArrayRef< MCCVLoc > getLinesForExtent(size_t L, size_t R)
Definition: MCCodeView.cpp:318
StringMap.h
llvm::MCCVLoc::setFileNum
void setFileNum(unsigned fileNum)
Set the FileNum of this MCCVLoc.
Definition: MCCodeView.h:72
llvm::CodeViewContext::emitInlineLineTableForFunction
void emitInlineLineTableForFunction(MCObjectStreamer &OS, unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
Definition: MCCodeView.cpp:421
llvm::StringMap< unsigned >
llvm::CodeViewContext::emitDefRange
MCFragment * emitDefRange(MCObjectStreamer &OS, ArrayRef< std::pair< const MCSymbol *, const MCSymbol * >> Ranges, StringRef FixedSizePortion)
Definition: MCCodeView.cpp:434
llvm::MCCVFunctionInfo::FunctionSentinel
@ FunctionSentinel
Definition: MCCodeView.h:99
llvm::MCCVLoc::getLabel
const MCSymbol * getLabel() const
Definition: MCCodeView.h:51
llvm::CodeViewContext::isValidFileNumber
bool isValidFileNumber(unsigned FileNumber) const
This is a valid number for use with .cv_loc if we've already seen a .cv_file for it.
Definition: MCCodeView.cpp:39
llvm::CodeViewContext::emitStringTable
void emitStringTable(MCObjectStreamer &OS)
Emits the string table substream.
Definition: MCCodeView.cpp:170
llvm::CodeViewContext::encodeInlineLineTable
void encodeInlineLineTable(MCAsmLayout &Layout, MCCVInlineLineTableFragment &F)
Encodes the binary annotations once we have a layout.
Definition: MCCodeView.cpp:461
llvm::CodeViewContext::emitFileChecksumOffset
void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo)
Emits the offset into the checksum table of the given file number.
Definition: MCCodeView.cpp:247
llvm::DenseMap
Definition: DenseMap.h:714
FuncId
Profile::FuncID FuncId
Definition: Profile.cpp:321
llvm::MCCVLoc::isStmt
bool isStmt() const
Definition: MCCodeView.h:65
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCCVFunctionInfo
Information describing a function or inlined call site introduced by .cv_func_id or ....
Definition: MCCodeView.h:91
llvm::CodeViewContext::getFunctionLineEntries
std::vector< MCCVLoc > getFunctionLineEntries(unsigned FuncId)
Definition: MCCodeView.cpp:275
llvm::MCCVFunctionInfo::LineInfo
Definition: MCCodeView.h:101
llvm::MCCVLoc::getLine
unsigned getLine() const
Get the Line of this MCCVLoc.
Definition: MCCodeView.h:59
llvm::ArrayRef< uint8_t >
llvm::MCCVLoc::isPrologueEnd
bool isPrologueEnd() const
Definition: MCCodeView.h:64
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::MCCVLoc::setLine
void setLine(unsigned line)
Set the Line of this MCCVLoc.
Definition: MCCodeView.h:75
uint32_t
llvm::MCCVLoc
Instances of this class represent the information from a .cv_loc directive.
Definition: MCCodeView.h:31
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MCCVLoc::setPrologueEnd
void setPrologueEnd(bool PE)
Definition: MCCodeView.h:83
llvm::CodeViewContext::recordFunctionId
bool recordFunctionId(unsigned FuncId)
Records the function id of a normal function.
Definition: MCCodeView.cpp:86
llvm::MCCVFunctionInfo::Section
MCSection * Section
The section of the first .cv_loc directive used for this function, or null if none has been seen yet.
Definition: MCCodeView.h:111
uint16_t
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::CodeViewContext::recordInlinedCallSiteId
bool recordInlinedCallSiteId(unsigned FuncId, unsigned IAFunc, unsigned IAFile, unsigned IALine, unsigned IACol)
Records the function id of an inlined call site.
Definition: MCCodeView.cpp:99
llvm::MCCVLoc::getFunctionId
unsigned getFunctionId() const
Definition: MCCodeView.h:53
llvm::lltok::ChecksumKind
@ ChecksumKind
Definition: LLToken.h:484
llvm::MCDataFragment
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
llvm::MCCVDefRangeFragment
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:536
llvm::MCCVFunctionInfo::isInlinedCallSite
bool isInlinedCallSite() const
Returns true if this represents an inlined call site, meaning ParentFuncIdPlusOne is neither zero nor...
Definition: MCCodeView.h:125
llvm::CodeViewContext::emitFileChecksums
void emitFileChecksums(MCObjectStreamer &OS)
Emits the file checksum substream.
Definition: MCCodeView.cpp:192
llvm::MCCVFunctionInfo::isUnallocatedFunctionInfo
bool isUnallocatedFunctionInfo() const
Returns true if this is function info has not yet been used in a .cv_func_id or .cv_inline_site_id di...
Definition: MCCodeView.h:121