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 "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> WasmEnableEmException;
42 extern cl::opt<bool> WasmEnableEmSjLj;
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     auto *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
51     // If the symbol doesn't have an explicit WasmSymbolType yet and the
52     // GlobalValue is actually a WebAssembly global, then ensure the symbol is a
53     // WASM_SYMBOL_TYPE_GLOBAL.
54     if (WebAssembly::isWasmVarAddressSpace(Global->getAddressSpace()) &&
55         !WasmSym->getType()) {
56       const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
57       const TargetMachine &TM = MF.getTarget();
58       const Function &CurrentFunc = MF.getFunction();
59       SmallVector<MVT, 1> VTs;
60       computeLegalValueVTs(CurrentFunc, TM, Global->getValueType(), VTs);
61       if (VTs.size() != 1)
62         report_fatal_error("Aggregate globals not yet implemented");
63 
64       bool Mutable = true;
65       wasm::ValType Type = WebAssembly::toValType(VTs[0]);
66       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
67       WasmSym->setGlobalType(wasm::WasmGlobalType{uint8_t(Type), Mutable});
68     }
69     return WasmSym;
70   }
71 
72   const auto *FuncTy = cast<FunctionType>(Global->getValueType());
73   const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
74   const TargetMachine &TM = MF.getTarget();
75   const Function &CurrentFunc = MF.getFunction();
76 
77   SmallVector<MVT, 1> ResultMVTs;
78   SmallVector<MVT, 4> ParamMVTs;
79   const auto *const F = dyn_cast<Function>(Global);
80   computeSignatureVTs(FuncTy, F, CurrentFunc, TM, ParamMVTs, ResultMVTs);
81   auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs);
82 
83   bool InvokeDetected = false;
84   auto *WasmSym = Printer.getMCSymbolForFunction(
85       F, WasmEnableEmException || WasmEnableEmSjLj, Signature.get(),
86       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   return Printer.getOrCreateWasmSymbol(MO.getSymbolName());
96 }
97 
98 MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
99                                                      MCSymbol *Sym) const {
100   MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
101   unsigned TargetFlags = MO.getTargetFlags();
102 
103   switch (TargetFlags) {
104     case WebAssemblyII::MO_NO_FLAG:
105       break;
106     case WebAssemblyII::MO_GOT:
107       Kind = MCSymbolRefExpr::VK_GOT;
108       break;
109     case WebAssemblyII::MO_MEMORY_BASE_REL:
110       Kind = MCSymbolRefExpr::VK_WASM_MBREL;
111       break;
112     case WebAssemblyII::MO_TLS_BASE_REL:
113       Kind = MCSymbolRefExpr::VK_WASM_TLSREL;
114       break;
115     case WebAssemblyII::MO_TABLE_BASE_REL:
116       Kind = MCSymbolRefExpr::VK_WASM_TBREL;
117       break;
118     default:
119       llvm_unreachable("Unknown target flag on GV operand");
120   }
121 
122   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx);
123 
124   if (MO.getOffset() != 0) {
125     const auto *WasmSym = cast<MCSymbolWasm>(Sym);
126     if (TargetFlags == WebAssemblyII::MO_GOT)
127       report_fatal_error("GOT symbol references do not support offsets");
128     if (WasmSym->isFunction())
129       report_fatal_error("Function addresses with offsets not supported");
130     if (WasmSym->isGlobal())
131       report_fatal_error("Global indexes with offsets not supported");
132     if (WasmSym->isTag())
133       report_fatal_error("Tag indexes with offsets not supported");
134     if (WasmSym->isTable())
135       report_fatal_error("Table indexes with offsets not supported");
136 
137     Expr = MCBinaryExpr::createAdd(
138         Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
139   }
140 
141   return MCOperand::createExpr(Expr);
142 }
143 
144 MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
145     SmallVector<wasm::ValType, 1> &&Returns,
146     SmallVector<wasm::ValType, 4> &&Params) const {
147   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
148                                                          std::move(Params));
149   MCSymbol *Sym = Printer.createTempSymbol("typeindex");
150   auto *WasmSym = cast<MCSymbolWasm>(Sym);
151   WasmSym->setSignature(Signature.get());
152   Printer.addSignature(std::move(Signature));
153   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
154   const MCExpr *Expr =
155       MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
156   return MCOperand::createExpr(Expr);
157 }
158 
159 // Return the WebAssembly type associated with the given register class.
160 static wasm::ValType getType(const TargetRegisterClass *RC) {
161   if (RC == &WebAssembly::I32RegClass)
162     return wasm::ValType::I32;
163   if (RC == &WebAssembly::I64RegClass)
164     return wasm::ValType::I64;
165   if (RC == &WebAssembly::F32RegClass)
166     return wasm::ValType::F32;
167   if (RC == &WebAssembly::F64RegClass)
168     return wasm::ValType::F64;
169   if (RC == &WebAssembly::V128RegClass)
170     return wasm::ValType::V128;
171   if (RC == &WebAssembly::EXTERNREFRegClass)
172     return wasm::ValType::EXTERNREF;
173   if (RC == &WebAssembly::FUNCREFRegClass)
174     return wasm::ValType::FUNCREF;
175   llvm_unreachable("Unexpected register class");
176 }
177 
178 static void getFunctionReturns(const MachineInstr *MI,
179                                SmallVectorImpl<wasm::ValType> &Returns) {
180   const Function &F = MI->getMF()->getFunction();
181   const TargetMachine &TM = MI->getMF()->getTarget();
182   Type *RetTy = F.getReturnType();
183   SmallVector<MVT, 4> CallerRetTys;
184   computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
185   valTypesFromMVTs(CallerRetTys, Returns);
186 }
187 
188 void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
189                                    MCInst &OutMI) const {
190   OutMI.setOpcode(MI->getOpcode());
191 
192   const MCInstrDesc &Desc = MI->getDesc();
193   unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
194   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
195     const MachineOperand &MO = MI->getOperand(I);
196 
197     MCOperand MCOp;
198     switch (MO.getType()) {
199     default:
200       MI->print(errs());
201       llvm_unreachable("unknown operand type");
202     case MachineOperand::MO_MachineBasicBlock:
203       MI->print(errs());
204       llvm_unreachable("MachineBasicBlock operand should have been rewritten");
205     case MachineOperand::MO_Register: {
206       // Ignore all implicit register operands.
207       if (MO.isImplicit())
208         continue;
209       const WebAssemblyFunctionInfo &MFI =
210           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
211       unsigned WAReg = MFI.getWAReg(MO.getReg());
212       MCOp = MCOperand::createReg(WAReg);
213       break;
214     }
215     case MachineOperand::MO_Immediate: {
216       unsigned DescIndex = I - NumVariadicDefs;
217       if (DescIndex < Desc.NumOperands) {
218         const MCOperandInfo &Info = Desc.OpInfo[DescIndex];
219         if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
220           SmallVector<wasm::ValType, 4> Returns;
221           SmallVector<wasm::ValType, 4> Params;
222 
223           const MachineRegisterInfo &MRI =
224               MI->getParent()->getParent()->getRegInfo();
225           for (const MachineOperand &MO : MI->defs())
226             Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
227           for (const MachineOperand &MO : MI->explicit_uses())
228             if (MO.isReg())
229               Params.push_back(getType(MRI.getRegClass(MO.getReg())));
230 
231           // call_indirect instructions have a callee operand at the end which
232           // doesn't count as a param.
233           if (WebAssembly::isCallIndirect(MI->getOpcode()))
234             Params.pop_back();
235 
236           // return_call_indirect instructions have the return type of the
237           // caller
238           if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
239             getFunctionReturns(MI, Returns);
240 
241           MCOp = lowerTypeIndexOperand(std::move(Returns), std::move(Params));
242           break;
243         } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
244           auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
245           assert(BT != WebAssembly::BlockType::Invalid);
246           if (BT == WebAssembly::BlockType::Multivalue) {
247             SmallVector<wasm::ValType, 1> Returns;
248             getFunctionReturns(MI, Returns);
249             MCOp = lowerTypeIndexOperand(std::move(Returns),
250                                          SmallVector<wasm::ValType, 4>());
251             break;
252           }
253         } else if (Info.OperandType == WebAssembly::OPERAND_HEAPTYPE) {
254           assert(static_cast<WebAssembly::HeapType>(MO.getImm()) !=
255                  WebAssembly::HeapType::Invalid);
256           // With typed function references, this will need a case for type
257           // index operands.  Otherwise, fall through.
258         }
259       }
260       MCOp = MCOperand::createImm(MO.getImm());
261       break;
262     }
263     case MachineOperand::MO_FPImmediate: {
264       const ConstantFP *Imm = MO.getFPImm();
265       const uint64_t BitPattern =
266           Imm->getValueAPF().bitcastToAPInt().getZExtValue();
267       if (Imm->getType()->isFloatTy())
268         MCOp = MCOperand::createSFPImm(static_cast<uint32_t>(BitPattern));
269       else if (Imm->getType()->isDoubleTy())
270         MCOp = MCOperand::createDFPImm(BitPattern);
271       else
272         llvm_unreachable("unknown floating point immediate type");
273       break;
274     }
275     case MachineOperand::MO_GlobalAddress:
276       MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
277       break;
278     case MachineOperand::MO_ExternalSymbol:
279       // The target flag indicates whether this is a symbol for a
280       // variable or a function.
281       assert(MO.getTargetFlags() == 0 &&
282              "WebAssembly uses only symbol flags on ExternalSymbols");
283       MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
284       break;
285     case MachineOperand::MO_MCSymbol:
286       // This is currently used only for LSDA symbols (GCC_except_table),
287       // because global addresses or other external symbols are handled above.
288       assert(MO.getTargetFlags() == 0 &&
289              "WebAssembly does not use target flags on MCSymbol");
290       MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
291       break;
292     }
293 
294     OutMI.addOperand(MCOp);
295   }
296 
297   if (!WasmKeepRegisters)
298     removeRegisterOperands(MI, OutMI);
299   else if (Desc.variadicOpsAreDefs())
300     OutMI.insert(OutMI.begin(), MCOperand::createImm(MI->getNumExplicitDefs()));
301 }
302 
303 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
304   // Remove all uses of stackified registers to bring the instruction format
305   // into its final stack form used thruout MC, and transition opcodes to
306   // their _S variant.
307   // We do this separate from the above code that still may need these
308   // registers for e.g. call_indirect signatures.
309   // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
310   // details.
311   // TODO: the code above creates new registers which are then removed here.
312   // That code could be slightly simplified by not doing that, though maybe
313   // it is simpler conceptually to keep the code above in "register mode"
314   // until this transition point.
315   // FIXME: we are not processing inline assembly, which contains register
316   // operands, because it is used by later target generic code.
317   if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
318     return;
319 
320   // Transform to _S instruction.
321   auto RegOpcode = OutMI.getOpcode();
322   auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
323   assert(StackOpcode != -1 && "Failed to stackify instruction");
324   OutMI.setOpcode(StackOpcode);
325 
326   // Remove register operands.
327   for (auto I = OutMI.getNumOperands(); I; --I) {
328     auto &MO = OutMI.getOperand(I - 1);
329     if (MO.isReg()) {
330       OutMI.erase(&MO);
331     }
332   }
333 }
334