Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef WASM_RUN_UTILS_H | 5 #ifndef WASM_RUN_UTILS_H |
| 6 #define WASM_RUN_UTILS_H | 6 #define WASM_RUN_UTILS_H |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| 11 | 11 |
| 12 #include "src/base/accounting-allocator.h" | |
| 12 #include "src/base/utils/random-number-generator.h" | 13 #include "src/base/utils/random-number-generator.h" |
| 13 | 14 |
| 14 #include "src/compiler/graph-visualizer.h" | 15 #include "src/compiler/graph-visualizer.h" |
| 15 #include "src/compiler/int64-lowering.h" | 16 #include "src/compiler/int64-lowering.h" |
| 16 #include "src/compiler/js-graph.h" | 17 #include "src/compiler/js-graph.h" |
| 17 #include "src/compiler/node.h" | 18 #include "src/compiler/node.h" |
| 18 #include "src/compiler/pipeline.h" | 19 #include "src/compiler/pipeline.h" |
| 19 #include "src/compiler/wasm-compiler.h" | 20 #include "src/compiler/wasm-compiler.h" |
| 20 #include "src/compiler/zone-pool.h" | 21 #include "src/compiler/zone-pool.h" |
| 21 | 22 |
| 22 #include "src/wasm/ast-decoder.h" | 23 #include "src/wasm/ast-decoder.h" |
| 24 #include "src/wasm/wasm-interpreter.h" | |
| 23 #include "src/wasm/wasm-js.h" | 25 #include "src/wasm/wasm-js.h" |
| 24 #include "src/wasm/wasm-macro-gen.h" | 26 #include "src/wasm/wasm-macro-gen.h" |
| 25 #include "src/wasm/wasm-module.h" | 27 #include "src/wasm/wasm-module.h" |
| 26 #include "src/wasm/wasm-opcodes.h" | 28 #include "src/wasm/wasm-opcodes.h" |
| 27 | 29 |
| 28 #include "src/zone.h" | 30 #include "src/zone.h" |
| 29 | 31 |
| 30 #include "test/cctest/cctest.h" | 32 #include "test/cctest/cctest.h" |
| 31 #include "test/cctest/compiler/call-tester.h" | 33 #include "test/cctest/compiler/call-tester.h" |
| 32 #include "test/cctest/compiler/graph-builder-tester.h" | 34 #include "test/cctest/compiler/graph-builder-tester.h" |
| 33 | 35 |
| 34 // TODO(titzer): pull WASM_64 up to a common header. | 36 // TODO(titzer): pull WASM_64 up to a common header. |
| 35 #if !V8_TARGET_ARCH_32_BIT || V8_TARGET_ARCH_X64 | 37 #if !V8_TARGET_ARCH_32_BIT || V8_TARGET_ARCH_X64 |
| 36 #define WASM_64 1 | 38 #define WASM_64 1 |
| 37 #else | 39 #else |
| 38 #define WASM_64 0 | 40 #define WASM_64 0 |
| 39 #endif | 41 #endif |
| 40 | 42 |
| 41 static const uint32_t kMaxFunctions = 10; | 43 static const uint32_t kMaxFunctions = 10; |
| 44 #define WASM_INTERPRETER 1 | |
| 42 | 45 |
| 43 // TODO(titzer): check traps more robustly in tests. | 46 // TODO(titzer): check traps more robustly in tests. |
| 44 // Currently, in tests, we just return 0xdeadbeef from the function in which | 47 // Currently, in tests, we just return 0xdeadbeef from the function in which |
| 45 // the trap occurs if the runtime context is not available to throw a JavaScript | 48 // the trap occurs if the runtime context is not available to throw a JavaScript |
| 46 // exception. | 49 // exception. |
| 47 #define CHECK_TRAP32(x) \ | 50 #define CHECK_TRAP32(x) \ |
| 48 CHECK_EQ(0xdeadbeef, (bit_cast<uint32_t>(x)) & 0xFFFFFFFF) | 51 CHECK_EQ(0xdeadbeef, (bit_cast<uint32_t>(x)) & 0xFFFFFFFF) |
| 49 #define CHECK_TRAP64(x) \ | 52 #define CHECK_TRAP64(x) \ |
| 50 CHECK_EQ(0xdeadbeefdeadbeef, (bit_cast<uint64_t>(x)) & 0xFFFFFFFFFFFFFFFF) | 53 CHECK_EQ(0xdeadbeefdeadbeef, (bit_cast<uint64_t>(x)) & 0xFFFFFFFFFFFFFFFF) |
| 51 #define CHECK_TRAP(x) CHECK_TRAP32(x) | 54 #define CHECK_TRAP(x) CHECK_TRAP32(x) |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 65 using namespace v8::internal::compiler; | 68 using namespace v8::internal::compiler; |
| 66 using namespace v8::internal::wasm; | 69 using namespace v8::internal::wasm; |
| 67 | 70 |
| 68 const uint32_t kMaxGlobalsSize = 128; | 71 const uint32_t kMaxGlobalsSize = 128; |
| 69 | 72 |
| 70 // A helper for module environments that adds the ability to allocate memory | 73 // A helper for module environments that adds the ability to allocate memory |
| 71 // and global variables. Contains a built-in {WasmModule} and | 74 // and global variables. Contains a built-in {WasmModule} and |
| 72 // {WasmModuleInstance}. | 75 // {WasmModuleInstance}. |
| 73 class TestingModule : public ModuleEnv { | 76 class TestingModule : public ModuleEnv { |
| 74 public: | 77 public: |
| 75 TestingModule() : instance_(&module_), global_offset(0) { | 78 TestingModule() |
| 79 : instance_(&module_), | |
| 80 global_offset(0), | |
| 81 interpreter_(&instance_, &allocator_) { | |
| 76 module_.shared_isolate = CcTest::InitIsolateOnce(); | 82 module_.shared_isolate = CcTest::InitIsolateOnce(); |
| 77 module = &module_; | 83 module = &module_; |
| 78 instance = &instance_; | 84 instance = &instance_; |
| 79 instance->module = &module_; | 85 instance->module = &module_; |
| 80 instance->globals_start = global_data; | 86 instance->globals_start = global_data; |
| 81 instance->globals_size = kMaxGlobalsSize; | 87 instance->globals_size = kMaxGlobalsSize; |
| 82 instance->mem_start = nullptr; | 88 instance->mem_start = nullptr; |
| 83 instance->mem_size = 0; | 89 instance->mem_size = 0; |
| 84 linker = nullptr; | 90 linker = nullptr; |
| 85 origin = kWasmOrigin; | 91 origin = kWasmOrigin; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 uint32_t AddFunction(FunctionSig* sig, Handle<Code> code) { | 170 uint32_t AddFunction(FunctionSig* sig, Handle<Code> code) { |
| 165 if (module->functions.size() == 0) { | 171 if (module->functions.size() == 0) { |
| 166 // TODO(titzer): Reserving space here to avoid the underlying WasmFunction | 172 // TODO(titzer): Reserving space here to avoid the underlying WasmFunction |
| 167 // structs from moving. | 173 // structs from moving. |
| 168 module->functions.reserve(kMaxFunctions); | 174 module->functions.reserve(kMaxFunctions); |
| 169 } | 175 } |
| 170 uint32_t index = static_cast<uint32_t>(module->functions.size()); | 176 uint32_t index = static_cast<uint32_t>(module->functions.size()); |
| 171 module->functions.push_back( | 177 module->functions.push_back( |
| 172 {sig, index, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false}); | 178 {sig, index, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false}); |
| 173 instance->function_code.push_back(code); | 179 instance->function_code.push_back(code); |
| 180 WasmFunction* function = &module->functions.back(); | |
| 181 int interpreter_index = interpreter_.AddFunctionForTesting(function); | |
| 182 DCHECK_EQ(index, static_cast<uint32_t>(interpreter_index)); | |
| 174 DCHECK_LT(index, kMaxFunctions); // limited for testing. | 183 DCHECK_LT(index, kMaxFunctions); // limited for testing. |
| 175 return index; | 184 return index; |
| 176 } | 185 } |
| 177 | 186 |
| 178 uint32_t AddJsFunction(FunctionSig* sig, const char* source) { | 187 uint32_t AddJsFunction(FunctionSig* sig, const char* source) { |
| 179 Handle<JSFunction> jsfunc = Handle<JSFunction>::cast(v8::Utils::OpenHandle( | 188 Handle<JSFunction> jsfunc = Handle<JSFunction>::cast(v8::Utils::OpenHandle( |
| 180 *v8::Local<v8::Function>::Cast(CompileRun(source)))); | 189 *v8::Local<v8::Function>::Cast(CompileRun(source)))); |
| 181 uint32_t index = AddFunction(sig, Handle<Code>::null()); | 190 uint32_t index = AddFunction(sig, Handle<Code>::null()); |
| 182 Isolate* isolate = module->shared_isolate; | 191 Isolate* isolate = module->shared_isolate; |
| 183 WasmName module_name = ArrayVector("test"); | 192 WasmName module_name = ArrayVector("test"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 int table_size = static_cast<int>(module->function_table.size()); | 228 int table_size = static_cast<int>(module->function_table.size()); |
| 220 for (int i = 0; i < table_size; i++) { | 229 for (int i = 0; i < table_size; i++) { |
| 221 int function_index = module->function_table[i]; | 230 int function_index = module->function_table[i]; |
| 222 WasmFunction* function = &module->functions[function_index]; | 231 WasmFunction* function = &module->functions[function_index]; |
| 223 instance->function_table->set(i, Smi::FromInt(function->sig_index)); | 232 instance->function_table->set(i, Smi::FromInt(function->sig_index)); |
| 224 instance->function_table->set(i + table_size, | 233 instance->function_table->set(i + table_size, |
| 225 *instance->function_code[function_index]); | 234 *instance->function_code[function_index]); |
| 226 } | 235 } |
| 227 } | 236 } |
| 228 | 237 |
| 238 WasmInterpreter* GetInterpreter() { return &interpreter_; } | |
| 239 | |
| 229 private: | 240 private: |
| 230 WasmModule module_; | 241 WasmModule module_; |
| 231 WasmModuleInstance instance_; | 242 WasmModuleInstance instance_; |
| 243 v8::base::AccountingAllocator allocator_; | |
| 232 uint32_t global_offset; | 244 uint32_t global_offset; |
| 233 V8_ALIGNED(8) byte global_data[kMaxGlobalsSize]; // preallocated global data. | 245 V8_ALIGNED(8) byte global_data[kMaxGlobalsSize]; // preallocated global data. |
| 246 // TODO(titzer): This is ugly. Because the functions must be manually added | |
| 247 // to the interpreter for testing, it needs to be accessible to each creation | |
| 248 // of a WasmFunctionCompiler below. | |
| 249 WasmInterpreter interpreter_; | |
| 234 | 250 |
| 235 WasmGlobal* AddGlobal(MachineType mem_type) { | 251 WasmGlobal* AddGlobal(MachineType mem_type) { |
| 236 byte size = WasmOpcodes::MemSize(mem_type); | 252 byte size = WasmOpcodes::MemSize(mem_type); |
| 237 global_offset = (global_offset + size - 1) & ~(size - 1); // align | 253 global_offset = (global_offset + size - 1) & ~(size - 1); // align |
| 238 module->globals.push_back({0, 0, mem_type, global_offset, false}); | 254 module->globals.push_back({0, 0, mem_type, global_offset, false}); |
| 239 global_offset += size; | 255 global_offset += size; |
| 240 // limit number of globals. | 256 // limit number of globals. |
| 241 CHECK_LT(global_offset, kMaxGlobalsSize); | 257 CHECK_LT(global_offset, kMaxGlobalsSize); |
| 242 return &module->globals.back(); | 258 return &module->globals.back(); |
| 243 } | 259 } |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 | 418 |
| 403 private: | 419 private: |
| 404 Node* inner_code_node_; | 420 Node* inner_code_node_; |
| 405 Handle<Code> code_; | 421 Handle<Code> code_; |
| 406 Signature<MachineType>* signature_; | 422 Signature<MachineType>* signature_; |
| 407 }; | 423 }; |
| 408 | 424 |
| 409 // A helper for compiling WASM functions for testing. This class can create a | 425 // A helper for compiling WASM functions for testing. This class can create a |
| 410 // standalone function if {module} is NULL or a function within a | 426 // standalone function if {module} is NULL or a function within a |
| 411 // {TestingModule}. It contains the internal state for compilation (i.e. | 427 // {TestingModule}. It contains the internal state for compilation (i.e. |
| 412 // TurboFan graph) and, later, interpretation. | 428 // TurboFan graph) and interpretation (by adding to the interpreter manually). |
| 413 class WasmFunctionCompiler : public HandleAndZoneScope, | 429 class WasmFunctionCompiler : public HandleAndZoneScope, |
| 414 private GraphAndBuilders { | 430 private GraphAndBuilders { |
| 415 public: | 431 public: |
| 416 explicit WasmFunctionCompiler( | 432 explicit WasmFunctionCompiler( |
| 417 FunctionSig* sig, TestingModule* module, | 433 FunctionSig* sig, TestingModule* module, |
| 418 Vector<const char> debug_name = ArrayVector("<WASM UNNAMED>")) | 434 Vector<const char> debug_name = ArrayVector("<WASM UNNAMED>")) |
| 419 : GraphAndBuilders(main_zone()), | 435 : GraphAndBuilders(main_zone()), |
| 420 jsgraph(this->isolate(), this->graph(), this->common(), nullptr, | 436 jsgraph(this->isolate(), this->graph(), this->common(), nullptr, |
| 421 nullptr, this->machine()), | 437 nullptr, this->machine()), |
| 422 sig(sig), | 438 sig(sig), |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 Zone* zone() const { return graph()->zone(); } | 472 Zone* zone() const { return graph()->zone(); } |
| 457 CommonOperatorBuilder* common() { return &main_common_; } | 473 CommonOperatorBuilder* common() { return &main_common_; } |
| 458 MachineOperatorBuilder* machine() { return &main_machine_; } | 474 MachineOperatorBuilder* machine() { return &main_machine_; } |
| 459 void InitializeDescriptor() { | 475 void InitializeDescriptor() { |
| 460 if (descriptor_ == nullptr) { | 476 if (descriptor_ == nullptr) { |
| 461 descriptor_ = testing_module_->GetWasmCallDescriptor(main_zone(), sig); | 477 descriptor_ = testing_module_->GetWasmCallDescriptor(main_zone(), sig); |
| 462 } | 478 } |
| 463 } | 479 } |
| 464 CallDescriptor* descriptor() { return descriptor_; } | 480 CallDescriptor* descriptor() { return descriptor_; } |
| 465 | 481 |
| 466 void Build(const byte* start, const byte* end) { | 482 void Build(const byte* start, const byte* end, |
| 483 WasmInterpreter* interpreter = nullptr) { | |
| 467 // Build the TurboFan graph. | 484 // Build the TurboFan graph. |
| 468 local_decls.Prepend(&start, &end); | 485 local_decls.Prepend(&start, &end); |
| 469 TestBuildingGraph(main_zone(), &jsgraph, testing_module_, sig, | 486 TestBuildingGraph(main_zone(), &jsgraph, testing_module_, sig, |
| 470 &source_position_table_, start, end); | 487 &source_position_table_, start, end); |
| 471 delete[] start; | 488 #if WASM_INTERPRETER |
| 489 // Build the interpreter code. | |
| 490 if (!testing_module_) { | |
| 491 // No module; the function was never added to the interpreter. | |
| 492 int index = interpreter->AddFunctionForTesting(function_); | |
| 493 CHECK_EQ(function_index_, index); | |
| 494 } else { | |
| 495 // Always use the interpreter from the module. | |
| 496 interpreter = testing_module_->GetInterpreter(); | |
| 497 } | |
| 498 CHECK(interpreter->SetFunctionCodeForTesting(function(), start, end)); | |
| 499 // TODO(titzer): memory leak!!!! delete[] start; | |
|
Clemens Hammacher
2016/05/12 15:20:55
SetFunctionCodeForTesting could allocate memory fo
| |
| 500 #endif | |
| 472 } | 501 } |
| 473 | 502 |
| 474 byte AllocateLocal(LocalType type) { | 503 byte AllocateLocal(LocalType type) { |
| 475 uint32_t index = local_decls.AddLocals(1, type, sig); | 504 uint32_t index = local_decls.AddLocals(1, type, sig); |
| 476 byte result = static_cast<byte>(index); | 505 byte result = static_cast<byte>(index); |
| 477 DCHECK_EQ(index, result); | 506 DCHECK_EQ(index, result); |
| 478 return result; | 507 return result; |
| 479 } | 508 } |
| 480 | 509 |
| 481 Handle<Code> Compile() { | 510 Handle<Code> Compile() { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 544 class WasmRunner { | 573 class WasmRunner { |
| 545 public: | 574 public: |
| 546 WasmRunner(MachineType p0 = MachineType::None(), | 575 WasmRunner(MachineType p0 = MachineType::None(), |
| 547 MachineType p1 = MachineType::None(), | 576 MachineType p1 = MachineType::None(), |
| 548 MachineType p2 = MachineType::None(), | 577 MachineType p2 = MachineType::None(), |
| 549 MachineType p3 = MachineType::None()) | 578 MachineType p3 = MachineType::None()) |
| 550 : zone(&allocator_), | 579 : zone(&allocator_), |
| 551 compiled_(false), | 580 compiled_(false), |
| 552 signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1, | 581 signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1, |
| 553 GetParameterCount(p0, p1, p2, p3), storage_), | 582 GetParameterCount(p0, p1, p2, p3), storage_), |
| 554 compiler_(&signature_, nullptr) { | 583 compiler_(&signature_, nullptr), |
| 584 interpreter_(new WasmInterpreter(nullptr, &allocator_)) { | |
| 555 InitSigStorage(p0, p1, p2, p3); | 585 InitSigStorage(p0, p1, p2, p3); |
| 556 } | 586 } |
| 557 | 587 |
| 558 WasmRunner(TestingModule* module, MachineType p0 = MachineType::None(), | 588 WasmRunner(TestingModule* module, MachineType p0 = MachineType::None(), |
| 559 MachineType p1 = MachineType::None(), | 589 MachineType p1 = MachineType::None(), |
| 560 MachineType p2 = MachineType::None(), | 590 MachineType p2 = MachineType::None(), |
| 561 MachineType p3 = MachineType::None()) | 591 MachineType p3 = MachineType::None()) |
| 562 : zone(&allocator_), | 592 : zone(&allocator_), |
| 563 compiled_(false), | 593 compiled_(false), |
| 564 signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1, | 594 signature_(MachineTypeForC<ReturnType>() == MachineType::None() ? 0 : 1, |
| 565 GetParameterCount(p0, p1, p2, p3), storage_), | 595 GetParameterCount(p0, p1, p2, p3), storage_), |
| 566 compiler_(&signature_, module) { | 596 compiler_(&signature_, module), |
| 597 interpreter_(nullptr) { | |
| 567 DCHECK(module); | 598 DCHECK(module); |
| 568 InitSigStorage(p0, p1, p2, p3); | 599 InitSigStorage(p0, p1, p2, p3); |
| 569 } | 600 } |
| 570 | 601 |
| 571 void InitSigStorage(MachineType p0, MachineType p1, MachineType p2, | 602 void InitSigStorage(MachineType p0, MachineType p1, MachineType p2, |
| 572 MachineType p3) { | 603 MachineType p3) { |
| 573 int index = 0; | 604 int index = 0; |
| 574 MachineType ret = MachineTypeForC<ReturnType>(); | 605 MachineType ret = MachineTypeForC<ReturnType>(); |
| 575 if (ret != MachineType::None()) { | 606 if (ret != MachineType::None()) { |
| 576 storage_[index++] = WasmOpcodes::LocalTypeFor(ret); | 607 storage_[index++] = WasmOpcodes::LocalTypeFor(ret); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 588 wrapper_.Init(compiler_.descriptor(), p0, p1, p2, p3); | 619 wrapper_.Init(compiler_.descriptor(), p0, p1, p2, p3); |
| 589 } | 620 } |
| 590 | 621 |
| 591 // Builds a graph from the given Wasm code and generates the machine | 622 // Builds a graph from the given Wasm code and generates the machine |
| 592 // code and call wrapper for that graph. This method must not be called | 623 // code and call wrapper for that graph. This method must not be called |
| 593 // more than once. | 624 // more than once. |
| 594 void Build(const byte* start, const byte* end) { | 625 void Build(const byte* start, const byte* end) { |
| 595 CHECK(!compiled_); | 626 CHECK(!compiled_); |
| 596 compiled_ = true; | 627 compiled_ = true; |
| 597 | 628 |
| 598 // Build the TF graph within the compiler. | 629 if (WASM_INTERPRETER) { |
| 599 compiler_.Build(start, end); | 630 // Build the interpreter code. |
| 600 // Generate code. | 631 compiler_.Build(start, end, interpreter_); |
| 601 Handle<Code> code = compiler_.Compile(); | 632 } else { |
| 633 // Build the TF graph within the compiler. | |
| 634 compiler_.Build(start, end); | |
| 635 // Generate code. | |
| 636 Handle<Code> code = compiler_.Compile(); | |
| 602 | 637 |
| 603 if (compiler_.testing_module_) { | 638 if (compiler_.testing_module_) { |
| 604 // Update the table of function code in the module. | 639 // Update the table of function code in the module. |
| 605 compiler_.testing_module_->SetFunctionCode(compiler_.function_index_, | 640 compiler_.testing_module_->SetFunctionCode(compiler_.function_index_, |
| 606 code); | 641 code); |
| 642 } | |
| 643 | |
| 644 wrapper_.SetInnerCode(code); | |
| 607 } | 645 } |
| 608 | |
| 609 wrapper_.SetInnerCode(code); | |
| 610 } | 646 } |
| 611 | 647 |
| 612 ReturnType Call() { return Call(0, 0, 0, 0); } | 648 ReturnType Call() { |
| 649 if (WASM_INTERPRETER) { | |
| 650 return CallInterpreter(nullptr, 0); | |
| 651 } else { | |
| 652 return Call(0, 0, 0, 0); | |
| 653 } | |
| 654 } | |
| 613 | 655 |
| 614 template <typename P0> | 656 template <typename P0> |
| 615 ReturnType Call(P0 p0) { | 657 ReturnType Call(P0 p0) { |
| 616 return Call(p0, 0, 0, 0); | 658 if (WASM_INTERPRETER) { |
| 659 WasmVal args[] = {WasmVal(p0)}; | |
| 660 return CallInterpreter(args, 1); | |
| 661 } else { | |
| 662 return Call(p0, 0, 0, 0); | |
| 663 } | |
| 617 } | 664 } |
| 618 | 665 |
| 619 template <typename P0, typename P1> | 666 template <typename P0, typename P1> |
| 620 ReturnType Call(P0 p0, P1 p1) { | 667 ReturnType Call(P0 p0, P1 p1) { |
| 621 return Call(p0, p1, 0, 0); | 668 if (WASM_INTERPRETER) { |
| 669 WasmVal args[] = {WasmVal(p0), WasmVal(p1)}; | |
| 670 return CallInterpreter(args, 2); | |
| 671 } else { | |
| 672 return Call(p0, p1, 0, 0); | |
| 673 } | |
| 622 } | 674 } |
| 623 | 675 |
| 624 template <typename P0, typename P1, typename P2> | 676 template <typename P0, typename P1, typename P2> |
| 625 ReturnType Call(P0 p0, P1 p1, P2 p2) { | 677 ReturnType Call(P0 p0, P1 p1, P2 p2) { |
| 626 return Call(p0, p1, p2, 0); | 678 if (WASM_INTERPRETER) { |
| 679 WasmVal args[] = {WasmVal(p0), WasmVal(p1), WasmVal(p2)}; | |
| 680 return CallInterpreter(args, 3); | |
| 681 } else { | |
| 682 return Call(p0, p1, p2, 0); | |
| 683 } | |
| 627 } | 684 } |
| 628 | 685 |
| 629 template <typename P0, typename P1, typename P2, typename P3> | 686 template <typename P0, typename P1, typename P2, typename P3> |
| 630 ReturnType Call(P0 p0, P1 p1, P2 p2, P3 p3) { | 687 ReturnType Call(P0 p0, P1 p1, P2 p2, P3 p3) { |
| 631 CodeRunner<int32_t> runner(CcTest::InitIsolateOnce(), | 688 if (WASM_INTERPRETER) { |
| 632 wrapper_.GetWrapperCode(), wrapper_.signature()); | 689 WasmVal args[] = {WasmVal(p0), WasmVal(p1), WasmVal(p2), WasmVal(p3)}; |
| 633 ReturnType return_value; | 690 return CallInterpreter(args, 4); |
| 634 int32_t result = runner.Call<void*, void*, void*, void*, void*>( | 691 } else { |
| 635 &p0, &p1, &p2, &p3, &return_value); | 692 CodeRunner<int32_t> runner(CcTest::InitIsolateOnce(), |
| 636 CHECK_EQ(WASM_WRAPPER_RETURN_VALUE, result); | 693 wrapper_.GetWrapperCode(), |
| 637 return return_value; | 694 wrapper_.signature()); |
| 695 ReturnType return_value; | |
| 696 int32_t result = runner.Call<void*, void*, void*, void*, void*>( | |
| 697 &p0, &p1, &p2, &p3, &return_value); | |
| 698 CHECK_EQ(WASM_WRAPPER_RETURN_VALUE, result); | |
| 699 return return_value; | |
| 700 } | |
| 701 } | |
| 702 | |
| 703 ReturnType CallInterpreter(WasmVal* args, int count) { | |
| 704 CHECK_EQ(count, | |
| 705 static_cast<int>(compiler_.function()->sig->parameter_count())); | |
| 706 WasmInterpreter* interpreter = GetInterpreter(); | |
| 707 | |
| 708 WasmInterpreter::Thread& thread = interpreter->GetThread(0); | |
| 709 thread.Reset(); | |
| 710 thread.PushFrame(compiler_.function(), args); | |
| 711 if (thread.Run() == WasmInterpreter::FINISHED) { | |
| 712 WasmVal val = thread.GetReturnValue(); | |
| 713 return val.to<ReturnType>(); | |
| 714 } else if (thread.state() == WasmInterpreter::TRAPPED) { | |
| 715 // TODO(titzer): return the correct trap code | |
| 716 int64_t result = 0xdeadbeefdeadbeef; | |
| 717 return static_cast<ReturnType>(result); | |
| 718 } else { | |
|
ahaas
2016/05/13 12:18:55
if the program is more than 1000 (wasm-interpreter
| |
| 719 // TODO(titzer): falling off end | |
| 720 ReturnType val = 0; | |
| 721 return val; | |
| 722 } | |
| 638 } | 723 } |
| 639 | 724 |
| 640 byte AllocateLocal(LocalType type) { return compiler_.AllocateLocal(type); } | 725 byte AllocateLocal(LocalType type) { return compiler_.AllocateLocal(type); } |
| 641 | 726 |
| 727 WasmInterpreter* GetInterpreter() { | |
| 728 return compiler_.testing_module_ | |
| 729 ? compiler_.testing_module_->GetInterpreter() | |
| 730 : interpreter_; | |
| 731 } | |
| 732 | |
| 733 WasmFunction* function() { return compiler_.function(); } | |
| 734 | |
| 642 protected: | 735 protected: |
| 643 v8::base::AccountingAllocator allocator_; | 736 v8::base::AccountingAllocator allocator_; |
| 644 Zone zone; | 737 Zone zone; |
| 645 bool compiled_; | 738 bool compiled_; |
| 646 LocalType storage_[WASM_RUNNER_MAX_NUM_PARAMETERS]; | 739 LocalType storage_[WASM_RUNNER_MAX_NUM_PARAMETERS]; |
| 647 FunctionSig signature_; | 740 FunctionSig signature_; |
| 648 WasmFunctionCompiler compiler_; | 741 WasmFunctionCompiler compiler_; |
| 649 WasmFunctionWrapper<ReturnType> wrapper_; | 742 WasmFunctionWrapper<ReturnType> wrapper_; |
| 743 WasmInterpreter* interpreter_; | |
| 650 | 744 |
| 651 static size_t GetParameterCount(MachineType p0, MachineType p1, | 745 static size_t GetParameterCount(MachineType p0, MachineType p1, |
| 652 MachineType p2, MachineType p3) { | 746 MachineType p2, MachineType p3) { |
| 653 if (p0 == MachineType::None()) return 0; | 747 if (p0 == MachineType::None()) return 0; |
| 654 if (p1 == MachineType::None()) return 1; | 748 if (p1 == MachineType::None()) return 1; |
| 655 if (p2 == MachineType::None()) return 2; | 749 if (p2 == MachineType::None()) return 2; |
| 656 if (p3 == MachineType::None()) return 3; | 750 if (p3 == MachineType::None()) return 3; |
| 657 return 4; | 751 return 4; |
| 658 } | 752 } |
| 659 }; | 753 }; |
| 660 | 754 |
| 661 } // namespace | 755 } // namespace |
| 662 | 756 |
| 663 #endif | 757 #endif |
| OLD | NEW |