blob: 64232f3d2c1b5ddc424cb3ff8a38f895c4769bea [file] [log] [blame]
Reid Spencera3f18552004-08-13 20:25:54 +00001//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
Reid Spencer5c56dc12004-08-13 20:22:43 +00002//
Misha Brukman3da94ae2005-04-22 00:00:37 +00003//
Reid Spencer5c56dc12004-08-13 20:22:43 +00004// The LLVM Compiler Infrastructure
5//
Misha Brukman3da94ae2005-04-22 00:00:37 +00006// This file was developed by Reid Spencer and is distributed under the
Reid Spencer5c56dc12004-08-13 20:22:43 +00007// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00008//
Reid Spencer5c56dc12004-08-13 20:22:43 +00009//===----------------------------------------------------------------------===//
10//
Reid Spencera3f18552004-08-13 20:25:54 +000011// This file implements the bulk of the LLVM Compiler Driver (llvmc).
Reid Spencer5c56dc12004-08-13 20:22:43 +000012//
Chris Lattner74f48d12006-05-29 18:52:05 +000013//===----------------------------------------------------------------------===//
Reid Spencer5c56dc12004-08-13 20:22:43 +000014
15#include "CompilerDriver.h"
Reid Spencerbf437722004-08-15 08:19:46 +000016#include "ConfigLexer.h"
Reid Spencera01439a2004-08-24 13:55:17 +000017#include "llvm/Module.h"
Reid Spencer52c2dc12004-08-29 19:26:56 +000018#include "llvm/Bytecode/Reader.h"
Reid Spencer54fafe42004-09-14 01:58:45 +000019#include "llvm/Support/Timer.h"
Reid Spencer93426ba2004-08-29 20:02:28 +000020#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/StringExtras.h"
Brian Gaekefabf41f2004-12-20 04:02:01 +000023#include "llvm/Config/alloca.h"
Misha Brukmanbaec07c2005-04-20 04:51:29 +000024#include <iostream>
Reid Spencer5c56dc12004-08-13 20:22:43 +000025using namespace llvm;
26
27namespace {
Reid Spencer5c56dc12004-08-13 20:22:43 +000028
Reid Spenceraf77d742004-10-28 04:05:06 +000029void WriteAction(CompilerDriver::Action* action ) {
30 std::cerr << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000031 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000032 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000033 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000034 ++I;
35 }
Misha Brukman0b861482005-05-03 20:30:34 +000036 std::cerr << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000037}
38
39void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000041 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000042 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000043 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000044 ++I;
45 }
Misha Brukman0b861482005-05-03 20:30:34 +000046 std::cerr << '\n';
47 std::cerr << "flags = " << action->flags << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000048}
49
50void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
Misha Brukman3da94ae2005-04-22 00:00:37 +000051 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
Reid Spenceraf77d742004-10-28 04:05:06 +000052 << ")\n";
53 std::cerr << "PreProcessor: ";
54 DumpAction(&cd->PreProcessor);
55 std::cerr << "Translator: ";
56 DumpAction(&cd->Translator);
57 std::cerr << "Optimizer: ";
58 DumpAction(&cd->Optimizer);
59 std::cerr << "Assembler: ";
60 DumpAction(&cd->Assembler);
61 std::cerr << "Linker: ";
62 DumpAction(&cd->Linker);
63}
64
65/// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
66/// which should reduce the volume of code and make compilation
Misha Brukman3da94ae2005-04-22 00:00:37 +000067/// faster. This is also safe on any llvm module.
Reid Spenceraf77d742004-10-28 04:05:06 +000068static const char* DefaultFastCompileOptimizations[] = {
69 "-simplifycfg", "-mem2reg", "-instcombine"
70};
71
72class CompilerDriverImpl : public CompilerDriver {
73/// @name Constructors
74/// @{
75public:
76 CompilerDriverImpl(ConfigDataProvider& confDatProv )
77 : cdp(&confDatProv)
78 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000079 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000080 , Flags(0)
81 , machine()
82 , LibraryPaths()
83 , TempDir()
84 , AdditionalArgs()
85 {
86 TempDir = sys::Path::GetTemporaryDirectory();
87 sys::RemoveDirectoryOnSignal(TempDir);
88 AdditionalArgs.reserve(NUM_PHASES);
89 StringVector emptyVec;
90 for (unsigned i = 0; i < NUM_PHASES; ++i)
91 AdditionalArgs.push_back(emptyVec);
92 }
93
94 virtual ~CompilerDriverImpl() {
95 cleanup();
96 cdp = 0;
97 LibraryPaths.clear();
98 IncludePaths.clear();
99 Defines.clear();
100 TempDir.clear();
101 AdditionalArgs.clear();
102 fOptions.clear();
103 MOptions.clear();
104 WOptions.clear();
105 }
106
107/// @}
108/// @name Methods
109/// @{
110public:
Misha Brukman0b861482005-05-03 20:30:34 +0000111 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000112 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000113 }
114
Misha Brukman0b861482005-05-03 20:30:34 +0000115 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000116 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000117 }
118
Misha Brukman0b861482005-05-03 20:30:34 +0000119 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000120 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000121 }
122
Misha Brukman0b861482005-05-03 20:30:34 +0000123 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000124 machine = machineName;
125 }
126
127 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
128 assert(phase <= LINKING && phase >= PREPROCESSING);
129 AdditionalArgs[phase] = opts;
130 }
131
132 virtual void setIncludePaths(const StringVector& paths) {
133 StringVector::const_iterator I = paths.begin();
134 StringVector::const_iterator E = paths.end();
135 while (I != E) {
136 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000137 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000138 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000139 ++I;
140 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000141 }
142
Reid Spenceraf77d742004-10-28 04:05:06 +0000143 virtual void setSymbolDefines(const StringVector& defs) {
144 Defines = defs;
145 }
146
147 virtual void setLibraryPaths(const StringVector& paths) {
148 StringVector::const_iterator I = paths.begin();
149 StringVector::const_iterator E = paths.end();
150 while (I != E) {
151 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000152 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000153 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000154 ++I;
155 }
Reid Spencerbf437722004-08-15 08:19:46 +0000156 }
157
Misha Brukman0b861482005-05-03 20:30:34 +0000158 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000159 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000160 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000161
Misha Brukman0b861482005-05-03 20:30:34 +0000162 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000163 ToolPaths.push_back(toolPath);
164 }
165
Reid Spenceraf77d742004-10-28 04:05:06 +0000166 virtual void setfPassThrough(const StringVector& fOpts) {
167 fOptions = fOpts;
168 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169
Reid Spenceraf77d742004-10-28 04:05:06 +0000170 /// @brief Set the list of -M options to be passed through
171 virtual void setMPassThrough(const StringVector& MOpts) {
172 MOptions = MOpts;
173 }
Reid Spencerbae68252004-08-19 04:49:47 +0000174
Reid Spenceraf77d742004-10-28 04:05:06 +0000175 /// @brief Set the list of -W options to be passed through
176 virtual void setWPassThrough(const StringVector& WOpts) {
177 WOptions = WOpts;
178 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000179
Reid Spenceraf77d742004-10-28 04:05:06 +0000180/// @}
181/// @name Functions
182/// @{
183private:
184 bool isSet(DriverFlags flag) {
185 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
186 }
Reid Spencerbae68252004-08-19 04:49:47 +0000187
Reid Spenceraf77d742004-10-28 04:05:06 +0000188 void cleanup() {
189 if (!isSet(KEEP_TEMPS_FLAG)) {
Chris Lattner33b0e9c2006-08-01 18:12:29 +0000190 sys::FileStatus Status;
191 if (!TempDir.getFileStatus(Status) && Status.isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000192 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000193 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000194 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000195 }
196 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000197
Misha Brukman3da94ae2005-04-22 00:00:37 +0000198 sys::Path MakeTempFile(const std::string& basename,
Misha Brukman0b861482005-05-03 20:30:34 +0000199 const std::string& suffix) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000200 sys::Path result(TempDir);
Reid Spencerdd04df02005-07-07 23:21:43 +0000201 if (!result.appendComponent(basename))
Reid Spenceraf77d742004-10-28 04:05:06 +0000202 throw basename + ": can't use this file name";
Reid Spencer07adb282004-11-05 22:15:36 +0000203 if (!result.appendSuffix(suffix))
Reid Spenceraf77d742004-10-28 04:05:06 +0000204 throw suffix + ": can't use this file suffix";
205 return result;
206 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000207
Misha Brukman3da94ae2005-04-22 00:00:37 +0000208 Action* GetAction(ConfigData* cd,
209 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000210 const sys::Path& output,
211 Phases phase)
212 {
213 Action* pat = 0; ///< The pattern/template for the action
214 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000215
Reid Spenceraf77d742004-10-28 04:05:06 +0000216 // Get the action pattern
217 switch (phase) {
218 case PREPROCESSING: pat = &cd->PreProcessor; break;
219 case TRANSLATION: pat = &cd->Translator; break;
220 case OPTIMIZATION: pat = &cd->Optimizer; break;
221 case ASSEMBLY: pat = &cd->Assembler; break;
222 case LINKING: pat = &cd->Linker; break;
223 default:
224 assert(!"Invalid driver phase!");
225 break;
226 }
227 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000228
Reid Spenceraf77d742004-10-28 04:05:06 +0000229 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000230 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000231
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000232 // See if program starts with wildcard...
233 std::string programName=pat->program.toString();
234 if (programName[0] == '%' && programName.length() >2) {
235 switch(programName[1]){
236 case 'b':
237 if (programName.substr(0,8) == "%bindir%") {
238 std::string tmp(LLVM_BINDIR);
239 tmp.append(programName.substr(8));
240 pat->program.set(tmp);
241 }
242 break;
243 case 'l':
244 if (programName.substr(0,12) == "%llvmgccdir%"){
245 std::string tmp(LLVMGCCDIR);
246 tmp.append(programName.substr(12));
247 pat->program.set(tmp);
248 }else if (programName.substr(0,13) == "%llvmgccarch%"){
249 std::string tmp(LLVMGCCARCH);
250 tmp.append(programName.substr(13));
251 pat->program.set(tmp);
252 }else if (programName.substr(0,9) == "%llvmgcc%"){
253 std::string tmp(LLVMGCC);
254 tmp.append(programName.substr(9));
255 pat->program.set(tmp);
256 }else if (programName.substr(0,9) == "%llvmgxx%"){
257 std::string tmp(LLVMGXX);
258 tmp.append(programName.substr(9));
259 pat->program.set(tmp);
260 }else if (programName.substr(0,9) == "%llvmcc1%"){
261 std::string tmp(LLVMCC1);
262 tmp.append(programName.substr(9));
263 pat->program.set(tmp);
264 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
265 std::string tmp(LLVMCC1PLUS);
266 tmp.append(programName.substr(13));
267 pat->program.set(tmp);
268 }else if (programName.substr(0,8) == "%libdir%") {
269 std::string tmp(LLVM_LIBDIR);
270 tmp.append(programName.substr(8));
271 pat->program.set(tmp);
272 }
273 break;
274 }
275 }
276 action->program = pat->program;
277
Reid Spenceraf77d742004-10-28 04:05:06 +0000278 // Do the substitutions from the pattern to the actual
279 StringVector::iterator PI = pat->args.begin();
280 StringVector::iterator PE = pat->args.end();
281 while (PI != PE) {
282 if ((*PI)[0] == '%' && PI->length() >2) {
283 bool found = true;
284 switch ((*PI)[1]) {
285 case 'a':
286 if (*PI == "%args%") {
287 if (AdditionalArgs.size() > unsigned(phase))
288 if (!AdditionalArgs[phase].empty()) {
289 // Get specific options for each kind of action type
290 StringVector& addargs = AdditionalArgs[phase];
291 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000292 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000293 addargs.end());
294 }
295 } else
296 found = false;
297 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000298 case 'b':
299 if (*PI == "%bindir%") {
300 std::string tmp(*PI);
301 tmp.replace(0,8,LLVM_BINDIR);
302 action->args.push_back(tmp);
303 } else
304 found = false;
305 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000306 case 'd':
307 if (*PI == "%defs%") {
308 StringVector::iterator I = Defines.begin();
309 StringVector::iterator E = Defines.end();
310 while (I != E) {
311 action->args.push_back( std::string("-D") + *I);
312 ++I;
313 }
314 } else
315 found = false;
316 break;
317 case 'f':
318 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000319 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000320 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000321 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000322 } else
323 found = false;
324 break;
325 case 'i':
326 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000327 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000328 } else if (*PI == "%incls%") {
329 PathVector::iterator I = IncludePaths.begin();
330 PathVector::iterator E = IncludePaths.end();
331 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000332 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000333 ++I;
334 }
335 } else
336 found = false;
337 break;
338 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000339 if ((*PI)[1] == 'l') {
340 std::string tmp(*PI);
341 if (*PI == "%llvmgccdir%")
342 tmp.replace(0,12,LLVMGCCDIR);
343 else if (*PI == "%llvmgccarch%")
344 tmp.replace(0,13,LLVMGCCARCH);
345 else if (*PI == "%llvmgcc%")
346 tmp.replace(0,9,LLVMGCC);
347 else if (*PI == "%llvmgxx%")
348 tmp.replace(0,9,LLVMGXX);
349 else if (*PI == "%llvmcc1%")
350 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000351 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000352 tmp.replace(0,9,LLVMCC1);
353 else
354 found = false;
355 if (found)
356 action->args.push_back(tmp);
357 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000358 PathVector::iterator I = LibraryPaths.begin();
359 PathVector::iterator E = LibraryPaths.end();
360 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000361 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000362 ++I;
363 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000364 } else if (*PI == "%libdir%") {
365 std::string tmp(*PI);
366 tmp.replace(0,8,LLVM_LIBDIR);
367 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000368 } else
369 found = false;
370 break;
371 case 'o':
372 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000373 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000374 } else if (*PI == "%opt%") {
375 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000376 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000377 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000378 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000379 cd->opts[optLevel].begin(),
380 cd->opts[optLevel].end());
381 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000382 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000383 utostr(unsigned(optLevel)) + " were not specified";
384 }
385 } else
386 found = false;
387 break;
388 case 's':
389 if (*PI == "%stats%") {
390 if (isSet(SHOW_STATS_FLAG))
391 action->args.push_back("-stats");
392 } else
393 found = false;
394 break;
395 case 't':
396 if (*PI == "%target%") {
397 action->args.push_back(std::string("-march=") + machine);
398 } else if (*PI == "%time%") {
399 if (isSet(TIME_PASSES_FLAG))
400 action->args.push_back("-time-passes");
401 } else
402 found = false;
403 break;
404 case 'v':
405 if (*PI == "%verbose%") {
406 if (isSet(VERBOSE_FLAG))
407 action->args.push_back("-v");
408 } else
409 found = false;
410 break;
411 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000412 if (*PI == "%Mopts%") {
413 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000414 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000415 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000416 } else
417 found = false;
418 break;
419 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000420 if (*PI == "%Wopts%") {
421 for (StringVector::iterator I = WOptions.begin(),
422 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000423 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000424 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000425 } else
426 found = false;
427 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000428 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000429 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000430 break;
431 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000432 if (!found) {
433 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000434 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000435 (*PI)[PI->length()-1] == '%') {
436 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000437 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000438 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000439 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000440 action->args.push_back(*PI);
441 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000442 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000443 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000444 // Its not a substitution, just put it in the action
445 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000446 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000447 PI++;
448 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000449
Reid Spenceraf77d742004-10-28 04:05:06 +0000450 // Finally, we're done
451 return action;
452 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000453
Reid Spenceraf77d742004-10-28 04:05:06 +0000454 bool DoAction(Action*action) {
455 assert(action != 0 && "Invalid Action!");
456 if (isSet(VERBOSE_FLAG))
457 WriteAction(action);
458 if (!isSet(DRY_RUN_FLAG)) {
459 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000460 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000461 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000462 throw std::string("Can't find program '" +
463 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000464 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000465 action->program = progpath;
466 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000467 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000468 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000469
470 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000471 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000472 alloca(sizeof(const char*)*(action->args.size()+2));
473 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000474 for (unsigned i = 1; i <= action->args.size(); ++i)
475 Args[i] = action->args[i-1].c_str();
476 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000477 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000478 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000479 timer.startTimer();
Chris Lattner7456e3c2005-02-13 23:10:45 +0000480 int resultCode = sys::Program::ExecuteAndWait(action->program, Args);
Reid Spenceraf77d742004-10-28 04:05:06 +0000481 timer.stopTimer();
482 timer.print(timer,std::cerr);
483 return resultCode == 0;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000484 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000485 else
Chris Lattner7456e3c2005-02-13 23:10:45 +0000486 return 0 == sys::Program::ExecuteAndWait(action->program, Args);
Reid Spenceraf77d742004-10-28 04:05:06 +0000487 }
488 return true;
489 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000490
Reid Spenceraf77d742004-10-28 04:05:06 +0000491 /// This method tries various variants of a linkage item's file
492 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000493 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000494 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000495 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000496 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000497 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000498 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000499 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000500 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000501 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000502 fullpath.set(PI->toString());
503 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000504 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000505 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000506 if (native) {
507 fullpath.appendSuffix("a");
508 } else {
509 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000510 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000511 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000512 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000513 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000514 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000515 return fullpath;
516 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000517 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000518 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000519 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000520 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000521 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000522 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000523 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000524 return fullpath;
525 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000526 }
527
528 // Didn't find one.
529 fullpath.clear();
530 return fullpath;
531 }
532
533 /// This method processes a linkage item. The item could be a
534 /// Bytecode file needing translation to native code and that is
535 /// dependent on other bytecode libraries, or a native code
536 /// library that should just be linked into the program.
537 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
538 SetVector<sys::Path>& set,
539 std::string& err) {
540 // First, see if the unadorned file name is not readable. If so,
541 // we must track down the file in the lib search path.
542 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000543 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000544 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000545 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000546 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000547
Reid Spenceraf77d742004-10-28 04:05:06 +0000548 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000549 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000550 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000551 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000552 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000553 return false;
554 }
555 } else {
556 fullpath = link_item;
557 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000558
Reid Spenceraf77d742004-10-28 04:05:06 +0000559 // If we got here fullpath is the path to the file, and its readable.
560 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000561
Reid Spenceraf77d742004-10-28 04:05:06 +0000562 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000563 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000564 // Process the dependent libraries recursively
565 Module::LibraryListType modlibs;
Reid Spencer1fce0912004-12-11 00:14:15 +0000566 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000567 // Traverse the dependent libraries list
568 Module::lib_iterator LI = modlibs.begin();
569 Module::lib_iterator LE = modlibs.end();
570 while ( LI != LE ) {
571 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
572 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000573 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000574 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000575 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000576 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000577 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000578 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000579 return false;
580 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000581 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000582 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000583 } else if (err.empty()) {
584 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000585 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000586 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000587 return false;
588 }
589 }
590 return true;
591 }
592
593/// @}
594/// @name Methods
595/// @{
596public:
597 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
598 try {
599 // Echo the configuration of options if we're running verbose
600 if (isSet(DEBUG_FLAG)) {
601 std::cerr << "Compiler Driver Options:\n";
602 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
603 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
604 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
605 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
606 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
607 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
608 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
609 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
610 std::cerr << "OutputMachine = " << machine << "\n";
611 InputList::const_iterator I = InpList.begin();
612 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000613 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000614 << ")\n";
615 ++I;
616 }
Reid Spencer12786d52004-12-13 08:53:36 +0000617 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000618 }
619
Reid Spenceraf77d742004-10-28 04:05:06 +0000620 // If there's no input, we're done.
621 if (InpList.empty())
622 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000623
Reid Spenceraf77d742004-10-28 04:05:06 +0000624 // If they are asking for linking and didn't provide an output
625 // file then its an error (no way for us to "make up" a meaningful
626 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000627 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000628 throw std::string(
629 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000630
Reid Spenceraf77d742004-10-28 04:05:06 +0000631 // If they are not asking for linking, provided an output file and
632 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000633 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000634 throw std::string("An output file name cannot be specified ") +
635 "with more than one input file name when not linking";
636
637 // This vector holds all the resulting actions of the following loop.
638 std::vector<Action*> actions;
639
640 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
641 // for each input item
642 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000643 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000644 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000645 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
646 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000647 // Get the suffix of the file name
648 const std::string& ftype = I->second;
649
Misha Brukman3da94ae2005-04-22 00:00:37 +0000650 // If its a library, bytecode file, or object file, save
651 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000652 // pre-processing/translation/assembly phases
653 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000654 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000655 // later going to link. Enforce this limit now.
656 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000657 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000658 "Pre-compiled objects found but linking not requested");
659 }
660 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000661 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000662 else
663 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000664 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000665 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000666
Reid Spenceraf77d742004-10-28 04:05:06 +0000667 // At this point, we know its something we need to translate
668 // and/or optimize. See if we can get the configuration data
669 // for this kind of file.
670 ConfigData* cd = cdp->ProvideConfigData(I->second);
671 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000672 throw std::string("Files of type '") + I->second +
673 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000674 if (isSet(DEBUG_FLAG))
675 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000676
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000677 // Add the config data's library paths to the end of the list
678 for (StringVector::iterator LPI = cd->libpaths.begin(),
679 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
680 LibraryPaths.push_back(sys::Path(*LPI));
681 }
682
Reid Spenceraf77d742004-10-28 04:05:06 +0000683 // Initialize the input and output files
684 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000685 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000686
Reid Spenceraf77d742004-10-28 04:05:06 +0000687 // PRE-PROCESSING PHASE
688 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000689
Reid Spenceraf77d742004-10-28 04:05:06 +0000690 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000691 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000692 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
693 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000694 if (Output.isEmpty()) {
695 OutFile.appendSuffix("E");
696 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
697 } else {
698 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
699 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000700 } else {
Reid Spencer07adb282004-11-05 22:15:36 +0000701 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000702 actions.push_back(GetAction(cd,InFile,TempFile,
703 PREPROCESSING));
704 InFile = TempFile;
705 }
706 }
707 } else if (finalPhase == PREPROCESSING) {
708 throw cd->langName + " does not support pre-processing";
709 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000710 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000711 cd->langName + " files";
712 }
713
Misha Brukman3da94ae2005-04-22 00:00:37 +0000714 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000715 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000716 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000717
718 /// TRANSLATION PHASE
719 action = cd->Translator;
720
721 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000722 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000723 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
724 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000725 if (Output.isEmpty()) {
726 OutFile.appendSuffix("o");
727 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
728 } else {
729 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
730 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000731 } else {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000732 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000733 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
734 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000735 }
736
Reid Spenceraf77d742004-10-28 04:05:06 +0000737 // ll -> bc Helper
738 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
739 /// The output of the translator is an LLVM Assembly program
740 /// We need to translate it to bytecode
741 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000742 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000743 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000744 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000745 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000746 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000747 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000748 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000749 }
750 } else if (finalPhase == TRANSLATION) {
751 throw cd->langName + " does not support translation";
752 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000753 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000754 cd->langName + " files";
755 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000756
Reid Spenceraf77d742004-10-28 04:05:06 +0000757 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000758 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000759
Reid Spenceraf77d742004-10-28 04:05:06 +0000760 /// OPTIMIZATION PHASE
761 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000762
Reid Spenceraf77d742004-10-28 04:05:06 +0000763 // Get the optimization action, if needed, or error if appropriate
764 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000765 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000766 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
767 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000768 if (Output.isEmpty()) {
769 OutFile.appendSuffix("o");
770 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
771 } else {
772 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
773 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000774 } else {
Reid Spencer07adb282004-11-05 22:15:36 +0000775 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
Reid Spenceraf77d742004-10-28 04:05:06 +0000776 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
777 InFile = TempFile;
778 }
779 // ll -> bc Helper
780 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
781 /// The output of the optimizer is an LLVM Assembly program
782 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000783 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000784 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000785 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000786 action->args.push_back("-f");
787 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000788 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000789 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000790 actions.push_back(action);
791 }
792 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000793 } else if (finalPhase == OPTIMIZATION) {
794 throw cd->langName + " does not support optimization";
795 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000796 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000797 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000798 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000799 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000800
801 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000802 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000803
804 /// ASSEMBLY PHASE
805 action = cd->Assembler;
806
807 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000808
809 // Build either a native compilation action or a disassembly action
810 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000811 if (isSet(EMIT_NATIVE_FLAG)) {
812 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000813 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000814 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000815 action->args.push_back("-f");
816 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000817 if (Output.isEmpty()) {
818 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000819 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000820 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000821 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000822 }
823 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000824 } else {
825 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000826 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000827 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000828 action->args.push_back("-f");
829 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000830 if (Output.isEmpty()) {
831 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000832 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000833 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000834 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000835 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000836 }
837
Reid Spencer679a7232004-11-20 20:39:33 +0000838 // Put the action on the list
839 actions.push_back(action);
840
Misha Brukman3da94ae2005-04-22 00:00:37 +0000841 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000842 continue;
843 }
844
845 // Register the result of the actions as a link candidate
846 LinkageItems.insert(InFile);
847
Reid Spenceraf77d742004-10-28 04:05:06 +0000848 } // end while loop over each input file
849
850 /// RUN THE COMPILATION ACTIONS
851 std::vector<Action*>::iterator AI = actions.begin();
852 std::vector<Action*>::iterator AE = actions.end();
853 while (AI != AE) {
854 if (!DoAction(*AI))
855 throw std::string("Action failed");
856 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000857 }
858
Reid Spenceraf77d742004-10-28 04:05:06 +0000859 /// LINKING PHASE
860 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000861
Reid Spenceraf77d742004-10-28 04:05:06 +0000862 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000863 std::vector<sys::Path> SysLibs;
864 sys::Path::GetSystemLibraryPaths(SysLibs);
865 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000866
867 // Set up the linking action with llvm-ld
868 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000869 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000870
871 // Add in the optimization level requested
872 switch (optLevel) {
873 case OPT_FAST_COMPILE:
874 link->args.push_back("-O1");
875 break;
876 case OPT_SIMPLE:
877 link->args.push_back("-O2");
878 break;
879 case OPT_AGGRESSIVE:
880 link->args.push_back("-O3");
881 break;
882 case OPT_LINK_TIME:
883 link->args.push_back("-O4");
884 break;
885 case OPT_AGGRESSIVE_LINK_TIME:
886 link->args.push_back("-O5");
887 break;
888 case OPT_NONE:
889 break;
890 }
891
892 // Add in all the linkage items we generated. This includes the
893 // output from the translation/optimization phases as well as any
894 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000895 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000896 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000897 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000898
899 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000900 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000901 E=LibFiles.end(); I != E; ++I )
902 link->args.push_back(std::string("-l")+*I);
903
904 // Add in all the library paths to the command line
905 for (PathVector::const_iterator I=LibraryPaths.begin(),
906 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000907 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000908
909 // Add in the additional linker arguments requested
910 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
911 E=AdditionalArgs[LINKING].end(); I != E; ++I)
912 link->args.push_back( *I );
913
914 // Add in other optional flags
915 if (isSet(EMIT_NATIVE_FLAG))
916 link->args.push_back("-native");
917 if (isSet(VERBOSE_FLAG))
918 link->args.push_back("-v");
919 if (isSet(TIME_PASSES_FLAG))
920 link->args.push_back("-time-passes");
921 if (isSet(SHOW_STATS_FLAG))
922 link->args.push_back("-stats");
923 if (isSet(STRIP_OUTPUT_FLAG))
924 link->args.push_back("-s");
925 if (isSet(DEBUG_FLAG)) {
926 link->args.push_back("-debug");
927 link->args.push_back("-debug-pass=Details");
928 }
929
930 // Add in mandatory flags
931 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000932 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000933
934 // Execute the link
935 if (!DoAction(link))
936 throw std::string("Action failed");
937 }
938 } catch (std::string& msg) {
939 cleanup();
940 throw;
941 } catch (...) {
942 cleanup();
943 throw std::string("Unspecified error");
944 }
945 cleanup();
946 return 0;
947 }
948
949/// @}
950/// @name Data
951/// @{
952private:
953 ConfigDataProvider* cdp; ///< Where we get configuration data from
954 Phases finalPhase; ///< The final phase of compilation
955 OptimizationLevels optLevel; ///< The optimization level to apply
956 unsigned Flags; ///< The driver flags
957 std::string machine; ///< Target machine name
958 PathVector LibraryPaths; ///< -L options
959 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000960 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +0000961 StringVector Defines; ///< -D options
962 sys::Path TempDir; ///< Name of the temporary directory.
963 StringTable AdditionalArgs; ///< The -Txyz options
964 StringVector fOptions; ///< -f options
965 StringVector MOptions; ///< -M options
966 StringVector WOptions; ///< -W options
967
968/// @}
969};
Reid Spencer5c56dc12004-08-13 20:22:43 +0000970}
971
972CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000973}
974
Reid Spencercc97cfc2005-05-19 00:52:28 +0000975CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
976
Reid Spencer52c2dc12004-08-29 19:26:56 +0000977CompilerDriver*
978CompilerDriver::Get(ConfigDataProvider& CDP) {
979 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +0000980}
981
982CompilerDriver::ConfigData::ConfigData()
983 : langName()
984 , PreProcessor()
985 , Translator()
986 , Optimizer()
987 , Assembler()
988 , Linker()
989{
990 StringVector emptyVec;
991 for (unsigned i = 0; i < NUM_PHASES; ++i)
992 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000993}