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> |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 Signature<MachineType>* signature_; | 403 Signature<MachineType>* signature_; |
404 }; | 404 }; |
405 | 405 |
406 // A helper for compiling WASM functions for testing. This class can create a | 406 // A helper for compiling WASM functions for testing. This class can create a |
407 // standalone function if {module} is NULL or a function within a | 407 // standalone function if {module} is NULL or a function within a |
408 // {TestingModule}. It contains the internal state for compilation (i.e. | 408 // {TestingModule}. It contains the internal state for compilation (i.e. |
409 // TurboFan graph) and, later, interpretation. | 409 // TurboFan graph) and, later, interpretation. |
410 class WasmFunctionCompiler : public HandleAndZoneScope, | 410 class WasmFunctionCompiler : public HandleAndZoneScope, |
411 private GraphAndBuilders { | 411 private GraphAndBuilders { |
412 public: | 412 public: |
413 explicit WasmFunctionCompiler(FunctionSig* sig, TestingModule* module) | 413 explicit WasmFunctionCompiler(FunctionSig* sig, TestingModule* module, |
414 const char* debug_name = "wasm_unnamed") | |
JF
2016/04/12 19:38:54
It's probably clearer to give it a name with unusu
Clemens Hammacher
2016/04/13 07:45:18
Done.
| |
414 : GraphAndBuilders(main_zone()), | 415 : GraphAndBuilders(main_zone()), |
415 jsgraph(this->isolate(), this->graph(), this->common(), nullptr, | 416 jsgraph(this->isolate(), this->graph(), this->common(), nullptr, |
416 nullptr, this->machine()), | 417 nullptr, this->machine()), |
417 sig(sig), | 418 sig(sig), |
418 descriptor_(nullptr), | 419 descriptor_(nullptr), |
419 testing_module_(module) { | 420 testing_module_(module), |
421 debug_name(debug_name) { | |
420 if (module) { | 422 if (module) { |
421 // Get a new function from the testing module. | 423 // Get a new function from the testing module. |
422 function_ = nullptr; | 424 function_ = nullptr; |
423 function_index_ = module->AddFunction(sig, Handle<Code>::null()); | 425 function_index_ = module->AddFunction(sig, Handle<Code>::null()); |
424 } else { | 426 } else { |
425 // Create our own function. | 427 // Create our own function. |
426 function_ = new WasmFunction(); | 428 function_ = new WasmFunction(); |
427 function_->sig = sig; | 429 function_->sig = sig; |
428 function_index_ = 0; | 430 function_index_ = 0; |
429 } | 431 } |
430 } | 432 } |
431 | 433 |
432 ~WasmFunctionCompiler() { | 434 ~WasmFunctionCompiler() { |
433 if (function_) delete function_; | 435 if (function_) delete function_; |
434 } | 436 } |
435 | 437 |
436 JSGraph jsgraph; | 438 JSGraph jsgraph; |
437 FunctionSig* sig; | 439 FunctionSig* sig; |
438 // The call descriptor is initialized when the function is compiled. | 440 // The call descriptor is initialized when the function is compiled. |
439 CallDescriptor* descriptor_; | 441 CallDescriptor* descriptor_; |
440 TestingModule* testing_module_; | 442 TestingModule* testing_module_; |
443 const char* debug_name; | |
441 WasmFunction* function_; | 444 WasmFunction* function_; |
442 int function_index_; | 445 int function_index_; |
443 LocalDeclEncoder local_decls; | 446 LocalDeclEncoder local_decls; |
444 | 447 |
445 Isolate* isolate() { return main_isolate(); } | 448 Isolate* isolate() { return main_isolate(); } |
446 Graph* graph() const { return main_graph_; } | 449 Graph* graph() const { return main_graph_; } |
447 Zone* zone() const { return graph()->zone(); } | 450 Zone* zone() const { return graph()->zone(); } |
448 CommonOperatorBuilder* common() { return &main_common_; } | 451 CommonOperatorBuilder* common() { return &main_common_; } |
449 MachineOperatorBuilder* machine() { return &main_machine_; } | 452 MachineOperatorBuilder* machine() { return &main_machine_; } |
450 void InitializeDescriptor() { | 453 void InitializeDescriptor() { |
(...skipping 16 matching lines...) Expand all Loading... | |
467 DCHECK_EQ(index, result); | 470 DCHECK_EQ(index, result); |
468 return result; | 471 return result; |
469 } | 472 } |
470 | 473 |
471 Handle<Code> Compile() { | 474 Handle<Code> Compile() { |
472 InitializeDescriptor(); | 475 InitializeDescriptor(); |
473 CallDescriptor* desc = descriptor_; | 476 CallDescriptor* desc = descriptor_; |
474 if (kPointerSize == 4) { | 477 if (kPointerSize == 4) { |
475 desc = testing_module_->GetI32WasmCallDescriptor(this->zone(), desc); | 478 desc = testing_module_->GetI32WasmCallDescriptor(this->zone(), desc); |
476 } | 479 } |
477 CompilationInfo info("wasm compile", this->isolate(), this->zone(), | 480 CompilationInfo info(debug_name, this->isolate(), this->zone(), |
478 Code::ComputeFlags(Code::WASM_FUNCTION)); | 481 Code::ComputeFlags(Code::WASM_FUNCTION)); |
479 Handle<Code> result = | 482 Handle<Code> result = |
480 Pipeline::GenerateCodeForTesting(&info, desc, this->graph()); | 483 Pipeline::GenerateCodeForTesting(&info, desc, this->graph()); |
481 #ifdef ENABLE_DISASSEMBLER | 484 #ifdef ENABLE_DISASSEMBLER |
482 if (!result.is_null() && FLAG_print_opt_code) { | 485 if (!result.is_null() && FLAG_print_opt_code) { |
483 OFStream os(stdout); | 486 OFStream os(stdout); |
484 result->Disassemble("wasm code", os); | 487 result->Disassemble("wasm code", os); |
485 } | 488 } |
486 #endif | 489 #endif |
487 | 490 |
488 return result; | 491 return result; |
489 } | 492 } |
490 | 493 |
491 uint32_t CompileAndAdd(uint16_t sig_index = 0) { | 494 uint32_t CompileAndAdd(uint16_t sig_index = 0) { |
492 CHECK(testing_module_); | 495 CHECK(testing_module_); |
493 function()->sig_index = sig_index; | 496 function()->sig_index = sig_index; |
494 Handle<Code> code = Compile(); | 497 Handle<Code> code = Compile(); |
495 testing_module_->SetFunctionCode(function_index_, code); | 498 testing_module_->SetFunctionCode(function_index_, code); |
496 return static_cast<uint32_t>(function_index_); | 499 return static_cast<uint32_t>(function_index_); |
497 } | 500 } |
498 | 501 |
499 WasmFunction* function() { | 502 WasmFunction* function() { |
500 if (function_) return function_; | 503 if (function_) return function_; |
501 return &testing_module_->module->functions[function_index_]; | 504 return &testing_module_->module->functions[function_index_]; |
502 } | 505 } |
506 | |
507 void SetModuleContext() { | |
508 // Set the context, such that e.g. runtime functions can be called | |
509 if (!testing_module_->instance->context.is_null()) { | |
510 CHECK(testing_module_->instance->context.is_identical_to( | |
511 main_isolate()->native_context())); | |
512 return; | |
513 } | |
514 testing_module_->instance->context = main_isolate()->native_context(); | |
515 } | |
503 }; | 516 }; |
504 | 517 |
505 // A helper class to build graphs from Wasm bytecode, generate machine | 518 // A helper class to build graphs from Wasm bytecode, generate machine |
506 // code, and run that code. | 519 // code, and run that code. |
507 template <typename ReturnType> | 520 template <typename ReturnType> |
508 class WasmRunner { | 521 class WasmRunner { |
509 public: | 522 public: |
510 WasmRunner(MachineType p0 = MachineType::None(), | 523 WasmRunner(MachineType p0 = MachineType::None(), |
511 MachineType p1 = MachineType::None(), | 524 MachineType p1 = MachineType::None(), |
512 MachineType p2 = MachineType::None(), | 525 MachineType p2 = MachineType::None(), |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
618 if (p1 == MachineType::None()) return 1; | 631 if (p1 == MachineType::None()) return 1; |
619 if (p2 == MachineType::None()) return 2; | 632 if (p2 == MachineType::None()) return 2; |
620 if (p3 == MachineType::None()) return 3; | 633 if (p3 == MachineType::None()) return 3; |
621 return 4; | 634 return 4; |
622 } | 635 } |
623 }; | 636 }; |
624 | 637 |
625 } // namespace | 638 } // namespace |
626 | 639 |
627 #endif | 640 #endif |
OLD | NEW |