LLVM 19.0.0git
MCWasmStreamer.cpp
Go to the documentation of this file.
1//===- lib/MC/MCWasmStreamer.cpp - Wasm 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 Wasm .o object files.
10//
11//===----------------------------------------------------------------------===//
12
17#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCFixup.h"
21#include "llvm/MC/MCFragment.h"
23#include "llvm/MC/MCSection.h"
25#include "llvm/MC/MCSymbol.h"
31
32namespace llvm {
33class MCContext;
34class MCStreamer;
35class MCSubtargetInfo;
36} // namespace llvm
37
38using namespace llvm;
39
40MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
41
42void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
43 flushPendingLabels(DF, DF->getContents().size());
44
45 for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
46 EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
47 DF->getContents().size());
48 DF->getFixups().push_back(EF->getFixups()[I]);
49 }
50 if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
51 DF->setHasInstructions(*EF->getSubtargetInfo());
52 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
53}
54
56 auto *Symbol = cast<MCSymbolWasm>(S);
57 MCObjectStreamer::emitLabel(Symbol, Loc);
58
59 const MCSectionWasm &Section =
60 static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
61 if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
62 Symbol->setTLS();
63}
64
67 auto *Symbol = cast<MCSymbolWasm>(S);
69
70 const MCSectionWasm &Section =
71 static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
72 if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
73 Symbol->setTLS();
74}
75
77 // Let the target do whatever target specific stuff it needs to do.
79
80 // Do any generic stuff we need to do.
81 llvm_unreachable("invalid assembler flag!");
82}
83
85 const MCExpr *Subsection) {
87 auto *SectionWasm = cast<MCSectionWasm>(Section);
88 const MCSymbol *Grp = SectionWasm->getGroup();
89 if (Grp)
90 Asm.registerSymbol(*Grp);
91
92 this->MCObjectStreamer::changeSection(Section, Subsection);
93 Asm.registerSymbol(*Section->getBeginSymbol());
94}
95
97 const MCSymbol *Symbol) {
101 Alias->setVariableValue(Value);
102}
103
105 assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
106
107 auto *Symbol = cast<MCSymbolWasm>(S);
108
109 // Adding a symbol attribute always introduces the symbol; note that an
110 // important side effect of calling registerSymbol here is to register the
111 // symbol with the assembler.
112 getAssembler().registerSymbol(*Symbol);
113
114 switch (Attribute) {
116 case MCSA_Reference:
121 case MCSA_Invalid:
123 case MCSA_Protected:
124 case MCSA_Exported:
125 return false;
126
127 case MCSA_Hidden:
128 Symbol->setHidden(true);
129 break;
130
131 case MCSA_Weak:
133 Symbol->setWeak(true);
134 Symbol->setExternal(true);
135 break;
136
137 case MCSA_Global:
138 Symbol->setExternal(true);
139 break;
140
142 Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
143 break;
144
145 case MCSA_ELF_TypeTLS:
146 Symbol->setTLS();
147 break;
148
150 case MCSA_Cold:
151 break;
152
153 case MCSA_NoDeadStrip:
154 Symbol->setNoStrip();
155 break;
156
157 default:
158 // unrecognized directive
159 llvm_unreachable("unexpected MCSymbolAttr");
160 return false;
161 }
162
163 return true;
164}
165
167 Align ByteAlignment) {
168 llvm_unreachable("Common symbols are not yet implemented for Wasm");
169}
170
172 cast<MCSymbolWasm>(Symbol)->setSize(Value);
173}
174
176 Align ByteAlignment) {
177 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
178}
179
181 // TODO(sbc): Add the ident section once we support mergable strings
182 // sections in the object format
183}
184
185void MCWasmStreamer::emitInstToFragment(const MCInst &Inst,
186 const MCSubtargetInfo &STI) {
188 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
189
190 for (auto &Fixup : F.getFixups())
191 fixSymbolsInTLSFixups(Fixup.getValue());
192}
193
194void MCWasmStreamer::emitInstToData(const MCInst &Inst,
195 const MCSubtargetInfo &STI) {
196 MCAssembler &Assembler = getAssembler();
198 SmallString<256> Code;
199 Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);
200
201 for (auto &Fixup : Fixups)
202 fixSymbolsInTLSFixups(Fixup.getValue());
203
204 // Append the encoded instruction to the current data fragment (or create a
205 // new such fragment if the current fragment is not a data fragment).
207
208 // Add the fixups and data.
209 for (unsigned I = 0, E = Fixups.size(); I != E; ++I) {
210 Fixups[I].setOffset(Fixups[I].getOffset() + DF->getContents().size());
211 DF->getFixups().push_back(Fixups[I]);
212 }
213 DF->setHasInstructions(STI);
214 DF->getContents().append(Code.begin(), Code.end());
215}
216
218 emitFrames(nullptr);
219
221}
222
223void MCWasmStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
224 switch (expr->getKind()) {
225 case MCExpr::Target:
226 case MCExpr::Constant:
227 break;
228
229 case MCExpr::Binary: {
230 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
231 fixSymbolsInTLSFixups(be->getLHS());
232 fixSymbolsInTLSFixups(be->getRHS());
233 break;
234 }
235
236 case MCExpr::SymbolRef: {
237 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
238 switch (symRef.getKind()) {
242 cast<MCSymbolWasm>(symRef.getSymbol()).setTLS();
243 break;
244 default:
245 break;
246 }
247 break;
248 }
249
250 case MCExpr::Unary:
251 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
252 break;
253 }
254}
255
257 llvm_unreachable("Generic Wasm doesn't support this directive");
258}
259
260void MCWasmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
261 llvm_unreachable("Wasm doesn't support this directive");
262}
263
265 uint64_t Size, Align ByteAlignment,
266 SMLoc Loc) {
267 llvm_unreachable("Wasm doesn't support this directive");
268}
269
271 uint64_t Size, Align ByteAlignment) {
272 llvm_unreachable("Wasm doesn't support this directive");
273}
274
276 std::unique_ptr<MCAsmBackend> &&MAB,
277 std::unique_ptr<MCObjectWriter> &&OW,
278 std::unique_ptr<MCCodeEmitter> &&CE,
279 bool RelaxAll) {
280 MCWasmStreamer *S =
281 new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
282 if (RelaxAll)
283 S->getAssembler().setRelaxAll(true);
284 return S;
285}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMContext & Context
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file defines the SmallVector class.
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:232
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:336
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:334
bool registerSymbol(const MCSymbol &Symbol)
void setRelaxAll(bool Value)
Definition: MCAssembler.h:363
Binary assembler expressions.
Definition: MCExpr.h:492
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:639
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:642
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:76
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:197
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:223
const MCSubtargetInfo * getSubtargetInfo() const
Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
Definition: MCFragment.h:173
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
@ Unary
Unary expressions.
Definition: MCExpr.h:41
@ Constant
Constant expressions.
Definition: MCExpr.h:39
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:40
@ Target
Target specific expression.
Definition: MCExpr.h:42
@ Binary
Binary expressions.
Definition: MCExpr.h:38
ExprKind getKind() const
Definition: MCExpr.h:81
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
MCDataFragment * getOrCreateDataFragment(const MCSubtargetInfo *STI=nullptr)
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment.
MCAssembler & getAssembler()
void flushPendingLabels()
Create a data fragment for any pending labels across all Sections and Subsections.
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F, uint64_t Offset)
virtual void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void finishImpl() override
Streamer specific finalization.
MCFragment * getCurrentFragment() const
void emitFrames(MCAsmBackend *MAB)
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:274
This represents a section on wasm.
Definition: MCSectionWasm.h:26
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
Streaming machine code generation interface.
Definition: MCStreamer.h:212
MCContext & getContext() const
Definition: MCStreamer.h:297
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:393
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:410
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:397
VariantKind getKind() const
Definition: MCExpr.h:412
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:47
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a common symbol.
void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a local common (.lcomm) symbol.
void emitIdent(StringRef IdentString) override
Emit the "identifiers" directive.
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override
Set the DescValue for the Symbol.
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void emitThumbFunc(MCSymbol *Func) override
Note in the output that the specified Func is a Thumb mode function (ARM target only).
~MCWasmStreamer() override
void finishImpl() override
Streamer specific finalization.
void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size, Align ByteAlignment=Align(1)) override
Emit a thread local bss (.tbss) symbol.
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F, uint64_t Offset) override
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override
Emit an weak reference from Alias to Symbol.
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
void emitZerofill(MCSection *Section, MCSymbol *Symbol=nullptr, uint64_t Size=0, Align ByteAlignment=Align(1), SMLoc Loc=SMLoc()) override
Emit the zerofill section and an optional symbol.
void emitAssemblerFlag(MCAssemblerFlag Flag) override
Note in the output the specified Flag.
Represents a location in source code.
Definition: SMLoc.h:23
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.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
NodeAddr< CodeNode * > Code
Definition: RDFGraph.h:388
@ WASM_SYMBOL_TYPE_FUNCTION
Definition: Wasm.h:208
@ WASM_SEG_FLAG_TLS
Definition: Wasm.h:218
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
MCAssemblerFlag
Definition: MCDirectives.h:53
MCStreamer * createWasmStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool RelaxAll)
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_WeakDefAutoPrivate
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:48
@ MCSA_Protected
.protected (ELF)
Definition: MCDirectives.h:43
@ MCSA_Exported
.globl _foo, exported (XCOFF)
Definition: MCDirectives.h:34
@ MCSA_PrivateExtern
.private_extern (MachO)
Definition: MCDirectives.h:42
@ MCSA_WeakReference
.weak_reference (MachO)
Definition: MCDirectives.h:47
@ MCSA_LazyReference
.lazy_reference (MachO)
Definition: MCDirectives.h:37
@ MCSA_Reference
.reference (MachO)
Definition: MCDirectives.h:44
@ MCSA_SymbolResolver
.symbol_resolver (MachO)
Definition: MCDirectives.h:40
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
@ MCSA_ELF_TypeTLS
.type _foo, STT_TLS # aka @tls_object
Definition: MCDirectives.h:26
@ MCSA_IndirectSymbol
.indirect_symbol (MachO)
Definition: MCDirectives.h:35
@ MCSA_WeakDefinition
.weak_definition (MachO)
Definition: MCDirectives.h:46
@ MCSA_Global
.type _foo, @gnu_unique_object
Definition: MCDirectives.h:30
@ MCSA_Cold
.cold (MachO)
Definition: MCDirectives.h:22
@ MCSA_ELF_TypeObject
.type _foo, STT_OBJECT # aka @object
Definition: MCDirectives.h:25
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
Definition: MCDirectives.h:23
@ MCSA_Hidden
.hidden (ELF)
Definition: MCDirectives.h:33
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19
@ MCSA_NoDeadStrip
.no_dead_strip (MachO)
Definition: MCDirectives.h:39
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39