Chromium Code Reviews| 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 <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "src/base/compiler-specific.h" | 10 #include "src/base/compiler-specific.h" |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 name ? name : "uint32_t"); | 166 name ? name : "uint32_t"); |
| 167 if (checkAvailable(4)) { | 167 if (checkAvailable(4)) { |
| 168 uint32_t val = read_u32(pc_); | 168 uint32_t val = read_u32(pc_); |
| 169 TRACE("%02x %02x %02x %02x = %u\n", pc_[0], pc_[1], pc_[2], pc_[3], val); | 169 TRACE("%02x %02x %02x %02x = %u\n", pc_[0], pc_[1], pc_[2], pc_[3], val); |
| 170 pc_ += 4; | 170 pc_ += 4; |
| 171 return val; | 171 return val; |
| 172 } | 172 } |
| 173 return traceOffEnd<uint32_t>(); | 173 return traceOffEnd<uint32_t>(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 // Reads a LEB128 variable-length 32-bit integer and advances {pc_}. | 176 // Reads a LEB128 variable-length unsigned 32-bit integer and advances {pc_}. |
| 177 uint32_t consume_u32v(const char* name = nullptr) { | 177 uint32_t consume_u32v(const char* name = nullptr) { |
| 178 TRACE(" +%d %-20s: ", static_cast<int>(pc_ - start_), | 178 return consume_leb<uint32_t, false>(name); |
| 179 name ? name : "varint"); | 179 } |
| 180 if (checkAvailable(1)) { | |
| 181 const byte* pos = pc_; | |
| 182 const byte* end = pc_ + 5; | |
| 183 if (end > limit_) end = limit_; | |
| 184 | 180 |
| 185 uint32_t result = 0; | 181 // Reads a LEB128 variable-length signed 32-bit integer and advances {pc_}. |
| 186 int shift = 0; | 182 int32_t consume_i32v(const char* name = nullptr) { |
|
ahaas
2016/10/11 07:34:23
Can you add tests for this function?
| |
| 187 byte b = 0; | 183 return consume_leb<int32_t, true>(name); |
| 188 while (pc_ < end) { | |
| 189 b = *pc_++; | |
| 190 TRACE("%02x ", b); | |
| 191 result = result | ((b & 0x7F) << shift); | |
| 192 if ((b & 0x80) == 0) break; | |
| 193 shift += 7; | |
| 194 } | |
| 195 | |
| 196 int length = static_cast<int>(pc_ - pos); | |
| 197 if (pc_ == end && (b & 0x80)) { | |
| 198 error(pc_ - 1, "varint too large"); | |
| 199 } else if (length == 0) { | |
| 200 error(pc_, "varint of length 0"); | |
| 201 } else { | |
| 202 TRACE("= %u\n", result); | |
| 203 } | |
| 204 return result; | |
| 205 } | |
| 206 return traceOffEnd<uint32_t>(); | |
| 207 } | 184 } |
| 208 | 185 |
| 209 // Consume {size} bytes and send them to the bit bucket, advancing {pc_}. | 186 // Consume {size} bytes and send them to the bit bucket, advancing {pc_}. |
| 210 void consume_bytes(int size) { | |
| 211 TRACE(" +%d %-20s: %d bytes\n", static_cast<int>(pc_ - start_), "skip", | |
| 212 size); | |
| 213 if (checkAvailable(size)) { | |
| 214 pc_ += size; | |
| 215 } else { | |
| 216 pc_ = limit_; | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 // Consume {size} bytes and send them to the bit bucket, advancing {pc_}. | |
| 221 void consume_bytes(uint32_t size, const char* name = "skip") { | 187 void consume_bytes(uint32_t size, const char* name = "skip") { |
| 222 TRACE(" +%d %-20s: %d bytes\n", static_cast<int>(pc_ - start_), name, | 188 TRACE(" +%d %-20s: %d bytes\n", static_cast<int>(pc_ - start_), name, |
| 223 size); | 189 size); |
| 224 if (checkAvailable(size)) { | 190 if (checkAvailable(size)) { |
| 225 pc_ += size; | 191 pc_ += size; |
| 226 } else { | 192 } else { |
| 227 pc_ = limit_; | 193 pc_ = limit_; |
| 228 } | 194 } |
| 229 } | 195 } |
| 230 | 196 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 error(base, ptr, "extra bits in varint"); | 342 error(base, ptr, "extra bits in varint"); |
| 377 return 0; | 343 return 0; |
| 378 } | 344 } |
| 379 if ((b & 0x80) != 0) { | 345 if ((b & 0x80) != 0) { |
| 380 error(base, ptr, "%s", msg); | 346 error(base, ptr, "%s", msg); |
| 381 return 0; | 347 return 0; |
| 382 } | 348 } |
| 383 } | 349 } |
| 384 return result; | 350 return result; |
| 385 } | 351 } |
| 352 | |
| 353 template <typename IntType, bool is_signed> | |
|
ahaas
2016/10/11 07:34:23
Can you measure a performance difference between i
| |
| 354 IntType consume_leb(const char* name = nullptr) { | |
| 355 TRACE(" +%d %-20s: ", static_cast<int>(pc_ - start_), | |
| 356 name ? name : "varint"); | |
| 357 if (checkAvailable(1)) { | |
| 358 constexpr int kMaxLength = (sizeof(IntType) * 8 + 6) / 7; | |
| 359 const byte* pos = pc_; | |
| 360 const byte* end = pc_ + kMaxLength; | |
| 361 if (end > limit_) end = limit_; | |
| 362 | |
| 363 IntType result = 0; | |
| 364 int shift = 0; | |
| 365 byte b = 0; | |
| 366 while (pc_ < end) { | |
| 367 b = *pc_++; | |
| 368 TRACE("%02x ", b); | |
| 369 result = result | (static_cast<IntType>(b & 0x7F) << shift); | |
| 370 shift += 7; | |
| 371 if ((b & 0x80) == 0) break; | |
| 372 } | |
| 373 | |
| 374 int length = static_cast<int>(pc_ - pos); | |
| 375 if (pc_ == end && (b & 0x80)) { | |
| 376 error(pc_ - 1, "varint too large"); | |
| 377 } else if (length == 0) { | |
| 378 error(pc_, "varint of length 0"); | |
| 379 } else if (is_signed) { | |
| 380 if (length > 0 && length < kMaxLength) { | |
| 381 int sign_ext_shift = 8 * sizeof(IntType) - shift; | |
| 382 // Perform sign extension. | |
| 383 result = (result << sign_ext_shift) >> sign_ext_shift; | |
| 384 } | |
| 385 TRACE("= %" PRIi64 "\n", static_cast<int64_t>(result)); | |
| 386 } else { | |
| 387 TRACE("= %" PRIu64 "\n", static_cast<uint64_t>(result)); | |
| 388 } | |
| 389 return result; | |
| 390 } | |
| 391 return traceOffEnd<uint32_t>(); | |
| 392 } | |
| 386 }; | 393 }; |
| 387 | 394 |
| 388 #undef TRACE | 395 #undef TRACE |
| 389 } // namespace wasm | 396 } // namespace wasm |
| 390 } // namespace internal | 397 } // namespace internal |
| 391 } // namespace v8 | 398 } // namespace v8 |
| 392 | 399 |
| 393 #endif // V8_WASM_DECODER_H_ | 400 #endif // V8_WASM_DECODER_H_ |
| OLD | NEW |