1// Copyright 2013 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/node.h"
6#include "src/compiler/node-properties.h"
7#include "src/compiler/node-properties-inl.h"
8#include "src/compiler/schedule.h"
9#include "src/ostreams.h"
10
11namespace v8 {
12namespace internal {
13namespace compiler {
14
15OStream& operator<<(OStream& os, const BasicBlockData::Control& c) {
16  switch (c) {
17    case BasicBlockData::kNone:
18      return os << "none";
19    case BasicBlockData::kGoto:
20      return os << "goto";
21    case BasicBlockData::kBranch:
22      return os << "branch";
23    case BasicBlockData::kReturn:
24      return os << "return";
25    case BasicBlockData::kThrow:
26      return os << "throw";
27  }
28  UNREACHABLE();
29  return os;
30}
31
32
33OStream& operator<<(OStream& os, const Schedule& s) {
34  // TODO(svenpanne) Const-correct the RPO stuff/iterators.
35  BasicBlockVector* rpo = const_cast<Schedule*>(&s)->rpo_order();
36  for (BasicBlockVectorIter i = rpo->begin(); i != rpo->end(); ++i) {
37    BasicBlock* block = *i;
38    os << "--- BLOCK B" << block->id();
39    if (block->PredecessorCount() != 0) os << " <- ";
40    BasicBlock::Predecessors predecessors = block->predecessors();
41    bool comma = false;
42    for (BasicBlock::Predecessors::iterator j = predecessors.begin();
43         j != predecessors.end(); ++j) {
44      if (comma) os << ", ";
45      comma = true;
46      os << "B" << (*j)->id();
47    }
48    os << " ---\n";
49    for (BasicBlock::const_iterator j = block->begin(); j != block->end();
50         ++j) {
51      Node* node = *j;
52      os << "  " << *node;
53      if (!NodeProperties::IsControl(node)) {
54        Bounds bounds = NodeProperties::GetBounds(node);
55        os << " : ";
56        bounds.lower->PrintTo(os);
57        if (!bounds.upper->Is(bounds.lower)) {
58          os << "..";
59          bounds.upper->PrintTo(os);
60        }
61      }
62      os << "\n";
63    }
64    BasicBlock::Control control = block->control_;
65    if (control != BasicBlock::kNone) {
66      os << "  ";
67      if (block->control_input_ != NULL) {
68        os << *block->control_input_;
69      } else {
70        os << "Goto";
71      }
72      os << " -> ";
73      BasicBlock::Successors successors = block->successors();
74      comma = false;
75      for (BasicBlock::Successors::iterator j = successors.begin();
76           j != successors.end(); ++j) {
77        if (comma) os << ", ";
78        comma = true;
79        os << "B" << (*j)->id();
80      }
81      os << "\n";
82    }
83  }
84  return os;
85}
86}  // namespace compiler
87}  // namespace internal
88}  // namespace v8
89