blob: 867229a650b611b21eefcd35c73fb627e095446a [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/rewriter.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/ast.h"
10#include "src/compiler.h"
11#include "src/scopes.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010012
Steve Blocka7e24c12009-10-30 11:49:00 +000013namespace v8 {
14namespace internal {
15
Steve Blocka7e24c12009-10-30 11:49:00 +000016class Processor: public AstVisitor {
17 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018 Processor(Variable* result, Zone* zone, AstNode::IdGen* ast_node_id_gen)
Steve Blocka7e24c12009-10-30 11:49:00 +000019 : result_(result),
20 result_assigned_(false),
21 is_set_(false),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010022 in_try_(false),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023 // 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 Murdoch3ef787d2012-04-12 10:51:47 +010028
29 virtual ~Processor() { }
Steve Blocka7e24c12009-10-30 11:49:00 +000030
31 void Process(ZoneList<Statement*>* statements);
Kristian Monsen0d5e1162010-09-30 15:31:59 +010032 bool result_assigned() const { return result_assigned_; }
Steve Blocka7e24c12009-10-30 11:49:00 +000033
Ben Murdoch3ef787d2012-04-12 10:51:47 +010034 AstNodeFactory<AstNullVisitor>* factory() {
35 return &factory_;
36 }
37
Steve Blocka7e24c12009-10-30 11:49:00 +000038 private:
Ben Murdochb0fe1622011-05-05 13:52:32 +010039 Variable* result_;
Steve Blocka7e24c12009-10-30 11:49:00 +000040
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 Murdoch3ef787d2012-04-12 10:51:47 +010054 AstNodeFactory<AstNullVisitor> factory_;
55
Steve Blocka7e24c12009-10-30 11:49:00 +000056 Expression* SetResult(Expression* value) {
57 result_assigned_ = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010058 VariableProxy* result_proxy = factory()->NewVariableProxy(result_);
59 return factory()->NewAssignment(
60 Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition);
Steve Blocka7e24c12009-10-30 11:49:00 +000061 }
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 Block3ce2e202009-11-05 08:53:23 +000068
69 void VisitIterationStatement(IterationStatement* stmt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070
71 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
Steve Blocka7e24c12009-10-30 11:49:00 +000072};
73
74
75void Processor::Process(ZoneList<Statement*>* statements) {
76 for (int i = statements->length() - 1; i >= 0; --i) {
77 Visit(statements->at(i));
78 }
79}
80
81
82void 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 Murdochb8a8cc12014-11-26 15:28:44 +000095void 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 Blocka7e24c12009-10-30 11:49:00 +0000102void Processor::VisitExpressionStatement(ExpressionStatement* node) {
103 // Rewrite : <x>; -> .result = <x>;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 if (!is_set_ && !node->expression()->IsThrow()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 node->set_expression(SetResult(node->expression()));
106 if (!in_try_) is_set_ = true;
107 }
108}
109
110
111void 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 Block3ce2e202009-11-05 08:53:23 +0000122void Processor::VisitIterationStatement(IterationStatement* node) {
123 // Rewrite the body.
Steve Blocka7e24c12009-10-30 11:49:00 +0000124 bool set_after_loop = is_set_;
125 Visit(node->body());
126 is_set_ = is_set_ && set_after_loop;
127}
128
129
Steve Block3ce2e202009-11-05 08:53:23 +0000130void Processor::VisitDoWhileStatement(DoWhileStatement* node) {
131 VisitIterationStatement(node);
Steve Blocka7e24c12009-10-30 11:49:00 +0000132}
133
134
Steve Block3ce2e202009-11-05 08:53:23 +0000135void Processor::VisitWhileStatement(WhileStatement* node) {
136 VisitIterationStatement(node);
137}
138
139
140void Processor::VisitForStatement(ForStatement* node) {
141 VisitIterationStatement(node);
142}
143
144
145void Processor::VisitForInStatement(ForInStatement* node) {
146 VisitIterationStatement(node);
147}
148
149
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150void Processor::VisitForOfStatement(ForOfStatement* node) {
151 VisitIterationStatement(node);
152}
153
154
Steve Block3ce2e202009-11-05 08:53:23 +0000155void Processor::VisitTryCatchStatement(TryCatchStatement* node) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 // 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 Block3ce2e202009-11-05 08:53:23 +0000167void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 // 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
177void 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
189void Processor::VisitContinueStatement(ContinueStatement* node) {
190 is_set_ = false;
191}
192
193
194void Processor::VisitBreakStatement(BreakStatement* node) {
195 is_set_ = false;
196}
197
198
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000199void 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 Blocka7e24c12009-10-30 11:49:00 +0000206// Do nothing:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100207void Processor::VisitVariableDeclaration(VariableDeclaration* node) {}
208void Processor::VisitFunctionDeclaration(FunctionDeclaration* node) {}
209void Processor::VisitModuleDeclaration(ModuleDeclaration* node) {}
210void Processor::VisitImportDeclaration(ImportDeclaration* node) {}
211void Processor::VisitExportDeclaration(ExportDeclaration* node) {}
212void Processor::VisitModuleLiteral(ModuleLiteral* node) {}
213void Processor::VisitModuleVariable(ModuleVariable* node) {}
214void Processor::VisitModulePath(ModulePath* node) {}
215void Processor::VisitModuleUrl(ModuleUrl* node) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000216void Processor::VisitEmptyStatement(EmptyStatement* node) {}
217void Processor::VisitReturnStatement(ReturnStatement* node) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000218void Processor::VisitDebuggerStatement(DebuggerStatement* node) {}
219
220
221// Expressions are never visited yet.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222#define DEF_VISIT(type) \
223 void Processor::Visit##type(type* expr) { UNREACHABLE(); }
224EXPRESSION_NODE_LIST(DEF_VISIT)
225#undef DEF_VISIT
Steve Blocka7e24c12009-10-30 11:49:00 +0000226
227
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000228// Assumes code has been parsed. Mutates the AST, so the AST should not
229// continue to be used in the case of failure.
Ben Murdochf87a2032010-10-22 12:50:53 +0100230bool Rewriter::Rewrite(CompilationInfo* info) {
231 FunctionLiteral* function = info->function();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 DCHECK(function != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 Scope* scope = function->scope();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 DCHECK(scope != NULL);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000235 if (!scope->is_global_scope() && !scope->is_eval_scope()) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +0000236
237 ZoneList<Statement*>* body = function->body();
Ben Murdochf87a2032010-10-22 12:50:53 +0100238 if (!body->is_empty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 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 Murdochf87a2032010-10-22 12:50:53 +0100244 processor.Process(body);
245 if (processor.HasStackOverflow()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000246
Ben Murdochb0fe1622011-05-05 13:52:32 +0100247 if (processor.result_assigned()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 DCHECK(function->end_position() != RelocInfo::kNoPosition);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100249 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000255 int pos = function->end_position();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100256 VariableProxy* result_proxy = processor.factory()->NewVariableProxy(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 result->raw_name(), false, result->interface(), pos);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100258 result_proxy->BindTo(result);
259 Statement* result_statement =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 processor.factory()->NewReturnStatement(result_proxy, pos);
261 body->Add(result_statement, info->zone());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100262 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100263 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000264
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 return true;
266}
267
268
Steve Blocka7e24c12009-10-30 11:49:00 +0000269} } // namespace v8::internal