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 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DOTGraphTraits.h" |
| 19 | #include "llvm/Support/GraphWriter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 20 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 22 | #include <iterator> |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 23 | #include <iostream> |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 24 | #include <limits> |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 25 | #include <queue> |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 26 | #include <stdexcept> |
| 27 | |
| 28 | using namespace llvm; |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 29 | using namespace llvmc; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 30 | |
| 31 | extern cl::list<std::string> InputFilenames; |
| 32 | extern cl::opt<std::string> OutputFilename; |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 33 | extern cl::list<std::string> Languages; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 34 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 37 | // Return the edge with the maximum weight. |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 38 | template <class C> |
| 39 | const Edge* ChooseEdge(const C& EdgesContainer, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 40 | const InputLanguagesSet& InLangs, |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 41 | const std::string& NodeName = "root") { |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 42 | const Edge* MaxEdge = 0; |
| 43 | unsigned MaxWeight = 0; |
| 44 | bool SingleMax = true; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 45 | |
| 46 | for (typename C::const_iterator B = EdgesContainer.begin(), |
| 47 | E = EdgesContainer.end(); B != E; ++B) { |
| 48 | const Edge* E = B->getPtr(); |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 49 | unsigned EW = E->Weight(InLangs); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 50 | if (EW > MaxWeight) { |
| 51 | MaxEdge = E; |
| 52 | MaxWeight = EW; |
| 53 | SingleMax = true; |
| 54 | } |
| 55 | else if (EW == MaxWeight) { |
| 56 | SingleMax = false; |
| 57 | } |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 60 | if (!SingleMax) |
| 61 | throw std::runtime_error("Node " + NodeName + |
| 62 | ": multiple maximal outward edges found!" |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 63 | " Most probably a specification error."); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame] | 64 | if (!MaxEdge) |
| 65 | throw std::runtime_error("Node " + NodeName + |
| 66 | ": no maximal outward edge found!" |
| 67 | " Most probably a specification error."); |
| 68 | return MaxEdge; |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 73 | CompilationGraph::CompilationGraph() { |
| 74 | NodesMap["root"] = Node(this); |
| 75 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 76 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 77 | Node& CompilationGraph::getNode(const std::string& ToolName) { |
| 78 | nodes_map_type::iterator I = NodesMap.find(ToolName); |
| 79 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 80 | throw std::runtime_error("Node " + ToolName + " is not in the graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 81 | return I->second; |
| 82 | } |
| 83 | |
| 84 | const Node& CompilationGraph::getNode(const std::string& ToolName) const { |
| 85 | nodes_map_type::const_iterator I = NodesMap.find(ToolName); |
| 86 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 87 | throw std::runtime_error("Node " + ToolName + " is not in the graph!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 88 | return I->second; |
| 89 | } |
| 90 | |
| 91 | const std::string& CompilationGraph::getLanguage(const sys::Path& File) const { |
| 92 | LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix()); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 93 | if (Lang == ExtsToLangs.end()) |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 94 | throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!'); |
| 95 | return Lang->second; |
| 96 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 97 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 98 | const CompilationGraph::tools_vector_type& |
| 99 | CompilationGraph::getToolsVector(const std::string& LangName) const |
| 100 | { |
| 101 | tools_map_type::const_iterator I = ToolsMap.find(LangName); |
| 102 | if (I == ToolsMap.end()) |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 103 | throw std::runtime_error("No tool corresponding to the language " |
| 104 | + LangName + "found!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 105 | return I->second; |
| 106 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 107 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 108 | void CompilationGraph::insertNode(Tool* V) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 109 | if (NodesMap.count(V->Name()) == 0) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 110 | Node N; |
| 111 | N.OwningGraph = this; |
| 112 | N.ToolPtr = V; |
| 113 | NodesMap[V->Name()] = N; |
| 114 | } |
| 115 | } |
| 116 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 117 | void CompilationGraph::insertEdge(const std::string& A, Edge* E) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 118 | Node& B = getNode(E->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 119 | if (A == "root") { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 120 | const std::string& InputLanguage = B.ToolPtr->InputLanguage(); |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 121 | ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E)); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 122 | NodesMap["root"].AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 123 | } |
| 124 | else { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 125 | Node& N = getNode(A); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 126 | N.AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 127 | } |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 128 | // Increase the inward edge counter. |
| 129 | B.IncrInEdges(); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 132 | namespace { |
| 133 | sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName, |
| 134 | const std::string& Suffix) { |
| 135 | sys::Path Out = TempDir; |
| 136 | Out.appendComponent(BaseName); |
| 137 | Out.appendSuffix(Suffix); |
| 138 | Out.makeUnique(true, NULL); |
| 139 | return Out; |
| 140 | } |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 143 | // Pass input file through the chain until we bump into a Join node or |
| 144 | // a node that says that it is the last. |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 145 | void CompilationGraph::PassThroughGraph (const sys::Path& InFile, |
| 146 | const Node* StartNode, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 147 | const InputLanguagesSet& InLangs, |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 148 | const sys::Path& TempDir) const { |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 149 | bool Last = false; |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 150 | sys::Path In = InFile; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 151 | const Node* CurNode = StartNode; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 152 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 153 | while(!Last) { |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 154 | sys::Path Out; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 155 | Tool* CurTool = CurNode->ToolPtr.getPtr(); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 156 | |
| 157 | if (CurTool->IsJoin()) { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 158 | JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool); |
| 159 | JT.AddToJoinList(In); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 163 | // Since toolchains do not have to end with a Join node, we should |
| 164 | // check if this Node is the last. |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 165 | if (!CurNode->HasChildren() || CurTool->IsLast()) { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 166 | if (!OutputFilename.empty()) { |
| 167 | Out.set(OutputFilename); |
| 168 | } |
| 169 | else { |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 170 | Out.set(In.getBasename()); |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 171 | Out.appendSuffix(CurTool->OutputSuffix()); |
| 172 | } |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 173 | Last = true; |
| 174 | } |
| 175 | else { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 176 | Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix()); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (CurTool->GenerateAction(In, Out).Execute() != 0) |
| 180 | throw std::runtime_error("Tool returned error code!"); |
| 181 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 182 | if (Last) |
| 183 | return; |
| 184 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 185 | CurNode = &getNode(ChooseEdge(CurNode->OutEdges, |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 186 | InLangs, |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 187 | CurNode->Name())->ToolName()); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 188 | In = Out; Out.clear(); |
| 189 | } |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 192 | // Sort the nodes in topological order. |
| 193 | void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) { |
| 194 | std::queue<const Node*> Q; |
| 195 | Q.push(&getNode("root")); |
| 196 | |
| 197 | while (!Q.empty()) { |
| 198 | const Node* A = Q.front(); |
| 199 | Q.pop(); |
| 200 | Out.push_back(A); |
| 201 | for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd(); |
| 202 | EB != EE; ++EB) { |
| 203 | Node* B = &getNode((*EB)->ToolName()); |
| 204 | B->DecrInEdges(); |
| 205 | if (B->HasNoInEdges()) |
| 206 | Q.push(B); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | namespace { |
| 212 | bool NotJoinNode(const Node* N) { |
| 213 | return N->ToolPtr ? !N->ToolPtr->IsJoin() : true; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Call TopologicalSort and filter the resulting list to include |
| 218 | // only Join nodes. |
| 219 | void CompilationGraph:: |
| 220 | TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) { |
| 221 | std::vector<const Node*> TopSorted; |
| 222 | TopologicalSort(TopSorted); |
| 223 | std::remove_copy_if(TopSorted.begin(), TopSorted.end(), |
| 224 | std::back_inserter(Out), NotJoinNode); |
| 225 | } |
| 226 | |
| 227 | // Find head of the toolchain corresponding to the given file. |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 228 | // Also, insert an input language into InLangs. |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 229 | const Node* CompilationGraph:: |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 230 | FindToolChain(const sys::Path& In, const std::string* forceLanguage, |
| 231 | InputLanguagesSet& InLangs) const { |
| 232 | |
| 233 | // Determine the input language. |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 234 | const std::string& InLanguage = |
| 235 | forceLanguage ? *forceLanguage : getLanguage(In); |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 236 | |
| 237 | // Add the current input language to the input language set. |
| 238 | InLangs.insert(InLanguage); |
| 239 | |
| 240 | // Find the toolchain for the input language. |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 241 | const tools_vector_type& TV = getToolsVector(InLanguage); |
| 242 | if (TV.empty()) |
| 243 | throw std::runtime_error("No toolchain corresponding to language" |
| 244 | + InLanguage + " found!"); |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 245 | return &getNode(ChooseEdge(TV, InLangs)->ToolName()); |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 248 | // Build the targets. Command-line options are passed through |
| 249 | // temporary variables. |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 250 | int CompilationGraph::Build (const sys::Path& TempDir) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 251 | |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 252 | InputLanguagesSet InLangs; |
| 253 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 254 | // This is related to -x option handling. |
| 255 | cl::list<std::string>::const_iterator xIter = Languages.begin(), |
| 256 | xBegin = xIter, xEnd = Languages.end(); |
| 257 | bool xEmpty = true; |
| 258 | const std::string* xLanguage = 0; |
| 259 | unsigned xPos = 0, xPosNext = 0, filePos = 0; |
| 260 | |
| 261 | if (xIter != xEnd) { |
| 262 | xEmpty = false; |
| 263 | xPos = Languages.getPosition(xIter - xBegin); |
| 264 | cl::list<std::string>::const_iterator xNext = llvm::next(xIter); |
| 265 | xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max() |
| 266 | : Languages.getPosition(xNext - xBegin); |
| 267 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 268 | } |
| 269 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 270 | // For each input file: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 271 | for (cl::list<std::string>::const_iterator B = InputFilenames.begin(), |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 272 | CB = B, E = InputFilenames.end(); B != E; ++B) { |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 273 | sys::Path In = sys::Path(*B); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 274 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 275 | // Code for handling the -x option. |
| 276 | // Output: std::string* xLanguage (can be NULL). |
| 277 | if (!xEmpty) { |
| 278 | filePos = InputFilenames.getPosition(B - CB); |
| 279 | |
| 280 | if (xPos < filePos) { |
| 281 | if (filePos < xPosNext) { |
| 282 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 283 | } |
| 284 | else { // filePos >= xPosNext |
| 285 | // Skip xIters while filePos > xPosNext |
| 286 | while (filePos > xPosNext) { |
| 287 | ++xIter; |
| 288 | xPos = xPosNext; |
| 289 | |
| 290 | cl::list<std::string>::const_iterator xNext = llvm::next(xIter); |
| 291 | if (xNext == xEnd) |
| 292 | xPosNext = std::numeric_limits<unsigned>::max(); |
| 293 | else |
| 294 | xPosNext = Languages.getPosition(xNext - xBegin); |
| 295 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 301 | // Find the toolchain corresponding to this file. |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 302 | const Node* N = FindToolChain(In, xLanguage, InLangs); |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 303 | // Pass file through the chain starting at head. |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 304 | PassThroughGraph(In, N, InLangs, TempDir); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 307 | std::vector<const Node*> JTV; |
| 308 | TopologicalSortFilterJoinNodes(JTV); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 309 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 310 | // For all join nodes in topological order: |
| 311 | for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end(); |
| 312 | B != E; ++B) { |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 313 | |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 314 | sys::Path Out; |
| 315 | const Node* CurNode = *B; |
| 316 | JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr()); |
| 317 | bool IsLast = false; |
| 318 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 319 | // Are there any files to be joined? |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 320 | if (JT->JoinListEmpty()) |
| 321 | continue; |
| 322 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 323 | // Is this the last tool in the chain? |
| 324 | // NOTE: we can process several chains in parallel. |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 325 | if (!CurNode->HasChildren() || JT->IsLast()) { |
| 326 | if (OutputFilename.empty()) { |
| 327 | Out.set("a"); |
| 328 | Out.appendSuffix(JT->OutputSuffix()); |
| 329 | } |
| 330 | else |
| 331 | Out.set(OutputFilename); |
| 332 | IsLast = true; |
| 333 | } |
| 334 | else { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 335 | Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix()); |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (JT->GenerateAction(Out).Execute() != 0) |
| 339 | throw std::runtime_error("Tool returned error code!"); |
| 340 | |
| 341 | if (!IsLast) { |
Mikhail Glushenkov | 76b1b24 | 2008-05-06 18:15:12 +0000 | [diff] [blame^] | 342 | const Node* NextNode = |
| 343 | &getNode(ChooseEdge(CurNode->OutEdges, InLangs, |
| 344 | CurNode->Name())->ToolName()); |
| 345 | PassThroughGraph(Out, NextNode, InLangs, TempDir); |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 346 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 347 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 348 | |
| 349 | return 0; |
| 350 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 351 | |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 352 | // Code related to graph visualization. |
| 353 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 354 | namespace llvm { |
| 355 | template <> |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 356 | struct DOTGraphTraits<llvmc::CompilationGraph*> |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 357 | : public DefaultDOTGraphTraits |
| 358 | { |
| 359 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 360 | template<typename GraphType> |
| 361 | static std::string getNodeLabel(const Node* N, const GraphType&) |
| 362 | { |
| 363 | if (N->ToolPtr) |
| 364 | if (N->ToolPtr->IsJoin()) |
| 365 | return N->Name() + "\n (join" + |
| 366 | (N->HasChildren() ? ")" |
| 367 | : std::string(": ") + N->ToolPtr->OutputLanguage() + ')'); |
| 368 | else |
| 369 | return N->Name(); |
| 370 | else |
| 371 | return "root"; |
| 372 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 373 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 374 | template<typename EdgeIter> |
| 375 | static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) { |
| 376 | if (N->ToolPtr) |
| 377 | return N->ToolPtr->OutputLanguage(); |
| 378 | else |
| 379 | return I->ToolPtr->InputLanguage(); |
| 380 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 381 | }; |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 382 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void CompilationGraph::writeGraph() { |
| 386 | std::ofstream O("CompilationGraph.dot"); |
| 387 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 388 | if (O.good()) { |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 389 | llvm::WriteGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 390 | O.close(); |
| 391 | } |
| 392 | else { |
| 393 | throw std::runtime_error(""); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void CompilationGraph::viewGraph() { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 398 | llvm::ViewGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 399 | } |