summaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm18/include/llvm/DebugInfo/LogicalView/Core/LVReader.h
blob: b5c983e6f70c9b27ddd6673405ecaacb187576ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===-- LVReader.h ----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the LVReader class, which is used to describe a debug
// information reader.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVREADER_H
#define LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVREADER_H

#include "llvm/DebugInfo/LogicalView/Core/LVOptions.h"
#include "llvm/DebugInfo/LogicalView/Core/LVRange.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/ToolOutputFile.h"
#include <map>

namespace llvm {
namespace logicalview {

constexpr LVSectionIndex UndefinedSectionIndex = 0;

class LVScopeCompileUnit;
class LVObject;

class LVSplitContext final {
  std::unique_ptr<ToolOutputFile> OutputFile;
  std::string Location;

public:
  LVSplitContext() = default;
  LVSplitContext(const LVSplitContext &) = delete;
  LVSplitContext &operator=(const LVSplitContext &) = delete;
  ~LVSplitContext() = default;

  Error createSplitFolder(StringRef Where);
  std::error_code open(std::string Name, std::string Extension,
                       raw_ostream &OS);
  void close() {
    if (OutputFile) {
      OutputFile->os().close();
      OutputFile = nullptr;
    }
  }

  std::string getLocation() const { return Location; }
  raw_fd_ostream &os() { return OutputFile->os(); }
};

/// The logical reader owns of all the logical elements created during
/// the debug information parsing. For its creation it uses a specific
///  bump allocator for each type of logical element.
class LVReader {
  LVBinaryType BinaryType;

  // Context used by '--output=split' command line option.
  LVSplitContext SplitContext;

  // Compile Units DIE Offset => Scope.
  using LVCompileUnits = std::map<LVOffset, LVScopeCompileUnit *>;
  LVCompileUnits CompileUnits;

  // Added elements to be used during elements comparison.
  LVLines Lines;
  LVScopes Scopes;
  LVSymbols Symbols;
  LVTypes Types;

  // Create split folder.
  Error createSplitFolder();
  bool OutputSplit = false;

// Define a specific bump allocator for the given KIND.
#define LV_OBJECT_ALLOCATOR(KIND)                                              \
  llvm::SpecificBumpPtrAllocator<LV##KIND> Allocated##KIND;

  // Lines allocator.
  LV_OBJECT_ALLOCATOR(Line)
  LV_OBJECT_ALLOCATOR(LineDebug)
  LV_OBJECT_ALLOCATOR(LineAssembler)

  // Locations allocator.
  LV_OBJECT_ALLOCATOR(Location)
  LV_OBJECT_ALLOCATOR(LocationSymbol)

  // Operations allocator.
  LV_OBJECT_ALLOCATOR(Operation)

  // Scopes allocator.
  LV_OBJECT_ALLOCATOR(Scope)
  LV_OBJECT_ALLOCATOR(ScopeAggregate)
  LV_OBJECT_ALLOCATOR(ScopeAlias)
  LV_OBJECT_ALLOCATOR(ScopeArray)
  LV_OBJECT_ALLOCATOR(ScopeCompileUnit)
  LV_OBJECT_ALLOCATOR(ScopeEnumeration)
  LV_OBJECT_ALLOCATOR(ScopeFormalPack)
  LV_OBJECT_ALLOCATOR(ScopeFunction)
  LV_OBJECT_ALLOCATOR(ScopeFunctionInlined)
  LV_OBJECT_ALLOCATOR(ScopeFunctionType)
  LV_OBJECT_ALLOCATOR(ScopeNamespace)
  LV_OBJECT_ALLOCATOR(ScopeRoot)
  LV_OBJECT_ALLOCATOR(ScopeTemplatePack)

  // Symbols allocator.
  LV_OBJECT_ALLOCATOR(Symbol)

  // Types allocator.
  LV_OBJECT_ALLOCATOR(Type)
  LV_OBJECT_ALLOCATOR(TypeDefinition)
  LV_OBJECT_ALLOCATOR(TypeEnumerator)
  LV_OBJECT_ALLOCATOR(TypeImport)
  LV_OBJECT_ALLOCATOR(TypeParam)
  LV_OBJECT_ALLOCATOR(TypeSubrange)

#undef LV_OBJECT_ALLOCATOR

protected:
  LVScopeRoot *Root = nullptr;
  std::string InputFilename;
  std::string FileFormatName;
  ScopedPrinter &W;
  raw_ostream &OS;
  LVScopeCompileUnit *CompileUnit = nullptr;

