2017-02-22 09:23:18 +08:00
|
|
|
//===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-22 09:23:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2021-10-09 01:48:15 +08:00
|
|
|
#include "llvm/MC/TargetRegistry.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
|
2017-02-22 09:23:18 +08:00
|
|
|
|
|
|
|
void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
|
|
|
|
flushPendingLabels(DF, DF->getContents().size());
|
|
|
|
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
|
|
|
|
EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
|
2017-02-22 09:23:18 +08:00
|
|
|
DF->getContents().size());
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
DF->getFixups().push_back(EF->getFixups()[I]);
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
2018-06-06 17:40:06 +08:00
|
|
|
if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
|
|
|
|
DF->setHasInstructions(*EF->getSubtargetInfo());
|
2017-02-22 09:23:18 +08:00
|
|
|
DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
|
|
|
|
}
|
|
|
|
|
2021-09-08 16:53:13 +08:00
|
|
|
void MCWasmStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {
|
|
|
|
auto *Symbol = cast<MCSymbolWasm>(S);
|
|
|
|
MCObjectStreamer::emitLabel(Symbol, Loc);
|
|
|
|
|
|
|
|
const MCSectionWasm &Section =
|
|
|
|
static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
|
|
|
|
if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
|
|
|
|
Symbol->setTLS();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCWasmStreamer::emitLabelAtPos(MCSymbol *S, SMLoc Loc, MCFragment *F,
|
|
|
|
uint64_t Offset) {
|
|
|
|
auto *Symbol = cast<MCSymbolWasm>(S);
|
|
|
|
MCObjectStreamer::emitLabelAtPos(Symbol, Loc, F, Offset);
|
|
|
|
|
|
|
|
const MCSectionWasm &Section =
|
|
|
|
static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
|
|
|
|
if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
|
|
|
|
Symbol->setTLS();
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:16:24 +08:00
|
|
|
void MCWasmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
|
2017-02-22 09:23:18 +08:00
|
|
|
// 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!");
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:28:13 +08:00
|
|
|
void MCWasmStreamer::changeSection(MCSection *Section,
|
2017-02-22 09:23:18 +08:00
|
|
|
const MCExpr *Subsection) {
|
|
|
|
MCAssembler &Asm = getAssembler();
|
2018-11-03 06:04:33 +08:00
|
|
|
auto *SectionWasm = cast<MCSectionWasm>(Section);
|
2017-02-22 09:23:18 +08:00
|
|
|
const MCSymbol *Grp = SectionWasm->getGroup();
|
|
|
|
if (Grp)
|
|
|
|
Asm.registerSymbol(*Grp);
|
|
|
|
|
2020-04-21 10:28:13 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-15 10:16:24 +08:00
|
|
|
void MCWasmStreamer::emitWeakReference(MCSymbol *Alias,
|
2017-02-22 09:23:18 +08:00
|
|
|
const MCSymbol *Symbol) {
|
|
|
|
getAssembler().registerSymbol(*Symbol);
|
|
|
|
const MCExpr *Value = MCSymbolRefExpr::create(
|
|
|
|
Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
|
|
|
|
Alias->setVariableValue(Value);
|
|
|
|
}
|
|
|
|
|
2020-11-03 04:32:06 +08:00
|
|
|
bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
|
2017-02-22 09:23:18 +08:00
|
|
|
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
|
|
|
|
2021-09-08 16:53:13 +08:00
|
|
|
case MCSA_ELF_TypeTLS:
|
|
|
|
Symbol->setTLS();
|
|
|
|
break;
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
case MCSA_ELF_TypeObject:
|
2019-02-02 06:27:34 +08:00
|
|
|
case MCSA_Cold:
|
2017-02-25 07:18:00 +08:00
|
|
|
break;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2019-02-07 09:24:44 +08:00
|
|
|
case MCSA_NoDeadStrip:
|
2019-08-30 06:40:00 +08:00
|
|
|
Symbol->setNoStrip();
|
2019-02-07 09:24:44 +08:00
|
|
|
break;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:16:24 +08:00
|
|
|
void MCWasmStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
|
2017-02-22 09:23:18 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:16:24 +08:00
|
|
|
void MCWasmStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
|
2017-02-22 09:23:18 +08:00
|
|
|
unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Local common symbols are not yet implemented for Wasm");
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:52:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-21 10:28:13 +08:00
|
|
|
void MCWasmStreamer::emitInstToFragment(const MCInst &Inst,
|
2017-02-22 09:23:18 +08:00
|
|
|
const MCSubtargetInfo &STI) {
|
2020-04-21 10:28:13 +08:00
|
|
|
this->MCObjectStreamer::emitInstToFragment(Inst, STI);
|
2021-09-08 16:53:13 +08:00
|
|
|
MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
|
|
|
|
|
|
|
|
for (auto &Fixup : F.getFixups())
|
|
|
|
fixSymbolsInTLSFixups(Fixup.getValue());
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
2020-04-20 17:53:00 +08:00
|
|
|
void MCWasmStreamer::emitInstToData(const MCInst &Inst,
|
2017-02-22 09:23:18 +08:00
|
|
|
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);
|
|
|
|
|
2021-09-08 16:53:13 +08:00
|
|
|
for (auto &Fixup : Fixups)
|
|
|
|
fixSymbolsInTLSFixups(Fixup.getValue());
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
// 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.
|
[WebAssembly] clang-tidy (NFC)
Summary:
This patch fixes clang-tidy warnings on wasm-only files.
The list of checks used is:
`-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,readability-identifier-naming,modernize-*`
(LLVM's default .clang-tidy list is the same except it does not have
`modernize-*`. But I've seen in multiple CLs in LLVM the modernize style
was recommended and code was fixed based on the style, so I added it as
well.)
The common fixes are:
- Variable names start with an uppercase letter
- Function names start with a lowercase letter
- Use `auto` when you use casts so the type is evident
- Use inline initialization for class member variables
- Use `= default` for empty constructors / destructors
- Use `using` in place of `typedef`
Reviewers: sbc100, tlively, aardappel
Subscribers: dschuff, sunfish, jgravelle-google, yurydelendik, kripken, MatzeB, mgorny, rupprecht, llvm-commits
Differential Revision: https://reviews.llvm.org/D57500
llvm-svn: 353075
2019-02-05 03:13:39 +08:00
|
|
|
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]);
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
2018-06-06 17:40:06 +08:00
|
|
|
DF->setHasInstructions(STI);
|
2017-02-22 09:23:18 +08:00
|
|
|
DF->getContents().append(Code.begin(), Code.end());
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:28:13 +08:00
|
|
|
void MCWasmStreamer::finishImpl() {
|
|
|
|
emitFrames(nullptr);
|
2017-02-22 09:23:18 +08:00
|
|
|
|
2020-04-21 10:28:13 +08:00
|
|
|
this->MCObjectStreamer::finishImpl();
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
2021-09-08 16:53:13 +08:00
|
|
|
void MCWasmStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
|
|
|
|
switch (expr->getKind()) {
|
|
|
|
case MCExpr::Target:
|
|
|
|
case MCExpr::Constant:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MCExpr::Binary: {
|
|
|
|
const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
|
|
|
|
fixSymbolsInTLSFixups(be->getLHS());
|
|
|
|
fixSymbolsInTLSFixups(be->getRHS());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MCExpr::SymbolRef: {
|
|
|
|
const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
|
2021-08-27 03:29:32 +08:00
|
|
|
switch (symRef.getKind()) {
|
|
|
|
case MCSymbolRefExpr::VK_WASM_TLSREL:
|
|
|
|
case MCSymbolRefExpr::VK_WASM_GOT_TLS:
|
2021-09-08 16:53:13 +08:00
|
|
|
getAssembler().registerSymbol(symRef.getSymbol());
|
|
|
|
cast<MCSymbolWasm>(symRef.getSymbol()).setTLS();
|
2021-08-27 03:29:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2021-09-08 16:53:13 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MCExpr::Unary:
|
|
|
|
fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
|
|
|
|
break;
|
|
|
|
}
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
2020-02-15 10:16:24 +08:00
|
|
|
void MCWasmStreamer::emitThumbFunc(MCSymbol *Func) {
|
2017-02-22 09:23:18 +08:00
|
|
|
llvm_unreachable("Generic Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:16:24 +08:00
|
|
|
void MCWasmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
2017-02-22 09:23:18 +08:00
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:16:24 +08:00
|
|
|
void MCWasmStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
|
2018-07-03 01:29:43 +08:00
|
|
|
uint64_t Size, unsigned ByteAlignment,
|
|
|
|
SMLoc Loc) {
|
2017-02-22 09:23:18 +08:00
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:52:56 +08:00
|
|
|
void MCWasmStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
|
2017-02-22 09:23:18 +08:00
|
|
|
uint64_t Size, unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|
2021-09-08 16:53:13 +08:00
|
|
|
|
|
|
|
MCStreamer *llvm::createWasmStreamer(MCContext &Context,
|
|
|
|
std::unique_ptr<MCAsmBackend> &&MAB,
|
|
|
|
std::unique_ptr<MCObjectWriter> &&OW,
|
|
|
|
std::unique_ptr<MCCodeEmitter> &&CE,
|
|
|
|
bool RelaxAll) {
|
|
|
|
MCWasmStreamer *S =
|
|
|
|
new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
|
|
|
|
if (RelaxAll)
|
|
|
|
S->getAssembler().setRelaxAll(true);
|
|
|
|
return S;
|
|
|
|
}
|