blob: 354662d9d3d59fc675398a4871d03801f8bd6bbc [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
11/// \brief This file contains code to lower WebAssembly MachineInstrs to their
12/// 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/MCSymbolELF.h"
29#include "llvm/MC/MCSymbolWasm.h"
Dan Gohmane9361d52015-11-05 19:28:16 +000030#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33
34MCSymbol *
35WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000036 const GlobalValue *Global = MO.getGlobal();
37 MCSymbol *Sym = Printer.getSymbol(Global);
38 if (isa<MCSymbolELF>(Sym))
39 return Sym;
40
41 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
42
43 if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
44 const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
45 const TargetMachine &TM = MF.getTarget();
46 const Function &CurrentFunc = *MF.getFunction();
47
48 SmallVector<unsigned, 4> Returns;
49 SmallVector<unsigned, 4> Params;
50
51 WebAssembly::ValType iPTR =
52 MF.getSubtarget<WebAssemblySubtarget>().hasAddr64() ?
53 WebAssembly::ValType::I64 :
54 WebAssembly::ValType::I32;
55
56 SmallVector<MVT, 4> ResultMVTs;
57 ComputeLegalValueVTs(CurrentFunc, TM, FuncTy->getReturnType(), ResultMVTs);
58 // WebAssembly can't currently handle returning tuples.
59 if (ResultMVTs.size() <= 1)
60 for (MVT ResultMVT : ResultMVTs)
61 Returns.push_back(unsigned(WebAssembly::toValType(ResultMVT)));
62 else
63 Params.push_back(unsigned(iPTR));
64
65 for (Type *Ty : FuncTy->params()) {
66 SmallVector<MVT, 4> ParamMVTs;
67 ComputeLegalValueVTs(CurrentFunc, TM, Ty, ParamMVTs);
68 for (MVT ParamMVT : ParamMVTs)
69 Params.push_back(unsigned(WebAssembly::toValType(ParamMVT)));
70 }
71
72 if (FuncTy->isVarArg())
73 Params.push_back(unsigned(iPTR));
74
75 WasmSym->setReturns(std::move(Returns));
76 WasmSym->setParams(std::move(Params));
77 WasmSym->setIsFunction(true);
78 }
79
80 return WasmSym;
Dan Gohmane9361d52015-11-05 19:28:16 +000081}
82
Dan Gohman7a6b9822015-11-29 22:32:02 +000083MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
84 const MachineOperand &MO) const {
Dan Gohmand934cb82017-02-24 23:18:00 +000085 const char *Name = MO.getSymbolName();
86 MCSymbol *Sym = Printer.GetExternalSymbolSymbol(Name);
87 if (isa<MCSymbolELF>(Sym))
88 return Sym;
89
90 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
91 const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();
92
93 // __stack_pointer is a global variable; all other external symbols used by
94 // CodeGen are functions.
95 if (strcmp(Name, "__stack_pointer") == 0)
96 return WasmSym;
97
98 SmallVector<unsigned, 4> Returns;
99 SmallVector<unsigned, 4> Params;
100 GetSignature(Subtarget, Name, Returns, Params);
101
102 WasmSym->setReturns(std::move(Returns));
103 WasmSym->setParams(std::move(Params));
104 WasmSym->setIsFunction(true);
105
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,
111 bool IsFunc) const {
112 MCSymbolRefExpr::VariantKind VK =
113 IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION
114 : MCSymbolRefExpr::VK_None;
Dan Gohmand934cb82017-02-24 23:18:00 +0000115 if (!isa<MCSymbolELF>(Sym))
116 cast<MCSymbolWasm>(Sym)->setIsFunction(IsFunc);
117
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");
Dan Gohmana4b710a2015-12-06 19:33:32 +0000123 Expr =
124 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx);
125 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000126
127 return MCOperand::createExpr(Expr);
128}
129
Dan Gohmand934cb82017-02-24 23:18:00 +0000130// Return the WebAssembly type associated with the given register class.
131static unsigned getType(const TargetRegisterClass *RC) {
132 if (RC == &WebAssembly::I32RegClass)
133 return unsigned(WebAssembly::ExprType::I32);
134 if (RC == &WebAssembly::I64RegClass)
135 return unsigned(WebAssembly::ExprType::I64);
136 if (RC == &WebAssembly::F32RegClass)
137 return unsigned(WebAssembly::ExprType::F32);
138 if (RC == &WebAssembly::F64RegClass)
139 return unsigned(WebAssembly::ExprType::F64);
140 llvm_unreachable("Unexpected register class");
141}
142
Dan Gohmane9361d52015-11-05 19:28:16 +0000143void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
144 MCInst &OutMI) const {
145 OutMI.setOpcode(MI->getOpcode());
146
Dan Gohmand934cb82017-02-24 23:18:00 +0000147 const MCInstrDesc &Desc = MI->getDesc();
Dan Gohmane9361d52015-11-05 19:28:16 +0000148 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
149 const MachineOperand &MO = MI->getOperand(i);
150
151 MCOperand MCOp;
152 switch (MO.getType()) {
153 default:
Richard Trieu3de487b2017-01-28 03:23:49 +0000154 MI->print(errs());
Dan Gohmane9361d52015-11-05 19:28:16 +0000155 llvm_unreachable("unknown operand type");
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000156 case MachineOperand::MO_MachineBasicBlock:
Richard Trieu3de487b2017-01-28 03:23:49 +0000157 MI->print(errs());
Dan Gohman1d68e80f2016-01-12 19:14:46 +0000158 llvm_unreachable("MachineBasicBlock operand should have been rewritten");
Dan Gohmancf4748f2015-11-12 17:04:33 +0000159 case MachineOperand::MO_Register: {
Dan Gohmane9361d52015-11-05 19:28:16 +0000160 // Ignore all implicit register operands.
161 if (MO.isImplicit())
162 continue;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000163 const WebAssemblyFunctionInfo &MFI =
164 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
165 unsigned WAReg = MFI.getWAReg(MO.getReg());
166 MCOp = MCOperand::createReg(WAReg);
Dan Gohmane9361d52015-11-05 19:28:16 +0000167 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000168 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000169 case MachineOperand::MO_Immediate:
Dan Gohmand934cb82017-02-24 23:18:00 +0000170 if (i < Desc.NumOperands) {
171 const MCOperandInfo &Info = Desc.OpInfo[i];
172 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
173 MCSymbol *Sym = Printer.createTempSymbol("typeindex");
174 if (!isa<MCSymbolELF>(Sym)) {
175 SmallVector<unsigned, 4> Returns;
176 SmallVector<unsigned, 4> Params;
177
178 const MachineRegisterInfo &MRI =
179 MI->getParent()->getParent()->getRegInfo();
180 for (const MachineOperand &MO : MI->defs())
181 Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
182 for (const MachineOperand &MO : MI->explicit_uses())
183 if (MO.isReg())
184 Params.push_back(getType(MRI.getRegClass(MO.getReg())));
185
186 // call_indirect instructions have a callee operand at the end which
187 // doesn't count as a param.
188 if (WebAssembly::isCallIndirect(*MI))
189 Params.pop_back();
190
191 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
192 WasmSym->setReturns(std::move(Returns));
193 WasmSym->setParams(std::move(Params));
194 WasmSym->setIsFunction(true);
195
196 const MCExpr *Expr =
197 MCSymbolRefExpr::create(WasmSym,
198 MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX,
199 Ctx);
200 MCOp = MCOperand::createExpr(Expr);
201 break;
202 }
203 }
204 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000205 MCOp = MCOperand::createImm(MO.getImm());
206 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000207 case MachineOperand::MO_FPImmediate: {
208 // TODO: MC converts all floating point immediate operands to double.
209 // This is fine for numeric values, but may cause NaNs to change bits.
210 const ConstantFP *Imm = MO.getFPImm();
211 if (Imm->getType()->isFloatTy())
212 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
213 else if (Imm->getType()->isDoubleTy())
214 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
215 else
216 llvm_unreachable("unknown floating point immediate type");
Dan Gohmane9361d52015-11-05 19:28:16 +0000217 break;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000218 }
Dan Gohmane9361d52015-11-05 19:28:16 +0000219 case MachineOperand::MO_GlobalAddress:
Dan Gohman26c67652016-01-11 23:38:05 +0000220 assert(MO.getTargetFlags() == 0 &&
221 "WebAssembly does not use target flags on GlobalAddresses");
222 MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
223 MO.getGlobal()->getValueType()->isFunctionTy());
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.
228 assert((MO.getTargetFlags() & -2) == 0 &&
229 "WebAssembly uses only one target flag bit on ExternalSymbols");
230 MCOp = LowerSymbolOperand(GetExternalSymbolSymbol(MO), /*Offset=*/0,
231 MO.getTargetFlags() & 1);
Dan Gohman2c8fe6a2015-11-25 16:44:29 +0000232 break;
Dan Gohmane9361d52015-11-05 19:28:16 +0000233 }
234
235 OutMI.addOperand(MCOp);
236 }
237}