1 // WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
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 code to lower WebAssembly MachineInstrs to their
11 /// corresponding MCInst records.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "WebAssemblyMCInstLower.h"
16 #include "TargetInfo/WebAssemblyTargetInfo.h"
17 #include "Utils/WebAssemblyTypeUtilities.h"
18 #include "Utils/WebAssemblyUtilities.h"
19 #include "WebAssemblyAsmPrinter.h"
20 #include "WebAssemblyMachineFunctionInfo.h"
21 #include "WebAssemblyRuntimeLibcallSignatures.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCSymbolWasm.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm;
33 
34 // This disables the removal of registers when lowering into MC, as required
35 // by some current tests.
36 cl::opt<bool>
37     WasmKeepRegisters("wasm-keep-registers", cl::Hidden,
38                       cl::desc("WebAssembly: output stack registers in"
39                                " instruction output for test purposes only."),
40                       cl::init(false));
41 
42 extern cl::opt<bool> EnableEmException;
43 extern cl::opt<bool> EnableEmSjLj;
44 
45 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
46 
47 MCSymbol *
48 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
49   const GlobalValue *Global = MO.getGlobal();
50   if (!isa<Function>(Global)) {
51     auto *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
52     // If the symbol doesn't have an explicit WasmSymbolType yet and the
53     // GlobalValue is actually a WebAssembly global, then ensure the symbol is a
54     // WASM_SYMBOL_TYPE_GLOBAL.
55     if (WebAssembly::isWasmVarAddressSpace(Global->getAddressSpace()) &&
56         !WasmSym->getType()) {
57       const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
58       const TargetMachine &TM = MF.getTarget();
59       const Function &CurrentFunc = MF.getFunction();
60       SmallVector<MVT, 1> VTs;
61       computeLegalValueVTs(CurrentFunc, TM, Global->getValueType(), VTs);
62       if (VTs.size() != 1)
63         report_fatal_error("Aggregate globals not yet implemented");
64 
65       bool Mutable = true;
66       wasm::ValType Type = WebAssembly::toValType(VTs[0]);
67       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
68       WasmSym->setGlobalType(wasm::WasmGlobalType{uint8_t(Type), Mutable});
69     }
70     return WasmSym;
71   }
72 
73   const auto *FuncTy = cast<FunctionType>(Global->getValueType());
74   const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
75   const TargetMachine &TM = MF.getTarget();
76   const Function &CurrentFunc = MF.getFunction();
77 
78   SmallVector<MVT, 1> ResultMVTs;
79   SmallVector<MVT, 4> ParamMVTs;
80   const auto *const F = dyn_cast<Function>(Global);
81   computeSignatureVTs(FuncTy, F, CurrentFunc, TM, ParamMVTs, ResultMVTs);
82   auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs);
83 
84   bool InvokeDetected = false;
85   auto *WasmSym = Printer.getMCSymbolForFunction(
86       F, EnableEmException || EnableEmSjLj, Signature.get(), InvokeDetected);
87   WasmSym->setSignature(Signature.get());
88   Printer.addSignature(std::move(Signature));
89   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
90   return WasmSym;
91 }
92 
93 MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
94     const MachineOperand &MO) const {
95   const char *Name = MO.getSymbolName();
96   auto *WasmSym = cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
97   const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
98 
99   // Except for certain known symbols, all symbols used by CodeGen are
100   // functions. It's OK to hardcode knowledge of specific symbols here; this
101   // method is precisely there for fetching the signatures of known
102   // Clang-provided symbols.
103   if (strcmp(Name, "__stack_pointer") == 0 || strcmp(Name, "__tls_base") == 0 ||
104       strcmp(Name, "__memory_base") == 0 || strcmp(Name, "__table_base") == 0 ||
105       strcmp(Name, "__tls_size") == 0 || strcmp(Name, "__tls_align") == 0) {
106     bool Mutable =
107         strcmp(Name, "__stack_pointer") == 0 || strcmp(Name, "__tls_base") == 0;
108     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
109     WasmSym->setGlobalType(wasm::WasmGlobalType{
110         uint8_t(Subtarget.hasAddr64() && strcmp(Name, "__table_base") != 0
111                     ? wasm::WASM_TYPE_I64
112                     : wasm::WASM_TYPE_I32),
113         Mutable});
114     return WasmSym;
115   }
116 
117   SmallVector<wasm::ValType, 4> Returns;
118   SmallVector<wasm::ValType, 4> Params;
119   if (strcmp(Name, "__cpp_exception") == 0) {
120     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
121     // We can't confirm its signature index for now because there can be
122     // imported exceptions. Set it to be 0 for now.
123     WasmSym->setEventType(
124         {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
125     // We may have multiple C++ compilation units to be linked together, each of
126     // which defines the exception symbol. To resolve them, we declare them as
127     // weak.
128     WasmSym->setWeak(true);
129     WasmSym->setExternal(true);
130 
131     // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
132     // (for wasm64) param type and void return type. The reaon is, all C++
133     // exception values are pointers, and to share the type section with
134     // functions, exceptions are assumed to have void return type.
135     Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
136                                            : wasm::ValType::I32);
137   } else { // Function symbols
138     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
139     getLibcallSignature(Subtarget, Name, Returns, Params);
140   }
141   auto Signature =
142       std::make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
143   WasmSym->setSignature(Signature.get());
144   Printer.addSignature(std::move(Signature));
145 
146   return WasmSym;
147 }
148 
149 MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
150                                                      MCSymbol *Sym) const {
151   MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
152   unsigned TargetFlags = MO.getTargetFlags();
153 
154   switch (TargetFlags) {
155     case WebAssemblyII::MO_NO_FLAG:
156       break;
157     case WebAssemblyII::MO_GOT:
158       Kind = MCSymbolRefExpr::VK_GOT;
159       break;
160     case WebAssemblyII::MO_MEMORY_BASE_REL:
161       Kind = MCSymbolRefExpr::VK_WASM_MBREL;
162       break;
163     case WebAssemblyII::MO_TLS_BASE_REL:
164       Kind = MCSymbolRefExpr::VK_WASM_TLSREL;
165       break;
166     case WebAssemblyII::MO_TABLE_BASE_REL:
167       Kind = MCSymbolRefExpr::VK_WASM_TBREL;
168       break;
169     default:
170       llvm_unreachable("Unknown target flag on GV operand");
171   }
172 
173   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx);
174 
175   if (MO.getOffset() != 0) {
176     const auto *WasmSym = cast<MCSymbolWasm>(Sym);
177     if (TargetFlags == WebAssemblyII::MO_GOT)
178       report_fatal_error("GOT symbol references do not support offsets");
179     if (WasmSym->isFunction())
180       report_fatal_error("Function addresses with offsets not supported");
181     if (WasmSym->isGlobal())
182       report_fatal_error("Global indexes with offsets not supported");
183     if (WasmSym->isEvent())
184       report_fatal_error("Event indexes with offsets not supported");
185     if (WasmSym->isTable())
186       report_fatal_error("Table indexes with offsets not supported");
187 
188     Expr = MCBinaryExpr::createAdd(
189         Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
190   }
191 
192   return MCOperand::createExpr(Expr);
193 }
194 
195 MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
196     SmallVector<wasm::ValType, 1> &&Returns,
197     SmallVector<wasm::ValType, 4> &&Params) const {
198   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
199                                                          std::move(Params));
200   MCSymbol *Sym = Printer.createTempSymbol("typeindex");
201   auto *WasmSym = cast<MCSymbolWasm>(Sym);
202   WasmSym->setSignature(Signature.get());
203   Printer.addSignature(std::move(Signature));
204   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
205   const MCExpr *Expr =
206       MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
207   return MCOperand::createExpr(Expr);
208 }
209 
210 // Return the WebAssembly type associated with the given register class.
211 static wasm::ValType getType(const TargetRegisterClass *RC) {
212   if (RC == &WebAssembly::I32RegClass)
213     return wasm::ValType::I32;
214   if (RC == &WebAssembly::I64RegClass)
215     return wasm::ValType::I64;
216   if (RC == &WebAssembly::F32RegClass)
217     return wasm::ValType::F32;
218   if (RC == &WebAssembly::F64RegClass)
219     return wasm::ValType::F64;
220   if (RC == &WebAssembly::V128RegClass)
221     return wasm::ValType::V128;
222   llvm_unreachable("Unexpected register class");
223 }
224 
225 static void getFunctionReturns(const MachineInstr *MI,
226                                SmallVectorImpl<wasm::ValType> &Returns) {
227   const Function &F = MI->getMF()->getFunction();
228   const TargetMachine &TM = MI->getMF()->getTarget();
229   Type *RetTy = F.getReturnType();
230   SmallVector<MVT, 4> CallerRetTys;
231   computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
232   valTypesFromMVTs(CallerRetTys, Returns);
233 }
234 
235 void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
236                                    MCInst &OutMI) const {
237   OutMI.setOpcode(MI->getOpcode());
238 
239   const MCInstrDesc &Desc = MI->getDesc();
240   unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
241   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
242     const MachineOperand &MO = MI->getOperand(I);
243 
244     MCOperand MCOp;
245     switch (MO.getType()) {
246     default:
247       MI->print(errs());
248       llvm_unreachable("unknown operand type");
249     case MachineOperand::MO_MachineBasicBlock:
250       MI->print(errs());
251       llvm_unreachable("MachineBasicBlock operand should have been rewritten");
252     case MachineOperand::MO_Register: {
253       // Ignore all implicit register operands.
254       if (MO.isImplicit())
255         continue;
256       const WebAssemblyFunctionInfo &MFI =
257           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
258       unsigned WAReg = MFI.getWAReg(MO.getReg());
259       MCOp = MCOperand::createReg(WAReg);
260       break;
261     }
262     case MachineOperand::MO_Immediate: {
263       unsigned DescIndex = I - NumVariadicDefs;
264       if (DescIndex < Desc.NumOperands) {
265         const MCOperandInfo &Info = Desc.OpInfo[DescIndex];
266         if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
267           SmallVector<wasm::ValType, 4> Returns;
268           SmallVector<wasm::ValType, 4> Params;
269 
270           const MachineRegisterInfo &MRI =
271               MI->getParent()->getParent()->getRegInfo();
272           for (const MachineOperand &MO : MI->defs())
273             Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
274           for (const MachineOperand &MO : MI->explicit_uses())
275             if (MO.isReg())
276               Params.push_back(getType(MRI.getRegClass(MO.getReg())));
277 
278           // call_indirect instructions have a callee operand at the end which
279           // doesn't count as a param.
280           if (WebAssembly::isCallIndirect(MI->getOpcode()))
281             Params.pop_back();
282 
283           // return_call_indirect instructions have the return type of the
284           // caller
285           if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
286             getFunctionReturns(MI, Returns);
287 
288           MCOp = lowerTypeIndexOperand(std::move(Returns), std::move(Params));
289           break;
290         } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
291           auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
292           assert(BT != WebAssembly::BlockType::Invalid);
293           if (BT == WebAssembly::BlockType::Multivalue) {
294             SmallVector<wasm::ValType, 1> Returns;
295             getFunctionReturns(MI, Returns);
296             MCOp = lowerTypeIndexOperand(std::move(Returns),
297                                          SmallVector<wasm::ValType, 4>());
298             break;
299           }
300         } else if (Info.OperandType == WebAssembly::OPERAND_HEAPTYPE) {
301           assert(static_cast<WebAssembly::HeapType>(MO.getImm()) !=
302                  WebAssembly::HeapType::Invalid);
303           // With typed function references, this will need a case for type
304           // index operands.  Otherwise, fall through.
305         }
306       }
307       MCOp = MCOperand::createImm(MO.getImm());
308       break;
309     }
310     case MachineOperand::MO_FPImmediate: {
311       const ConstantFP *Imm = MO.getFPImm();
312       const uint64_t BitPattern =
313           Imm->getValueAPF().bitcastToAPInt().getZExtValue();
314       if (Imm->getType()->isFloatTy())
315         MCOp = MCOperand::createSFPImm(static_cast<uint32_t>(BitPattern));
316       else if (Imm->getType()->isDoubleTy())
317         MCOp = MCOperand::createDFPImm(BitPattern);
318       else
319         llvm_unreachable("unknown floating point immediate type");
320       break;
321     }
322     case MachineOperand::MO_GlobalAddress:
323       MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
324       break;
325     case MachineOperand::MO_ExternalSymbol:
326       // The target flag indicates whether this is a symbol for a
327       // variable or a function.
328       assert(MO.getTargetFlags() == 0 &&
329              "WebAssembly uses only symbol flags on ExternalSymbols");
330       MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
331       break;
332     case MachineOperand::MO_MCSymbol:
333       // This is currently used only for LSDA symbols (GCC_except_table),
334       // because global addresses or other external symbols are handled above.
335       assert(MO.getTargetFlags() == 0 &&
336              "WebAssembly does not use target flags on MCSymbol");
337       MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
338       break;
339     }
340 
341     OutMI.addOperand(MCOp);
342   }
343 
344   if (!WasmKeepRegisters)
345     removeRegisterOperands(MI, OutMI);
346   else if (Desc.variadicOpsAreDefs())
347     OutMI.insert(OutMI.begin(), MCOperand::createImm(MI->getNumExplicitDefs()));
348 }
349 
350 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
351   // Remove all uses of stackified registers to bring the instruction format
352   // into its final stack form used thruout MC, and transition opcodes to
353   // their _S variant.
354   // We do this separate from the above code that still may need these
355   // registers for e.g. call_indirect signatures.
356   // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
357   // details.
358   // TODO: the code above creates new registers which are then removed here.
359   // That code could be slightly simplified by not doing that, though maybe
360   // it is simpler conceptually to keep the code above in "register mode"
361   // until this transition point.
362   // FIXME: we are not processing inline assembly, which contains register
363   // operands, because it is used by later target generic code.
364   if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
365     return;
366 
367   // Transform to _S instruction.
368   auto RegOpcode = OutMI.getOpcode();
369   auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
370   assert(StackOpcode != -1 && "Failed to stackify instruction");
371   OutMI.setOpcode(StackOpcode);
372 
373   // Remove register operands.
374   for (auto I = OutMI.getNumOperands(); I; --I) {
375     auto &MO = OutMI.getOperand(I - 1);
376     if (MO.isReg()) {
377       OutMI.erase(&MO);
378     }
379   }
380 }
381