move an llvmc-specific function out of the bcreader into llvmc.
[oota-llvm.git] / tools / llvmc / CompilerDriver.cpp
1 //===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
2 //
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file was developed by Reid Spencer and is distributed under the
7 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file implements the bulk of the LLVM Compiler Driver (llvmc).
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "CompilerDriver.h"
16 #include "ConfigLexer.h"
17 #include "llvm/Module.h"
18 #include "llvm/Bytecode/Reader.h"
19 #include "llvm/Support/Timer.h"
20 #include "llvm/System/Signals.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/Config/alloca.h"
24 #include <iostream>
25 using namespace llvm;
26
27 namespace {
28
29 void WriteAction(CompilerDriver::Action* action ) {
30   std::cerr << action->program.c_str();
31   std::vector<std::string>::const_iterator I = action->args.begin();
32   while (I != action->args.end()) {
33     std::cerr << ' ' << *I;
34     ++I;
35   }
36   std::cerr << '\n';
37 }
38
39 void DumpAction(CompilerDriver::Action* action) {
40   std::cerr << "command = " << action->program.c_str();
41   std::vector<std::string>::const_iterator I = action->args.begin();
42   while (I != action->args.end()) {
43     std::cerr << ' ' << *I;
44     ++I;
45   }
46   std::cerr << '\n';
47   std::cerr << "flags = " << action->flags << '\n';
48 }
49
50 void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
51   std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
52     << ")\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 static bool GetBytecodeDependentLibraries(const std::string &fname,
66                                           Module::LibraryListType& deplibs,
67                                           BCDecompressor_t *BCDC,
68                                           std::string* ErrMsg) {
69   ModuleProvider* MP = getBytecodeModuleProvider(fname, BCDC, ErrMsg);
70   if (!MP) {
71     deplibs.clear();
72     return true;
73   }
74   Module* M = MP->releaseModule(ErrMsg);
75   deplibs = M->getLibraries();
76   delete M;
77   delete MP;
78   return false;
79 }
80
81
82 class CompilerDriverImpl : public CompilerDriver {
83 /// @name Constructors
84 /// @{
85 public:
86   CompilerDriverImpl(ConfigDataProvider& confDatProv )
87     : cdp(&confDatProv)
88     , finalPhase(LINKING)
89     , optLevel(OPT_FAST_COMPILE)
90     , Flags(0)
91     , machine()
92     , LibraryPaths()
93     , TempDir()
94     , AdditionalArgs()
95   {
96     AdditionalArgs.reserve(NUM_PHASES);
97     StringVector emptyVec;
98     for (unsigned i = 0; i < NUM_PHASES; ++i)
99       AdditionalArgs.push_back(emptyVec);
100   }
101
102   virtual ~CompilerDriverImpl() {
103     cleanup();
104     cdp = 0;
105     LibraryPaths.clear();
106     IncludePaths.clear();
107     Defines.clear();
108     TempDir.clear();
109     AdditionalArgs.clear();
110     fOptions.clear();
111     MOptions.clear();
112     WOptions.clear();
113   }
114
115 /// @}
116 /// @name Methods
117 /// @{
118 public:
119   virtual void setFinalPhase(Phases phase) {
120     finalPhase = phase;
121   }
122
123   virtual void setOptimization(OptimizationLevels level) {
124     optLevel = level;
125   }
126
127   virtual void setDriverFlags(unsigned flags) {
128     Flags = flags & DRIVER_FLAGS_MASK;
129   }
130
131   virtual void setOutputMachine(const std::string& machineName) {
132     machine = machineName;
133   }
134
135   virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
136     assert(phase <= LINKING && phase >= PREPROCESSING);
137     AdditionalArgs[phase] = opts;
138   }
139
140   virtual void setIncludePaths(const StringVector& paths) {
141     StringVector::const_iterator I = paths.begin();
142     StringVector::const_iterator E = paths.end();
143     while (I != E) {
144       sys::Path tmp;
145       tmp.set(*I);
146       IncludePaths.push_back(tmp);
147       ++I;
148     }
149   }
150
151   virtual void setSymbolDefines(const StringVector& defs) {
152     Defines = defs;
153   }
154
155   virtual void setLibraryPaths(const StringVector& paths) {
156     StringVector::const_iterator I = paths.begin();
157     StringVector::const_iterator E = paths.end();
158     while (I != E) {
159       sys::Path tmp;
160       tmp.set(*I);
161       LibraryPaths.push_back(tmp);
162       ++I;
163     }
164   }
165
166   virtual void addLibraryPath(const sys::Path& libPath) {
167     LibraryPaths.push_back(libPath);
168   }
169
170   virtual void addToolPath(const sys::Path& toolPath) {
171     ToolPaths.push_back(toolPath);
172   }
173
174   virtual void setfPassThrough(const StringVector& fOpts) {
175     fOptions = fOpts;
176   }
177
178   /// @brief Set the list of -M options to be passed through
179   virtual void setMPassThrough(const StringVector& MOpts) {
180     MOptions = MOpts;
181   }
182
183   /// @brief Set the list of -W options to be passed through
184   virtual void setWPassThrough(const StringVector& WOpts) {
185     WOptions = WOpts;
186   }
187
188 /// @}
189 /// @name Functions
190 /// @{
191 private:
192   bool isSet(DriverFlags flag) {
193     return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
194   }
195
196   void cleanup() {
197     if (!isSet(KEEP_TEMPS_FLAG)) {
198       sys::FileStatus Status;
199       if (!TempDir.getFileStatus(Status) && Status.isDir)
200         TempDir.eraseFromDisk(/*remove_contents=*/true);
201     } else {
202       std::cout << "Temporary files are in " << TempDir << "\n";
203     }
204   }
205
206   sys::Path MakeTempFile(const std::string& basename,
207                          const std::string& suffix,
208                          std::string* ErrMsg) {
209     if (TempDir.isEmpty()) {
210       TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
211       if (TempDir.isEmpty())
212         return sys::Path();
213       sys::RemoveDirectoryOnSignal(TempDir);
214     }
215     sys::Path result(TempDir);
216     if (!result.appendComponent(basename)) {
217       if (ErrMsg)
218         *ErrMsg = basename + ": can't use this file name";
219       return sys::Path();
220     }
221     if (!result.appendSuffix(suffix)) {
222       if (ErrMsg)
223         *ErrMsg = suffix + ": can't use this file suffix";
224       return sys::Path();
225     }
226     return result;
227   }
228
229   Action* GetAction(ConfigData* cd,
230                     const sys::Path& input,
231                     const sys::Path& output,
232                     Phases phase)
233   {
234     Action* pat = 0; ///< The pattern/template for the action
235     Action* action = new Action; ///< The actual action to execute
236
237     // Get the action pattern
238     switch (phase) {
239       case PREPROCESSING: pat = &cd->PreProcessor; break;
240       case TRANSLATION:   pat = &cd->Translator; break;
241       case OPTIMIZATION:  pat = &cd->Optimizer; break;
242       case ASSEMBLY:      pat = &cd->Assembler; break;
243       case LINKING:       pat = &cd->Linker; break;
244       default:
245         assert(!"Invalid driver phase!");
246         break;
247     }
248     assert(pat != 0 && "Invalid command pattern");
249
250     // Copy over some pattern things that don't need to change
251     action->flags = pat->flags;
252
253     // See if program starts with wildcard...
254     std::string programName=pat->program.toString();
255     if (programName[0] == '%' && programName.length() >2) {
256       switch(programName[1]){
257       case 'b':
258         if (programName.substr(0,8) == "%bindir%") {
259           std::string tmp(LLVM_BINDIR);
260           tmp.append(programName.substr(8));
261           pat->program.set(tmp);
262         }
263         break;
264       case 'l':
265         if (programName.substr(0,12) == "%llvmgccdir%"){
266           std::string tmp(LLVMGCCDIR);
267           tmp.append(programName.substr(12));
268           pat->program.set(tmp);
269         }else if (programName.substr(0,13) == "%llvmgccarch%"){
270           std::string tmp(LLVMGCCARCH);
271           tmp.append(programName.substr(13));
272           pat->program.set(tmp);
273         }else if (programName.substr(0,9) == "%llvmgcc%"){
274           std::string tmp(LLVMGCC);
275           tmp.append(programName.substr(9));
276           pat->program.set(tmp);
277         }else if (programName.substr(0,9) == "%llvmgxx%"){
278           std::string tmp(LLVMGXX);
279           tmp.append(programName.substr(9));
280           pat->program.set(tmp);
281         }else if (programName.substr(0,9) == "%llvmcc1%"){
282           std::string tmp(LLVMCC1);
283           tmp.append(programName.substr(9));
284           pat->program.set(tmp);
285         }else if (programName.substr(0,13) == "%llvmcc1plus%"){
286           std::string tmp(LLVMCC1PLUS);
287           tmp.append(programName.substr(13));
288           pat->program.set(tmp);
289         }else if (programName.substr(0,8) == "%libdir%") {
290           std::string tmp(LLVM_LIBDIR);
291           tmp.append(programName.substr(8));
292           pat->program.set(tmp);
293         }
294           break;
295       }
296     }
297     action->program = pat->program;
298
299     // Do the substitutions from the pattern to the actual
300     StringVector::iterator PI = pat->args.begin();
301     StringVector::iterator PE = pat->args.end();
302     while (PI != PE) {
303       if ((*PI)[0] == '%' && PI->length() >2) {
304         bool found = true;
305         switch ((*PI)[1]) {
306           case 'a':
307             if (*PI == "%args%") {
308               if (AdditionalArgs.size() > unsigned(phase))
309                 if (!AdditionalArgs[phase].empty()) {
310                   // Get specific options for each kind of action type
311                   StringVector& addargs = AdditionalArgs[phase];
312                   // Add specific options for each kind of action type
313                   action->args.insert(action->args.end(), addargs.begin(),
314                                       addargs.end());
315                 }
316             } else
317               found = false;
318             break;
319           case 'b':
320             if (*PI == "%bindir%") {
321               std::string tmp(*PI);
322               tmp.replace(0,8,LLVM_BINDIR);
323               action->args.push_back(tmp);
324             } else
325               found = false;
326             break;
327           case 'd':
328             if (*PI == "%defs%") {
329               StringVector::iterator I = Defines.begin();
330               StringVector::iterator E = Defines.end();
331               while (I != E) {
332                 action->args.push_back( std::string("-D") + *I);
333                 ++I;
334               }
335             } else
336               found = false;
337             break;
338           case 'f':
339             if (*PI == "%fOpts%") {
340               if (!fOptions.empty())
341                 action->args.insert(action->args.end(), fOptions.begin(),
342                                     fOptions.end());
343             } else
344               found = false;
345             break;
346           case 'i':
347             if (*PI == "%in%") {
348               action->args.push_back(input.toString());
349             } else if (*PI == "%incls%") {
350               PathVector::iterator I = IncludePaths.begin();
351               PathVector::iterator E = IncludePaths.end();
352               while (I != E) {
353                 action->args.push_back( std::string("-I") + I->toString() );
354                 ++I;
355               }
356             } else
357               found = false;
358             break;
359           case 'l':
360             if ((*PI)[1] == 'l') {
361               std::string tmp(*PI);
362               if (*PI == "%llvmgccdir%")
363                 tmp.replace(0,12,LLVMGCCDIR);
364               else if (*PI == "%llvmgccarch%")
365                 tmp.replace(0,13,LLVMGCCARCH);
366               else if (*PI == "%llvmgcc%")
367                 tmp.replace(0,9,LLVMGCC);
368               else if (*PI == "%llvmgxx%")
369                 tmp.replace(0,9,LLVMGXX);
370               else if (*PI == "%llvmcc1%")
371                 tmp.replace(0,9,LLVMCC1);
372               else if (*PI == "%llvmcc1plus%")
373                 tmp.replace(0,9,LLVMCC1);
374               else
375                 found = false;
376               if (found)
377                 action->args.push_back(tmp);
378             } else if (*PI == "%libs%") {
379               PathVector::iterator I = LibraryPaths.begin();
380               PathVector::iterator E = LibraryPaths.end();
381               while (I != E) {
382                 action->args.push_back( std::string("-L") + I->toString() );
383                 ++I;
384               }
385             } else if (*PI == "%libdir%") {
386               std::string tmp(*PI);
387               tmp.replace(0,8,LLVM_LIBDIR);
388               action->args.push_back(tmp);
389             } else
390               found = false;
391             break;
392           case 'o':
393             if (*PI == "%out%") {
394               action->args.push_back(output.toString());
395             } else if (*PI == "%opt%") {
396               if (!isSet(EMIT_RAW_FLAG)) {
397                 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
398                     !cd->opts[optLevel].empty())
399                   action->args.insert(action->args.end(),
400                                       cd->opts[optLevel].begin(),
401                                       cd->opts[optLevel].end());
402                 else
403                   throw std::string("Optimization options for level ") +
404                         utostr(unsigned(optLevel)) + " were not specified";
405               }
406             } else
407               found = false;
408             break;
409           case 's':
410             if (*PI == "%stats%") {
411               if (isSet(SHOW_STATS_FLAG))
412                 action->args.push_back("-stats");
413             } else
414               found = false;
415             break;
416           case 't':
417             if (*PI == "%target%") {
418               action->args.push_back(std::string("-march=") + machine);
419             } else if (*PI == "%time%") {
420               if (isSet(TIME_PASSES_FLAG))
421                 action->args.push_back("-time-passes");
422             } else
423               found = false;
424             break;
425           case 'v':
426             if (*PI == "%verbose%") {
427               if (isSet(VERBOSE_FLAG))
428                 action->args.push_back("-v");
429             } else
430               found  = false;
431             break;
432           case 'M':
433             if (*PI == "%Mopts%") {
434               if (!MOptions.empty())
435                 action->args.insert(action->args.end(), MOptions.begin(),
436                                     MOptions.end());
437             } else
438               found = false;
439             break;
440           case 'W':
441             if (*PI == "%Wopts%") {
442               for (StringVector::iterator I = WOptions.begin(),
443                    E = WOptions.end(); I != E ; ++I ) {
444                 action->args.push_back(std::string("-W") + *I);
445               }
446             } else
447               found = false;
448             break;
449           default:
450             found = false;
451             break;
452         }
453         if (!found) {
454           // Did it even look like a substitution?
455           if (PI->length()>1 && (*PI)[0] == '%' &&
456               (*PI)[PI->length()-1] == '%') {
457             throw std::string("Invalid substitution token: '") + *PI +
458                   "' for command '" + pat->program.toString() + "'";
459           } else if (!PI->empty()) {
460             // It's not a legal substitution, just pass it through
461             action->args.push_back(*PI);
462           }
463         }
464       } else if (!PI->empty()) {
465         // Its not a substitution, just put it in the action
466         action->args.push_back(*PI);
467       }
468       PI++;
469     }
470
471     // Finally, we're done
472     return action;
473   }
474
475   int DoAction(Action*action, std::string& ErrMsg) {
476     assert(action != 0 && "Invalid Action!");
477     if (isSet(VERBOSE_FLAG))
478       WriteAction(action);
479     if (!isSet(DRY_RUN_FLAG)) {
480       sys::Path progpath = sys::Program::FindProgramByName(
481         action->program.toString());
482       if (progpath.isEmpty())
483         throw std::string("Can't find program '" +
484                           action->program.toString()+"'");
485       else if (progpath.canExecute())
486         action->program = progpath;
487       else
488         throw std::string("Program '"+action->program.toString()+
489                           "' is not executable.");
490
491       // Invoke the program
492       const char** Args = (const char**)
493         alloca(sizeof(const char*)*(action->args.size()+2));
494       Args[0] = action->program.toString().c_str();
495       for (unsigned i = 1; i <= action->args.size(); ++i)
496         Args[i] = action->args[i-1].c_str();
497       Args[action->args.size()+1] = 0;  // null terminate list.
498       if (isSet(TIME_ACTIONS_FLAG)) {
499         Timer timer(action->program.toString());
500         timer.startTimer();
501         int resultCode = 
502           sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg);
503         timer.stopTimer();
504         timer.print(timer,std::cerr);
505         return resultCode;
506       }
507       else
508         return 
509           sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg);
510     }
511     return 0;
512   }
513
514   /// This method tries various variants of a linkage item's file
515   /// name to see if it can find an appropriate file to link with
516   /// in the directories of the LibraryPaths.
517   llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
518                                         bool native = false) {
519     sys::Path fullpath;
520     fullpath.set(link_item);
521     if (fullpath.canRead())
522       return fullpath;
523     for (PathVector::iterator PI = LibraryPaths.begin(),
524          PE = LibraryPaths.end(); PI != PE; ++PI) {
525       fullpath.set(PI->toString());
526       fullpath.appendComponent(link_item);
527       if (fullpath.canRead())
528         return fullpath;
529       if (native) {
530         fullpath.appendSuffix("a");
531       } else {
532         fullpath.appendSuffix("bc");
533         if (fullpath.canRead())
534           return fullpath;
535         fullpath.eraseSuffix();
536         fullpath.appendSuffix("o");
537         if (fullpath.canRead())
538           return fullpath;
539         fullpath = *PI;
540         fullpath.appendComponent(std::string("lib") + link_item);
541         fullpath.appendSuffix("a");
542         if (fullpath.canRead())
543           return fullpath;
544         fullpath.eraseSuffix();
545         fullpath.appendSuffix("so");
546         if (fullpath.canRead())
547           return fullpath;
548       }
549     }
550
551     // Didn't find one.
552     fullpath.clear();
553     return fullpath;
554   }
555
556   /// This method processes a linkage item. The item could be a
557   /// Bytecode file needing translation to native code and that is
558   /// dependent on other bytecode libraries, or a native code
559   /// library that should just be linked into the program.
560   bool ProcessLinkageItem(const llvm::sys::Path& link_item,
561                           SetVector<sys::Path>& set,
562                           std::string& err) {
563     // First, see if the unadorned file name is not readable. If so,
564     // we must track down the file in the lib search path.
565     sys::Path fullpath;
566     if (!link_item.canRead()) {
567       // look for the library using the -L arguments specified
568       // on the command line.
569       fullpath = GetPathForLinkageItem(link_item.toString());
570
571       // If we didn't find the file in any of the library search paths
572       // we have to bail. No where else to look.
573       if (fullpath.isEmpty()) {
574         err =
575           std::string("Can't find linkage item '") + link_item.toString() + "'";
576         return false;
577       }
578     } else {
579       fullpath = link_item;
580     }
581
582     // If we got here fullpath is the path to the file, and its readable.
583     set.insert(fullpath);
584
585     // If its an LLVM bytecode file ...
586     if (fullpath.isBytecodeFile()) {
587       // Process the dependent libraries recursively
588       Module::LibraryListType modlibs;
589       if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,
590                                         Compressor::decompressToNewBuffer,
591                                         &err)) {
592         // Traverse the dependent libraries list
593         Module::lib_iterator LI = modlibs.begin();
594         Module::lib_iterator LE = modlibs.end();
595         while ( LI != LE ) {
596           if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
597             if (err.empty()) {
598               err = std::string("Library '") + *LI +
599                     "' is not valid for linking but is required by file '" +
600                     fullpath.toString() + "'";
601             } else {
602               err += " which is required by file '" + fullpath.toString() + "'";
603             }
604             return false;
605           }
606           ++LI;
607         }
608       } else if (err.empty()) {
609         err = std::string(
610           "The dependent libraries could not be extracted from '") +
611           fullpath.toString();
612         return false;
613       } else 
614         return false;
615     }
616     return true;
617   }
618
619 /// @}
620 /// @name Methods
621 /// @{
622 public:
623   virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
624     try {
625       // Echo the configuration of options if we're running verbose
626       if (isSet(DEBUG_FLAG)) {
627         std::cerr << "Compiler Driver Options:\n";
628         std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
629         std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
630         std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
631         std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
632         std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
633         std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
634         std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
635         std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
636         std::cerr << "OutputMachine = " << machine << "\n";
637         InputList::const_iterator I = InpList.begin();
638         while ( I != InpList.end() ) {
639           std::cerr << "Input: " << I->first << "(" << I->second
640                     << ")\n";
641           ++I;
642         }
643         std::cerr << "Output: " << Output << "\n";
644       }
645
646       // If there's no input, we're done.
647       if (InpList.empty())
648         throw std::string("Nothing to compile.");
649
650       // If they are asking for linking and didn't provide an output
651       // file then its an error (no way for us to "make up" a meaningful
652       // file name based on the various linker input files).
653       if (finalPhase == LINKING && Output.isEmpty())
654         throw std::string(
655           "An output file name must be specified for linker output");
656
657       // If they are not asking for linking, provided an output file and
658       // there is more than one input file, its an error
659       if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
660         throw std::string("An output file name cannot be specified ") +
661           "with more than one input file name when not linking";
662
663       // This vector holds all the resulting actions of the following loop.
664       std::vector<Action*> actions;
665
666       /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
667       // for each input item
668       SetVector<sys::Path> LinkageItems;
669       StringVector LibFiles;
670       InputList::const_iterator I = InpList.begin();
671       for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
672            I != E; ++I ) {
673         // Get the suffix of the file name
674         const std::string& ftype = I->second;
675
676         // If its a library, bytecode file, or object file, save
677         // it for linking below and short circuit the
678         // pre-processing/translation/assembly phases
679         if (ftype.empty() ||  ftype == "o" || ftype == "bc" || ftype=="a") {
680           // We shouldn't get any of these types of files unless we're
681           // later going to link. Enforce this limit now.
682           if (finalPhase != LINKING) {
683             throw std::string(
684               "Pre-compiled objects found but linking not requested");
685           }
686           if (ftype.empty())
687             LibFiles.push_back(I->first.toString());
688           else
689             LinkageItems.insert(I->first);
690           continue; // short circuit remainder of loop
691         }
692
693         // At this point, we know its something we need to translate
694         // and/or optimize. See if we can get the configuration data
695         // for this kind of file.
696         ConfigData* cd = cdp->ProvideConfigData(I->second);
697         if (cd == 0)
698           throw std::string("Files of type '") + I->second +
699                 "' are not recognized.";
700         if (isSet(DEBUG_FLAG))
701           DumpConfigData(cd,I->second);
702
703         // Add the config data's library paths to the end of the list
704         for (StringVector::iterator LPI = cd->libpaths.begin(),
705              LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
706           LibraryPaths.push_back(sys::Path(*LPI));
707         }
708
709         // Initialize the input and output files
710         sys::Path InFile(I->first);
711         sys::Path OutFile(I->first.getBasename());
712
713         // PRE-PROCESSING PHASE
714         Action& action = cd->PreProcessor;
715
716         // Get the preprocessing action, if needed, or error if appropriate
717         if (!action.program.isEmpty()) {
718           if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
719             if (finalPhase == PREPROCESSING) {
720               if (Output.isEmpty()) {
721                 OutFile.appendSuffix("E");
722                 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
723               } else {
724                 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
725               }
726             } else {
727               sys::Path TempFile(
728                   MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
729               if (TempFile.isEmpty())
730                 return 1;
731               actions.push_back(GetAction(cd,InFile,TempFile,
732                 PREPROCESSING));
733               InFile = TempFile;
734             }
735           }
736         } else if (finalPhase == PREPROCESSING) {
737           throw cd->langName + " does not support pre-processing";
738         } else if (action.isSet(REQUIRED_FLAG)) {
739           throw std::string("Don't know how to pre-process ") +
740                 cd->langName + " files";
741         }
742
743         // Short-circuit remaining actions if all they want is
744         // pre-processing
745         if (finalPhase == PREPROCESSING) { continue; };
746
747         /// TRANSLATION PHASE
748         action = cd->Translator;
749
750         // Get the translation action, if needed, or error if appropriate
751         if (!action.program.isEmpty()) {
752           if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
753             if (finalPhase == TRANSLATION) {
754               if (Output.isEmpty()) {
755                 OutFile.appendSuffix("o");
756                 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
757               } else {
758                 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
759               }
760             } else {
761               sys::Path TempFile(
762                   MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
763               if (TempFile.isEmpty())
764                 return 1;
765               actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
766               InFile = TempFile;
767             }
768
769             // ll -> bc Helper
770             if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
771               /// The output of the translator is an LLVM Assembly program
772               /// We need to translate it to bytecode
773               Action* action = new Action();
774               action->program.set("llvm-as");
775               action->args.push_back(InFile.toString());
776               action->args.push_back("-o");
777               InFile.appendSuffix("bc");
778               action->args.push_back(InFile.toString());
779               actions.push_back(action);
780             }
781           }
782         } else if (finalPhase == TRANSLATION) {
783           throw cd->langName + " does not support translation";
784         } else if (action.isSet(REQUIRED_FLAG)) {
785           throw std::string("Don't know how to translate ") +
786                 cd->langName + " files";
787         }
788
789         // Short-circuit remaining actions if all they want is translation
790         if (finalPhase == TRANSLATION) { continue; }
791
792         /// OPTIMIZATION PHASE
793         action = cd->Optimizer;
794
795         // Get the optimization action, if needed, or error if appropriate
796         if (!isSet(EMIT_RAW_FLAG)) {
797           if (!action.program.isEmpty()) {
798             if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
799               if (finalPhase == OPTIMIZATION) {
800                 if (Output.isEmpty()) {
801                   OutFile.appendSuffix("o");
802                   actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
803                 } else {
804                   actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
805                 }
806               } else {
807                 sys::Path TempFile(
808                   MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
809                 if (TempFile.isEmpty())
810                   return 1;
811                 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
812                 InFile = TempFile;
813               }
814               // ll -> bc Helper
815               if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
816                 /// The output of the optimizer is an LLVM Assembly program
817                 /// We need to translate it to bytecode with llvm-as
818                 Action* action = new Action();
819                 action->program.set("llvm-as");
820                 action->args.push_back(InFile.toString());
821                 action->args.push_back("-f");
822                 action->args.push_back("-o");
823                 InFile.appendSuffix("bc");
824                 action->args.push_back(InFile.toString());
825                 actions.push_back(action);
826               }
827             }
828           } else if (finalPhase == OPTIMIZATION) {
829             throw cd->langName + " does not support optimization";
830           } else if (action.isSet(REQUIRED_FLAG)) {
831             throw std::string("Don't know how to optimize ") +
832                 cd->langName + " files";
833           }
834         }
835
836         // Short-circuit remaining actions if all they want is optimization
837         if (finalPhase == OPTIMIZATION) { continue; }
838
839         /// ASSEMBLY PHASE
840         action = cd->Assembler;
841
842         if (finalPhase == ASSEMBLY) {
843
844           // Build either a native compilation action or a disassembly action
845           Action* action = new Action();
846           if (isSet(EMIT_NATIVE_FLAG)) {
847             // Use llc to get the native assembly file
848             action->program.set("llc");
849             action->args.push_back(InFile.toString());
850             action->args.push_back("-f");
851             action->args.push_back("-o");
852             if (Output.isEmpty()) {
853               OutFile.appendSuffix("o");
854               action->args.push_back(OutFile.toString());
855             } else {
856               action->args.push_back(Output.toString());
857             }
858             actions.push_back(action);
859           } else {
860             // Just convert back to llvm assembly with llvm-dis
861             action->program.set("llvm-dis");
862             action->args.push_back(InFile.toString());
863             action->args.push_back("-f");
864             action->args.push_back("-o");
865             if (Output.isEmpty()) {
866               OutFile.appendSuffix("ll");
867               action->args.push_back(OutFile.toString());
868             } else {
869               action->args.push_back(Output.toString());
870             }
871           }
872
873           // Put the action on the list
874           actions.push_back(action);
875
876           // Short circuit the rest of the loop, we don't want to link
877           continue;
878         }
879
880         // Register the result of the actions as a link candidate
881         LinkageItems.insert(InFile);
882
883       } // end while loop over each input file
884
885       /// RUN THE COMPILATION ACTIONS
886       std::vector<Action*>::iterator AI = actions.begin();
887       std::vector<Action*>::iterator AE = actions.end();
888       while (AI != AE) {
889         int ActionResult = DoAction(*AI, ErrMsg);
890         if (ActionResult != 0)
891           return ActionResult;
892         AI++;
893       }
894
895       /// LINKING PHASE
896       if (finalPhase == LINKING) {
897
898         // Insert the platform-specific system libraries to the path list
899         std::vector<sys::Path> SysLibs;
900         sys::Path::GetSystemLibraryPaths(SysLibs);
901         LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
902
903         // Set up the linking action with llvm-ld
904         Action* link = new Action();
905         link->program.set("llvm-ld");
906
907         // Add in the optimization level requested
908         switch (optLevel) {
909           case OPT_FAST_COMPILE:
910             link->args.push_back("-O1");
911             break;
912           case OPT_SIMPLE:
913             link->args.push_back("-O2");
914             break;
915           case OPT_AGGRESSIVE:
916             link->args.push_back("-O3");
917             break;
918           case OPT_LINK_TIME:
919             link->args.push_back("-O4");
920             break;
921           case OPT_AGGRESSIVE_LINK_TIME:
922             link->args.push_back("-O5");
923             break;
924           case OPT_NONE:
925             break;
926         }
927
928         // Add in all the linkage items we generated. This includes the
929         // output from the translation/optimization phases as well as any
930         // -l arguments specified.
931         for (PathVector::const_iterator I=LinkageItems.begin(),
932              E=LinkageItems.end(); I != E; ++I )
933           link->args.push_back(I->toString());
934
935         // Add in all the libraries we found.
936         for (StringVector::const_iterator I=LibFiles.begin(),
937              E=LibFiles.end(); I != E; ++I )
938           link->args.push_back(std::string("-l")+*I);
939
940         // Add in all the library paths to the command line
941         for (PathVector::const_iterator I=LibraryPaths.begin(),
942              E=LibraryPaths.end(); I != E; ++I)
943           link->args.push_back( std::string("-L") + I->toString());
944
945         // Add in the additional linker arguments requested
946         for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
947              E=AdditionalArgs[LINKING].end(); I != E; ++I)
948           link->args.push_back( *I );
949
950         // Add in other optional flags
951         if (isSet(EMIT_NATIVE_FLAG))
952           link->args.push_back("-native");
953         if (isSet(VERBOSE_FLAG))
954           link->args.push_back("-v");
955         if (isSet(TIME_PASSES_FLAG))
956           link->args.push_back("-time-passes");
957         if (isSet(SHOW_STATS_FLAG))
958           link->args.push_back("-stats");
959         if (isSet(STRIP_OUTPUT_FLAG))
960           link->args.push_back("-s");
961         if (isSet(DEBUG_FLAG)) {
962           link->args.push_back("-debug");
963           link->args.push_back("-debug-pass=Details");
964         }
965
966         // Add in mandatory flags
967         link->args.push_back("-o");
968         link->args.push_back(Output.toString());
969
970         // Execute the link
971         int ActionResult = DoAction(link, ErrMsg);
972         if (ActionResult != 0)
973           return ActionResult;
974       }
975     } catch (std::string& msg) {
976       cleanup();
977       throw;
978     } catch (...) {
979       cleanup();
980       throw std::string("Unspecified error");
981     }
982     cleanup();
983     return 0;
984   }
985
986 /// @}
987 /// @name Data
988 /// @{
989 private:
990   ConfigDataProvider* cdp;      ///< Where we get configuration data from
991   Phases finalPhase;            ///< The final phase of compilation
992   OptimizationLevels optLevel;  ///< The optimization level to apply
993   unsigned Flags;               ///< The driver flags
994   std::string machine;          ///< Target machine name
995   PathVector LibraryPaths;      ///< -L options
996   PathVector IncludePaths;      ///< -I options
997   PathVector ToolPaths;         ///< -B options
998   StringVector Defines;         ///< -D options
999   sys::Path TempDir;            ///< Name of the temporary directory.
1000   StringTable AdditionalArgs;   ///< The -Txyz options
1001   StringVector fOptions;        ///< -f options
1002   StringVector MOptions;        ///< -M options
1003   StringVector WOptions;        ///< -W options
1004
1005 /// @}
1006 };
1007 }
1008
1009 CompilerDriver::~CompilerDriver() {
1010 }
1011
1012 CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
1013
1014 CompilerDriver*
1015 CompilerDriver::Get(ConfigDataProvider& CDP) {
1016   return new CompilerDriverImpl(CDP);
1017 }
1018
1019 CompilerDriver::ConfigData::ConfigData()
1020   : langName()
1021   , PreProcessor()
1022   , Translator()
1023   , Optimizer()
1024   , Assembler()
1025   , Linker()
1026 {
1027   StringVector emptyVec;
1028   for (unsigned i = 0; i < NUM_PHASES; ++i)
1029     opts.push_back(emptyVec);
1030 }