Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 1 | //===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===// |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 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 | // |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 10 | // Compilation graph - implementation. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 14 | #include "CompilationGraph.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 15 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DOTGraphTraits.h" |
| 18 | #include "llvm/Support/GraphWriter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 19 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 20 | #include <stdexcept> |
| 21 | |
| 22 | using namespace llvm; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 23 | using namespace llvmcc; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 24 | |
| 25 | extern cl::list<std::string> InputFilenames; |
| 26 | extern cl::opt<std::string> OutputFilename; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 27 | |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 28 | // Choose one of the edges based on command-line options. |
| 29 | const Edge* Node::ChooseEdge() const { |
| 30 | const Edge* DefaultEdge = 0; |
| 31 | for (const_iterator B = EdgesBegin(), E = EdgesEnd(); |
| 32 | B != E; ++B) { |
| 33 | const Edge* E = (*B).getPtr(); |
| 34 | if (E->isDefault()) |
| 35 | if (!DefaultEdge) |
| 36 | DefaultEdge = E; |
| 37 | else |
| 38 | throw std::runtime_error("Node " + Name() + |
| 39 | ": multiple default edges found!" |
| 40 | "Most probably a specification error."); |
| 41 | if (E->isEnabled()) |
| 42 | return E; |
| 43 | } |
| 44 | |
| 45 | if (DefaultEdge) |
| 46 | return DefaultEdge; |
| 47 | else |
| 48 | throw std::runtime_error("Node " + Name() + |
| 49 | ": no suitable edge found! " |
| 50 | "Most probably a specification error."); |
| 51 | } |
| 52 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 53 | CompilationGraph::CompilationGraph() { |
| 54 | NodesMap["root"] = Node(this); |
| 55 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 56 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 57 | Node& CompilationGraph::getNode(const std::string& ToolName) { |
| 58 | nodes_map_type::iterator I = NodesMap.find(ToolName); |
| 59 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 60 | throw std::runtime_error("Node " + ToolName + " is not in the graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 61 | return I->second; |
| 62 | } |
| 63 | |
| 64 | const Node& CompilationGraph::getNode(const std::string& ToolName) const { |
| 65 | nodes_map_type::const_iterator I = NodesMap.find(ToolName); |
| 66 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 67 | throw std::runtime_error("Node " + ToolName + " is not in the graph!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 68 | return I->second; |
| 69 | } |
| 70 | |
| 71 | const std::string& CompilationGraph::getLanguage(const sys::Path& File) const { |
| 72 | LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix()); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 73 | if (Lang == ExtsToLangs.end()) |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 74 | throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!'); |
| 75 | return Lang->second; |
| 76 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 77 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 78 | const CompilationGraph::tools_vector_type& |
| 79 | CompilationGraph::getToolsVector(const std::string& LangName) const |
| 80 | { |
| 81 | tools_map_type::const_iterator I = ToolsMap.find(LangName); |
| 82 | if (I == ToolsMap.end()) |
| 83 | throw std::runtime_error("No tools corresponding to " + LangName |
| 84 | + " found!"); |
| 85 | return I->second; |
| 86 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 87 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 88 | void CompilationGraph::insertNode(Tool* V) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 89 | if (NodesMap.count(V->Name()) == 0) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 90 | Node N; |
| 91 | N.OwningGraph = this; |
| 92 | N.ToolPtr = V; |
| 93 | NodesMap[V->Name()] = N; |
| 94 | } |
| 95 | } |
| 96 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 97 | void CompilationGraph::insertEdge(const std::string& A, Edge* E) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 98 | Node& B = getNode(E->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 99 | if (A == "root") { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 100 | const std::string& InputLanguage = B.ToolPtr->InputLanguage(); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 101 | ToolsMap[InputLanguage].push_back(E->ToolName()); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 102 | NodesMap["root"].AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 103 | } |
| 104 | else { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 105 | Node& N = getNode(A); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 106 | N.AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 107 | } |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 108 | // Increase the inward edge counter. |
| 109 | B.IncrInEdges(); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 112 | // Pass input file through the chain until we bump into a Join node or |
| 113 | // a node that says that it is the last. |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 114 | const JoinTool* |
| 115 | CompilationGraph::PassThroughGraph (sys::Path& In, sys::Path Out, |
| 116 | const sys::Path& TempDir) const { |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 117 | bool Last = false; |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 118 | JoinTool* ret = 0; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 119 | |
| 120 | // Get to the head of the toolchain. |
| 121 | const tools_vector_type& TV = getToolsVector(getLanguage(In)); |
| 122 | if (TV.empty()) |
| 123 | throw std::runtime_error("Tool names vector is empty!"); |
| 124 | const Node* N = &getNode(*TV.begin()); |
| 125 | |
| 126 | while(!Last) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 127 | Tool* CurTool = N->ToolPtr.getPtr(); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 128 | |
| 129 | if (CurTool->IsJoin()) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 130 | ret = &dynamic_cast<JoinTool&>(*CurTool); |
| 131 | ret->AddToJoinList(In); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | |
| 135 | // Is this the last tool? |
| 136 | if (!N->HasChildren() || CurTool->IsLast()) { |
| 137 | // Check if the first tool is also the last |
| 138 | if (Out.empty()) |
| 139 | Out.set(In.getBasename()); |
| 140 | else |
| 141 | Out.appendComponent(In.getBasename()); |
| 142 | Out.appendSuffix(CurTool->OutputSuffix()); |
| 143 | Last = true; |
| 144 | } |
| 145 | else { |
| 146 | Out = TempDir; |
| 147 | Out.appendComponent(In.getBasename()); |
| 148 | Out.appendSuffix(CurTool->OutputSuffix()); |
| 149 | Out.makeUnique(true, NULL); |
| 150 | Out.eraseFromDisk(); |
| 151 | } |
| 152 | |
| 153 | if (CurTool->GenerateAction(In, Out).Execute() != 0) |
| 154 | throw std::runtime_error("Tool returned error code!"); |
| 155 | |
| 156 | N = &getNode(N->ChooseEdge()->ToolName()); |
| 157 | In = Out; Out.clear(); |
| 158 | } |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 163 | int CompilationGraph::Build (const sys::Path& TempDir) const { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 164 | const JoinTool* JT = 0; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 165 | sys::Path In, Out; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 166 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 167 | // For each input file |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 168 | for (cl::list<std::string>::const_iterator B = InputFilenames.begin(), |
| 169 | E = InputFilenames.end(); B != E; ++B) { |
| 170 | In = sys::Path(*B); |
| 171 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 172 | const JoinTool* NewJoin = PassThroughGraph(In, Out, TempDir); |
| 173 | if (JT && NewJoin && JT != NewJoin) |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 174 | throw std::runtime_error("Graphs with multiple Join nodes" |
| 175 | "are not yet supported!"); |
| 176 | else if (NewJoin) |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 177 | JT = NewJoin; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 180 | if (JT) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 181 | // If the final output name is empty, set it to "a.out" |
| 182 | if (!OutputFilename.empty()) { |
| 183 | Out = sys::Path(OutputFilename); |
| 184 | } |
| 185 | else { |
| 186 | Out = sys::Path("a"); |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 187 | Out.appendSuffix(JT->OutputSuffix()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 188 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 189 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame^] | 190 | if (JT->GenerateAction(Out).Execute() != 0) |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 191 | throw std::runtime_error("Tool returned error code!"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 192 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 196 | |
| 197 | namespace llvm { |
| 198 | template <> |
| 199 | struct DOTGraphTraits<llvmcc::CompilationGraph*> |
| 200 | : public DefaultDOTGraphTraits |
| 201 | { |
| 202 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 203 | template<typename GraphType> |
| 204 | static std::string getNodeLabel(const Node* N, const GraphType&) |
| 205 | { |
| 206 | if (N->ToolPtr) |
| 207 | if (N->ToolPtr->IsJoin()) |
| 208 | return N->Name() + "\n (join" + |
| 209 | (N->HasChildren() ? ")" |
| 210 | : std::string(": ") + N->ToolPtr->OutputLanguage() + ')'); |
| 211 | else |
| 212 | return N->Name(); |
| 213 | else |
| 214 | return "root"; |
| 215 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 216 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 217 | template<typename EdgeIter> |
| 218 | static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) { |
| 219 | if (N->ToolPtr) |
| 220 | return N->ToolPtr->OutputLanguage(); |
| 221 | else |
| 222 | return I->ToolPtr->InputLanguage(); |
| 223 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 224 | }; |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 225 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void CompilationGraph::writeGraph() { |
| 229 | std::ofstream O("CompilationGraph.dot"); |
| 230 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 231 | if (O.good()) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 232 | llvm::WriteGraph(this, "CompilationGraph"); |
| 233 | O.close(); |
| 234 | } |
| 235 | else { |
| 236 | throw std::runtime_error(""); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void CompilationGraph::viewGraph() { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 241 | llvm::ViewGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 242 | } |