109467b48Spatrick //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick ///
909467b48Spatrick /// \file
1009467b48Spatrick /// This file contains a printer that converts from our internal
1109467b48Spatrick /// representation of machine-dependent LLVM code to the WebAssembly assembly
1209467b48Spatrick /// language.
1309467b48Spatrick ///
1409467b48Spatrick //===----------------------------------------------------------------------===//
1509467b48Spatrick 
1609467b48Spatrick #include "WebAssemblyAsmPrinter.h"
1709467b48Spatrick #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
1809467b48Spatrick #include "MCTargetDesc/WebAssemblyTargetStreamer.h"
1909467b48Spatrick #include "TargetInfo/WebAssemblyTargetInfo.h"
2073471bf0Spatrick #include "Utils/WebAssemblyTypeUtilities.h"
2173471bf0Spatrick #include "Utils/WebAssemblyUtilities.h"
2209467b48Spatrick #include "WebAssembly.h"
2309467b48Spatrick #include "WebAssemblyMCInstLower.h"
2409467b48Spatrick #include "WebAssemblyMachineFunctionInfo.h"
2509467b48Spatrick #include "WebAssemblyRegisterInfo.h"
2673471bf0Spatrick #include "WebAssemblyRuntimeLibcallSignatures.h"
2709467b48Spatrick #include "WebAssemblyTargetMachine.h"
2809467b48Spatrick #include "llvm/ADT/SmallSet.h"
2909467b48Spatrick #include "llvm/ADT/StringExtras.h"
3009467b48Spatrick #include "llvm/BinaryFormat/Wasm.h"
3109467b48Spatrick #include "llvm/CodeGen/Analysis.h"
3209467b48Spatrick #include "llvm/CodeGen/AsmPrinter.h"
3309467b48Spatrick #include "llvm/CodeGen/MachineConstantPool.h"
3409467b48Spatrick #include "llvm/CodeGen/MachineInstr.h"
3509467b48Spatrick #include "llvm/CodeGen/MachineModuleInfoImpls.h"
3609467b48Spatrick #include "llvm/IR/DataLayout.h"
3709467b48Spatrick #include "llvm/IR/DebugInfoMetadata.h"
3809467b48Spatrick #include "llvm/IR/GlobalVariable.h"
3909467b48Spatrick #include "llvm/IR/Metadata.h"
4009467b48Spatrick #include "llvm/MC/MCContext.h"
4109467b48Spatrick #include "llvm/MC/MCSectionWasm.h"
4209467b48Spatrick #include "llvm/MC/MCStreamer.h"
4309467b48Spatrick #include "llvm/MC/MCSymbol.h"
4409467b48Spatrick #include "llvm/MC/MCSymbolWasm.h"
45*d415bd75Srobert #include "llvm/MC/TargetRegistry.h"
4609467b48Spatrick #include "llvm/Support/Debug.h"
4709467b48Spatrick #include "llvm/Support/raw_ostream.h"
4809467b48Spatrick 
4909467b48Spatrick using namespace llvm;
5009467b48Spatrick 
5109467b48Spatrick #define DEBUG_TYPE "asm-printer"
5209467b48Spatrick 
5309467b48Spatrick extern cl::opt<bool> WasmKeepRegisters;
5409467b48Spatrick 
5509467b48Spatrick //===----------------------------------------------------------------------===//
5609467b48Spatrick // Helpers.
5709467b48Spatrick //===----------------------------------------------------------------------===//
5809467b48Spatrick 
getRegType(unsigned RegNo) const5909467b48Spatrick MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
6009467b48Spatrick   const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
6109467b48Spatrick   const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
6209467b48Spatrick   for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
6309467b48Spatrick                 MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64})
6409467b48Spatrick     if (TRI->isTypeLegalForClass(*TRC, T))
6509467b48Spatrick       return T;
6609467b48Spatrick   LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
6709467b48Spatrick   llvm_unreachable("Unknown register type");
6809467b48Spatrick   return MVT::Other;
6909467b48Spatrick }
7009467b48Spatrick 
regToString(const MachineOperand & MO)7109467b48Spatrick std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
7209467b48Spatrick   Register RegNo = MO.getReg();
73*d415bd75Srobert   assert(RegNo.isVirtual() &&
7409467b48Spatrick          "Unlowered physical register encountered during assembly printing");
7509467b48Spatrick   assert(!MFI->isVRegStackified(RegNo));
7609467b48Spatrick   unsigned WAReg = MFI->getWAReg(RegNo);
7709467b48Spatrick   assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
7809467b48Spatrick   return '$' + utostr(WAReg);
7909467b48Spatrick }
8009467b48Spatrick 
getTargetStreamer()8109467b48Spatrick WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
8209467b48Spatrick   MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
8309467b48Spatrick   return static_cast<WebAssemblyTargetStreamer *>(TS);
8409467b48Spatrick }
8509467b48Spatrick 
8673471bf0Spatrick // Emscripten exception handling helpers
8773471bf0Spatrick //
8873471bf0Spatrick // This converts invoke names generated by LowerEmscriptenEHSjLj to real names
8973471bf0Spatrick // that are expected by JavaScript glue code. The invoke names generated by
9073471bf0Spatrick // Emscripten JS glue code are based on their argument and return types; for
9173471bf0Spatrick // example, for a function that takes an i32 and returns nothing, it is
9273471bf0Spatrick // 'invoke_vi'. But the format of invoke generated by LowerEmscriptenEHSjLj pass
9373471bf0Spatrick // contains a mangled string generated from their IR types, for example,
9473471bf0Spatrick // "__invoke_void_%struct.mystruct*_int", because final wasm types are not
9573471bf0Spatrick // available in the IR pass. So we convert those names to the form that
9673471bf0Spatrick // Emscripten JS code expects.
9773471bf0Spatrick //
9873471bf0Spatrick // Refer to LowerEmscriptenEHSjLj pass for more details.
9973471bf0Spatrick 
10073471bf0Spatrick // Returns true if the given function name is an invoke name generated by
10173471bf0Spatrick // LowerEmscriptenEHSjLj pass.
isEmscriptenInvokeName(StringRef Name)10273471bf0Spatrick static bool isEmscriptenInvokeName(StringRef Name) {
10373471bf0Spatrick   if (Name.front() == '"' && Name.back() == '"')
10473471bf0Spatrick     Name = Name.substr(1, Name.size() - 2);
10573471bf0Spatrick   return Name.startswith("__invoke_");
10673471bf0Spatrick }
10773471bf0Spatrick 
10873471bf0Spatrick // Returns a character that represents the given wasm value type in invoke
10973471bf0Spatrick // signatures.
getInvokeSig(wasm::ValType VT)11073471bf0Spatrick static char getInvokeSig(wasm::ValType VT) {
11173471bf0Spatrick   switch (VT) {
11273471bf0Spatrick   case wasm::ValType::I32:
11373471bf0Spatrick     return 'i';
11473471bf0Spatrick   case wasm::ValType::I64:
11573471bf0Spatrick     return 'j';
11673471bf0Spatrick   case wasm::ValType::F32:
11773471bf0Spatrick     return 'f';
11873471bf0Spatrick   case wasm::ValType::F64:
11973471bf0Spatrick     return 'd';
12073471bf0Spatrick   case wasm::ValType::V128:
12173471bf0Spatrick     return 'V';
12273471bf0Spatrick   case wasm::ValType::FUNCREF:
12373471bf0Spatrick     return 'F';
12473471bf0Spatrick   case wasm::ValType::EXTERNREF:
12573471bf0Spatrick     return 'X';
12673471bf0Spatrick   }
12773471bf0Spatrick   llvm_unreachable("Unhandled wasm::ValType enum");
12873471bf0Spatrick }
12973471bf0Spatrick 
13073471bf0Spatrick // Given the wasm signature, generate the invoke name in the format JS glue code
13173471bf0Spatrick // expects.
getEmscriptenInvokeSymbolName(wasm::WasmSignature * Sig)13273471bf0Spatrick static std::string getEmscriptenInvokeSymbolName(wasm::WasmSignature *Sig) {
13373471bf0Spatrick   assert(Sig->Returns.size() <= 1);
13473471bf0Spatrick   std::string Ret = "invoke_";
13573471bf0Spatrick   if (!Sig->Returns.empty())
13673471bf0Spatrick     for (auto VT : Sig->Returns)
13773471bf0Spatrick       Ret += getInvokeSig(VT);
13873471bf0Spatrick   else
13973471bf0Spatrick     Ret += 'v';
14073471bf0Spatrick   // Invokes' first argument is a pointer to the original function, so skip it
14173471bf0Spatrick   for (unsigned I = 1, E = Sig->Params.size(); I < E; I++)
14273471bf0Spatrick     Ret += getInvokeSig(Sig->Params[I]);
14373471bf0Spatrick   return Ret;
14473471bf0Spatrick }
14573471bf0Spatrick 
14609467b48Spatrick //===----------------------------------------------------------------------===//
14709467b48Spatrick // WebAssemblyAsmPrinter Implementation.
14809467b48Spatrick //===----------------------------------------------------------------------===//
14909467b48Spatrick 
getMCSymbolForFunction(const Function * F,bool EnableEmEH,wasm::WasmSignature * Sig,bool & InvokeDetected)15073471bf0Spatrick MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction(
15173471bf0Spatrick     const Function *F, bool EnableEmEH, wasm::WasmSignature *Sig,
15273471bf0Spatrick     bool &InvokeDetected) {
15373471bf0Spatrick   MCSymbolWasm *WasmSym = nullptr;
15473471bf0Spatrick   if (EnableEmEH && isEmscriptenInvokeName(F->getName())) {
15573471bf0Spatrick     assert(Sig);
15673471bf0Spatrick     InvokeDetected = true;
15773471bf0Spatrick     if (Sig->Returns.size() > 1) {
15873471bf0Spatrick       std::string Msg =
15973471bf0Spatrick           "Emscripten EH/SjLj does not support multivalue returns: " +
16073471bf0Spatrick           std::string(F->getName()) + ": " +
16173471bf0Spatrick           WebAssembly::signatureToString(Sig);
162*d415bd75Srobert       report_fatal_error(Twine(Msg));
16373471bf0Spatrick     }
16473471bf0Spatrick     WasmSym = cast<MCSymbolWasm>(
16573471bf0Spatrick         GetExternalSymbolSymbol(getEmscriptenInvokeSymbolName(Sig)));
16673471bf0Spatrick   } else {
16773471bf0Spatrick     WasmSym = cast<MCSymbolWasm>(getSymbol(F));
16873471bf0Spatrick   }
16973471bf0Spatrick   return WasmSym;
17009467b48Spatrick }
17109467b48Spatrick 
emitGlobalVariable(const GlobalVariable * GV)17273471bf0Spatrick void WebAssemblyAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
17373471bf0Spatrick   if (!WebAssembly::isWasmVarAddressSpace(GV->getAddressSpace())) {
17473471bf0Spatrick     AsmPrinter::emitGlobalVariable(GV);
17573471bf0Spatrick     return;
17673471bf0Spatrick   }
17773471bf0Spatrick 
17873471bf0Spatrick   assert(!GV->isThreadLocal());
17973471bf0Spatrick 
18073471bf0Spatrick   MCSymbolWasm *Sym = cast<MCSymbolWasm>(getSymbol(GV));
18173471bf0Spatrick 
18273471bf0Spatrick   if (!Sym->getType()) {
183*d415bd75Srobert     SmallVector<MVT, 1> VTs;
184*d415bd75Srobert     Type *GlobalVT = GV->getValueType();
185*d415bd75Srobert     if (Subtarget) {
186*d415bd75Srobert       // Subtarget is only set when a function is defined, because
187*d415bd75Srobert       // each function can declare a different subtarget. For example,
188*d415bd75Srobert       // on ARM a compilation unit might have a function on ARM and
189*d415bd75Srobert       // another on Thumb. Therefore only if Subtarget is non-null we
190*d415bd75Srobert       // can actually calculate the legal VTs.
19173471bf0Spatrick       const WebAssemblyTargetLowering &TLI = *Subtarget->getTargetLowering();
192*d415bd75Srobert       computeLegalValueVTs(TLI, GV->getParent()->getContext(),
193*d415bd75Srobert                            GV->getParent()->getDataLayout(), GlobalVT, VTs);
194*d415bd75Srobert     }
195*d415bd75Srobert     WebAssembly::wasmSymbolSetType(Sym, GlobalVT, VTs);
19673471bf0Spatrick   }
19773471bf0Spatrick 
19873471bf0Spatrick   emitVisibility(Sym, GV->getVisibility(), !GV->isDeclaration());
199*d415bd75Srobert   emitSymbolType(Sym);
20073471bf0Spatrick   if (GV->hasInitializer()) {
20173471bf0Spatrick     assert(getSymbolPreferLocal(*GV) == Sym);
20273471bf0Spatrick     emitLinkage(GV, Sym);
20373471bf0Spatrick     OutStreamer->emitLabel(Sym);
20473471bf0Spatrick     // TODO: Actually emit the initializer value.  Otherwise the global has the
20573471bf0Spatrick     // default value for its type (0, ref.null, etc).
206*d415bd75Srobert     OutStreamer->addBlankLine();
20773471bf0Spatrick   }
20873471bf0Spatrick }
20973471bf0Spatrick 
getOrCreateWasmSymbol(StringRef Name)21073471bf0Spatrick MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) {
21173471bf0Spatrick   auto *WasmSym = cast<MCSymbolWasm>(GetExternalSymbolSymbol(Name));
21273471bf0Spatrick 
21373471bf0Spatrick   // May be called multiple times, so early out.
214*d415bd75Srobert   if (WasmSym->getType())
21573471bf0Spatrick     return WasmSym;
21673471bf0Spatrick 
21773471bf0Spatrick   const WebAssemblySubtarget &Subtarget = getSubtarget();
21873471bf0Spatrick 
21973471bf0Spatrick   // Except for certain known symbols, all symbols used by CodeGen are
22073471bf0Spatrick   // functions. It's OK to hardcode knowledge of specific symbols here; this
22173471bf0Spatrick   // method is precisely there for fetching the signatures of known
22273471bf0Spatrick   // Clang-provided symbols.
22373471bf0Spatrick   if (Name == "__stack_pointer" || Name == "__tls_base" ||
22473471bf0Spatrick       Name == "__memory_base" || Name == "__table_base" ||
22573471bf0Spatrick       Name == "__tls_size" || Name == "__tls_align") {
22673471bf0Spatrick     bool Mutable =
22773471bf0Spatrick         Name == "__stack_pointer" || Name == "__tls_base";
22873471bf0Spatrick     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
22973471bf0Spatrick     WasmSym->setGlobalType(wasm::WasmGlobalType{
23073471bf0Spatrick         uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
23173471bf0Spatrick                                       : wasm::WASM_TYPE_I32),
23273471bf0Spatrick         Mutable});
23373471bf0Spatrick     return WasmSym;
23473471bf0Spatrick   }
23573471bf0Spatrick 
236*d415bd75Srobert   if (Name.startswith("GCC_except_table")) {
237*d415bd75Srobert     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
238*d415bd75Srobert     return WasmSym;
239*d415bd75Srobert   }
240*d415bd75Srobert 
24173471bf0Spatrick   SmallVector<wasm::ValType, 4> Returns;
24273471bf0Spatrick   SmallVector<wasm::ValType, 4> Params;
243*d415bd75Srobert   if (Name == "__cpp_exception" || Name == "__c_longjmp") {
24473471bf0Spatrick     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
245*d415bd75Srobert     // In static linking we define tag symbols in WasmException::endModule().
246*d415bd75Srobert     // But we may have multiple objects to be linked together, each of which
247*d415bd75Srobert     // defines the tag symbols. To resolve them, we declare them as weak. In
248*d415bd75Srobert     // dynamic linking we make tag symbols undefined in the backend, define it
249*d415bd75Srobert     // in JS, and feed them to each importing module.
250*d415bd75Srobert     if (!isPositionIndependent())
25173471bf0Spatrick       WasmSym->setWeak(true);
25273471bf0Spatrick     WasmSym->setExternal(true);
25373471bf0Spatrick 
254*d415bd75Srobert     // Currently both C++ exceptions and C longjmps have a single pointer type
255*d415bd75Srobert     // param. For C++ exceptions it is a pointer to an exception object, and for
256*d415bd75Srobert     // C longjmps it is pointer to a struct that contains a setjmp buffer and a
257*d415bd75Srobert     // longjmp return value. We may consider using multiple value parameters for
258*d415bd75Srobert     // longjmps later when multivalue support is ready.
259*d415bd75Srobert     wasm::ValType AddrType =
260*d415bd75Srobert         Subtarget.hasAddr64() ? wasm::ValType::I64 : wasm::ValType::I32;
261*d415bd75Srobert     Params.push_back(AddrType);
26273471bf0Spatrick   } else { // Function symbols
26373471bf0Spatrick     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
26473471bf0Spatrick     getLibcallSignature(Subtarget, Name, Returns, Params);
26573471bf0Spatrick   }
26673471bf0Spatrick   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
26773471bf0Spatrick                                                          std::move(Params));
26873471bf0Spatrick   WasmSym->setSignature(Signature.get());
26973471bf0Spatrick   addSignature(std::move(Signature));
27073471bf0Spatrick 
27173471bf0Spatrick   return WasmSym;
27273471bf0Spatrick }
27373471bf0Spatrick 
emitSymbolType(const MCSymbolWasm * Sym)274*d415bd75Srobert void WebAssemblyAsmPrinter::emitSymbolType(const MCSymbolWasm *Sym) {
275*d415bd75Srobert   std::optional<wasm::WasmSymbolType> WasmTy = Sym->getType();
276*d415bd75Srobert   if (!WasmTy)
277*d415bd75Srobert     return;
278*d415bd75Srobert 
279*d415bd75Srobert   switch (*WasmTy) {
280*d415bd75Srobert   case wasm::WASM_SYMBOL_TYPE_GLOBAL:
281*d415bd75Srobert     getTargetStreamer()->emitGlobalType(Sym);
282*d415bd75Srobert     break;
283*d415bd75Srobert   case wasm::WASM_SYMBOL_TYPE_TAG:
284*d415bd75Srobert     getTargetStreamer()->emitTagType(Sym);
285*d415bd75Srobert     break;
286*d415bd75Srobert   case wasm::WASM_SYMBOL_TYPE_TABLE:
287*d415bd75Srobert     getTargetStreamer()->emitTableType(Sym);
288*d415bd75Srobert     break;
289*d415bd75Srobert   default:
290*d415bd75Srobert     break; // We only handle globals, tags and tables here
291*d415bd75Srobert   }
292*d415bd75Srobert }
293*d415bd75Srobert 
emitDecls(const Module & M)294*d415bd75Srobert void WebAssemblyAsmPrinter::emitDecls(const Module &M) {
29573471bf0Spatrick   if (signaturesEmitted)
29673471bf0Spatrick     return;
29773471bf0Spatrick   signaturesEmitted = true;
29873471bf0Spatrick 
29973471bf0Spatrick   // Normally symbols for globals get discovered as the MI gets lowered,
300*d415bd75Srobert   // but we need to know about them ahead of time. This will however,
301*d415bd75Srobert   // only find symbols that have been used. Unused symbols from globals will
302*d415bd75Srobert   // not be found here.
30373471bf0Spatrick   MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo<MachineModuleInfoWasm>();
30473471bf0Spatrick   for (const auto &Name : MMIW.MachineSymbolsUsed) {
305*d415bd75Srobert     auto *WasmSym = cast<MCSymbolWasm>(getOrCreateWasmSymbol(Name.getKey()));
306*d415bd75Srobert     if (WasmSym->isFunction()) {
307*d415bd75Srobert       // TODO(wvo): is there any case where this overlaps with the call to
308*d415bd75Srobert       // emitFunctionType in the loop below?
309*d415bd75Srobert       getTargetStreamer()->emitFunctionType(WasmSym);
310*d415bd75Srobert     }
31173471bf0Spatrick   }
31273471bf0Spatrick 
31373471bf0Spatrick   for (auto &It : OutContext.getSymbols()) {
314*d415bd75Srobert     // Emit .globaltype, .tagtype, or .tabletype declarations for extern
315*d415bd75Srobert     // declarations, i.e. those that have only been declared (but not defined)
316*d415bd75Srobert     // in the current module
31773471bf0Spatrick     auto Sym = cast<MCSymbolWasm>(It.getValue());
318*d415bd75Srobert     if (!Sym->isDefined())
319*d415bd75Srobert       emitSymbolType(Sym);
32073471bf0Spatrick   }
32173471bf0Spatrick 
32273471bf0Spatrick   DenseSet<MCSymbol *> InvokeSymbols;
32309467b48Spatrick   for (const auto &F : M) {
32409467b48Spatrick     if (F.isIntrinsic())
32509467b48Spatrick       continue;
32609467b48Spatrick 
327*d415bd75Srobert     // Emit function type info for all functions. This will emit duplicate
328*d415bd75Srobert     // information for defined functions (which already have function type
329*d415bd75Srobert     // info emitted alongside their definition), but this is necessary in
330*d415bd75Srobert     // order to enable the single-pass WebAssemblyAsmTypeCheck to succeed.
33109467b48Spatrick     SmallVector<MVT, 4> Results;
33209467b48Spatrick     SmallVector<MVT, 4> Params;
333097a140dSpatrick     computeSignatureVTs(F.getFunctionType(), &F, F, TM, Params, Results);
33473471bf0Spatrick     // At this point these MCSymbols may or may not have been created already
33573471bf0Spatrick     // and thus also contain a signature, but we need to get the signature
33673471bf0Spatrick     // anyway here in case it is an invoke that has not yet been created. We
33773471bf0Spatrick     // will discard it later if it turns out not to be necessary.
33873471bf0Spatrick     auto Signature = signatureFromMVTs(Results, Params);
33973471bf0Spatrick     bool InvokeDetected = false;
340*d415bd75Srobert     auto *Sym = getMCSymbolForFunction(
341*d415bd75Srobert         &F, WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj,
34273471bf0Spatrick         Signature.get(), InvokeDetected);
34373471bf0Spatrick 
34473471bf0Spatrick     // Multiple functions can be mapped to the same invoke symbol. For
34573471bf0Spatrick     // example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32'
34673471bf0Spatrick     // are both mapped to '__invoke_vi'. We keep them in a set once we emit an
34773471bf0Spatrick     // Emscripten EH symbol so we don't emit the same symbol twice.
34873471bf0Spatrick     if (InvokeDetected && !InvokeSymbols.insert(Sym).second)
34973471bf0Spatrick       continue;
35073471bf0Spatrick 
35109467b48Spatrick     Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
35209467b48Spatrick     if (!Sym->getSignature()) {
35309467b48Spatrick       Sym->setSignature(Signature.get());
35409467b48Spatrick       addSignature(std::move(Signature));
35573471bf0Spatrick     } else {
35673471bf0Spatrick       // This symbol has already been created and had a signature. Discard it.
35773471bf0Spatrick       Signature.reset();
35809467b48Spatrick     }
35973471bf0Spatrick 
36009467b48Spatrick     getTargetStreamer()->emitFunctionType(Sym);
36109467b48Spatrick 
36273471bf0Spatrick     if (F.hasFnAttribute("wasm-import-module")) {
36309467b48Spatrick       StringRef Name =
36409467b48Spatrick           F.getFnAttribute("wasm-import-module").getValueAsString();
365097a140dSpatrick       Sym->setImportModule(storeName(Name));
36609467b48Spatrick       getTargetStreamer()->emitImportModule(Sym, Name);
36709467b48Spatrick     }
36873471bf0Spatrick     if (F.hasFnAttribute("wasm-import-name")) {
36973471bf0Spatrick       // If this is a converted Emscripten EH/SjLj symbol, we shouldn't use
37073471bf0Spatrick       // the original function name but the converted symbol name.
37109467b48Spatrick       StringRef Name =
37273471bf0Spatrick           InvokeDetected
37373471bf0Spatrick               ? Sym->getName()
37473471bf0Spatrick               : F.getFnAttribute("wasm-import-name").getValueAsString();
375097a140dSpatrick       Sym->setImportName(storeName(Name));
37609467b48Spatrick       getTargetStreamer()->emitImportName(Sym, Name);
37709467b48Spatrick     }
37809467b48Spatrick 
37909467b48Spatrick     if (F.hasFnAttribute("wasm-export-name")) {
38009467b48Spatrick       auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
38109467b48Spatrick       StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString();
382097a140dSpatrick       Sym->setExportName(storeName(Name));
38309467b48Spatrick       getTargetStreamer()->emitExportName(Sym, Name);
38409467b48Spatrick     }
38509467b48Spatrick   }
38673471bf0Spatrick }
38773471bf0Spatrick 
emitEndOfAsmFile(Module & M)38873471bf0Spatrick void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) {
389*d415bd75Srobert   // This is required to emit external declarations (like .functypes) when
390*d415bd75Srobert   // no functions are defined in the compilation unit and therefore,
391*d415bd75Srobert   // emitDecls() is not called until now.
392*d415bd75Srobert   emitDecls(M);
39373471bf0Spatrick 
39473471bf0Spatrick   // When a function's address is taken, a TABLE_INDEX relocation is emitted
39573471bf0Spatrick   // against the function symbol at the use site.  However the relocation
39673471bf0Spatrick   // doesn't explicitly refer to the table.  In the future we may want to
39773471bf0Spatrick   // define a new kind of reloc against both the function and the table, so
39873471bf0Spatrick   // that the linker can see that the function symbol keeps the table alive,
39973471bf0Spatrick   // but for now manually mark the table as live.
40073471bf0Spatrick   for (const auto &F : M) {
40173471bf0Spatrick     if (!F.isIntrinsic() && F.hasAddressTaken()) {
40273471bf0Spatrick       MCSymbolWasm *FunctionTable =
40373471bf0Spatrick           WebAssembly::getOrCreateFunctionTableSymbol(OutContext, Subtarget);
40473471bf0Spatrick       OutStreamer->emitSymbolAttribute(FunctionTable, MCSA_NoDeadStrip);
40573471bf0Spatrick       break;
40673471bf0Spatrick     }
40773471bf0Spatrick   }
40809467b48Spatrick 
40909467b48Spatrick   for (const auto &G : M.globals()) {
41073471bf0Spatrick     if (!G.hasInitializer() && G.hasExternalLinkage() &&
41173471bf0Spatrick         !WebAssembly::isWasmVarAddressSpace(G.getAddressSpace()) &&
41273471bf0Spatrick         G.getValueType()->isSized()) {
41309467b48Spatrick       uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
41409467b48Spatrick       OutStreamer->emitELFSize(getSymbol(&G),
41509467b48Spatrick                                MCConstantExpr::create(Size, OutContext));
41609467b48Spatrick     }
41709467b48Spatrick   }
41809467b48Spatrick 
41909467b48Spatrick   if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
42009467b48Spatrick     for (const Metadata *MD : Named->operands()) {
42109467b48Spatrick       const auto *Tuple = dyn_cast<MDTuple>(MD);
42209467b48Spatrick       if (!Tuple || Tuple->getNumOperands() != 2)
42309467b48Spatrick         continue;
42409467b48Spatrick       const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
42509467b48Spatrick       const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
42609467b48Spatrick       if (!Name || !Contents)
42709467b48Spatrick         continue;
42809467b48Spatrick 
429*d415bd75Srobert       OutStreamer->pushSection();
43009467b48Spatrick       std::string SectionName = (".custom_section." + Name->getString()).str();
43109467b48Spatrick       MCSectionWasm *MySection =
43209467b48Spatrick           OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
433*d415bd75Srobert       OutStreamer->switchSection(MySection);
434097a140dSpatrick       OutStreamer->emitBytes(Contents->getString());
435*d415bd75Srobert       OutStreamer->popSection();
43609467b48Spatrick     }
43709467b48Spatrick   }
43809467b48Spatrick 
43909467b48Spatrick   EmitProducerInfo(M);
44009467b48Spatrick   EmitTargetFeatures(M);
44109467b48Spatrick }
44209467b48Spatrick 
EmitProducerInfo(Module & M)44309467b48Spatrick void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
44409467b48Spatrick   llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages;
44509467b48Spatrick   if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) {
44609467b48Spatrick     llvm::SmallSet<StringRef, 4> SeenLanguages;
44709467b48Spatrick     for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) {
44809467b48Spatrick       const auto *CU = cast<DICompileUnit>(Debug->getOperand(I));
44909467b48Spatrick       StringRef Language = dwarf::LanguageString(CU->getSourceLanguage());
45009467b48Spatrick       Language.consume_front("DW_LANG_");
45109467b48Spatrick       if (SeenLanguages.insert(Language).second)
45209467b48Spatrick         Languages.emplace_back(Language.str(), "");
45309467b48Spatrick     }
45409467b48Spatrick   }
45509467b48Spatrick 
45609467b48Spatrick   llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools;
45709467b48Spatrick   if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
45809467b48Spatrick     llvm::SmallSet<StringRef, 4> SeenTools;
45909467b48Spatrick     for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) {
46009467b48Spatrick       const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0));
46109467b48Spatrick       std::pair<StringRef, StringRef> Field = S->getString().split("version");
46209467b48Spatrick       StringRef Name = Field.first.trim();
46309467b48Spatrick       StringRef Version = Field.second.trim();
46409467b48Spatrick       if (SeenTools.insert(Name).second)
46509467b48Spatrick         Tools.emplace_back(Name.str(), Version.str());
46609467b48Spatrick     }
46709467b48Spatrick   }
46809467b48Spatrick 
46909467b48Spatrick   int FieldCount = int(!Languages.empty()) + int(!Tools.empty());
47009467b48Spatrick   if (FieldCount != 0) {
47109467b48Spatrick     MCSectionWasm *Producers = OutContext.getWasmSection(
47209467b48Spatrick         ".custom_section.producers", SectionKind::getMetadata());
473*d415bd75Srobert     OutStreamer->pushSection();
474*d415bd75Srobert     OutStreamer->switchSection(Producers);
475097a140dSpatrick     OutStreamer->emitULEB128IntValue(FieldCount);
47609467b48Spatrick     for (auto &Producers : {std::make_pair("language", &Languages),
47709467b48Spatrick             std::make_pair("processed-by", &Tools)}) {
47809467b48Spatrick       if (Producers.second->empty())
47909467b48Spatrick         continue;
480097a140dSpatrick       OutStreamer->emitULEB128IntValue(strlen(Producers.first));
481097a140dSpatrick       OutStreamer->emitBytes(Producers.first);
482097a140dSpatrick       OutStreamer->emitULEB128IntValue(Producers.second->size());
48309467b48Spatrick       for (auto &Producer : *Producers.second) {
484097a140dSpatrick         OutStreamer->emitULEB128IntValue(Producer.first.size());
485097a140dSpatrick         OutStreamer->emitBytes(Producer.first);
486097a140dSpatrick         OutStreamer->emitULEB128IntValue(Producer.second.size());
487097a140dSpatrick         OutStreamer->emitBytes(Producer.second);
48809467b48Spatrick       }
48909467b48Spatrick     }
490*d415bd75Srobert     OutStreamer->popSection();
49109467b48Spatrick   }
49209467b48Spatrick }
49309467b48Spatrick 
EmitTargetFeatures(Module & M)49409467b48Spatrick void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
49509467b48Spatrick   struct FeatureEntry {
49609467b48Spatrick     uint8_t Prefix;
497097a140dSpatrick     std::string Name;
49809467b48Spatrick   };
49909467b48Spatrick 
50009467b48Spatrick   // Read target features and linkage policies from module metadata
50109467b48Spatrick   SmallVector<FeatureEntry, 4> EmittedFeatures;
502097a140dSpatrick   auto EmitFeature = [&](std::string Feature) {
503097a140dSpatrick     std::string MDKey = (StringRef("wasm-feature-") + Feature).str();
50409467b48Spatrick     Metadata *Policy = M.getModuleFlag(MDKey);
50509467b48Spatrick     if (Policy == nullptr)
506097a140dSpatrick       return;
50709467b48Spatrick 
50809467b48Spatrick     FeatureEntry Entry;
50909467b48Spatrick     Entry.Prefix = 0;
510097a140dSpatrick     Entry.Name = Feature;
51109467b48Spatrick 
51209467b48Spatrick     if (auto *MD = cast<ConstantAsMetadata>(Policy))
51309467b48Spatrick       if (auto *I = cast<ConstantInt>(MD->getValue()))
51409467b48Spatrick         Entry.Prefix = I->getZExtValue();
51509467b48Spatrick 
51609467b48Spatrick     // Silently ignore invalid metadata
51709467b48Spatrick     if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED &&
51809467b48Spatrick         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED &&
51909467b48Spatrick         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED)
520097a140dSpatrick       return;
52109467b48Spatrick 
52209467b48Spatrick     EmittedFeatures.push_back(Entry);
523097a140dSpatrick   };
524097a140dSpatrick 
525097a140dSpatrick   for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
526097a140dSpatrick     EmitFeature(KV.Key);
52709467b48Spatrick   }
528097a140dSpatrick   // This pseudo-feature tells the linker whether shared memory would be safe
529097a140dSpatrick   EmitFeature("shared-mem");
53009467b48Spatrick 
531*d415bd75Srobert   // This is an "architecture", not a "feature", but we emit it as such for
532*d415bd75Srobert   // the benefit of tools like Binaryen and consistency with other producers.
533*d415bd75Srobert   // FIXME: Subtarget is null here, so can't Subtarget->hasAddr64() ?
534*d415bd75Srobert   if (M.getDataLayout().getPointerSize() == 8) {
535*d415bd75Srobert     // Can't use EmitFeature since "wasm-feature-memory64" is not a module
536*d415bd75Srobert     // flag.
537*d415bd75Srobert     EmittedFeatures.push_back({wasm::WASM_FEATURE_PREFIX_USED, "memory64"});
538*d415bd75Srobert   }
539*d415bd75Srobert 
54009467b48Spatrick   if (EmittedFeatures.size() == 0)
54109467b48Spatrick     return;
54209467b48Spatrick 
54309467b48Spatrick   // Emit features and linkage policies into the "target_features" section
54409467b48Spatrick   MCSectionWasm *FeaturesSection = OutContext.getWasmSection(
54509467b48Spatrick       ".custom_section.target_features", SectionKind::getMetadata());
546*d415bd75Srobert   OutStreamer->pushSection();
547*d415bd75Srobert   OutStreamer->switchSection(FeaturesSection);
54809467b48Spatrick 
549097a140dSpatrick   OutStreamer->emitULEB128IntValue(EmittedFeatures.size());
55009467b48Spatrick   for (auto &F : EmittedFeatures) {
551097a140dSpatrick     OutStreamer->emitIntValue(F.Prefix, 1);
552097a140dSpatrick     OutStreamer->emitULEB128IntValue(F.Name.size());
553097a140dSpatrick     OutStreamer->emitBytes(F.Name);
55409467b48Spatrick   }
55509467b48Spatrick 
556*d415bd75Srobert   OutStreamer->popSection();
55709467b48Spatrick }
55809467b48Spatrick 
emitConstantPool()559097a140dSpatrick void WebAssemblyAsmPrinter::emitConstantPool() {
560*d415bd75Srobert   emitDecls(*MMI->getModule());
56109467b48Spatrick   assert(MF->getConstantPool()->getConstants().empty() &&
56209467b48Spatrick          "WebAssembly disables constant pools");
56309467b48Spatrick }
56409467b48Spatrick 
emitJumpTableInfo()565097a140dSpatrick void WebAssemblyAsmPrinter::emitJumpTableInfo() {
56609467b48Spatrick   // Nothing to do; jump tables are incorporated into the instruction stream.
56709467b48Spatrick }
56809467b48Spatrick 
emitFunctionBodyStart()569097a140dSpatrick void WebAssemblyAsmPrinter::emitFunctionBodyStart() {
57009467b48Spatrick   const Function &F = MF->getFunction();
57109467b48Spatrick   SmallVector<MVT, 1> ResultVTs;
57209467b48Spatrick   SmallVector<MVT, 4> ParamVTs;
573097a140dSpatrick   computeSignatureVTs(F.getFunctionType(), &F, F, TM, ParamVTs, ResultVTs);
574097a140dSpatrick 
57509467b48Spatrick   auto Signature = signatureFromMVTs(ResultVTs, ParamVTs);
57609467b48Spatrick   auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym);
57709467b48Spatrick   WasmSym->setSignature(Signature.get());
57809467b48Spatrick   addSignature(std::move(Signature));
57909467b48Spatrick   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
58009467b48Spatrick 
58109467b48Spatrick   getTargetStreamer()->emitFunctionType(WasmSym);
58209467b48Spatrick 
58309467b48Spatrick   // Emit the function index.
58409467b48Spatrick   if (MDNode *Idx = F.getMetadata("wasm.index")) {
58509467b48Spatrick     assert(Idx->getNumOperands() == 1);
58609467b48Spatrick 
58709467b48Spatrick     getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
58809467b48Spatrick         cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
58909467b48Spatrick   }
59009467b48Spatrick 
59109467b48Spatrick   SmallVector<wasm::ValType, 16> Locals;
59209467b48Spatrick   valTypesFromMVTs(MFI->getLocals(), Locals);
59309467b48Spatrick   getTargetStreamer()->emitLocal(Locals);
59409467b48Spatrick 
595097a140dSpatrick   AsmPrinter::emitFunctionBodyStart();
59609467b48Spatrick }
59709467b48Spatrick 
emitInstruction(const MachineInstr * MI)598097a140dSpatrick void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) {
59909467b48Spatrick   LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
600*d415bd75Srobert   WebAssembly_MC::verifyInstructionPredicates(MI->getOpcode(),
601*d415bd75Srobert                                               Subtarget->getFeatureBits());
60209467b48Spatrick 
60309467b48Spatrick   switch (MI->getOpcode()) {
60409467b48Spatrick   case WebAssembly::ARGUMENT_i32:
60509467b48Spatrick   case WebAssembly::ARGUMENT_i32_S:
60609467b48Spatrick   case WebAssembly::ARGUMENT_i64:
60709467b48Spatrick   case WebAssembly::ARGUMENT_i64_S:
60809467b48Spatrick   case WebAssembly::ARGUMENT_f32:
60909467b48Spatrick   case WebAssembly::ARGUMENT_f32_S:
61009467b48Spatrick   case WebAssembly::ARGUMENT_f64:
61109467b48Spatrick   case WebAssembly::ARGUMENT_f64_S:
61209467b48Spatrick   case WebAssembly::ARGUMENT_v16i8:
61309467b48Spatrick   case WebAssembly::ARGUMENT_v16i8_S:
61409467b48Spatrick   case WebAssembly::ARGUMENT_v8i16:
61509467b48Spatrick   case WebAssembly::ARGUMENT_v8i16_S:
61609467b48Spatrick   case WebAssembly::ARGUMENT_v4i32:
61709467b48Spatrick   case WebAssembly::ARGUMENT_v4i32_S:
61809467b48Spatrick   case WebAssembly::ARGUMENT_v2i64:
61909467b48Spatrick   case WebAssembly::ARGUMENT_v2i64_S:
62009467b48Spatrick   case WebAssembly::ARGUMENT_v4f32:
62109467b48Spatrick   case WebAssembly::ARGUMENT_v4f32_S:
62209467b48Spatrick   case WebAssembly::ARGUMENT_v2f64:
62309467b48Spatrick   case WebAssembly::ARGUMENT_v2f64_S:
62409467b48Spatrick     // These represent values which are live into the function entry, so there's
62509467b48Spatrick     // no instruction to emit.
62609467b48Spatrick     break;
62709467b48Spatrick   case WebAssembly::FALLTHROUGH_RETURN: {
62809467b48Spatrick     // These instructions represent the implicit return at the end of a
62909467b48Spatrick     // function body.
63009467b48Spatrick     if (isVerbose()) {
63109467b48Spatrick       OutStreamer->AddComment("fallthrough-return");
632*d415bd75Srobert       OutStreamer->addBlankLine();
63309467b48Spatrick     }
63409467b48Spatrick     break;
63509467b48Spatrick   }
63609467b48Spatrick   case WebAssembly::COMPILER_FENCE:
63709467b48Spatrick     // This is a compiler barrier that prevents instruction reordering during
63809467b48Spatrick     // backend compilation, and should not be emitted.
63909467b48Spatrick     break;
64009467b48Spatrick   default: {
64109467b48Spatrick     WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
64209467b48Spatrick     MCInst TmpInst;
64309467b48Spatrick     MCInstLowering.lower(MI, TmpInst);
64409467b48Spatrick     EmitToStreamer(*OutStreamer, TmpInst);
64509467b48Spatrick     break;
64609467b48Spatrick   }
64709467b48Spatrick   }
64809467b48Spatrick }
64909467b48Spatrick 
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & OS)65009467b48Spatrick bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
65109467b48Spatrick                                             unsigned OpNo,
65209467b48Spatrick                                             const char *ExtraCode,
65309467b48Spatrick                                             raw_ostream &OS) {
65409467b48Spatrick   // First try the generic code, which knows about modifiers like 'c' and 'n'.
65509467b48Spatrick   if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
65609467b48Spatrick     return false;
65709467b48Spatrick 
65809467b48Spatrick   if (!ExtraCode) {
65909467b48Spatrick     const MachineOperand &MO = MI->getOperand(OpNo);
66009467b48Spatrick     switch (MO.getType()) {
66109467b48Spatrick     case MachineOperand::MO_Immediate:
66209467b48Spatrick       OS << MO.getImm();
66309467b48Spatrick       return false;
66409467b48Spatrick     case MachineOperand::MO_Register:
66509467b48Spatrick       // FIXME: only opcode that still contains registers, as required by
66609467b48Spatrick       // MachineInstr::getDebugVariable().
66709467b48Spatrick       assert(MI->getOpcode() == WebAssembly::INLINEASM);
66809467b48Spatrick       OS << regToString(MO);
66909467b48Spatrick       return false;
67009467b48Spatrick     case MachineOperand::MO_GlobalAddress:
67109467b48Spatrick       PrintSymbolOperand(MO, OS);
67209467b48Spatrick       return false;
67309467b48Spatrick     case MachineOperand::MO_ExternalSymbol:
67409467b48Spatrick       GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
67509467b48Spatrick       printOffset(MO.getOffset(), OS);
67609467b48Spatrick       return false;
67709467b48Spatrick     case MachineOperand::MO_MachineBasicBlock:
67809467b48Spatrick       MO.getMBB()->getSymbol()->print(OS, MAI);
67909467b48Spatrick       return false;
68009467b48Spatrick     default:
68109467b48Spatrick       break;
68209467b48Spatrick     }
68309467b48Spatrick   }
68409467b48Spatrick 
68509467b48Spatrick   return true;
68609467b48Spatrick }
68709467b48Spatrick 
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & OS)68809467b48Spatrick bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
68909467b48Spatrick                                                   unsigned OpNo,
69009467b48Spatrick                                                   const char *ExtraCode,
69109467b48Spatrick                                                   raw_ostream &OS) {
69209467b48Spatrick   // The current approach to inline asm is that "r" constraints are expressed
69309467b48Spatrick   // as local indices, rather than values on the operand stack. This simplifies
69409467b48Spatrick   // using "r" as it eliminates the need to push and pop the values in a
69509467b48Spatrick   // particular order, however it also makes it impossible to have an "m"
69609467b48Spatrick   // constraint. So we don't support it.
69709467b48Spatrick 
69809467b48Spatrick   return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
69909467b48Spatrick }
70009467b48Spatrick 
70109467b48Spatrick // Force static initialization.
LLVMInitializeWebAssemblyAsmPrinter()70209467b48Spatrick extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmPrinter() {
70309467b48Spatrick   RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
70409467b48Spatrick   RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
70509467b48Spatrick }
706