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

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

Issue 2891053003: Add support for converted closures with explicit contexts to VM (Closed)
Patch Set: Update the code according to Martin's comments Created 3 years, 6 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/become.h" 10 #include "vm/become.h"
11 #include "vm/cpu.h"
12 #include "vm/bit_vector.h" 11 #include "vm/bit_vector.h"
13 #include "vm/bootstrap.h" 12 #include "vm/bootstrap.h"
14 #include "vm/class_finalizer.h" 13 #include "vm/class_finalizer.h"
15 #include "vm/code_observers.h" 14 #include "vm/code_observers.h"
16 #include "vm/compiler.h" 15 #include "vm/compiler.h"
17 #include "vm/compiler_stats.h" 16 #include "vm/compiler_stats.h"
17 #include "vm/cpu.h"
18 #include "vm/dart.h" 18 #include "vm/dart.h"
19 #include "vm/dart_api_state.h" 19 #include "vm/dart_api_state.h"
20 #include "vm/dart_entry.h" 20 #include "vm/dart_entry.h"
21 #include "vm/datastream.h" 21 #include "vm/datastream.h"
22 #include "vm/debugger.h" 22 #include "vm/debugger.h"
23 #include "vm/deopt_instructions.h" 23 #include "vm/deopt_instructions.h"
24 #include "vm/disassembler.h" 24 #include "vm/disassembler.h"
25 #include "vm/double_conversion.h" 25 #include "vm/double_conversion.h"
26 #include "vm/exceptions.h" 26 #include "vm/exceptions.h"
27 #include "vm/growable_array.h" 27 #include "vm/growable_array.h"
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 Bool* Object::bool_true_ = NULL; 129 Bool* Object::bool_true_ = NULL;
130 Bool* Object::bool_false_ = NULL; 130 Bool* Object::bool_false_ = NULL;
131 Smi* Object::smi_illegal_cid_ = NULL; 131 Smi* Object::smi_illegal_cid_ = NULL;
132 LanguageError* Object::snapshot_writer_error_ = NULL; 132 LanguageError* Object::snapshot_writer_error_ = NULL;
133 LanguageError* Object::branch_offset_error_ = NULL; 133 LanguageError* Object::branch_offset_error_ = NULL;
134 LanguageError* Object::speculative_inlining_error_ = NULL; 134 LanguageError* Object::speculative_inlining_error_ = NULL;
135 LanguageError* Object::background_compilation_error_ = NULL; 135 LanguageError* Object::background_compilation_error_ = NULL;
136 Array* Object::vm_isolate_snapshot_object_table_ = NULL; 136 Array* Object::vm_isolate_snapshot_object_table_ = NULL;
137 Type* Object::dynamic_type_ = NULL; 137 Type* Object::dynamic_type_ = NULL;
138 Type* Object::void_type_ = NULL; 138 Type* Object::void_type_ = NULL;
139 Type* Object::vector_type_ = NULL;
139 140
140 RawObject* Object::null_ = reinterpret_cast<RawObject*>(RAW_NULL); 141 RawObject* Object::null_ = reinterpret_cast<RawObject*>(RAW_NULL);
141 RawClass* Object::class_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 142 RawClass* Object::class_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
142 RawClass* Object::dynamic_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 143 RawClass* Object::dynamic_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
144 RawClass* Object::vector_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
143 RawClass* Object::void_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 145 RawClass* Object::void_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
144 RawClass* Object::unresolved_class_class_ = 146 RawClass* Object::unresolved_class_class_ =
145 reinterpret_cast<RawClass*>(RAW_NULL); 147 reinterpret_cast<RawClass*>(RAW_NULL);
146 RawClass* Object::type_arguments_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 148 RawClass* Object::type_arguments_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
147 RawClass* Object::patch_class_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 149 RawClass* Object::patch_class_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
148 RawClass* Object::function_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 150 RawClass* Object::function_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
149 RawClass* Object::closure_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 151 RawClass* Object::closure_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
150 RawClass* Object::signature_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL); 152 RawClass* Object::signature_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
151 RawClass* Object::redirection_data_class_ = 153 RawClass* Object::redirection_data_class_ =
152 reinterpret_cast<RawClass*>(RAW_NULL); 154 reinterpret_cast<RawClass*>(RAW_NULL);
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 bool_true_ = Bool::ReadOnlyHandle(); 536 bool_true_ = Bool::ReadOnlyHandle();
535 bool_false_ = Bool::ReadOnlyHandle(); 537 bool_false_ = Bool::ReadOnlyHandle();
536 smi_illegal_cid_ = Smi::ReadOnlyHandle(); 538 smi_illegal_cid_ = Smi::ReadOnlyHandle();
537 snapshot_writer_error_ = LanguageError::ReadOnlyHandle(); 539 snapshot_writer_error_ = LanguageError::ReadOnlyHandle();
538 branch_offset_error_ = LanguageError::ReadOnlyHandle(); 540 branch_offset_error_ = LanguageError::ReadOnlyHandle();
539 speculative_inlining_error_ = LanguageError::ReadOnlyHandle(); 541 speculative_inlining_error_ = LanguageError::ReadOnlyHandle();
540 background_compilation_error_ = LanguageError::ReadOnlyHandle(); 542 background_compilation_error_ = LanguageError::ReadOnlyHandle();
541 vm_isolate_snapshot_object_table_ = Array::ReadOnlyHandle(); 543 vm_isolate_snapshot_object_table_ = Array::ReadOnlyHandle();
542 dynamic_type_ = Type::ReadOnlyHandle(); 544 dynamic_type_ = Type::ReadOnlyHandle();
543 void_type_ = Type::ReadOnlyHandle(); 545 void_type_ = Type::ReadOnlyHandle();
546 vector_type_ = Type::ReadOnlyHandle();
544 547
545 *null_object_ = Object::null(); 548 *null_object_ = Object::null();
546 *null_array_ = Array::null(); 549 *null_array_ = Array::null();
547 *null_string_ = String::null(); 550 *null_string_ = String::null();
548 *null_instance_ = Instance::null(); 551 *null_instance_ = Instance::null();
549 *null_type_arguments_ = TypeArguments::null(); 552 *null_type_arguments_ = TypeArguments::null();
550 553
551 // Initialize the empty and zero array handles to null_ in order to be able to 554 // Initialize the empty and zero array handles to null_ in order to be able to
552 // check if the empty and zero arrays were allocated (RAW_NULL is not 555 // check if the empty and zero arrays were allocated (RAW_NULL is not
553 // available). 556 // available).
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
893 dynamic_class_ = cls.raw(); 896 dynamic_class_ = cls.raw();
894 897
895 cls = Class::New<Instance>(kVoidCid); 898 cls = Class::New<Instance>(kVoidCid);
896 cls.set_num_type_arguments(0); 899 cls.set_num_type_arguments(0);
897 cls.set_num_own_type_arguments(0); 900 cls.set_num_own_type_arguments(0);
898 cls.set_is_finalized(); 901 cls.set_is_finalized();
899 cls.set_is_type_finalized(); 902 cls.set_is_type_finalized();
900 cls.set_is_cycle_free(); 903 cls.set_is_cycle_free();
901 void_class_ = cls.raw(); 904 void_class_ = cls.raw();
902 905
906 cls = Class::New<Instance>(kVectorCid);
907 cls.set_num_type_arguments(0);
908 cls.set_num_own_type_arguments(0);
909 cls.set_is_finalized();
910 cls.set_is_type_finalized();
911 cls.set_is_cycle_free();
912 vector_class_ = cls.raw();
913
903 cls = Class::New<Type>(); 914 cls = Class::New<Type>();
904 cls.set_is_finalized(); 915 cls.set_is_finalized();
905 cls.set_is_type_finalized(); 916 cls.set_is_type_finalized();
906 cls.set_is_cycle_free(); 917 cls.set_is_cycle_free();
907 918
908 cls = dynamic_class_; 919 cls = dynamic_class_;
909 *dynamic_type_ = Type::NewNonParameterizedType(cls); 920 *dynamic_type_ = Type::NewNonParameterizedType(cls);
910 921
911 cls = void_class_; 922 cls = void_class_;
912 *void_type_ = Type::NewNonParameterizedType(cls); 923 *void_type_ = Type::NewNonParameterizedType(cls);
913 924
925 cls = vector_class_;
926 *vector_type_ = Type::NewNonParameterizedType(cls);
927
914 // Allocate and initialize singleton true and false boolean objects. 928 // Allocate and initialize singleton true and false boolean objects.
915 cls = Class::New<Bool>(); 929 cls = Class::New<Bool>();
916 isolate->object_store()->set_bool_class(cls); 930 isolate->object_store()->set_bool_class(cls);
917 *bool_true_ = Bool::New(true); 931 *bool_true_ = Bool::New(true);
918 *bool_false_ = Bool::New(false); 932 *bool_false_ = Bool::New(false);
919 933
920 *smi_illegal_cid_ = Smi::New(kIllegalCid); 934 *smi_illegal_cid_ = Smi::New(kIllegalCid);
921 935
922 String& error_str = String::Handle(); 936 String& error_str = String::Handle();
923 error_str = String::New("SnapshotWriter Error", Heap::kOld); 937 error_str = String::New("SnapshotWriter Error", Heap::kOld);
(...skipping 4554 matching lines...) Expand 10 before | Expand all | Expand 10 after
5478 UNREACHABLE(); 5492 UNREACHABLE();
5479 #else 5493 #else
5480 ASSERT(Thread::Current()->IsMutatorThread()); 5494 ASSERT(Thread::Current()->IsMutatorThread());
5481 ASSERT(value.IsNull() || !value.is_optimized()); 5495 ASSERT(value.IsNull() || !value.is_optimized());
5482 StorePointer(&raw_ptr()->unoptimized_code_, value.raw()); 5496 StorePointer(&raw_ptr()->unoptimized_code_, value.raw());
5483 #endif 5497 #endif
5484 } 5498 }
5485 5499
5486 5500
5487 RawContextScope* Function::context_scope() const { 5501 RawContextScope* Function::context_scope() const {
5488 if (IsClosureFunction()) { 5502 if (IsClosureFunction() || IsConvertedClosureFunction()) {
5489 const Object& obj = Object::Handle(raw_ptr()->data_); 5503 const Object& obj = Object::Handle(raw_ptr()->data_);
5490 ASSERT(!obj.IsNull()); 5504 ASSERT(!obj.IsNull());
5491 return ClosureData::Cast(obj).context_scope(); 5505 return ClosureData::Cast(obj).context_scope();
5492 } 5506 }
5493 return ContextScope::null(); 5507 return ContextScope::null();
5494 } 5508 }
5495 5509
5496 5510
5497 void Function::set_context_scope(const ContextScope& value) const { 5511 void Function::set_context_scope(const ContextScope& value) const {
5498 if (IsClosureFunction()) { 5512 if (IsClosureFunction() || IsConvertedClosureFunction()) {
5499 const Object& obj = Object::Handle(raw_ptr()->data_); 5513 const Object& obj = Object::Handle(raw_ptr()->data_);
5500 ASSERT(!obj.IsNull()); 5514 ASSERT(!obj.IsNull());
5501 ClosureData::Cast(obj).set_context_scope(value); 5515 ClosureData::Cast(obj).set_context_scope(value);
5502 return; 5516 return;
5503 } 5517 }
5504 UNREACHABLE(); 5518 UNREACHABLE();
5505 } 5519 }
5506 5520
5507 5521
5508 RawInstance* Function::implicit_static_closure() const { 5522 RawInstance* Function::implicit_static_closure() const {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
5588 ASSERT(!field.IsNull()); 5602 ASSERT(!field.IsNull());
5589 if (field.token_pos() == token_pos()) { 5603 if (field.token_pos() == token_pos()) {
5590 return field.raw(); 5604 return field.raw();
5591 } 5605 }
5592 } 5606 }
5593 return Field::null(); 5607 return Field::null();
5594 } 5608 }
5595 5609
5596 5610
5597 RawFunction* Function::parent_function() const { 5611 RawFunction* Function::parent_function() const {
5598 if (IsClosureFunction() || IsSignatureFunction()) { 5612 if (IsClosureFunction() || IsConvertedClosureFunction() ||
5613 IsSignatureFunction()) {
5599 const Object& obj = Object::Handle(raw_ptr()->data_); 5614 const Object& obj = Object::Handle(raw_ptr()->data_);
5600 ASSERT(!obj.IsNull()); 5615 ASSERT(!obj.IsNull());
5601 if (IsClosureFunction()) { 5616 if (IsClosureFunction() || IsConvertedClosureFunction()) {
5602 return ClosureData::Cast(obj).parent_function(); 5617 return ClosureData::Cast(obj).parent_function();
5603 } else { 5618 } else {
5604 return SignatureData::Cast(obj).parent_function(); 5619 return SignatureData::Cast(obj).parent_function();
5605 } 5620 }
5606 } 5621 }
5607 return Function::null(); 5622 return Function::null();
5608 } 5623 }
5609 5624
5610 5625
5611 void Function::set_parent_function(const Function& value) const { 5626 void Function::set_parent_function(const Function& value) const {
5612 const Object& obj = Object::Handle(raw_ptr()->data_); 5627 const Object& obj = Object::Handle(raw_ptr()->data_);
5613 ASSERT(!obj.IsNull()); 5628 ASSERT(!obj.IsNull());
5614 if (IsClosureFunction()) { 5629 if (IsClosureFunction() || IsConvertedClosureFunction()) {
5615 ClosureData::Cast(obj).set_parent_function(value); 5630 ClosureData::Cast(obj).set_parent_function(value);
5616 } else { 5631 } else {
5617 ASSERT(IsSignatureFunction()); 5632 ASSERT(IsSignatureFunction());
5618 SignatureData::Cast(obj).set_parent_function(value); 5633 SignatureData::Cast(obj).set_parent_function(value);
5619 } 5634 }
5620 } 5635 }
5621 5636
5622 5637
5623 bool Function::HasGenericParent() const { 5638 bool Function::HasGenericParent() const {
5624 if (IsImplicitClosureFunction()) { 5639 if (IsImplicitClosureFunction()) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
5662 const Object& obj = Object::Handle(raw_ptr()->data_); 5677 const Object& obj = Object::Handle(raw_ptr()->data_);
5663 ASSERT(obj.IsArray()); 5678 ASSERT(obj.IsArray());
5664 ASSERT((Array::Cast(obj).At(1) == Object::null()) || value.IsNull()); 5679 ASSERT((Array::Cast(obj).At(1) == Object::null()) || value.IsNull());
5665 Array::Cast(obj).SetAt(1, value); 5680 Array::Cast(obj).SetAt(1, value);
5666 } else { 5681 } else {
5667 ASSERT((raw_ptr()->data_ == Object::null()) || value.IsNull()); 5682 ASSERT((raw_ptr()->data_ == Object::null()) || value.IsNull());
5668 set_data(value); 5683 set_data(value);
5669 } 5684 }
5670 } 5685 }
5671 5686
5687 RawFunction* Function::converted_closure_function() const {
5688 if (IsClosureFunction() || IsSignatureFunction() || IsFactory()) {
5689 return Function::null();
5690 }
5691 const Object& obj = Object::Handle(raw_ptr()->data_);
5692 ASSERT(obj.IsNull() || obj.IsFunction());
5693 if (obj.IsFunction()) {
5694 return Function::Cast(obj).raw();
5695 }
5696 return Function::null();
5697 }
5698
5699
5700 void Function::set_converted_closure_function(const Function& value) const {
5701 ASSERT(!IsClosureFunction() && !IsSignatureFunction() && !is_native());
5702 ASSERT((raw_ptr()->data_ == Object::null()) || value.IsNull());
5703 set_data(value);
5704 }
5705
5672 5706
5673 RawType* Function::ExistingSignatureType() const { 5707 RawType* Function::ExistingSignatureType() const {
5674 const Object& obj = Object::Handle(raw_ptr()->data_); 5708 const Object& obj = Object::Handle(raw_ptr()->data_);
5675 ASSERT(!obj.IsNull()); 5709 ASSERT(!obj.IsNull());
5676 if (IsSignatureFunction()) { 5710 if (IsSignatureFunction()) {
5677 return SignatureData::Cast(obj).signature_type(); 5711 return SignatureData::Cast(obj).signature_type();
5678 } else { 5712 } else {
5679 ASSERT(IsClosureFunction()); 5713 ASSERT(IsClosureFunction() || IsConvertedClosureFunction());
5680 return ClosureData::Cast(obj).signature_type(); 5714 return ClosureData::Cast(obj).signature_type();
5681 } 5715 }
5682 } 5716 }
5683 5717
5684 5718
5685 RawType* Function::SignatureType() const { 5719 RawType* Function::SignatureType() const {
5686 Type& type = Type::Handle(ExistingSignatureType()); 5720 Type& type = Type::Handle(ExistingSignatureType());
5687 if (type.IsNull()) { 5721 if (type.IsNull()) {
5688 // The function type of this function is not yet cached and needs to be 5722 // The function type of this function is not yet cached and needs to be
5689 // constructed and cached here. 5723 // constructed and cached here.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
5724 } 5758 }
5725 5759
5726 5760
5727 void Function::SetSignatureType(const Type& value) const { 5761 void Function::SetSignatureType(const Type& value) const {
5728 const Object& obj = Object::Handle(raw_ptr()->data_); 5762 const Object& obj = Object::Handle(raw_ptr()->data_);
5729 ASSERT(!obj.IsNull()); 5763 ASSERT(!obj.IsNull());
5730 if (IsSignatureFunction()) { 5764 if (IsSignatureFunction()) {
5731 SignatureData::Cast(obj).set_signature_type(value); 5765 SignatureData::Cast(obj).set_signature_type(value);
5732 ASSERT(!value.IsCanonical() || (value.signature() == this->raw())); 5766 ASSERT(!value.IsCanonical() || (value.signature() == this->raw()));
5733 } else { 5767 } else {
5734 ASSERT(IsClosureFunction()); 5768 ASSERT(IsClosureFunction() || IsConvertedClosureFunction());
5735 ClosureData::Cast(obj).set_signature_type(value); 5769 ClosureData::Cast(obj).set_signature_type(value);
5736 } 5770 }
5737 } 5771 }
5738 5772
5739 5773
5740 bool Function::IsRedirectingFactory() const { 5774 bool Function::IsRedirectingFactory() const {
5741 if (!IsFactory() || !is_redirecting()) { 5775 if (!IsFactory() || !is_redirecting()) {
5742 return false; 5776 return false;
5743 } 5777 }
5744 ASSERT(!IsClosureFunction()); // A factory cannot also be a closure. 5778 ASSERT(!IsClosureFunction()); // A factory cannot also be a closure.
(...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after
6814 NOT_IN_PRECOMPILED(result.set_deoptimization_counter(0)); 6848 NOT_IN_PRECOMPILED(result.set_deoptimization_counter(0));
6815 NOT_IN_PRECOMPILED(result.set_optimized_instruction_count(0)); 6849 NOT_IN_PRECOMPILED(result.set_optimized_instruction_count(0));
6816 NOT_IN_PRECOMPILED(result.set_optimized_call_site_count(0)); 6850 NOT_IN_PRECOMPILED(result.set_optimized_call_site_count(0));
6817 result.set_kernel_offset(0); 6851 result.set_kernel_offset(0);
6818 result.set_is_optimizable(is_native ? false : true); 6852 result.set_is_optimizable(is_native ? false : true);
6819 result.set_is_inlinable(true); 6853 result.set_is_inlinable(true);
6820 result.set_allows_hoisting_check_class(true); 6854 result.set_allows_hoisting_check_class(true);
6821 result.set_allows_bounds_check_generalization(true); 6855 result.set_allows_bounds_check_generalization(true);
6822 result.SetInstructionsSafe( 6856 result.SetInstructionsSafe(
6823 Code::Handle(StubCode::LazyCompile_entry()->code())); 6857 Code::Handle(StubCode::LazyCompile_entry()->code()));
6824 if (kind == RawFunction::kClosureFunction) { 6858 if (kind == RawFunction::kClosureFunction ||
6859 kind == RawFunction::kConvertedClosureFunction) {
6825 ASSERT(space == Heap::kOld); 6860 ASSERT(space == Heap::kOld);
6826 const ClosureData& data = ClosureData::Handle(ClosureData::New()); 6861 const ClosureData& data = ClosureData::Handle(ClosureData::New());
6827 result.set_data(data); 6862 result.set_data(data);
6828 } else if (kind == RawFunction::kSignatureFunction) { 6863 } else if (kind == RawFunction::kSignatureFunction) {
6829 const SignatureData& data = 6864 const SignatureData& data =
6830 SignatureData::Handle(SignatureData::New(space)); 6865 SignatureData::Handle(SignatureData::New(space));
6831 result.set_data(data); 6866 result.set_data(data);
6832 } else { 6867 } else {
6833 // Functions other than signature functions have no reason to be allocated 6868 // Functions other than signature functions have no reason to be allocated
6834 // in new space. 6869 // in new space.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
6900 /* is_static = */ parent.is_static(), 6935 /* is_static = */ parent.is_static(),
6901 /* is_const = */ false, 6936 /* is_const = */ false,
6902 /* is_abstract = */ false, 6937 /* is_abstract = */ false,
6903 /* is_external = */ false, 6938 /* is_external = */ false,
6904 /* is_native = */ false, parent_owner, token_pos)); 6939 /* is_native = */ false, parent_owner, token_pos));
6905 result.set_parent_function(parent); 6940 result.set_parent_function(parent);
6906 return result.raw(); 6941 return result.raw();
6907 } 6942 }
6908 6943
6909 6944
6945 RawFunction* Function::NewConvertedClosureFunction(const String& name,
6946 const Function& parent,
6947 TokenPosition token_pos) {
6948 ASSERT(!parent.IsNull());
6949 // Only static top-level functions are allowed to be converted right now.
6950 ASSERT(parent.is_static());
6951 // Use the owner defining the parent function and not the class containing it.
6952 const Object& parent_owner = Object::Handle(parent.raw_ptr()->owner_);
6953 ASSERT(!parent_owner.IsNull());
6954 const Function& result = Function::Handle(
6955 Function::New(name, RawFunction::kConvertedClosureFunction,
6956 /* is_static = */ true,
6957 /* is_const = */ false,
6958 /* is_abstract = */ false,
6959 /* is_external = */ false,
6960 /* is_native = */ false, parent_owner, token_pos));
6961 result.set_parent_function(parent);
6962 return result.raw();
6963 }
6964
6965
6910 RawFunction* Function::NewSignatureFunction(const Object& owner, 6966 RawFunction* Function::NewSignatureFunction(const Object& owner,
6911 TokenPosition token_pos, 6967 TokenPosition token_pos,
6912 Heap::Space space) { 6968 Heap::Space space) {
6913 const Function& result = Function::Handle(Function::New( 6969 const Function& result = Function::Handle(Function::New(
6914 Symbols::AnonymousSignature(), RawFunction::kSignatureFunction, 6970 Symbols::AnonymousSignature(), RawFunction::kSignatureFunction,
6915 /* is_static = */ false, 6971 /* is_static = */ false,
6916 /* is_const = */ false, 6972 /* is_const = */ false,
6917 /* is_abstract = */ false, 6973 /* is_abstract = */ false,
6918 /* is_external = */ false, 6974 /* is_external = */ false,
6919 /* is_native = */ false, 6975 /* is_native = */ false,
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
7026 void Function::DropUncompiledImplicitClosureFunction() const { 7082 void Function::DropUncompiledImplicitClosureFunction() const {
7027 if (implicit_closure_function() != Function::null()) { 7083 if (implicit_closure_function() != Function::null()) {
7028 const Function& func = Function::Handle(implicit_closure_function()); 7084 const Function& func = Function::Handle(implicit_closure_function());
7029 if (!func.HasCode()) { 7085 if (!func.HasCode()) {
7030 set_implicit_closure_function(Function::Handle()); 7086 set_implicit_closure_function(Function::Handle());
7031 } 7087 }
7032 } 7088 }
7033 } 7089 }
7034 7090
7035 7091
7092 // Converted closure functions represent a pair of a top-level function and a
7093 // vector of captured variables. When being invoked, converted closure
7094 // functions get the vector as the first argument, and the arguments supplied at
7095 // the invocation are passed as the remaining arguments to that function.
7096 //
7097 // Consider the following example in Kernel:
7098 //
7099 // static method foo(core::int start) → core::Function {
7100 // core::int i = 0;
7101 // return () → dynamic => start.+(
7102 // let final dynamic #t1 = i in
7103 // let final dynamic #t2 = i = #t1.+(1) in
7104 // #t1
7105 // );
7106 // }
7107 //
7108 // The method [foo] above produces a closure as a result. The closure captures
7109 // two variables: function parameter [start] and local variable [i]. Here is
7110 // how this code would look like after closure conversion:
7111 //
7112 // static method foo(core::int start) → core::Function {
7113 // final Vector #context = MakeVector(3);
7114 // #context[1] = start;
7115 // #context[2] = 0;
7116 // return MakeClosure<() → dynamic>(com::closure#foo#function, #context);
7117 // }
7118 // static method closure#foo#function(Vector #contextParameter) → dynamic {
7119 // return (#contextParameter[1]).+(
7120 // let final dynamic #t1 = #contextParameter[2] in
7121 // let final dynamic #t2 = #contextParameter[2] = #t1.+(1) in
7122 // #t1
7123 // );
7124 // }
7125 //
7126 // Converted closure functions are used in VM Closure instances that represent
7127 // the results of evaluation of [MakeClosure] primitive operations.
7128 //
7129 // Internally, converted closure functins are represented with the same Closure
7130 // class as implicit closure functions (that are used for dealing with
7131 // tear-offs). The Closure class instances have two fields, one for the
7132 // function, and one for the captured context. Implicit closure functions have
7133 // pre-defined shape of the context: it's a single variable that is used as
7134 // 'this'. Converted closure functions use the context field to store the
7135 // vector of captured variables that will be supplied as the first argument
7136 // during invocation.
7137 //
7138 // The top-level functions used in converted closure functions are generated
7139 // during a front-end transformation in Kernel. Those functions have some
7140 // common properties:
7141 // * they expect the captured context to be passed as the first argument,
7142 // * the first argument should be of [Vector] type (index-based storage),
7143 // * they retrieve the captured variables from [Vector] explicitly, so they
7144 // don't need the variables from the context to be put into their current
7145 // scope.
7146 //
7147 // During closure-conversion pass in Kernel, the contexts are generated
7148 // explicitly and are represented as [Vector]s. Then they are paired together
7149 // with the top-level functions to form a closure. When the closure, created
7150 // this way, is invoked, it should receive the [Vector] as the first argument,
7151 // and take the rest of the arguments from the invocation.
7152 //
7153 // Converted cosure functions in VM follow same discipline as implicit closure
7154 // functions, because they are similar in many ways. For further deatils, please
7155 // refer to the following methods:
7156 // -> Function::ConvertedClosureFunction
7157 // -> FlowGraphBuilder::BuildGraphOfConvertedClosureFunction
7158 // -> FlowGraphBuilder::BuildGraph (small change that calls
7159 // BuildGraphOfConvertedClosureFunction)
7160 // -> FlowGraphBuilder::VisitClosureCreation (converted closure functions are
7161 // created here)
7162 //
7163 // Function::ConvertedClosureFunction method follows the logic similar to that
7164 // of Function::ImplicitClosureFunction method.
7165 //
7166 // TODO(29181): Adjust the method to correctly pass type parameters after they
7167 // are enabled in closure conversion.
7168 RawFunction* Function::ConvertedClosureFunction() const {
7169 // Return the existing converted closure function if any.
7170 if (converted_closure_function() != Function::null()) {
7171 return converted_closure_function();
7172 }
7173 ASSERT(!IsSignatureFunction() && !IsClosureFunction());
7174 Thread* thread = Thread::Current();
7175 Zone* zone = thread->zone();
7176 // Create closure function.
7177 const String& closure_name = String::Handle(zone, name());
7178 const Function& closure_function = Function::Handle(
7179 zone, NewConvertedClosureFunction(closure_name, *this, token_pos()));
7180
7181 // Currently only static top-level functions are allowed to be converted.
7182 ASSERT(is_static());
7183 closure_function.set_context_scope(Object::empty_context_scope());
7184
7185 // Set closure function's type parameters.
7186 closure_function.set_type_parameters(
7187 TypeArguments::Handle(zone, type_parameters()));
7188
7189 // Set closure function's result type to this result type.
7190 closure_function.set_result_type(AbstractType::Handle(zone, result_type()));
7191
7192 // Set closure function's end token to this end token.
7193 closure_function.set_end_token_pos(end_token_pos());
7194
7195 // The closurized method stub just calls into the original method and should
7196 // therefore be skipped by the debugger and in stack traces.
7197 closure_function.set_is_debuggable(false);
7198 closure_function.set_is_visible(false);
7199
7200 // Set closure function's formal parameters to this formal parameters,
7201 // removing the first parameter over which the currying is done, and adding
7202 // the closure class instance as the first parameter. So, the overall number
7203 // of fixed parameters doesn't change.
7204 const int num_fixed_params = num_fixed_parameters();
7205 const int num_opt_params = NumOptionalParameters();
7206 const bool has_opt_pos_params = HasOptionalPositionalParameters();
7207 const int num_params = num_fixed_params + num_opt_params;
7208
7209 closure_function.set_num_fixed_parameters(num_fixed_params);
7210 closure_function.SetNumOptionalParameters(num_opt_params, has_opt_pos_params);
7211 closure_function.set_parameter_types(
7212 Array::Handle(zone, Array::New(num_params, Heap::kOld)));
7213 closure_function.set_parameter_names(
7214 Array::Handle(zone, Array::New(num_params, Heap::kOld)));
7215
7216 AbstractType& param_type = AbstractType::Handle(zone);
7217 String& param_name = String::Handle(zone);
7218
7219 // Add implicit closure object as the first parameter.
7220 param_type = Type::DynamicType();
7221 closure_function.SetParameterTypeAt(0, param_type);
7222 closure_function.SetParameterNameAt(0, Symbols::ClosureParameter());
7223
7224 // All the parameters, but the first one, are the same for the top-level
7225 // function being converted, and the method of the closure class that is being
7226 // generated.
7227 for (int i = 1; i < num_params; i++) {
7228 param_type = ParameterTypeAt(i);
7229 closure_function.SetParameterTypeAt(i, param_type);
7230 param_name = ParameterNameAt(i);
7231 closure_function.SetParameterNameAt(i, param_name);
7232 }
7233 closure_function.set_kernel_offset(kernel_offset());
7234
7235 const Type& signature_type =
7236 Type::Handle(zone, closure_function.SignatureType());
7237 if (!signature_type.IsFinalized()) {
7238 ClassFinalizer::FinalizeType(Class::Handle(zone, Owner()), signature_type);
7239 }
7240
7241 set_converted_closure_function(closure_function);
7242
7243 return closure_function.raw();
7244 }
7245
7246
7247 void Function::DropUncompiledConvertedClosureFunction() const {
7248 if (converted_closure_function() != Function::null()) {
7249 const Function& func = Function::Handle(converted_closure_function());
7250 if (!func.HasCode()) {
7251 set_converted_closure_function(Function::Handle());
7252 }
7253 }
7254 }
7255
7256
7036 RawString* Function::UserVisibleFormalParameters() const { 7257 RawString* Function::UserVisibleFormalParameters() const {
7037 Thread* thread = Thread::Current(); 7258 Thread* thread = Thread::Current();
7038 Zone* zone = thread->zone(); 7259 Zone* zone = thread->zone();
7039 // Typically 3, 5,.. elements in 'pieces', e.g.: 7260 // Typically 3, 5,.. elements in 'pieces', e.g.:
7040 // '_LoadRequest', CommaSpace, '_LoadError'. 7261 // '_LoadRequest', CommaSpace, '_LoadError'.
7041 GrowableHandlePtrArray<const String> pieces(zone, 5); 7262 GrowableHandlePtrArray<const String> pieces(zone, 5);
7042 BuildSignatureParameters(thread, zone, kUserVisibleName, &pieces); 7263 BuildSignatureParameters(thread, zone, kUserVisibleName, &pieces);
7043 return Symbols::FromConcatAll(thread, pieces); 7264 return Symbols::FromConcatAll(thread, pieces);
7044 } 7265 }
7045 7266
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
7520 if (IsNull()) { 7741 if (IsNull()) {
7521 return "Function: null"; 7742 return "Function: null";
7522 } 7743 }
7523 const char* static_str = is_static() ? " static" : ""; 7744 const char* static_str = is_static() ? " static" : "";
7524 const char* abstract_str = is_abstract() ? " abstract" : ""; 7745 const char* abstract_str = is_abstract() ? " abstract" : "";
7525 const char* kind_str = NULL; 7746 const char* kind_str = NULL;
7526 const char* const_str = is_const() ? " const" : ""; 7747 const char* const_str = is_const() ? " const" : "";
7527 switch (kind()) { 7748 switch (kind()) {
7528 case RawFunction::kRegularFunction: 7749 case RawFunction::kRegularFunction:
7529 case RawFunction::kClosureFunction: 7750 case RawFunction::kClosureFunction:
7751 case RawFunction::kConvertedClosureFunction:
7530 case RawFunction::kGetterFunction: 7752 case RawFunction::kGetterFunction:
7531 case RawFunction::kSetterFunction: 7753 case RawFunction::kSetterFunction:
7532 kind_str = ""; 7754 kind_str = "";
7533 break; 7755 break;
7534 case RawFunction::kSignatureFunction: 7756 case RawFunction::kSignatureFunction:
7535 kind_str = " signature"; 7757 kind_str = " signature";
7536 break; 7758 break;
7537 case RawFunction::kConstructor: 7759 case RawFunction::kConstructor:
7538 kind_str = is_static() ? " factory" : " constructor"; 7760 kind_str = is_static() ? " factory" : " constructor";
7539 break; 7761 break;
(...skipping 5102 matching lines...) Expand 10 before | Expand all | Expand 10 after
12642 if (kind == RawLocalVarDescriptors::kContextLevel) { 12864 if (kind == RawLocalVarDescriptors::kContextLevel) {
12643 return OS::SNPrint(buffer, len, 12865 return OS::SNPrint(buffer, len,
12644 "%2" Pd 12866 "%2" Pd
12645 " %-13s level=%-3d" 12867 " %-13s level=%-3d"
12646 " begin=%-3d end=%d\n", 12868 " begin=%-3d end=%d\n",
12647 i, LocalVarDescriptors::KindToCString(kind), index, 12869 i, LocalVarDescriptors::KindToCString(kind), index,
12648 static_cast<int>(info.begin_pos.value()), 12870 static_cast<int>(info.begin_pos.value()),
12649 static_cast<int>(info.end_pos.value())); 12871 static_cast<int>(info.end_pos.value()));
12650 } else if (kind == RawLocalVarDescriptors::kContextVar) { 12872 } else if (kind == RawLocalVarDescriptors::kContextVar) {
12651 return OS::SNPrint( 12873 return OS::SNPrint(
12652 buffer, len, "%2" Pd 12874 buffer, len,
12653 " %-13s level=%-3d index=%-3d" 12875 "%2" Pd
12654 " begin=%-3d end=%-3d name=%s\n", 12876 " %-13s level=%-3d index=%-3d"
12877 " begin=%-3d end=%-3d name=%s\n",
12655 i, LocalVarDescriptors::KindToCString(kind), info.scope_id, index, 12878 i, LocalVarDescriptors::KindToCString(kind), info.scope_id, index,
12656 static_cast<int>(info.begin_pos.Pos()), 12879 static_cast<int>(info.begin_pos.Pos()),
12657 static_cast<int>(info.end_pos.Pos()), var_name.ToCString()); 12880 static_cast<int>(info.end_pos.Pos()), var_name.ToCString());
12658 } else { 12881 } else {
12659 return OS::SNPrint( 12882 return OS::SNPrint(
12660 buffer, len, "%2" Pd 12883 buffer, len,
12661 " %-13s scope=%-3d index=%-3d" 12884 "%2" Pd
12662 " begin=%-3d end=%-3d name=%s\n", 12885 " %-13s scope=%-3d index=%-3d"
12886 " begin=%-3d end=%-3d name=%s\n",
12663 i, LocalVarDescriptors::KindToCString(kind), info.scope_id, index, 12887 i, LocalVarDescriptors::KindToCString(kind), info.scope_id, index,
12664 static_cast<int>(info.begin_pos.Pos()), 12888 static_cast<int>(info.begin_pos.Pos()),
12665 static_cast<int>(info.end_pos.Pos()), var_name.ToCString()); 12889 static_cast<int>(info.end_pos.Pos()), var_name.ToCString());
12666 } 12890 }
12667 } 12891 }
12668 12892
12669 12893
12670 const char* LocalVarDescriptors::ToCString() const { 12894 const char* LocalVarDescriptors::ToCString() const {
12671 if (IsNull()) { 12895 if (IsNull()) {
12672 return "LocalVarDescriptors: null"; 12896 return "LocalVarDescriptors: null";
(...skipping 3265 matching lines...) Expand 10 before | Expand all | Expand 10 after
15938 instantiated_other.IsDartFunctionType()) { 16162 instantiated_other.IsDartFunctionType()) {
15939 return true; 16163 return true;
15940 } 16164 }
15941 } 16165 }
15942 if (!instantiated_other.IsFunctionType()) { 16166 if (!instantiated_other.IsFunctionType()) {
15943 return false; 16167 return false;
15944 } 16168 }
15945 Function& other_signature = 16169 Function& other_signature =
15946 Function::Handle(zone, Type::Cast(instantiated_other).signature()); 16170 Function::Handle(zone, Type::Cast(instantiated_other).signature());
15947 Function& sig_fun = Function::Handle(zone, Closure::Cast(*this).function()); 16171 Function& sig_fun = Function::Handle(zone, Closure::Cast(*this).function());
16172 if (sig_fun.IsConvertedClosureFunction()) {
16173 const String& closure_name = String::Handle(zone, sig_fun.name());
16174 const Function& new_sig_fun = Function::Handle(
16175 zone,
16176 Function::NewConvertedClosureFunction(
16177 closure_name, Function::Handle(zone, sig_fun.parent_function()),
16178 TokenPosition::kNoSource));
16179
16180 new_sig_fun.set_type_parameters(
16181 TypeArguments::Handle(zone, sig_fun.type_parameters()));
16182 new_sig_fun.set_result_type(
16183 AbstractType::Handle(zone, sig_fun.result_type()));
16184 new_sig_fun.set_end_token_pos(TokenPosition::kNoSource);
16185
16186 new_sig_fun.set_is_debuggable(false);
16187 new_sig_fun.set_is_visible(false);
16188
16189 // The converted closed top-level function type should have its first
16190 // required optional parameter, i.e. context, removed.
16191 const int num_fixed_params = sig_fun.num_fixed_parameters() - 1;
16192 const int num_opt_params = sig_fun.NumOptionalParameters();
16193 const bool has_opt_pos_params = sig_fun.HasOptionalPositionalParameters();
16194 const int num_params = num_fixed_params + num_opt_params;
16195 new_sig_fun.set_num_fixed_parameters(num_fixed_params);
16196 new_sig_fun.SetNumOptionalParameters(num_opt_params, has_opt_pos_params);
16197 new_sig_fun.set_parameter_types(
16198 Array::Handle(zone, Array::New(num_params, Heap::kOld)));
16199 new_sig_fun.set_parameter_names(
16200 Array::Handle(zone, Array::New(num_params, Heap::kOld)));
16201 AbstractType& param_type = AbstractType::Handle(zone);
16202 String& param_name = String::Handle(zone);
16203 for (int i = 0; i < num_params; i++) {
16204 param_type = sig_fun.ParameterTypeAt(i + 1);
16205 new_sig_fun.SetParameterTypeAt(i, param_type);
16206 param_name = sig_fun.ParameterNameAt(i + 1);
16207 new_sig_fun.SetParameterNameAt(i, param_name);
16208 }
16209
16210 sig_fun = new_sig_fun.raw();
16211 }
15948 if (!sig_fun.HasInstantiatedSignature()) { 16212 if (!sig_fun.HasInstantiatedSignature()) {
15949 const TypeArguments& instantiator_type_arguments = TypeArguments::Handle( 16213 const TypeArguments& instantiator_type_arguments = TypeArguments::Handle(
15950 zone, Closure::Cast(*this).instantiator_type_arguments()); 16214 zone, Closure::Cast(*this).instantiator_type_arguments());
15951 const TypeArguments& function_type_arguments = TypeArguments::Handle( 16215 const TypeArguments& function_type_arguments = TypeArguments::Handle(
15952 zone, Closure::Cast(*this).function_type_arguments()); 16216 zone, Closure::Cast(*this).function_type_arguments());
15953 sig_fun = sig_fun.InstantiateSignatureFrom( 16217 sig_fun = sig_fun.InstantiateSignatureFrom(
15954 instantiator_type_arguments, function_type_arguments, Heap::kOld); 16218 instantiator_type_arguments, function_type_arguments, Heap::kOld);
15955 } 16219 }
15956 return sig_fun.IsSubtypeOf(other_signature, bound_error, NULL, Heap::kOld); 16220 return sig_fun.IsSubtypeOf(other_signature, bound_error, NULL, Heap::kOld);
15957 } 16221 }
(...skipping 1181 matching lines...) Expand 10 before | Expand all | Expand 10 after
17139 if (HasResolvedTypeClass()) { 17403 if (HasResolvedTypeClass()) {
17140 const Class& cls = Class::Handle(type_class()); 17404 const Class& cls = Class::Handle(type_class());
17141 len = cls.NumTypeParameters(); // Check the type parameters only. 17405 len = cls.NumTypeParameters(); // Check the type parameters only.
17142 if (len > num_type_args) { 17406 if (len > num_type_args) {
17143 // This type has the wrong number of arguments and is not finalized yet. 17407 // This type has the wrong number of arguments and is not finalized yet.
17144 // Type arguments are reset to null when finalizing such a type. 17408 // Type arguments are reset to null when finalizing such a type.
17145 ASSERT(!IsFinalized()); 17409 ASSERT(!IsFinalized());
17146 len = num_type_args; 17410 len = num_type_args;
17147 } 17411 }
17148 } 17412 }
17149 return (len == 0) || 17413 return (len == 0) || args.IsSubvectorInstantiated(num_type_args - len, len,
17150 args.IsSubvectorInstantiated(num_type_args - len, len, genericity, 17414 genericity, trail);
17151 trail);
17152 } 17415 }
17153 17416
17154 17417
17155 RawAbstractType* Type::InstantiateFrom( 17418 RawAbstractType* Type::InstantiateFrom(
17156 const TypeArguments& instantiator_type_arguments, 17419 const TypeArguments& instantiator_type_arguments,
17157 const TypeArguments& function_type_arguments, 17420 const TypeArguments& function_type_arguments,
17158 Error* bound_error, 17421 Error* bound_error,
17159 TrailPtr instantiation_trail, 17422 TrailPtr instantiation_trail,
17160 TrailPtr bound_trail, 17423 TrailPtr bound_trail,
17161 Heap::Space space) const { 17424 Heap::Space space) const {
(...skipping 2488 matching lines...) Expand 10 before | Expand all | Expand 10 after
19650 abs_value = value; 19913 abs_value = value;
19651 } 19914 }
19652 for (intptr_t i = 0; i < digit_shift; i++) { 19915 for (intptr_t i = 0; i < digit_shift; i++) {
19653 SetDigitAt(digits, i, 0); 19916 SetDigitAt(digits, i, 0);
19654 } 19917 }
19655 SetDigitAt(digits, 0 + digit_shift, 19918 SetDigitAt(digits, 0 + digit_shift,
19656 static_cast<uint32_t>(abs_value << bit_shift)); 19919 static_cast<uint32_t>(abs_value << bit_shift));
19657 SetDigitAt(digits, 1 + digit_shift, 19920 SetDigitAt(digits, 1 + digit_shift,
19658 static_cast<uint32_t>(abs_value >> (32 - bit_shift))); 19921 static_cast<uint32_t>(abs_value >> (32 - bit_shift)));
19659 SetDigitAt(digits, 2 + digit_shift, 19922 SetDigitAt(digits, 2 + digit_shift,
19660 (bit_shift == 0) ? 0 : static_cast<uint32_t>(abs_value >> 19923 (bit_shift == 0)
19661 (64 - bit_shift))); 19924 ? 0
19925 : static_cast<uint32_t>(abs_value >> (64 - bit_shift)));
19662 return New(neg, used, digits, space); 19926 return New(neg, used, digits, space);
19663 } 19927 }
19664 19928
19665 19929
19666 RawBigint* Bigint::NewFromCString(const char* str, Heap::Space space) { 19930 RawBigint* Bigint::NewFromCString(const char* str, Heap::Space space) {
19667 // Allow parser to scan Bigint literal, even with --limit-ints-to-64-bits. 19931 // Allow parser to scan Bigint literal, even with --limit-ints-to-64-bits.
19668 ASSERT(str != NULL); 19932 ASSERT(str != NULL);
19669 bool neg = false; 19933 bool neg = false;
19670 TypedData& digits = TypedData::Handle(); 19934 TypedData& digits = TypedData::Handle();
19671 if (str[0] == '-') { 19935 if (str[0] == '-') {
(...skipping 3747 matching lines...) Expand 10 before | Expand all | Expand 10 after
23419 return UserTag::null(); 23683 return UserTag::null();
23420 } 23684 }
23421 23685
23422 23686
23423 const char* UserTag::ToCString() const { 23687 const char* UserTag::ToCString() const {
23424 const String& tag_label = String::Handle(label()); 23688 const String& tag_label = String::Handle(label());
23425 return tag_label.ToCString(); 23689 return tag_label.ToCString();
23426 } 23690 }
23427 23691
23428 } // namespace dart 23692 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698