blob: 7ba244d8586683f2cde82df15608403303a8a3c2 [file] [log] [blame]
Ben Murdochc5610432016-08-08 18:44:38 +01001// Copyright 2016 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_LEB_HELPER_H_
6#define V8_WASM_LEB_HELPER_H_
7
8namespace v8 {
9namespace internal {
10namespace wasm {
11
12class LEBHelper {
13 public:
14 // Write a 32-bit unsigned LEB to {dest}, updating {dest} to point after
15 // the last uint8_t written. No safety checks.
16 static void write_u32v(uint8_t** dest, uint32_t val) {
17 while (val >= 0x80) {
18 *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
19 val >>= 7;
20 }
21 *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
22 }
23
24 // Write a 32-bit signed LEB to {dest}, updating {dest} to point after
25 // the last uint8_t written. No safety checks.
26 static void write_i32v(uint8_t** dest, int32_t val) {
27 if (val >= 0) {
28 while (val >= 0x40) { // prevent sign extension.
29 *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
30 val >>= 7;
31 }
32 *((*dest)++) = static_cast<uint8_t>(val & 0xFF);
33 } else {
34 while ((val >> 6) != -1) {
35 *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
36 val >>= 7;
37 }
38 *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
39 }
40 }
41
42 // Write a 64-bit unsigned LEB to {dest}, updating {dest} to point after
43 // the last uint8_t written. No safety checks.
44 static void write_u64v(uint8_t** dest, uint64_t val) {
45 while (val >= 0x80) {
46 *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
47 val >>= 7;
48 }
49 *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
50 }
51
52 // Write a 64-bit signed LEB to {dest}, updating {dest} to point after
53 // the last uint8_t written. No safety checks.
54 static void write_i64v(uint8_t** dest, int64_t val) {
55 if (val >= 0) {
56 while (val >= 0x40) { // prevent sign extension.
57 *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
58 val >>= 7;
59 }
60 *((*dest)++) = static_cast<uint8_t>(val & 0xFF);
61 } else {
62 while ((val >> 6) != -1) {
63 *((*dest)++) = static_cast<uint8_t>(0x80 | (val & 0x7F));
64 val >>= 7;
65 }
66 *((*dest)++) = static_cast<uint8_t>(val & 0x7F);
67 }
68 }
69
70 // TODO(titzer): move core logic for decoding LEBs from decoder.h to here.
71
72 // Compute the size of {val} if emitted as an LEB32.
73 static inline size_t sizeof_u32v(size_t val) {
74 size_t size = 0;
75 do {
76 size++;
77 val = val >> 7;
78 } while (val > 0);
79 return size;
80 }
81
82 // Compute the size of {val} if emitted as an LEB32.
83 static inline size_t sizeof_i32v(int32_t val) {
84 size_t size = 1;
85 if (val >= 0) {
86 while (val >= 0x40) { // prevent sign extension.
87 size++;
88 val >>= 7;
89 }
90 } else {
91 while ((val >> 6) != -1) {
92 size++;
93 val >>= 7;
94 }
95 }
96 return size;
97 }
98
99 // Compute the size of {val} if emitted as an unsigned LEB64.
100 static inline size_t sizeof_u64v(uint64_t val) {
101 size_t size = 0;
102 do {
103 size++;
104 val = val >> 7;
105 } while (val > 0);
106 return size;
107 }
108
109 // Compute the size of {val} if emitted as a signed LEB64.
110 static inline size_t sizeof_i64v(int64_t val) {
111 size_t size = 1;
112 if (val >= 0) {
113 while (val >= 0x40) { // prevent sign extension.
114 size++;
115 val >>= 7;
116 }
117 } else {
118 while ((val >> 6) != -1) {
119 size++;
120 val >>= 7;
121 }
122 }
123 return size;
124 }
125};
126
127} // namespace wasm
128} // namespace internal
129} // namespace v8
130
131#endif // V8_WASM_LEB_HELPER_H_