blob: a4fda4834fc737f23548aa78241cff2ffcc29430 [file] [log] [blame]
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00001//===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
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//
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000010// Compilation graph - implementation.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkova6730372008-05-12 16:31:42 +000014#include "Error.h"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000015#include "llvm/CompilerDriver/CompilationGraph.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000016
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000017#include "llvm/ADT/STLExtras.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000019#include "llvm/Support/DOTGraphTraits.h"
20#include "llvm/Support/GraphWriter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021
Mikhail Glushenkov02606582008-05-06 18:07:14 +000022#include <algorithm>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000023#include <iterator>
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000024#include <limits>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000025#include <queue>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <stdexcept>
27
28using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000029using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000030
31extern cl::list<std::string> InputFilenames;
32extern cl::opt<std::string> OutputFilename;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000033extern cl::list<std::string> Languages;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000034
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000035namespace llvmc {
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000036
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000037 const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
38 LanguageMap::const_iterator Lang = this->find(File.getSuffix());
39 if (Lang == this->end())
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000040 throw std::runtime_error("Unknown suffix: " + File.getSuffix());
41 return Lang->second;
42 }
43}
44
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000045namespace {
46
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047 /// ChooseEdge - Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000048 template <class C>
49 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000050 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000051 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000052 const Edge* MaxEdge = 0;
53 unsigned MaxWeight = 0;
54 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000055
56 for (typename C::const_iterator B = EdgesContainer.begin(),
57 E = EdgesContainer.end(); B != E; ++B) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000058 const Edge* e = B->getPtr();
59 unsigned EW = e->Weight(InLangs);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000060 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000061 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000062 MaxWeight = EW;
63 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000064 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000065 SingleMax = false;
66 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000067 }
68
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000069 if (!SingleMax)
70 throw std::runtime_error("Node " + NodeName +
71 ": multiple maximal outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000072 " Most probably a specification error.");
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000073 if (!MaxEdge)
74 throw std::runtime_error("Node " + NodeName +
75 ": no maximal outward edge found!"
76 " Most probably a specification error.");
77 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000078 }
79
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000080}
81
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000082void Node::AddEdge(Edge* Edg) {
83 // If there already was an edge between two nodes, modify it instead
84 // of adding a new edge.
85 const std::string& ToolName = Edg->ToolName();
86 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
87 B != E; ++B) {
88 if ((*B)->ToolName() == ToolName) {
89 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
90 return;
91 }
92 }
93 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
94}
95
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000096CompilationGraph::CompilationGraph() {
97 NodesMap["root"] = Node(this);
98}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000099
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000100Node& CompilationGraph::getNode(const std::string& ToolName) {
101 nodes_map_type::iterator I = NodesMap.find(ToolName);
102 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000103 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000104 return I->second;
105}
106
107const Node& CompilationGraph::getNode(const std::string& ToolName) const {
108 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
109 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000110 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 return I->second;
112}
113
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000114// Find the tools list corresponding to the given language name.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000115const CompilationGraph::tools_vector_type&
116CompilationGraph::getToolsVector(const std::string& LangName) const
117{
118 tools_map_type::const_iterator I = ToolsMap.find(LangName);
119 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000120 throw std::runtime_error("No tool corresponding to the language "
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000121 + LangName + " found");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000122 return I->second;
123}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000124
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000125void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000126 if (NodesMap.count(V->Name()) == 0)
127 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000128}
129
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000130void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
131 Node& B = getNode(Edg->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000132 if (A == "root") {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000133 const char** InLangs = B.ToolPtr->InputLanguages();
134 for (;*InLangs; ++InLangs)
135 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000136 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000137 }
138 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000139 Node& N = getNode(A);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000140 N.AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000141 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000142 // Increase the inward edge counter.
143 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000144}
145
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000146namespace {
147 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
148 const std::string& Suffix) {
Mikhail Glushenkov73296102008-05-30 06:29:17 +0000149 sys::Path Out;
150
151 // Make sure we don't end up with path names like '/file.o' if the
152 // TempDir is empty.
153 if (TempDir.empty()) {
154 Out.set(BaseName);
155 }
156 else {
157 Out = TempDir;
158 Out.appendComponent(BaseName);
159 }
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000160 Out.appendSuffix(Suffix);
Mikhail Glushenkov74a0c282008-05-30 19:56:27 +0000161 // NOTE: makeUnique always *creates* a unique temporary file,
162 // which is good, since there will be no races. However, some
163 // tools do not like it when the output file already exists, so
164 // they have to be placated with -f or something like that.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000165 Out.makeUnique(true, NULL);
166 return Out;
167 }
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000168}
169
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000170// Pass input file through the chain until we bump into a Join node or
171// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000172void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
173 const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000174 const InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000175 const sys::Path& TempDir,
176 const LanguageMap& LangMap) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000177 bool Last = false;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000178 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000179 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000180
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000181 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000182 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000183 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000184
185 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000186 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
187 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000188 break;
189 }
190
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000191 // Since toolchains do not have to end with a Join node, we should
192 // check if this Node is the last.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000193 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000194 if (!OutputFilename.empty()) {
195 Out.set(OutputFilename);
196 }
197 else {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000198 Out.set(In.getBasename());
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000199 Out.appendSuffix(CurTool->OutputSuffix());
200 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000201 Last = true;
202 }
203 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000204 Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000205 }
206
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000207 if (int ret = CurTool->GenerateAction(In, Out, InLangs, LangMap).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000208 throw error_code(ret);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000209
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000210 if (Last)
211 return;
212
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000213 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000214 InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000215 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000216 In = Out; Out.clear();
217 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000218}
219
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000220// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000221// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000222const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000223FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
224 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000225
226 // Determine the input language.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000227 const std::string& InLanguage =
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000228 ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000229
230 // Add the current input language to the input language set.
231 InLangs.insert(InLanguage);
232
233 // Find the toolchain for the input language.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000234 const tools_vector_type& TV = getToolsVector(InLanguage);
235 if (TV.empty())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000236 throw std::runtime_error("No toolchain corresponding to language "
237 + InLanguage + " found");
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000238 return &getNode(ChooseEdge(TV, InLangs)->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000239}
240
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000241// Helper function used by Build().
242// Traverses initial portions of the toolchains (up to the first Join node).
243// This function is also responsible for handling the -x option.
244void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000245 const sys::Path& TempDir,
246 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000247 // This is related to -x option handling.
248 cl::list<std::string>::const_iterator xIter = Languages.begin(),
249 xBegin = xIter, xEnd = Languages.end();
250 bool xEmpty = true;
251 const std::string* xLanguage = 0;
252 unsigned xPos = 0, xPosNext = 0, filePos = 0;
253
254 if (xIter != xEnd) {
255 xEmpty = false;
256 xPos = Languages.getPosition(xIter - xBegin);
257 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
258 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
259 : Languages.getPosition(xNext - xBegin);
260 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
261 }
262
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000263 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000264 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000265 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000266 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000267
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000268 // Code for handling the -x option.
269 // Output: std::string* xLanguage (can be NULL).
270 if (!xEmpty) {
271 filePos = InputFilenames.getPosition(B - CB);
272
273 if (xPos < filePos) {
274 if (filePos < xPosNext) {
275 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
276 }
277 else { // filePos >= xPosNext
278 // Skip xIters while filePos > xPosNext
279 while (filePos > xPosNext) {
280 ++xIter;
281 xPos = xPosNext;
282
283 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
284 if (xNext == xEnd)
285 xPosNext = std::numeric_limits<unsigned>::max();
286 else
287 xPosNext = Languages.getPosition(xNext - xBegin);
288 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
289 }
290 }
291 }
292 }
293
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000294 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000295 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000296 // Pass file through the chain starting at head.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000297 PassThroughGraph(In, N, InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000298 }
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000299}
300
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000301// Sort the nodes in topological order.
302void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
303 std::queue<const Node*> Q;
304 Q.push(&getNode("root"));
305
306 while (!Q.empty()) {
307 const Node* A = Q.front();
308 Q.pop();
309 Out.push_back(A);
310 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
311 EB != EE; ++EB) {
312 Node* B = &getNode((*EB)->ToolName());
313 B->DecrInEdges();
314 if (B->HasNoInEdges())
315 Q.push(B);
316 }
317 }
318}
319
320namespace {
321 bool NotJoinNode(const Node* N) {
322 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
323 }
324}
325
326// Call TopologicalSort and filter the resulting list to include
327// only Join nodes.
328void CompilationGraph::
329TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
330 std::vector<const Node*> TopSorted;
331 TopologicalSort(TopSorted);
332 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
333 std::back_inserter(Out), NotJoinNode);
334}
335
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000336int CompilationGraph::Build (const sys::Path& TempDir,
337 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000338
339 InputLanguagesSet InLangs;
340
341 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000342 BuildInitial(InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000343
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000344 std::vector<const Node*> JTV;
345 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000346
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000347 // For all join nodes in topological order:
348 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
349 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000350
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000351 sys::Path Out;
352 const Node* CurNode = *B;
353 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
354 bool IsLast = false;
355
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000356 // Are there any files in the join list?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000357 if (JT->JoinListEmpty())
358 continue;
359
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000360 // Is this the last tool in the toolchain?
361 // NOTE: we can process several toolchains in parallel.
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000362 if (!CurNode->HasChildren() || JT->IsLast()) {
363 if (OutputFilename.empty()) {
364 Out.set("a");
365 Out.appendSuffix(JT->OutputSuffix());
366 }
367 else
368 Out.set(OutputFilename);
369 IsLast = true;
370 }
371 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000372 Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000373 }
374
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000375 if (int ret = JT->GenerateAction(Out, InLangs, LangMap).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000376 throw error_code(ret);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000377
378 if (!IsLast) {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000379 const Node* NextNode =
380 &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
381 CurNode->Name())->ToolName());
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000382 PassThroughGraph(Out, NextNode, InLangs, TempDir, LangMap);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000383 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000384 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000385
386 return 0;
387}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000388
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000389// Code related to graph visualization.
390
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000391namespace llvm {
392 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000393 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000394 : public DefaultDOTGraphTraits
395 {
396
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000397 template<typename GraphType>
398 static std::string getNodeLabel(const Node* N, const GraphType&)
399 {
400 if (N->ToolPtr)
401 if (N->ToolPtr->IsJoin())
402 return N->Name() + "\n (join" +
403 (N->HasChildren() ? ")"
404 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
405 else
406 return N->Name();
407 else
408 return "root";
409 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000410
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000411 template<typename EdgeIter>
412 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000413 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000414 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000415 }
416 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000417 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000418 std::string ret;
419
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000420 for (; *InLangs; ++InLangs) {
421 if (*(InLangs + 1)) {
422 ret += *InLangs;
423 ret += ", ";
424 }
425 else {
426 ret += *InLangs;
427 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000428 }
429
430 return ret;
431 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000432 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000433 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000434
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000435}
436
437void CompilationGraph::writeGraph() {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000438 std::ofstream O("compilation-graph.dot");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000439
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000440 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000441 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000442 O.close();
443 }
444 else {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000445 throw std::runtime_error("Error opening file 'compilation-graph.dot'"
446 " for writing!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000447 }
448}
449
450void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000451 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000452}