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