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 | |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 33 | // This disables the removal of registers when lowering into MC, as required |
| 34 | // by some current tests. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 35 | static cl::opt<bool> |
| 36 | WasmKeepRegisters("wasm-keep-registers", cl::Hidden, |
| 37 | cl::desc("WebAssembly: output stack registers in" |
| 38 | " instruction output for test purposes only."), |
| 39 | cl::init(false)); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 40 | |
| 41 | static unsigned regInstructionToStackInstruction(unsigned OpCode); |
| 42 | static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI); |
| 43 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 44 | MCSymbol * |
| 45 | WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 46 | const GlobalValue *Global = MO.getGlobal(); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 47 | MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 48 | |
| 49 | if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) { |
| 50 | const MachineFunction &MF = *MO.getParent()->getParent()->getParent(); |
| 51 | const TargetMachine &TM = MF.getTarget(); |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 52 | const Function &CurrentFunc = MF.getFunction(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 53 | |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 54 | SmallVector<wasm::ValType, 4> Returns; |
| 55 | SmallVector<wasm::ValType, 4> Params; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 56 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 57 | wasm::ValType iPTR = MF.getSubtarget<WebAssemblySubtarget>().hasAddr64() |
| 58 | ? wasm::ValType::I64 |
| 59 | : wasm::ValType::I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 60 | |
| 61 | SmallVector<MVT, 4> ResultMVTs; |
| 62 | ComputeLegalValueVTs(CurrentFunc, TM, FuncTy->getReturnType(), ResultMVTs); |
| 63 | // WebAssembly can't currently handle returning tuples. |
| 64 | if (ResultMVTs.size() <= 1) |
| 65 | for (MVT ResultMVT : ResultMVTs) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 66 | Returns.push_back(WebAssembly::toValType(ResultMVT)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 67 | else |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 68 | Params.push_back(iPTR); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 69 | |
| 70 | for (Type *Ty : FuncTy->params()) { |
| 71 | SmallVector<MVT, 4> ParamMVTs; |
| 72 | ComputeLegalValueVTs(CurrentFunc, TM, Ty, ParamMVTs); |
| 73 | for (MVT ParamMVT : ParamMVTs) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 74 | Params.push_back(WebAssembly::toValType(ParamMVT)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (FuncTy->isVarArg()) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 78 | Params.push_back(iPTR); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 79 | |
| 80 | WasmSym->setReturns(std::move(Returns)); |
| 81 | WasmSym->setParams(std::move(Params)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 82 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return WasmSym; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 88 | MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol( |
| 89 | const MachineOperand &MO) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 90 | const char *Name = MO.getSymbolName(); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 91 | MCSymbolWasm *WasmSym = |
| 92 | cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 93 | const WebAssemblySubtarget &Subtarget = Printer.getSubtarget(); |
| 94 | |
| 95 | // __stack_pointer is a global variable; all other external symbols used by |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 96 | // CodeGen are functions. It's OK to hardcode knowledge of specific symbols |
| 97 | // here; this method is precisely there for fetching the signatures of known |
| 98 | // Clang-provided symbols. |
| 99 | if (strcmp(Name, "__stack_pointer") == 0) { |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 100 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 101 | WasmSym->setGlobalType(wasm::WasmGlobalType{ |
Sam Clegg | 503fdea | 2018-03-01 18:48:08 +0000 | [diff] [blame] | 102 | uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64 |
| 103 | : wasm::WASM_TYPE_I32), |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 104 | true}); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 105 | return WasmSym; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 106 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 107 | |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 108 | SmallVector<wasm::ValType, 4> Returns; |
| 109 | SmallVector<wasm::ValType, 4> Params; |
Derek Schuff | 70ce1af | 2018-09-27 22:20:33 +0000 | [diff] [blame^] | 110 | GetLibcallSignature(Subtarget, Name, Returns, Params); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 111 | |
| 112 | WasmSym->setReturns(std::move(Returns)); |
| 113 | WasmSym->setParams(std::move(Params)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 114 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 115 | |
| 116 | return WasmSym; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 119 | MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym, |
| 120 | int64_t Offset, |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 121 | bool IsFunc, |
| 122 | bool IsGlob) const { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 123 | MCSymbolRefExpr::VariantKind VK = |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 124 | IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION |
| 125 | : IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL |
| 126 | : MCSymbolRefExpr::VK_None; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 127 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 128 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 129 | |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 130 | if (Offset != 0) { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 131 | if (IsFunc) |
| 132 | report_fatal_error("Function addresses with offsets not supported"); |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 133 | if (IsGlob) |
| 134 | report_fatal_error("Global indexes with offsets not supported"); |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 135 | Expr = |
| 136 | MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx); |
| 137 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 138 | |
| 139 | return MCOperand::createExpr(Expr); |
| 140 | } |
| 141 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 142 | // Return the WebAssembly type associated with the given register class. |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 143 | static wasm::ValType getType(const TargetRegisterClass *RC) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 144 | if (RC == &WebAssembly::I32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 145 | return wasm::ValType::I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 146 | if (RC == &WebAssembly::I64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 147 | return wasm::ValType::I64; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 148 | if (RC == &WebAssembly::F32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 149 | return wasm::ValType::F32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 150 | if (RC == &WebAssembly::F64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 151 | return wasm::ValType::F64; |
Thomas Lively | 6f21a13 | 2018-09-20 22:04:44 +0000 | [diff] [blame] | 152 | if (RC == &WebAssembly::V128RegClass) |
| 153 | return wasm::ValType::V128; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 154 | llvm_unreachable("Unexpected register class"); |
| 155 | } |
| 156 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 157 | void WebAssemblyMCInstLower::Lower(const MachineInstr *MI, |
| 158 | MCInst &OutMI) const { |
| 159 | OutMI.setOpcode(MI->getOpcode()); |
| 160 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 161 | const MCInstrDesc &Desc = MI->getDesc(); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 162 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 163 | const MachineOperand &MO = MI->getOperand(i); |
| 164 | |
| 165 | MCOperand MCOp; |
| 166 | switch (MO.getType()) { |
| 167 | default: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 168 | MI->print(errs()); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 169 | llvm_unreachable("unknown operand type"); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 170 | case MachineOperand::MO_MachineBasicBlock: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 171 | MI->print(errs()); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 172 | llvm_unreachable("MachineBasicBlock operand should have been rewritten"); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 173 | case MachineOperand::MO_Register: { |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 174 | // Ignore all implicit register operands. |
| 175 | if (MO.isImplicit()) |
| 176 | continue; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 177 | const WebAssemblyFunctionInfo &MFI = |
| 178 | *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 179 | unsigned WAReg = MFI.getWAReg(MO.getReg()); |
| 180 | MCOp = MCOperand::createReg(WAReg); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 181 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 182 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 183 | case MachineOperand::MO_Immediate: |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 184 | if (i < Desc.NumOperands) { |
| 185 | const MCOperandInfo &Info = Desc.OpInfo[i]; |
| 186 | if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) { |
| 187 | MCSymbol *Sym = Printer.createTempSymbol("typeindex"); |
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 | SmallVector<wasm::ValType, 4> Returns; |
| 190 | SmallVector<wasm::ValType, 4> Params; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 191 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 192 | const MachineRegisterInfo &MRI = |
| 193 | MI->getParent()->getParent()->getRegInfo(); |
| 194 | for (const MachineOperand &MO : MI->defs()) |
| 195 | Returns.push_back(getType(MRI.getRegClass(MO.getReg()))); |
| 196 | for (const MachineOperand &MO : MI->explicit_uses()) |
| 197 | if (MO.isReg()) |
| 198 | Params.push_back(getType(MRI.getRegClass(MO.getReg()))); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 199 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 200 | // call_indirect instructions have a callee operand at the end which |
| 201 | // doesn't count as a param. |
| 202 | if (WebAssembly::isCallIndirect(*MI)) |
| 203 | Params.pop_back(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 204 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 205 | MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym); |
| 206 | WasmSym->setReturns(std::move(Returns)); |
| 207 | WasmSym->setParams(std::move(Params)); |
| 208 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 209 | |
| 210 | const MCExpr *Expr = MCSymbolRefExpr::create( |
| 211 | WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx); |
| 212 | MCOp = MCOperand::createExpr(Expr); |
| 213 | break; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 216 | MCOp = MCOperand::createImm(MO.getImm()); |
| 217 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 218 | case MachineOperand::MO_FPImmediate: { |
| 219 | // TODO: MC converts all floating point immediate operands to double. |
| 220 | // This is fine for numeric values, but may cause NaNs to change bits. |
| 221 | const ConstantFP *Imm = MO.getFPImm(); |
| 222 | if (Imm->getType()->isFloatTy()) |
| 223 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat()); |
| 224 | else if (Imm->getType()->isDoubleTy()) |
| 225 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble()); |
| 226 | else |
| 227 | llvm_unreachable("unknown floating point immediate type"); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 228 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 229 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 230 | case MachineOperand::MO_GlobalAddress: |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 231 | assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG && |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 232 | "WebAssembly does not use target flags on GlobalAddresses"); |
| 233 | MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 234 | MO.getGlobal()->getValueType()->isFunctionTy(), |
| 235 | false); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 236 | break; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 237 | case MachineOperand::MO_ExternalSymbol: |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 238 | // The target flag indicates whether this is a symbol for a |
| 239 | // variable or a function. |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 240 | assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 && |
| 241 | "WebAssembly uses only symbol flags on ExternalSymbols"); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 242 | MCOp = LowerSymbolOperand( |
| 243 | GetExternalSymbolSymbol(MO), /*Offset=*/0, |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 244 | (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0, |
| 245 | (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0); |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 246 | break; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | OutMI.addOperand(MCOp); |
| 250 | } |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 251 | |
| 252 | if (!WasmKeepRegisters) |
| 253 | removeRegisterOperands(MI, OutMI); |
| 254 | } |
| 255 | |
| 256 | static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) { |
| 257 | // Remove all uses of stackified registers to bring the instruction format |
| 258 | // into its final stack form used thruout MC, and transition opcodes to |
| 259 | // their _S variant. |
| 260 | // We do this seperate from the above code that still may need these |
| 261 | // registers for e.g. call_indirect signatures. |
| 262 | // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for |
| 263 | // details. |
| 264 | // TODO: the code above creates new registers which are then removed here. |
| 265 | // That code could be slightly simplified by not doing that, though maybe |
| 266 | // it is simpler conceptually to keep the code above in "register mode" |
| 267 | // until this transition point. |
| 268 | // FIXME: we are not processing inline assembly, which contains register |
| 269 | // operands, because it is used by later target generic code. |
| 270 | if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm()) |
| 271 | return; |
| 272 | |
| 273 | // Transform to _S instruction. |
| 274 | auto RegOpcode = OutMI.getOpcode(); |
| 275 | auto StackOpcode = regInstructionToStackInstruction(RegOpcode); |
| 276 | OutMI.setOpcode(StackOpcode); |
| 277 | |
| 278 | // Remove register operands. |
| 279 | for (auto I = OutMI.getNumOperands(); I; --I) { |
| 280 | auto &MO = OutMI.getOperand(I - 1); |
| 281 | if (MO.isReg()) { |
| 282 | OutMI.erase(&MO); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | static unsigned regInstructionToStackInstruction(unsigned OpCode) { |
Thomas Lively | 211874d | 2018-08-27 22:02:09 +0000 | [diff] [blame] | 288 | // For most opcodes, this function could have been implemented as "return |
| 289 | // OpCode + 1", but since table-gen alphabetically sorts them, this cannot be |
Thomas Lively | adb6da1 | 2018-08-28 18:49:47 +0000 | [diff] [blame] | 290 | // guaranteed (see e.g. BR and BR_IF). Instead we use a giant switch statement |
| 291 | // generated by a custom TableGen backend (WebAssemblyStackifierEmitter.cpp) |
| 292 | // that emits switch cases of the form |
| 293 | // |
| 294 | // case WebAssembly::RegisterInstr: return WebAssembly::StackInstr; |
| 295 | // |
| 296 | // for every pair of equivalent register and stack instructions. |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 297 | switch (OpCode) { |
| 298 | default: |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 299 | llvm_unreachable( |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 300 | "unknown WebAssembly instruction in WebAssemblyMCInstLower pass"); |
Thomas Lively | 211874d | 2018-08-27 22:02:09 +0000 | [diff] [blame] | 301 | #include "WebAssemblyGenStackifier.inc" |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 302 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 303 | } |