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 <setjmp.h> | 8 #include <setjmp.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 uint32_t index = AddFunction(sig, Handle<Code>::null(), nullptr); | 217 uint32_t index = AddFunction(sig, Handle<Code>::null(), nullptr); |
218 Handle<Code> code = CompileWasmToJSWrapper( | 218 Handle<Code> code = CompileWasmToJSWrapper( |
219 isolate_, jsfunc, sig, index, Handle<String>::null(), | 219 isolate_, jsfunc, sig, index, Handle<String>::null(), |
220 Handle<String>::null(), module->origin); | 220 Handle<String>::null(), module->origin); |
221 instance->function_code[index] = code; | 221 instance->function_code[index] = code; |
222 return index; | 222 return index; |
223 } | 223 } |
224 | 224 |
225 Handle<JSFunction> WrapCode(uint32_t index) { | 225 Handle<JSFunction> WrapCode(uint32_t index) { |
226 // Wrap the code so it can be called as a JS function. | 226 // Wrap the code so it can be called as a JS function. |
227 Handle<WasmInstanceObject> instance_obj(0, isolate_); | |
228 Handle<Code> code = instance->function_code[index]; | 227 Handle<Code> code = instance->function_code[index]; |
229 Handle<Code> ret_code = | 228 Handle<Code> ret_code = |
230 compiler::CompileJSToWasmWrapper(isolate_, &module_, code, index); | 229 compiler::CompileJSToWasmWrapper(isolate_, &module_, code, index); |
231 Handle<JSFunction> ret = WasmExportedFunction::New( | 230 Handle<JSFunction> ret = WasmExportedFunction::New( |
232 isolate_, instance_obj, MaybeHandle<String>(), static_cast<int>(index), | 231 isolate_, instance_object(), MaybeHandle<String>(), |
| 232 static_cast<int>(index), |
233 static_cast<int>(this->module->functions[index].sig->parameter_count()), | 233 static_cast<int>(this->module->functions[index].sig->parameter_count()), |
234 ret_code); | 234 ret_code); |
| 235 |
| 236 // Add weak reference to exported functions. |
| 237 Handle<WasmCompiledModule> compiled_module( |
| 238 instance_object()->compiled_module(), isolate_); |
| 239 Handle<FixedArray> old_arr = compiled_module->weak_exported_functions(); |
| 240 Handle<FixedArray> new_arr = |
| 241 isolate_->factory()->NewFixedArray(old_arr->length() + 1); |
| 242 old_arr->CopyTo(0, *new_arr, 0, old_arr->length()); |
| 243 Handle<WeakCell> weak_fn = isolate_->factory()->NewWeakCell(ret); |
| 244 new_arr->set(old_arr->length(), *weak_fn); |
| 245 compiled_module->set_weak_exported_functions(new_arr); |
| 246 |
235 return ret; | 247 return ret; |
236 } | 248 } |
237 | 249 |
238 void SetFunctionCode(uint32_t index, Handle<Code> code) { | 250 void SetFunctionCode(uint32_t index, Handle<Code> code) { |
239 instance->function_code[index] = code; | 251 instance->function_code[index] = code; |
240 } | 252 } |
241 | 253 |
242 void AddIndirectFunctionTable(uint16_t* function_indexes, | 254 void AddIndirectFunctionTable(uint16_t* function_indexes, |
243 uint32_t table_size) { | 255 uint32_t table_size) { |
244 module_.function_tables.push_back({table_size, table_size, true, | 256 module_.function_tables.push_back({table_size, table_size, true, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 Handle<WasmSharedModuleData> shared_module_data = | 339 Handle<WasmSharedModuleData> shared_module_data = |
328 WasmSharedModuleData::New(isolate_, module_wrapper, empty_string, | 340 WasmSharedModuleData::New(isolate_, module_wrapper, empty_string, |
329 script, Handle<ByteArray>::null()); | 341 script, Handle<ByteArray>::null()); |
330 Handle<WasmCompiledModule> compiled_module = | 342 Handle<WasmCompiledModule> compiled_module = |
331 WasmCompiledModule::New(isolate_, shared_module_data); | 343 WasmCompiledModule::New(isolate_, shared_module_data); |
332 // Minimally initialize the compiled module such that IsWasmCompiledModule | 344 // Minimally initialize the compiled module such that IsWasmCompiledModule |
333 // passes. | 345 // passes. |
334 // If tests need more (correct) information, add it later. | 346 // If tests need more (correct) information, add it later. |
335 compiled_module->set_min_mem_pages(0); | 347 compiled_module->set_min_mem_pages(0); |
336 compiled_module->set_max_mem_pages(Smi::kMaxValue); | 348 compiled_module->set_max_mem_pages(Smi::kMaxValue); |
| 349 Handle<FixedArray> code_table = isolate_->factory()->NewFixedArray(0); |
| 350 compiled_module->set_code_table(code_table); |
| 351 Handle<FixedArray> weak_exported = isolate_->factory()->NewFixedArray(0); |
| 352 compiled_module->set_weak_exported_functions(weak_exported); |
337 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); | 353 DCHECK(WasmCompiledModule::IsWasmCompiledModule(*compiled_module)); |
338 return WasmInstanceObject::New(isolate_, compiled_module); | 354 return WasmInstanceObject::New(isolate_, compiled_module); |
339 } | 355 } |
340 }; | 356 }; |
341 | 357 |
342 inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, | 358 inline void TestBuildingGraph(Zone* zone, JSGraph* jsgraph, ModuleEnv* module, |
343 FunctionSig* sig, | 359 FunctionSig* sig, |
344 SourcePositionTable* source_position_table, | 360 SourcePositionTable* source_position_table, |
345 const byte* start, const byte* end) { | 361 const byte* start, const byte* end) { |
346 compiler::WasmGraphBuilder builder(module, zone, jsgraph, sig, | 362 compiler::WasmGraphBuilder builder(module, zone, jsgraph, sig, |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 // Add the code to the interpreter. | 550 // Add the code to the interpreter. |
535 CHECK(interpreter_->SetFunctionCodeForTesting(function_, start, end)); | 551 CHECK(interpreter_->SetFunctionCodeForTesting(function_, start, end)); |
536 return; | 552 return; |
537 } | 553 } |
538 | 554 |
539 // Build the TurboFan graph. | 555 // Build the TurboFan graph. |
540 TestBuildingGraph(zone(), &jsgraph, testing_module_, sig, | 556 TestBuildingGraph(zone(), &jsgraph, testing_module_, sig, |
541 &source_position_table_, start, end); | 557 &source_position_table_, start, end); |
542 Handle<Code> code = Compile(); | 558 Handle<Code> code = Compile(); |
543 testing_module_->SetFunctionCode(function_index(), code); | 559 testing_module_->SetFunctionCode(function_index(), code); |
| 560 |
| 561 // Add to code table. |
| 562 Handle<WasmCompiledModule> compiled_module( |
| 563 testing_module_->instance_object()->compiled_module(), isolate()); |
| 564 Handle<FixedArray> code_table = compiled_module->code_table(); |
| 565 code_table = FixedArray::SetAndGrow(code_table, function_index(), code); |
| 566 compiled_module->set_code_table(code_table); |
544 } | 567 } |
545 | 568 |
546 byte AllocateLocal(ValueType type) { | 569 byte AllocateLocal(ValueType type) { |
547 uint32_t index = local_decls.AddLocals(1, type); | 570 uint32_t index = local_decls.AddLocals(1, type); |
548 byte result = static_cast<byte>(index); | 571 byte result = static_cast<byte>(index); |
549 DCHECK_EQ(index, result); | 572 DCHECK_EQ(index, result); |
550 return result; | 573 return result; |
551 } | 574 } |
552 | 575 |
553 void SetSigIndex(int sig_index) { function_->sig_index = sig_index; } | 576 void SetSigIndex(int sig_index) { function_->sig_index = sig_index; } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 const char* name = nullptr) { | 675 const char* name = nullptr) { |
653 functions_.emplace_back( | 676 functions_.emplace_back( |
654 new WasmFunctionCompiler(&zone_, sig, &module_, name)); | 677 new WasmFunctionCompiler(&zone_, sig, &module_, name)); |
655 return *functions_.back(); | 678 return *functions_.back(); |
656 } | 679 } |
657 | 680 |
658 byte AllocateLocal(ValueType type) { | 681 byte AllocateLocal(ValueType type) { |
659 return functions_[0]->AllocateLocal(type); | 682 return functions_[0]->AllocateLocal(type); |
660 } | 683 } |
661 | 684 |
| 685 uint32_t function_index() { return functions_[0]->function_index(); } |
662 WasmFunction* function() { return functions_[0]->function_; } | 686 WasmFunction* function() { return functions_[0]->function_; } |
663 WasmInterpreter* interpreter() { return functions_[0]->interpreter_; } | 687 WasmInterpreter* interpreter() { return functions_[0]->interpreter_; } |
664 bool possible_nondeterminism() { return possible_nondeterminism_; } | 688 bool possible_nondeterminism() { return possible_nondeterminism_; } |
665 TestingModule& module() { return module_; } | 689 TestingModule& module() { return module_; } |
666 Zone* zone() { return &zone_; } | 690 Zone* zone() { return &zone_; } |
667 | 691 |
668 // Set the context, such that e.g. runtime functions can be called. | 692 // Set the context, such that e.g. runtime functions can be called. |
669 void SetModuleContext() { | 693 void SetModuleContext() { |
670 if (!module_.instance->context.is_null()) { | 694 if (!module_.instance->context.is_null()) { |
671 CHECK(module_.instance->context.is_identical_to( | 695 CHECK(module_.instance->context.is_identical_to( |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
813 void RunWasm_##name(WasmExecutionMode execution_mode) | 837 void RunWasm_##name(WasmExecutionMode execution_mode) |
814 | 838 |
815 #define WASM_EXEC_COMPILED_TEST(name) \ | 839 #define WASM_EXEC_COMPILED_TEST(name) \ |
816 void RunWasm_##name(WasmExecutionMode execution_mode); \ | 840 void RunWasm_##name(WasmExecutionMode execution_mode); \ |
817 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ | 841 TEST(RunWasmCompiled_##name) { RunWasm_##name(kExecuteCompiled); } \ |
818 void RunWasm_##name(WasmExecutionMode execution_mode) | 842 void RunWasm_##name(WasmExecutionMode execution_mode) |
819 | 843 |
820 } // namespace | 844 } // namespace |
821 | 845 |
822 #endif | 846 #endif |
OLD | NEW |