Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions llvm/include/llvm/MC/MCExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,6 @@ class MCSymbolRefExpr : public MCExpr {

VK_COFF_IMGREL32, // symbol@imgrel (image-relative)

VK_WASM_TYPEINDEX, // Reference to a symbol's type (signature)
VK_WASM_TLSREL, // Memory address relative to __tls_base
VK_WASM_MBREL, // Memory address relative to __memory_base
VK_WASM_TBREL, // Table index relative to __table_base
VK_WASM_GOT_TLS, // Wasm global index of TLS symbol.
VK_WASM_FUNCINDEX, // Wasm function index.

FirstTargetSpecifier,
};
Expand Down
3 changes: 0 additions & 3 deletions llvm/include/llvm/MC/MCWasmStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,8 @@ class MCWasmStreamer : public MCObjectStreamer {
void finishImpl() override;

private:
void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &) override;
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override;

void fixSymbolsInTLSFixups(const MCExpr *expr);

bool SeenIdent;
};

Expand Down
45 changes: 0 additions & 45 deletions llvm/lib/MC/MCWasmStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,13 @@ void MCWasmStreamer::emitIdent(StringRef IdentString) {
// sections in the object format
}

void MCWasmStreamer::emitInstToFragment(const MCInst &Inst,
const MCSubtargetInfo &STI) {
this->MCObjectStreamer::emitInstToFragment(Inst, STI);
MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());

for (auto &Fixup : F.getFixups())
fixSymbolsInTLSFixups(Fixup.getValue());
}

void MCWasmStreamer::emitInstToData(const MCInst &Inst,
const MCSubtargetInfo &STI) {
MCAssembler &Assembler = getAssembler();
SmallVector<MCFixup, 4> Fixups;
SmallString<256> Code;
Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);

for (auto &Fixup : Fixups)
fixSymbolsInTLSFixups(Fixup.getValue());

// 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();
Expand All @@ -205,39 +193,6 @@ void MCWasmStreamer::finishImpl() {
this->MCObjectStreamer::finishImpl();
}

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);
switch (symRef.getKind()) {
case MCSymbolRefExpr::VK_WASM_TLSREL:
case MCSymbolRefExpr::VK_WASM_GOT_TLS:
getAssembler().registerSymbol(symRef.getSymbol());
cast<MCSymbolWasm>(symRef.getSymbol()).setTLS();
break;
default:
break;
}
break;
}

case MCExpr::Unary:
fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
break;
}
}

void MCWasmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
llvm_unreachable("Wasm doesn't support this directive");
}
Expand Down
9 changes: 0 additions & 9 deletions llvm/lib/MC/WasmObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,6 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
SymA->setUsedInReloc();
}

switch (Target.getSpecifier()) {
case MCSymbolRefExpr::VK_GOT:
case MCSymbolRefExpr::VK_WASM_GOT_TLS:
SymA->setUsedInGOT();
break;
default:
break;
}

WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//===----------------------------------------------------------------------===//

