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

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

Issue 2485993002: VM: Support bootstrapping core libraries from Kernel binaries instead of source. (Closed)
Patch Set: rebase Created 4 years, 1 month 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/longjump.h" 10 #include "vm/longjump.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 const dart::Instance& SimpleValue() { return *simple_value_; } 74 const dart::Instance& SimpleValue() { return *simple_value_; }
75 dart::Zone* zone() const { return zone_; } 75 dart::Zone* zone() const { return zone_; }
76 76
77 private: 77 private:
78 TranslationHelper translation_helper_; 78 TranslationHelper translation_helper_;
79 dart::Zone* zone_; 79 dart::Zone* zone_;
80 bool is_simple_; 80 bool is_simple_;
81 dart::Instance* simple_value_; 81 dart::Instance* simple_value_;
82 }; 82 };
83 83
84 void BuildingTranslationHelper::SetFinalize(bool finalize) {
85 reader_->finalize_ = finalize;
86 }
87 84
88 RawLibrary* BuildingTranslationHelper::LookupLibraryByKernelLibrary( 85 RawLibrary* BuildingTranslationHelper::LookupLibraryByKernelLibrary(
89 Library* library) { 86 Library* library) {
90 return reader_->LookupLibrary(library).raw(); 87 return reader_->LookupLibrary(library).raw();
91 } 88 }
92 89
90
93 RawClass* BuildingTranslationHelper::LookupClassByKernelClass(Class* klass) { 91 RawClass* BuildingTranslationHelper::LookupClassByKernelClass(Class* klass) {
94 return reader_->LookupClass(klass).raw(); 92 return reader_->LookupClass(klass).raw();
95 } 93 }
96 94
95
97 Object& KernelReader::ReadProgram() { 96 Object& KernelReader::ReadProgram() {
98 ASSERT(!bootstrapping_); 97 ASSERT(!bootstrapping_);
99 Program* program = ReadPrecompiledKernelFromBuffer(buffer_, buffer_length_); 98 Program* program = ReadPrecompiledKernelFromBuffer(buffer_, buffer_length_);
100 if (program == NULL) { 99 if (program == NULL) {
101 const dart::String& error = H.DartString("Failed to read .kernell file"); 100 const dart::String& error = H.DartString("Failed to read .kernell file");
102 return Object::Handle(Z, ApiError::New(error)); 101 return Object::Handle(Z, ApiError::New(error));
103 } 102 }
104 103
105 LongJumpScope jump; 104 LongJumpScope jump;
106 if (setjmp(*jump.Set()) == 0) { 105 if (setjmp(*jump.Set()) == 0) {
107 Procedure* main = program->main_method(); 106 Procedure* main = program->main_method();
108 Library* kernel_main_library = Library::Cast(main->parent()); 107 Library* kernel_main_library = Library::Cast(main->parent());
109 108
110 intptr_t length = program->libraries().length(); 109 intptr_t length = program->libraries().length();
111 for (intptr_t i = 0; i < length; i++) { 110 for (intptr_t i = 0; i < length; i++) {
112 Library* kernel_library = program->libraries()[i]; 111 Library* kernel_library = program->libraries()[i];
113 ReadLibrary(kernel_library); 112 ReadLibrary(kernel_library);
114 } 113 }
115 114
116 // We finalize classes after we've constructed all classes since we
117 // currently don't construct them in pre-order of the class hierarchy (and
118 // finalization of a class needs all of its superclasses to be finalized).
119 dart::String& name = dart::String::Handle(Z);
120 for (intptr_t i = 0; i < length; i++) { 115 for (intptr_t i = 0; i < length; i++) {
121 Library* kernel_library = program->libraries()[i]; 116 dart::Library& library = LookupLibrary(program->libraries()[i]);
122 dart::Library& library = LookupLibrary(kernel_library); 117 if (!library.Loaded()) library.SetLoaded();
123 name = library.url(); 118 }
124 119
125 // TODO(27590) unskip this library when we fix underlying issue. 120 if (!ClassFinalizer::ProcessPendingClasses(/*from_kernel=*/true)) {
126 if (name.Equals("dart:vmservice_io")) { 121 FATAL("Error in class finalization during bootstrapping.");
127 continue;
128 }
129
130 if (!library.Loaded()) {
131 dart::Class& klass = dart::Class::Handle(Z);
132 for (intptr_t i = 0; i < kernel_library->classes().length(); i++) {
133 klass = LookupClass(kernel_library->classes()[i]).raw();
134 ClassFinalizer::FinalizeTypesInClass(klass);
135 ClassFinalizer::FinalizeClass(klass);
136 }
137 library.SetLoaded();
138 }
139 } 122 }
140 123
141 dart::Library& library = LookupLibrary(kernel_main_library); 124 dart::Library& library = LookupLibrary(kernel_main_library);
142 125
143 // Sanity check that we can find the main entrypoint. 126 // Sanity check that we can find the main entrypoint.
144 Object& main_obj = Object::Handle( 127 Object& main_obj = Object::Handle(
145 Z, library.LookupObjectAllowPrivate(H.DartSymbol("main"))); 128 Z, library.LookupObjectAllowPrivate(H.DartSymbol("main")));
146 ASSERT(!main_obj.IsNull()); 129 ASSERT(!main_obj.IsNull());
147 return library; 130 return library;
148 } else { 131 } else {
149 // Everything else is a compile-time error. We don't use the [error] since 132 // Everything else is a compile-time error. We don't use the [error] since
150 // it sometimes causes the higher-level error handling to try to read the 133 // it sometimes causes the higher-level error handling to try to read the
151 // script and token position (which we don't have) to produce a nice error 134 // script and token position (which we don't have) to produce a nice error
152 // message. 135 // message.
153 Error& error = Error::Handle(Z); 136 Error& error = Error::Handle(Z);
154 error = thread_->sticky_error(); 137 error = thread_->sticky_error();
155 thread_->clear_sticky_error(); 138 thread_->clear_sticky_error();
156 139
157 // Instead we simply make a non-informative error message. 140 // Instead we simply make a non-informative error message.
158 const dart::String& error_message = 141 const dart::String& error_message =
159 H.DartString("Failed to read .kernell file => CompileTimeError."); 142 H.DartString("Failed to read .kernell file => CompileTimeError.");
160 return Object::Handle(Z, LanguageError::New(error_message)); 143 return Object::Handle(Z, LanguageError::New(error_message));
161 } 144 }
162 } 145 }
163 146
147
164 void KernelReader::ReadLibrary(Library* kernel_library) { 148 void KernelReader::ReadLibrary(Library* kernel_library) {
165 dart::Library& library = LookupLibrary(kernel_library); 149 dart::Library& library = LookupLibrary(kernel_library);
166 if (library.Loaded()) return; 150 if (library.Loaded()) return;
167 151
168 // The bootstrapper will take care of creating the native wrapper classes, but 152 // The bootstrapper will take care of creating the native wrapper classes, but
169 // we will add the synthetic constructors to them here. 153 // we will add the synthetic constructors to them here.
170 if (library.name() == 154 if (library.name() ==
171 Symbols::Symbol(Symbols::kDartNativeWrappersLibNameId).raw()) { 155 Symbols::Symbol(Symbols::kDartNativeWrappersLibNameId).raw()) {
172 ASSERT(library.LoadInProgress()); 156 ASSERT(library.LoadInProgress());
173 } else { 157 } else {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 toplevel_class.AddField(field); 194 toplevel_class.AddField(field);
211 library.AddObject(field, name); 195 library.AddObject(field, name);
212 } 196 }
213 197
214 // Load toplevel procedures. 198 // Load toplevel procedures.
215 for (intptr_t i = 0; i < kernel_library->procedures().length(); i++) { 199 for (intptr_t i = 0; i < kernel_library->procedures().length(); i++) {
216 Procedure* kernel_procedure = kernel_library->procedures()[i]; 200 Procedure* kernel_procedure = kernel_library->procedures()[i];
217 ReadProcedure(library, toplevel_class, kernel_procedure); 201 ReadProcedure(library, toplevel_class, kernel_procedure);
218 } 202 }
219 203
204 const GrowableObjectArray& classes =
205 GrowableObjectArray::Handle(I->object_store()->pending_classes());
206
220 // Load all classes. 207 // Load all classes.
221 for (intptr_t i = 0; i < kernel_library->classes().length(); i++) { 208 for (intptr_t i = 0; i < kernel_library->classes().length(); i++) {
222 Class* kernel_klass = kernel_library->classes()[i]; 209 Class* kernel_klass = kernel_library->classes()[i];
223 ReadClass(library, kernel_klass); 210 classes.Add(ReadClass(library, kernel_klass), Heap::kOld);
224 } 211 }
225 } 212 }
226 213
214
227 void KernelReader::ReadPreliminaryClass(dart::Class* klass, 215 void KernelReader::ReadPreliminaryClass(dart::Class* klass,
228 Class* kernel_klass) { 216 Class* kernel_klass) {
229 ASSERT(kernel_klass->IsNormalClass()); 217 ASSERT(kernel_klass->IsNormalClass());
230 NormalClass* kernel_normal_class = NormalClass::Cast(kernel_klass); 218 NormalClass* kernel_normal_class = NormalClass::Cast(kernel_klass);
231 219
232 ActiveClassScope active_class_scope(&active_class_, kernel_klass, klass); 220 ActiveClassScope active_class_scope(&active_class_, kernel_klass, klass);
233 221
234 // First setup the type parameters, so if any of the following code uses it 222 // First setup the type parameters, so if any of the following code uses it
235 // (in a recursive way) we're fine. 223 // (in a recursive way) we're fine.
236 TypeArguments& type_parameters = 224 TypeArguments& type_parameters =
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 AbstractType& super_type = 265 AbstractType& super_type =
278 T.TranslateTypeWithoutFinalization(kernel_normal_class->super_class()); 266 T.TranslateTypeWithoutFinalization(kernel_normal_class->super_class());
279 if (super_type.IsMalformed()) H.ReportError("Malformed super type"); 267 if (super_type.IsMalformed()) H.ReportError("Malformed super type");
280 klass->set_super_type(super_type); 268 klass->set_super_type(super_type);
281 } 269 }
282 270
283 // Build implemented interface types 271 // Build implemented interface types
284 intptr_t interface_count = kernel_klass->implemented_classes().length(); 272 intptr_t interface_count = kernel_klass->implemented_classes().length();
285 const dart::Array& interfaces = 273 const dart::Array& interfaces =
286 dart::Array::Handle(Z, dart::Array::New(interface_count)); 274 dart::Array::Handle(Z, dart::Array::New(interface_count));
287 dart::Class& interface_class = dart::Class::Handle(Z);
288 for (intptr_t i = 0; i < interface_count; i++) { 275 for (intptr_t i = 0; i < interface_count; i++) {
289 InterfaceType* kernel_interface_type = 276 InterfaceType* kernel_interface_type =
290 kernel_klass->implemented_classes()[i]; 277 kernel_klass->implemented_classes()[i];
291 const AbstractType& type = 278 const AbstractType& type =
292 T.TranslateTypeWithoutFinalization(kernel_interface_type); 279 T.TranslateTypeWithoutFinalization(kernel_interface_type);
293 if (type.IsMalformed()) H.ReportError("Malformed interface type."); 280 if (type.IsMalformed()) H.ReportError("Malformed interface type.");
294 interfaces.SetAt(i, type); 281 interfaces.SetAt(i, type);
295
296 // NOTE: Normally the DartVM keeps a list of pending classes and iterates
297 // through them later on using `ClassFinalizer::ProcessPendingClasses()`.
298 // This involes calling `ClassFinalizer::ResolveSuperTypeAndInterfaces()`
299 // which does a lot of error validation (e.g. cycle checks) which we don't
300 // need here. But we do need to do one thing which this resolving phase
301 // normally does for us: set the `is_implemented` boolean.
302
303 // TODO(27590): Maybe we can do this differently once we have
304 // "bootstrapping from kernel"-support.
305 interface_class = type.type_class();
306 interface_class.set_is_implemented();
307 } 282 }
308 klass->set_interfaces(interfaces); 283 klass->set_interfaces(interfaces);
309 if (kernel_klass->is_abstract()) klass->set_is_abstract(); 284 if (kernel_klass->is_abstract()) klass->set_is_abstract();
310 klass->set_is_cycle_free();
311
312 // When bootstrapping we should not finalize types yet because they will be
313 // finalized when the object store's pending_classes list is drained by
314 // ClassFinalizer::ProcessPendingClasses. Even when not bootstrapping we are
315 // careful not to eagerly finalize types that may introduce a circularity
316 // (such as type arguments, interface types, field types, etc.).
317 if (finalize_) ClassFinalizer::FinalizeTypesInClass(*klass);
318 } 285 }
319 286
320 void KernelReader::ReadClass(const dart::Library& library, 287
321 Class* kernel_klass) { 288 dart::Class& KernelReader::ReadClass(const dart::Library& library,
289 Class* kernel_klass) {
322 // This will trigger a call to [ReadPreliminaryClass] if not already done. 290 // This will trigger a call to [ReadPreliminaryClass] if not already done.
323 dart::Class& klass = LookupClass(kernel_klass); 291 dart::Class& klass = LookupClass(kernel_klass);
324 292
325 ActiveClassScope active_class_scope(&active_class_, kernel_klass, &klass); 293 ActiveClassScope active_class_scope(&active_class_, kernel_klass, &klass);
326 294
327 TokenPosition pos(0); 295 TokenPosition pos(0);
328 296
329 for (intptr_t i = 0; i < kernel_klass->fields().length(); i++) { 297 for (intptr_t i = 0; i < kernel_klass->fields().length(); i++) {
330 Field* kernel_field = kernel_klass->fields()[i]; 298 Field* kernel_field = kernel_klass->fields()[i];
331 ActiveMemberScope active_member_scope(&active_class_, kernel_field); 299 ActiveMemberScope active_member_scope(&active_class_, kernel_field);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 Procedure* kernel_procedure = kernel_klass->procedures()[i]; 344 Procedure* kernel_procedure = kernel_klass->procedures()[i];
377 ActiveMemberScope active_member_scope(&active_class_, kernel_procedure); 345 ActiveMemberScope active_member_scope(&active_class_, kernel_procedure);
378 ReadProcedure(library, klass, kernel_procedure, kernel_klass); 346 ReadProcedure(library, klass, kernel_procedure, kernel_klass);
379 } 347 }
380 348
381 if (bootstrapping_ && !klass.is_marked_for_parsing()) { 349 if (bootstrapping_ && !klass.is_marked_for_parsing()) {
382 klass.set_is_marked_for_parsing(); 350 klass.set_is_marked_for_parsing();
383 GrowableObjectArray::Handle(Z, I->object_store()->pending_classes()) 351 GrowableObjectArray::Handle(Z, I->object_store()->pending_classes())
384 .Add(klass, Heap::kOld); 352 .Add(klass, Heap::kOld);
385 } 353 }
354
355 return klass;
386 } 356 }
387 357
358
388 void KernelReader::ReadProcedure(const dart::Library& library, 359 void KernelReader::ReadProcedure(const dart::Library& library,
389 const dart::Class& owner, 360 const dart::Class& owner,
390 Procedure* kernel_procedure, 361 Procedure* kernel_procedure,
391 Class* kernel_klass) { 362 Class* kernel_klass) {
392 ActiveClassScope active_class_scope(&active_class_, kernel_klass, &owner); 363 ActiveClassScope active_class_scope(&active_class_, kernel_klass, &owner);
393 ActiveMemberScope active_member_scope(&active_class_, kernel_procedure); 364 ActiveMemberScope active_member_scope(&active_class_, kernel_procedure);
394 ActiveFunctionScope active_function_scope(&active_class_, 365 ActiveFunctionScope active_function_scope(&active_class_,
395 kernel_procedure->function()); 366 kernel_procedure->function());
396 367
397 const dart::String& name = H.DartProcedureName(kernel_procedure); 368 const dart::String& name = H.DartProcedureName(kernel_procedure);
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 false, // is_native 488 false, // is_native
518 klass, pos)); 489 klass, pos));
519 klass.AddFunction(setter); 490 klass.AddFunction(setter);
520 setter.set_kernel_function(kernel_field); 491 setter.set_kernel_function(kernel_field);
521 setter.set_result_type(Object::void_type()); 492 setter.set_result_type(Object::void_type());
522 setter.set_is_debuggable(false); 493 setter.set_is_debuggable(false);
523 SetupFieldAccessorFunction(klass, setter); 494 SetupFieldAccessorFunction(klass, setter);
524 } 495 }
525 } 496 }
526 497
498
527 void KernelReader::SetupFunctionParameters(TranslationHelper translation_helper, 499 void KernelReader::SetupFunctionParameters(TranslationHelper translation_helper,
528 DartTypeTranslator type_translator, 500 DartTypeTranslator type_translator,
529 const dart::Class& klass, 501 const dart::Class& klass,
530 const dart::Function& function, 502 const dart::Function& function,
531 FunctionNode* node, 503 FunctionNode* node,
532 bool is_method, 504 bool is_method,
533 bool is_closure) { 505 bool is_closure) {
534 dart::Zone* zone = translation_helper.zone(); 506 dart::Zone* zone = translation_helper.zone();
535 507
536 ASSERT(!(is_method && is_closure)); 508 ASSERT(!(is_method && is_closure));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 function.SetParameterNameAt( 559 function.SetParameterNameAt(
588 pos, translation_helper.DartSymbol(named_expression->name())); 560 pos, translation_helper.DartSymbol(named_expression->name()));
589 } 561 }
590 562
591 const AbstractType& return_type = 563 const AbstractType& return_type =
592 type_translator.TranslateType(node->return_type()); 564 type_translator.TranslateType(node->return_type());
593 function.set_result_type(return_type.IsMalformed() ? Type::dynamic_type() 565 function.set_result_type(return_type.IsMalformed() ? Type::dynamic_type()
594 : return_type); 566 : return_type);
595 } 567 }
596 568
569
597 void KernelReader::SetupFieldAccessorFunction(const dart::Class& klass, 570 void KernelReader::SetupFieldAccessorFunction(const dart::Class& klass,
598 const dart::Function& function) { 571 const dart::Function& function) {
599 bool is_setter = function.IsImplicitSetterFunction(); 572 bool is_setter = function.IsImplicitSetterFunction();
600 bool is_method = !function.IsStaticFunction(); 573 bool is_method = !function.IsStaticFunction();
601 intptr_t num_parameters = (is_method ? 1 : 0) + (is_setter ? 1 : 0); 574 intptr_t num_parameters = (is_method ? 1 : 0) + (is_setter ? 1 : 0);
602 575
603 function.SetNumOptionalParameters(0, false); 576 function.SetNumOptionalParameters(0, false);
604 function.set_num_fixed_parameters(num_parameters); 577 function.set_num_fixed_parameters(num_parameters);
605 function.set_parameter_types( 578 function.set_parameter_types(
606 Array::Handle(Z, Array::New(num_parameters, Heap::kOld))); 579 Array::Handle(Z, Array::New(num_parameters, Heap::kOld)));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 // we do not risk allocating the class again by calling LookupClass 645 // we do not risk allocating the class again by calling LookupClass
673 // recursively from ReadPreliminaryClass for the same class. 646 // recursively from ReadPreliminaryClass for the same class.
674 classes_.Insert(klass, handle); 647 classes_.Insert(klass, handle);
675 if (!handle->is_type_finalized()) { 648 if (!handle->is_type_finalized()) {
676 ReadPreliminaryClass(handle, klass); 649 ReadPreliminaryClass(handle, klass);
677 } 650 }
678 } 651 }
679 return *handle; 652 return *handle;
680 } 653 }
681 654
655
682 RawFunction::Kind KernelReader::GetFunctionType(Procedure* kernel_procedure) { 656 RawFunction::Kind KernelReader::GetFunctionType(Procedure* kernel_procedure) {
683 intptr_t lookuptable[] = { 657 intptr_t lookuptable[] = {
684 RawFunction::kRegularFunction, // Procedure::kMethod 658 RawFunction::kRegularFunction, // Procedure::kMethod
685 RawFunction::kGetterFunction, // Procedure::kGetter 659 RawFunction::kGetterFunction, // Procedure::kGetter
686 RawFunction::kSetterFunction, // Procedure::kSetter 660 RawFunction::kSetterFunction, // Procedure::kSetter
687 RawFunction::kRegularFunction, // Procedure::kOperator 661 RawFunction::kRegularFunction, // Procedure::kOperator
688 RawFunction::kConstructor, // Procedure::kFactory 662 RawFunction::kConstructor, // Procedure::kFactory
689 }; 663 };
690 intptr_t kind = static_cast<int>(kernel_procedure->kind()); 664 intptr_t kind = static_cast<int>(kernel_procedure->kind());
691 if (kind == Procedure::kIncompleteProcedure) { 665 if (kind == Procedure::kIncompleteProcedure) {
692 return RawFunction::kSignatureFunction; 666 return RawFunction::kSignatureFunction;
693 } else { 667 } else {
694 ASSERT(0 <= kind && kind <= Procedure::kFactory); 668 ASSERT(0 <= kind && kind <= Procedure::kFactory);
695 return static_cast<RawFunction::Kind>(lookuptable[kind]); 669 return static_cast<RawFunction::Kind>(lookuptable[kind]);
696 } 670 }
697 } 671 }
698 672
673
699 } // namespace kernel 674 } // namespace kernel
700 } // namespace dart 675 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698