P4C
The P4 Compiler
Loading...
Searching...
No Matches
graphs.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef BACKENDS_GRAPHS_GRAPHS_H_
9#define BACKENDS_GRAPHS_GRAPHS_H_
10
11#include <map>
12#include <optional>
13#include <utility> // std::pair
14#include <vector>
15
16#include <boost/graph/adjacency_list.hpp>
17#include <boost/graph/graph_traits.hpp>
18#include <boost/graph/graphviz.hpp>
19
20#include "frontends/p4/parserCallGraph.h"
21#include "ir/ir.h"
22#include "ir/visitor.h"
23#include "lib/cstring.h"
24
25namespace P4 {
26
27class ReferenceMap;
28class TypeMap;
29
30} // namespace P4
31
32namespace P4::graphs {
33
34using namespace P4::literals;
35
37 public:
38 virtual ~EdgeTypeIface() {}
39 virtual cstring label() const = 0;
40};
41
42class EdgeUnconditional : public EdgeTypeIface {
43 public:
44 EdgeUnconditional() = default;
45 cstring label() const override { return cstring::empty; };
46};
47
48class EdgeIf : public EdgeTypeIface {
49 public:
50 enum class Branch { TRUE, FALSE };
51 explicit EdgeIf(Branch branch) : branch(branch) {}
52 cstring label() const override {
53 switch (branch) {
54 case Branch::TRUE:
55 return "TRUE"_cs;
56 case Branch::FALSE:
57 return "FALSE"_cs;
58 }
59 BUG("unreachable");
60 return cstring::empty;
61 };
62
63 private:
64 Branch branch;
65};
66
67class EdgeSwitch : public EdgeTypeIface {
68 public:
69 explicit EdgeSwitch(const IR::Expression *labelExpr) : labelExpr(labelExpr) {}
70 cstring label() const override {
71 std::stringstream sstream;
72 labelExpr->dbprint(sstream);
73 return cstring(sstream);
74 };
75
76 private:
77 const IR::Expression *labelExpr;
78};
79
80class Graphs : public Inspector {
81 public:
82 enum class VertexType {
83 TABLE,
84 KEY,
85 ACTION,
86 CONDITION,
87 SWITCH,
88 STATEMENTS,
89 CONTROL,
90 OTHER,
91 STATE,
92 EMPTY
93 };
94 struct Vertex {
95 cstring name;
96 VertexType type;
97 };
98
104 using GraphvizAttributes = std::map<cstring, cstring>;
105 using vertexProperties = boost::property<boost::vertex_attribute_t, GraphvizAttributes, Vertex>;
106 using edgeProperties = boost::property<
107 boost::edge_attribute_t, GraphvizAttributes,
108 boost::property<boost::edge_name_t, cstring, boost::property<boost::edge_index_t, int>>>;
109 using graphProperties = boost::property<
110 boost::graph_name_t, std::string,
111 boost::property<
112 boost::graph_graph_attribute_t, GraphvizAttributes,
113 boost::property<boost::graph_vertex_attribute_t, GraphvizAttributes,
114 boost::property<boost::graph_edge_attribute_t, GraphvizAttributes>>>>;
115 using Graph_ = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
116 vertexProperties, edgeProperties, graphProperties>;
117 using Graph = boost::subgraph<Graph_>;
118 using vertex_t = boost::graph_traits<Graph>::vertex_descriptor;
119
120 using Parents = std::vector<std::pair<vertex_t, EdgeTypeIface *>>;
121
124 std::optional<vertex_t> merge_other_statements_into_vertex();
125
126 vertex_t add_vertex(const cstring &name, VertexType type);
127 vertex_t add_and_connect_vertex(const cstring &name, VertexType type);
128 void add_edge(const vertex_t &from, const vertex_t &to, const cstring &name);
135 void add_edge(const vertex_t &from, const vertex_t &to, const cstring &name,
136 unsigned cluster_id);
137
139 public:
140 void operator()(Graph &g) const {
141 auto vertices = boost::vertices(g);
142 for (auto &vit = vertices.first; vit != vertices.second; ++vit) {
143 const auto &vinfo = g[*vit];
144 auto attrs = boost::get(boost::vertex_attribute, g);
145 attrs[*vit]["label"_cs] = vinfo.name;
146 attrs[*vit]["style"_cs] = vertexTypeGetStyle(vinfo.type);
147 attrs[*vit]["shape"_cs] = vertexTypeGetShape(vinfo.type);
148 attrs[*vit]["margin"_cs] = vertexTypeGetMargin(vinfo.type);
149 }
150 auto edges = boost::edges(g);
151 for (auto &eit = edges.first; eit != edges.second; ++eit) {
152 auto attrs = boost::get(boost::edge_attribute, g);
153 attrs[*eit]["label"_cs] = boost::get(boost::edge_name, g, *eit);
154 }
155 }
156
157 private:
158 static cstring vertexTypeGetShape(VertexType type) {
159 switch (type) {
160 case VertexType::TABLE:
161 case VertexType::ACTION:
162 return "ellipse"_cs;
163 default:
164 return "rectangle"_cs;
165 }
166 BUG("unreachable");
167 return cstring::empty;
168 }
169
170 static cstring vertexTypeGetStyle(VertexType type) {
171 switch (type) {
172 case VertexType::CONTROL:
173 return "dashed"_cs;
174 case VertexType::EMPTY:
175 return "invis"_cs;
176 case VertexType::KEY:
177 case VertexType::CONDITION:
178 case VertexType::SWITCH:
179 return "rounded"_cs;
180 default:
181 return "solid"_cs;
182 }
183 BUG("unreachable");
184 return cstring::empty;
185 }
186
187 static cstring vertexTypeGetMargin(VertexType type) {
188 switch (type) {
189 default:
190 return cstring::empty;
191 }
192 }
193 }; // end class GraphAttributeSetter
194
195 protected:
196 Graph *g{nullptr};
197 vertex_t start_v{};
198 vertex_t exit_v{};
199 Parents parents{};
200 std::vector<const IR::Statement *> statementsStack{};
201
202 private:
207 void limitStringSize(std::stringstream &sstream, std::stringstream &helper_sstream);
208};
209
210} // namespace P4::graphs
211
212#endif /* BACKENDS_GRAPHS_GRAPHS_H_ */
Class used to encode maps from paths to declarations.
Definition referenceMap.h:58
Definition typeMap.h:32
Definition cstring.h:76
Definition graphs.h:36
Definition graphs.h:80
std::optional< vertex_t > merge_other_statements_into_vertex()
Definition graphs.cpp:50
std::map< cstring, cstring > GraphvizAttributes
Definition graphs.h:104
Definition graphs.h:94
Definition controls.cpp:19
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13