blob: 7d198b228285e701b2ae440039207190c2ec5d67 [file] [log] [blame]
Dragos Sbirlea0e260a32013-06-21 09:20:34 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_SEA_IR_SEA_NODE_H_
18#define ART_COMPILER_SEA_IR_SEA_NODE_H_
19
20namespace sea_ir {
21class Region;
22class IRVisitor;
23
24class IVisitable {
25 public:
26 virtual void Accept(IRVisitor* visitor) = 0;
27 virtual ~IVisitable() {}
28};
29
30// This abstract class provides the essential services that
31// we want each SEA IR element should have.
32// At the moment, these are:
33// - an id and corresponding string representation.
34// - a .dot graph language representation for .dot output.
35//
36// Note that SEA IR nodes could also be Regions, Projects
37// which are not instructions.
38class SeaNode: public IVisitable {
39 public:
40 explicit SeaNode():id_(GetNewId()), string_id_() {
41 std::stringstream ss;
42 ss << id_;
43 string_id_.append(ss.str());
44 }
45 // Adds CFG predecessors and successors to each block.
46 void AddSuccessor(Region* successor);
47 void AddPredecessor(Region* predecesor);
48
49 // Returns the id of the current block as string
50 const std::string& StringId() const {
51 return string_id_;
52 }
53 // Returns the id of this node as int. The id is supposed to be unique among
54 // all instances of all subclasses of this class.
55 int Id() const {
56 return id_;
57 }
58 // Appends to @result a dot language formatted string representing the node and
59 // (by convention) outgoing edges, so that the composition of theToDot() of all nodes
60 // builds a complete dot graph, but without prolog ("digraph {") and epilog ("}").
61 virtual void ToDot(std::string& result) const = 0;
62
Dragos Sbirleaa86acf72013-07-23 14:11:37 -070063 virtual ~SeaNode() { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070064
65 protected:
66 static int GetNewId() {
67 return current_max_node_id_++;
68 }
69
70 const int id_;
71 std::string string_id_;
72
73 private:
Dragos Sbirleaa86acf72013-07-23 14:11:37 -070074 // Creating new instances of sea node objects should not be done through copy or assignment
75 // operators because that would lead to duplication of their unique ids.
76 DISALLOW_COPY_AND_ASSIGN(SeaNode);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070077 static int current_max_node_id_;
78};
79} // end namespace sea_ir
80#endif // ART_COMPILER_SEA_IR_SEA_NODE_H_