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 <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "src/wasm/module-decoder.h" | 8 #include "src/wasm/module-decoder.h" |
9 #include "src/wasm/wasm-macro-gen.h" | 9 #include "src/wasm/wasm-macro-gen.h" |
10 #include "src/wasm/wasm-module-builder.h" | 10 #include "src/wasm/wasm-module-builder.h" |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 } | 392 } |
393 | 393 |
394 v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate)); | 394 v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate)); |
395 Handle<Object> params[1] = { | 395 Handle<Object> params[1] = { |
396 Handle<Object>(Smi::FromInt(25 * kPageSize), isolate)}; | 396 Handle<Object>(Smi::FromInt(25 * kPageSize), isolate)}; |
397 testing::RunWasmModuleForTesting(isolate, instance, 1, params, | 397 testing::RunWasmModuleForTesting(isolate, instance, 1, params, |
398 ModuleOrigin::kWasmOrigin); | 398 ModuleOrigin::kWasmOrigin); |
399 CHECK(try_catch.HasCaught()); | 399 CHECK(try_catch.HasCaught()); |
400 isolate->clear_pending_exception(); | 400 isolate->clear_pending_exception(); |
401 } | 401 } |
| 402 |
| 403 TEST(Run_WasmModule_Global_init) { |
| 404 v8::internal::AccountingAllocator allocator; |
| 405 Zone zone(&allocator); |
| 406 TestSignatures sigs; |
| 407 |
| 408 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 409 uint32_t global1 = |
| 410 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(777777)); |
| 411 uint32_t global2 = |
| 412 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(222222)); |
| 413 WasmFunctionBuilder* f1 = builder->AddFunction(sigs.i_v()); |
| 414 byte code[] = { |
| 415 WASM_I32_ADD(WASM_GET_GLOBAL(global1), WASM_GET_GLOBAL(global2))}; |
| 416 f1->EmitCode(code, sizeof(code)); |
| 417 ExportAsMain(f1); |
| 418 TestModule(&zone, builder, 999999); |
| 419 } |
| 420 |
| 421 template <typename CType> |
| 422 static void RunWasmModuleGlobalInitTest(LocalType type, CType expected) { |
| 423 v8::internal::AccountingAllocator allocator; |
| 424 Zone zone(&allocator); |
| 425 TestSignatures sigs; |
| 426 |
| 427 LocalType types[] = {type}; |
| 428 FunctionSig sig(1, 0, types); |
| 429 |
| 430 for (int padding = 0; padding < 5; padding++) { |
| 431 // Test with a simple initializer |
| 432 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 433 |
| 434 for (int i = 0; i < padding; i++) { // pad global before |
| 435 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 20000)); |
| 436 } |
| 437 uint32_t global = |
| 438 builder->AddGlobal(type, false, false, WasmInitExpr(expected)); |
| 439 for (int i = 0; i < padding; i++) { // pad global after |
| 440 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 30000)); |
| 441 } |
| 442 |
| 443 WasmFunctionBuilder* f1 = builder->AddFunction(&sig); |
| 444 byte code[] = {WASM_GET_GLOBAL(global)}; |
| 445 f1->EmitCode(code, sizeof(code)); |
| 446 ExportAsMain(f1); |
| 447 TestModule(&zone, builder, expected); |
| 448 } |
| 449 |
| 450 for (int padding = 0; padding < 5; padding++) { |
| 451 // Test with a global index |
| 452 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 453 for (int i = 0; i < padding; i++) { // pad global before |
| 454 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 40000)); |
| 455 } |
| 456 |
| 457 uint32_t global1 = |
| 458 builder->AddGlobal(type, false, false, WasmInitExpr(expected)); |
| 459 |
| 460 for (int i = 0; i < padding; i++) { // pad global middle |
| 461 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 50000)); |
| 462 } |
| 463 |
| 464 uint32_t global2 = builder->AddGlobal( |
| 465 type, false, false, WasmInitExpr(WasmInitExpr::kGlobalIndex, global1)); |
| 466 |
| 467 for (int i = 0; i < padding; i++) { // pad global after |
| 468 builder->AddGlobal(kAstI32, false, false, WasmInitExpr(i + 60000)); |
| 469 } |
| 470 |
| 471 WasmFunctionBuilder* f1 = builder->AddFunction(&sig); |
| 472 byte code[] = {WASM_GET_GLOBAL(global2)}; |
| 473 f1->EmitCode(code, sizeof(code)); |
| 474 ExportAsMain(f1); |
| 475 TestModule(&zone, builder, expected); |
| 476 } |
| 477 } |
| 478 |
| 479 TEST(Run_WasmModule_Global_i32) { |
| 480 RunWasmModuleGlobalInitTest<int32_t>(kAstI32, -983489); |
| 481 RunWasmModuleGlobalInitTest<int32_t>(kAstI32, 11223344); |
| 482 } |
| 483 |
| 484 TEST(Run_WasmModule_Global_f32) { |
| 485 RunWasmModuleGlobalInitTest<float>(kAstF32, -983.9f); |
| 486 RunWasmModuleGlobalInitTest<float>(kAstF32, 1122.99f); |
| 487 } |
| 488 |
| 489 TEST(Run_WasmModule_Global_f64) { |
| 490 RunWasmModuleGlobalInitTest<double>(kAstF64, -833.9); |
| 491 RunWasmModuleGlobalInitTest<double>(kAstF64, 86374.25); |
| 492 } |
OLD | NEW |