#include "AsmParser/WebAssemblyAsmTypeCheck.h"
#include "MCTargetDesc/WebAssemblyMCExpr.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "MCTargetDesc/WebAssemblyMCTypeUtilities.h"
#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
Expand Down Expand Up @@ -701,7 +702,7 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
WasmSym->setSignature(Signature);
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
const MCExpr *Expr = MCSymbolRefExpr::create(
WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
WasmSym, WebAssemblyMCExpr::VK_TYPEINDEX, Ctx);
Operands.push_back(std::make_unique<WebAssemblyOperand>(
Loc.getLoc(), Loc.getEndLoc(), WebAssemblyOperand::SymOp{Expr}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//===----------------------------------------------------------------------===//

#include "AsmParser/WebAssemblyAsmTypeCheck.h"
#include "MCTargetDesc/WebAssemblyMCExpr.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "MCTargetDesc/WebAssemblyMCTypeUtilities.h"
#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
Expand Down Expand Up @@ -264,9 +265,9 @@ bool WebAssemblyAsmTypeCheck::getGlobal(SMLoc ErrorLoc,
break;
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
case wasm::WASM_SYMBOL_TYPE_DATA:
switch (SymRef->getKind()) {
case MCSymbolRefExpr::VK_GOT:
case MCSymbolRefExpr::VK_WASM_GOT_TLS:
switch (getSpecifier(SymRef)) {
case WebAssemblyMCExpr::VK_GOT:
case WebAssemblyMCExpr::VK_GOT_TLS:
Type = Is64 ? wasm::ValType::I64 : wasm::ValType::I32;
return false;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
///
//===----------------------------------------------------------------------===//

#include "MCTargetDesc/WebAssemblyMCExpr.h"
#include "MCTargetDesc/WebAssemblyMCTypeUtilities.h"
#include "TargetInfo/WebAssemblyTargetInfo.h"
#include "llvm/BinaryFormat/Wasm.h"
Expand Down Expand Up @@ -238,7 +239,7 @@ MCDisassembler::DecodeStatus WebAssemblyDisassembler::getInstruction(
auto *WasmSym = cast<MCSymbolWasm>(Sym);
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
const MCExpr *Expr = MCSymbolRefExpr::create(
WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, getContext());
WasmSym, WebAssemblyMCExpr::VK_TYPEINDEX, getContext());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the VariantKind in these resulting expressions? i.e. what geKind() now return instead of the old VK_WASM_.. values?

MI.addOperand(MCOperand::createExpr(Expr));
}
break;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/WebAssembly/MCTargetDesc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_llvm_component_library(LLVMWebAssemblyDesc
WebAssemblyInstPrinter.cpp
WebAssemblyMCAsmInfo.cpp
WebAssemblyMCCodeEmitter.cpp
WebAssemblyMCExpr.cpp
WebAssemblyMCTargetDesc.cpp
WebAssemblyMCTypeUtilities.cpp
WebAssemblyTargetStreamer.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//

#include "MCTargetDesc/WebAssemblyInstPrinter.h"
#include "MCTargetDesc/WebAssemblyMCExpr.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "MCTargetDesc/WebAssemblyMCTypeUtilities.h"
#include "llvm/ADT/APFloat.h"
Expand Down Expand Up @@ -339,7 +340,7 @@ void WebAssemblyInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
// as a signature here, such that the assembler can recover this
// information.
auto SRE = static_cast<const MCSymbolRefExpr *>(Op.getExpr());
if (SRE->getKind() == MCSymbolRefExpr::VK_WASM_TYPEINDEX) {
if (getSpecifier(SRE) == WebAssemblyMCExpr::VK_TYPEINDEX) {
auto &Sym = static_cast<const MCSymbolWasm &>(SRE->getSymbol());
O << WebAssembly::signatureToString(Sym.getSignature());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

#include "WebAssemblyMCAsmInfo.h"
#include "WebAssemblyMCExpr.h"
#include "WebAssemblyMCTargetDesc.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/TargetParser/Triple.h"
Expand All @@ -22,13 +23,13 @@ using namespace llvm;
#define DEBUG_TYPE "wasm-mc-asm-info"

const MCAsmInfo::VariantKindDesc variantKindDescs[] = {
{MCSymbolRefExpr::VK_WASM_TYPEINDEX, "TYPEINDEX"},
{MCSymbolRefExpr::VK_WASM_TBREL, "TBREL"},
{MCSymbolRefExpr::VK_WASM_MBREL, "MBREL"},
{MCSymbolRefExpr::VK_WASM_TLSREL, "TLSREL"},
{MCSymbolRefExpr::VK_GOT, "GOT"},
{MCSymbolRefExpr::VK_WASM_GOT_TLS, "GOT@TLS"},
{MCSymbolRefExpr::VK_WASM_FUNCINDEX, "FUNCINDEX"},
{WebAssemblyMCExpr::VK_TYPEINDEX, "TYPEINDEX"},
{WebAssemblyMCExpr::VK_TBREL, "TBREL"},
{WebAssemblyMCExpr::VK_MBREL, "MBREL"},
{WebAssemblyMCExpr::VK_TLSREL, "TLSREL"},
{WebAssemblyMCExpr::VK_GOT, "GOT"},
{WebAssemblyMCExpr::VK_GOT_TLS, "GOT@TLS"},
{WebAssemblyMCExpr::VK_FUNCINDEX, "FUNCINDEX"},
};

WebAssemblyMCAsmInfo::~WebAssemblyMCAsmInfo() = default; // anchor.
Expand Down
34 changes: 34 additions & 0 deletions llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCExpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===- WebAssembly specific MC expression classes ---------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "WebAssemblyMCExpr.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCValue.h"

using namespace llvm;

const WebAssemblyMCExpr *
WebAssemblyMCExpr::create(const MCExpr *Expr, Specifier S, MCContext &Ctx) {
return new (Ctx) WebAssemblyMCExpr(Expr, S);
}

void WebAssemblyMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
}

bool WebAssemblyMCExpr::evaluateAsRelocatableImpl(
MCValue &Res, const MCAssembler *Asm) const {
if (!getSubExpr()->evaluateAsRelocatable(Res, Asm))
return false;
Res.setSpecifier(specifier);
return !Res.getSubSym();
}

void WebAssemblyMCExpr::visitUsedExpr(MCStreamer &S) const {
S.visitUsedExpr(*Expr);
}
64 changes: 64 additions & 0 deletions llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCExpr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//===- WebAssembly specific MC expression classes ---------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// The MCTargetExpr subclass describes a relocatable expression with a
// WebAssembly-specific relocation specifier.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCEXPR_H
#define LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCEXPR_H

#include "llvm/MC/MCExpr.h"

namespace llvm {

class WebAssemblyMCExpr : public MCTargetExpr {
public:
enum Specifier {
VK_None,
VK_TYPEINDEX,
VK_TBREL,
VK_MBREL,
VK_TLSREL,
VK_GOT,
VK_GOT_TLS,
VK_FUNCINDEX,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should VK_ prefix be changed? As assume the K in is for Kind which is now separate to specifier?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that we don't necessarily use VK_. For AArch64 Mach-O I've used M_.

Renamed these to S_ for (S)pecifier.

};

private:
const MCExpr *Expr;
const Specifier specifier;

protected:
explicit WebAssemblyMCExpr(const MCExpr *Expr, Specifier S)
: Expr(Expr), specifier(S) {}

public:
static const WebAssemblyMCExpr *create(const MCExpr *, Specifier,
MCContext &);

Specifier getSpecifier() const { return specifier; }
const MCExpr *getSubExpr() const { return Expr; }

void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
bool evaluateAsRelocatableImpl(MCValue &Res,
const MCAssembler *Asm) const override;
void visitUsedExpr(MCStreamer &Streamer) const override;
MCFragment *findAssociatedFragment() const override {
return getSubExpr()->findAssociatedFragment();
}
};

static inline WebAssemblyMCExpr::Specifier
getSpecifier(const MCSymbolRefExpr *SRE) {
return WebAssemblyMCExpr::Specifier(SRE->getKind());
}
} // namespace llvm

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

#include "MCTargetDesc/WebAssemblyFixupKinds.h"
#include "MCTargetDesc/WebAssemblyMCExpr.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/MC/MCAsmBackend.h"
Expand Down Expand Up @@ -66,31 +67,33 @@ unsigned WebAssemblyWasmObjectWriter::getRelocType(
const MCValue &Target, const MCFixup &Fixup,
const MCSectionWasm &FixupSection, bool IsLocRel) const {
auto &SymA = cast<MCSymbolWasm>(*Target.getAddSym());
auto Spec = Target.getSpecifier();
auto Spec = WebAssemblyMCExpr::Specifier(Target.getSpecifier());
switch (Spec) {
case MCSymbolRefExpr::VK_GOT:
case MCSymbolRefExpr::VK_WASM_GOT_TLS:
case WebAssemblyMCExpr::VK_GOT:
SymA.setUsedInGOT();
return wasm::R_WASM_GLOBAL_INDEX_LEB;
case MCSymbolRefExpr::VK_WASM_TBREL:
case WebAssemblyMCExpr::VK_GOT_TLS:
SymA.setUsedInGOT();
SymA.setTLS();
return wasm::R_WASM_GLOBAL_INDEX_LEB;
case WebAssemblyMCExpr::VK_TBREL:
assert(SymA.isFunction());
return is64Bit() ? wasm::R_WASM_TABLE_INDEX_REL_SLEB64
: wasm::R_WASM_TABLE_INDEX_REL_SLEB;
case MCSymbolRefExpr::VK_WASM_TLSREL:
case WebAssemblyMCExpr::VK_TLSREL:
SymA.setTLS();
return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64
: wasm::R_WASM_MEMORY_ADDR_TLS_SLEB;
case MCSymbolRefExpr::VK_WASM_MBREL:
case WebAssemblyMCExpr::VK_MBREL:
assert(SymA.isData());
return is64Bit() ? wasm::R_WASM_MEMORY_ADDR_REL_SLEB64
: wasm::R_WASM_MEMORY_ADDR_REL_SLEB;
case MCSymbolRefExpr::VK_WASM_TYPEINDEX:
case WebAssemblyMCExpr::VK_TYPEINDEX:
return wasm::R_WASM_TYPE_INDEX_LEB;
case MCSymbolRefExpr::VK_None:
case WebAssemblyMCExpr::VK_None:
break;
case MCSymbolRefExpr::VK_WASM_FUNCINDEX:
case WebAssemblyMCExpr::VK_FUNCINDEX:
return wasm::R_WASM_FUNCTION_INDEX_I32;
default:
report_fatal_error("unknown VariantKind");
break;
}

switch (unsigned(Fixup.getKind())) {
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//===----------------------------------------------------------------------===//

#include "WebAssemblyAsmPrinter.h"
#include "MCTargetDesc/WebAssemblyMCExpr.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
#include "TargetInfo/WebAssemblyTargetInfo.h"
Expand Down Expand Up @@ -590,7 +591,7 @@ void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {

for (auto &Sym : Symbols) {
OutStreamer->emitValue(
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_WASM_FUNCINDEX,
MCSymbolRefExpr::create(Sym, WebAssemblyMCExpr::VK_FUNCINDEX,
OutContext),
4);
}
Expand Down
Loading
Loading