1 //===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file assembles .s files and emits Wasm .o object files.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/MC/MCWasmStreamer.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmLayout.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCObjectStreamer.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionWasm.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCSymbolWasm.h"
28 #include "llvm/MC/MCValue.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
37 MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
39 void MCWasmStreamer::mergeFragment(MCDataFragment
*DF
, MCDataFragment
*EF
) {
40 flushPendingLabels(DF
, DF
->getContents().size());
42 for (unsigned I
= 0, E
= EF
->getFixups().size(); I
!= E
; ++I
) {
43 EF
->getFixups()[I
].setOffset(EF
->getFixups()[I
].getOffset() +
44 DF
->getContents().size());
45 DF
->getFixups().push_back(EF
->getFixups()[I
]);
47 if (DF
->getSubtargetInfo() == nullptr && EF
->getSubtargetInfo())
48 DF
->setHasInstructions(*EF
->getSubtargetInfo());
49 DF
->getContents().append(EF
->getContents().begin(), EF
->getContents().end());
52 void MCWasmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag
) {
53 // Let the target do whatever target specific stuff it needs to do.
54 getAssembler().getBackend().handleAssemblerFlag(Flag
);
56 // Do any generic stuff we need to do.
57 llvm_unreachable("invalid assembler flag!");
60 void MCWasmStreamer::ChangeSection(MCSection
*Section
,
61 const MCExpr
*Subsection
) {
62 MCAssembler
&Asm
= getAssembler();
63 auto *SectionWasm
= cast
<MCSectionWasm
>(Section
);
64 const MCSymbol
*Grp
= SectionWasm
->getGroup();
66 Asm
.registerSymbol(*Grp
);
68 this->MCObjectStreamer::ChangeSection(Section
, Subsection
);
69 Asm
.registerSymbol(*Section
->getBeginSymbol());
72 void MCWasmStreamer::EmitWeakReference(MCSymbol
*Alias
,
73 const MCSymbol
*Symbol
) {
74 getAssembler().registerSymbol(*Symbol
);
75 const MCExpr
*Value
= MCSymbolRefExpr::create(
76 Symbol
, MCSymbolRefExpr::VK_WEAKREF
, getContext());
77 Alias
->setVariableValue(Value
);
80 bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol
*S
, MCSymbolAttr Attribute
) {
81 assert(Attribute
!= MCSA_IndirectSymbol
&& "indirect symbols not supported");
83 auto *Symbol
= cast
<MCSymbolWasm
>(S
);
85 // Adding a symbol attribute always introduces the symbol; note that an
86 // important side effect of calling registerSymbol here is to register the
87 // symbol with the assembler.
88 getAssembler().registerSymbol(*Symbol
);
91 case MCSA_LazyReference
:
93 case MCSA_SymbolResolver
:
94 case MCSA_PrivateExtern
:
95 case MCSA_WeakDefinition
:
96 case MCSA_WeakDefAutoPrivate
:
98 case MCSA_IndirectSymbol
:
103 Symbol
->setHidden(true);
107 case MCSA_WeakReference
:
108 Symbol
->setWeak(true);
109 Symbol
->setExternal(true);
113 Symbol
->setExternal(true);
116 case MCSA_ELF_TypeFunction
:
117 Symbol
->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION
);
120 case MCSA_ELF_TypeObject
:
124 case MCSA_NoDeadStrip
:
125 Symbol
->setNoStrip();
129 // unrecognized directive
130 llvm_unreachable("unexpected MCSymbolAttr");
137 void MCWasmStreamer::EmitCommonSymbol(MCSymbol
*S
, uint64_t Size
,
138 unsigned ByteAlignment
) {
139 llvm_unreachable("Common symbols are not yet implemented for Wasm");
142 void MCWasmStreamer::emitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
) {
143 cast
<MCSymbolWasm
>(Symbol
)->setSize(Value
);
146 void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol
*S
, uint64_t Size
,
147 unsigned ByteAlignment
) {
148 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
151 void MCWasmStreamer::EmitValueImpl(const MCExpr
*Value
, unsigned Size
,
153 MCObjectStreamer::EmitValueImpl(Value
, Size
, Loc
);
156 void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment
, int64_t Value
,
158 unsigned MaxBytesToEmit
) {
159 MCObjectStreamer::EmitValueToAlignment(ByteAlignment
, Value
, ValueSize
,
163 void MCWasmStreamer::EmitIdent(StringRef IdentString
) {
164 // TODO(sbc): Add the ident section once we support mergable strings
165 // sections in the object format
168 void MCWasmStreamer::EmitInstToFragment(const MCInst
&Inst
,
169 const MCSubtargetInfo
&STI
) {
170 this->MCObjectStreamer::EmitInstToFragment(Inst
, STI
);
173 void MCWasmStreamer::EmitInstToData(const MCInst
&Inst
,
174 const MCSubtargetInfo
&STI
) {
175 MCAssembler
&Assembler
= getAssembler();
176 SmallVector
<MCFixup
, 4> Fixups
;
177 SmallString
<256> Code
;
178 raw_svector_ostream
VecOS(Code
);
179 Assembler
.getEmitter().encodeInstruction(Inst
, VecOS
, Fixups
, STI
);
181 // Append the encoded instruction to the current data fragment (or create a
182 // new such fragment if the current fragment is not a data fragment).
183 MCDataFragment
*DF
= getOrCreateDataFragment();
185 // Add the fixups and data.
186 for (unsigned I
= 0, E
= Fixups
.size(); I
!= E
; ++I
) {
187 Fixups
[I
].setOffset(Fixups
[I
].getOffset() + DF
->getContents().size());
188 DF
->getFixups().push_back(Fixups
[I
]);
190 DF
->setHasInstructions(STI
);
191 DF
->getContents().append(Code
.begin(), Code
.end());
194 void MCWasmStreamer::FinishImpl() {
197 this->MCObjectStreamer::FinishImpl();
200 MCStreamer
*llvm::createWasmStreamer(MCContext
&Context
,
201 std::unique_ptr
<MCAsmBackend
> &&MAB
,
202 std::unique_ptr
<MCObjectWriter
> &&OW
,
203 std::unique_ptr
<MCCodeEmitter
> &&CE
,
206 new MCWasmStreamer(Context
, std::move(MAB
), std::move(OW
), std::move(CE
));
208 S
->getAssembler().setRelaxAll(true);
212 void MCWasmStreamer::EmitThumbFunc(MCSymbol
*Func
) {
213 llvm_unreachable("Generic Wasm doesn't support this directive");
216 void MCWasmStreamer::EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
217 llvm_unreachable("Wasm doesn't support this directive");
220 void MCWasmStreamer::EmitZerofill(MCSection
*Section
, MCSymbol
*Symbol
,
221 uint64_t Size
, unsigned ByteAlignment
,
223 llvm_unreachable("Wasm doesn't support this directive");
226 void MCWasmStreamer::EmitTBSSSymbol(MCSection
*Section
, MCSymbol
*Symbol
,
227 uint64_t Size
, unsigned ByteAlignment
) {
228 llvm_unreachable("Wasm doesn't support this directive");