Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_WASM_MACRO_GEN_H_ |
| 6 | #define V8_WASM_MACRO_GEN_H_ |
| 7 | |
| 8 | #include "src/wasm/wasm-opcodes.h" |
| 9 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 10 | #define U32_LE(v) \ |
| 11 | static_cast<byte>(v), static_cast<byte>((v) >> 8), \ |
| 12 | static_cast<byte>((v) >> 16), static_cast<byte>((v) >> 24) |
| 13 | |
| 14 | #define U16_LE(v) static_cast<byte>(v), static_cast<byte>((v) >> 8) |
| 15 | |
| 16 | #define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion) |
| 17 | |
| 18 | #define SIG_INDEX(v) U16_LE(v) |
| 19 | // TODO(binji): make SIG_INDEX match this. |
| 20 | #define IMPORT_SIG_INDEX(v) U32V_1(v) |
| 21 | #define FUNC_INDEX(v) U32V_1(v) |
| 22 | #define NO_NAME U32V_1(0) |
| 23 | #define NAME_LENGTH(v) U32V_1(v) |
| 24 | |
| 25 | #define ZERO_ALIGNMENT 0 |
| 26 | #define ZERO_OFFSET 0 |
| 27 | |
| 28 | #define BR_TARGET(v) U32_LE(v) |
| 29 | |
| 30 | #define MASK_7 ((1 << 7) - 1) |
| 31 | #define MASK_14 ((1 << 14) - 1) |
| 32 | #define MASK_21 ((1 << 21) - 1) |
| 33 | #define MASK_28 ((1 << 28) - 1) |
| 34 | |
| 35 | #define U32V_1(x) static_cast<byte>((x)&MASK_7) |
| 36 | #define U32V_2(x) \ |
| 37 | static_cast<byte>(((x)&MASK_7) | 0x80), static_cast<byte>(((x) >> 7) & MASK_7) |
| 38 | #define U32V_3(x) \ |
| 39 | static_cast<byte>((((x)) & MASK_7) | 0x80), \ |
| 40 | static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
| 41 | static_cast<byte>(((x) >> 14) & MASK_7) |
| 42 | #define U32V_4(x) \ |
| 43 | static_cast<byte>(((x)&MASK_7) | 0x80), \ |
| 44 | static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
| 45 | static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \ |
| 46 | static_cast<byte>(((x) >> 21) & MASK_7) |
| 47 | #define U32V_5(x) \ |
| 48 | static_cast<byte>(((x)&MASK_7) | 0x80), \ |
| 49 | static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
| 50 | static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \ |
| 51 | static_cast<byte>((((x) >> 21) & MASK_7) | 0x80), \ |
| 52 | static_cast<byte>((((x) >> 28) & MASK_7)) |
| 53 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | // Convenience macros for building Wasm bytecode directly into a byte array. |
| 55 | |
| 56 | //------------------------------------------------------------------------------ |
| 57 | // Control. |
| 58 | //------------------------------------------------------------------------------ |
| 59 | #define WASM_NOP kExprNop |
| 60 | |
| 61 | #define WASM_BLOCK(count, ...) kExprBlock, static_cast<byte>(count), __VA_ARGS__ |
| 62 | #define WASM_INFINITE_LOOP kExprLoop, 1, kExprBr, 0, kExprNop |
| 63 | #define WASM_LOOP(count, ...) kExprLoop, static_cast<byte>(count), __VA_ARGS__ |
| 64 | #define WASM_IF(cond, tstmt) kExprIf, cond, tstmt |
| 65 | #define WASM_IF_ELSE(cond, tstmt, fstmt) kExprIfElse, cond, tstmt, fstmt |
| 66 | #define WASM_SELECT(cond, tval, fval) kExprSelect, cond, tval, fval |
| 67 | #define WASM_BR(depth) kExprBr, static_cast<byte>(depth), kExprNop |
| 68 | #define WASM_BR_IF(depth, cond) \ |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 69 | kExprBrIf, static_cast<byte>(depth), kExprNop, cond |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 70 | #define WASM_BRV(depth, val) kExprBr, static_cast<byte>(depth), val |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 71 | #define WASM_BRV_IF(depth, val, cond) \ |
| 72 | kExprBrIf, static_cast<byte>(depth), val, cond |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 73 | #define WASM_BREAK(depth) kExprBr, static_cast<byte>(depth + 1), kExprNop |
| 74 | #define WASM_CONTINUE(depth) kExprBr, static_cast<byte>(depth), kExprNop |
| 75 | #define WASM_BREAKV(depth, val) kExprBr, static_cast<byte>(depth + 1), val |
| 76 | #define WASM_RETURN0 kExprReturn |
| 77 | #define WASM_RETURN(...) kExprReturn, __VA_ARGS__ |
| 78 | #define WASM_UNREACHABLE kExprUnreachable |
| 79 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 80 | #define WASM_BR_TABLE(key, count, ...) \ |
| 81 | kExprBrTable, U32V_1(count), __VA_ARGS__, key |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 82 | |
| 83 | #define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8) |
| 84 | #define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8) |
| 85 | |
| 86 | //------------------------------------------------------------------------------ |
| 87 | // Misc expressions. |
| 88 | //------------------------------------------------------------------------------ |
| 89 | #define WASM_ID(...) __VA_ARGS__ |
| 90 | #define WASM_ZERO kExprI8Const, 0 |
| 91 | #define WASM_ONE kExprI8Const, 1 |
| 92 | #define WASM_I8(val) kExprI8Const, static_cast<byte>(val) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 93 | |
| 94 | #define I32V_MIN(length) -(1 << (6 + (7 * ((length) - 1)))) |
| 95 | #define I32V_MAX(length) ((1 << (6 + (7 * ((length) - 1)))) - 1) |
| 96 | #define I64V_MIN(length) -(1LL << (6 + (7 * ((length) - 1)))) |
| 97 | #define I64V_MAX(length) ((1LL << (6 + 7 * ((length) - 1))) - 1) |
| 98 | |
| 99 | #define I32V_IN_RANGE(value, length) \ |
| 100 | ((value) >= I32V_MIN(length) && (value) <= I32V_MAX(length)) |
| 101 | #define I64V_IN_RANGE(value, length) \ |
| 102 | ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length)) |
| 103 | |
| 104 | #define WASM_NO_LOCALS 0 |
| 105 | |
| 106 | namespace v8 { |
| 107 | namespace internal { |
| 108 | namespace wasm { |
| 109 | |
| 110 | inline void CheckI32v(int32_t value, int length) { |
| 111 | DCHECK(length >= 1 && length <= 5); |
| 112 | DCHECK(length == 5 || I32V_IN_RANGE(value, length)); |
| 113 | } |
| 114 | |
| 115 | inline void CheckI64v(int64_t value, int length) { |
| 116 | DCHECK(length >= 1 && length <= 10); |
| 117 | DCHECK(length == 10 || I64V_IN_RANGE(value, length)); |
| 118 | } |
| 119 | |
| 120 | // A helper for encoding local declarations prepended to the body of a |
| 121 | // function. |
| 122 | class LocalDeclEncoder { |
| 123 | public: |
| 124 | // Prepend local declarations by creating a new buffer and copying data |
| 125 | // over. The new buffer must be delete[]'d by the caller. |
| 126 | void Prepend(const byte** start, const byte** end) const { |
| 127 | size_t size = (*end - *start); |
| 128 | byte* buffer = new byte[Size() + size]; |
| 129 | size_t pos = Emit(buffer); |
| 130 | memcpy(buffer + pos, *start, size); |
| 131 | pos += size; |
| 132 | *start = buffer; |
| 133 | *end = buffer + pos; |
| 134 | } |
| 135 | |
| 136 | size_t Emit(byte* buffer) const { |
| 137 | size_t pos = 0; |
| 138 | pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size())); |
| 139 | for (size_t i = 0; i < local_decls.size(); i++) { |
| 140 | pos = WriteUint32v(buffer, pos, local_decls[i].first); |
| 141 | buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second); |
| 142 | } |
| 143 | DCHECK_EQ(Size(), pos); |
| 144 | return pos; |
| 145 | } |
| 146 | |
| 147 | // Add locals declarations to this helper. Return the index of the newly added |
| 148 | // local(s), with an optional adjustment for the parameters. |
| 149 | uint32_t AddLocals(uint32_t count, LocalType type, |
| 150 | FunctionSig* sig = nullptr) { |
| 151 | if (count == 0) { |
| 152 | return static_cast<uint32_t>((sig ? sig->parameter_count() : 0) + |
| 153 | local_decls.size()); |
| 154 | } |
| 155 | size_t pos = local_decls.size(); |
| 156 | if (local_decls.size() > 0 && local_decls.back().second == type) { |
| 157 | count += local_decls.back().first; |
| 158 | local_decls.pop_back(); |
| 159 | } |
| 160 | local_decls.push_back(std::pair<uint32_t, LocalType>(count, type)); |
| 161 | return static_cast<uint32_t>(pos + (sig ? sig->parameter_count() : 0)); |
| 162 | } |
| 163 | |
| 164 | size_t Size() const { |
| 165 | size_t size = SizeofUint32v(static_cast<uint32_t>(local_decls.size())); |
| 166 | for (auto p : local_decls) size += 1 + SizeofUint32v(p.first); |
| 167 | return size; |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | std::vector<std::pair<uint32_t, LocalType>> local_decls; |
| 172 | |
| 173 | size_t SizeofUint32v(uint32_t val) const { |
| 174 | size_t size = 1; |
| 175 | while (true) { |
| 176 | byte b = val & MASK_7; |
| 177 | if (b == val) return size; |
| 178 | size++; |
| 179 | val = val >> 7; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // TODO(titzer): lift encoding of u32v to a common place. |
| 184 | size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const { |
| 185 | while (true) { |
| 186 | byte b = val & MASK_7; |
| 187 | if (b == val) { |
| 188 | buffer[pos++] = b; |
| 189 | break; |
| 190 | } |
| 191 | buffer[pos++] = 0x80 | b; |
| 192 | val = val >> 7; |
| 193 | } |
| 194 | return pos; |
| 195 | } |
| 196 | }; |
| 197 | } // namespace wasm |
| 198 | } // namespace internal |
| 199 | } // namespace v8 |
| 200 | |
| 201 | //------------------------------------------------------------------------------ |
| 202 | // Int32 Const operations |
| 203 | //------------------------------------------------------------------------------ |
| 204 | #define WASM_I32V(val) kExprI32Const, U32V_5(val) |
| 205 | |
| 206 | #define WASM_I32V_1(val) \ |
| 207 | static_cast<byte>(CheckI32v((val), 1), kExprI32Const), U32V_1(val) |
| 208 | #define WASM_I32V_2(val) \ |
| 209 | static_cast<byte>(CheckI32v((val), 2), kExprI32Const), U32V_2(val) |
| 210 | #define WASM_I32V_3(val) \ |
| 211 | static_cast<byte>(CheckI32v((val), 3), kExprI32Const), U32V_3(val) |
| 212 | #define WASM_I32V_4(val) \ |
| 213 | static_cast<byte>(CheckI32v((val), 4), kExprI32Const), U32V_4(val) |
| 214 | #define WASM_I32V_5(val) \ |
| 215 | static_cast<byte>(CheckI32v((val), 5), kExprI32Const), U32V_5(val) |
| 216 | |
| 217 | //------------------------------------------------------------------------------ |
| 218 | // Int64 Const operations |
| 219 | //------------------------------------------------------------------------------ |
| 220 | #define WASM_I64V(val) \ |
| 221 | kExprI64Const, \ |
| 222 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 223 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 224 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 225 | static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 226 | static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 227 | static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 228 | static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 229 | static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \ |
| 230 | static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \ |
| 231 | static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7) |
| 232 | |
| 233 | #define WASM_I64V_1(val) \ |
| 234 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 1), kExprI64Const), \ |
| 235 | static_cast<byte>(static_cast<int64_t>(val) & MASK_7) |
| 236 | #define WASM_I64V_2(val) \ |
| 237 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 2), kExprI64Const), \ |
| 238 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 239 | static_cast<byte>((static_cast<int64_t>(val) >> 7) & MASK_7) |
| 240 | #define WASM_I64V_3(val) \ |
| 241 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 3), kExprI64Const), \ |
| 242 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 243 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 244 | static_cast<byte>((static_cast<int64_t>(val) >> 14) & MASK_7) |
| 245 | #define WASM_I64V_4(val) \ |
| 246 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 4), kExprI64Const), \ |
| 247 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 248 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 249 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 250 | static_cast<byte>((static_cast<int64_t>(val) >> 21) & MASK_7) |
| 251 | #define WASM_I64V_5(val) \ |
| 252 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 5), kExprI64Const), \ |
| 253 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 254 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 255 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 256 | static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 257 | static_cast<byte>((static_cast<int64_t>(val) >> 28) & MASK_7) |
| 258 | #define WASM_I64V_6(val) \ |
| 259 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 6), kExprI64Const), \ |
| 260 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 261 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 262 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 263 | static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 264 | static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 265 | static_cast<byte>((static_cast<int64_t>(val) >> 35) & MASK_7) |
| 266 | #define WASM_I64V_7(val) \ |
| 267 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 7), kExprI64Const), \ |
| 268 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 269 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 270 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 271 | static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 272 | static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 273 | static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 274 | static_cast<byte>((static_cast<int64_t>(val) >> 42) & MASK_7) |
| 275 | #define WASM_I64V_8(val) \ |
| 276 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 8), kExprI64Const), \ |
| 277 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 278 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 279 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 280 | static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 281 | static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 282 | static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 283 | static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 284 | static_cast<byte>((static_cast<int64_t>(val) >> 49) & MASK_7) |
| 285 | #define WASM_I64V_9(val) \ |
| 286 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 9), kExprI64Const), \ |
| 287 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 288 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 289 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 290 | static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 291 | static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 292 | static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 293 | static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 294 | static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \ |
| 295 | static_cast<byte>((static_cast<int64_t>(val) >> 56) & MASK_7) |
| 296 | #define WASM_I64V_10(val) \ |
| 297 | static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 10), kExprI64Const), \ |
| 298 | static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 299 | static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 300 | static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 301 | static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 302 | static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 303 | static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 304 | static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 305 | static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \ |
| 306 | static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \ |
| 307 | static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7) |
| 308 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | #define WASM_F32(val) \ |
| 310 | kExprF32Const, \ |
| 311 | static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))), \ |
| 312 | static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8), \ |
| 313 | static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \ |
| 314 | static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24) |
| 315 | #define WASM_F64(val) \ |
| 316 | kExprF64Const, static_cast<byte>(bit_cast<uint64_t>(val)), \ |
| 317 | static_cast<byte>(bit_cast<uint64_t>(val) >> 8), \ |
| 318 | static_cast<byte>(bit_cast<uint64_t>(val) >> 16), \ |
| 319 | static_cast<byte>(bit_cast<uint64_t>(val) >> 24), \ |
| 320 | static_cast<byte>(bit_cast<uint64_t>(val) >> 32), \ |
| 321 | static_cast<byte>(bit_cast<uint64_t>(val) >> 40), \ |
| 322 | static_cast<byte>(bit_cast<uint64_t>(val) >> 48), \ |
| 323 | static_cast<byte>(bit_cast<uint64_t>(val) >> 56) |
| 324 | #define WASM_GET_LOCAL(index) kExprGetLocal, static_cast<byte>(index) |
| 325 | #define WASM_SET_LOCAL(index, val) kExprSetLocal, static_cast<byte>(index), val |
| 326 | #define WASM_LOAD_GLOBAL(index) kExprLoadGlobal, static_cast<byte>(index) |
| 327 | #define WASM_STORE_GLOBAL(index, val) \ |
| 328 | kExprStoreGlobal, static_cast<byte>(index), val |
| 329 | #define WASM_LOAD_MEM(type, index) \ |
| 330 | static_cast<byte>( \ |
| 331 | v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 332 | ZERO_ALIGNMENT, ZERO_OFFSET, index |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 333 | #define WASM_STORE_MEM(type, index, val) \ |
| 334 | static_cast<byte>( \ |
| 335 | v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 336 | ZERO_ALIGNMENT, ZERO_OFFSET, index, val |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 337 | #define WASM_LOAD_MEM_OFFSET(type, offset, index) \ |
| 338 | static_cast<byte>( \ |
| 339 | v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 340 | ZERO_ALIGNMENT, U32V_1(offset), index |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 341 | #define WASM_STORE_MEM_OFFSET(type, offset, index, val) \ |
| 342 | static_cast<byte>( \ |
| 343 | v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \ |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 344 | ZERO_ALIGNMENT, U32V_1(offset), index, val |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 345 | #define WASM_CALL_FUNCTION(index, ...) \ |
| 346 | kExprCallFunction, static_cast<byte>(index), __VA_ARGS__ |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 347 | #define WASM_CALL_IMPORT(index, ...) \ |
| 348 | kExprCallImport, static_cast<byte>(index), __VA_ARGS__ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 349 | #define WASM_CALL_INDIRECT(index, func, ...) \ |
| 350 | kExprCallIndirect, static_cast<byte>(index), func, __VA_ARGS__ |
| 351 | #define WASM_CALL_FUNCTION0(index) kExprCallFunction, static_cast<byte>(index) |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 352 | #define WASM_CALL_IMPORT0(index) kExprCallImport, static_cast<byte>(index) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 353 | #define WASM_CALL_INDIRECT0(index, func) \ |
| 354 | kExprCallIndirect, static_cast<byte>(index), func |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 355 | #define WASM_NOT(x) kExprI32Eqz, x |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 356 | |
| 357 | //------------------------------------------------------------------------------ |
| 358 | // Constructs that are composed of multiple bytecodes. |
| 359 | //------------------------------------------------------------------------------ |
| 360 | #define WASM_WHILE(x, y) kExprLoop, 1, kExprIf, x, kExprBr, 0, y |
| 361 | #define WASM_INC_LOCAL(index) \ |
| 362 | kExprSetLocal, static_cast<byte>(index), kExprI32Add, kExprGetLocal, \ |
| 363 | static_cast<byte>(index), kExprI8Const, 1 |
| 364 | #define WASM_INC_LOCAL_BY(index, count) \ |
| 365 | kExprSetLocal, static_cast<byte>(index), kExprI32Add, kExprGetLocal, \ |
| 366 | static_cast<byte>(index), kExprI8Const, static_cast<int8_t>(count) |
| 367 | |
| 368 | #define WASM_UNOP(opcode, x) static_cast<byte>(opcode), x |
| 369 | #define WASM_BINOP(opcode, x, y) static_cast<byte>(opcode), x, y |
| 370 | |
| 371 | //------------------------------------------------------------------------------ |
| 372 | // Int32 operations |
| 373 | //------------------------------------------------------------------------------ |
| 374 | #define WASM_I32_ADD(x, y) kExprI32Add, x, y |
| 375 | #define WASM_I32_SUB(x, y) kExprI32Sub, x, y |
| 376 | #define WASM_I32_MUL(x, y) kExprI32Mul, x, y |
| 377 | #define WASM_I32_DIVS(x, y) kExprI32DivS, x, y |
| 378 | #define WASM_I32_DIVU(x, y) kExprI32DivU, x, y |
| 379 | #define WASM_I32_REMS(x, y) kExprI32RemS, x, y |
| 380 | #define WASM_I32_REMU(x, y) kExprI32RemU, x, y |
| 381 | #define WASM_I32_AND(x, y) kExprI32And, x, y |
| 382 | #define WASM_I32_IOR(x, y) kExprI32Ior, x, y |
| 383 | #define WASM_I32_XOR(x, y) kExprI32Xor, x, y |
| 384 | #define WASM_I32_SHL(x, y) kExprI32Shl, x, y |
| 385 | #define WASM_I32_SHR(x, y) kExprI32ShrU, x, y |
| 386 | #define WASM_I32_SAR(x, y) kExprI32ShrS, x, y |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 387 | #define WASM_I32_ROR(x, y) kExprI32Ror, x, y |
| 388 | #define WASM_I32_ROL(x, y) kExprI32Rol, x, y |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 389 | #define WASM_I32_EQ(x, y) kExprI32Eq, x, y |
| 390 | #define WASM_I32_NE(x, y) kExprI32Ne, x, y |
| 391 | #define WASM_I32_LTS(x, y) kExprI32LtS, x, y |
| 392 | #define WASM_I32_LES(x, y) kExprI32LeS, x, y |
| 393 | #define WASM_I32_LTU(x, y) kExprI32LtU, x, y |
| 394 | #define WASM_I32_LEU(x, y) kExprI32LeU, x, y |
| 395 | #define WASM_I32_GTS(x, y) kExprI32GtS, x, y |
| 396 | #define WASM_I32_GES(x, y) kExprI32GeS, x, y |
| 397 | #define WASM_I32_GTU(x, y) kExprI32GtU, x, y |
| 398 | #define WASM_I32_GEU(x, y) kExprI32GeU, x, y |
| 399 | #define WASM_I32_CLZ(x) kExprI32Clz, x |
| 400 | #define WASM_I32_CTZ(x) kExprI32Ctz, x |
| 401 | #define WASM_I32_POPCNT(x) kExprI32Popcnt, x |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 402 | #define WASM_I32_EQZ(x) kExprI32Eqz, x |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 403 | |
| 404 | //------------------------------------------------------------------------------ |
| 405 | // Int64 operations |
| 406 | //------------------------------------------------------------------------------ |
| 407 | #define WASM_I64_ADD(x, y) kExprI64Add, x, y |
| 408 | #define WASM_I64_SUB(x, y) kExprI64Sub, x, y |
| 409 | #define WASM_I64_MUL(x, y) kExprI64Mul, x, y |
| 410 | #define WASM_I64_DIVS(x, y) kExprI64DivS, x, y |
| 411 | #define WASM_I64_DIVU(x, y) kExprI64DivU, x, y |
| 412 | #define WASM_I64_REMS(x, y) kExprI64RemS, x, y |
| 413 | #define WASM_I64_REMU(x, y) kExprI64RemU, x, y |
| 414 | #define WASM_I64_AND(x, y) kExprI64And, x, y |
| 415 | #define WASM_I64_IOR(x, y) kExprI64Ior, x, y |
| 416 | #define WASM_I64_XOR(x, y) kExprI64Xor, x, y |
| 417 | #define WASM_I64_SHL(x, y) kExprI64Shl, x, y |
| 418 | #define WASM_I64_SHR(x, y) kExprI64ShrU, x, y |
| 419 | #define WASM_I64_SAR(x, y) kExprI64ShrS, x, y |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 420 | #define WASM_I64_ROR(x, y) kExprI64Ror, x, y |
| 421 | #define WASM_I64_ROL(x, y) kExprI64Rol, x, y |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 422 | #define WASM_I64_EQ(x, y) kExprI64Eq, x, y |
| 423 | #define WASM_I64_NE(x, y) kExprI64Ne, x, y |
| 424 | #define WASM_I64_LTS(x, y) kExprI64LtS, x, y |
| 425 | #define WASM_I64_LES(x, y) kExprI64LeS, x, y |
| 426 | #define WASM_I64_LTU(x, y) kExprI64LtU, x, y |
| 427 | #define WASM_I64_LEU(x, y) kExprI64LeU, x, y |
| 428 | #define WASM_I64_GTS(x, y) kExprI64GtS, x, y |
| 429 | #define WASM_I64_GES(x, y) kExprI64GeS, x, y |
| 430 | #define WASM_I64_GTU(x, y) kExprI64GtU, x, y |
| 431 | #define WASM_I64_GEU(x, y) kExprI64GeU, x, y |
| 432 | #define WASM_I64_CLZ(x) kExprI64Clz, x |
| 433 | #define WASM_I64_CTZ(x) kExprI64Ctz, x |
| 434 | #define WASM_I64_POPCNT(x) kExprI64Popcnt, x |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 435 | #define WASM_I64_EQZ(x) kExprI64Eqz, x |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 436 | |
| 437 | //------------------------------------------------------------------------------ |
| 438 | // Float32 operations |
| 439 | //------------------------------------------------------------------------------ |
| 440 | #define WASM_F32_ADD(x, y) kExprF32Add, x, y |
| 441 | #define WASM_F32_SUB(x, y) kExprF32Sub, x, y |
| 442 | #define WASM_F32_MUL(x, y) kExprF32Mul, x, y |
| 443 | #define WASM_F32_DIV(x, y) kExprF32Div, x, y |
| 444 | #define WASM_F32_MIN(x, y) kExprF32Min, x, y |
| 445 | #define WASM_F32_MAX(x, y) kExprF32Max, x, y |
| 446 | #define WASM_F32_ABS(x) kExprF32Abs, x |
| 447 | #define WASM_F32_NEG(x) kExprF32Neg, x |
| 448 | #define WASM_F32_COPYSIGN(x, y) kExprF32CopySign, x, y |
| 449 | #define WASM_F32_CEIL(x) kExprF32Ceil, x |
| 450 | #define WASM_F32_FLOOR(x) kExprF32Floor, x |
| 451 | #define WASM_F32_TRUNC(x) kExprF32Trunc, x |
| 452 | #define WASM_F32_NEARESTINT(x) kExprF32NearestInt, x |
| 453 | #define WASM_F32_SQRT(x) kExprF32Sqrt, x |
| 454 | #define WASM_F32_EQ(x, y) kExprF32Eq, x, y |
| 455 | #define WASM_F32_NE(x, y) kExprF32Ne, x, y |
| 456 | #define WASM_F32_LT(x, y) kExprF32Lt, x, y |
| 457 | #define WASM_F32_LE(x, y) kExprF32Le, x, y |
| 458 | #define WASM_F32_GT(x, y) kExprF32Gt, x, y |
| 459 | #define WASM_F32_GE(x, y) kExprF32Ge, x, y |
| 460 | |
| 461 | //------------------------------------------------------------------------------ |
| 462 | // Float64 operations |
| 463 | //------------------------------------------------------------------------------ |
| 464 | #define WASM_F64_ADD(x, y) kExprF64Add, x, y |
| 465 | #define WASM_F64_SUB(x, y) kExprF64Sub, x, y |
| 466 | #define WASM_F64_MUL(x, y) kExprF64Mul, x, y |
| 467 | #define WASM_F64_DIV(x, y) kExprF64Div, x, y |
| 468 | #define WASM_F64_MIN(x, y) kExprF64Min, x, y |
| 469 | #define WASM_F64_MAX(x, y) kExprF64Max, x, y |
| 470 | #define WASM_F64_ABS(x) kExprF64Abs, x |
| 471 | #define WASM_F64_NEG(x) kExprF64Neg, x |
| 472 | #define WASM_F64_COPYSIGN(x, y) kExprF64CopySign, x, y |
| 473 | #define WASM_F64_CEIL(x) kExprF64Ceil, x |
| 474 | #define WASM_F64_FLOOR(x) kExprF64Floor, x |
| 475 | #define WASM_F64_TRUNC(x) kExprF64Trunc, x |
| 476 | #define WASM_F64_NEARESTINT(x) kExprF64NearestInt, x |
| 477 | #define WASM_F64_SQRT(x) kExprF64Sqrt, x |
| 478 | #define WASM_F64_EQ(x, y) kExprF64Eq, x, y |
| 479 | #define WASM_F64_NE(x, y) kExprF64Ne, x, y |
| 480 | #define WASM_F64_LT(x, y) kExprF64Lt, x, y |
| 481 | #define WASM_F64_LE(x, y) kExprF64Le, x, y |
| 482 | #define WASM_F64_GT(x, y) kExprF64Gt, x, y |
| 483 | #define WASM_F64_GE(x, y) kExprF64Ge, x, y |
| 484 | |
| 485 | //------------------------------------------------------------------------------ |
| 486 | // Type conversions. |
| 487 | //------------------------------------------------------------------------------ |
| 488 | #define WASM_I32_SCONVERT_F32(x) kExprI32SConvertF32, x |
| 489 | #define WASM_I32_SCONVERT_F64(x) kExprI32SConvertF64, x |
| 490 | #define WASM_I32_UCONVERT_F32(x) kExprI32UConvertF32, x |
| 491 | #define WASM_I32_UCONVERT_F64(x) kExprI32UConvertF64, x |
| 492 | #define WASM_I32_CONVERT_I64(x) kExprI32ConvertI64, x |
| 493 | #define WASM_I64_SCONVERT_F32(x) kExprI64SConvertF32, x |
| 494 | #define WASM_I64_SCONVERT_F64(x) kExprI64SConvertF64, x |
| 495 | #define WASM_I64_UCONVERT_F32(x) kExprI64UConvertF32, x |
| 496 | #define WASM_I64_UCONVERT_F64(x) kExprI64UConvertF64, x |
| 497 | #define WASM_I64_SCONVERT_I32(x) kExprI64SConvertI32, x |
| 498 | #define WASM_I64_UCONVERT_I32(x) kExprI64UConvertI32, x |
| 499 | #define WASM_F32_SCONVERT_I32(x) kExprF32SConvertI32, x |
| 500 | #define WASM_F32_UCONVERT_I32(x) kExprF32UConvertI32, x |
| 501 | #define WASM_F32_SCONVERT_I64(x) kExprF32SConvertI64, x |
| 502 | #define WASM_F32_UCONVERT_I64(x) kExprF32UConvertI64, x |
| 503 | #define WASM_F32_CONVERT_F64(x) kExprF32ConvertF64, x |
| 504 | #define WASM_F32_REINTERPRET_I32(x) kExprF32ReinterpretI32, x |
| 505 | #define WASM_F64_SCONVERT_I32(x) kExprF64SConvertI32, x |
| 506 | #define WASM_F64_UCONVERT_I32(x) kExprF64UConvertI32, x |
| 507 | #define WASM_F64_SCONVERT_I64(x) kExprF64SConvertI64, x |
| 508 | #define WASM_F64_UCONVERT_I64(x) kExprF64UConvertI64, x |
| 509 | #define WASM_F64_CONVERT_F32(x) kExprF64ConvertF32, x |
| 510 | #define WASM_F64_REINTERPRET_I64(x) kExprF64ReinterpretI64, x |
| 511 | #define WASM_I32_REINTERPRET_F32(x) kExprI32ReinterpretF32, x |
| 512 | #define WASM_I64_REINTERPRET_F64(x) kExprI64ReinterpretF64, x |
| 513 | |
| 514 | #endif // V8_WASM_MACRO_GEN_H_ |