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 "test/cctest/wasm/test-signatures.h" | 9 #include "test/cctest/wasm/test-signatures.h" |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 Verify(kSuccess, env, code, code + arraysize(code)); \ | 48 Verify(kSuccess, env, code, code + arraysize(code)); \ |
49 } while (false) | 49 } while (false) |
50 | 50 |
51 | 51 |
52 #define EXPECT_FAILURE_INLINE(env, ...) \ | 52 #define EXPECT_FAILURE_INLINE(env, ...) \ |
53 do { \ | 53 do { \ |
54 static byte code[] = {__VA_ARGS__}; \ | 54 static byte code[] = {__VA_ARGS__}; \ |
55 Verify(kError, env, code, code + arraysize(code)); \ | 55 Verify(kError, env, code, code + arraysize(code)); \ |
56 } while (false) | 56 } while (false) |
57 | 57 |
58 #define VERIFY(...) \ | 58 #define VERIFY(...) \ |
59 do { \ | 59 do { \ |
60 static const byte code[] = {__VA_ARGS__}; \ | 60 static const byte code[] = {__VA_ARGS__}; \ |
61 Verify(kSuccess, &env_v_i, code, code + sizeof(code)); \ | 61 Verify(kSuccess, sigs.v_i(), code, code + sizeof(code)); \ |
62 } while (false) | 62 } while (false) |
63 | 63 |
64 class AstDecoderTest : public TestWithZone { | 64 class AstDecoderTest : public TestWithZone { |
65 public: | 65 public: |
66 AstDecoderTest() : TestWithZone(), sigs() { | 66 typedef std::pair<uint32_t, LocalType> LocalsDecl; |
67 init_env(&env_i_i, sigs.i_i()); | 67 |
68 init_env(&env_v_v, sigs.v_v()); | 68 AstDecoderTest() : module(nullptr) {} |
69 init_env(&env_v_i, sigs.v_i()); | 69 TestSignatures sigs; |
70 init_env(&env_i_f, sigs.i_f()); | 70 ModuleEnv* module; |
71 init_env(&env_i_d, sigs.i_d()); | 71 LocalDeclEncoder local_decls; |
72 init_env(&env_l_l, sigs.l_l()); | 72 |
73 init_env(&env_f_ff, sigs.f_ff()); | 73 void AddLocals(LocalType type, uint32_t count) { |
74 init_env(&env_d_dd, sigs.d_dd()); | 74 local_decls.AddLocals(count, type); |
75 } | 75 } |
76 | 76 |
77 TestSignatures sigs; | 77 // Preprends local variable declarations and renders nice error messages for |
| 78 // verification failures. |
| 79 void Verify(ErrorCode expected, FunctionSig* sig, const byte* start, |
| 80 const byte* end) { |
| 81 local_decls.Prepend(&start, &end); |
| 82 // Verify the code. |
| 83 TreeResult result = VerifyWasmCode(module, sig, start, end); |
78 | 84 |
79 FunctionEnv env_i_i; | |
80 FunctionEnv env_v_v; | |
81 FunctionEnv env_v_i; | |
82 FunctionEnv env_i_f; | |
83 FunctionEnv env_i_d; | |
84 FunctionEnv env_l_l; | |
85 FunctionEnv env_f_ff; | |
86 FunctionEnv env_d_dd; | |
87 | |
88 static void init_env(FunctionEnv* env, FunctionSig* sig) { | |
89 env->module = nullptr; | |
90 env->sig = sig; | |
91 env->local_i32_count = 0; | |
92 env->local_i64_count = 0; | |
93 env->local_f32_count = 0; | |
94 env->local_f64_count = 0; | |
95 env->SumLocals(); | |
96 } | |
97 | |
98 // A wrapper around VerifyWasmCode() that renders a nice failure message. | |
99 void Verify(ErrorCode expected, FunctionEnv* env, const byte* start, | |
100 const byte* end) { | |
101 TreeResult result = VerifyWasmCode(env, start, end); | |
102 if (result.error_code != expected) { | 85 if (result.error_code != expected) { |
103 ptrdiff_t pc = result.error_pc - result.start; | 86 ptrdiff_t pc = result.error_pc - result.start; |
104 ptrdiff_t pt = result.error_pt - result.start; | 87 ptrdiff_t pt = result.error_pt - result.start; |
105 std::ostringstream str; | 88 std::ostringstream str; |
106 if (expected == kSuccess) { | 89 if (expected == kSuccess) { |
107 str << "Verification failed: " << result.error_code << " pc = +" << pc; | 90 str << "Verification failed: " << result.error_code << " pc = +" << pc; |
108 if (result.error_pt) str << ", pt = +" << pt; | 91 if (result.error_pt) str << ", pt = +" << pt; |
109 str << ", msg = " << result.error_msg.get(); | 92 str << ", msg = " << result.error_msg.get(); |
110 } else { | 93 } else { |
111 str << "Verification expected: " << expected << ", but got " | 94 str << "Verification expected: " << expected << ", but got " |
112 << result.error_code; | 95 << result.error_code; |
113 if (result.error_code != kSuccess) { | 96 if (result.error_code != kSuccess) { |
114 str << " pc = +" << pc; | 97 str << " pc = +" << pc; |
115 if (result.error_pt) str << ", pt = +" << pt; | 98 if (result.error_pt) str << ", pt = +" << pt; |
116 } | 99 } |
117 } | 100 } |
118 FATAL(str.str().c_str()); | 101 FATAL(str.str().c_str()); |
119 } | 102 } |
| 103 |
| 104 delete[] start; // local_decls.Prepend() allocated a new buffer. |
120 } | 105 } |
121 | 106 |
122 void TestBinop(WasmOpcode opcode, FunctionSig* success) { | 107 void TestBinop(WasmOpcode opcode, FunctionSig* success) { |
123 // op(local[0], local[1]) | 108 // op(local[0], local[1]) |
124 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0, kExprGetLocal, | 109 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0, kExprGetLocal, |
125 1}; | 110 1}; |
126 FunctionEnv env; | 111 EXPECT_VERIFIES(success, code); |
127 init_env(&env, success); | |
128 EXPECT_VERIFIES(&env, code); | |
129 | 112 |
130 // Try all combinations of return and parameter types. | 113 // Try all combinations of return and parameter types. |
131 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 114 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
132 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { | 115 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { |
133 for (size_t k = 0; k < arraysize(kLocalTypes); k++) { | 116 for (size_t k = 0; k < arraysize(kLocalTypes); k++) { |
134 LocalType types[] = {kLocalTypes[i], kLocalTypes[j], kLocalTypes[k]}; | 117 LocalType types[] = {kLocalTypes[i], kLocalTypes[j], kLocalTypes[k]}; |
135 if (types[0] != success->GetReturn(0) || | 118 if (types[0] != success->GetReturn(0) || |
136 types[1] != success->GetParam(0) || | 119 types[1] != success->GetParam(0) || |
137 types[2] != success->GetParam(1)) { | 120 types[2] != success->GetParam(1)) { |
138 // Test signature mismatch. | 121 // Test signature mismatch. |
139 FunctionSig sig(1, 2, types); | 122 FunctionSig sig(1, 2, types); |
140 init_env(&env, &sig); | 123 EXPECT_FAILURE(&sig, code); |
141 EXPECT_FAILURE(&env, code); | |
142 } | 124 } |
143 } | 125 } |
144 } | 126 } |
145 } | 127 } |
146 } | 128 } |
147 | 129 |
148 void TestUnop(WasmOpcode opcode, FunctionSig* success) { | 130 void TestUnop(WasmOpcode opcode, FunctionSig* success) { |
149 TestUnop(opcode, success->GetReturn(), success->GetParam(0)); | 131 TestUnop(opcode, success->GetReturn(), success->GetParam(0)); |
150 } | 132 } |
151 | 133 |
152 void TestUnop(WasmOpcode opcode, LocalType ret_type, LocalType param_type) { | 134 void TestUnop(WasmOpcode opcode, LocalType ret_type, LocalType param_type) { |
153 // Return(op(local[0])) | 135 // Return(op(local[0])) |
154 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0}; | 136 byte code[] = {static_cast<byte>(opcode), kExprGetLocal, 0}; |
155 FunctionEnv env; | |
156 { | 137 { |
157 LocalType types[] = {ret_type, param_type}; | 138 LocalType types[] = {ret_type, param_type}; |
158 FunctionSig sig(1, 1, types); | 139 FunctionSig sig(1, 1, types); |
159 init_env(&env, &sig); | 140 EXPECT_VERIFIES(&sig, code); |
160 EXPECT_VERIFIES(&env, code); | |
161 } | 141 } |
162 | 142 |
163 // Try all combinations of return and parameter types. | 143 // Try all combinations of return and parameter types. |
164 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 144 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
165 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { | 145 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { |
166 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; | 146 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; |
167 if (types[0] != ret_type || types[1] != param_type) { | 147 if (types[0] != ret_type || types[1] != param_type) { |
168 // Test signature mismatch. | 148 // Test signature mismatch. |
169 FunctionSig sig(1, 1, types); | 149 FunctionSig sig(1, 1, types); |
170 init_env(&env, &sig); | 150 EXPECT_FAILURE(&sig, code); |
171 EXPECT_FAILURE(&env, code); | |
172 } | 151 } |
173 } | 152 } |
174 } | 153 } |
175 } | 154 } |
176 }; | 155 }; |
177 | 156 |
178 | 157 |
179 static FunctionEnv CreateInt32FunctionEnv(FunctionSig* sig, int count) { | |
180 FunctionEnv env; | |
181 env.module = nullptr; | |
182 env.sig = sig; | |
183 env.local_i32_count = count; | |
184 env.local_f64_count = 0; | |
185 env.local_f32_count = 0; | |
186 env.total_locals = static_cast<unsigned>(count + sig->parameter_count()); | |
187 return env; | |
188 } | |
189 | |
190 TEST_F(AstDecoderTest, Int8Const) { | 158 TEST_F(AstDecoderTest, Int8Const) { |
191 byte code[] = {kExprI8Const, 0}; | 159 byte code[] = {kExprI8Const, 0}; |
192 for (int i = -128; i < 128; i++) { | 160 for (int i = -128; i < 128; i++) { |
193 code[1] = static_cast<byte>(i); | 161 code[1] = static_cast<byte>(i); |
194 EXPECT_VERIFIES(&env_i_i, code); | 162 EXPECT_VERIFIES(sigs.i_i(), code); |
195 } | 163 } |
196 } | 164 } |
197 | 165 |
198 TEST_F(AstDecoderTest, EmptyFunction) { | 166 TEST_F(AstDecoderTest, EmptyFunction) { |
199 byte code[] = {0}; | 167 byte code[] = {0}; |
200 Verify(kSuccess, &env_v_v, code, code); | 168 Verify(kSuccess, sigs.v_v(), code, code); |
201 Verify(kError, &env_i_i, code, code); | 169 Verify(kError, sigs.i_i(), code, code); |
202 } | 170 } |
203 | 171 |
204 TEST_F(AstDecoderTest, IncompleteIf1) { | 172 TEST_F(AstDecoderTest, IncompleteIf1) { |
205 byte code[] = {kExprIf}; | 173 byte code[] = {kExprIf}; |
206 EXPECT_FAILURE(&env_v_v, code); | 174 EXPECT_FAILURE(sigs.v_v(), code); |
207 EXPECT_FAILURE(&env_i_i, code); | 175 EXPECT_FAILURE(sigs.i_i(), code); |
208 } | 176 } |
209 | 177 |
210 TEST_F(AstDecoderTest, IncompleteIf2) { | 178 TEST_F(AstDecoderTest, IncompleteIf2) { |
211 byte code[] = {kExprIf, kExprI8Const, 0}; | 179 byte code[] = {kExprIf, kExprI8Const, 0}; |
212 EXPECT_FAILURE(&env_v_v, code); | 180 EXPECT_FAILURE(sigs.v_v(), code); |
213 EXPECT_FAILURE(&env_i_i, code); | 181 EXPECT_FAILURE(sigs.i_i(), code); |
214 } | 182 } |
215 | 183 |
216 TEST_F(AstDecoderTest, Int8Const_fallthru) { | 184 TEST_F(AstDecoderTest, Int8Const_fallthru) { |
217 byte code[] = {kExprI8Const, 0, kExprI8Const, 1}; | 185 byte code[] = {kExprI8Const, 0, kExprI8Const, 1}; |
218 EXPECT_VERIFIES(&env_i_i, code); | 186 EXPECT_VERIFIES(sigs.i_i(), code); |
219 } | 187 } |
220 | 188 |
221 TEST_F(AstDecoderTest, Int32Const) { | 189 TEST_F(AstDecoderTest, Int32Const) { |
222 const int kInc = 4498211; | 190 const int kInc = 4498211; |
223 for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { | 191 for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { |
224 // TODO(binji): expand test for other sized int32s; 1 through 5 bytes. | 192 // TODO(binji): expand test for other sized int32s; 1 through 5 bytes. |
225 byte code[] = {WASM_I32V(i)}; | 193 byte code[] = {WASM_I32V(i)}; |
226 EXPECT_VERIFIES(&env_i_i, code); | 194 EXPECT_VERIFIES(sigs.i_i(), code); |
227 } | 195 } |
228 } | 196 } |
229 | 197 |
230 TEST_F(AstDecoderTest, Int8Const_fallthru2) { | 198 TEST_F(AstDecoderTest, Int8Const_fallthru2) { |
231 byte code[] = {WASM_I8(0), WASM_I32V_4(0x1122334)}; | 199 byte code[] = {WASM_I8(0), WASM_I32V_4(0x1122334)}; |
232 EXPECT_VERIFIES(&env_i_i, code); | 200 EXPECT_VERIFIES(sigs.i_i(), code); |
233 } | 201 } |
234 | 202 |
235 TEST_F(AstDecoderTest, Int64Const) { | 203 TEST_F(AstDecoderTest, Int64Const) { |
236 const int kInc = 4498211; | 204 const int kInc = 4498211; |
237 for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { | 205 for (int32_t i = kMinInt; i < kMaxInt - kInc; i = i + kInc) { |
238 byte code[] = {WASM_I64V((static_cast<int64_t>(i) << 32) | i)}; | 206 byte code[] = {WASM_I64V((static_cast<int64_t>(i) << 32) | i)}; |
239 EXPECT_VERIFIES(&env_l_l, code); | 207 EXPECT_VERIFIES(sigs.l_l(), code); |
240 } | 208 } |
241 } | 209 } |
242 | 210 |
243 TEST_F(AstDecoderTest, Float32Const) { | 211 TEST_F(AstDecoderTest, Float32Const) { |
244 byte code[] = {kExprF32Const, 0, 0, 0, 0}; | 212 byte code[] = {kExprF32Const, 0, 0, 0, 0}; |
245 float* ptr = reinterpret_cast<float*>(code + 1); | 213 float* ptr = reinterpret_cast<float*>(code + 1); |
246 for (int i = 0; i < 30; i++) { | 214 for (int i = 0; i < 30; i++) { |
247 *ptr = i * -7.75f; | 215 *ptr = i * -7.75f; |
248 EXPECT_VERIFIES(&env_f_ff, code); | 216 EXPECT_VERIFIES(sigs.f_ff(), code); |
249 } | 217 } |
250 } | 218 } |
251 | 219 |
252 TEST_F(AstDecoderTest, Float64Const) { | 220 TEST_F(AstDecoderTest, Float64Const) { |
253 byte code[] = {kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0}; | 221 byte code[] = {kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0}; |
254 double* ptr = reinterpret_cast<double*>(code + 1); | 222 double* ptr = reinterpret_cast<double*>(code + 1); |
255 for (int i = 0; i < 30; i++) { | 223 for (int i = 0; i < 30; i++) { |
256 *ptr = i * 33.45; | 224 *ptr = i * 33.45; |
257 EXPECT_VERIFIES(&env_d_dd, code); | 225 EXPECT_VERIFIES(sigs.d_dd(), code); |
258 } | 226 } |
259 } | 227 } |
260 | 228 |
261 TEST_F(AstDecoderTest, Int32Const_off_end) { | 229 TEST_F(AstDecoderTest, Int32Const_off_end) { |
262 byte code[] = {kExprI32Const, 0xaa, 0xbb, 0xcc, 0x44}; | 230 byte code[] = {kExprI32Const, 0xaa, 0xbb, 0xcc, 0x44}; |
263 | 231 |
264 for (int size = 1; size <= 4; size++) { | 232 for (int size = 1; size <= 4; size++) { |
265 Verify(kError, &env_i_i, code, code + size); | 233 Verify(kError, sigs.i_i(), code, code + size); |
266 } | 234 } |
267 } | 235 } |
268 | 236 |
269 TEST_F(AstDecoderTest, GetLocal0_param) { | 237 TEST_F(AstDecoderTest, GetLocal0_param) { |
270 EXPECT_VERIFIES(&env_i_i, kCodeGetLocal0); | 238 EXPECT_VERIFIES(sigs.i_i(), kCodeGetLocal0); |
271 } | 239 } |
272 | 240 |
273 TEST_F(AstDecoderTest, GetLocal0_local) { | 241 TEST_F(AstDecoderTest, GetLocal0_local) { |
274 FunctionEnv env; | 242 AddLocals(kAstI32, 1); |
275 init_env(&env, sigs.i_v()); | 243 EXPECT_VERIFIES(sigs.i_v(), kCodeGetLocal0); |
276 env.AddLocals(kAstI32, 1); | |
277 EXPECT_VERIFIES(&env, kCodeGetLocal0); | |
278 } | 244 } |
279 | 245 |
280 TEST_F(AstDecoderTest, GetLocal0_param_n) { | 246 TEST_F(AstDecoderTest, GetLocal0_param_n) { |
281 FunctionSig* array[] = {sigs.i_i(), sigs.i_ii(), sigs.i_iii()}; | 247 FunctionSig* array[] = {sigs.i_i(), sigs.i_ii(), sigs.i_iii()}; |
282 | 248 |
283 for (size_t i = 0; i < arraysize(array); i++) { | 249 for (size_t i = 0; i < arraysize(array); i++) { |
284 FunctionEnv env = CreateInt32FunctionEnv(array[i], 0); | 250 EXPECT_VERIFIES(array[i], kCodeGetLocal0); |
285 EXPECT_VERIFIES(&env, kCodeGetLocal0); | |
286 } | 251 } |
287 } | 252 } |
288 | 253 |
289 TEST_F(AstDecoderTest, GetLocalN_local) { | 254 TEST_F(AstDecoderTest, GetLocalN_local) { |
290 for (byte i = 1; i < 8; i++) { | 255 for (byte i = 1; i < 8; i++) { |
291 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), i); | 256 AddLocals(kAstI32, 1); |
292 for (byte j = 0; j < i; j++) { | 257 for (byte j = 0; j < i; j++) { |
293 byte code[] = {kExprGetLocal, j}; | 258 byte code[] = {kExprGetLocal, j}; |
294 EXPECT_VERIFIES(&env, code); | 259 EXPECT_VERIFIES(sigs.i_v(), code); |
295 } | 260 } |
296 } | 261 } |
297 } | 262 } |
298 | 263 |
299 TEST_F(AstDecoderTest, GetLocal0_fail_no_params) { | 264 TEST_F(AstDecoderTest, GetLocal0_fail_no_params) { |
300 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), 0); | 265 EXPECT_FAILURE(sigs.i_v(), kCodeGetLocal0); |
301 | |
302 EXPECT_FAILURE(&env, kCodeGetLocal0); | |
303 } | 266 } |
304 | 267 |
305 TEST_F(AstDecoderTest, GetLocal1_fail_no_locals) { | 268 TEST_F(AstDecoderTest, GetLocal1_fail_no_locals) { |
306 EXPECT_FAILURE(&env_i_i, kCodeGetLocal1); | 269 EXPECT_FAILURE(sigs.i_i(), kCodeGetLocal1); |
307 } | 270 } |
308 | 271 |
309 TEST_F(AstDecoderTest, GetLocal_off_end) { | 272 TEST_F(AstDecoderTest, GetLocal_off_end) { |
310 static const byte code[] = {kExprGetLocal}; | 273 static const byte code[] = {kExprGetLocal}; |
311 EXPECT_FAILURE(&env_i_i, code); | 274 EXPECT_FAILURE(sigs.i_i(), code); |
312 } | 275 } |
313 | 276 |
314 TEST_F(AstDecoderTest, GetLocal_varint) { | 277 TEST_F(AstDecoderTest, GetLocal_varint) { |
315 env_i_i.local_i32_count = 1000000000; | 278 const int kMaxLocals = 8000000; |
316 env_i_i.total_locals += 1000000000; | 279 AddLocals(kAstI32, kMaxLocals); |
317 | 280 |
318 { | 281 for (int index = 0; index < kMaxLocals; index = index * 11 + 5) { |
319 static const byte code[] = {kExprGetLocal, 0xFF, 0x01}; | 282 EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_1(index)); |
320 EXPECT_VERIFIES(&env_i_i, code); | 283 EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_2(index)); |
321 EXPECT_FAILURE(&env_i_f, code); | 284 EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_3(index)); |
| 285 EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(index)); |
322 } | 286 } |
323 | 287 |
324 { | 288 EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_5(kMaxLocals - 1)); |
325 static const byte code[] = {kExprGetLocal, 0xF0, 0x80, 0x01}; | |
326 EXPECT_VERIFIES(&env_i_i, code); | |
327 EXPECT_FAILURE(&env_i_f, code); | |
328 } | |
329 | 289 |
330 { | 290 EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(kMaxLocals - 1)); |
331 static const byte code[] = {kExprGetLocal, 0xF2, 0x81, 0x82, 0x01}; | 291 EXPECT_VERIFIES_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(kMaxLocals)); |
332 EXPECT_VERIFIES(&env_i_i, code); | 292 EXPECT_FAILURE_INLINE(sigs.i_i(), kExprGetLocal, U32V_4(kMaxLocals + 1)); |
333 EXPECT_FAILURE(&env_i_f, code); | |
334 } | |
335 | 293 |
336 { | 294 EXPECT_FAILURE_INLINE(sigs.i_v(), kExprGetLocal, U32V_4(kMaxLocals)); |
337 static const byte code[] = {kExprGetLocal, 0xF3, 0xA1, 0xB1, 0xC1, 0x01}; | 295 EXPECT_FAILURE_INLINE(sigs.i_v(), kExprGetLocal, U32V_4(kMaxLocals + 1)); |
338 EXPECT_VERIFIES(&env_i_i, code); | |
339 EXPECT_FAILURE(&env_i_f, code); | |
340 } | |
341 } | 296 } |
342 | 297 |
343 TEST_F(AstDecoderTest, Binops_off_end) { | 298 TEST_F(AstDecoderTest, Binops_off_end) { |
344 byte code1[] = {0}; // [opcode] | 299 byte code1[] = {0}; // [opcode] |
345 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { | 300 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { |
346 code1[0] = kInt32BinopOpcodes[i]; | 301 code1[0] = kInt32BinopOpcodes[i]; |
347 EXPECT_FAILURE(&env_i_i, code1); | 302 EXPECT_FAILURE(sigs.i_i(), code1); |
348 } | 303 } |
349 | 304 |
350 byte code3[] = {0, kExprGetLocal, 0}; // [opcode] [expr] | 305 byte code3[] = {0, kExprGetLocal, 0}; // [opcode] [expr] |
351 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { | 306 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { |
352 code3[0] = kInt32BinopOpcodes[i]; | 307 code3[0] = kInt32BinopOpcodes[i]; |
353 EXPECT_FAILURE(&env_i_i, code3); | 308 EXPECT_FAILURE(sigs.i_i(), code3); |
354 } | 309 } |
355 | 310 |
356 byte code4[] = {0, kExprGetLocal, 0, 0}; // [opcode] [expr] [opcode] | 311 byte code4[] = {0, kExprGetLocal, 0, 0}; // [opcode] [expr] [opcode] |
357 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { | 312 for (size_t i = 0; i < arraysize(kInt32BinopOpcodes); i++) { |
358 code4[0] = kInt32BinopOpcodes[i]; | 313 code4[0] = kInt32BinopOpcodes[i]; |
359 code4[3] = kInt32BinopOpcodes[i]; | 314 code4[3] = kInt32BinopOpcodes[i]; |
360 EXPECT_FAILURE(&env_i_i, code4); | 315 EXPECT_FAILURE(sigs.i_i(), code4); |
361 } | 316 } |
362 } | 317 } |
363 | 318 |
364 | 319 |
365 //=================================================================== | 320 //=================================================================== |
366 //== Statements | 321 //== Statements |
367 //=================================================================== | 322 //=================================================================== |
368 TEST_F(AstDecoderTest, Nop) { | 323 TEST_F(AstDecoderTest, Nop) { |
369 static const byte code[] = {kExprNop}; | 324 static const byte code[] = {kExprNop}; |
370 EXPECT_VERIFIES(&env_v_v, code); | 325 EXPECT_VERIFIES(sigs.v_v(), code); |
371 } | 326 } |
372 | 327 |
373 TEST_F(AstDecoderTest, SetLocal0_param) { | 328 TEST_F(AstDecoderTest, SetLocal0_param) { |
374 static const byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; | 329 static const byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; |
375 EXPECT_VERIFIES(&env_i_i, code); | 330 EXPECT_VERIFIES(sigs.i_i(), code); |
376 } | 331 } |
377 | 332 |
378 TEST_F(AstDecoderTest, SetLocal0_local) { | 333 TEST_F(AstDecoderTest, SetLocal0_local) { |
379 byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; | 334 byte code[] = {kExprSetLocal, 0, kExprI8Const, 0}; |
380 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), 1); | 335 AddLocals(kAstI32, 1); |
381 | 336 EXPECT_VERIFIES(sigs.i_v(), code); |
382 EXPECT_VERIFIES(&env, code); | |
383 } | 337 } |
384 | 338 |
385 TEST_F(AstDecoderTest, SetLocalN_local) { | 339 TEST_F(AstDecoderTest, SetLocalN_local) { |
386 for (byte i = 1; i < 8; i++) { | 340 for (byte i = 1; i < 8; i++) { |
387 FunctionEnv env = CreateInt32FunctionEnv(sigs.i_v(), i); | 341 AddLocals(kAstI32, 1); |
388 for (byte j = 0; j < i; j++) { | 342 for (byte j = 0; j < i; j++) { |
389 byte code[] = {kExprSetLocal, j, kExprI8Const, i}; | 343 byte code[] = {kExprSetLocal, j, kExprI8Const, i}; |
390 EXPECT_VERIFIES(&env, code); | 344 EXPECT_VERIFIES(sigs.v_v(), code); |
391 } | 345 } |
392 } | 346 } |
393 } | 347 } |
394 | 348 |
395 TEST_F(AstDecoderTest, Block0) { | 349 TEST_F(AstDecoderTest, Block0) { |
396 static const byte code[] = {kExprBlock, 0}; | 350 static const byte code[] = {kExprBlock, 0}; |
397 EXPECT_VERIFIES(&env_v_v, code); | 351 EXPECT_VERIFIES(sigs.v_v(), code); |
398 } | 352 } |
399 | 353 |
400 TEST_F(AstDecoderTest, Block0_fallthru1) { | 354 TEST_F(AstDecoderTest, Block0_fallthru1) { |
401 static const byte code[] = {kExprBlock, 0, kExprBlock, 0}; | 355 static const byte code[] = {kExprBlock, 0, kExprBlock, 0}; |
402 EXPECT_VERIFIES(&env_v_v, code); | 356 EXPECT_VERIFIES(sigs.v_v(), code); |
403 } | 357 } |
404 | 358 |
405 TEST_F(AstDecoderTest, Block1) { | 359 TEST_F(AstDecoderTest, Block1) { |
406 static const byte code[] = {kExprBlock, 1, kExprSetLocal, 0, kExprI8Const, 0}; | 360 static const byte code[] = {kExprBlock, 1, kExprSetLocal, 0, kExprI8Const, 0}; |
407 EXPECT_VERIFIES(&env_i_i, code); | 361 EXPECT_VERIFIES(sigs.i_i(), code); |
408 } | 362 } |
409 | 363 |
410 TEST_F(AstDecoderTest, Block0_fallthru2) { | 364 TEST_F(AstDecoderTest, Block0_fallthru2) { |
411 static const byte code[] = {kExprBlock, 0, kExprSetLocal, 0, kExprI8Const, 0}; | 365 static const byte code[] = {kExprBlock, 0, kExprSetLocal, 0, kExprI8Const, 0}; |
412 EXPECT_VERIFIES(&env_i_i, code); | 366 EXPECT_VERIFIES(sigs.i_i(), code); |
413 } | 367 } |
414 | 368 |
415 TEST_F(AstDecoderTest, Block2) { | 369 TEST_F(AstDecoderTest, Block2) { |
416 static const byte code[] = {kExprBlock, 2, // -- | 370 static const byte code[] = {kExprBlock, 2, // -- |
417 kExprSetLocal, 0, kExprI8Const, 0, // -- | 371 kExprSetLocal, 0, kExprI8Const, 0, // -- |
418 kExprSetLocal, 0, kExprI8Const, 0}; // -- | 372 kExprSetLocal, 0, kExprI8Const, 0}; // -- |
419 EXPECT_VERIFIES(&env_i_i, code); | 373 EXPECT_VERIFIES(sigs.i_i(), code); |
420 } | 374 } |
421 | 375 |
422 TEST_F(AstDecoderTest, Block2_fallthru) { | 376 TEST_F(AstDecoderTest, Block2_fallthru) { |
423 static const byte code[] = {kExprBlock, 2, // -- | 377 static const byte code[] = {kExprBlock, 2, // -- |
424 kExprSetLocal, 0, kExprI8Const, 0, // -- | 378 kExprSetLocal, 0, kExprI8Const, 0, // -- |
425 kExprSetLocal, 0, kExprI8Const, 0, // -- | 379 kExprSetLocal, 0, kExprI8Const, 0, // -- |
426 kExprI8Const, 11}; // -- | 380 kExprI8Const, 11}; // -- |
427 EXPECT_VERIFIES(&env_i_i, code); | 381 EXPECT_VERIFIES(sigs.i_i(), code); |
428 } | 382 } |
429 | 383 |
430 TEST_F(AstDecoderTest, BlockN) { | 384 TEST_F(AstDecoderTest, BlockN) { |
431 byte block[] = {kExprBlock, 2}; | 385 byte block[] = {kExprBlock, 2}; |
432 | 386 |
433 for (size_t i = 0; i < 10; i++) { | 387 for (size_t i = 0; i < 10; i++) { |
434 size_t total = sizeof(block) + sizeof(kCodeSetLocal0) * i; | 388 size_t total = sizeof(block) + sizeof(kCodeSetLocal0) * i; |
435 byte* code = reinterpret_cast<byte*>(malloc(total)); | 389 byte* code = reinterpret_cast<byte*>(malloc(total)); |
436 memcpy(code, block, sizeof(block)); | 390 memcpy(code, block, sizeof(block)); |
437 code[1] = static_cast<byte>(i); | 391 code[1] = static_cast<byte>(i); |
438 for (size_t j = 0; j < i; j++) { | 392 for (size_t j = 0; j < i; j++) { |
439 memcpy(code + sizeof(block) + j * sizeof(kCodeSetLocal0), kCodeSetLocal0, | 393 memcpy(code + sizeof(block) + j * sizeof(kCodeSetLocal0), kCodeSetLocal0, |
440 sizeof(kCodeSetLocal0)); | 394 sizeof(kCodeSetLocal0)); |
441 } | 395 } |
442 Verify(kSuccess, &env_v_i, code, code + total); | 396 Verify(kSuccess, sigs.v_i(), code, code + total); |
443 free(code); | 397 free(code); |
444 } | 398 } |
445 } | 399 } |
446 | 400 |
447 TEST_F(AstDecoderTest, BlockN_off_end) { | 401 TEST_F(AstDecoderTest, BlockN_off_end) { |
448 for (byte i = 2; i < 10; i++) { | 402 for (byte i = 2; i < 10; i++) { |
449 byte code[] = {kExprBlock, i, kExprNop}; | 403 byte code[] = {kExprBlock, i, kExprNop}; |
450 EXPECT_FAILURE(&env_v_v, code); | 404 EXPECT_FAILURE(sigs.v_v(), code); |
451 } | 405 } |
452 } | 406 } |
453 | 407 |
454 TEST_F(AstDecoderTest, Block1_break) { | 408 TEST_F(AstDecoderTest, Block1_break) { |
455 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; | 409 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; |
456 EXPECT_VERIFIES(&env_v_v, code); | 410 EXPECT_VERIFIES(sigs.v_v(), code); |
457 } | 411 } |
458 | 412 |
459 TEST_F(AstDecoderTest, Block2_break) { | 413 TEST_F(AstDecoderTest, Block2_break) { |
460 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 0, kExprNop}; | 414 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 0, kExprNop}; |
461 EXPECT_VERIFIES(&env_v_v, code); | 415 EXPECT_VERIFIES(sigs.v_v(), code); |
462 } | 416 } |
463 | 417 |
464 TEST_F(AstDecoderTest, Block1_continue) { | 418 TEST_F(AstDecoderTest, Block1_continue) { |
465 static const byte code[] = {kExprBlock, 1, kExprBr, 1, kExprNop}; | 419 static const byte code[] = {kExprBlock, 1, kExprBr, 1, kExprNop}; |
466 EXPECT_FAILURE(&env_v_v, code); | 420 EXPECT_FAILURE(sigs.v_v(), code); |
467 } | 421 } |
468 | 422 |
469 TEST_F(AstDecoderTest, Block2_continue) { | 423 TEST_F(AstDecoderTest, Block2_continue) { |
470 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 1, kExprNop}; | 424 static const byte code[] = {kExprBlock, 2, kExprNop, kExprBr, 1, kExprNop}; |
471 EXPECT_FAILURE(&env_v_v, code); | 425 EXPECT_FAILURE(sigs.v_v(), code); |
472 } | 426 } |
473 | 427 |
474 TEST_F(AstDecoderTest, ExprBlock0) { | 428 TEST_F(AstDecoderTest, ExprBlock0) { |
475 static const byte code[] = {kExprBlock, 0}; | 429 static const byte code[] = {kExprBlock, 0}; |
476 EXPECT_VERIFIES(&env_v_v, code); | 430 EXPECT_VERIFIES(sigs.v_v(), code); |
477 } | 431 } |
478 | 432 |
479 TEST_F(AstDecoderTest, ExprBlock1a) { | 433 TEST_F(AstDecoderTest, ExprBlock1a) { |
480 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; | 434 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; |
481 EXPECT_VERIFIES(&env_i_i, code); | 435 EXPECT_VERIFIES(sigs.i_i(), code); |
482 } | 436 } |
483 | 437 |
484 TEST_F(AstDecoderTest, ExprBlock1b) { | 438 TEST_F(AstDecoderTest, ExprBlock1b) { |
485 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; | 439 static const byte code[] = {kExprBlock, 1, kExprI8Const, 0}; |
486 EXPECT_FAILURE(&env_f_ff, code); | 440 EXPECT_FAILURE(sigs.f_ff(), code); |
487 } | 441 } |
488 | 442 |
489 TEST_F(AstDecoderTest, ExprBlock1c) { | 443 TEST_F(AstDecoderTest, ExprBlock1c) { |
490 static const byte code[] = {kExprBlock, 1, kExprF32Const, 0, 0, 0, 0}; | 444 static const byte code[] = {kExprBlock, 1, kExprF32Const, 0, 0, 0, 0}; |
491 EXPECT_VERIFIES(&env_f_ff, code); | 445 EXPECT_VERIFIES(sigs.f_ff(), code); |
492 } | 446 } |
493 | 447 |
494 TEST_F(AstDecoderTest, IfEmpty) { | 448 TEST_F(AstDecoderTest, IfEmpty) { |
495 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprNop}; | 449 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprNop}; |
496 EXPECT_VERIFIES(&env_v_i, code); | 450 EXPECT_VERIFIES(sigs.v_i(), code); |
497 } | 451 } |
498 | 452 |
499 TEST_F(AstDecoderTest, IfSet) { | 453 TEST_F(AstDecoderTest, IfSet) { |
500 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprSetLocal, | 454 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprSetLocal, |
501 0, kExprI8Const, 0, kExprNop}; | 455 0, kExprI8Const, 0, kExprNop}; |
502 EXPECT_VERIFIES(&env_v_i, code); | 456 EXPECT_VERIFIES(sigs.v_i(), code); |
503 } | 457 } |
504 | 458 |
505 TEST_F(AstDecoderTest, IfBlock1) { | 459 TEST_F(AstDecoderTest, IfBlock1) { |
506 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprBlock, | 460 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprBlock, |
507 1, kExprSetLocal, 0, kExprI8Const, | 461 1, kExprSetLocal, 0, kExprI8Const, |
508 0, kExprNop}; | 462 0, kExprNop}; |
509 EXPECT_VERIFIES(&env_v_i, code); | 463 EXPECT_VERIFIES(sigs.v_i(), code); |
510 } | 464 } |
511 | 465 |
512 TEST_F(AstDecoderTest, IfBlock2) { | 466 TEST_F(AstDecoderTest, IfBlock2) { |
513 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprBlock, | 467 static const byte code[] = {kExprIf, kExprGetLocal, 0, kExprBlock, |
514 2, kExprSetLocal, 0, kExprI8Const, | 468 2, kExprSetLocal, 0, kExprI8Const, |
515 0, kExprSetLocal, 0, kExprI8Const, | 469 0, kExprSetLocal, 0, kExprI8Const, |
516 0}; | 470 0}; |
517 EXPECT_VERIFIES(&env_v_i, code); | 471 EXPECT_VERIFIES(sigs.v_i(), code); |
518 } | 472 } |
519 | 473 |
520 TEST_F(AstDecoderTest, IfElseEmpty) { | 474 TEST_F(AstDecoderTest, IfElseEmpty) { |
521 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprNop, | 475 static const byte code[] = {kExprIfElse, kExprGetLocal, 0, kExprNop, |
522 kExprNop}; | 476 kExprNop}; |
523 EXPECT_VERIFIES(&env_v_i, code); | 477 EXPECT_VERIFIES(sigs.v_i(), code); |
524 } | 478 } |
525 | 479 |
526 TEST_F(AstDecoderTest, IfElseSet) { | 480 TEST_F(AstDecoderTest, IfElseSet) { |
527 static const byte code[] = {kExprIfElse, | 481 static const byte code[] = {kExprIfElse, |
528 kExprGetLocal, | 482 kExprGetLocal, |
529 0, // -- | 483 0, // -- |
530 kExprSetLocal, | 484 kExprSetLocal, |
531 0, | 485 0, |
532 kExprI8Const, | 486 kExprI8Const, |
533 0, // -- | 487 0, // -- |
534 kExprSetLocal, | 488 kExprSetLocal, |
535 0, | 489 0, |
536 kExprI8Const, | 490 kExprI8Const, |
537 1}; // -- | 491 1}; // -- |
538 EXPECT_VERIFIES(&env_v_i, code); | 492 EXPECT_VERIFIES(sigs.v_i(), code); |
539 } | 493 } |
540 | 494 |
541 TEST_F(AstDecoderTest, IfElseUnreachable) { | 495 TEST_F(AstDecoderTest, IfElseUnreachable) { |
542 static const byte code[] = {kExprIfElse, kExprI8Const, 0, | 496 static const byte code[] = {kExprIfElse, kExprI8Const, 0, |
543 kExprUnreachable, kExprGetLocal, 0}; | 497 kExprUnreachable, kExprGetLocal, 0}; |
544 | 498 |
545 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 499 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
546 LocalType types[] = {kAstI32, kLocalTypes[i]}; | 500 LocalType types[] = {kAstI32, kLocalTypes[i]}; |
547 FunctionEnv env; | |
548 FunctionSig sig(1, 1, types); | 501 FunctionSig sig(1, 1, types); |
549 init_env(&env, &sig); | |
550 | 502 |
551 if (kLocalTypes[i] == kAstI32) { | 503 if (kLocalTypes[i] == kAstI32) { |
552 EXPECT_VERIFIES(&env, code); | 504 EXPECT_VERIFIES(&sig, code); |
553 } else { | 505 } else { |
554 EXPECT_FAILURE(&env, code); | 506 EXPECT_FAILURE(&sig, code); |
555 } | 507 } |
556 } | 508 } |
557 } | 509 } |
558 | 510 |
559 TEST_F(AstDecoderTest, Loop0) { | 511 TEST_F(AstDecoderTest, Loop0) { |
560 static const byte code[] = {kExprLoop, 0}; | 512 static const byte code[] = {kExprLoop, 0}; |
561 EXPECT_VERIFIES(&env_v_v, code); | 513 EXPECT_VERIFIES(sigs.v_v(), code); |
562 } | 514 } |
563 | 515 |
564 TEST_F(AstDecoderTest, Loop1) { | 516 TEST_F(AstDecoderTest, Loop1) { |
565 static const byte code[] = {kExprLoop, 1, kExprSetLocal, 0, kExprI8Const, 0}; | 517 static const byte code[] = {kExprLoop, 1, kExprSetLocal, 0, kExprI8Const, 0}; |
566 EXPECT_VERIFIES(&env_v_i, code); | 518 EXPECT_VERIFIES(sigs.v_i(), code); |
567 } | 519 } |
568 | 520 |
569 TEST_F(AstDecoderTest, Loop2) { | 521 TEST_F(AstDecoderTest, Loop2) { |
570 static const byte code[] = {kExprLoop, 2, // -- | 522 static const byte code[] = {kExprLoop, 2, // -- |
571 kExprSetLocal, 0, kExprI8Const, 0, // -- | 523 kExprSetLocal, 0, kExprI8Const, 0, // -- |
572 kExprSetLocal, 0, kExprI8Const, 0}; // -- | 524 kExprSetLocal, 0, kExprI8Const, 0}; // -- |
573 EXPECT_VERIFIES(&env_v_i, code); | 525 EXPECT_VERIFIES(sigs.v_i(), code); |
574 } | 526 } |
575 | 527 |
576 TEST_F(AstDecoderTest, Loop1_continue) { | 528 TEST_F(AstDecoderTest, Loop1_continue) { |
577 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprNop}; | 529 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprNop}; |
578 EXPECT_VERIFIES(&env_v_v, code); | 530 EXPECT_VERIFIES(sigs.v_v(), code); |
579 } | 531 } |
580 | 532 |
581 TEST_F(AstDecoderTest, Loop1_break) { | 533 TEST_F(AstDecoderTest, Loop1_break) { |
582 static const byte code[] = {kExprLoop, 1, kExprBr, 1, kExprNop}; | 534 static const byte code[] = {kExprLoop, 1, kExprBr, 1, kExprNop}; |
583 EXPECT_VERIFIES(&env_v_v, code); | 535 EXPECT_VERIFIES(sigs.v_v(), code); |
584 } | 536 } |
585 | 537 |
586 TEST_F(AstDecoderTest, Loop2_continue) { | 538 TEST_F(AstDecoderTest, Loop2_continue) { |
587 static const byte code[] = {kExprLoop, 2, // -- | 539 static const byte code[] = {kExprLoop, 2, // -- |
588 kExprSetLocal, 0, kExprI8Const, 0, // -- | 540 kExprSetLocal, 0, kExprI8Const, 0, // -- |
589 kExprBr, 0, kExprNop}; // -- | 541 kExprBr, 0, kExprNop}; // -- |
590 EXPECT_VERIFIES(&env_v_i, code); | 542 EXPECT_VERIFIES(sigs.v_i(), code); |
591 } | 543 } |
592 | 544 |
593 TEST_F(AstDecoderTest, Loop2_break) { | 545 TEST_F(AstDecoderTest, Loop2_break) { |
594 static const byte code[] = {kExprLoop, 2, // -- | 546 static const byte code[] = {kExprLoop, 2, // -- |
595 kExprSetLocal, 0, kExprI8Const, 0, // -- | 547 kExprSetLocal, 0, kExprI8Const, 0, // -- |
596 kExprBr, 1, kExprNop}; // -- | 548 kExprBr, 1, kExprNop}; // -- |
597 EXPECT_VERIFIES(&env_v_i, code); | 549 EXPECT_VERIFIES(sigs.v_i(), code); |
598 } | 550 } |
599 | 551 |
600 TEST_F(AstDecoderTest, ExprLoop0) { | 552 TEST_F(AstDecoderTest, ExprLoop0) { |
601 static const byte code[] = {kExprLoop, 0}; | 553 static const byte code[] = {kExprLoop, 0}; |
602 EXPECT_VERIFIES(&env_v_v, code); | 554 EXPECT_VERIFIES(sigs.v_v(), code); |
603 } | 555 } |
604 | 556 |
605 TEST_F(AstDecoderTest, ExprLoop1a) { | 557 TEST_F(AstDecoderTest, ExprLoop1a) { |
606 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; | 558 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; |
607 EXPECT_VERIFIES(&env_i_i, code); | 559 EXPECT_VERIFIES(sigs.i_i(), code); |
608 } | 560 } |
609 | 561 |
610 TEST_F(AstDecoderTest, ExprLoop1b) { | 562 TEST_F(AstDecoderTest, ExprLoop1b) { |
611 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; | 563 static const byte code[] = {kExprLoop, 1, kExprBr, 0, kExprI8Const, 0}; |
612 EXPECT_VERIFIES(&env_i_i, code); | 564 EXPECT_VERIFIES(sigs.i_i(), code); |
613 } | 565 } |
614 | 566 |
615 TEST_F(AstDecoderTest, ExprLoop2_unreachable) { | 567 TEST_F(AstDecoderTest, ExprLoop2_unreachable) { |
616 static const byte code[] = {kExprLoop, 2, kExprBr, 0, | 568 static const byte code[] = {kExprLoop, 2, kExprBr, 0, |
617 kExprI8Const, 0, kExprNop}; | 569 kExprI8Const, 0, kExprNop}; |
618 EXPECT_VERIFIES(&env_i_i, code); | 570 EXPECT_VERIFIES(sigs.i_i(), code); |
619 } | 571 } |
620 | 572 |
621 TEST_F(AstDecoderTest, ReturnVoid1) { | 573 TEST_F(AstDecoderTest, ReturnVoid1) { |
622 static const byte code[] = {kExprNop}; | 574 static const byte code[] = {kExprNop}; |
623 EXPECT_VERIFIES(&env_v_v, code); | 575 EXPECT_VERIFIES(sigs.v_v(), code); |
624 EXPECT_FAILURE(&env_i_i, code); | 576 EXPECT_FAILURE(sigs.i_i(), code); |
625 EXPECT_FAILURE(&env_i_f, code); | 577 EXPECT_FAILURE(sigs.i_f(), code); |
626 } | 578 } |
627 | 579 |
628 TEST_F(AstDecoderTest, ReturnVoid2) { | 580 TEST_F(AstDecoderTest, ReturnVoid2) { |
629 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; | 581 static const byte code[] = {kExprBlock, 1, kExprBr, 0, kExprNop}; |
630 EXPECT_VERIFIES(&env_v_v, code); | 582 EXPECT_VERIFIES(sigs.v_v(), code); |
631 EXPECT_FAILURE(&env_i_i, code); | 583 EXPECT_FAILURE(sigs.i_i(), code); |
632 EXPECT_FAILURE(&env_i_f, code); | 584 EXPECT_FAILURE(sigs.i_f(), code); |
633 } | 585 } |
634 | 586 |
635 TEST_F(AstDecoderTest, ReturnVoid3) { | 587 TEST_F(AstDecoderTest, ReturnVoid3) { |
636 EXPECT_VERIFIES_INLINE(&env_v_v, kExprI8Const, 0); | 588 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI8Const, 0); |
637 EXPECT_VERIFIES_INLINE(&env_v_v, kExprI32Const, 0, 0, 0, 0); | 589 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI32Const, 0, 0, 0, 0); |
638 EXPECT_VERIFIES_INLINE(&env_v_v, kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0); | 590 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprI64Const, 0, 0, 0, 0, 0, 0, 0, 0); |
639 EXPECT_VERIFIES_INLINE(&env_v_v, kExprF32Const, 0, 0, 0, 0); | 591 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF32Const, 0, 0, 0, 0); |
640 EXPECT_VERIFIES_INLINE(&env_v_v, kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0); | 592 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0); |
641 | 593 |
642 EXPECT_VERIFIES_INLINE(&env_v_i, kExprGetLocal, 0); | 594 EXPECT_VERIFIES_INLINE(sigs.v_i(), kExprGetLocal, 0); |
643 } | 595 } |
644 | 596 |
645 TEST_F(AstDecoderTest, Unreachable1) { | 597 TEST_F(AstDecoderTest, Unreachable1) { |
646 EXPECT_VERIFIES_INLINE(&env_v_v, kExprUnreachable); | 598 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable); |
647 EXPECT_VERIFIES_INLINE(&env_v_v, kExprUnreachable, kExprUnreachable); | 599 EXPECT_VERIFIES_INLINE(sigs.v_v(), kExprUnreachable, kExprUnreachable); |
648 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(2, WASM_UNREACHABLE, WASM_ZERO)); | 600 EXPECT_VERIFIES_INLINE(sigs.v_v(), |
649 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(2, WASM_BR(0), WASM_ZERO)); | 601 WASM_BLOCK(2, WASM_UNREACHABLE, WASM_ZERO)); |
650 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(2, WASM_UNREACHABLE, WASM_ZERO)); | 602 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(2, WASM_BR(0), WASM_ZERO)); |
651 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(2, WASM_BR(0), WASM_ZERO)); | 603 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_UNREACHABLE, WASM_ZERO)); |
| 604 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(2, WASM_BR(0), WASM_ZERO)); |
652 } | 605 } |
653 | 606 |
654 TEST_F(AstDecoderTest, Codeiness) { | 607 TEST_F(AstDecoderTest, Codeiness) { |
655 VERIFY(kExprLoop, 2, // -- | 608 VERIFY(kExprLoop, 2, // -- |
656 kExprSetLocal, 0, kExprI8Const, 0, // -- | 609 kExprSetLocal, 0, kExprI8Const, 0, // -- |
657 kExprBr, 0, kExprNop); // -- | 610 kExprBr, 0, kExprNop); // -- |
658 } | 611 } |
659 | 612 |
660 TEST_F(AstDecoderTest, ExprIf1) { | 613 TEST_F(AstDecoderTest, ExprIf1) { |
661 VERIFY(kExprIf, kExprGetLocal, 0, kExprI8Const, 0, kExprI8Const, 1); | 614 VERIFY(kExprIf, kExprGetLocal, 0, kExprI8Const, 0, kExprI8Const, 1); |
662 VERIFY(kExprIf, kExprGetLocal, 0, kExprGetLocal, 0, kExprGetLocal, 0); | 615 VERIFY(kExprIf, kExprGetLocal, 0, kExprGetLocal, 0, kExprGetLocal, 0); |
663 VERIFY(kExprIf, kExprGetLocal, 0, kExprI32Add, kExprGetLocal, 0, | 616 VERIFY(kExprIf, kExprGetLocal, 0, kExprI32Add, kExprGetLocal, 0, |
664 kExprGetLocal, 0, kExprI8Const, 1); | 617 kExprGetLocal, 0, kExprI8Const, 1); |
665 } | 618 } |
666 | 619 |
667 TEST_F(AstDecoderTest, ExprIf_off_end) { | 620 TEST_F(AstDecoderTest, ExprIf_off_end) { |
668 static const byte kCode[] = {kExprIf, kExprGetLocal, 0, kExprGetLocal, | 621 static const byte kCode[] = {kExprIf, kExprGetLocal, 0, kExprGetLocal, |
669 0, kExprGetLocal, 0}; | 622 0, kExprGetLocal, 0}; |
670 for (size_t len = 1; len < arraysize(kCode); len++) { | 623 for (size_t len = 1; len < arraysize(kCode); len++) { |
671 Verify(kError, &env_i_i, kCode, kCode + len); | 624 Verify(kError, sigs.i_i(), kCode, kCode + len); |
672 } | 625 } |
673 } | 626 } |
674 | 627 |
675 TEST_F(AstDecoderTest, ExprIf_type) { | 628 TEST_F(AstDecoderTest, ExprIf_type) { |
676 { | 629 { |
677 // float|double ? 1 : 2 | 630 // float|double ? 1 : 2 |
678 static const byte kCode[] = {kExprIfElse, kExprGetLocal, 0, kExprI8Const, | 631 static const byte kCode[] = {kExprIfElse, kExprGetLocal, 0, kExprI8Const, |
679 1, kExprI8Const, 2}; | 632 1, kExprI8Const, 2}; |
680 EXPECT_FAILURE(&env_i_f, kCode); | 633 EXPECT_FAILURE(sigs.i_f(), kCode); |
681 EXPECT_FAILURE(&env_i_d, kCode); | 634 EXPECT_FAILURE(sigs.i_d(), kCode); |
682 } | 635 } |
683 { | 636 { |
684 // 1 ? float|double : 2 | 637 // 1 ? float|double : 2 |
685 static const byte kCode[] = {kExprIfElse, kExprI8Const, 1, kExprGetLocal, | 638 static const byte kCode[] = {kExprIfElse, kExprI8Const, 1, kExprGetLocal, |
686 0, kExprI8Const, 2}; | 639 0, kExprI8Const, 2}; |
687 EXPECT_FAILURE(&env_i_f, kCode); | 640 EXPECT_FAILURE(sigs.i_f(), kCode); |
688 EXPECT_FAILURE(&env_i_d, kCode); | 641 EXPECT_FAILURE(sigs.i_d(), kCode); |
689 } | 642 } |
690 { | 643 { |
691 // stmt ? 0 : 1 | 644 // stmt ? 0 : 1 |
692 static const byte kCode[] = {kExprIfElse, kExprNop, kExprI8Const, | 645 static const byte kCode[] = {kExprIfElse, kExprNop, kExprI8Const, |
693 0, kExprI8Const, 1}; | 646 0, kExprI8Const, 1}; |
694 EXPECT_FAILURE(&env_i_i, kCode); | 647 EXPECT_FAILURE(sigs.i_i(), kCode); |
695 } | 648 } |
696 { | 649 { |
697 // 0 ? stmt : 1 | 650 // 0 ? stmt : 1 |
698 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, | 651 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, |
699 kExprNop, kExprI8Const, 1}; | 652 kExprNop, kExprI8Const, 1}; |
700 EXPECT_FAILURE(&env_i_i, kCode); | 653 EXPECT_FAILURE(sigs.i_i(), kCode); |
701 } | 654 } |
702 { | 655 { |
703 // 0 ? 1 : stmt | 656 // 0 ? 1 : stmt |
704 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, kExprI8Const, 1, | 657 static const byte kCode[] = {kExprIfElse, kExprI8Const, 0, kExprI8Const, 1, |
705 0, kExprBlock}; | 658 0, kExprBlock}; |
706 EXPECT_FAILURE(&env_i_i, kCode); | 659 EXPECT_FAILURE(sigs.i_i(), kCode); |
707 } | 660 } |
708 } | 661 } |
709 | 662 |
710 TEST_F(AstDecoderTest, Int64Local_param) { | 663 TEST_F(AstDecoderTest, Int64Local_param) { |
711 EXPECT_VERIFIES(&env_l_l, kCodeGetLocal0); | 664 EXPECT_VERIFIES(sigs.l_l(), kCodeGetLocal0); |
712 } | 665 } |
713 | 666 |
714 TEST_F(AstDecoderTest, Int64Locals) { | 667 TEST_F(AstDecoderTest, Int64Locals) { |
715 for (byte i = 1; i < 8; i++) { | 668 for (byte i = 1; i < 8; i++) { |
716 FunctionEnv env; | 669 AddLocals(kAstI64, 1); |
717 init_env(&env, sigs.l_v()); | |
718 env.AddLocals(kAstI64, i); | |
719 for (byte j = 0; j < i; j++) { | 670 for (byte j = 0; j < i; j++) { |
720 byte code[] = {kExprGetLocal, j}; | 671 byte code[] = {kExprGetLocal, j}; |
721 EXPECT_VERIFIES(&env, code); | 672 EXPECT_VERIFIES(sigs.l_v(), code); |
722 } | 673 } |
723 } | 674 } |
724 } | 675 } |
725 | 676 |
726 TEST_F(AstDecoderTest, Int32Binops) { | 677 TEST_F(AstDecoderTest, Int32Binops) { |
727 TestBinop(kExprI32Add, sigs.i_ii()); | 678 TestBinop(kExprI32Add, sigs.i_ii()); |
728 TestBinop(kExprI32Sub, sigs.i_ii()); | 679 TestBinop(kExprI32Sub, sigs.i_ii()); |
729 TestBinop(kExprI32Mul, sigs.i_ii()); | 680 TestBinop(kExprI32Mul, sigs.i_ii()); |
730 TestBinop(kExprI32DivS, sigs.i_ii()); | 681 TestBinop(kExprI32DivS, sigs.i_ii()); |
731 TestBinop(kExprI32DivU, sigs.i_ii()); | 682 TestBinop(kExprI32DivU, sigs.i_ii()); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 VERIFY(WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); | 736 VERIFY(WASM_IF(WASM_GET_LOCAL(0), WASM_NOP)); |
786 VERIFY(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); | 737 VERIFY(WASM_IF_ELSE(WASM_GET_LOCAL(0), WASM_NOP, WASM_NOP)); |
787 VERIFY(WASM_NOP); | 738 VERIFY(WASM_NOP); |
788 VERIFY(WASM_BLOCK(1, WASM_NOP)); | 739 VERIFY(WASM_BLOCK(1, WASM_NOP)); |
789 VERIFY(WASM_LOOP(1, WASM_NOP)); | 740 VERIFY(WASM_LOOP(1, WASM_NOP)); |
790 VERIFY(WASM_LOOP(1, WASM_BREAK(0))); | 741 VERIFY(WASM_LOOP(1, WASM_BREAK(0))); |
791 VERIFY(WASM_LOOP(1, WASM_CONTINUE(0))); | 742 VERIFY(WASM_LOOP(1, WASM_CONTINUE(0))); |
792 } | 743 } |
793 | 744 |
794 TEST_F(AstDecoderTest, MacrosBreak) { | 745 TEST_F(AstDecoderTest, MacrosBreak) { |
795 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BREAK(0))); | 746 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BREAK(0))); |
796 | 747 |
797 EXPECT_VERIFIES_INLINE(&env_i_i, WASM_LOOP(1, WASM_BREAKV(0, WASM_ZERO))); | 748 EXPECT_VERIFIES_INLINE(sigs.i_i(), WASM_LOOP(1, WASM_BREAKV(0, WASM_ZERO))); |
798 EXPECT_VERIFIES_INLINE(&env_l_l, | 749 EXPECT_VERIFIES_INLINE(sigs.l_l(), |
799 WASM_LOOP(1, WASM_BREAKV(0, WASM_I64V_1(0)))); | 750 WASM_LOOP(1, WASM_BREAKV(0, WASM_I64V_1(0)))); |
800 EXPECT_VERIFIES_INLINE(&env_f_ff, | 751 EXPECT_VERIFIES_INLINE(sigs.f_ff(), |
801 WASM_LOOP(1, WASM_BREAKV(0, WASM_F32(0.0)))); | 752 WASM_LOOP(1, WASM_BREAKV(0, WASM_F32(0.0)))); |
802 EXPECT_VERIFIES_INLINE(&env_d_dd, | 753 EXPECT_VERIFIES_INLINE(sigs.d_dd(), |
803 WASM_LOOP(1, WASM_BREAKV(0, WASM_F64(0.0)))); | 754 WASM_LOOP(1, WASM_BREAKV(0, WASM_F64(0.0)))); |
804 } | 755 } |
805 | 756 |
806 TEST_F(AstDecoderTest, MacrosContinue) { | 757 TEST_F(AstDecoderTest, MacrosContinue) { |
807 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_CONTINUE(0))); | 758 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_CONTINUE(0))); |
808 } | 759 } |
809 | 760 |
810 TEST_F(AstDecoderTest, MacrosVariadic) { | 761 TEST_F(AstDecoderTest, MacrosVariadic) { |
811 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_NOP)); | 762 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_NOP)); |
812 VERIFY(WASM_BLOCK(3, WASM_NOP, WASM_NOP, WASM_NOP)); | 763 VERIFY(WASM_BLOCK(3, WASM_NOP, WASM_NOP, WASM_NOP)); |
813 VERIFY(WASM_LOOP(2, WASM_NOP, WASM_NOP)); | 764 VERIFY(WASM_LOOP(2, WASM_NOP, WASM_NOP)); |
814 VERIFY(WASM_LOOP(3, WASM_NOP, WASM_NOP, WASM_NOP)); | 765 VERIFY(WASM_LOOP(3, WASM_NOP, WASM_NOP, WASM_NOP)); |
815 } | 766 } |
816 | 767 |
817 TEST_F(AstDecoderTest, MacrosNestedBlocks) { | 768 TEST_F(AstDecoderTest, MacrosNestedBlocks) { |
818 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_BLOCK(2, WASM_NOP, WASM_NOP))); | 769 VERIFY(WASM_BLOCK(2, WASM_NOP, WASM_BLOCK(2, WASM_NOP, WASM_NOP))); |
819 VERIFY(WASM_BLOCK(3, WASM_NOP, // -- | 770 VERIFY(WASM_BLOCK(3, WASM_NOP, // -- |
820 WASM_BLOCK(2, WASM_NOP, WASM_NOP), // -- | 771 WASM_BLOCK(2, WASM_NOP, WASM_NOP), // -- |
821 WASM_BLOCK(2, WASM_NOP, WASM_NOP))); // -- | 772 WASM_BLOCK(2, WASM_NOP, WASM_NOP))); // -- |
822 VERIFY(WASM_BLOCK(1, WASM_BLOCK(1, WASM_BLOCK(2, WASM_NOP, WASM_NOP)))); | 773 VERIFY(WASM_BLOCK(1, WASM_BLOCK(1, WASM_BLOCK(2, WASM_NOP, WASM_NOP)))); |
823 } | 774 } |
824 | 775 |
825 TEST_F(AstDecoderTest, MultipleReturn) { | 776 TEST_F(AstDecoderTest, MultipleReturn) { |
826 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; | 777 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; |
827 FunctionSig sig_ii_v(2, 0, kIntTypes5); | 778 FunctionSig sig_ii_v(2, 0, kIntTypes5); |
828 FunctionEnv env_ii_v; | 779 EXPECT_VERIFIES_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); |
829 init_env(&env_ii_v, &sig_ii_v); | 780 EXPECT_FAILURE_INLINE(&sig_ii_v, WASM_RETURN(WASM_ZERO)); |
830 EXPECT_VERIFIES_INLINE(&env_ii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); | |
831 EXPECT_FAILURE_INLINE(&env_ii_v, WASM_RETURN(WASM_ZERO)); | |
832 | 781 |
833 FunctionSig sig_iii_v(3, 0, kIntTypes5); | 782 FunctionSig sig_iii_v(3, 0, kIntTypes5); |
834 FunctionEnv env_iii_v; | 783 EXPECT_VERIFIES_INLINE(&sig_iii_v, |
835 init_env(&env_iii_v, &sig_iii_v); | |
836 EXPECT_VERIFIES_INLINE(&env_iii_v, | |
837 WASM_RETURN(WASM_ZERO, WASM_ONE, WASM_I8(44))); | 784 WASM_RETURN(WASM_ZERO, WASM_ONE, WASM_I8(44))); |
838 EXPECT_FAILURE_INLINE(&env_iii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); | 785 EXPECT_FAILURE_INLINE(&sig_iii_v, WASM_RETURN(WASM_ZERO, WASM_ONE)); |
839 } | 786 } |
840 | 787 |
841 TEST_F(AstDecoderTest, MultipleReturn_fallthru) { | 788 TEST_F(AstDecoderTest, MultipleReturn_fallthru) { |
842 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; | 789 static LocalType kIntTypes5[] = {kAstI32, kAstI32, kAstI32, kAstI32, kAstI32}; |
843 FunctionSig sig_ii_v(2, 0, kIntTypes5); | 790 FunctionSig sig_ii_v(2, 0, kIntTypes5); |
844 FunctionEnv env_ii_v; | |
845 init_env(&env_ii_v, &sig_ii_v); | |
846 | 791 |
847 EXPECT_VERIFIES_INLINE(&env_ii_v, WASM_ZERO, WASM_ONE); | 792 EXPECT_VERIFIES_INLINE(&sig_ii_v, WASM_ZERO, WASM_ONE); |
848 EXPECT_FAILURE_INLINE(&env_ii_v, WASM_ZERO); | 793 EXPECT_FAILURE_INLINE(&sig_ii_v, WASM_ZERO); |
849 | 794 |
850 FunctionSig sig_iii_v(3, 0, kIntTypes5); | 795 FunctionSig sig_iii_v(3, 0, kIntTypes5); |
851 FunctionEnv env_iii_v; | 796 EXPECT_VERIFIES_INLINE(&sig_iii_v, WASM_ZERO, WASM_ONE, WASM_I8(44)); |
852 init_env(&env_iii_v, &sig_iii_v); | 797 EXPECT_FAILURE_INLINE(&sig_iii_v, WASM_ZERO, WASM_ONE); |
853 EXPECT_VERIFIES_INLINE(&env_iii_v, WASM_ZERO, WASM_ONE, WASM_I8(44)); | |
854 EXPECT_FAILURE_INLINE(&env_iii_v, WASM_ZERO, WASM_ONE); | |
855 } | 798 } |
856 | 799 |
857 TEST_F(AstDecoderTest, MacrosInt32) { | 800 TEST_F(AstDecoderTest, MacrosInt32) { |
858 VERIFY(WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_I8(12))); | 801 VERIFY(WASM_I32_ADD(WASM_GET_LOCAL(0), WASM_I8(12))); |
859 VERIFY(WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(13))); | 802 VERIFY(WASM_I32_SUB(WASM_GET_LOCAL(0), WASM_I8(13))); |
860 VERIFY(WASM_I32_MUL(WASM_GET_LOCAL(0), WASM_I8(14))); | 803 VERIFY(WASM_I32_MUL(WASM_GET_LOCAL(0), WASM_I8(14))); |
861 VERIFY(WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I8(15))); | 804 VERIFY(WASM_I32_DIVS(WASM_GET_LOCAL(0), WASM_I8(15))); |
862 VERIFY(WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_I8(16))); | 805 VERIFY(WASM_I32_DIVU(WASM_GET_LOCAL(0), WASM_I8(16))); |
863 VERIFY(WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_I8(17))); | 806 VERIFY(WASM_I32_REMS(WASM_GET_LOCAL(0), WASM_I8(17))); |
864 VERIFY(WASM_I32_REMU(WASM_GET_LOCAL(0), WASM_I8(18))); | 807 VERIFY(WASM_I32_REMU(WASM_GET_LOCAL(0), WASM_I8(18))); |
(...skipping 13 matching lines...) Expand all Loading... |
878 VERIFY(WASM_I32_LTU(WASM_GET_LOCAL(0), WASM_I8(28))); | 821 VERIFY(WASM_I32_LTU(WASM_GET_LOCAL(0), WASM_I8(28))); |
879 VERIFY(WASM_I32_LEU(WASM_GET_LOCAL(0), WASM_I8(29))); | 822 VERIFY(WASM_I32_LEU(WASM_GET_LOCAL(0), WASM_I8(29))); |
880 | 823 |
881 VERIFY(WASM_I32_GTS(WASM_GET_LOCAL(0), WASM_I8(26))); | 824 VERIFY(WASM_I32_GTS(WASM_GET_LOCAL(0), WASM_I8(26))); |
882 VERIFY(WASM_I32_GES(WASM_GET_LOCAL(0), WASM_I8(27))); | 825 VERIFY(WASM_I32_GES(WASM_GET_LOCAL(0), WASM_I8(27))); |
883 VERIFY(WASM_I32_GTU(WASM_GET_LOCAL(0), WASM_I8(28))); | 826 VERIFY(WASM_I32_GTU(WASM_GET_LOCAL(0), WASM_I8(28))); |
884 VERIFY(WASM_I32_GEU(WASM_GET_LOCAL(0), WASM_I8(29))); | 827 VERIFY(WASM_I32_GEU(WASM_GET_LOCAL(0), WASM_I8(29))); |
885 } | 828 } |
886 | 829 |
887 TEST_F(AstDecoderTest, MacrosInt64) { | 830 TEST_F(AstDecoderTest, MacrosInt64) { |
888 FunctionEnv env_i_ll; | 831 #define VERIFY_L_LL(...) EXPECT_VERIFIES_INLINE(sigs.l_ll(), __VA_ARGS__) |
889 FunctionEnv env_l_ll; | 832 #define VERIFY_I_LL(...) EXPECT_VERIFIES_INLINE(sigs.i_ll(), __VA_ARGS__) |
890 init_env(&env_i_ll, sigs.i_ll()); | |
891 init_env(&env_l_ll, sigs.l_ll()); | |
892 | |
893 #define VERIFY_L_LL(...) EXPECT_VERIFIES_INLINE(&env_l_ll, __VA_ARGS__) | |
894 #define VERIFY_I_LL(...) EXPECT_VERIFIES_INLINE(&env_i_ll, __VA_ARGS__) | |
895 | 833 |
896 VERIFY_L_LL(WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_I64V_1(12))); | 834 VERIFY_L_LL(WASM_I64_ADD(WASM_GET_LOCAL(0), WASM_I64V_1(12))); |
897 VERIFY_L_LL(WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_I64V_1(13))); | 835 VERIFY_L_LL(WASM_I64_SUB(WASM_GET_LOCAL(0), WASM_I64V_1(13))); |
898 VERIFY_L_LL(WASM_I64_MUL(WASM_GET_LOCAL(0), WASM_I64V_1(14))); | 836 VERIFY_L_LL(WASM_I64_MUL(WASM_GET_LOCAL(0), WASM_I64V_1(14))); |
899 VERIFY_L_LL(WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_I64V_1(15))); | 837 VERIFY_L_LL(WASM_I64_DIVS(WASM_GET_LOCAL(0), WASM_I64V_1(15))); |
900 VERIFY_L_LL(WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_I64V_1(16))); | 838 VERIFY_L_LL(WASM_I64_DIVU(WASM_GET_LOCAL(0), WASM_I64V_1(16))); |
901 VERIFY_L_LL(WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_I64V_1(17))); | 839 VERIFY_L_LL(WASM_I64_REMS(WASM_GET_LOCAL(0), WASM_I64V_1(17))); |
902 VERIFY_L_LL(WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_I64V_1(18))); | 840 VERIFY_L_LL(WASM_I64_REMU(WASM_GET_LOCAL(0), WASM_I64V_1(18))); |
903 VERIFY_L_LL(WASM_I64_AND(WASM_GET_LOCAL(0), WASM_I64V_1(19))); | 841 VERIFY_L_LL(WASM_I64_AND(WASM_GET_LOCAL(0), WASM_I64V_1(19))); |
904 VERIFY_L_LL(WASM_I64_IOR(WASM_GET_LOCAL(0), WASM_I64V_1(20))); | 842 VERIFY_L_LL(WASM_I64_IOR(WASM_GET_LOCAL(0), WASM_I64V_1(20))); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 } \ | 874 } \ |
937 } | 875 } |
938 | 876 |
939 FOREACH_SIMPLE_OPCODE(DECODE_TEST); | 877 FOREACH_SIMPLE_OPCODE(DECODE_TEST); |
940 | 878 |
941 #undef DECODE_TEST | 879 #undef DECODE_TEST |
942 } | 880 } |
943 | 881 |
944 TEST_F(AstDecoderTest, MemorySize) { | 882 TEST_F(AstDecoderTest, MemorySize) { |
945 byte code[] = {kExprMemorySize}; | 883 byte code[] = {kExprMemorySize}; |
946 EXPECT_VERIFIES(&env_i_i, code); | 884 EXPECT_VERIFIES(sigs.i_i(), code); |
947 EXPECT_FAILURE(&env_f_ff, code); | 885 EXPECT_FAILURE(sigs.f_ff(), code); |
948 } | 886 } |
949 | 887 |
950 TEST_F(AstDecoderTest, GrowMemory) { | 888 TEST_F(AstDecoderTest, GrowMemory) { |
951 byte code[] = {kExprGrowMemory, kExprGetLocal, 0}; | 889 byte code[] = {kExprGrowMemory, kExprGetLocal, 0}; |
952 EXPECT_VERIFIES(&env_i_i, code); | 890 EXPECT_VERIFIES(sigs.i_i(), code); |
953 EXPECT_FAILURE(&env_i_d, code); | 891 EXPECT_FAILURE(sigs.i_d(), code); |
954 } | 892 } |
955 | 893 |
956 TEST_F(AstDecoderTest, LoadMemOffset) { | 894 TEST_F(AstDecoderTest, LoadMemOffset) { |
957 for (int offset = 0; offset < 128; offset += 7) { | 895 for (int offset = 0; offset < 128; offset += 7) { |
958 byte code[] = {kExprI32LoadMem, WasmOpcodes::LoadStoreAccessOf(true), | 896 byte code[] = {kExprI32LoadMem, WasmOpcodes::LoadStoreAccessOf(true), |
959 static_cast<byte>(offset), kExprI8Const, 0}; | 897 static_cast<byte>(offset), kExprI8Const, 0}; |
960 EXPECT_VERIFIES(&env_i_i, code); | 898 EXPECT_VERIFIES(sigs.i_i(), code); |
961 } | 899 } |
962 } | 900 } |
963 | 901 |
964 TEST_F(AstDecoderTest, StoreMemOffset) { | 902 TEST_F(AstDecoderTest, StoreMemOffset) { |
965 for (int offset = 0; offset < 128; offset += 7) { | 903 for (int offset = 0; offset < 128; offset += 7) { |
966 byte code[] = {kExprI32StoreMem, | 904 byte code[] = {kExprI32StoreMem, |
967 WasmOpcodes::LoadStoreAccessOf(true), | 905 WasmOpcodes::LoadStoreAccessOf(true), |
968 static_cast<byte>(offset), | 906 static_cast<byte>(offset), |
969 kExprI8Const, | 907 kExprI8Const, |
970 0, | 908 0, |
971 kExprI8Const, | 909 kExprI8Const, |
972 0}; | 910 0}; |
973 EXPECT_VERIFIES(&env_i_i, code); | 911 EXPECT_VERIFIES(sigs.i_i(), code); |
974 } | 912 } |
975 } | 913 } |
976 | 914 |
977 TEST_F(AstDecoderTest, LoadMemOffset_varint) { | 915 TEST_F(AstDecoderTest, LoadMemOffset_varint) { |
978 byte code1[] = {kExprI32LoadMem, WasmOpcodes::LoadStoreAccessOf(true), 0, | 916 byte code1[] = {kExprI32LoadMem, WasmOpcodes::LoadStoreAccessOf(true), 0, |
979 kExprI8Const, 0}; | 917 kExprI8Const, 0}; |
980 byte code2[] = {kExprI32LoadMem, | 918 byte code2[] = {kExprI32LoadMem, |
981 WasmOpcodes::LoadStoreAccessOf(true), | 919 WasmOpcodes::LoadStoreAccessOf(true), |
982 0x80, | 920 0x80, |
983 1, | 921 1, |
984 kExprI8Const, | 922 kExprI8Const, |
985 0}; | 923 0}; |
986 byte code3[] = {kExprI32LoadMem, | 924 byte code3[] = {kExprI32LoadMem, |
987 WasmOpcodes::LoadStoreAccessOf(true), | 925 WasmOpcodes::LoadStoreAccessOf(true), |
988 0x81, | 926 0x81, |
989 0x82, | 927 0x82, |
990 5, | 928 5, |
991 kExprI8Const, | 929 kExprI8Const, |
992 0}; | 930 0}; |
993 byte code4[] = {kExprI32LoadMem, | 931 byte code4[] = {kExprI32LoadMem, |
994 WasmOpcodes::LoadStoreAccessOf(true), | 932 WasmOpcodes::LoadStoreAccessOf(true), |
995 0x83, | 933 0x83, |
996 0x84, | 934 0x84, |
997 0x85, | 935 0x85, |
998 7, | 936 7, |
999 kExprI8Const, | 937 kExprI8Const, |
1000 0}; | 938 0}; |
1001 | 939 |
1002 EXPECT_VERIFIES(&env_i_i, code1); | 940 EXPECT_VERIFIES(sigs.i_i(), code1); |
1003 EXPECT_VERIFIES(&env_i_i, code2); | 941 EXPECT_VERIFIES(sigs.i_i(), code2); |
1004 EXPECT_VERIFIES(&env_i_i, code3); | 942 EXPECT_VERIFIES(sigs.i_i(), code3); |
1005 EXPECT_VERIFIES(&env_i_i, code4); | 943 EXPECT_VERIFIES(sigs.i_i(), code4); |
1006 } | 944 } |
1007 | 945 |
1008 TEST_F(AstDecoderTest, StoreMemOffset_varint) { | 946 TEST_F(AstDecoderTest, StoreMemOffset_varint) { |
1009 byte code1[] = {kExprI32StoreMem, | 947 byte code1[] = {kExprI32StoreMem, |
1010 WasmOpcodes::LoadStoreAccessOf(true), | 948 WasmOpcodes::LoadStoreAccessOf(true), |
1011 0, | 949 0, |
1012 kExprI8Const, | 950 kExprI8Const, |
1013 0, | 951 0, |
1014 kExprI8Const, | 952 kExprI8Const, |
1015 0}; | 953 0}; |
(...skipping 18 matching lines...) Expand all Loading... |
1034 WasmOpcodes::LoadStoreAccessOf(true), | 972 WasmOpcodes::LoadStoreAccessOf(true), |
1035 0x83, | 973 0x83, |
1036 0x84, | 974 0x84, |
1037 0x85, | 975 0x85, |
1038 7, | 976 7, |
1039 kExprI8Const, | 977 kExprI8Const, |
1040 0, | 978 0, |
1041 kExprI8Const, | 979 kExprI8Const, |
1042 0}; | 980 0}; |
1043 | 981 |
1044 EXPECT_VERIFIES(&env_i_i, code1); | 982 EXPECT_VERIFIES(sigs.i_i(), code1); |
1045 EXPECT_VERIFIES(&env_i_i, code2); | 983 EXPECT_VERIFIES(sigs.i_i(), code2); |
1046 EXPECT_VERIFIES(&env_i_i, code3); | 984 EXPECT_VERIFIES(sigs.i_i(), code3); |
1047 EXPECT_VERIFIES(&env_i_i, code4); | 985 EXPECT_VERIFIES(sigs.i_i(), code4); |
1048 } | 986 } |
1049 | 987 |
1050 TEST_F(AstDecoderTest, AllLoadMemCombinations) { | 988 TEST_F(AstDecoderTest, AllLoadMemCombinations) { |
1051 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 989 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1052 LocalType local_type = kLocalTypes[i]; | 990 LocalType local_type = kLocalTypes[i]; |
1053 for (size_t j = 0; j < arraysize(machineTypes); j++) { | 991 for (size_t j = 0; j < arraysize(machineTypes); j++) { |
1054 MachineType mem_type = machineTypes[j]; | 992 MachineType mem_type = machineTypes[j]; |
1055 byte code[] = { | 993 byte code[] = { |
1056 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, false)), | 994 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, false)), |
1057 WasmOpcodes::LoadStoreAccessOf(false), kExprI8Const, 0}; | 995 WasmOpcodes::LoadStoreAccessOf(false), kExprI8Const, 0}; |
1058 FunctionEnv env; | |
1059 FunctionSig sig(1, 0, &local_type); | 996 FunctionSig sig(1, 0, &local_type); |
1060 init_env(&env, &sig); | |
1061 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { | 997 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { |
1062 EXPECT_VERIFIES(&env, code); | 998 EXPECT_VERIFIES(&sig, code); |
1063 } else { | 999 } else { |
1064 EXPECT_FAILURE(&env, code); | 1000 EXPECT_FAILURE(&sig, code); |
1065 } | 1001 } |
1066 } | 1002 } |
1067 } | 1003 } |
1068 } | 1004 } |
1069 | 1005 |
1070 TEST_F(AstDecoderTest, AllStoreMemCombinations) { | 1006 TEST_F(AstDecoderTest, AllStoreMemCombinations) { |
1071 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1007 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1072 LocalType local_type = kLocalTypes[i]; | 1008 LocalType local_type = kLocalTypes[i]; |
1073 for (size_t j = 0; j < arraysize(machineTypes); j++) { | 1009 for (size_t j = 0; j < arraysize(machineTypes); j++) { |
1074 MachineType mem_type = machineTypes[j]; | 1010 MachineType mem_type = machineTypes[j]; |
1075 byte code[] = { | 1011 byte code[] = { |
1076 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, true)), | 1012 static_cast<byte>(WasmOpcodes::LoadStoreOpcodeOf(mem_type, true)), |
1077 WasmOpcodes::LoadStoreAccessOf(false), | 1013 WasmOpcodes::LoadStoreAccessOf(false), |
1078 kExprI8Const, | 1014 kExprI8Const, |
1079 0, | 1015 0, |
1080 kExprGetLocal, | 1016 kExprGetLocal, |
1081 0}; | 1017 0}; |
1082 FunctionEnv env; | |
1083 FunctionSig sig(0, 1, &local_type); | 1018 FunctionSig sig(0, 1, &local_type); |
1084 init_env(&env, &sig); | |
1085 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { | 1019 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { |
1086 EXPECT_VERIFIES(&env, code); | 1020 EXPECT_VERIFIES(&sig, code); |
1087 } else { | 1021 } else { |
1088 EXPECT_FAILURE(&env, code); | 1022 EXPECT_FAILURE(&sig, code); |
1089 } | 1023 } |
1090 } | 1024 } |
1091 } | 1025 } |
1092 } | 1026 } |
1093 | 1027 |
1094 | 1028 |
1095 namespace { | 1029 namespace { |
1096 // A helper for tests that require a module environment for functions and | 1030 // A helper for tests that require a module environment for functions and |
1097 // globals. | 1031 // globals. |
1098 class TestModuleEnv : public ModuleEnv { | 1032 class TestModuleEnv : public ModuleEnv { |
(...skipping 23 matching lines...) Expand all Loading... |
1122 CHECK(mod.import_table.size() <= 127); | 1056 CHECK(mod.import_table.size() <= 127); |
1123 return static_cast<byte>(mod.import_table.size() - 1); | 1057 return static_cast<byte>(mod.import_table.size() - 1); |
1124 } | 1058 } |
1125 | 1059 |
1126 private: | 1060 private: |
1127 WasmModule mod; | 1061 WasmModule mod; |
1128 }; | 1062 }; |
1129 } // namespace | 1063 } // namespace |
1130 | 1064 |
1131 TEST_F(AstDecoderTest, SimpleCalls) { | 1065 TEST_F(AstDecoderTest, SimpleCalls) { |
1132 FunctionEnv* env = &env_i_i; | 1066 FunctionSig* sig = sigs.i_i(); |
1133 TestModuleEnv module_env; | 1067 TestModuleEnv module_env; |
1134 env->module = &module_env; | 1068 module = &module_env; |
1135 | 1069 |
1136 module_env.AddFunction(sigs.i_v()); | 1070 module_env.AddFunction(sigs.i_v()); |
1137 module_env.AddFunction(sigs.i_i()); | 1071 module_env.AddFunction(sigs.i_i()); |
1138 module_env.AddFunction(sigs.i_ii()); | 1072 module_env.AddFunction(sigs.i_ii()); |
1139 | 1073 |
1140 EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(0)); | 1074 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(0)); |
1141 EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I8(27))); | 1075 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I8(27))); |
1142 EXPECT_VERIFIES_INLINE(env, WASM_CALL_FUNCTION(2, WASM_I8(37), WASM_I8(77))); | 1076 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_I8(37), WASM_I8(77))); |
1143 } | 1077 } |
1144 | 1078 |
1145 TEST_F(AstDecoderTest, CallsWithTooFewArguments) { | 1079 TEST_F(AstDecoderTest, CallsWithTooFewArguments) { |
1146 FunctionEnv* env = &env_i_i; | 1080 FunctionSig* sig = sigs.i_i(); |
1147 TestModuleEnv module_env; | 1081 TestModuleEnv module_env; |
1148 env->module = &module_env; | 1082 module = &module_env; |
1149 | 1083 |
1150 module_env.AddFunction(sigs.i_i()); | 1084 module_env.AddFunction(sigs.i_i()); |
1151 module_env.AddFunction(sigs.i_ii()); | 1085 module_env.AddFunction(sigs.i_ii()); |
1152 module_env.AddFunction(sigs.f_ff()); | 1086 module_env.AddFunction(sigs.f_ff()); |
1153 | 1087 |
1154 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION0(0)); | 1088 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION0(0)); |
1155 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_ZERO)); | 1089 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_ZERO)); |
1156 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0))); | 1090 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(2, WASM_GET_LOCAL(0))); |
1157 } | 1091 } |
1158 | 1092 |
1159 TEST_F(AstDecoderTest, CallsWithSpilloverArgs) { | 1093 TEST_F(AstDecoderTest, CallsWithSpilloverArgs) { |
1160 static LocalType a_i_ff[] = {kAstI32, kAstF32, kAstF32}; | 1094 static LocalType a_i_ff[] = {kAstI32, kAstF32, kAstF32}; |
1161 FunctionSig sig_i_ff(1, 2, a_i_ff); | 1095 FunctionSig sig_i_ff(1, 2, a_i_ff); |
1162 FunctionEnv env_i_ff; | |
1163 init_env(&env_i_ff, &sig_i_ff); | |
1164 | 1096 |
1165 TestModuleEnv module_env; | 1097 TestModuleEnv module_env; |
1166 env_i_ff.module = &module_env; | 1098 module = &module_env; |
1167 env_i_i.module = &module_env; | |
1168 env_f_ff.module = &module_env; | |
1169 | 1099 |
1170 module_env.AddFunction(&sig_i_ff); | 1100 module_env.AddFunction(&sig_i_ff); |
1171 | 1101 |
1172 EXPECT_VERIFIES_INLINE(&env_i_i, | 1102 EXPECT_VERIFIES_INLINE(sigs.i_i(), |
1173 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); | 1103 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); |
1174 | 1104 |
1175 EXPECT_VERIFIES_INLINE(&env_i_ff, | 1105 EXPECT_VERIFIES_INLINE(sigs.i_ff(), |
1176 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); | 1106 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); |
1177 | 1107 |
1178 EXPECT_FAILURE_INLINE(&env_f_ff, | 1108 EXPECT_FAILURE_INLINE(sigs.f_ff(), |
1179 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); | 1109 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1))); |
1180 | 1110 |
1181 EXPECT_FAILURE_INLINE( | 1111 EXPECT_FAILURE_INLINE( |
1182 &env_i_i, | 1112 sigs.i_i(), |
1183 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(0.2))); | 1113 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(0.2))); |
1184 | 1114 |
1185 EXPECT_VERIFIES_INLINE( | 1115 EXPECT_VERIFIES_INLINE( |
1186 &env_f_ff, | 1116 sigs.f_ff(), |
1187 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(11))); | 1117 WASM_CALL_FUNCTION(0, WASM_F32(0.1), WASM_F32(0.1), WASM_F32(11))); |
1188 } | 1118 } |
1189 | 1119 |
1190 TEST_F(AstDecoderTest, CallsWithMismatchedSigs2) { | 1120 TEST_F(AstDecoderTest, CallsWithMismatchedSigs2) { |
1191 FunctionEnv* env = &env_i_i; | 1121 FunctionSig* sig = sigs.i_i(); |
1192 TestModuleEnv module_env; | 1122 TestModuleEnv module_env; |
1193 env->module = &module_env; | 1123 module = &module_env; |
1194 | 1124 |
1195 module_env.AddFunction(sigs.i_i()); | 1125 module_env.AddFunction(sigs.i_i()); |
1196 | 1126 |
1197 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I64V_1(17))); | 1127 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I64V_1(17))); |
1198 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F32(17.1))); | 1128 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_F32(17.1))); |
1199 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F64(17.1))); | 1129 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_F64(17.1))); |
1200 } | 1130 } |
1201 | 1131 |
1202 TEST_F(AstDecoderTest, CallsWithMismatchedSigs3) { | 1132 TEST_F(AstDecoderTest, CallsWithMismatchedSigs3) { |
1203 FunctionEnv* env = &env_i_i; | 1133 FunctionSig* sig = sigs.i_i(); |
1204 TestModuleEnv module_env; | 1134 TestModuleEnv module_env; |
1205 env->module = &module_env; | 1135 module = &module_env; |
1206 | 1136 |
1207 module_env.AddFunction(sigs.i_f()); | 1137 module_env.AddFunction(sigs.i_f()); |
1208 | 1138 |
1209 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I8(17))); | 1139 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I8(17))); |
1210 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_I64V_1(27))); | 1140 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_I64V_1(27))); |
1211 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(0, WASM_F64(37.2))); | 1141 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(0, WASM_F64(37.2))); |
1212 | 1142 |
1213 module_env.AddFunction(sigs.i_d()); | 1143 module_env.AddFunction(sigs.i_d()); |
1214 | 1144 |
1215 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I8(16))); | 1145 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I8(16))); |
1216 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_I64V_1(16))); | 1146 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_I64V_1(16))); |
1217 EXPECT_FAILURE_INLINE(env, WASM_CALL_FUNCTION(1, WASM_F32(17.6))); | 1147 EXPECT_FAILURE_INLINE(sig, WASM_CALL_FUNCTION(1, WASM_F32(17.6))); |
1218 } | 1148 } |
1219 | 1149 |
1220 TEST_F(AstDecoderTest, SimpleIndirectCalls) { | 1150 TEST_F(AstDecoderTest, SimpleIndirectCalls) { |
1221 FunctionEnv* env = &env_i_i; | 1151 FunctionSig* sig = sigs.i_i(); |
1222 TestModuleEnv module_env; | 1152 TestModuleEnv module_env; |
1223 env->module = &module_env; | 1153 module = &module_env; |
1224 | 1154 |
1225 byte f0 = module_env.AddSignature(sigs.i_v()); | 1155 byte f0 = module_env.AddSignature(sigs.i_v()); |
1226 byte f1 = module_env.AddSignature(sigs.i_i()); | 1156 byte f1 = module_env.AddSignature(sigs.i_i()); |
1227 byte f2 = module_env.AddSignature(sigs.i_ii()); | 1157 byte f2 = module_env.AddSignature(sigs.i_ii()); |
1228 | 1158 |
1229 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); | 1159 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_ZERO)); |
1230 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(22))); | 1160 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(22))); |
1231 EXPECT_VERIFIES_INLINE( | 1161 EXPECT_VERIFIES_INLINE( |
1232 env, WASM_CALL_INDIRECT(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); | 1162 sig, WASM_CALL_INDIRECT(f2, WASM_ZERO, WASM_I8(32), WASM_I8(72))); |
1233 } | 1163 } |
1234 | 1164 |
1235 TEST_F(AstDecoderTest, IndirectCallsOutOfBounds) { | 1165 TEST_F(AstDecoderTest, IndirectCallsOutOfBounds) { |
1236 FunctionEnv* env = &env_i_i; | 1166 FunctionSig* sig = sigs.i_i(); |
1237 TestModuleEnv module_env; | 1167 TestModuleEnv module_env; |
1238 env->module = &module_env; | 1168 module = &module_env; |
1239 | 1169 |
1240 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(0, WASM_ZERO)); | 1170 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); |
1241 module_env.AddSignature(sigs.i_v()); | 1171 module_env.AddSignature(sigs.i_v()); |
1242 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT0(0, WASM_ZERO)); | 1172 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT0(0, WASM_ZERO)); |
1243 | 1173 |
1244 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(22))); | 1174 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(22))); |
1245 module_env.AddSignature(sigs.i_i()); | 1175 module_env.AddSignature(sigs.i_i()); |
1246 EXPECT_VERIFIES_INLINE(env, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(27))); | 1176 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_INDIRECT(1, WASM_ZERO, WASM_I8(27))); |
1247 | 1177 |
1248 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(2, WASM_ZERO, WASM_I8(27))); | 1178 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(2, WASM_ZERO, WASM_I8(27))); |
1249 } | 1179 } |
1250 | 1180 |
1251 TEST_F(AstDecoderTest, IndirectCallsWithMismatchedSigs3) { | 1181 TEST_F(AstDecoderTest, IndirectCallsWithMismatchedSigs3) { |
1252 FunctionEnv* env = &env_i_i; | 1182 FunctionSig* sig = sigs.i_i(); |
1253 TestModuleEnv module_env; | 1183 TestModuleEnv module_env; |
1254 env->module = &module_env; | 1184 module = &module_env; |
1255 | 1185 |
1256 byte f0 = module_env.AddFunction(sigs.i_f()); | 1186 byte f0 = module_env.AddFunction(sigs.i_f()); |
1257 | 1187 |
1258 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I8(17))); | 1188 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I8(17))); |
1259 EXPECT_FAILURE_INLINE(env, | 1189 EXPECT_FAILURE_INLINE(sig, |
1260 WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I64V_1(27))); | 1190 WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_I64V_1(27))); |
1261 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_F64(37.2))); | 1191 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f0, WASM_ZERO, WASM_F64(37.2))); |
1262 | 1192 |
1263 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_I8(17))); | 1193 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_I8(17))); |
1264 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_I64V_1(27))); | 1194 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_I64V_1(27))); |
1265 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT0(f0, WASM_F64(37.2))); | 1195 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT0(f0, WASM_F64(37.2))); |
1266 | 1196 |
1267 byte f1 = module_env.AddFunction(sigs.i_d()); | 1197 byte f1 = module_env.AddFunction(sigs.i_d()); |
1268 | 1198 |
1269 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(16))); | 1199 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I8(16))); |
1270 EXPECT_FAILURE_INLINE(env, | 1200 EXPECT_FAILURE_INLINE(sig, |
1271 WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I64V_1(16))); | 1201 WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_I64V_1(16))); |
1272 EXPECT_FAILURE_INLINE(env, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_F32(17.6))); | 1202 EXPECT_FAILURE_INLINE(sig, WASM_CALL_INDIRECT(f1, WASM_ZERO, WASM_F32(17.6))); |
1273 } | 1203 } |
1274 | 1204 |
1275 TEST_F(AstDecoderTest, SimpleImportCalls) { | 1205 TEST_F(AstDecoderTest, SimpleImportCalls) { |
1276 FunctionEnv* env = &env_i_i; | 1206 FunctionSig* sig = sigs.i_i(); |
1277 TestModuleEnv module_env; | 1207 TestModuleEnv module_env; |
1278 env->module = &module_env; | 1208 module = &module_env; |
1279 | 1209 |
1280 byte f0 = module_env.AddImport(sigs.i_v()); | 1210 byte f0 = module_env.AddImport(sigs.i_v()); |
1281 byte f1 = module_env.AddImport(sigs.i_i()); | 1211 byte f1 = module_env.AddImport(sigs.i_i()); |
1282 byte f2 = module_env.AddImport(sigs.i_ii()); | 1212 byte f2 = module_env.AddImport(sigs.i_ii()); |
1283 | 1213 |
1284 EXPECT_VERIFIES_INLINE(env, WASM_CALL_IMPORT0(f0)); | 1214 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_IMPORT0(f0)); |
1285 EXPECT_VERIFIES_INLINE(env, WASM_CALL_IMPORT(f1, WASM_I8(22))); | 1215 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_I8(22))); |
1286 EXPECT_VERIFIES_INLINE(env, WASM_CALL_IMPORT(f2, WASM_I8(32), WASM_I8(72))); | 1216 EXPECT_VERIFIES_INLINE(sig, WASM_CALL_IMPORT(f2, WASM_I8(32), WASM_I8(72))); |
1287 } | 1217 } |
1288 | 1218 |
1289 TEST_F(AstDecoderTest, ImportCallsWithMismatchedSigs3) { | 1219 TEST_F(AstDecoderTest, ImportCallsWithMismatchedSigs3) { |
1290 FunctionEnv* env = &env_i_i; | 1220 FunctionSig* sig = sigs.i_i(); |
1291 TestModuleEnv module_env; | 1221 TestModuleEnv module_env; |
1292 env->module = &module_env; | 1222 module = &module_env; |
1293 | 1223 |
1294 byte f0 = module_env.AddImport(sigs.i_f()); | 1224 byte f0 = module_env.AddImport(sigs.i_f()); |
1295 | 1225 |
1296 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT0(f0)); | 1226 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT0(f0)); |
1297 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f0, WASM_I8(17))); | 1227 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f0, WASM_I8(17))); |
1298 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f0, WASM_I64V_1(27))); | 1228 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f0, WASM_I64V_1(27))); |
1299 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f0, WASM_F64(37.2))); | 1229 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f0, WASM_F64(37.2))); |
1300 | 1230 |
1301 byte f1 = module_env.AddImport(sigs.i_d()); | 1231 byte f1 = module_env.AddImport(sigs.i_d()); |
1302 | 1232 |
1303 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT0(f1)); | 1233 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT0(f1)); |
1304 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f1, WASM_I8(16))); | 1234 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_I8(16))); |
1305 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f1, WASM_I64V_1(16))); | 1235 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_I64V_1(16))); |
1306 EXPECT_FAILURE_INLINE(env, WASM_CALL_IMPORT(f1, WASM_F32(17.6))); | 1236 EXPECT_FAILURE_INLINE(sig, WASM_CALL_IMPORT(f1, WASM_F32(17.6))); |
1307 } | 1237 } |
1308 | 1238 |
1309 TEST_F(AstDecoderTest, Int32Globals) { | 1239 TEST_F(AstDecoderTest, Int32Globals) { |
1310 FunctionEnv* env = &env_i_i; | 1240 FunctionSig* sig = sigs.i_i(); |
1311 TestModuleEnv module_env; | 1241 TestModuleEnv module_env; |
1312 env->module = &module_env; | 1242 module = &module_env; |
1313 | 1243 |
1314 module_env.AddGlobal(MachineType::Int8()); | 1244 module_env.AddGlobal(MachineType::Int8()); |
1315 module_env.AddGlobal(MachineType::Uint8()); | 1245 module_env.AddGlobal(MachineType::Uint8()); |
1316 module_env.AddGlobal(MachineType::Int16()); | 1246 module_env.AddGlobal(MachineType::Int16()); |
1317 module_env.AddGlobal(MachineType::Uint16()); | 1247 module_env.AddGlobal(MachineType::Uint16()); |
1318 module_env.AddGlobal(MachineType::Int32()); | 1248 module_env.AddGlobal(MachineType::Int32()); |
1319 module_env.AddGlobal(MachineType::Uint32()); | 1249 module_env.AddGlobal(MachineType::Uint32()); |
1320 | 1250 |
1321 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); | 1251 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); |
1322 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(1)); | 1252 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(1)); |
1323 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(2)); | 1253 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(2)); |
1324 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(3)); | 1254 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(3)); |
1325 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(4)); | 1255 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(4)); |
1326 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(5)); | 1256 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(5)); |
1327 | 1257 |
1328 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); | 1258 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); |
1329 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); | 1259 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); |
1330 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); | 1260 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); |
1331 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); | 1261 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); |
1332 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(4, WASM_GET_LOCAL(0))); | 1262 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(4, WASM_GET_LOCAL(0))); |
1333 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(5, WASM_GET_LOCAL(0))); | 1263 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(5, WASM_GET_LOCAL(0))); |
1334 } | 1264 } |
1335 | 1265 |
1336 TEST_F(AstDecoderTest, Int32Globals_fail) { | 1266 TEST_F(AstDecoderTest, Int32Globals_fail) { |
1337 FunctionEnv* env = &env_i_i; | 1267 FunctionSig* sig = sigs.i_i(); |
1338 TestModuleEnv module_env; | 1268 TestModuleEnv module_env; |
1339 env->module = &module_env; | 1269 module = &module_env; |
1340 | 1270 |
1341 module_env.AddGlobal(MachineType::Int64()); | 1271 module_env.AddGlobal(MachineType::Int64()); |
1342 module_env.AddGlobal(MachineType::Uint64()); | 1272 module_env.AddGlobal(MachineType::Uint64()); |
1343 module_env.AddGlobal(MachineType::Float32()); | 1273 module_env.AddGlobal(MachineType::Float32()); |
1344 module_env.AddGlobal(MachineType::Float64()); | 1274 module_env.AddGlobal(MachineType::Float64()); |
1345 | 1275 |
1346 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(0)); | 1276 EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(0)); |
1347 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(1)); | 1277 EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(1)); |
1348 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(2)); | 1278 EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(2)); |
1349 EXPECT_FAILURE_INLINE(env, WASM_LOAD_GLOBAL(3)); | 1279 EXPECT_FAILURE_INLINE(sig, WASM_LOAD_GLOBAL(3)); |
1350 | 1280 |
1351 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); | 1281 EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); |
1352 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); | 1282 EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); |
1353 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); | 1283 EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(2, WASM_GET_LOCAL(0))); |
1354 EXPECT_FAILURE_INLINE(env, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); | 1284 EXPECT_FAILURE_INLINE(sig, WASM_STORE_GLOBAL(3, WASM_GET_LOCAL(0))); |
1355 } | 1285 } |
1356 | 1286 |
1357 TEST_F(AstDecoderTest, Int64Globals) { | 1287 TEST_F(AstDecoderTest, Int64Globals) { |
1358 FunctionEnv* env = &env_l_l; | 1288 FunctionSig* sig = sigs.l_l(); |
1359 TestModuleEnv module_env; | 1289 TestModuleEnv module_env; |
1360 env->module = &module_env; | 1290 module = &module_env; |
1361 | 1291 |
1362 module_env.AddGlobal(MachineType::Int64()); | 1292 module_env.AddGlobal(MachineType::Int64()); |
1363 module_env.AddGlobal(MachineType::Uint64()); | 1293 module_env.AddGlobal(MachineType::Uint64()); |
1364 | 1294 |
1365 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); | 1295 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); |
1366 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(1)); | 1296 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(1)); |
1367 | 1297 |
1368 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); | 1298 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); |
1369 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); | 1299 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(1, WASM_GET_LOCAL(0))); |
1370 } | 1300 } |
1371 | 1301 |
1372 TEST_F(AstDecoderTest, Float32Globals) { | 1302 TEST_F(AstDecoderTest, Float32Globals) { |
1373 FunctionEnv env_f_ff; | 1303 FunctionSig* sig = sigs.f_ff(); |
1374 FunctionEnv* env = &env_f_ff; | |
1375 init_env(env, sigs.f_ff()); | |
1376 TestModuleEnv module_env; | 1304 TestModuleEnv module_env; |
1377 env->module = &module_env; | 1305 module = &module_env; |
1378 | 1306 |
1379 module_env.AddGlobal(MachineType::Float32()); | 1307 module_env.AddGlobal(MachineType::Float32()); |
1380 | 1308 |
1381 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); | 1309 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); |
1382 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); | 1310 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); |
1383 } | 1311 } |
1384 | 1312 |
1385 TEST_F(AstDecoderTest, Float64Globals) { | 1313 TEST_F(AstDecoderTest, Float64Globals) { |
1386 FunctionEnv env_d_dd; | 1314 FunctionSig* sig = sigs.d_dd(); |
1387 FunctionEnv* env = &env_d_dd; | |
1388 init_env(env, sigs.d_dd()); | |
1389 TestModuleEnv module_env; | 1315 TestModuleEnv module_env; |
1390 env->module = &module_env; | 1316 module = &module_env; |
1391 | 1317 |
1392 module_env.AddGlobal(MachineType::Float64()); | 1318 module_env.AddGlobal(MachineType::Float64()); |
1393 | 1319 |
1394 EXPECT_VERIFIES_INLINE(env, WASM_LOAD_GLOBAL(0)); | 1320 EXPECT_VERIFIES_INLINE(sig, WASM_LOAD_GLOBAL(0)); |
1395 EXPECT_VERIFIES_INLINE(env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); | 1321 EXPECT_VERIFIES_INLINE(sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); |
1396 } | 1322 } |
1397 | 1323 |
1398 TEST_F(AstDecoderTest, AllLoadGlobalCombinations) { | 1324 TEST_F(AstDecoderTest, AllLoadGlobalCombinations) { |
1399 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1325 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1400 LocalType local_type = kLocalTypes[i]; | 1326 LocalType local_type = kLocalTypes[i]; |
1401 for (size_t j = 0; j < arraysize(machineTypes); j++) { | 1327 for (size_t j = 0; j < arraysize(machineTypes); j++) { |
1402 MachineType mem_type = machineTypes[j]; | 1328 MachineType mem_type = machineTypes[j]; |
1403 FunctionEnv env; | |
1404 FunctionSig sig(1, 0, &local_type); | 1329 FunctionSig sig(1, 0, &local_type); |
1405 TestModuleEnv module_env; | 1330 TestModuleEnv module_env; |
1406 init_env(&env, &sig); | 1331 module = &module_env; |
1407 env.module = &module_env; | |
1408 module_env.AddGlobal(mem_type); | 1332 module_env.AddGlobal(mem_type); |
1409 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { | 1333 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { |
1410 EXPECT_VERIFIES_INLINE(&env, WASM_LOAD_GLOBAL(0)); | 1334 EXPECT_VERIFIES_INLINE(&sig, WASM_LOAD_GLOBAL(0)); |
1411 } else { | 1335 } else { |
1412 EXPECT_FAILURE_INLINE(&env, WASM_LOAD_GLOBAL(0)); | 1336 EXPECT_FAILURE_INLINE(&sig, WASM_LOAD_GLOBAL(0)); |
1413 } | 1337 } |
1414 } | 1338 } |
1415 } | 1339 } |
1416 } | 1340 } |
1417 | 1341 |
1418 TEST_F(AstDecoderTest, AllStoreGlobalCombinations) { | 1342 TEST_F(AstDecoderTest, AllStoreGlobalCombinations) { |
1419 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1343 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1420 LocalType local_type = kLocalTypes[i]; | 1344 LocalType local_type = kLocalTypes[i]; |
1421 for (size_t j = 0; j < arraysize(machineTypes); j++) { | 1345 for (size_t j = 0; j < arraysize(machineTypes); j++) { |
1422 MachineType mem_type = machineTypes[j]; | 1346 MachineType mem_type = machineTypes[j]; |
1423 FunctionEnv env; | |
1424 FunctionSig sig(0, 1, &local_type); | 1347 FunctionSig sig(0, 1, &local_type); |
1425 TestModuleEnv module_env; | 1348 TestModuleEnv module_env; |
1426 init_env(&env, &sig); | 1349 module = &module_env; |
1427 env.module = &module_env; | |
1428 module_env.AddGlobal(mem_type); | 1350 module_env.AddGlobal(mem_type); |
1429 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { | 1351 if (local_type == WasmOpcodes::LocalTypeFor(mem_type)) { |
1430 EXPECT_VERIFIES_INLINE(&env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); | 1352 EXPECT_VERIFIES_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); |
1431 } else { | 1353 } else { |
1432 EXPECT_FAILURE_INLINE(&env, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); | 1354 EXPECT_FAILURE_INLINE(&sig, WASM_STORE_GLOBAL(0, WASM_GET_LOCAL(0))); |
1433 } | 1355 } |
1434 } | 1356 } |
1435 } | 1357 } |
1436 } | 1358 } |
1437 | 1359 |
1438 TEST_F(AstDecoderTest, BreakNesting1) { | 1360 TEST_F(AstDecoderTest, BreakNesting1) { |
1439 for (int i = 0; i < 5; i++) { | 1361 for (int i = 0; i < 5; i++) { |
1440 // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) p) | 1362 // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) p) |
1441 byte code[] = {WASM_BLOCK( | 1363 byte code[] = {WASM_BLOCK( |
1442 2, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(i, WASM_ZERO)), | 1364 2, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BRV(i, WASM_ZERO)), |
1443 WASM_SET_LOCAL(0, WASM_I8(1))), | 1365 WASM_SET_LOCAL(0, WASM_I8(1))), |
1444 WASM_GET_LOCAL(0))}; | 1366 WASM_GET_LOCAL(0))}; |
1445 if (i < 3) { | 1367 if (i < 3) { |
1446 EXPECT_VERIFIES(&env_i_i, code); | 1368 EXPECT_VERIFIES(sigs.i_i(), code); |
1447 } else { | 1369 } else { |
1448 EXPECT_FAILURE(&env_i_i, code); | 1370 EXPECT_FAILURE(sigs.i_i(), code); |
1449 } | 1371 } |
1450 } | 1372 } |
1451 } | 1373 } |
1452 | 1374 |
1453 TEST_F(AstDecoderTest, BreakNesting2) { | 1375 TEST_F(AstDecoderTest, BreakNesting2) { |
1454 env_v_v.AddLocals(kAstI32, 1); | 1376 AddLocals(kAstI32, 1); |
1455 for (int i = 0; i < 5; i++) { | 1377 for (int i = 0; i < 5; i++) { |
1456 // (block[2] (loop[2] (if (get p) break[N]) (set p 1)) (return p)) (11) | 1378 // (block[2] (loop[2] (if 0 break[N]) (set p 1)) (return p)) (11) |
1457 byte code[] = { | 1379 byte code[] = {WASM_BLOCK(1, WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BREAK(i)), |
1458 WASM_BLOCK(1, WASM_LOOP(2, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(i)), | 1380 WASM_SET_LOCAL(0, WASM_I8(1)))), |
1459 WASM_SET_LOCAL(0, WASM_I8(1)))), | 1381 WASM_I8(11)}; |
1460 WASM_I8(11)}; | |
1461 if (i < 2) { | 1382 if (i < 2) { |
1462 EXPECT_VERIFIES(&env_v_v, code); | 1383 EXPECT_VERIFIES(sigs.v_v(), code); |
1463 } else { | 1384 } else { |
1464 EXPECT_FAILURE(&env_v_v, code); | 1385 EXPECT_FAILURE(sigs.v_v(), code); |
1465 } | 1386 } |
1466 } | 1387 } |
1467 } | 1388 } |
1468 | 1389 |
1469 TEST_F(AstDecoderTest, BreakNesting3) { | 1390 TEST_F(AstDecoderTest, BreakNesting3) { |
1470 env_v_v.AddLocals(kAstI32, 1); | |
1471 for (int i = 0; i < 5; i++) { | 1391 for (int i = 0; i < 5; i++) { |
1472 // (block[1] (loop[1] (block[1] (if (get p) break[N]) | 1392 // (block[1] (loop[1] (block[1] (if 0 break[N]) |
1473 byte code[] = {WASM_BLOCK( | 1393 byte code[] = {WASM_BLOCK( |
1474 1, WASM_LOOP( | 1394 1, WASM_LOOP(1, WASM_BLOCK(1, WASM_IF(WASM_ZERO, WASM_BREAK(i)))))}; |
1475 1, WASM_BLOCK(1, WASM_IF(WASM_GET_LOCAL(0), WASM_BREAK(i)))))}; | |
1476 if (i < 3) { | 1395 if (i < 3) { |
1477 EXPECT_VERIFIES(&env_v_v, code); | 1396 EXPECT_VERIFIES(sigs.v_v(), code); |
1478 } else { | 1397 } else { |
1479 EXPECT_FAILURE(&env_v_v, code); | 1398 EXPECT_FAILURE(sigs.v_v(), code); |
1480 } | 1399 } |
1481 } | 1400 } |
1482 } | 1401 } |
1483 | 1402 |
1484 TEST_F(AstDecoderTest, BreaksWithMultipleTypes) { | 1403 TEST_F(AstDecoderTest, BreaksWithMultipleTypes) { |
1485 EXPECT_FAILURE_INLINE( | 1404 EXPECT_FAILURE_INLINE( |
1486 &env_i_i, WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), WASM_F32(7.7))); | 1405 sigs.i_i(), |
| 1406 WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), WASM_F32(7.7))); |
1487 | 1407 |
1488 EXPECT_FAILURE_INLINE(&env_i_i, | 1408 EXPECT_FAILURE_INLINE(sigs.i_i(), |
1489 WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), | 1409 WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_I8(7)), |
1490 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); | 1410 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); |
1491 EXPECT_FAILURE_INLINE(&env_i_i, | 1411 EXPECT_FAILURE_INLINE(sigs.i_i(), |
1492 WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(8)), | 1412 WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(8)), |
1493 WASM_BRV_IF_ZERO(0, WASM_I8(0)), | 1413 WASM_BRV_IF_ZERO(0, WASM_I8(0)), |
1494 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); | 1414 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)))); |
1495 EXPECT_FAILURE_INLINE(&env_i_i, WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(9)), | 1415 EXPECT_FAILURE_INLINE(sigs.i_i(), |
1496 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)), | 1416 WASM_BLOCK(3, WASM_BRV_IF_ZERO(0, WASM_I8(9)), |
1497 WASM_BRV_IF_ZERO(0, WASM_I8(11)))); | 1417 WASM_BRV_IF_ZERO(0, WASM_F32(7.7)), |
| 1418 WASM_BRV_IF_ZERO(0, WASM_I8(11)))); |
1498 } | 1419 } |
1499 | 1420 |
1500 TEST_F(AstDecoderTest, BreakNesting_6_levels) { | 1421 TEST_F(AstDecoderTest, BreakNesting_6_levels) { |
1501 for (int mask = 0; mask < 64; mask++) { | 1422 for (int mask = 0; mask < 64; mask++) { |
1502 for (int i = 0; i < 14; i++) { | 1423 for (int i = 0; i < 14; i++) { |
1503 byte code[] = { | 1424 byte code[] = { |
1504 kExprBlock, 1, // -- | 1425 kExprBlock, 1, // -- |
1505 kExprBlock, 1, // -- | 1426 kExprBlock, 1, // -- |
1506 kExprBlock, 1, // -- | 1427 kExprBlock, 1, // -- |
1507 kExprBlock, 1, // -- | 1428 kExprBlock, 1, // -- |
1508 kExprBlock, 1, // -- | 1429 kExprBlock, 1, // -- |
1509 kExprBlock, 1, // -- | 1430 kExprBlock, 1, // -- |
1510 kExprBr, static_cast<byte>(i), | 1431 kExprBr, static_cast<byte>(i), |
1511 kExprNop // -- | 1432 kExprNop // -- |
1512 }; | 1433 }; |
1513 | 1434 |
1514 int depth = 6; | 1435 int depth = 6; |
1515 for (int l = 0; l < 6; l++) { | 1436 for (int l = 0; l < 6; l++) { |
1516 if (mask & (1 << l)) { | 1437 if (mask & (1 << l)) { |
1517 code[l * 2] = kExprLoop; | 1438 code[l * 2] = kExprLoop; |
1518 depth++; | 1439 depth++; |
1519 } | 1440 } |
1520 } | 1441 } |
1521 | 1442 |
1522 if (i < depth) { | 1443 if (i < depth) { |
1523 EXPECT_VERIFIES(&env_v_v, code); | 1444 EXPECT_VERIFIES(sigs.v_v(), code); |
1524 } else { | 1445 } else { |
1525 EXPECT_FAILURE(&env_v_v, code); | 1446 EXPECT_FAILURE(sigs.v_v(), code); |
1526 } | 1447 } |
1527 } | 1448 } |
1528 } | 1449 } |
1529 } | 1450 } |
1530 | 1451 |
1531 TEST_F(AstDecoderTest, ExprBreak_TypeCheck) { | 1452 TEST_F(AstDecoderTest, ExprBreak_TypeCheck) { |
1532 FunctionEnv* envs[] = {&env_i_i, &env_l_l, &env_f_ff, &env_d_dd}; | 1453 FunctionSig* sigarray[] = {sigs.i_i(), sigs.l_l(), sigs.f_ff(), sigs.d_dd()}; |
1533 for (size_t i = 0; i < arraysize(envs); i++) { | 1454 for (size_t i = 0; i < arraysize(sigarray); i++) { |
1534 FunctionEnv* env = envs[i]; | 1455 FunctionSig* sig = sigarray[i]; |
1535 // unify X and X => OK | 1456 // unify X and X => OK |
1536 EXPECT_VERIFIES_INLINE( | 1457 EXPECT_VERIFIES_INLINE( |
1537 env, WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), | 1458 sig, WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), |
1538 WASM_GET_LOCAL(0))); | 1459 WASM_GET_LOCAL(0))); |
1539 } | 1460 } |
1540 | 1461 |
1541 // unify i32 and f32 => fail | 1462 // unify i32 and f32 => fail |
1542 EXPECT_FAILURE_INLINE( | 1463 EXPECT_FAILURE_INLINE( |
1543 &env_i_i, | 1464 sigs.i_i(), |
1544 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_ZERO)), WASM_F32(1.2))); | 1465 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_ZERO)), WASM_F32(1.2))); |
1545 | 1466 |
1546 // unify f64 and f64 => OK | 1467 // unify f64 and f64 => OK |
1547 EXPECT_VERIFIES_INLINE( | 1468 EXPECT_VERIFIES_INLINE( |
1548 &env_d_dd, | 1469 sigs.d_dd(), |
1549 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), | 1470 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), |
1550 WASM_F64(1.2))); | 1471 WASM_F64(1.2))); |
1551 } | 1472 } |
1552 | 1473 |
1553 TEST_F(AstDecoderTest, ExprBreak_TypeCheckAll) { | 1474 TEST_F(AstDecoderTest, ExprBreak_TypeCheckAll) { |
1554 byte code1[] = {WASM_BLOCK(2, | 1475 byte code1[] = {WASM_BLOCK(2, |
1555 WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), | 1476 WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(0))), |
1556 WASM_GET_LOCAL(1))}; | 1477 WASM_GET_LOCAL(1))}; |
1557 byte code2[] = { | 1478 byte code2[] = { |
1558 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0))), | 1479 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(0))), |
1559 WASM_GET_LOCAL(1))}; | 1480 WASM_GET_LOCAL(1))}; |
1560 | 1481 |
1561 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1482 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1562 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { | 1483 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { |
1563 FunctionEnv env; | |
1564 LocalType storage[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j]}; | 1484 LocalType storage[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j]}; |
1565 FunctionSig sig(1, 2, storage); | 1485 FunctionSig sig(1, 2, storage); |
1566 init_env(&env, &sig); | |
1567 | 1486 |
1568 if (i == j) { | 1487 if (i == j) { |
1569 EXPECT_VERIFIES(&env, code1); | 1488 EXPECT_VERIFIES(&sig, code1); |
1570 EXPECT_VERIFIES(&env, code2); | 1489 EXPECT_VERIFIES(&sig, code2); |
1571 } else { | 1490 } else { |
1572 EXPECT_FAILURE(&env, code1); | 1491 EXPECT_FAILURE(&sig, code1); |
1573 EXPECT_FAILURE(&env, code2); | 1492 EXPECT_FAILURE(&sig, code2); |
1574 } | 1493 } |
1575 } | 1494 } |
1576 } | 1495 } |
1577 } | 1496 } |
1578 | 1497 |
1579 TEST_F(AstDecoderTest, ExprBr_Unify) { | 1498 TEST_F(AstDecoderTest, ExprBr_Unify) { |
1580 FunctionEnv env; | |
1581 | |
1582 for (int which = 0; which < 2; which++) { | 1499 for (int which = 0; which < 2; which++) { |
1583 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1500 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1584 LocalType type = kLocalTypes[i]; | 1501 LocalType type = kLocalTypes[i]; |
1585 LocalType storage[] = {kAstI32, kAstI32, type}; | 1502 LocalType storage[] = {kAstI32, kAstI32, type}; |
1586 FunctionSig sig(1, 2, storage); | 1503 FunctionSig sig(1, 2, storage); |
1587 init_env(&env, &sig); // (i32, X) -> i32 | |
1588 | 1504 |
1589 byte code1[] = { | 1505 byte code1[] = { |
1590 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(which))), | 1506 WASM_BLOCK(2, WASM_IF(WASM_ZERO, WASM_BRV(0, WASM_GET_LOCAL(which))), |
1591 WASM_GET_LOCAL(which ^ 1))}; | 1507 WASM_GET_LOCAL(which ^ 1))}; |
1592 byte code2[] = { | 1508 byte code2[] = { |
1593 WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BRV(1, WASM_GET_LOCAL(which))), | 1509 WASM_LOOP(2, WASM_IF(WASM_ZERO, WASM_BRV(1, WASM_GET_LOCAL(which))), |
1594 WASM_GET_LOCAL(which ^ 1))}; | 1510 WASM_GET_LOCAL(which ^ 1))}; |
1595 | 1511 |
1596 | 1512 |
1597 if (type == kAstI32) { | 1513 if (type == kAstI32) { |
1598 EXPECT_VERIFIES(&env, code1); | 1514 EXPECT_VERIFIES(&sig, code1); |
1599 EXPECT_VERIFIES(&env, code2); | 1515 EXPECT_VERIFIES(&sig, code2); |
1600 } else { | 1516 } else { |
1601 EXPECT_FAILURE(&env, code1); | 1517 EXPECT_FAILURE(&sig, code1); |
1602 EXPECT_FAILURE(&env, code2); | 1518 EXPECT_FAILURE(&sig, code2); |
1603 } | 1519 } |
1604 } | 1520 } |
1605 } | 1521 } |
1606 } | 1522 } |
1607 | 1523 |
1608 TEST_F(AstDecoderTest, ExprBrIf_cond_type) { | 1524 TEST_F(AstDecoderTest, ExprBrIf_cond_type) { |
1609 FunctionEnv env; | |
1610 byte code[] = { | 1525 byte code[] = { |
1611 WASM_BLOCK(1, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))}; | 1526 WASM_BLOCK(1, WASM_BRV_IF(0, WASM_GET_LOCAL(0), WASM_GET_LOCAL(1)))}; |
1612 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1527 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1613 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { | 1528 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { |
1614 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; | 1529 LocalType types[] = {kLocalTypes[i], kLocalTypes[j]}; |
1615 FunctionSig sig(0, 2, types); | 1530 FunctionSig sig(0, 2, types); |
1616 init_env(&env, &sig); | |
1617 | 1531 |
1618 if (types[1] == kAstI32) { | 1532 if (types[1] == kAstI32) { |
1619 EXPECT_VERIFIES(&env, code); | 1533 EXPECT_VERIFIES(&sig, code); |
1620 } else { | 1534 } else { |
1621 EXPECT_FAILURE(&env, code); | 1535 EXPECT_FAILURE(&sig, code); |
1622 } | 1536 } |
1623 } | 1537 } |
1624 } | 1538 } |
1625 } | 1539 } |
1626 | 1540 |
1627 TEST_F(AstDecoderTest, ExprBrIf_val_type) { | 1541 TEST_F(AstDecoderTest, ExprBrIf_val_type) { |
1628 FunctionEnv env; | |
1629 byte code[] = { | 1542 byte code[] = { |
1630 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)), | 1543 WASM_BLOCK(2, WASM_BRV_IF(0, WASM_GET_LOCAL(1), WASM_GET_LOCAL(2)), |
1631 WASM_GET_LOCAL(0))}; | 1544 WASM_GET_LOCAL(0))}; |
1632 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1545 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1633 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { | 1546 for (size_t j = 0; j < arraysize(kLocalTypes); j++) { |
1634 LocalType types[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j], | 1547 LocalType types[] = {kLocalTypes[i], kLocalTypes[i], kLocalTypes[j], |
1635 kAstI32}; | 1548 kAstI32}; |
1636 FunctionSig sig(1, 3, types); | 1549 FunctionSig sig(1, 3, types); |
1637 init_env(&env, &sig); | |
1638 | 1550 |
1639 if (i == j) { | 1551 if (i == j) { |
1640 EXPECT_VERIFIES(&env, code); | 1552 EXPECT_VERIFIES(&sig, code); |
1641 } else { | 1553 } else { |
1642 EXPECT_FAILURE(&env, code); | 1554 EXPECT_FAILURE(&sig, code); |
1643 } | 1555 } |
1644 } | 1556 } |
1645 } | 1557 } |
1646 } | 1558 } |
1647 | 1559 |
1648 TEST_F(AstDecoderTest, ExprBrIf_Unify) { | 1560 TEST_F(AstDecoderTest, ExprBrIf_Unify) { |
1649 FunctionEnv env; | |
1650 | |
1651 for (int which = 0; which < 2; which++) { | 1561 for (int which = 0; which < 2; which++) { |
1652 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1562 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1653 LocalType type = kLocalTypes[i]; | 1563 LocalType type = kLocalTypes[i]; |
1654 LocalType storage[] = {kAstI32, kAstI32, type}; | 1564 LocalType storage[] = {kAstI32, kAstI32, type}; |
1655 FunctionSig sig(1, 2, storage); | 1565 FunctionSig sig(1, 2, storage); |
1656 init_env(&env, &sig); // (i32, X) -> i32 | |
1657 | 1566 |
1658 byte code1[] = {WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(which)), | 1567 byte code1[] = {WASM_BLOCK(2, WASM_BRV_IF_ZERO(0, WASM_GET_LOCAL(which)), |
1659 WASM_GET_LOCAL(which ^ 1))}; | 1568 WASM_GET_LOCAL(which ^ 1))}; |
1660 byte code2[] = {WASM_LOOP(2, WASM_BRV_IF_ZERO(1, WASM_GET_LOCAL(which)), | 1569 byte code2[] = {WASM_LOOP(2, WASM_BRV_IF_ZERO(1, WASM_GET_LOCAL(which)), |
1661 WASM_GET_LOCAL(which ^ 1))}; | 1570 WASM_GET_LOCAL(which ^ 1))}; |
1662 | 1571 |
1663 if (type == kAstI32) { | 1572 if (type == kAstI32) { |
1664 EXPECT_VERIFIES(&env, code1); | 1573 EXPECT_VERIFIES(&sig, code1); |
1665 EXPECT_VERIFIES(&env, code2); | 1574 EXPECT_VERIFIES(&sig, code2); |
1666 } else { | 1575 } else { |
1667 EXPECT_FAILURE(&env, code1); | 1576 EXPECT_FAILURE(&sig, code1); |
1668 EXPECT_FAILURE(&env, code2); | 1577 EXPECT_FAILURE(&sig, code2); |
1669 } | 1578 } |
1670 } | 1579 } |
1671 } | 1580 } |
1672 } | 1581 } |
1673 | 1582 |
1674 TEST_F(AstDecoderTest, BrTable0) { | 1583 TEST_F(AstDecoderTest, BrTable0) { |
1675 static byte code[] = {kExprBrTable, 0, 0}; | 1584 static byte code[] = {kExprBrTable, 0, 0}; |
1676 EXPECT_FAILURE(&env_v_v, code); | 1585 EXPECT_FAILURE(sigs.v_v(), code); |
1677 } | 1586 } |
1678 | 1587 |
1679 TEST_F(AstDecoderTest, BrTable0b) { | 1588 TEST_F(AstDecoderTest, BrTable0b) { |
1680 static byte code[] = {kExprBrTable, 0, 0, kExprI8Const, 11}; | 1589 static byte code[] = {kExprBrTable, 0, 0, kExprI32Const, 11}; |
1681 EXPECT_FAILURE(&env_v_v, code); | 1590 EXPECT_FAILURE(sigs.v_v(), code); |
1682 EXPECT_FAILURE(&env_i_i, code); | 1591 EXPECT_FAILURE(sigs.i_i(), code); |
1683 } | 1592 } |
1684 | 1593 |
1685 TEST_F(AstDecoderTest, BrTable0c) { | 1594 TEST_F(AstDecoderTest, BrTable0c) { |
1686 static byte code[] = {kExprBrTable, 0, 1, 0, 0, kExprI8Const, 11}; | 1595 static byte code[] = {kExprBrTable, 0, 1, 0, 0, kExprI32Const, 11}; |
1687 EXPECT_FAILURE(&env_v_v, code); | 1596 EXPECT_FAILURE(sigs.v_v(), code); |
1688 EXPECT_FAILURE(&env_i_i, code); | 1597 EXPECT_FAILURE(sigs.i_i(), code); |
1689 } | 1598 } |
1690 | 1599 |
1691 TEST_F(AstDecoderTest, BrTable1a) { | 1600 TEST_F(AstDecoderTest, BrTable1a) { |
1692 static byte code[] = { | 1601 static byte code[] = { |
1693 WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 0, BR_TARGET(0)))}; | 1602 WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 0, BR_TARGET(0)))}; |
1694 EXPECT_VERIFIES(&env_v_v, code); | 1603 EXPECT_VERIFIES(sigs.v_v(), code); |
1695 } | 1604 } |
1696 | 1605 |
1697 TEST_F(AstDecoderTest, BrTable1b) { | 1606 TEST_F(AstDecoderTest, BrTable1b) { |
1698 static byte code[] = { | 1607 static byte code[] = { |
1699 WASM_BLOCK(1, WASM_BR_TABLE(WASM_ZERO, 0, BR_TARGET(0)))}; | 1608 WASM_BLOCK(1, WASM_BR_TABLE(WASM_ZERO, 0, BR_TARGET(0)))}; |
1700 EXPECT_VERIFIES(&env_v_v, code); | 1609 EXPECT_VERIFIES(sigs.v_v(), code); |
1701 EXPECT_FAILURE(&env_i_i, code); | 1610 EXPECT_FAILURE(sigs.i_i(), code); |
1702 EXPECT_FAILURE(&env_f_ff, code); | 1611 EXPECT_FAILURE(sigs.f_ff(), code); |
1703 EXPECT_FAILURE(&env_d_dd, code); | 1612 EXPECT_FAILURE(sigs.d_dd(), code); |
1704 } | 1613 } |
1705 | 1614 |
1706 TEST_F(AstDecoderTest, BrTable2a) { | 1615 TEST_F(AstDecoderTest, BrTable2a) { |
1707 static byte code[] = { | 1616 static byte code[] = { |
1708 WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(0)))}; | 1617 WASM_BLOCK(1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(0)))}; |
1709 EXPECT_VERIFIES(&env_v_v, code); | 1618 EXPECT_VERIFIES(sigs.v_v(), code); |
1710 } | 1619 } |
1711 | 1620 |
1712 TEST_F(AstDecoderTest, BrTable2b) { | 1621 TEST_F(AstDecoderTest, BrTable2b) { |
1713 static byte code[] = {WASM_BLOCK( | 1622 static byte code[] = {WASM_BLOCK( |
1714 1, WASM_BLOCK( | 1623 1, WASM_BLOCK( |
1715 1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(1))))}; | 1624 1, WASM_BR_TABLE(WASM_I8(67), 1, BR_TARGET(0), BR_TARGET(1))))}; |
1716 EXPECT_VERIFIES(&env_v_v, code); | 1625 EXPECT_VERIFIES(sigs.v_v(), code); |
1717 } | 1626 } |
1718 | 1627 |
1719 TEST_F(AstDecoderTest, BrTable_off_end) { | 1628 TEST_F(AstDecoderTest, BrTable_off_end) { |
1720 static byte code[] = { | 1629 static byte code[] = { |
1721 WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))}; | 1630 WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(0)))}; |
1722 for (size_t len = 1; len < sizeof(code); len++) { | 1631 for (size_t len = 1; len < sizeof(code); len++) { |
1723 Verify(kError, &env_i_i, code, code + len); | 1632 Verify(kError, sigs.i_i(), code, code + len); |
1724 } | 1633 } |
1725 } | 1634 } |
1726 | 1635 |
1727 TEST_F(AstDecoderTest, BrTable_invalid_br1) { | 1636 TEST_F(AstDecoderTest, BrTable_invalid_br1) { |
1728 for (int depth = 0; depth < 4; depth++) { | 1637 for (int depth = 0; depth < 4; depth++) { |
1729 byte code[] = { | 1638 byte code[] = { |
1730 WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))}; | 1639 WASM_BLOCK(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))}; |
1731 if (depth == 0) { | 1640 if (depth == 0) { |
1732 EXPECT_VERIFIES(&env_v_i, code); | 1641 EXPECT_VERIFIES(sigs.v_i(), code); |
1733 } else { | 1642 } else { |
1734 EXPECT_FAILURE(&env_v_i, code); | 1643 EXPECT_FAILURE(sigs.v_i(), code); |
1735 } | 1644 } |
1736 } | 1645 } |
1737 } | 1646 } |
1738 | 1647 |
1739 TEST_F(AstDecoderTest, BrTable_invalid_br2) { | 1648 TEST_F(AstDecoderTest, BrTable_invalid_br2) { |
1740 for (int depth = 0; depth < 4; depth++) { | 1649 for (int depth = 0; depth < 4; depth++) { |
1741 byte code[] = { | 1650 byte code[] = { |
1742 WASM_LOOP(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))}; | 1651 WASM_LOOP(1, WASM_BR_TABLE(WASM_GET_LOCAL(0), 0, BR_TARGET(depth)))}; |
1743 if (depth <= 1) { | 1652 if (depth <= 1) { |
1744 EXPECT_VERIFIES(&env_v_i, code); | 1653 EXPECT_VERIFIES(sigs.v_i(), code); |
1745 } else { | 1654 } else { |
1746 EXPECT_FAILURE(&env_v_i, code); | 1655 EXPECT_FAILURE(sigs.v_i(), code); |
1747 } | 1656 } |
1748 } | 1657 } |
1749 } | 1658 } |
1750 | 1659 |
1751 TEST_F(AstDecoderTest, ExprBreakNesting1) { | 1660 TEST_F(AstDecoderTest, ExprBreakNesting1) { |
1752 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BRV(0, WASM_ZERO))); | 1661 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BRV(0, WASM_ZERO))); |
1753 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BR(0))); | 1662 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BR(0))); |
1754 EXPECT_VERIFIES_INLINE(&env_v_v, | 1663 EXPECT_VERIFIES_INLINE(sigs.v_v(), |
1755 WASM_BLOCK(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); | 1664 WASM_BLOCK(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); |
1756 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_BLOCK(1, WASM_BR_IF(0, WASM_ZERO))); | 1665 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_BLOCK(1, WASM_BR_IF(0, WASM_ZERO))); |
1757 | 1666 |
1758 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BRV(0, WASM_ZERO))); | 1667 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(0, WASM_ZERO))); |
1759 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR(0))); | 1668 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(0))); |
1760 EXPECT_VERIFIES_INLINE(&env_v_v, | 1669 EXPECT_VERIFIES_INLINE(sigs.v_v(), |
1761 WASM_LOOP(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); | 1670 WASM_LOOP(1, WASM_BRV_IF(0, WASM_ZERO, WASM_ZERO))); |
1762 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO))); | 1671 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR_IF(0, WASM_ZERO))); |
1763 | 1672 |
1764 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BRV(1, WASM_ZERO))); | 1673 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BRV(1, WASM_ZERO))); |
1765 EXPECT_VERIFIES_INLINE(&env_v_v, WASM_LOOP(1, WASM_BR(1))); | 1674 EXPECT_VERIFIES_INLINE(sigs.v_v(), WASM_LOOP(1, WASM_BR(1))); |
1766 } | 1675 } |
1767 | 1676 |
1768 TEST_F(AstDecoderTest, Select) { | 1677 TEST_F(AstDecoderTest, Select) { |
1769 EXPECT_VERIFIES_INLINE( | 1678 EXPECT_VERIFIES_INLINE( |
1770 &env_i_i, WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_ZERO)); | 1679 sigs.i_i(), WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_ZERO)); |
1771 EXPECT_VERIFIES_INLINE(&env_f_ff, | 1680 EXPECT_VERIFIES_INLINE(sigs.f_ff(), |
1772 WASM_SELECT(WASM_F32(0.0), WASM_F32(0.0), WASM_ZERO)); | 1681 WASM_SELECT(WASM_F32(0.0), WASM_F32(0.0), WASM_ZERO)); |
1773 EXPECT_VERIFIES_INLINE(&env_d_dd, | 1682 EXPECT_VERIFIES_INLINE(sigs.d_dd(), |
1774 WASM_SELECT(WASM_F64(0.0), WASM_F64(0.0), WASM_ZERO)); | 1683 WASM_SELECT(WASM_F64(0.0), WASM_F64(0.0), WASM_ZERO)); |
1775 EXPECT_VERIFIES_INLINE( | 1684 EXPECT_VERIFIES_INLINE( |
1776 &env_l_l, WASM_SELECT(WASM_I64V_1(0), WASM_I64V_1(0), WASM_ZERO)); | 1685 sigs.l_l(), WASM_SELECT(WASM_I64V_1(0), WASM_I64V_1(0), WASM_ZERO)); |
1777 } | 1686 } |
1778 | 1687 |
1779 TEST_F(AstDecoderTest, Select_fail1) { | 1688 TEST_F(AstDecoderTest, Select_fail1) { |
1780 EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_F32(0.0), WASM_GET_LOCAL(0), | |
1781 WASM_GET_LOCAL(0))); | |
1782 EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_GET_LOCAL(0), WASM_F32(0.0), | |
1783 WASM_GET_LOCAL(0))); | |
1784 EXPECT_FAILURE_INLINE( | 1689 EXPECT_FAILURE_INLINE( |
1785 &env_i_i, | 1690 sigs.i_i(), |
| 1691 WASM_SELECT(WASM_F32(0.0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); |
| 1692 EXPECT_FAILURE_INLINE( |
| 1693 sigs.i_i(), |
| 1694 WASM_SELECT(WASM_GET_LOCAL(0), WASM_F32(0.0), WASM_GET_LOCAL(0))); |
| 1695 EXPECT_FAILURE_INLINE( |
| 1696 sigs.i_i(), |
1786 WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_F32(0.0))); | 1697 WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_F32(0.0))); |
1787 } | 1698 } |
1788 | 1699 |
1789 TEST_F(AstDecoderTest, Select_fail2) { | 1700 TEST_F(AstDecoderTest, Select_fail2) { |
1790 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { | 1701 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
1791 LocalType type = kLocalTypes[i]; | 1702 LocalType type = kLocalTypes[i]; |
1792 if (type == kAstI32) continue; | 1703 if (type == kAstI32) continue; |
1793 | 1704 |
1794 LocalType types[] = {type, kAstI32, type}; | 1705 LocalType types[] = {type, kAstI32, type}; |
1795 FunctionSig sig(1, 2, types); | 1706 FunctionSig sig(1, 2, types); |
1796 FunctionEnv env; | |
1797 init_env(&env, &sig); | |
1798 | 1707 |
1799 EXPECT_VERIFIES_INLINE( | 1708 EXPECT_VERIFIES_INLINE( |
1800 &env, | 1709 &sig, |
1801 WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))); | 1710 WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))); |
1802 | 1711 |
1803 EXPECT_FAILURE_INLINE( | 1712 EXPECT_FAILURE_INLINE( |
1804 &env, | 1713 &sig, |
1805 WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); | 1714 WASM_SELECT(WASM_GET_LOCAL(1), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); |
1806 | 1715 |
1807 EXPECT_FAILURE_INLINE( | 1716 EXPECT_FAILURE_INLINE( |
1808 &env, | 1717 &sig, |
1809 WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))); | 1718 WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(1), WASM_GET_LOCAL(0))); |
1810 | 1719 |
1811 EXPECT_FAILURE_INLINE( | 1720 EXPECT_FAILURE_INLINE( |
1812 &env, | 1721 &sig, |
1813 WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); | 1722 WASM_SELECT(WASM_GET_LOCAL(0), WASM_GET_LOCAL(0), WASM_GET_LOCAL(1))); |
1814 } | 1723 } |
1815 } | 1724 } |
1816 | 1725 |
1817 TEST_F(AstDecoderTest, Select_TypeCheck) { | 1726 TEST_F(AstDecoderTest, Select_TypeCheck) { |
1818 EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), | 1727 EXPECT_FAILURE_INLINE( |
1819 WASM_GET_LOCAL(0))); | 1728 sigs.i_i(), |
1820 | 1729 WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), WASM_GET_LOCAL(0))); |
1821 EXPECT_FAILURE_INLINE(&env_i_i, WASM_SELECT(WASM_GET_LOCAL(0), WASM_F64(0.25), | |
1822 WASM_GET_LOCAL(0))); | |
1823 | 1730 |
1824 EXPECT_FAILURE_INLINE( | 1731 EXPECT_FAILURE_INLINE( |
1825 &env_i_i, WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), WASM_I64V_1(0))); | 1732 sigs.i_i(), |
| 1733 WASM_SELECT(WASM_GET_LOCAL(0), WASM_F64(0.25), WASM_GET_LOCAL(0))); |
| 1734 |
| 1735 EXPECT_FAILURE_INLINE( |
| 1736 sigs.i_i(), |
| 1737 WASM_SELECT(WASM_F32(9.9), WASM_GET_LOCAL(0), WASM_I64V_1(0))); |
1826 } | 1738 } |
1827 | 1739 |
1828 | 1740 |
1829 class WasmOpcodeLengthTest : public TestWithZone { | 1741 class WasmOpcodeLengthTest : public TestWithZone { |
1830 public: | 1742 public: |
1831 WasmOpcodeLengthTest() : TestWithZone() {} | 1743 WasmOpcodeLengthTest() : TestWithZone() {} |
1832 }; | 1744 }; |
1833 | 1745 |
1834 #define EXPECT_LENGTH(expected, opcode) \ | 1746 #define EXPECT_LENGTH(expected, opcode) \ |
1835 { \ | 1747 { \ |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2038 EXPECT_LENGTH(1, kExprF64ConvertF32); | 1950 EXPECT_LENGTH(1, kExprF64ConvertF32); |
2039 EXPECT_LENGTH(1, kExprF64ReinterpretI64); | 1951 EXPECT_LENGTH(1, kExprF64ReinterpretI64); |
2040 EXPECT_LENGTH(1, kExprI32ReinterpretF32); | 1952 EXPECT_LENGTH(1, kExprI32ReinterpretF32); |
2041 EXPECT_LENGTH(1, kExprI64ReinterpretF64); | 1953 EXPECT_LENGTH(1, kExprI64ReinterpretF64); |
2042 } | 1954 } |
2043 | 1955 |
2044 | 1956 |
2045 class WasmOpcodeArityTest : public TestWithZone { | 1957 class WasmOpcodeArityTest : public TestWithZone { |
2046 public: | 1958 public: |
2047 WasmOpcodeArityTest() : TestWithZone() {} | 1959 WasmOpcodeArityTest() : TestWithZone() {} |
| 1960 TestModuleEnv module; |
| 1961 TestSignatures sigs; |
2048 }; | 1962 }; |
2049 | 1963 |
2050 #define EXPECT_ARITY(expected, ...) \ | 1964 #define EXPECT_ARITY(expected, ...) \ |
2051 { \ | 1965 { \ |
2052 static const byte code[] = {__VA_ARGS__}; \ | 1966 static const byte code[] = {__VA_ARGS__}; \ |
2053 EXPECT_EQ(expected, OpcodeArity(&env, code, code + sizeof(code))); \ | 1967 EXPECT_EQ(expected, OpcodeArity(&module, sig, code, code + sizeof(code))); \ |
2054 } | 1968 } |
2055 | 1969 |
2056 TEST_F(WasmOpcodeArityTest, Control) { | 1970 TEST_F(WasmOpcodeArityTest, Control) { |
2057 FunctionEnv env; | 1971 FunctionSig* sig = sigs.v_v(); |
2058 EXPECT_ARITY(0, kExprNop); | 1972 EXPECT_ARITY(0, kExprNop); |
2059 | 1973 |
2060 EXPECT_ARITY(0, kExprBlock, 0); | 1974 EXPECT_ARITY(0, kExprBlock, 0); |
2061 EXPECT_ARITY(1, kExprBlock, 1); | 1975 EXPECT_ARITY(1, kExprBlock, 1); |
2062 EXPECT_ARITY(2, kExprBlock, 2); | 1976 EXPECT_ARITY(2, kExprBlock, 2); |
2063 EXPECT_ARITY(5, kExprBlock, 5); | 1977 EXPECT_ARITY(5, kExprBlock, 5); |
2064 EXPECT_ARITY(10, kExprBlock, 10); | 1978 EXPECT_ARITY(10, kExprBlock, 10); |
2065 | 1979 |
2066 EXPECT_ARITY(0, kExprLoop, 0); | 1980 EXPECT_ARITY(0, kExprLoop, 0); |
2067 EXPECT_ARITY(1, kExprLoop, 1); | 1981 EXPECT_ARITY(1, kExprLoop, 1); |
2068 EXPECT_ARITY(2, kExprLoop, 2); | 1982 EXPECT_ARITY(2, kExprLoop, 2); |
2069 EXPECT_ARITY(7, kExprLoop, 7); | 1983 EXPECT_ARITY(7, kExprLoop, 7); |
2070 EXPECT_ARITY(11, kExprLoop, 11); | 1984 EXPECT_ARITY(11, kExprLoop, 11); |
2071 | 1985 |
2072 EXPECT_ARITY(2, kExprIf); | 1986 EXPECT_ARITY(2, kExprIf); |
2073 EXPECT_ARITY(3, kExprIfElse); | 1987 EXPECT_ARITY(3, kExprIfElse); |
2074 EXPECT_ARITY(3, kExprSelect); | 1988 EXPECT_ARITY(3, kExprSelect); |
2075 | 1989 |
2076 EXPECT_ARITY(1, kExprBr); | 1990 EXPECT_ARITY(1, kExprBr); |
2077 EXPECT_ARITY(2, kExprBrIf); | 1991 EXPECT_ARITY(2, kExprBrIf); |
2078 | 1992 |
2079 { | 1993 { |
2080 TestSignatures sigs; | 1994 sig = sigs.v_v(); |
2081 FunctionEnv env; | |
2082 AstDecoderTest::init_env(&env, sigs.v_v()); | |
2083 EXPECT_ARITY(0, kExprReturn); | 1995 EXPECT_ARITY(0, kExprReturn); |
2084 AstDecoderTest::init_env(&env, sigs.i_i()); | 1996 sig = sigs.i_i(); |
2085 EXPECT_ARITY(1, kExprReturn); | 1997 EXPECT_ARITY(1, kExprReturn); |
2086 } | 1998 } |
2087 } | 1999 } |
2088 | 2000 |
2089 | 2001 |
2090 TEST_F(WasmOpcodeArityTest, Misc) { | 2002 TEST_F(WasmOpcodeArityTest, Misc) { |
2091 FunctionEnv env; | 2003 FunctionSig* sig = sigs.v_v(); |
2092 | |
2093 EXPECT_ARITY(0, kExprI8Const); | 2004 EXPECT_ARITY(0, kExprI8Const); |
2094 EXPECT_ARITY(0, kExprI32Const); | 2005 EXPECT_ARITY(0, kExprI32Const); |
2095 EXPECT_ARITY(0, kExprF32Const); | 2006 EXPECT_ARITY(0, kExprF32Const); |
2096 EXPECT_ARITY(0, kExprI64Const); | 2007 EXPECT_ARITY(0, kExprI64Const); |
2097 EXPECT_ARITY(0, kExprF64Const); | 2008 EXPECT_ARITY(0, kExprF64Const); |
2098 EXPECT_ARITY(0, kExprGetLocal); | 2009 EXPECT_ARITY(0, kExprGetLocal); |
2099 EXPECT_ARITY(1, kExprSetLocal); | 2010 EXPECT_ARITY(1, kExprSetLocal); |
2100 EXPECT_ARITY(0, kExprLoadGlobal); | 2011 EXPECT_ARITY(0, kExprLoadGlobal); |
2101 EXPECT_ARITY(1, kExprStoreGlobal); | 2012 EXPECT_ARITY(1, kExprStoreGlobal); |
2102 } | 2013 } |
2103 | 2014 |
2104 | 2015 |
2105 TEST_F(WasmOpcodeArityTest, Calls) { | 2016 TEST_F(WasmOpcodeArityTest, Calls) { |
2106 TestSignatures sigs; | |
2107 TestModuleEnv module; | |
2108 module.AddFunction(sigs.i_ii()); | 2017 module.AddFunction(sigs.i_ii()); |
2109 module.AddFunction(sigs.i_i()); | 2018 module.AddFunction(sigs.i_i()); |
2110 | 2019 |
2111 module.AddSignature(sigs.f_ff()); | 2020 module.AddSignature(sigs.f_ff()); |
2112 module.AddSignature(sigs.i_d()); | 2021 module.AddSignature(sigs.i_d()); |
2113 | 2022 |
2114 module.AddImport(sigs.f_ff()); | 2023 module.AddImport(sigs.f_ff()); |
2115 module.AddImport(sigs.i_d()); | 2024 module.AddImport(sigs.i_d()); |
2116 | 2025 |
2117 { | 2026 { |
2118 FunctionEnv env; | 2027 FunctionSig* sig = sigs.i_ii(); |
2119 AstDecoderTest::init_env(&env, sigs.i_ii()); | |
2120 env.module = &module; | |
2121 | 2028 |
2122 EXPECT_ARITY(2, kExprCallFunction, 0); | 2029 EXPECT_ARITY(2, kExprCallFunction, 0); |
2123 EXPECT_ARITY(2, kExprCallImport, 0); | 2030 EXPECT_ARITY(2, kExprCallImport, 0); |
2124 EXPECT_ARITY(3, kExprCallIndirect, 0); | 2031 EXPECT_ARITY(3, kExprCallIndirect, 0); |
2125 EXPECT_ARITY(1, kExprBr); | 2032 EXPECT_ARITY(1, kExprBr); |
2126 EXPECT_ARITY(2, kExprBrIf); | 2033 EXPECT_ARITY(2, kExprBrIf); |
2127 } | 2034 } |
2128 | 2035 |
2129 { | 2036 { |
2130 FunctionEnv env; | 2037 FunctionSig* sig = sigs.v_v(); |
2131 AstDecoderTest::init_env(&env, sigs.v_v()); | |
2132 env.module = &module; | |
2133 | 2038 |
2134 EXPECT_ARITY(1, kExprCallFunction, 1); | 2039 EXPECT_ARITY(1, kExprCallFunction, 1); |
2135 EXPECT_ARITY(1, kExprCallImport, 1); | 2040 EXPECT_ARITY(1, kExprCallImport, 1); |
2136 EXPECT_ARITY(2, kExprCallIndirect, 1); | 2041 EXPECT_ARITY(2, kExprCallIndirect, 1); |
2137 EXPECT_ARITY(1, kExprBr); | 2042 EXPECT_ARITY(1, kExprBr); |
2138 EXPECT_ARITY(2, kExprBrIf); | 2043 EXPECT_ARITY(2, kExprBrIf); |
2139 } | 2044 } |
2140 } | 2045 } |
2141 | 2046 |
2142 | 2047 |
2143 TEST_F(WasmOpcodeArityTest, LoadsAndStores) { | 2048 TEST_F(WasmOpcodeArityTest, LoadsAndStores) { |
2144 FunctionEnv env; | 2049 FunctionSig* sig = sigs.v_v(); |
2145 | |
2146 EXPECT_ARITY(1, kExprI32LoadMem8S); | 2050 EXPECT_ARITY(1, kExprI32LoadMem8S); |
2147 EXPECT_ARITY(1, kExprI32LoadMem8U); | 2051 EXPECT_ARITY(1, kExprI32LoadMem8U); |
2148 EXPECT_ARITY(1, kExprI32LoadMem16S); | 2052 EXPECT_ARITY(1, kExprI32LoadMem16S); |
2149 EXPECT_ARITY(1, kExprI32LoadMem16U); | 2053 EXPECT_ARITY(1, kExprI32LoadMem16U); |
2150 EXPECT_ARITY(1, kExprI32LoadMem); | 2054 EXPECT_ARITY(1, kExprI32LoadMem); |
2151 | 2055 |
2152 EXPECT_ARITY(1, kExprI64LoadMem8S); | 2056 EXPECT_ARITY(1, kExprI64LoadMem8S); |
2153 EXPECT_ARITY(1, kExprI64LoadMem8U); | 2057 EXPECT_ARITY(1, kExprI64LoadMem8U); |
2154 EXPECT_ARITY(1, kExprI64LoadMem16S); | 2058 EXPECT_ARITY(1, kExprI64LoadMem16S); |
2155 EXPECT_ARITY(1, kExprI64LoadMem16U); | 2059 EXPECT_ARITY(1, kExprI64LoadMem16U); |
2156 EXPECT_ARITY(1, kExprI64LoadMem32S); | 2060 EXPECT_ARITY(1, kExprI64LoadMem32S); |
2157 EXPECT_ARITY(1, kExprI64LoadMem32U); | 2061 EXPECT_ARITY(1, kExprI64LoadMem32U); |
2158 EXPECT_ARITY(1, kExprI64LoadMem); | 2062 EXPECT_ARITY(1, kExprI64LoadMem); |
2159 EXPECT_ARITY(1, kExprF32LoadMem); | 2063 EXPECT_ARITY(1, kExprF32LoadMem); |
2160 EXPECT_ARITY(1, kExprF64LoadMem); | 2064 EXPECT_ARITY(1, kExprF64LoadMem); |
2161 | 2065 |
2162 EXPECT_ARITY(2, kExprI32StoreMem8); | 2066 EXPECT_ARITY(2, kExprI32StoreMem8); |
2163 EXPECT_ARITY(2, kExprI32StoreMem16); | 2067 EXPECT_ARITY(2, kExprI32StoreMem16); |
2164 EXPECT_ARITY(2, kExprI32StoreMem); | 2068 EXPECT_ARITY(2, kExprI32StoreMem); |
2165 EXPECT_ARITY(2, kExprI64StoreMem8); | 2069 EXPECT_ARITY(2, kExprI64StoreMem8); |
2166 EXPECT_ARITY(2, kExprI64StoreMem16); | 2070 EXPECT_ARITY(2, kExprI64StoreMem16); |
2167 EXPECT_ARITY(2, kExprI64StoreMem32); | 2071 EXPECT_ARITY(2, kExprI64StoreMem32); |
2168 EXPECT_ARITY(2, kExprI64StoreMem); | 2072 EXPECT_ARITY(2, kExprI64StoreMem); |
2169 EXPECT_ARITY(2, kExprF32StoreMem); | 2073 EXPECT_ARITY(2, kExprF32StoreMem); |
2170 EXPECT_ARITY(2, kExprF64StoreMem); | 2074 EXPECT_ARITY(2, kExprF64StoreMem); |
2171 } | 2075 } |
2172 | 2076 |
2173 | 2077 |
2174 TEST_F(WasmOpcodeArityTest, MiscMemExpressions) { | 2078 TEST_F(WasmOpcodeArityTest, MiscMemExpressions) { |
2175 FunctionEnv env; | 2079 FunctionSig* sig = sigs.v_v(); |
2176 | |
2177 EXPECT_ARITY(0, kExprMemorySize); | 2080 EXPECT_ARITY(0, kExprMemorySize); |
2178 EXPECT_ARITY(1, kExprGrowMemory); | 2081 EXPECT_ARITY(1, kExprGrowMemory); |
2179 } | 2082 } |
2180 | 2083 |
2181 | 2084 |
2182 TEST_F(WasmOpcodeArityTest, SimpleExpressions) { | 2085 TEST_F(WasmOpcodeArityTest, SimpleExpressions) { |
2183 FunctionEnv env; | 2086 FunctionSig* sig = sigs.v_v(); |
2184 | |
2185 EXPECT_ARITY(2, kExprI32Add); | 2087 EXPECT_ARITY(2, kExprI32Add); |
2186 EXPECT_ARITY(2, kExprI32Sub); | 2088 EXPECT_ARITY(2, kExprI32Sub); |
2187 EXPECT_ARITY(2, kExprI32Mul); | 2089 EXPECT_ARITY(2, kExprI32Mul); |
2188 EXPECT_ARITY(2, kExprI32DivS); | 2090 EXPECT_ARITY(2, kExprI32DivS); |
2189 EXPECT_ARITY(2, kExprI32DivU); | 2091 EXPECT_ARITY(2, kExprI32DivU); |
2190 EXPECT_ARITY(2, kExprI32RemS); | 2092 EXPECT_ARITY(2, kExprI32RemS); |
2191 EXPECT_ARITY(2, kExprI32RemU); | 2093 EXPECT_ARITY(2, kExprI32RemU); |
2192 EXPECT_ARITY(2, kExprI32And); | 2094 EXPECT_ARITY(2, kExprI32And); |
2193 EXPECT_ARITY(2, kExprI32Ior); | 2095 EXPECT_ARITY(2, kExprI32Ior); |
2194 EXPECT_ARITY(2, kExprI32Xor); | 2096 EXPECT_ARITY(2, kExprI32Xor); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2294 EXPECT_ARITY(1, kExprF32ReinterpretI32); | 2196 EXPECT_ARITY(1, kExprF32ReinterpretI32); |
2295 EXPECT_ARITY(1, kExprF64SConvertI32); | 2197 EXPECT_ARITY(1, kExprF64SConvertI32); |
2296 EXPECT_ARITY(1, kExprF64UConvertI32); | 2198 EXPECT_ARITY(1, kExprF64UConvertI32); |
2297 EXPECT_ARITY(1, kExprF64SConvertI64); | 2199 EXPECT_ARITY(1, kExprF64SConvertI64); |
2298 EXPECT_ARITY(1, kExprF64UConvertI64); | 2200 EXPECT_ARITY(1, kExprF64UConvertI64); |
2299 EXPECT_ARITY(1, kExprF64ConvertF32); | 2201 EXPECT_ARITY(1, kExprF64ConvertF32); |
2300 EXPECT_ARITY(1, kExprF64ReinterpretI64); | 2202 EXPECT_ARITY(1, kExprF64ReinterpretI64); |
2301 EXPECT_ARITY(1, kExprI32ReinterpretF32); | 2203 EXPECT_ARITY(1, kExprI32ReinterpretF32); |
2302 EXPECT_ARITY(1, kExprI64ReinterpretF64); | 2204 EXPECT_ARITY(1, kExprI64ReinterpretF64); |
2303 } | 2205 } |
| 2206 |
| 2207 typedef std::vector<LocalType>* LocalTypeMap; |
| 2208 |
| 2209 class LocalDeclDecoderTest : public TestWithZone { |
| 2210 public: |
| 2211 size_t ExpectRun(LocalTypeMap map, size_t pos, LocalType expected, |
| 2212 size_t count) { |
| 2213 for (size_t i = 0; i < count; i++) { |
| 2214 EXPECT_EQ(expected, map->at(pos++)); |
| 2215 } |
| 2216 return pos; |
| 2217 } |
| 2218 }; |
| 2219 |
| 2220 TEST_F(LocalDeclDecoderTest, NoLocals) { |
| 2221 static const byte data[] = {0}; |
| 2222 LocalTypeMap map = DecodeLocalDeclsForTesting(data, data + sizeof(data)); |
| 2223 EXPECT_EQ(0, map->size()); |
| 2224 if (map) delete map; |
| 2225 } |
| 2226 |
| 2227 TEST_F(LocalDeclDecoderTest, OneLocal) { |
| 2228 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
| 2229 LocalType type = kLocalTypes[i]; |
| 2230 const byte data[] = { |
| 2231 1, 1, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))}; |
| 2232 LocalTypeMap map = DecodeLocalDeclsForTesting(data, data + sizeof(data)); |
| 2233 EXPECT_EQ(1, map->size()); |
| 2234 EXPECT_EQ(type, map->at(0)); |
| 2235 if (map) delete map; |
| 2236 } |
| 2237 } |
| 2238 |
| 2239 TEST_F(LocalDeclDecoderTest, FiveLocals) { |
| 2240 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { |
| 2241 LocalType type = kLocalTypes[i]; |
| 2242 const byte data[] = { |
| 2243 1, 5, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))}; |
| 2244 LocalTypeMap map = DecodeLocalDeclsForTesting(data, data + sizeof(data)); |
| 2245 EXPECT_EQ(5, map->size()); |
| 2246 ExpectRun(map, 0, type, 5); |
| 2247 if (map) delete map; |
| 2248 } |
| 2249 } |
| 2250 |
| 2251 TEST_F(LocalDeclDecoderTest, MixedLocals) { |
| 2252 for (byte a = 0; a < 3; a++) { |
| 2253 for (byte b = 0; b < 3; b++) { |
| 2254 for (byte c = 0; c < 3; c++) { |
| 2255 for (byte d = 0; d < 3; d++) { |
| 2256 const byte data[] = {4, a, kLocalI32, b, kLocalI64, |
| 2257 c, kLocalF32, d, kLocalF64}; |
| 2258 LocalTypeMap map = |
| 2259 DecodeLocalDeclsForTesting(data, data + sizeof(data)); |
| 2260 EXPECT_EQ(a + b + c + d, map->size()); |
| 2261 |
| 2262 size_t pos = 0; |
| 2263 pos = ExpectRun(map, pos, kAstI32, a); |
| 2264 pos = ExpectRun(map, pos, kAstI64, b); |
| 2265 pos = ExpectRun(map, pos, kAstF32, c); |
| 2266 pos = ExpectRun(map, pos, kAstF64, d); |
| 2267 |
| 2268 if (map) delete map; |
| 2269 } |
| 2270 } |
| 2271 } |
| 2272 } |
| 2273 } |
| 2274 |
| 2275 TEST_F(LocalDeclDecoderTest, UseEncoder) { |
| 2276 const byte* data = nullptr; |
| 2277 const byte* end = nullptr; |
| 2278 LocalDeclEncoder local_decls; |
| 2279 |
| 2280 local_decls.AddLocals(5, kAstF32); |
| 2281 local_decls.AddLocals(1337, kAstI32); |
| 2282 local_decls.AddLocals(212, kAstI64); |
| 2283 local_decls.Prepend(&data, &end); |
| 2284 |
| 2285 LocalTypeMap map = DecodeLocalDeclsForTesting(data, end); |
| 2286 size_t pos = 0; |
| 2287 pos = ExpectRun(map, pos, kAstF32, 5); |
| 2288 pos = ExpectRun(map, pos, kAstI32, 1337); |
| 2289 pos = ExpectRun(map, pos, kAstI64, 212); |
| 2290 |
| 2291 if (map) delete map; |
| 2292 delete[] data; |
| 2293 } |
| 2294 |
2304 } // namespace wasm | 2295 } // namespace wasm |
2305 } // namespace internal | 2296 } // namespace internal |
2306 } // namespace v8 | 2297 } // namespace v8 |
OLD | NEW |