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 const std::vector<uint8_t>& out_index = UnsignedLEB128From(index); | 20 const std::vector<uint8_t>& out_index = UnsignedLEB128From(index); |
21 std::vector<uint8_t> code; | 21 std::vector<uint8_t> code; |
22 code.push_back(kExprGetLocal); | 22 code.push_back(kExprGetLocal); |
23 for (size_t i = 0; i < out_index.size(); i++) { | 23 for (size_t i = 0; i < out_index.size(); i++) { |
24 code.push_back(out_index.at(i)); | 24 code.push_back(out_index.at(i)); |
25 } | 25 } |
26 uint32_t local_indices[] = {1}; | 26 uint32_t local_indices[] = {1}; |
27 f->EmitCode(code.data(), static_cast<uint32_t>(code.size()), local_indices, | 27 f->EmitCode(&code[0], static_cast<uint32_t>(code.size()), local_indices, 1); |
28 1); | |
29 } | 28 } |
30 | 29 |
31 void CheckReadValue(uint8_t* leb_value, uint32_t expected_result, | 30 void CheckReadValue(uint8_t* leb_value, uint32_t expected_result, |
32 int expected_length, | 31 int expected_length, |
33 ReadUnsignedLEB128ErrorCode expected_error_code) { | 32 ReadUnsignedLEB128ErrorCode expected_error_code) { |
34 int length; | 33 int length; |
35 uint32_t result; | 34 uint32_t result; |
36 ReadUnsignedLEB128ErrorCode error_code = | 35 ReadUnsignedLEB128ErrorCode error_code = |
37 ReadUnsignedLEB128Operand(leb_value, leb_value + 5, &length, &result); | 36 ReadUnsignedLEB128Operand(leb_value, leb_value + 5, &length, &result); |
38 CHECK_EQ(error_code, expected_error_code); | 37 CHECK_EQ(error_code, expected_error_code); |
39 if (error_code == 0) { | 38 if (error_code == 0) { |
40 CHECK_EQ(result, expected_result); | 39 CHECK_EQ(result, expected_result); |
41 CHECK_EQ(length, expected_length); | 40 CHECK_EQ(length, expected_length); |
42 } | 41 } |
43 } | 42 } |
44 | 43 |
45 void CheckWriteValue(uint32_t input, int length, uint8_t* vals) { | 44 void CheckWriteValue(uint32_t input, int length, uint8_t* vals) { |
46 const std::vector<uint8_t> result = UnsignedLEB128From(input); | 45 const std::vector<uint8_t> result = UnsignedLEB128From(input); |
47 CHECK_EQ(result.size(), length); | 46 CHECK_EQ(result.size(), length); |
48 for (size_t i = 0; i < length; i++) { | 47 for (int i = 0; i < length; i++) { |
49 CHECK_EQ(result.at(i), vals[i]); | 48 CHECK_EQ(result.at(i), vals[i]); |
50 } | 49 } |
51 } | 50 } |
52 }; | 51 }; |
53 | 52 |
54 | 53 |
55 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { | 54 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { |
56 Zone zone; | 55 Zone zone; |
57 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 56 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
58 uint16_t f_index = builder->AddFunction(); | 57 uint16_t f_index = builder->AddFunction(); |
(...skipping 17 matching lines...) Expand all Loading... |
76 function->EmitCode(code, sizeof(code), local_indices, 1); | 75 function->EmitCode(code, sizeof(code), local_indices, 1); |
77 code[1] = static_cast<uint8_t>(local_int64); | 76 code[1] = static_cast<uint8_t>(local_int64); |
78 function->EmitCode(code, sizeof(code), local_indices, 1); | 77 function->EmitCode(code, sizeof(code), local_indices, 1); |
79 code[1] = static_cast<uint8_t>(local_float32); | 78 code[1] = static_cast<uint8_t>(local_float32); |
80 function->EmitCode(code, sizeof(code), local_indices, 1); | 79 function->EmitCode(code, sizeof(code), local_indices, 1); |
81 code[1] = static_cast<uint8_t>(local_float64); | 80 code[1] = static_cast<uint8_t>(local_float64); |
82 function->EmitCode(code, sizeof(code), local_indices, 1); | 81 function->EmitCode(code, sizeof(code), local_indices, 1); |
83 | 82 |
84 WasmFunctionEncoder* f = function->Build(&zone, builder); | 83 WasmFunctionEncoder* f = function->Build(&zone, builder); |
85 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); | 84 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); |
86 byte* buffer = buffer_vector.data(); | 85 byte* buffer = &buffer_vector[0]; |
87 byte* header = buffer; | 86 byte* header = buffer; |
88 byte* body = buffer + f->HeaderSize(); | 87 byte* body = buffer + f->HeaderSize(); |
89 f->Serialize(buffer, &header, &body); | 88 f->Serialize(buffer, &header, &body); |
90 for (size_t i = 0; i < 7; i++) { | 89 for (size_t i = 0; i < 7; i++) { |
91 CHECK_EQ(i, static_cast<size_t>(*(buffer + 2 * i + f->HeaderSize() + 1))); | 90 CHECK_EQ(i, static_cast<size_t>(*(buffer + 2 * i + f->HeaderSize() + 1))); |
92 } | 91 } |
93 } | 92 } |
94 | 93 |
95 | 94 |
96 TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) { | 95 TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) { |
97 Zone zone; | 96 Zone zone; |
98 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 97 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
99 uint16_t f_index = builder->AddFunction(); | 98 uint16_t f_index = builder->AddFunction(); |
100 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | 99 WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
101 for (size_t i = 0; i < 128; i++) { | 100 for (size_t i = 0; i < 128; i++) { |
102 AddLocal(function, kAstF32); | 101 AddLocal(function, kAstF32); |
103 } | 102 } |
104 AddLocal(function, kAstI32); | 103 AddLocal(function, kAstI32); |
105 | 104 |
106 WasmFunctionEncoder* f = function->Build(&zone, builder); | 105 WasmFunctionEncoder* f = function->Build(&zone, builder); |
107 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); | 106 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); |
108 byte* buffer = buffer_vector.data(); | 107 byte* buffer = &buffer_vector[0]; |
109 byte* header = buffer; | 108 byte* header = buffer; |
110 byte* body = buffer + f->HeaderSize(); | 109 byte* body = buffer + f->HeaderSize(); |
111 f->Serialize(buffer, &header, &body); | 110 f->Serialize(buffer, &header, &body); |
112 body = buffer + f->HeaderSize(); | 111 body = buffer + f->HeaderSize(); |
113 for (size_t i = 0; i < 127; i++) { | 112 for (size_t i = 0; i < 127; i++) { |
114 CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * i))); | 113 CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * i))); |
115 CHECK_EQ(i + 1, static_cast<size_t>(*(body + 2 * i + 1))); | 114 CHECK_EQ(i + 1, static_cast<size_t>(*(body + 2 * i + 1))); |
116 } | 115 } |
117 CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * 127))); | 116 CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * 127))); |
118 CHECK_EQ(0x80, static_cast<size_t>(*(body + 2 * 127 + 1))); | 117 CHECK_EQ(0x80, static_cast<size_t>(*(body + 2 * 127 + 1))); |
(...skipping 24 matching lines...) Expand all Loading... |
143 leb_value[0] = 0x80; | 142 leb_value[0] = 0x80; |
144 leb_value[1] = 0x80; | 143 leb_value[1] = 0x80; |
145 leb_value[2] = 0x80; | 144 leb_value[2] = 0x80; |
146 leb_value[3] = 0x80; | 145 leb_value[3] = 0x80; |
147 leb_value[4] = 0x80; | 146 leb_value[4] = 0x80; |
148 CheckReadValue(leb_value, -1, -1, kInvalidLEB128); | 147 CheckReadValue(leb_value, -1, -1, kInvalidLEB128); |
149 } | 148 } |
150 } // namespace wasm | 149 } // namespace wasm |
151 } // namespace internal | 150 } // namespace internal |
152 } // namespace v8 | 151 } // namespace v8 |
OLD | NEW |