llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
Heejin Ahn 77b921b870 [WebAssembly] Tidy up EH/SjLj options
This CL is small, but the description can be a little long because I'm
trying to sum up the status quo for Emscripten/Wasm EH/SjLj options.

First, this CL adds an option for Wasm SjLj (`-wasm-enable-sjlj`), which
handles SjLj using Wasm EH. The implementation for this will be added as
a followup CL, but this adds the option first to do error checking.

This also adds an option for Wasm EH (`-wasm-enable-eh`), which has been
already implemented. Before we used `-exception-model=wasm` as the same
meaning as enabling Wasm EH, but after we add Wasm SjLj, it will be
possible to use Wasm EH instructions for Wasm SjLj while not enabling
EH, so going forward, to use Wasm EH, `opt` and `llc` will need this
option. This only affects `opt` and `llc` command lines and does not
affect Emscripten user interface.

Now we have two modes of EH (Emscripten/Wasm) and also two modes of SjLj
(also Emscripten/Wasm). The options corresponding to each of are:
- Emscripten EH: `-enable-emscripten-cxx-exceptions`
- Emscripten SjLj: `-enable-emscripten-sjlj`
- Wasm EH: `-wasm-enable-eh -exception-model=wasm`
           `-mattr=+exception-handling`
- Wasm SjLj: `-wasm-enable-sjlj -exception-model=wasm`
             `-mattr=+exception-handling`
The reason Wasm EH/SjLj's options are a little complicated are
`-exception-model` and `-mattr` are common LLVM options ane not under
our control. (`-mattr` can be omitted if it is embedded within the
bitcode file.)

And we have the following rules of the option composition:
- Emscripten EH and Wasm EH cannot be turned on at the same itme
- Emscripten SjLj and Wasm SjLj cannot be turned on at the same time
- Wasm SjLj should be used with Wasm EH

Which means we now allow these combinations:
- Emscripten EH + Emscripten SjLj: the current default in `emcc`
- Wasm EH + Emscripten SjLj:
  This is allowed, but only as an interim step in which we are testing
  Wasm EH but not yet have a working implementation of Wasm SjLj. This
  will error out (D107687) in compile time if `setjmp` is called in a
  function in which Wasm exception is used.
- Wasm EH + Wasm SjLj:
  This will be the default mode later when using Wasm EH. Currently Wasm
  SjLj implementation doesn't exist, so it doesn't work.
- Emscripten EH + Wasm SjLj will not work.

This CL moves these error checking routines to
`WebAssemblyPassConfig::addIRPasses`. Not sure if this is an ideal place
to do this, but I couldn't find elsewhere. Currently some checking is
done within LowerEmscriptenEHSjLj, but these checks only run if
LowerEmscriptenEHSjLj runs so it may not run when Wasm EH is used. This
moves that to `addIRPasses` and adds some more checks.

Currently LowerEmscriptenEHSjLj pass is responsible for Emscripten EH
and Emscripten SjLj. Wasm EH transformations are done in multiple
places, including WasmEHPrepare, LateEHPrepare, and CFGStackify. But in
the followup CL, LowerEmscriptenEHSjLj pass will be also responsible for
a part of Wasm SjLj transformation, because WasmSjLj will also be using
several Emscripten library functions, and we will be sharing more than
half of the transformation to do that between Emscripten SjLj and Wasm
SjLj.

Currently we have `-enable-emscripten-cxx-exceptions` and
`-enable-emscripten-sjlj` but these only work for `llc`, because for
`llc` we feed these options to the pass but when we run the pass using
`opt` the pass will be created with no options and the default options
will be used, which turns both Emscripten EH and Emscripten SjLj on.

Now we have one more SjLj option to care for, LowerEmscriptenEHSjLj pass
needs a finer way to control these options. This CL removes those
default parameters and make LowerEmscriptenEHSjLj pass read directly
from command line options specified. So if we only run
`opt -wasm-lower-em-ehsjlj`, currently both Emscripten EH and Emscripten
SjLj will run, but with this CL, none will run unless we additionally
pass `-enable-emscripten-cxx-exceptions` or `-enable-emscripten-sjlj`,
or both. This does not affect users; this only affects our `opt` tests
because `emcc` will not call either `opt` or `llc`. As a result of this,
our existing Emscripten EH/SjLj tests gained one or both of those
options in their `RUN` lines.

