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(); |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 46 | const Function &CurrentFunc = MF.getFunction(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 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)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame^] | 77 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 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 |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame^] | 94 | // CodeGen are functions. It's OK to hardcode knowledge of specific symbols |
| 95 | // here; this method is precisely there for fetching the signatures of known |
| 96 | // Clang-provided symbols. |
| 97 | if (strcmp(Name, "__stack_pointer") == 0) { |
| 98 | wasm::ValType iPTR = |
| 99 | Subtarget.hasAddr64() ? wasm::ValType::I64 : wasm::ValType::I32; |
| 100 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
| 101 | WasmSym->setGlobalType(wasm::WasmGlobalType{int32_t(iPTR), true}); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 102 | return WasmSym; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame^] | 103 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 104 | |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 105 | SmallVector<wasm::ValType, 4> Returns; |
| 106 | SmallVector<wasm::ValType, 4> Params; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 107 | GetSignature(Subtarget, Name, Returns, Params); |
| 108 | |
| 109 | WasmSym->setReturns(std::move(Returns)); |
| 110 | WasmSym->setParams(std::move(Params)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame^] | 111 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 112 | |
| 113 | return WasmSym; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 116 | MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym, |
| 117 | int64_t Offset, |
| 118 | bool IsFunc) const { |
| 119 | MCSymbolRefExpr::VariantKind VK = |
| 120 | IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION |
| 121 | : MCSymbolRefExpr::VK_None; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 122 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 123 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 124 | |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 125 | if (Offset != 0) { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 126 | if (IsFunc) |
| 127 | report_fatal_error("Function addresses with offsets not supported"); |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 128 | Expr = |
| 129 | MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx); |
| 130 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 131 | |
| 132 | return MCOperand::createExpr(Expr); |
| 133 | } |
| 134 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 135 | // Return the WebAssembly type associated with the given register class. |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 136 | static wasm::ValType getType(const TargetRegisterClass *RC) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 137 | if (RC == &WebAssembly::I32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 138 | return wasm::ValType::I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 139 | if (RC == &WebAssembly::I64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 140 | return wasm::ValType::I64; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 141 | if (RC == &WebAssembly::F32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 142 | return wasm::ValType::F32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 143 | if (RC == &WebAssembly::F64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 144 | return wasm::ValType::F64; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 145 | llvm_unreachable("Unexpected register class"); |
| 146 | } |
| 147 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 148 | void WebAssemblyMCInstLower::Lower(const MachineInstr *MI, |
| 149 | MCInst &OutMI) const { |
| 150 | OutMI.setOpcode(MI->getOpcode()); |
| 151 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 152 | const MCInstrDesc &Desc = MI->getDesc(); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 153 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 154 | const MachineOperand &MO = MI->getOperand(i); |
| 155 | |
| 156 | MCOperand MCOp; |
| 157 | switch (MO.getType()) { |
| 158 | default: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 159 | MI->print(errs()); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 160 | llvm_unreachable("unknown operand type"); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 161 | case MachineOperand::MO_MachineBasicBlock: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 162 | MI->print(errs()); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 163 | llvm_unreachable("MachineBasicBlock operand should have been rewritten"); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 164 | case MachineOperand::MO_Register: { |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 165 | // Ignore all implicit register operands. |
| 166 | if (MO.isImplicit()) |
| 167 | continue; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 168 | const WebAssemblyFunctionInfo &MFI = |
| 169 | *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 170 | unsigned WAReg = MFI.getWAReg(MO.getReg()); |
| 171 | MCOp = MCOperand::createReg(WAReg); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 172 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 173 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 174 | case MachineOperand::MO_Immediate: |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 175 | if (i < Desc.NumOperands) { |
| 176 | const MCOperandInfo &Info = Desc.OpInfo[i]; |
| 177 | if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) { |
| 178 | MCSymbol *Sym = Printer.createTempSymbol("typeindex"); |
| 179 | if (!isa<MCSymbolELF>(Sym)) { |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 180 | SmallVector<wasm::ValType, 4> Returns; |
| 181 | SmallVector<wasm::ValType, 4> Params; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 182 | |
| 183 | const MachineRegisterInfo &MRI = |
| 184 | MI->getParent()->getParent()->getRegInfo(); |
| 185 | for (const MachineOperand &MO : MI->defs()) |
| 186 | Returns.push_back(getType(MRI.getRegClass(MO.getReg()))); |
| 187 | for (const MachineOperand &MO : MI->explicit_uses()) |
| 188 | if (MO.isReg()) |
| 189 | Params.push_back(getType(MRI.getRegClass(MO.getReg()))); |
| 190 | |
| 191 | // call_indirect instructions have a callee operand at the end which |
| 192 | // doesn't count as a param. |
| 193 | if (WebAssembly::isCallIndirect(*MI)) |
| 194 | Params.pop_back(); |
| 195 | |
| 196 | MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym); |
| 197 | WasmSym->setReturns(std::move(Returns)); |
| 198 | WasmSym->setParams(std::move(Params)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame^] | 199 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 200 | |
| 201 | const MCExpr *Expr = |
| 202 | MCSymbolRefExpr::create(WasmSym, |
| 203 | MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, |
| 204 | Ctx); |
| 205 | MCOp = MCOperand::createExpr(Expr); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 210 | MCOp = MCOperand::createImm(MO.getImm()); |
| 211 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 212 | case MachineOperand::MO_FPImmediate: { |
| 213 | // TODO: MC converts all floating point immediate operands to double. |
| 214 | // This is fine for numeric values, but may cause NaNs to change bits. |
| 215 | const ConstantFP *Imm = MO.getFPImm(); |
| 216 | if (Imm->getType()->isFloatTy()) |
| 217 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat()); |
| 218 | else if (Imm->getType()->isDoubleTy()) |
| 219 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble()); |
| 220 | else |
| 221 | llvm_unreachable("unknown floating point immediate type"); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 222 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 223 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 224 | case MachineOperand::MO_GlobalAddress: |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 225 | assert(MO.getTargetFlags() == 0 && |
| 226 | "WebAssembly does not use target flags on GlobalAddresses"); |
| 227 | MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), |
| 228 | MO.getGlobal()->getValueType()->isFunctionTy()); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 229 | break; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 230 | case MachineOperand::MO_ExternalSymbol: |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 231 | // The target flag indicates whether this is a symbol for a |
| 232 | // variable or a function. |
| 233 | assert((MO.getTargetFlags() & -2) == 0 && |
| 234 | "WebAssembly uses only one target flag bit on ExternalSymbols"); |
| 235 | MCOp = LowerSymbolOperand(GetExternalSymbolSymbol(MO), /*Offset=*/0, |
| 236 | MO.getTargetFlags() & 1); |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 237 | break; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | OutMI.addOperand(MCOp); |
| 241 | } |
| 242 | } |