2017-02-22 01:23:18 +00:00
|
|
|
//===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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 01:23:18 +00: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"
|
|
|
|
#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;
|
|
|
|
|
[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-04 19:13:39 +00:00
|
|
|
MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
|
2017-02-22 01:23:18 +00: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-04 19:13:39 +00:00
|
|
|
for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
|
|
|
|
EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
|
2017-02-22 01:23:18 +00: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-04 19:13:39 +00:00
|
|
|
DF->getFixups().push_back(EF->getFixups()[I]);
|
2017-02-22 01:23:18 +00:00
|
|
|
}
|
2018-06-06 09:40:06 +00:00
|
|
|
if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
|
|
|
|
DF->setHasInstructions(*EF->getSubtargetInfo());
|
2017-02-22 01:23:18 +00:00
|
|
|
DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
void MCWasmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
|
2017-02-22 01:23:18 +00: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 02:28:13 +00:00
|
|
|
void MCWasmStreamer::changeSection(MCSection *Section,
|
2017-02-22 01:23:18 +00:00
|
|
|
const MCExpr *Subsection) {
|
|
|
|
MCAssembler &Asm = getAssembler();
|
2018-11-02 22:04:33 +00:00
|
|
|
auto *SectionWasm = cast<MCSectionWasm>(Section);
|
2017-02-22 01:23:18 +00:00
|
|
|
const MCSymbol *Grp = SectionWasm->getGroup();
|
|
|
|
if (Grp)
|
|
|
|
Asm.registerSymbol(*Grp);
|
|
|
|
|
2020-04-21 02:28:13 +00:00
|
|
|
this->MCObjectStreamer::changeSection(Section, Subsection);
|
2018-05-10 17:38:35 +00:00
|
|
|
Asm.registerSymbol(*Section->getBeginSymbol());
|
2017-02-22 01:23:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
void MCWasmStreamer::emitWeakReference(MCSymbol *Alias,
|
2017-02-22 01:23:18 +00:00
|
|
|
const MCSymbol *Symbol) {
|
|
|
|
getAssembler().registerSymbol(*Symbol);
|
|
|
|
const MCExpr *Value = MCSymbolRefExpr::create(
|
|
|
|
Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
|
|
|
|
Alias->setVariableValue(Value);
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
|
2017-02-22 01:23:18 +00:00
|
|
|
assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
|
|
|
|
|
|
|
|
auto *Symbol = cast<MCSymbolWasm>(S);
|
|
|
|
|
2018-02-12 13:17:09 +00: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 01:23:18 +00:00
|
|
|
getAssembler().registerSymbol(*Symbol);
|
|
|
|
|
2017-02-24 23:18:00 +00: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-20 17:41:12 +00:00
|
|
|
case MCSA_Protected:
|
2017-02-24 23:18:00 +00:00
|
|
|
return false;
|
2017-06-20 04:04:59 +00:00
|
|
|
|
2017-12-03 01:19:23 +00:00
|
|
|
case MCSA_Hidden:
|
|
|
|
Symbol->setHidden(true);
|
|
|
|
break;
|
|
|
|
|
2017-06-20 04:04:59 +00:00
|
|
|
case MCSA_Weak:
|
|
|
|
case MCSA_WeakReference:
|
|
|
|
Symbol->setWeak(true);
|
|
|
|
Symbol->setExternal(true);
|
|
|
|
break;
|
|
|
|
|
2017-02-24 23:18:00 +00:00
|
|
|
case MCSA_Global:
|
|
|
|
Symbol->setExternal(true);
|
|
|
|
break;
|
2017-06-20 04:04:59 +00:00
|
|
|
|
2017-02-24 23:18:00 +00:00
|
|
|
case MCSA_ELF_TypeFunction:
|
2018-02-23 05:08:34 +00:00
|
|
|
Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
|
2017-02-24 23:18:00 +00:00
|
|
|
break;
|
2017-06-20 04:04:59 +00:00
|
|
|
|
2017-02-24 23:18:00 +00:00
|
|
|
case MCSA_ELF_TypeObject:
|
2019-02-01 22:27:34 +00:00
|
|
|
case MCSA_Cold:
|
2017-02-24 23:18:00 +00:00
|
|
|
break;
|
2017-06-20 04:04:59 +00:00
|
|
|
|
2019-02-07 01:24:44 +00:00
|
|
|
case MCSA_NoDeadStrip:
|
2019-08-29 22:40:00 +00:00
|
|
|
Symbol->setNoStrip();
|
2019-02-07 01:24:44 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-24 23:18:00 +00:00
|
|
|
default:
|
|
|
|
// unrecognized directive
|
2017-06-20 04:04:59 +00:00
|
|
|
llvm_unreachable("unexpected MCSymbolAttr");
|
2017-02-24 23:18:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-22 01:23:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
void MCWasmStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
|
2017-02-22 01:23:18 +00:00
|
|
|
unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Common symbols are not yet implemented for Wasm");
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:18:00 +00:00
|
|
|
void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
|
|
|
|
cast<MCSymbolWasm>(Symbol)->setSize(Value);
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
void MCWasmStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
|
2017-02-22 01:23:18 +00:00
|
|
|
unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Local common symbols are not yet implemented for Wasm");
|
|
|
|
}
|
|
|
|
|
2020-02-15 03:21:58 +00:00
|
|
|
void MCWasmStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
|
2017-02-22 01:23:18 +00:00
|
|
|
SMLoc Loc) {
|
2020-02-15 03:21:58 +00:00
|
|
|
MCObjectStreamer::emitValueImpl(Value, Size, Loc);
|
2017-02-22 01:23:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 03:21:58 +00:00
|
|
|
void MCWasmStreamer::emitValueToAlignment(unsigned ByteAlignment, int64_t Value,
|
2017-02-22 01:23:18 +00:00
|
|
|
unsigned ValueSize,
|
|
|
|
unsigned MaxBytesToEmit) {
|
2020-02-15 03:21:58 +00:00
|
|
|
MCObjectStreamer::emitValueToAlignment(ByteAlignment, Value, ValueSize,
|
2017-02-22 01:23:18 +00:00
|
|
|
MaxBytesToEmit);
|
|
|
|
}
|
|
|
|
|
2020-02-15 16:52:56 +00:00
|
|
|
void MCWasmStreamer::emitIdent(StringRef IdentString) {
|
2018-05-02 23:11:38 +00:00
|
|
|
// TODO(sbc): Add the ident section once we support mergable strings
|
|
|
|
// sections in the object format
|
2017-02-22 01:23:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 02:28:13 +00:00
|
|
|
void MCWasmStreamer::emitInstToFragment(const MCInst &Inst,
|
2017-02-22 01:23:18 +00:00
|
|
|
const MCSubtargetInfo &STI) {
|
2020-04-21 02:28:13 +00:00
|
|
|
this->MCObjectStreamer::emitInstToFragment(Inst, STI);
|
2017-02-22 01:23:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 09:53:00 +00:00
|
|
|
void MCWasmStreamer::emitInstToData(const MCInst &Inst,
|
2017-02-22 01:23:18 +00: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);
|
|
|
|
|
|
|
|
// 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-04 19:13:39 +00: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 01:23:18 +00:00
|
|
|
}
|
2018-06-06 09:40:06 +00:00
|
|
|
DF->setHasInstructions(STI);
|
2017-02-22 01:23:18 +00:00
|
|
|
DF->getContents().append(Code.begin(), Code.end());
|
|
|
|
}
|
|
|
|
|
2020-04-21 02:28:13 +00:00
|
|
|
void MCWasmStreamer::finishImpl() {
|
|
|
|
emitFrames(nullptr);
|
2017-02-22 01:23:18 +00:00
|
|
|
|
2020-04-21 02:28:13 +00:00
|
|
|
this->MCObjectStreamer::finishImpl();
|
2017-02-22 01:23:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 01:57:21 +00:00
|
|
|
MCStreamer *llvm::createWasmStreamer(MCContext &Context,
|
|
|
|
std::unique_ptr<MCAsmBackend> &&MAB,
|
2018-05-18 18:26:45 +00:00
|
|
|
std::unique_ptr<MCObjectWriter> &&OW,
|
2017-10-11 23:34:47 +00:00
|
|
|
std::unique_ptr<MCCodeEmitter> &&CE,
|
2017-02-22 01:23:18 +00:00
|
|
|
bool RelaxAll) {
|
2017-10-11 23:34:47 +00:00
|
|
|
MCWasmStreamer *S =
|
2018-05-18 18:26:45 +00:00
|
|
|
new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
|
2017-02-22 01:23:18 +00:00
|
|
|
if (RelaxAll)
|
|
|
|
S->getAssembler().setRelaxAll(true);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
void MCWasmStreamer::emitThumbFunc(MCSymbol *Func) {
|
2017-02-22 01:23:18 +00:00
|
|
|
llvm_unreachable("Generic Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
void MCWasmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
2017-02-22 01:23:18 +00:00
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
2020-02-15 02:16:24 +00:00
|
|
|
void MCWasmStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
|
2018-07-02 17:29:43 +00:00
|
|
|
uint64_t Size, unsigned ByteAlignment,
|
|
|
|
SMLoc Loc) {
|
2017-02-22 01:23:18 +00:00
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|
|
|
|
|
2020-02-15 16:52:56 +00:00
|
|
|
void MCWasmStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
|
2017-02-22 01:23:18 +00:00
|
|
|
uint64_t Size, unsigned ByteAlignment) {
|
|
|
|
llvm_unreachable("Wasm doesn't support this directive");
|
|
|
|
}
|