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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "src/assembler-inl.h" | 7 #include "src/assembler-inl.h" |
8 #include "src/base/adapters.h" | 8 #include "src/base/adapters.h" |
9 #include "src/base/atomic-utils.h" | 9 #include "src/base/atomic-utils.h" |
10 #include "src/code-stubs.h" | 10 #include "src/code-stubs.h" |
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 | 601 |
602 Handle<WasmSharedModuleData> shared = WasmSharedModuleData::New( | 602 Handle<WasmSharedModuleData> shared = WasmSharedModuleData::New( |
603 isolate_, module_wrapper, Handle<SeqOneByteString>::cast(module_bytes), | 603 isolate_, module_wrapper, Handle<SeqOneByteString>::cast(module_bytes), |
604 script, asm_js_offset_table); | 604 script, asm_js_offset_table); |
605 if (lazy_compile) WasmSharedModuleData::PrepareForLazyCompilation(shared); | 605 if (lazy_compile) WasmSharedModuleData::PrepareForLazyCompilation(shared); |
606 | 606 |
607 // Create the compiled module object, and populate with compiled functions | 607 // Create the compiled module object, and populate with compiled functions |
608 // and information needed at instantiation time. This object needs to be | 608 // and information needed at instantiation time. This object needs to be |
609 // serializable. Instantiation may occur off a deserialized version of this | 609 // serializable. Instantiation may occur off a deserialized version of this |
610 // object. | 610 // object. |
611 Handle<WasmCompiledModule> compiled_module = | 611 Handle<WasmCompiledModule> compiled_module = WasmCompiledModule::New( |
612 WasmCompiledModule::New(isolate_, shared); | 612 isolate_, shared, code_table, function_tables, signature_tables); |
613 compiled_module->set_num_imported_functions( | |
614 module_->num_imported_functions); | |
615 compiled_module->set_code_table(code_table); | |
616 compiled_module->set_min_mem_pages(module_->min_mem_pages); | |
617 compiled_module->set_max_mem_pages(module_->max_mem_pages); | |
618 if (function_table_count > 0) { | 613 if (function_table_count > 0) { |
619 compiled_module->set_function_tables(function_tables); | 614 compiled_module->set_function_tables(function_tables); |
620 compiled_module->set_signature_tables(signature_tables); | |
621 compiled_module->set_empty_function_tables(function_tables); | |
622 } | 615 } |
623 | 616 |
624 // If we created a wasm script, finish it now and make it public to the | 617 // If we created a wasm script, finish it now and make it public to the |
625 // debugger. | 618 // debugger. |
626 if (asm_js_script.is_null()) { | 619 if (asm_js_script.is_null()) { |
627 script->set_wasm_compiled_module(*compiled_module); | 620 script->set_wasm_compiled_module(*compiled_module); |
628 isolate_->debug()->OnAfterCompile(script); | 621 isolate_->debug()->OnAfterCompile(script); |
629 } | 622 } |
630 | 623 |
631 // Compile JS->WASM wrappers for exported functions. | 624 // Compile JS->WASM wrappers for exported functions. |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1091 HistogramTimerScope wasm_instantiate_module_time_scope( | 1084 HistogramTimerScope wasm_instantiate_module_time_scope( |
1092 module_->is_wasm() | 1085 module_->is_wasm() |
1093 ? isolate_->counters()->wasm_instantiate_wasm_module_time() | 1086 ? isolate_->counters()->wasm_instantiate_wasm_module_time() |
1094 : isolate_->counters()->wasm_instantiate_asm_module_time()); | 1087 : isolate_->counters()->wasm_instantiate_asm_module_time()); |
1095 Factory* factory = isolate_->factory(); | 1088 Factory* factory = isolate_->factory(); |
1096 | 1089 |
1097 //-------------------------------------------------------------------------- | 1090 //-------------------------------------------------------------------------- |
1098 // Reuse the compiled module (if no owner), otherwise clone. | 1091 // Reuse the compiled module (if no owner), otherwise clone. |
1099 //-------------------------------------------------------------------------- | 1092 //-------------------------------------------------------------------------- |
1100 Handle<FixedArray> code_table; | 1093 Handle<FixedArray> code_table; |
| 1094 // We keep around a copy of the old code table, because we'll be replacing |
| 1095 // imports for the new instance, and then we need the old imports to be |
| 1096 // able to relocate. |
1101 Handle<FixedArray> old_code_table; | 1097 Handle<FixedArray> old_code_table; |
1102 MaybeHandle<WasmInstanceObject> owner; | 1098 MaybeHandle<WasmInstanceObject> owner; |
1103 | 1099 |
1104 TRACE("Starting new module instantiation\n"); | 1100 TRACE("Starting new module instantiation\n"); |
1105 { | 1101 { |
1106 // Root the owner, if any, before doing any allocations, which | 1102 // Root the owner, if any, before doing any allocations, which |
1107 // may trigger GC. | 1103 // may trigger GC. |
1108 // Both owner and original template need to be in sync. Even | 1104 // Both owner and original template need to be in sync. Even |
1109 // after we lose the original template handle, the code | 1105 // after we lose the original template handle, the code |
1110 // objects we copied from it have data relative to the | 1106 // objects we copied from it have data relative to the |
1111 // instance - such as globals addresses. | 1107 // instance - such as globals addresses. |
1112 Handle<WasmCompiledModule> original; | 1108 Handle<WasmCompiledModule> original; |
1113 { | 1109 { |
1114 DisallowHeapAllocation no_gc; | 1110 DisallowHeapAllocation no_gc; |
1115 original = handle(module_object_->compiled_module()); | 1111 original = handle(module_object_->compiled_module()); |
1116 if (original->has_weak_owning_instance()) { | 1112 if (original->has_weak_owning_instance()) { |
1117 owner = handle(WasmInstanceObject::cast( | 1113 owner = handle(WasmInstanceObject::cast( |
1118 original->weak_owning_instance()->value())); | 1114 original->weak_owning_instance()->value())); |
1119 } | 1115 } |
1120 } | 1116 } |
1121 DCHECK(!original.is_null()); | 1117 DCHECK(!original.is_null()); |
1122 // Always make a new copy of the code_table, since the old_code_table | |
1123 // may still have placeholders for imports. | |
1124 old_code_table = original->code_table(); | |
1125 code_table = factory->CopyFixedArray(old_code_table); | |
1126 | |
1127 if (original->has_weak_owning_instance()) { | 1118 if (original->has_weak_owning_instance()) { |
1128 // Clone, but don't insert yet the clone in the instances chain. | 1119 // Clone, but don't insert yet the clone in the instances chain. |
1129 // We do that last. Since we are holding on to the owner instance, | 1120 // We do that last. Since we are holding on to the owner instance, |
1130 // the owner + original state used for cloning and patching | 1121 // the owner + original state used for cloning and patching |
1131 // won't be mutated by possible finalizer runs. | 1122 // won't be mutated by possible finalizer runs. |
1132 DCHECK(!owner.is_null()); | 1123 DCHECK(!owner.is_null()); |
1133 TRACE("Cloning from %d\n", original->instance_id()); | 1124 TRACE("Cloning from %d\n", original->instance_id()); |
| 1125 old_code_table = original->code_table(); |
1134 compiled_module_ = WasmCompiledModule::Clone(isolate_, original); | 1126 compiled_module_ = WasmCompiledModule::Clone(isolate_, original); |
| 1127 code_table = compiled_module_->code_table(); |
1135 // Avoid creating too many handles in the outer scope. | 1128 // Avoid creating too many handles in the outer scope. |
1136 HandleScope scope(isolate_); | 1129 HandleScope scope(isolate_); |
1137 | 1130 |
1138 // Clone the code for WASM functions and exports. | 1131 // Clone the code for WASM functions and exports. |
1139 for (int i = 0; i < code_table->length(); ++i) { | 1132 for (int i = 0; i < code_table->length(); ++i) { |
1140 Handle<Code> orig_code(Code::cast(code_table->get(i)), isolate_); | 1133 Handle<Code> orig_code(Code::cast(code_table->get(i)), isolate_); |
1141 switch (orig_code->kind()) { | 1134 switch (orig_code->kind()) { |
1142 case Code::WASM_TO_JS_FUNCTION: | 1135 case Code::WASM_TO_JS_FUNCTION: |
1143 // Imports will be overwritten with newly compiled wrappers. | 1136 // Imports will be overwritten with newly compiled wrappers. |
1144 break; | 1137 break; |
(...skipping 17 matching lines...) Expand all Loading... |
1162 break; | 1155 break; |
1163 } | 1156 } |
1164 default: | 1157 default: |
1165 UNREACHABLE(); | 1158 UNREACHABLE(); |
1166 } | 1159 } |
1167 } | 1160 } |
1168 RecordStats(isolate_, code_table); | 1161 RecordStats(isolate_, code_table); |
1169 } else { | 1162 } else { |
1170 // There was no owner, so we can reuse the original. | 1163 // There was no owner, so we can reuse the original. |
1171 compiled_module_ = original; | 1164 compiled_module_ = original; |
| 1165 old_code_table = |
| 1166 factory->CopyFixedArray(compiled_module_->code_table()); |
| 1167 code_table = compiled_module_->code_table(); |
1172 TRACE("Reusing existing instance %d\n", | 1168 TRACE("Reusing existing instance %d\n", |
1173 compiled_module_->instance_id()); | 1169 compiled_module_->instance_id()); |
1174 } | 1170 } |
1175 compiled_module_->set_code_table(code_table); | |
1176 compiled_module_->set_native_context(isolate_->native_context()); | 1171 compiled_module_->set_native_context(isolate_->native_context()); |
1177 } | 1172 } |
1178 | 1173 |
1179 //-------------------------------------------------------------------------- | 1174 //-------------------------------------------------------------------------- |
1180 // Allocate the instance object. | 1175 // Allocate the instance object. |
1181 //-------------------------------------------------------------------------- | 1176 //-------------------------------------------------------------------------- |
1182 Zone instantiation_zone(isolate_->allocator(), ZONE_NAME); | 1177 Zone instantiation_zone(isolate_->allocator(), ZONE_NAME); |
1183 CodeSpecialization code_specialization(isolate_, &instantiation_zone); | 1178 CodeSpecialization code_specialization(isolate_, &instantiation_zone); |
1184 Handle<WasmInstanceObject> instance = | 1179 Handle<WasmInstanceObject> instance = |
1185 WasmInstanceObject::New(isolate_, compiled_module_); | 1180 WasmInstanceObject::New(isolate_, compiled_module_); |
(...skipping 1757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2943 callee_compiled->instruction_start()); | 2938 callee_compiled->instruction_start()); |
2944 } | 2939 } |
2945 DCHECK_EQ(non_compiled_functions.size(), idx); | 2940 DCHECK_EQ(non_compiled_functions.size(), idx); |
2946 } | 2941 } |
2947 | 2942 |
2948 Code* ret = | 2943 Code* ret = |
2949 Code::cast(compiled_module->code_table()->get(func_to_return_idx)); | 2944 Code::cast(compiled_module->code_table()->get(func_to_return_idx)); |
2950 DCHECK_EQ(Code::WASM_FUNCTION, ret->kind()); | 2945 DCHECK_EQ(Code::WASM_FUNCTION, ret->kind()); |
2951 return handle(ret, isolate); | 2946 return handle(ret, isolate); |
2952 } | 2947 } |
OLD | NEW |