OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_WASM_MACRO_GEN_H_ | 5 #ifndef V8_WASM_MACRO_GEN_H_ |
6 #define V8_WASM_MACRO_GEN_H_ | 6 #define V8_WASM_MACRO_GEN_H_ |
7 | 7 |
8 #include "src/wasm/wasm-opcodes.h" | 8 #include "src/wasm/wasm-opcodes.h" |
9 | 9 |
10 // Convenience macros for building Wasm bytecode directly into a byte array. | 10 // Convenience macros for building Wasm bytecode directly into a byte array. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 #define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8) | 45 #define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8) |
46 #define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8) | 46 #define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8) |
47 | 47 |
48 //------------------------------------------------------------------------------ | 48 //------------------------------------------------------------------------------ |
49 // Misc expressions. | 49 // Misc expressions. |
50 //------------------------------------------------------------------------------ | 50 //------------------------------------------------------------------------------ |
51 #define WASM_ID(...) __VA_ARGS__ | 51 #define WASM_ID(...) __VA_ARGS__ |
52 #define WASM_ZERO kExprI8Const, 0 | 52 #define WASM_ZERO kExprI8Const, 0 |
53 #define WASM_ONE kExprI8Const, 1 | 53 #define WASM_ONE kExprI8Const, 1 |
54 #define WASM_I8(val) kExprI8Const, static_cast<byte>(val) | 54 #define WASM_I8(val) kExprI8Const, static_cast<byte>(val) |
55 #define WASM_I32(val) \ | 55 |
56 kExprI32Const, static_cast<byte>(val), static_cast<byte>(val >> 8), \ | 56 #define I32V_MIN(length) -(1 << (6 + (7 * ((length) - 1)))) |
57 static_cast<byte>(val >> 16), static_cast<byte>(val >> 24) | 57 #define I32V_MAX(length) ((1 << (6 + (7 * ((length) - 1)))) - 1) |
58 #define WASM_I64(val) \ | 58 #define I64V_MIN(length) -(1LL << (6 + (7 * ((length) - 1)))) |
59 kExprI64Const, static_cast<byte>(static_cast<uint64_t>(val)), \ | 59 #define I64V_MAX(length) ((1LL << (6 + 7 * ((length) - 1))) - 1) |
60 static_cast<byte>(static_cast<uint64_t>(val) >> 8), \ | 60 |
61 static_cast<byte>(static_cast<uint64_t>(val) >> 16), \ | 61 #define I32V_IN_RANGE(value, length) \ |
62 static_cast<byte>(static_cast<uint64_t>(val) >> 24), \ | 62 ((value) >= I32V_MIN(length) && (value) <= I32V_MAX(length)) |
63 static_cast<byte>(static_cast<uint64_t>(val) >> 32), \ | 63 #define I64V_IN_RANGE(value, length) \ |
64 static_cast<byte>(static_cast<uint64_t>(val) >> 40), \ | 64 ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length)) |
65 static_cast<byte>(static_cast<uint64_t>(val) >> 48), \ | 65 |
66 static_cast<byte>(static_cast<uint64_t>(val) >> 56) | 66 namespace v8 { |
| 67 namespace internal { |
| 68 namespace wasm { |
| 69 |
| 70 inline void CheckI32v(int32_t value, int length) { |
| 71 DCHECK(length >= 1 && length <= 5); |
| 72 DCHECK(length == 5 || I32V_IN_RANGE(value, length)); |
| 73 DCHECK(length == 1 || !I32V_IN_RANGE(value, length - 1)); |
| 74 } |
| 75 |
| 76 inline void CheckI64v(int64_t value, int length) { |
| 77 DCHECK(length >= 1 && length <= 10); |
| 78 DCHECK(length == 10 || I64V_IN_RANGE(value, length)); |
| 79 DCHECK(length == 1 || !I64V_IN_RANGE(value, length - 1)); |
| 80 } |
| 81 |
| 82 } // namespace wasm |
| 83 } // namespace internal |
| 84 } // namespace v8 |
| 85 |
| 86 //------------------------------------------------------------------------------ |
| 87 // Int32 Const operations |
| 88 //------------------------------------------------------------------------------ |
| 89 #define WASM_I32V(val) kExprI32Const, U32V_5(val) |
| 90 |
| 91 #define WASM_I32V_1(val) \ |
| 92 static_cast<byte>(CheckI32v((val), 1), kExprI32Const), U32V_1(val) |
| 93 #define WASM_I32V_2(val) \ |
| 94 static_cast<byte>(CheckI32v((val), 2), kExprI32Const), U32V_2(val) |
| 95 #define WASM_I32V_3(val) \ |
| 96 static_cast<byte>(CheckI32v((val), 3), kExprI32Const), U32V_3(val) |
| 97 #define WASM_I32V_4(val) \ |
| 98 static_cast<byte>(CheckI32v((val), 4), kExprI32Const), U32V_4(val) |
| 99 #define WASM_I32V_5(val) \ |
| 100 static_cast<byte>(CheckI32v((val), 5), kExprI32Const), U32V_5(val) |
| 101 |
| 102 //------------------------------------------------------------------------------ |
| 103 // Int64 Const operations |
| 104 //------------------------------------------------------------------------------ |
| 105 #define WASM_I64V(val) \ |
| 106 kExprI64Const, \ |
| 107 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 108 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 109 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 110 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 111 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 112 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 113 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 114 static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \ |
| 115 static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \ |
| 116 static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7) |
| 117 |
| 118 #define WASM_I64V_1(val) \ |
| 119 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 1), kExprI64Const), \ |
| 120 static_cast<byte>(static_cast<int64_t>(val) & MASK_7) |
| 121 #define WASM_I64V_2(val) \ |
| 122 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 2), kExprI64Const), \ |
| 123 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 124 static_cast<byte>((static_cast<int64_t>(val) >> 7) & MASK_7) |
| 125 #define WASM_I64V_3(val) \ |
| 126 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 3), kExprI64Const), \ |
| 127 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 128 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 129 static_cast<byte>((static_cast<int64_t>(val) >> 14) & MASK_7) |
| 130 #define WASM_I64V_4(val) \ |
| 131 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 4), kExprI64Const), \ |
| 132 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 133 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 134 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 135 static_cast<byte>((static_cast<int64_t>(val) >> 21) & MASK_7) |
| 136 #define WASM_I64V_5(val) \ |
| 137 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 5), kExprI64Const), \ |
| 138 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 139 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 140 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 141 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 142 static_cast<byte>((static_cast<int64_t>(val) >> 28) & MASK_7) |
| 143 #define WASM_I64V_6(val) \ |
| 144 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 6), kExprI64Const), \ |
| 145 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 146 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 147 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 148 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 149 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 150 static_cast<byte>((static_cast<int64_t>(val) >> 35) & MASK_7) |
| 151 #define WASM_I64V_7(val) \ |
| 152 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 7), kExprI64Const), \ |
| 153 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 154 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 155 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 156 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 157 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 158 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 159 static_cast<byte>((static_cast<int64_t>(val) >> 42) & MASK_7) |
| 160 #define WASM_I64V_8(val) \ |
| 161 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 8), kExprI64Const), \ |
| 162 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 163 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 164 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 165 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 166 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 167 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 168 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 169 static_cast<byte>((static_cast<int64_t>(val) >> 49) & MASK_7) |
| 170 #define WASM_I64V_9(val) \ |
| 171 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 9), kExprI64Const), \ |
| 172 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 173 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 174 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 175 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 176 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 177 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 178 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 179 static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \ |
| 180 static_cast<byte>((static_cast<int64_t>(val) >> 56) & MASK_7) |
| 181 #define WASM_I64V_10(val) \ |
| 182 static_cast<byte>(CheckI64v(static_cast<int64_t>(val), 10), kExprI64Const), \ |
| 183 static_cast<byte>((static_cast<int64_t>(val) & MASK_7) | 0x80), \ |
| 184 static_cast<byte>(((static_cast<int64_t>(val) >> 7) & MASK_7) | 0x80), \ |
| 185 static_cast<byte>(((static_cast<int64_t>(val) >> 14) & MASK_7) | 0x80), \ |
| 186 static_cast<byte>(((static_cast<int64_t>(val) >> 21) & MASK_7) | 0x80), \ |
| 187 static_cast<byte>(((static_cast<int64_t>(val) >> 28) & MASK_7) | 0x80), \ |
| 188 static_cast<byte>(((static_cast<int64_t>(val) >> 35) & MASK_7) | 0x80), \ |
| 189 static_cast<byte>(((static_cast<int64_t>(val) >> 42) & MASK_7) | 0x80), \ |
| 190 static_cast<byte>(((static_cast<int64_t>(val) >> 49) & MASK_7) | 0x80), \ |
| 191 static_cast<byte>(((static_cast<int64_t>(val) >> 56) & MASK_7) | 0x80), \ |
| 192 static_cast<byte>((static_cast<int64_t>(val) >> 63) & MASK_7) |
| 193 |
67 #define WASM_F32(val) \ | 194 #define WASM_F32(val) \ |
68 kExprF32Const, \ | 195 kExprF32Const, \ |
69 static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))), \ | 196 static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))), \ |
70 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8), \ | 197 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8), \ |
71 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \ | 198 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \ |
72 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24) | 199 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24) |
73 #define WASM_F64(val) \ | 200 #define WASM_F64(val) \ |
74 kExprF64Const, static_cast<byte>(bit_cast<uint64_t>(val)), \ | 201 kExprF64Const, static_cast<byte>(bit_cast<uint64_t>(val)), \ |
75 static_cast<byte>(bit_cast<uint64_t>(val) >> 8), \ | 202 static_cast<byte>(bit_cast<uint64_t>(val) >> 8), \ |
76 static_cast<byte>(bit_cast<uint64_t>(val) >> 16), \ | 203 static_cast<byte>(bit_cast<uint64_t>(val) >> 16), \ |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 #define U32V_4(x) \ | 423 #define U32V_4(x) \ |
297 static_cast<byte>((x & MASK_7) | 0x80), \ | 424 static_cast<byte>((x & MASK_7) | 0x80), \ |
298 static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ | 425 static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ |
299 static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ | 426 static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ |
300 static_cast<byte>((x >> 21) & MASK_7) | 427 static_cast<byte>((x >> 21) & MASK_7) |
301 #define U32V_5(x) \ | 428 #define U32V_5(x) \ |
302 static_cast<byte>((x & MASK_7) | 0x80), \ | 429 static_cast<byte>((x & MASK_7) | 0x80), \ |
303 static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ | 430 static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ |
304 static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ | 431 static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ |
305 static_cast<byte>(((x >> 21) & MASK_7) | 0x80), \ | 432 static_cast<byte>(((x >> 21) & MASK_7) | 0x80), \ |
306 static_cast<byte>((x >> 28) & 0xF) | 433 static_cast<byte>(((x >> 28) & MASK_7)) |
307 | 434 |
308 #endif // V8_WASM_MACRO_GEN_H_ | 435 #endif // V8_WASM_MACRO_GEN_H_ |
OLD | NEW |