Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: test/unittests/wasm/encoder-unittest.cc

Issue 1504713014: Initial import of v8-native WASM. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "test/unittests/test-utils.h"
6
7 #include "src/v8.h"
8
9 #include "src/wasm/ast-decoder.h"
10 #include "src/wasm/encoder.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace wasm {
15
16 class EncoderTest : public TestWithZone {
17 protected:
18 void AddLocal(WasmFunctionBuilder* f, LocalType type) {
19 uint16_t index = f->AddLocal(type);
20 const std::vector<uint8_t>& out_index = UnsignedLEB128From(index);
21 std::vector<uint8_t> code;
22 code.push_back(kExprGetLocal);
23 for (size_t i = 0; i < out_index.size(); i++) {
24 code.push_back(out_index.at(i));
25 }
26 uint32_t local_indices[] = {1};
27 f->EmitCode(code.data(), static_cast<uint32_t>(code.size()), local_indices,
28 1);
29 }
30
31 void CheckReadValue(uint8_t* leb_value, uint32_t expected_result,
32 int expected_length,
33 ReadUnsignedLEB128ErrorCode expected_error_code) {
34 int length;
35 uint32_t result;
36 ReadUnsignedLEB128ErrorCode error_code =
37 ReadUnsignedLEB128Operand(leb_value, leb_value + 5, &length, &result);
38 CHECK_EQ(error_code, expected_error_code);
39 if (error_code == 0) {
40 CHECK_EQ(result, expected_result);
41 CHECK_EQ(length, expected_length);
42 }
43 }
44
45 void CheckWriteValue(uint32_t input, int length, uint8_t* vals) {
46 const std::vector<uint8_t> result = UnsignedLEB128From(input);
47 CHECK_EQ(result.size(), length);
48 for (size_t i = 0; i < length; i++) {
49 CHECK_EQ(result.at(i), vals[i]);
50 }
51 }
52 };
53
54
55 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) {
56 Zone zone;
57 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
58 uint16_t f_index = builder->AddFunction();
59 WasmFunctionBuilder* function = builder->FunctionAt(f_index);
60 uint16_t local_float32 = function->AddLocal(kAstF32);
61 uint16_t param_float32 = function->AddParam(kAstF32);
62 uint16_t local_int32 = function->AddLocal(kAstI32);
63 uint16_t local_float64 = function->AddLocal(kAstF64);
64 uint16_t local_int64 = function->AddLocal(kAstI64);
65 uint16_t param_int32 = function->AddParam(kAstI32);
66 uint16_t local_int32_2 = function->AddLocal(kAstI32);
67
68 byte code[] = {kExprGetLocal, static_cast<uint8_t>(param_float32)};
69 uint32_t local_indices[] = {1};
70 function->EmitCode(code, sizeof(code), local_indices, 1);
71 code[1] = static_cast<uint8_t>(param_int32);
72 function->EmitCode(code, sizeof(code), local_indices, 1);
73 code[1] = static_cast<uint8_t>(local_int32);
74 function->EmitCode(code, sizeof(code), local_indices, 1);
75 code[1] = static_cast<uint8_t>(local_int32_2);
76 function->EmitCode(code, sizeof(code), local_indices, 1);
77 code[1] = static_cast<uint8_t>(local_int64);
78 function->EmitCode(code, sizeof(code), local_indices, 1);
79 code[1] = static_cast<uint8_t>(local_float32);
80 function->EmitCode(code, sizeof(code), local_indices, 1);
81 code[1] = static_cast<uint8_t>(local_float64);
82 function->EmitCode(code, sizeof(code), local_indices, 1);
83
84 WasmFunctionEncoder* f = function->Build(&zone, builder);
85 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone);
86 byte* buffer = buffer_vector.data();
87 byte* header = buffer;
88 byte* body = buffer + f->HeaderSize();
89 f->Serialize(buffer, &header, &body);
90 for (size_t i = 0; i < 7; i++) {
91 CHECK_EQ(i, static_cast<size_t>(*(buffer + 2 * i + f->HeaderSize() + 1)));
92 }
93 }
94
95
96 TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) {
97 Zone zone;
98 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone);
99 uint16_t f_index = builder->AddFunction();
100 WasmFunctionBuilder* function = builder->FunctionAt(f_index);
101 for (size_t i = 0; i < 128; i++) {
102 AddLocal(function, kAstF32);
103 }
104 AddLocal(function, kAstI32);
105
106 WasmFunctionEncoder* f = function->Build(&zone, builder);
107 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone);
108 byte* buffer = buffer_vector.data();
109 byte* header = buffer;
110 byte* body = buffer + f->HeaderSize();
111 f->Serialize(buffer, &header, &body);
112 body = buffer + f->HeaderSize();
113 for (size_t i = 0; i < 127; i++) {
114 CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * i)));
115 CHECK_EQ(i + 1, static_cast<size_t>(*(body + 2 * i + 1)));
116 }
117 CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * 127)));
118 CHECK_EQ(0x80, static_cast<size_t>(*(body + 2 * 127 + 1)));
119 CHECK_EQ(0x01, static_cast<size_t>(*(body + 2 * 127 + 2)));
120 CHECK_EQ(kExprGetLocal, static_cast<size_t>(*(body + 2 * 127 + 3)));
121 CHECK_EQ(0x00, static_cast<size_t>(*(body + 2 * 127 + 4)));
122 }
123
124
125 TEST_F(EncoderTest, LEB_Functions) {
126 byte leb_value[5] = {0, 0, 0, 0, 0};
127 CheckReadValue(leb_value, 0, 1, kNoError);
128 CheckWriteValue(0, 1, leb_value);
129 leb_value[0] = 23;
130 CheckReadValue(leb_value, 23, 1, kNoError);
131 CheckWriteValue(23, 1, leb_value);
132 leb_value[0] = 0x80;
133 leb_value[1] = 0x01;
134 CheckReadValue(leb_value, 128, 2, kNoError);
135 CheckWriteValue(128, 2, leb_value);
136 leb_value[0] = 0x80;
137 leb_value[1] = 0x80;
138 leb_value[2] = 0x80;
139 leb_value[3] = 0x80;
140 leb_value[4] = 0x01;
141 CheckReadValue(leb_value, 0x10000000, 5, kNoError);
142 CheckWriteValue(0x10000000, 5, leb_value);
143 leb_value[0] = 0x80;
144 leb_value[1] = 0x80;
145 leb_value[2] = 0x80;
146 leb_value[3] = 0x80;
147 leb_value[4] = 0x80;
148 CheckReadValue(leb_value, -1, -1, kInvalidLEB128);
149 }
150 } // namespace wasm
151 } // namespace internal
152 } // namespace v8
OLDNEW
« no previous file with comments | « test/unittests/wasm/ast-decoder-unittest.cc ('k') | test/unittests/wasm/module-decoder-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698