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