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 "InstPrinter/WebAssemblyInstPrinter.h"
18 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
19 #include "MCTargetDesc/WebAssemblyTargetStreamer.h"
20 #include "WebAssembly.h"
21 #include "WebAssemblyMCInstLower.h"
22 #include "WebAssemblyMachineFunctionInfo.h"
23 #include "WebAssemblyRegisterInfo.h"
24 #include "WebAssemblyTargetMachine.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/BinaryFormat/Wasm.h"
28 #include "llvm/CodeGen/Analysis.h"
29 #include "llvm/CodeGen/AsmPrinter.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/DebugInfoMetadata.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Metadata.h"
37 #include "llvm/MC/MCContext.h"
38 #include "llvm/MC/MCSectionWasm.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbol.h"
41 #include "llvm/MC/MCSymbolWasm.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/raw_ostream.h"
45 
46 using namespace llvm;
47 
48 #define DEBUG_TYPE "asm-printer"
49 
50 extern cl::opt<bool> WasmKeepRegisters;
51 
52 //===----------------------------------------------------------------------===//
53 // Helpers.
54 //===----------------------------------------------------------------------===//
55 
56 MVT WebAssemblyAsmPrinter::getRegType(unsigned RegNo) const {
57   const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
58   const TargetRegisterClass *TRC = MRI->getRegClass(RegNo);
59   for (MVT T : {MVT::i32, MVT::i64, MVT::f32, MVT::f64, MVT::v16i8, MVT::v8i16,
60                 MVT::v4i32, MVT::v2i64, MVT::v4f32, MVT::v2f64})
61     if (TRI->isTypeLegalForClass(*TRC, T))
62       return T;
63   LLVM_DEBUG(errs() << "Unknown type for register number: " << RegNo);
64   llvm_unreachable("Unknown register type");
65   return MVT::Other;
66 }
67 
68 std::string WebAssemblyAsmPrinter::regToString(const MachineOperand &MO) {
69   unsigned RegNo = MO.getReg();
70   assert(TargetRegisterInfo::isVirtualRegister(RegNo) &&
71          "Unlowered physical register encountered during assembly printing");
72   assert(!MFI->isVRegStackified(RegNo));
73   unsigned WAReg = MFI->getWAReg(RegNo);
74   assert(WAReg != WebAssemblyFunctionInfo::UnusedReg);
75   return '$' + utostr(WAReg);
76 }
77 
78 WebAssemblyTargetStreamer *WebAssemblyAsmPrinter::getTargetStreamer() {
79   MCTargetStreamer *TS = OutStreamer->getTargetStreamer();
80   return static_cast<WebAssemblyTargetStreamer *>(TS);
81 }
82 
83 //===----------------------------------------------------------------------===//
84 // WebAssemblyAsmPrinter Implementation.
85 //===----------------------------------------------------------------------===//
86 
87 void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) {
88   for (auto &It : OutContext.getSymbols()) {
89     // Emit a .globaltype and .eventtype declaration.
90     auto Sym = cast<MCSymbolWasm>(It.getValue());
91     if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL)
92       getTargetStreamer()->emitGlobalType(Sym);
93     else if (Sym->getType() == wasm::WASM_SYMBOL_TYPE_EVENT)
94       getTargetStreamer()->emitEventType(Sym);
95   }
96 
97   for (const auto &F : M) {
98     // Emit function type info for all undefined functions
99     if (F.isDeclarationForLinker() && !F.isIntrinsic()) {
100       SmallVector<MVT, 4> Results;
101       SmallVector<MVT, 4> Params;
102       computeSignatureVTs(F.getFunctionType(), F, TM, Params, Results);
103       auto *Sym = cast<MCSymbolWasm>(getSymbol(&F));
104       Sym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
105       if (!Sym->getSignature()) {
106         auto Signature = signatureFromMVTs(Results, Params);
107         Sym->setSignature(Signature.get());
108         addSignature(std::move(Signature));
109       }
110       // FIXME: this was originally intended for post-linking and was only used
111       // for imports that were only called indirectly (i.e. s2wasm could not
112       // infer the type from a call). With object files it applies to all
113       // imports. so fix the names and the tests, or rethink how import
114       // delcarations work in asm files.
115       getTargetStreamer()->emitFunctionType(Sym);
116 
117       if (TM.getTargetTriple().isOSBinFormatWasm() &&
118           F.hasFnAttribute("wasm-import-module")) {
119         StringRef Name =
120             F.getFnAttribute("wasm-import-module").getValueAsString();
121         Sym->setImportModule(Name);
122         getTargetStreamer()->emitImportModule(Sym, Name);
123       }
124       if (TM.getTargetTriple().isOSBinFormatWasm() &&
125           F.hasFnAttribute("wasm-import-name")) {
126         StringRef Name =
127             F.getFnAttribute("wasm-import-name").getValueAsString();
128         Sym->setImportName(Name);
129         getTargetStreamer()->emitImportName(Sym, Name);
130       }
131     }
132   }
133 
134   for (const auto &G : M.globals()) {
135     if (!G.hasInitializer() && G.hasExternalLinkage()) {
136       if (G.getValueType()->isSized()) {
137         uint16_t Size = M.getDataLayout().getTypeAllocSize(G.getValueType());
138         OutStreamer->emitELFSize(getSymbol(&G),
139                                  MCConstantExpr::create(Size, OutContext));
140       }
141     }
142   }
143 
144   if (const NamedMDNode *Named = M.getNamedMetadata("wasm.custom_sections")) {
145     for (const Metadata *MD : Named->operands()) {
146       const auto *Tuple = dyn_cast<MDTuple>(MD);
147       if (!Tuple || Tuple->getNumOperands() != 2)
148         continue;
149       const MDString *Name = dyn_cast<MDString>(Tuple->getOperand(0));
150       const MDString *Contents = dyn_cast<MDString>(Tuple->getOperand(1));
151       if (!Name || !Contents)
152         continue;
153 
154       OutStreamer->PushSection();
155       std::string SectionName = (".custom_section." + Name->getString()).str();
156       MCSectionWasm *MySection =
157           OutContext.getWasmSection(SectionName, SectionKind::getMetadata());
158       OutStreamer->SwitchSection(MySection);
159       OutStreamer->EmitBytes(Contents->getString());
160       OutStreamer->PopSection();
161     }
162   }
163 
164   EmitProducerInfo(M);
165   EmitTargetFeatures(M);
166 }
167 
168 void WebAssemblyAsmPrinter::EmitProducerInfo(Module &M) {
169   llvm::SmallVector<std::pair<std::string, std::string>, 4> Languages;
170   if (const NamedMDNode *Debug = M.getNamedMetadata("llvm.dbg.cu")) {
171     llvm::SmallSet<StringRef, 4> SeenLanguages;
172     for (size_t I = 0, E = Debug->getNumOperands(); I < E; ++I) {
173       const auto *CU = cast<DICompileUnit>(Debug->getOperand(I));
174       StringRef Language = dwarf::LanguageString(CU->getSourceLanguage());
175       Language.consume_front("DW_LANG_");
176       if (SeenLanguages.insert(Language).second)
177         Languages.emplace_back(Language.str(), "");
178     }
179   }
180 
181   llvm::SmallVector<std::pair<std::string, std::string>, 4> Tools;
182   if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
183     llvm::SmallSet<StringRef, 4> SeenTools;
184     for (size_t I = 0, E = Ident->getNumOperands(); I < E; ++I) {
185       const auto *S = cast<MDString>(Ident->getOperand(I)->getOperand(0));
186       std::pair<StringRef, StringRef> Field = S->getString().split("version");
187       StringRef Name = Field.first.trim();
188       StringRef Version = Field.second.trim();
189       if (SeenTools.insert(Name).second)
190         Tools.emplace_back(Name.str(), Version.str());
191     }
192   }
193 
194   int FieldCount = int(!Languages.empty()) + int(!Tools.empty());
195   if (FieldCount != 0) {
196     MCSectionWasm *Producers = OutContext.getWasmSection(
197         ".custom_section.producers", SectionKind::getMetadata());
198     OutStreamer->PushSection();
199     OutStreamer->SwitchSection(Producers);
200     OutStreamer->EmitULEB128IntValue(FieldCount);
201     for (auto &Producers : {std::make_pair("language", &Languages),
202             std::make_pair("processed-by", &Tools)}) {
203       if (Producers.second->empty())
204         continue;
205       OutStreamer->EmitULEB128IntValue(strlen(Producers.first));
206       OutStreamer->EmitBytes(Producers.first);
207       OutStreamer->EmitULEB128IntValue(Producers.second->size());
208       for (auto &Producer : *Producers.second) {
209         OutStreamer->EmitULEB128IntValue(Producer.first.size());
210         OutStreamer->EmitBytes(Producer.first);
211         OutStreamer->EmitULEB128IntValue(Producer.second.size());
212         OutStreamer->EmitBytes(Producer.second);
213       }
214     }
215     OutStreamer->PopSection();
216   }
217 }
218 
219 void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
220   struct FeatureEntry {
221     uint8_t Prefix;
222     StringRef Name;
223   };
224 
225   // Read target features and linkage policies from module metadata
226   SmallVector<FeatureEntry, 4> EmittedFeatures;
227   for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) {
228     std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str();
229     Metadata *Policy = M.getModuleFlag(MDKey);
230     if (Policy == nullptr)
231       continue;
232 
233     FeatureEntry Entry;
234     Entry.Prefix = 0;
235     Entry.Name = KV.Key;
236 
237     if (auto *MD = cast<ConstantAsMetadata>(Policy))
238       if (auto *I = cast<ConstantInt>(MD->getValue()))
239         Entry.Prefix = I->getZExtValue();
240 
241     // Silently ignore invalid metadata
242     if (Entry.Prefix != wasm::WASM_FEATURE_PREFIX_USED &&
243         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_REQUIRED &&
244         Entry.Prefix != wasm::WASM_FEATURE_PREFIX_DISALLOWED)
245       continue;
246 
247     EmittedFeatures.push_back(Entry);
248   }
249 
250   if (EmittedFeatures.size() == 0)
251     return;
252 
253   // Emit features and linkage policies into the "target_features" section
254   MCSectionWasm *FeaturesSection = OutContext.getWasmSection(
255       ".custom_section.target_features", SectionKind::getMetadata());
256   OutStreamer->PushSection();
257   OutStreamer->SwitchSection(FeaturesSection);
258 
259   OutStreamer->EmitULEB128IntValue(EmittedFeatures.size());
260   for (auto &F : EmittedFeatures) {
261     OutStreamer->EmitIntValue(F.Prefix, 1);
262     OutStreamer->EmitULEB128IntValue(F.Name.size());
263     OutStreamer->EmitBytes(F.Name);
264   }
265 
266   OutStreamer->PopSection();
267 }
268 
269 void WebAssemblyAsmPrinter::EmitConstantPool() {
270   assert(MF->getConstantPool()->getConstants().empty() &&
271          "WebAssembly disables constant pools");
272 }
273 
274 void WebAssemblyAsmPrinter::EmitJumpTableInfo() {
275   // Nothing to do; jump tables are incorporated into the instruction stream.
276 }
277 
278 void WebAssemblyAsmPrinter::EmitFunctionBodyStart() {
279   const Function &F = MF->getFunction();
280   SmallVector<MVT, 1> ResultVTs;
281   SmallVector<MVT, 4> ParamVTs;
282   computeSignatureVTs(F.getFunctionType(), F, TM, ParamVTs, ResultVTs);
283   auto Signature = signatureFromMVTs(ResultVTs, ParamVTs);
284   auto *WasmSym = cast<MCSymbolWasm>(CurrentFnSym);
285   WasmSym->setSignature(Signature.get());
286   addSignature(std::move(Signature));
287   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
288 
289   // FIXME: clean up how params and results are emitted (use signatures)
290   getTargetStreamer()->emitFunctionType(WasmSym);
291 
292   // Emit the function index.
293   if (MDNode *Idx = F.getMetadata("wasm.index")) {
294     assert(Idx->getNumOperands() == 1);
295 
296     getTargetStreamer()->emitIndIdx(AsmPrinter::lowerConstant(
297         cast<ConstantAsMetadata>(Idx->getOperand(0))->getValue()));
298   }
299 
300   SmallVector<wasm::ValType, 16> Locals;
301   valTypesFromMVTs(MFI->getLocals(), Locals);
302   getTargetStreamer()->emitLocal(Locals);
303 
304   AsmPrinter::EmitFunctionBodyStart();
305 }
306 
307 void WebAssemblyAsmPrinter::EmitInstruction(const MachineInstr *MI) {
308   LLVM_DEBUG(dbgs() << "EmitInstruction: " << *MI << '\n');
309 
310   switch (MI->getOpcode()) {
311   case WebAssembly::ARGUMENT_i32:
312   case WebAssembly::ARGUMENT_i32_S:
313   case WebAssembly::ARGUMENT_i64:
314   case WebAssembly::ARGUMENT_i64_S:
315   case WebAssembly::ARGUMENT_f32:
316   case WebAssembly::ARGUMENT_f32_S:
317   case WebAssembly::ARGUMENT_f64:
318   case WebAssembly::ARGUMENT_f64_S:
319   case WebAssembly::ARGUMENT_v16i8:
320   case WebAssembly::ARGUMENT_v16i8_S:
321   case WebAssembly::ARGUMENT_v8i16:
322   case WebAssembly::ARGUMENT_v8i16_S:
323   case WebAssembly::ARGUMENT_v4i32:
324   case WebAssembly::ARGUMENT_v4i32_S:
325   case WebAssembly::ARGUMENT_v2i64:
326   case WebAssembly::ARGUMENT_v2i64_S:
327   case WebAssembly::ARGUMENT_v4f32:
328   case WebAssembly::ARGUMENT_v4f32_S:
329   case WebAssembly::ARGUMENT_v2f64:
330   case WebAssembly::ARGUMENT_v2f64_S:
331     // These represent values which are live into the function entry, so there's
332     // no instruction to emit.
333     break;
334   case WebAssembly::FALLTHROUGH_RETURN_I32:
335   case WebAssembly::FALLTHROUGH_RETURN_I32_S:
336   case WebAssembly::FALLTHROUGH_RETURN_I64:
337   case WebAssembly::FALLTHROUGH_RETURN_I64_S:
338   case WebAssembly::FALLTHROUGH_RETURN_F32:
339   case WebAssembly::FALLTHROUGH_RETURN_F32_S:
340   case WebAssembly::FALLTHROUGH_RETURN_F64:
341   case WebAssembly::FALLTHROUGH_RETURN_F64_S:
342   case WebAssembly::FALLTHROUGH_RETURN_v16i8:
343   case WebAssembly::FALLTHROUGH_RETURN_v16i8_S:
344   case WebAssembly::FALLTHROUGH_RETURN_v8i16:
345   case WebAssembly::FALLTHROUGH_RETURN_v8i16_S:
346   case WebAssembly::FALLTHROUGH_RETURN_v4i32:
347   case WebAssembly::FALLTHROUGH_RETURN_v4i32_S:
348   case WebAssembly::FALLTHROUGH_RETURN_v2i64:
349   case WebAssembly::FALLTHROUGH_RETURN_v2i64_S:
350   case WebAssembly::FALLTHROUGH_RETURN_v4f32:
351   case WebAssembly::FALLTHROUGH_RETURN_v4f32_S:
352   case WebAssembly::FALLTHROUGH_RETURN_v2f64:
353   case WebAssembly::FALLTHROUGH_RETURN_v2f64_S: {
354     // These instructions represent the implicit return at the end of a
355     // function body. Always pops one value off the stack.
356     if (isVerbose()) {
357       OutStreamer->AddComment("fallthrough-return-value");
358       OutStreamer->AddBlankLine();
359     }
360     break;
361   }
362   case WebAssembly::FALLTHROUGH_RETURN_VOID:
363   case WebAssembly::FALLTHROUGH_RETURN_VOID_S:
364     // This instruction represents the implicit return at the end of a
365     // function body with no return value.
366     if (isVerbose()) {
367       OutStreamer->AddComment("fallthrough-return-void");
368       OutStreamer->AddBlankLine();
369     }
370     break;
371   case WebAssembly::EXTRACT_EXCEPTION_I32:
372   case WebAssembly::EXTRACT_EXCEPTION_I32_S:
373     // These are pseudo instructions that simulates popping values from stack.
374     // We print these only when we have -wasm-keep-registers on for assembly
375     // readability.
376     if (!WasmKeepRegisters)
377       break;
378     LLVM_FALLTHROUGH;
379   default: {
380     WebAssemblyMCInstLower MCInstLowering(OutContext, *this);
381     MCInst TmpInst;
382     MCInstLowering.lower(MI, TmpInst);
383     EmitToStreamer(*OutStreamer, TmpInst);
384     break;
385   }
386   }
387 }
388 
389 bool WebAssemblyAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
390                                             unsigned OpNo, unsigned AsmVariant,
391                                             const char *ExtraCode,
392                                             raw_ostream &OS) {
393   if (AsmVariant != 0)
394     report_fatal_error("There are no defined alternate asm variants");
395 
396   // First try the generic code, which knows about modifiers like 'c' and 'n'.
397   if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS))
398     return false;
399 
400   if (!ExtraCode) {
401     const MachineOperand &MO = MI->getOperand(OpNo);
402     switch (MO.getType()) {
403     case MachineOperand::MO_Immediate:
404       OS << MO.getImm();
405       return false;
406     case MachineOperand::MO_Register:
407       // FIXME: only opcode that still contains registers, as required by
408       // MachineInstr::getDebugVariable().
409       assert(MI->getOpcode() == WebAssembly::INLINEASM);
410       OS << regToString(MO);
411       return false;
412     case MachineOperand::MO_GlobalAddress:
413       getSymbol(MO.getGlobal())->print(OS, MAI);
414       printOffset(MO.getOffset(), OS);
415       return false;
416     case MachineOperand::MO_ExternalSymbol:
417       GetExternalSymbolSymbol(MO.getSymbolName())->print(OS, MAI);
418       printOffset(MO.getOffset(), OS);
419       return false;
420     case MachineOperand::MO_MachineBasicBlock:
421       MO.getMBB()->getSymbol()->print(OS, MAI);
422       return false;
423     default:
424       break;
425     }
426   }
427 
428   return true;
429 }
430 
431 bool WebAssemblyAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
432                                                   unsigned OpNo,
433                                                   unsigned AsmVariant,
434                                                   const char *ExtraCode,
435                                                   raw_ostream &OS) {
436   if (AsmVariant != 0)
437     report_fatal_error("There are no defined alternate asm variants");
438 
439   // The current approach to inline asm is that "r" constraints are expressed
440   // as local indices, rather than values on the operand stack. This simplifies
441   // using "r" as it eliminates the need to push and pop the values in a
442   // particular order, however it also makes it impossible to have an "m"
443   // constraint. So we don't support it.
444 
445   return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
446 }
447 
448 // Force static initialization.
449 extern "C" void LLVMInitializeWebAssemblyAsmPrinter() {
450   RegisterAsmPrinter<WebAssemblyAsmPrinter> X(getTheWebAssemblyTarget32());
451   RegisterAsmPrinter<WebAssemblyAsmPrinter> Y(getTheWebAssemblyTarget64());
452 }
453