Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 1 | //=- WebAssemblyMCCodeEmitter.cpp - Convert WebAssembly code to machine code -// |
| 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 | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file implements the WebAssemblyMCCodeEmitter class. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/WebAssemblyFixupKinds.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/MC/MCCodeEmitter.h" |
| 19 | #include "llvm/MC/MCFixup.h" |
| 20 | #include "llvm/MC/MCInst.h" |
| 21 | #include "llvm/MC/MCInstrInfo.h" |
| 22 | #include "llvm/MC/MCRegisterInfo.h" |
| 23 | #include "llvm/MC/MCSubtargetInfo.h" |
| 24 | #include "llvm/MC/MCSymbol.h" |
Sam Clegg | 685c5e8 | 2018-04-04 22:27:58 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Reid Kleckner | 8f4bd1f | 2016-06-23 18:12:31 +0000 | [diff] [blame] | 26 | #include "llvm/Support/EndianStream.h" |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 27 | #include "llvm/Support/LEB128.h" |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Sam Clegg | 685c5e8 | 2018-04-04 22:27:58 +0000 | [diff] [blame] | 29 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "mccodeemitter" |
| 33 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 34 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted."); |
| 35 | STATISTIC(MCNumFixups, "Number of MC fixups created."); |
| 36 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | class WebAssemblyMCCodeEmitter final : public MCCodeEmitter { |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 39 | const MCInstrInfo &MCII; |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 40 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 41 | // Implementation generated by tablegen. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 42 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
| 43 | SmallVectorImpl<MCFixup> &Fixups, |
| 44 | const MCSubtargetInfo &STI) const; |
| 45 | |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 46 | void encodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 47 | SmallVectorImpl<MCFixup> &Fixups, |
| 48 | const MCSubtargetInfo &STI) const override; |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 49 | |
| 50 | public: |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 51 | WebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) : MCII(MCII) {} |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 52 | }; |
| 53 | } // end anonymous namespace |
| 54 | |
Sam Clegg | 9d24fb7 | 2017-06-16 23:59:10 +0000 | [diff] [blame] | 55 | MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) { |
| 56 | return new WebAssemblyMCCodeEmitter(MCII); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void WebAssemblyMCCodeEmitter::encodeInstruction( |
| 60 | const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups, |
| 61 | const MCSubtargetInfo &STI) const { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 62 | uint64_t Start = OS.tell(); |
| 63 | |
| 64 | uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 65 | if (Binary <= UINT8_MAX) { |
| 66 | OS << uint8_t(Binary); |
| 67 | } else { |
| 68 | assert(Binary <= UINT16_MAX && "Several-byte opcodes not supported yet"); |
Thomas Lively | 299d214 | 2018-11-09 01:45:56 +0000 | [diff] [blame] | 69 | OS << uint8_t(Binary >> 8); |
| 70 | encodeULEB128(uint8_t(Binary), OS); |
Dan Gohman | cdd48b8 | 2017-11-28 01:13:40 +0000 | [diff] [blame] | 71 | } |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 72 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 73 | // For br_table instructions, encode the size of the table. In the MCInst, |
Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 74 | // there's an index operand (if not a stack instruction), one operand for |
| 75 | // each table entry, and the default operand. |
| 76 | if (MI.getOpcode() == WebAssembly::BR_TABLE_I32_S || |
| 77 | MI.getOpcode() == WebAssembly::BR_TABLE_I64_S) |
| 78 | encodeULEB128(MI.getNumOperands() - 1, OS); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 79 | if (MI.getOpcode() == WebAssembly::BR_TABLE_I32 || |
| 80 | MI.getOpcode() == WebAssembly::BR_TABLE_I64) |
| 81 | encodeULEB128(MI.getNumOperands() - 2, OS); |
| 82 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 83 | const MCInstrDesc &Desc = MCII.get(MI.getOpcode()); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 84 | for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) { |
| 85 | const MCOperand &MO = MI.getOperand(I); |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 86 | if (MO.isReg()) { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 87 | /* nothing to encode */ |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 88 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 89 | } else if (MO.isImm()) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 90 | if (I < Desc.getNumOperands()) { |
| 91 | const MCOperandInfo &Info = Desc.OpInfo[I]; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 92 | LLVM_DEBUG(dbgs() << "Encoding immediate: type=" |
| 93 | << int(Info.OperandType) << "\n"); |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 94 | switch (Info.OperandType) { |
| 95 | case WebAssembly::OPERAND_I32IMM: |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 96 | encodeSLEB128(int32_t(MO.getImm()), OS); |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 97 | break; |
| 98 | case WebAssembly::OPERAND_OFFSET32: |
Sam Clegg | 685c5e8 | 2018-04-04 22:27:58 +0000 | [diff] [blame] | 99 | encodeULEB128(uint32_t(MO.getImm()), OS); |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 100 | break; |
| 101 | case WebAssembly::OPERAND_I64IMM: |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 102 | encodeSLEB128(int64_t(MO.getImm()), OS); |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 103 | break; |
| 104 | case WebAssembly::OPERAND_SIGNATURE: |
Heejin Ahn | 0c69a3e | 2018-03-02 20:52:59 +0000 | [diff] [blame] | 105 | OS << uint8_t(MO.getImm()); |
Thomas Lively | 2244292 | 2018-08-21 21:03:18 +0000 | [diff] [blame] | 106 | break; |
| 107 | case WebAssembly::OPERAND_VEC_I8IMM: |
| 108 | support::endian::write<uint8_t>(OS, MO.getImm(), support::little); |
| 109 | break; |
| 110 | case WebAssembly::OPERAND_VEC_I16IMM: |
| 111 | support::endian::write<uint16_t>(OS, MO.getImm(), support::little); |
| 112 | break; |
| 113 | case WebAssembly::OPERAND_VEC_I32IMM: |
| 114 | support::endian::write<uint32_t>(OS, MO.getImm(), support::little); |
| 115 | break; |
| 116 | case WebAssembly::OPERAND_VEC_I64IMM: |
| 117 | support::endian::write<uint64_t>(OS, MO.getImm(), support::little); |
| 118 | break; |
| 119 | case WebAssembly::OPERAND_GLOBAL: |
| 120 | llvm_unreachable("wasm globals should only be accessed symbolicly"); |
| 121 | default: |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 122 | encodeULEB128(uint64_t(MO.getImm()), OS); |
| 123 | } |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 124 | } else { |
| 125 | encodeULEB128(uint64_t(MO.getImm()), OS); |
| 126 | } |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 127 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 128 | } else if (MO.isFPImm()) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 129 | const MCOperandInfo &Info = Desc.OpInfo[I]; |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 130 | if (Info.OperandType == WebAssembly::OPERAND_F32IMM) { |
| 131 | // TODO: MC converts all floating point immediate operands to double. |
| 132 | // This is fine for numeric values, but may cause NaNs to change bits. |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 133 | auto F = float(MO.getFPImm()); |
| 134 | support::endian::write<float>(OS, F, support::little); |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 135 | } else { |
| 136 | assert(Info.OperandType == WebAssembly::OPERAND_F64IMM); |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 137 | double D = MO.getFPImm(); |
| 138 | support::endian::write<double>(OS, D, support::little); |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 139 | } |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 140 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 141 | } else if (MO.isExpr()) { |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 142 | const MCOperandInfo &Info = Desc.OpInfo[I]; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 143 | llvm::MCFixupKind FixupKind; |
Sam Clegg | 66a99e4 | 2017-09-15 20:34:47 +0000 | [diff] [blame] | 144 | size_t PaddedSize = 5; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 145 | switch (Info.OperandType) { |
| 146 | case WebAssembly::OPERAND_I32IMM: |
Sam Clegg | a5e175c | 2019-03-28 02:07:28 +0000 | [diff] [blame] | 147 | FixupKind = MCFixupKind(WebAssembly::fixup_sleb128_i32); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 148 | break; |
| 149 | case WebAssembly::OPERAND_I64IMM: |
Sam Clegg | a5e175c | 2019-03-28 02:07:28 +0000 | [diff] [blame] | 150 | FixupKind = MCFixupKind(WebAssembly::fixup_sleb128_i64); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 151 | PaddedSize = 10; |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 152 | break; |
| 153 | case WebAssembly::OPERAND_FUNCTION32: |
| 154 | case WebAssembly::OPERAND_OFFSET32: |
| 155 | case WebAssembly::OPERAND_TYPEINDEX: |
| 156 | case WebAssembly::OPERAND_GLOBAL: |
| 157 | case WebAssembly::OPERAND_EVENT: |
Sam Clegg | a5e175c | 2019-03-28 02:07:28 +0000 | [diff] [blame] | 158 | FixupKind = MCFixupKind(WebAssembly::fixup_uleb128_i32); |
Heejin Ahn | da419bd | 2018-11-14 02:46:21 +0000 | [diff] [blame] | 159 | break; |
| 160 | default: |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 161 | llvm_unreachable("unexpected symbolic operand kind"); |
| 162 | } |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 163 | Fixups.push_back(MCFixup::create(OS.tell() - Start, MO.getExpr(), |
| 164 | FixupKind, MI.getLoc())); |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 165 | ++MCNumFixups; |
Sam Clegg | 66a99e4 | 2017-09-15 20:34:47 +0000 | [diff] [blame] | 166 | encodeULEB128(0, OS, PaddedSize); |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 167 | } else { |
| 168 | llvm_unreachable("unexpected operand kind"); |
| 169 | } |
| 170 | } |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 171 | |
Dan Gohman | 1a42728 | 2016-01-12 03:32:29 +0000 | [diff] [blame] | 172 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | #include "WebAssemblyGenMCCodeEmitter.inc" |