Upstream version 7.36.149.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 "v8.h"
6
7 #include "rewriter.h"
8
9 #include "ast.h"
10 #include "compiler.h"
11 #include "scopes.h"
12
13 namespace v8 {
14 namespace internal {
15
16 class Processor: public AstVisitor {
17  public:
18   Processor(Variable* result, Zone* zone)
19       : result_(result),
20         result_assigned_(false),
21         is_set_(false),
22         in_try_(false),
23         factory_(zone) {
24     InitializeAstVisitor(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) \
63   virtual void Visit##type(type* node);
64   AST_NODE_LIST(DEF_VISIT)
65 #undef DEF_VISIT
66
67   void VisitIterationStatement(IterationStatement* stmt);
68
69   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
70 };
71
72
73 void Processor::Process(ZoneList<Statement*>* statements) {
74   for (int i = statements->length() - 1; i >= 0; --i) {
75     Visit(statements->at(i));
76   }
77 }
78
79
80 void Processor::VisitBlock(Block* node) {
81   // An initializer block is the rewritten form of a variable declaration
82   // with initialization expressions. The initializer block contains the
83   // list of assignments corresponding to the initialization expressions.
84   // While unclear from the spec (ECMA-262, 3rd., 12.2), the value of
85   // a variable declaration with initialization expression is 'undefined'
86   // with some JS VMs: For instance, using smjs, print(eval('var x = 7'))
87   // returns 'undefined'. To obtain the same behavior with v8, we need
88   // to prevent rewriting in that case.
89   if (!node->is_initializer_block()) Process(node->statements());
90 }
91
92
93 void Processor::VisitModuleStatement(ModuleStatement* node) {
94   bool set_after_body = is_set_;
95   Visit(node->body());
96   is_set_ = is_set_ && set_after_body;
97 }
98
99
100 void Processor::VisitExpressionStatement(ExpressionStatement* node) {
101   // Rewrite : <x>; -> .result = <x>;
102   if (!is_set_ && !node->expression()->IsThrow()) {
103     node->set_expression(SetResult(node->expression()));
104     if (!in_try_) is_set_ = true;
105   }
106 }
107
108
109 void Processor::VisitIfStatement(IfStatement* node) {
110   // Rewrite both then and else parts (reversed).
111   bool save = is_set_;
112   Visit(node->else_statement());
113   bool set_after_then = is_set_;
114   is_set_ = save;
115   Visit(node->then_statement());
116   is_set_ = is_set_ && set_after_then;
117 }
118
119
120 void Processor::VisitIterationStatement(IterationStatement* node) {
121   // Rewrite the body.
122   bool set_after_loop = is_set_;
123   Visit(node->body());
124   is_set_ = is_set_ && set_after_loop;
125 }
126
127
128 void Processor::VisitDoWhileStatement(DoWhileStatement* node) {
129   VisitIterationStatement(node);
130 }
131
132
133 void Processor::VisitWhileStatement(WhileStatement* node) {
134   VisitIterationStatement(node);
135 }
136
137
138 void Processor::VisitForStatement(ForStatement* node) {
139   VisitIterationStatement(node);
140 }
141
142
143 void Processor::VisitForInStatement(ForInStatement* node) {
144   VisitIterationStatement(node);
145 }
146
147
148 void Processor::VisitForOfStatement(ForOfStatement* node) {
149   VisitIterationStatement(node);
150 }
151
152
153 void Processor::VisitTryCatchStatement(TryCatchStatement* node) {
154   // Rewrite both try and catch blocks (reversed order).
155   bool set_after_catch = is_set_;
156   Visit(node->catch_block());
157   is_set_ = is_set_ && set_after_catch;
158   bool save = in_try_;
159   in_try_ = true;
160   Visit(node->try_block());
161   in_try_ = save;
162 }
163
164
165 void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) {
166   // Rewrite both try and finally block (reversed order).
167   Visit(node->finally_block());
168   bool save = in_try_;
169   in_try_ = true;
170   Visit(node->try_block());
171   in_try_ = save;
172 }
173
174
175 void Processor::VisitSwitchStatement(SwitchStatement* node) {
176   // Rewrite statements in all case clauses in reversed order.
177   ZoneList<CaseClause*>* clauses = node->cases();
178   bool set_after_switch = is_set_;
179   for (int i = clauses->length() - 1; i >= 0; --i) {
180     CaseClause* clause = clauses->at(i);
181     Process(clause->statements());
182   }
183   is_set_ = is_set_ && set_after_switch;
184 }
185
186
187 void Processor::VisitContinueStatement(ContinueStatement* node) {
188   is_set_ = false;
189 }
190
191
192 void Processor::VisitBreakStatement(BreakStatement* node) {
193   is_set_ = false;
194 }
195
196
197 void Processor::VisitWithStatement(WithStatement* node) {
198   bool set_after_body = is_set_;
199   Visit(node->statement());
200   is_set_ = is_set_ && set_after_body;
201 }
202
203
204 // Do nothing:
205 void Processor::VisitVariableDeclaration(VariableDeclaration* node) {}
206 void Processor::VisitFunctionDeclaration(FunctionDeclaration* node) {}
207 void Processor::VisitModuleDeclaration(ModuleDeclaration* node) {}
208 void Processor::VisitImportDeclaration(ImportDeclaration* node) {}
209 void Processor::VisitExportDeclaration(ExportDeclaration* node) {}
210 void Processor::VisitModuleLiteral(ModuleLiteral* node) {}
211 void Processor::VisitModuleVariable(ModuleVariable* node) {}
212 void Processor::VisitModulePath(ModulePath* node) {}
213 void Processor::VisitModuleUrl(ModuleUrl* node) {}
214 void Processor::VisitEmptyStatement(EmptyStatement* node) {}
215 void Processor::VisitReturnStatement(ReturnStatement* node) {}
216 void Processor::VisitDebuggerStatement(DebuggerStatement* node) {}
217
218
219 // Expressions are never visited yet.
220 #define DEF_VISIT(type)                                         \
221   void Processor::Visit##type(type* expr) { UNREACHABLE(); }
222 EXPRESSION_NODE_LIST(DEF_VISIT)
223 #undef DEF_VISIT
224
225
226 // Assumes code has been parsed.  Mutates the AST, so the AST should not
227 // continue to be used in the case of failure.
228 bool Rewriter::Rewrite(CompilationInfo* info) {
229   FunctionLiteral* function = info->function();
230   ASSERT(function != NULL);
231   Scope* scope = function->scope();
232   ASSERT(scope != NULL);
233   if (!scope->is_global_scope() && !scope->is_eval_scope()) return true;
234
235   ZoneList<Statement*>* body = function->body();
236   if (!body->is_empty()) {
237     Variable* result = scope->NewTemporary(
238         info->isolate()->factory()->dot_result_string());
239     Processor processor(result, info->zone());
240     processor.Process(body);
241     if (processor.HasStackOverflow()) return false;
242
243     if (processor.result_assigned()) {
244       ASSERT(function->end_position() != RelocInfo::kNoPosition);
245       // Set the position of the assignment statement one character past the
246       // source code, such that it definitely is not in the source code range
247       // of an immediate inner scope. For example in
248       //   eval('with ({x:1}) x = 1');
249       // the end position of the function generated for executing the eval code
250       // coincides with the end of the with scope which is the position of '1'.
251       int pos = function->end_position();
252       VariableProxy* result_proxy = processor.factory()->NewVariableProxy(
253           result->name(), false, result->interface(), pos);
254       result_proxy->BindTo(result);
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