1 //===-- WebAssemblyAsmPrinter.cpp - WebAssembly LLVM assembly writer ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains a printer that converts from our internal
11 /// representation of machine-dependent LLVM code to the WebAssembly assembly
12 /// language.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "WebAssemblyAsmPrinter.h"
17 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18 #include "MCTargetDesc/WebAssemblyTargetStreamer.h"
19 #include "TargetInfo/WebAssemblyTargetInfo.h"
20 #include "Utils/WebAssemblyTypeUtilities.h"
21 #include "WebAssembly.h"
22 #include "WebAssemblyMCInstLower.h"
23 #include "WebAssemblyMachineFunctionInfo.h"
24 #include "WebAssemblyRegisterInfo.h"
25 #include "WebAssemblyRuntimeLibcallSignatures.h"
26 #include "WebAssemblyTargetMachine.h"
27 #include "WebAssemblyUtilities.h"
28 #include "llvm/ADT/MapVector.h"
29 #include "llvm/ADT/SmallSet.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Analysis/ValueTracking.h"
32 #include "llvm/BinaryFormat/Wasm.h"
33 #include "llvm/CodeGen/Analysis.h"
34 #include "llvm/CodeGen/AsmPrinter.h"
35 #include "llvm/CodeGen/MachineConstantPool.h"
36 #include "llvm/CodeGen/MachineInstr.h"
37 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
38 #include "llvm/IR/DataLayout.h"
39 #include "llvm/IR/DebugInfoMetadata.h"
40 #include "llvm/IR/GlobalVariable.h"
41 #include "llvm/IR/Metadata.h"
42 #include "llvm/MC/MCContext.h"
43 #include "llvm/MC/MCSectionWasm.h"
44 #include "llvm/MC/MCStreamer.h"
45 #include "llvm/MC/MCSymbol.h"
46 #include "llvm/MC/MCSymbolWasm.h"
47 #include "llvm/MC/TargetRegistry.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/raw_ostream.h"
50 
51 using namespace llvm;
52 
53 #define DEBUG_TYPE "asm-printer"
54 
55 extern cl::opt<bool> WasmKeepRegisters;
56 
57 //===----------------------------------------------------------------------===//
58 // Helpers.
59 //===----------------------------------------------------------------------===//
60 
getRegType(unsigned RegNo) const61 MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
62   const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
63   const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
64   for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
65                 MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64})
66     if (TRI->isTypeLegalForClass(*TRC, T))
67       return T;
68   LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
69   llvm_unreachable("Unknown register type");
70   return MVT::Other;
71 }
72 
regToString(const MachineOperand & MO)73 std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
74   Register RegNo = MO.getReg();
75   assert(RegNo.isVirtual() &&
76          "Unlowered physical register encountered during assembly printing");
77   assert(!MFI->isVRegStackified(RegNo));
78   unsigned WAReg = MFI->getWAReg(RegNo);
79   assert(WAReg != WebAssembly::UnusedReg);
80   return '$' + utostr(WAReg);
81 }
82 
getTargetStreamer()83 WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
84   MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
85   return static_cast<WebAssemblyTargetStreamer *>(TS);
86 }
87 
88 // Emscripten exception handling helpers
89 //
90 // This converts invoke names generated by LowerEmscriptenEHSjLj to real names
91 // that are expected by JavaScript glue code. The invoke names generated by
92 // Emscripten JS glue code are based on their argument and return types; for
93 // example, for a function that takes an i32 and returns nothing, it is
94 // 'invoke_vi'. But the format of invoke generated by LowerEmscriptenEHSjLj pass
95 // contains a mangled string generated from their IR types, for example,
96 // "__invoke_void_%struct.mystruct*_int", because final wasm types are not
97 // available in the IR pass. So we convert those names to the form that
98 // Emscripten JS code expects.
99 //
100 // Refer to LowerEmscriptenEHSjLj pass for more details.
101 
102 // Returns true if the given function name is an invoke name generated by
103 // LowerEmscriptenEHSjLj pass.
isEmscriptenInvokeName(StringRef Name)104 static bool isEmscriptenInvokeName(StringRef Name) {
105   if (Name.front() == '"' && Name.back() == '"')
106     Name = Name.substr(1, Name.size() - 2);
107   return Name.starts_with("__invoke_");
108 }
109 
110 // Returns a character that represents the given wasm value type in invoke
111 // signatures.
getInvokeSig(wasm::ValType VT)112 static char getInvokeSig(wasm::ValType VT) {
113   switch (VT) {
114   case wasm::ValType::I32:
115     return 'i';
116   case wasm::ValType::I64:
117     return 'j';
118   case wasm::ValType::F32:
119     return 'f';
120   case wasm::ValType::F64:
121     return 'd';
122   case wasm::ValType::V128:
123     return 'V';
124   case wasm::ValType::FUNCREF:
125     return 'F';
126   case wasm::ValType::EXTERNREF:
127     return 'X';
128   }
129   llvm_unreachable("Unhandled wasm::ValType enum");
130 }
131 
132 // Given the wasm signature, generate the invoke name in the format JS glue code
133 // expects.
getEmscriptenInvokeSymbolName(wasm::WasmSignature * Sig)134 static std::string getEmscriptenInvokeSymbolName(wasm::WasmSignature *Sig) {
135   assert(Sig->Returns.size() <= 1);
136   std::string Ret = "invoke_";
137   if (!Sig->Returns.empty())
138     for (auto VT : Sig->Returns)
139       Ret += getInvokeSig(VT);
140   else
141     Ret += 'v';
142   // Invokes' first argument is a pointer to the original function, so skip it
143   for (unsigned I = 1, E = Sig->Params.size(); I < E; I++)
144     Ret += getInvokeSig(Sig->Params[I]);
145   return Ret;
146 }
147 
148 //===----------------------------------------------------------------------===//
149 // WebAssemblyAsmPrinter Implementation.
150 //===----------------------------------------------------------------------===//
151 
getMCSymbolForFunction(const Function * F,bool EnableEmEH,wasm::WasmSignature * Sig,bool & InvokeDetected)152 MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction(
153     const Function *F, bool EnableEmEH, wasm::WasmSignature *Sig,
154     bool &InvokeDetected) {
155   MCSymbolWasm *WasmSym = nullptr;
156   if (EnableEmEH && isEmscriptenInvokeName(F->getName())) {
157     assert(Sig);
158     InvokeDetected = true;
159     if (Sig->Returns.size() > 1) {
160       std::string Msg =
161           "Emscripten EH/SjLj does not support multivalue returns: " +
162           std::string(F->getName()) + ": " +
163           WebAssembly::signatureToString(Sig);
164       report_fatal_error(Twine(Msg));
165     }
166     WasmSym = cast<MCSymbolWasm>(
167         GetExternalSymbolSymbol(getEmscriptenInvokeSymbolName(Sig)));
168   } else {
169     WasmSym = cast<MCSymbolWasm>(getSymbol(F));
170   }
171   return WasmSym;
172 }
173 
emitGlobalVariable(const GlobalVariable * GV)174 void WebAssemblyAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
175   if (!WebAssembly::isWasmVarAddressSpace(GV->getAddressSpace())) {
176     AsmPrinter::emitGlobalVariable(GV);
177     return;
178   }
179 
180   assert(!GV->isThreadLocal());
181 
182   MCSymbolWasm *Sym = cast<MCSymbolWasm>(getSymbol(GV));
183 
184   if (!Sym->getType()) {
185     SmallVector<MVT, 1> VTs;
186     Type *GlobalVT = GV->getValueType();
187     if (Subtarget) {
188       // Subtarget is only set when a function is defined, because
189       // each function can declare a different subtarget. For example,
190       // on ARM a compilation unit might have a function on ARM and
191       // another on Thumb. Therefore only if Subtarget is non-null we
192       // can actually calculate the legal VTs.
193       const WebAssemblyTargetLowering &TLI = *Subtarget->getTargetLowering();
194       computeLegalValueVTs(TLI, GV->getParent()->getContext(),
195                            GV->getParent()->getDataLayout(), GlobalVT, VTs);
196     }
197     WebAssembly::wasmSymbolSetType(Sym, GlobalVT, VTs);
198   }
199 
200   emitVisibility(Sym, GV->getVisibility(), !GV->isDeclaration());
201   emitSymbolType(Sym);
202   if (GV->hasInitializer()) {
203     assert(getSymbolPreferLocal(*GV) == Sym);
204     emitLinkage(GV, Sym);
205     OutStreamer->emitLabel(Sym);
206     // TODO: Actually emit the initializer value.  Otherwise the global has the
207     // default value for its type (0, ref.null, etc).
208     OutStreamer->addBlankLine();
209   }
210 }
211 
getOrCreateWasmSymbol(StringRef Name)212 MCSymbol *WebAssemblyAsmPrinter::getOrCreateWasmSymbol(StringRef Name) {
213   auto *WasmSym = cast<MCSymbolWasm>(GetExternalSymbolSymbol(Name));
214 
215   // May be called multiple times, so early out.
216   if (WasmSym->getType())
217     return WasmSym;
218 
219   const WebAssemblySubtarget &Subtarget = getSubtarget();
220 
221   // Except for certain known symbols, all symbols used by CodeGen are
222   // functions. It's OK to hardcode knowledge of specific symbols here; this
223   // method is precisely there for fetching the signatures of known
224   // Clang-provided symbols.
225   if (Name == "__stack_pointer" || Name == "__tls_base" ||
226       Name == "__memory_base" || Name == "__table_base" ||
227       Name == "__tls_size" || Name == "__tls_align") {
228     bool Mutable =
229         Name == "__stack_pointer" || Name == "__tls_base";
230     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
231     WasmSym->setGlobalType(wasm::WasmGlobalType{
232         uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
233                                       : wasm::WASM_TYPE_I32),
234         Mutable});
235     return WasmSym;
236   }
237 
238   if (Name.starts_with("GCC_except_table")) {
239     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
240     return WasmSym;
241   }
242 
243   SmallVector<wasm::ValType, 4> Returns;
244   SmallVector<wasm::ValType, 4> Params;
245   if (Name == "__cpp_exception" || Name == "__c_longjmp") {
246     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TAG);
247     // In static linking we define tag symbols in WasmException::endModule().
248     // But we may have multiple objects to be linked together, each of which
249     // defines the tag symbols. To resolve them, we declare them as weak. In
250     // dynamic linking we make tag symbols undefined in the backend, define it
251     // in JS, and feed them to each importing module.
252     if (!isPositionIndependent())
253       WasmSym->setWeak(true);
254     WasmSym->setExternal(true);
255 
256     // Currently both C++ exceptions and C longjmps have a single pointer type
257     // param. For C++ exceptions it is a pointer to an exception object, and for
258     // C longjmps it is pointer to a struct that contains a setjmp buffer and a
259     // longjmp return value. We may consider using multiple value parameters for
260     // longjmps later when multivalue support is ready.
261     wasm::ValType AddrType =
262         Subtarget.hasAddr64() ? wasm::ValType::I64 : wasm::ValType::I32;
263     Params.push_back(AddrType);
264   } else { // Function symbols
265     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
266     getLibcallSignature(Subtarget, Name, Returns, Params);
267   }
268   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
269                                                          std::move(Params));
270   WasmSym->setSignature(Signature.get());
271   addSignature(std::move(Signature));
272 
273   return WasmSym;
274 }
275 
emitSymbolType(const MCSymbolWasm * Sym)276 void WebAssemblyAsmPrinter::emitSymbolType(const MCSymbolWasm *Sym) {
277   std::optional<wasm::WasmSymbolType> WasmTy = Sym->getType();
278   if (!WasmTy)
279     return;
280 
281   switch (*WasmTy) {
282   case wasm::WASM_SYMBOL_TYPE_GLOBAL:
283     getTargetStreamer()->emitGlobalType(Sym);
284     break;
285   case wasm::WASM_SYMBOL_TYPE_TAG:
286     getTargetStreamer()->emitTagType(Sym);
287     break;
288   case wasm::WASM_SYMBOL_TYPE_TABLE:
289     getTargetStreamer()->emitTableType(Sym);
290     break;
291   default:
292     break; // We only handle globals, tags and tables here
293   }
294 }
295 
emitDecls(const Module & M)296 void WebAssemblyAsmPrinter::emitDecls(const Module &M) {
297   if (signaturesEmitted)
298     return;
299   signaturesEmitted = true;
300 
301   // Normally symbols for globals get discovered as the MI gets lowered,
302   // but we need to know about them ahead of time. This will however,
303   // only find symbols that have been used. Unused symbols from globals will
304   // not be found here.
305   MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo<MachineModuleInfoWasm>();
306   for (StringRef Name : MMIW.MachineSymbolsUsed) {
307     auto *WasmSym = cast<MCSymbolWasm>(getOrCreateWasmSymbol(Name));
308     if (WasmSym->isFunction()) {
309       // TODO(wvo): is there any case where this overlaps with the call to
310       // emitFunctionType in the loop below?
311       getTargetStreamer()->emitFunctionType(WasmSym);
312     }
313   }
314 
315   for (auto &It : OutContext.getSymbols()) {
316     // Emit .globaltype, .tagtype, or .tabletype declarations for extern
317     // declarations, i.e. those that have only been declared (but not defined)
318     // in the current module
319     auto Sym = cast<MCSymbolWasm>(It.getValue());
320     if (!Sym->isDefined())
321       emitSymbolType(Sym);
322   }
323 
324   DenseSet<MCSymbol *> InvokeSymbols;
325   for (const auto &F : M) {
326     if (F.isIntrinsic())
327       continue;
328 
329     // Emit function type info for all functions. This will emit duplicate
330     // information for defined functions (which already have function type
331     // info emitted alongside their definition), but this is necessary in
332     // order to enable the single-pass WebAssemblyAsmTypeCheck to succeed.
333     SmallVector<MVT, 4> Results;
334     SmallVector<MVT, 4> Params;
335     computeSignatureVTs(F.getFunctionType(), &F, F, TM, Params, Results);
336     // At this point these MCSymbols may or may not have been created already
337     // and thus also contain a signature, but we need to get the signature
338     // anyway here in case it is an invoke that has not yet been created. We
339     // will discard it later if it turns out not to be necessary.
340     auto Signature = signatureFromMVTs(Results, Params);
341     bool InvokeDetected = false;
342     auto *Sym = getMCSymbolForFunction(
343         &F, WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj,
344         Signature.get(), InvokeDetected);
345 
346     // Multiple functions can be mapped to the same invoke symbol. For
347     // example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32'
348     // are both mapped to '__invoke_vi'. We keep them in a set once we emit an
349     // Emscripten EH symbol so we don't emit the same symbol twice.
350     if (InvokeDetected && !InvokeSymbols.insert(Sym).second)
351       continue;
352 
353     Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
354     if (!Sym->getSignature()) {
355       Sym->setSignature(Signature.get());
356       addSignature(std::move(Signature));
357     } else {
358       // This symbol has already been created and had a signature. Discard it.
359       Signature.reset();
360     }
361 
362     getTargetStreamer()->emitFunctionType(Sym);
363 
364     if (F.hasFnAttribute("wasm-import-module")) {
365       StringRef Name =
366           F.getFnAttribute("wasm-import-module").getValueAsString();
367       Sym->setImportModule(storeName(Name));
368       getTargetStreamer()->emitImportModule(Sym, Name);
369     }
370     if (F.hasFnAttribute("wasm-import-name")) {
371       // If this is a converted Emscripten EH/SjLj symbol, we shouldn't use
372       // the original function name but the converted symbol name.
373       StringRef Name =
374           InvokeDetected
375               ? Sym->getName()
376               : F.getFnAttribute("wasm-import-name").getValueAsString();
377       Sym->setImportName(storeName(Name));
378       getTargetStreamer()->emitImportName(Sym, Name);
379     }
380 
381     if (F.hasFnAttribute("wasm-export-name")) {
382       auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
383       StringRef Name = F.getFnAttribute("wasm-export-name").getValueAsString();
384       Sym->setExportName(storeName(Name));
385       getTargetStreamer()->emitExportName(Sym, Name);
386     }
387   }
388 }
389 
emitEndOfAsmFile(Module & M)390 void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) {
391   // This is required to emit external declarations (like .functypes) when
392   // no functions are defined in the compilation unit and therefore,
393   // emitDecls() is not called until now.
394   emitDecls(M);
395 
396   // When a function's address is taken, a TABLE_INDEX relocation is emitted
397   // against the function symbol at the use site.  However the relocation
398   // doesn't explicitly refer to the table.  In the future we may want to
399   // define a new kind of reloc against both the function and the table, so
400   // that the linker can see that the function symbol keeps the table alive,
401   // but for now manually mark the table as live.
402   for (const auto &F : M) {
403     if (!F.isIntrinsic() && F.hasAddressTaken()) {
404       MCSymbolWasm *FunctionTable =
405           WebAssembly::getOrCreateFunctionTableSymbol(OutContext, Subtarget);
406       OutStreamer->emitSymbolAttribute(FunctionTable, MCSA_NoDeadStrip);
407       break;
408     }
409   }
410 
411   for (const auto &G : M.globals()) {
412     if (!G.hasInitializer() && G.hasExternalLinkage() &&
413         !WebAssembly::isWasmVarAddressSpace(G.getAddressSpace()) &&
414         G.getValueType()->isSized()) {
415       uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
416       OutStreamer->emitELFSize(getSymbol(&G),
417                                MCConstantExpr::create(Size, OutContext));
418     }
419   }
420 
421   if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
422     for (const Metadata *MD : Named->operands()) {
423       const auto *Tuple = dyn_cast<MDTuple>(MD);
424       if (!Tuple || Tuple->getNumOperands() != 2)
425         continue;
426       const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
427       const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
428       if (!Name || !Contents)
429         continue;
430 
431       OutStreamer->pushSection();
432       std::string SectionName = (".custom_section." + Name->getString()).str();
433       MCSectionWasm *MySection =
434           OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
435       OutStreamer->switchSection(MySection);
436       OutStreamer->emitBytes(Contents->getString());
437       OutStreamer->popSection();
438     }
439   }
440 
441   EmitProducerInfo(M);
442   EmitTargetFeatures(M);
443   EmitFunctionAttributes(M);
444 }
445 
EmitProducerInfo(Module & M)446 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
447   llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages;
448   if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) {
449     llvm::SmallSet<StringRef, 4> SeenLanguages;
450     for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) {
451       const auto *CU = cast<DICompileUnit>(Debug->getOperand(I));
452       StringRef Language = dwarf::LanguageString(CU->getSourceLanguage());
453       Language.consume_front("DW_LANG_");
454       if (SeenLanguages.insert(Language).second)
455         Languages.emplace_back(Language.str(), "");
456     }
457   }
458 
459   llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools;
460   if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
461     llvm::SmallSet<StringRef, 4> SeenTools;
462     for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) {
463       const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0));
464       std::pair<StringRef, StringRef> Field = S->getString().split("version");
465       StringRef Name = Field.first.trim();
466       StringRef Version = Field.second.trim();
467       if (SeenTools.insert(Name).second)
468         Tools.emplace_back(Name.str(), Version.str());
469     }
470   }
471 
472   int FieldCount = int(!Languages.empty()) + int(!Tools.empty());
473   if (FieldCount != 0) {
474     MCSectionWasm *Producers = OutContext.getWasmSection(
475         ".custom_section.producers", SectionKind::getMetadata());
476     OutStreamer->pushSection();
477     OutStreamer->switchSection(Producers);
478     OutStreamer->emitULEB128IntValue(FieldCount);
479     for (auto &Producers : {std::make_pair("language", &Languages),
480             std::make_pair("processed-by", &Tools)}) {
481       if (Producers.second->empty())
482         continue;
483       OutStreamer->emitULEB128IntValue(strlen(Producers.first));
484       OutStreamer->emitBytes(Producers.first);
485       OutStreamer->emitULEB128IntValue(Producers.second->size());
486       for (auto &Producer : *Producers.second) {
487         OutStreamer->emitULEB128IntValue(Producer.first.size());
488         OutStreamer->emitBytes(Producer.first);
489         OutStreamer->emitULEB128IntValue(Producer.second.size());
490         OutStreamer->emitBytes(Producer.second);
491       }
492     }
493     OutStreamer->popSection();
494   }
495 }
496 
EmitTargetFeatures(Module & M)497 void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
498   struct FeatureEntry {
499     uint8_t Prefix;
500     std::string Name;
501   };
502 
503   // Read target features and linkage policies from module metadata
504   SmallVector<FeatureEntry, 4> EmittedFeatures;
505   auto EmitFeature = [&](std::string Feature) {
506     std::string MDKey = (StringRef("wasm-feature-") + Feature).str();
507     Metadata *Policy = M.getModuleFlag(MDKey);
508     if (Policy == nullptr)
509       return;
510 
511     FeatureEntry Entry;
512     Entry.Prefix = 0;
513     Entry.Name = Feature;
514 
515     if (auto *MD = cast<ConstantAsMetadata>(Policy))
516       if (auto *I = cast<ConstantInt>(MD->getValue()))
517         Entry.Prefix = I->getZExtValue();
518 
519     // Silently ignore invalid metadata
520     if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED &&
521         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED &&
522         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED)
523       return;
524 
525     EmittedFeatures.push_back(Entry);
526   };
527 
528   for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
529     EmitFeature(KV.Key);
530   }
531   // This pseudo-feature tells the linker whether shared memory would be safe
532   EmitFeature("shared-mem");
533 
534   // This is an "architecture", not a "feature", but we emit it as such for
535   // the benefit of tools like Binaryen and consistency with other producers.
536   // FIXME: Subtarget is null here, so can't Subtarget->hasAddr64() ?
537   if (M.getDataLayout().getPointerSize() == 8) {
538     // Can't use EmitFeature since "wasm-feature-memory64" is not a module
539     // flag.
540     EmittedFeatures.push_back({wasm::WASM_FEATURE_PREFIX_USED, "memory64"});
541   }
542 
543   if (EmittedFeatures.size() == 0)
544     return;
545 
546   // Emit features and linkage policies into the "target_features" section
547   MCSectionWasm *FeaturesSection = OutContext.getWasmSection(
548       ".custom_section.target_features", SectionKind::getMetadata());
549   OutStreamer->pushSection();
550   OutStreamer->switchSection(FeaturesSection);
551 
552   OutStreamer->emitULEB128IntValue(EmittedFeatures.size());
553   for (auto &F : EmittedFeatures) {
554     OutStreamer->emitIntValue(F.Prefix, 1);
555     OutStreamer->emitULEB128IntValue(F.Name.size());
556     OutStreamer->emitBytes(F.Name);
557   }
558 
559   OutStreamer->popSection();
560 }
561 
EmitFunctionAttributes(Module & M)562 void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {
563   auto V = M.getNamedGlobal("llvm.global.annotations");
564   if (!V)
565     return;
566 
567   // Group all the custom attributes by name.
568   MapVector<StringRef, SmallVector<MCSymbol *, 4>> CustomSections;
569   const ConstantArray *CA = cast<ConstantArray>(V->getOperand(0));
570   for (Value *Op : CA->operands()) {
571     auto *CS = cast<ConstantStruct>(Op);
572     // The first field is a pointer to the annotated variable.
573     Value *AnnotatedVar = CS->getOperand(0)->stripPointerCasts();
574     // Only annotated functions are supported for now.
575     if (!isa<Function>(AnnotatedVar))
576       continue;
577     auto *F = cast<Function>(AnnotatedVar);
578 
579     // The second field is a pointer to a global annotation string.
580     auto *GV = cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
581     StringRef AnnotationString;
582     getConstantStringInfo(GV, AnnotationString);
583     auto *Sym = cast<MCSymbolWasm>(getSymbol(F));
584     CustomSections[AnnotationString].push_back(Sym);
585   }
586 
587   // Emit a custom section for each unique attribute.
588   for (const auto &[Name, Symbols] : CustomSections) {
589     MCSectionWasm *CustomSection = OutContext.getWasmSection(
590         ".custom_section.llvm.func_attr.annotate." + Name, SectionKind::getMetadata());
591     OutStreamer->pushSection();
592     OutStreamer->switchSection(CustomSection);
593 
594     for (auto &Sym : Symbols) {
595       OutStreamer->emitValue(
596           MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_WASM_FUNCINDEX,
597                                   OutContext),
598           4);
599     }
600     OutStreamer->popSection();
601   }
602 }
603 
emitConstantPool()604 void WebAssemblyAsmPrinter::emitConstantPool() {
605   emitDecls(*MMI->getModule());
606   assert(MF->getConstantPool()->getConstants().empty() &&
607          "WebAssembly disables constant pools");
608 }
609 
emitJumpTableInfo()610 void WebAssemblyAsmPrinter::emitJumpTableInfo() {
611   // Nothing to do; jump tables are incorporated into the instruction stream.
612 }
613 
emitFunctionBodyStart()614 void WebAssemblyAsmPrinter::emitFunctionBodyStart() {
615   const Function &F = MF->getFunction();
616   SmallVector<MVT, 1> ResultVTs;
617   SmallVector<MVT, 4> ParamVTs;
618   computeSignatureVTs(F.getFunctionType(), &F, F, TM, ParamVTs, ResultVTs);
619 
620   auto Signature = signatureFromMVTs(ResultVTs, ParamVTs);
621   auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym);
622   WasmSym->setSignature(Signature.get());
623   addSignature(std::move(Signature));
624   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
625 
626   getTargetStreamer()->emitFunctionType(WasmSym);
627 
628   // Emit the function index.
629   if (MDNode *Idx = F.getMetadata("wasm.index")) {
630     assert(Idx->getNumOperands() == 1);
631 
632     getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
633         cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
634   }
635 
636   SmallVector<wasm::ValType, 16> Locals;
637   valTypesFromMVTs(MFI->getLocals(), Locals);
638   getTargetStreamer()->emitLocal(Locals);
639 
640   AsmPrinter::emitFunctionBodyStart();
641 }
642 
emitInstruction(const MachineInstr * MI)643 void WebAssemblyAsmPrinter::emitInstruction(const MachineInstr *MI) {
644   LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
645   WebAssembly_MC::verifyInstructionPredicates(MI->getOpcode(),
646                                               Subtarget->getFeatureBits());
647 
648   switch (MI->getOpcode()) {
649   case WebAssembly::ARGUMENT_i32:
650   case WebAssembly::ARGUMENT_i32_S:
651   case WebAssembly::ARGUMENT_i64:
652   case WebAssembly::ARGUMENT_i64_S:
653   case WebAssembly::ARGUMENT_f32:
654   case WebAssembly::ARGUMENT_f32_S:
655   case WebAssembly::ARGUMENT_f64:
656   case WebAssembly::ARGUMENT_f64_S:
657   case WebAssembly::ARGUMENT_v16i8:
658   case WebAssembly::ARGUMENT_v16i8_S:
659   case WebAssembly::ARGUMENT_v8i16:
660   case WebAssembly::ARGUMENT_v8i16_S:
661   case WebAssembly::ARGUMENT_v4i32:
662   case WebAssembly::ARGUMENT_v4i32_S:
663   case WebAssembly::ARGUMENT_v2i64:
664   case WebAssembly::ARGUMENT_v2i64_S:
665   case WebAssembly::ARGUMENT_v4f32:
666   case WebAssembly::ARGUMENT_v4f32_S:
667   case WebAssembly::ARGUMENT_v2f64:
668   case WebAssembly::ARGUMENT_v2f64_S:
669     // These represent values which are live into the function entry, so there's
670     // no instruction to emit.
671     break;
672   case WebAssembly::FALLTHROUGH_RETURN: {
673     // These instructions represent the implicit return at the end of a
674     // function body.
675     if (isVerbose()) {
676       OutStreamer->AddComment("fallthrough-return");
677       OutStreamer->addBlankLine();
678     }
679     break;
680   }
681   case WebAssembly::COMPILER_FENCE:
682     // This is a compiler barrier that prevents instruction reordering during
683     // backend compilation, and should not be emitted.
684     break;
685   default: {
686     WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
687     MCInst TmpInst;
688     MCInstLowering.lower(MI, TmpInst);
689     EmitToStreamer(*OutStreamer, TmpInst);
690     break;
691   }
692   }
693 }
694 
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & OS)695 bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
696                                             unsigned OpNo,
697                                             const char *ExtraCode,
698                                             raw_ostream &OS) {
699   // First try the generic code, which knows about modifiers like 'c' and 'n'.
700   if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
701     return false;
702 
703   if (!ExtraCode) {
704     const MachineOperand &MO = MI->getOperand(OpNo);
705     switch (MO.getType()) {
706     case MachineOperand::MO_Immediate:
707       OS << MO.getImm();
708       return false;
709     case MachineOperand::MO_Register:
710       // FIXME: only opcode that still contains registers, as required by
711       // MachineInstr::getDebugVariable().
712       assert(MI->getOpcode() == WebAssembly::INLINEASM);
713       OS << regToString(MO);
714       return false;
715     case MachineOperand::MO_GlobalAddress:
716       PrintSymbolOperand(MO, OS);
717       return false;
718     case MachineOperand::MO_ExternalSymbol:
719       GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
720       printOffset(MO.getOffset(), OS);
721       return false;
722     case MachineOperand::MO_MachineBasicBlock:
723       MO.getMBB()->getSymbol()->print(OS, MAI);
724       return false;
725     default:
726       break;
727     }
728   }
729 
730   return true;
731 }
732 
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & OS)733 bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
734                                                   unsigned OpNo,
735                                                   const char *ExtraCode,
736                                                   raw_ostream &OS) {
737   // The current approach to inline asm is that "r" constraints are expressed
738   // as local indices, rather than values on the operand stack. This simplifies
739   // using "r" as it eliminates the need to push and pop the values in a
740   // particular order, however it also makes it impossible to have an "m"
741   // constraint. So we don't support it.
742 
743   return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
744 }
745 
746 // Force static initialization.
LLVMInitializeWebAssemblyAsmPrinter()747 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmPrinter() {
748   RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
749   RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
750 }
751