1 //===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
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.
9 //===----------------------------------------------------------------------===//
11 // This file implements the bulk of the LLVM Compiler Driver (llvmc).
13 //===------------------------------------------------------------------------===
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"
29 void WriteAction(CompilerDriver::Action* action ) {
30 std::cerr << action->program.c_str();
31 std::vector<std::string>::iterator I = action->args.begin();
32 while (I != action->args.end()) {
33 std::cerr << " " + *I;
39 void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
41 std::vector<std::string>::iterator I = action->args.begin();
42 while (I != action->args.end()) {
43 std::cerr << " " + *I;
47 std::cerr << "flags = " << action->flags << "\n";
50 void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
51 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
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);
65 /// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
66 /// which should reduce the volume of code and make compilation
67 /// faster. This is also safe on any llvm module.
68 static const char* DefaultFastCompileOptimizations[] = {
69 "-simplifycfg", "-mem2reg", "-instcombine"
72 class CompilerDriverImpl : public CompilerDriver {
73 /// @name Constructors
76 CompilerDriverImpl(ConfigDataProvider& confDatProv )
79 , optLevel(OPT_FAST_COMPILE)
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);
94 virtual ~CompilerDriverImpl() {
101 AdditionalArgs.clear();
111 virtual void setFinalPhase( Phases phase ) {
115 virtual void setOptimization( OptimizationLevels level ) {
119 virtual void setDriverFlags( unsigned flags ) {
120 Flags = flags & DRIVER_FLAGS_MASK;
123 virtual void setOutputMachine( const std::string& machineName ) {
124 machine = machineName;
127 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
128 assert(phase <= LINKING && phase >= PREPROCESSING);
129 AdditionalArgs[phase] = opts;
132 virtual void setIncludePaths(const StringVector& paths) {
133 StringVector::const_iterator I = paths.begin();
134 StringVector::const_iterator E = paths.end();
137 tmp.setDirectory(*I);
138 IncludePaths.push_back(tmp);
143 virtual void setSymbolDefines(const StringVector& defs) {
147 virtual void setLibraryPaths(const StringVector& paths) {
148 StringVector::const_iterator I = paths.begin();
149 StringVector::const_iterator E = paths.end();
152 tmp.setDirectory(*I);
153 LibraryPaths.push_back(tmp);
158 virtual void addLibraryPath( const sys::Path& libPath ) {
159 LibraryPaths.push_back(libPath);
162 virtual void addToolPath( const sys::Path& toolPath ) {
163 ToolPaths.push_back(toolPath);
166 virtual void setfPassThrough(const StringVector& fOpts) {
170 /// @brief Set the list of -M options to be passed through
171 virtual void setMPassThrough(const StringVector& MOpts) {
175 /// @brief Set the list of -W options to be passed through
176 virtual void setWPassThrough(const StringVector& WOpts) {
183 bool isSet(DriverFlags flag) {
184 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
188 if (!isSet(KEEP_TEMPS_FLAG)) {
189 if (TempDir.isDirectory() && TempDir.writable())
190 TempDir.destroyDirectory(/*remove_contents=*/true);
192 std::cout << "Temporary files are in " << TempDir.get() << "\n";
196 sys::Path MakeTempFile(const std::string& basename,
197 const std::string& suffix ) {
198 sys::Path result(TempDir);
199 if (!result.appendFile(basename))
200 throw basename + ": can't use this file name";
201 if (!result.appendSuffix(suffix))
202 throw suffix + ": can't use this file suffix";
206 Action* GetAction(ConfigData* cd,
207 const sys::Path& input,
208 const sys::Path& output,
211 Action* pat = 0; ///< The pattern/template for the action
212 Action* action = new Action; ///< The actual action to execute
214 // Get the action pattern
216 case PREPROCESSING: pat = &cd->PreProcessor; break;
217 case TRANSLATION: pat = &cd->Translator; break;
218 case OPTIMIZATION: pat = &cd->Optimizer; break;
219 case ASSEMBLY: pat = &cd->Assembler; break;
220 case LINKING: pat = &cd->Linker; break;
222 assert(!"Invalid driver phase!");
225 assert(pat != 0 && "Invalid command pattern");
227 // Copy over some pattern things that don't need to change
228 action->program = pat->program;
229 action->flags = pat->flags;
231 // Do the substitutions from the pattern to the actual
232 StringVector::iterator PI = pat->args.begin();
233 StringVector::iterator PE = pat->args.end();
235 if ((*PI)[0] == '%' && PI->length() >2) {
239 if (*PI == "%args%") {
240 if (AdditionalArgs.size() > unsigned(phase))
241 if (!AdditionalArgs[phase].empty()) {
242 // Get specific options for each kind of action type
243 StringVector& addargs = AdditionalArgs[phase];
244 // Add specific options for each kind of action type
245 action->args.insert(action->args.end(), addargs.begin(),
252 if (*PI == "%defs%") {
253 StringVector::iterator I = Defines.begin();
254 StringVector::iterator E = Defines.end();
256 action->args.push_back( std::string("-D") + *I);
263 if (*PI == "%fOpts%") {
264 action->args.insert(action->args.end(), fOptions.begin(),
271 action->args.push_back(input.get());
272 } else if (*PI == "%incls%") {
273 PathVector::iterator I = IncludePaths.begin();
274 PathVector::iterator E = IncludePaths.end();
276 action->args.push_back( std::string("-I") + I->get() );
283 if (*PI == "%libs%") {
284 PathVector::iterator I = LibraryPaths.begin();
285 PathVector::iterator E = LibraryPaths.end();
287 action->args.push_back( std::string("-L") + I->get() );
294 if (*PI == "%out%") {
295 action->args.push_back(output.get());
296 } else if (*PI == "%opt%") {
297 if (!isSet(EMIT_RAW_FLAG)) {
298 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
299 !cd->opts[optLevel].empty())
300 action->args.insert(action->args.end(),
301 cd->opts[optLevel].begin(),
302 cd->opts[optLevel].end());
304 throw std::string("Optimization options for level ") +
305 utostr(unsigned(optLevel)) + " were not specified";
311 if (*PI == "%stats%") {
312 if (isSet(SHOW_STATS_FLAG))
313 action->args.push_back("-stats");
318 if (*PI == "%target%") {
319 action->args.push_back(std::string("-march=") + machine);
320 } else if (*PI == "%time%") {
321 if (isSet(TIME_PASSES_FLAG))
322 action->args.push_back("-time-passes");
327 if (*PI == "%verbose%") {
328 if (isSet(VERBOSE_FLAG))
329 action->args.push_back("-v");
334 if (*PI == "%Mopts") {
335 action->args.insert(action->args.end(), MOptions.begin(),
341 if (*PI == "%Wopts") {
342 action->args.insert(action->args.end(), WOptions.begin(),
352 // Did it even look like a substitution?
353 if (PI->length()>1 && (*PI)[0] == '%' &&
354 (*PI)[PI->length()-1] == '%') {
355 throw std::string("Invalid substitution token: '") + *PI +
356 "' for command '" + pat->program.get() + "'";
358 // It's not a legal substitution, just pass it through
359 action->args.push_back(*PI);
363 // Its not a substitution, just put it in the action
364 action->args.push_back(*PI);
369 // Finally, we're done
373 bool DoAction(Action*action) {
374 assert(action != 0 && "Invalid Action!");
375 if (isSet(VERBOSE_FLAG))
377 if (!isSet(DRY_RUN_FLAG)) {
378 sys::Path progpath = sys::Program::FindProgramByName(
379 action->program.get());
380 if (progpath.isEmpty())
381 throw std::string("Can't find program '"+progpath.get()+"'");
382 else if (progpath.executable())
383 action->program = progpath;
385 throw std::string("Program '"+progpath.get()+"' is not executable.");
387 // Invoke the program
388 if (isSet(TIME_ACTIONS_FLAG)) {
389 Timer timer(action->program.get());
392 sys::Program::ExecuteAndWait(action->program,action->args);
394 timer.print(timer,std::cerr);
395 return resultCode == 0;
399 sys::Program::ExecuteAndWait(action->program, action->args);
404 /// This method tries various variants of a linkage item's file
405 /// name to see if it can find an appropriate file to link with
406 /// in the directory specified.
407 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
408 const sys::Path& dir,
409 bool native = false) {
410 sys::Path fullpath(dir);
411 fullpath.appendFile(link_item);
413 fullpath.appendSuffix("a");
415 fullpath.appendSuffix("bc");
416 if (fullpath.readable())
418 fullpath.elideSuffix();
419 fullpath.appendSuffix("o");
420 if (fullpath.readable())
423 fullpath.appendFile(std::string("lib") + link_item);
424 fullpath.appendSuffix("a");
425 if (fullpath.readable())
427 fullpath.elideSuffix();
428 fullpath.appendSuffix("so");
429 if (fullpath.readable())
438 /// This method processes a linkage item. The item could be a
439 /// Bytecode file needing translation to native code and that is
440 /// dependent on other bytecode libraries, or a native code
441 /// library that should just be linked into the program.
442 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
443 SetVector<sys::Path>& set,
445 // First, see if the unadorned file name is not readable. If so,
446 // we must track down the file in the lib search path.
448 if (!link_item.readable()) {
449 // First, look for the library using the -L arguments specified
450 // on the command line.
451 PathVector::iterator PI = LibraryPaths.begin();
452 PathVector::iterator PE = LibraryPaths.end();
453 while (PI != PE && fullpath.isEmpty()) {
454 fullpath = GetPathForLinkageItem(link_item.get(),*PI);
458 // If we didn't find the file in any of the library search paths
459 // so we have to bail. No where else to look.
460 if (fullpath.isEmpty()) {
462 std::string("Can't find linkage item '") + link_item.get() + "'";
466 fullpath = link_item;
469 // If we got here fullpath is the path to the file, and its readable.
470 set.insert(fullpath);
472 // If its an LLVM bytecode file ...
473 if (fullpath.isBytecodeFile()) {
474 // Process the dependent libraries recursively
475 Module::LibraryListType modlibs;
476 if (GetBytecodeDependentLibraries(fullpath.get(),modlibs)) {
477 // Traverse the dependent libraries list
478 Module::lib_iterator LI = modlibs.begin();
479 Module::lib_iterator LE = modlibs.end();
481 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
483 err = std::string("Library '") + *LI +
484 "' is not valid for linking but is required by file '" +
485 fullpath.get() + "'";
487 err += " which is required by file '" + fullpath.get() + "'";
493 } else if (err.empty()) {
495 "The dependent libraries could not be extracted from '") +
507 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
509 // Echo the configuration of options if we're running verbose
510 if (isSet(DEBUG_FLAG)) {
511 std::cerr << "Compiler Driver Options:\n";
512 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
513 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
514 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
515 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
516 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
517 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
518 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
519 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
520 std::cerr << "OutputMachine = " << machine << "\n";
521 InputList::const_iterator I = InpList.begin();
522 while ( I != InpList.end() ) {
523 std::cerr << "Input: " << I->first.get() << "(" << I->second
527 std::cerr << "Output: " << Output.get() << "\n";
530 // If there's no input, we're done.
532 throw std::string("Nothing to compile.");
534 // If they are asking for linking and didn't provide an output
535 // file then its an error (no way for us to "make up" a meaningful
536 // file name based on the various linker input files).
537 if (finalPhase == LINKING && Output.isEmpty())
539 "An output file name must be specified for linker output");
541 // If they are not asking for linking, provided an output file and
542 // there is more than one input file, its an error
543 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
544 throw std::string("An output file name cannot be specified ") +
545 "with more than one input file name when not linking";
547 // This vector holds all the resulting actions of the following loop.
548 std::vector<Action*> actions;
550 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
551 // for each input item
552 SetVector<sys::Path> LinkageItems;
553 std::vector<std::string> LibFiles;
554 InputList::const_iterator I = InpList.begin();
555 while ( I != InpList.end() ) {
556 // Get the suffix of the file name
557 const std::string& ftype = I->second;
559 // If its a library, bytecode file, or object file, save
560 // it for linking below and short circuit the
561 // pre-processing/translation/assembly phases
562 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
563 // We shouldn't get any of these types of files unless we're
564 // later going to link. Enforce this limit now.
565 if (finalPhase != LINKING) {
567 "Pre-compiled objects found but linking not requested");
570 LibFiles.push_back(I->first.get());
572 LinkageItems.insert(I->first);
573 ++I; continue; // short circuit remainder of loop
576 // At this point, we know its something we need to translate
577 // and/or optimize. See if we can get the configuration data
578 // for this kind of file.
579 ConfigData* cd = cdp->ProvideConfigData(I->second);
581 throw std::string("Files of type '") + I->second +
582 "' are not recognized.";
583 if (isSet(DEBUG_FLAG))
584 DumpConfigData(cd,I->second);
586 // Initialize the input and output files
587 sys::Path InFile(I->first);
588 sys::Path OutFile(I->first.getBasename());
590 // PRE-PROCESSING PHASE
591 Action& action = cd->PreProcessor;
593 // Get the preprocessing action, if needed, or error if appropriate
594 if (!action.program.isEmpty()) {
595 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
596 if (finalPhase == PREPROCESSING) {
597 OutFile.appendSuffix("E");
598 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
600 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
601 actions.push_back(GetAction(cd,InFile,TempFile,
606 } else if (finalPhase == PREPROCESSING) {
607 throw cd->langName + " does not support pre-processing";
608 } else if (action.isSet(REQUIRED_FLAG)) {
609 throw std::string("Don't know how to pre-process ") +
610 cd->langName + " files";
613 // Short-circuit remaining actions if all they want is
615 if (finalPhase == PREPROCESSING) { ++I; continue; };
617 /// TRANSLATION PHASE
618 action = cd->Translator;
620 // Get the translation action, if needed, or error if appropriate
621 if (!action.program.isEmpty()) {
622 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
623 if (finalPhase == TRANSLATION) {
624 OutFile.appendSuffix("o");
625 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
627 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
628 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
633 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
634 /// The output of the translator is an LLVM Assembly program
635 /// We need to translate it to bytecode
636 Action* action = new Action();
637 action->program.setFile("llvm-as");
638 action->args.push_back(InFile.get());
639 action->args.push_back("-o");
640 InFile.appendSuffix("bc");
641 action->args.push_back(InFile.get());
642 actions.push_back(action);
645 } else if (finalPhase == TRANSLATION) {
646 throw cd->langName + " does not support translation";
647 } else if (action.isSet(REQUIRED_FLAG)) {
648 throw std::string("Don't know how to translate ") +
649 cd->langName + " files";
652 // Short-circuit remaining actions if all they want is translation
653 if (finalPhase == TRANSLATION) { ++I; continue; }
655 /// OPTIMIZATION PHASE
656 action = cd->Optimizer;
658 // Get the optimization action, if needed, or error if appropriate
659 if (!isSet(EMIT_RAW_FLAG)) {
660 if (!action.program.isEmpty()) {
661 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
662 if (finalPhase == OPTIMIZATION) {
663 OutFile.appendSuffix("o");
664 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
666 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
667 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
671 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
672 /// The output of the optimizer is an LLVM Assembly program
673 /// We need to translate it to bytecode with llvm-as
674 Action* action = new Action();
675 action->program.setFile("llvm-as");
676 action->args.push_back(InFile.get());
677 action->args.push_back("-f");
678 action->args.push_back("-o");
679 InFile.appendSuffix("bc");
680 action->args.push_back(InFile.get());
681 actions.push_back(action);
684 } else if (finalPhase == OPTIMIZATION) {
685 throw cd->langName + " does not support optimization";
686 } else if (action.isSet(REQUIRED_FLAG)) {
687 throw std::string("Don't know how to optimize ") +
688 cd->langName + " files";
692 // Short-circuit remaining actions if all they want is optimization
693 if (finalPhase == OPTIMIZATION) { ++I; continue; }
696 action = cd->Assembler;
698 if (finalPhase == ASSEMBLY) {
699 if (isSet(EMIT_NATIVE_FLAG)) {
700 // Use llc to get the native assembly file
701 Action* action = new Action();
702 action->program.setFile("llc");
703 action->args.push_back(InFile.get());
704 action->args.push_back("-f");
705 action->args.push_back("-o");
706 OutFile.appendSuffix("s");
707 action->args.push_back(OutFile.get());
709 // Just convert back to llvm assembly with llvm-dis
710 Action* action = new Action();
711 action->program.setFile("llvm-dis");
712 action->args.push_back(InFile.get());
713 action->args.push_back("-f");
714 action->args.push_back("-o");
715 OutFile.appendSuffix("ll");
716 action->args.push_back(OutFile.get());
717 actions.push_back(action);
720 // Short circuit the rest of the loop, we don't want to link
725 // Register the result of the actions as a link candidate
726 LinkageItems.insert(InFile);
728 // Go to next file to be processed
730 } // end while loop over each input file
732 /// RUN THE COMPILATION ACTIONS
733 std::vector<Action*>::iterator AI = actions.begin();
734 std::vector<Action*>::iterator AE = actions.end();
737 throw std::string("Action failed");
742 if (finalPhase == LINKING) {
744 // Insert the platform-specific system libraries to the path list
745 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath1());
746 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath2());
748 // Set up the linking action with llvm-ld
749 Action* link = new Action();
750 link->program.setFile("llvm-ld");
752 // Add in the optimization level requested
754 case OPT_FAST_COMPILE:
755 link->args.push_back("-O1");
758 link->args.push_back("-O2");
761 link->args.push_back("-O3");
764 link->args.push_back("-O4");
766 case OPT_AGGRESSIVE_LINK_TIME:
767 link->args.push_back("-O5");
773 // Add in all the linkage items we generated. This includes the
774 // output from the translation/optimization phases as well as any
775 // -l arguments specified.
776 for (PathVector::const_iterator I=LinkageItems.begin(),
777 E=LinkageItems.end(); I != E; ++I )
778 link->args.push_back(I->get());
780 // Add in all the libraries we found.
781 for (std::vector<std::string>::const_iterator I=LibFiles.begin(),
782 E=LibFiles.end(); I != E; ++I )
783 link->args.push_back(std::string("-l")+*I);
785 // Add in all the library paths to the command line
786 for (PathVector::const_iterator I=LibraryPaths.begin(),
787 E=LibraryPaths.end(); I != E; ++I)
788 link->args.push_back( std::string("-L") + I->get());
790 // Add in the additional linker arguments requested
791 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
792 E=AdditionalArgs[LINKING].end(); I != E; ++I)
793 link->args.push_back( *I );
795 // Add in other optional flags
796 if (isSet(EMIT_NATIVE_FLAG))
797 link->args.push_back("-native");
798 if (isSet(VERBOSE_FLAG))
799 link->args.push_back("-v");
800 if (isSet(TIME_PASSES_FLAG))
801 link->args.push_back("-time-passes");
802 if (isSet(SHOW_STATS_FLAG))
803 link->args.push_back("-stats");
804 if (isSet(STRIP_OUTPUT_FLAG))
805 link->args.push_back("-s");
806 if (isSet(DEBUG_FLAG)) {
807 link->args.push_back("-debug");
808 link->args.push_back("-debug-pass=Details");
811 // Add in mandatory flags
812 link->args.push_back("-o");
813 link->args.push_back(Output.get());
817 throw std::string("Action failed");
819 } catch (std::string& msg) {
824 throw std::string("Unspecified error");
834 ConfigDataProvider* cdp; ///< Where we get configuration data from
835 Phases finalPhase; ///< The final phase of compilation
836 OptimizationLevels optLevel; ///< The optimization level to apply
837 unsigned Flags; ///< The driver flags
838 std::string machine; ///< Target machine name
839 PathVector LibraryPaths; ///< -L options
840 PathVector IncludePaths; ///< -I options
841 PathVector ToolPaths; ///< -B options
842 StringVector Defines; ///< -D options
843 sys::Path TempDir; ///< Name of the temporary directory.
844 StringTable AdditionalArgs; ///< The -Txyz options
845 StringVector fOptions; ///< -f options
846 StringVector MOptions; ///< -M options
847 StringVector WOptions; ///< -W options
853 CompilerDriver::~CompilerDriver() {
857 CompilerDriver::Get(ConfigDataProvider& CDP) {
858 return new CompilerDriverImpl(CDP);
861 CompilerDriver::ConfigData::ConfigData()
869 StringVector emptyVec;
870 for (unsigned i = 0; i < NUM_PHASES; ++i)
871 opts.push_back(emptyVec);
874 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab