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

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

Issue 1274133002: Don't zone-register async callbacks for every await call in the VM. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Move async-classes to async patch. Address comments. Created 5 years, 4 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/parser.h" 5 #include "vm/parser.h"
6 6
7 #include "lib/invocation_mirror.h" 7 #include "lib/invocation_mirror.h"
8 #include "platform/utils.h" 8 #include "platform/utils.h"
9 #include "vm/ast_transformer.h" 9 #include "vm/ast_transformer.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 6724 matching lines...) Expand 10 before | Expand all | Expand 10 after
6735 Scanner::kNoSourcePos, Symbols::AwaitContextVar(), dynamic_type); 6735 Scanner::kNoSourcePos, Symbols::AwaitContextVar(), dynamic_type);
6736 current_block_->scope->AddVariable(await_ctx_var); 6736 current_block_->scope->AddVariable(await_ctx_var);
6737 current_block_->scope->CaptureVariable(Symbols::AwaitContextVar()); 6737 current_block_->scope->CaptureVariable(Symbols::AwaitContextVar());
6738 await_ctx_var->set_is_captured(); 6738 await_ctx_var->set_is_captured();
6739 } 6739 }
6740 6740
6741 6741
6742 void Parser::AddAsyncClosureVariables() { 6742 void Parser::AddAsyncClosureVariables() {
6743 // Add to current block's scope: 6743 // Add to current block's scope:
6744 // var :async_op; 6744 // var :async_op;
6745 // var :async_then_callback;
6746 // var :async_catch_error_callback;
6745 // var :async_completer; 6747 // var :async_completer;
6746 const Type& dynamic_type = Type::ZoneHandle(Z, Type::DynamicType()); 6748 const Type& dynamic_type = Type::ZoneHandle(Z, Type::DynamicType());
6747 LocalVariable* async_op_var = new(Z) LocalVariable( 6749 LocalVariable* async_op_var = new(Z) LocalVariable(
6748 Scanner::kNoSourcePos, Symbols::AsyncOperation(), dynamic_type); 6750 Scanner::kNoSourcePos, Symbols::AsyncOperation(), dynamic_type);
6749 current_block_->scope->AddVariable(async_op_var); 6751 current_block_->scope->AddVariable(async_op_var);
6750 current_block_->scope->CaptureVariable(Symbols::AsyncOperation()); 6752 LocalVariable* async_then_callback_var = new(Z) LocalVariable(
6751 async_op_var->set_is_captured(); 6753 Scanner::kNoSourcePos, Symbols::AsyncThenCallback(), dynamic_type);
6754 current_block_->scope->AddVariable(async_then_callback_var);
6755 current_block_->scope->CaptureVariable(Symbols::AsyncThenCallback());
6756 async_then_callback_var->set_is_captured();
6757 LocalVariable* async_catch_error_callback_var = new(Z) LocalVariable(
6758 Scanner::kNoSourcePos, Symbols::AsyncCatchErrorCallback(), dynamic_type);
6759 current_block_->scope->AddVariable(async_catch_error_callback_var);
6760 current_block_->scope->CaptureVariable(Symbols::AsyncCatchErrorCallback());
6761 async_catch_error_callback_var->set_is_captured();
6752 LocalVariable* async_completer = new(Z) LocalVariable( 6762 LocalVariable* async_completer = new(Z) LocalVariable(
6753 Scanner::kNoSourcePos, Symbols::AsyncCompleter(), dynamic_type); 6763 Scanner::kNoSourcePos, Symbols::AsyncCompleter(), dynamic_type);
6754 current_block_->scope->AddVariable(async_completer); 6764 current_block_->scope->AddVariable(async_completer);
6755 current_block_->scope->CaptureVariable(Symbols::AsyncCompleter()); 6765 current_block_->scope->CaptureVariable(Symbols::AsyncCompleter());
6756 async_completer->set_is_captured(); 6766 async_completer->set_is_captured();
6757 } 6767 }
6758 6768
6759 6769
6760 void Parser::AddAsyncGeneratorVariables() { 6770 void Parser::AddAsyncGeneratorVariables() {
6761 // Add to current block's scope: 6771 // Add to current block's scope:
6762 // var :controller; 6772 // var :controller;
6763 // The :controller variable is used by the async generator closure to 6773 // The :controller variable is used by the async generator closure to
6764 // store the StreamController object to which the yielded expressions 6774 // store the StreamController object to which the yielded expressions
6765 // are added. 6775 // are added.
6766 // var :async_op; 6776 // var :async_op;
6767 // This variable is used to store the async generator closure containing 6777 // var :async_then_callback;
6768 // the body of the async* function. It is used by the await operator. 6778 // var :async_catch_error_callback;
6779 // These variables are used to store the async generator closure containing
6780 // the body of the async* function. They are used by the await operator.
6769 const Type& dynamic_type = Type::ZoneHandle(Z, Type::DynamicType()); 6781 const Type& dynamic_type = Type::ZoneHandle(Z, Type::DynamicType());
6770 LocalVariable* controller_var = new(Z) LocalVariable( 6782 LocalVariable* controller_var = new(Z) LocalVariable(
6771 Scanner::kNoSourcePos, Symbols::Controller(), dynamic_type); 6783 Scanner::kNoSourcePos, Symbols::Controller(), dynamic_type);
6772 current_block_->scope->AddVariable(controller_var); 6784 current_block_->scope->AddVariable(controller_var);
6773 current_block_->scope->CaptureVariable(Symbols::Controller()); 6785 current_block_->scope->CaptureVariable(Symbols::Controller());
6774 controller_var->set_is_captured(); 6786 controller_var->set_is_captured();
6775 6787
6776 LocalVariable* async_op_var = new(Z) LocalVariable( 6788 LocalVariable* async_op_var = new(Z) LocalVariable(
6777 Scanner::kNoSourcePos, Symbols::AsyncOperation(), dynamic_type); 6789 Scanner::kNoSourcePos, Symbols::AsyncOperation(), dynamic_type);
6778 current_block_->scope->AddVariable(async_op_var); 6790 current_block_->scope->AddVariable(async_op_var);
6779 current_block_->scope->CaptureVariable(Symbols::AsyncOperation()); 6791 LocalVariable* async_then_callback_var = new(Z) LocalVariable(
6780 async_op_var->set_is_captured(); 6792 Scanner::kNoSourcePos, Symbols::AsyncThenCallback(), dynamic_type);
6793 current_block_->scope->AddVariable(async_then_callback_var);
6794 current_block_->scope->CaptureVariable(Symbols::AsyncThenCallback());
6795 async_then_callback_var->set_is_captured();
6796 LocalVariable* async_catch_error_callback_var = new(Z) LocalVariable(
6797 Scanner::kNoSourcePos, Symbols::AsyncCatchErrorCallback(), dynamic_type);
6798 current_block_->scope->AddVariable(async_catch_error_callback_var);
6799 current_block_->scope->CaptureVariable(Symbols::AsyncCatchErrorCallback());
6800 async_catch_error_callback_var->set_is_captured();
6781 } 6801 }
6782 6802
6783 6803
6784 RawFunction* Parser::OpenAsyncGeneratorFunction(intptr_t async_func_pos) { 6804 RawFunction* Parser::OpenAsyncGeneratorFunction(intptr_t async_func_pos) {
6785 TRACE_PARSER("OpenAsyncGeneratorFunction"); 6805 TRACE_PARSER("OpenAsyncGeneratorFunction");
6786 AddContinuationVariables(); 6806 AddContinuationVariables();
6787 AddAsyncGeneratorVariables(); 6807 AddAsyncGeneratorVariables();
6788 6808
6789 Function& closure = Function::Handle(Z); 6809 Function& closure = Function::Handle(Z);
6790 bool is_new_closure = false; 6810 bool is_new_closure = false;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
6852 // Generate the Ast nodes for the implicit code of the async* function. 6872 // Generate the Ast nodes for the implicit code of the async* function.
6853 // 6873 //
6854 // f(...) async* { 6874 // f(...) async* {
6855 // var :controller; 6875 // var :controller;
6856 // var :await_jump_var = -1; 6876 // var :await_jump_var = -1;
6857 // var :await_context_var; 6877 // var :await_context_var;
6858 // f_async_body() { 6878 // f_async_body() {
6859 // ... source code of f ... 6879 // ... source code of f ...
6860 // } 6880 // }
6861 // var :async_op = f_async_body; 6881 // var :async_op = f_async_body;
6882 // var :async_then_callback = _asyncThenWrapperHelper(:async_op);
6883 // var :async_catch_error_callback = _asyncCatchErrorWrapperHelper(:async_op);
6862 // :controller = new _AsyncStarStreamController(:async_op); 6884 // :controller = new _AsyncStarStreamController(:async_op);
6863 // return :controller.stream; 6885 // return :controller.stream;
6864 // } 6886 // }
6865 SequenceNode* Parser::CloseAsyncGeneratorFunction(const Function& closure_func, 6887 SequenceNode* Parser::CloseAsyncGeneratorFunction(const Function& closure_func,
6866 SequenceNode* closure_body) { 6888 SequenceNode* closure_body) {
6867 TRACE_PARSER("CloseAsyncGeneratorFunction"); 6889 TRACE_PARSER("CloseAsyncGeneratorFunction");
6868 ASSERT(!closure_func.IsNull()); 6890 ASSERT(!closure_func.IsNull());
6869 ASSERT(closure_body != NULL); 6891 ASSERT(closure_body != NULL);
6870 6892
6871 // The block for the async closure body has already been closed. Close the 6893 // The block for the async closure body has already been closed. Close the
6872 // corresponding function block. 6894 // corresponding function block.
6873 CloseBlock(); 6895 CloseBlock();
6874 6896
6875 // Make sure the implicit variables of the async generator function 6897 // Make sure the implicit variables of the async generator function
6876 // are captured. 6898 // are captured.
6877 closure_body->scope()->LookupVariable(Symbols::AwaitJumpVar(), false); 6899 closure_body->scope()->LookupVariable(Symbols::AwaitJumpVar(), false);
6878 closure_body->scope()->LookupVariable(Symbols::AwaitContextVar(), false); 6900 closure_body->scope()->LookupVariable(Symbols::AwaitContextVar(), false);
6879 closure_body->scope()->LookupVariable(Symbols::Controller(), false); 6901 closure_body->scope()->LookupVariable(Symbols::Controller(), false);
6880 closure_body->scope()->LookupVariable(Symbols::AsyncOperation(), false); 6902 closure_body->scope()->LookupVariable(Symbols::AsyncOperation(), false);
6903 closure_body->scope()->LookupVariable(Symbols::AsyncThenCallback(), false);
6904 closure_body->scope()->LookupVariable(
6905 Symbols::AsyncCatchErrorCallback(), false);
6906
6907 const Library& async_lib = Library::Handle(Library::AsyncLibrary());
6881 6908
6882 const Class& controller_class = Class::Handle(Z, 6909 const Class& controller_class = Class::Handle(Z,
6883 Library::LookupCoreClass(Symbols::_AsyncStarStreamController())); 6910 async_lib.LookupClassAllowPrivate(
6911 Symbols::_AsyncStarStreamController()));
6884 ASSERT(!controller_class.IsNull()); 6912 ASSERT(!controller_class.IsNull());
6885 const Function& controller_constructor = Function::ZoneHandle(Z, 6913 const Function& controller_constructor = Function::ZoneHandle(Z,
6886 controller_class.LookupConstructorAllowPrivate( 6914 controller_class.LookupConstructorAllowPrivate(
6887 Symbols::_AsyncStarStreamControllerConstructor())); 6915 Symbols::_AsyncStarStreamControllerConstructor()));
6888 6916
6889 // :await_jump_var = -1; 6917 // :await_jump_var = -1;
6890 LocalVariable* jump_var = 6918 LocalVariable* jump_var =
6891 current_block_->scope->LookupVariable(Symbols::AwaitJumpVar(), false); 6919 current_block_->scope->LookupVariable(Symbols::AwaitJumpVar(), false);
6892 LiteralNode* init_value = 6920 LiteralNode* init_value =
6893 new(Z) LiteralNode(Scanner::kNoSourcePos, Smi::ZoneHandle(Smi::New(-1))); 6921 new(Z) LiteralNode(Scanner::kNoSourcePos, Smi::ZoneHandle(Smi::New(-1)));
6894 current_block_->statements->Add( 6922 current_block_->statements->Add(
6895 new(Z) StoreLocalNode(Scanner::kNoSourcePos, jump_var, init_value)); 6923 new(Z) StoreLocalNode(Scanner::kNoSourcePos, jump_var, init_value));
6896 6924
6897 // Add to AST: 6925 // Add to AST:
6898 // :async_op = <closure>; (containing the original body) 6926 // :async_op = <closure>; (containing the original body)
6899 LocalVariable* async_op_var = 6927 LocalVariable* async_op_var =
6900 current_block_->scope->LookupVariable(Symbols::AsyncOperation(), false); 6928 current_block_->scope->LookupVariable(Symbols::AsyncOperation(), false);
6901 ClosureNode* closure_obj = new(Z) ClosureNode( 6929 ClosureNode* closure_obj = new(Z) ClosureNode(
6902 Scanner::kNoSourcePos, closure_func, NULL, closure_body->scope()); 6930 Scanner::kNoSourcePos, closure_func, NULL, closure_body->scope());
6903 StoreLocalNode* store_async_op = new (Z) StoreLocalNode( 6931 StoreLocalNode* store_async_op = new (Z) StoreLocalNode(
6904 Scanner::kNoSourcePos, 6932 Scanner::kNoSourcePos,
6905 async_op_var, 6933 async_op_var,
6906 closure_obj); 6934 closure_obj);
6935
6907 current_block_->statements->Add(store_async_op); 6936 current_block_->statements->Add(store_async_op);
6908 6937
6938 // :async_then_callback = _asyncThenWrapperHelper(:async_op)
6939 const Function& async_then_wrapper_helper = Function::ZoneHandle(
6940 Z, async_lib.LookupFunctionAllowPrivate(
6941 Symbols::AsyncThenWrapperHelper()));
6942 ASSERT(!async_then_wrapper_helper.IsNull());
6943 ArgumentListNode* async_then_wrapper_helper_args = new (Z) ArgumentListNode(
6944 Scanner::kNoSourcePos);
6945 async_then_wrapper_helper_args->Add(
6946 new (Z) LoadLocalNode(Scanner::kNoSourcePos, async_op_var));
6947 StaticCallNode* then_wrapper_call = new (Z) StaticCallNode(
6948 Scanner::kNoSourcePos,
6949 async_then_wrapper_helper,
6950 async_then_wrapper_helper_args);
6951 LocalVariable* async_then_callback_var =
6952 current_block_->scope->LookupVariable(
6953 Symbols::AsyncThenCallback(), false);
6954 StoreLocalNode* store_async_then_callback = new (Z) StoreLocalNode(
6955 Scanner::kNoSourcePos,
6956 async_then_callback_var,
6957 then_wrapper_call);
6958
6959 current_block_->statements->Add(store_async_then_callback);
6960
6961 // :async_catch_error_callback = _asyncErrorWrapperHelper(:async_op)
6962
6963 const Function& async_error_wrapper_helper = Function::ZoneHandle(
6964 Z, async_lib.LookupFunctionAllowPrivate(
6965 Symbols::AsyncErrorWrapperHelper()));
6966 ASSERT(!async_error_wrapper_helper.IsNull());
6967 ArgumentListNode* async_error_wrapper_helper_args = new (Z) ArgumentListNode(
6968 Scanner::kNoSourcePos);
6969 async_error_wrapper_helper_args->Add(
6970 new (Z) LoadLocalNode(Scanner::kNoSourcePos, async_op_var));
6971 StaticCallNode* error_wrapper_call = new (Z) StaticCallNode(
6972 Scanner::kNoSourcePos,
6973 async_error_wrapper_helper,
6974 async_error_wrapper_helper_args);
6975 LocalVariable* async_catch_error_callback_var =
6976 current_block_->scope->LookupVariable(
6977 Symbols::AsyncCatchErrorCallback(), false);
6978 StoreLocalNode* store_async_catch_error_callback = new (Z) StoreLocalNode(
6979 Scanner::kNoSourcePos,
6980 async_catch_error_callback_var,
6981 error_wrapper_call);
6982
6983 current_block_->statements->Add(store_async_catch_error_callback);
6984
6909 // :controller = new _AsyncStarStreamController(body_closure); 6985 // :controller = new _AsyncStarStreamController(body_closure);
6910 ArgumentListNode* arguments = new(Z) ArgumentListNode(Scanner::kNoSourcePos); 6986 ArgumentListNode* arguments = new(Z) ArgumentListNode(Scanner::kNoSourcePos);
6911 arguments->Add(new (Z) LoadLocalNode(Scanner::kNoSourcePos, async_op_var)); 6987 arguments->Add(new (Z) LoadLocalNode(Scanner::kNoSourcePos, async_op_var));
6912 ConstructorCallNode* controller_constructor_call = 6988 ConstructorCallNode* controller_constructor_call =
6913 new(Z) ConstructorCallNode(Scanner::kNoSourcePos, 6989 new(Z) ConstructorCallNode(Scanner::kNoSourcePos,
6914 TypeArguments::ZoneHandle(Z), 6990 TypeArguments::ZoneHandle(Z),
6915 controller_constructor, 6991 controller_constructor,
6916 arguments); 6992 arguments);
6917 LocalVariable* controller_var = 6993 LocalVariable* controller_var =
6918 current_block_->scope->LookupVariable(Symbols::Controller(), false); 6994 current_block_->scope->LookupVariable(Symbols::Controller(), false);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
7055 LocalVariable* async_op_var = current_block_->scope->LookupVariable( 7131 LocalVariable* async_op_var = current_block_->scope->LookupVariable(
7056 Symbols::AsyncOperation(), false); 7132 Symbols::AsyncOperation(), false);
7057 ClosureNode* cn = new(Z) ClosureNode( 7133 ClosureNode* cn = new(Z) ClosureNode(
7058 Scanner::kNoSourcePos, closure, NULL, closure_body->scope()); 7134 Scanner::kNoSourcePos, closure, NULL, closure_body->scope());
7059 StoreLocalNode* store_async_op = new (Z) StoreLocalNode( 7135 StoreLocalNode* store_async_op = new (Z) StoreLocalNode(
7060 Scanner::kNoSourcePos, 7136 Scanner::kNoSourcePos,
7061 async_op_var, 7137 async_op_var,
7062 cn); 7138 cn);
7063 current_block_->statements->Add(store_async_op); 7139 current_block_->statements->Add(store_async_op);
7064 7140
7141 const Library& async_lib = Library::Handle(Library::AsyncLibrary());
7142 // :async_then_callback = _asyncThenWrapperHelper(:async_op)
7143 const Function& async_then_wrapper_helper = Function::ZoneHandle(
7144 Z, async_lib.LookupFunctionAllowPrivate(
7145 Symbols::AsyncThenWrapperHelper()));
7146 ASSERT(!async_then_wrapper_helper.IsNull());
7147 ArgumentListNode* async_then_wrapper_helper_args = new (Z) ArgumentListNode(
7148 Scanner::kNoSourcePos);
7149 async_then_wrapper_helper_args->Add(
7150 new (Z) LoadLocalNode(Scanner::kNoSourcePos, async_op_var));
7151 StaticCallNode* then_wrapper_call = new (Z) StaticCallNode(
7152 Scanner::kNoSourcePos,
7153 async_then_wrapper_helper,
7154 async_then_wrapper_helper_args);
7155 LocalVariable* async_then_callback_var =
7156 current_block_->scope->LookupVariable(
7157 Symbols::AsyncThenCallback(), false);
7158 StoreLocalNode* store_async_then_callback = new (Z) StoreLocalNode(
7159 Scanner::kNoSourcePos,
7160 async_then_callback_var,
7161 then_wrapper_call);
7162
7163 current_block_->statements->Add(store_async_then_callback);
7164
7165 // :async_catch_error_callback = _asyncErrorWrapperHelper(:async_op)
7166
7167 const Function& async_error_wrapper_helper = Function::ZoneHandle(
7168 Z, async_lib.LookupFunctionAllowPrivate(
7169 Symbols::AsyncErrorWrapperHelper()));
7170 ASSERT(!async_error_wrapper_helper.IsNull());
7171 ArgumentListNode* async_error_wrapper_helper_args = new (Z) ArgumentListNode(
7172 Scanner::kNoSourcePos);
7173 async_error_wrapper_helper_args->Add(
7174 new (Z) LoadLocalNode(Scanner::kNoSourcePos, async_op_var));
7175 StaticCallNode* error_wrapper_call = new (Z) StaticCallNode(
7176 Scanner::kNoSourcePos,
7177 async_error_wrapper_helper,
7178 async_error_wrapper_helper_args);
7179 LocalVariable* async_catch_error_callback_var =
7180 current_block_->scope->LookupVariable(
7181 Symbols::AsyncCatchErrorCallback(), false);
7182 StoreLocalNode* store_async_catch_error_callback = new (Z) StoreLocalNode(
7183 Scanner::kNoSourcePos,
7184 async_catch_error_callback_var,
7185 error_wrapper_call);
7186
7187 current_block_->statements->Add(store_async_catch_error_callback);
7188
7065 // Add to AST: 7189 // Add to AST:
7066 // new Future.microtask(:async_op); 7190 // new Future.microtask(:async_op);
7067 ArgumentListNode* arguments = new (Z) ArgumentListNode(Scanner::kNoSourcePos); 7191 ArgumentListNode* arguments = new (Z) ArgumentListNode(Scanner::kNoSourcePos);
7068 arguments->Add(new (Z) LoadLocalNode( 7192 arguments->Add(new (Z) LoadLocalNode(
7069 Scanner::kNoSourcePos, async_op_var)); 7193 Scanner::kNoSourcePos, async_op_var));
7070 ConstructorCallNode* future_node = new (Z) ConstructorCallNode( 7194 ConstructorCallNode* future_node = new (Z) ConstructorCallNode(
7071 Scanner::kNoSourcePos, TypeArguments::ZoneHandle(Z), constructor, 7195 Scanner::kNoSourcePos, TypeArguments::ZoneHandle(Z), constructor,
7072 arguments); 7196 arguments);
7073 current_block_->statements->Add(future_node); 7197 current_block_->statements->Add(future_node);
7074 7198
(...skipping 6989 matching lines...) Expand 10 before | Expand all | Expand 10 after
14064 void Parser::SkipQualIdent() { 14188 void Parser::SkipQualIdent() {
14065 ASSERT(IsIdentifier()); 14189 ASSERT(IsIdentifier());
14066 ConsumeToken(); 14190 ConsumeToken();
14067 if (CurrentToken() == Token::kPERIOD) { 14191 if (CurrentToken() == Token::kPERIOD) {
14068 ConsumeToken(); // Consume the kPERIOD token. 14192 ConsumeToken(); // Consume the kPERIOD token.
14069 ExpectIdentifier("identifier expected after '.'"); 14193 ExpectIdentifier("identifier expected after '.'");
14070 } 14194 }
14071 } 14195 }
14072 14196
14073 } // namespace dart 14197 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698