1 //===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file assembles .s files and emits Wasm .o object files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/MCWasmStreamer.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCObjectStreamer.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSectionWasm.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCSymbolWasm.h"
29 #include "llvm/MC/MCValue.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
38 MCWasmStreamer::~MCWasmStreamer() {}
40 void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
41 flushPendingLabels(DF, DF->getContents().size());
43 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
44 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
45 DF->getContents().size());
46 DF->getFixups().push_back(EF->getFixups()[i]);
48 DF->setHasInstructions(true);
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 = static_cast<const MCSectionWasm *>(Section);
64 const MCSymbol *Grp = SectionWasm->getGroup();
66 Asm.registerSymbol(*Grp);
68 this->MCObjectStreamer::ChangeSection(Section, Subsection);
71 void MCWasmStreamer::EmitWeakReference(MCSymbol *Alias,
72 const MCSymbol *Symbol) {
73 getAssembler().registerSymbol(*Symbol);
74 const MCExpr *Value = MCSymbolRefExpr::create(
75 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
76 Alias->setVariableValue(Value);
79 bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
80 assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
82 auto *Symbol = cast<MCSymbolWasm>(S);
84 // Adding a symbol attribute always introduces the symbol, note that an
85 // important side effect of calling registerSymbol here is to register
86 // the symbol with the assembler.
87 getAssembler().registerSymbol(*Symbol);
90 case MCSA_LazyReference:
92 case MCSA_SymbolResolver:
93 case MCSA_PrivateExtern:
94 case MCSA_WeakDefinition:
95 case MCSA_WeakDefAutoPrivate:
97 case MCSA_IndirectSymbol:
102 Symbol->setHidden(true);
106 case MCSA_WeakReference:
107 Symbol->setWeak(true);
108 Symbol->setExternal(true);
112 Symbol->setExternal(true);
115 case MCSA_ELF_TypeFunction:
116 Symbol->setIsFunction(true);
119 case MCSA_ELF_TypeObject:
120 Symbol->setIsFunction(false);
124 // unrecognized directive
125 llvm_unreachable("unexpected MCSymbolAttr");
132 void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
133 unsigned ByteAlignment) {
134 llvm_unreachable("Common symbols are not yet implemented for Wasm");
137 void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
138 cast<MCSymbolWasm>(Symbol)->setSize(Value);
141 void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
142 unsigned ByteAlignment) {
143 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
146 void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
148 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
151 void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
153 unsigned MaxBytesToEmit) {
154 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
158 void MCWasmStreamer::EmitIdent(StringRef IdentString) {
159 MCSection *Comment = getAssembler().getContext().getWasmSection(
160 ".comment", SectionKind::getMetadata());
162 SwitchSection(Comment);
167 EmitBytes(IdentString);
172 void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
173 const MCSubtargetInfo &STI) {
174 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
177 void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
178 const MCSubtargetInfo &STI) {
179 MCAssembler &Assembler = getAssembler();
180 SmallVector<MCFixup, 4> Fixups;
181 SmallString<256> Code;
182 raw_svector_ostream VecOS(Code);
183 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
185 // Append the encoded instruction to the current data fragment (or create a
186 // new such fragment if the current fragment is not a data fragment).
187 MCDataFragment *DF = getOrCreateDataFragment();
189 // Add the fixups and data.
190 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
191 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
192 DF->getFixups().push_back(Fixups[i]);
194 DF->setHasInstructions(true);
195 DF->getContents().append(Code.begin(), Code.end());
198 void MCWasmStreamer::FinishImpl() {
201 this->MCObjectStreamer::FinishImpl();
204 MCStreamer *llvm::createWasmStreamer(MCContext &Context,
205 std::unique_ptr<MCAsmBackend> &&MAB,
206 raw_pwrite_stream &OS,
207 std::unique_ptr<MCCodeEmitter> &&CE,
210 new MCWasmStreamer(Context, std::move(MAB), OS, std::move(CE));
212 S->getAssembler().setRelaxAll(true);
216 void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
217 llvm_unreachable("Generic Wasm doesn't support this directive");
220 void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
221 llvm_unreachable("Wasm doesn't support this directive");
224 void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
225 uint64_t Size, unsigned ByteAlignment) {
226 llvm_unreachable("Wasm doesn't support this directive");
229 void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
230 uint64_t Size, unsigned ByteAlignment) {
231 llvm_unreachable("Wasm doesn't support this directive");