blob: 054fe5496d593386cedf7f29a70f0ee56e9165f0 [file] [log] [blame]
Dan Gohmane9361d52015-11-05 19:28:16 +00001// WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst //
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohmane9361d52015-11-05 19:28:16 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file contains code to lower WebAssembly MachineInstrs to their
Dan Gohmane9361d52015-11-05 19:28:16 +000011/// corresponding MCInst records.
12///
13//===----------------------------------------------------------------------===//
14
15#include "WebAssemblyMCInstLower.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000016#include "WebAssemblyAsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000017#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000018#include "WebAssemblyRuntimeLibcallSignatures.h"
19#include "WebAssemblyUtilities.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000020#include "llvm/CodeGen/AsmPrinter.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmancf4748f2015-11-12 17:04:33 +000022#include "llvm/IR/Constants.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000023#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCInst.h"
Dan Gohmand934cb82017-02-24 23:18:00 +000027#include "llvm/MC/MCSymbolWasm.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000028#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace llvm;
31
Thomas Livelyc63b5fc2018-10-22 21:55:26 +000032// Defines llvm::WebAssembly::getStackOpcode to convert register instructions to
33// stack instructions
34#define GET_INSTRMAP_INFO 1
35#include "WebAssemblyGenInstrInfo.inc"
36
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000037// This disables the removal of registers when lowering into MC, as required
38// by some current tests.
Heejin Ahnd6f48782019-01-30 03:21:57 +000039cl::opt<bool>
Heejin Ahnf208f632018-09-05 01:27:38 +000040 WasmKeepRegisters("wasm-keep-registers", cl::Hidden,
41 cl::desc("WebAssembly: output stack registers in"
42 " instruction output for test purposes only."),
43 cl::init(false));
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000044
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000045static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
46
Dan Gohmane9361d52015-11-05 19:28:16 +000047MCSymbol *
48WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000049 const GlobalValue *Global = MO.getGlobal();
Heejin Ahn18c56a02019-02-04 19:13:39 +000050 auto *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
Dan Gohmand934cb82017-02-24 23:18:00 +000051
52 if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
53 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
54 const TargetMachine &TM = MF.getTarget();
David Blaikie21109242017-12-15 23:52:06 +000055 const Function &CurrentFunc = MF.getFunction();
Dan Gohmand934cb82017-02-24 23:18:00 +000056
Derek Schuff77a7a382018-10-03 22:22:48 +000057 SmallVector<MVT, 1> ResultMVTs;
58 SmallVector<MVT, 4> ParamMVTs;
Heejin Ahn18c56a02019-02-04 19:13:39 +000059 computeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs);
Dan Gohmand934cb82017-02-24 23:18:00 +000060
Heejin Ahn18c56a02019-02-04 19:13:39 +000061 auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs);
Derek Schuff77a7a382018-10-03 22:22:48 +000062 WasmSym->setSignature(Signature.get());
63 Printer.addSignature(std::move(Signature));
Sam Clegg6c899ba2018-02-23 05:08:34 +000064 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +000065 }
66
67 return WasmSym;
Dan Gohmane9361d52015-11-05 19:28:16 +000068}
69
Dan Gohman7a6b9822015-11-29 22:32:02 +000070MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
71 const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000072 const char *Name = MO.getSymbolName();
Heejin Ahn18c56a02019-02-04 19:13:39 +000073 auto *WasmSym = cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
Dan Gohmand934cb82017-02-24 23:18:00 +000074 const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
75
Heejin Ahnda419bd2018-11-14 02:46:21 +000076 // Except for the two exceptions (__stack_pointer and __cpp_exception), all
77 // other external symbols used by CodeGen are functions. It's OK to hardcode
78 // knowledge of specific symbols here; this method is precisely there for
79 // fetching the signatures of known Clang-provided symbols.
Sam Clegg6c899ba2018-02-23 05:08:34 +000080 if (strcmp(Name, "__stack_pointer") == 0) {
Sam Clegg6c899ba2018-02-23 05:08:34 +000081 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
Sam Clegg03e101f2018-03-01 18:06:21 +000082 WasmSym->setGlobalType(wasm::WasmGlobalType{
Sam Clegg503fdea2018-03-01 18:48:08 +000083 uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
84 : wasm::WASM_TYPE_I32),
Sam Clegg03e101f2018-03-01 18:06:21 +000085 true});
Dan Gohmand934cb82017-02-24 23:18:00 +000086 return WasmSym;
Sam Clegg6c899ba2018-02-23 05:08:34 +000087 }
Dan Gohmand934cb82017-02-24 23:18:00 +000088
Derek Schuffe2688c42017-03-14 20:23:22 +000089 SmallVector<wasm::ValType, 4> Returns;
90 SmallVector<wasm::ValType, 4> Params;
Heejin Ahnda419bd2018-11-14 02:46:21 +000091 if (strcmp(Name, "__cpp_exception") == 0) {
92 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
93 // We can't confirm its signature index for now because there can be
94 // imported exceptions. Set it to be 0 for now.
95 WasmSym->setEventType(
96 {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
97 // We may have multiple C++ compilation units to be linked together, each of
98 // which defines the exception symbol. To resolve them, we declare them as
99 // weak.
100 WasmSym->setWeak(true);
101 WasmSym->setExternal(true);
102
103 // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
104 // (for wasm64) param type and void return type. The reaon is, all C++
105 // exception values are pointers, and to share the type section with
106 // functions, exceptions are assumed to have void return type.
107 Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
108 : wasm::ValType::I32);
109 } else { // Function symbols
110 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Heejin Ahn18c56a02019-02-04 19:13:39 +0000111 getLibcallSignature(Subtarget, Name, Returns, Params);
Heejin Ahnda419bd2018-11-14 02:46:21 +0000112 }
Derek Schuff77a7a382018-10-03 22:22:48 +0000113 auto Signature =
114 make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
115 WasmSym->setSignature(Signature.get());
116 Printer.addSignature(std::move(Signature));
Dan Gohmand934cb82017-02-24 23:18:00 +0000117
118 return WasmSym;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000119}
120
Heejin Ahn18c56a02019-02-04 19:13:39 +0000121MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(MCSymbol *Sym,
Dan Gohman26c67652016-01-11 23:38:05 +0000122 int64_t Offset,
Heejin Ahnda419bd2018-11-14 02:46:21 +0000123 bool IsFunc, bool IsGlob,
124 bool IsEvent) const {
Dan Gohman26c67652016-01-11 23:38:05 +0000125 MCSymbolRefExpr::VariantKind VK =
Heejin Ahnf208f632018-09-05 01:27:38 +0000126 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION
127 : IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL
Heejin Ahnda419bd2018-11-14 02:46:21 +0000128 : IsEvent ? MCSymbolRefExpr::VK_WebAssembly_EVENT
129 : MCSymbolRefExpr::VK_None;
Dan Gohmand934cb82017-02-24 23:18:00 +0000130
Dan Gohman26c67652016-01-11 23:38:05 +0000131 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
Dan Gohmane9361d52015-11-05 19:28:16 +0000132
Dan Gohmana4b710a2015-12-06 19:33:32 +0000133 if (Offset != 0) {
Dan Gohman26c67652016-01-11 23:38:05 +0000134 if (IsFunc)
135 report_fatal_error("Function addresses with offsets not supported");
Nicholas Wilsone408a892018-08-03 14:33:37 +0000136 if (IsGlob)
137 report_fatal_error("Global indexes with offsets not supported");
Heejin Ahnda419bd2018-11-14 02:46:21 +0000138 if (IsEvent)
139 report_fatal_error("Event indexes with offsets not supported");
Dan Gohmana4b710a2015-12-06 19:33:32 +0000140 Expr =
141 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
142 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000143
144 return MCOperand::createExpr(Expr);
145}
146
Dan Gohmand934cb82017-02-24 23:18:00 +0000147// Return the WebAssembly type associated with the given register class.
Derek Schuffe2688c42017-03-14 20:23:22 +0000148static wasm::ValType getType(const TargetRegisterClass *RC) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000149 if (RC == &WebAssembly::I32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000150 return wasm::ValType::I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000151 if (RC == &WebAssembly::I64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000152 return wasm::ValType::I64;
Dan Gohmand934cb82017-02-24 23:18:00 +0000153 if (RC == &WebAssembly::F32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000154 return wasm::ValType::F32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000155 if (RC == &WebAssembly::F64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000156 return wasm::ValType::F64;
Thomas Lively6f21a132018-09-20 22:04:44 +0000157 if (RC == &WebAssembly::V128RegClass)
158 return wasm::ValType::V128;
Dan Gohmand934cb82017-02-24 23:18:00 +0000159 llvm_unreachable("Unexpected register class");
160}
161
Heejin Ahn18c56a02019-02-04 19:13:39 +0000162void WebAssemblyMCInstLower::lower(const MachineInstr *MI,
Dan Gohmane9361d52015-11-05 19:28:16 +0000163 MCInst &OutMI) const {
164 OutMI.setOpcode(MI->getOpcode());
165
Dan Gohmand934cb82017-02-24 23:18:00 +0000166 const MCInstrDesc &Desc = MI->getDesc();
Heejin Ahn18c56a02019-02-04 19:13:39 +0000167 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
168 const MachineOperand &MO = MI->getOperand(I);
Dan Gohmane9361d52015-11-05 19:28:16 +0000169
170 MCOperand MCOp;
171 switch (MO.getType()) {
172 default:
Richard Trieu3de487b2017-01-28 03:23:49 +0000173 MI->print(errs());
Dan Gohmane9361d52015-11-05 19:28:16 +0000174 llvm_unreachable("unknown operand type");
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000175 case MachineOperand::MO_MachineBasicBlock:
Richard Trieu3de487b2017-01-28 03:23:49 +0000176 MI->print(errs());
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000177 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
Dan Gohmancf4748f2015-11-12 17:04:33 +0000178 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +0000179 // Ignore all implicit register operands.
180 if (MO.isImplicit())
181 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000182 const WebAssemblyFunctionInfo &MFI =
183 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
184 unsigned WAReg = MFI.getWAReg(MO.getReg());
185 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +0000186 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000187 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000188 case MachineOperand::MO_Immediate:
Heejin Ahn18c56a02019-02-04 19:13:39 +0000189 if (I < Desc.NumOperands) {
190 const MCOperandInfo &Info = Desc.OpInfo[I];
Dan Gohmand934cb82017-02-24 23:18:00 +0000191 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
192 MCSymbol *Sym = Printer.createTempSymbol("typeindex");
Dan Gohmand934cb82017-02-24 23:18:00 +0000193
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000194 SmallVector<wasm::ValType, 4> Returns;
195 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000196
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000197 const MachineRegisterInfo &MRI =
198 MI->getParent()->getParent()->getRegInfo();
199 for (const MachineOperand &MO : MI->defs())
200 Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
201 for (const MachineOperand &MO : MI->explicit_uses())
202 if (MO.isReg())
203 Params.push_back(getType(MRI.getRegClass(MO.getReg())));
Dan Gohmand934cb82017-02-24 23:18:00 +0000204
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000205 // call_indirect instructions have a callee operand at the end which
206 // doesn't count as a param.
207 if (WebAssembly::isCallIndirect(*MI))
208 Params.pop_back();
Dan Gohmand934cb82017-02-24 23:18:00 +0000209
Heejin Ahn18c56a02019-02-04 19:13:39 +0000210 auto *WasmSym = cast<MCSymbolWasm>(Sym);
Derek Schuff77a7a382018-10-03 22:22:48 +0000211 auto Signature = make_unique<wasm::WasmSignature>(std::move(Returns),
212 std::move(Params));
213 WasmSym->setSignature(Signature.get());
214 Printer.addSignature(std::move(Signature));
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000215 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
216
217 const MCExpr *Expr = MCSymbolRefExpr::create(
218 WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx);
219 MCOp = MCOperand::createExpr(Expr);
220 break;
Dan Gohmand934cb82017-02-24 23:18:00 +0000221 }
222 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000223 MCOp = MCOperand::createImm(MO.getImm());
224 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000225 case MachineOperand::MO_FPImmediate: {
226 // TODO: MC converts all floating point immediate operands to double.
227 // This is fine for numeric values, but may cause NaNs to change bits.
228 const ConstantFP *Imm = MO.getFPImm();
229 if (Imm->getType()->isFloatTy())
230 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
231 else if (Imm->getType()->isDoubleTy())
232 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
233 else
234 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +0000235 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000236 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000237 case MachineOperand::MO_GlobalAddress:
Nicholas Wilsone408a892018-08-03 14:33:37 +0000238 assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG &&
Dan Gohman26c67652016-01-11 23:38:05 +0000239 "WebAssembly does not use target flags on GlobalAddresses");
Heejin Ahn18c56a02019-02-04 19:13:39 +0000240 MCOp = lowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
Nicholas Wilsone408a892018-08-03 14:33:37 +0000241 MO.getGlobal()->getValueType()->isFunctionTy(),
Heejin Ahnda419bd2018-11-14 02:46:21 +0000242 false, false);
Dan Gohmane9361d52015-11-05 19:28:16 +0000243 break;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000244 case MachineOperand::MO_ExternalSymbol:
Dan Gohman26c67652016-01-11 23:38:05 +0000245 // The target flag indicates whether this is a symbol for a
246 // variable or a function.
Nicholas Wilsone408a892018-08-03 14:33:37 +0000247 assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 &&
248 "WebAssembly uses only symbol flags on ExternalSymbols");
Heejin Ahn18c56a02019-02-04 19:13:39 +0000249 MCOp = lowerSymbolOperand(
Heejin Ahnf208f632018-09-05 01:27:38 +0000250 GetExternalSymbolSymbol(MO), /*Offset=*/0,
Nicholas Wilsone408a892018-08-03 14:33:37 +0000251 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0,
Heejin Ahnda419bd2018-11-14 02:46:21 +0000252 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0,
253 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_EVENT) != 0);
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000254 break;
Heejin Ahn24faf852018-10-25 23:55:10 +0000255 case MachineOperand::MO_MCSymbol:
256 // This is currently used only for LSDA symbols (GCC_except_table),
257 // because global addresses or other external symbols are handled above.
258 assert(MO.getTargetFlags() == 0 &&
259 "WebAssembly does not use target flags on MCSymbol");
Heejin Ahn18c56a02019-02-04 19:13:39 +0000260 MCOp = lowerSymbolOperand(MO.getMCSymbol(), /*Offset=*/0, false, false,
Heejin Ahnda419bd2018-11-14 02:46:21 +0000261 false);
Heejin Ahn24faf852018-10-25 23:55:10 +0000262 break;
Dan Gohmane9361d52015-11-05 19:28:16 +0000263 }
264
265 OutMI.addOperand(MCOp);
266 }
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000267
268 if (!WasmKeepRegisters)
269 removeRegisterOperands(MI, OutMI);
270}
271
272static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
273 // Remove all uses of stackified registers to bring the instruction format
274 // into its final stack form used thruout MC, and transition opcodes to
275 // their _S variant.
276 // We do this seperate from the above code that still may need these
277 // registers for e.g. call_indirect signatures.
278 // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
279 // details.
280 // TODO: the code above creates new registers which are then removed here.
281 // That code could be slightly simplified by not doing that, though maybe
282 // it is simpler conceptually to keep the code above in "register mode"
283 // until this transition point.
284 // FIXME: we are not processing inline assembly, which contains register
285 // operands, because it is used by later target generic code.
286 if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
287 return;
288
289 // Transform to _S instruction.
290 auto RegOpcode = OutMI.getOpcode();
Thomas Livelyc63b5fc2018-10-22 21:55:26 +0000291 auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
292 assert(StackOpcode != -1 && "Failed to stackify instruction");
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000293 OutMI.setOpcode(StackOpcode);
294
295 // Remove register operands.
296 for (auto I = OutMI.getNumOperands(); I; --I) {
297 auto &MO = OutMI.getOperand(I - 1);
298 if (MO.isReg()) {
299 OutMI.erase(&MO);
300 }
301 }
302}