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

Side by Side Diff: runtime/vm/ast_transformer.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: load async-op var. 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
« no previous file with comments | « runtime/lib/core_patch.dart ('k') | runtime/vm/parser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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/ast_transformer.h" 5 #include "vm/ast_transformer.h"
6 6
7 #include "vm/object_store.h" 7 #include "vm/object_store.h"
8 #include "vm/parser.h" 8 #include "vm/parser.h"
9 #include "vm/thread.h" 9 #include "vm/thread.h"
10 10
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 105
106 void AwaitTransformer::VisitTypeNode(TypeNode* node) { 106 void AwaitTransformer::VisitTypeNode(TypeNode* node) {
107 result_ = new(Z) TypeNode(node->token_pos(), node->type()); 107 result_ = new(Z) TypeNode(node->token_pos(), node->type());
108 } 108 }
109 109
110 110
111 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) { 111 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) {
112 // Await transformation: 112 // Await transformation:
113 // 113 //
114 // :await_temp_var_X = <expr>; 114 // :await_temp_var_X = <expr>;
115 // :result_param = :await_temp_var_X;
116 // if (:result_param is !Future) {
117 // :result_param = Future.value(:result_param);
118 // }
119 // AwaitMarker(kNewContinuationState); 115 // AwaitMarker(kNewContinuationState);
120 // :result_param = :result_param.then(:async_op); 116 // :result_param = _awaitHelper(
121 // _asyncCatchHelper(:result_param.catchError, :async_op); 117 // :await_temp_var_X, :async_then_callback, :async_catch_error_callback);
122 // return; // (return_type() == kContinuationTarget) 118 // return; // (return_type() == kContinuationTarget)
123 // 119 //
124 // :saved_try_ctx_var = :await_saved_try_ctx_var_y; 120 // :saved_try_ctx_var = :await_saved_try_ctx_var_y;
125 // :await_temp_var_(X+1) = :result_param; 121 // :await_temp_var_(X+1) = :result_param;
126 122
127 LocalVariable* async_op = GetVariableInScope( 123 LocalVariable* async_op = GetVariableInScope(
128 preamble_->scope(), Symbols::AsyncOperation()); 124 preamble_->scope(), Symbols::AsyncOperation());
125 LocalVariable* async_then_callback = GetVariableInScope(
126 preamble_->scope(), Symbols::AsyncThenCallback());
127 LocalVariable* async_catch_error_callback = GetVariableInScope(
128 preamble_->scope(), Symbols::AsyncCatchErrorCallback());
129 LocalVariable* result_param = GetVariableInScope( 129 LocalVariable* result_param = GetVariableInScope(
130 preamble_->scope(), Symbols::AsyncOperationParam()); 130 preamble_->scope(), Symbols::AsyncOperationParam());
131 LocalVariable* error_param = GetVariableInScope( 131 LocalVariable* error_param = GetVariableInScope(
132 preamble_->scope(), Symbols::AsyncOperationErrorParam()); 132 preamble_->scope(), Symbols::AsyncOperationErrorParam());
133 LocalVariable* stack_trace_param = GetVariableInScope( 133 LocalVariable* stack_trace_param = GetVariableInScope(
134 preamble_->scope(), Symbols::AsyncOperationStackTraceParam()); 134 preamble_->scope(), Symbols::AsyncOperationStackTraceParam());
135 135
136 AstNode* transformed_expr = Transform(node->expr()); 136 AstNode* transformed_expr = Transform(node->expr());
137 preamble_->Add(new(Z) StoreLocalNode( 137 LocalVariable* await_temp = AddToPreambleNewTempVar(transformed_expr);
138 Scanner::kNoSourcePos, result_param, transformed_expr));
139
140 LoadLocalNode* load_result_param = new(Z) LoadLocalNode(
141 Scanner::kNoSourcePos, result_param);
142
143 const Class& future_cls =
144 Class::ZoneHandle(Z, thread()->isolate()->object_store()->future_class());
145 ASSERT(!future_cls.IsNull());
146 const AbstractType& future_type =
147 AbstractType::ZoneHandle(Z, future_cls.RareType());
148 ASSERT(!future_type.IsNull());
149
150 LocalScope* is_not_future_scope = ChainNewScope(preamble_->scope());
151 SequenceNode* is_not_future_branch =
152 new (Z) SequenceNode(Scanner::kNoSourcePos, is_not_future_scope);
153
154 // if (:result_param is !Future) {
155 // :result_param = Future.value(:result_param);
156 // }
157 const Function& value_ctor = Function::ZoneHandle(
158 Z, future_cls.LookupFunction(Symbols::FutureValue()));
159 ASSERT(!value_ctor.IsNull());
160 ArgumentListNode* ctor_args = new (Z) ArgumentListNode(Scanner::kNoSourcePos);
161 ctor_args->Add(new (Z) LoadLocalNode(Scanner::kNoSourcePos, result_param));
162 ConstructorCallNode* ctor_call =
163 new (Z) ConstructorCallNode(Scanner::kNoSourcePos,
164 TypeArguments::ZoneHandle(Z),
165 value_ctor,
166 ctor_args);
167 is_not_future_branch->Add(new (Z) StoreLocalNode(
168 Scanner::kNoSourcePos, result_param, ctor_call));
169 AstNode* is_not_future_test = new (Z) ComparisonNode(
170 Scanner::kNoSourcePos,
171 Token::kISNOT,
172 load_result_param,
173 new (Z) TypeNode(Scanner::kNoSourcePos, future_type));
174 preamble_->Add(new(Z) IfNode(Scanner::kNoSourcePos,
175 is_not_future_test,
176 is_not_future_branch,
177 NULL));
178 138
179 AwaitMarkerNode* await_marker = new (Z) AwaitMarkerNode(); 139 AwaitMarkerNode* await_marker = new (Z) AwaitMarkerNode();
180 await_marker->set_scope(preamble_->scope()); 140 await_marker->set_scope(preamble_->scope());
181 preamble_->Add(await_marker); 141 preamble_->Add(await_marker);
182 ArgumentListNode* args = new(Z) ArgumentListNode(Scanner::kNoSourcePos);
183 142
184 args->Add(new(Z) LoadLocalNode(Scanner::kNoSourcePos, async_op)); 143 // :result_param = _awaitHelper(
185 preamble_->Add(new (Z) StoreLocalNode( 144 // :await_temp, :async_then_callback, :async_catch_error_callback)
186 Scanner::kNoSourcePos, 145 const Library& async_lib = Library::Handle(Library::AsyncLibrary());
187 result_param, 146 const Function& async_await_helper = Function::ZoneHandle(
188 new(Z) InstanceCallNode(node->token_pos(), 147 Z, async_lib.LookupFunctionAllowPrivate(Symbols::AsyncAwaitHelper()));
189 load_result_param, 148 ASSERT(!async_await_helper.IsNull());
190 Symbols::FutureThen(), 149 ArgumentListNode* async_await_helper_args = new (Z) ArgumentListNode(
191 args)));
192 const Library& core_lib = Library::Handle(Library::CoreLibrary());
193 const Function& async_catch_helper = Function::ZoneHandle(
194 Z, core_lib.LookupFunctionAllowPrivate(Symbols::AsyncCatchHelper()));
195 ASSERT(!async_catch_helper.IsNull());
196 ArgumentListNode* catch_helper_args = new (Z) ArgumentListNode(
197 Scanner::kNoSourcePos); 150 Scanner::kNoSourcePos);
198 InstanceGetterNode* catch_error_getter = new (Z) InstanceGetterNode( 151 async_await_helper_args->Add(
199 Scanner::kNoSourcePos, 152 new(Z) LoadLocalNode(Scanner::kNoSourcePos, await_temp));
200 load_result_param, 153 async_await_helper_args->Add(
201 Symbols::FutureCatchError()); 154 new(Z) LoadLocalNode(Scanner::kNoSourcePos, async_then_callback));
202 catch_helper_args->Add(catch_error_getter); 155 async_await_helper_args->Add(
203 catch_helper_args->Add(new (Z) LoadLocalNode( 156 new(Z) LoadLocalNode(Scanner::kNoSourcePos, async_catch_error_callback));
204 Scanner::kNoSourcePos, async_op)); 157 StaticCallNode* await_helper_call = new (Z) StaticCallNode(
205 preamble_->Add(new (Z) StaticCallNode( 158 node->token_pos(),
206 Scanner::kNoSourcePos, 159 async_await_helper,
207 async_catch_helper, 160 async_await_helper_args);
208 catch_helper_args)); 161
162 preamble_->Add(new(Z) StoreLocalNode(
163 Scanner::kNoSourcePos, result_param, await_helper_call));
164
209 ReturnNode* continuation_return = new(Z) ReturnNode(Scanner::kNoSourcePos); 165 ReturnNode* continuation_return = new(Z) ReturnNode(Scanner::kNoSourcePos);
210 continuation_return->set_return_type(ReturnNode::kContinuationTarget); 166 continuation_return->set_return_type(ReturnNode::kContinuationTarget);
211 preamble_->Add(continuation_return); 167 preamble_->Add(continuation_return);
212 168
213 // If this expression is part of a try block, also append the code for 169 // If this expression is part of a try block, also append the code for
214 // restoring the saved try context that lives on the stack and possibly the 170 // restoring the saved try context that lives on the stack and possibly the
215 // saved try context of the outer try block. 171 // saved try context of the outer try block.
216 if (node->saved_try_ctx() != NULL) { 172 if (node->saved_try_ctx() != NULL) {
217 preamble_->Add(new (Z) StoreLocalNode( 173 preamble_->Add(new (Z) StoreLocalNode(
218 Scanner::kNoSourcePos, 174 Scanner::kNoSourcePos,
219 node->saved_try_ctx(), 175 node->saved_try_ctx(),
220 new (Z) LoadLocalNode(Scanner::kNoSourcePos, 176 new (Z) LoadLocalNode(Scanner::kNoSourcePos,
221 node->async_saved_try_ctx()))); 177 node->async_saved_try_ctx())));
222 if (node->outer_saved_try_ctx() != NULL) { 178 if (node->outer_saved_try_ctx() != NULL) {
223 preamble_->Add(new (Z) StoreLocalNode( 179 preamble_->Add(new (Z) StoreLocalNode(
224 Scanner::kNoSourcePos, 180 Scanner::kNoSourcePos,
225 node->outer_saved_try_ctx(), 181 node->outer_saved_try_ctx(),
226 new (Z) LoadLocalNode(Scanner::kNoSourcePos, 182 new (Z) LoadLocalNode(Scanner::kNoSourcePos,
227 node->outer_async_saved_try_ctx()))); 183 node->outer_async_saved_try_ctx())));
228 } 184 }
229 } else { 185 } else {
230 ASSERT(node->outer_saved_try_ctx() == NULL); 186 ASSERT(node->outer_saved_try_ctx() == NULL);
231 } 187 }
232 188
189 // Load the async_op variable. It is unused, but the observatory uses it
190 // to determine if a breakpoint is inside an asynchronous function.
191 LoadLocalNode* load_async_op = new (Z) LoadLocalNode(
192 Scanner::kNoSourcePos, async_op);
193 preamble_->Add(load_async_op);
194
233 LoadLocalNode* load_error_param = new (Z) LoadLocalNode( 195 LoadLocalNode* load_error_param = new (Z) LoadLocalNode(
234 Scanner::kNoSourcePos, error_param); 196 Scanner::kNoSourcePos, error_param);
235 LoadLocalNode* load_stack_trace_param = new (Z) LoadLocalNode( 197 LoadLocalNode* load_stack_trace_param = new (Z) LoadLocalNode(
236 Scanner::kNoSourcePos, stack_trace_param); 198 Scanner::kNoSourcePos, stack_trace_param);
237 SequenceNode* error_ne_null_branch = new (Z) SequenceNode( 199 SequenceNode* error_ne_null_branch = new (Z) SequenceNode(
238 Scanner::kNoSourcePos, ChainNewScope(preamble_->scope())); 200 Scanner::kNoSourcePos, ChainNewScope(preamble_->scope()));
239 error_ne_null_branch->Add(new (Z) ThrowNode( 201 error_ne_null_branch->Add(new (Z) ThrowNode(
240 Scanner::kNoSourcePos, 202 Scanner::kNoSourcePos,
241 load_error_param, 203 load_error_param,
242 load_stack_trace_param)); 204 load_stack_trace_param));
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 578
617 579
618 void AwaitTransformer::VisitThrowNode(ThrowNode* node) { 580 void AwaitTransformer::VisitThrowNode(ThrowNode* node) {
619 AstNode* new_exception = Transform(node->exception()); 581 AstNode* new_exception = Transform(node->exception());
620 result_ = new(Z) ThrowNode(node->token_pos(), 582 result_ = new(Z) ThrowNode(node->token_pos(),
621 new_exception, 583 new_exception,
622 node->stacktrace()); 584 node->stacktrace());
623 } 585 }
624 586
625 } // namespace dart 587 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/lib/core_patch.dart ('k') | runtime/vm/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698