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

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

Issue 634603002: Await always waits (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 2 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 | Annotate | Revision Log
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/parser.h" 8 #include "vm/parser.h"
8 9
9 namespace dart { 10 namespace dart {
10 11
11 // Quick access to the locally defined isolate() method. 12 // Quick access to the locally defined isolate() method.
12 #define I (isolate()) 13 #define I (isolate())
13 14
14 // Nodes that are unreachable from already parsed expressions. 15 // Nodes that are unreachable from already parsed expressions.
15 #define FOR_EACH_UNREACHABLE_NODE(V) \ 16 #define FOR_EACH_UNREACHABLE_NODE(V) \
16 V(AwaitMarker) \ 17 V(AwaitMarker) \
(...skipping 19 matching lines...) Expand all
36 37
37 #define DEFINE_UNREACHABLE(BaseName) \ 38 #define DEFINE_UNREACHABLE(BaseName) \
38 void AwaitTransformer::Visit##BaseName##Node(BaseName##Node* node) { \ 39 void AwaitTransformer::Visit##BaseName##Node(BaseName##Node* node) { \
39 UNREACHABLE(); \ 40 UNREACHABLE(); \
40 } 41 }
41 42
42 FOR_EACH_UNREACHABLE_NODE(DEFINE_UNREACHABLE) 43 FOR_EACH_UNREACHABLE_NODE(DEFINE_UNREACHABLE)
43 #undef DEFINE_UNREACHABLE 44 #undef DEFINE_UNREACHABLE
44 45
45 AwaitTransformer::AwaitTransformer(SequenceNode* preamble, 46 AwaitTransformer::AwaitTransformer(SequenceNode* preamble,
46 const Library& library,
47 ParsedFunction* const parsed_function, 47 ParsedFunction* const parsed_function,
48 LocalScope* function_top) 48 LocalScope* function_top)
49 : preamble_(preamble), 49 : preamble_(preamble),
50 temp_cnt_(0), 50 temp_cnt_(0),
51 library_(library),
52 parsed_function_(parsed_function), 51 parsed_function_(parsed_function),
53 function_top_(function_top), 52 function_top_(function_top),
54 isolate_(Isolate::Current()) { 53 isolate_(Isolate::Current()) {
55 ASSERT(function_top_ != NULL); 54 ASSERT(function_top_ != NULL);
56 } 55 }
57 56
58 57
59 AstNode* AwaitTransformer::Transform(AstNode* expr) { 58 AstNode* AwaitTransformer::Transform(AstNode* expr) {
60 expr->Visit(this); 59 expr->Visit(this);
61 return result_; 60 return result_;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 void AwaitTransformer::VisitTypeNode(TypeNode* node) { 106 void AwaitTransformer::VisitTypeNode(TypeNode* node) {
108 result_ = new(I) TypeNode(node->token_pos(), node->type()); 107 result_ = new(I) TypeNode(node->token_pos(), node->type());
109 } 108 }
110 109
111 110
112 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) { 111 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) {
113 // Await transformation: 112 // Await transformation:
114 // 113 //
115 // :await_temp_var_X = <expr>; 114 // :await_temp_var_X = <expr>;
116 // :result_param = :await_temp_var_X; 115 // :result_param = :await_temp_var_X;
117 // if (:result_param is Future) { 116 // if (:result_param is !Future) {
118 // AwaitMarker(kNewContinuationState); 117 // :result_param = Future.value(:result_param);
119 // :result_param = :result_param.then(:async_op);
120 // _asyncCatchHelper(:result_param.catchError, :async_op);
121 // return; // (return_type() == kContinuation)
122 // } 118 // }
119 // AwaitMarker(kNewContinuationState);
120 // :result_param = :result_param.then(:async_op);
121 // _asyncCatchHelper(:result_param.catchError, :async_op);
122 // return; // (return_type() == kContinuation)
123 //
123 // AwaitMarker(kTargetForContinuation); // Join happens here. 124 // AwaitMarker(kTargetForContinuation); // Join happens here.
124 // :saved_try_ctx_var = :await_saved_try_ctx_var_y; 125 // :saved_try_ctx_var = :await_saved_try_ctx_var_y;
125 // :await_temp_var_(X+1) = :result_param; 126 // :await_temp_var_(X+1) = :result_param;
126 127
127 LocalVariable* async_op = GetVariableInScope( 128 LocalVariable* async_op = GetVariableInScope(
128 preamble_->scope(), Symbols::AsyncOperation()); 129 preamble_->scope(), Symbols::AsyncOperation());
129 LocalVariable* result_param = GetVariableInScope( 130 LocalVariable* result_param = GetVariableInScope(
130 preamble_->scope(), Symbols::AsyncOperationParam()); 131 preamble_->scope(), Symbols::AsyncOperationParam());
131 LocalVariable* error_param = GetVariableInScope( 132 LocalVariable* error_param = GetVariableInScope(
132 preamble_->scope(), Symbols::AsyncOperationErrorParam()); 133 preamble_->scope(), Symbols::AsyncOperationErrorParam());
133 134
134 node->expr()->Visit(this); 135 AstNode* transformed_expr = Transform(node->expr());
135 preamble_->Add(new(I) StoreLocalNode( 136 preamble_->Add(new(I) StoreLocalNode(
136 Scanner::kNoSourcePos, result_param, result_)); 137 Scanner::kNoSourcePos, result_param, transformed_expr));
138
137 LoadLocalNode* load_result_param = new(I) LoadLocalNode( 139 LoadLocalNode* load_result_param = new(I) LoadLocalNode(
138 Scanner::kNoSourcePos, result_param); 140 Scanner::kNoSourcePos, result_param);
139 LocalScope* is_future_scope = ChainNewScope(preamble_->scope()); 141
140 SequenceNode* is_future_branch = new (I) SequenceNode( 142 const Class& future_cls =
141 Scanner::kNoSourcePos, is_future_scope); 143 Class::ZoneHandle(I, I->object_store()->future_class());
144 ASSERT(!future_cls.IsNull());
145 const AbstractType& future_type =
146 AbstractType::ZoneHandle(I, future_cls.RareType());
147 ASSERT(!future_type.IsNull());
148
149 LocalScope* is_not_future_scope = ChainNewScope(preamble_->scope());
150 SequenceNode* is_not_future_branch =
151 new (I) SequenceNode(Scanner::kNoSourcePos, is_not_future_scope);
152
153 // if (:result_param is !Future) {
154 // :result_param = Future.value(:result_param);
155 // }
156 const Function& value_ctor = Function::ZoneHandle(
157 I, future_cls.LookupFunction(Symbols::FutureValue()));
158 ASSERT(!value_ctor.IsNull());
159 ArgumentListNode* ctor_args = new (I) ArgumentListNode(Scanner::kNoSourcePos);
160 ctor_args->Add(new (I) LoadLocalNode(Scanner::kNoSourcePos, result_param));
161 ConstructorCallNode* ctor_call =
162 new (I) ConstructorCallNode(Scanner::kNoSourcePos,
163 TypeArguments::ZoneHandle(I),
164 value_ctor,
165 ctor_args);
166 is_not_future_branch->Add(new (I) StoreLocalNode(
167 Scanner::kNoSourcePos, result_param, ctor_call));
168 AstNode* is_not_future_test = new (I) ComparisonNode(
169 Scanner::kNoSourcePos,
170 Token::kISNOT,
171 load_result_param,
172 new (I) TypeNode(Scanner::kNoSourcePos, future_type));
173 preamble_->Add(new(I) IfNode(Scanner::kNoSourcePos,
174 is_not_future_test,
175 is_not_future_branch,
176 NULL));
177
142 AwaitMarkerNode* await_marker = new (I) AwaitMarkerNode( 178 AwaitMarkerNode* await_marker = new (I) AwaitMarkerNode(
143 AwaitMarkerNode::kNewContinuationState); 179 AwaitMarkerNode::kNewContinuationState);
144 await_marker->set_scope(is_future_scope); 180 await_marker->set_scope(preamble_->scope());
145 GetVariableInScope(is_future_scope, Symbols::AwaitJumpVar()); 181 GetVariableInScope(preamble_->scope(), Symbols::AwaitJumpVar());
146 GetVariableInScope(is_future_scope, Symbols::AwaitContextVar()); 182 GetVariableInScope(preamble_->scope(), Symbols::AwaitContextVar());
147 is_future_branch->Add(await_marker); 183 preamble_->Add(await_marker);
148 ArgumentListNode* args = new(I) ArgumentListNode(Scanner::kNoSourcePos); 184 ArgumentListNode* args = new(I) ArgumentListNode(Scanner::kNoSourcePos);
185
149 args->Add(new(I) LoadLocalNode(Scanner::kNoSourcePos, async_op)); 186 args->Add(new(I) LoadLocalNode(Scanner::kNoSourcePos, async_op));
150 is_future_branch->Add(new (I) StoreLocalNode( 187 preamble_->Add(new (I) StoreLocalNode(
151 Scanner::kNoSourcePos, 188 Scanner::kNoSourcePos,
152 result_param, 189 result_param,
153 new(I) InstanceCallNode( 190 new(I) InstanceCallNode(Scanner::kNoSourcePos,
154 Scanner::kNoSourcePos, 191 load_result_param,
155 load_result_param, 192 Symbols::FutureThen(),
156 Symbols::FutureThen(), 193 args)));
157 args)));
158 const Library& core_lib = Library::Handle(Library::CoreLibrary()); 194 const Library& core_lib = Library::Handle(Library::CoreLibrary());
159 const Function& async_catch_helper = Function::ZoneHandle( 195 const Function& async_catch_helper = Function::ZoneHandle(
160 I, core_lib.LookupFunctionAllowPrivate(Symbols::AsyncCatchHelper())); 196 I, core_lib.LookupFunctionAllowPrivate(Symbols::AsyncCatchHelper()));
161 ASSERT(!async_catch_helper.IsNull()); 197 ASSERT(!async_catch_helper.IsNull());
162 ArgumentListNode* catch_helper_args = new (I) ArgumentListNode( 198 ArgumentListNode* catch_helper_args = new (I) ArgumentListNode(
163 Scanner::kNoSourcePos); 199 Scanner::kNoSourcePos);
164 InstanceGetterNode* catch_error_getter = new (I) InstanceGetterNode( 200 InstanceGetterNode* catch_error_getter = new (I) InstanceGetterNode(
165 Scanner::kNoSourcePos, 201 Scanner::kNoSourcePos,
166 load_result_param, 202 load_result_param,
167 Symbols::FutureCatchError()); 203 Symbols::FutureCatchError());
168 catch_helper_args->Add(catch_error_getter); 204 catch_helper_args->Add(catch_error_getter);
169 catch_helper_args->Add(new (I) LoadLocalNode( 205 catch_helper_args->Add(new (I) LoadLocalNode(
170 Scanner::kNoSourcePos, async_op)); 206 Scanner::kNoSourcePos, async_op));
171 is_future_branch->Add(new (I) StaticCallNode( 207 preamble_->Add(new (I) StaticCallNode(
172 Scanner::kNoSourcePos, 208 Scanner::kNoSourcePos,
173 async_catch_helper, 209 async_catch_helper,
174 catch_helper_args)); 210 catch_helper_args));
175 ReturnNode* continuation_return = new(I) ReturnNode(Scanner::kNoSourcePos); 211 ReturnNode* continuation_return = new(I) ReturnNode(Scanner::kNoSourcePos);
176 continuation_return->set_return_type(ReturnNode::kContinuation); 212 continuation_return->set_return_type(ReturnNode::kContinuation);
177 is_future_branch->Add(continuation_return); 213 preamble_->Add(continuation_return);
178 214 preamble_->Add(
179 const Class& cls = Class::ZoneHandle( 215 new (I) AwaitMarkerNode(AwaitMarkerNode::kTargetForContinuation));
180 I, library_.LookupClass(Symbols::Future()));
181 const AbstractType& future_type = AbstractType::ZoneHandle(I, cls.RareType());
182 ASSERT(!future_type.IsNull());
183 preamble_->Add(new(I) IfNode(
184 Scanner::kNoSourcePos,
185 new (I) ComparisonNode(
186 Scanner::kNoSourcePos,
187 Token::kIS,
188 load_result_param,
189 new (I) TypeNode(Scanner::kNoSourcePos, future_type)),
190 is_future_branch,
191 NULL));
192 preamble_->Add(new (I) AwaitMarkerNode(
193 AwaitMarkerNode::kTargetForContinuation));
194 216
195 // If this expression is part of a try block, also append the code for 217 // If this expression is part of a try block, also append the code for
196 // restoring the saved try context that lives on the stack. 218 // restoring the saved try context that lives on the stack.
197 const String& async_saved_try_ctx_name = 219 const String& async_saved_try_ctx_name =
198 String::Handle(I, parsed_function_->async_saved_try_ctx_name()); 220 String::Handle(I, parsed_function_->async_saved_try_ctx_name());
199 if (!async_saved_try_ctx_name.IsNull()) { 221 if (!async_saved_try_ctx_name.IsNull()) {
200 LocalVariable* async_saved_try_ctx = 222 LocalVariable* async_saved_try_ctx =
201 GetVariableInScope(preamble_->scope(), async_saved_try_ctx_name); 223 GetVariableInScope(preamble_->scope(), async_saved_try_ctx_name);
202 preamble_->Add(new (I) StoreLocalNode( 224 preamble_->Add(new (I) StoreLocalNode(
203 Scanner::kNoSourcePos, 225 Scanner::kNoSourcePos,
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 void AwaitTransformer::VisitThrowNode(ThrowNode* node) { 606 void AwaitTransformer::VisitThrowNode(ThrowNode* node) {
585 // TODO(mlippautz): Check if relevant. 607 // TODO(mlippautz): Check if relevant.
586 AstNode* new_exception = Transform(node->exception()); 608 AstNode* new_exception = Transform(node->exception());
587 AstNode* new_stacktrace = Transform(node->stacktrace()); 609 AstNode* new_stacktrace = Transform(node->stacktrace());
588 result_ = new(I) ThrowNode(node->token_pos(), 610 result_ = new(I) ThrowNode(node->token_pos(),
589 new_exception, 611 new_exception,
590 new_stacktrace); 612 new_stacktrace);
591 } 613 }
592 614
593 } // namespace dart 615 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698