xref: /llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp (revision 31859f896cf90d64904134ce7b31230f374c3fcc)
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() ? wasm::WASM_TYPE_I64
111                                       : wasm::WASM_TYPE_I32),
112         Mutable});
113     return WasmSym;
114   }
115 
116   SmallVector<wasm::ValType, 4> Returns;
117   SmallVector<wasm::ValType, 4> Params;
118   if (strcmp(Name, "__cpp_exception") == 0) {
119     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
120     // We can't confirm its signature index for now because there can be
121     // imported exceptions. Set it to be 0 for now.
122     WasmSym->setEventType(
123         {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
124     // We may have multiple C++ compilation units to be linked together, each of
125     // which defines the exception symbol. To resolve them, we declare them as
126     // weak.
127     WasmSym->setWeak(true);
128     WasmSym->setExternal(true);
129 
130     // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
131     // (for wasm64) param type and void return type. The reaon is, all C++
132     // exception values are pointers, and to share the type section with
133     // functions, exceptions are assumed to have void return type.
134     Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
135                                            : wasm::ValType::I32);
136   } else { // Function symbols
137     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
138     getLibcallSignature(Subtarget, Name, Returns, Params);
139   }
140   auto Signature =
141       std::make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
142   WasmSym->setSignature(Signature.get());
143   Printer.addSignature(std::move(Signature));
144 
145   return WasmSym;
146 }
147 
148 MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
149                                                      MCSymbol *Sym) const {
150   MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
151   unsigned TargetFlags = MO.getTargetFlags();
152 
153   switch (TargetFlags) {
154     case WebAssemblyII::MO_NO_FLAG:
155       break;
156     case WebAssemblyII::MO_GOT:
157       Kind = MCSymbolRefExpr::VK_GOT;
158       break;
159     case WebAssemblyII::MO_MEMORY_BASE_REL:
160       Kind = MCSymbolRefExpr::VK_WASM_MBREL;
161       break;
162     case WebAssemblyII::MO_TLS_BASE_REL:
163       Kind = MCSymbolRefExpr::VK_WASM_TLSREL;
164       break;
165     case WebAssemblyII::MO_TABLE_BASE_REL:
166       Kind = MCSymbolRefExpr::VK_WASM_TBREL;
167       break;
168     default:
169       llvm_unreachable("Unknown target flag on GV operand");
170   }
171 
172   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx);
173 
174   if (MO.getOffset() != 0) {
175     const auto *WasmSym = cast<MCSymbolWasm>(Sym);
176     if (TargetFlags == WebAssemblyII::MO_GOT)
177       report_fatal_error("GOT symbol references do not support offsets");
178     if (WasmSym->isFunction())
179       report_fatal_error("Function addresses with offsets not supported");
180     if (WasmSym->isGlobal())
181       report_fatal_error("Global indexes with offsets not supported");
182     if (WasmSym->isEvent())
183       report_fatal_error("Event indexes with offsets not supported");
184     if (WasmSym->isTable())
185       report_fatal_error("Table indexes with offsets not supported");
186 
187     Expr = MCBinaryExpr::createAdd(
188         Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
189   }
190 
191   return MCOperand::createExpr(Expr);
192 }
193 
194 MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
195     SmallVector<wasm::ValType, 1> &&Returns,
196     SmallVector<wasm::ValType, 4> &&Params) const {
197   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
198                                                          std::move(Params));
199   MCSymbol *Sym = Printer.createTempSymbol("typeindex");
200   auto *WasmSym = cast<MCSymbolWasm>(Sym);
201   WasmSym->setSignature(Signature.get());
202   Printer.addSignature(std::move(Signature));
203   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
204   const MCExpr *Expr =
205       MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
206   return MCOperand::createExpr(Expr);
207 }
208 
209 // Return the WebAssembly type associated with the given register class.
210 static wasm::ValType getType(const TargetRegisterClass *RC) {
211   if (RC == &WebAssembly::I32RegClass)
212     return wasm::ValType::I32;
213   if (RC == &WebAssembly::I64RegClass)
214     return wasm::ValType::I64;
215   if (RC == &WebAssembly::F32RegClass)
216     return wasm::ValType::F32;
217   if (RC == &WebAssembly::F64RegClass)
218     return wasm::ValType::F64;
219   if (RC == &WebAssembly::V128RegClass)
220     return wasm::ValType::V128;
221   if (RC == &WebAssembly::EXTERNREFRegClass)
222     return wasm::ValType::EXTERNREF;
223   if (RC == &WebAssembly::FUNCREFRegClass)
224     return wasm::ValType::FUNCREF;
225   llvm_unreachable("Unexpected register class");
226 }
227 
228 static void getFunctionReturns(const MachineInstr *MI,
229                                SmallVectorImpl<wasm::ValType> &Returns) {
230   const Function &F = MI->getMF()->getFunction();
231   const TargetMachine &TM = MI->getMF()->getTarget();
232   Type *RetTy = F.getReturnType();
233   SmallVector<MVT, 4> CallerRetTys;
234   computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
235   valTypesFromMVTs(CallerRetTys, Returns);
236 }
237 
238 void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
239                                    MCInst &OutMI) const {
240   OutMI.setOpcode(MI->getOpcode());
241 
242   const MCInstrDesc &Desc = MI->getDesc();
243   unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
244   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
245     const MachineOperand &MO = MI->getOperand(I);
246 
247     MCOperand MCOp;
248     switch (MO.getType()) {
249     default:
250       MI->print(errs());
251       llvm_unreachable("unknown operand type");
252     case MachineOperand::MO_MachineBasicBlock:
253       MI->print(errs());
254       llvm_unreachable("MachineBasicBlock operand should have been rewritten");
255     case MachineOperand::MO_Register: {
256       // Ignore all implicit register operands.
257       if (MO.isImplicit())
258         continue;
259       const WebAssemblyFunctionInfo &MFI =
260           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
261       unsigned WAReg = MFI.getWAReg(MO.getReg());
262       MCOp = MCOperand::createReg(WAReg);
263       break;
264     }
265     case MachineOperand::MO_Immediate: {
266       unsigned DescIndex = I - NumVariadicDefs;
267       if (DescIndex < Desc.NumOperands) {
268         const MCOperandInfo &Info = Desc.OpInfo[DescIndex];
269         if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
270           SmallVector<wasm::ValType, 4> Returns;
271           SmallVector<wasm::ValType, 4> Params;
272 
273           const MachineRegisterInfo &MRI =
274               MI->getParent()->getParent()->getRegInfo();
275           for (const MachineOperand &MO : MI->defs())
276             Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
277           for (const MachineOperand &MO : MI->explicit_uses())
278             if (MO.isReg())
279               Params.push_back(getType(MRI.getRegClass(MO.getReg())));
280 
281           // call_indirect instructions have a callee operand at the end which
282           // doesn't count as a param.
283           if (WebAssembly::isCallIndirect(MI->getOpcode()))
284             Params.pop_back();
285 
286           // return_call_indirect instructions have the return type of the
287           // caller
288           if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
289             getFunctionReturns(MI, Returns);
290 
291           MCOp = lowerTypeIndexOperand(std::move(Returns), std::move(Params));
292           break;
293         } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
294           auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
295           assert(BT != WebAssembly::BlockType::Invalid);
296           if (BT == WebAssembly::BlockType::Multivalue) {
297             SmallVector<wasm::ValType, 1> Returns;
298             getFunctionReturns(MI, Returns);
299             MCOp = lowerTypeIndexOperand(std::move(Returns),
300                                          SmallVector<wasm::ValType, 4>());
301             break;
302           }
303         } else if (Info.OperandType == WebAssembly::OPERAND_HEAPTYPE) {
304           assert(static_cast<WebAssembly::HeapType>(MO.getImm()) !=
305                  WebAssembly::HeapType::Invalid);
306           // With typed function references, this will need a case for type
307           // index operands.  Otherwise, fall through.
308         }
309       }
310       MCOp = MCOperand::createImm(MO.getImm());
311       break;
312     }
313     case MachineOperand::MO_FPImmediate: {
314       const ConstantFP *Imm = MO.getFPImm();
315       const uint64_t BitPattern =
316           Imm->getValueAPF().bitcastToAPInt().getZExtValue();
317       if (Imm->getType()->isFloatTy())
318         MCOp = MCOperand::createSFPImm(static_cast<uint32_t>(BitPattern));
319       else if (Imm->getType()->isDoubleTy())
320         MCOp = MCOperand::createDFPImm(BitPattern);
321       else
322         llvm_unreachable("unknown floating point immediate type");
323       break;
324     }
325     case MachineOperand::MO_GlobalAddress:
326       MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
327       break;
328     case MachineOperand::MO_ExternalSymbol:
329       // The target flag indicates whether this is a symbol for a
330       // variable or a function.
331       assert(MO.getTargetFlags() == 0 &&
332              "WebAssembly uses only symbol flags on ExternalSymbols");
333       MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
334       break;
335     case MachineOperand::MO_MCSymbol:
336       // This is currently used only for LSDA symbols (GCC_except_table),
337       // because global addresses or other external symbols are handled above.
338       assert(MO.getTargetFlags() == 0 &&
339              "WebAssembly does not use target flags on MCSymbol");
340       MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
341       break;
342     }
343 
344     OutMI.addOperand(MCOp);
345   }
346 
347   if (!WasmKeepRegisters)
348     removeRegisterOperands(MI, OutMI);
349   else if (Desc.variadicOpsAreDefs())
350     OutMI.insert(OutMI.begin(), MCOperand::createImm(MI->getNumExplicitDefs()));
351 }
352 
353 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
354   // Remove all uses of stackified registers to bring the instruction format
355   // into its final stack form used thruout MC, and transition opcodes to
356   // their _S variant.
357   // We do this separate from the above code that still may need these
358   // registers for e.g. call_indirect signatures.
359   // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
360   // details.
361   // TODO: the code above creates new registers which are then removed here.
362   // That code could be slightly simplified by not doing that, though maybe
363   // it is simpler conceptually to keep the code above in "register mode"
364   // until this transition point.
365   // FIXME: we are not processing inline assembly, which contains register
366   // operands, because it is used by later target generic code.
367   if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
368     return;
369 
370   // Transform to _S instruction.
371   auto RegOpcode = OutMI.getOpcode();
372   auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
373   assert(StackOpcode != -1 && "Failed to stackify instruction");
374   OutMI.setOpcode(StackOpcode);
375 
376   // Remove register operands.
377   for (auto I = OutMI.getNumOperands(); I; --I) {
378     auto &MO = OutMI.getOperand(I - 1);
379     if (MO.isReg()) {
380       OutMI.erase(&MO);
381     }
382   }
383 }
384