blob: 5e9d217d2ac0b72aad0de174fc0cce9b222d5229 [file] [log] [blame]
Leon Clarke4515c472010-02-03 11:58:03 +00001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "data-flow.h"
31
32namespace v8 {
33namespace internal {
34
35
Andrei Popescu31002712010-02-23 13:46:05 +000036void AstLabeler::Label(CompilationInfo* info) {
37 info_ = info;
38 VisitStatements(info_->function()->body());
Leon Clarke4515c472010-02-03 11:58:03 +000039}
40
41
42void AstLabeler::VisitStatements(ZoneList<Statement*>* stmts) {
43 for (int i = 0, len = stmts->length(); i < len; i++) {
44 Visit(stmts->at(i));
45 }
46}
47
48
49void AstLabeler::VisitDeclarations(ZoneList<Declaration*>* decls) {
50 UNREACHABLE();
51}
52
53
54void AstLabeler::VisitBlock(Block* stmt) {
55 VisitStatements(stmt->statements());
56}
57
58
59void AstLabeler::VisitExpressionStatement(
60 ExpressionStatement* stmt) {
61 Visit(stmt->expression());
62}
63
64
65void AstLabeler::VisitEmptyStatement(EmptyStatement* stmt) {
66 // Do nothing.
67}
68
69
70void AstLabeler::VisitIfStatement(IfStatement* stmt) {
71 UNREACHABLE();
72}
73
74
75void AstLabeler::VisitContinueStatement(ContinueStatement* stmt) {
76 UNREACHABLE();
77}
78
79
80void AstLabeler::VisitBreakStatement(BreakStatement* stmt) {
81 UNREACHABLE();
82}
83
84
85void AstLabeler::VisitReturnStatement(ReturnStatement* stmt) {
86 UNREACHABLE();
87}
88
89
90void AstLabeler::VisitWithEnterStatement(
91 WithEnterStatement* stmt) {
92 UNREACHABLE();
93}
94
95
96void AstLabeler::VisitWithExitStatement(WithExitStatement* stmt) {
97 UNREACHABLE();
98}
99
100
101void AstLabeler::VisitSwitchStatement(SwitchStatement* stmt) {
102 UNREACHABLE();
103}
104
105
106void AstLabeler::VisitDoWhileStatement(DoWhileStatement* stmt) {
107 UNREACHABLE();
108}
109
110
111void AstLabeler::VisitWhileStatement(WhileStatement* stmt) {
112 UNREACHABLE();
113}
114
115
116void AstLabeler::VisitForStatement(ForStatement* stmt) {
117 UNREACHABLE();
118}
119
120
121void AstLabeler::VisitForInStatement(ForInStatement* stmt) {
122 UNREACHABLE();
123}
124
125
126void AstLabeler::VisitTryCatchStatement(TryCatchStatement* stmt) {
127 UNREACHABLE();
128}
129
130
131void AstLabeler::VisitTryFinallyStatement(
132 TryFinallyStatement* stmt) {
133 UNREACHABLE();
134}
135
136
137void AstLabeler::VisitDebuggerStatement(
138 DebuggerStatement* stmt) {
139 UNREACHABLE();
140}
141
142
143void AstLabeler::VisitFunctionLiteral(FunctionLiteral* expr) {
144 UNREACHABLE();
145}
146
147
148void AstLabeler::VisitFunctionBoilerplateLiteral(
149 FunctionBoilerplateLiteral* expr) {
150 UNREACHABLE();
151}
152
153
154void AstLabeler::VisitConditional(Conditional* expr) {
155 UNREACHABLE();
156}
157
158
159void AstLabeler::VisitSlot(Slot* expr) {
160 UNREACHABLE();
161}
162
163
164void AstLabeler::VisitVariableProxy(VariableProxy* expr) {
165 expr->set_num(next_number_++);
Andrei Popescu31002712010-02-23 13:46:05 +0000166 Variable* var = expr->var();
167 if (var->is_global() && !var->is_this()) {
168 info_->set_has_globals(true);
169 }
Leon Clarke4515c472010-02-03 11:58:03 +0000170}
171
172
173void AstLabeler::VisitLiteral(Literal* expr) {
174 UNREACHABLE();
175}
176
177
178void AstLabeler::VisitRegExpLiteral(RegExpLiteral* expr) {
179 UNREACHABLE();
180}
181
182
183void AstLabeler::VisitObjectLiteral(ObjectLiteral* expr) {
184 UNREACHABLE();
185}
186
187
188void AstLabeler::VisitArrayLiteral(ArrayLiteral* expr) {
189 UNREACHABLE();
190}
191
192
193void AstLabeler::VisitCatchExtensionObject(
194 CatchExtensionObject* expr) {
195 UNREACHABLE();
196}
197
198
199void AstLabeler::VisitAssignment(Assignment* expr) {
200 Property* prop = expr->target()->AsProperty();
201 ASSERT(prop != NULL);
Andrei Popescu402d9372010-02-26 13:31:12 +0000202 ASSERT(prop->key()->IsPropertyName());
203 VariableProxy* proxy = prop->obj()->AsVariableProxy();
204 USE(proxy);
205 ASSERT(proxy != NULL && proxy->var()->is_this());
206 info()->set_has_this_properties(true);
Leon Clarke4515c472010-02-03 11:58:03 +0000207 Visit(expr->value());
208 expr->set_num(next_number_++);
209}
210
211
212void AstLabeler::VisitThrow(Throw* expr) {
213 UNREACHABLE();
214}
215
216
217void AstLabeler::VisitProperty(Property* expr) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000218 ASSERT(expr->key()->IsPropertyName());
219 VariableProxy* proxy = expr->obj()->AsVariableProxy();
220 USE(proxy);
221 ASSERT(proxy != NULL && proxy->var()->is_this());
222 info()->set_has_this_properties(true);
223 expr->set_num(next_number_++);
Leon Clarke4515c472010-02-03 11:58:03 +0000224}
225
226
227void AstLabeler::VisitCall(Call* expr) {
228 UNREACHABLE();
229}
230
231
232void AstLabeler::VisitCallNew(CallNew* expr) {
233 UNREACHABLE();
234}
235
236
237void AstLabeler::VisitCallRuntime(CallRuntime* expr) {
238 UNREACHABLE();
239}
240
241
242void AstLabeler::VisitUnaryOperation(UnaryOperation* expr) {
243 UNREACHABLE();
244}
245
246
247void AstLabeler::VisitCountOperation(CountOperation* expr) {
248 UNREACHABLE();
249}
250
251
252void AstLabeler::VisitBinaryOperation(BinaryOperation* expr) {
253 Visit(expr->left());
254 Visit(expr->right());
255 expr->set_num(next_number_++);
256}
257
258
259void AstLabeler::VisitCompareOperation(CompareOperation* expr) {
260 UNREACHABLE();
261}
262
263
264void AstLabeler::VisitThisFunction(ThisFunction* expr) {
265 UNREACHABLE();
266}
267
268
269void AstLabeler::VisitDeclaration(Declaration* decl) {
270 UNREACHABLE();
271}
272
Andrei Popescu402d9372010-02-26 13:31:12 +0000273
274ZoneList<Expression*>* VarUseMap::Lookup(Variable* var) {
275 HashMap::Entry* entry = HashMap::Lookup(var, var->name()->Hash(), true);
276 if (entry->value == NULL) {
277 entry->value = new ZoneList<Expression*>(1);
278 }
279 return reinterpret_cast<ZoneList<Expression*>*>(entry->value);
280}
281
282
283void LivenessAnalyzer::Analyze(FunctionLiteral* fun) {
284 // Process the function body.
285 VisitStatements(fun->body());
286
287 // All variables are implicitly defined at the function start.
288 // Record a definition of all variables live at function entry.
289 for (HashMap::Entry* p = live_vars_.Start();
290 p != NULL;
291 p = live_vars_.Next(p)) {
292 Variable* var = reinterpret_cast<Variable*>(p->key);
293 RecordDef(var, fun);
294 }
295}
296
297
298void LivenessAnalyzer::VisitStatements(ZoneList<Statement*>* stmts) {
299 // Visit statements right-to-left.
300 for (int i = stmts->length() - 1; i >= 0; i--) {
301 Visit(stmts->at(i));
302 }
303}
304
305
306void LivenessAnalyzer::RecordUse(Variable* var, Expression* expr) {
307 ASSERT(var->is_global() || var->is_this());
308 ZoneList<Expression*>* uses = live_vars_.Lookup(var);
309 uses->Add(expr);
310}
311
312
313void LivenessAnalyzer::RecordDef(Variable* var, Expression* expr) {
314 ASSERT(var->is_global() || var->is_this());
315
316 // We do not support other expressions that can define variables.
317 ASSERT(expr->AsFunctionLiteral() != NULL);
318
319 // Add the variable to the list of defined variables.
320 if (expr->defined_vars() == NULL) {
321 expr->set_defined_vars(new ZoneList<DefinitionInfo*>(1));
322 }
323 DefinitionInfo* def = new DefinitionInfo();
324 expr->AsFunctionLiteral()->defined_vars()->Add(def);
325
326 // Compute the last use of the definition. The variable uses are
327 // inserted in reversed evaluation order. The first element
328 // in the list of live uses is the last use.
329 ZoneList<Expression*>* uses = live_vars_.Lookup(var);
330 while (uses->length() > 0) {
331 Expression* use_site = uses->RemoveLast();
332 use_site->set_var_def(def);
333 if (uses->length() == 0) {
334 def->set_last_use(use_site);
335 }
336 }
337}
338
339
340// Visitor functions for live variable analysis.
341void LivenessAnalyzer::VisitDeclaration(Declaration* decl) {
342 UNREACHABLE();
343}
344
345
346void LivenessAnalyzer::VisitBlock(Block* stmt) {
347 VisitStatements(stmt->statements());
348}
349
350
351void LivenessAnalyzer::VisitExpressionStatement(
352 ExpressionStatement* stmt) {
353 Visit(stmt->expression());
354}
355
356
357void LivenessAnalyzer::VisitEmptyStatement(EmptyStatement* stmt) {
358 // Do nothing.
359}
360
361
362void LivenessAnalyzer::VisitIfStatement(IfStatement* stmt) {
363 UNREACHABLE();
364}
365
366
367void LivenessAnalyzer::VisitContinueStatement(ContinueStatement* stmt) {
368 UNREACHABLE();
369}
370
371
372void LivenessAnalyzer::VisitBreakStatement(BreakStatement* stmt) {
373 UNREACHABLE();
374}
375
376
377void LivenessAnalyzer::VisitReturnStatement(ReturnStatement* stmt) {
378 UNREACHABLE();
379}
380
381
382void LivenessAnalyzer::VisitWithEnterStatement(
383 WithEnterStatement* stmt) {
384 UNREACHABLE();
385}
386
387
388void LivenessAnalyzer::VisitWithExitStatement(WithExitStatement* stmt) {
389 UNREACHABLE();
390}
391
392
393void LivenessAnalyzer::VisitSwitchStatement(SwitchStatement* stmt) {
394 UNREACHABLE();
395}
396
397
398void LivenessAnalyzer::VisitDoWhileStatement(DoWhileStatement* stmt) {
399 UNREACHABLE();
400}
401
402
403void LivenessAnalyzer::VisitWhileStatement(WhileStatement* stmt) {
404 UNREACHABLE();
405}
406
407
408void LivenessAnalyzer::VisitForStatement(ForStatement* stmt) {
409 UNREACHABLE();
410}
411
412
413void LivenessAnalyzer::VisitForInStatement(ForInStatement* stmt) {
414 UNREACHABLE();
415}
416
417
418void LivenessAnalyzer::VisitTryCatchStatement(TryCatchStatement* stmt) {
419 UNREACHABLE();
420}
421
422
423void LivenessAnalyzer::VisitTryFinallyStatement(
424 TryFinallyStatement* stmt) {
425 UNREACHABLE();
426}
427
428
429void LivenessAnalyzer::VisitDebuggerStatement(
430 DebuggerStatement* stmt) {
431 UNREACHABLE();
432}
433
434
435void LivenessAnalyzer::VisitFunctionLiteral(FunctionLiteral* expr) {
436 UNREACHABLE();
437}
438
439
440void LivenessAnalyzer::VisitFunctionBoilerplateLiteral(
441 FunctionBoilerplateLiteral* expr) {
442 UNREACHABLE();
443}
444
445
446void LivenessAnalyzer::VisitConditional(Conditional* expr) {
447 UNREACHABLE();
448}
449
450
451void LivenessAnalyzer::VisitSlot(Slot* expr) {
452 UNREACHABLE();
453}
454
455
456void LivenessAnalyzer::VisitVariableProxy(VariableProxy* expr) {
457 Variable* var = expr->var();
458 ASSERT(var->is_global());
459 ASSERT(!var->is_this());
460 RecordUse(var, expr);
461}
462
463
464void LivenessAnalyzer::VisitLiteral(Literal* expr) {
465 UNREACHABLE();
466}
467
468
469void LivenessAnalyzer::VisitRegExpLiteral(RegExpLiteral* expr) {
470 UNREACHABLE();
471}
472
473
474void LivenessAnalyzer::VisitObjectLiteral(ObjectLiteral* expr) {
475 UNREACHABLE();
476}
477
478
479void LivenessAnalyzer::VisitArrayLiteral(ArrayLiteral* expr) {
480 UNREACHABLE();
481}
482
483
484void LivenessAnalyzer::VisitCatchExtensionObject(
485 CatchExtensionObject* expr) {
486 UNREACHABLE();
487}
488
489
490void LivenessAnalyzer::VisitAssignment(Assignment* expr) {
491 Property* prop = expr->target()->AsProperty();
492 ASSERT(prop != NULL);
493 ASSERT(prop->key()->IsPropertyName());
494 VariableProxy* proxy = prop->obj()->AsVariableProxy();
495 ASSERT(proxy != NULL && proxy->var()->is_this());
496
497 // Record use of this at the assignment node. Assignments to
498 // this-properties are treated like unary operations.
499 RecordUse(proxy->var(), expr);
500
501 // Visit right-hand side.
502 Visit(expr->value());
503}
504
505
506void LivenessAnalyzer::VisitThrow(Throw* expr) {
507 UNREACHABLE();
508}
509
510
511void LivenessAnalyzer::VisitProperty(Property* expr) {
512 ASSERT(expr->key()->IsPropertyName());
513 VariableProxy* proxy = expr->obj()->AsVariableProxy();
514 ASSERT(proxy != NULL && proxy->var()->is_this());
515 RecordUse(proxy->var(), expr);
516}
517
518
519void LivenessAnalyzer::VisitCall(Call* expr) {
520 UNREACHABLE();
521}
522
523
524void LivenessAnalyzer::VisitCallNew(CallNew* expr) {
525 UNREACHABLE();
526}
527
528
529void LivenessAnalyzer::VisitCallRuntime(CallRuntime* expr) {
530 UNREACHABLE();
531}
532
533
534void LivenessAnalyzer::VisitUnaryOperation(UnaryOperation* expr) {
535 UNREACHABLE();
536}
537
538
539void LivenessAnalyzer::VisitCountOperation(CountOperation* expr) {
540 UNREACHABLE();
541}
542
543
544void LivenessAnalyzer::VisitBinaryOperation(BinaryOperation* expr) {
545 // Visit child nodes in reverse evaluation order.
546 Visit(expr->right());
547 Visit(expr->left());
548}
549
550
551void LivenessAnalyzer::VisitCompareOperation(CompareOperation* expr) {
552 UNREACHABLE();
553}
554
555
556void LivenessAnalyzer::VisitThisFunction(ThisFunction* expr) {
557 UNREACHABLE();
558}
559
560
Leon Clarke4515c472010-02-03 11:58:03 +0000561} } // namespace v8::internal