OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/compiler/ast-loop-assignment-analyzer.h" |
| 6 #include "src/parser.h" |
| 7 |
| 8 namespace v8 { |
| 9 namespace internal { |
| 10 namespace compiler { |
| 11 |
| 12 typedef class AstLoopAssignmentAnalyzer ALAA; // for code shortitude. |
| 13 |
| 14 |
| 15 ALAA::AstLoopAssignmentAnalyzer(Zone* zone, CompilationInfo* info) |
| 16 : info_(info), loop_stack_(zone) { |
| 17 InitializeAstVisitor(zone); |
| 18 } |
| 19 |
| 20 |
| 21 LoopAssignmentAnalysis* ALAA::Analyze() { |
| 22 LoopAssignmentAnalysis* a = new (zone()) LoopAssignmentAnalysis(zone()); |
| 23 result_ = a; |
| 24 VisitStatements(info()->function()->body()); |
| 25 result_ = NULL; |
| 26 return a; |
| 27 } |
| 28 |
| 29 |
| 30 void ALAA::Enter(IterationStatement* loop) { |
| 31 int num_variables = 1 + info()->scope()->num_parameters() + |
| 32 info()->scope()->num_stack_slots(); |
| 33 BitVector* bits = new (zone()) BitVector(num_variables, zone()); |
| 34 loop_stack_.push_back(bits); |
| 35 } |
| 36 |
| 37 |
| 38 void ALAA::Exit(IterationStatement* loop) { |
| 39 DCHECK(loop_stack_.size() > 0); |
| 40 BitVector* bits = loop_stack_.back(); |
| 41 loop_stack_.pop_back(); |
| 42 if (!loop_stack_.empty()) { |
| 43 loop_stack_.back()->Union(*bits); |
| 44 } |
| 45 result_->list_.push_back( |
| 46 std::pair<IterationStatement*, BitVector*>(loop, bits)); |
| 47 } |
| 48 |
| 49 |
| 50 // --------------------------------------------------------------------------- |
| 51 // -- Leaf nodes ------------------------------------------------------------- |
| 52 // --------------------------------------------------------------------------- |
| 53 |
| 54 void ALAA::VisitVariableDeclaration(VariableDeclaration* leaf) {} |
| 55 void ALAA::VisitFunctionDeclaration(FunctionDeclaration* leaf) {} |
| 56 void ALAA::VisitModuleDeclaration(ModuleDeclaration* leaf) {} |
| 57 void ALAA::VisitImportDeclaration(ImportDeclaration* leaf) {} |
| 58 void ALAA::VisitExportDeclaration(ExportDeclaration* leaf) {} |
| 59 void ALAA::VisitModuleVariable(ModuleVariable* leaf) {} |
| 60 void ALAA::VisitModulePath(ModulePath* leaf) {} |
| 61 void ALAA::VisitModuleUrl(ModuleUrl* leaf) {} |
| 62 void ALAA::VisitEmptyStatement(EmptyStatement* leaf) {} |
| 63 void ALAA::VisitContinueStatement(ContinueStatement* leaf) {} |
| 64 void ALAA::VisitBreakStatement(BreakStatement* leaf) {} |
| 65 void ALAA::VisitDebuggerStatement(DebuggerStatement* leaf) {} |
| 66 void ALAA::VisitFunctionLiteral(FunctionLiteral* leaf) {} |
| 67 void ALAA::VisitNativeFunctionLiteral(NativeFunctionLiteral* leaf) {} |
| 68 void ALAA::VisitVariableProxy(VariableProxy* leaf) {} |
| 69 void ALAA::VisitLiteral(Literal* leaf) {} |
| 70 void ALAA::VisitRegExpLiteral(RegExpLiteral* leaf) {} |
| 71 void ALAA::VisitThisFunction(ThisFunction* leaf) {} |
| 72 void ALAA::VisitSuperReference(SuperReference* leaf) {} |
| 73 |
| 74 |
| 75 // --------------------------------------------------------------------------- |
| 76 // -- Pass-through nodes------------------------------------------------------ |
| 77 // --------------------------------------------------------------------------- |
| 78 void ALAA::VisitModuleLiteral(ModuleLiteral* e) { Visit(e->body()); } |
| 79 |
| 80 |
| 81 void ALAA::VisitBlock(Block* stmt) { VisitStatements(stmt->statements()); } |
| 82 |
| 83 |
| 84 void ALAA::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 85 Visit(stmt->expression()); |
| 86 } |
| 87 |
| 88 |
| 89 void ALAA::VisitIfStatement(IfStatement* stmt) { |
| 90 Visit(stmt->condition()); |
| 91 Visit(stmt->then_statement()); |
| 92 Visit(stmt->else_statement()); |
| 93 } |
| 94 |
| 95 |
| 96 void ALAA::VisitReturnStatement(ReturnStatement* stmt) { |
| 97 Visit(stmt->expression()); |
| 98 } |
| 99 |
| 100 |
| 101 void ALAA::VisitWithStatement(WithStatement* stmt) { |
| 102 Visit(stmt->expression()); |
| 103 Visit(stmt->statement()); |
| 104 } |
| 105 |
| 106 |
| 107 void ALAA::VisitSwitchStatement(SwitchStatement* stmt) { |
| 108 Visit(stmt->tag()); |
| 109 ZoneList<CaseClause*>* clauses = stmt->cases(); |
| 110 for (int i = 0; i < clauses->length(); i++) { |
| 111 Visit(clauses->at(i)); |
| 112 } |
| 113 } |
| 114 |
| 115 |
| 116 void ALAA::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 117 Visit(stmt->try_block()); |
| 118 Visit(stmt->finally_block()); |
| 119 } |
| 120 |
| 121 |
| 122 void ALAA::VisitClassLiteral(ClassLiteral* e) { |
| 123 VisitIfNotNull(e->extends()); |
| 124 VisitIfNotNull(e->constructor()); |
| 125 ZoneList<ObjectLiteralProperty*>* properties = e->properties(); |
| 126 for (int i = 0; i < properties->length(); i++) { |
| 127 Visit(properties->at(i)->value()); |
| 128 } |
| 129 } |
| 130 |
| 131 |
| 132 void ALAA::VisitConditional(Conditional* e) { |
| 133 Visit(e->condition()); |
| 134 Visit(e->then_expression()); |
| 135 Visit(e->else_expression()); |
| 136 } |
| 137 |
| 138 |
| 139 void ALAA::VisitObjectLiteral(ObjectLiteral* e) { |
| 140 ZoneList<ObjectLiteralProperty*>* properties = e->properties(); |
| 141 for (int i = 0; i < properties->length(); i++) { |
| 142 Visit(properties->at(i)->value()); |
| 143 } |
| 144 } |
| 145 |
| 146 |
| 147 void ALAA::VisitArrayLiteral(ArrayLiteral* e) { VisitExpressions(e->values()); } |
| 148 |
| 149 |
| 150 void ALAA::VisitYield(Yield* stmt) { |
| 151 Visit(stmt->generator_object()); |
| 152 Visit(stmt->expression()); |
| 153 } |
| 154 |
| 155 |
| 156 void ALAA::VisitThrow(Throw* stmt) { Visit(stmt->exception()); } |
| 157 |
| 158 |
| 159 void ALAA::VisitProperty(Property* e) { |
| 160 Visit(e->obj()); |
| 161 Visit(e->key()); |
| 162 } |
| 163 |
| 164 |
| 165 void ALAA::VisitCall(Call* e) { |
| 166 Visit(e->expression()); |
| 167 VisitExpressions(e->arguments()); |
| 168 } |
| 169 |
| 170 |
| 171 void ALAA::VisitCallNew(CallNew* e) { |
| 172 Visit(e->expression()); |
| 173 VisitExpressions(e->arguments()); |
| 174 } |
| 175 |
| 176 |
| 177 void ALAA::VisitCallRuntime(CallRuntime* e) { |
| 178 VisitExpressions(e->arguments()); |
| 179 } |
| 180 |
| 181 |
| 182 void ALAA::VisitUnaryOperation(UnaryOperation* e) { Visit(e->expression()); } |
| 183 |
| 184 |
| 185 void ALAA::VisitBinaryOperation(BinaryOperation* e) { |
| 186 Visit(e->left()); |
| 187 Visit(e->right()); |
| 188 } |
| 189 |
| 190 |
| 191 void ALAA::VisitCompareOperation(CompareOperation* e) { |
| 192 Visit(e->left()); |
| 193 Visit(e->right()); |
| 194 } |
| 195 |
| 196 |
| 197 void ALAA::VisitCaseClause(CaseClause* cc) { |
| 198 if (!cc->is_default()) Visit(cc->label()); |
| 199 VisitStatements(cc->statements()); |
| 200 } |
| 201 |
| 202 |
| 203 // --------------------------------------------------------------------------- |
| 204 // -- Interesting nodes------------------------------------------------------- |
| 205 // --------------------------------------------------------------------------- |
| 206 void ALAA::VisitModuleStatement(ModuleStatement* stmt) { |
| 207 Visit(stmt->body()); |
| 208 // TODO(turbofan): can a module appear in a loop? |
| 209 AnalyzeAssignment(stmt->proxy()->var()); |
| 210 } |
| 211 |
| 212 |
| 213 void ALAA::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 214 Visit(stmt->try_block()); |
| 215 Visit(stmt->catch_block()); |
| 216 // TODO(turbofan): are catch variables well-scoped? |
| 217 AnalyzeAssignment(stmt->variable()); |
| 218 } |
| 219 |
| 220 |
| 221 void ALAA::VisitDoWhileStatement(DoWhileStatement* loop) { |
| 222 Enter(loop); |
| 223 Visit(loop->body()); |
| 224 Visit(loop->cond()); |
| 225 Exit(loop); |
| 226 } |
| 227 |
| 228 |
| 229 void ALAA::VisitWhileStatement(WhileStatement* loop) { |
| 230 Enter(loop); |
| 231 Visit(loop->cond()); |
| 232 Visit(loop->body()); |
| 233 Exit(loop); |
| 234 } |
| 235 |
| 236 |
| 237 void ALAA::VisitForStatement(ForStatement* loop) { |
| 238 VisitIfNotNull(loop->init()); |
| 239 Enter(loop); |
| 240 VisitIfNotNull(loop->cond()); |
| 241 Visit(loop->body()); |
| 242 VisitIfNotNull(loop->next()); |
| 243 Exit(loop); |
| 244 } |
| 245 |
| 246 |
| 247 void ALAA::VisitForInStatement(ForInStatement* loop) { |
| 248 Enter(loop); |
| 249 Visit(loop->each()); |
| 250 Visit(loop->subject()); |
| 251 Visit(loop->body()); |
| 252 Exit(loop); |
| 253 } |
| 254 |
| 255 |
| 256 void ALAA::VisitForOfStatement(ForOfStatement* loop) { |
| 257 Enter(loop); |
| 258 Visit(loop->each()); |
| 259 Visit(loop->subject()); |
| 260 Visit(loop->body()); |
| 261 Exit(loop); |
| 262 } |
| 263 |
| 264 |
| 265 void ALAA::VisitAssignment(Assignment* stmt) { |
| 266 Expression* l = stmt->target(); |
| 267 Visit(l); |
| 268 Visit(stmt->value()); |
| 269 if (l->IsVariableProxy()) AnalyzeAssignment(l->AsVariableProxy()->var()); |
| 270 } |
| 271 |
| 272 |
| 273 void ALAA::VisitCountOperation(CountOperation* e) { |
| 274 Expression* l = e->expression(); |
| 275 Visit(l); |
| 276 if (l->IsVariableProxy()) AnalyzeAssignment(l->AsVariableProxy()->var()); |
| 277 } |
| 278 |
| 279 |
| 280 void ALAA::AnalyzeAssignment(Variable* var) { |
| 281 if (!loop_stack_.empty() && var->IsStackAllocated()) { |
| 282 int index = var->IsParameter() |
| 283 ? 1 + var->index() |
| 284 : 1 + info()->scope()->num_parameters() + var->index(); |
| 285 loop_stack_.back()->Add(index); |
| 286 } |
| 287 } |
| 288 } |
| 289 } |
| 290 } // namespace v8::internal::compiler |
OLD | NEW |