1 // WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file contains code to lower WebAssembly MachineInstrs to their
12 /// corresponding MCInst records.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "WebAssemblyMCInstLower.h"
17 #include "WebAssemblyAsmPrinter.h"
18 #include "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblyRuntimeLibcallSignatures.h"
20 #include "WebAssemblyUtilities.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 static cl::opt<bool> WasmKeepRegisters(
36     "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 static unsigned regInstructionToStackInstruction(unsigned OpCode);
42 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
43 
44 MCSymbol *
45 WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
46   const GlobalValue *Global = MO.getGlobal();
47   MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
48 
49   if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
50     const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
51     const TargetMachine &TM = MF.getTarget();
52     const Function &CurrentFunc = MF.getFunction();
53 
54     SmallVector<wasm::ValType, 4> Returns;
55     SmallVector<wasm::ValType, 4> Params;
56 
57     wasm::ValType iPTR =
58         MF.getSubtarget<WebAssemblySubtarget>().hasAddr64() ?
59         wasm::ValType::I64 :
60         wasm::ValType::I32;
61 
62     SmallVector<MVT, 4> ResultMVTs;
63     ComputeLegalValueVTs(CurrentFunc, TM, FuncTy->getReturnType(), ResultMVTs);
64     // WebAssembly can't currently handle returning tuples.
65     if (ResultMVTs.size() <= 1)
66       for (MVT ResultMVT : ResultMVTs)
67         Returns.push_back(WebAssembly::toValType(ResultMVT));
68     else
69       Params.push_back(iPTR);
70 
71     for (Type *Ty : FuncTy->params()) {
72       SmallVector<MVT, 4> ParamMVTs;
73       ComputeLegalValueVTs(CurrentFunc, TM, Ty, ParamMVTs);
74       for (MVT ParamMVT : ParamMVTs)
75         Params.push_back(WebAssembly::toValType(ParamMVT));
76     }
77 
78     if (FuncTy->isVarArg())
79       Params.push_back(iPTR);
80 
81     WasmSym->setReturns(std::move(Returns));
82     WasmSym->setParams(std::move(Params));
83     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
84   }
85 
86   return WasmSym;
87 }
88 
89 MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
90     const MachineOperand &MO) const {
91   const char *Name = MO.getSymbolName();
92   MCSymbolWasm *WasmSym =
93       cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
94   const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
95 
96   // __stack_pointer is a global variable; all other external symbols used by
97   // CodeGen are functions.  It's OK to hardcode knowledge of specific symbols
98   // here; this method is precisely there for fetching the signatures of known
99   // Clang-provided symbols.
100   if (strcmp(Name, "__stack_pointer") == 0) {
101     WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
102     WasmSym->setGlobalType(wasm::WasmGlobalType{
103         uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
104                                       : wasm::WASM_TYPE_I32),
105         true});
106     return WasmSym;
107   }
108 
109   SmallVector<wasm::ValType, 4> Returns;
110   SmallVector<wasm::ValType, 4> Params;
111   GetSignature(Subtarget, Name, Returns, Params);
112 
113   WasmSym->setReturns(std::move(Returns));
114   WasmSym->setParams(std::move(Params));
115   WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
116 
117   return WasmSym;
118 }
119 
120 MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym,
121                                                      int64_t Offset,
122                                                      bool IsFunc,
123                                                      bool IsGlob) const {
124   MCSymbolRefExpr::VariantKind VK =
125       IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION :
126       IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL
127              : MCSymbolRefExpr::VK_None;
128 
129   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
130 
131   if (Offset != 0) {
132     if (IsFunc)
133       report_fatal_error("Function addresses with offsets not supported");
134     if (IsGlob)
135       report_fatal_error("Global indexes with offsets not supported");
136     Expr =
137         MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
138   }
139 
140   return MCOperand::createExpr(Expr);
141 }
142 
143 // Return the WebAssembly type associated with the given register class.
144 static wasm::ValType getType(const TargetRegisterClass *RC) {
145   if (RC == &WebAssembly::I32RegClass)
146     return wasm::ValType::I32;
147   if (RC == &WebAssembly::I64RegClass)
148     return wasm::ValType::I64;
149   if (RC == &WebAssembly::F32RegClass)
150     return wasm::ValType::F32;
151   if (RC == &WebAssembly::F64RegClass)
152     return wasm::ValType::F64;
153   llvm_unreachable("Unexpected register class");
154 }
155 
156 void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
157                                    MCInst &OutMI) const {
158   OutMI.setOpcode(MI->getOpcode());
159 
160   const MCInstrDesc &Desc = MI->getDesc();
161   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
162     const MachineOperand &MO = MI->getOperand(i);
163 
164     MCOperand MCOp;
165     switch (MO.getType()) {
166     default:
167       MI->print(errs());
168       llvm_unreachable("unknown operand type");
169     case MachineOperand::MO_MachineBasicBlock:
170       MI->print(errs());
171       llvm_unreachable("MachineBasicBlock operand should have been rewritten");
172     case MachineOperand::MO_Register: {
173       // Ignore all implicit register operands.
174       if (MO.isImplicit())
175         continue;
176       const WebAssemblyFunctionInfo &MFI =
177           *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
178       unsigned WAReg = MFI.getWAReg(MO.getReg());
179       MCOp = MCOperand::createReg(WAReg);
180       break;
181     }
182     case MachineOperand::MO_Immediate:
183       if (i < Desc.NumOperands) {
184         const MCOperandInfo &Info = Desc.OpInfo[i];
185         if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
186           MCSymbol *Sym = Printer.createTempSymbol("typeindex");
187 
188           SmallVector<wasm::ValType, 4> Returns;
189           SmallVector<wasm::ValType, 4> Params;
190 
191           const MachineRegisterInfo &MRI =
192               MI->getParent()->getParent()->getRegInfo();
193           for (const MachineOperand &MO : MI->defs())
194             Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
195           for (const MachineOperand &MO : MI->explicit_uses())
196             if (MO.isReg())
197               Params.push_back(getType(MRI.getRegClass(MO.getReg())));
198 
199           // call_indirect instructions have a callee operand at the end which
200           // doesn't count as a param.
201           if (WebAssembly::isCallIndirect(*MI))
202             Params.pop_back();
203 
204           MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
205           WasmSym->setReturns(std::move(Returns));
206           WasmSym->setParams(std::move(Params));
207           WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
208 
209           const MCExpr *Expr = MCSymbolRefExpr::create(
210               WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx);
211           MCOp = MCOperand::createExpr(Expr);
212           break;
213         }
214       }
215       MCOp = MCOperand::createImm(MO.getImm());
216       break;
217     case MachineOperand::MO_FPImmediate: {
218       // TODO: MC converts all floating point immediate operands to double.
219       // This is fine for numeric values, but may cause NaNs to change bits.
220       const ConstantFP *Imm = MO.getFPImm();
221       if (Imm->getType()->isFloatTy())
222         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
223       else if (Imm->getType()->isDoubleTy())
224         MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
225       else
226         llvm_unreachable("unknown floating point immediate type");
227       break;
228     }
229     case MachineOperand::MO_GlobalAddress:
230       assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG &&
231              "WebAssembly does not use target flags on GlobalAddresses");
232       MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
233                                 MO.getGlobal()->getValueType()->isFunctionTy(),
234                                 false);
235       break;
236     case MachineOperand::MO_ExternalSymbol:
237       // The target flag indicates whether this is a symbol for a
238       // variable or a function.
239       assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 &&
240              "WebAssembly uses only symbol flags on ExternalSymbols");
241       MCOp = LowerSymbolOperand(GetExternalSymbolSymbol(MO), /*Offset=*/0,
242           (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0,
243           (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0);
244       break;
245     }
246 
247     OutMI.addOperand(MCOp);
248   }
249 
250   if (!WasmKeepRegisters)
251     removeRegisterOperands(MI, OutMI);
252 }
253 
254 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
255   // Remove all uses of stackified registers to bring the instruction format
256   // into its final stack form used thruout MC, and transition opcodes to
257   // their _S variant.
258   // We do this seperate from the above code that still may need these
259   // registers for e.g. call_indirect signatures.
260   // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
261   // details.
262   // TODO: the code above creates new registers which are then removed here.
263   // That code could be slightly simplified by not doing that, though maybe
264   // it is simpler conceptually to keep the code above in "register mode"
265   // until this transition point.
266   // FIXME: we are not processing inline assembly, which contains register
267   // operands, because it is used by later target generic code.
268   if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
269     return;
270 
271   // Transform to _S instruction.
272   auto RegOpcode = OutMI.getOpcode();
273   auto StackOpcode = regInstructionToStackInstruction(RegOpcode);
274   OutMI.setOpcode(StackOpcode);
275 
276   // Remove register operands.
277   for (auto I = OutMI.getNumOperands(); I; --I) {
278     auto &MO = OutMI.getOperand(I - 1);
279     if (MO.isReg()) {
280       OutMI.erase(&MO);
281     }
282   }
283 }
284 
285 static unsigned regInstructionToStackInstruction(unsigned OpCode) {
286   // For most opcodes, this function could have been implemented as "return
287   // OpCode + 1", but since table-gen alphabetically sorts them, this cannot be
288   // guaranteed (see e.g. BR and BR_IF). Instead we use a giant switch statement
289   // generated by a custom TableGen backend (WebAssemblyStackifierEmitter.cpp)
290   // that emits switch cases of the form
291   //
292   //   case WebAssembly::RegisterInstr: return WebAssembly::StackInstr;
293   //
294   // for every pair of equivalent register and stack instructions.
295   switch (OpCode) {
296   default:
297     llvm_unreachable(
298           "unknown WebAssembly instruction in Explicit Locals pass");
299 #include "WebAssemblyGenStackifier.inc"
300   }
301 }
302