#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/MC/TargetRegistry.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "asm-printer"
extern cl::opt<bool> WasmKeepRegisters;
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);
}
static bool isEmscriptenInvokeName(StringRef Name) {
if (Name.front() == '"' && Name.back() == '"')
Name = Name.substr(1, Name.size() - 2);
return Name.startswith("__invoke_");
}
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");
}
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';
for (unsigned I = 1, E = Sig->Params.size(); I < E; I++)
Ret += getInvokeSig(Sig->Params[I]);
return Ret;
}
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(Twine(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()) {
SmallVector<MVT, 1> VTs;
Type *GlobalVT = GV->getValueType();
if (Subtarget) {
const WebAssemblyTargetLowering &TLI = *Subtarget->getTargetLowering();
computeLegalValueVTs(TLI, GV->getParent()->getContext(),
GV->getParent()->getDataLayout(), GlobalVT, VTs);
}
WebAssembly::wasmSymbolSetType(Sym, GlobalVT, VTs);
}
emitVisibility(Sym, GV->getVisibility(), !GV->isDeclaration());
emitSymbolType(Sym);
if (GV->hasInitializer()) {
assert(getSymbolPreferLocal(*GV) == Sym);
emitLinkage(GV, Sym);
OutStreamer->emitLabel(Sym);
OutStreamer->addBlankLine();
}
}
MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) {
auto *WasmSym = cast<MCSymbolWasm>(GetExternalSymbolSymbol(Name));
if (WasmSym->getType())
return WasmSym;
const WebAssemblySubtarget &Subtarget = getSubtarget();
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;
}
if (Name.startswith("GCC_except_table")) {
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
return WasmSym;
}
SmallVector<wasm::ValType, 4> Returns;
SmallVector<wasm::ValType, 4> Params;
if (Name == "__cpp_exception" || Name == "__c_longjmp") {
WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
if (!isPositionIndependent())
WasmSym->setWeak(true);
WasmSym->setExternal(true);
wasm::ValType AddrType =
Subtarget.hasAddr64() ? wasm::ValType::I64 : wasm::ValType::I32;
Params.push_back(AddrType);
} else { 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::emitSymbolType(const MCSymbolWasm *Sym) {
Optional<wasm::WasmSymbolType> WasmTy = Sym->getType();
if (!WasmTy)
return;
switch (*WasmTy) {
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
getTargetStreamer()->emitGlobalType(Sym);
break;
case wasm::WASM_SYMBOL_TYPE_TAG:
getTargetStreamer()->emitTagType(Sym);
break;
case wasm::WASM_SYMBOL_TYPE_TABLE:
getTargetStreamer()->emitTableType(Sym);
break;
default:
break; }
}
void WebAssemblyAsmPrinter::emitDecls(const Module &M) {
if (signaturesEmitted)
return;
signaturesEmitted = true;
MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo<MachineModuleInfoWasm>();
for (const auto &Name : MMIW.MachineSymbolsUsed) {
auto *WasmSym = cast<MCSymbolWasm>(getOrCreateWasmSymbol(Name.getKey()));
if (WasmSym->isFunction()) {
getTargetStreamer()->emitFunctionType(WasmSym);
}
}
for (auto &It : OutContext.getSymbols()) {
auto Sym = cast<MCSymbolWasm>(It.getValue());
if (!Sym->isDefined())
emitSymbolType(Sym);
}
DenseSet<MCSymbol *> InvokeSymbols;
for (const auto &F : M) {
if (F.isIntrinsic())
continue;
SmallVector<MVT, 4> Results;
SmallVector<MVT, 4> Params;
computeSignatureVTs(F.getFunctionType(), &F, F, TM, Params, Results);
auto Signature = signatureFromMVTs(Results, Params);
bool InvokeDetected = false;
auto *Sym = getMCSymbolForFunction(
&F, WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj,
Signature.get(), InvokeDetected);
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 {
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")) {
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) {
emitDecls(M);
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;
};
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();
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);
}
EmitFeature("shared-mem");
if (M.getDataLayout().getPointerSize() == 8) {
EmittedFeatures.push_back({wasm::WASM_FEATURE_PREFIX_USED, "memory64"});
}
if (EmittedFeatures.size() == 0)
return;
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() {
emitDecls(*MMI->getModule());
assert(MF->getConstantPool()->getConstants().empty() &&
"WebAssembly disables constant pools");
}
void WebAssemblyAsmPrinter::emitJumpTableInfo() {
}
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);
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');
WebAssembly_MC::verifyInstructionPredicates(MI->getOpcode(),
Subtarget->getFeatureBits());
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:
break;
case WebAssembly::FALLTHROUGH_RETURN: {
if (isVerbose()) {
OutStreamer->AddComment("fallthrough-return");
OutStreamer->addBlankLine();
}
break;
}
case WebAssembly::COMPILER_FENCE:
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) {
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:
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) {
return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
}
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmPrinter() {
RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
}