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

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

Powered by Google App Engine
This is Rietveld 408576698