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