106f32e7eSjoerg //===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file assembles .s files and emits Wasm .o object files.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #include "llvm/MC/MCWasmStreamer.h"
1406f32e7eSjoerg #include "llvm/ADT/STLExtras.h"
1506f32e7eSjoerg #include "llvm/ADT/SmallPtrSet.h"
1606f32e7eSjoerg #include "llvm/MC/MCAsmBackend.h"
1706f32e7eSjoerg #include "llvm/MC/MCAsmLayout.h"
1806f32e7eSjoerg #include "llvm/MC/MCAssembler.h"
1906f32e7eSjoerg #include "llvm/MC/MCCodeEmitter.h"
2006f32e7eSjoerg #include "llvm/MC/MCContext.h"
2106f32e7eSjoerg #include "llvm/MC/MCExpr.h"
2206f32e7eSjoerg #include "llvm/MC/MCInst.h"
2306f32e7eSjoerg #include "llvm/MC/MCObjectStreamer.h"
2406f32e7eSjoerg #include "llvm/MC/MCSection.h"
2506f32e7eSjoerg #include "llvm/MC/MCSectionWasm.h"
2606f32e7eSjoerg #include "llvm/MC/MCSymbol.h"
2706f32e7eSjoerg #include "llvm/MC/MCSymbolWasm.h"
2806f32e7eSjoerg #include "llvm/MC/MCValue.h"
2906f32e7eSjoerg #include "llvm/Support/Casting.h"
3006f32e7eSjoerg #include "llvm/Support/Debug.h"
3106f32e7eSjoerg #include "llvm/Support/ErrorHandling.h"
3206f32e7eSjoerg #include "llvm/Support/TargetRegistry.h"
3306f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
3406f32e7eSjoerg 
3506f32e7eSjoerg using namespace llvm;
3606f32e7eSjoerg 
3706f32e7eSjoerg MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
3806f32e7eSjoerg 
mergeFragment(MCDataFragment * DF,MCDataFragment * EF)3906f32e7eSjoerg void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
4006f32e7eSjoerg   flushPendingLabels(DF, DF->getContents().size());
4106f32e7eSjoerg 
4206f32e7eSjoerg   for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
4306f32e7eSjoerg     EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
4406f32e7eSjoerg                                  DF->getContents().size());
4506f32e7eSjoerg     DF->getFixups().push_back(EF->getFixups()[I]);
4606f32e7eSjoerg   }
4706f32e7eSjoerg   if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
4806f32e7eSjoerg     DF->setHasInstructions(*EF->getSubtargetInfo());
4906f32e7eSjoerg   DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
5006f32e7eSjoerg }
5106f32e7eSjoerg 
emitAssemblerFlag(MCAssemblerFlag Flag)52*da58b97aSjoerg void MCWasmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
5306f32e7eSjoerg   // Let the target do whatever target specific stuff it needs to do.
5406f32e7eSjoerg   getAssembler().getBackend().handleAssemblerFlag(Flag);
5506f32e7eSjoerg 
5606f32e7eSjoerg   // Do any generic stuff we need to do.
5706f32e7eSjoerg   llvm_unreachable("invalid assembler flag!");
5806f32e7eSjoerg }
5906f32e7eSjoerg 
changeSection(MCSection * Section,const MCExpr * Subsection)60*da58b97aSjoerg void MCWasmStreamer::changeSection(MCSection *Section,
6106f32e7eSjoerg                                    const MCExpr *Subsection) {
6206f32e7eSjoerg   MCAssembler &Asm = getAssembler();
6306f32e7eSjoerg   auto *SectionWasm = cast<MCSectionWasm>(Section);
6406f32e7eSjoerg   const MCSymbol *Grp = SectionWasm->getGroup();
6506f32e7eSjoerg   if (Grp)
6606f32e7eSjoerg     Asm.registerSymbol(*Grp);
6706f32e7eSjoerg 
68*da58b97aSjoerg   this->MCObjectStreamer::changeSection(Section, Subsection);
6906f32e7eSjoerg   Asm.registerSymbol(*Section->getBeginSymbol());
7006f32e7eSjoerg }
7106f32e7eSjoerg 
emitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)72*da58b97aSjoerg void MCWasmStreamer::emitWeakReference(MCSymbol *Alias,
7306f32e7eSjoerg                                        const MCSymbol *Symbol) {
7406f32e7eSjoerg   getAssembler().registerSymbol(*Symbol);
7506f32e7eSjoerg   const MCExpr *Value = MCSymbolRefExpr::create(
7606f32e7eSjoerg       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
7706f32e7eSjoerg   Alias->setVariableValue(Value);
7806f32e7eSjoerg }
7906f32e7eSjoerg 
emitSymbolAttribute(MCSymbol * S,MCSymbolAttr Attribute)80*da58b97aSjoerg bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
8106f32e7eSjoerg   assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
8206f32e7eSjoerg 
8306f32e7eSjoerg   auto *Symbol = cast<MCSymbolWasm>(S);
8406f32e7eSjoerg 
8506f32e7eSjoerg   // Adding a symbol attribute always introduces the symbol; note that an
8606f32e7eSjoerg   // important side effect of calling registerSymbol here is to register the
8706f32e7eSjoerg   // symbol with the assembler.
8806f32e7eSjoerg   getAssembler().registerSymbol(*Symbol);
8906f32e7eSjoerg 
9006f32e7eSjoerg   switch (Attribute) {
9106f32e7eSjoerg   case MCSA_LazyReference:
9206f32e7eSjoerg   case MCSA_Reference:
9306f32e7eSjoerg   case MCSA_SymbolResolver:
9406f32e7eSjoerg   case MCSA_PrivateExtern:
9506f32e7eSjoerg   case MCSA_WeakDefinition:
9606f32e7eSjoerg   case MCSA_WeakDefAutoPrivate:
9706f32e7eSjoerg   case MCSA_Invalid:
9806f32e7eSjoerg   case MCSA_IndirectSymbol:
9906f32e7eSjoerg   case MCSA_Protected:
10006f32e7eSjoerg     return false;
10106f32e7eSjoerg 
10206f32e7eSjoerg   case MCSA_Hidden:
10306f32e7eSjoerg     Symbol->setHidden(true);
10406f32e7eSjoerg     break;
10506f32e7eSjoerg 
10606f32e7eSjoerg   case MCSA_Weak:
10706f32e7eSjoerg   case MCSA_WeakReference:
10806f32e7eSjoerg     Symbol->setWeak(true);
10906f32e7eSjoerg     Symbol->setExternal(true);
11006f32e7eSjoerg     break;
11106f32e7eSjoerg 
11206f32e7eSjoerg   case MCSA_Global:
11306f32e7eSjoerg     Symbol->setExternal(true);
11406f32e7eSjoerg     break;
11506f32e7eSjoerg 
11606f32e7eSjoerg   case MCSA_ELF_TypeFunction:
11706f32e7eSjoerg     Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
11806f32e7eSjoerg     break;
11906f32e7eSjoerg 
12006f32e7eSjoerg   case MCSA_ELF_TypeObject:
12106f32e7eSjoerg   case MCSA_Cold:
12206f32e7eSjoerg     break;
12306f32e7eSjoerg 
12406f32e7eSjoerg   case MCSA_NoDeadStrip:
12506f32e7eSjoerg     Symbol->setNoStrip();
12606f32e7eSjoerg     break;
12706f32e7eSjoerg 
12806f32e7eSjoerg   default:
12906f32e7eSjoerg     // unrecognized directive
13006f32e7eSjoerg     llvm_unreachable("unexpected MCSymbolAttr");
13106f32e7eSjoerg     return false;
13206f32e7eSjoerg   }
13306f32e7eSjoerg 
13406f32e7eSjoerg   return true;
13506f32e7eSjoerg }
13606f32e7eSjoerg 
emitCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)137*da58b97aSjoerg void MCWasmStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
13806f32e7eSjoerg                                       unsigned ByteAlignment) {
13906f32e7eSjoerg   llvm_unreachable("Common symbols are not yet implemented for Wasm");
14006f32e7eSjoerg }
14106f32e7eSjoerg 
emitELFSize(MCSymbol * Symbol,const MCExpr * Value)14206f32e7eSjoerg void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
14306f32e7eSjoerg   cast<MCSymbolWasm>(Symbol)->setSize(Value);
14406f32e7eSjoerg }
14506f32e7eSjoerg 
emitLocalCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment)146*da58b97aSjoerg void MCWasmStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
14706f32e7eSjoerg                                            unsigned ByteAlignment) {
14806f32e7eSjoerg   llvm_unreachable("Local common symbols are not yet implemented for Wasm");
14906f32e7eSjoerg }
15006f32e7eSjoerg 
emitIdent(StringRef IdentString)151*da58b97aSjoerg void MCWasmStreamer::emitIdent(StringRef IdentString) {
15206f32e7eSjoerg   // TODO(sbc): Add the ident section once we support mergable strings
15306f32e7eSjoerg   // sections in the object format
15406f32e7eSjoerg }
15506f32e7eSjoerg 
emitInstToFragment(const MCInst & Inst,const MCSubtargetInfo & STI)156*da58b97aSjoerg void MCWasmStreamer::emitInstToFragment(const MCInst &Inst,
15706f32e7eSjoerg                                         const MCSubtargetInfo &STI) {
158*da58b97aSjoerg   this->MCObjectStreamer::emitInstToFragment(Inst, STI);
15906f32e7eSjoerg }
16006f32e7eSjoerg 
emitInstToData(const MCInst & Inst,const MCSubtargetInfo & STI)161*da58b97aSjoerg void MCWasmStreamer::emitInstToData(const MCInst &Inst,
16206f32e7eSjoerg                                     const MCSubtargetInfo &STI) {
16306f32e7eSjoerg   MCAssembler &Assembler = getAssembler();
16406f32e7eSjoerg   SmallVector<MCFixup, 4> Fixups;
16506f32e7eSjoerg   SmallString<256> Code;
16606f32e7eSjoerg   raw_svector_ostream VecOS(Code);
16706f32e7eSjoerg   Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
16806f32e7eSjoerg 
16906f32e7eSjoerg   // Append the encoded instruction to the current data fragment (or create a
17006f32e7eSjoerg   // new such fragment if the current fragment is not a data fragment).
17106f32e7eSjoerg   MCDataFragment *DF = getOrCreateDataFragment();
17206f32e7eSjoerg 
17306f32e7eSjoerg   // Add the fixups and data.
17406f32e7eSjoerg   for (unsigned I = 0, E = Fixups.size(); I != E; ++I) {
17506f32e7eSjoerg     Fixups[I].setOffset(Fixups[I].getOffset() + DF->getContents().size());
17606f32e7eSjoerg     DF->getFixups().push_back(Fixups[I]);
17706f32e7eSjoerg   }
17806f32e7eSjoerg   DF->setHasInstructions(STI);
17906f32e7eSjoerg   DF->getContents().append(Code.begin(), Code.end());
18006f32e7eSjoerg }
18106f32e7eSjoerg 
finishImpl()182*da58b97aSjoerg void MCWasmStreamer::finishImpl() {
183*da58b97aSjoerg   emitFrames(nullptr);
18406f32e7eSjoerg 
185*da58b97aSjoerg   this->MCObjectStreamer::finishImpl();
18606f32e7eSjoerg }
18706f32e7eSjoerg 
createWasmStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> && MAB,std::unique_ptr<MCObjectWriter> && OW,std::unique_ptr<MCCodeEmitter> && CE,bool RelaxAll)18806f32e7eSjoerg MCStreamer *llvm::createWasmStreamer(MCContext &Context,
18906f32e7eSjoerg                                      std::unique_ptr<MCAsmBackend> &&MAB,
19006f32e7eSjoerg                                      std::unique_ptr<MCObjectWriter> &&OW,
19106f32e7eSjoerg                                      std::unique_ptr<MCCodeEmitter> &&CE,
19206f32e7eSjoerg                                      bool RelaxAll) {
19306f32e7eSjoerg   MCWasmStreamer *S =
19406f32e7eSjoerg       new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
19506f32e7eSjoerg   if (RelaxAll)
19606f32e7eSjoerg     S->getAssembler().setRelaxAll(true);
19706f32e7eSjoerg   return S;
19806f32e7eSjoerg }
19906f32e7eSjoerg 
emitThumbFunc(MCSymbol * Func)200*da58b97aSjoerg void MCWasmStreamer::emitThumbFunc(MCSymbol *Func) {
20106f32e7eSjoerg   llvm_unreachable("Generic Wasm doesn't support this directive");
20206f32e7eSjoerg }
20306f32e7eSjoerg 
emitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)204*da58b97aSjoerg void MCWasmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
20506f32e7eSjoerg   llvm_unreachable("Wasm doesn't support this directive");
20606f32e7eSjoerg }
20706f32e7eSjoerg 
emitZerofill(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment,SMLoc Loc)208*da58b97aSjoerg void MCWasmStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
20906f32e7eSjoerg                                   uint64_t Size, unsigned ByteAlignment,
21006f32e7eSjoerg                                   SMLoc Loc) {
21106f32e7eSjoerg   llvm_unreachable("Wasm doesn't support this directive");
21206f32e7eSjoerg }
21306f32e7eSjoerg 
emitTBSSSymbol(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)214*da58b97aSjoerg void MCWasmStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
21506f32e7eSjoerg                                     uint64_t Size, unsigned ByteAlignment) {
21606f32e7eSjoerg   llvm_unreachable("Wasm doesn't support this directive");
21706f32e7eSjoerg }
218