| 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/utils/random-number-generator.h" | 12 #include "src/base/utils/random-number-generator.h" |
| 13 | 13 |
| 14 #include "src/compiler/graph-visualizer.h" | 14 #include "src/compiler/graph-visualizer.h" |
| 15 #include "src/compiler/int64-lowering.h" | 15 #include "src/compiler/int64-lowering.h" |
| 16 #include "src/compiler/js-graph.h" | 16 #include "src/compiler/js-graph.h" |
| 17 #include "src/compiler/node.h" | 17 #include "src/compiler/node.h" |
| 18 #include "src/compiler/pipeline.h" | 18 #include "src/compiler/pipeline.h" |
| 19 #include "src/compiler/wasm-compiler.h" | 19 #include "src/compiler/wasm-compiler.h" |
| 20 #include "src/compiler/zone-pool.h" |
| 20 | 21 |
| 21 #include "src/wasm/ast-decoder.h" | 22 #include "src/wasm/ast-decoder.h" |
| 22 #include "src/wasm/wasm-js.h" | 23 #include "src/wasm/wasm-js.h" |
| 23 #include "src/wasm/wasm-module.h" | 24 #include "src/wasm/wasm-module.h" |
| 24 #include "src/wasm/wasm-opcodes.h" | 25 #include "src/wasm/wasm-opcodes.h" |
| 25 | 26 |
| 26 #include "src/zone.h" | 27 #include "src/zone.h" |
| 27 | 28 |
| 28 #include "test/cctest/cctest.h" | 29 #include "test/cctest/cctest.h" |
| 29 #include "test/cctest/compiler/call-tester.h" | 30 #include "test/cctest/compiler/call-tester.h" |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 global_offset = (global_offset + size - 1) & ~(size - 1); // align | 236 global_offset = (global_offset + size - 1) & ~(size - 1); // align |
| 236 module->globals.push_back({0, 0, mem_type, global_offset, false}); | 237 module->globals.push_back({0, 0, mem_type, global_offset, false}); |
| 237 global_offset += size; | 238 global_offset += size; |
| 238 // limit number of globals. | 239 // limit number of globals. |
| 239 CHECK_LT(global_offset, kMaxGlobalsSize); | 240 CHECK_LT(global_offset, kMaxGlobalsSize); |
| 240 return &module->globals.back(); | 241 return &module->globals.back(); |
| 241 } | 242 } |
| 242 }; | 243 }; |
| 243 | 244 |
| 244 inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, | 245 inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, |
| 245 FunctionSig* sig, const byte* start, | 246 FunctionSig* sig, |
| 246 const byte* end) { | 247 SourcePositionTable* source_position_table, |
| 247 compiler::WasmGraphBuilder builder(zone, jsgraph, sig); | 248 const byte* start, const byte* end) { |
| 249 compiler::WasmGraphBuilder builder(zone, jsgraph, sig, source_position_table); |
| 248 TreeResult result = | 250 TreeResult result = |
| 249 BuildTFGraph(zone->allocator(), &builder, module, sig, start, end); | 251 BuildTFGraph(zone->allocator(), &builder, module, sig, start, end); |
| 250 if (result.failed()) { | 252 if (result.failed()) { |
| 251 ptrdiff_t pc = result.error_pc - result.start; | 253 ptrdiff_t pc = result.error_pc - result.start; |
| 252 ptrdiff_t pt = result.error_pt - result.start; | 254 ptrdiff_t pt = result.error_pt - result.start; |
| 253 std::ostringstream str; | 255 std::ostringstream str; |
| 254 str << "Verification failed: " << result.error_code << " pc = +" << pc; | 256 str << "Verification failed: " << result.error_code << " pc = +" << pc; |
| 255 if (result.error_pt) str << ", pt = +" << pt; | 257 if (result.error_pt) str << ", pt = +" << pt; |
| 256 str << ", msg = " << result.error_msg.get(); | 258 str << ", msg = " << result.error_msg.get(); |
| 257 FATAL(str.str().c_str()); | 259 FATAL(str.str().c_str()); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 private GraphAndBuilders { | 413 private GraphAndBuilders { |
| 412 public: | 414 public: |
| 413 explicit WasmFunctionCompiler(FunctionSig* sig, TestingModule* module, | 415 explicit WasmFunctionCompiler(FunctionSig* sig, TestingModule* module, |
| 414 const char* debug_name = "<WASM UNNAMED>") | 416 const char* debug_name = "<WASM UNNAMED>") |
| 415 : GraphAndBuilders(main_zone()), | 417 : GraphAndBuilders(main_zone()), |
| 416 jsgraph(this->isolate(), this->graph(), this->common(), nullptr, | 418 jsgraph(this->isolate(), this->graph(), this->common(), nullptr, |
| 417 nullptr, this->machine()), | 419 nullptr, this->machine()), |
| 418 sig(sig), | 420 sig(sig), |
| 419 descriptor_(nullptr), | 421 descriptor_(nullptr), |
| 420 testing_module_(module), | 422 testing_module_(module), |
| 421 debug_name_(debug_name) { | 423 debug_name_(debug_name), |
| 424 source_position_table_(this->graph()) { |
| 422 if (module) { | 425 if (module) { |
| 423 // Get a new function from the testing module. | 426 // Get a new function from the testing module. |
| 424 function_ = nullptr; | 427 function_ = nullptr; |
| 425 function_index_ = module->AddFunction(sig, Handle<Code>::null()); | 428 function_index_ = module->AddFunction(sig, Handle<Code>::null()); |
| 426 } else { | 429 } else { |
| 427 // Create our own function. | 430 // Create our own function. |
| 428 function_ = new WasmFunction(); | 431 function_ = new WasmFunction(); |
| 429 function_->sig = sig; | 432 function_->sig = sig; |
| 430 function_index_ = 0; | 433 function_index_ = 0; |
| 431 } | 434 } |
| 432 } | 435 } |
| 433 | 436 |
| 434 ~WasmFunctionCompiler() { | 437 ~WasmFunctionCompiler() { |
| 435 if (function_) delete function_; | 438 if (function_) delete function_; |
| 436 } | 439 } |
| 437 | 440 |
| 438 JSGraph jsgraph; | 441 JSGraph jsgraph; |
| 439 FunctionSig* sig; | 442 FunctionSig* sig; |
| 440 // The call descriptor is initialized when the function is compiled. | 443 // The call descriptor is initialized when the function is compiled. |
| 441 CallDescriptor* descriptor_; | 444 CallDescriptor* descriptor_; |
| 442 TestingModule* testing_module_; | 445 TestingModule* testing_module_; |
| 443 const char* debug_name_; | 446 const char* debug_name_; |
| 444 WasmFunction* function_; | 447 WasmFunction* function_; |
| 445 int function_index_; | 448 int function_index_; |
| 446 LocalDeclEncoder local_decls; | 449 LocalDeclEncoder local_decls; |
| 450 SourcePositionTable source_position_table_; |
| 447 | 451 |
| 448 Isolate* isolate() { return main_isolate(); } | 452 Isolate* isolate() { return main_isolate(); } |
| 449 Graph* graph() const { return main_graph_; } | 453 Graph* graph() const { return main_graph_; } |
| 450 Zone* zone() const { return graph()->zone(); } | 454 Zone* zone() const { return graph()->zone(); } |
| 451 CommonOperatorBuilder* common() { return &main_common_; } | 455 CommonOperatorBuilder* common() { return &main_common_; } |
| 452 MachineOperatorBuilder* machine() { return &main_machine_; } | 456 MachineOperatorBuilder* machine() { return &main_machine_; } |
| 453 void InitializeDescriptor() { | 457 void InitializeDescriptor() { |
| 454 if (descriptor_ == nullptr) { | 458 if (descriptor_ == nullptr) { |
| 455 descriptor_ = testing_module_->GetWasmCallDescriptor(main_zone(), sig); | 459 descriptor_ = testing_module_->GetWasmCallDescriptor(main_zone(), sig); |
| 456 } | 460 } |
| 457 } | 461 } |
| 458 CallDescriptor* descriptor() { return descriptor_; } | 462 CallDescriptor* descriptor() { return descriptor_; } |
| 459 | 463 |
| 460 void Build(const byte* start, const byte* end) { | 464 void Build(const byte* start, const byte* end) { |
| 461 // Build the TurboFan graph. | 465 // Build the TurboFan graph. |
| 462 local_decls.Prepend(&start, &end); | 466 local_decls.Prepend(&start, &end); |
| 463 TestBuildingGraph(main_zone(), &jsgraph, testing_module_, sig, start, end); | 467 TestBuildingGraph(main_zone(), &jsgraph, testing_module_, sig, |
| 468 &source_position_table_, start, end); |
| 464 delete[] start; | 469 delete[] start; |
| 465 } | 470 } |
| 466 | 471 |
| 467 byte AllocateLocal(LocalType type) { | 472 byte AllocateLocal(LocalType type) { |
| 468 uint32_t index = local_decls.AddLocals(1, type, sig); | 473 uint32_t index = local_decls.AddLocals(1, type, sig); |
| 469 byte result = static_cast<byte>(index); | 474 byte result = static_cast<byte>(index); |
| 470 DCHECK_EQ(index, result); | 475 DCHECK_EQ(index, result); |
| 471 return result; | 476 return result; |
| 472 } | 477 } |
| 473 | 478 |
| 474 Handle<Code> Compile() { | 479 Handle<Code> Compile() { |
| 475 InitializeDescriptor(); | 480 InitializeDescriptor(); |
| 476 CallDescriptor* desc = descriptor_; | 481 CallDescriptor* desc = descriptor_; |
| 477 if (kPointerSize == 4) { | 482 if (kPointerSize == 4) { |
| 478 desc = testing_module_->GetI32WasmCallDescriptor(this->zone(), desc); | 483 desc = testing_module_->GetI32WasmCallDescriptor(this->zone(), desc); |
| 479 } | 484 } |
| 480 CompilationInfo info(debug_name_, this->isolate(), this->zone(), | 485 CompilationInfo info(debug_name_, this->isolate(), this->zone(), |
| 481 Code::ComputeFlags(Code::WASM_FUNCTION)); | 486 Code::ComputeFlags(Code::WASM_FUNCTION)); |
| 482 Handle<Code> result = | 487 compiler::ZonePool zone_pool(this->isolate()->allocator()); |
| 483 Pipeline::GenerateCodeForTesting(&info, desc, this->graph()); | 488 compiler::ZonePool::Scope pipeline_zone_scope(&zone_pool); |
| 489 Pipeline pipeline(&info); |
| 490 pipeline.InitializeWasmCompilation(this->zone(), &zone_pool, this->graph(), |
| 491 &source_position_table_); |
| 492 Handle<Code> code; |
| 493 if (pipeline.ExecuteWasmCompilation(desc)) { |
| 494 code = pipeline.FinalizeWasmCompilation(desc); |
| 495 } else { |
| 496 code = Handle<Code>::null(); |
| 497 } |
| 498 pipeline_zone_scope.Destroy(); |
| 484 #ifdef ENABLE_DISASSEMBLER | 499 #ifdef ENABLE_DISASSEMBLER |
| 485 if (!result.is_null() && FLAG_print_opt_code) { | 500 if (!code.is_null() && FLAG_print_opt_code) { |
| 486 OFStream os(stdout); | 501 OFStream os(stdout); |
| 487 result->Disassemble("wasm code", os); | 502 code->Disassemble("wasm code", os); |
| 488 } | 503 } |
| 489 #endif | 504 #endif |
| 490 | 505 |
| 491 return result; | 506 return code; |
| 492 } | 507 } |
| 493 | 508 |
| 494 uint32_t CompileAndAdd(uint16_t sig_index = 0) { | 509 uint32_t CompileAndAdd(uint16_t sig_index = 0) { |
| 495 CHECK(testing_module_); | 510 CHECK(testing_module_); |
| 496 function()->sig_index = sig_index; | 511 function()->sig_index = sig_index; |
| 497 Handle<Code> code = Compile(); | 512 Handle<Code> code = Compile(); |
| 498 testing_module_->SetFunctionCode(function_index_, code); | 513 testing_module_->SetFunctionCode(function_index_, code); |
| 499 return static_cast<uint32_t>(function_index_); | 514 return static_cast<uint32_t>(function_index_); |
| 500 } | 515 } |
| 501 | 516 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 if (p1 == MachineType::None()) return 1; | 646 if (p1 == MachineType::None()) return 1; |
| 632 if (p2 == MachineType::None()) return 2; | 647 if (p2 == MachineType::None()) return 2; |
| 633 if (p3 == MachineType::None()) return 3; | 648 if (p3 == MachineType::None()) return 3; |
| 634 return 4; | 649 return 4; |
| 635 } | 650 } |
| 636 }; | 651 }; |
| 637 | 652 |
| 638 } // namespace | 653 } // namespace |
| 639 | 654 |
| 640 #endif | 655 #endif |
| OLD | NEW |