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

Unified Diff: runtime/vm/object.cc

Issue 2891053003: Add support for converted closures with explicit contexts to VM (Closed)
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: runtime/vm/object.cc
diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc
index 950c03e5621fbbc7c1f621e80be6c1b189be4a4f..08f3a16a87ee3c2db195701ae79080311b2bf734 100644
--- a/runtime/vm/object.cc
+++ b/runtime/vm/object.cc
@@ -136,10 +136,12 @@ LanguageError* Object::background_compilation_error_ = NULL;
Array* Object::vm_isolate_snapshot_object_table_ = NULL;
Type* Object::dynamic_type_ = NULL;
Type* Object::void_type_ = NULL;
+Type* Object::vector_type_ = NULL;
RawObject* Object::null_ = reinterpret_cast<RawObject*>(RAW_NULL);
RawClass* Object::class_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
RawClass* Object::dynamic_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
+RawClass* Object::vector_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
RawClass* Object::void_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
RawClass* Object::unresolved_class_class_ =
reinterpret_cast<RawClass*>(RAW_NULL);
@@ -541,6 +543,7 @@ void Object::InitOnce(Isolate* isolate) {
vm_isolate_snapshot_object_table_ = Array::ReadOnlyHandle();
dynamic_type_ = Type::ReadOnlyHandle();
void_type_ = Type::ReadOnlyHandle();
+ vector_type_ = Type::ReadOnlyHandle();
*null_object_ = Object::null();
*null_array_ = Array::null();
@@ -900,6 +903,14 @@ void Object::InitOnce(Isolate* isolate) {
cls.set_is_cycle_free();
void_class_ = cls.raw();
+ cls = Class::New<Instance>(kVectorCid);
+ cls.set_num_type_arguments(0);
+ cls.set_num_own_type_arguments(0);
+ cls.set_is_finalized();
+ cls.set_is_type_finalized();
+ cls.set_is_cycle_free();
+ vector_class_ = cls.raw();
+
cls = Class::New<Type>();
cls.set_is_finalized();
cls.set_is_type_finalized();
@@ -911,6 +922,9 @@ void Object::InitOnce(Isolate* isolate) {
cls = void_class_;
*void_type_ = Type::NewNonParameterizedType(cls);
+ cls = vector_class_;
+ *vector_type_ = Type::NewNonParameterizedType(cls);
+
// Allocate and initialize singleton true and false boolean objects.
cls = Class::New<Bool>();
isolate->object_store()->set_bool_class(cls);
@@ -5483,7 +5497,7 @@ void Function::set_unoptimized_code(const Code& value) const {
RawContextScope* Function::context_scope() const {
- if (IsClosureFunction()) {
+ if (IsClosureFunction() || IsConvertedClosureFunction()) {
const Object& obj = Object::Handle(raw_ptr()->data_);
ASSERT(!obj.IsNull());
return ClosureData::Cast(obj).context_scope();
@@ -5493,7 +5507,7 @@ RawContextScope* Function::context_scope() const {
void Function::set_context_scope(const ContextScope& value) const {
- if (IsClosureFunction()) {
+ if (IsClosureFunction() || IsConvertedClosureFunction()) {
const Object& obj = Object::Handle(raw_ptr()->data_);
ASSERT(!obj.IsNull());
ClosureData::Cast(obj).set_context_scope(value);
@@ -5593,10 +5607,11 @@ RawField* Function::LookupImplicitGetterSetterField() const {
RawFunction* Function::parent_function() const {
- if (IsClosureFunction() || IsSignatureFunction()) {
+ if (IsClosureFunction() || IsConvertedClosureFunction() ||
+ IsSignatureFunction()) {
const Object& obj = Object::Handle(raw_ptr()->data_);
ASSERT(!obj.IsNull());
- if (IsClosureFunction()) {
+ if (IsClosureFunction() || IsConvertedClosureFunction()) {
return ClosureData::Cast(obj).parent_function();
} else {
return SignatureData::Cast(obj).parent_function();
@@ -5609,7 +5624,7 @@ RawFunction* Function::parent_function() const {
void Function::set_parent_function(const Function& value) const {
const Object& obj = Object::Handle(raw_ptr()->data_);
ASSERT(!obj.IsNull());
- if (IsClosureFunction()) {
+ if (IsClosureFunction() || IsConvertedClosureFunction()) {
ClosureData::Cast(obj).set_parent_function(value);
} else {
ASSERT(IsSignatureFunction());
@@ -5667,6 +5682,25 @@ void Function::set_implicit_closure_function(const Function& value) const {
}
}
+RawFunction* Function::converted_closure_function() const {
+ if (IsClosureFunction() || IsSignatureFunction() || IsFactory()) {
+ return Function::null();
+ }
+ const Object& obj = Object::Handle(raw_ptr()->data_);
+ ASSERT(obj.IsNull() || obj.IsFunction());
+ if (obj.IsFunction()) {
+ return Function::Cast(obj).raw();
+ }
+ return Function::null();
+}
+
+
+void Function::set_converted_closure_function(const Function& value) const {
+ ASSERT(!IsClosureFunction() && !IsSignatureFunction() && !is_native());
+ ASSERT((raw_ptr()->data_ == Object::null()) || value.IsNull());
+ set_data(value);
+}
+
RawType* Function::ExistingSignatureType() const {
const Object& obj = Object::Handle(raw_ptr()->data_);
@@ -5674,7 +5708,7 @@ RawType* Function::ExistingSignatureType() const {
if (IsSignatureFunction()) {
return SignatureData::Cast(obj).signature_type();
} else {
- ASSERT(IsClosureFunction());
+ ASSERT(IsClosureFunction() || IsConvertedClosureFunction());
return ClosureData::Cast(obj).signature_type();
}
}
@@ -5729,7 +5763,7 @@ void Function::SetSignatureType(const Type& value) const {
SignatureData::Cast(obj).set_signature_type(value);
ASSERT(!value.IsCanonical() || (value.signature() == this->raw()));
} else {
- ASSERT(IsClosureFunction());
+ ASSERT(IsClosureFunction() || IsConvertedClosureFunction());
ClosureData::Cast(obj).set_signature_type(value);
}
}
@@ -6802,7 +6836,8 @@ RawFunction* Function::New(const String& name,
result.set_allows_bounds_check_generalization(true);
result.SetInstructionsSafe(
Code::Handle(StubCode::LazyCompile_entry()->code()));
- if (kind == RawFunction::kClosureFunction) {
+ if (kind == RawFunction::kClosureFunction ||
+ kind == RawFunction::kConvertedClosureFunction) {
ASSERT(space == Heap::kOld);
const ClosureData& data = ClosureData::Handle(ClosureData::New());
result.set_data(data);
@@ -6888,6 +6923,27 @@ RawFunction* Function::NewClosureFunction(const String& name,
}
+RawFunction* Function::NewConvertedClosureFunction(const String& name,
+ const Function& parent,
+ TokenPosition token_pos) {
+ ASSERT(!parent.IsNull());
+ // Only static top-level functions are allowed to be converted right now.
+ ASSERT(parent.is_static());
+ // Use the owner defining the parent function and not the class containing it.
+ const Object& parent_owner = Object::Handle(parent.raw_ptr()->owner_);
+ ASSERT(!parent_owner.IsNull());
+ const Function& result = Function::Handle(
+ Function::New(name, RawFunction::kConvertedClosureFunction,
+ /* is_static = */ true,
+ /* is_const = */ false,
+ /* is_abstract = */ false,
+ /* is_external = */ false,
+ /* is_native = */ false, parent_owner, token_pos));
+ result.set_parent_function(parent);
+ return result.raw();
+}
+
+
RawFunction* Function::NewSignatureFunction(const Object& owner,
TokenPosition token_pos,
Heap::Space space) {
@@ -7014,6 +7070,130 @@ void Function::DropUncompiledImplicitClosureFunction() const {
}
+// Converted closure functions represent a pair of a top-level function and a
+// vector of captured variables. When being invoked, converted clousre
+// functions get the vector as the first argument, and the arguments supplied at
+// the invocation are passed as the remaining arguments to that function.
+//
+// Internally, converted closure functins are represented with the same Closure
+// class as implicit closure functions (that are used for dealing with
+// tear-offs). The Closure class instances have two fields, one for the
+// function, and one for the captured context. Implicit closure functions have
+// pre-defined shape of the context: it's a single variable that is used as
+// 'this'. Converted closure functions use the context field to store the
+// vector of captured variables that will be supplied as the first argument
+// during invocation.
+//
+// The top-level functions used in converted closure functions are generated
+// during a front-end transformation in Kernel. Those functions have some
+// common properties:
+// * they expect the captured context to be passed as the first argument,
+// * the first argument should be of Vector type (index-based storage),
+// * they retrieve the captured variables from Vectors explicitly, so they
+// don't need the variables from the context to be put into their current
+// scope.
+//
+// During closure-conversion pass in Kernel, the contexts are generated
+// explicitly and are represented as Vectors. Then they are paired together
+// with the top-level functions to form a closure. When the closure, created
+// this way, is invoked, it should receive the Vector as the first argument, and
+// take the rest of the arguments from the invocation.
+//
+// Converted cosure functions in VM follow same discipline as implicit closure
+// functions, because they are similar in many ways. For further deatils, please
+// refer to the following methods:
+// -> Function::ConvertedClosureFunction
+// -> FlowGraphBuilder::BuildGraphOfConvertedClosureFunction
+// -> FlowGraphBuilder::BuildGraph (small change that calls
+// BuildGraphOfConvertedClosureFunction)
+// -> FlowGraphBuilder::VisitClosureCreation (converted closure functions are
+// created here)
+//
+// Function::ConvertedClosureFunction method follows the logic similar to that
+// of Function::ImplicitClosureFunction method.
+RawFunction* Function::ConvertedClosureFunction() const {
+ // Return the existing converted closure function if any.
+ if (converted_closure_function() != Function::null()) {
+ return converted_closure_function();
+ }
+ ASSERT(!IsSignatureFunction() && !IsClosureFunction());
+ Thread* thread = Thread::Current();
+ Zone* zone = thread->zone();
+ // Create closure function.
+ const String& closure_name = String::Handle(zone, name());
+ const Function& closure_function = Function::Handle(
+ zone, NewConvertedClosureFunction(closure_name, *this, token_pos()));
+
+ // Currently only static top-level functions are allowed to be converted.
+ ASSERT(is_static());
+ closure_function.set_context_scope(Object::empty_context_scope());
+
+ // Set closure function's type parameters.
+ closure_function.set_type_parameters(
+ TypeArguments::Handle(zone, type_parameters()));
+
+ // Set closure function's result type to this result type.
+ closure_function.set_result_type(AbstractType::Handle(zone, result_type()));
+
+ // Set closure function's end token to this end token.
+ closure_function.set_end_token_pos(end_token_pos());
+
+ // The closurized method stub just calls into the original method and should
+ // therefore be skipped by the debugger and in stack traces.
+ closure_function.set_is_debuggable(false);
+ closure_function.set_is_visible(false);
+
+ // Set closure function's formal parameters to this formal parameters,
+ // removing the first parameter over which the currying is done, and adding
+ // the closure class instance as the first parameter. So, the overall number
+ // of fixed parameters doesn't change.
+ const int num_fixed_params = num_fixed_parameters();
+ const int num_opt_params = NumOptionalParameters();
+ const bool has_opt_pos_params = HasOptionalPositionalParameters();
+ const int num_params = num_fixed_params + num_opt_params;
+ closure_function.set_num_fixed_parameters(num_fixed_params);
+ closure_function.SetNumOptionalParameters(num_opt_params, has_opt_pos_params);
+ closure_function.set_parameter_types(
+ Array::Handle(zone, Array::New(num_params, Heap::kOld)));
+ closure_function.set_parameter_names(
+ Array::Handle(zone, Array::New(num_params, Heap::kOld)));
+ AbstractType& param_type = AbstractType::Handle(zone);
+ String& param_name = String::Handle(zone);
+ // Add implicit closure object as the first parameter.
+ param_type = Type::DynamicType();
+ closure_function.SetParameterTypeAt(0, param_type);
+ closure_function.SetParameterNameAt(0, Symbols::ClosureParameter());
+ // All the parameters, but the first one, are the same for the top-level
+ // function being converted, and the method of the closure class that is being
+ // generated.
+ for (int i = 1; i < num_params; i++) {
+ param_type = ParameterTypeAt(i);
+ closure_function.SetParameterTypeAt(i, param_type);
+ param_name = ParameterNameAt(i);
+ closure_function.SetParameterNameAt(i, param_name);
+ }
+ closure_function.set_kernel_function(kernel_function());
+
+ const Type& signature_type =
+ Type::Handle(zone, closure_function.SignatureType());
+ if (!signature_type.IsFinalized()) {
+ ClassFinalizer::FinalizeType(Class::Handle(zone, Owner()), signature_type);
+ }
+ set_converted_closure_function(closure_function);
+ return closure_function.raw();
+}
+
+
+void Function::DropUncompiledConvertedClosureFunction() const {
+ if (converted_closure_function() != Function::null()) {
+ const Function& func = Function::Handle(converted_closure_function());
+ if (!func.HasCode()) {
+ set_converted_closure_function(Function::Handle());
+ }
+ }
+}
+
+
RawString* Function::UserVisibleFormalParameters() const {
Thread* thread = Thread::Current();
Zone* zone = thread->zone();
@@ -7508,6 +7688,7 @@ const char* Function::ToCString() const {
switch (kind()) {
case RawFunction::kRegularFunction:
case RawFunction::kClosureFunction:
+ case RawFunction::kConvertedClosureFunction:
case RawFunction::kGetterFunction:
case RawFunction::kSetterFunction:
kind_str = "";
« runtime/vm/kernel_to_il.cc ('K') | « runtime/vm/object.h ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698