WebAssemblyMCInstLower.cpp revision 327952
1254402Serwin// WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst // 2224090Sdougb// 3224090Sdougb// The LLVM Compiler Infrastructure 4224090Sdougb// 5224090Sdougb// This file is distributed under the University of Illinois Open Source 6224090Sdougb// License. See LICENSE.TXT for details. 7224090Sdougb// 8224090Sdougb//===----------------------------------------------------------------------===// 9224090Sdougb/// 10224090Sdougb/// \file 11224090Sdougb/// \brief This file contains code to lower WebAssembly MachineInstrs to their 12224090Sdougb/// corresponding MCInst records. 13224090Sdougb/// 14224090Sdougb//===----------------------------------------------------------------------===// 15234010Sdougb 16224090Sdougb#include "WebAssemblyMCInstLower.h" 17224090Sdougb#include "WebAssemblyAsmPrinter.h" 18224090Sdougb#include "WebAssemblyMachineFunctionInfo.h" 19224090Sdougb#include "WebAssemblyRuntimeLibcallSignatures.h" 20224090Sdougb#include "WebAssemblyUtilities.h" 21224090Sdougb#include "llvm/CodeGen/AsmPrinter.h" 22224090Sdougb#include "llvm/CodeGen/MachineFunction.h" 23224090Sdougb#include "llvm/IR/Constants.h" 24224090Sdougb#include "llvm/MC/MCAsmInfo.h" 25224090Sdougb#include "llvm/MC/MCContext.h" 26224090Sdougb#include "llvm/MC/MCExpr.h" 27224090Sdougb#include "llvm/MC/MCInst.h" 28224090Sdougb#include "llvm/MC/MCSymbolELF.h" 29224090Sdougb#include "llvm/MC/MCSymbolWasm.h" 30224090Sdougb#include "llvm/Support/ErrorHandling.h" 31224090Sdougb#include "llvm/Support/raw_ostream.h" 32224090Sdougbusing namespace llvm; 33224090Sdougb 34254402SerwinMCSymbol * 35224090SdougbWebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { 36224090Sdougb const GlobalValue *Global = MO.getGlobal(); 37224090Sdougb MCSymbol *Sym = Printer.getSymbol(Global); 38224090Sdougb if (isa<MCSymbolELF>(Sym)) 39224090Sdougb return Sym; 40224090Sdougb 41224090Sdougb MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym); 42224090Sdougb 43224090Sdougb if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) { 44224090Sdougb const MachineFunction &MF = *MO.getParent()->getParent()->getParent(); 45224090Sdougb const TargetMachine &TM = MF.getTarget(); 46224090Sdougb const Function &CurrentFunc = MF.getFunction(); 47224090Sdougb 48224090Sdougb SmallVector<wasm::ValType, 4> Returns; 49224090Sdougb SmallVector<wasm::ValType, 4> Params; 50224090Sdougb 51224090Sdougb wasm::ValType iPTR = 52224090Sdougb MF.getSubtarget<WebAssemblySubtarget>().hasAddr64() ? 53224090Sdougb wasm::ValType::I64 : 54224090Sdougb wasm::ValType::I32; 55224090Sdougb 56224090Sdougb SmallVector<MVT, 4> ResultMVTs; 57224090Sdougb ComputeLegalValueVTs(CurrentFunc, TM, FuncTy->getReturnType(), ResultMVTs); 58224090Sdougb // WebAssembly can't currently handle returning tuples. 59224090Sdougb if (ResultMVTs.size() <= 1) 60224090Sdougb for (MVT ResultMVT : ResultMVTs) 61224090Sdougb Returns.push_back(WebAssembly::toValType(ResultMVT)); 62224090Sdougb else 63224090Sdougb Params.push_back(iPTR); 64224090Sdougb 65224090Sdougb for (Type *Ty : FuncTy->params()) { 66224090Sdougb SmallVector<MVT, 4> ParamMVTs; 67224090Sdougb ComputeLegalValueVTs(CurrentFunc, TM, Ty, ParamMVTs); 68224090Sdougb for (MVT ParamMVT : ParamMVTs) 69224090Sdougb Params.push_back(WebAssembly::toValType(ParamMVT)); 70224090Sdougb } 71224090Sdougb 72224090Sdougb if (FuncTy->isVarArg()) 73224090Sdougb Params.push_back(iPTR); 74224090Sdougb 75224090Sdougb WasmSym->setReturns(std::move(Returns)); 76224090Sdougb WasmSym->setParams(std::move(Params)); 77224090Sdougb WasmSym->setIsFunction(true); 78224090Sdougb } 79224090Sdougb 80225361Sdougb return WasmSym; 81224090Sdougb} 82224090Sdougb 83224090SdougbMCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol( 84 const MachineOperand &MO) const { 85 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<wasm::ValType, 4> Returns; 99 SmallVector<wasm::ValType, 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; 107} 108 109MCOperand 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; 115 116 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx); 117 118 if (Offset != 0) { 119 if (IsFunc) 120 report_fatal_error("Function addresses with offsets not supported"); 121 Expr = 122 MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx); 123 } 124 125 return MCOperand::createExpr(Expr); 126} 127 128// Return the WebAssembly type associated with the given register class. 129static wasm::ValType getType(const TargetRegisterClass *RC) { 130 if (RC == &WebAssembly::I32RegClass) 131 return wasm::ValType::I32; 132 if (RC == &WebAssembly::I64RegClass) 133 return wasm::ValType::I64; 134 if (RC == &WebAssembly::F32RegClass) 135 return wasm::ValType::F32; 136 if (RC == &WebAssembly::F64RegClass) 137 return wasm::ValType::F64; 138 llvm_unreachable("Unexpected register class"); 139} 140 141void WebAssemblyMCInstLower::Lower(const MachineInstr *MI, 142 MCInst &OutMI) const { 143 OutMI.setOpcode(MI->getOpcode()); 144 145 const MCInstrDesc &Desc = MI->getDesc(); 146 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 147 const MachineOperand &MO = MI->getOperand(i); 148 149 MCOperand MCOp; 150 switch (MO.getType()) { 151 default: 152 MI->print(errs()); 153 llvm_unreachable("unknown operand type"); 154 case MachineOperand::MO_MachineBasicBlock: 155 MI->print(errs()); 156 llvm_unreachable("MachineBasicBlock operand should have been rewritten"); 157 case MachineOperand::MO_Register: { 158 // Ignore all implicit register operands. 159 if (MO.isImplicit()) 160 continue; 161 const WebAssemblyFunctionInfo &MFI = 162 *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); 163 unsigned WAReg = MFI.getWAReg(MO.getReg()); 164 MCOp = MCOperand::createReg(WAReg); 165 break; 166 } 167 case MachineOperand::MO_Immediate: 168 if (i < Desc.NumOperands) { 169 const MCOperandInfo &Info = Desc.OpInfo[i]; 170 if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) { 171 MCSymbol *Sym = Printer.createTempSymbol("typeindex"); 172 if (!isa<MCSymbolELF>(Sym)) { 173 SmallVector<wasm::ValType, 4> Returns; 174 SmallVector<wasm::ValType, 4> Params; 175 176 const MachineRegisterInfo &MRI = 177 MI->getParent()->getParent()->getRegInfo(); 178 for (const MachineOperand &MO : MI->defs()) 179 Returns.push_back(getType(MRI.getRegClass(MO.getReg()))); 180 for (const MachineOperand &MO : MI->explicit_uses()) 181 if (MO.isReg()) 182 Params.push_back(getType(MRI.getRegClass(MO.getReg()))); 183 184 // call_indirect instructions have a callee operand at the end which 185 // doesn't count as a param. 186 if (WebAssembly::isCallIndirect(*MI)) 187 Params.pop_back(); 188 189 MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym); 190 WasmSym->setReturns(std::move(Returns)); 191 WasmSym->setParams(std::move(Params)); 192 WasmSym->setIsFunction(true); 193 194 const MCExpr *Expr = 195 MCSymbolRefExpr::create(WasmSym, 196 MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, 197 Ctx); 198 MCOp = MCOperand::createExpr(Expr); 199 break; 200 } 201 } 202 } 203 MCOp = MCOperand::createImm(MO.getImm()); 204 break; 205 case MachineOperand::MO_FPImmediate: { 206 // TODO: MC converts all floating point immediate operands to double. 207 // This is fine for numeric values, but may cause NaNs to change bits. 208 const ConstantFP *Imm = MO.getFPImm(); 209 if (Imm->getType()->isFloatTy()) 210 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat()); 211 else if (Imm->getType()->isDoubleTy()) 212 MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble()); 213 else 214 llvm_unreachable("unknown floating point immediate type"); 215 break; 216 } 217 case MachineOperand::MO_GlobalAddress: 218 assert(MO.getTargetFlags() == 0 && 219 "WebAssembly does not use target flags on GlobalAddresses"); 220 MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), 221 MO.getGlobal()->getValueType()->isFunctionTy()); 222 break; 223 case MachineOperand::MO_ExternalSymbol: 224 // The target flag indicates whether this is a symbol for a 225 // variable or a function. 226 assert((MO.getTargetFlags() & -2) == 0 && 227 "WebAssembly uses only one target flag bit on ExternalSymbols"); 228 MCOp = LowerSymbolOperand(GetExternalSymbolSymbol(MO), /*Offset=*/0, 229 MO.getTargetFlags() & 1); 230 break; 231 } 232 233 OutMI.addOperand(MCOp); 234 } 235} 236