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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file contains code to lower WebAssembly MachineInstrs to their |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 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/MCSymbolWasm.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | using namespace llvm; |
| 32 | |
Thomas Lively | c63b5fc | 2018-10-22 21:55:26 +0000 | [diff] [blame^] | 33 | // Defines llvm::WebAssembly::getStackOpcode to convert register instructions to |
| 34 | // stack instructions |
| 35 | #define GET_INSTRMAP_INFO 1 |
| 36 | #include "WebAssemblyGenInstrInfo.inc" |
| 37 | |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 38 | // This disables the removal of registers when lowering into MC, as required |
| 39 | // by some current tests. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 40 | static cl::opt<bool> |
| 41 | WasmKeepRegisters("wasm-keep-registers", cl::Hidden, |
| 42 | cl::desc("WebAssembly: output stack registers in" |
| 43 | " instruction output for test purposes only."), |
| 44 | cl::init(false)); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 45 | |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 46 | static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI); |
| 47 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 48 | MCSymbol * |
| 49 | WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 50 | const GlobalValue *Global = MO.getGlobal(); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 51 | MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 52 | |
| 53 | if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) { |
| 54 | const MachineFunction &MF = *MO.getParent()->getParent()->getParent(); |
| 55 | const TargetMachine &TM = MF.getTarget(); |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 56 | const Function &CurrentFunc = MF.getFunction(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 57 | |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 58 | SmallVector<MVT, 1> ResultMVTs; |
| 59 | SmallVector<MVT, 4> ParamMVTs; |
| 60 | ComputeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 61 | |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 62 | auto Signature = SignatureFromMVTs(ResultMVTs, ParamMVTs); |
| 63 | WasmSym->setSignature(Signature.get()); |
| 64 | Printer.addSignature(std::move(Signature)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 65 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return WasmSym; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 71 | MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol( |
| 72 | const MachineOperand &MO) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 73 | const char *Name = MO.getSymbolName(); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 74 | MCSymbolWasm *WasmSym = |
| 75 | cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 76 | const WebAssemblySubtarget &Subtarget = Printer.getSubtarget(); |
| 77 | |
| 78 | // __stack_pointer is a global variable; all other external symbols used by |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 79 | // CodeGen are functions. It's OK to hardcode knowledge of specific symbols |
| 80 | // here; this method is precisely there for fetching the signatures of known |
| 81 | // Clang-provided symbols. |
| 82 | if (strcmp(Name, "__stack_pointer") == 0) { |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 83 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 84 | WasmSym->setGlobalType(wasm::WasmGlobalType{ |
Sam Clegg | 503fdea | 2018-03-01 18:48:08 +0000 | [diff] [blame] | 85 | uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64 |
| 86 | : wasm::WASM_TYPE_I32), |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 87 | true}); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 88 | return WasmSym; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 89 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 90 | |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 91 | SmallVector<wasm::ValType, 4> Returns; |
| 92 | SmallVector<wasm::ValType, 4> Params; |
Derek Schuff | 70ce1af | 2018-09-27 22:20:33 +0000 | [diff] [blame] | 93 | GetLibcallSignature(Subtarget, Name, Returns, Params); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 94 | auto Signature = |
| 95 | make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params)); |
| 96 | WasmSym->setSignature(Signature.get()); |
| 97 | Printer.addSignature(std::move(Signature)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 98 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 99 | |
| 100 | return WasmSym; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 103 | MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym, |
| 104 | int64_t Offset, |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 105 | bool IsFunc, |
| 106 | bool IsGlob) const { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 107 | MCSymbolRefExpr::VariantKind VK = |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 108 | IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION |
| 109 | : IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL |
| 110 | : MCSymbolRefExpr::VK_None; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 111 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 112 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 113 | |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 114 | if (Offset != 0) { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 115 | if (IsFunc) |
| 116 | report_fatal_error("Function addresses with offsets not supported"); |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 117 | if (IsGlob) |
| 118 | report_fatal_error("Global indexes with offsets not supported"); |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 119 | Expr = |
| 120 | MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx); |
| 121 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 122 | |
| 123 | return MCOperand::createExpr(Expr); |
| 124 | } |
| 125 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 126 | // Return the WebAssembly type associated with the given register class. |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 127 | static wasm::ValType getType(const TargetRegisterClass *RC) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 128 | if (RC == &WebAssembly::I32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 129 | return wasm::ValType::I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 130 | if (RC == &WebAssembly::I64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 131 | return wasm::ValType::I64; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 132 | if (RC == &WebAssembly::F32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 133 | return wasm::ValType::F32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 134 | if (RC == &WebAssembly::F64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 135 | return wasm::ValType::F64; |
Thomas Lively | 6f21a13 | 2018-09-20 22:04:44 +0000 | [diff] [blame] | 136 | if (RC == &WebAssembly::V128RegClass) |
| 137 | return wasm::ValType::V128; |
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"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 172 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +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 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 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()))); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 183 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 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(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 188 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 189 | MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 190 | auto Signature = make_unique<wasm::WasmSignature>(std::move(Returns), |
| 191 | std::move(Params)); |
| 192 | WasmSym->setSignature(Signature.get()); |
| 193 | Printer.addSignature(std::move(Signature)); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 194 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 195 | |
| 196 | const MCExpr *Expr = MCSymbolRefExpr::create( |
| 197 | WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx); |
| 198 | MCOp = MCOperand::createExpr(Expr); |
| 199 | break; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 202 | MCOp = MCOperand::createImm(MO.getImm()); |
| 203 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 204 | case MachineOperand::MO_FPImmediate: { |
| 205 | // TODO: MC converts all floating point immediate operands to double. |
| 206 | // This is fine for numeric values, but may cause NaNs to change bits. |
| 207 | const ConstantFP *Imm = MO.getFPImm(); |
| 208 | if (Imm->getType()->isFloatTy()) |
| 209 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat()); |
| 210 | else if (Imm->getType()->isDoubleTy()) |
| 211 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble()); |
| 212 | else |
| 213 | llvm_unreachable("unknown floating point immediate type"); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 214 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 215 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 216 | case MachineOperand::MO_GlobalAddress: |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 217 | assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG && |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 218 | "WebAssembly does not use target flags on GlobalAddresses"); |
| 219 | MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 220 | MO.getGlobal()->getValueType()->isFunctionTy(), |
| 221 | false); |
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. |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 226 | assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 && |
| 227 | "WebAssembly uses only symbol flags on ExternalSymbols"); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 228 | MCOp = LowerSymbolOperand( |
| 229 | GetExternalSymbolSymbol(MO), /*Offset=*/0, |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 230 | (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0, |
| 231 | (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0); |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 232 | break; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | OutMI.addOperand(MCOp); |
| 236 | } |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 237 | |
| 238 | if (!WasmKeepRegisters) |
| 239 | removeRegisterOperands(MI, OutMI); |
| 240 | } |
| 241 | |
| 242 | static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) { |
| 243 | // Remove all uses of stackified registers to bring the instruction format |
| 244 | // into its final stack form used thruout MC, and transition opcodes to |
| 245 | // their _S variant. |
| 246 | // We do this seperate from the above code that still may need these |
| 247 | // registers for e.g. call_indirect signatures. |
| 248 | // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for |
| 249 | // details. |
| 250 | // TODO: the code above creates new registers which are then removed here. |
| 251 | // That code could be slightly simplified by not doing that, though maybe |
| 252 | // it is simpler conceptually to keep the code above in "register mode" |
| 253 | // until this transition point. |
| 254 | // FIXME: we are not processing inline assembly, which contains register |
| 255 | // operands, because it is used by later target generic code. |
| 256 | if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm()) |
| 257 | return; |
| 258 | |
| 259 | // Transform to _S instruction. |
| 260 | auto RegOpcode = OutMI.getOpcode(); |
Thomas Lively | c63b5fc | 2018-10-22 21:55:26 +0000 | [diff] [blame^] | 261 | auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode); |
| 262 | assert(StackOpcode != -1 && "Failed to stackify instruction"); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 263 | OutMI.setOpcode(StackOpcode); |
| 264 | |
| 265 | // Remove register operands. |
| 266 | for (auto I = OutMI.getNumOperands(); I; --I) { |
| 267 | auto &MO = OutMI.getOperand(I - 1); |
| 268 | if (MO.isReg()) { |
| 269 | OutMI.erase(&MO); |
| 270 | } |
| 271 | } |
| 272 | } |