2008-05-06 16:34:12 +00:00
|
|
|
//===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open
|
|
|
|
// Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-05-06 16:34:12 +00:00
|
|
|
// Compilation graph - implementation.
|
2008-03-23 08:57:20 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-06 16:34:12 +00:00
|
|
|
#include "CompilationGraph.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-05-06 16:35:25 +00:00
|
|
|
#include "llvm/Support/DOTGraphTraits.h"
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace llvm;
|
2008-05-06 16:35:25 +00:00
|
|
|
using namespace llvmcc;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
|
|
|
extern cl::list<std::string> InputFilenames;
|
|
|
|
extern cl::opt<std::string> OutputFilename;
|
|
|
|
|
2008-05-06 17:23:50 +00:00
|
|
|
// Choose one of the edges based on command-line options.
|
|
|
|
const Edge* Node::ChooseEdge() const {
|
|
|
|
const Edge* DefaultEdge = 0;
|
|
|
|
for (const_iterator B = EdgesBegin(), E = EdgesEnd();
|
|
|
|
B != E; ++B) {
|
|
|
|
const Edge* E = (*B).getPtr();
|
|
|
|
if (E->isDefault())
|
|
|
|
if (!DefaultEdge)
|
|
|
|
DefaultEdge = E;
|
|
|
|
else
|
|
|
|
throw std::runtime_error("Node " + Name() +
|
|
|
|
": multiple default edges found!"
|
|
|
|
"Most probably a specification error.");
|
|
|
|
if (E->isEnabled())
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DefaultEdge)
|
|
|
|
return DefaultEdge;
|
|
|
|
else
|
|
|
|
throw std::runtime_error("Node " + Name() +
|
|
|
|
": no suitable edge found! "
|
|
|
|
"Most probably a specification error.");
|
|
|
|
}
|
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
CompilationGraph::CompilationGraph() {
|
|
|
|
NodesMap["root"] = Node(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node& CompilationGraph::getNode(const std::string& ToolName) {
|
|
|
|
nodes_map_type::iterator I = NodesMap.find(ToolName);
|
|
|
|
if (I == NodesMap.end())
|
2008-05-06 16:36:50 +00:00
|
|
|
throw std::runtime_error("Node " + ToolName + " is not in the graph");
|
2008-05-06 16:35:25 +00:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Node& CompilationGraph::getNode(const std::string& ToolName) const {
|
|
|
|
nodes_map_type::const_iterator I = NodesMap.find(ToolName);
|
|
|
|
if (I == NodesMap.end())
|
2008-05-06 16:36:50 +00:00
|
|
|
throw std::runtime_error("Node " + ToolName + " is not in the graph!");
|
2008-05-06 16:35:25 +00:00
|
|
|
return I->second;
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
|
|
|
|
LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
|
2008-03-23 08:57:20 +00:00
|
|
|
if (Lang == ExtsToLangs.end())
|
2008-05-06 16:35:25 +00:00
|
|
|
throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
|
|
|
|
return Lang->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CompilationGraph::tools_vector_type&
|
|
|
|
CompilationGraph::getToolsVector(const std::string& LangName) const
|
|
|
|
{
|
|
|
|
tools_map_type::const_iterator I = ToolsMap.find(LangName);
|
|
|
|
if (I == ToolsMap.end())
|
|
|
|
throw std::runtime_error("No tools corresponding to " + LangName
|
|
|
|
+ " found!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
void CompilationGraph::insertNode(Tool* V) {
|
2008-05-06 17:26:53 +00:00
|
|
|
if (NodesMap.count(V->Name()) == 0) {
|
2008-05-06 16:35:25 +00:00
|
|
|
Node N;
|
|
|
|
N.OwningGraph = this;
|
|
|
|
N.ToolPtr = V;
|
|
|
|
NodesMap[V->Name()] = N;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 16:36:50 +00:00
|
|
|
void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
|
2008-05-06 17:26:53 +00:00
|
|
|
Node& B = getNode(E->ToolName());
|
2008-05-06 16:35:25 +00:00
|
|
|
if (A == "root") {
|
2008-05-06 17:26:53 +00:00
|
|
|
const std::string& InputLanguage = B.ToolPtr->InputLanguage();
|
2008-05-06 16:36:50 +00:00
|
|
|
ToolsMap[InputLanguage].push_back(E->ToolName());
|
|
|
|
NodesMap["root"].AddEdge(E);
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-05-06 16:36:06 +00:00
|
|
|
Node& N = getNode(A);
|
2008-05-06 16:36:50 +00:00
|
|
|
N.AddEdge(E);
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
2008-05-06 17:26:53 +00:00
|
|
|
// Increase the inward edge counter.
|
|
|
|
B.IncrInEdges();
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 17:25:51 +00:00
|
|
|
// Pass input file through the chain until we bump into a Join node or
|
|
|
|
// a node that says that it is the last.
|
2008-05-06 17:26:53 +00:00
|
|
|
const JoinTool*
|
|
|
|
CompilationGraph::PassThroughGraph (sys::Path& In, sys::Path Out,
|
|
|
|
const sys::Path& TempDir) const {
|
2008-05-06 17:25:51 +00:00
|
|
|
bool Last = false;
|
2008-05-06 17:26:53 +00:00
|
|
|
JoinTool* ret = 0;
|
2008-05-06 17:25:51 +00:00
|
|
|
|
|
|
|
// Get to the head of the toolchain.
|
|
|
|
const tools_vector_type& TV = getToolsVector(getLanguage(In));
|
|
|
|
if (TV.empty())
|
|
|
|
throw std::runtime_error("Tool names vector is empty!");
|
|
|
|
const Node* N = &getNode(*TV.begin());
|
|
|
|
|
|
|
|
while(!Last) {
|
2008-05-06 17:26:53 +00:00
|
|
|
Tool* CurTool = N->ToolPtr.getPtr();
|
2008-05-06 17:25:51 +00:00
|
|
|
|
|
|
|
if (CurTool->IsJoin()) {
|
2008-05-06 17:26:53 +00:00
|
|
|
ret = &dynamic_cast<JoinTool&>(*CurTool);
|
|
|
|
ret->AddToJoinList(In);
|
2008-05-06 17:25:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this the last tool?
|
|
|
|
if (!N->HasChildren() || CurTool->IsLast()) {
|
|
|
|
// Check if the first tool is also the last
|
|
|
|
if (Out.empty())
|
|
|
|
Out.set(In.getBasename());
|
|
|
|
else
|
|
|
|
Out.appendComponent(In.getBasename());
|
|
|
|
Out.appendSuffix(CurTool->OutputSuffix());
|
|
|
|
Last = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Out = TempDir;
|
|
|
|
Out.appendComponent(In.getBasename());
|
|
|
|
Out.appendSuffix(CurTool->OutputSuffix());
|
|
|
|
Out.makeUnique(true, NULL);
|
|
|
|
Out.eraseFromDisk();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurTool->GenerateAction(In, Out).Execute() != 0)
|
|
|
|
throw std::runtime_error("Tool returned error code!");
|
|
|
|
|
|
|
|
N = &getNode(N->ChooseEdge()->ToolName());
|
|
|
|
In = Out; Out.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CompilationGraph::Build (const sys::Path& TempDir) const {
|
2008-05-06 17:26:53 +00:00
|
|
|
const JoinTool* JT = 0;
|
2008-05-06 16:35:25 +00:00
|
|
|
sys::Path In, Out;
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
// For each input file
|
2008-03-23 08:57:20 +00:00
|
|
|
for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
|
|
|
|
E = InputFilenames.end(); B != E; ++B) {
|
|
|
|
In = sys::Path(*B);
|
|
|
|
|
2008-05-06 17:26:53 +00:00
|
|
|
const JoinTool* NewJoin = PassThroughGraph(In, Out, TempDir);
|
|
|
|
if (JT && NewJoin && JT != NewJoin)
|
2008-05-06 17:25:51 +00:00
|
|
|
throw std::runtime_error("Graphs with multiple Join nodes"
|
|
|
|
"are not yet supported!");
|
|
|
|
else if (NewJoin)
|
2008-05-06 17:26:53 +00:00
|
|
|
JT = NewJoin;
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 17:26:53 +00:00
|
|
|
if (JT) {
|
2008-05-06 16:35:25 +00:00
|
|
|
// If the final output name is empty, set it to "a.out"
|
|
|
|
if (!OutputFilename.empty()) {
|
|
|
|
Out = sys::Path(OutputFilename);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Out = sys::Path("a");
|
2008-05-06 17:26:53 +00:00
|
|
|
Out.appendSuffix(JT->OutputSuffix());
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 17:26:53 +00:00
|
|
|
if (JT->GenerateAction(Out).Execute() != 0)
|
2008-05-06 16:35:25 +00:00
|
|
|
throw std::runtime_error("Tool returned error code!");
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template <>
|
|
|
|
struct DOTGraphTraits<llvmcc::CompilationGraph*>
|
|
|
|
: public DefaultDOTGraphTraits
|
|
|
|
{
|
|
|
|
|
2008-05-06 17:26:14 +00:00
|
|
|
template<typename GraphType>
|
|
|
|
static std::string getNodeLabel(const Node* N, const GraphType&)
|
|
|
|
{
|
|
|
|
if (N->ToolPtr)
|
|
|
|
if (N->ToolPtr->IsJoin())
|
|
|
|
return N->Name() + "\n (join" +
|
|
|
|
(N->HasChildren() ? ")"
|
|
|
|
: std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
|
|
|
|
else
|
|
|
|
return N->Name();
|
|
|
|
else
|
|
|
|
return "root";
|
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 17:26:14 +00:00
|
|
|
template<typename EdgeIter>
|
|
|
|
static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
|
|
|
|
if (N->ToolPtr)
|
|
|
|
return N->ToolPtr->OutputLanguage();
|
|
|
|
else
|
|
|
|
return I->ToolPtr->InputLanguage();
|
|
|
|
}
|
2008-05-06 16:35:25 +00:00
|
|
|
};
|
2008-05-06 17:26:14 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
2008-03-23 08:57:20 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
void CompilationGraph::writeGraph() {
|
|
|
|
std::ofstream O("CompilationGraph.dot");
|
|
|
|
|
2008-05-06 16:37:33 +00:00
|
|
|
if (O.good()) {
|
2008-05-06 16:35:25 +00:00
|
|
|
llvm::WriteGraph(this, "CompilationGraph");
|
|
|
|
O.close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw std::runtime_error("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilationGraph::viewGraph() {
|
2008-05-06 16:36:50 +00:00
|
|
|
llvm::ViewGraph(this, "compilation-graph");
|
2008-03-23 08:57:20 +00:00
|
|
|
}
|