LLVM 22.0.0git
MCDwarf.h
Go to the documentation of this file.
1//===- MCDwarf.h - Machine Code Dwarf 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// This file contains the declaration of the MCDwarfFile to support the dwarf
10// .file directive and the .loc directive.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_MC_MCDWARF_H
15#define LLVM_MC_MCDWARF_H
16
17#include "llvm/ADT/MapVector.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/MD5.h"
25#include "llvm/Support/SMLoc.h"
27#include <cassert>
28#include <cstdint>
29#include <optional>
30#include <string>
31#include <utility>
32#include <variant>
33#include <vector>
34
35namespace llvm {
36
37template <typename T> class ArrayRef;
38class MCAsmBackend;
39class MCContext;
41class MCSection;
42class MCStreamer;
43class MCSymbol;
44class raw_ostream;
45class SourceMgr;
46
47namespace mcdwarf {
48// Emit the common part of the DWARF 5 range/locations list tables header.
50} // namespace mcdwarf
51
52/// Manage the .debug_line_str section contents, if we use it.
54 BumpPtrAllocator Alloc;
55 StringSaver Saver{Alloc};
56 MCSymbol *LineStrLabel = nullptr;
58 bool UseRelocs = false;
59
60public:
61 /// Construct an instance that can emit .debug_line_str (for use in a normal
62 /// v5 line table).
63 LLVM_ABI explicit MCDwarfLineStr(MCContext &Ctx);
64
65 StringSaver &getSaver() { return Saver; }
66
67 /// Emit a reference to the string.
68 LLVM_ABI void emitRef(MCStreamer *MCOS, StringRef Path);
69
70 /// Emit the .debug_line_str section if appropriate.
72
73 /// Returns finalized section.
75
76 /// Adds path \p Path to the line string. Returns offset in the
77 /// .debug_line_str section.
78 LLVM_ABI size_t addString(StringRef Path);
79};
80
81/// Instances of this class represent the name of the dwarf .file directive and
82/// its associated dwarf file number in the MC file. MCDwarfFile's are created
83/// and uniqued by the MCContext class. In Dwarf 4 file numbers start from 1;
84/// i.e. the entry with file number 1 is the first element in the vector of
85/// DwarfFiles and there is no MCDwarfFile with file number 0. In Dwarf 5 file
86/// numbers start from 0, with the MCDwarfFile with file number 0 being the
87/// primary source file, and file numbers correspond to their index in the
88/// vector.
90 // The base name of the file without its directory path.
91 std::string Name;
92
93 // The index into the list of directory names for this file name.
94 unsigned DirIndex = 0;
95
96 /// The MD5 checksum, if there is one. Non-owning pointer to data allocated
97 /// in MCContext.
98 std::optional<MD5::MD5Result> Checksum;
99
100 /// The source code of the file. Non-owning reference to data allocated in
101 /// MCContext.
102 std::optional<StringRef> Source;
103};
104
105/// Instances of this class represent the information from a
106/// dwarf .loc directive.
107class MCDwarfLoc {
108 uint32_t FileNum;
109 uint32_t Line;
110 uint16_t Column;
111 // Flags (see #define's below)
112 uint8_t Flags;
113 uint8_t Isa;
114 uint32_t Discriminator;
115
116// Flag that indicates the initial value of the is_stmt_start flag.
117#define DWARF2_LINE_DEFAULT_IS_STMT 1
118
119#define DWARF2_FLAG_IS_STMT (1 << 0)
120#define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
121#define DWARF2_FLAG_PROLOGUE_END (1 << 2)
122#define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
123
124private: // MCContext manages these
125 friend class MCContext;
126 friend class MCDwarfLineEntry;
127
128 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
129 unsigned isa, unsigned discriminator)
130 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
131 Discriminator(discriminator) {}
132
133 // Allow the default copy constructor and assignment operator to be used
134 // for an MCDwarfLoc object.
135
136public:
137 /// Get the FileNum of this MCDwarfLoc.
138 unsigned getFileNum() const { return FileNum; }
139
140 /// Get the Line of this MCDwarfLoc.
141 unsigned getLine() const { return Line; }
142
143 /// Get the Column of this MCDwarfLoc.
144 unsigned getColumn() const { return Column; }
145
146 /// Get the Flags of this MCDwarfLoc.
147 unsigned getFlags() const { return Flags; }
148
149 /// Get the Isa of this MCDwarfLoc.
150 unsigned getIsa() const { return Isa; }
151
152 /// Get the Discriminator of this MCDwarfLoc.
153 unsigned getDiscriminator() const { return Discriminator; }
154
155 /// Set the FileNum of this MCDwarfLoc.
156 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
157
158 /// Set the Line of this MCDwarfLoc.
159 void setLine(unsigned line) { Line = line; }
160
161 /// Set the Column of this MCDwarfLoc.
162 void setColumn(unsigned column) {
163 assert(column <= UINT16_MAX);
164 Column = column;
165 }
166
167 /// Set the Flags of this MCDwarfLoc.
168 void setFlags(unsigned flags) {
169 assert(flags <= UINT8_MAX);
170 Flags = flags;
171 }
172
173 /// Set the Isa of this MCDwarfLoc.
174 void setIsa(unsigned isa) {
175 assert(isa <= UINT8_MAX);
176 Isa = isa;
177 }
178
179 /// Set the Discriminator of this MCDwarfLoc.
180 void setDiscriminator(unsigned discriminator) {
181 Discriminator = discriminator;
182 }
183};
184
185/// Instances of this class represent the line information for
186/// the dwarf line table entries. Which is created after a machine
187/// instruction is assembled and uses an address from a temporary label
188/// created at the current address in the current section and the info from
189/// the last .loc directive seen as stored in the context.
190class MCDwarfLineEntry : public MCDwarfLoc {
191 MCSymbol *Label;
192
193private:
194 // Allow the default copy constructor and assignment operator to be used
195 // for an MCDwarfLineEntry object.
196
197public:
198 // Constructor to create an MCDwarfLineEntry given a symbol and the dwarf loc.
199 MCDwarfLineEntry(MCSymbol *label, const MCDwarfLoc loc,
200 MCSymbol *lineStreamLabel = nullptr,
201 SMLoc streamLabelDefLoc = {})
202 : MCDwarfLoc(loc), Label(label), LineStreamLabel(lineStreamLabel),
203 StreamLabelDefLoc(streamLabelDefLoc) {}
204
205 MCSymbol *getLabel() const { return Label; }
206
207 // This is the label that is to be emitted into the line stream. If this is
208 // non-null and we need to emit a label, also make sure to restart the current
209 // line sequence.
211
212 // Location where LineStreamLabel was defined. If there is an error emitting
213 // LineStreamLabel, we can use the SMLoc to report an error.
215
216 // This indicates the line entry is synthesized for an end entry.
217 bool IsEndEntry = false;
218
219 // Override the label with the given EndLabel.
220 void setEndLabel(MCSymbol *EndLabel) {
221 // If we're setting this to be an end entry, make sure we don't have
222 // LineStreamLabel set.
223 assert(LineStreamLabel == nullptr);
224 Label = EndLabel;
225 IsEndEntry = true;
226 }
227
228 // This is called when an instruction is assembled into the specified
229 // section and if there is information from the last .loc directive that
230 // has yet to have a line entry made for it is made.
231 LLVM_ABI static void make(MCStreamer *MCOS, MCSection *Section);
232};
233
234/// Instances of this class represent the line information for a compile
235/// unit where machine instructions have been assembled after seeing .loc
236/// directives. This is the information used to build the dwarf line
237/// table for a section.
239public:
240 // Add an entry to this MCLineSection's line entries.
241 void addLineEntry(const MCDwarfLineEntry &LineEntry, MCSection *Sec) {
242 MCLineDivisions[Sec].push_back(LineEntry);
243 }
244
245 // Add an end entry by cloning the last entry, if exists, for the section
246 // the given EndLabel belongs to. The label is replaced by the given EndLabel.
247 LLVM_ABI void addEndEntry(MCSymbol *EndLabel);
248
249 using MCDwarfLineEntryCollection = std::vector<MCDwarfLineEntry>;
250 using iterator = MCDwarfLineEntryCollection::iterator;
251 using const_iterator = MCDwarfLineEntryCollection::const_iterator;
253
254private:
255 // A collection of MCDwarfLineEntry for each section.
256 MCLineDivisionMap MCLineDivisions;
257
258public:
259 // Returns the collection of MCDwarfLineEntry for a given Compile Unit ID.
261 return MCLineDivisions;
262 }
263};
264
266 /// First special line opcode - leave room for the standard opcodes.
267 /// Note: If you want to change this, you'll have to update the
268 /// "StandardOpcodeLengths" table that is emitted in
269 /// \c Emit().
271 /// Minimum line offset in a special line info. opcode. The value
272 /// -5 was chosen to give a reasonable range of values.
273 int8_t DWARF2LineBase = -5;
274 /// Range of line offsets in a special line info. opcode.
276};
277
279 MCSymbol *Label = nullptr;
283 std::string CompilationDir;
285 bool HasAnySource = false;
286
287private:
288 bool HasAllMD5 = true;
289 bool HasAnyMD5 = false;
290
291public:
293
295 StringRef &FileName,
296 std::optional<MD5::MD5Result> Checksum,
297 std::optional<StringRef> Source,
298 uint16_t DwarfVersion,
299 unsigned FileNumber = 0);
300 LLVM_ABI std::pair<MCSymbol *, MCSymbol *>
302 std::optional<MCDwarfLineStr> &LineStr) const;
303 LLVM_ABI std::pair<MCSymbol *, MCSymbol *>
305 ArrayRef<char> SpecialOpcodeLengths,
306 std::optional<MCDwarfLineStr> &LineStr) const;
308 HasAllMD5 = true;
309 HasAnyMD5 = false;
310 }
311 void trackMD5Usage(bool MD5Used) {
312 HasAllMD5 &= MD5Used;
313 HasAnyMD5 |= MD5Used;
314 }
315 bool isMD5UsageConsistent() const {
316 return MCDwarfFiles.empty() || (HasAllMD5 == HasAnyMD5);
317 }
318
319 void setRootFile(StringRef Directory, StringRef FileName,
320 std::optional<MD5::MD5Result> Checksum,
321 std::optional<StringRef> Source) {
322 CompilationDir = std::string(Directory);
323 RootFile.Name = std::string(FileName);
324 RootFile.DirIndex = 0;
325 RootFile.Checksum = Checksum;
326 RootFile.Source = Source;
327 trackMD5Usage(Checksum.has_value());
328 HasAnySource |= Source.has_value();
329 }
330
332 MCDwarfDirs.clear();
333 MCDwarfFiles.clear();
334 RootFile.Name.clear();
336 HasAnySource = false;
337 }
338
339private:
340 void emitV2FileDirTables(MCStreamer *MCOS) const;
341 void emitV5FileDirTables(MCStreamer *MCOS,
342 std::optional<MCDwarfLineStr> &LineStr) const;
343};
344
347 bool HasSplitLineTable = false;
348
349public:
350 void maybeSetRootFile(StringRef Directory, StringRef FileName,
351 std::optional<MD5::MD5Result> Checksum,
352 std::optional<StringRef> Source) {
353 if (!Header.RootFile.Name.empty())
354 return;
355 Header.setRootFile(Directory, FileName, Checksum, Source);
356 }
357
358 unsigned getFile(StringRef Directory, StringRef FileName,
359 std::optional<MD5::MD5Result> Checksum,
360 uint16_t DwarfVersion, std::optional<StringRef> Source) {
361 HasSplitLineTable = true;
362 return cantFail(Header.tryGetFile(Directory, FileName, Checksum, Source,
363 DwarfVersion));
364 }
365
367 MCSection *Section) const;
368};
369
372 MCLineSection MCLineSections;
373
374public:
375 // This emits the Dwarf file and the line tables for all Compile Units.
376 LLVM_ABI static void emit(MCStreamer *MCOS, MCDwarfLineTableParams Params);
377
378 // This emits the Dwarf file and the line tables for a given Compile Unit.
380 std::optional<MCDwarfLineStr> &LineStr) const;
381
382 // This emits a single line table associated with a given Section.
383 LLVM_ABI static void
384 emitOne(MCStreamer *MCOS, MCSection *Section,
386
388 SMLoc DefLoc,
389 StringRef Name);
390
392 StringRef &FileName,
393 std::optional<MD5::MD5Result> Checksum,
394 std::optional<StringRef> Source,
395 uint16_t DwarfVersion,
396 unsigned FileNumber = 0);
397 unsigned getFile(StringRef &Directory, StringRef &FileName,
398 std::optional<MD5::MD5Result> Checksum,
399 std::optional<StringRef> Source, uint16_t DwarfVersion,
400 unsigned FileNumber = 0) {
401 return cantFail(tryGetFile(Directory, FileName, Checksum, Source,
402 DwarfVersion, FileNumber));
403 }
404
405 void setRootFile(StringRef Directory, StringRef FileName,
406 std::optional<MD5::MD5Result> Checksum,
407 std::optional<StringRef> Source) {
408 Header.CompilationDir = std::string(Directory);
409 Header.RootFile.Name = std::string(FileName);
410 Header.RootFile.DirIndex = 0;
411 Header.RootFile.Checksum = Checksum;
412 Header.RootFile.Source = Source;
413 Header.trackMD5Usage(Checksum.has_value());
414 Header.HasAnySource |= Source.has_value();
415 }
416
417 void resetFileTable() { Header.resetFileTable(); }
418
419 bool hasRootFile() const { return !Header.RootFile.Name.empty(); }
420
421 MCDwarfFile &getRootFile() { return Header.RootFile; }
422 const MCDwarfFile &getRootFile() const { return Header.RootFile; }
423
424 // Report whether MD5 usage has been consistent (all-or-none).
425 bool isMD5UsageConsistent() const { return Header.isMD5UsageConsistent(); }
426
428 return Header.Label;
429 }
430
431 void setLabel(MCSymbol *Label) {
432 Header.Label = Label;
433 }
434
436 return Header.MCDwarfDirs;
437 }
438
440 return Header.MCDwarfDirs;
441 }
442
444 return Header.MCDwarfFiles;
445 }
446
448 return Header.MCDwarfFiles;
449 }
450
452 return MCLineSections;
453 }
455 return MCLineSections;
456 }
457};
458
460public:
461 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
462 LLVM_ABI static void encode(MCContext &Context, MCDwarfLineTableParams Params,
463 int64_t LineDelta, uint64_t AddrDelta,
465
466 /// Utility function to emit the encoding to a streamer.
467 LLVM_ABI static void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
468 int64_t LineDelta, uint64_t AddrDelta);
469};
470
472public:
473 //
474 // When generating dwarf for assembly source files this emits the Dwarf
475 // sections.
476 //
477 LLVM_ABI static void Emit(MCStreamer *MCOS);
478};
479
480// When generating dwarf for assembly source files this is the info that is
481// needed to be gathered for each symbol that will have a dwarf label.
483private:
484 // Name of the symbol without a leading underbar, if any.
485 StringRef Name;
486 // The dwarf file number this symbol is in.
487 unsigned FileNumber;
488 // The line number this symbol is at.
489 unsigned LineNumber;
490 // The low_pc for the dwarf label is taken from this symbol.
491 MCSymbol *Label;
492
493public:
494 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
495 MCSymbol *label)
496 : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
497 Label(label) {}
498
499 StringRef getName() const { return Name; }
500 unsigned getFileNumber() const { return FileNumber; }
501 unsigned getLineNumber() const { return LineNumber; }
502 MCSymbol *getLabel() const { return Label; }
503
504 // This is called when label is created when we are generating dwarf for
505 // assembly source files.
506 LLVM_ABI static void Make(MCSymbol *Symbol, MCStreamer *MCOS,
508};
509
510class MCCFIInstruction {
511public:
534
535 // Held in ExtraFields for most common OpTypes, exceptions follow.
537 unsigned Register;
538 int64_t Offset;
539 unsigned Register2;
540 unsigned AddressSpace;
541 // FIXME: Workaround for GCC7 bug with nested class used as std::variant
542 // alternative where the compiler really wants a user-defined default
543 // constructor. Once we no longer support GCC7 these constructors can be
544 // replaced with default member initializers and aggregate initialization.
545 CommonFields(unsigned Reg, int64_t Off = 0,
546 unsigned Reg2 = std::numeric_limits<unsigned>::max(),
547 unsigned AddrSpace = 0)
548 : Register(Reg), Offset(Off), Register2(Reg2), AddressSpace(AddrSpace) {
549 }
550 CommonFields() : CommonFields(std::numeric_limits<unsigned>::max()) {}
551 };
552 // Held in ExtraFields when OpEscape.
554 std::vector<char> Values;
555 std::string Comment;
556 };
557 // Held in ExtraFields when OpLabel.
558 struct LabelFields {
559 MCSymbol *CfiLabel = nullptr;
560 };
561
562private:
563 MCSymbol *Label;
564 std::variant<CommonFields, EscapeFields, LabelFields> ExtraFields;
565 OpType Operation;
566 SMLoc Loc;
567
568 template <class FieldsType>
569 MCCFIInstruction(OpType Op, MCSymbol *L, FieldsType &&EF, SMLoc Loc)
570 : Label(L), ExtraFields(std::forward<FieldsType>(EF)), Operation(Op),
571 Loc(Loc) {}
572
573public:
574 /// .cfi_def_cfa defines a rule for computing CFA as: take address from
575 /// Register and add Offset to it.
576 static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register,
577 int64_t Offset, SMLoc Loc = {}) {
578 return {OpDefCfa, L, CommonFields{Register, Offset}, Loc};
579 }
580
581 /// .cfi_def_cfa_register modifies a rule for computing CFA. From now
582 /// on Register will be used instead of the old one. Offset remains the same.
583 static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register,
584 SMLoc Loc = {}) {
585 return {OpDefCfaRegister, L, CommonFields{Register}, Loc};
586 }
587
588 /// .cfi_def_cfa_offset modifies a rule for computing CFA. Register
589 /// remains the same, but offset is new. Note that it is the absolute offset
590 /// that will be added to a defined register to the compute CFA address.
591 static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset,
592 SMLoc Loc = {}) {
593 return {OpDefCfaOffset, L, CommonFields{0, Offset}, Loc};
594 }
595
596 /// .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
597 /// Offset is a relative value that is added/subtracted from the previous
598 /// offset.
599 static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int64_t Adjustment,
600 SMLoc Loc = {}) {
601 return {OpAdjustCfaOffset, L, CommonFields{0, Adjustment}, Loc};
602 }
603
604 // FIXME: Update the remaining docs to use the new proposal wording.
605 /// .cfi_llvm_def_aspace_cfa defines the rule for computing the CFA to
606 /// be the result of evaluating the DWARF operation expression
607 /// `DW_OP_constu AS; DW_OP_aspace_bregx R, B` as a location description.
608 static MCCFIInstruction createLLVMDefAspaceCfa(MCSymbol *L, unsigned Register,
609 int64_t Offset,
610 unsigned AddressSpace,
611 SMLoc Loc) {
612 return {OpLLVMDefAspaceCfa, L,
614 }
615
616 /// .cfi_offset Previous value of Register is saved at offset Offset
617 /// from CFA.
618 static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
619 int64_t Offset, SMLoc Loc = {}) {
620 return {OpOffset, L, CommonFields{Register, Offset}, Loc};
621 }
622
623 /// .cfi_rel_offset Previous value of Register is saved at offset
624 /// Offset from the current CFA register. This is transformed to .cfi_offset
625 /// using the known displacement of the CFA register from the CFA.
626 static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
627 int64_t Offset, SMLoc Loc = {}) {
628 return {OpRelOffset, L, CommonFields{Register, Offset}, Loc};
629 }
630
631 /// .cfi_register Previous value of Register1 is saved in
632 /// register Register2.
633 static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
634 unsigned Register2, SMLoc Loc = {}) {
635 return {OpRegister, L, CommonFields{Register1, 0, Register2}, Loc};
636 }
637
638 /// .cfi_window_save SPARC register window is saved.
639 static MCCFIInstruction createWindowSave(MCSymbol *L, SMLoc Loc = {}) {
640 return {OpWindowSave, L, CommonFields{}, Loc};
641 }
642
643 /// .cfi_negate_ra_state AArch64 negate RA state.
644 static MCCFIInstruction createNegateRAState(MCSymbol *L, SMLoc Loc = {}) {
645 return {OpNegateRAState, L, CommonFields{}, Loc};
646 }
647
648 /// .cfi_negate_ra_state_with_pc AArch64 negate RA state with PC.
649 static MCCFIInstruction createNegateRAStateWithPC(MCSymbol *L,
650 SMLoc Loc = {}) {
651 return {OpNegateRAStateWithPC, L, CommonFields{}, Loc};
652 }
653
654 /// .cfi_restore says that the rule for Register is now the same as it
655 /// was at the beginning of the function, after all initial instructions added
656 /// by .cfi_startproc were executed.
657 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register,
658 SMLoc Loc = {}) {
659 return {OpRestore, L, CommonFields{Register}, Loc};
660 }
661
662 /// .cfi_undefined From now on the previous value of Register can't be
663 /// restored anymore.
664 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register,
665 SMLoc Loc = {}) {
666 return {OpUndefined, L, CommonFields{Register}, Loc};
667 }
668
669 /// .cfi_same_value Current value of Register is the same as in the
670 /// previous frame. I.e., no restoration is needed.
671 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register,
672 SMLoc Loc = {}) {
673 return {OpSameValue, L, CommonFields{Register}, Loc};
674 }
675
676 /// .cfi_remember_state Save all current rules for all registers.
677 static MCCFIInstruction createRememberState(MCSymbol *L, SMLoc Loc = {}) {
678 return {OpRememberState, L, CommonFields{}, Loc};
679 }
680
681 /// .cfi_restore_state Restore the previously saved state.
682 static MCCFIInstruction createRestoreState(MCSymbol *L, SMLoc Loc = {}) {
683 return {OpRestoreState, L, CommonFields{}, Loc};
684 }
685
686 /// .cfi_escape Allows the user to add arbitrary bytes to the unwind
687 /// info.
688 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals,
689 SMLoc Loc = {}, StringRef Comment = "") {
690 return {OpEscape, L,
691 EscapeFields{std::vector<char>(Vals.begin(), Vals.end()),
692 Comment.str()},
693 Loc};
694 }
695
696 /// A special wrapper for .cfi_escape that indicates GNU_ARGS_SIZE
697 static MCCFIInstruction createGnuArgsSize(MCSymbol *L, int64_t Size,
698 SMLoc Loc = {}) {
699 return {OpGnuArgsSize, L, CommonFields{0, Size}, Loc};
700 }
701
702 static MCCFIInstruction createLabel(MCSymbol *L, MCSymbol *CfiLabel,
703 SMLoc Loc) {
704 return {OpLabel, L, LabelFields{CfiLabel}, Loc};
705 }
706
707 /// .cfi_val_offset Previous value of Register is offset Offset from the
708 /// current CFA register.
709 static MCCFIInstruction createValOffset(MCSymbol *L, unsigned Register,
710 int64_t Offset, SMLoc Loc = {}) {
711 return {OpValOffset, L, CommonFields{Register, Offset}, Loc};
712 }
713
714 OpType getOperation() const { return Operation; }
715 MCSymbol *getLabel() const { return Label; }
716
717 unsigned getRegister() const {
718 assert(Operation == OpDefCfa || Operation == OpOffset ||
719 Operation == OpRestore || Operation == OpUndefined ||
720 Operation == OpSameValue || Operation == OpDefCfaRegister ||
721 Operation == OpRelOffset || Operation == OpValOffset ||
722 Operation == OpRegister || Operation == OpLLVMDefAspaceCfa);
723 return std::get<CommonFields>(ExtraFields).Register;
724 }
725
726 unsigned getRegister2() const {
727 assert(Operation == OpRegister);
728 return std::get<CommonFields>(ExtraFields).Register2;
729 }
730
731 unsigned getAddressSpace() const {
732 assert(Operation == OpLLVMDefAspaceCfa);
733 return std::get<CommonFields>(ExtraFields).AddressSpace;
734 }
735
736 int64_t getOffset() const {
737 assert(Operation == OpDefCfa || Operation == OpOffset ||
738 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
739 Operation == OpAdjustCfaOffset || Operation == OpGnuArgsSize ||
740 Operation == OpValOffset || Operation == OpLLVMDefAspaceCfa);
741 return std::get<CommonFields>(ExtraFields).Offset;
742 }
743
745 assert(Operation == OpLabel);
746 return std::get<LabelFields>(ExtraFields).CfiLabel;
747 }
748
750 assert(Operation == OpEscape);
751 auto &Values = std::get<EscapeFields>(ExtraFields).Values;
752 return StringRef(&Values[0], Values.size());
753 }
754
756 assert(Operation == OpEscape);
757 return std::get<EscapeFields>(ExtraFields).Comment;
758 }
759 SMLoc getLoc() const { return Loc; }
760};
761
763 MCDwarfFrameInfo() = default;
764
765 MCSymbol *Begin = nullptr;
766 MCSymbol *End = nullptr;
767 const MCSymbol *Personality = nullptr;
768 const MCSymbol *Lsda = nullptr;
769 std::vector<MCCFIInstruction> Instructions;
770 unsigned CurrentCfaRegister = 0;
772 unsigned LsdaEncoding = 0;
774 bool IsSignalFrame = false;
775 bool IsSimple = false;
776 unsigned RAReg = static_cast<unsigned>(INT_MAX);
777 bool IsBKeyFrame = false;
778 bool IsMTETaggedFrame = false;
779};
780
782public:
783 //
784 // This emits the frame info section.
785 //
786 LLVM_ABI static void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB,
787 bool isEH);
788 LLVM_ABI static void encodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
790};
791
792} // end namespace llvm
793
794#endif // LLVM_MC_MCDWARF_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
#define LLVM_ABI
Definition Compiler.h:213
Register Reg
This file implements a map that provides insertion order iteration.
static const char * name
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Tagged union holding either a T or a Error.
Definition Error.h:485
Generic interface to target specific assembler backends.
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
Definition MCDwarf.h:583
MCSymbol * getLabel() const
Definition MCDwarf.h:715
static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_undefined From now on the previous value of Register can't be restored anymore.
Definition MCDwarf.h:664
static MCCFIInstruction createGnuArgsSize(MCSymbol *L, int64_t Size, SMLoc Loc={})
A special wrapper for .cfi_escape that indicates GNU_ARGS_SIZE.
Definition MCDwarf.h:697
unsigned getAddressSpace() const
Definition MCDwarf.h:731
static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_restore says that the rule for Register is now the same as it was at the beginning of the functi...
Definition MCDwarf.h:657
unsigned getRegister2() const
Definition MCDwarf.h:726
static MCCFIInstruction createLLVMDefAspaceCfa(MCSymbol *L, unsigned Register, int64_t Offset, unsigned AddressSpace, SMLoc Loc)
.cfi_llvm_def_aspace_cfa defines the rule for computing the CFA to be the result of evaluating the DW...
Definition MCDwarf.h:608
unsigned getRegister() const
Definition MCDwarf.h:717
static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1, unsigned Register2, SMLoc Loc={})
.cfi_register Previous value of Register1 is saved in register Register2.
Definition MCDwarf.h:633
static MCCFIInstruction cfiDefCfa(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa defines a rule for computing CFA as: take address from Register and add Offset to it.
Definition MCDwarf.h:576
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
Definition MCDwarf.h:618
SMLoc getLoc() const
Definition MCDwarf.h:759
static MCCFIInstruction createValOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_val_offset Previous value of Register is offset Offset from the current CFA register.
Definition MCDwarf.h:709
static MCCFIInstruction createNegateRAStateWithPC(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state_with_pc AArch64 negate RA state with PC.
Definition MCDwarf.h:649
static MCCFIInstruction createNegateRAState(MCSymbol *L, SMLoc Loc={})
.cfi_negate_ra_state AArch64 negate RA state.
Definition MCDwarf.h:644
static MCCFIInstruction createRememberState(MCSymbol *L, SMLoc Loc={})
.cfi_remember_state Save all current rules for all registers.
Definition MCDwarf.h:677
OpType getOperation() const
Definition MCDwarf.h:714
StringRef getComment() const
Definition MCDwarf.h:755
StringRef getValues() const
Definition MCDwarf.h:749
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
Definition MCDwarf.h:591
static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals, SMLoc Loc={}, StringRef Comment="")
.cfi_escape Allows the user to add arbitrary bytes to the unwind info.
Definition MCDwarf.h:688
static MCCFIInstruction createWindowSave(MCSymbol *L, SMLoc Loc={})
.cfi_window_save SPARC register window is saved.
Definition MCDwarf.h:639
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int64_t Adjustment, SMLoc Loc={})
.cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but Offset is a relative value that is added/subt...
Definition MCDwarf.h:599
MCSymbol * getCfiLabel() const
Definition MCDwarf.h:744
static MCCFIInstruction createRestoreState(MCSymbol *L, SMLoc Loc={})
.cfi_restore_state Restore the previously saved state.
Definition MCDwarf.h:682
static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_same_value Current value of Register is the same as in the previous frame.
Definition MCDwarf.h:671
static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_rel_offset Previous value of Register is saved at offset Offset from the current CFA register.
Definition MCDwarf.h:626
int64_t getOffset() const
Definition MCDwarf.h:736
static MCCFIInstruction createLabel(MCSymbol *L, MCSymbol *CfiLabel, SMLoc Loc)
Definition MCDwarf.h:702
Context object for machine code objects.
Definition MCContext.h:83
void maybeSetRootFile(StringRef Directory, StringRef FileName, std::optional< MD5::MD5Result > Checksum, std::optional< StringRef > Source)
Definition MCDwarf.h:350
unsigned getFile(StringRef Directory, StringRef FileName, std::optional< MD5::MD5Result > Checksum, uint16_t DwarfVersion, std::optional< StringRef > Source)
Definition MCDwarf.h:358
LLVM_ABI void Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params, MCSection *Section) const
Definition MCDwarf.cpp:335
static LLVM_ABI void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH)
Definition MCDwarf.cpp:1906
static LLVM_ABI void encodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, SmallVectorImpl< char > &OS)
Definition MCDwarf.cpp:1982
static LLVM_ABI void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta)
Utility function to emit the encoding to a streamer.
Definition MCDwarf.cpp:730
static LLVM_ABI void encode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, SmallVectorImpl< char > &OS)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Definition MCDwarf.cpp:745
Instances of this class represent the line information for the dwarf line table entries.
Definition MCDwarf.h:190
void setEndLabel(MCSymbol *EndLabel)
Definition MCDwarf.h:220
MCSymbol * getLabel() const
Definition MCDwarf.h:205
MCDwarfLineEntry(MCSymbol *label, const MCDwarfLoc loc, MCSymbol *lineStreamLabel=nullptr, SMLoc streamLabelDefLoc={})
Definition MCDwarf.h:199
MCSymbol * LineStreamLabel
Definition MCDwarf.h:210
static LLVM_ABI void make(MCStreamer *MCOS, MCSection *Section)
Definition MCDwarf.cpp:91
StringSaver & getSaver()
Definition MCDwarf.h:65
LLVM_ABI void emitSection(MCStreamer *MCOS)
Emit the .debug_line_str section if appropriate.
Definition MCDwarf.cpp:385
LLVM_ABI MCDwarfLineStr(MCContext &Ctx)
Construct an instance that can emit .debug_line_str (for use in a normal v5 line table).
Definition MCDwarf.cpp:76
LLVM_ABI SmallString< 0 > getFinalizedData()
Returns finalized section.
Definition MCDwarf.cpp:393
LLVM_ABI void emitRef(MCStreamer *MCOS, StringRef Path)
Emit a reference to the string.
Definition MCDwarf.cpp:407
LLVM_ABI size_t addString(StringRef Path)
Adds path Path to the line string.
Definition MCDwarf.cpp:403
bool isMD5UsageConsistent() const
Definition MCDwarf.h:425
const MCDwarfFile & getRootFile() const
Definition MCDwarf.h:422
void setLabel(MCSymbol *Label)
Definition MCDwarf.h:431
const SmallVectorImpl< std::string > & getMCDwarfDirs() const
Definition MCDwarf.h:435
LLVM_ABI void endCurrentSeqAndEmitLineStreamLabel(MCStreamer *MCOS, SMLoc DefLoc, StringRef Name)
Definition MCDwarf.cpp:289
void setRootFile(StringRef Directory, StringRef FileName, std::optional< MD5::MD5Result > Checksum, std::optional< StringRef > Source)
Definition MCDwarf.h:405
MCDwarfFile & getRootFile()
Definition MCDwarf.h:421
const MCLineSection & getMCLineSections() const
Definition MCDwarf.h:451
static LLVM_ABI void emit(MCStreamer *MCOS, MCDwarfLineTableParams Params)
Definition MCDwarf.cpp:308
bool hasRootFile() const
Definition MCDwarf.h:419
unsigned getFile(StringRef &Directory, StringRef &FileName, std::optional< MD5::MD5Result > Checksum, std::optional< StringRef > Source, uint16_t DwarfVersion, unsigned FileNumber=0)
Definition MCDwarf.h:397
SmallVectorImpl< std::string > & getMCDwarfDirs()
Definition MCDwarf.h:439
SmallVectorImpl< MCDwarfFile > & getMCDwarfFiles()
Definition MCDwarf.h:447
MCLineSection & getMCLineSections()
Definition MCDwarf.h:454
MCSymbol * getLabel() const
Definition MCDwarf.h:427
static LLVM_ABI void emitOne(MCStreamer *MCOS, MCSection *Section, const MCLineSection::MCDwarfLineEntryCollection &LineEntries)
Definition MCDwarf.cpp:178
LLVM_ABI Expected< unsigned > tryGetFile(StringRef &Directory, StringRef &FileName, std::optional< MD5::MD5Result > Checksum, std::optional< StringRef > Source, uint16_t DwarfVersion, unsigned FileNumber=0)
Definition MCDwarf.cpp:631
LLVM_ABI void emitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params, std::optional< MCDwarfLineStr > &LineStr) const
Definition MCDwarf.cpp:617
const SmallVectorImpl< MCDwarfFile > & getMCDwarfFiles() const
Definition MCDwarf.h:443
Instances of this class represent the information from a dwarf .loc directive.
Definition MCDwarf.h:107
friend class MCDwarfLineEntry
Definition MCDwarf.h:126
unsigned getLine() const
Get the Line of this MCDwarfLoc.
Definition MCDwarf.h:141
void setLine(unsigned line)
Set the Line of this MCDwarfLoc.
Definition MCDwarf.h:159
unsigned getIsa() const
Get the Isa of this MCDwarfLoc.
Definition MCDwarf.h:150
void setIsa(unsigned isa)
Set the Isa of this MCDwarfLoc.
Definition MCDwarf.h:174
void setDiscriminator(unsigned discriminator)
Set the Discriminator of this MCDwarfLoc.
Definition MCDwarf.h:180
unsigned getFlags() const
Get the Flags of this MCDwarfLoc.
Definition MCDwarf.h:147
unsigned getColumn() const
Get the Column of this MCDwarfLoc.
Definition MCDwarf.h:144
friend class MCContext
Definition MCDwarf.h:125
void setFlags(unsigned flags)
Set the Flags of this MCDwarfLoc.
Definition MCDwarf.h:168
unsigned getDiscriminator() const
Get the Discriminator of this MCDwarfLoc.
Definition MCDwarf.h:153
void setColumn(unsigned column)
Set the Column of this MCDwarfLoc.
Definition MCDwarf.h:162
unsigned getFileNum() const
Get the FileNum of this MCDwarfLoc.
Definition MCDwarf.h:138
void setFileNum(unsigned fileNum)
Set the FileNum of this MCDwarfLoc.
Definition MCDwarf.h:156
static LLVM_ABI void Emit(MCStreamer *MCOS)
Definition MCDwarf.cpp:1195
MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber, MCSymbol *label)
Definition MCDwarf.h:494
StringRef getName() const
Definition MCDwarf.h:499
static LLVM_ABI void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr, SMLoc &Loc)
Definition MCDwarf.cpp:1258
MCSymbol * getLabel() const
Definition MCDwarf.h:502
unsigned getLineNumber() const
Definition MCDwarf.h:501
unsigned getFileNumber() const
Definition MCDwarf.h:500
Instances of this class represent the line information for a compile unit where machine instructions ...
Definition MCDwarf.h:238
const MCLineDivisionMap & getMCLineEntries() const
Definition MCDwarf.h:260
LLVM_ABI void addEndEntry(MCSymbol *EndLabel)
Definition MCDwarf.cpp:142
MapVector< MCSection *, MCDwarfLineEntryCollection > MCLineDivisionMap
Definition MCDwarf.h:252
MCDwarfLineEntryCollection::const_iterator const_iterator
Definition MCDwarf.h:251
MCDwarfLineEntryCollection::iterator iterator
Definition MCDwarf.h:250
void addLineEntry(const MCDwarfLineEntry &LineEntry, MCSection *Sec)
Definition MCDwarf.h:241
std::vector< MCDwarfLineEntry > MCDwarfLineEntryCollection
Definition MCDwarf.h:249
Streaming object file generation interface.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:517
Streaming machine code generation interface.
Definition MCStreamer.h:220
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represents a location in source code.
Definition SMLoc.h:22
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:37
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
iterator begin() const
Definition StringRef.h:112
iterator end() const
Definition StringRef.h:114
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition StringSaver.h:22
Utility for building string tables with deduplicated suffixes.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI MCSymbol * emitListsTableHeaderStart(MCStreamer &S)
Definition MCDwarf.cpp:44
This is an optimization pass for GlobalISel generic memory operations.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
@ Offset
Definition DWP.cpp:532
SourceMgr SrcMgr
Definition Error.cpp:24
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
DWARFExpression::Operation Op
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
CommonFields(unsigned Reg, int64_t Off=0, unsigned Reg2=std::numeric_limits< unsigned >::max(), unsigned AddrSpace=0)
Definition MCDwarf.h:545
Instances of this class represent the name of the dwarf .file directive and its associated dwarf file...
Definition MCDwarf.h:89
std::optional< MD5::MD5Result > Checksum
The MD5 checksum, if there is one.
Definition MCDwarf.h:98
std::string Name
Definition MCDwarf.h:91
std::optional< StringRef > Source
The source code of the file.
Definition MCDwarf.h:102
unsigned DirIndex
Definition MCDwarf.h:94
const MCSymbol * Personality
Definition MCDwarf.h:767
unsigned PersonalityEncoding
Definition MCDwarf.h:771
uint64_t CompactUnwindEncoding
Definition MCDwarf.h:773
std::vector< MCCFIInstruction > Instructions
Definition MCDwarf.h:769
const MCSymbol * Lsda
Definition MCDwarf.h:768
unsigned CurrentCfaRegister
Definition MCDwarf.h:770
void trackMD5Usage(bool MD5Used)
Definition MCDwarf.h:311
SmallVector< MCDwarfFile, 3 > MCDwarfFiles
Definition MCDwarf.h:281
bool isMD5UsageConsistent() const
Definition MCDwarf.h:315
SmallVector< std::string, 3 > MCDwarfDirs
Definition MCDwarf.h:280
LLVM_ABI std::pair< MCSymbol *, MCSymbol * > Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, std::optional< MCDwarfLineStr > &LineStr) const
Definition MCDwarf.cpp:345
StringMap< unsigned > SourceIdMap
Definition MCDwarf.h:282
void setRootFile(StringRef Directory, StringRef FileName, std::optional< MD5::MD5Result > Checksum, std::optional< StringRef > Source)
Definition MCDwarf.h:319
LLVM_ABI Expected< unsigned > tryGetFile(StringRef &Directory, StringRef &FileName, std::optional< MD5::MD5Result > Checksum, std::optional< StringRef > Source, uint16_t DwarfVersion, unsigned FileNumber=0)
Definition MCDwarf.cpp:648
uint8_t DWARF2LineOpcodeBase
First special line opcode - leave room for the standard opcodes.
Definition MCDwarf.h:270
uint8_t DWARF2LineRange
Range of line offsets in a special line info. opcode.
Definition MCDwarf.h:275
int8_t DWARF2LineBase
Minimum line offset in a special line info.
Definition MCDwarf.h:273