10b57cec5SDimitry Andric //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// This file contains a printer that converts from our internal 110b57cec5SDimitry Andric /// representation of machine-dependent LLVM code to the WebAssembly assembly 120b57cec5SDimitry Andric /// language. 130b57cec5SDimitry Andric /// 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "WebAssemblyAsmPrinter.h" 170b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyInstPrinter.h" 180b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 190b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyTargetStreamer.h" 200b57cec5SDimitry Andric #include "TargetInfo/WebAssemblyTargetInfo.h" 210b57cec5SDimitry Andric #include "WebAssembly.h" 220b57cec5SDimitry Andric #include "WebAssemblyMCInstLower.h" 230b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h" 240b57cec5SDimitry Andric #include "WebAssemblyRegisterInfo.h" 250b57cec5SDimitry Andric #include "WebAssemblyTargetMachine.h" 260b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 270b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 280b57cec5SDimitry Andric #include "llvm/BinaryFormat/Wasm.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h" 320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h" 340b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 350b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 360b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 370b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 380b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 390b57cec5SDimitry Andric #include "llvm/MC/MCSectionWasm.h" 400b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 410b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 420b57cec5SDimitry Andric #include "llvm/MC/MCSymbolWasm.h" 430b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 440b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h" 450b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric using namespace llvm; 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric #define DEBUG_TYPE "asm-printer" 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric extern cl::opt<bool> WasmKeepRegisters; 52*e8d8bef9SDimitry Andric extern cl::opt<bool> EnableEmException; 53*e8d8bef9SDimitry Andric extern cl::opt<bool> EnableEmSjLj; 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 560b57cec5SDimitry Andric // Helpers. 570b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const { 600b57cec5SDimitry Andric const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo(); 610b57cec5SDimitry Andric const TargetRegisterClass *TRC = MRI->getRegClass(RegNo); 620b57cec5SDimitry Andric for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16, 630b57cec5SDimitry Andric MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64}) 640b57cec5SDimitry Andric if (TRI->isTypeLegalForClass(*TRC, T)) 650b57cec5SDimitry Andric return T; 660b57cec5SDimitry Andric LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo); 670b57cec5SDimitry Andric llvm_unreachable("Unknown register type"); 680b57cec5SDimitry Andric return MVT::Other; 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) { 728bcb0991SDimitry Andric Register RegNo = MO.getReg(); 738bcb0991SDimitry Andric assert(Register::isVirtualRegister(RegNo) && 740b57cec5SDimitry Andric "Unlowered physical register encountered during assembly printing"); 750b57cec5SDimitry Andric assert(!MFI->isVRegStackified(RegNo)); 760b57cec5SDimitry Andric unsigned WAReg = MFI->getWAReg(RegNo); 770b57cec5SDimitry Andric assert(WAReg != WebAssemblyFunctionInfo::UnusedReg); 780b57cec5SDimitry Andric return '$' + utostr(WAReg); 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() { 820b57cec5SDimitry Andric MCTargetStreamer *TS = OutStreamer->getTargetStreamer(); 830b57cec5SDimitry Andric return static_cast<WebAssemblyTargetStreamer *>(TS); 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 86*e8d8bef9SDimitry Andric // Emscripten exception handling helpers 87*e8d8bef9SDimitry Andric // 88*e8d8bef9SDimitry Andric // This converts invoke names generated by LowerEmscriptenEHSjLj to real names 89*e8d8bef9SDimitry Andric // that are expected by JavaScript glue code. The invoke names generated by 90*e8d8bef9SDimitry Andric // Emscripten JS glue code are based on their argument and return types; for 91*e8d8bef9SDimitry Andric // example, for a function that takes an i32 and returns nothing, it is 92*e8d8bef9SDimitry Andric // 'invoke_vi'. But the format of invoke generated by LowerEmscriptenEHSjLj pass 93*e8d8bef9SDimitry Andric // contains a mangled string generated from their IR types, for example, 94*e8d8bef9SDimitry Andric // "__invoke_void_%struct.mystruct*_int", because final wasm types are not 95*e8d8bef9SDimitry Andric // available in the IR pass. So we convert those names to the form that 96*e8d8bef9SDimitry Andric // Emscripten JS code expects. 97*e8d8bef9SDimitry Andric // 98*e8d8bef9SDimitry Andric // Refer to LowerEmscriptenEHSjLj pass for more details. 99*e8d8bef9SDimitry Andric 100*e8d8bef9SDimitry Andric // Returns true if the given function name is an invoke name generated by 101*e8d8bef9SDimitry Andric // LowerEmscriptenEHSjLj pass. 102*e8d8bef9SDimitry Andric static bool isEmscriptenInvokeName(StringRef Name) { 103*e8d8bef9SDimitry Andric if (Name.front() == '"' && Name.back() == '"') 104*e8d8bef9SDimitry Andric Name = Name.substr(1, Name.size() - 2); 105*e8d8bef9SDimitry Andric return Name.startswith("__invoke_"); 106*e8d8bef9SDimitry Andric } 107*e8d8bef9SDimitry Andric 108*e8d8bef9SDimitry Andric // Returns a character that represents the given wasm value type in invoke 109*e8d8bef9SDimitry Andric // signatures. 110*e8d8bef9SDimitry Andric static char getInvokeSig(wasm::ValType VT) { 111*e8d8bef9SDimitry Andric switch (VT) { 112*e8d8bef9SDimitry Andric case wasm::ValType::I32: 113*e8d8bef9SDimitry Andric return 'i'; 114*e8d8bef9SDimitry Andric case wasm::ValType::I64: 115*e8d8bef9SDimitry Andric return 'j'; 116*e8d8bef9SDimitry Andric case wasm::ValType::F32: 117*e8d8bef9SDimitry Andric return 'f'; 118*e8d8bef9SDimitry Andric case wasm::ValType::F64: 119*e8d8bef9SDimitry Andric return 'd'; 120*e8d8bef9SDimitry Andric case wasm::ValType::V128: 121*e8d8bef9SDimitry Andric return 'V'; 122*e8d8bef9SDimitry Andric case wasm::ValType::FUNCREF: 123*e8d8bef9SDimitry Andric return 'F'; 124*e8d8bef9SDimitry Andric case wasm::ValType::EXTERNREF: 125*e8d8bef9SDimitry Andric return 'X'; 126*e8d8bef9SDimitry Andric } 127*e8d8bef9SDimitry Andric llvm_unreachable("Unhandled wasm::ValType enum"); 128*e8d8bef9SDimitry Andric } 129*e8d8bef9SDimitry Andric 130*e8d8bef9SDimitry Andric // Given the wasm signature, generate the invoke name in the format JS glue code 131*e8d8bef9SDimitry Andric // expects. 132*e8d8bef9SDimitry Andric static std::string getEmscriptenInvokeSymbolName(wasm::WasmSignature *Sig) { 133*e8d8bef9SDimitry Andric assert(Sig->Returns.size() <= 1); 134*e8d8bef9SDimitry Andric std::string Ret = "invoke_"; 135*e8d8bef9SDimitry Andric if (!Sig->Returns.empty()) 136*e8d8bef9SDimitry Andric for (auto VT : Sig->Returns) 137*e8d8bef9SDimitry Andric Ret += getInvokeSig(VT); 138*e8d8bef9SDimitry Andric else 139*e8d8bef9SDimitry Andric Ret += 'v'; 140*e8d8bef9SDimitry Andric // Invokes' first argument is a pointer to the original function, so skip it 141*e8d8bef9SDimitry Andric for (unsigned I = 1, E = Sig->Params.size(); I < E; I++) 142*e8d8bef9SDimitry Andric Ret += getInvokeSig(Sig->Params[I]); 143*e8d8bef9SDimitry Andric return Ret; 144*e8d8bef9SDimitry Andric } 145*e8d8bef9SDimitry Andric 1460b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1470b57cec5SDimitry Andric // WebAssemblyAsmPrinter Implementation. 1480b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1490b57cec5SDimitry Andric 150*e8d8bef9SDimitry Andric MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction( 151*e8d8bef9SDimitry Andric const Function *F, bool EnableEmEH, wasm::WasmSignature *Sig, 152*e8d8bef9SDimitry Andric bool &InvokeDetected) { 153*e8d8bef9SDimitry Andric MCSymbolWasm *WasmSym = nullptr; 154*e8d8bef9SDimitry Andric if (EnableEmEH && isEmscriptenInvokeName(F->getName())) { 155*e8d8bef9SDimitry Andric assert(Sig); 156*e8d8bef9SDimitry Andric InvokeDetected = true; 157*e8d8bef9SDimitry Andric if (Sig->Returns.size() > 1) { 158*e8d8bef9SDimitry Andric std::string Msg = 159*e8d8bef9SDimitry Andric "Emscripten EH/SjLj does not support multivalue returns: " + 160*e8d8bef9SDimitry Andric std::string(F->getName()) + ": " + 161*e8d8bef9SDimitry Andric WebAssembly::signatureToString(Sig); 162*e8d8bef9SDimitry Andric report_fatal_error(Msg); 163*e8d8bef9SDimitry Andric } 164*e8d8bef9SDimitry Andric WasmSym = cast<MCSymbolWasm>( 165*e8d8bef9SDimitry Andric GetExternalSymbolSymbol(getEmscriptenInvokeSymbolName(Sig))); 166*e8d8bef9SDimitry Andric } else { 167*e8d8bef9SDimitry Andric WasmSym = cast<MCSymbolWasm>(getSymbol(F)); 168*e8d8bef9SDimitry Andric } 169*e8d8bef9SDimitry Andric return WasmSym; 170*e8d8bef9SDimitry Andric } 171*e8d8bef9SDimitry Andric 1725ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) { 1730b57cec5SDimitry Andric for (auto &It : OutContext.getSymbols()) { 1740b57cec5SDimitry Andric // Emit a .globaltype and .eventtype declaration. 1750b57cec5SDimitry Andric auto Sym = cast<MCSymbolWasm>(It.getValue()); 1760b57cec5SDimitry Andric if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL) 1770b57cec5SDimitry Andric getTargetStreamer()->emitGlobalType(Sym); 1780b57cec5SDimitry Andric else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_EVENT) 1790b57cec5SDimitry Andric getTargetStreamer()->emitEventType(Sym); 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 182*e8d8bef9SDimitry Andric DenseSet<MCSymbol *> InvokeSymbols; 1830b57cec5SDimitry Andric for (const auto &F : M) { 184480093f4SDimitry Andric if (F.isIntrinsic()) 185480093f4SDimitry Andric continue; 186480093f4SDimitry Andric 1870b57cec5SDimitry Andric // Emit function type info for all undefined functions 188480093f4SDimitry Andric if (F.isDeclarationForLinker()) { 1890b57cec5SDimitry Andric SmallVector<MVT, 4> Results; 1900b57cec5SDimitry Andric SmallVector<MVT, 4> Params; 1915ffd83dbSDimitry Andric computeSignatureVTs(F.getFunctionType(), &F, F, TM, Params, Results); 192*e8d8bef9SDimitry Andric // At this point these MCSymbols may or may not have been created already 193*e8d8bef9SDimitry Andric // and thus also contain a signature, but we need to get the signature 194*e8d8bef9SDimitry Andric // anyway here in case it is an invoke that has not yet been created. We 195*e8d8bef9SDimitry Andric // will discard it later if it turns out not to be necessary. 196*e8d8bef9SDimitry Andric auto Signature = signatureFromMVTs(Results, Params); 197*e8d8bef9SDimitry Andric bool InvokeDetected = false; 198*e8d8bef9SDimitry Andric auto *Sym = getMCSymbolForFunction(&F, EnableEmException || EnableEmSjLj, 199*e8d8bef9SDimitry Andric Signature.get(), InvokeDetected); 200*e8d8bef9SDimitry Andric 201*e8d8bef9SDimitry Andric // Multiple functions can be mapped to the same invoke symbol. For 202*e8d8bef9SDimitry Andric // example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32' 203*e8d8bef9SDimitry Andric // are both mapped to '__invoke_vi'. We keep them in a set once we emit an 204*e8d8bef9SDimitry Andric // Emscripten EH symbol so we don't emit the same symbol twice. 205*e8d8bef9SDimitry Andric if (InvokeDetected && !InvokeSymbols.insert(Sym).second) 206*e8d8bef9SDimitry Andric continue; 207*e8d8bef9SDimitry Andric 2080b57cec5SDimitry Andric Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); 2090b57cec5SDimitry Andric if (!Sym->getSignature()) { 2100b57cec5SDimitry Andric Sym->setSignature(Signature.get()); 2110b57cec5SDimitry Andric addSignature(std::move(Signature)); 212*e8d8bef9SDimitry Andric } else { 213*e8d8bef9SDimitry Andric // This symbol has already been created and had a signature. Discard it. 214*e8d8bef9SDimitry Andric Signature.reset(); 2150b57cec5SDimitry Andric } 216*e8d8bef9SDimitry Andric 2170b57cec5SDimitry Andric getTargetStreamer()->emitFunctionType(Sym); 2180b57cec5SDimitry Andric 219*e8d8bef9SDimitry Andric if (F.hasFnAttribute("wasm-import-module")) { 2200b57cec5SDimitry Andric StringRef Name = 2210b57cec5SDimitry Andric F.getFnAttribute("wasm-import-module").getValueAsString(); 2225ffd83dbSDimitry Andric Sym->setImportModule(storeName(Name)); 2230b57cec5SDimitry Andric getTargetStreamer()->emitImportModule(Sym, Name); 2240b57cec5SDimitry Andric } 225*e8d8bef9SDimitry Andric if (F.hasFnAttribute("wasm-import-name")) { 226*e8d8bef9SDimitry Andric // If this is a converted Emscripten EH/SjLj symbol, we shouldn't use 227*e8d8bef9SDimitry Andric // the original function name but the converted symbol name. 2280b57cec5SDimitry Andric StringRef Name = 229*e8d8bef9SDimitry Andric InvokeDetected 230*e8d8bef9SDimitry Andric ? Sym->getName() 231*e8d8bef9SDimitry Andric : F.getFnAttribute("wasm-import-name").getValueAsString(); 2325ffd83dbSDimitry Andric Sym->setImportName(storeName(Name)); 2330b57cec5SDimitry Andric getTargetStreamer()->emitImportName(Sym, Name); 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric } 236480093f4SDimitry Andric 237480093f4SDimitry Andric if (F.hasFnAttribute("wasm-export-name")) { 238480093f4SDimitry Andric auto *Sym = cast<MCSymbolWasm>(getSymbol(&F)); 239480093f4SDimitry Andric StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString(); 2405ffd83dbSDimitry Andric Sym->setExportName(storeName(Name)); 241480093f4SDimitry Andric getTargetStreamer()->emitExportName(Sym, Name); 242480093f4SDimitry Andric } 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric for (const auto &G : M.globals()) { 2460b57cec5SDimitry Andric if (!G.hasInitializer() && G.hasExternalLinkage()) { 2470b57cec5SDimitry Andric if (G.getValueType()->isSized()) { 2480b57cec5SDimitry Andric uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType()); 2490b57cec5SDimitry Andric OutStreamer->emitELFSize(getSymbol(&G), 2500b57cec5SDimitry Andric MCConstantExpr::create(Size, OutContext)); 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) { 2560b57cec5SDimitry Andric for (const Metadata *MD : Named->operands()) { 2570b57cec5SDimitry Andric const auto *Tuple = dyn_cast<MDTuple>(MD); 2580b57cec5SDimitry Andric if (!Tuple || Tuple->getNumOperands() != 2) 2590b57cec5SDimitry Andric continue; 2600b57cec5SDimitry Andric const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0)); 2610b57cec5SDimitry Andric const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1)); 2620b57cec5SDimitry Andric if (!Name || !Contents) 2630b57cec5SDimitry Andric continue; 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric OutStreamer->PushSection(); 2660b57cec5SDimitry Andric std::string SectionName = (".custom_section." + Name->getString()).str(); 2670b57cec5SDimitry Andric MCSectionWasm *MySection = 2680b57cec5SDimitry Andric OutContext.getWasmSection(SectionName, SectionKind::getMetadata()); 2690b57cec5SDimitry Andric OutStreamer->SwitchSection(MySection); 2705ffd83dbSDimitry Andric OutStreamer->emitBytes(Contents->getString()); 2710b57cec5SDimitry Andric OutStreamer->PopSection(); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric EmitProducerInfo(M); 2760b57cec5SDimitry Andric EmitTargetFeatures(M); 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) { 2800b57cec5SDimitry Andric llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages; 2810b57cec5SDimitry Andric if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) { 2820b57cec5SDimitry Andric llvm::SmallSet<StringRef, 4> SeenLanguages; 2830b57cec5SDimitry Andric for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) { 2840b57cec5SDimitry Andric const auto *CU = cast<DICompileUnit>(Debug->getOperand(I)); 2850b57cec5SDimitry Andric StringRef Language = dwarf::LanguageString(CU->getSourceLanguage()); 2860b57cec5SDimitry Andric Language.consume_front("DW_LANG_"); 2870b57cec5SDimitry Andric if (SeenLanguages.insert(Language).second) 2880b57cec5SDimitry Andric Languages.emplace_back(Language.str(), ""); 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools; 2930b57cec5SDimitry Andric if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) { 2940b57cec5SDimitry Andric llvm::SmallSet<StringRef, 4> SeenTools; 2950b57cec5SDimitry Andric for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) { 2960b57cec5SDimitry Andric const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0)); 2970b57cec5SDimitry Andric std::pair<StringRef, StringRef> Field = S->getString().split("version"); 2980b57cec5SDimitry Andric StringRef Name = Field.first.trim(); 2990b57cec5SDimitry Andric StringRef Version = Field.second.trim(); 3000b57cec5SDimitry Andric if (SeenTools.insert(Name).second) 3010b57cec5SDimitry Andric Tools.emplace_back(Name.str(), Version.str()); 3020b57cec5SDimitry Andric } 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric int FieldCount = int(!Languages.empty()) + int(!Tools.empty()); 3060b57cec5SDimitry Andric if (FieldCount != 0) { 3070b57cec5SDimitry Andric MCSectionWasm *Producers = OutContext.getWasmSection( 3080b57cec5SDimitry Andric ".custom_section.producers", SectionKind::getMetadata()); 3090b57cec5SDimitry Andric OutStreamer->PushSection(); 3100b57cec5SDimitry Andric OutStreamer->SwitchSection(Producers); 3115ffd83dbSDimitry Andric OutStreamer->emitULEB128IntValue(FieldCount); 3120b57cec5SDimitry Andric for (auto &Producers : {std::make_pair("language", &Languages), 3130b57cec5SDimitry Andric std::make_pair("processed-by", &Tools)}) { 3140b57cec5SDimitry Andric if (Producers.second->empty()) 3150b57cec5SDimitry Andric continue; 3165ffd83dbSDimitry Andric OutStreamer->emitULEB128IntValue(strlen(Producers.first)); 3175ffd83dbSDimitry Andric OutStreamer->emitBytes(Producers.first); 3185ffd83dbSDimitry Andric OutStreamer->emitULEB128IntValue(Producers.second->size()); 3190b57cec5SDimitry Andric for (auto &Producer : *Producers.second) { 3205ffd83dbSDimitry Andric OutStreamer->emitULEB128IntValue(Producer.first.size()); 3215ffd83dbSDimitry Andric OutStreamer->emitBytes(Producer.first); 3225ffd83dbSDimitry Andric OutStreamer->emitULEB128IntValue(Producer.second.size()); 3235ffd83dbSDimitry Andric OutStreamer->emitBytes(Producer.second); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric OutStreamer->PopSection(); 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric } 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) { 3310b57cec5SDimitry Andric struct FeatureEntry { 3320b57cec5SDimitry Andric uint8_t Prefix; 3335ffd83dbSDimitry Andric std::string Name; 3340b57cec5SDimitry Andric }; 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // Read target features and linkage policies from module metadata 3370b57cec5SDimitry Andric SmallVector<FeatureEntry, 4> EmittedFeatures; 3385ffd83dbSDimitry Andric auto EmitFeature = [&](std::string Feature) { 3395ffd83dbSDimitry Andric std::string MDKey = (StringRef("wasm-feature-") + Feature).str(); 3400b57cec5SDimitry Andric Metadata *Policy = M.getModuleFlag(MDKey); 3410b57cec5SDimitry Andric if (Policy == nullptr) 3425ffd83dbSDimitry Andric return; 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric FeatureEntry Entry; 3450b57cec5SDimitry Andric Entry.Prefix = 0; 3465ffd83dbSDimitry Andric Entry.Name = Feature; 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric if (auto *MD = cast<ConstantAsMetadata>(Policy)) 3490b57cec5SDimitry Andric if (auto *I = cast<ConstantInt>(MD->getValue())) 3500b57cec5SDimitry Andric Entry.Prefix = I->getZExtValue(); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric // Silently ignore invalid metadata 3530b57cec5SDimitry Andric if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED && 3540b57cec5SDimitry Andric Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED && 3550b57cec5SDimitry Andric Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED) 3565ffd83dbSDimitry Andric return; 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric EmittedFeatures.push_back(Entry); 3595ffd83dbSDimitry Andric }; 3605ffd83dbSDimitry Andric 3615ffd83dbSDimitry Andric for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { 3625ffd83dbSDimitry Andric EmitFeature(KV.Key); 3630b57cec5SDimitry Andric } 3645ffd83dbSDimitry Andric // This pseudo-feature tells the linker whether shared memory would be safe 3655ffd83dbSDimitry Andric EmitFeature("shared-mem"); 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric if (EmittedFeatures.size() == 0) 3680b57cec5SDimitry Andric return; 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric // Emit features and linkage policies into the "target_features" section 3710b57cec5SDimitry Andric MCSectionWasm *FeaturesSection = OutContext.getWasmSection( 3720b57cec5SDimitry Andric ".custom_section.target_features", SectionKind::getMetadata()); 3730b57cec5SDimitry Andric OutStreamer->PushSection(); 3740b57cec5SDimitry Andric OutStreamer->SwitchSection(FeaturesSection); 3750b57cec5SDimitry Andric 3765ffd83dbSDimitry Andric OutStreamer->emitULEB128IntValue(EmittedFeatures.size()); 3770b57cec5SDimitry Andric for (auto &F : EmittedFeatures) { 3785ffd83dbSDimitry Andric OutStreamer->emitIntValue(F.Prefix, 1); 3795ffd83dbSDimitry Andric OutStreamer->emitULEB128IntValue(F.Name.size()); 3805ffd83dbSDimitry Andric OutStreamer->emitBytes(F.Name); 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric OutStreamer->PopSection(); 3840b57cec5SDimitry Andric } 3850b57cec5SDimitry Andric 3865ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitConstantPool() { 3870b57cec5SDimitry Andric assert(MF->getConstantPool()->getConstants().empty() && 3880b57cec5SDimitry Andric "WebAssembly disables constant pools"); 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3915ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitJumpTableInfo() { 3920b57cec5SDimitry Andric // Nothing to do; jump tables are incorporated into the instruction stream. 3930b57cec5SDimitry Andric } 3940b57cec5SDimitry Andric 3955ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitFunctionBodyStart() { 3960b57cec5SDimitry Andric const Function &F = MF->getFunction(); 3970b57cec5SDimitry Andric SmallVector<MVT, 1> ResultVTs; 3980b57cec5SDimitry Andric SmallVector<MVT, 4> ParamVTs; 3995ffd83dbSDimitry Andric computeSignatureVTs(F.getFunctionType(), &F, F, TM, ParamVTs, ResultVTs); 4005ffd83dbSDimitry Andric 4010b57cec5SDimitry Andric auto Signature = signatureFromMVTs(ResultVTs, ParamVTs); 4020b57cec5SDimitry Andric auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym); 4030b57cec5SDimitry Andric WasmSym->setSignature(Signature.get()); 4040b57cec5SDimitry Andric addSignature(std::move(Signature)); 4050b57cec5SDimitry Andric WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric getTargetStreamer()->emitFunctionType(WasmSym); 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric // Emit the function index. 4100b57cec5SDimitry Andric if (MDNode *Idx = F.getMetadata("wasm.index")) { 4110b57cec5SDimitry Andric assert(Idx->getNumOperands() == 1); 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant( 4140b57cec5SDimitry Andric cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue())); 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric SmallVector<wasm::ValType, 16> Locals; 4180b57cec5SDimitry Andric valTypesFromMVTs(MFI->getLocals(), Locals); 4190b57cec5SDimitry Andric getTargetStreamer()->emitLocal(Locals); 4200b57cec5SDimitry Andric 4215ffd83dbSDimitry Andric AsmPrinter::emitFunctionBodyStart(); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric 4245ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) { 4250b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n'); 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric switch (MI->getOpcode()) { 4280b57cec5SDimitry Andric case WebAssembly::ARGUMENT_i32: 4290b57cec5SDimitry Andric case WebAssembly::ARGUMENT_i32_S: 4300b57cec5SDimitry Andric case WebAssembly::ARGUMENT_i64: 4310b57cec5SDimitry Andric case WebAssembly::ARGUMENT_i64_S: 4320b57cec5SDimitry Andric case WebAssembly::ARGUMENT_f32: 4330b57cec5SDimitry Andric case WebAssembly::ARGUMENT_f32_S: 4340b57cec5SDimitry Andric case WebAssembly::ARGUMENT_f64: 4350b57cec5SDimitry Andric case WebAssembly::ARGUMENT_f64_S: 4360b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v16i8: 4370b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v16i8_S: 4380b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v8i16: 4390b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v8i16_S: 4400b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v4i32: 4410b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v4i32_S: 4420b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v2i64: 4430b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v2i64_S: 4440b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v4f32: 4450b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v4f32_S: 4460b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v2f64: 4470b57cec5SDimitry Andric case WebAssembly::ARGUMENT_v2f64_S: 4480b57cec5SDimitry Andric // These represent values which are live into the function entry, so there's 4490b57cec5SDimitry Andric // no instruction to emit. 4500b57cec5SDimitry Andric break; 4518bcb0991SDimitry Andric case WebAssembly::FALLTHROUGH_RETURN: { 4520b57cec5SDimitry Andric // These instructions represent the implicit return at the end of a 4538bcb0991SDimitry Andric // function body. 4540b57cec5SDimitry Andric if (isVerbose()) { 4558bcb0991SDimitry Andric OutStreamer->AddComment("fallthrough-return"); 4560b57cec5SDimitry Andric OutStreamer->AddBlankLine(); 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric break; 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric case WebAssembly::COMPILER_FENCE: 4610b57cec5SDimitry Andric // This is a compiler barrier that prevents instruction reordering during 4620b57cec5SDimitry Andric // backend compilation, and should not be emitted. 4630b57cec5SDimitry Andric break; 4640b57cec5SDimitry Andric default: { 4650b57cec5SDimitry Andric WebAssemblyMCInstLower MCInstLowering(OutContext, *this); 4660b57cec5SDimitry Andric MCInst TmpInst; 4670b57cec5SDimitry Andric MCInstLowering.lower(MI, TmpInst); 4680b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst); 4690b57cec5SDimitry Andric break; 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric } 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI, 4750b57cec5SDimitry Andric unsigned OpNo, 4760b57cec5SDimitry Andric const char *ExtraCode, 4770b57cec5SDimitry Andric raw_ostream &OS) { 4780b57cec5SDimitry Andric // First try the generic code, which knows about modifiers like 'c' and 'n'. 4790b57cec5SDimitry Andric if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS)) 4800b57cec5SDimitry Andric return false; 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric if (!ExtraCode) { 4830b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNo); 4840b57cec5SDimitry Andric switch (MO.getType()) { 4850b57cec5SDimitry Andric case MachineOperand::MO_Immediate: 4860b57cec5SDimitry Andric OS << MO.getImm(); 4870b57cec5SDimitry Andric return false; 4880b57cec5SDimitry Andric case MachineOperand::MO_Register: 4890b57cec5SDimitry Andric // FIXME: only opcode that still contains registers, as required by 4900b57cec5SDimitry Andric // MachineInstr::getDebugVariable(). 4910b57cec5SDimitry Andric assert(MI->getOpcode() == WebAssembly::INLINEASM); 4920b57cec5SDimitry Andric OS << regToString(MO); 4930b57cec5SDimitry Andric return false; 4940b57cec5SDimitry Andric case MachineOperand::MO_GlobalAddress: 4950b57cec5SDimitry Andric PrintSymbolOperand(MO, OS); 4960b57cec5SDimitry Andric return false; 4970b57cec5SDimitry Andric case MachineOperand::MO_ExternalSymbol: 4980b57cec5SDimitry Andric GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI); 4990b57cec5SDimitry Andric printOffset(MO.getOffset(), OS); 5000b57cec5SDimitry Andric return false; 5010b57cec5SDimitry Andric case MachineOperand::MO_MachineBasicBlock: 5020b57cec5SDimitry Andric MO.getMBB()->getSymbol()->print(OS, MAI); 5030b57cec5SDimitry Andric return false; 5040b57cec5SDimitry Andric default: 5050b57cec5SDimitry Andric break; 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric return true; 5100b57cec5SDimitry Andric } 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 5130b57cec5SDimitry Andric unsigned OpNo, 5140b57cec5SDimitry Andric const char *ExtraCode, 5150b57cec5SDimitry Andric raw_ostream &OS) { 5160b57cec5SDimitry Andric // The current approach to inline asm is that "r" constraints are expressed 5170b57cec5SDimitry Andric // as local indices, rather than values on the operand stack. This simplifies 5180b57cec5SDimitry Andric // using "r" as it eliminates the need to push and pop the values in a 5190b57cec5SDimitry Andric // particular order, however it also makes it impossible to have an "m" 5200b57cec5SDimitry Andric // constraint. So we don't support it. 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS); 5230b57cec5SDimitry Andric } 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric // Force static initialization. 526480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmPrinter() { 5270b57cec5SDimitry Andric RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32()); 5280b57cec5SDimitry Andric RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64()); 5290b57cec5SDimitry Andric } 530