| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/rewriter.h" | 5 #include "src/rewriter.h" |
| 6 | 6 |
| 7 #include "src/ast.h" | 7 #include "src/ast.h" |
| 8 #include "src/parser.h" | 8 #include "src/parser.h" |
| 9 #include "src/scopes.h" | 9 #include "src/scopes.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 class Processor: public AstVisitor { | 14 class Processor: public AstVisitor { |
| 15 public: | 15 public: |
| 16 Processor(Isolate* isolate, Variable* result, | 16 Processor(Isolate* isolate, Variable* result, |
| 17 AstValueFactory* ast_value_factory) | 17 AstValueFactory* ast_value_factory) |
| 18 : result_(result), | 18 : result_(result), |
| 19 result_assigned_(false), |
| 19 is_set_(false), | 20 is_set_(false), |
| 21 in_try_(false), |
| 20 factory_(ast_value_factory) { | 22 factory_(ast_value_factory) { |
| 21 InitializeAstVisitor(isolate, ast_value_factory->zone()); | 23 InitializeAstVisitor(isolate, ast_value_factory->zone()); |
| 22 } | 24 } |
| 23 | 25 |
| 24 virtual ~Processor() { } | 26 virtual ~Processor() { } |
| 25 | 27 |
| 26 void Process(ZoneList<Statement*>* statements); | 28 void Process(ZoneList<Statement*>* statements); |
| 29 bool result_assigned() const { return result_assigned_; } |
| 27 | 30 |
| 28 AstNodeFactory* factory() { return &factory_; } | 31 AstNodeFactory* factory() { return &factory_; } |
| 29 | 32 |
| 30 private: | 33 private: |
| 31 Variable* result_; | 34 Variable* result_; |
| 32 | 35 |
| 36 // We are not tracking result usage via the result_'s use |
| 37 // counts (we leave the accurate computation to the |
| 38 // usage analyzer). Instead we simple remember if |
| 39 // there was ever an assignment to result_. |
| 40 bool result_assigned_; |
| 41 |
| 33 // To avoid storing to .result all the time, we eliminate some of | 42 // To avoid storing to .result all the time, we eliminate some of |
| 34 // the stores by keeping track of whether or not we're sure .result | 43 // the stores by keeping track of whether or not we're sure .result |
| 35 // will be overwritten anyway. This is a bit more tricky than what I | 44 // will be overwritten anyway. This is a bit more tricky than what I |
| 36 // was hoping for. | 45 // was hoping for |
| 37 bool is_set_; | 46 bool is_set_; |
| 47 bool in_try_; |
| 38 | 48 |
| 39 AstNodeFactory factory_; | 49 AstNodeFactory factory_; |
| 40 | 50 |
| 41 Expression* SetResult(Expression* value) { | 51 Expression* SetResult(Expression* value) { |
| 52 result_assigned_ = true; |
| 42 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); | 53 VariableProxy* result_proxy = factory()->NewVariableProxy(result_); |
| 43 return factory()->NewAssignment( | 54 return factory()->NewAssignment( |
| 44 Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition); | 55 Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition); |
| 45 } | 56 } |
| 46 | 57 |
| 47 // Node visitors. | 58 // Node visitors. |
| 48 #define DEF_VISIT(type) virtual void Visit##type(type* node) override; | 59 #define DEF_VISIT(type) virtual void Visit##type(type* node) override; |
| 49 AST_NODE_LIST(DEF_VISIT) | 60 AST_NODE_LIST(DEF_VISIT) |
| 50 #undef DEF_VISIT | 61 #undef DEF_VISIT |
| 51 | 62 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 70 // a variable declaration with initialization expression is 'undefined' | 81 // a variable declaration with initialization expression is 'undefined' |
| 71 // with some JS VMs: For instance, using smjs, print(eval('var x = 7')) | 82 // with some JS VMs: For instance, using smjs, print(eval('var x = 7')) |
| 72 // returns 'undefined'. To obtain the same behavior with v8, we need | 83 // returns 'undefined'. To obtain the same behavior with v8, we need |
| 73 // to prevent rewriting in that case. | 84 // to prevent rewriting in that case. |
| 74 if (!node->ignore_completion_value()) Process(node->statements()); | 85 if (!node->ignore_completion_value()) Process(node->statements()); |
| 75 } | 86 } |
| 76 | 87 |
| 77 | 88 |
| 78 void Processor::VisitExpressionStatement(ExpressionStatement* node) { | 89 void Processor::VisitExpressionStatement(ExpressionStatement* node) { |
| 79 // Rewrite : <x>; -> .result = <x>; | 90 // Rewrite : <x>; -> .result = <x>; |
| 80 if (!is_set_) { | 91 if (!is_set_ && !node->expression()->IsThrow()) { |
| 81 node->set_expression(SetResult(node->expression())); | 92 node->set_expression(SetResult(node->expression())); |
| 82 is_set_ = true; | 93 if (!in_try_) is_set_ = true; |
| 83 } | 94 } |
| 84 } | 95 } |
| 85 | 96 |
| 86 | 97 |
| 87 void Processor::VisitIfStatement(IfStatement* node) { | 98 void Processor::VisitIfStatement(IfStatement* node) { |
| 88 // Rewrite both branches. | 99 // Rewrite both then and else parts (reversed). |
| 89 bool set_after = is_set_; | 100 bool save = is_set_; |
| 101 Visit(node->else_statement()); |
| 102 bool set_after_then = is_set_; |
| 103 is_set_ = save; |
| 90 Visit(node->then_statement()); | 104 Visit(node->then_statement()); |
| 91 bool set_in_then = is_set_; | 105 is_set_ = is_set_ && set_after_then; |
| 92 is_set_ = set_after; | |
| 93 Visit(node->else_statement()); | |
| 94 is_set_ = is_set_ && set_in_then; | |
| 95 } | 106 } |
| 96 | 107 |
| 97 | 108 |
| 98 void Processor::VisitIterationStatement(IterationStatement* node) { | 109 void Processor::VisitIterationStatement(IterationStatement* node) { |
| 99 // Rewrite the body. | 110 // Rewrite the body. |
| 100 bool set_after = is_set_; | 111 bool set_after_loop = is_set_; |
| 101 is_set_ = false; // We are in a loop, so we can't rely on [set_after]. | |
| 102 Visit(node->body()); | 112 Visit(node->body()); |
| 103 is_set_ = is_set_ && set_after; | 113 is_set_ = is_set_ && set_after_loop; |
| 104 } | 114 } |
| 105 | 115 |
| 106 | 116 |
| 107 void Processor::VisitDoWhileStatement(DoWhileStatement* node) { | 117 void Processor::VisitDoWhileStatement(DoWhileStatement* node) { |
| 108 VisitIterationStatement(node); | 118 VisitIterationStatement(node); |
| 109 } | 119 } |
| 110 | 120 |
| 111 | 121 |
| 112 void Processor::VisitWhileStatement(WhileStatement* node) { | 122 void Processor::VisitWhileStatement(WhileStatement* node) { |
| 113 VisitIterationStatement(node); | 123 VisitIterationStatement(node); |
| 114 } | 124 } |
| 115 | 125 |
| 116 | 126 |
| 117 void Processor::VisitForStatement(ForStatement* node) { | 127 void Processor::VisitForStatement(ForStatement* node) { |
| 118 VisitIterationStatement(node); | 128 VisitIterationStatement(node); |
| 119 } | 129 } |
| 120 | 130 |
| 121 | 131 |
| 122 void Processor::VisitForInStatement(ForInStatement* node) { | 132 void Processor::VisitForInStatement(ForInStatement* node) { |
| 123 VisitIterationStatement(node); | 133 VisitIterationStatement(node); |
| 124 } | 134 } |
| 125 | 135 |
| 126 | 136 |
| 127 void Processor::VisitForOfStatement(ForOfStatement* node) { | 137 void Processor::VisitForOfStatement(ForOfStatement* node) { |
| 128 VisitIterationStatement(node); | 138 VisitIterationStatement(node); |
| 129 } | 139 } |
| 130 | 140 |
| 131 | 141 |
| 132 void Processor::VisitTryCatchStatement(TryCatchStatement* node) { | 142 void Processor::VisitTryCatchStatement(TryCatchStatement* node) { |
| 133 // Rewrite both try and catch block. | 143 // Rewrite both try and catch blocks (reversed order). |
| 134 bool set_after = is_set_; | 144 bool set_after_catch = is_set_; |
| 145 Visit(node->catch_block()); |
| 146 is_set_ = is_set_ && set_after_catch; |
| 147 bool save = in_try_; |
| 148 in_try_ = true; |
| 135 Visit(node->try_block()); | 149 Visit(node->try_block()); |
| 136 bool set_in_try = is_set_; | 150 in_try_ = save; |
| 137 is_set_ = set_after; | |
| 138 Visit(node->catch_block()); | |
| 139 is_set_ = is_set_ && set_in_try; | |
| 140 } | 151 } |
| 141 | 152 |
| 142 | 153 |
| 143 void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) { | 154 void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) { |
| 144 // Rewrite both try and finally block (in reverse order). | 155 // Rewrite both try and finally block (reversed order). |
| 145 Visit(node->finally_block()); | 156 Visit(node->finally_block()); |
| 157 bool save = in_try_; |
| 158 in_try_ = true; |
| 146 Visit(node->try_block()); | 159 Visit(node->try_block()); |
| 160 in_try_ = save; |
| 147 } | 161 } |
| 148 | 162 |
| 149 | 163 |
| 150 void Processor::VisitSwitchStatement(SwitchStatement* node) { | 164 void Processor::VisitSwitchStatement(SwitchStatement* node) { |
| 151 // Rewrite statements in all case clauses (in reverse order). | 165 // Rewrite statements in all case clauses in reversed order. |
| 152 ZoneList<CaseClause*>* clauses = node->cases(); | 166 ZoneList<CaseClause*>* clauses = node->cases(); |
| 153 bool set_after = is_set_; | 167 bool set_after_switch = is_set_; |
| 154 for (int i = clauses->length() - 1; i >= 0; --i) { | 168 for (int i = clauses->length() - 1; i >= 0; --i) { |
| 155 CaseClause* clause = clauses->at(i); | 169 CaseClause* clause = clauses->at(i); |
| 156 Process(clause->statements()); | 170 Process(clause->statements()); |
| 157 } | 171 } |
| 158 is_set_ = is_set_ && set_after; | 172 is_set_ = is_set_ && set_after_switch; |
| 159 } | 173 } |
| 160 | 174 |
| 161 | 175 |
| 162 void Processor::VisitContinueStatement(ContinueStatement* node) { | 176 void Processor::VisitContinueStatement(ContinueStatement* node) { |
| 163 is_set_ = false; | 177 is_set_ = false; |
| 164 } | 178 } |
| 165 | 179 |
| 166 | 180 |
| 167 void Processor::VisitBreakStatement(BreakStatement* node) { | 181 void Processor::VisitBreakStatement(BreakStatement* node) { |
| 168 is_set_ = false; | 182 is_set_ = false; |
| 169 } | 183 } |
| 170 | 184 |
| 171 | 185 |
| 172 void Processor::VisitWithStatement(WithStatement* node) { | 186 void Processor::VisitWithStatement(WithStatement* node) { |
| 187 bool set_after_body = is_set_; |
| 173 Visit(node->statement()); | 188 Visit(node->statement()); |
| 189 is_set_ = is_set_ && set_after_body; |
| 174 } | 190 } |
| 175 | 191 |
| 176 | 192 |
| 177 void Processor::VisitSloppyBlockFunctionStatement( | 193 void Processor::VisitSloppyBlockFunctionStatement( |
| 178 SloppyBlockFunctionStatement* node) { | 194 SloppyBlockFunctionStatement* node) { |
| 179 Visit(node->statement()); | 195 Visit(node->statement()); |
| 180 } | 196 } |
| 181 | 197 |
| 182 | 198 |
| 183 void Processor::VisitReturnStatement(ReturnStatement* node) { is_set_ = true; } | |
| 184 | |
| 185 | |
| 186 // Do nothing: | 199 // Do nothing: |
| 200 void Processor::VisitVariableDeclaration(VariableDeclaration* node) {} |
| 201 void Processor::VisitFunctionDeclaration(FunctionDeclaration* node) {} |
| 202 void Processor::VisitImportDeclaration(ImportDeclaration* node) {} |
| 203 void Processor::VisitExportDeclaration(ExportDeclaration* node) {} |
| 187 void Processor::VisitEmptyStatement(EmptyStatement* node) {} | 204 void Processor::VisitEmptyStatement(EmptyStatement* node) {} |
| 205 void Processor::VisitReturnStatement(ReturnStatement* node) {} |
| 188 void Processor::VisitDebuggerStatement(DebuggerStatement* node) {} | 206 void Processor::VisitDebuggerStatement(DebuggerStatement* node) {} |
| 189 | 207 |
| 190 | 208 |
| 191 // Expressions are never visited. | 209 // Expressions are never visited yet. |
| 192 #define DEF_VISIT(type) \ | 210 #define DEF_VISIT(type) \ |
| 193 void Processor::Visit##type(type* expr) { UNREACHABLE(); } | 211 void Processor::Visit##type(type* expr) { UNREACHABLE(); } |
| 194 EXPRESSION_NODE_LIST(DEF_VISIT) | 212 EXPRESSION_NODE_LIST(DEF_VISIT) |
| 195 #undef DEF_VISIT | 213 #undef DEF_VISIT |
| 196 | 214 |
| 197 | 215 |
| 198 // Declarations are never visited. | |
| 199 #define DEF_VISIT(type) \ | |
| 200 void Processor::Visit##type(type* expr) { UNREACHABLE(); } | |
| 201 DECLARATION_NODE_LIST(DEF_VISIT) | |
| 202 #undef DEF_VISIT | |
| 203 | |
| 204 | |
| 205 // Assumes code has been parsed. Mutates the AST, so the AST should not | 216 // Assumes code has been parsed. Mutates the AST, so the AST should not |
| 206 // continue to be used in the case of failure. | 217 // continue to be used in the case of failure. |
| 207 bool Rewriter::Rewrite(ParseInfo* info) { | 218 bool Rewriter::Rewrite(ParseInfo* info) { |
| 208 FunctionLiteral* function = info->literal(); | 219 FunctionLiteral* function = info->literal(); |
| 209 DCHECK(function != NULL); | 220 DCHECK(function != NULL); |
| 210 Scope* scope = function->scope(); | 221 Scope* scope = function->scope(); |
| 211 DCHECK(scope != NULL); | 222 DCHECK(scope != NULL); |
| 212 if (!scope->is_script_scope() && !scope->is_eval_scope()) return true; | 223 if (!scope->is_script_scope() && !scope->is_eval_scope()) return true; |
| 213 | 224 |
| 214 ZoneList<Statement*>* body = function->body(); | 225 ZoneList<Statement*>* body = function->body(); |
| 215 if (!body->is_empty()) { | 226 if (!body->is_empty()) { |
| 216 Variable* result = | 227 Variable* result = |
| 217 scope->NewTemporary(info->ast_value_factory()->dot_result_string()); | 228 scope->NewTemporary(info->ast_value_factory()->dot_result_string()); |
| 218 // The name string must be internalized at this point. | 229 // The name string must be internalized at this point. |
| 219 DCHECK(!result->name().is_null()); | 230 DCHECK(!result->name().is_null()); |
| 220 Processor processor(info->isolate(), result, info->ast_value_factory()); | 231 Processor processor(info->isolate(), result, info->ast_value_factory()); |
| 221 processor.Process(body); | 232 processor.Process(body); |
| 222 if (processor.HasStackOverflow()) return false; | 233 if (processor.HasStackOverflow()) return false; |
| 223 | 234 |
| 224 DCHECK(function->end_position() != RelocInfo::kNoPosition); | 235 if (processor.result_assigned()) { |
| 225 // Set the position of the assignment statement one character past the | 236 DCHECK(function->end_position() != RelocInfo::kNoPosition); |
| 226 // source code, such that it definitely is not in the source code range | 237 // Set the position of the assignment statement one character past the |
| 227 // of an immediate inner scope. For example in | 238 // source code, such that it definitely is not in the source code range |
| 228 // eval('with ({x:1}) x = 1'); | 239 // of an immediate inner scope. For example in |
| 229 // the end position of the function generated for executing the eval code | 240 // eval('with ({x:1}) x = 1'); |
| 230 // coincides with the end of the with scope which is the position of '1'. | 241 // the end position of the function generated for executing the eval code |
| 231 int pos = function->end_position(); | 242 // coincides with the end of the with scope which is the position of '1'. |
| 232 VariableProxy* result_proxy = | 243 int pos = function->end_position(); |
| 233 processor.factory()->NewVariableProxy(result, pos); | 244 VariableProxy* result_proxy = |
| 234 Statement* result_statement = | 245 processor.factory()->NewVariableProxy(result, pos); |
| 235 processor.factory()->NewReturnStatement(result_proxy, pos); | 246 Statement* result_statement = |
| 236 body->Add(result_statement, info->zone()); | 247 processor.factory()->NewReturnStatement(result_proxy, pos); |
| 248 body->Add(result_statement, info->zone()); |
| 249 } |
| 237 } | 250 } |
| 238 | 251 |
| 239 return true; | 252 return true; |
| 240 } | 253 } |
| 241 | 254 |
| 242 | 255 |
| 243 } // namespace internal | 256 } // namespace internal |
| 244 } // namespace v8 | 257 } // namespace v8 |
| OLD | NEW |