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