Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 1 | // WebAssemblyMCInstLower.cpp - Convert WebAssembly MachineInstr to an MCInst // |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file contains code to lower WebAssembly MachineInstrs to their |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 11 | /// corresponding MCInst records. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "WebAssemblyMCInstLower.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 16 | #include "WebAssemblyAsmPrinter.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 17 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 18 | #include "WebAssemblyRuntimeLibcallSignatures.h" |
Wouter van Oortmerssen | d8ddf83 | 2019-07-12 22:08:25 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/AsmPrinter.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.h" |
| 24 | #include "llvm/MC/MCContext.h" |
| 25 | #include "llvm/MC/MCExpr.h" |
| 26 | #include "llvm/MC/MCInst.h" |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCSymbolWasm.h" |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | using namespace llvm; |
| 31 | |
Thomas Lively | c63b5fc | 2018-10-22 21:55:26 +0000 | [diff] [blame] | 32 | // Defines llvm::WebAssembly::getStackOpcode to convert register instructions to |
| 33 | // stack instructions |
| 34 | #define GET_INSTRMAP_INFO 1 |
| 35 | #include "WebAssemblyGenInstrInfo.inc" |
| 36 | |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 37 | // This disables the removal of registers when lowering into MC, as required |
| 38 | // by some current tests. |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 39 | cl::opt<bool> |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 40 | WasmKeepRegisters("wasm-keep-registers", cl::Hidden, |
| 41 | cl::desc("WebAssembly: output stack registers in" |
| 42 | " instruction output for test purposes only."), |
| 43 | cl::init(false)); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 44 | |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 45 | static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI); |
| 46 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 47 | MCSymbol * |
| 48 | WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 49 | const GlobalValue *Global = MO.getGlobal(); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 50 | auto *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 51 | |
| 52 | if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) { |
| 53 | const MachineFunction &MF = *MO.getParent()->getParent()->getParent(); |
| 54 | const TargetMachine &TM = MF.getTarget(); |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 55 | const Function &CurrentFunc = MF.getFunction(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 56 | |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 57 | SmallVector<MVT, 1> ResultMVTs; |
| 58 | SmallVector<MVT, 4> ParamMVTs; |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 59 | computeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 60 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 61 | auto Signature = signatureFromMVTs(ResultMVTs, ParamMVTs); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 62 | WasmSym->setSignature(Signature.get()); |
| 63 | Printer.addSignature(std::move(Signature)); |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 64 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return WasmSym; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 70 | MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol( |
| 71 | const MachineOperand &MO) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 72 | const char *Name = MO.getSymbolName(); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 73 | auto *WasmSym = cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 74 | const WebAssemblySubtarget &Subtarget = Printer.getSubtarget(); |
| 75 | |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 76 | // Except for certain known symbols, all symbols used by CodeGen are |
| 77 | // functions. It's OK to hardcode knowledge of specific symbols here; this |
| 78 | // method is precisely there for fetching the signatures of known |
| 79 | // Clang-provided symbols. |
| 80 | if (strcmp(Name, "__stack_pointer") == 0 || |
| 81 | strcmp(Name, "__memory_base") == 0 || strcmp(Name, "__table_base") == 0) { |
| 82 | bool Mutable = 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 | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 87 | Mutable}); |
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; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 93 | if (strcmp(Name, "__cpp_exception") == 0) { |
| 94 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT); |
| 95 | // We can't confirm its signature index for now because there can be |
| 96 | // imported exceptions. Set it to be 0 for now. |
| 97 | WasmSym->setEventType( |
| 98 | {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0}); |
| 99 | // We may have multiple C++ compilation units to be linked together, each of |
| 100 | // which defines the exception symbol. To resolve them, we declare them as |
| 101 | // weak. |
| 102 | WasmSym->setWeak(true); |
| 103 | WasmSym->setExternal(true); |
| 104 | |
| 105 | // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64 |
| 106 | // (for wasm64) param type and void return type. The reaon is, all C++ |
| 107 | // exception values are pointers, and to share the type section with |
| 108 | // functions, exceptions are assumed to have void return type. |
| 109 | Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64 |
| 110 | : wasm::ValType::I32); |
| 111 | } else { // Function symbols |
| 112 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 113 | getLibcallSignature(Subtarget, Name, Returns, Params); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 114 | } |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 115 | auto Signature = |
| 116 | make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params)); |
| 117 | WasmSym->setSignature(Signature.get()); |
| 118 | Printer.addSignature(std::move(Signature)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 119 | |
| 120 | return WasmSym; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 123 | MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO, |
| 124 | MCSymbol *Sym) const { |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 125 | MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; |
| 126 | unsigned TargetFlags = MO.getTargetFlags(); |
| 127 | |
| 128 | switch (TargetFlags) { |
| 129 | case WebAssemblyII::MO_NO_FLAG: |
| 130 | break; |
| 131 | case WebAssemblyII::MO_GOT: |
| 132 | Kind = MCSymbolRefExpr::VK_GOT; |
| 133 | break; |
| 134 | case WebAssemblyII::MO_MEMORY_BASE_REL: |
| 135 | Kind = MCSymbolRefExpr::VK_WASM_MBREL; |
| 136 | break; |
| 137 | case WebAssemblyII::MO_TABLE_BASE_REL: |
| 138 | Kind = MCSymbolRefExpr::VK_WASM_TBREL; |
| 139 | break; |
| 140 | default: |
| 141 | llvm_unreachable("Unknown target flag on GV operand"); |
| 142 | } |
| 143 | |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 144 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 145 | |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 146 | if (MO.getOffset() != 0) { |
| 147 | const auto *WasmSym = cast<MCSymbolWasm>(Sym); |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 148 | if (TargetFlags == WebAssemblyII::MO_GOT) |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 149 | report_fatal_error("GOT symbol references do not support offsets"); |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 150 | if (WasmSym->isFunction()) |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 151 | report_fatal_error("Function addresses with offsets not supported"); |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 152 | if (WasmSym->isGlobal()) |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 153 | report_fatal_error("Global indexes with offsets not supported"); |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 154 | if (WasmSym->isEvent()) |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 155 | report_fatal_error("Event indexes with offsets not supported"); |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 156 | |
| 157 | Expr = MCBinaryExpr::createAdd( |
| 158 | Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx); |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 159 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 160 | |
| 161 | return MCOperand::createExpr(Expr); |
| 162 | } |
| 163 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 164 | // Return the WebAssembly type associated with the given register class. |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 165 | static wasm::ValType getType(const TargetRegisterClass *RC) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 166 | if (RC == &WebAssembly::I32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 167 | return wasm::ValType::I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 168 | if (RC == &WebAssembly::I64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 169 | return wasm::ValType::I64; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 170 | if (RC == &WebAssembly::F32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 171 | return wasm::ValType::F32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 172 | if (RC == &WebAssembly::F64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 173 | return wasm::ValType::F64; |
Thomas Lively | 6f21a13 | 2018-09-20 22:04:44 +0000 | [diff] [blame] | 174 | if (RC == &WebAssembly::V128RegClass) |
| 175 | return wasm::ValType::V128; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 176 | llvm_unreachable("Unexpected register class"); |
| 177 | } |
| 178 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 179 | void WebAssemblyMCInstLower::lower(const MachineInstr *MI, |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 180 | MCInst &OutMI) const { |
| 181 | OutMI.setOpcode(MI->getOpcode()); |
| 182 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 183 | const MCInstrDesc &Desc = MI->getDesc(); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 184 | for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { |
| 185 | const MachineOperand &MO = MI->getOperand(I); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 186 | |
| 187 | MCOperand MCOp; |
| 188 | switch (MO.getType()) { |
| 189 | default: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 190 | MI->print(errs()); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 191 | llvm_unreachable("unknown operand type"); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 192 | case MachineOperand::MO_MachineBasicBlock: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 193 | MI->print(errs()); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 194 | llvm_unreachable("MachineBasicBlock operand should have been rewritten"); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 195 | case MachineOperand::MO_Register: { |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 196 | // Ignore all implicit register operands. |
| 197 | if (MO.isImplicit()) |
| 198 | continue; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 199 | const WebAssemblyFunctionInfo &MFI = |
| 200 | *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 201 | unsigned WAReg = MFI.getWAReg(MO.getReg()); |
| 202 | MCOp = MCOperand::createReg(WAReg); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 203 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 204 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 205 | case MachineOperand::MO_Immediate: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 206 | if (I < Desc.NumOperands) { |
| 207 | const MCOperandInfo &Info = Desc.OpInfo[I]; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 208 | if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) { |
| 209 | MCSymbol *Sym = Printer.createTempSymbol("typeindex"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 210 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 211 | SmallVector<wasm::ValType, 4> Returns; |
| 212 | SmallVector<wasm::ValType, 4> Params; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 213 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 214 | const MachineRegisterInfo &MRI = |
| 215 | MI->getParent()->getParent()->getRegInfo(); |
| 216 | for (const MachineOperand &MO : MI->defs()) |
| 217 | Returns.push_back(getType(MRI.getRegClass(MO.getReg()))); |
| 218 | for (const MachineOperand &MO : MI->explicit_uses()) |
| 219 | if (MO.isReg()) |
| 220 | Params.push_back(getType(MRI.getRegClass(MO.getReg()))); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 221 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 222 | // call_indirect instructions have a callee operand at the end which |
| 223 | // doesn't count as a param. |
Wouter van Oortmerssen | d8ddf83 | 2019-07-12 22:08:25 +0000 | [diff] [blame] | 224 | if (WebAssembly::isCallIndirect(MI->getOpcode())) |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 225 | Params.pop_back(); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 226 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 227 | auto *WasmSym = cast<MCSymbolWasm>(Sym); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 228 | auto Signature = make_unique<wasm::WasmSignature>(std::move(Returns), |
| 229 | std::move(Params)); |
| 230 | WasmSym->setSignature(Signature.get()); |
| 231 | Printer.addSignature(std::move(Signature)); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 232 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 233 | |
| 234 | const MCExpr *Expr = MCSymbolRefExpr::create( |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 235 | WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 236 | MCOp = MCOperand::createExpr(Expr); |
| 237 | break; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 240 | MCOp = MCOperand::createImm(MO.getImm()); |
| 241 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 242 | case MachineOperand::MO_FPImmediate: { |
| 243 | // TODO: MC converts all floating point immediate operands to double. |
| 244 | // This is fine for numeric values, but may cause NaNs to change bits. |
| 245 | const ConstantFP *Imm = MO.getFPImm(); |
| 246 | if (Imm->getType()->isFloatTy()) |
| 247 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat()); |
| 248 | else if (Imm->getType()->isDoubleTy()) |
| 249 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble()); |
| 250 | else |
| 251 | llvm_unreachable("unknown floating point immediate type"); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 252 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 253 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 254 | case MachineOperand::MO_GlobalAddress: |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 255 | MCOp = lowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 256 | break; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 257 | case MachineOperand::MO_ExternalSymbol: |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 258 | // The target flag indicates whether this is a symbol for a |
| 259 | // variable or a function. |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 260 | assert(MO.getTargetFlags() == 0 && |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 261 | "WebAssembly uses only symbol flags on ExternalSymbols"); |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 262 | MCOp = lowerSymbolOperand(MO, GetExternalSymbolSymbol(MO)); |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 263 | break; |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 264 | case MachineOperand::MO_MCSymbol: |
| 265 | // This is currently used only for LSDA symbols (GCC_except_table), |
| 266 | // because global addresses or other external symbols are handled above. |
| 267 | assert(MO.getTargetFlags() == 0 && |
| 268 | "WebAssembly does not use target flags on MCSymbol"); |
Sam Clegg | ef4c66c | 2019-04-03 00:17:29 +0000 | [diff] [blame] | 269 | MCOp = lowerSymbolOperand(MO, MO.getMCSymbol()); |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 270 | break; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | OutMI.addOperand(MCOp); |
| 274 | } |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 275 | |
| 276 | if (!WasmKeepRegisters) |
| 277 | removeRegisterOperands(MI, OutMI); |
| 278 | } |
| 279 | |
| 280 | static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) { |
| 281 | // Remove all uses of stackified registers to bring the instruction format |
| 282 | // into its final stack form used thruout MC, and transition opcodes to |
| 283 | // their _S variant. |
| 284 | // We do this seperate from the above code that still may need these |
| 285 | // registers for e.g. call_indirect signatures. |
| 286 | // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for |
| 287 | // details. |
| 288 | // TODO: the code above creates new registers which are then removed here. |
| 289 | // That code could be slightly simplified by not doing that, though maybe |
| 290 | // it is simpler conceptually to keep the code above in "register mode" |
| 291 | // until this transition point. |
| 292 | // FIXME: we are not processing inline assembly, which contains register |
| 293 | // operands, because it is used by later target generic code. |
| 294 | if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm()) |
| 295 | return; |
| 296 | |
| 297 | // Transform to _S instruction. |
| 298 | auto RegOpcode = OutMI.getOpcode(); |
Thomas Lively | c63b5fc | 2018-10-22 21:55:26 +0000 | [diff] [blame] | 299 | auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode); |
| 300 | assert(StackOpcode != -1 && "Failed to stackify instruction"); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 301 | OutMI.setOpcode(StackOpcode); |
| 302 | |
| 303 | // Remove register operands. |
| 304 | for (auto I = OutMI.getNumOperands(); I; --I) { |
| 305 | auto &MO = OutMI.getOperand(I - 1); |
| 306 | if (MO.isReg()) { |
| 307 | OutMI.erase(&MO); |
| 308 | } |
| 309 | } |
| 310 | } |