blob: e9a0cf519055c042c68e29c26a4a6a9656313230 [file] [log] [blame]
Dan Gohmane9361d52015-11-05 19:28:16 +00001// 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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file contains code to lower WebAssembly MachineInstrs to their
Dan Gohmane9361d52015-11-05 19:28:16 +000012/// corresponding MCInst records.
13///
14//===----------------------------------------------------------------------===//
15
16#include "WebAssemblyMCInstLower.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000017#include "WebAssemblyAsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000018#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000019#include "WebAssemblyRuntimeLibcallSignatures.h"
20#include "WebAssemblyUtilities.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000021#include "llvm/CodeGen/AsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000022#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000023#include "llvm/IR/Constants.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000024#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCInst.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000028#include "llvm/MC/MCSymbolWasm.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
31using namespace llvm;
32
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000033// This disables the removal of registers when lowering into MC, as required
34// by some current tests.
Heejin Ahnf208f632018-09-05 01:27:38 +000035static 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));
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000040
41static unsigned regInstructionToStackInstruction(unsigned OpCode);
42static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
43
Dan Gohmane9361d52015-11-05 19:28:16 +000044MCSymbol *
45WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000046 const GlobalValue *Global = MO.getGlobal();
Sam Cleggcf2a9e22018-07-16 23:09:29 +000047 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
Dan Gohmand934cb82017-02-24 23:18:00 +000048
49 if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
50 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
51 const TargetMachine &TM = MF.getTarget();
David Blaikie21109242017-12-15 23:52:06 +000052 const Function &CurrentFunc = MF.getFunction();
Dan Gohmand934cb82017-02-24 23:18:00 +000053
Derek Schuff77a7a382018-10-03 22:22:48 +000054 SmallVector<MVT, 1> ResultMVTs;
55 SmallVector<MVT, 4> ParamMVTs;
56 ComputeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs);
Dan Gohmand934cb82017-02-24 23:18:00 +000057
Derek Schuff77a7a382018-10-03 22:22:48 +000058 auto Signature = SignatureFromMVTs(ResultMVTs, ParamMVTs);
59 WasmSym->setSignature(Signature.get());
60 Printer.addSignature(std::move(Signature));
Sam Clegg6c899ba2018-02-23 05:08:34 +000061 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +000062 }
63
64 return WasmSym;
Dan Gohmane9361d52015-11-05 19:28:16 +000065}
66
Dan Gohman7a6b9822015-11-29 22:32:02 +000067MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
68 const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000069 const char *Name = MO.getSymbolName();
Sam Cleggcf2a9e22018-07-16 23:09:29 +000070 MCSymbolWasm *WasmSym =
71 cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
Dan Gohmand934cb82017-02-24 23:18:00 +000072 const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
73
74 // __stack_pointer is a global variable; all other external symbols used by
Sam Clegg6c899ba2018-02-23 05:08:34 +000075 // CodeGen are functions. It's OK to hardcode knowledge of specific symbols
76 // here; this method is precisely there for fetching the signatures of known
77 // Clang-provided symbols.
78 if (strcmp(Name, "__stack_pointer") == 0) {
Sam Clegg6c899ba2018-02-23 05:08:34 +000079 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
Sam Clegg03e101f2018-03-01 18:06:21 +000080 WasmSym->setGlobalType(wasm::WasmGlobalType{
Sam Clegg503fdea2018-03-01 18:48:08 +000081 uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
82 : wasm::WASM_TYPE_I32),
Sam Clegg03e101f2018-03-01 18:06:21 +000083 true});
Dan Gohmand934cb82017-02-24 23:18:00 +000084 return WasmSym;
Sam Clegg6c899ba2018-02-23 05:08:34 +000085 }
Dan Gohmand934cb82017-02-24 23:18:00 +000086
Derek Schuffe2688c42017-03-14 20:23:22 +000087 SmallVector<wasm::ValType, 4> Returns;
88 SmallVector<wasm::ValType, 4> Params;
Derek Schuff70ce1af2018-09-27 22:20:33 +000089 GetLibcallSignature(Subtarget, Name, Returns, Params);
Derek Schuff77a7a382018-10-03 22:22:48 +000090 auto Signature =
91 make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
92 WasmSym->setSignature(Signature.get());
93 Printer.addSignature(std::move(Signature));
Sam Clegg6c899ba2018-02-23 05:08:34 +000094 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +000095
96 return WasmSym;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +000097}
98
Dan Gohman26c67652016-01-11 23:38:05 +000099MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym,
100 int64_t Offset,
Nicholas Wilsone408a892018-08-03 14:33:37 +0000101 bool IsFunc,
102 bool IsGlob) const {
Dan Gohman26c67652016-01-11 23:38:05 +0000103 MCSymbolRefExpr::VariantKind VK =
Heejin Ahnf208f632018-09-05 01:27:38 +0000104 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION
105 : IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL
106 : MCSymbolRefExpr::VK_None;
Dan Gohmand934cb82017-02-24 23:18:00 +0000107
Dan Gohman26c67652016-01-11 23:38:05 +0000108 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
Dan Gohmane9361d52015-11-05 19:28:16 +0000109
Dan Gohmana4b710a2015-12-06 19:33:32 +0000110 if (Offset != 0) {
Dan Gohman26c67652016-01-11 23:38:05 +0000111 if (IsFunc)
112 report_fatal_error("Function addresses with offsets not supported");
Nicholas Wilsone408a892018-08-03 14:33:37 +0000113 if (IsGlob)
114 report_fatal_error("Global indexes with offsets not supported");
Dan Gohmana4b710a2015-12-06 19:33:32 +0000115 Expr =
116 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
117 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000118
119 return MCOperand::createExpr(Expr);
120}
121
Dan Gohmand934cb82017-02-24 23:18:00 +0000122// Return the WebAssembly type associated with the given register class.
Derek Schuffe2688c42017-03-14 20:23:22 +0000123static wasm::ValType getType(const TargetRegisterClass *RC) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000124 if (RC == &WebAssembly::I32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000125 return wasm::ValType::I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000126 if (RC == &WebAssembly::I64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000127 return wasm::ValType::I64;
Dan Gohmand934cb82017-02-24 23:18:00 +0000128 if (RC == &WebAssembly::F32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000129 return wasm::ValType::F32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000130 if (RC == &WebAssembly::F64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000131 return wasm::ValType::F64;
Thomas Lively6f21a132018-09-20 22:04:44 +0000132 if (RC == &WebAssembly::V128RegClass)
133 return wasm::ValType::V128;
Dan Gohmand934cb82017-02-24 23:18:00 +0000134 llvm_unreachable("Unexpected register class");
135}
136
Dan Gohmane9361d52015-11-05 19:28:16 +0000137void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
138 MCInst &OutMI) const {
139 OutMI.setOpcode(MI->getOpcode());
140
Dan Gohmand934cb82017-02-24 23:18:00 +0000141 const MCInstrDesc &Desc = MI->getDesc();
Dan Gohmane9361d52015-11-05 19:28:16 +0000142 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
143 const MachineOperand &MO = MI->getOperand(i);
144
145 MCOperand MCOp;
146 switch (MO.getType()) {
147 default:
Richard Trieu3de487b2017-01-28 03:23:49 +0000148 MI->print(errs());
Dan Gohmane9361d52015-11-05 19:28:16 +0000149 llvm_unreachable("unknown operand type");
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000150 case MachineOperand::MO_MachineBasicBlock:
Richard Trieu3de487b2017-01-28 03:23:49 +0000151 MI->print(errs());
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000152 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
Dan Gohmancf4748f2015-11-12 17:04:33 +0000153 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +0000154 // Ignore all implicit register operands.
155 if (MO.isImplicit())
156 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000157 const WebAssemblyFunctionInfo &MFI =
158 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
159 unsigned WAReg = MFI.getWAReg(MO.getReg());
160 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +0000161 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000162 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000163 case MachineOperand::MO_Immediate:
Dan Gohmand934cb82017-02-24 23:18:00 +0000164 if (i < Desc.NumOperands) {
165 const MCOperandInfo &Info = Desc.OpInfo[i];
166 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
167 MCSymbol *Sym = Printer.createTempSymbol("typeindex");
Dan Gohmand934cb82017-02-24 23:18:00 +0000168
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000169 SmallVector<wasm::ValType, 4> Returns;
170 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000171
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000172 const MachineRegisterInfo &MRI =
173 MI->getParent()->getParent()->getRegInfo();
174 for (const MachineOperand &MO : MI->defs())
175 Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
176 for (const MachineOperand &MO : MI->explicit_uses())
177 if (MO.isReg())
178 Params.push_back(getType(MRI.getRegClass(MO.getReg())));
Dan Gohmand934cb82017-02-24 23:18:00 +0000179
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000180 // call_indirect instructions have a callee operand at the end which
181 // doesn't count as a param.
182 if (WebAssembly::isCallIndirect(*MI))
183 Params.pop_back();
Dan Gohmand934cb82017-02-24 23:18:00 +0000184
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000185 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
Derek Schuff77a7a382018-10-03 22:22:48 +0000186 auto Signature = make_unique<wasm::WasmSignature>(std::move(Returns),
187 std::move(Params));
188 WasmSym->setSignature(Signature.get());
189 Printer.addSignature(std::move(Signature));
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000190 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
191
192 const MCExpr *Expr = MCSymbolRefExpr::create(
193 WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx);
194 MCOp = MCOperand::createExpr(Expr);
195 break;
Dan Gohmand934cb82017-02-24 23:18:00 +0000196 }
197 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000198 MCOp = MCOperand::createImm(MO.getImm());
199 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000200 case MachineOperand::MO_FPImmediate: {
201 // TODO: MC converts all floating point immediate operands to double.
202 // This is fine for numeric values, but may cause NaNs to change bits.
203 const ConstantFP *Imm = MO.getFPImm();
204 if (Imm->getType()->isFloatTy())
205 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
206 else if (Imm->getType()->isDoubleTy())
207 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
208 else
209 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +0000210 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000211 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000212 case MachineOperand::MO_GlobalAddress:
Nicholas Wilsone408a892018-08-03 14:33:37 +0000213 assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG &&
Dan Gohman26c67652016-01-11 23:38:05 +0000214 "WebAssembly does not use target flags on GlobalAddresses");
215 MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
Nicholas Wilsone408a892018-08-03 14:33:37 +0000216 MO.getGlobal()->getValueType()->isFunctionTy(),
217 false);
Dan Gohmane9361d52015-11-05 19:28:16 +0000218 break;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000219 case MachineOperand::MO_ExternalSymbol:
Dan Gohman26c67652016-01-11 23:38:05 +0000220 // The target flag indicates whether this is a symbol for a
221 // variable or a function.
Nicholas Wilsone408a892018-08-03 14:33:37 +0000222 assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 &&
223 "WebAssembly uses only symbol flags on ExternalSymbols");
Heejin Ahnf208f632018-09-05 01:27:38 +0000224 MCOp = LowerSymbolOperand(
225 GetExternalSymbolSymbol(MO), /*Offset=*/0,
Nicholas Wilsone408a892018-08-03 14:33:37 +0000226 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0,
227 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0);
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000228 break;
Dan Gohmane9361d52015-11-05 19:28:16 +0000229 }
230
231 OutMI.addOperand(MCOp);
232 }
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000233
234 if (!WasmKeepRegisters)
235 removeRegisterOperands(MI, OutMI);
236}
237
238static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
239 // Remove all uses of stackified registers to bring the instruction format
240 // into its final stack form used thruout MC, and transition opcodes to
241 // their _S variant.
242 // We do this seperate from the above code that still may need these
243 // registers for e.g. call_indirect signatures.
244 // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
245 // details.
246 // TODO: the code above creates new registers which are then removed here.
247 // That code could be slightly simplified by not doing that, though maybe
248 // it is simpler conceptually to keep the code above in "register mode"
249 // until this transition point.
250 // FIXME: we are not processing inline assembly, which contains register
251 // operands, because it is used by later target generic code.
252 if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
253 return;
254
255 // Transform to _S instruction.
256 auto RegOpcode = OutMI.getOpcode();
257 auto StackOpcode = regInstructionToStackInstruction(RegOpcode);
258 OutMI.setOpcode(StackOpcode);
259
260 // Remove register operands.
261 for (auto I = OutMI.getNumOperands(); I; --I) {
262 auto &MO = OutMI.getOperand(I - 1);
263 if (MO.isReg()) {
264 OutMI.erase(&MO);
265 }
266 }
267}
268
269static unsigned regInstructionToStackInstruction(unsigned OpCode) {
Thomas Lively211874d2018-08-27 22:02:09 +0000270 // For most opcodes, this function could have been implemented as "return
271 // OpCode + 1", but since table-gen alphabetically sorts them, this cannot be
Thomas Livelyadb6da12018-08-28 18:49:47 +0000272 // guaranteed (see e.g. BR and BR_IF). Instead we use a giant switch statement
273 // generated by a custom TableGen backend (WebAssemblyStackifierEmitter.cpp)
274 // that emits switch cases of the form
275 //
276 // case WebAssembly::RegisterInstr: return WebAssembly::StackInstr;
277 //
278 // for every pair of equivalent register and stack instructions.
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000279 switch (OpCode) {
280 default:
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000281 llvm_unreachable(
Heejin Ahnf208f632018-09-05 01:27:38 +0000282 "unknown WebAssembly instruction in WebAssemblyMCInstLower pass");
Thomas Lively211874d2018-08-27 22:02:09 +0000283#include "WebAssemblyGenStackifier.inc"
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000284 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000285}