OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 | 5 |
6 #include "v8.h" | 6 #include "v8.h" |
7 | 7 |
8 #include "liveedit.h" | 8 #include "liveedit.h" |
9 | 9 |
10 #include "code-stubs.h" | 10 #include "code-stubs.h" |
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 | 932 |
933 // Finds all references to original and replaces them with substitution. | 933 // Finds all references to original and replaces them with substitution. |
934 static void ReplaceCodeObject(Handle<Code> original, | 934 static void ReplaceCodeObject(Handle<Code> original, |
935 Handle<Code> substitution) { | 935 Handle<Code> substitution) { |
936 // Perform a full GC in order to ensure that we are not in the middle of an | 936 // Perform a full GC in order to ensure that we are not in the middle of an |
937 // incremental marking phase when we are replacing the code object. | 937 // incremental marking phase when we are replacing the code object. |
938 // Since we are not in an incremental marking phase we can write pointers | 938 // Since we are not in an incremental marking phase we can write pointers |
939 // to code objects (that are never in new space) without worrying about | 939 // to code objects (that are never in new space) without worrying about |
940 // write barriers. | 940 // write barriers. |
941 Heap* heap = original->GetHeap(); | 941 Heap* heap = original->GetHeap(); |
942 HeapIterator iterator(heap); | 942 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 943 "liveedit.cc ReplaceCodeObject"); |
943 | 944 |
944 ASSERT(!heap->InNewSpace(*substitution)); | 945 ASSERT(!heap->InNewSpace(*substitution)); |
945 | 946 |
| 947 DisallowHeapAllocation no_allocation; |
| 948 |
946 ReplacingVisitor visitor(*original, *substitution); | 949 ReplacingVisitor visitor(*original, *substitution); |
947 | 950 |
948 // Iterate over all roots. Stack frames may have pointer into original code, | 951 // Iterate over all roots. Stack frames may have pointer into original code, |
949 // so temporary replace the pointers with offset numbers | 952 // so temporary replace the pointers with offset numbers |
950 // in prologue/epilogue. | 953 // in prologue/epilogue. |
951 heap->IterateRoots(&visitor, VISIT_ALL); | 954 heap->IterateRoots(&visitor, VISIT_ALL); |
952 | 955 |
953 // Now iterate over all pointers of all objects, including code_target | 956 // Now iterate over all pointers of all objects, including code_target |
954 // implicit pointers. | 957 // implicit pointers. |
| 958 HeapIterator iterator(heap); |
955 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { | 959 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
956 obj->Iterate(&visitor); | 960 obj->Iterate(&visitor); |
957 } | 961 } |
958 } | 962 } |
959 | 963 |
960 | 964 |
961 // Patch function literals. | 965 // Patch function literals. |
962 // Name 'literals' is a misnomer. Rather it's a cache for complex object | 966 // Name 'literals' is a misnomer. Rather it's a cache for complex object |
963 // boilerplates and for a native context. We must clean cached values. | 967 // boilerplates and for a native context. We must clean cached values. |
964 // Additionally we may need to allocate a new array if number of literals | 968 // Additionally we may need to allocate a new array if number of literals |
965 // changed. | 969 // changed. |
966 class LiteralFixer { | 970 class LiteralFixer { |
967 public: | 971 public: |
968 static void PatchLiterals(FunctionInfoWrapper* compile_info_wrapper, | 972 static void PatchLiterals(FunctionInfoWrapper* compile_info_wrapper, |
969 Handle<SharedFunctionInfo> shared_info, | 973 Handle<SharedFunctionInfo> shared_info, |
970 Isolate* isolate) { | 974 Isolate* isolate) { |
971 int new_literal_count = compile_info_wrapper->GetLiteralCount(); | 975 int new_literal_count = compile_info_wrapper->GetLiteralCount(); |
972 if (new_literal_count > 0) { | 976 if (new_literal_count > 0) { |
973 new_literal_count += JSFunction::kLiteralsPrefixSize; | 977 new_literal_count += JSFunction::kLiteralsPrefixSize; |
974 } | 978 } |
975 int old_literal_count = shared_info->num_literals(); | 979 int old_literal_count = shared_info->num_literals(); |
976 | 980 |
977 if (old_literal_count == new_literal_count) { | 981 if (old_literal_count == new_literal_count) { |
978 // If literal count didn't change, simply go over all functions | 982 // If literal count didn't change, simply go over all functions |
979 // and clear literal arrays. | 983 // and clear literal arrays. |
980 ClearValuesVisitor visitor; | 984 ClearValuesVisitor visitor; |
981 IterateJSFunctions(shared_info, &visitor); | 985 IterateJSFunctions(*shared_info, &visitor); |
982 } else { | 986 } else { |
983 // When literal count changes, we have to create new array instances. | 987 // When literal count changes, we have to create new array instances. |
984 // Since we cannot create instances when iterating heap, we should first | 988 // Since we cannot create instances when iterating heap, we should first |
985 // collect all functions and fix their literal arrays. | 989 // collect all functions and fix their literal arrays. |
986 Handle<FixedArray> function_instances = | 990 Handle<FixedArray> function_instances = |
987 CollectJSFunctions(shared_info, isolate); | 991 CollectJSFunctions(shared_info, isolate); |
988 for (int i = 0; i < function_instances->length(); i++) { | 992 for (int i = 0; i < function_instances->length(); i++) { |
989 Handle<JSFunction> fun(JSFunction::cast(function_instances->get(i))); | 993 Handle<JSFunction> fun(JSFunction::cast(function_instances->get(i))); |
990 Handle<FixedArray> old_literals(fun->literals()); | 994 Handle<FixedArray> old_literals(fun->literals()); |
991 Handle<FixedArray> new_literals = | 995 Handle<FixedArray> new_literals = |
(...skipping 14 matching lines...) Expand all Loading... |
1006 } | 1010 } |
1007 | 1011 |
1008 shared_info->set_num_literals(new_literal_count); | 1012 shared_info->set_num_literals(new_literal_count); |
1009 } | 1013 } |
1010 } | 1014 } |
1011 | 1015 |
1012 private: | 1016 private: |
1013 // Iterates all function instances in the HEAP that refers to the | 1017 // Iterates all function instances in the HEAP that refers to the |
1014 // provided shared_info. | 1018 // provided shared_info. |
1015 template<typename Visitor> | 1019 template<typename Visitor> |
1016 static void IterateJSFunctions(Handle<SharedFunctionInfo> shared_info, | 1020 static void IterateJSFunctions(SharedFunctionInfo* shared_info, |
1017 Visitor* visitor) { | 1021 Visitor* visitor) { |
| 1022 DisallowHeapAllocation no_allocation; |
| 1023 |
1018 HeapIterator iterator(shared_info->GetHeap()); | 1024 HeapIterator iterator(shared_info->GetHeap()); |
1019 for (HeapObject* obj = iterator.next(); obj != NULL; | 1025 for (HeapObject* obj = iterator.next(); obj != NULL; |
1020 obj = iterator.next()) { | 1026 obj = iterator.next()) { |
1021 if (obj->IsJSFunction()) { | 1027 if (obj->IsJSFunction()) { |
1022 JSFunction* function = JSFunction::cast(obj); | 1028 JSFunction* function = JSFunction::cast(obj); |
1023 if (function->shared() == *shared_info) { | 1029 if (function->shared() == shared_info) { |
1024 visitor->visit(function); | 1030 visitor->visit(function); |
1025 } | 1031 } |
1026 } | 1032 } |
1027 } | 1033 } |
1028 } | 1034 } |
1029 | 1035 |
1030 // Finds all instances of JSFunction that refers to the provided shared_info | 1036 // Finds all instances of JSFunction that refers to the provided shared_info |
1031 // and returns array with them. | 1037 // and returns array with them. |
1032 static Handle<FixedArray> CollectJSFunctions( | 1038 static Handle<FixedArray> CollectJSFunctions( |
1033 Handle<SharedFunctionInfo> shared_info, Isolate* isolate) { | 1039 Handle<SharedFunctionInfo> shared_info, Isolate* isolate) { |
1034 CountVisitor count_visitor; | 1040 CountVisitor count_visitor; |
1035 count_visitor.count = 0; | 1041 count_visitor.count = 0; |
1036 IterateJSFunctions(shared_info, &count_visitor); | 1042 IterateJSFunctions(*shared_info, &count_visitor); |
1037 int size = count_visitor.count; | 1043 int size = count_visitor.count; |
1038 | 1044 |
1039 Handle<FixedArray> result = isolate->factory()->NewFixedArray(size); | 1045 Handle<FixedArray> result = isolate->factory()->NewFixedArray(size); |
1040 if (size > 0) { | 1046 if (size > 0) { |
1041 CollectVisitor collect_visitor(result); | 1047 CollectVisitor collect_visitor(result); |
1042 IterateJSFunctions(shared_info, &collect_visitor); | 1048 IterateJSFunctions(*shared_info, &collect_visitor); |
1043 } | 1049 } |
1044 return result; | 1050 return result; |
1045 } | 1051 } |
1046 | 1052 |
1047 class ClearValuesVisitor { | 1053 class ClearValuesVisitor { |
1048 public: | 1054 public: |
1049 void visit(JSFunction* fun) { | 1055 void visit(JSFunction* fun) { |
1050 FixedArray* literals = fun->literals(); | 1056 FixedArray* literals = fun->literals(); |
1051 int len = literals->length(); | 1057 int len = literals->length(); |
1052 for (int j = JSFunction::kLiteralsPrefixSize; j < len; j++) { | 1058 for (int j = JSFunction::kLiteralsPrefixSize; j < len; j++) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1152 void LiveEdit::ReplaceFunctionCode( | 1158 void LiveEdit::ReplaceFunctionCode( |
1153 Handle<JSArray> new_compile_info_array, | 1159 Handle<JSArray> new_compile_info_array, |
1154 Handle<JSArray> shared_info_array) { | 1160 Handle<JSArray> shared_info_array) { |
1155 Isolate* isolate = new_compile_info_array->GetIsolate(); | 1161 Isolate* isolate = new_compile_info_array->GetIsolate(); |
1156 | 1162 |
1157 FunctionInfoWrapper compile_info_wrapper(new_compile_info_array); | 1163 FunctionInfoWrapper compile_info_wrapper(new_compile_info_array); |
1158 SharedInfoWrapper shared_info_wrapper(shared_info_array); | 1164 SharedInfoWrapper shared_info_wrapper(shared_info_array); |
1159 | 1165 |
1160 Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); | 1166 Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); |
1161 | 1167 |
1162 isolate->heap()->MakeHeapIterable(); | 1168 isolate->heap()->EnsureHeapIsIterable(); |
1163 | 1169 |
1164 if (IsJSFunctionCode(shared_info->code())) { | 1170 if (IsJSFunctionCode(shared_info->code())) { |
1165 Handle<Code> code = compile_info_wrapper.GetFunctionCode(); | 1171 Handle<Code> code = compile_info_wrapper.GetFunctionCode(); |
1166 ReplaceCodeObject(Handle<Code>(shared_info->code()), code); | 1172 ReplaceCodeObject(Handle<Code>(shared_info->code()), code); |
1167 Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo(); | 1173 Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo(); |
1168 if (code_scope_info->IsFixedArray()) { | 1174 if (code_scope_info->IsFixedArray()) { |
1169 shared_info->set_scope_info(ScopeInfo::cast(*code_scope_info)); | 1175 shared_info->set_scope_info(ScopeInfo::cast(*code_scope_info)); |
1170 } | 1176 } |
1171 shared_info->DisableOptimization(kLiveEdit); | 1177 shared_info->DisableOptimization(kLiveEdit); |
1172 // Update the type feedback vector | 1178 // Update the type feedback vector |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1389 position_change_array); | 1395 position_change_array); |
1390 int new_function_end = TranslatePosition(info->end_position(), | 1396 int new_function_end = TranslatePosition(info->end_position(), |
1391 position_change_array); | 1397 position_change_array); |
1392 int new_function_token_pos = | 1398 int new_function_token_pos = |
1393 TranslatePosition(info->function_token_position(), position_change_array); | 1399 TranslatePosition(info->function_token_position(), position_change_array); |
1394 | 1400 |
1395 info->set_start_position(new_function_start); | 1401 info->set_start_position(new_function_start); |
1396 info->set_end_position(new_function_end); | 1402 info->set_end_position(new_function_end); |
1397 info->set_function_token_position(new_function_token_pos); | 1403 info->set_function_token_position(new_function_token_pos); |
1398 | 1404 |
1399 info->GetIsolate()->heap()->MakeHeapIterable(); | 1405 info->GetIsolate()->heap()->EnsureHeapIsIterable(); |
1400 | 1406 |
1401 if (IsJSFunctionCode(info->code())) { | 1407 if (IsJSFunctionCode(info->code())) { |
1402 // Patch relocation info section of the code. | 1408 // Patch relocation info section of the code. |
1403 Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()), | 1409 Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()), |
1404 position_change_array); | 1410 position_change_array); |
1405 if (*patched_code != info->code()) { | 1411 if (*patched_code != info->code()) { |
1406 // Replace all references to the code across the heap. In particular, | 1412 // Replace all references to the code across the heap. In particular, |
1407 // some stubs may refer to this code and this code may be being executed | 1413 // some stubs may refer to this code and this code may be being executed |
1408 // on stack (it is safe to substitute the code object on stack, because | 1414 // on stack (it is safe to substitute the code object on stack, because |
1409 // we only change the structure of rinfo and leave instructions | 1415 // we only change the structure of rinfo and leave instructions |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1964 void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { | 1970 void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { |
1965 isolate_->active_function_info_listener()->FunctionCode(code); | 1971 isolate_->active_function_info_listener()->FunctionCode(code); |
1966 } | 1972 } |
1967 | 1973 |
1968 | 1974 |
1969 bool LiveEditFunctionTracker::IsActive(Isolate* isolate) { | 1975 bool LiveEditFunctionTracker::IsActive(Isolate* isolate) { |
1970 return isolate->active_function_info_listener() != NULL; | 1976 return isolate->active_function_info_listener() != NULL; |
1971 } | 1977 } |
1972 | 1978 |
1973 } } // namespace v8::internal | 1979 } } // namespace v8::internal |
OLD | NEW |