  // Only for ELF format. The CodeView is handled in a different way.
  LVSectionIndex DotTextSectionIndex = UndefinedSectionIndex;

  // Record Compilation Unit entry.
  void addCompileUnitOffset(LVOffset Offset, LVScopeCompileUnit *CompileUnit) {
    CompileUnits.emplace(Offset, CompileUnit);
  }

  // Create the Scope Root.
  virtual Error createScopes() {
    Root = createScopeRoot();
    Root->setName(getFilename());
    if (options().getAttributeFormat())
      Root->setFileFormatName(FileFormatName);
    return Error::success();
  }

  // Return a pathname composed by: parent_path(InputFilename)/filename(From).
  // This is useful when a type server (PDB file associated with an object
  // file or a precompiled header file) or a DWARF split object have been
  // moved from their original location. That is the case when running
  // regression tests, where object files are created in one location and
  // executed in a different location.
  std::string createAlternativePath(StringRef From) {
    // During the reader initialization, any backslashes in 'InputFilename'
    // are converted to forward slashes.
    SmallString<128> Path;
    sys::path::append(Path, sys::path::Style::posix,
                      sys::path::parent_path(InputFilename),
                      sys::path::filename(sys::path::convert_to_slash(
                          From, sys::path::Style::windows)));
    return std::string(Path);
  }

  virtual Error printScopes();
  virtual Error printMatchedElements(bool UseMatchedElements);
  virtual void sortScopes() {}

public:
  LVReader() = delete;
  LVReader(StringRef InputFilename, StringRef FileFormatName, ScopedPrinter &W,
           LVBinaryType BinaryType = LVBinaryType::NONE)
      : BinaryType(BinaryType), OutputSplit(options().getOutputSplit()),
        InputFilename(InputFilename), FileFormatName(FileFormatName), W(W),
        OS(W.getOStream()) {}
  LVReader(const LVReader &) = delete;
  LVReader &operator=(const LVReader &) = delete;
  virtual ~LVReader() = default;

// Creates a logical object of the given KIND. The signature for the created
// functions looks like:
//   ...
//   LVScope *createScope()
//   LVScopeRoot *creatScopeRoot()
//   LVType *createType();
//   ...
#define LV_CREATE_OBJECT(KIND)                                                 \
  LV##KIND *create##KIND() {                                                   \
    return new (Allocated##KIND.Allocate()) LV##KIND();                        \
  }

  // Lines creation.
  LV_CREATE_OBJECT(Line)
  LV_CREATE_OBJECT(LineDebug)
  LV_CREATE_OBJECT(LineAssembler)

  // Locations creation.
  LV_CREATE_OBJECT(Location)
  LV_CREATE_OBJECT(LocationSymbol)

  // Scopes creation.
  LV_CREATE_OBJECT(Scope)
  LV_CREATE_OBJECT(ScopeAggregate)
  LV_CREATE_OBJECT(ScopeAlias)
  LV_CREATE_OBJECT(ScopeArray)
  LV_CREATE_OBJECT(ScopeCompileUnit)
  LV_CREATE_OBJECT(ScopeEnumeration)
  LV_CREATE_OBJECT(ScopeFormalPack)
  LV_CREATE_OBJECT(ScopeFunction)
  LV_CREATE_OBJECT(ScopeFunctionInlined)
  LV_CREATE_OBJECT(ScopeFunctionType)
  LV_CREATE_OBJECT(ScopeNamespace)
  LV_CREATE_OBJECT(ScopeRoot)
  LV_CREATE_OBJECT(ScopeTemplatePack)

  // Symbols creation.
  LV_CREATE_OBJECT(Symbol)

  // Types creation.
  LV_CREATE_OBJECT(Type)
  LV_CREATE_OBJECT(TypeDefinition)
  LV_CREATE_OBJECT(TypeEnumerator)
  LV_CREATE_OBJECT(TypeImport)
  LV_CREATE_OBJECT(TypeParam)
  LV_CREATE_OBJECT(TypeSubrange)

#undef LV_CREATE_OBJECT

