| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 1 | // 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 Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 17 | #include "WebAssemblyAsmPrinter.h" | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 18 | #include "WebAssemblyMachineFunctionInfo.h" | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 19 | #include "WebAssemblyRuntimeLibcallSignatures.h" | 
|  | 20 | #include "WebAssemblyUtilities.h" | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/AsmPrinter.h" | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunction.h" | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCAsmInfo.h" | 
|  | 25 | #include "llvm/MC/MCContext.h" | 
|  | 26 | #include "llvm/MC/MCExpr.h" | 
|  | 27 | #include "llvm/MC/MCInst.h" | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSymbolELF.h" | 
|  | 29 | #include "llvm/MC/MCSymbolWasm.h" | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" | 
|  | 31 | #include "llvm/Support/raw_ostream.h" | 
|  | 32 | using namespace llvm; | 
|  | 33 |  | 
|  | 34 | MCSymbol * | 
|  | 35 | WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 36 | 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 |  | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 48 | SmallVector<wasm::ValType, 4> Returns; | 
|  | 49 | SmallVector<wasm::ValType, 4> Params; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 50 |  | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 51 | wasm::ValType iPTR = | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 52 | MF.getSubtarget<WebAssemblySubtarget>().hasAddr64() ? | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 53 | wasm::ValType::I64 : | 
|  | 54 | wasm::ValType::I32; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 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) | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 61 | Returns.push_back(WebAssembly::toValType(ResultMVT)); | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 62 | else | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 63 | Params.push_back(iPTR); | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 64 |  | 
|  | 65 | for (Type *Ty : FuncTy->params()) { | 
|  | 66 | SmallVector<MVT, 4> ParamMVTs; | 
|  | 67 | ComputeLegalValueVTs(CurrentFunc, TM, Ty, ParamMVTs); | 
|  | 68 | for (MVT ParamMVT : ParamMVTs) | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 69 | Params.push_back(WebAssembly::toValType(ParamMVT)); | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
|  | 72 | if (FuncTy->isVarArg()) | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 73 | Params.push_back(iPTR); | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 74 |  | 
|  | 75 | WasmSym->setReturns(std::move(Returns)); | 
|  | 76 | WasmSym->setParams(std::move(Params)); | 
|  | 77 | WasmSym->setIsFunction(true); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | return WasmSym; | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 83 | MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol( | 
|  | 84 | const MachineOperand &MO) const { | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 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 |  | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 98 | SmallVector<wasm::ValType, 4> Returns; | 
|  | 99 | SmallVector<wasm::ValType, 4> Params; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 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 Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 109 | MCOperand 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 Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 115 |  | 
| Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 116 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx); | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 117 |  | 
| Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 118 | if (Offset != 0) { | 
| Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 119 | if (IsFunc) | 
|  | 120 | report_fatal_error("Function addresses with offsets not supported"); | 
| Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 121 | Expr = | 
|  | 122 | MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx); | 
|  | 123 | } | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 124 |  | 
|  | 125 | return MCOperand::createExpr(Expr); | 
|  | 126 | } | 
|  | 127 |  | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 128 | // Return the WebAssembly type associated with the given register class. | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 129 | static wasm::ValType getType(const TargetRegisterClass *RC) { | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 130 | if (RC == &WebAssembly::I32RegClass) | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 131 | return wasm::ValType::I32; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 132 | if (RC == &WebAssembly::I64RegClass) | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 133 | return wasm::ValType::I64; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 134 | if (RC == &WebAssembly::F32RegClass) | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 135 | return wasm::ValType::F32; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 136 | if (RC == &WebAssembly::F64RegClass) | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 137 | return wasm::ValType::F64; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 138 | llvm_unreachable("Unexpected register class"); | 
|  | 139 | } | 
|  | 140 |  | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 141 | void WebAssemblyMCInstLower::Lower(const MachineInstr *MI, | 
|  | 142 | MCInst &OutMI) const { | 
|  | 143 | OutMI.setOpcode(MI->getOpcode()); | 
|  | 144 |  | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 145 | const MCInstrDesc &Desc = MI->getDesc(); | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 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: | 
| Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 152 | MI->print(errs()); | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 153 | llvm_unreachable("unknown operand type"); | 
| Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 154 | case MachineOperand::MO_MachineBasicBlock: | 
| Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 155 | MI->print(errs()); | 
| Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 156 | llvm_unreachable("MachineBasicBlock operand should have been rewritten"); | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 157 | case MachineOperand::MO_Register: { | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 158 | // Ignore all implicit register operands. | 
|  | 159 | if (MO.isImplicit()) | 
|  | 160 | continue; | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 161 | const WebAssemblyFunctionInfo &MFI = | 
|  | 162 | *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); | 
|  | 163 | unsigned WAReg = MFI.getWAReg(MO.getReg()); | 
|  | 164 | MCOp = MCOperand::createReg(WAReg); | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 165 | break; | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 166 | } | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 167 | case MachineOperand::MO_Immediate: | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 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)) { | 
| Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 173 | SmallVector<wasm::ValType, 4> Returns; | 
|  | 174 | SmallVector<wasm::ValType, 4> Params; | 
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 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 | } | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 203 | MCOp = MCOperand::createImm(MO.getImm()); | 
|  | 204 | break; | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 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"); | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 215 | break; | 
| Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 216 | } | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 217 | case MachineOperand::MO_GlobalAddress: | 
| Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 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()); | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 222 | break; | 
| Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 223 | case MachineOperand::MO_ExternalSymbol: | 
| Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 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); | 
| Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 230 | break; | 
| Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
|  | 233 | OutMI.addOperand(MCOp); | 
|  | 234 | } | 
|  | 235 | } |