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