Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / MC / MCXCOFFStreamer.cpp
blob8585416cd08185f7e8cc17e53c809b6b18bb09f6
1 //===- lib/MC/MCXCOFFStreamer.cpp - XCOFF Object Output -------------------===//
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 assembles .s files and emits XCOFF .o object files.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/MC/MCXCOFFStreamer.h"
14 #include "llvm/BinaryFormat/XCOFF.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSectionXCOFF.h"
21 #include "llvm/MC/MCSymbolXCOFF.h"
22 #include "llvm/MC/TargetRegistry.h"
23 #include "llvm/Support/Casting.h"
25 using namespace llvm;
27 MCXCOFFStreamer::MCXCOFFStreamer(MCContext &Context,
28 std::unique_ptr<MCAsmBackend> MAB,
29 std::unique_ptr<MCObjectWriter> OW,
30 std::unique_ptr<MCCodeEmitter> Emitter)
31 : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
32 std::move(Emitter)) {}
34 bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
35 MCSymbolAttr Attribute) {
36 auto *Symbol = cast<MCSymbolXCOFF>(Sym);
37 getAssembler().registerSymbol(*Symbol);
39 switch (Attribute) {
40 // XCOFF doesn't support the cold feature.
41 case MCSA_Cold:
42 return false;
44 case MCSA_Global:
45 case MCSA_Extern:
46 Symbol->setStorageClass(XCOFF::C_EXT);
47 Symbol->setExternal(true);
48 break;
49 case MCSA_LGlobal:
50 Symbol->setStorageClass(XCOFF::C_HIDEXT);
51 Symbol->setExternal(true);
52 break;
53 case llvm::MCSA_Weak:
54 Symbol->setStorageClass(XCOFF::C_WEAKEXT);
55 Symbol->setExternal(true);
56 break;
57 case llvm::MCSA_Hidden:
58 Symbol->setVisibilityType(XCOFF::SYM_V_HIDDEN);
59 break;
60 case llvm::MCSA_Protected:
61 Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
62 break;
63 case llvm::MCSA_Exported:
64 Symbol->setVisibilityType(XCOFF::SYM_V_EXPORTED);
65 break;
66 default:
67 report_fatal_error("Not implemented yet.");
69 return true;
72 void MCXCOFFStreamer::emitXCOFFSymbolLinkageWithVisibility(
73 MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) {
75 emitSymbolAttribute(Symbol, Linkage);
77 // When the caller passes `MCSA_Invalid` for the visibility, do not emit one.
78 if (Visibility == MCSA_Invalid)
79 return;
81 emitSymbolAttribute(Symbol, Visibility);
84 void MCXCOFFStreamer::emitXCOFFRefDirective(const MCSymbol *Symbol) {
85 // Add a Fixup here to later record a relocation of type R_REF to prevent the
86 // ref symbol from being garbage collected (by the binder).
87 MCDataFragment *DF = getOrCreateDataFragment();
88 const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
89 std::optional<MCFixupKind> MaybeKind =
90 getAssembler().getBackend().getFixupKind("R_REF");
91 if (!MaybeKind)
92 report_fatal_error("failed to get fixup kind for R_REF relocation");
94 MCFixupKind Kind = *MaybeKind;
95 MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, Kind);
96 DF->getFixups().push_back(Fixup);
99 void MCXCOFFStreamer::emitXCOFFExceptDirective(const MCSymbol *Symbol,
100 const MCSymbol *Trap,
101 unsigned Lang, unsigned Reason,
102 unsigned FunctionSize,
103 bool hasDebug) {
104 getAssembler().getWriter().addExceptionEntry(Symbol, Trap, Lang, Reason,
105 FunctionSize, hasDebug);
108 void MCXCOFFStreamer::emitXCOFFCInfoSym(StringRef Name, StringRef Metadata) {
109 getAssembler().getWriter().addCInfoSymEntry(Name, Metadata);
112 void MCXCOFFStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
113 Align ByteAlignment) {
114 getAssembler().registerSymbol(*Symbol);
115 Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() !=
116 XCOFF::C_HIDEXT);
117 Symbol->setCommon(Size, ByteAlignment);
119 // Default csect align is 4, but common symbols have explicit alignment values
120 // and we should honor it.
121 cast<MCSymbolXCOFF>(Symbol)->getRepresentedCsect()->setAlignment(
122 ByteAlignment);
124 // Emit the alignment and storage for the variable to the section.
125 emitValueToAlignment(ByteAlignment);
126 emitZeros(Size);
129 void MCXCOFFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
130 uint64_t Size, Align ByteAlignment,
131 SMLoc Loc) {
132 report_fatal_error("Zero fill not implemented for XCOFF.");
135 void MCXCOFFStreamer::emitInstToData(const MCInst &Inst,
136 const MCSubtargetInfo &STI) {
137 MCAssembler &Assembler = getAssembler();
138 SmallVector<MCFixup, 4> Fixups;
139 SmallString<256> Code;
140 Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);
142 // Add the fixups and data.
143 MCDataFragment *DF = getOrCreateDataFragment(&STI);
144 const size_t ContentsSize = DF->getContents().size();
145 auto &DataFragmentFixups = DF->getFixups();
146 for (auto &Fixup : Fixups) {
147 Fixup.setOffset(Fixup.getOffset() + ContentsSize);
148 DataFragmentFixups.push_back(Fixup);
151 DF->setHasInstructions(STI);
152 DF->getContents().append(Code.begin(), Code.end());
155 MCStreamer *llvm::createXCOFFStreamer(MCContext &Context,
156 std::unique_ptr<MCAsmBackend> &&MAB,
157 std::unique_ptr<MCObjectWriter> &&OW,
158 std::unique_ptr<MCCodeEmitter> &&CE,
159 bool RelaxAll) {
160 MCXCOFFStreamer *S = new MCXCOFFStreamer(Context, std::move(MAB),
161 std::move(OW), std::move(CE));
162 if (RelaxAll)
163 S->getAssembler().setRelaxAll(true);
164 return S;
167 void MCXCOFFStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym,
168 uint64_t Size,
169 MCSymbol *CsectSym,
170 Align Alignment) {
171 emitCommonSymbol(CsectSym, Size, Alignment);