xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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/WebAssemblyMCTargetDesc.h"
180b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyTargetStreamer.h"
190b57cec5SDimitry Andric #include "TargetInfo/WebAssemblyTargetInfo.h"
20*fe6060f1SDimitry Andric #include "Utils/WebAssemblyTypeUtilities.h"
21*fe6060f1SDimitry Andric #include "Utils/WebAssemblyUtilities.h"
220b57cec5SDimitry Andric #include "WebAssembly.h"
230b57cec5SDimitry Andric #include "WebAssemblyMCInstLower.h"
240b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
250b57cec5SDimitry Andric #include "WebAssemblyRegisterInfo.h"
26*fe6060f1SDimitry Andric #include "WebAssemblyRuntimeLibcallSignatures.h"
270b57cec5SDimitry Andric #include "WebAssemblyTargetMachine.h"
280b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
290b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
300b57cec5SDimitry Andric #include "llvm/BinaryFormat/Wasm.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
360b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
370b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
380b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
390b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
400b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
410b57cec5SDimitry Andric #include "llvm/MC/MCSectionWasm.h"
420b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
430b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
440b57cec5SDimitry Andric #include "llvm/MC/MCSymbolWasm.h"
450b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
460b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
470b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric using namespace llvm;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric #define DEBUG_TYPE "asm-printer"
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric extern cl::opt<bool> WasmKeepRegisters;
54e8d8bef9SDimitry Andric extern cl::opt<bool> EnableEmException;
55e8d8bef9SDimitry Andric extern cl::opt<bool> EnableEmSjLj;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
580b57cec5SDimitry Andric // Helpers.
590b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
620b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
630b57cec5SDimitry Andric   const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
640b57cec5SDimitry Andric   for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
650b57cec5SDimitry Andric                 MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64})
660b57cec5SDimitry Andric     if (TRI->isTypeLegalForClass(*TRC, T))
670b57cec5SDimitry Andric       return T;
680b57cec5SDimitry Andric   LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
690b57cec5SDimitry Andric   llvm_unreachable("Unknown register type");
700b57cec5SDimitry Andric   return MVT::Other;
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
748bcb0991SDimitry Andric   Register RegNo = MO.getReg();
758bcb0991SDimitry Andric   assert(Register::isVirtualRegister(RegNo) &&
760b57cec5SDimitry Andric          "Unlowered physical register encountered during assembly printing");
770b57cec5SDimitry Andric   assert(!MFI->isVRegStackified(RegNo));
780b57cec5SDimitry Andric   unsigned WAReg = MFI->getWAReg(RegNo);
790b57cec5SDimitry Andric   assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
800b57cec5SDimitry Andric   return '$' + utostr(WAReg);
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
840b57cec5SDimitry Andric   MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
850b57cec5SDimitry Andric   return static_cast<WebAssemblyTargetStreamer *>(TS);
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
88e8d8bef9SDimitry Andric // Emscripten exception handling helpers
89e8d8bef9SDimitry Andric //
90e8d8bef9SDimitry Andric // This converts invoke names generated by LowerEmscriptenEHSjLj to real names
91e8d8bef9SDimitry Andric // that are expected by JavaScript glue code. The invoke names generated by
92e8d8bef9SDimitry Andric // Emscripten JS glue code are based on their argument and return types; for
93e8d8bef9SDimitry Andric // example, for a function that takes an i32 and returns nothing, it is
94e8d8bef9SDimitry Andric // 'invoke_vi'. But the format of invoke generated by LowerEmscriptenEHSjLj pass
95e8d8bef9SDimitry Andric // contains a mangled string generated from their IR types, for example,
96e8d8bef9SDimitry Andric // "__invoke_void_%struct.mystruct*_int", because final wasm types are not
97e8d8bef9SDimitry Andric // available in the IR pass. So we convert those names to the form that
98e8d8bef9SDimitry Andric // Emscripten JS code expects.
99e8d8bef9SDimitry Andric //
100e8d8bef9SDimitry Andric // Refer to LowerEmscriptenEHSjLj pass for more details.
101e8d8bef9SDimitry Andric 
102e8d8bef9SDimitry Andric // Returns true if the given function name is an invoke name generated by
103e8d8bef9SDimitry Andric // LowerEmscriptenEHSjLj pass.
104e8d8bef9SDimitry Andric static bool isEmscriptenInvokeName(StringRef Name) {
105e8d8bef9SDimitry Andric   if (Name.front() == '"' && Name.back() == '"')
106e8d8bef9SDimitry Andric     Name = Name.substr(1, Name.size() - 2);
107e8d8bef9SDimitry Andric   return Name.startswith("__invoke_");
108e8d8bef9SDimitry Andric }
109e8d8bef9SDimitry Andric 
110e8d8bef9SDimitry Andric // Returns a character that represents the given wasm value type in invoke
111e8d8bef9SDimitry Andric // signatures.
112e8d8bef9SDimitry Andric static char getInvokeSig(wasm::ValType VT) {
113e8d8bef9SDimitry Andric   switch (VT) {
114e8d8bef9SDimitry Andric   case wasm::ValType::I32:
115e8d8bef9SDimitry Andric     return 'i';
116e8d8bef9SDimitry Andric   case wasm::ValType::I64:
117e8d8bef9SDimitry Andric     return 'j';
118e8d8bef9SDimitry Andric   case wasm::ValType::F32:
119e8d8bef9SDimitry Andric     return 'f';
120e8d8bef9SDimitry Andric   case wasm::ValType::F64:
121e8d8bef9SDimitry Andric     return 'd';
122e8d8bef9SDimitry Andric   case wasm::ValType::V128:
123e8d8bef9SDimitry Andric     return 'V';
124e8d8bef9SDimitry Andric   case wasm::ValType::FUNCREF:
125e8d8bef9SDimitry Andric     return 'F';
126e8d8bef9SDimitry Andric   case wasm::ValType::EXTERNREF:
127e8d8bef9SDimitry Andric     return 'X';
128e8d8bef9SDimitry Andric   }
129e8d8bef9SDimitry Andric   llvm_unreachable("Unhandled wasm::ValType enum");
130e8d8bef9SDimitry Andric }
131e8d8bef9SDimitry Andric 
132e8d8bef9SDimitry Andric // Given the wasm signature, generate the invoke name in the format JS glue code
133e8d8bef9SDimitry Andric // expects.
134e8d8bef9SDimitry Andric static std::string getEmscriptenInvokeSymbolName(wasm::WasmSignature *Sig) {
135e8d8bef9SDimitry Andric   assert(Sig->Returns.size() <= 1);
136e8d8bef9SDimitry Andric   std::string Ret = "invoke_";
137e8d8bef9SDimitry Andric   if (!Sig->Returns.empty())
138e8d8bef9SDimitry Andric     for (auto VT : Sig->Returns)
139e8d8bef9SDimitry Andric       Ret += getInvokeSig(VT);
140e8d8bef9SDimitry Andric   else
141e8d8bef9SDimitry Andric     Ret += 'v';
142e8d8bef9SDimitry Andric   // Invokes' first argument is a pointer to the original function, so skip it
143e8d8bef9SDimitry Andric   for (unsigned I = 1, E = Sig->Params.size(); I < E; I++)
144e8d8bef9SDimitry Andric     Ret += getInvokeSig(Sig->Params[I]);
145e8d8bef9SDimitry Andric   return Ret;
146e8d8bef9SDimitry Andric }
147e8d8bef9SDimitry Andric 
1480b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1490b57cec5SDimitry Andric // WebAssemblyAsmPrinter Implementation.
1500b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1510b57cec5SDimitry Andric 
152e8d8bef9SDimitry Andric MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction(
153e8d8bef9SDimitry Andric     const Function *F, bool EnableEmEH, wasm::WasmSignature *Sig,
154e8d8bef9SDimitry Andric     bool &InvokeDetected) {
155e8d8bef9SDimitry Andric   MCSymbolWasm *WasmSym = nullptr;
156e8d8bef9SDimitry Andric   if (EnableEmEH && isEmscriptenInvokeName(F->getName())) {
157e8d8bef9SDimitry Andric     assert(Sig);
158e8d8bef9SDimitry Andric     InvokeDetected = true;
159e8d8bef9SDimitry Andric     if (Sig->Returns.size() > 1) {
160e8d8bef9SDimitry Andric       std::string Msg =
161e8d8bef9SDimitry Andric           "Emscripten EH/SjLj does not support multivalue returns: " +
162e8d8bef9SDimitry Andric           std::string(F->getName()) + ": " +
163e8d8bef9SDimitry Andric           WebAssembly::signatureToString(Sig);
164e8d8bef9SDimitry Andric       report_fatal_error(Msg);
165e8d8bef9SDimitry Andric     }
166e8d8bef9SDimitry Andric     WasmSym = cast<MCSymbolWasm>(
167e8d8bef9SDimitry Andric         GetExternalSymbolSymbol(getEmscriptenInvokeSymbolName(Sig)));
168e8d8bef9SDimitry Andric   } else {
169e8d8bef9SDimitry Andric     WasmSym = cast<MCSymbolWasm>(getSymbol(F));
170e8d8bef9SDimitry Andric   }
171e8d8bef9SDimitry Andric   return WasmSym;
172e8d8bef9SDimitry Andric }
173e8d8bef9SDimitry Andric 
174*fe6060f1SDimitry Andric void WebAssemblyAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
175*fe6060f1SDimitry Andric   if (!WebAssembly::isWasmVarAddressSpace(GV->getAddressSpace())) {
176*fe6060f1SDimitry Andric     AsmPrinter::emitGlobalVariable(GV);
177*fe6060f1SDimitry Andric     return;
178*fe6060f1SDimitry Andric   }
179*fe6060f1SDimitry Andric 
180*fe6060f1SDimitry Andric   assert(!GV->isThreadLocal());
181*fe6060f1SDimitry Andric 
182*fe6060f1SDimitry Andric   MCSymbolWasm *Sym = cast<MCSymbolWasm>(getSymbol(GV));
183*fe6060f1SDimitry Andric 
184*fe6060f1SDimitry Andric   if (!Sym->getType()) {
185*fe6060f1SDimitry Andric     const WebAssemblyTargetLowering &TLI = *Subtarget->getTargetLowering();
186*fe6060f1SDimitry Andric     SmallVector<EVT, 1> VTs;
187*fe6060f1SDimitry Andric     ComputeValueVTs(TLI, GV->getParent()->getDataLayout(), GV->getValueType(),
188*fe6060f1SDimitry Andric                     VTs);
189*fe6060f1SDimitry Andric     if (VTs.size() != 1 ||
190*fe6060f1SDimitry Andric         TLI.getNumRegisters(GV->getParent()->getContext(), VTs[0]) != 1)
191*fe6060f1SDimitry Andric       report_fatal_error("Aggregate globals not yet implemented");
192*fe6060f1SDimitry Andric     MVT VT = TLI.getRegisterType(GV->getParent()->getContext(), VTs[0]);
193*fe6060f1SDimitry Andric     bool Mutable = true;
194*fe6060f1SDimitry Andric     wasm::ValType Type = WebAssembly::toValType(VT);
195*fe6060f1SDimitry Andric     Sym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
196*fe6060f1SDimitry Andric     Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(Type), Mutable});
197*fe6060f1SDimitry Andric   }
198*fe6060f1SDimitry Andric 
199*fe6060f1SDimitry Andric   emitVisibility(Sym, GV->getVisibility(), !GV->isDeclaration());
200*fe6060f1SDimitry Andric   if (GV->hasInitializer()) {
201*fe6060f1SDimitry Andric     assert(getSymbolPreferLocal(*GV) == Sym);
202*fe6060f1SDimitry Andric     emitLinkage(GV, Sym);
2030b57cec5SDimitry Andric     getTargetStreamer()->emitGlobalType(Sym);
204*fe6060f1SDimitry Andric     OutStreamer->emitLabel(Sym);
205*fe6060f1SDimitry Andric     // TODO: Actually emit the initializer value.  Otherwise the global has the
206*fe6060f1SDimitry Andric     // default value for its type (0, ref.null, etc).
207*fe6060f1SDimitry Andric     OutStreamer->AddBlankLine();
208*fe6060f1SDimitry Andric   }
209*fe6060f1SDimitry Andric }
210*fe6060f1SDimitry Andric 
211*fe6060f1SDimitry Andric MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) {
212*fe6060f1SDimitry Andric   auto *WasmSym = cast<MCSymbolWasm>(GetExternalSymbolSymbol(Name));
213*fe6060f1SDimitry Andric 
214*fe6060f1SDimitry Andric   // May be called multiple times, so early out.
215*fe6060f1SDimitry Andric   if (WasmSym->getType().hasValue())
216*fe6060f1SDimitry Andric     return WasmSym;
217*fe6060f1SDimitry Andric 
218*fe6060f1SDimitry Andric   const WebAssemblySubtarget &Subtarget = getSubtarget();
219*fe6060f1SDimitry Andric 
220*fe6060f1SDimitry Andric   // Except for certain known symbols, all symbols used by CodeGen are
221*fe6060f1SDimitry Andric   // functions. It's OK to hardcode knowledge of specific symbols here; this
222*fe6060f1SDimitry Andric   // method is precisely there for fetching the signatures of known
223*fe6060f1SDimitry Andric   // Clang-provided symbols.
224*fe6060f1SDimitry Andric   if (Name == "__stack_pointer" || Name == "__tls_base" ||
225*fe6060f1SDimitry Andric       Name == "__memory_base" || Name == "__table_base" ||
226*fe6060f1SDimitry Andric       Name == "__tls_size" || Name == "__tls_align") {
227*fe6060f1SDimitry Andric     bool Mutable =
228*fe6060f1SDimitry Andric         Name == "__stack_pointer" || Name == "__tls_base";
229*fe6060f1SDimitry Andric     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
230*fe6060f1SDimitry Andric     WasmSym->setGlobalType(wasm::WasmGlobalType{
231*fe6060f1SDimitry Andric         uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
232*fe6060f1SDimitry Andric                                       : wasm::WASM_TYPE_I32),
233*fe6060f1SDimitry Andric         Mutable});
234*fe6060f1SDimitry Andric     return WasmSym;
235*fe6060f1SDimitry Andric   }
236*fe6060f1SDimitry Andric 
237*fe6060f1SDimitry Andric   SmallVector<wasm::ValType, 4> Returns;
238*fe6060f1SDimitry Andric   SmallVector<wasm::ValType, 4> Params;
239*fe6060f1SDimitry Andric   if (Name == "__cpp_exception") {
240*fe6060f1SDimitry Andric     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
241*fe6060f1SDimitry Andric     // We can't confirm its signature index for now because there can be
242*fe6060f1SDimitry Andric     // imported exceptions. Set it to be 0 for now.
243*fe6060f1SDimitry Andric     WasmSym->setTagType(
244*fe6060f1SDimitry Andric         {wasm::WASM_TAG_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
245*fe6060f1SDimitry Andric     // We may have multiple C++ compilation units to be linked together, each of
246*fe6060f1SDimitry Andric     // which defines the exception symbol. To resolve them, we declare them as
247*fe6060f1SDimitry Andric     // weak.
248*fe6060f1SDimitry Andric     WasmSym->setWeak(true);
249*fe6060f1SDimitry Andric     WasmSym->setExternal(true);
250*fe6060f1SDimitry Andric 
251*fe6060f1SDimitry Andric     // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
252*fe6060f1SDimitry Andric     // (for wasm64) param type and void return type. The reaon is, all C++
253*fe6060f1SDimitry Andric     // exception values are pointers, and to share the type section with
254*fe6060f1SDimitry Andric     // functions, exceptions are assumed to have void return type.
255*fe6060f1SDimitry Andric     Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
256*fe6060f1SDimitry Andric                                            : wasm::ValType::I32);
257*fe6060f1SDimitry Andric   } else { // Function symbols
258*fe6060f1SDimitry Andric     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
259*fe6060f1SDimitry Andric     getLibcallSignature(Subtarget, Name, Returns, Params);
260*fe6060f1SDimitry Andric   }
261*fe6060f1SDimitry Andric   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
262*fe6060f1SDimitry Andric                                                          std::move(Params));
263*fe6060f1SDimitry Andric   WasmSym->setSignature(Signature.get());
264*fe6060f1SDimitry Andric   addSignature(std::move(Signature));
265*fe6060f1SDimitry Andric 
266*fe6060f1SDimitry Andric   return WasmSym;
267*fe6060f1SDimitry Andric }
268*fe6060f1SDimitry Andric 
269*fe6060f1SDimitry Andric void WebAssemblyAsmPrinter::emitExternalDecls(const Module &M) {
270*fe6060f1SDimitry Andric   if (signaturesEmitted)
271*fe6060f1SDimitry Andric     return;
272*fe6060f1SDimitry Andric   signaturesEmitted = true;
273*fe6060f1SDimitry Andric 
274*fe6060f1SDimitry Andric   // Normally symbols for globals get discovered as the MI gets lowered,
275*fe6060f1SDimitry Andric   // but we need to know about them ahead of time.
276*fe6060f1SDimitry Andric   MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo<MachineModuleInfoWasm>();
277*fe6060f1SDimitry Andric   for (const auto &Name : MMIW.MachineSymbolsUsed) {
278*fe6060f1SDimitry Andric     getOrCreateWasmSymbol(Name.getKey());
279*fe6060f1SDimitry Andric   }
280*fe6060f1SDimitry Andric 
281*fe6060f1SDimitry Andric   for (auto &It : OutContext.getSymbols()) {
282*fe6060f1SDimitry Andric     // Emit .globaltype, .tagtype, or .tabletype declarations.
283*fe6060f1SDimitry Andric     auto Sym = cast<MCSymbolWasm>(It.getValue());
284*fe6060f1SDimitry Andric     if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL) {
285*fe6060f1SDimitry Andric       // .globaltype already handled by emitGlobalVariable for defined
286*fe6060f1SDimitry Andric       // variables; here we make sure the types of external wasm globals get
287*fe6060f1SDimitry Andric       // written to the file.
288*fe6060f1SDimitry Andric       if (Sym->isUndefined())
289*fe6060f1SDimitry Andric         getTargetStreamer()->emitGlobalType(Sym);
290*fe6060f1SDimitry Andric     } else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_TAG)
291*fe6060f1SDimitry Andric       getTargetStreamer()->emitTagType(Sym);
292*fe6060f1SDimitry Andric     else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_TABLE)
293*fe6060f1SDimitry Andric       getTargetStreamer()->emitTableType(Sym);
2940b57cec5SDimitry Andric   }
2950b57cec5SDimitry Andric 
296e8d8bef9SDimitry Andric   DenseSet<MCSymbol *> InvokeSymbols;
2970b57cec5SDimitry Andric   for (const auto &F : M) {
298480093f4SDimitry Andric     if (F.isIntrinsic())
299480093f4SDimitry Andric       continue;
300480093f4SDimitry Andric 
3010b57cec5SDimitry Andric     // Emit function type info for all undefined functions
302480093f4SDimitry Andric     if (F.isDeclarationForLinker()) {
3030b57cec5SDimitry Andric       SmallVector<MVT, 4> Results;
3040b57cec5SDimitry Andric       SmallVector<MVT, 4> Params;
3055ffd83dbSDimitry Andric       computeSignatureVTs(F.getFunctionType(), &F, F, TM, Params, Results);
306e8d8bef9SDimitry Andric       // At this point these MCSymbols may or may not have been created already
307e8d8bef9SDimitry Andric       // and thus also contain a signature, but we need to get the signature
308e8d8bef9SDimitry Andric       // anyway here in case it is an invoke that has not yet been created. We
309e8d8bef9SDimitry Andric       // will discard it later if it turns out not to be necessary.
310e8d8bef9SDimitry Andric       auto Signature = signatureFromMVTs(Results, Params);
311e8d8bef9SDimitry Andric       bool InvokeDetected = false;
312e8d8bef9SDimitry Andric       auto *Sym = getMCSymbolForFunction(&F, EnableEmException || EnableEmSjLj,
313e8d8bef9SDimitry Andric                                          Signature.get(), InvokeDetected);
314e8d8bef9SDimitry Andric 
315e8d8bef9SDimitry Andric       // Multiple functions can be mapped to the same invoke symbol. For
316e8d8bef9SDimitry Andric       // example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32'
317e8d8bef9SDimitry Andric       // are both mapped to '__invoke_vi'. We keep them in a set once we emit an
318e8d8bef9SDimitry Andric       // Emscripten EH symbol so we don't emit the same symbol twice.
319e8d8bef9SDimitry Andric       if (InvokeDetected && !InvokeSymbols.insert(Sym).second)
320e8d8bef9SDimitry Andric         continue;
321e8d8bef9SDimitry Andric 
3220b57cec5SDimitry Andric       Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
3230b57cec5SDimitry Andric       if (!Sym->getSignature()) {
3240b57cec5SDimitry Andric         Sym->setSignature(Signature.get());
3250b57cec5SDimitry Andric         addSignature(std::move(Signature));
326e8d8bef9SDimitry Andric       } else {
327e8d8bef9SDimitry Andric         // This symbol has already been created and had a signature. Discard it.
328e8d8bef9SDimitry Andric         Signature.reset();
3290b57cec5SDimitry Andric       }
330e8d8bef9SDimitry Andric 
3310b57cec5SDimitry Andric       getTargetStreamer()->emitFunctionType(Sym);
3320b57cec5SDimitry Andric 
333e8d8bef9SDimitry Andric       if (F.hasFnAttribute("wasm-import-module")) {
3340b57cec5SDimitry Andric         StringRef Name =
3350b57cec5SDimitry Andric             F.getFnAttribute("wasm-import-module").getValueAsString();
3365ffd83dbSDimitry Andric         Sym->setImportModule(storeName(Name));
3370b57cec5SDimitry Andric         getTargetStreamer()->emitImportModule(Sym, Name);
3380b57cec5SDimitry Andric       }
339e8d8bef9SDimitry Andric       if (F.hasFnAttribute("wasm-import-name")) {
340e8d8bef9SDimitry Andric         // If this is a converted Emscripten EH/SjLj symbol, we shouldn't use
341e8d8bef9SDimitry Andric         // the original function name but the converted symbol name.
3420b57cec5SDimitry Andric         StringRef Name =
343e8d8bef9SDimitry Andric             InvokeDetected
344e8d8bef9SDimitry Andric                 ? Sym->getName()
345e8d8bef9SDimitry Andric                 : F.getFnAttribute("wasm-import-name").getValueAsString();
3465ffd83dbSDimitry Andric         Sym->setImportName(storeName(Name));
3470b57cec5SDimitry Andric         getTargetStreamer()->emitImportName(Sym, Name);
3480b57cec5SDimitry Andric       }
3490b57cec5SDimitry Andric     }
350480093f4SDimitry Andric 
351480093f4SDimitry Andric     if (F.hasFnAttribute("wasm-export-name")) {
352480093f4SDimitry Andric       auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
353480093f4SDimitry Andric       StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString();
3545ffd83dbSDimitry Andric       Sym->setExportName(storeName(Name));
355480093f4SDimitry Andric       getTargetStreamer()->emitExportName(Sym, Name);
356480093f4SDimitry Andric     }
3570b57cec5SDimitry Andric   }
358*fe6060f1SDimitry Andric }
359*fe6060f1SDimitry Andric 
360*fe6060f1SDimitry Andric void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) {
361*fe6060f1SDimitry Andric   emitExternalDecls(M);
362*fe6060f1SDimitry Andric 
363*fe6060f1SDimitry Andric   // When a function's address is taken, a TABLE_INDEX relocation is emitted
364*fe6060f1SDimitry Andric   // against the function symbol at the use site.  However the relocation
365*fe6060f1SDimitry Andric   // doesn't explicitly refer to the table.  In the future we may want to
366*fe6060f1SDimitry Andric   // define a new kind of reloc against both the function and the table, so
367*fe6060f1SDimitry Andric   // that the linker can see that the function symbol keeps the table alive,
368*fe6060f1SDimitry Andric   // but for now manually mark the table as live.
369*fe6060f1SDimitry Andric   for (const auto &F : M) {
370*fe6060f1SDimitry Andric     if (!F.isIntrinsic() && F.hasAddressTaken()) {
371*fe6060f1SDimitry Andric       MCSymbolWasm *FunctionTable =
372*fe6060f1SDimitry Andric           WebAssembly::getOrCreateFunctionTableSymbol(OutContext, Subtarget);
373*fe6060f1SDimitry Andric       OutStreamer->emitSymbolAttribute(FunctionTable, MCSA_NoDeadStrip);
374*fe6060f1SDimitry Andric       break;
375*fe6060f1SDimitry Andric     }
376*fe6060f1SDimitry Andric   }
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric   for (const auto &G : M.globals()) {
379*fe6060f1SDimitry Andric     if (!G.hasInitializer() && G.hasExternalLinkage() &&
380*fe6060f1SDimitry Andric         !WebAssembly::isWasmVarAddressSpace(G.getAddressSpace()) &&
381*fe6060f1SDimitry Andric         G.getValueType()->isSized()) {
3820b57cec5SDimitry Andric       uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
3830b57cec5SDimitry Andric       OutStreamer->emitELFSize(getSymbol(&G),
3840b57cec5SDimitry Andric                                MCConstantExpr::create(Size, OutContext));
3850b57cec5SDimitry Andric     }
3860b57cec5SDimitry Andric   }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric   if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
3890b57cec5SDimitry Andric     for (const Metadata *MD : Named->operands()) {
3900b57cec5SDimitry Andric       const auto *Tuple = dyn_cast<MDTuple>(MD);
3910b57cec5SDimitry Andric       if (!Tuple || Tuple->getNumOperands() != 2)
3920b57cec5SDimitry Andric         continue;
3930b57cec5SDimitry Andric       const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
3940b57cec5SDimitry Andric       const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
3950b57cec5SDimitry Andric       if (!Name || !Contents)
3960b57cec5SDimitry Andric         continue;
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric       OutStreamer->PushSection();
3990b57cec5SDimitry Andric       std::string SectionName = (".custom_section." + Name->getString()).str();
4000b57cec5SDimitry Andric       MCSectionWasm *MySection =
4010b57cec5SDimitry Andric           OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
4020b57cec5SDimitry Andric       OutStreamer->SwitchSection(MySection);
4035ffd83dbSDimitry Andric       OutStreamer->emitBytes(Contents->getString());
4040b57cec5SDimitry Andric       OutStreamer->PopSection();
4050b57cec5SDimitry Andric     }
4060b57cec5SDimitry Andric   }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   EmitProducerInfo(M);
4090b57cec5SDimitry Andric   EmitTargetFeatures(M);
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
4130b57cec5SDimitry Andric   llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages;
4140b57cec5SDimitry Andric   if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) {
4150b57cec5SDimitry Andric     llvm::SmallSet<StringRef, 4> SeenLanguages;
4160b57cec5SDimitry Andric     for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) {
4170b57cec5SDimitry Andric       const auto *CU = cast<DICompileUnit>(Debug->getOperand(I));
4180b57cec5SDimitry Andric       StringRef Language = dwarf::LanguageString(CU->getSourceLanguage());
4190b57cec5SDimitry Andric       Language.consume_front("DW_LANG_");
4200b57cec5SDimitry Andric       if (SeenLanguages.insert(Language).second)
4210b57cec5SDimitry Andric         Languages.emplace_back(Language.str(), "");
4220b57cec5SDimitry Andric     }
4230b57cec5SDimitry Andric   }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools;
4260b57cec5SDimitry Andric   if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
4270b57cec5SDimitry Andric     llvm::SmallSet<StringRef, 4> SeenTools;
4280b57cec5SDimitry Andric     for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) {
4290b57cec5SDimitry Andric       const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0));
4300b57cec5SDimitry Andric       std::pair<StringRef, StringRef> Field = S->getString().split("version");
4310b57cec5SDimitry Andric       StringRef Name = Field.first.trim();
4320b57cec5SDimitry Andric       StringRef Version = Field.second.trim();
4330b57cec5SDimitry Andric       if (SeenTools.insert(Name).second)
4340b57cec5SDimitry Andric         Tools.emplace_back(Name.str(), Version.str());
4350b57cec5SDimitry Andric     }
4360b57cec5SDimitry Andric   }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   int FieldCount = int(!Languages.empty()) + int(!Tools.empty());
4390b57cec5SDimitry Andric   if (FieldCount != 0) {
4400b57cec5SDimitry Andric     MCSectionWasm *Producers = OutContext.getWasmSection(
4410b57cec5SDimitry Andric         ".custom_section.producers", SectionKind::getMetadata());
4420b57cec5SDimitry Andric     OutStreamer->PushSection();
4430b57cec5SDimitry Andric     OutStreamer->SwitchSection(Producers);
4445ffd83dbSDimitry Andric     OutStreamer->emitULEB128IntValue(FieldCount);
4450b57cec5SDimitry Andric     for (auto &Producers : {std::make_pair("language", &Languages),
4460b57cec5SDimitry Andric             std::make_pair("processed-by", &Tools)}) {
4470b57cec5SDimitry Andric       if (Producers.second->empty())
4480b57cec5SDimitry Andric         continue;
4495ffd83dbSDimitry Andric       OutStreamer->emitULEB128IntValue(strlen(Producers.first));
4505ffd83dbSDimitry Andric       OutStreamer->emitBytes(Producers.first);
4515ffd83dbSDimitry Andric       OutStreamer->emitULEB128IntValue(Producers.second->size());
4520b57cec5SDimitry Andric       for (auto &Producer : *Producers.second) {
4535ffd83dbSDimitry Andric         OutStreamer->emitULEB128IntValue(Producer.first.size());
4545ffd83dbSDimitry Andric         OutStreamer->emitBytes(Producer.first);
4555ffd83dbSDimitry Andric         OutStreamer->emitULEB128IntValue(Producer.second.size());
4565ffd83dbSDimitry Andric         OutStreamer->emitBytes(Producer.second);
4570b57cec5SDimitry Andric       }
4580b57cec5SDimitry Andric     }
4590b57cec5SDimitry Andric     OutStreamer->PopSection();
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
4640b57cec5SDimitry Andric   struct FeatureEntry {
4650b57cec5SDimitry Andric     uint8_t Prefix;
4665ffd83dbSDimitry Andric     std::string Name;
4670b57cec5SDimitry Andric   };
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric   // Read target features and linkage policies from module metadata
4700b57cec5SDimitry Andric   SmallVector<FeatureEntry, 4> EmittedFeatures;
4715ffd83dbSDimitry Andric   auto EmitFeature = [&](std::string Feature) {
4725ffd83dbSDimitry Andric     std::string MDKey = (StringRef("wasm-feature-") + Feature).str();
4730b57cec5SDimitry Andric     Metadata *Policy = M.getModuleFlag(MDKey);
4740b57cec5SDimitry Andric     if (Policy == nullptr)
4755ffd83dbSDimitry Andric       return;
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric     FeatureEntry Entry;
4780b57cec5SDimitry Andric     Entry.Prefix = 0;
4795ffd83dbSDimitry Andric     Entry.Name = Feature;
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric     if (auto *MD = cast<ConstantAsMetadata>(Policy))
4820b57cec5SDimitry Andric       if (auto *I = cast<ConstantInt>(MD->getValue()))
4830b57cec5SDimitry Andric         Entry.Prefix = I->getZExtValue();
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric     // Silently ignore invalid metadata
4860b57cec5SDimitry Andric     if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED &&
4870b57cec5SDimitry Andric         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED &&
4880b57cec5SDimitry Andric         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED)
4895ffd83dbSDimitry Andric       return;
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric     EmittedFeatures.push_back(Entry);
4925ffd83dbSDimitry Andric   };
4935ffd83dbSDimitry Andric 
4945ffd83dbSDimitry Andric   for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
4955ffd83dbSDimitry Andric     EmitFeature(KV.Key);
4960b57cec5SDimitry Andric   }
4975ffd83dbSDimitry Andric   // This pseudo-feature tells the linker whether shared memory would be safe
4985ffd83dbSDimitry Andric   EmitFeature("shared-mem");
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric   if (EmittedFeatures.size() == 0)
5010b57cec5SDimitry Andric     return;
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   // Emit features and linkage policies into the "target_features" section
5040b57cec5SDimitry Andric   MCSectionWasm *FeaturesSection = OutContext.getWasmSection(
5050b57cec5SDimitry Andric       ".custom_section.target_features", SectionKind::getMetadata());
5060b57cec5SDimitry Andric   OutStreamer->PushSection();
5070b57cec5SDimitry Andric   OutStreamer->SwitchSection(FeaturesSection);
5080b57cec5SDimitry Andric 
5095ffd83dbSDimitry Andric   OutStreamer->emitULEB128IntValue(EmittedFeatures.size());
5100b57cec5SDimitry Andric   for (auto &F : EmittedFeatures) {
5115ffd83dbSDimitry Andric     OutStreamer->emitIntValue(F.Prefix, 1);
5125ffd83dbSDimitry Andric     OutStreamer->emitULEB128IntValue(F.Name.size());
5135ffd83dbSDimitry Andric     OutStreamer->emitBytes(F.Name);
5140b57cec5SDimitry Andric   }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   OutStreamer->PopSection();
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric 
5195ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitConstantPool() {
5200b57cec5SDimitry Andric   assert(MF->getConstantPool()->getConstants().empty() &&
5210b57cec5SDimitry Andric          "WebAssembly disables constant pools");
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric 
5245ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitJumpTableInfo() {
5250b57cec5SDimitry Andric   // Nothing to do; jump tables are incorporated into the instruction stream.
5260b57cec5SDimitry Andric }
5270b57cec5SDimitry Andric 
528*fe6060f1SDimitry Andric void WebAssemblyAsmPrinter::emitLinkage(const GlobalValue *GV, MCSymbol *Sym)
529*fe6060f1SDimitry Andric   const {
530*fe6060f1SDimitry Andric   AsmPrinter::emitLinkage(GV, Sym);
531*fe6060f1SDimitry Andric   // This gets called before the function label and type are emitted.
532*fe6060f1SDimitry Andric   // We use it to emit signatures of external functions.
533*fe6060f1SDimitry Andric   // FIXME casts!
534*fe6060f1SDimitry Andric   const_cast<WebAssemblyAsmPrinter *>(this)
535*fe6060f1SDimitry Andric     ->emitExternalDecls(*MMI->getModule());
536*fe6060f1SDimitry Andric }
537*fe6060f1SDimitry Andric 
538*fe6060f1SDimitry Andric 
5395ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitFunctionBodyStart() {
5400b57cec5SDimitry Andric   const Function &F = MF->getFunction();
5410b57cec5SDimitry Andric   SmallVector<MVT, 1> ResultVTs;
5420b57cec5SDimitry Andric   SmallVector<MVT, 4> ParamVTs;
5435ffd83dbSDimitry Andric   computeSignatureVTs(F.getFunctionType(), &F, F, TM, ParamVTs, ResultVTs);
5445ffd83dbSDimitry Andric 
5450b57cec5SDimitry Andric   auto Signature = signatureFromMVTs(ResultVTs, ParamVTs);
5460b57cec5SDimitry Andric   auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym);
5470b57cec5SDimitry Andric   WasmSym->setSignature(Signature.get());
5480b57cec5SDimitry Andric   addSignature(std::move(Signature));
5490b57cec5SDimitry Andric   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric   getTargetStreamer()->emitFunctionType(WasmSym);
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   // Emit the function index.
5540b57cec5SDimitry Andric   if (MDNode *Idx = F.getMetadata("wasm.index")) {
5550b57cec5SDimitry Andric     assert(Idx->getNumOperands() == 1);
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric     getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
5580b57cec5SDimitry Andric         cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
5590b57cec5SDimitry Andric   }
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric   SmallVector<wasm::ValType, 16> Locals;
5620b57cec5SDimitry Andric   valTypesFromMVTs(MFI->getLocals(), Locals);
5630b57cec5SDimitry Andric   getTargetStreamer()->emitLocal(Locals);
5640b57cec5SDimitry Andric 
5655ffd83dbSDimitry Andric   AsmPrinter::emitFunctionBodyStart();
5660b57cec5SDimitry Andric }
5670b57cec5SDimitry Andric 
5685ffd83dbSDimitry Andric void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) {
5690b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   switch (MI->getOpcode()) {
5720b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_i32:
5730b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_i32_S:
5740b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_i64:
5750b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_i64_S:
5760b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_f32:
5770b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_f32_S:
5780b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_f64:
5790b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_f64_S:
5800b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v16i8:
5810b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v16i8_S:
5820b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v8i16:
5830b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v8i16_S:
5840b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v4i32:
5850b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v4i32_S:
5860b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v2i64:
5870b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v2i64_S:
5880b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v4f32:
5890b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v4f32_S:
5900b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v2f64:
5910b57cec5SDimitry Andric   case WebAssembly::ARGUMENT_v2f64_S:
5920b57cec5SDimitry Andric     // These represent values which are live into the function entry, so there's
5930b57cec5SDimitry Andric     // no instruction to emit.
5940b57cec5SDimitry Andric     break;
5958bcb0991SDimitry Andric   case WebAssembly::FALLTHROUGH_RETURN: {
5960b57cec5SDimitry Andric     // These instructions represent the implicit return at the end of a
5978bcb0991SDimitry Andric     // function body.
5980b57cec5SDimitry Andric     if (isVerbose()) {
5998bcb0991SDimitry Andric       OutStreamer->AddComment("fallthrough-return");
6000b57cec5SDimitry Andric       OutStreamer->AddBlankLine();
6010b57cec5SDimitry Andric     }
6020b57cec5SDimitry Andric     break;
6030b57cec5SDimitry Andric   }
6040b57cec5SDimitry Andric   case WebAssembly::COMPILER_FENCE:
6050b57cec5SDimitry Andric     // This is a compiler barrier that prevents instruction reordering during
6060b57cec5SDimitry Andric     // backend compilation, and should not be emitted.
6070b57cec5SDimitry Andric     break;
6080b57cec5SDimitry Andric   default: {
6090b57cec5SDimitry Andric     WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
6100b57cec5SDimitry Andric     MCInst TmpInst;
6110b57cec5SDimitry Andric     MCInstLowering.lower(MI, TmpInst);
6120b57cec5SDimitry Andric     EmitToStreamer(*OutStreamer, TmpInst);
6130b57cec5SDimitry Andric     break;
6140b57cec5SDimitry Andric   }
6150b57cec5SDimitry Andric   }
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
6190b57cec5SDimitry Andric                                             unsigned OpNo,
6200b57cec5SDimitry Andric                                             const char *ExtraCode,
6210b57cec5SDimitry Andric                                             raw_ostream &OS) {
6220b57cec5SDimitry Andric   // First try the generic code, which knows about modifiers like 'c' and 'n'.
6230b57cec5SDimitry Andric   if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
6240b57cec5SDimitry Andric     return false;
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric   if (!ExtraCode) {
6270b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(OpNo);
6280b57cec5SDimitry Andric     switch (MO.getType()) {
6290b57cec5SDimitry Andric     case MachineOperand::MO_Immediate:
6300b57cec5SDimitry Andric       OS << MO.getImm();
6310b57cec5SDimitry Andric       return false;
6320b57cec5SDimitry Andric     case MachineOperand::MO_Register:
6330b57cec5SDimitry Andric       // FIXME: only opcode that still contains registers, as required by
6340b57cec5SDimitry Andric       // MachineInstr::getDebugVariable().
6350b57cec5SDimitry Andric       assert(MI->getOpcode() == WebAssembly::INLINEASM);
6360b57cec5SDimitry Andric       OS << regToString(MO);
6370b57cec5SDimitry Andric       return false;
6380b57cec5SDimitry Andric     case MachineOperand::MO_GlobalAddress:
6390b57cec5SDimitry Andric       PrintSymbolOperand(MO, OS);
6400b57cec5SDimitry Andric       return false;
6410b57cec5SDimitry Andric     case MachineOperand::MO_ExternalSymbol:
6420b57cec5SDimitry Andric       GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
6430b57cec5SDimitry Andric       printOffset(MO.getOffset(), OS);
6440b57cec5SDimitry Andric       return false;
6450b57cec5SDimitry Andric     case MachineOperand::MO_MachineBasicBlock:
6460b57cec5SDimitry Andric       MO.getMBB()->getSymbol()->print(OS, MAI);
6470b57cec5SDimitry Andric       return false;
6480b57cec5SDimitry Andric     default:
6490b57cec5SDimitry Andric       break;
6500b57cec5SDimitry Andric     }
6510b57cec5SDimitry Andric   }
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric   return true;
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
6570b57cec5SDimitry Andric                                                   unsigned OpNo,
6580b57cec5SDimitry Andric                                                   const char *ExtraCode,
6590b57cec5SDimitry Andric                                                   raw_ostream &OS) {
6600b57cec5SDimitry Andric   // The current approach to inline asm is that "r" constraints are expressed
6610b57cec5SDimitry Andric   // as local indices, rather than values on the operand stack. This simplifies
6620b57cec5SDimitry Andric   // using "r" as it eliminates the need to push and pop the values in a
6630b57cec5SDimitry Andric   // particular order, however it also makes it impossible to have an "m"
6640b57cec5SDimitry Andric   // constraint. So we don't support it.
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric   return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric // Force static initialization.
670480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmPrinter() {
6710b57cec5SDimitry Andric   RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
6720b57cec5SDimitry Andric   RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
6730b57cec5SDimitry Andric }
674