blob: 5a9e3c67176625469d3ead23b93c7b3b368fda5e [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
33MCSymbol *
34WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000035 const GlobalValue *Global = MO.getGlobal();
Sam Cleggcf2a9e22018-07-16 23:09:29 +000036 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));
Dan Gohmand934cb82017-02-24 23:18:00 +000037
38 if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
39 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
40 const TargetMachine &TM = MF.getTarget();
David Blaikie21109242017-12-15 23:52:06 +000041 const Function &CurrentFunc = MF.getFunction();
Dan Gohmand934cb82017-02-24 23:18:00 +000042
Derek Schuffe2688c42017-03-14 20:23:22 +000043 SmallVector<wasm::ValType, 4> Returns;
44 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +000045
Derek Schuffe2688c42017-03-14 20:23:22 +000046 wasm::ValType iPTR =
Dan Gohmand934cb82017-02-24 23:18:00 +000047 MF.getSubtarget<WebAssemblySubtarget>().hasAddr64() ?
Derek Schuffe2688c42017-03-14 20:23:22 +000048 wasm::ValType::I64 :
49 wasm::ValType::I32;
Dan Gohmand934cb82017-02-24 23:18:00 +000050
51 SmallVector<MVT, 4> ResultMVTs;
52 ComputeLegalValueVTs(CurrentFunc, TM, FuncTy->getReturnType(), ResultMVTs);
53 // WebAssembly can't currently handle returning tuples.
54 if (ResultMVTs.size() <= 1)
55 for (MVT ResultMVT : ResultMVTs)
Derek Schuffe2688c42017-03-14 20:23:22 +000056 Returns.push_back(WebAssembly::toValType(ResultMVT));
Dan Gohmand934cb82017-02-24 23:18:00 +000057 else
Derek Schuffe2688c42017-03-14 20:23:22 +000058 Params.push_back(iPTR);
Dan Gohmand934cb82017-02-24 23:18:00 +000059
60 for (Type *Ty : FuncTy->params()) {
61 SmallVector<MVT, 4> ParamMVTs;
62 ComputeLegalValueVTs(CurrentFunc, TM, Ty, ParamMVTs);
63 for (MVT ParamMVT : ParamMVTs)
Derek Schuffe2688c42017-03-14 20:23:22 +000064 Params.push_back(WebAssembly::toValType(ParamMVT));
Dan Gohmand934cb82017-02-24 23:18:00 +000065 }
66
67 if (FuncTy->isVarArg())
Derek Schuffe2688c42017-03-14 20:23:22 +000068 Params.push_back(iPTR);
Dan Gohmand934cb82017-02-24 23:18:00 +000069
70 WasmSym->setReturns(std::move(Returns));
71 WasmSym->setParams(std::move(Params));
Sam Clegg6c899ba2018-02-23 05:08:34 +000072 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +000073 }
74
75 return WasmSym;
Dan Gohmane9361d52015-11-05 19:28:16 +000076}
77
Dan Gohman7a6b9822015-11-29 22:32:02 +000078MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
79 const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000080 const char *Name = MO.getSymbolName();
Sam Cleggcf2a9e22018-07-16 23:09:29 +000081 MCSymbolWasm *WasmSym =
82 cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
Dan Gohmand934cb82017-02-24 23:18:00 +000083 const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
84
85 // __stack_pointer is a global variable; all other external symbols used by
Sam Clegg6c899ba2018-02-23 05:08:34 +000086 // CodeGen are functions. It's OK to hardcode knowledge of specific symbols
87 // here; this method is precisely there for fetching the signatures of known
88 // Clang-provided symbols.
89 if (strcmp(Name, "__stack_pointer") == 0) {
Sam Clegg6c899ba2018-02-23 05:08:34 +000090 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
Sam Clegg03e101f2018-03-01 18:06:21 +000091 WasmSym->setGlobalType(wasm::WasmGlobalType{
Sam Clegg503fdea2018-03-01 18:48:08 +000092 uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
93 : wasm::WASM_TYPE_I32),
Sam Clegg03e101f2018-03-01 18:06:21 +000094 true});
Dan Gohmand934cb82017-02-24 23:18:00 +000095 return WasmSym;
Sam Clegg6c899ba2018-02-23 05:08:34 +000096 }
Dan Gohmand934cb82017-02-24 23:18:00 +000097
Derek Schuffe2688c42017-03-14 20:23:22 +000098 SmallVector<wasm::ValType, 4> Returns;
99 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000100 GetSignature(Subtarget, Name, Returns, Params);
101
102 WasmSym->setReturns(std::move(Returns));
103 WasmSym->setParams(std::move(Params));
Sam Clegg6c899ba2018-02-23 05:08:34 +0000104 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
Dan Gohmand934cb82017-02-24 23:18:00 +0000105
106 return WasmSym;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000107}
108
Dan Gohman26c67652016-01-11 23:38:05 +0000109MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym,
110 int64_t Offset,
Nicholas Wilsone408a892018-08-03 14:33:37 +0000111 bool IsFunc,
112 bool IsGlob) const {
Dan Gohman26c67652016-01-11 23:38:05 +0000113 MCSymbolRefExpr::VariantKind VK =
Nicholas Wilsone408a892018-08-03 14:33:37 +0000114 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION :
115 IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL
Dan Gohman26c67652016-01-11 23:38:05 +0000116 : MCSymbolRefExpr::VK_None;
Dan Gohmand934cb82017-02-24 23:18:00 +0000117
Dan Gohman26c67652016-01-11 23:38:05 +0000118 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx);
Dan Gohmane9361d52015-11-05 19:28:16 +0000119
Dan Gohmana4b710a2015-12-06 19:33:32 +0000120 if (Offset != 0) {
Dan Gohman26c67652016-01-11 23:38:05 +0000121 if (IsFunc)
122 report_fatal_error("Function addresses with offsets not supported");
Nicholas Wilsone408a892018-08-03 14:33:37 +0000123 if (IsGlob)
124 report_fatal_error("Global indexes with offsets not supported");
Dan Gohmana4b710a2015-12-06 19:33:32 +0000125 Expr =
126 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
127 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000128
129 return MCOperand::createExpr(Expr);
130}
131
Dan Gohmand934cb82017-02-24 23:18:00 +0000132// Return the WebAssembly type associated with the given register class.
Derek Schuffe2688c42017-03-14 20:23:22 +0000133static wasm::ValType getType(const TargetRegisterClass *RC) {
Dan Gohmand934cb82017-02-24 23:18:00 +0000134 if (RC == &WebAssembly::I32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000135 return wasm::ValType::I32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000136 if (RC == &WebAssembly::I64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000137 return wasm::ValType::I64;
Dan Gohmand934cb82017-02-24 23:18:00 +0000138 if (RC == &WebAssembly::F32RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000139 return wasm::ValType::F32;
Dan Gohmand934cb82017-02-24 23:18:00 +0000140 if (RC == &WebAssembly::F64RegClass)
Derek Schuffe2688c42017-03-14 20:23:22 +0000141 return wasm::ValType::F64;
Dan Gohmand934cb82017-02-24 23:18:00 +0000142 llvm_unreachable("Unexpected register class");
143}
144
Dan Gohmane9361d52015-11-05 19:28:16 +0000145void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
146 MCInst &OutMI) const {
147 OutMI.setOpcode(MI->getOpcode());
148
Dan Gohmand934cb82017-02-24 23:18:00 +0000149 const MCInstrDesc &Desc = MI->getDesc();
Dan Gohmane9361d52015-11-05 19:28:16 +0000150 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
151 const MachineOperand &MO = MI->getOperand(i);
152
153 MCOperand MCOp;
154 switch (MO.getType()) {
155 default:
Richard Trieu3de487b2017-01-28 03:23:49 +0000156 MI->print(errs());
Dan Gohmane9361d52015-11-05 19:28:16 +0000157 llvm_unreachable("unknown operand type");
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000158 case MachineOperand::MO_MachineBasicBlock:
Richard Trieu3de487b2017-01-28 03:23:49 +0000159 MI->print(errs());
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000160 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
Dan Gohmancf4748f2015-11-12 17:04:33 +0000161 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +0000162 // Ignore all implicit register operands.
163 if (MO.isImplicit())
164 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000165 const WebAssemblyFunctionInfo &MFI =
166 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
167 unsigned WAReg = MFI.getWAReg(MO.getReg());
168 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +0000169 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000170 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000171 case MachineOperand::MO_Immediate:
Dan Gohmand934cb82017-02-24 23:18:00 +0000172 if (i < Desc.NumOperands) {
173 const MCOperandInfo &Info = Desc.OpInfo[i];
174 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
175 MCSymbol *Sym = Printer.createTempSymbol("typeindex");
Dan Gohmand934cb82017-02-24 23:18:00 +0000176
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000177 SmallVector<wasm::ValType, 4> Returns;
178 SmallVector<wasm::ValType, 4> Params;
Dan Gohmand934cb82017-02-24 23:18:00 +0000179
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000180 const MachineRegisterInfo &MRI =
181 MI->getParent()->getParent()->getRegInfo();
182 for (const MachineOperand &MO : MI->defs())
183 Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
184 for (const MachineOperand &MO : MI->explicit_uses())
185 if (MO.isReg())
186 Params.push_back(getType(MRI.getRegClass(MO.getReg())));
Dan Gohmand934cb82017-02-24 23:18:00 +0000187
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000188 // call_indirect instructions have a callee operand at the end which
189 // doesn't count as a param.
190 if (WebAssembly::isCallIndirect(*MI))
191 Params.pop_back();
Dan Gohmand934cb82017-02-24 23:18:00 +0000192
Sam Cleggcf2a9e22018-07-16 23:09:29 +0000193 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
194 WasmSym->setReturns(std::move(Returns));
195 WasmSym->setParams(std::move(Params));
196 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
197
198 const MCExpr *Expr = MCSymbolRefExpr::create(
199 WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx);
200 MCOp = MCOperand::createExpr(Expr);
201 break;
Dan Gohmand934cb82017-02-24 23:18:00 +0000202 }
203 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000204 MCOp = MCOperand::createImm(MO.getImm());
205 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000206 case MachineOperand::MO_FPImmediate: {
207 // TODO: MC converts all floating point immediate operands to double.
208 // This is fine for numeric values, but may cause NaNs to change bits.
209 const ConstantFP *Imm = MO.getFPImm();
210 if (Imm->getType()->isFloatTy())
211 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
212 else if (Imm->getType()->isDoubleTy())
213 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
214 else
215 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +0000216 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000217 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000218 case MachineOperand::MO_GlobalAddress:
Nicholas Wilsone408a892018-08-03 14:33:37 +0000219 assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG &&
Dan Gohman26c67652016-01-11 23:38:05 +0000220 "WebAssembly does not use target flags on GlobalAddresses");
221 MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
Nicholas Wilsone408a892018-08-03 14:33:37 +0000222 MO.getGlobal()->getValueType()->isFunctionTy(),
223 false);
Dan Gohmane9361d52015-11-05 19:28:16 +0000224 break;
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000225 case MachineOperand::MO_ExternalSymbol:
Dan Gohman26c67652016-01-11 23:38:05 +0000226 // The target flag indicates whether this is a symbol for a
227 // variable or a function.
Nicholas Wilsone408a892018-08-03 14:33:37 +0000228 assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 &&
229 "WebAssembly uses only symbol flags on ExternalSymbols");
Dan Gohman26c67652016-01-11 23:38:05 +0000230 MCOp = LowerSymbolOperand(GetExternalSymbolSymbol(MO), /*Offset=*/0,
Nicholas Wilsone408a892018-08-03 14:33:37 +0000231 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0,
232 (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0);
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000233 break;
Dan Gohmane9361d52015-11-05 19:28:16 +0000234 }
235
236 OutMI.addOperand(MCOp);
237 }
238}