summaryrefslogtreecommitdiff
path: root/tools/llvmc/CompilerDriver.cpp
blob: f1d3e58bad9c927334436837a6a6b4f8a86f3e9b (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
217
218
219
220
221
222
223
224
225
226
//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
//
// 
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the 
// University of Illinois Open Source License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
// This file implements the bulk of the LLVM Compiler Driver (llvmc).
//
//===------------------------------------------------------------------------===

#include "CompilerDriver.h"
#include <iostream>

using namespace llvm;

namespace {
  inline std::string RemoveSuffix(const std::string& fullName) {
    size_t dotpos = fullName.rfind('.',fullName.size());
    if ( dotpos == std::string::npos ) return fullName;
    return fullName.substr(0, dotpos);
  }

  inline std::string GetSuffix(const std::string& fullName) {
    size_t dotpos = fullName.rfind('.',fullName.size());
    if ( dotpos = std::string::npos ) return "";
    return fullName.substr(dotpos+1);
  }
  const char OutputSuffix[] = ".o";

}


CompilerDriver::CompilerDriver(ConfigDataProvider& confDatProv )
  : cdp(&confDatProv)
  , finalPhase(LINKING)
  , optLevel(OPT_FAST_COMPILE) 
  , isDryRun(false)
  , isVerbose(false)
  , isDebug(false)
  , timeActions(false)
  , emitRawCode(false)
  , emitNativeCode(false)
  , machine()
  , libPaths()
{
  // FIXME: These libraries are platform specific
  libPaths.push_back("/lib");
  libPaths.push_back("/usr/lib");
}

CompilerDriver::~CompilerDriver() {
  cdp = 0;
  libPaths.clear();
}

void CompilerDriver::error( const std::string& errmsg ) {
  std::cerr << "Error: " << errmsg << ".\n";
  exit(1);
}

CompilerDriver::Action* CompilerDriver::GetAction(ConfigData* cd, 
                          const std::string& input, 
                          const std::string& output,
                          Phases phase)
{
  Action* pat = 0;
  switch (phase) {
    case PREPROCESSING: pat = &cd->PreProcessor; break;
    case TRANSLATION:   pat = &cd->Translator; break;
    case OPTIMIZATION:  pat = &cd->Optimizer; break;
    case ASSEMBLY:      pat = &cd->Assembler; break;
    case LINKING:       pat = &cd->Linker; break;
    default:
      assert(!"Invalid driver phase!");
      break;
  }
  assert(pat != 0 && "Invalid command pattern");
  Action* a = new Action(*pat);
  a->args[pat->inputAt] = input;
  a->args[pat->outputAt] = output;
  return a;
}

void CompilerDriver::WriteAction(Action* a) {
  std::cerr << a->program;
  std::vector<std::string>::iterator I = a->args.begin();
  while (I != a->args.end()) {
    std::cerr << " " + *I;
    ++I;
  }
  std::cerr << "\n";
}

void CompilerDriver::DoAction(Action*a)
{
  if (isVerbose)
    WriteAction(a);
  if (!isDryRun) {
    std::cerr << "execve(\"" << a->program << "\",[\n";
    std::vector<std::string>::iterator I = a->args.begin();
    while (I != a->args.end()) {
      std::cerr << "  \"" << *I << "\",\n";
      ++I;
    }
    std::cerr << "],ENV);\n";
  }
}

int CompilerDriver::execute(const InputList& InpList, 
                            const std::string& Output ) {
  // Echo the configuration of options if we're running verbose
  if (isDebug)
  {
    std::cerr << "Compiler Driver Options:\n";
    std::cerr << "DryRun = " << isDryRun << "\n";
    std::cerr << "Verbose = " << isVerbose << " \n";
    std::cerr << "TimeActions = " << timeActions << "\n";
    std::cerr << "EmitRawCode = " << emitRawCode << "\n";
    std::cerr << "OutputMachine = " << machine << "\n";
    std::cerr << "EmitNativeCode = " << emitNativeCode << "\n";
    InputList::const_iterator I = InpList.begin();
    while ( I != InpList.end() ) {
      std::cerr << "Input: " << I->first << "(" << I->second << ")\n";
      ++I;
    }
    std::cerr << "Output: " << Output << "\n";
  }

  // If there's no input, we're done.
  if (InpList.empty())
    error("Nothing to compile.");

  // If they are asking for linking and didn't provide an output
  // file then its an error (no way for us to "make up" a meaningful
  // file name based on the various linker input files).
  if (finalPhase == LINKING && Output.empty())
    error("An output file name must be specified for linker output");

  std::vector<Action*> actions;

  /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
  // for each input item
  std::vector<std::string> LinkageItems;
  InputList::const_iterator I = InpList.begin();
  while ( I != InpList.end() ) {
    // Get the suffix of the file name
    std::string suffix = GetSuffix(I->first);

    // If its a library, bytecode file, or object file, save 
    // it for linking below and short circuit the 
    // pre-processing/translation/assembly phases
    if (I->second.empty() || suffix == "o" || suffix == "bc") {
      // We shouldn't get any of these types of files unless we're 
      // later going to link. Enforce this limit now.
      if (finalPhase != LINKING) {
        error("Pre-compiled objects found but linking not requested");
      }
      LinkageItems.push_back(I->first);
      continue; // short circuit remainder of loop
    }

    // At this point, we know its something we need to translate
    // and/or optimize. See if we can get the configuration data
    // for this kind of file.
    ConfigData* cd = cdp->ProvideConfigData(I->second);
    if (cd == 0)
      error(std::string("Files of type '") + I->second + 
            "' are not recognized." ); 

    // We have valid configuration data, now figure out where the output
    // of compilation should end up.
    std::string OutFile;
    if (finalPhase != LINKING) {
      if (InpList.size() == 1 && !Output.empty()) 
        OutFile = Output;
      else
        OutFile = RemoveSuffix(I->first) + OutputSuffix;
    } else {
      OutFile = Output;
    }

    /// PRE-PROCESSING PHASE
    if (finalPhase == PREPROCESSING) {
      if (cd->PreProcessor.program.empty())
        error(cd->langName + " does not support pre-processing");
      else
        actions.push_back(GetAction(cd,I->first,OutFile,PREPROCESSING));
    } else if (cd->PreprocessorNeeded && !cd->TranslatorPreprocesses) {
      if (!cd->PreProcessor.program.empty()) {
        actions.push_back(GetAction(cd,I->first,OutFile,PREPROCESSING));
      }
    }

    // Short-circuit remaining actions if all they want is pre-processing
    if (finalPhase == PREPROCESSING) { ++I; continue; };

    /// TRANSLATION PHASE
    actions.push_back(GetAction(cd,I->first,OutFile,TRANSLATION));
    // Short-circuit remaining actions if all they want is translation
    if (finalPhase == TRANSLATION) { ++I; continue; }

    /// OPTIMIZATION PHASE
    actions.push_back(GetAction(cd,I->first,OutFile,OPTIMIZATION));
    // Short-circuit remaining actions if all they want is optimization
    if (finalPhase == OPTIMIZATION) { ++I; continue; }

    ++I;
  }

  /// LINKING PHASE

  /// RUN THE ACTIONS
  std::vector<Action*>::iterator aIter = actions.begin();
  while (aIter != actions.end()) {
    DoAction(*aIter);
    aIter++;
  }

  return 0;
}

// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab