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_DECODER_H_ | 5 #ifndef V8_WASM_DECODER_H_ |
6 #define V8_WASM_DECODER_H_ | 6 #define V8_WASM_DECODER_H_ |
7 | 7 |
8 #include "src/base/compiler-specific.h" | 8 #include "src/base/compiler-specific.h" |
9 #include "src/base/smart-pointers.h" | 9 #include "src/base/smart-pointers.h" |
10 #include "src/flags.h" | 10 #include "src/flags.h" |
11 #include "src/signature.h" | 11 #include "src/signature.h" |
12 #include "src/utils.h" | 12 #include "src/utils.h" |
13 #include "src/wasm/wasm-result.h" | 13 #include "src/wasm/wasm-result.h" |
14 #include "src/zone-containers.h" | 14 #include "src/zone-containers.h" |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace internal { | 17 namespace internal { |
18 namespace wasm { | 18 namespace wasm { |
19 | 19 |
20 #if DEBUG | 20 #if DEBUG |
21 #define TRACE(...) \ | 21 #define TRACE(...) \ |
22 do { \ | 22 do { \ |
23 if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \ | 23 if (FLAG_trace_wasm_decoder) PrintF(__VA_ARGS__); \ |
24 } while (false) | 24 } while (false) |
25 #else | 25 #else |
26 #define TRACE(...) | 26 #define TRACE(...) |
27 #endif | 27 #endif |
28 | 28 |
29 #if !(V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 || V8_TARGET_ARCH_ARM) | |
30 #define UNALIGNED_ACCESS_OK 1 | |
31 #else | |
32 #define UNALIGNED_ACCESS_OK 0 | |
33 #endif | |
34 | |
35 // A helper utility to decode bytes, integers, fields, varints, etc, from | 29 // A helper utility to decode bytes, integers, fields, varints, etc, from |
36 // a buffer of bytes. | 30 // a buffer of bytes. |
37 class Decoder { | 31 class Decoder { |
38 public: | 32 public: |
39 Decoder(const byte* start, const byte* end) | 33 Decoder(const byte* start, const byte* end) |
40 : start_(start), | 34 : start_(start), |
41 pc_(start), | 35 pc_(start), |
42 limit_(end), | 36 limit_(end), |
43 end_(end), | 37 end_(end), |
44 error_pc_(nullptr), | 38 error_pc_(nullptr), |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 int shift = 64 - 7 * *length; | 109 int shift = 64 - 7 * *length; |
116 // Perform sign extension. | 110 // Perform sign extension. |
117 return bit_cast<int64_t>(result << shift) >> shift; | 111 return bit_cast<int64_t>(result << shift) >> shift; |
118 } | 112 } |
119 return 0; | 113 return 0; |
120 } | 114 } |
121 | 115 |
122 // Reads a single 16-bit unsigned integer (little endian). | 116 // Reads a single 16-bit unsigned integer (little endian). |
123 inline uint16_t read_u16(const byte* ptr) { | 117 inline uint16_t read_u16(const byte* ptr) { |
124 DCHECK(ptr >= start_ && (ptr + 2) <= end_); | 118 DCHECK(ptr >= start_ && (ptr + 2) <= end_); |
125 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK | 119 return ReadLittleEndianValue<uint16_t>(ptr); |
126 return *reinterpret_cast<const uint16_t*>(ptr); | |
127 #else | |
128 uint16_t b0 = ptr[0]; | |
129 uint16_t b1 = ptr[1]; | |
130 return (b1 << 8) | b0; | |
131 #endif | |
132 } | 120 } |
133 | 121 |
134 // Reads a single 32-bit unsigned integer (little endian). | 122 // Reads a single 32-bit unsigned integer (little endian). |
135 inline uint32_t read_u32(const byte* ptr) { | 123 inline uint32_t read_u32(const byte* ptr) { |
136 DCHECK(ptr >= start_ && (ptr + 4) <= end_); | 124 DCHECK(ptr >= start_ && (ptr + 4) <= end_); |
137 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK | 125 return ReadLittleEndianValue<uint32_t>(ptr); |
138 return *reinterpret_cast<const uint32_t*>(ptr); | |
139 #else | |
140 uint32_t b0 = ptr[0]; | |
141 uint32_t b1 = ptr[1]; | |
142 uint32_t b2 = ptr[2]; | |
143 uint32_t b3 = ptr[3]; | |
144 return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; | |
145 #endif | |
146 } | 126 } |
147 | 127 |
148 // Reads a single 64-bit unsigned integer (little endian). | 128 // Reads a single 64-bit unsigned integer (little endian). |
149 inline uint64_t read_u64(const byte* ptr) { | 129 inline uint64_t read_u64(const byte* ptr) { |
150 DCHECK(ptr >= start_ && (ptr + 8) <= end_); | 130 DCHECK(ptr >= start_ && (ptr + 8) <= end_); |
151 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK | 131 return ReadLittleEndianValue<uint64_t>(ptr); |
152 return *reinterpret_cast<const uint64_t*>(ptr); | |
153 #else | |
154 uint32_t b0 = ptr[0]; | |
155 uint32_t b1 = ptr[1]; | |
156 uint32_t b2 = ptr[2]; | |
157 uint32_t b3 = ptr[3]; | |
158 uint32_t low = (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; | |
159 uint32_t b4 = ptr[4]; | |
160 uint32_t b5 = ptr[5]; | |
161 uint32_t b6 = ptr[6]; | |
162 uint32_t b7 = ptr[7]; | |
163 uint64_t high = (b7 << 24) | (b6 << 16) | (b5 << 8) | b4; | |
164 return (high << 32) | low; | |
165 #endif | |
166 } | 132 } |
167 | 133 |
168 // Reads a 8-bit unsigned integer (byte) and advances {pc_}. | 134 // Reads a 8-bit unsigned integer (byte) and advances {pc_}. |
169 uint8_t consume_u8(const char* name = nullptr) { | 135 uint8_t consume_u8(const char* name = nullptr) { |
170 TRACE(" +%d %-20s: ", static_cast<int>(pc_ - start_), | 136 TRACE(" +%d %-20s: ", static_cast<int>(pc_ - start_), |
171 name ? name : "uint8_t"); | 137 name ? name : "uint8_t"); |
172 if (checkAvailable(1)) { | 138 if (checkAvailable(1)) { |
173 byte val = *(pc_++); | 139 byte val = *(pc_++); |
174 TRACE("%02x = %d\n", val, val); | 140 TRACE("%02x = %d\n", val, val); |
175 return val; | 141 return val; |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 return result; | 368 return result; |
403 } | 369 } |
404 }; | 370 }; |
405 | 371 |
406 #undef TRACE | 372 #undef TRACE |
407 } // namespace wasm | 373 } // namespace wasm |
408 } // namespace internal | 374 } // namespace internal |
409 } // namespace v8 | 375 } // namespace v8 |
410 | 376 |
411 #endif // V8_WASM_DECODER_H_ | 377 #endif // V8_WASM_DECODER_H_ |
OLD | NEW |