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 |
29 // A helper utility to decode bytes, integers, fields, varints, etc, from | 35 // A helper utility to decode bytes, integers, fields, varints, etc, from |
30 // a buffer of bytes. | 36 // a buffer of bytes. |
31 class Decoder { | 37 class Decoder { |
32 public: | 38 public: |
33 Decoder(const byte* start, const byte* end) | 39 Decoder(const byte* start, const byte* end) |
34 : start_(start), | 40 : start_(start), |
35 pc_(start), | 41 pc_(start), |
36 limit_(end), | 42 limit_(end), |
37 end_(end), | 43 end_(end), |
38 error_pc_(nullptr), | 44 error_pc_(nullptr), |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 int shift = 64 - 7 * *length; | 115 int shift = 64 - 7 * *length; |
110 // Perform sign extension. | 116 // Perform sign extension. |
111 return bit_cast<int64_t>(result << shift) >> shift; | 117 return bit_cast<int64_t>(result << shift) >> shift; |
112 } | 118 } |
113 return 0; | 119 return 0; |
114 } | 120 } |
115 | 121 |
116 // Reads a single 16-bit unsigned integer (little endian). | 122 // Reads a single 16-bit unsigned integer (little endian). |
117 inline uint16_t read_u16(const byte* ptr) { | 123 inline uint16_t read_u16(const byte* ptr) { |
118 DCHECK(ptr >= start_ && (ptr + 2) <= end_); | 124 DCHECK(ptr >= start_ && (ptr + 2) <= end_); |
119 return ReadLittleEndianValue<uint16_t>(ptr); | 125 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK |
| 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 |
120 } | 132 } |
121 | 133 |
122 // Reads a single 32-bit unsigned integer (little endian). | 134 // Reads a single 32-bit unsigned integer (little endian). |
123 inline uint32_t read_u32(const byte* ptr) { | 135 inline uint32_t read_u32(const byte* ptr) { |
124 DCHECK(ptr >= start_ && (ptr + 4) <= end_); | 136 DCHECK(ptr >= start_ && (ptr + 4) <= end_); |
125 return ReadLittleEndianValue<uint32_t>(ptr); | 137 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK |
| 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 |
126 } | 146 } |
127 | 147 |
128 // Reads a single 64-bit unsigned integer (little endian). | 148 // Reads a single 64-bit unsigned integer (little endian). |
129 inline uint64_t read_u64(const byte* ptr) { | 149 inline uint64_t read_u64(const byte* ptr) { |
130 DCHECK(ptr >= start_ && (ptr + 8) <= end_); | 150 DCHECK(ptr >= start_ && (ptr + 8) <= end_); |
131 return ReadLittleEndianValue<uint64_t>(ptr); | 151 #if V8_TARGET_LITTLE_ENDIAN && UNALIGNED_ACCESS_OK |
| 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 |
132 } | 166 } |
133 | 167 |
134 // Reads a 8-bit unsigned integer (byte) and advances {pc_}. | 168 // Reads a 8-bit unsigned integer (byte) and advances {pc_}. |
135 uint8_t consume_u8(const char* name = nullptr) { | 169 uint8_t consume_u8(const char* name = nullptr) { |
136 TRACE(" +%d %-20s: ", static_cast<int>(pc_ - start_), | 170 TRACE(" +%d %-20s: ", static_cast<int>(pc_ - start_), |
137 name ? name : "uint8_t"); | 171 name ? name : "uint8_t"); |
138 if (checkAvailable(1)) { | 172 if (checkAvailable(1)) { |
139 byte val = *(pc_++); | 173 byte val = *(pc_++); |
140 TRACE("%02x = %d\n", val, val); | 174 TRACE("%02x = %d\n", val, val); |
141 return val; | 175 return val; |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 return result; | 402 return result; |
369 } | 403 } |
370 }; | 404 }; |
371 | 405 |
372 #undef TRACE | 406 #undef TRACE |
373 } // namespace wasm | 407 } // namespace wasm |
374 } // namespace internal | 408 } // namespace internal |
375 } // namespace v8 | 409 } // namespace v8 |
376 | 410 |
377 #endif // V8_WASM_DECODER_H_ | 411 #endif // V8_WASM_DECODER_H_ |
OLD | NEW |