1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00

Make ChooseEdge more generic and use it to choose between different toolchains.

llvm-svn: 50741
This commit is contained in:
Mikhail Glushenkov 2008-05-06 17:28:03 +00:00
parent 22a474513a
commit 9d7f3626fa
2 changed files with 56 additions and 38 deletions

View File

@ -25,29 +25,37 @@ using namespace llvmcc;
extern cl::list<std::string> InputFilenames; extern cl::list<std::string> InputFilenames;
extern cl::opt<std::string> OutputFilename; extern cl::opt<std::string> OutputFilename;
// Choose one of the edges based on command-line options. namespace {
const Edge* Node::ChooseEdge() const {
const Edge* DefaultEdge = 0; // Choose edge that returns
for (const_iterator B = EdgesBegin(), E = EdgesEnd(); template <class C>
B != E; ++B) { const Edge* ChooseEdge(const C& EdgesContainer,
const Edge* E = (*B).getPtr(); const std::string& NodeName = "root") {
if (E->isDefault()) const Edge* DefaultEdge = 0;
if (!DefaultEdge)
DefaultEdge = E; for (typename C::const_iterator B = EdgesContainer.begin(),
else E = EdgesContainer.end(); B != E; ++B) {
throw std::runtime_error("Node " + Name() + const Edge* E = B->getPtr();
": multiple default edges found!"
"Most probably a specification error."); if (E->isDefault())
if (E->isEnabled()) if (!DefaultEdge)
return E; DefaultEdge = E;
else
throw std::runtime_error("Node " + NodeName
+ ": multiple default outward edges found!"
"Most probably a specification error.");
if (E->isEnabled())
return E;
}
if (DefaultEdge)
return DefaultEdge;
else
throw std::runtime_error("Node " + NodeName
+ ": no default outward edge found!"
"Most probably a specification error.");
} }
if (DefaultEdge)
return DefaultEdge;
else
throw std::runtime_error("Node " + Name() +
": no suitable edge found! "
"Most probably a specification error.");
} }
CompilationGraph::CompilationGraph() { CompilationGraph::CompilationGraph() {
@ -98,7 +106,7 @@ void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Node& B = getNode(E->ToolName()); Node& B = getNode(E->ToolName());
if (A == "root") { if (A == "root") {
const std::string& InputLanguage = B.ToolPtr->InputLanguage(); const std::string& InputLanguage = B.ToolPtr->InputLanguage();
ToolsMap[InputLanguage].push_back(E->ToolName()); ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
NodesMap["root"].AddEdge(E); NodesMap["root"].AddEdge(E);
} }
else { else {
@ -113,19 +121,15 @@ void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
// a node that says that it is the last. // a node that says that it is the last.
const JoinTool* const JoinTool*
CompilationGraph::PassThroughGraph (sys::Path& In, CompilationGraph::PassThroughGraph (sys::Path& In,
const Node* StartNode,
const sys::Path& TempDir) const { const sys::Path& TempDir) const {
bool Last = false; bool Last = false;
const Node* CurNode = StartNode;
JoinTool* ret = 0; JoinTool* ret = 0;
// 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) { while(!Last) {
sys::Path Out; sys::Path Out;
Tool* CurTool = N->ToolPtr.getPtr(); Tool* CurTool = CurNode->ToolPtr.getPtr();
if (CurTool->IsJoin()) { if (CurTool->IsJoin()) {
ret = &dynamic_cast<JoinTool&>(*CurTool); ret = &dynamic_cast<JoinTool&>(*CurTool);
@ -134,7 +138,7 @@ CompilationGraph::PassThroughGraph (sys::Path& In,
} }
// Is this the last tool? // Is this the last tool?
if (!N->HasChildren() || CurTool->IsLast()) { if (!CurNode->HasChildren() || CurTool->IsLast()) {
// Check if the first tool is also the last // Check if the first tool is also the last
if (Out.empty()) if (Out.empty())
Out.set(In.getBasename()); Out.set(In.getBasename());
@ -154,7 +158,8 @@ CompilationGraph::PassThroughGraph (sys::Path& In,
if (CurTool->GenerateAction(In, Out).Execute() != 0) if (CurTool->GenerateAction(In, Out).Execute() != 0)
throw std::runtime_error("Tool returned error code!"); throw std::runtime_error("Tool returned error code!");
N = &getNode(N->ChooseEdge()->ToolName()); CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
CurNode->Name())->ToolName());
In = Out; Out.clear(); In = Out; Out.clear();
} }
@ -164,12 +169,21 @@ CompilationGraph::PassThroughGraph (sys::Path& In,
int CompilationGraph::Build (const sys::Path& TempDir) const { int CompilationGraph::Build (const sys::Path& TempDir) const {
const JoinTool* JT = 0; const JoinTool* JT = 0;
// For each input file // For each input file:
for (cl::list<std::string>::const_iterator B = InputFilenames.begin(), for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
E = InputFilenames.end(); B != E; ++B) { E = InputFilenames.end(); B != E; ++B) {
sys::Path In = sys::Path(*B); sys::Path In = sys::Path(*B);
const JoinTool* NewJoin = PassThroughGraph(In, TempDir); // Get to the head of the toolchain.
const std::string& InLanguage = getLanguage(In);
const tools_vector_type& TV = getToolsVector(InLanguage);
if (TV.empty())
throw std::runtime_error("No toolchain corresponding to language"
+ InLanguage + " found!");
const Node* N = &getNode(ChooseEdge(TV)->ToolName());
// Pass it through the chain starting at head.
const JoinTool* NewJoin = PassThroughGraph(In, N, TempDir);
if (JT && NewJoin && JT != NewJoin) if (JT && NewJoin && JT != NewJoin)
throw std::runtime_error("Graphs with multiple Join nodes" throw std::runtime_error("Graphs with multiple Join nodes"
"are not yet supported!"); "are not yet supported!");
@ -177,6 +191,8 @@ int CompilationGraph::Build (const sys::Path& TempDir) const {
JT = NewJoin; JT = NewJoin;
} }
// For all join nodes in topological order:
// TOFIX: implement.
if (JT) { if (JT) {
sys::Path Out; sys::Path Out;
// If the final output name is empty, set it to "a.out" // If the final output name is empty, set it to "a.out"

View File

@ -69,9 +69,6 @@ namespace llvmcc {
iterator EdgesEnd() { return OutEdges.end(); } iterator EdgesEnd() { return OutEdges.end(); }
const_iterator EdgesEnd() const { return OutEdges.end(); } const_iterator EdgesEnd() const { return OutEdges.end(); }
// Choose one of the outward edges based on command-line options.
const Edge* ChooseEdge() const;
// Add an outward edge. Takes ownership of the Edge object. // Add an outward edge. Takes ownership of the Edge object.
void AddEdge(Edge* E) void AddEdge(Edge* E)
{ OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); } { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
@ -95,9 +92,13 @@ namespace llvmcc {
class NodesIterator; class NodesIterator;
class CompilationGraph { class CompilationGraph {
typedef llvm::SmallVector<std::string, 3> tools_vector_type; // Main data structure.
typedef llvm::StringMap<tools_vector_type> tools_map_type;
typedef llvm::StringMap<Node> nodes_map_type; typedef llvm::StringMap<Node> nodes_map_type;
// These are used to map from language names-> tools. (We can have
// several tools associated with each language name.)
typedef
llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
typedef llvm::StringMap<tools_vector_type> tools_map_type;
// Map from file extensions to language names. // Map from file extensions to language names.
LanguageMap ExtsToLangs; LanguageMap ExtsToLangs;
@ -154,6 +155,7 @@ namespace llvmcc {
// Pass the input file through the toolchain. // Pass the input file through the toolchain.
const JoinTool* PassThroughGraph (llvm::sys::Path& In, const JoinTool* PassThroughGraph (llvm::sys::Path& In,
const Node* StartNode,
const llvm::sys::Path& TempDir) const; const llvm::sys::Path& TempDir) const;
}; };