xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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 
165     Expr = MCBinaryExpr::createAdd(
166         Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
167   }
168 
169   return MCOperand::createExpr(Expr);
170 }
171 
172 MCOperand WebAssemblyMCInstLower::lowerTypeIndexOperand(
173     SmallVector<wasm::ValType, 1> &&Returns,
174     SmallVector<wasm::ValType, 4> &&Params) const {
175   auto Signature = std::make_unique<wasm::WasmSignature>(std::move(Returns),
176                                                          std::move(Params));
177   MCSymbol *Sym = Printer.createTempSymbol("typeindex");
178   auto *WasmSym = cast<MCSymbolWasm>(Sym);
179   WasmSym->setSignature(Signature.get());
180   Printer.addSignature(std::move(Signature));
181   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
182   const MCExpr *Expr =
183       MCSymbolRefExpr::create(WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
184   return MCOperand::createExpr(Expr);
185 }
186 
187 // Return the WebAssembly type associated with the given register class.
188 static wasm::ValType getType(const TargetRegisterClass *RC) {
189   if (RC == &WebAssembly::I32RegClass)
190     return wasm::ValType::I32;
191   if (RC == &WebAssembly::I64RegClass)
192     return wasm::ValType::I64;
193   if (RC == &WebAssembly::F32RegClass)
194     return wasm::ValType::F32;
195   if (RC == &WebAssembly::F64RegClass)
196     return wasm::ValType::F64;
197   if (RC == &WebAssembly::V128RegClass)
198     return wasm::ValType::V128;
199   llvm_unreachable("Unexpected register class");
200 }
201 
202 static void getFunctionReturns(const MachineInstr *MI,
203                                SmallVectorImpl<wasm::ValType> &Returns) {
204   const Function &F = MI->getMF()->getFunction();
205   const TargetMachine &TM = MI->getMF()->getTarget();
206   Type *RetTy = F.getReturnType();
207   SmallVector<MVT, 4> CallerRetTys;
208   computeLegalValueVTs(F, TM, RetTy, CallerRetTys);
209   valTypesFromMVTs(CallerRetTys, Returns);
210 }
211 
212 void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
213                                    MCInst &OutMI) const {
214   OutMI.setOpcode(MI->getOpcode());
215 
216   const MCInstrDesc &Desc = MI->getDesc();
217   unsigned NumVariadicDefs = MI->getNumExplicitDefs() - Desc.getNumDefs();
218   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
219     const MachineOperand &MO = MI->getOperand(I);
220 
221     MCOperand MCOp;
222     switch (MO.getType()) {
223     default:
224       MI->print(errs());
225       llvm_unreachable("unknown operand type");
226     case MachineOperand::MO_MachineBasicBlock:
227       MI->print(errs());
228       llvm_unreachable("MachineBasicBlock operand should have been rewritten");
229     case MachineOperand::MO_Register: {
230       // Ignore all implicit register operands.
231       if (MO.isImplicit())
232         continue;
233       const WebAssemblyFunctionInfo &MFI =
234           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
235       unsigned WAReg = MFI.getWAReg(MO.getReg());
236       MCOp = MCOperand::createReg(WAReg);
237       break;
238     }
239     case MachineOperand::MO_Immediate: {
240       unsigned DescIndex = I - NumVariadicDefs;
241       if (DescIndex < Desc.NumOperands) {
242         const MCOperandInfo &Info = Desc.OpInfo[DescIndex];
243         if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
244           SmallVector<wasm::ValType, 4> Returns;
245           SmallVector<wasm::ValType, 4> Params;
246 
247           const MachineRegisterInfo &MRI =
248               MI->getParent()->getParent()->getRegInfo();
249           for (const MachineOperand &MO : MI->defs())
250             Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
251           for (const MachineOperand &MO : MI->explicit_uses())
252             if (MO.isReg())
253               Params.push_back(getType(MRI.getRegClass(MO.getReg())));
254 
255           // call_indirect instructions have a callee operand at the end which
256           // doesn't count as a param.
257           if (WebAssembly::isCallIndirect(MI->getOpcode()))
258             Params.pop_back();
259 
260           // return_call_indirect instructions have the return type of the
261           // caller
262           if (MI->getOpcode() == WebAssembly::RET_CALL_INDIRECT)
263             getFunctionReturns(MI, Returns);
264 
265           MCOp = lowerTypeIndexOperand(std::move(Returns), std::move(Params));
266           break;
267         } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
268           auto BT = static_cast<WebAssembly::BlockType>(MO.getImm());
269           assert(BT != WebAssembly::BlockType::Invalid);
270           if (BT == WebAssembly::BlockType::Multivalue) {
271             SmallVector<wasm::ValType, 1> Returns;
272             getFunctionReturns(MI, Returns);
273             MCOp = lowerTypeIndexOperand(std::move(Returns),
274                                          SmallVector<wasm::ValType, 4>());
275             break;
276           }
277         } else if (Info.OperandType == WebAssembly::OPERAND_HEAPTYPE) {
278           assert(static_cast<WebAssembly::HeapType>(MO.getImm()) !=
279                  WebAssembly::HeapType::Invalid);
280           // With typed function references, this will need a case for type
281           // index operands.  Otherwise, fall through.
282         }
283       }
284       MCOp = MCOperand::createImm(MO.getImm());
285       break;
286     }
287     case MachineOperand::MO_FPImmediate: {
288       // TODO: MC converts all floating point immediate operands to double.
289       // This is fine for numeric values, but may cause NaNs to change bits.
290       const ConstantFP *Imm = MO.getFPImm();
291       if (Imm->getType()->isFloatTy())
292         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
293       else if (Imm->getType()->isDoubleTy())
294         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
295       else
296         llvm_unreachable("unknown floating point immediate type");
297       break;
298     }
299     case MachineOperand::MO_GlobalAddress:
300       MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
301       break;
302     case MachineOperand::MO_ExternalSymbol:
303       // The target flag indicates whether this is a symbol for a
304       // variable or a function.
305       assert(MO.getTargetFlags() == 0 &&
306              "WebAssembly uses only symbol flags on ExternalSymbols");
307       MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
308       break;
309     case MachineOperand::MO_MCSymbol:
310       // This is currently used only for LSDA symbols (GCC_except_table),
311       // because global addresses or other external symbols are handled above.
312       assert(MO.getTargetFlags() == 0 &&
313              "WebAssembly does not use target flags on MCSymbol");
314       MCOp = lowerSymbolOperand(MO, MO.getMCSymbol());
315       break;
316     }
317 
318     OutMI.addOperand(MCOp);
319   }
320 
321   if (!WasmKeepRegisters)
322     removeRegisterOperands(MI, OutMI);
323   else if (Desc.variadicOpsAreDefs())
324     OutMI.insert(OutMI.begin(), MCOperand::createImm(MI->getNumExplicitDefs()));
325 }
326 
327 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
328   // Remove all uses of stackified registers to bring the instruction format
329   // into its final stack form used thruout MC, and transition opcodes to
330   // their _S variant.
331   // We do this separate from the above code that still may need these
332   // registers for e.g. call_indirect signatures.
333   // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
334   // details.
335   // TODO: the code above creates new registers which are then removed here.
336   // That code could be slightly simplified by not doing that, though maybe
337   // it is simpler conceptually to keep the code above in "register mode"
338   // until this transition point.
339   // FIXME: we are not processing inline assembly, which contains register
340   // operands, because it is used by later target generic code.
341   if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
342     return;
343 
344   // Transform to _S instruction.
345   auto RegOpcode = OutMI.getOpcode();
346   auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
347   assert(StackOpcode != -1 && "Failed to stackify instruction");
348   OutMI.setOpcode(StackOpcode);
349 
350   // Remove register operands.
351   for (auto I = OutMI.getNumOperands(); I; --I) {
352     auto &MO = OutMI.getOperand(I - 1);
353     if (MO.isReg()) {
354       OutMI.erase(&MO);
355     }
356   }
357 }
358