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 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "src/wasm/ast-decoder.h" | 9 #include "src/wasm/ast-decoder.h" |
10 #include "src/wasm/encoder.h" | 10 #include "src/wasm/encoder.h" |
(...skipping 90 matching lines...) Loading... |
101 | 101 |
102 WasmFunctionEncoder* f = function->Build(&zone, builder); | 102 WasmFunctionEncoder* f = function->Build(&zone, builder); |
103 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); | 103 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); |
104 byte* buffer = &buffer_vector[0]; | 104 byte* buffer = &buffer_vector[0]; |
105 byte* header = buffer; | 105 byte* header = buffer; |
106 byte* body = buffer + f->HeaderSize(); | 106 byte* body = buffer + f->HeaderSize(); |
107 f->Serialize(buffer, &header, &body); | 107 f->Serialize(buffer, &header, &body); |
108 body = buffer + f->HeaderSize(); | 108 body = buffer + f->HeaderSize(); |
109 } | 109 } |
110 | 110 |
| 111 TEST_F(EncoderTest, Function_Builder_Block_Variable_Width) { |
| 112 Zone zone; |
| 113 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 114 uint16_t f_index = builder->AddFunction(); |
| 115 WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
| 116 function->EmitWithVarInt(kExprBlock, 200); |
| 117 for (int i = 0; i < 200; ++i) { |
| 118 function->Emit(kExprNop); |
| 119 } |
| 120 |
| 121 WasmFunctionEncoder* f = function->Build(&zone, builder); |
| 122 CHECK_EQ(f->BodySize(), 204); |
| 123 } |
| 124 |
| 125 TEST_F(EncoderTest, Function_Builder_EmitEditableVarIntImmediate) { |
| 126 Zone zone; |
| 127 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 128 uint16_t f_index = builder->AddFunction(); |
| 129 WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
| 130 function->Emit(kExprLoop); |
| 131 uint32_t offset = function->EmitEditableVarIntImmediate(); |
| 132 for (int i = 0; i < 200; ++i) { |
| 133 function->Emit(kExprNop); |
| 134 } |
| 135 function->EditVarIntImmediate(offset, 200); |
| 136 |
| 137 WasmFunctionEncoder* f = function->Build(&zone, builder); |
| 138 CHECK_EQ(f->BodySize(), 204); |
| 139 } |
| 140 |
| 141 TEST_F(EncoderTest, Function_Builder_EmitEditableVarIntImmediate_Locals) { |
| 142 Zone zone; |
| 143 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 144 uint16_t f_index = builder->AddFunction(); |
| 145 WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
| 146 function->Emit(kExprBlock); |
| 147 uint32_t offset = function->EmitEditableVarIntImmediate(); |
| 148 for (int i = 0; i < 200; ++i) { |
| 149 AddLocal(function, kAstI32); |
| 150 } |
| 151 function->EditVarIntImmediate(offset, 200); |
| 152 |
| 153 WasmFunctionEncoder* f = function->Build(&zone, builder); |
| 154 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); |
| 155 byte* buffer = &buffer_vector[0]; |
| 156 byte* header = buffer; |
| 157 byte* body = buffer + f->HeaderSize(); |
| 158 f->Serialize(buffer, &header, &body); |
| 159 body = buffer + f->HeaderSize(); |
| 160 |
| 161 CHECK_EQ(f->BodySize(), 479); |
| 162 const uint8_t varint200_low = (200 & 0x7f) | 0x80; |
| 163 const uint8_t varint200_high = (200 >> 7) & 0x7f; |
| 164 offset = 0; |
| 165 CHECK_EQ(body[offset++], 1); // Local decl count. |
| 166 CHECK_EQ(body[offset++], varint200_low); |
| 167 CHECK_EQ(body[offset++], varint200_high); |
| 168 CHECK_EQ(body[offset++], kLocalI32); |
| 169 CHECK_EQ(body[offset++], kExprBlock); |
| 170 CHECK_EQ(body[offset++], varint200_low); |
| 171 CHECK_EQ(body[offset++], varint200_high); |
| 172 // GetLocal with one-byte indices. |
| 173 for (int i = 0; i <= 127; ++i) { |
| 174 CHECK_EQ(body[offset++], kExprGetLocal); |
| 175 CHECK_EQ(body[offset++], i); |
| 176 } |
| 177 // GetLocal with two-byte indices. |
| 178 for (int i = 128; i < 200; ++i) { |
| 179 CHECK_EQ(body[offset++], kExprGetLocal); |
| 180 CHECK_EQ(body[offset++], (i & 0x7f) | 0x80); |
| 181 CHECK_EQ(body[offset++], (i >> 7) & 0x7f); |
| 182 } |
| 183 CHECK_EQ(offset, 479); |
| 184 } |
111 | 185 |
112 TEST_F(EncoderTest, LEB_Functions) { | 186 TEST_F(EncoderTest, LEB_Functions) { |
113 byte leb_value[5] = {0, 0, 0, 0, 0}; | 187 byte leb_value[5] = {0, 0, 0, 0, 0}; |
114 CheckReadValue(leb_value, 0, 1, kNoError); | 188 CheckReadValue(leb_value, 0, 1, kNoError); |
115 CheckWriteValue(0, 1, leb_value); | 189 CheckWriteValue(0, 1, leb_value); |
116 leb_value[0] = 23; | 190 leb_value[0] = 23; |
117 CheckReadValue(leb_value, 23, 1, kNoError); | 191 CheckReadValue(leb_value, 23, 1, kNoError); |
118 CheckWriteValue(23, 1, leb_value); | 192 CheckWriteValue(23, 1, leb_value); |
119 leb_value[0] = 0x80; | 193 leb_value[0] = 0x80; |
120 leb_value[1] = 0x01; | 194 leb_value[1] = 0x01; |
121 CheckReadValue(leb_value, 128, 2, kNoError); | 195 CheckReadValue(leb_value, 128, 2, kNoError); |
122 CheckWriteValue(128, 2, leb_value); | 196 CheckWriteValue(128, 2, leb_value); |
123 leb_value[0] = 0x80; | 197 leb_value[0] = 0x80; |
124 leb_value[1] = 0x80; | 198 leb_value[1] = 0x80; |
125 leb_value[2] = 0x80; | 199 leb_value[2] = 0x80; |
126 leb_value[3] = 0x80; | 200 leb_value[3] = 0x80; |
127 leb_value[4] = 0x01; | 201 leb_value[4] = 0x01; |
128 CheckReadValue(leb_value, 0x10000000, 5, kNoError); | 202 CheckReadValue(leb_value, 0x10000000, 5, kNoError); |
129 CheckWriteValue(0x10000000, 5, leb_value); | 203 CheckWriteValue(0x10000000, 5, leb_value); |
130 leb_value[0] = 0x80; | 204 leb_value[0] = 0x80; |
131 leb_value[1] = 0x80; | 205 leb_value[1] = 0x80; |
132 leb_value[2] = 0x80; | 206 leb_value[2] = 0x80; |
133 leb_value[3] = 0x80; | 207 leb_value[3] = 0x80; |
134 leb_value[4] = 0x80; | 208 leb_value[4] = 0x80; |
135 CheckReadValue(leb_value, -1, -1, kInvalidLEB128); | 209 CheckReadValue(leb_value, -1, -1, kInvalidLEB128); |
136 } | 210 } |
137 } // namespace wasm | 211 } // namespace wasm |
138 } // namespace internal | 212 } // namespace internal |
139 } // namespace v8 | 213 } // namespace v8 |
OLD | NEW |