| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "data-flow.h" | 30 #include "data-flow.h" |
| 31 | 31 |
| 32 namespace v8 { | 32 namespace v8 { |
| 33 namespace internal { | 33 namespace internal { |
| 34 | 34 |
| 35 | 35 |
| 36 void FlowGraph::AppendInstruction(AstNode* instruction) { |
| 37 ASSERT(instruction != NULL); |
| 38 if (is_empty() || !exit()->IsBlockNode()) { |
| 39 AppendNode(new BlockNode()); |
| 40 } |
| 41 BlockNode::cast(exit())->AddInstruction(instruction); |
| 42 } |
| 43 |
| 44 |
| 45 void FlowGraph::AppendNode(Node* node) { |
| 46 ASSERT(node != NULL); |
| 47 if (is_empty()) { |
| 48 entry_ = exit_ = node; |
| 49 } else { |
| 50 exit()->AddSuccessor(node); |
| 51 node->AddPredecessor(exit()); |
| 52 exit_ = node; |
| 53 } |
| 54 } |
| 55 |
| 56 |
| 57 void FlowGraph::AppendGraph(FlowGraph* graph) { |
| 58 ASSERT(!graph->is_empty()); |
| 59 if (is_empty()) { |
| 60 entry_ = graph->entry(); |
| 61 exit_ = graph->exit(); |
| 62 } else { |
| 63 exit()->AddSuccessor(graph->entry()); |
| 64 graph->entry()->AddPredecessor(exit()); |
| 65 exit_ = graph->exit(); |
| 66 } |
| 67 } |
| 68 |
| 69 |
| 70 void FlowGraph::Split(BranchNode* branch, |
| 71 FlowGraph* left, |
| 72 FlowGraph* right, |
| 73 JoinNode* merge) { |
| 74 // Graphs are in edge split form. Add empty blocks if necessary. |
| 75 if (left->is_empty()) left->AppendNode(new BlockNode()); |
| 76 if (right->is_empty()) right->AppendNode(new BlockNode()); |
| 77 |
| 78 // Add the branch, left flowgraph and merge. |
| 79 AppendNode(branch); |
| 80 AppendGraph(left); |
| 81 AppendNode(merge); |
| 82 |
| 83 // Splice in the right flowgraph. |
| 84 right->AppendNode(merge); |
| 85 branch->AddSuccessor(right->entry()); |
| 86 right->entry()->AddPredecessor(branch); |
| 87 } |
| 88 |
| 89 |
| 90 void FlowGraph::Loop(JoinNode* merge, |
| 91 FlowGraph* condition, |
| 92 BranchNode* branch, |
| 93 FlowGraph* body) { |
| 94 // Add the merge, condition and branch. Add merge's predecessors in |
| 95 // left-to-right order. |
| 96 AppendNode(merge); |
| 97 body->AppendNode(merge); |
| 98 AppendGraph(condition); |
| 99 AppendNode(branch); |
| 100 |
| 101 // Splice in the body flowgraph. |
| 102 branch->AddSuccessor(body->entry()); |
| 103 body->entry()->AddPredecessor(branch); |
| 104 } |
| 105 |
| 106 |
| 107 void EntryNode::Traverse(bool mark, |
| 108 ZoneList<Node*>* preorder, |
| 109 ZoneList<Node*>* postorder) { |
| 110 ASSERT(successor_ != NULL); |
| 111 preorder->Add(this); |
| 112 if (!successor_->IsMarkedWith(mark)) { |
| 113 successor_->MarkWith(mark); |
| 114 successor_->Traverse(mark, preorder, postorder); |
| 115 } |
| 116 postorder->Add(this); |
| 117 } |
| 118 |
| 119 |
| 120 void ExitNode::Traverse(bool mark, |
| 121 ZoneList<Node*>* preorder, |
| 122 ZoneList<Node*>* postorder) { |
| 123 preorder->Add(this); |
| 124 postorder->Add(this); |
| 125 } |
| 126 |
| 127 |
| 128 void BlockNode::Traverse(bool mark, |
| 129 ZoneList<Node*>* preorder, |
| 130 ZoneList<Node*>* postorder) { |
| 131 ASSERT(successor_ != NULL); |
| 132 preorder->Add(this); |
| 133 if (!successor_->IsMarkedWith(mark)) { |
| 134 successor_->MarkWith(mark); |
| 135 successor_->Traverse(mark, preorder, postorder); |
| 136 } |
| 137 postorder->Add(this); |
| 138 } |
| 139 |
| 140 |
| 141 void BranchNode::Traverse(bool mark, |
| 142 ZoneList<Node*>* preorder, |
| 143 ZoneList<Node*>* postorder) { |
| 144 ASSERT(successor0_ != NULL && successor1_ != NULL); |
| 145 preorder->Add(this); |
| 146 if (!successor0_->IsMarkedWith(mark)) { |
| 147 successor0_->MarkWith(mark); |
| 148 successor0_->Traverse(mark, preorder, postorder); |
| 149 } |
| 150 if (!successor1_->IsMarkedWith(mark)) { |
| 151 successor1_->MarkWith(mark); |
| 152 successor1_->Traverse(mark, preorder, postorder); |
| 153 } |
| 154 postorder->Add(this); |
| 155 } |
| 156 |
| 157 |
| 158 void JoinNode::Traverse(bool mark, |
| 159 ZoneList<Node*>* preorder, |
| 160 ZoneList<Node*>* postorder) { |
| 161 ASSERT(successor_ != NULL); |
| 162 preorder->Add(this); |
| 163 if (!successor_->IsMarkedWith(mark)) { |
| 164 successor_->MarkWith(mark); |
| 165 successor_->Traverse(mark, preorder, postorder); |
| 166 } |
| 167 postorder->Add(this); |
| 168 } |
| 169 |
| 170 |
| 171 void FlowGraphBuilder::Build(FunctionLiteral* lit) { |
| 172 graph_ = FlowGraph::Empty(); |
| 173 graph_.AppendNode(new EntryNode()); |
| 174 global_exit_ = new ExitNode(); |
| 175 VisitStatements(lit->body()); |
| 176 |
| 177 if (HasStackOverflow()) { |
| 178 graph_ = FlowGraph::Empty(); |
| 179 return; |
| 180 } |
| 181 |
| 182 graph_.AppendNode(global_exit_); |
| 183 |
| 184 // Build preorder and postorder traversal orders. All the nodes in |
| 185 // the graph have the same mark flag. For the traversal, use that |
| 186 // flag's negation. Traversal will flip all the flags. |
| 187 bool mark = graph_.entry()->IsMarkedWith(false); |
| 188 graph_.entry()->MarkWith(mark); |
| 189 graph_.entry()->Traverse(mark, &preorder_, &postorder_); |
| 190 } |
| 191 |
| 192 |
| 193 void FlowGraphBuilder::VisitDeclaration(Declaration* decl) { |
| 194 UNREACHABLE(); |
| 195 } |
| 196 |
| 197 |
| 198 void FlowGraphBuilder::VisitBlock(Block* stmt) { |
| 199 VisitStatements(stmt->statements()); |
| 200 } |
| 201 |
| 202 |
| 203 void FlowGraphBuilder::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 204 Visit(stmt->expression()); |
| 205 } |
| 206 |
| 207 |
| 208 void FlowGraphBuilder::VisitEmptyStatement(EmptyStatement* stmt) { |
| 209 // Nothing to do. |
| 210 } |
| 211 |
| 212 |
| 213 void FlowGraphBuilder::VisitIfStatement(IfStatement* stmt) { |
| 214 Visit(stmt->condition()); |
| 215 |
| 216 BranchNode* branch = new BranchNode(); |
| 217 FlowGraph original = graph_; |
| 218 graph_ = FlowGraph::Empty(); |
| 219 Visit(stmt->then_statement()); |
| 220 |
| 221 FlowGraph left = graph_; |
| 222 graph_ = FlowGraph::Empty(); |
| 223 Visit(stmt->else_statement()); |
| 224 |
| 225 JoinNode* join = new JoinNode(); |
| 226 original.Split(branch, &left, &graph_, join); |
| 227 graph_ = original; |
| 228 } |
| 229 |
| 230 |
| 231 void FlowGraphBuilder::VisitContinueStatement(ContinueStatement* stmt) { |
| 232 SetStackOverflow(); |
| 233 } |
| 234 |
| 235 |
| 236 void FlowGraphBuilder::VisitBreakStatement(BreakStatement* stmt) { |
| 237 SetStackOverflow(); |
| 238 } |
| 239 |
| 240 |
| 241 void FlowGraphBuilder::VisitReturnStatement(ReturnStatement* stmt) { |
| 242 Visit(stmt->expression()); |
| 243 graph_.AppendInstruction(stmt); |
| 244 graph_.AppendNode(global_exit()); |
| 245 } |
| 246 |
| 247 |
| 248 void FlowGraphBuilder::VisitWithEnterStatement(WithEnterStatement* stmt) { |
| 249 Visit(stmt->expression()); |
| 250 graph_.AppendInstruction(stmt); |
| 251 } |
| 252 |
| 253 |
| 254 void FlowGraphBuilder::VisitWithExitStatement(WithExitStatement* stmt) { |
| 255 graph_.AppendInstruction(stmt); |
| 256 } |
| 257 |
| 258 |
| 259 void FlowGraphBuilder::VisitSwitchStatement(SwitchStatement* stmt) { |
| 260 SetStackOverflow(); |
| 261 } |
| 262 |
| 263 |
| 264 void FlowGraphBuilder::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 265 JoinNode* join = new JoinNode(); |
| 266 FlowGraph original = graph_; |
| 267 graph_ = FlowGraph::Empty(); |
| 268 Visit(stmt->body()); |
| 269 |
| 270 FlowGraph body = graph_; |
| 271 graph_ = FlowGraph::Empty(); |
| 272 Visit(stmt->cond()); |
| 273 |
| 274 BranchNode* branch = new BranchNode(); |
| 275 |
| 276 // Add body, condition and branch. |
| 277 original.AppendNode(join); |
| 278 original.AppendGraph(&body); |
| 279 original.AppendGraph(&graph_); // The condition. |
| 280 original.AppendNode(branch); |
| 281 |
| 282 // Tie the knot. |
| 283 branch->AddSuccessor(join); |
| 284 join->AddPredecessor(branch); |
| 285 |
| 286 graph_ = original; |
| 287 } |
| 288 |
| 289 |
| 290 void FlowGraphBuilder::VisitWhileStatement(WhileStatement* stmt) { |
| 291 JoinNode* join = new JoinNode(); |
| 292 FlowGraph original = graph_; |
| 293 graph_ = FlowGraph::Empty(); |
| 294 Visit(stmt->cond()); |
| 295 |
| 296 BranchNode* branch = new BranchNode(); |
| 297 FlowGraph condition = graph_; |
| 298 graph_ = FlowGraph::Empty(); |
| 299 Visit(stmt->body()); |
| 300 |
| 301 original.Loop(join, &condition, branch, &graph_); |
| 302 graph_ = original; |
| 303 } |
| 304 |
| 305 |
| 306 void FlowGraphBuilder::VisitForStatement(ForStatement* stmt) { |
| 307 if (stmt->init() != NULL) Visit(stmt->init()); |
| 308 |
| 309 JoinNode* join = new JoinNode(); |
| 310 FlowGraph original = graph_; |
| 311 graph_ = FlowGraph::Empty(); |
| 312 if (stmt->cond() != NULL) Visit(stmt->cond()); |
| 313 |
| 314 BranchNode* branch = new BranchNode(); |
| 315 FlowGraph condition = graph_; |
| 316 graph_ = FlowGraph::Empty(); |
| 317 Visit(stmt->body()); |
| 318 |
| 319 if (stmt->next() != NULL) Visit(stmt->next()); |
| 320 |
| 321 original.Loop(join, &condition, branch, &graph_); |
| 322 graph_ = original; |
| 323 } |
| 324 |
| 325 |
| 326 void FlowGraphBuilder::VisitForInStatement(ForInStatement* stmt) { |
| 327 Visit(stmt->enumerable()); |
| 328 |
| 329 JoinNode* join = new JoinNode(); |
| 330 FlowGraph empty; |
| 331 BranchNode* branch = new BranchNode(); |
| 332 FlowGraph original = graph_; |
| 333 graph_ = FlowGraph::Empty(); |
| 334 Visit(stmt->body()); |
| 335 |
| 336 original.Loop(join, &empty, branch, &graph_); |
| 337 graph_ = original; |
| 338 } |
| 339 |
| 340 |
| 341 void FlowGraphBuilder::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 342 SetStackOverflow(); |
| 343 } |
| 344 |
| 345 |
| 346 void FlowGraphBuilder::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 347 SetStackOverflow(); |
| 348 } |
| 349 |
| 350 |
| 351 void FlowGraphBuilder::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 352 graph_.AppendInstruction(stmt); |
| 353 } |
| 354 |
| 355 |
| 356 void FlowGraphBuilder::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 357 graph_.AppendInstruction(expr); |
| 358 } |
| 359 |
| 360 |
| 361 void FlowGraphBuilder::VisitFunctionBoilerplateLiteral( |
| 362 FunctionBoilerplateLiteral* expr) { |
| 363 graph_.AppendInstruction(expr); |
| 364 } |
| 365 |
| 366 |
| 367 void FlowGraphBuilder::VisitConditional(Conditional* expr) { |
| 368 Visit(expr->condition()); |
| 369 |
| 370 BranchNode* branch = new BranchNode(); |
| 371 FlowGraph original = graph_; |
| 372 graph_ = FlowGraph::Empty(); |
| 373 Visit(expr->then_expression()); |
| 374 |
| 375 FlowGraph left = graph_; |
| 376 graph_ = FlowGraph::Empty(); |
| 377 Visit(expr->else_expression()); |
| 378 |
| 379 JoinNode* join = new JoinNode(); |
| 380 original.Split(branch, &left, &graph_, join); |
| 381 graph_ = original; |
| 382 } |
| 383 |
| 384 |
| 385 void FlowGraphBuilder::VisitSlot(Slot* expr) { |
| 386 UNREACHABLE(); |
| 387 } |
| 388 |
| 389 |
| 390 void FlowGraphBuilder::VisitVariableProxy(VariableProxy* expr) { |
| 391 graph_.AppendInstruction(expr); |
| 392 } |
| 393 |
| 394 |
| 395 void FlowGraphBuilder::VisitLiteral(Literal* expr) { |
| 396 graph_.AppendInstruction(expr); |
| 397 } |
| 398 |
| 399 |
| 400 void FlowGraphBuilder::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 401 graph_.AppendInstruction(expr); |
| 402 } |
| 403 |
| 404 |
| 405 void FlowGraphBuilder::VisitObjectLiteral(ObjectLiteral* expr) { |
| 406 ZoneList<ObjectLiteral::Property*>* properties = expr->properties(); |
| 407 for (int i = 0, len = properties->length(); i < len; i++) { |
| 408 Visit(properties->at(i)->value()); |
| 409 } |
| 410 graph_.AppendInstruction(expr); |
| 411 } |
| 412 |
| 413 |
| 414 void FlowGraphBuilder::VisitArrayLiteral(ArrayLiteral* expr) { |
| 415 ZoneList<Expression*>* values = expr->values(); |
| 416 for (int i = 0, len = values->length(); i < len; i++) { |
| 417 Visit(values->at(i)); |
| 418 } |
| 419 graph_.AppendInstruction(expr); |
| 420 } |
| 421 |
| 422 |
| 423 void FlowGraphBuilder::VisitCatchExtensionObject(CatchExtensionObject* expr) { |
| 424 graph_.AppendInstruction(expr); |
| 425 } |
| 426 |
| 427 |
| 428 void FlowGraphBuilder::VisitAssignment(Assignment* expr) { |
| 429 Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 430 Property* prop = expr->target()->AsProperty(); |
| 431 // Left-hand side can be a variable or property (or reference error) but |
| 432 // not both. |
| 433 ASSERT(var == NULL || prop == NULL); |
| 434 if (var != NULL) { |
| 435 Visit(expr->value()); |
| 436 if (var->IsStackAllocated()) definitions_.Add(expr); |
| 437 |
| 438 } else if (prop != NULL) { |
| 439 Visit(prop->obj()); |
| 440 if (!prop->key()->IsPropertyName()) Visit(prop->key()); |
| 441 Visit(expr->value()); |
| 442 } |
| 443 graph_.AppendInstruction(expr); |
| 444 } |
| 445 |
| 446 |
| 447 void FlowGraphBuilder::VisitThrow(Throw* expr) { |
| 448 Visit(expr->exception()); |
| 449 graph_.AppendInstruction(expr); |
| 450 } |
| 451 |
| 452 |
| 453 void FlowGraphBuilder::VisitProperty(Property* expr) { |
| 454 Visit(expr->obj()); |
| 455 if (!expr->key()->IsPropertyName()) Visit(expr->key()); |
| 456 graph_.AppendInstruction(expr); |
| 457 } |
| 458 |
| 459 |
| 460 void FlowGraphBuilder::VisitCall(Call* expr) { |
| 461 Visit(expr->expression()); |
| 462 ZoneList<Expression*>* arguments = expr->arguments(); |
| 463 for (int i = 0, len = arguments->length(); i < len; i++) { |
| 464 Visit(arguments->at(i)); |
| 465 } |
| 466 graph_.AppendInstruction(expr); |
| 467 } |
| 468 |
| 469 |
| 470 void FlowGraphBuilder::VisitCallNew(CallNew* expr) { |
| 471 Visit(expr->expression()); |
| 472 ZoneList<Expression*>* arguments = expr->arguments(); |
| 473 for (int i = 0, len = arguments->length(); i < len; i++) { |
| 474 Visit(arguments->at(i)); |
| 475 } |
| 476 graph_.AppendInstruction(expr); |
| 477 } |
| 478 |
| 479 |
| 480 void FlowGraphBuilder::VisitCallRuntime(CallRuntime* expr) { |
| 481 ZoneList<Expression*>* arguments = expr->arguments(); |
| 482 for (int i = 0, len = arguments->length(); i < len; i++) { |
| 483 Visit(arguments->at(i)); |
| 484 } |
| 485 graph_.AppendInstruction(expr); |
| 486 } |
| 487 |
| 488 |
| 489 void FlowGraphBuilder::VisitUnaryOperation(UnaryOperation* expr) { |
| 490 Visit(expr->expression()); |
| 491 graph_.AppendInstruction(expr); |
| 492 } |
| 493 |
| 494 |
| 495 void FlowGraphBuilder::VisitCountOperation(CountOperation* expr) { |
| 496 Visit(expr->expression()); |
| 497 Variable* var = expr->expression()->AsVariableProxy()->AsVariable(); |
| 498 if (var != NULL && var->IsStackAllocated()) { |
| 499 definitions_.Add(expr); |
| 500 } |
| 501 graph_.AppendInstruction(expr); |
| 502 } |
| 503 |
| 504 |
| 505 void FlowGraphBuilder::VisitBinaryOperation(BinaryOperation* expr) { |
| 506 Visit(expr->left()); |
| 507 |
| 508 switch (expr->op()) { |
| 509 case Token::COMMA: |
| 510 Visit(expr->right()); |
| 511 break; |
| 512 |
| 513 case Token::OR: { |
| 514 BranchNode* branch = new BranchNode(); |
| 515 FlowGraph original = graph_; |
| 516 graph_ = FlowGraph::Empty(); |
| 517 Visit(expr->right()); |
| 518 FlowGraph empty; |
| 519 JoinNode* join = new JoinNode(); |
| 520 original.Split(branch, &empty, &graph_, join); |
| 521 graph_ = original; |
| 522 break; |
| 523 } |
| 524 |
| 525 case Token::AND: { |
| 526 BranchNode* branch = new BranchNode(); |
| 527 FlowGraph original = graph_; |
| 528 graph_ = FlowGraph::Empty(); |
| 529 Visit(expr->right()); |
| 530 FlowGraph empty; |
| 531 JoinNode* join = new JoinNode(); |
| 532 original.Split(branch, &graph_, &empty, join); |
| 533 graph_ = original; |
| 534 break; |
| 535 } |
| 536 |
| 537 case Token::BIT_OR: |
| 538 case Token::BIT_XOR: |
| 539 case Token::BIT_AND: |
| 540 case Token::SHL: |
| 541 case Token::SAR: |
| 542 case Token::SHR: |
| 543 case Token::ADD: |
| 544 case Token::SUB: |
| 545 case Token::MUL: |
| 546 case Token::DIV: |
| 547 case Token::MOD: |
| 548 Visit(expr->right()); |
| 549 graph_.AppendInstruction(expr); |
| 550 break; |
| 551 |
| 552 default: |
| 553 UNREACHABLE(); |
| 554 } |
| 555 } |
| 556 |
| 557 |
| 558 void FlowGraphBuilder::VisitCompareOperation(CompareOperation* expr) { |
| 559 Visit(expr->left()); |
| 560 Visit(expr->right()); |
| 561 graph_.AppendInstruction(expr); |
| 562 } |
| 563 |
| 564 |
| 565 void FlowGraphBuilder::VisitThisFunction(ThisFunction* expr) { |
| 566 graph_.AppendInstruction(expr); |
| 567 } |
| 568 |
| 569 |
| 36 void AstLabeler::Label(CompilationInfo* info) { | 570 void AstLabeler::Label(CompilationInfo* info) { |
| 37 info_ = info; | 571 info_ = info; |
| 38 VisitStatements(info_->function()->body()); | 572 VisitStatements(info_->function()->body()); |
| 39 } | 573 } |
| 40 | 574 |
| 41 | 575 |
| 42 void AstLabeler::VisitStatements(ZoneList<Statement*>* stmts) { | 576 void AstLabeler::VisitStatements(ZoneList<Statement*>* stmts) { |
| 43 for (int i = 0, len = stmts->length(); i < len; i++) { | 577 for (int i = 0, len = stmts->length(); i < len; i++) { |
| 44 Visit(stmts->at(i)); | 578 Visit(stmts->at(i)); |
| 45 } | 579 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | 731 |
| 198 | 732 |
| 199 void AstLabeler::VisitAssignment(Assignment* expr) { | 733 void AstLabeler::VisitAssignment(Assignment* expr) { |
| 200 Property* prop = expr->target()->AsProperty(); | 734 Property* prop = expr->target()->AsProperty(); |
| 201 ASSERT(prop != NULL); | 735 ASSERT(prop != NULL); |
| 202 ASSERT(prop->key()->IsPropertyName()); | 736 ASSERT(prop->key()->IsPropertyName()); |
| 203 VariableProxy* proxy = prop->obj()->AsVariableProxy(); | 737 VariableProxy* proxy = prop->obj()->AsVariableProxy(); |
| 204 USE(proxy); | 738 USE(proxy); |
| 205 ASSERT(proxy != NULL && proxy->var()->is_this()); | 739 ASSERT(proxy != NULL && proxy->var()->is_this()); |
| 206 info()->set_has_this_properties(true); | 740 info()->set_has_this_properties(true); |
| 741 |
| 742 prop->obj()->set_num(AstNode::kNoNumber); |
| 743 prop->key()->set_num(AstNode::kNoNumber); |
| 207 Visit(expr->value()); | 744 Visit(expr->value()); |
| 208 expr->set_num(next_number_++); | 745 expr->set_num(next_number_++); |
| 209 } | 746 } |
| 210 | 747 |
| 211 | 748 |
| 212 void AstLabeler::VisitThrow(Throw* expr) { | 749 void AstLabeler::VisitThrow(Throw* expr) { |
| 213 UNREACHABLE(); | 750 UNREACHABLE(); |
| 214 } | 751 } |
| 215 | 752 |
| 216 | 753 |
| 217 void AstLabeler::VisitProperty(Property* expr) { | 754 void AstLabeler::VisitProperty(Property* expr) { |
| 218 ASSERT(expr->key()->IsPropertyName()); | 755 ASSERT(expr->key()->IsPropertyName()); |
| 219 VariableProxy* proxy = expr->obj()->AsVariableProxy(); | 756 VariableProxy* proxy = expr->obj()->AsVariableProxy(); |
| 220 USE(proxy); | 757 USE(proxy); |
| 221 ASSERT(proxy != NULL && proxy->var()->is_this()); | 758 ASSERT(proxy != NULL && proxy->var()->is_this()); |
| 222 info()->set_has_this_properties(true); | 759 info()->set_has_this_properties(true); |
| 760 |
| 761 expr->obj()->set_num(AstNode::kNoNumber); |
| 762 expr->key()->set_num(AstNode::kNoNumber); |
| 223 expr->set_num(next_number_++); | 763 expr->set_num(next_number_++); |
| 224 } | 764 } |
| 225 | 765 |
| 226 | 766 |
| 227 void AstLabeler::VisitCall(Call* expr) { | 767 void AstLabeler::VisitCall(Call* expr) { |
| 228 UNREACHABLE(); | 768 UNREACHABLE(); |
| 229 } | 769 } |
| 230 | 770 |
| 231 | 771 |
| 232 void AstLabeler::VisitCallNew(CallNew* expr) { | 772 void AstLabeler::VisitCallNew(CallNew* expr) { |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 void LivenessAnalyzer::VisitCompareOperation(CompareOperation* expr) { | 1091 void LivenessAnalyzer::VisitCompareOperation(CompareOperation* expr) { |
| 552 UNREACHABLE(); | 1092 UNREACHABLE(); |
| 553 } | 1093 } |
| 554 | 1094 |
| 555 | 1095 |
| 556 void LivenessAnalyzer::VisitThisFunction(ThisFunction* expr) { | 1096 void LivenessAnalyzer::VisitThisFunction(ThisFunction* expr) { |
| 557 UNREACHABLE(); | 1097 UNREACHABLE(); |
| 558 } | 1098 } |
| 559 | 1099 |
| 560 | 1100 |
| 1101 #ifdef DEBUG |
| 1102 |
| 1103 // Print a textual representation of an instruction in a flow graph. Using |
| 1104 // the AstVisitor is overkill because there is no recursion here. It is |
| 1105 // only used for printing in debug mode. |
| 1106 class TextInstructionPrinter: public AstVisitor { |
| 1107 public: |
| 1108 TextInstructionPrinter() {} |
| 1109 |
| 1110 private: |
| 1111 // AST node visit functions. |
| 1112 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); |
| 1113 AST_NODE_LIST(DECLARE_VISIT) |
| 1114 #undef DECLARE_VISIT |
| 1115 |
| 1116 DISALLOW_COPY_AND_ASSIGN(TextInstructionPrinter); |
| 1117 }; |
| 1118 |
| 1119 |
| 1120 void TextInstructionPrinter::VisitDeclaration(Declaration* decl) { |
| 1121 UNREACHABLE(); |
| 1122 } |
| 1123 |
| 1124 |
| 1125 void TextInstructionPrinter::VisitBlock(Block* stmt) { |
| 1126 PrintF("Block"); |
| 1127 } |
| 1128 |
| 1129 |
| 1130 void TextInstructionPrinter::VisitExpressionStatement( |
| 1131 ExpressionStatement* stmt) { |
| 1132 PrintF("ExpressionStatement"); |
| 1133 } |
| 1134 |
| 1135 |
| 1136 void TextInstructionPrinter::VisitEmptyStatement(EmptyStatement* stmt) { |
| 1137 PrintF("EmptyStatement"); |
| 1138 } |
| 1139 |
| 1140 |
| 1141 void TextInstructionPrinter::VisitIfStatement(IfStatement* stmt) { |
| 1142 PrintF("IfStatement"); |
| 1143 } |
| 1144 |
| 1145 |
| 1146 void TextInstructionPrinter::VisitContinueStatement(ContinueStatement* stmt) { |
| 1147 UNREACHABLE(); |
| 1148 } |
| 1149 |
| 1150 |
| 1151 void TextInstructionPrinter::VisitBreakStatement(BreakStatement* stmt) { |
| 1152 UNREACHABLE(); |
| 1153 } |
| 1154 |
| 1155 |
| 1156 void TextInstructionPrinter::VisitReturnStatement(ReturnStatement* stmt) { |
| 1157 PrintF("return @%d", stmt->expression()->num()); |
| 1158 } |
| 1159 |
| 1160 |
| 1161 void TextInstructionPrinter::VisitWithEnterStatement(WithEnterStatement* stmt) { |
| 1162 PrintF("WithEnterStatement"); |
| 1163 } |
| 1164 |
| 1165 |
| 1166 void TextInstructionPrinter::VisitWithExitStatement(WithExitStatement* stmt) { |
| 1167 PrintF("WithExitStatement"); |
| 1168 } |
| 1169 |
| 1170 |
| 1171 void TextInstructionPrinter::VisitSwitchStatement(SwitchStatement* stmt) { |
| 1172 UNREACHABLE(); |
| 1173 } |
| 1174 |
| 1175 |
| 1176 void TextInstructionPrinter::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 1177 PrintF("DoWhileStatement"); |
| 1178 } |
| 1179 |
| 1180 |
| 1181 void TextInstructionPrinter::VisitWhileStatement(WhileStatement* stmt) { |
| 1182 PrintF("WhileStatement"); |
| 1183 } |
| 1184 |
| 1185 |
| 1186 void TextInstructionPrinter::VisitForStatement(ForStatement* stmt) { |
| 1187 PrintF("ForStatement"); |
| 1188 } |
| 1189 |
| 1190 |
| 1191 void TextInstructionPrinter::VisitForInStatement(ForInStatement* stmt) { |
| 1192 PrintF("ForInStatement"); |
| 1193 } |
| 1194 |
| 1195 |
| 1196 void TextInstructionPrinter::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 1197 UNREACHABLE(); |
| 1198 } |
| 1199 |
| 1200 |
| 1201 void TextInstructionPrinter::VisitTryFinallyStatement( |
| 1202 TryFinallyStatement* stmt) { |
| 1203 UNREACHABLE(); |
| 1204 } |
| 1205 |
| 1206 |
| 1207 void TextInstructionPrinter::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 1208 PrintF("DebuggerStatement"); |
| 1209 } |
| 1210 |
| 1211 |
| 1212 void TextInstructionPrinter::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 1213 PrintF("FunctionLiteral"); |
| 1214 } |
| 1215 |
| 1216 |
| 1217 void TextInstructionPrinter::VisitFunctionBoilerplateLiteral( |
| 1218 FunctionBoilerplateLiteral* expr) { |
| 1219 PrintF("FunctionBoilerplateLiteral"); |
| 1220 } |
| 1221 |
| 1222 |
| 1223 void TextInstructionPrinter::VisitConditional(Conditional* expr) { |
| 1224 PrintF("Conditional"); |
| 1225 } |
| 1226 |
| 1227 |
| 1228 void TextInstructionPrinter::VisitSlot(Slot* expr) { |
| 1229 UNREACHABLE(); |
| 1230 } |
| 1231 |
| 1232 |
| 1233 void TextInstructionPrinter::VisitVariableProxy(VariableProxy* expr) { |
| 1234 Variable* var = expr->AsVariable(); |
| 1235 if (var != NULL) { |
| 1236 SmartPointer<char> name = var->name()->ToCString(); |
| 1237 PrintF("%s", *name); |
| 1238 } else { |
| 1239 ASSERT(expr->AsProperty() != NULL); |
| 1240 VisitProperty(expr->AsProperty()); |
| 1241 } |
| 1242 } |
| 1243 |
| 1244 |
| 1245 void TextInstructionPrinter::VisitLiteral(Literal* expr) { |
| 1246 expr->handle()->ShortPrint(); |
| 1247 } |
| 1248 |
| 1249 |
| 1250 void TextInstructionPrinter::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 1251 PrintF("RegExpLiteral"); |
| 1252 } |
| 1253 |
| 1254 |
| 1255 void TextInstructionPrinter::VisitObjectLiteral(ObjectLiteral* expr) { |
| 1256 PrintF("ObjectLiteral"); |
| 1257 } |
| 1258 |
| 1259 |
| 1260 void TextInstructionPrinter::VisitArrayLiteral(ArrayLiteral* expr) { |
| 1261 PrintF("ArrayLiteral"); |
| 1262 } |
| 1263 |
| 1264 |
| 1265 void TextInstructionPrinter::VisitCatchExtensionObject( |
| 1266 CatchExtensionObject* expr) { |
| 1267 PrintF("CatchExtensionObject"); |
| 1268 } |
| 1269 |
| 1270 |
| 1271 void TextInstructionPrinter::VisitAssignment(Assignment* expr) { |
| 1272 Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 1273 Property* prop = expr->target()->AsProperty(); |
| 1274 |
| 1275 if (var != NULL) { |
| 1276 SmartPointer<char> name = var->name()->ToCString(); |
| 1277 PrintF("%s %s @%d", |
| 1278 *name, |
| 1279 Token::String(expr->op()), |
| 1280 expr->value()->num()); |
| 1281 } else if (prop != NULL) { |
| 1282 if (prop->key()->IsPropertyName()) { |
| 1283 PrintF("@%d.", prop->obj()->num()); |
| 1284 ASSERT(prop->key()->AsLiteral() != NULL); |
| 1285 prop->key()->AsLiteral()->handle()->Print(); |
| 1286 PrintF(" %s @%d", |
| 1287 Token::String(expr->op()), |
| 1288 expr->value()->num()); |
| 1289 } else { |
| 1290 PrintF("@%d[@%d] %s @%d", |
| 1291 prop->obj()->num(), |
| 1292 prop->key()->num(), |
| 1293 Token::String(expr->op()), |
| 1294 expr->value()->num()); |
| 1295 } |
| 1296 } else { |
| 1297 // Throw reference error. |
| 1298 Visit(expr->target()); |
| 1299 } |
| 1300 } |
| 1301 |
| 1302 |
| 1303 void TextInstructionPrinter::VisitThrow(Throw* expr) { |
| 1304 PrintF("throw @%d", expr->exception()->num()); |
| 1305 } |
| 1306 |
| 1307 |
| 1308 void TextInstructionPrinter::VisitProperty(Property* expr) { |
| 1309 if (expr->key()->IsPropertyName()) { |
| 1310 PrintF("@%d.", expr->obj()->num()); |
| 1311 ASSERT(expr->key()->AsLiteral() != NULL); |
| 1312 expr->key()->AsLiteral()->handle()->Print(); |
| 1313 } else { |
| 1314 PrintF("@%d[@%d]", expr->obj()->num(), expr->key()->num()); |
| 1315 } |
| 1316 } |
| 1317 |
| 1318 |
| 1319 void TextInstructionPrinter::VisitCall(Call* expr) { |
| 1320 PrintF("@%d(", expr->expression()->num()); |
| 1321 ZoneList<Expression*>* arguments = expr->arguments(); |
| 1322 for (int i = 0, len = arguments->length(); i < len; i++) { |
| 1323 if (i != 0) PrintF(", "); |
| 1324 PrintF("@%d", arguments->at(i)->num()); |
| 1325 } |
| 1326 PrintF(")"); |
| 1327 } |
| 1328 |
| 1329 |
| 1330 void TextInstructionPrinter::VisitCallNew(CallNew* expr) { |
| 1331 PrintF("new @%d(", expr->expression()->num()); |
| 1332 ZoneList<Expression*>* arguments = expr->arguments(); |
| 1333 for (int i = 0, len = arguments->length(); i < len; i++) { |
| 1334 if (i != 0) PrintF(", "); |
| 1335 PrintF("@%d", arguments->at(i)->num()); |
| 1336 } |
| 1337 PrintF(")"); |
| 1338 } |
| 1339 |
| 1340 |
| 1341 void TextInstructionPrinter::VisitCallRuntime(CallRuntime* expr) { |
| 1342 SmartPointer<char> name = expr->name()->ToCString(); |
| 1343 PrintF("%s(", *name); |
| 1344 ZoneList<Expression*>* arguments = expr->arguments(); |
| 1345 for (int i = 0, len = arguments->length(); i < len; i++) { |
| 1346 if (i != 0) PrintF(", "); |
| 1347 PrintF("@%d", arguments->at(i)->num()); |
| 1348 } |
| 1349 PrintF(")"); |
| 1350 } |
| 1351 |
| 1352 |
| 1353 void TextInstructionPrinter::VisitUnaryOperation(UnaryOperation* expr) { |
| 1354 PrintF("%s(@%d)", Token::String(expr->op()), expr->expression()->num()); |
| 1355 } |
| 1356 |
| 1357 |
| 1358 void TextInstructionPrinter::VisitCountOperation(CountOperation* expr) { |
| 1359 if (expr->is_prefix()) { |
| 1360 PrintF("%s@%d", Token::String(expr->op()), expr->expression()->num()); |
| 1361 } else { |
| 1362 PrintF("@%d%s", expr->expression()->num(), Token::String(expr->op())); |
| 1363 } |
| 1364 } |
| 1365 |
| 1366 |
| 1367 void TextInstructionPrinter::VisitBinaryOperation(BinaryOperation* expr) { |
| 1368 ASSERT(expr->op() != Token::COMMA); |
| 1369 ASSERT(expr->op() != Token::OR); |
| 1370 ASSERT(expr->op() != Token::AND); |
| 1371 PrintF("@%d %s @%d", |
| 1372 expr->left()->num(), |
| 1373 Token::String(expr->op()), |
| 1374 expr->right()->num()); |
| 1375 } |
| 1376 |
| 1377 |
| 1378 void TextInstructionPrinter::VisitCompareOperation(CompareOperation* expr) { |
| 1379 PrintF("@%d %s @%d", |
| 1380 expr->left()->num(), |
| 1381 Token::String(expr->op()), |
| 1382 expr->right()->num()); |
| 1383 } |
| 1384 |
| 1385 |
| 1386 void TextInstructionPrinter::VisitThisFunction(ThisFunction* expr) { |
| 1387 PrintF("ThisFunction"); |
| 1388 } |
| 1389 |
| 1390 |
| 1391 static int node_count = 0; |
| 1392 static int instruction_count = 0; |
| 1393 |
| 1394 |
| 1395 void Node::AssignNumbers() { |
| 1396 set_number(node_count++); |
| 1397 } |
| 1398 |
| 1399 |
| 1400 void BlockNode::AssignNumbers() { |
| 1401 set_number(node_count++); |
| 1402 for (int i = 0, len = instructions_.length(); i < len; i++) { |
| 1403 instructions_[i]->set_num(instruction_count++); |
| 1404 } |
| 1405 } |
| 1406 |
| 1407 |
| 1408 void EntryNode::PrintText() { |
| 1409 PrintF("L%d: Entry\n", number()); |
| 1410 PrintF("goto L%d\n\n", successor_->number()); |
| 1411 } |
| 1412 |
| 1413 void ExitNode::PrintText() { |
| 1414 PrintF("L%d: Exit\n\n", number()); |
| 1415 } |
| 1416 |
| 1417 |
| 1418 void BlockNode::PrintText() { |
| 1419 // Print the instructions in the block. |
| 1420 PrintF("L%d: Block\n", number()); |
| 1421 TextInstructionPrinter printer; |
| 1422 for (int i = 0, len = instructions_.length(); i < len; i++) { |
| 1423 PrintF("%d ", instructions_[i]->num()); |
| 1424 printer.Visit(instructions_[i]); |
| 1425 PrintF("\n"); |
| 1426 } |
| 1427 PrintF("goto L%d\n\n", successor_->number()); |
| 1428 } |
| 1429 |
| 1430 |
| 1431 void BranchNode::PrintText() { |
| 1432 PrintF("L%d: Branch\n", number()); |
| 1433 PrintF("goto (L%d, L%d)\n\n", successor0_->number(), successor1_->number()); |
| 1434 } |
| 1435 |
| 1436 |
| 1437 void JoinNode::PrintText() { |
| 1438 PrintF("L%d: Join(", number()); |
| 1439 for (int i = 0, len = predecessors_.length(); i < len; i++) { |
| 1440 if (i != 0) PrintF(", "); |
| 1441 PrintF("L%d", predecessors_[i]->number()); |
| 1442 } |
| 1443 PrintF(")\ngoto L%d\n\n", successor_->number()); |
| 1444 } |
| 1445 |
| 1446 |
| 1447 void FlowGraph::PrintText(ZoneList<Node*>* postorder) { |
| 1448 PrintF("\n========\n"); |
| 1449 |
| 1450 // Number nodes and instructions in reverse postorder. |
| 1451 node_count = 0; |
| 1452 instruction_count = 0; |
| 1453 for (int i = postorder->length() - 1; i >= 0; i--) { |
| 1454 postorder->at(i)->AssignNumbers(); |
| 1455 } |
| 1456 |
| 1457 // Print basic blocks in reverse postorder. |
| 1458 for (int i = postorder->length() - 1; i >= 0; i--) { |
| 1459 postorder->at(i)->PrintText(); |
| 1460 } |
| 1461 } |
| 1462 |
| 1463 |
| 1464 #endif // defined(DEBUG) |
| 1465 |
| 1466 |
| 561 } } // namespace v8::internal | 1467 } } // namespace v8::internal |
| OLD | NEW |