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" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 namespace wasm { | 14 namespace wasm { |
15 | 15 |
16 class EncoderTest : public TestWithZone { | 16 class EncoderTest : public TestWithZone { |
17 protected: | 17 protected: |
18 void AddLocal(WasmFunctionBuilder* f, LocalType type) { | 18 void AddLocal(WasmFunctionBuilder* f, LocalType type) { |
19 uint16_t index = f->AddLocal(type); | 19 uint16_t index = f->AddLocal(type); |
20 f->EmitGetLocal(index); | 20 f->EmitGetLocal(index); |
21 } | 21 } |
22 }; | 22 }; |
23 | 23 |
24 | |
25 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { | 24 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { |
26 base::AccountingAllocator allocator; | 25 base::AccountingAllocator allocator; |
27 Zone zone(&allocator); | 26 Zone zone(&allocator); |
28 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 27 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
29 uint16_t f_index = builder->AddFunction(); | 28 uint16_t f_index = builder->AddFunction(); |
30 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | 29 WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
31 uint16_t local_f32 = function->AddLocal(kAstF32); | 30 uint16_t local_f32 = function->AddLocal(kAstF32); |
32 uint16_t param_float32 = function->AddParam(kAstF32); | 31 uint16_t param_float32 = function->AddParam(kAstF32); |
33 uint16_t local_i32 = function->AddLocal(kAstI32); | 32 uint16_t local_i32 = function->AddLocal(kAstI32); |
34 uint16_t local_f64 = function->AddLocal(kAstF64); | 33 uint16_t local_f64 = function->AddLocal(kAstF64); |
(...skipping 18 matching lines...) Expand all Loading... |
53 function->EmitCode(code, sizeof(code), local_indices, 1); | 52 function->EmitCode(code, sizeof(code), local_indices, 1); |
54 | 53 |
55 WasmFunctionEncoder* f = function->Build(&zone, builder); | 54 WasmFunctionEncoder* f = function->Build(&zone, builder); |
56 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); | 55 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); |
57 byte* buffer = &buffer_vector[0]; | 56 byte* buffer = &buffer_vector[0]; |
58 byte* header = buffer; | 57 byte* header = buffer; |
59 byte* body = buffer + f->HeaderSize(); | 58 byte* body = buffer + f->HeaderSize(); |
60 f->Serialize(buffer, &header, &body); | 59 f->Serialize(buffer, &header, &body); |
61 } | 60 } |
62 | 61 |
63 | |
64 TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) { | 62 TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) { |
65 base::AccountingAllocator allocator; | 63 base::AccountingAllocator allocator; |
66 Zone zone(&allocator); | 64 Zone zone(&allocator); |
67 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 65 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
68 uint16_t f_index = builder->AddFunction(); | 66 uint16_t f_index = builder->AddFunction(); |
69 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | 67 WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
70 for (size_t i = 0; i < 128; i++) { | 68 for (size_t i = 0; i < 128; i++) { |
71 AddLocal(function, kAstF32); | 69 AddLocal(function, kAstF32); |
72 } | 70 } |
73 AddLocal(function, kAstI32); | 71 AddLocal(function, kAstI32); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 for (int i = 128; i < 200; ++i) { | 152 for (int i = 128; i < 200; ++i) { |
155 CHECK_EQ(body[offset++], kExprGetLocal); | 153 CHECK_EQ(body[offset++], kExprGetLocal); |
156 CHECK_EQ(body[offset++], (i & 0x7f) | 0x80); | 154 CHECK_EQ(body[offset++], (i & 0x7f) | 0x80); |
157 CHECK_EQ(body[offset++], (i >> 7) & 0x7f); | 155 CHECK_EQ(body[offset++], (i >> 7) & 0x7f); |
158 } | 156 } |
159 CHECK_EQ(offset, 479); | 157 CHECK_EQ(offset, 479); |
160 } | 158 } |
161 } // namespace wasm | 159 } // namespace wasm |
162 } // namespace internal | 160 } // namespace internal |
163 } // namespace v8 | 161 } // namespace v8 |
OLD | NEW |