summaryrefslogtreecommitdiff
path: root/tools/llvmc2/CompilationGraph.cpp
blob: f6cbe0875435d550468fdf30589ed3fb40f3826f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open
// Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  Compilation graph - implementation.
//
//===----------------------------------------------------------------------===//

#include "CompilationGraph.h"

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/DOTGraphTraits.h"
#include "llvm/Support/GraphWriter.h"

#include <stdexcept>

using namespace llvm;
using namespace llvmcc;

extern cl::list<std::string> InputFilenames;
extern cl::opt<std::string> OutputFilename;

// 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.");
}

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())
    throw std::runtime_error("Node " + ToolName + " is not in the graph");
  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())
    throw std::runtime_error("Node " + ToolName + " is not in the graph!");
  return I->second;
}

const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
  LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
  if (Lang == ExtsToLangs.end())
    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;
}

void CompilationGraph::insertNode(Tool* V) {
  if (!NodesMap.count(V->Name())) {
    Node N;
    N.OwningGraph = this;
    N.ToolPtr = V;
    NodesMap[V->Name()] = N;
  }
}

void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
  if (A == "root") {
    const Node& N = getNode(E->ToolName());
    const std::string& InputLanguage = N.ToolPtr->InputLanguage();
    ToolsMap[InputLanguage].push_back(E->ToolName());

    // Needed to support iteration via GraphTraits.
    NodesMap["root"].AddEdge(E);
  }
  else {
    Node& N = getNode(A);
    N.AddEdge(E);
  }
}

// TOFIX: support more interesting graph topologies. We will need to
// do topological sorting to process multiple Join nodes correctly.
int CompilationGraph::Build (const sys::Path& tempDir) const {
  PathVector JoinList;
  const Tool* JoinTool = 0;
  sys::Path In, Out;

  // For each input file
  for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
        E = InputFilenames.end(); B != E; ++B) {
    In = sys::Path(*B);

    // 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());

    // Pass file through the chain until we bump into a Join node or a
    // node that says that it is the last.
    bool Last = false;
    while(!Last) {
      const Tool* CurTool = N->ToolPtr.getPtr();

      if (CurTool->IsJoin()) {
        JoinList.push_back(In);
        JoinTool = CurTool;
        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();
    }
  }

  if (JoinTool) {
    // If the final output name is empty, set it to "a.out"
    if (!OutputFilename.empty()) {
      Out = sys::Path(OutputFilename);
    }
    else {
      Out = sys::Path("a");
      Out.appendSuffix(JoinTool->OutputSuffix());
    }

    if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0)
      throw std::runtime_error("Tool returned error code!");
  }

  return 0;
}

namespace llvm {
  template <>
  struct DOTGraphTraits<llvmcc::CompilationGraph*>
    : public DefaultDOTGraphTraits
  {

  template<typename GraphType>
  static std::string getNodeLabel(const Node* N, const GraphType&) {
    if (N->ToolPtr)
      return N->Name();
    else
      return "root";
  }

  };
}

void CompilationGraph::writeGraph() {
  std::ofstream O("CompilationGraph.dot");

  if (O.good()) {
    llvm::WriteGraph(this, "CompilationGraph");
    O.close();
  }
  else {
    throw std::runtime_error("");
  }
}

void CompilationGraph::viewGraph() {
  llvm::ViewGraph(this, "compilation-graph");
}