2017-02-22 09:23:18 +08:00
|
|
|
//===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file assembles .s files and emits Wasm .o object files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCWasmStreamer.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "llvm/MC/MCAsmLayout.h"
|
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCSectionWasm.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/MC/MCSymbolWasm.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
MCWasmStreamer::~MCWasmStreamer() {}
|
|
|
|
|
|
|
|
void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
|
|
|
|
flushPendingLabels(DF, DF->getContents().size());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
|
|
|
|
EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
|
|
|
|
DF->getContents().size());
|
|
|
|
DF->getFixups().push_back(EF->getFixups()[i]);
|
|
|
|
}
|
|
|
|
DF->setHasInstructions(true);
|
|
|
|
DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
|
|
|
// Let the target do whatever target specific stuff it needs to do.
|
|
|
|
getAssembler().getBackend().handleAssemblerFlag(Flag);
|
|
|
|
|
|
|
|
// Do any generic stuff we need to do.
|
|
|
|
llvm_unreachable("invalid assembler flag!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::ChangeSection(MCSection *Section,
|
|
|
|
const MCExpr *Subsection) {
|
|
|
|
MCAssembler &Asm = getAssembler();
|
|
|
|
auto *SectionWasm = static_cast<const MCSectionWasm *>(Section);
|
|
|
|
const MCSymbol *Grp = SectionWasm->getGroup();
|
|
|
|
if (Grp)
|
|
|
|
Asm.registerSymbol(*Grp);
|
|
|
|
|
|
|
|
this->MCObjectStreamer::ChangeSection(Section, Subsection);
|
2018-05-11 01:38:35 +08:00
|
|
|
Asm.registerSymbol(*Section->getBeginSymbol());
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitWeakReference(MCSymbol *Alias,
|
|
|
|
const MCSymbol *Symbol) {
|
|
|
|
getAssembler().registerSymbol(*Symbol);
|
|
|
|
const MCExpr *Value = MCSymbolRefExpr::create(
|
|
|
|
Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
|
|
|
|
Alias->setVariableValue(Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
|
|
|
|
assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
|
|
|
|
|
|
|
|
auto *Symbol = cast<MCSymbolWasm>(S);
|
|
|
|
|
2018-02-12 21:17:09 +08:00
|
|
|
// Adding a symbol attribute always introduces the symbol; note that an
|
|
|
|
// important side effect of calling registerSymbol here is to register the
|
|
|
|
// symbol with the assembler.
|
2017-02-22 09:23:18 +08:00
|
|
|
getAssembler().registerSymbol(*Symbol);
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
switch (Attribute) {
|
|
|
|
case MCSA_LazyReference:
|
|
|
|
case MCSA_Reference:
|
|
|
|
case MCSA_SymbolResolver:
|
|
|
|
case MCSA_PrivateExtern:
|
|
|
|
case MCSA_WeakDefinition:
|
|
|
|
case MCSA_WeakDefAutoPrivate:
|
|
|
|
case MCSA_Invalid:
|
|
|
|
case MCSA_IndirectSymbol:
|
2017-10-21 01:41:12 +08:00
|
|
|
case MCSA_Protected:
|
2017-02-25 07:18:00 +08:00
|
|
|
return false;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-12-03 09:19:23 +08:00
|
|
|
case MCSA_Hidden:
|
|
|
|
Symbol->setHidden(true);
|
|
|
|
break;
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
case MCSA_Weak:
|
|
|
|
case MCSA_WeakReference:
|
|
|
|
Symbol->setWeak(true);
|
|
|
|
Symbol->setExternal(true);
|
|
|
|
break;
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
case MCSA_Global:
|
|
|
|
Symbol->setExternal(true);
|
|
|
|
break;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
case MCSA_ELF_TypeFunction:
|
2018-02-23 13:08:34 +08:00
|
|
|
Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
|
2017-02-25 07:18:00 +08:00
|
|
|
break;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
case MCSA_ELF_TypeObject:
|
2018-02-23 13:08:34 +08:00
|
|
|
Symbol->setType(wasm::WASM_SYMBOL_TYPE_DATA);
|
2017-02-25 07:18:00 +08:00
|
|
|
break;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
default:
|
|
|
|
// unrecognized directive
|
2017-06-20 12:04:59 +08:00
|
|
|
llvm_unreachable("unexpected MCSymbolAttr");
|
2017-02-25 07:18:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-22 09:23:18 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Common symbols are not yet implemented for Wasm");
|
|
|
|
}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
|
|
|
|
cast<MCSymbolWasm>(Symbol)->setSize(Value);
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Local common symbols are not yet implemented for Wasm");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
|
|
|
|
SMLoc Loc) {
|
|
|
|
MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
|
|
|
|
unsigned ValueSize,
|
|
|
|
unsigned MaxBytesToEmit) {
|
|
|
|
MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
|
|
|
|
MaxBytesToEmit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitIdent(StringRef IdentString) {
|
2018-05-03 07:11:38 +08:00
|
|
|
// TODO(sbc): Add the ident section once we support mergable strings
|
|
|
|
// sections in the object format
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
|
|
|
|
const MCSubtargetInfo &STI) {
|
|
|
|
this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
|
|
|
|
const MCSubtargetInfo &STI) {
|
|
|
|
MCAssembler &Assembler = getAssembler();
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallString<256> Code;
|
|
|
|
raw_svector_ostream VecOS(Code);
|
|
|
|
Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
|
|
|
|
|
|
|
|
// Append the encoded instruction to the current data fragment (or create a
|
|
|
|
// new such fragment if the current fragment is not a data fragment).
|
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
|
|
|
|
|
|
|
// Add the fixups and data.
|
|
|
|
for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
|
|
|
|
Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
|
|
|
|
DF->getFixups().push_back(Fixups[i]);
|
|
|
|
}
|
|
|
|
DF->setHasInstructions(true);
|
|
|
|
DF->getContents().append(Code.begin(), Code.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::FinishImpl() {
|
|
|
|
EmitFrames(nullptr);
|
|
|
|
|
2018-05-03 07:01:10 +08:00
|
|
|
// Set fragment atoms so we can map from code fragment to defining symbol
|
|
|
|
addFragmentAtoms();
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
this->MCObjectStreamer::FinishImpl();
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:57:21 +08:00
|
|
|
MCStreamer *llvm::createWasmStreamer(MCContext &Context,
|
|
|
|
std::unique_ptr<MCAsmBackend> &&MAB,
|
2018-05-19 02:26:45 +08:00
|
|
|
std::unique_ptr<MCObjectWriter> &&OW,
|
2017-10-12 07:34:47 +08:00
|
|
|
std::unique_ptr<MCCodeEmitter> &&CE,
|
2017-02-22 09:23:18 +08:00
|
|
|
bool RelaxAll) {
|
2017-10-12 07:34:47 +08:00
|
|
|
MCWasmStreamer *S =
|
2018-05-19 02:26:45 +08:00
|
|
|
new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
|
2017-02-22 09:23:18 +08:00
|
|
|
if (RelaxAll)
|
|
|
|
S->getAssembler().setRelaxAll(true);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
|
|
|
|
llvm_unreachable("Generic Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
|
|
|
|
uint64_t Size, unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
|
|
|
|
uint64_t Size, unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|