blob: 5d28f8aa7209dc9d84799a99a75ec006a519b51d [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
Dragos Sbirleadb063062013-07-23 16:29:09 -070020#include "base/stringprintf.h"
Dragos Sbirlea81f79a62013-07-23 14:31:47 -070021
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070022namespace sea_ir {
23class Region;
24class IRVisitor;
25
26class IVisitable {
27 public:
28 virtual void Accept(IRVisitor* visitor) = 0;
29 virtual ~IVisitable() {}
30};
31
32// This abstract class provides the essential services that
33// we want each SEA IR element should have.
34// At the moment, these are:
35// - an id and corresponding string representation.
36// - a .dot graph language representation for .dot output.
37//
38// Note that SEA IR nodes could also be Regions, Projects
39// which are not instructions.
40class SeaNode: public IVisitable {
41 public:
42 explicit SeaNode():id_(GetNewId()), string_id_() {
Dragos Sbirleadb063062013-07-23 16:29:09 -070043 string_id_ = art::StringPrintf("%d", id_);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070044 }
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 Sbirleadb063062013-07-23 16:29:09 -070074 static int current_max_node_id_;
Dragos Sbirleaa86acf72013-07-23 14:11:37 -070075 // Creating new instances of sea node objects should not be done through copy or assignment
76 // operators because that would lead to duplication of their unique ids.
77 DISALLOW_COPY_AND_ASSIGN(SeaNode);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070078};
Brian Carlstrom7934ac22013-07-26 10:54:15 -070079} // namespace sea_ir
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070080#endif // ART_COMPILER_SEA_IR_SEA_NODE_H_