Reviewed By: dschuff

Differential Revision: https://reviews.llvm.org/D107685
2021-08-24 17:54:39 -07:00

674 lines
25 KiB
C++

//===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains a printer that converts from our internal
/// representation of machine-dependent LLVM code to the WebAssembly assembly
/// language.
///
//===----------------------------------------------------------------------===//
#include "WebAssemblyAsmPrinter.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
#include "TargetInfo/WebAssemblyTargetInfo.h"
#include "Utils/WebAssemblyTypeUtilities.h"
#include "Utils/WebAssemblyUtilities.h"
#include "WebAssembly.h"
#include "WebAssemblyMCInstLower.h"
#include "WebAssemblyMachineFunctionInfo.h"
#include "WebAssemblyRegisterInfo.h"
#include "WebAssemblyRuntimeLibcallSignatures.h"
#include "WebAssemblyTargetMachine.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/CodeGen/Analysis.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Metadata.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSectionWasm.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolWasm.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "asm-printer"
extern cl::opt<bool> WasmKeepRegisters;
extern cl::opt<bool> WasmEnableEmEH;
extern cl::opt<bool> WasmEnableEmSjLj;
//===----------------------------------------------------------------------===//
// Helpers.
//===----------------------------------------------------------------------===//
MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64})
if (TRI->isTypeLegalForClass(*TRC, T))
return T;
LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
llvm_unreachable("Unknown register type");
return MVT::Other;
}
std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
Register RegNo = MO.getReg();
assert(Register::isVirtualRegister(RegNo) &&
"Unlowered physical register encountered during assembly printing");
assert(!MFI->isVRegStackified(RegNo));
unsigned WAReg = MFI->getWAReg(RegNo);
assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
return '$' + utostr(WAReg);
}
WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
return static_cast<WebAssemblyTargetStreamer *>(TS);
}
// Emscripten exception handling helpers
//
// This converts invoke names generated by LowerEmscriptenEHSjLj to real names
// that are expected by JavaScript glue code. The invoke names generated by
// Emscripten JS glue code are based on their argument and return types; for
// example, for a function that takes an i32 and returns nothing, it is
// 'invoke_vi'. But the format of invoke generated by LowerEmscriptenEHSjLj pass
// contains a mangled string generated from their IR types, for example,
// "__invoke_void_%struct.mystruct*_int", because final wasm types are not
// available in the IR pass. So we convert those names to the form that
// Emscripten JS code expects.
//
// Refer to LowerEmscriptenEHSjLj pass for more details.
// Returns true if the given function name is an invoke name generated by
// LowerEmscriptenEHSjLj pass.
static bool isEmscriptenInvokeName(StringRef Name) {
if (Name.front() == '"' && Name.back() == '"')
Name = Name.substr(1, Name.size() - 2);
return Name.startswith("__invoke_");
}
// Returns a character that represents the given wasm value type in invoke
// signatures.
static char getInvokeSig(wasm::ValType VT) {
switch (VT) {
case wasm::ValType::I32:
return 'i';
case wasm::ValType::I64:
return 'j';
case wasm::ValType::F32:
return 'f';
case wasm::ValType::F64:
return 'd';
case wasm::ValType::V128:
return 'V';
case wasm::ValType::FUNCREF:
return 'F';
case wasm::ValType::EXTERNREF:
return 'X';
}
llvm_unreachable("Unhandled wasm::ValType enum");
}
// Given the wasm signature, generate the invoke name in the format JS glue code
// expects.
static std::string getEmscriptenInvokeSymbolName(wasm::WasmSignature *Sig) {
assert(Sig->Returns.size() <= 1);
std::string Ret = "invoke_";
if (!Sig->Returns.empty())
for (auto VT : Sig->Returns)
Ret += getInvokeSig(VT);
else
Ret += 'v';
// Invokes' first argument is a pointer to the original function, so skip it
for (unsigned I = 1, E = Sig->Params.size(); I < E; I++)
Ret += getInvokeSig(Sig->Params[I]);
return Ret;
}
//===----------------------------------------------------------------------===//
// WebAssemblyAsmPrinter Implementation.
//===----------------------------------------------------------------------===//
MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction(
const Function *F, bool EnableEmEH, wasm::WasmSignature *Sig,
bool &InvokeDetected) {
MCSymbolWasm *WasmSym = nullptr;
if (EnableEmEH && isEmscriptenInvokeName(F->getName())) {
assert(Sig);
InvokeDetected = true;
if (Sig->Returns.size() > 1) {
std::string Msg =
"Emscripten EH/SjLj does not support multivalue returns: " +
std::string(F->getName()) + ": " +
WebAssembly::signatureToString(Sig);
report_fatal_error(Msg);
}
WasmSym = cast<MCSymbolWasm>(
GetExternalSymbolSymbol(getEmscriptenInvokeSymbolName(Sig)));
} else {
WasmSym = cast<MCSymbolWasm>(getSymbol(F));
}
return WasmSym;
}
void WebAssemblyAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
if (!WebAssembly::isWasmVarAddressSpace(GV->getAddressSpace())) {
AsmPrinter::emitGlobalVariable(GV);
return;
}
assert(!GV->isThreadLocal());
MCSymbolWasm *Sym = cast<MCSymbolWasm>(getSymbol(GV));
if (!Sym->getType()) {
const WebAssemblyTargetLowering &TLI = *Subtarget->getTargetLowering();
SmallVector<EVT, 1> VTs;
ComputeValueVTs(TLI, GV->getParent()->getDataLayout(), GV->getValueType(),
VTs);
if (VTs.size() != 1 ||
TLI.getNumRegisters(GV->getParent()->getContext(), VTs[0]) != 1)
report_fatal_error("Aggregate globals not yet implemented");
MVT VT = TLI.getRegisterType(GV->getParent()->getContext(), VTs[0]);
bool Mutable = true;
wasm::ValType Type = WebAssembly::toValType(VT);
Sym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(Type), Mutable});
}
emitVisibility(Sym, GV->getVisibility(), !GV->isDeclaration());
if (GV->hasInitializer()) {
assert(getSymbolPreferLocal(*GV) == Sym);
emitLinkage(GV, Sym);
getTargetStreamer()->emitGlobalType(Sym);
OutStreamer->emitLabel(Sym);
// TODO: Actually emit the initializer value. Otherwise the global has the
// default value for its type (0, ref.null, etc).
OutStreamer->AddBlankLine();
}
}
MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) {
auto *WasmSym = cast<MCSymbolWasm>(GetExternalSymbolSymbol(Name));
// May be called multiple times, so early out.
if (WasmSym->getType().hasValue())
return WasmSym;
const WebAssemblySubtarget &Subtarget = getSubtarget();
// Except for certain known symbols, all symbols used by CodeGen are
// functions. It's OK to hardcode knowledge of specific symbols here; this
// method is precisely there for fetching the signatures of known
// Clang-provided symbols.
if (Name == "__stack_pointer" || Name == "__tls_base" ||
Name == "__memory_base" || Name == "__table_base" ||
Name == "__tls_size" || Name == "__tls_align") {
bool Mutable =
Name == "__stack_pointer" || Name == "__tls_base";
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
WasmSym->setGlobalType(wasm::WasmGlobalType{
uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
: wasm::WASM_TYPE_I32),
Mutable});
return WasmSym;
}
SmallVector<wasm::ValType, 4> Returns;
SmallVector<wasm::ValType, 4> Params;
if (Name == "__cpp_exception") {
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
// We can't confirm its signature index for now because there can be
// imported exceptions. Set it to be 0 for now.
WasmSym->setTagType(
{wasm::WASM_TAG_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
// We may have multiple C++ compilation units to be linked together, each of
// which defines the exception symbol. To resolve them, we declare them as
// weak.
WasmSym->setWeak(true);
WasmSym->setExternal(true);
// All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
// (for wasm64) param type and void return type. The reaon is, all C++
// exception values are pointers, and to share the type section with
// functions, exceptions are assumed to have void return type.
Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
: wasm::ValType::I32);
} else { // Function symbols
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
getLibcallSignature(Subtarget, Name, Returns, Params);
}
auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
std::move(Params));
WasmSym->setSignature(Signature.get());
addSignature(std::move(Signature));
return WasmSym;
}
void WebAssemblyAsmPrinter::emitExternalDecls(const Module &M) {
if (signaturesEmitted)
return;
signaturesEmitted = true;
// Normally symbols for globals get discovered as the MI gets lowered,
// but we need to know about them ahead of time.
MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo<MachineModuleInfoWasm>();
for (const auto &Name : MMIW.MachineSymbolsUsed) {
getOrCreateWasmSymbol(Name.getKey());
}
for (auto &It : OutContext.getSymbols()) {
// Emit .globaltype, .tagtype, or .tabletype declarations.
auto Sym = cast<MCSymbolWasm>(It.getValue());
if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
// .globaltype already handled by emitGlobalVariable for defined
// variables; here we make sure the types of external wasm globals get
// written to the file.
if (Sym->isUndefined())
getTargetStreamer()->emitGlobalType(Sym);
} else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_TAG)
getTargetStreamer()->emitTagType(Sym);
else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_TABLE)
getTargetStreamer()->emitTableType(Sym);
}
DenseSet<MCSymbol *> InvokeSymbols;
for (const auto &F : M) {
if (F.isIntrinsic())
continue;
// Emit function type info for all undefined functions
if (F.isDeclarationForLinker()) {
SmallVector<MVT, 4> Results;
SmallVector<MVT, 4> Params;
computeSignatureVTs(F.getFunctionType(), &F, F, TM, Params, Results);
// At this point these MCSymbols may or may not have been created already
// and thus also contain a signature, but we need to get the signature
// anyway here in case it is an invoke that has not yet been created. We
// will discard it later if it turns out not to be necessary.
auto Signature = signatureFromMVTs(Results, Params);
bool InvokeDetected = false;
auto *Sym = getMCSymbolForFunction(&F, WasmEnableEmEH || WasmEnableEmSjLj,
Signature.get(), InvokeDetected);
// Multiple functions can be mapped to the same invoke symbol. For
// example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32'
// are both mapped to '__invoke_vi'. We keep them in a set once we emit an
// Emscripten EH symbol so we don't emit the same symbol twice.
if (InvokeDetected && !InvokeSymbols.insert(Sym).second)
continue;
Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
if (!Sym->getSignature()) {
Sym->setSignature(Signature.get());
addSignature(std::move(Signature));
} else {
// This symbol has already been created and had a signature. Discard it.
Signature.reset();
}
getTargetStreamer()->emitFunctionType(Sym);
if (F.hasFnAttribute("wasm-import-module")) {
StringRef Name =
F.getFnAttribute("wasm-import-module").getValueAsString();
Sym->setImportModule(storeName(Name));
getTargetStreamer()->emitImportModule(Sym, Name);
}
if (F.hasFnAttribute("wasm-import-name")) {
// If this is a converted Emscripten EH/SjLj symbol, we shouldn't use
// the original function name but the converted symbol name.
StringRef Name =
InvokeDetected
? Sym->getName()
: F.getFnAttribute("wasm-import-name").getValueAsString();
Sym->setImportName(storeName(Name));
getTargetStreamer()->emitImportName(Sym, Name);
}
}
if (F.hasFnAttribute("wasm-export-name")) {
auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString();
Sym->setExportName(storeName(Name));
getTargetStreamer()->emitExportName(Sym, Name);
}
}
}
void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) {
emitExternalDecls(M);
// When a function's address is taken, a TABLE_INDEX relocation is emitted
// against the function symbol at the use site. However the relocation
// doesn't explicitly refer to the table. In the future we may want to
// define a new kind of reloc against both the function and the table, so
// that the linker can see that the function symbol keeps the table alive,
// but for now manually mark the table as live.
for (const auto &F : M) {
if (!F.isIntrinsic() && F.hasAddressTaken()) {
MCSymbolWasm *FunctionTable =
WebAssembly::getOrCreateFunctionTableSymbol(OutContext, Subtarget);
OutStreamer->emitSymbolAttribute(FunctionTable, MCSA_NoDeadStrip);
break;
}
}
for (const auto &G : M.globals()) {
if (!G.hasInitializer() && G.hasExternalLinkage() &&
!WebAssembly::isWasmVarAddressSpace(G.getAddressSpace()) &&
G.getValueType()->isSized()) {
uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
OutStreamer->emitELFSize(getSymbol(&G),
MCConstantExpr::create(Size, OutContext));
}
}
if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
for (const Metadata *MD : Named->operands()) {
const auto *Tuple = dyn_cast<MDTuple>(MD);
if (!Tuple || Tuple->getNumOperands() != 2)
continue;
const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
if (!Name || !Contents)
continue;
OutStreamer->PushSection();
std::string SectionName = (".custom_section." + Name->getString()).str();
MCSectionWasm *MySection =
OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
OutStreamer->SwitchSection(MySection);
OutStreamer->emitBytes(Contents->getString());
OutStreamer->PopSection();
}
}
EmitProducerInfo(M);
EmitTargetFeatures(M);
}
void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages;
if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) {
llvm::SmallSet<StringRef, 4> SeenLanguages;
for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) {
const auto *CU = cast<DICompileUnit>(Debug->getOperand(I));
StringRef Language = dwarf::LanguageString(CU->getSourceLanguage());
Language.consume_front("DW_LANG_");
if (SeenLanguages.insert(Language).second)
Languages.emplace_back(Language.str(), "");
}
}
llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools;
if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
llvm::SmallSet<StringRef, 4> SeenTools;
for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) {
const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0));
std::pair<StringRef, StringRef> Field = S->getString().split("version");
StringRef Name = Field.first.trim();
StringRef Version = Field.second.trim();
if (SeenTools.insert(Name).second)
Tools.emplace_back(Name.str(), Version.str());
}
}
int FieldCount = int(!Languages.empty()) + int(!Tools.empty());
if (FieldCount != 0) {
MCSectionWasm *Producers = OutContext.getWasmSection(
".custom_section.producers", SectionKind::getMetadata());
OutStreamer->PushSection();
OutStreamer->SwitchSection(Producers);
OutStreamer->emitULEB128IntValue(FieldCount);
for (auto &Producers : {std::make_pair("language", &Languages),
std::make_pair("processed-by", &Tools)}) {
if (Producers.second->empty())
continue;
OutStreamer->emitULEB128IntValue(strlen(Producers.first));
OutStreamer->emitBytes(Producers.first);
OutStreamer->emitULEB128IntValue(Producers.second->size());
for (auto &Producer : *Producers.second) {
OutStreamer->emitULEB128IntValue(Producer.first.size());
OutStreamer->emitBytes(Producer.first);
OutStreamer->emitULEB128IntValue(Producer.second.size());
OutStreamer->emitBytes(Producer.second);
}
}
OutStreamer->PopSection();
}
}
void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
struct FeatureEntry {
uint8_t Prefix;
std::string Name;
};
// Read target features and linkage policies from module metadata
SmallVector<FeatureEntry, 4> EmittedFeatures;
auto EmitFeature = [&](std::string Feature) {
std::string MDKey = (StringRef("wasm-feature-") + Feature).str();
Metadata *Policy = M.getModuleFlag(MDKey);
if (Policy == nullptr)
return;
FeatureEntry Entry;
Entry.Prefix = 0;
Entry.Name = Feature;
if (auto *MD = cast<ConstantAsMetadata>(Policy))
if (auto *I = cast<ConstantInt>(MD->getValue()))
Entry.Prefix = I->getZExtValue();
// Silently ignore invalid metadata
if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED &&
Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED &&
Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED)
return;
EmittedFeatures.push_back(Entry);
};
for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
EmitFeature(KV.Key);
}
// This pseudo-feature tells the linker whether shared memory would be safe
EmitFeature("shared-mem");
if (EmittedFeatures.size() == 0)
return;
// Emit features and linkage policies into the "target_features" section
MCSectionWasm *FeaturesSection = OutContext.getWasmSection(
".custom_section.target_features", SectionKind::getMetadata());
OutStreamer->PushSection();
OutStreamer->SwitchSection(FeaturesSection);
OutStreamer->emitULEB128IntValue(EmittedFeatures.size());
for (auto &F : EmittedFeatures) {
OutStreamer->emitIntValue(F.Prefix, 1);
OutStreamer->emitULEB128IntValue(F.Name.size());
OutStreamer->emitBytes(F.Name);
}
OutStreamer->PopSection();
}
void WebAssemblyAsmPrinter::emitConstantPool() {
assert(MF->getConstantPool()->getConstants().empty() &&
"WebAssembly disables constant pools");
}
void WebAssemblyAsmPrinter::emitJumpTableInfo() {
// Nothing to do; jump tables are incorporated into the instruction stream.
}
void WebAssemblyAsmPrinter::emitLinkage(const GlobalValue *GV, MCSymbol *Sym)
const {
AsmPrinter::emitLinkage(GV, Sym);
// This gets called before the function label and type are emitted.
// We use it to emit signatures of external functions.
// FIXME casts!
const_cast<WebAssemblyAsmPrinter *>(this)
->emitExternalDecls(*MMI->getModule());
}
void WebAssemblyAsmPrinter::emitFunctionBodyStart() {
const Function &F = MF->getFunction();
SmallVector<MVT, 1> ResultVTs;
SmallVector<MVT, 4> ParamVTs;
computeSignatureVTs(F.getFunctionType(), &F, F, TM, ParamVTs, ResultVTs);
auto Signature = signatureFromMVTs(ResultVTs, ParamVTs);
auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym);
WasmSym->setSignature(Signature.get());
addSignature(std::move(Signature));
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
getTargetStreamer()->emitFunctionType(WasmSym);
// Emit the function index.
if (MDNode *Idx = F.getMetadata("wasm.index")) {
assert(Idx->getNumOperands() == 1);
getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
}
SmallVector<wasm::ValType, 16> Locals;
valTypesFromMVTs(MFI->getLocals(), Locals);
getTargetStreamer()->emitLocal(Locals);
AsmPrinter::emitFunctionBodyStart();
}
void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) {
LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
switch (MI->getOpcode()) {
case WebAssembly::ARGUMENT_i32:
case WebAssembly::ARGUMENT_i32_S:
case WebAssembly::ARGUMENT_i64:
case WebAssembly::ARGUMENT_i64_S:
case WebAssembly::ARGUMENT_f32:
case WebAssembly::ARGUMENT_f32_S:
case WebAssembly::ARGUMENT_f64:
case WebAssembly::ARGUMENT_f64_S:
case WebAssembly::ARGUMENT_v16i8:
case WebAssembly::ARGUMENT_v16i8_S:
case WebAssembly::ARGUMENT_v8i16:
case WebAssembly::ARGUMENT_v8i16_S:
case WebAssembly::ARGUMENT_v4i32:
case WebAssembly::ARGUMENT_v4i32_S:
case WebAssembly::ARGUMENT_v2i64:
case WebAssembly::ARGUMENT_v2i64_S:
case WebAssembly::ARGUMENT_v4f32:
case WebAssembly::ARGUMENT_v4f32_S:
case WebAssembly::ARGUMENT_v2f64:
case WebAssembly::ARGUMENT_v2f64_S:
// These represent values which are live into the function entry, so there's
// no instruction to emit.
break;
case WebAssembly::FALLTHROUGH_RETURN: {
// These instructions represent the implicit return at the end of a
// function body.
if (isVerbose()) {
OutStreamer->AddComment("fallthrough-return");
OutStreamer->AddBlankLine();
}
break;
}
case WebAssembly::COMPILER_FENCE:
// This is a compiler barrier that prevents instruction reordering during
// backend compilation, and should not be emitted.
break;
default: {
WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
MCInst TmpInst;
MCInstLowering.lower(MI, TmpInst);
EmitToStreamer(*OutStreamer, TmpInst);
break;
}
}
}
bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
unsigned OpNo,
const char *ExtraCode,
raw_ostream &OS) {
// First try the generic code, which knows about modifiers like 'c' and 'n'.
if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
return false;
if (!ExtraCode) {
const MachineOperand &MO = MI->getOperand(OpNo);
switch (MO.getType()) {
case MachineOperand::MO_Immediate:
OS << MO.getImm();
return false;
case MachineOperand::MO_Register:
// FIXME: only opcode that still contains registers, as required by
// MachineInstr::getDebugVariable().
assert(MI->getOpcode() == WebAssembly::INLINEASM);
OS << regToString(MO);
return false;
case MachineOperand::MO_GlobalAddress:
PrintSymbolOperand(MO, OS);
return false;
case MachineOperand::MO_ExternalSymbol:
GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
printOffset(MO.getOffset(), OS);
return false;
case MachineOperand::MO_MachineBasicBlock:
MO.getMBB()->getSymbol()->print(OS, MAI);
return false;
default:
break;
}
}
return true;
}
bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
unsigned OpNo,
const char *ExtraCode,
raw_ostream &OS) {
// The current approach to inline asm is that "r" constraints are expressed
// as local indices, rather than values on the operand stack. This simplifies
// using "r" as it eliminates the need to push and pop the values in a
// particular order, however it also makes it impossible to have an "m"
// constraint. So we don't support it.
return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
}
// Force static initialization.
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmPrinter() {
RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
}