LLVM 23.0.0git
MCObjectStreamer.cpp
Go to the documentation of this file.
1//===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
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
11#include "llvm/MC/MCAsmInfo.h"
12#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCCodeView.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCDwarf.h"
17#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCSFrame.h"
21#include "llvm/MC/MCSection.h"
22#include "llvm/MC/MCSymbol.h"
25using namespace llvm;
26
28 std::unique_ptr<MCAsmBackend> TAB,
29 std::unique_ptr<MCObjectWriter> OW,
30 std::unique_ptr<MCCodeEmitter> Emitter)
31 : MCStreamer(Context),
32 Assembler(std::make_unique<MCAssembler>(
33 Context, std::move(TAB), std::move(Emitter), std::move(OW))),
34 EmitEHFrame(true), EmitDebugFrame(false), EmitSFrame(false) {
35 assert(Assembler->getBackendPtr() && Assembler->getEmitterPtr());
36 IsObj = true;
37 setAllowAutoPadding(Assembler->getBackend().allowAutoPadding());
38 if (Context.getTargetOptions() && Context.getTargetOptions()->MCRelaxAll)
39 Assembler->setRelaxAll(true);
40}
41
43
46 return Assembler.get();
47 return nullptr;
48}
49
50constexpr size_t FragBlockSize = 16384;
51// Ensure the new fragment can at least store a few bytes.
52constexpr size_t NewFragHeadroom = 8;
53
54static_assert(NewFragHeadroom >= alignof(MCFragment));
55static_assert(FragBlockSize >= sizeof(MCFragment) + NewFragHeadroom);
56
58 auto Size = std::max(FragBlockSize, sizeof(MCFragment) + Headroom);
59 FragSpace = Size - sizeof(MCFragment);
60 auto Block = std::unique_ptr<uint8_t[]>(new uint8_t[Size]);
61 auto *F = reinterpret_cast<MCFragment *>(Block.get());
62 FragStorage.push_back(std::move(Block));
63 return F;
64}
65
68 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
69 auto End = reinterpret_cast<size_t>(getCurFragEnd());
70 F = reinterpret_cast<MCFragment *>(
71 alignToPowerOf2(End, alignof(MCFragment)));
72 FragSpace -= size_t(F) - End + sizeof(MCFragment);
73 } else {
74 F = allocFragSpace(0);
75 }
76 new (F) MCFragment();
78}
79
80void MCObjectStreamer::ensureHeadroom(size_t Headroom) {
81 if (Headroom <= FragSpace)
82 return;
83 auto *F = allocFragSpace(Headroom);
84 new (F) MCFragment();
86}
87
88void MCObjectStreamer::addSpecialFragment(MCFragment *Frag) {
90 "Frag should have a variable-size tail");
91 // Frag is not connected to FragSpace. Before modifying CurFrag with
92 // addFragment(Frag), allocate an empty fragment to maintain FragSpace
93 // connectivity, potentially reusing CurFrag's associated space.
95 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
96 auto End = reinterpret_cast<size_t>(getCurFragEnd());
97 F = reinterpret_cast<MCFragment *>(
98 alignToPowerOf2(End, alignof(MCFragment)));
99 FragSpace -= size_t(F) - End + sizeof(MCFragment);
100 } else {
101 F = allocFragSpace(0);
102 }
103 new (F) MCFragment();
104
105 addFragment(Frag);
106 addFragment(F);
107}
108
110 ensureHeadroom(Contents.size());
111 assert(FragSpace >= Contents.size());
112 // As this is performance-sensitive code, explicitly use std::memcpy.
113 // Optimization of std::copy to memmove is unreliable.
114 if (!Contents.empty())
115 std::memcpy(getCurFragEnd(), Contents.begin(), Contents.size());
116 CurFrag->FixedSize += Contents.size();
117 FragSpace -= Contents.size();
118}
119
121 ensureHeadroom(Num);
123 llvm::fill(Data, Elt);
124 CurFrag->FixedSize += Num;
125 FragSpace -= Num;
126}
127
131
132// As a compile-time optimization, avoid allocating and evaluating an MCExpr
133// tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment's fixed
134// part.
135static std::optional<uint64_t> absoluteSymbolDiff(const MCSymbol *Hi,
136 const MCSymbol *Lo) {
137 assert(Hi && Lo);
138 if (Lo == Hi)
139 return 0;
140 if (Hi->isVariable() || Lo->isVariable())
141 return std::nullopt;
142 auto *LoF = Lo->getFragment();
143 if (!LoF || Hi->getFragment() != LoF || LoF->isLinkerRelaxable())
144 return std::nullopt;
145 // If either symbol resides in the variable part, bail out.
146 auto Fixed = LoF->getFixedSize();
147 if (Lo->getOffset() > Fixed || Hi->getOffset() > Fixed)
148 return std::nullopt;
149
150 return Hi->getOffset() - Lo->getOffset();
151}
152
154 const MCSymbol *Lo,
155 unsigned Size) {
156 if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo))
157 emitIntValue(*Diff, Size);
158 else
160}
161
163 const MCSymbol *Lo) {
164 if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo))
165 emitULEB128IntValue(*Diff);
166 else
168}
169
171 if (Assembler) {
172 Assembler->reset();
173 if (getContext().getTargetOptions())
174 Assembler->setRelaxAll(getContext().getTargetOptions()->MCRelaxAll);
175 }
176 EmitEHFrame = true;
177 EmitDebugFrame = false;
178 FragStorage.clear();
179 FragSpace = 0;
180 SpecialFragAllocator.Reset();
182}
183
185 if (!getNumFrameInfos())
186 return;
187
188 auto *MAB = &getAssembler().getBackend();
189 if (EmitEHFrame)
190 MCDwarfFrameEmitter::Emit(*this, MAB, true);
191
192 if (EmitDebugFrame)
193 MCDwarfFrameEmitter::Emit(*this, MAB, false);
194
195 if (EmitSFrame || (getContext().getTargetOptions() &&
196 getContext().getTargetOptions()->EmitSFrameUnwind))
198}
199
201 Assembler->registerSymbol(Sym);
202}
203
204void MCObjectStreamer::emitCFISections(bool EH, bool Debug, bool SFrame) {
206 EmitEHFrame = EH;
207 EmitDebugFrame = Debug;
208 EmitSFrame = SFrame;
209}
210
212 SMLoc Loc) {
214
216
217 // Avoid fixups when possible.
218 int64_t AbsValue;
219 if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
220 if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
222 Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
223 return;
224 }
225 emitIntValue(AbsValue, Size);
226 return;
227 }
231}
232
234 MCSymbol *Label = getContext().createTempSymbol("cfi");
235 emitLabel(Label);
236 return Label;
237}
238
239void MCObjectStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
240 // We need to create a local symbol to avoid relocations.
242 emitLabel(Frame.Begin);
243}
244
245void MCObjectStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
246 Frame.End = getContext().createTempSymbol();
247 emitLabel(Frame.End);
248}
249
251 MCStreamer::emitLabel(Symbol, Loc);
252 // If Symbol is a non-redefiniable variable, emitLabel has reported an error.
253 // Bail out.
254 if (Symbol->isVariable())
255 return;
256
257 getAssembler().registerSymbol(*Symbol);
258
259 // Set the fragment and offset. This function might be called by
260 // changeSection, when the section stack top hasn't been changed to the new
261 // section.
263 Symbol->setFragment(F);
264 Symbol->setOffset(F->getFixedSize());
265
267}
268
270 auto Assignments = pendingAssignments.find(Symbol);
271 if (Assignments != pendingAssignments.end()) {
272 for (const PendingAssignment &A : Assignments->second)
273 emitAssignment(A.Symbol, A.Value);
274
275 pendingAssignments.erase(Assignments);
276 }
277}
278
279// Emit a label at a previously emitted fragment/offset position. This must be
280// within the currently-active section.
283 assert(F.getParent() == getCurrentSectionOnly());
284 MCStreamer::emitLabel(Symbol, Loc);
285 getAssembler().registerSymbol(*Symbol);
286 Symbol->setFragment(&F);
287 Symbol->setOffset(Offset);
288}
289
291 int64_t IntValue;
292 if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
293 emitULEB128IntValue(IntValue);
294 return;
295 }
296 auto *F = getCurrentFragment();
297 F->makeLEB(false, Value);
298 newFragment();
299}
300
302 int64_t IntValue;
303 if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
304 emitSLEB128IntValue(IntValue);
305 return;
306 }
307 auto *F = getCurrentFragment();
308 F->makeLEB(true, Value);
309 newFragment();
310}
311
313 const MCSymbol *Target) {
314 reportFatalUsageError("this file format doesn't support weak aliases");
315}
316
318 assert(Section && "Cannot switch to a null section!");
320
321 // Register the section and create an initial fragment for subsection 0
322 // if `Subsection` is non-zero.
323 bool NewSec = getAssembler().registerSection(*Section);
324 MCFragment *F0 = nullptr;
325 if (NewSec && Subsection) {
326 changeSection(Section, 0);
327 F0 = CurFrag;
328 }
329
330 // To maintain connectivity between CurFrag and FragSpace when CurFrag is
331 // modified, allocate an empty fragment and append it to the fragment list.
332 // (Subsections[I].second.Tail is not connected to FragSpace.)
333 MCFragment *F;
334 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
335 auto End = reinterpret_cast<size_t>(getCurFragEnd());
336 F = reinterpret_cast<MCFragment *>(
337 alignToPowerOf2(End, alignof(MCFragment)));
338 FragSpace -= size_t(F) - End + sizeof(MCFragment);
339 } else {
340 F = allocFragSpace(0);
341 }
342 new (F) MCFragment();
343 F->setParent(Section);
344
345 auto &Subsections = Section->Subsections;
346 size_t I = 0, E = Subsections.size();
347 while (I != E && Subsections[I].first < Subsection)
348 ++I;
349 // If the subsection number is not in the sorted Subsections list, create a
350 // new fragment list.
351 if (I == E || Subsections[I].first != Subsection) {
352 Subsections.insert(Subsections.begin() + I,
353 {Subsection, MCSection::FragList{F, F}});
354 Section->CurFragList = &Subsections[I].second;
355 CurFrag = F;
356 } else {
357 Section->CurFragList = &Subsections[I].second;
358 CurFrag = Subsections[I].second.Tail;
359 // Ensure CurFrag is associated with FragSpace.
360 addFragment(F);
361 }
362
363 // Define the section symbol at subsection 0's initial fragment if required.
364 if (!NewSec)
365 return;
366 if (auto *Sym = Section->getBeginSymbol()) {
367 Sym->setFragment(Subsection ? F0 : CurFrag);
368 getAssembler().registerSymbol(*Sym);
369 }
370}
371
377
379 const MCExpr *Value) {
380 const MCSymbol *Target = &cast<MCSymbolRefExpr>(*Value).getSymbol();
381
382 // If the symbol already exists, emit the assignment. Otherwise, emit it
383 // later only if the symbol is also emitted.
384 if (Target->isRegistered())
385 emitAssignment(Symbol, Value);
386 else
387 pendingAssignments[Target].push_back({Symbol, Value});
388}
389
391 return Sec.hasInstructions();
392}
393
395 const MCSubtargetInfo &STI) {
397
399 Sec->setHasInstructions(true);
400
401 // Now that a machine instruction has been assembled into this section, make
402 // a line entry for any .loc directive that has been seen.
404
405 // If this instruction doesn't need relaxation, just emit it as data.
406 MCAssembler &Assembler = getAssembler();
407 MCAsmBackend &Backend = Assembler.getBackend();
408 if (!(Backend.mayNeedRelaxation(Inst.getOpcode(), Inst.getOperands(), STI) ||
409 Backend.allowEnhancedRelaxation())) {
410 emitInstToData(Inst, STI);
411 return;
412 }
413
414 // Otherwise, relax and emit it as data if RelaxAll is specified.
415 if (Assembler.getRelaxAll()) {
416 MCInst Relaxed = Inst;
417 while (Backend.mayNeedRelaxation(Relaxed.getOpcode(), Relaxed.getOperands(),
418 STI))
419 Backend.relaxInstruction(Relaxed, STI);
420 emitInstToData(Relaxed, STI);
421 return;
422 }
423
424 emitInstToFragment(Inst, STI);
425}
426
427void MCObjectStreamer::emitInstToData(const MCInst &Inst,
428 const MCSubtargetInfo &STI) {
430
431 // Append the instruction to the data fragment.
432 size_t CodeOffset = getCurFragSize();
433 SmallString<16> Content;
435 getAssembler().getEmitter().encodeInstruction(Inst, Content, Fixups, STI);
436 appendContents(Content);
437 if (CurFrag != F) {
438 F = CurFrag;
439 CodeOffset = 0;
440 }
441 F->setHasInstructions(STI);
442
443 if (Fixups.empty())
444 return;
445 bool MarkedLinkerRelaxable = false;
446 for (auto &Fixup : Fixups) {
447 Fixup.setOffset(Fixup.getOffset() + CodeOffset);
448 if (!Fixup.isLinkerRelaxable() || MarkedLinkerRelaxable)
449 continue;
450 MarkedLinkerRelaxable = true;
451 // Set the fragment's order within the subsection for use by
452 // MCAssembler::relaxAlign.
453 auto *Sec = F->getParent();
454 if (!Sec->isLinkerRelaxable())
455 Sec->setFirstLinkerRelaxable(F->getLayoutOrder());
456 // Do not add data after a linker-relaxable instruction. The difference
457 // between a new label and a label at or before the linker-relaxable
458 // instruction cannot be resolved at assemble-time.
459 F->setLinkerRelaxable();
460 newFragment();
461 }
462 F->appendFixups(Fixups);
463}
464
466 const MCSubtargetInfo &STI) {
467 auto *F = getCurrentFragment();
470 getAssembler().getEmitter().encodeInstruction(Inst, Data, Fixups, STI);
471
473 F->setHasInstructions(STI);
474
475 F->setVarContents(Data);
476 F->setInst(Inst);
477
478 bool MarkedLinkerRelaxable = false;
479 for (auto &Fixup : Fixups) {
480 if (!Fixup.isLinkerRelaxable() || MarkedLinkerRelaxable)
481 continue;
482 MarkedLinkerRelaxable = true;
483 auto *Sec = F->getParent();
484 if (!Sec->isLinkerRelaxable())
485 Sec->setFirstLinkerRelaxable(F->getLayoutOrder());
486 F->setLinkerRelaxable();
487 }
488 F->setVarFixups(Fixups);
489
490 newFragment();
491}
492
493void MCObjectStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
494 unsigned Column, unsigned Flags,
495 unsigned Isa,
496 unsigned Discriminator,
497 StringRef FileName,
498 StringRef Comment) {
499 // In case we see two .loc directives in a row, make sure the
500 // first one gets a line entry.
502
503 this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
504 Discriminator, FileName, Comment);
505}
506
508 const MCSymbol *B, SMLoc Loc) {
509 MCContext &Context = OS.getContext();
510 const MCExpr *ARef = MCSymbolRefExpr::create(A, Context);
511 const MCExpr *BRef = MCSymbolRefExpr::create(B, Context);
512 const MCExpr *AddrDelta =
513 MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context, Loc);
514 return AddrDelta;
515}
516
519 int64_t LineDelta, const MCSymbol *Label,
520 int PointerSize) {
521 // emit the sequence to set the address
522 OS.emitIntValue(dwarf::DW_LNS_extended_op, 1);
523 OS.emitULEB128IntValue(PointerSize + 1);
524 OS.emitIntValue(dwarf::DW_LNE_set_address, 1);
525 OS.emitSymbolValue(Label, PointerSize);
526
527 // emit the sequence for the LineDelta (from 1) and a zero address delta.
528 MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
529}
530
532 const MCSymbol *LastLabel,
533 const MCSymbol *Label,
534 unsigned PointerSize) {
535 if (!LastLabel) {
536 emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
537 Label, PointerSize);
538 return;
539 }
540
541 // If the two labels are within the same fragment, then the address-offset is
542 // already a fixed constant and is not relaxable. Emit the advance-line-addr
543 // data immediately to save time and memory.
544 if (auto OptAddrDelta = absoluteSymbolDiff(Label, LastLabel)) {
545 SmallString<16> Tmp;
546 MCDwarfLineAddr::encode(getContext(), Assembler->getDWARFLinetableParams(),
547 LineDelta, *OptAddrDelta, Tmp);
548 emitBytes(Tmp);
549 return;
550 }
551
552 auto *F = getCurrentFragment();
553 F->Kind = MCFragment::FT_Dwarf;
554 F->setDwarfAddrDelta(buildSymbolDiff(*this, Label, LastLabel, SMLoc()));
555 F->setDwarfLineDelta(LineDelta);
556 newFragment();
557}
558
560 MCSymbol *LastLabel,
561 MCSymbol *EndLabel) {
562 // Emit a DW_LNE_end_sequence into the line table. When EndLabel is null, it
563 // means we should emit the entry for the end of the section and therefore we
564 // use the section end label for the reference label. After having the
565 // appropriate reference label, we emit the address delta and use INT64_MAX as
566 // the line delta which is the signal that this is actually a
567 // DW_LNE_end_sequence.
568 if (!EndLabel)
569 EndLabel = endSection(Section);
570
571 // Switch back the dwarf line section, in case endSection had to switch the
572 // section.
573 MCContext &Ctx = getContext();
574 switchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
575
576 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
577 emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, EndLabel,
578 AsmInfo->getCodePointerSize());
579}
580
582 const MCSymbol *Label,
583 SMLoc Loc) {
584 auto *F = getCurrentFragment();
586 F->setDwarfAddrDelta(buildSymbolDiff(*this, Label, LastLabel, Loc));
587 newFragment();
588}
589
591 const MCSymbol *FREBegin,
592 MCFragment *FDEFrag,
593 SMLoc Loc) {
594 assert(FuncBase && "No function base address");
595 assert(FREBegin && "FRE doesn't describe a location");
596 auto *F = getCurrentFragment();
597 F->Kind = MCFragment::FT_SFrame;
598 F->setSFrameAddrDelta(buildSymbolDiff(*this, FREBegin, FuncBase, Loc));
599 F->setSFrameFDE(FDEFrag);
600 newFragment();
601}
602
604 unsigned Line, unsigned Column,
605 bool PrologueEnd, bool IsStmt,
606 StringRef FileName, SMLoc Loc) {
607 // Validate the directive.
608 if (!checkCVLocSection(FunctionId, FileNo, Loc))
609 return;
610
611 // Emit a label at the current position and record it in the CodeViewContext.
612 MCSymbol *LineSym = getContext().createTempSymbol();
613 emitLabel(LineSym);
615 FileNo, Line, Column, PrologueEnd,
616 IsStmt);
617}
618
620 const MCSymbol *Begin,
621 const MCSymbol *End) {
623 End);
624 this->MCStreamer::emitCVLinetableDirective(FunctionId, Begin, End);
625}
626
628 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
629 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
631 *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
632 FnEndSym);
634 PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
635}
636
638 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
639 StringRef FixedSizePortion) {
640 getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
641 // Attach labels that were pending before we created the defrange fragment to
642 // the beginning of the new fragment.
643 this->MCStreamer::emitCVDefRangeDirective(Ranges, FixedSizePortion);
644}
645
652
656
661
663 uint8_t FillLen,
664 unsigned MaxBytesToEmit) {
665 if (MaxBytesToEmit == 0)
666 MaxBytesToEmit = Alignment.value();
668 F->makeAlign(Alignment, Fill, FillLen, MaxBytesToEmit);
669 newFragment();
670
671 // Update the maximum alignment on the current section if necessary.
672 F->getParent()->ensureMinAlignment(Alignment);
673}
674
676 const MCSubtargetInfo *STI,
677 unsigned MaxBytesToEmit) {
678 auto *F = getCurrentFragment();
679 emitValueToAlignment(Alignment, 0, 1, MaxBytesToEmit);
680 F->u.align.EmitNops = true;
681 F->STI = STI;
682}
683
687
693
695 const MCExpr *Expr, SMLoc Loc) {
696 std::optional<MCFixupKind> MaybeKind =
697 Assembler->getBackend().getFixupKind(Name);
698 if (!MaybeKind) {
699 getContext().reportError(Loc, "unknown relocation name");
700 return;
701 }
702
703 MCFixupKind Kind = *MaybeKind;
704 if (Expr)
705 visitUsedExpr(*Expr);
706 else
707 Expr =
708 MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
709
710 auto *O = &Offset;
711 int64_t Val;
712 if (Offset.evaluateAsAbsolute(Val, nullptr)) {
713 auto *SecSym = getCurrentSectionOnly()->getBeginSymbol();
715 O, getContext(), Loc);
716 }
717 getAssembler().addRelocDirective({*O, Expr, Kind});
718}
719
720void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
721 SMLoc Loc) {
722 assert(getCurrentSectionOnly() && "need a section");
723 newSpecialFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc);
724}
725
726void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
727 int64_t Expr, SMLoc Loc) {
728 int64_t IntNumValues;
729 // Do additional checking now if we can resolve the value.
730 if (NumValues.evaluateAsAbsolute(IntNumValues, getAssembler())) {
731 if (IntNumValues < 0) {
734 "'.fill' directive with negative repeat count has no effect");
735 return;
736 }
737 // Emit now if we can for better errors.
738 int64_t NonZeroSize = Size > 4 ? 4 : Size;
739 Expr &= ~0ULL >> (64 - NonZeroSize * 8);
740 for (uint64_t i = 0, e = IntNumValues; i != e; ++i) {
741 emitIntValue(Expr, NonZeroSize);
742 if (NonZeroSize < Size)
743 emitIntValue(0, Size - NonZeroSize);
744 }
745 return;
746 }
747
748 // Otherwise emit as fragment.
749 assert(getCurrentSectionOnly() && "need a section");
751}
752
753void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,
754 SMLoc Loc, const MCSubtargetInfo &STI) {
755 assert(getCurrentSectionOnly() && "need a section");
756 newSpecialFragment<MCNopsFragment>(NumBytes, ControlledNopLength, Loc, STI);
757}
758
760 MCAssembler &Asm = getAssembler();
761 Asm.getWriter().addFileName(Filename);
762}
763
765 StringRef CompilerVersion,
766 StringRef TimeStamp,
767 StringRef Description) {
769 W.addFileName(Filename);
770 if (CompilerVersion.size())
771 W.setCompilerVersion(CompilerVersion);
772 // TODO: add TimeStamp and Description to .file symbol table entry
773 // with the integrated assembler.
774}
775
779
783
786
787 // If we are generating dwarf for assembly source files dump out the sections.
788 if (getContext().getGenDwarfForAssembly())
790
791 // Dump out the dwarf file & directory tables and line tables.
792 MCDwarfLineTable::emit(this, getAssembler().getDWARFLinetableParams());
793
794 // Emit pseudo probes for the current module.
796
798}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
dxil DXContainer Global Emitter
static const MCExpr * buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, const MCSymbol *B, SMLoc Loc)
constexpr size_t FragBlockSize
static void emitDwarfSetLineAddr(MCObjectStreamer &OS, MCDwarfLineTableParams Params, int64_t LineDelta, const MCSymbol *Label, int PointerSize)
static std::optional< uint64_t > absoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo)
constexpr size_t NewFragHeadroom
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static constexpr StringLiteral Filename
PowerPC TLS Dynamic Call Fixup
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
void emitLineTableForFunction(MCObjectStreamer &OS, unsigned FuncId, const MCSymbol *FuncBegin, const MCSymbol *FuncEnd)
Emits a line table substream.
void emitFileChecksums(MCObjectStreamer &OS)
Emits the file checksum substream.
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.
void emitDefRange(MCObjectStreamer &OS, ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion)
void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo)
Emits the offset into the checksum table of the given file number.
void emitInlineLineTableForFunction(MCObjectStreamer &OS, unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
void emitStringTable(MCObjectStreamer &OS)
Emits the string table substream.
Generic interface to target specific assembler backends.
bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
virtual void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const
Relax the instruction in the given fragment to the next wider instruction.
virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:64
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition MCAsmInfo.h:446
LLVM_ABI void Finish()
Finish - Do final processing and write the object to the output stream.
MCObjectWriter & getWriter() const
LLVM_ABI void addRelocDirective(RelocDirective RD)
MCCodeEmitter & getEmitter() const
MCAsmBackend & getBackend() const
LLVM_ABI bool registerSection(MCSection &Section)
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:343
static LLVM_ABI const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:201
@ Sub
Subtraction.
Definition MCExpr.h:324
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI void RemapDebugPaths()
void clearDwarfLocSeen()
Definition MCContext.h:770
LLVM_ABI CodeViewContext & getCVContext()
const SourceMgr * getSourceManager() const
Definition MCContext.h:401
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
static LLVM_ABI void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH)
Definition MCDwarf.cpp:1905
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
static LLVM_ABI void make(MCStreamer *MCOS, MCSection *Section)
Definition MCDwarf.cpp:91
static LLVM_ABI void emit(MCStreamer *MCOS, MCDwarfLineTableParams Params)
Definition MCDwarf.cpp:308
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Definition MCFixup.h:110
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:86
FragmentType getKind() const
Definition MCSection.h:165
static LLVM_ABI void Emit(MCStreamer *MCOS)
Definition MCDwarf.cpp:1194
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getOpcode() const
Definition MCInst.h:202
ArrayRef< MCOperand > getOperands() const
Definition MCInst.h:214
Streaming object file generation interface.
void reset() override
state management
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
FT * newSpecialFragment(Args &&...args)
void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol.
void emitCFISections(bool EH, bool Debug, bool SFrame) override
void emitULEB128Value(const MCExpr *Value) override
void emitSLEB128Value(const MCExpr *Value) override
void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc, const MCSubtargetInfo &STI) override
void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) override
This implements the CodeView '.cv_inline_linetable' assembler directive.
void emitCVStringTableDirective() override
This implements the CodeView '.cv_stringtable' assembler directive.
MCAssembler & getAssembler()
void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize) override
If targets does not support representing debug line section by .loc/.file directives in assembly outp...
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override
Emit an weak reference from Alias to Symbol.
void appendContents(ArrayRef< char > Contents)
void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt, StringRef FileName, SMLoc Loc) override
This implements the CodeView '.cv_loc' assembler directive.
void emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc={}) override
Record a relocation described by the .reloc directive.
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitPendingAssignments(MCSymbol *Symbol)
Emits pending conditional assignments that depend on Symbol being emitted.
void addFixup(const MCExpr *Value, MCFixupKind Kind)
void emitFileDirective(StringRef Filename) override
Switch to a new logical file.
void emitSFrameCalculateFuncOffset(const MCSymbol *FunCabsel, const MCSymbol *FREBegin, MCFragment *FDEFrag, SMLoc Loc)
void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName, StringRef Comment={}) override
This implements the DWARF2 '.loc fileno lineno ...' assembler directive.
MCSymbol * emitCFILabel() override
When emitting an object file, create and emit a real label.
void emitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion) override
This implements the CodeView '.cv_def_range' assembler directive.
MCAssembler * getAssemblerPtr() override
void emitAddrsigSym(const MCSymbol *Sym) override
void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel, MCSymbol *EndLabel=nullptr) override
Emit the debug line end entry.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void visitUsedSymbol(const MCSymbol &Sym) override
void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F, uint64_t Offset)
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
MCObjectStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void emitPrefAlign(Align Alignment) override
void finishImpl() override
Streamer specific finalization.
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) override
Emit the absolute difference between two symbols encoded with ULEB128.
MCFragment * allocFragSpace(size_t Headroom)
void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, const MCSymbol *Label, SMLoc Loc)
bool mayHaveInstructions(MCSection &Sec) const override
void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
~MCObjectStreamer() override
void ensureHeadroom(size_t Headroom)
void emitCVFileChecksumOffsetDirective(unsigned FileNo) override
This implements the CodeView '.cv_filechecksumoffset' assembler directive.
void emitConditionalAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol, but only if Value is also emitted.
void emitCVFileChecksumsDirective() override
This implements the CodeView '.cv_filechecksums' assembler directive.
void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin, const MCSymbol *End) override
This implements the CodeView '.cv_linetable' assembler directive.
void emitValueToOffset(const MCExpr *Offset, unsigned char Value, SMLoc Loc) override
Emit some number of copies of Value until the byte offset Offset is reached.
uint8_t * getCurFragEnd() const
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) override
Emit the absolute difference between two symbols if possible.
Defines the object file and target independent interfaces used by the assembler backend to write nati...
void emitAddrsigSection()
Tell the object writer to emit an address-significance table during writeObject().
void addAddrsigSymbol(const MCSymbol *Sym)
Record the given symbol in the address-significance table to be written diring writeObject().
static LLVM_ABI void emit(MCObjectStreamer *MCOS)
static void emit(MCObjectStreamer &Streamer)
Definition MCSFrame.cpp:587
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:517
bool hasInstructions() const
Definition MCSection.h:626
void setHasInstructions(bool Value)
Definition MCSection.h:627
void ensurePreferredAlignment(Align PrefAlign)
Definition MCSection.h:616
MCSymbol * getBeginSymbol()
Definition MCSection.h:590
virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Emit an assignment of Value to Symbol.
virtual void emitCFISections(bool EH, bool Debug, bool SFrame)
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
MCFragment * getCurrentFragment() const
Definition MCStreamer.h:432
virtual void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo)
Emit the absolute difference between two symbols encoded with ULEB128.
bool getUseAssemblerInfoForParsing()
Definition MCStreamer.h:322
MCContext & getContext() const
Definition MCStreamer.h:314
void emitSymbolValue(const MCSymbol *Sym, unsigned Size, bool IsSectionRelative=false)
Special case of EmitValue that avoids the client having to pass in a MCExpr for MCSymbols.
bool checkCVLocSection(unsigned FuncId, unsigned FileNo, SMLoc Loc)
Returns true if the .cv_loc directive is in the right section.
virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size)
Emit the absolute difference between two symbols.
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
void setAllowAutoPadding(bool v)
Definition MCStreamer.h:328
virtual void reset()
State management.
virtual void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName, StringRef Comment={})
This implements the DWARF2 '.loc fileno lineno ...' assembler directive.
virtual void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *FnStart, const MCSymbol *FnEnd)
This implements the CodeView '.cv_linetable' assembler directive.
MCStreamer(MCContext &Ctx)
MCFragment * CurFrag
Definition MCStreamer.h:267
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
unsigned getNumFrameInfos()
size_t getCurFragSize() const
Definition MCStreamer.h:441
unsigned emitULEB128IntValue(uint64_t Value, unsigned PadTo=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
MCSymbol * endSection(MCSection *Section)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
virtual void emitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion)
This implements the CodeView '.cv_def_range' assembler directive.
MCSection * getCurrentSectionOnly() const
Definition MCStreamer.h:421
virtual void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Emit the expression Value into the output as a native integer of the given Size bytes.
void addFragment(MCFragment *F)
unsigned emitSLEB128IntValue(int64_t Value)
Special case of EmitSLEB128Value that avoids the client having to pass in a MCExpr for constant integ...
virtual void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
This implements the CodeView '.cv_inline_linetable' assembler directive.
void visitUsedExpr(const MCExpr &Expr)
Generic base class for all target subtargets.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
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 is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
This class represents a function that is read from a sample profile.
Definition FunctionId.h:36
#define INT64_MAX
Definition DataTypes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1757
@ Debug
Register 'use' is for debugging purpose.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition MathExtras.h:493
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1915
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:248
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77