blob: bddd8bdf3a1a00dd0468f401df1332ce94a086c5 [file] [log] [blame]
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00001//===--- CompilationGraph.h - The LLVM Compiler Driver ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Compilation graph - definition.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
15#define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
16
17#include "AutoGenerated.h"
18#include "Tool.h"
19
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000020#include "llvm/ADT/GraphTraits.h"
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000021#include "llvm/ADT/IntrusiveRefCntPtr.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000022#include "llvm/ADT/iterator"
23#include "llvm/ADT/SmallVector.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000024#include "llvm/ADT/StringMap.h"
25#include "llvm/System/Path.h"
26
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000027#include <string>
28
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000029namespace llvmcc {
30
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000031 // An edge in the graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000032 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
33 public:
34 Edge(const std::string& T) : ToolName_(T) {}
35 virtual ~Edge() {};
36
37 const std::string& ToolName() const { return ToolName_; }
38 virtual bool isEnabled() const = 0;
39 virtual bool isDefault() const = 0;
40 private:
41 std::string ToolName_;
42 };
43
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000044 // Edges with no properties are instances of this class.
45 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000046 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000047 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000048 bool isEnabled() const { return false;}
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000049 bool isDefault() const { return true;}
50 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000051
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000052 // A node in the graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000053 struct Node {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000054 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
55 typedef container_type::iterator iterator;
56 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000057
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000058 Node() {}
59 Node(CompilationGraph* G) : OwningGraph(G) {}
60 Node(CompilationGraph* G, Tool* T) : OwningGraph(G), ToolPtr(T) {}
61
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000062 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000063 const std::string Name() const { return ToolPtr->Name(); }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000064
65 iterator EdgesBegin() { return OutEdges.begin(); }
66 const_iterator EdgesBegin() const { return OutEdges.begin(); }
67 iterator EdgesEnd() { return OutEdges.end(); }
68 const_iterator EdgesEnd() const { return OutEdges.end(); }
69
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000070 // Choose one of the edges based on command-line options.
71 const Edge* ChooseEdge() const;
72
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000073 // Takes ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000074 void AddEdge(Edge* E)
75 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
76
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000077 // Needed to implement NodeChildIterator/GraphTraits
78 CompilationGraph* OwningGraph;
79 // The corresponding Tool.
80 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
81 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000082 container_type OutEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000083 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000084
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000085 class NodesIterator;
86
87 class CompilationGraph {
88 typedef llvm::SmallVector<std::string, 3> tools_vector_type;
89 typedef llvm::StringMap<tools_vector_type> tools_map_type;
90 typedef llvm::StringMap<Node> nodes_map_type;
91
92 // Map from file extensions to language names.
93 LanguageMap ExtsToLangs;
94 // Map from language names to lists of tool names.
95 tools_map_type ToolsMap;
96 // Map from tool names to Tool objects.
97 nodes_map_type NodesMap;
98
99 public:
100
101 CompilationGraph();
102
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000103 // insertVertex - insert a new node into the graph. Takes
104 // ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000105 void insertNode(Tool* T);
106
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000107 // insertEdge - Insert a new edge into the graph. Takes ownership
108 // of the object.
109 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000110
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000111 // Build - Build target(s) from the input file set. Command-line
112 // options are passed implicitly as global variables.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000113 int Build(llvm::sys::Path const& tempDir) const;
114
115 // Return a reference to the node correponding to the given tool
116 // name. Throws std::runtime_error.
117 Node& getNode(const std::string& ToolName);
118 const Node& getNode(const std::string& ToolName) const;
119
120 // viewGraph - This function is meant for use from the debugger.
121 // You can just say 'call G->viewGraph()' and a ghostview window
122 // should pop up from the program, displaying the compilation
123 // graph. This depends on there being a 'dot' and 'gv' program
124 // in your path.
125 void viewGraph();
126
127 // Write a CompilationGraph.dot file.
128 void writeGraph();
129
130 // GraphTraits support
131 friend NodesIterator GraphBegin(CompilationGraph*);
132 friend NodesIterator GraphEnd(CompilationGraph*);
133 friend void PopulateCompilationGraph(CompilationGraph&);
134
135 private:
136 // Helper functions.
137
138 // Find out which language corresponds to the suffix of this file.
139 const std::string& getLanguage(const llvm::sys::Path& File) const;
140
141 // Return a reference to the list of tool names corresponding to
142 // the given language name. Throws std::runtime_error.
143 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000144
145 // Pass the input file through the toolchain.
146 const Tool* PassThroughGraph (llvm::sys::Path& In, llvm::sys::Path Out,
147 const llvm::sys::Path& TempDir,
148 PathVector& JoinList) const;
149
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000150 };
151
152 /// GraphTraits support code.
153
154 // Auxiliary class needed to implement GraphTraits support. Can be
155 // generalised to something like value_iterator for map-like
156 // containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000157 class NodesIterator : public llvm::StringMap<Node>::iterator {
158 typedef llvm::StringMap<Node>::iterator super;
159 typedef NodesIterator ThisType;
160 typedef Node* pointer;
161 typedef Node& reference;
162
163 public:
164 NodesIterator(super I) : super(I) {}
165
166 inline reference operator*() const {
167 return super::operator->()->second;
168 }
169 inline pointer operator->() const {
170 return &super::operator->()->second;
171 }
172 };
173
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000174 inline NodesIterator GraphBegin(CompilationGraph* G) {
175 return NodesIterator(G->NodesMap.begin());
176 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000177
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000178 inline NodesIterator GraphEnd(CompilationGraph* G) {
179 return NodesIterator(G->NodesMap.end());
180 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000181
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000182
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000183 // Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000184 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
185 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000186 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000187
188 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000189 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000190 public:
191 typedef Node* pointer;
192 typedef Node& reference;
193
194 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000195 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000196
197 const ThisType& operator=(const ThisType& I) {
198 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000199 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000200 return *this;
201 }
202
203 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000204 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000205 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000206 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000207
208 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000209 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000210 }
211 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000212 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000213 }
214
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000215 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000216 ThisType operator++(int) { // Postincrement
217 ThisType tmp = *this;
218 ++*this;
219 return tmp;
220 }
221
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000222 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000223 inline ThisType operator--(int) { // Postdecrement
224 ThisType tmp = *this;
225 --*this;
226 return tmp;
227 }
228
229 };
230}
231
232namespace llvm {
233 template <>
234 struct GraphTraits<llvmcc::CompilationGraph*> {
235 typedef llvmcc::CompilationGraph GraphType;
236 typedef llvmcc::Node NodeType;
237 typedef llvmcc::NodeChildIterator ChildIteratorType;
238
239 static NodeType* getEntryNode(GraphType* G) {
240 return &G->getNode("root");
241 }
242
243 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000244 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000245 }
246 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000247 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000248 }
249
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000250 typedef llvmcc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000251 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000252 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000253 }
254 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000255 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000256 }
257 };
258
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000259}
260
261#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H