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" |
| 19 | #include "WebAssemblyUtilities.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(); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 50 | MCSymbolWasm *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; |
| 59 | ComputeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 60 | |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 61 | auto Signature = SignatureFromMVTs(ResultMVTs, ParamMVTs); |
| 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(); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 73 | MCSymbolWasm *WasmSym = |
| 74 | cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 75 | const WebAssemblySubtarget &Subtarget = Printer.getSubtarget(); |
| 76 | |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 77 | // Except for the two exceptions (__stack_pointer and __cpp_exception), all |
| 78 | // other external symbols used by CodeGen are functions. It's OK to hardcode |
| 79 | // knowledge of specific symbols here; this method is precisely there for |
| 80 | // fetching the signatures of known Clang-provided symbols. |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 81 | if (strcmp(Name, "__stack_pointer") == 0) { |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 82 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 83 | WasmSym->setGlobalType(wasm::WasmGlobalType{ |
Sam Clegg | 503fdea | 2018-03-01 18:48:08 +0000 | [diff] [blame] | 84 | uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64 |
| 85 | : wasm::WASM_TYPE_I32), |
Sam Clegg | 03e101f | 2018-03-01 18:06:21 +0000 | [diff] [blame] | 86 | true}); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 87 | return WasmSym; |
Sam Clegg | 6c899ba | 2018-02-23 05:08:34 +0000 | [diff] [blame] | 88 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 89 | |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 90 | SmallVector<wasm::ValType, 4> Returns; |
| 91 | SmallVector<wasm::ValType, 4> Params; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 92 | if (strcmp(Name, "__cpp_exception") == 0) { |
| 93 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT); |
| 94 | // We can't confirm its signature index for now because there can be |
| 95 | // imported exceptions. Set it to be 0 for now. |
| 96 | WasmSym->setEventType( |
| 97 | {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0}); |
| 98 | // We may have multiple C++ compilation units to be linked together, each of |
| 99 | // which defines the exception symbol. To resolve them, we declare them as |
| 100 | // weak. |
| 101 | WasmSym->setWeak(true); |
| 102 | WasmSym->setExternal(true); |
| 103 | |
| 104 | // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64 |
| 105 | // (for wasm64) param type and void return type. The reaon is, all C++ |
| 106 | // exception values are pointers, and to share the type section with |
| 107 | // functions, exceptions are assumed to have void return type. |
| 108 | Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64 |
| 109 | : wasm::ValType::I32); |
| 110 | } else { // Function symbols |
| 111 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 112 | GetLibcallSignature(Subtarget, Name, Returns, Params); |
| 113 | } |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 114 | auto Signature = |
| 115 | make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params)); |
| 116 | WasmSym->setSignature(Signature.get()); |
| 117 | Printer.addSignature(std::move(Signature)); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 118 | |
| 119 | return WasmSym; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 122 | MCOperand WebAssemblyMCInstLower::LowerSymbolOperand(MCSymbol *Sym, |
| 123 | int64_t Offset, |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 124 | bool IsFunc, bool IsGlob, |
| 125 | bool IsEvent) const { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 126 | MCSymbolRefExpr::VariantKind VK = |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 127 | IsFunc ? MCSymbolRefExpr::VK_WebAssembly_FUNCTION |
| 128 | : IsGlob ? MCSymbolRefExpr::VK_WebAssembly_GLOBAL |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 129 | : IsEvent ? MCSymbolRefExpr::VK_WebAssembly_EVENT |
| 130 | : MCSymbolRefExpr::VK_None; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 131 | |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 132 | const MCExpr *Expr = MCSymbolRefExpr::create(Sym, VK, Ctx); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 133 | |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 134 | if (Offset != 0) { |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 135 | if (IsFunc) |
| 136 | report_fatal_error("Function addresses with offsets not supported"); |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 137 | if (IsGlob) |
| 138 | report_fatal_error("Global indexes with offsets not supported"); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 139 | if (IsEvent) |
| 140 | report_fatal_error("Event indexes with offsets not supported"); |
Dan Gohman | a4b710a | 2015-12-06 19:33:32 +0000 | [diff] [blame] | 141 | Expr = |
| 142 | MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(Offset, Ctx), Ctx); |
| 143 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 144 | |
| 145 | return MCOperand::createExpr(Expr); |
| 146 | } |
| 147 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 148 | // Return the WebAssembly type associated with the given register class. |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 149 | static wasm::ValType getType(const TargetRegisterClass *RC) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 150 | if (RC == &WebAssembly::I32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 151 | return wasm::ValType::I32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 152 | if (RC == &WebAssembly::I64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 153 | return wasm::ValType::I64; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 154 | if (RC == &WebAssembly::F32RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 155 | return wasm::ValType::F32; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 156 | if (RC == &WebAssembly::F64RegClass) |
Derek Schuff | e2688c4 | 2017-03-14 20:23:22 +0000 | [diff] [blame] | 157 | return wasm::ValType::F64; |
Thomas Lively | 6f21a13 | 2018-09-20 22:04:44 +0000 | [diff] [blame] | 158 | if (RC == &WebAssembly::V128RegClass) |
| 159 | return wasm::ValType::V128; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 160 | llvm_unreachable("Unexpected register class"); |
| 161 | } |
| 162 | |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 163 | void WebAssemblyMCInstLower::Lower(const MachineInstr *MI, |
| 164 | MCInst &OutMI) const { |
| 165 | OutMI.setOpcode(MI->getOpcode()); |
| 166 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 167 | const MCInstrDesc &Desc = MI->getDesc(); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 168 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 169 | const MachineOperand &MO = MI->getOperand(i); |
| 170 | |
| 171 | MCOperand MCOp; |
| 172 | switch (MO.getType()) { |
| 173 | default: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 174 | MI->print(errs()); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 175 | llvm_unreachable("unknown operand type"); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 176 | case MachineOperand::MO_MachineBasicBlock: |
Richard Trieu | 3de487b | 2017-01-28 03:23:49 +0000 | [diff] [blame] | 177 | MI->print(errs()); |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 178 | llvm_unreachable("MachineBasicBlock operand should have been rewritten"); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 179 | case MachineOperand::MO_Register: { |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 180 | // Ignore all implicit register operands. |
| 181 | if (MO.isImplicit()) |
| 182 | continue; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 183 | const WebAssemblyFunctionInfo &MFI = |
| 184 | *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 185 | unsigned WAReg = MFI.getWAReg(MO.getReg()); |
| 186 | MCOp = MCOperand::createReg(WAReg); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 187 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 188 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 189 | case MachineOperand::MO_Immediate: |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 190 | if (i < Desc.NumOperands) { |
| 191 | const MCOperandInfo &Info = Desc.OpInfo[i]; |
| 192 | if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) { |
| 193 | MCSymbol *Sym = Printer.createTempSymbol("typeindex"); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 194 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 195 | SmallVector<wasm::ValType, 4> Returns; |
| 196 | SmallVector<wasm::ValType, 4> Params; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 197 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 198 | const MachineRegisterInfo &MRI = |
| 199 | MI->getParent()->getParent()->getRegInfo(); |
| 200 | for (const MachineOperand &MO : MI->defs()) |
| 201 | Returns.push_back(getType(MRI.getRegClass(MO.getReg()))); |
| 202 | for (const MachineOperand &MO : MI->explicit_uses()) |
| 203 | if (MO.isReg()) |
| 204 | Params.push_back(getType(MRI.getRegClass(MO.getReg()))); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 205 | |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 206 | // call_indirect instructions have a callee operand at the end which |
| 207 | // doesn't count as a param. |
| 208 | if (WebAssembly::isCallIndirect(*MI)) |
| 209 | Params.pop_back(); |
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 | MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym); |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 212 | auto Signature = make_unique<wasm::WasmSignature>(std::move(Returns), |
| 213 | std::move(Params)); |
| 214 | WasmSym->setSignature(Signature.get()); |
| 215 | Printer.addSignature(std::move(Signature)); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 216 | WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); |
| 217 | |
| 218 | const MCExpr *Expr = MCSymbolRefExpr::create( |
| 219 | WasmSym, MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX, Ctx); |
| 220 | MCOp = MCOperand::createExpr(Expr); |
| 221 | break; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 224 | MCOp = MCOperand::createImm(MO.getImm()); |
| 225 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 226 | case MachineOperand::MO_FPImmediate: { |
| 227 | // TODO: MC converts all floating point immediate operands to double. |
| 228 | // This is fine for numeric values, but may cause NaNs to change bits. |
| 229 | const ConstantFP *Imm = MO.getFPImm(); |
| 230 | if (Imm->getType()->isFloatTy()) |
| 231 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat()); |
| 232 | else if (Imm->getType()->isDoubleTy()) |
| 233 | MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble()); |
| 234 | else |
| 235 | llvm_unreachable("unknown floating point immediate type"); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 236 | break; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 237 | } |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 238 | case MachineOperand::MO_GlobalAddress: |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 239 | assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG && |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 240 | "WebAssembly does not use target flags on GlobalAddresses"); |
| 241 | MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 242 | MO.getGlobal()->getValueType()->isFunctionTy(), |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 243 | false, false); |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 244 | break; |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 245 | case MachineOperand::MO_ExternalSymbol: |
Dan Gohman | 26c6765 | 2016-01-11 23:38:05 +0000 | [diff] [blame] | 246 | // The target flag indicates whether this is a symbol for a |
| 247 | // variable or a function. |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 248 | assert((MO.getTargetFlags() & ~WebAssemblyII::MO_SYMBOL_MASK) == 0 && |
| 249 | "WebAssembly uses only symbol flags on ExternalSymbols"); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 250 | MCOp = LowerSymbolOperand( |
| 251 | GetExternalSymbolSymbol(MO), /*Offset=*/0, |
Nicholas Wilson | e408a89 | 2018-08-03 14:33:37 +0000 | [diff] [blame] | 252 | (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0, |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 253 | (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0, |
| 254 | (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_EVENT) != 0); |
Dan Gohman | 2c8fe6a | 2015-11-25 16:44:29 +0000 | [diff] [blame] | 255 | break; |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 256 | case MachineOperand::MO_MCSymbol: |
| 257 | // This is currently used only for LSDA symbols (GCC_except_table), |
| 258 | // because global addresses or other external symbols are handled above. |
| 259 | assert(MO.getTargetFlags() == 0 && |
| 260 | "WebAssembly does not use target flags on MCSymbol"); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 261 | MCOp = LowerSymbolOperand(MO.getMCSymbol(), /*Offset=*/0, false, false, |
| 262 | false); |
Heejin Ahn | 24faf85 | 2018-10-25 23:55:10 +0000 | [diff] [blame] | 263 | break; |
Dan Gohman | e9361d5 | 2015-11-05 19:28:16 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | OutMI.addOperand(MCOp); |
| 267 | } |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 268 | |
| 269 | if (!WasmKeepRegisters) |
| 270 | removeRegisterOperands(MI, OutMI); |
| 271 | } |
| 272 | |
| 273 | static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI) { |
| 274 | // Remove all uses of stackified registers to bring the instruction format |
| 275 | // into its final stack form used thruout MC, and transition opcodes to |
| 276 | // their _S variant. |
| 277 | // We do this seperate from the above code that still may need these |
| 278 | // registers for e.g. call_indirect signatures. |
| 279 | // See comments in lib/Target/WebAssembly/WebAssemblyInstrFormats.td for |
| 280 | // details. |
| 281 | // TODO: the code above creates new registers which are then removed here. |
| 282 | // That code could be slightly simplified by not doing that, though maybe |
| 283 | // it is simpler conceptually to keep the code above in "register mode" |
| 284 | // until this transition point. |
| 285 | // FIXME: we are not processing inline assembly, which contains register |
| 286 | // operands, because it is used by later target generic code. |
| 287 | if (MI->isDebugInstr() || MI->isLabel() || MI->isInlineAsm()) |
| 288 | return; |
| 289 | |
| 290 | // Transform to _S instruction. |
| 291 | auto RegOpcode = OutMI.getOpcode(); |
Thomas Lively | c63b5fc | 2018-10-22 21:55:26 +0000 | [diff] [blame] | 292 | auto StackOpcode = WebAssembly::getStackOpcode(RegOpcode); |
| 293 | assert(StackOpcode != -1 && "Failed to stackify instruction"); |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 294 | OutMI.setOpcode(StackOpcode); |
| 295 | |
| 296 | // Remove register operands. |
| 297 | for (auto I = OutMI.getNumOperands(); I; --I) { |
| 298 | auto &MO = OutMI.getOperand(I - 1); |
| 299 | if (MO.isReg()) { |
| 300 | OutMI.erase(&MO); |
| 301 | } |
| 302 | } |
| 303 | } |