blob: fa862fbaa634c2b3772f9958af28049ed00d2983 [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
Thomas Livelyc63b5fc2018-10-22 21:55:26 +000033// Defines llvm::WebAssembly::getStackOpcode to convert register instructions to
34// stack instructions
35#define GET_INSTRMAP_INFO 1
36#include "WebAssemblyGenInstrInfo.inc"
37
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000038// This disables the removal of registers when lowering into MC, as required
39// by some current tests.
Heejin Ahnf208f632018-09-05 01:27:38 +000040static cl::opt<bool>
41 WasmKeepRegisters("wasm-keep-registers", cl::Hidden,
42 cl::desc("WebAssembly: output stack registers in"
43 " instruction output for test purposes only."),
44 cl::init(false));
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000045
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000046static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
47
Dan Gohmane9361d52015-11-05 19:28:16 +000048MCSymbol *
49WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000050 const GlobalValue *Global = MO.getGlobal();
Sam Cleggcf2a9e22018-07-16 23:09:29 +000051 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
Dan Gohmand934cb82017-02-24 23:18:00 +000052
53 if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
54 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
55 const TargetMachine &TM = MF.getTarget();
David Blaikie21109242017-12-15 23:52:06 +000056 const Function &CurrentFunc = MF.getFunction();
Dan Gohmand934cb82017-02-24 23:18:00 +000057
Derek Schuff77a7a382018-10-03 22:22:48 +000058 SmallVector<MVT, 1> ResultMVTs;
59 SmallVector<MVT, 4> ParamMVTs;
60 ComputeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs);
Dan Gohmand934cb82017-02-24 23:18:00 +000061
Derek Schuff77a7a382018-10-03 22:22:48 +000062 auto Signature = SignatureFromMVTs(ResultMVTs, ParamMVTs);
63 WasmSym->setSignature(Signature.get());
64 Printer.addSignature(std::move(Signature));
Sam Clegg6c899ba2018-02-23 05:08:34 +000065 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +000066 }
67
68 return WasmSym;
Dan Gohmane9361d52015-11-05 19:28:16 +000069}
70
Dan Gohman7a6b9822015-11-29 22:32:02 +000071MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
72 const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000073 const char *Name = MO.getSymbolName();
Sam Cleggcf2a9e22018-07-16 23:09:29 +000074 MCSymbolWasm *WasmSym =
75 cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
Dan Gohmand934cb82017-02-24 23:18:00 +000076 const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
77
Heejin Ahnda419bd2018-11-14 02:46:21 +000078 // Except for the two exceptions (__stack_pointer and __cpp_exception), all
79 // other external symbols used by CodeGen are functions. It's OK to hardcode
80 // knowledge of specific symbols here; this method is precisely there for
81 // fetching the signatures of known Clang-provided symbols.
Sam Clegg6c899ba2018-02-23 05:08:34 +000082 if (strcmp(Name, "__stack_pointer") == 0) {
Sam Clegg6c899ba2018-02-23 05:08:34 +000083 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
Sam Clegg03e101f2018-03-01 18:06:21 +000084 WasmSym->setGlobalType(wasm::WasmGlobalType{
Sam Clegg503fdea2018-03-01 18:48:08 +000085 uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
86 : wasm::WASM_TYPE_I32),
Sam Clegg03e101f2018-03-01 18:06:21 +000087 true});
Dan Gohmand934cb82017-02-24 23:18:00 +000088 return WasmSym;
Sam Clegg6c899ba2018-02-23 05:08:34 +000089 }
Dan Gohmand934cb82017-02-24 23:18:00 +000090
Derek Schuffe2688c42017-03-14 20:23:22 +000091 SmallVector<wasm::ValType, 4> Returns;
92 SmallVector<wasm::ValType, 4> Params;
Heejin Ahnda419bd2018-11-14 02:46:21 +000093 if (strcmp(Name, "__cpp_exception") == 0) {
94 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
95 // We can't confirm its signature index for now because there can be
96 // imported exceptions. Set it to be 0 for now.
97 WasmSym->setEventType(
98 {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
99 // We may have multiple C++ compilation units to be linked together, each of
100 // which defines the exception symbol. To resolve them, we declare them as
101 // weak.
102 WasmSym->setWeak(true);
103 WasmSym->setExternal(true);
104
105 // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
106 // (for wasm64) param type and void return type. The reaon is, all C++
107 // exception values are pointers, and to share the type section with
108 // functions, exceptions are assumed to have void return type.
109 Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
110 : wasm::ValType::I32);
111 } else { // Function symbols
112 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
113 GetLibcallSignature(Subtarget, Name, Returns, Params);
114 }
Derek Schuff77a7a382018-10-03 22:22:48 +0000115 auto Signature =
116 make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
117 WasmSym->setSignature(Signature.get());
118 Printer.addSignature(std::move(Signature));
Dan Gohmand934cb82017-02-24 23:18:00 +0000119
120 return WasmSym;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000121}
122
Dan Gohman26c67652016-01-11 23:38:05 +0000123MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym,
124 int64_t Offset,
Heejin Ahnda419bd2018-11-14 02:46:21 +0000125 bool IsFunc, bool IsGlob,
126 bool IsEvent) const {
Dan Gohman26c67652016-01-11 23:38:05 +0000127 MCSymbolRefExpr::VariantKind VK =
Heejin Ahnf208f632018-09-05 01:27:38 +0000128 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION
129 : IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL
Heejin Ahnda419bd2018-11-14 02:46:21 +0000130 : IsEvent ? MCSymbolRefExpr::VK_WebAssembly_EVENT
131 : MCSymbolRefExpr::VK_None;
Dan Gohmand934cb82017-02-24 23:18:00 +0000132
Dan Gohman26c67652016-01-11 23:38:05 +0000133 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
Dan Gohmane9361d52015-11-05 19:28:16 +0000134
Dan Gohmana4b710a2015-12-06 19:33:32 +0000135 if (Offset != 0) {
Dan Gohman26c67652016-01-11 23:38:05 +0000136 if (IsFunc)
137 report_fatal_error("Function addresses with offsets not supported");
Nicholas Wilsone408a892018-08-03 14:33:37 +0000138 if (IsGlob)
139 report_fatal_error("Global indexes with offsets not supported");
Heejin Ahnda419bd2018-11-14 02:46:21 +0000140 if (IsEvent)
141 report_fatal_error("Event indexes with offsets not supported");
Dan Gohmana4b710a2015-12-06 19:33:32 +0000142 Expr =
143 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
144 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000145
146 return MCOperand::createExpr(Expr);
147}
148
Dan Gohmand934cb82017-02-24 23:18:00 +0000149// Return the WebAssembly type associated with the given register class.
Derek Schuffe2688c42017-03-14 20:23:22 +0000150static wasm::ValType getType(const TargetRegisterClass *RC) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000151 if (RC == &WebAssembly::I32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000152 return wasm::ValType::I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000153 if (RC == &WebAssembly::I64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000154 return wasm::ValType::I64;
Dan Gohmand934cb82017-02-24 23:18:00 +0000155 if (RC == &WebAssembly::F32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000156 return wasm::ValType::F32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000157 if (RC == &WebAssembly::F64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000158 return wasm::ValType::F64;
Thomas Lively6f21a132018-09-20 22:04:44 +0000159 if (RC == &WebAssembly::V128RegClass)
160 return wasm::ValType::V128;
Dan Gohmand934cb82017-02-24 23:18:00 +0000161 llvm_unreachable("Unexpected register class");
162}
163
Dan Gohmane9361d52015-11-05 19:28:16 +0000164void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
165 MCInst &OutMI) const {
166 OutMI.setOpcode(MI->getOpcode());
167
Dan Gohmand934cb82017-02-24 23:18:00 +0000168 const MCInstrDesc &Desc = MI->getDesc();
Dan Gohmane9361d52015-11-05 19:28:16 +0000169 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
170 const MachineOperand &MO = MI->getOperand(i);
171
172 MCOperand MCOp;
173 switch (MO.getType()) {
174 default:
Richard Trieu3de487b2017-01-28 03:23:49 +0000175 MI->print(errs());
Dan Gohmane9361d52015-11-05 19:28:16 +0000176 llvm_unreachable("unknown operand type");
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000177 case MachineOperand::MO_MachineBasicBlock:
Richard Trieu3de487b2017-01-28 03:23:49 +0000178 MI->print(errs());
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000179 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
Dan Gohmancf4748f2015-11-12 17:04:33 +0000180 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +0000181 // Ignore all implicit register operands.
182 if (MO.isImplicit())
183 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000184 const WebAssemblyFunctionInfo &MFI =
185 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
186 unsigned WAReg = MFI.getWAReg(MO.getReg());
187 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +0000188 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000189 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000190 case MachineOperand::MO_Immediate:
Dan Gohmand934cb82017-02-24 23:18:00 +0000191 if (i < Desc.NumOperands) {
192 const MCOperandInfo &Info = Desc.OpInfo[i];
193 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
194 MCSymbol *Sym = Printer.createTempSymbol("typeindex");
Dan Gohmand934cb82017-02-24 23:18:00 +0000195
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000196 SmallVector<wasm::ValType, 4> Returns;
197 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000198
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000199 const MachineRegisterInfo &MRI =
200 MI->getParent()->getParent()->getRegInfo();
201 for (const MachineOperand &MO : MI->defs())
202 Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
203 for (const MachineOperand &MO : MI->explicit_uses())
204 if (MO.isReg())
205 Params.push_back(getType(MRI.getRegClass(MO.getReg())));
Dan Gohmand934cb82017-02-24 23:18:00 +0000206
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000207 // call_indirect instructions have a callee operand at the end which
208 // doesn't count as a param.
209 if (WebAssembly::isCallIndirect(*MI))
210 Params.pop_back();
Dan Gohmand934cb82017-02-24 23:18:00 +0000211
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000212 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
Derek Schuff77a7a382018-10-03 22:22:48 +0000213 auto Signature = make_unique<wasm::WasmSignature>(std::move(Returns),
214 std::move(Params));
215 WasmSym->setSignature(Signature.get());
216 Printer.addSignature(std::move(Signature));
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000217 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
218
219 const MCExpr *Expr = MCSymbolRefExpr::create(
220 WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx);
221 MCOp = MCOperand::createExpr(Expr);
222 break;
Dan Gohmand934cb82017-02-24 23:18:00 +0000223 }
224 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000225 MCOp = MCOperand::createImm(MO.getImm());
226 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000227 case MachineOperand::MO_FPImmediate: {
228 // TODO: MC converts all floating point immediate operands to double.
229 // This is fine for numeric values, but may cause NaNs to change bits.
230 const ConstantFP *Imm = MO.getFPImm();
231 if (Imm->getType()->isFloatTy())
232 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
233 else if (Imm->getType()->isDoubleTy())
234 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
235 else
236 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +0000237 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000238 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000239 case MachineOperand::MO_GlobalAddress:
Nicholas Wilsone408a892018-08-03 14:33:37 +0000240 assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG &&
Dan Gohman26c67652016-01-11 23:38:05 +0000241 "WebAssembly does not use target flags on GlobalAddresses");
242 MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
Nicholas Wilsone408a892018-08-03 14:33:37 +0000243 MO.getGlobal()->getValueType()->isFunctionTy(),
Heejin Ahnda419bd2018-11-14 02:46:21 +0000244 false, false);
Dan Gohmane9361d52015-11-05 19:28:16 +0000245 break;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000246 case MachineOperand::MO_ExternalSymbol:
Dan Gohman26c67652016-01-11 23:38:05 +0000247 // The target flag indicates whether this is a symbol for a
248 // variable or a function.
Nicholas Wilsone408a892018-08-03 14:33:37 +0000249 assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 &&
250 "WebAssembly uses only symbol flags on ExternalSymbols");
Heejin Ahnf208f632018-09-05 01:27:38 +0000251 MCOp = LowerSymbolOperand(
252 GetExternalSymbolSymbol(MO), /*Offset=*/0,
Nicholas Wilsone408a892018-08-03 14:33:37 +0000253 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0,
Heejin Ahnda419bd2018-11-14 02:46:21 +0000254 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0,
255 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_EVENT) != 0);
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000256 break;
Heejin Ahn24faf852018-10-25 23:55:10 +0000257 case MachineOperand::MO_MCSymbol:
258 // This is currently used only for LSDA symbols (GCC_except_table),
259 // because global addresses or other external symbols are handled above.
260 assert(MO.getTargetFlags() == 0 &&
261 "WebAssembly does not use target flags on MCSymbol");
Heejin Ahnda419bd2018-11-14 02:46:21 +0000262 MCOp = LowerSymbolOperand(MO.getMCSymbol(), /*Offset=*/0, false, false,
263 false);
Heejin Ahn24faf852018-10-25 23:55:10 +0000264 break;
Dan Gohmane9361d52015-11-05 19:28:16 +0000265 }
266
267 OutMI.addOperand(MCOp);
268 }
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000269
270 if (!WasmKeepRegisters)
271 removeRegisterOperands(MI, OutMI);
272}
273
274static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) {
275 // Remove all uses of stackified registers to bring the instruction format
276 // into its final stack form used thruout MC, and transition opcodes to
277 // their _S variant.
278 // We do this seperate from the above code that still may need these
279 // registers for e.g. call_indirect signatures.
280 // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for
281 // details.
282 // TODO: the code above creates new registers which are then removed here.
283 // That code could be slightly simplified by not doing that, though maybe
284 // it is simpler conceptually to keep the code above in "register mode"
285 // until this transition point.
286 // FIXME: we are not processing inline assembly, which contains register
287 // operands, because it is used by later target generic code.
288 if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm())
289 return;
290
291 // Transform to _S instruction.
292 auto RegOpcode = OutMI.getOpcode();
Thomas Livelyc63b5fc2018-10-22 21:55:26 +0000293 auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode);
294 assert(StackOpcode != -1 && "Failed to stackify instruction");
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000295 OutMI.setOpcode(StackOpcode);
296
297 // Remove register operands.
298 for (auto I = OutMI.getNumOperands(); I; --I) {
299 auto &MO = OutMI.getOperand(I - 1);
300 if (MO.isReg()) {
301 OutMI.erase(&MO);
302 }
303 }
304}