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"
21#include "llvm/MC/MCSFrame.h"
22#include "llvm/MC/MCSection.h"
23#include "llvm/MC/MCSymbol.h"
26using namespace llvm;
27
29 std::unique_ptr<MCAsmBackend> TAB,
30 std::unique_ptr<MCObjectWriter> OW,
31 std::unique_ptr<MCCodeEmitter> Emitter)
32 : MCStreamer(Context),
33 Assembler(std::make_unique<MCAssembler>(
34 Context, std::move(TAB), std::move(Emitter), std::move(OW))),
35 EmitEHFrame(true), EmitDebugFrame(false), EmitSFrame(false) {
36 assert(Assembler->getBackendPtr() && Assembler->getEmitterPtr());
37 IsObj = true;
38 setAllowAutoPadding(Assembler->getBackend().allowAutoPadding());
39 if (Context.getTargetOptions().MCRelaxAll)
40 Assembler->setRelaxAll(true);
41}
42
44
47 return Assembler.get();
48 return nullptr;
49}
50
51constexpr size_t FragBlockSize = 16384;
52// Ensure the new fragment can at least store a few bytes.
53constexpr size_t NewFragHeadroom = 8;
54
55static_assert(NewFragHeadroom >= alignof(MCFragment));
56static_assert(FragBlockSize >= sizeof(MCFragment) + NewFragHeadroom);
57
59 auto Size = std::max(FragBlockSize, sizeof(MCFragment) + Headroom);
60 FragSpace = Size - sizeof(MCFragment);
61 auto Block = std::unique_ptr<uint8_t[]>(new uint8_t[Size]);
62 auto *F = reinterpret_cast<MCFragment *>(Block.get());
63 FragStorage.push_back(std::move(Block));
64 return F;
65}
66
69 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
70 auto End = reinterpret_cast<size_t>(getCurFragEnd());
71 F = reinterpret_cast<MCFragment *>(
72 alignToPowerOf2(End, alignof(MCFragment)));
73 FragSpace -= size_t(F) - End + sizeof(MCFragment);
74 } else {
75 F = allocFragSpace(0);
76 }
77 new (F) MCFragment();
79}
80
81void MCObjectStreamer::ensureHeadroom(size_t Headroom) {
82 if (Headroom <= FragSpace)
83 return;
84 auto *F = allocFragSpace(Headroom);
85 new (F) MCFragment();
87}
88
89void MCObjectStreamer::addSpecialFragment(MCFragment *Frag) {
91 "Frag should have a variable-size tail");
92 // Frag is not connected to FragSpace. Before modifying CurFrag with
93 // addFragment(Frag), allocate an empty fragment to maintain FragSpace
94 // connectivity, potentially reusing CurFrag's associated space.
96 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
97 auto End = reinterpret_cast<size_t>(getCurFragEnd());
98 F = reinterpret_cast<MCFragment *>(
99 alignToPowerOf2(End, alignof(MCFragment)));
100 FragSpace -= size_t(F) - End + sizeof(MCFragment);
101 } else {
102 F = allocFragSpace(0);
103 }
104 new (F) MCFragment();
105
106 addFragment(Frag);
107 addFragment(F);
108}
109
111 ensureHeadroom(Contents.size());
112 assert(FragSpace >= Contents.size());
113 // As this is performance-sensitive code, explicitly use std::memcpy.
114 // Optimization of std::copy to memmove is unreliable.
115 if (!Contents.empty())
116 std::memcpy(getCurFragEnd(), Contents.begin(), Contents.size());
117 CurFrag->FixedSize += Contents.size();
118 FragSpace -= Contents.size();
119}
120
122 ensureHeadroom(Num);
124 llvm::fill(Data, Elt);
125 CurFrag->FixedSize += Num;
126 FragSpace -= Num;
127}
128
132
133// As a compile-time optimization, avoid allocating and evaluating an MCExpr
134// tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment's fixed
135// part.
136static std::optional<uint64_t> absoluteSymbolDiff(const MCSymbol *Hi,
137 const MCSymbol *Lo) {
138 assert(Hi && Lo);
139 if (Lo == Hi)
140 return 0;
141 if (Hi->isVariable() || Lo->isVariable())
142 return std::nullopt;
143 auto *LoF = Lo->getFragment();
144 if (!LoF || Hi->getFragment() != LoF || LoF->isLinkerRelaxable())
145 return std::nullopt;
146 // If either symbol resides in the variable part, bail out.
147 auto Fixed = LoF->getFixedSize();
148 if (Lo->getOffset() > Fixed || Hi->getOffset() > Fixed)
149 return std::nullopt;
150
151 return Hi->getOffset() - Lo->getOffset();
152}
153
155 const MCSymbol *Lo,
156 unsigned Size) {
157 if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo))
158 emitIntValue(*Diff, Size);
159 else
161}
162
164 const MCSymbol *Lo) {
165 if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo))
166 emitULEB128IntValue(*Diff);
167 else
169}
170
172 if (Assembler) {
173 Assembler->reset();
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 auto &Backend = getAssembler().getBackend();
186 for (auto &FI : DwarfFrameInfos)
187 FI.CompactUnwindEncoding =
189}
190
192 if (!getNumFrameInfos())
193 return;
194
195 if (EmitEHFrame)
196 MCDwarfFrameEmitter::emit(*this, true);
197 if (EmitDebugFrame)
198 MCDwarfFrameEmitter::emit(*this, false);
199
200 if (EmitSFrame || getContext().getTargetOptions().EmitSFrameUnwind)
202}
203
205 Assembler->registerSymbol(Sym);
206}
207
208void MCObjectStreamer::emitCFISections(bool EH, bool Debug, bool SFrame) {
210 EmitEHFrame = EH;
211 EmitDebugFrame = Debug;
212 EmitSFrame = SFrame;
213}
214
216 SMLoc Loc) {
218
220
221 // Avoid fixups when possible.
222 int64_t AbsValue;
223 if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
224 if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
226 Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
227 return;
228 }
229 emitIntValue(AbsValue, Size);
230 return;
231 }
235}
236
238 MCSymbol *Label = getContext().createTempSymbol("cfi");
239 emitLabel(Label);
240 return Label;
241}
242
243void MCObjectStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
244 // We need to create a local symbol to avoid relocations.
246 emitLabel(Frame.Begin);
247}
248
249void MCObjectStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
250 Frame.End = getContext().createTempSymbol();
251 emitLabel(Frame.End);
252}
253
255 MCStreamer::emitLabel(Symbol, Loc);
256 // If Symbol is a non-redefiniable variable, emitLabel has reported an error.
257 // Bail out.
258 if (Symbol->isVariable())
259 return;
260
261 getAssembler().registerSymbol(*Symbol);
262
263 // Set the fragment and offset. This function might be called by
264 // changeSection, when the section stack top hasn't been changed to the new
265 // section.
267 Symbol->setFragment(F);
268 Symbol->setOffset(F->getFixedSize());
269
271}
272
274 auto Assignments = pendingAssignments.find(Symbol);
275 if (Assignments == pendingAssignments.end())
276 return;
277
278 // emitAssignment can recursively re-enter emitPendingAssignments for
279 // other symbols, so move the list out and erase before iterating.
280 SmallVector<PendingAssignment, 1> Pending = std::move(Assignments->second);
281 pendingAssignments.erase(Assignments);
282 for (const PendingAssignment &A : Pending)
283 emitAssignment(A.Symbol, A.Value);
284}
285
286// Emit a label at a previously emitted fragment/offset position. This must be
287// within the currently-active section.
290 assert(F.getParent() == getCurrentSectionOnly());
291 MCStreamer::emitLabel(Symbol, Loc);
292 getAssembler().registerSymbol(*Symbol);
293 Symbol->setFragment(&F);
294 Symbol->setOffset(Offset);
295}
296
298 int64_t IntValue;
299 if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
300 emitULEB128IntValue(IntValue);
301 return;
302 }
303 auto *F = getCurrentFragment();
304 F->makeLEB(false, Value);
305 newFragment();
306}
307
309 int64_t IntValue;
310 if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
311 emitSLEB128IntValue(IntValue);
312 return;
313 }
314 auto *F = getCurrentFragment();
315 F->makeLEB(true, Value);
316 newFragment();
317}
318
320 const MCSymbol *Target) {
321 reportFatalUsageError("this file format doesn't support weak aliases");
322}
323
325 assert(Section && "Cannot switch to a null section!");
327
328 // Register the section and create an initial fragment for subsection 0
329 // if `Subsection` is non-zero.
330 bool NewSec = getAssembler().registerSection(*Section);
331 MCFragment *F0 = nullptr;
332 if (NewSec && Subsection) {
333 changeSection(Section, 0);
334 F0 = CurFrag;
335 }
336
337 // To maintain connectivity between CurFrag and FragSpace when CurFrag is
338 // modified, allocate an empty fragment and append it to the fragment list.
339 // (Subsections[I].second.Tail is not connected to FragSpace.)
340 MCFragment *F;
341 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
342 auto End = reinterpret_cast<size_t>(getCurFragEnd());
343 F = reinterpret_cast<MCFragment *>(
344 alignToPowerOf2(End, alignof(MCFragment)));
345 FragSpace -= size_t(F) - End + sizeof(MCFragment);
346 } else {
347 F = allocFragSpace(0);
348 }
349 new (F) MCFragment();
350 F->setParent(Section);
351
352 auto &Subsections = Section->Subsections;
353 size_t I = 0, E = Subsections.size();
354 while (I != E && Subsections[I].first < Subsection)
355 ++I;
356 // If the subsection number is not in the sorted Subsections list, create a
357 // new fragment list.
358 if (I == E || Subsections[I].first != Subsection) {
359 Subsections.insert(Subsections.begin() + I,
360 {Subsection, MCSection::FragList{F, F}});
361 Section->CurFragList = &Subsections[I].second;
362 CurFrag = F;
363 } else {
364 Section->CurFragList = &Subsections[I].second;
365 CurFrag = Subsections[I].second.Tail;
366 // Ensure CurFrag is associated with FragSpace.
367 addFragment(F);
368 }
369
370 // Define the section symbol at subsection 0's initial fragment if required.
371 if (!NewSec)
372 return;
373 if (auto *Sym = Section->getBeginSymbol()) {
374 Sym->setFragment(Subsection ? F0 : CurFrag);
375 getAssembler().registerSymbol(*Sym);
376 }
377}
378
384
386 const MCExpr *Value) {
387 const MCSymbol *Target = &cast<MCSymbolRefExpr>(*Value).getSymbol();
388
389 // If the symbol already exists, emit the assignment. Otherwise, emit it
390 // later only if the symbol is also emitted.
391 if (Target->isRegistered())
392 emitAssignment(Symbol, Value);
393 else
394 pendingAssignments[Target].push_back({Symbol, Value});
395}
396
398 return Sec.hasInstructions();
399}
400
402 const MCSubtargetInfo &STI) {
403 if (LFIRewriter && LFIRewriter->rewriteInst(Inst, *this, STI))
404 return;
405
407
409 Sec->setHasInstructions(true);
410
411 // Now that a machine instruction has been assembled into this section, make
412 // a line entry for any .loc directive that has been seen.
414
415 // If this instruction doesn't need relaxation, just emit it as data.
416 MCAssembler &Assembler = getAssembler();
417 MCAsmBackend &Backend = Assembler.getBackend();
418 if (!(Backend.mayNeedRelaxation(Inst.getOpcode(), Inst.getOperands(), STI) ||
419 Backend.allowEnhancedRelaxation())) {
420 emitInstToData(Inst, STI);
421 return;
422 }
423
424 // Otherwise, relax and emit it as data if RelaxAll is specified.
425 if (Assembler.getRelaxAll()) {
426 MCInst Relaxed = Inst;
427 while (Backend.mayNeedRelaxation(Relaxed.getOpcode(), Relaxed.getOperands(),
428 STI))
429 Backend.relaxInstruction(Relaxed, STI);
430 emitInstToData(Relaxed, STI);
431 return;
432 }
433
434 emitInstToFragment(Inst, STI);
435}
436
437void MCObjectStreamer::emitInstToData(const MCInst &Inst,
438 const MCSubtargetInfo &STI) {
440
441 // Append the instruction to the data fragment.
442 size_t CodeOffset = getCurFragSize();
443 SmallString<16> Content;
445 getAssembler().getEmitter().encodeInstruction(Inst, Content, Fixups, STI);
446 appendContents(Content);
447 if (CurFrag != F) {
448 F = CurFrag;
449 CodeOffset = 0;
450 }
451 F->setHasInstructions(STI);
452
453 if (Fixups.empty())
454 return;
455 bool MarkedLinkerRelaxable = false;
456 for (auto &Fixup : Fixups) {
457 Fixup.setOffset(Fixup.getOffset() + CodeOffset);
458 if (!Fixup.isLinkerRelaxable() || MarkedLinkerRelaxable)
459 continue;
460 MarkedLinkerRelaxable = true;
461 // Set the fragment's order within the subsection for use by
462 // MCAssembler::relaxAlign.
463 auto *Sec = F->getParent();
464 if (!Sec->isLinkerRelaxable())
465 Sec->setFirstLinkerRelaxable(F->getLayoutOrder());
466 // Do not add data after a linker-relaxable instruction. The difference
467 // between a new label and a label at or before the linker-relaxable
468 // instruction cannot be resolved at assemble-time.
469 F->setLinkerRelaxable();
470 newFragment();
471 }
472 F->appendFixups(Fixups);
473}
474
476 const MCSubtargetInfo &STI) {
477 auto *F = getCurrentFragment();
480 getAssembler().getEmitter().encodeInstruction(Inst, Data, Fixups, STI);
481
483 F->setHasInstructions(STI);
484
485 F->setVarContents(Data);
486 F->setInst(Inst);
487
488 bool MarkedLinkerRelaxable = false;
489 for (auto &Fixup : Fixups) {
490 if (!Fixup.isLinkerRelaxable() || MarkedLinkerRelaxable)
491 continue;
492 MarkedLinkerRelaxable = true;
493 auto *Sec = F->getParent();
494 if (!Sec->isLinkerRelaxable())
495 Sec->setFirstLinkerRelaxable(F->getLayoutOrder());
496 F->setLinkerRelaxable();
497 }
498 F->setVarFixups(Fixups);
499
500 newFragment();
501}
502
503void MCObjectStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
504 unsigned Column, unsigned Flags,
505 unsigned Isa,
506 unsigned Discriminator,
507 StringRef FileName,
508 StringRef Comment) {
509 // In case we see two .loc directives in a row, make sure the
510 // first one gets a line entry.
512
513 this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
514 Discriminator, FileName, Comment);
515}
516
518 const MCSymbol *B, SMLoc Loc) {
519 MCContext &Context = OS.getContext();
520 const MCExpr *ARef = MCSymbolRefExpr::create(A, Context);
521 const MCExpr *BRef = MCSymbolRefExpr::create(B, Context);
522 const MCExpr *AddrDelta =
523 MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context, Loc);
524 return AddrDelta;
525}
526
529 int64_t LineDelta, const MCSymbol *Label,
530 int PointerSize) {
531 // emit the sequence to set the address
532 OS.emitIntValue(dwarf::DW_LNS_extended_op, 1);
533 OS.emitULEB128IntValue(PointerSize + 1);
534 OS.emitIntValue(dwarf::DW_LNE_set_address, 1);
535 OS.emitSymbolValue(Label, PointerSize);
536
537 // emit the sequence for the LineDelta (from 1) and a zero address delta.
538 MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
539}
540
542 const MCSymbol *LastLabel,
543 const MCSymbol *Label,
544 unsigned PointerSize) {
545 if (!LastLabel) {
546 emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
547 Label, PointerSize);
548 return;
549 }
550
551 // If the two labels are within the same fragment, then the address-offset is
552 // already a fixed constant and is not relaxable. Emit the advance-line-addr
553 // data immediately to save time and memory.
554 if (auto OptAddrDelta = absoluteSymbolDiff(Label, LastLabel)) {
555 SmallString<16> Tmp;
556 MCDwarfLineAddr::encode(getContext(), Assembler->getDWARFLinetableParams(),
557 LineDelta, *OptAddrDelta, Tmp);
558 emitBytes(Tmp);
559 return;
560 }
561
562 auto *F = getCurrentFragment();
563 F->Kind = MCFragment::FT_Dwarf;
564 F->setDwarfAddrDelta(buildSymbolDiff(*this, Label, LastLabel, SMLoc()));
565 F->setDwarfLineDelta(LineDelta);
566 newFragment();
567}
568
570 MCSymbol *LastLabel,
571 MCSymbol *EndLabel) {
572 // Emit a DW_LNE_end_sequence into the line table. When EndLabel is null, it
573 // means we should emit the entry for the end of the section and therefore we
574 // use the section end label for the reference label. After having the
575 // appropriate reference label, we emit the address delta and use INT64_MAX as
576 // the line delta which is the signal that this is actually a
577 // DW_LNE_end_sequence.
578 if (!EndLabel)
579 EndLabel = endSection(Section);
580
581 // Switch back the dwarf line section, in case endSection had to switch the
582 // section.
583 MCContext &Ctx = getContext();
584 switchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
585
586 const MCAsmInfo &AsmInfo = Ctx.getAsmInfo();
587 emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, EndLabel,
588 AsmInfo.getCodePointerSize());
589}
590
592 const MCSymbol *Label,
593 SMLoc Loc) {
594 auto *F = getCurrentFragment();
596 F->setDwarfAddrDelta(buildSymbolDiff(*this, Label, LastLabel, Loc));
597 newFragment();
598}
599
601 const MCSymbol *FREBegin,
602 MCFragment *FDEFrag,
603 SMLoc Loc) {
604 assert(FuncBase && "No function base address");
605 assert(FREBegin && "FRE doesn't describe a location");
606 auto *F = getCurrentFragment();
607 F->Kind = MCFragment::FT_SFrame;
608 F->setSFrameAddrDelta(buildSymbolDiff(*this, FREBegin, FuncBase, Loc));
609 F->setSFrameFDE(FDEFrag);
610 newFragment();
611}
612
614 unsigned Line, unsigned Column,
615 bool PrologueEnd, bool IsStmt,
616 StringRef FileName, SMLoc Loc) {
617 // Validate the directive.
618 if (!checkCVLocSection(FunctionId, FileNo, Loc))
619 return;
620
621 // Emit a label at the current position and record it in the CodeViewContext.
622 MCSymbol *LineSym = getContext().createTempSymbol();
623 emitLabel(LineSym);
625 FileNo, Line, Column, PrologueEnd,
626 IsStmt);
627}
628
630 const MCSymbol *Begin,
631 const MCSymbol *End) {
633 End);
634 this->MCStreamer::emitCVLinetableDirective(FunctionId, Begin, End);
635}
636
638 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
639 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
641 *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
642 FnEndSym);
644 PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
645}
646
648 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
649 StringRef FixedSizePortion) {
650 getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
651 // Attach labels that were pending before we created the defrange fragment to
652 // the beginning of the new fragment.
653 this->MCStreamer::emitCVDefRangeDirective(Ranges, FixedSizePortion);
654}
655
662
666
671
673 uint8_t FillLen,
674 unsigned MaxBytesToEmit) {
675 if (MaxBytesToEmit == 0)
676 MaxBytesToEmit = Alignment.value();
678 F->makeAlign(Alignment, Fill, FillLen, MaxBytesToEmit);
679 newFragment();
680
681 // Update the maximum alignment on the current section if necessary.
682 F->getParent()->ensureMinAlignment(Alignment);
683}
684
686 const MCSubtargetInfo *STI,
687 unsigned MaxBytesToEmit) {
688 auto *F = getCurrentFragment();
689 emitValueToAlignment(Alignment, 0, 1, MaxBytesToEmit);
690 F->u.align.EmitNops = true;
691 F->STI = STI;
692}
693
695 bool EmitNops, uint8_t Fill,
696 const MCSubtargetInfo &STI) {
697 auto *F = getCurrentFragment();
698 F->makePrefAlign(Alignment, End, EmitNops, Fill);
699 if (EmitNops)
700 F->STI = &STI;
701 newFragment();
702}
703
709
711 const MCExpr *Expr, SMLoc Loc) {
712 std::optional<MCFixupKind> MaybeKind =
713 Assembler->getBackend().getFixupKind(Name);
714 if (!MaybeKind) {
715 getContext().reportError(Loc, "unknown relocation name");
716 return;
717 }
718
719 MCFixupKind Kind = *MaybeKind;
720 if (Expr)
721 visitUsedExpr(*Expr);
722 else
723 Expr =
724 MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
725
726 auto *O = &Offset;
727 int64_t Val;
728 if (Offset.evaluateAsAbsolute(Val, nullptr)) {
729 auto *SecSym = getCurrentSectionOnly()->getBeginSymbol();
731 O, getContext(), Loc);
732 }
733 getAssembler().addRelocDirective({*O, Expr, Kind});
734}
735
736void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
737 SMLoc Loc) {
738 assert(getCurrentSectionOnly() && "need a section");
739 newSpecialFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc);
740}
741
742void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
743 int64_t Expr, SMLoc Loc) {
744 int64_t IntNumValues;
745 // Do additional checking now if we can resolve the value.
746 if (NumValues.evaluateAsAbsolute(IntNumValues, getAssembler()) &&
747 IntNumValues < 0) {
750 "'.fill' directive with negative repeat count has no effect");
751 return;
752 }
753
754 assert(getCurrentSectionOnly() && "need a section");
756}
757
758void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,
759 SMLoc Loc, const MCSubtargetInfo &STI) {
760 assert(getCurrentSectionOnly() && "need a section");
761 newSpecialFragment<MCNopsFragment>(NumBytes, ControlledNopLength, Loc, STI);
762}
763
765 MCAssembler &Asm = getAssembler();
766 Asm.getWriter().addFileName(Filename);
767}
768
770 StringRef CompilerVersion,
771 StringRef TimeStamp,
772 StringRef Description) {
774 W.addFileName(Filename);
775 if (CompilerVersion.size())
776 W.setCompilerVersion(CompilerVersion);
777 // TODO: add TimeStamp and Description to .file symbol table entry
778 // with the integrated assembler.
779}
780
784
788
791
792 // If we are generating dwarf for assembly source files dump out the sections.
793 if (getContext().getGenDwarfForAssembly())
795
796 // Dump out the dwarf file & directory tables and line tables.
797 MCDwarfLineTable::emit(this, getAssembler().getDWARFLinetableParams());
798
799 // Emit pseudo probes for the current module.
801
803}
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
This file declares the MCLFIRewriter class, an abstract class that encapsulates the rewriting logic f...
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
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
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.
virtual uint64_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI, const MCContext *Ctxt) const
Generate the compact unwind encoding for the CFI instructions.
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:66
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition MCAsmInfo.h:455
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:767
LLVM_ABI CodeViewContext & getCVContext()
const SourceMgr * getSourceManager() const
Definition MCContext.h:398
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
static LLVM_ABI void emit(MCObjectStreamer &streamer, bool isEH)
Definition MCDwarf.cpp:2110
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:179
static LLVM_ABI void Emit(MCStreamer *MCOS)
Definition MCDwarf.cpp:1205
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 emitPrefAlign(Align Alignment, const MCSymbol &End, bool EmitNops, uint8_t Fill, const MCSubtargetInfo &STI) 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 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:573
bool hasInstructions() const
Definition MCSection.h:669
void setHasInstructions(bool Value)
Definition MCSection.h:670
MCSymbol * getBeginSymbol()
Definition MCSection.h:646
virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Emit an assignment of Value to Symbol.
std::unique_ptr< MCLFIRewriter > LFIRewriter
Definition MCStreamer.h:296
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:439
SmallVector< MCDwarfFrameInfo, 0 > DwarfFrameInfos
Definition MCStreamer.h:270
virtual void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo)
Emit the absolute difference between two symbols encoded with ULEB128.
bool getUseAssemblerInfoForParsing()
Definition MCStreamer.h:331
MCContext & getContext() const
Definition MCStreamer.h:323
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:337
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:268
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:448
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:428
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
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
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.
@ Offset
Definition DWP.cpp:558
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:1758
@ 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:221
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:1916
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:874
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