Skip to content

[WebAssembly] Add WebAssembly::Specifier #133116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 0 additions & 7 deletions llvm/include/llvm/MC/MCExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,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 @@ -700,8 +701,8 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser {
auto *WasmSym = cast<MCSymbolWasm>(Sym);
WasmSym->setSignature(Signature);
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
const MCExpr *Expr = MCSymbolRefExpr::create(
WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
const MCExpr *Expr =
MCSymbolRefExpr::create(WasmSym, WebAssembly::S_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 (SymRef->getSpecifier()) {
case WebAssembly::S_GOT:
case WebAssembly::S_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, WebAssembly::S_TYPEINDEX, getContext());
MI.addOperand(MCOperand::createExpr(Expr));
}
break;
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 (SRE->getSpecifier() == WebAssembly::S_TYPEINDEX) {
auto &Sym = static_cast<const MCSymbolWasm &>(SRE->getSymbol());
O << WebAssembly::signatureToString(Sym.getSignature());
} else {
Expand Down
19 changes: 10 additions & 9 deletions llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCAsmInfo.cpp
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 @@ -21,14 +22,14 @@ 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"},
const MCAsmInfo::AtSpecifier atSpecifiers[] = {
{WebAssembly::S_TYPEINDEX, "TYPEINDEX"},
{WebAssembly::S_TBREL, "TBREL"},
{WebAssembly::S_MBREL, "MBREL"},
{WebAssembly::S_TLSREL, "TLSREL"},
{WebAssembly::S_GOT, "GOT"},
{WebAssembly::S_GOT_TLS, "GOT@TLS"},
{WebAssembly::S_FUNCINDEX, "FUNCINDEX"},
};

WebAssemblyMCAsmInfo::~WebAssemblyMCAsmInfo() = default; // anchor.
Expand Down Expand Up @@ -64,5 +65,5 @@ WebAssemblyMCAsmInfo::WebAssemblyMCAsmInfo(const Triple &T,
if (WebAssembly::WasmEnableEH || WebAssembly::WasmEnableSjLj)
ExceptionsType = ExceptionHandling::Wasm;

initializeVariantKinds(variantKindDescs);
initializeVariantKinds(atSpecifiers);
}
29 changes: 29 additions & 0 deletions llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCExpr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===- 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
//
//===----------------------------------------------------------------------===//
//
// Defines WebAssembly-specific relocation specifiers.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCEXPR_H
#define LLVM_LIB_TARGET_WEBASSEMBLY_MCTARGETDESC_WEBASSEMBLYMCEXPR_H

namespace llvm::WebAssembly {
enum Specifier {
S_None,
S_FUNCINDEX, // Wasm function index
S_GOT,
S_GOT_TLS, // Wasm global index of TLS symbol
S_MBREL, // Memory address relative to __memory_base
S_TBREL, // Table index relative to __table_base
S_TLSREL, // Memory address relative to __tls_base
S_TYPEINDEX, // Reference to a symbol's type (signature)
};
} // namespace llvm::WebAssembly

#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 = WebAssembly::Specifier(Target.getSpecifier());
switch (Spec) {
case MCSymbolRefExpr::VK_GOT:
case MCSymbolRefExpr::VK_WASM_GOT_TLS:
case WebAssembly::S_GOT:
SymA.setUsedInGOT();
return wasm::R_WASM_GLOBAL_INDEX_LEB;
case MCSymbolRefExpr::VK_WASM_TBREL:
case WebAssembly::S_GOT_TLS:
SymA.setUsedInGOT();
SymA.setTLS();
return wasm::R_WASM_GLOBAL_INDEX_LEB;
case WebAssembly::S_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 WebAssembly::S_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 WebAssembly::S_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 WebAssembly::S_TYPEINDEX:
return wasm::R_WASM_TYPE_INDEX_LEB;
case MCSymbolRefExpr::VK_None:
case WebAssembly::S_None:
break;
case MCSymbolRefExpr::VK_WASM_FUNCINDEX:
case WebAssembly::S_FUNCINDEX:
return wasm::R_WASM_FUNCTION_INDEX_I32;
default:
report_fatal_error("unknown VariantKind");
break;
}

switch (unsigned(Fixup.getKind())) {
Expand Down
4 changes: 2 additions & 2 deletions 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,8 +591,7 @@ void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {

for (auto &Sym : Symbols) {
OutStreamer->emitValue(
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_WASM_FUNCINDEX,
OutContext),
MCSymbolRefExpr::create(Sym, WebAssembly::S_FUNCINDEX, OutContext),
4);
}
OutStreamer->popSection();
Expand Down
17 changes: 9 additions & 8 deletions llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

#include "WebAssemblyMCInstLower.h"
#include "MCTargetDesc/WebAssemblyMCExpr.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "TargetInfo/WebAssemblyTargetInfo.h"
#include "Utils/WebAssemblyTypeUtilities.h"
Expand Down Expand Up @@ -91,32 +92,32 @@ MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(

MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
MCSymbol *Sym) const {
MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
auto Spec = WebAssembly::S_None;
unsigned TargetFlags = MO.getTargetFlags();

switch (TargetFlags) {
case WebAssemblyII::MO_NO_FLAG:
break;
case WebAssemblyII::MO_GOT_TLS:
Kind = MCSymbolRefExpr::VK_WASM_GOT_TLS;
Spec = WebAssembly::S_GOT_TLS;
break;
case WebAssemblyII::MO_GOT:
Kind = MCSymbolRefExpr::VK_GOT;
Spec = WebAssembly::S_GOT;
break;
case WebAssemblyII::MO_MEMORY_BASE_REL:
Kind = MCSymbolRefExpr::VK_WASM_MBREL;
Spec = WebAssembly::S_MBREL;
break;
case WebAssemblyII::MO_TLS_BASE_REL:
Kind = MCSymbolRefExpr::VK_WASM_TLSREL;
Spec = WebAssembly::S_TLSREL;
break;
case WebAssemblyII::MO_TABLE_BASE_REL:
Kind = MCSymbolRefExpr::VK_WASM_TBREL;
Spec = WebAssembly::S_TBREL;
break;
default:
llvm_unreachable("Unknown target flag on GV operand");
}

const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx);
const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Spec, Ctx);

if (MO.getOffset() != 0) {
const auto *WasmSym = cast<MCSymbolWasm>(Sym);
Expand Down Expand Up @@ -149,7 +150,7 @@ MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
WasmSym->setSignature(Signature);
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
const MCExpr *Expr =
MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
MCSymbolRefExpr::create(WasmSym, WebAssembly::S_TYPEINDEX, Ctx);
return MCOperand::createExpr(Expr);
}

Expand Down