Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: runtime/vm/kernel_reader.cc

Issue 2853423002: Move the Kernel canonical name table into the VM's heap (Closed)
Patch Set: Merge a bugfix Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/kernel_reader.h" 5 #include "vm/kernel_reader.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/kernel_binary.h" 10 #include "vm/kernel_binary.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 const intptr_t len = functions_.length(); 88 const intptr_t len = functions_.length();
89 const Array& res = Array::Handle(zone_, Array::New(len, Heap::kOld)); 89 const Array& res = Array::Handle(zone_, Array::New(len, Heap::kOld));
90 for (intptr_t i = 0; i < len; i++) { 90 for (intptr_t i = 0; i < len; i++) {
91 res.SetAt(i, *functions_[i]); 91 res.SetAt(i, *functions_[i]);
92 } 92 }
93 return res.raw(); 93 return res.raw();
94 } 94 }
95 95
96 96
97 RawLibrary* BuildingTranslationHelper::LookupLibraryByKernelLibrary( 97 RawLibrary* BuildingTranslationHelper::LookupLibraryByKernelLibrary(
98 CanonicalName* library) { 98 intptr_t library) {
99 return reader_->LookupLibrary(library).raw(); 99 return reader_->LookupLibrary(library).raw();
100 } 100 }
101 101
102 102
103 RawClass* BuildingTranslationHelper::LookupClassByKernelClass( 103 RawClass* BuildingTranslationHelper::LookupClassByKernelClass(intptr_t klass) {
Vyacheslav Egorov (Google) 2017/05/03 06:28:48 I think we *do* need need named type for these ind
104 CanonicalName* klass) {
105 return reader_->LookupClass(klass).raw(); 104 return reader_->LookupClass(klass).raw();
106 } 105 }
107 106
108 107
109 KernelReader::KernelReader(Program* program) 108 KernelReader::KernelReader(Program* program)
110 : program_(program), 109 : program_(program),
111 thread_(dart::Thread::Current()), 110 thread_(dart::Thread::Current()),
112 zone_(thread_->zone()), 111 zone_(thread_->zone()),
113 isolate_(thread_->isolate()), 112 isolate_(thread_->isolate()),
114 scripts_(Array::ZoneHandle(zone_)), 113 scripts_(Array::ZoneHandle(zone_)),
(...skipping 22 matching lines...) Expand all
137 offsets.SetUint32(i << 2, end_offset); 136 offsets.SetUint32(i << 2, end_offset);
138 } 137 }
139 138
140 // Copy the string data out of the binary and into the VM's heap. 139 // Copy the string data out of the binary and into the VM's heap.
141 TypedData& data = TypedData::Handle( 140 TypedData& data = TypedData::Handle(
142 Z, TypedData::New(kTypedDataUint8ArrayCid, end_offset, Heap::kOld)); 141 Z, TypedData::New(kTypedDataUint8ArrayCid, end_offset, Heap::kOld));
143 { 142 {
144 NoSafepointScope no_safepoint; 143 NoSafepointScope no_safepoint;
145 memmove(data.DataAddr(0), reader.buffer() + reader.offset(), end_offset); 144 memmove(data.DataAddr(0), reader.buffer() + reader.offset(), end_offset);
146 } 145 }
146
147 // Copy the canonical names into the VM's heap. Encode them as unsigned, so
148 // the parent indexes are adjusted when extracted.
149 reader.set_offset(program->name_table_offset());
150 count = reader.ReadUInt() * 2;
151 TypedData& names = TypedData::Handle(
152 Z, TypedData::New(kTypedDataUint32ArrayCid, count, Heap::kOld));
153 for (intptr_t i = 0; i < count; ++i) {
154 names.SetUint32(i << 2, reader.ReadUInt());
155 }
156
147 H.SetStringOffsets(offsets); 157 H.SetStringOffsets(offsets);
148 H.SetStringData(data); 158 H.SetStringData(data);
159 H.SetCanonicalNames(names);
149 } 160 }
150 161
151 162
152 Object& KernelReader::ReadProgram() { 163 Object& KernelReader::ReadProgram() {
153 LongJumpScope jump; 164 LongJumpScope jump;
154 if (setjmp(*jump.Set()) == 0) { 165 if (setjmp(*jump.Set()) == 0) {
155 intptr_t length = program_->libraries().length(); 166 intptr_t length = program_->libraries().length();
156 for (intptr_t i = 0; i < length; i++) { 167 for (intptr_t i = 0; i < length; i++) {
157 Library* kernel_library = program_->libraries()[i]; 168 Library* kernel_library = program_->libraries()[i];
158 ReadLibrary(kernel_library); 169 ReadLibrary(kernel_library);
159 } 170 }
160 171
161 for (intptr_t i = 0; i < length; i++) { 172 for (intptr_t i = 0; i < length; i++) {
162 dart::Library& library = 173 dart::Library& library =
163 LookupLibrary(program_->libraries()[i]->canonical_name()); 174 LookupLibrary(program_->libraries()[i]->canonical_name());
164 if (!library.Loaded()) library.SetLoaded(); 175 if (!library.Loaded()) library.SetLoaded();
165 } 176 }
166 177
167 if (ClassFinalizer::ProcessPendingClasses(/*from_kernel=*/true)) { 178 if (ClassFinalizer::ProcessPendingClasses(/*from_kernel=*/true)) {
168 // There is a function _getMainClosure in dart:_builtin that returns the 179 // There is a function _getMainClosure in dart:_builtin that returns the
169 // main procedure. Since the platform libraries are compiled before the 180 // main procedure. Since the platform libraries are compiled before the
170 // program script, this function might need to be patched here. 181 // program script, this function might need to be patched here.
171 182
172 // If there is no main method then we have compiled a partial Kernel file 183 // If there is no main method then we have compiled a partial Kernel file
173 // and do not need to patch here. 184 // and do not need to patch here.
174 CanonicalName* main = program_->main_method(); 185 intptr_t main = program_->main_method();
175 if (main == NULL) { 186 if (main == -1) {
176 return dart::Library::Handle(Z); 187 return dart::Library::Handle(Z);
177 } 188 }
178 189
179 // If the builtin library is not set in the object store, then we are 190 // If the builtin library is not set in the object store, then we are
180 // bootstrapping and do not need to patch here. 191 // bootstrapping and do not need to patch here.
181 dart::Library& builtin_library = 192 dart::Library& builtin_library =
182 dart::Library::Handle(Z, I->object_store()->builtin_library()); 193 dart::Library::Handle(Z, I->object_store()->builtin_library());
183 if (builtin_library.IsNull()) { 194 if (builtin_library.IsNull()) {
184 return dart::Library::Handle(Z); 195 return dart::Library::Handle(Z);
185 } 196 }
186 197
187 CanonicalName* main_library = H.EnclosingName(main); 198 intptr_t main_library = H.EnclosingName(main);
188 dart::Library& library = LookupLibrary(main_library); 199 dart::Library& library = LookupLibrary(main_library);
189 // Sanity check that we can find the main entrypoint. 200 // Sanity check that we can find the main entrypoint.
190 Object& main_obj = Object::Handle( 201 Object& main_obj = Object::Handle(
191 Z, library.LookupObjectAllowPrivate(H.DartSymbol("main"))); 202 Z, library.LookupObjectAllowPrivate(H.DartSymbol("main")));
192 ASSERT(!main_obj.IsNull()); 203 ASSERT(!main_obj.IsNull());
193 204
194 Function& to_patch = Function::Handle( 205 Function& to_patch = Function::Handle(
195 Z, builtin_library.LookupFunctionAllowPrivate( 206 Z, builtin_library.LookupFunctionAllowPrivate(
196 dart::String::Handle(dart::String::New("_getMainClosure")))); 207 dart::String::Handle(dart::String::New("_getMainClosure"))));
197 208
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 dart::String* native_name = NULL; 498 dart::String* native_name = NULL;
488 if (is_external) { 499 if (is_external) {
489 // Maybe it has a native implementation, which is not external as far as 500 // Maybe it has a native implementation, which is not external as far as
490 // the VM is concerned because it does have an implementation. Check for 501 // the VM is concerned because it does have an implementation. Check for
491 // an ExternalName annotation and extract the string from it. 502 // an ExternalName annotation and extract the string from it.
492 for (int i = 0; i < kernel_procedure->annotations().length(); ++i) { 503 for (int i = 0; i < kernel_procedure->annotations().length(); ++i) {
493 Expression* annotation = kernel_procedure->annotations()[i]; 504 Expression* annotation = kernel_procedure->annotations()[i];
494 if (!annotation->IsConstructorInvocation()) continue; 505 if (!annotation->IsConstructorInvocation()) continue;
495 ConstructorInvocation* invocation = 506 ConstructorInvocation* invocation =
496 ConstructorInvocation::Cast(annotation); 507 ConstructorInvocation::Cast(annotation);
497 CanonicalName* annotation_class = H.EnclosingName(invocation->target()); 508 intptr_t annotation_class = H.EnclosingName(invocation->target());
498 ASSERT(H.IsClass(annotation_class)); 509 ASSERT(H.IsClass(annotation_class));
499 intptr_t class_name_index = annotation_class->name(); 510 intptr_t class_name_index = H.CanonicalNameString(annotation_class);
500 // Just compare by name, do not generate the annotation class. 511 // Just compare by name, do not generate the annotation class.
501 if (!H.StringEquals(class_name_index, "ExternalName")) continue; 512 if (!H.StringEquals(class_name_index, "ExternalName")) continue;
502 ASSERT(H.IsLibrary(annotation_class->parent())); 513 ASSERT(H.IsLibrary(H.CanonicalNameParent(annotation_class)));
503 intptr_t library_name_index = annotation_class->parent()->name(); 514 intptr_t library_name_index =
515 H.CanonicalNameString(H.CanonicalNameParent(annotation_class));
504 if (!H.StringEquals(library_name_index, "dart:_internal")) continue; 516 if (!H.StringEquals(library_name_index, "dart:_internal")) continue;
505 517
506 is_external = false; 518 is_external = false;
507 ASSERT(invocation->arguments()->positional().length() == 1 && 519 ASSERT(invocation->arguments()->positional().length() == 1 &&
508 invocation->arguments()->named().length() == 0); 520 invocation->arguments()->named().length() == 0);
509 StringLiteral* literal = 521 StringLiteral* literal =
510 StringLiteral::Cast(invocation->arguments()->positional()[0]); 522 StringLiteral::Cast(invocation->arguments()->positional()[0]);
511 native_name = &H.DartSymbol(literal->value()); 523 native_name = &H.DartSymbol(literal->value());
512 break; 524 break;
513 } 525 }
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 dart::String& import_uri_string = 639 dart::String& import_uri_string =
628 import_uri == -1 ? uri_string : H.DartString(import_uri, Heap::kOld); 640 import_uri == -1 ? uri_string : H.DartString(import_uri, Heap::kOld);
629 uint8_t* source_buffer = program_->source_table().SourceCodeFor(index); 641 uint8_t* source_buffer = program_->source_table().SourceCodeFor(index);
630 intptr_t source_size = program_->source_table().SourceCodeSizeFor(index); 642 intptr_t source_size = program_->source_table().SourceCodeSizeFor(index);
631 dart::String& source_code = 643 dart::String& source_code =
632 H.DartString(source_buffer, source_size, Heap::kOld); 644 H.DartString(source_buffer, source_size, Heap::kOld);
633 script = Script::New(import_uri_string, uri_string, source_code, 645 script = Script::New(import_uri_string, uri_string, source_code,
634 RawScript::kKernelTag); 646 RawScript::kKernelTag);
635 script.set_kernel_string_offsets(H.string_offsets()); 647 script.set_kernel_string_offsets(H.string_offsets());
636 script.set_kernel_string_data(H.string_data()); 648 script.set_kernel_string_data(H.string_data());
649 script.set_kernel_canonical_names(H.canonical_names());
637 scripts_.SetAt(index, script); 650 scripts_.SetAt(index, script);
638 651
639 // Create line_starts array for the script. 652 // Create line_starts array for the script.
640 intptr_t* line_starts = program_->source_table().LineStartsFor(index); 653 intptr_t* line_starts = program_->source_table().LineStartsFor(index);
641 intptr_t line_count = program_->source_table().LineCountFor(index); 654 intptr_t line_count = program_->source_table().LineCountFor(index);
642 Array& array_object = Array::Handle(Z, Array::New(line_count, Heap::kOld)); 655 Array& array_object = Array::Handle(Z, Array::New(line_count, Heap::kOld));
643 Smi& value = Smi::Handle(Z); 656 Smi& value = Smi::Handle(Z);
644 for (intptr_t i = 0; i < line_count; ++i) { 657 for (intptr_t i = 0; i < line_count; ++i) {
645 value = Smi::New(line_starts[i]); 658 value = Smi::New(line_starts[i]);
646 array_object.SetAt(i, value); 659 array_object.SetAt(i, value);
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 pos++; 853 pos++;
841 } 854 }
842 if (is_setter) { 855 if (is_setter) {
843 function.SetParameterTypeAt(pos, AbstractType::dynamic_type()); 856 function.SetParameterTypeAt(pos, AbstractType::dynamic_type());
844 function.SetParameterNameAt(pos, Symbols::Value()); 857 function.SetParameterNameAt(pos, Symbols::Value());
845 pos++; 858 pos++;
846 } 859 }
847 } 860 }
848 861
849 862
850 dart::Library& KernelReader::LookupLibrary(CanonicalName* library) { 863 dart::Library& KernelReader::LookupLibrary(intptr_t library) {
851 dart::Library* handle = NULL; 864 dart::Library* handle = NULL;
852 if (!libraries_.Lookup(library, &handle)) { 865 if (!libraries_.Lookup(library, &handle)) {
853 const dart::String& url = H.DartSymbol(library->name()); 866 const dart::String& url = H.DartSymbol(H.CanonicalNameString(library));
854 handle = 867 handle =
855 &dart::Library::Handle(Z, dart::Library::LookupLibrary(thread_, url)); 868 &dart::Library::Handle(Z, dart::Library::LookupLibrary(thread_, url));
856 if (handle->IsNull()) { 869 if (handle->IsNull()) {
857 *handle = dart::Library::New(url); 870 *handle = dart::Library::New(url);
858 handle->Register(thread_); 871 handle->Register(thread_);
859 } 872 }
860 ASSERT(!handle->IsNull()); 873 ASSERT(!handle->IsNull());
861 libraries_.Insert(library, handle); 874 libraries_.Insert(library, handle);
862 } 875 }
863 return *handle; 876 return *handle;
864 } 877 }
865 878
866 879
867 dart::Class& KernelReader::LookupClass(CanonicalName* klass) { 880 dart::Class& KernelReader::LookupClass(intptr_t klass) {
868 dart::Class* handle = NULL; 881 dart::Class* handle = NULL;
869 if (!classes_.Lookup(klass, &handle)) { 882 if (!classes_.Lookup(klass, &handle)) {
870 dart::Library& library = LookupLibrary(klass->parent()); 883 dart::Library& library = LookupLibrary(H.CanonicalNameParent(klass));
871 const dart::String& name = H.DartClassName(klass); 884 const dart::String& name = H.DartClassName(klass);
872 handle = &dart::Class::Handle(Z, library.LookupClass(name)); 885 handle = &dart::Class::Handle(Z, library.LookupClass(name));
873 if (handle->IsNull()) { 886 if (handle->IsNull()) {
874 *handle = dart::Class::New(library, name, Script::Handle(Z), 887 *handle = dart::Class::New(library, name, Script::Handle(Z),
875 TokenPosition::kNoSource); 888 TokenPosition::kNoSource);
876 library.AddClass(*handle); 889 library.AddClass(*handle);
877 } 890 }
878 // Insert the class in the cache before calling ReadPreliminaryClass so 891 // Insert the class in the cache before calling ReadPreliminaryClass so
879 // we do not risk allocating the class again by calling LookupClass 892 // we do not risk allocating the class again by calling LookupClass
880 // recursively from ReadPreliminaryClass for the same class. 893 // recursively from ReadPreliminaryClass for the same class.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 initializer_fun.set_is_debuggable(false); 940 initializer_fun.set_is_debuggable(false);
928 initializer_fun.set_is_reflectable(false); 941 initializer_fun.set_is_reflectable(false);
929 initializer_fun.set_is_inlinable(false); 942 initializer_fun.set_is_inlinable(false);
930 return new (zone) ParsedFunction(thread, initializer_fun); 943 return new (zone) ParsedFunction(thread, initializer_fun);
931 } 944 }
932 945
933 946
934 } // namespace kernel 947 } // namespace kernel
935 } // namespace dart 948 } // namespace dart
936 #endif // !defined(DART_PRECOMPILED_RUNTIME) 949 #endif // !defined(DART_PRECOMPILED_RUNTIME)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698