  // Operations creation.
  LVOperation *createOperation(LVSmall OpCode, ArrayRef<LVUnsigned> Operands) {
    return new (AllocatedOperation.Allocate()) LVOperation(OpCode, Operands);
  }

  StringRef getFilename(LVObject *Object, size_t Index) const;
  StringRef getFilename() const { return InputFilename; }
  void setFilename(std::string Name) { InputFilename = std::move(Name); }
  StringRef getFileFormatName() const { return FileFormatName; }

  raw_ostream &outputStream() { return OS; }

  bool isBinaryTypeNone() const { return BinaryType == LVBinaryType::NONE; }
  bool isBinaryTypeELF() const { return BinaryType == LVBinaryType::ELF; }
  bool isBinaryTypeCOFF() const { return BinaryType == LVBinaryType::COFF; }

  LVScopeCompileUnit *getCompileUnit() const { return CompileUnit; }
  void setCompileUnit(LVScope *Scope) {
    assert(Scope && Scope->isCompileUnit() && "Scope is not a compile unit");
    CompileUnit = static_cast<LVScopeCompileUnit *>(Scope);
  }
  void setCompileUnitCPUType(codeview::CPUType Type) {
    CompileUnit->setCPUType(Type);
  }
  codeview::CPUType getCompileUnitCPUType() {
    return CompileUnit->getCPUType();
  }

  // Access to the scopes root.
  LVScopeRoot *getScopesRoot() const { return Root; }

  Error doPrint();
  Error doLoad();

  virtual std::string getRegisterName(LVSmall Opcode,
                                      ArrayRef<uint64_t> Operands) {
    llvm_unreachable("Invalid instance reader.");
    return {};
  }

  LVSectionIndex getDotTextSectionIndex() const { return DotTextSectionIndex; }
  virtual LVSectionIndex getSectionIndex(LVScope *Scope) {
    return getDotTextSectionIndex();
  }

  virtual bool isSystemEntry(LVElement *Element, StringRef Name = {}) const {
    return false;
  };

  // Access to split context.
  LVSplitContext &getSplitContext() { return SplitContext; }

  // In the case of element comparison, register that added element.
  void notifyAddedElement(LVLine *Line) {
    if (!options().getCompareContext() && options().getCompareLines())
      Lines.push_back(Line);
  }
  void notifyAddedElement(LVScope *Scope) {
    if (!options().getCompareContext() && options().getCompareScopes())
      Scopes.push_back(Scope);
  }
  void notifyAddedElement(LVSymbol *Symbol) {
    if (!options().getCompareContext() && options().getCompareSymbols())
      Symbols.push_back(Symbol);
  }
  void notifyAddedElement(LVType *Type) {
    if (!options().getCompareContext() && options().getCompareTypes())
      Types.push_back(Type);
  }

  const LVLines &getLines() const { return Lines; }
  const LVScopes &getScopes() const { return Scopes; }
  const LVSymbols &getSymbols() const { return Symbols; }
  const LVTypes &getTypes() const { return Types; }

  // Conditions to print an object.
  bool doPrintLine(const LVLine *Line) const {
    return patterns().printElement(Line);
  }
  bool doPrintLocation(const LVLocation *Location) const {
    return patterns().printObject(Location);
  }
  bool doPrintScope(const LVScope *Scope) const {
    return patterns().printElement(Scope);
  }
  bool doPrintSymbol(const LVSymbol *Symbol) const {
    return patterns().printElement(Symbol);
  }
  bool doPrintType(const LVType *Type) const {
    return patterns().printElement(Type);
  }

  static LVReader &getInstance();
  static void setInstance(LVReader *Reader);

  void print(raw_ostream &OS) const;
  virtual void printRecords(raw_ostream &OS) const {}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  void dump() const { print(dbgs()); }
#endif
};

inline LVReader &getReader() { return LVReader::getInstance(); }
inline LVSplitContext &getReaderSplitContext() {
  return getReader().getSplitContext();
}
inline LVScopeCompileUnit *getReaderCompileUnit() {
  return getReader().getCompileUnit();
}

} // end namespace logicalview
} // end namespace llvm

#endif // LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVREADER_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif