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.set_directory(*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.set_directory(*I);
153 LibraryPaths.push_back(tmp);
158 virtual void addLibraryPath( const sys::Path& libPath ) {
159 LibraryPaths.push_back(libPath);
162 virtual void setfPassThrough(const StringVector& fOpts) {
166 /// @brief Set the list of -M options to be passed through
167 virtual void setMPassThrough(const StringVector& MOpts) {
171 /// @brief Set the list of -W options to be passed through
172 virtual void setWPassThrough(const StringVector& WOpts) {
179 bool isSet(DriverFlags flag) {
180 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
184 if (!isSet(KEEP_TEMPS_FLAG)) {
185 if (TempDir.is_directory() && TempDir.writable())
186 TempDir.destroy_directory(/*remove_contents=*/true);
188 std::cout << "Temporary files are in " << TempDir.get() << "\n";
192 sys::Path MakeTempFile(const std::string& basename,
193 const std::string& suffix ) {
194 sys::Path result(TempDir);
195 if (!result.append_file(basename))
196 throw basename + ": can't use this file name";
197 if (!result.append_suffix(suffix))
198 throw suffix + ": can't use this file suffix";
202 Action* GetAction(ConfigData* cd,
203 const sys::Path& input,
204 const sys::Path& output,
207 Action* pat = 0; ///< The pattern/template for the action
208 Action* action = new Action; ///< The actual action to execute
210 // Get the action pattern
212 case PREPROCESSING: pat = &cd->PreProcessor; break;
213 case TRANSLATION: pat = &cd->Translator; break;
214 case OPTIMIZATION: pat = &cd->Optimizer; break;
215 case ASSEMBLY: pat = &cd->Assembler; break;
216 case LINKING: pat = &cd->Linker; break;
218 assert(!"Invalid driver phase!");
221 assert(pat != 0 && "Invalid command pattern");
223 // Copy over some pattern things that don't need to change
224 action->program = pat->program;
225 action->flags = pat->flags;
227 // Do the substitutions from the pattern to the actual
228 StringVector::iterator PI = pat->args.begin();
229 StringVector::iterator PE = pat->args.end();
231 if ((*PI)[0] == '%' && PI->length() >2) {
235 if (*PI == "%args%") {
236 if (AdditionalArgs.size() > unsigned(phase))
237 if (!AdditionalArgs[phase].empty()) {
238 // Get specific options for each kind of action type
239 StringVector& addargs = AdditionalArgs[phase];
240 // Add specific options for each kind of action type
241 action->args.insert(action->args.end(), addargs.begin(),
248 if (*PI == "%defs%") {
249 StringVector::iterator I = Defines.begin();
250 StringVector::iterator E = Defines.end();
252 action->args.push_back( std::string("-D") + *I);
259 if (*PI == "%fOpts%") {
260 action->args.insert(action->args.end(), fOptions.begin(),
267 action->args.push_back(input.get());
268 } else if (*PI == "%incls%") {
269 PathVector::iterator I = IncludePaths.begin();
270 PathVector::iterator E = IncludePaths.end();
272 action->args.push_back( std::string("-I") + I->get() );
279 if (*PI == "%libs%") {
280 PathVector::iterator I = LibraryPaths.begin();
281 PathVector::iterator E = LibraryPaths.end();
283 action->args.push_back( std::string("-L") + I->get() );
290 if (*PI == "%out%") {
291 action->args.push_back(output.get());
292 } else if (*PI == "%opt%") {
293 if (!isSet(EMIT_RAW_FLAG)) {
294 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
295 !cd->opts[optLevel].empty())
296 action->args.insert(action->args.end(),
297 cd->opts[optLevel].begin(),
298 cd->opts[optLevel].end());
300 throw std::string("Optimization options for level ") +
301 utostr(unsigned(optLevel)) + " were not specified";
307 if (*PI == "%stats%") {
308 if (isSet(SHOW_STATS_FLAG))
309 action->args.push_back("-stats");
314 if (*PI == "%target%") {
315 action->args.push_back(std::string("-march=") + machine);
316 } else if (*PI == "%time%") {
317 if (isSet(TIME_PASSES_FLAG))
318 action->args.push_back("-time-passes");
323 if (*PI == "%verbose%") {
324 if (isSet(VERBOSE_FLAG))
325 action->args.push_back("-v");
330 if (*PI == "%Mopts") {
331 action->args.insert(action->args.end(), MOptions.begin(),
337 if (*PI == "%Wopts") {
338 action->args.insert(action->args.end(), WOptions.begin(),
348 // Did it even look like a substitution?
349 if (PI->length()>1 && (*PI)[0] == '%' &&
350 (*PI)[PI->length()-1] == '%') {
351 throw std::string("Invalid substitution token: '") + *PI +
352 "' for command '" + pat->program.get() + "'";
354 // It's not a legal substitution, just pass it through
355 action->args.push_back(*PI);
359 // Its not a substitution, just put it in the action
360 action->args.push_back(*PI);
365 // Finally, we're done
369 bool DoAction(Action*action) {
370 assert(action != 0 && "Invalid Action!");
371 if (isSet(VERBOSE_FLAG))
373 if (!isSet(DRY_RUN_FLAG)) {
374 sys::Path progpath = sys::Program::FindProgramByName(
375 action->program.get());
376 if (progpath.is_empty())
377 throw std::string("Can't find program '"+progpath.get()+"'");
378 else if (progpath.executable())
379 action->program = progpath;
381 throw std::string("Program '"+progpath.get()+"' is not executable.");
383 // Invoke the program
384 if (isSet(TIME_ACTIONS_FLAG)) {
385 Timer timer(action->program.get());
388 sys::Program::ExecuteAndWait(action->program,action->args);
390 timer.print(timer,std::cerr);
391 return resultCode == 0;
395 sys::Program::ExecuteAndWait(action->program, action->args);
400 /// This method tries various variants of a linkage item's file
401 /// name to see if it can find an appropriate file to link with
402 /// in the directory specified.
403 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
404 const sys::Path& dir,
405 bool native = false) {
406 sys::Path fullpath(dir);
407 fullpath.append_file(link_item);
409 fullpath.append_suffix("a");
411 fullpath.append_suffix("bc");
412 if (fullpath.readable())
414 fullpath.elide_suffix();
415 fullpath.append_suffix("o");
416 if (fullpath.readable())
419 fullpath.append_file(std::string("lib") + link_item);
420 fullpath.append_suffix("a");
421 if (fullpath.readable())
423 fullpath.elide_suffix();
424 fullpath.append_suffix("so");
425 if (fullpath.readable())
434 /// This method processes a linkage item. The item could be a
435 /// Bytecode file needing translation to native code and that is
436 /// dependent on other bytecode libraries, or a native code
437 /// library that should just be linked into the program.
438 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
439 SetVector<sys::Path>& set,
441 // First, see if the unadorned file name is not readable. If so,
442 // we must track down the file in the lib search path.
444 if (!link_item.readable()) {
445 // First, look for the library using the -L arguments specified
446 // on the command line.
447 PathVector::iterator PI = LibraryPaths.begin();
448 PathVector::iterator PE = LibraryPaths.end();
449 while (PI != PE && fullpath.is_empty()) {
450 fullpath = GetPathForLinkageItem(link_item.get(),*PI);
454 // If we didn't find the file in any of the library search paths
455 // so we have to bail. No where else to look.
456 if (fullpath.is_empty()) {
458 std::string("Can't find linkage item '") + link_item.get() + "'";
462 fullpath = link_item;
465 // If we got here fullpath is the path to the file, and its readable.
466 set.insert(fullpath);
468 // If its an LLVM bytecode file ...
469 if (fullpath.is_bytecode_file()) {
470 // Process the dependent libraries recursively
471 Module::LibraryListType modlibs;
472 if (GetBytecodeDependentLibraries(fullpath.get(),modlibs)) {
473 // Traverse the dependent libraries list
474 Module::lib_iterator LI = modlibs.begin();
475 Module::lib_iterator LE = modlibs.end();
477 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
479 err = std::string("Library '") + *LI +
480 "' is not valid for linking but is required by file '" +
481 fullpath.get() + "'";
483 err += " which is required by file '" + fullpath.get() + "'";
489 } else if (err.empty()) {
491 "The dependent libraries could not be extracted from '") +
503 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
505 // Echo the configuration of options if we're running verbose
506 if (isSet(DEBUG_FLAG)) {
507 std::cerr << "Compiler Driver Options:\n";
508 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
509 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
510 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
511 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
512 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
513 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
514 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
515 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
516 std::cerr << "OutputMachine = " << machine << "\n";
517 InputList::const_iterator I = InpList.begin();
518 while ( I != InpList.end() ) {
519 std::cerr << "Input: " << I->first.get() << "(" << I->second
523 std::cerr << "Output: " << Output.get() << "\n";
526 // If there's no input, we're done.
528 throw std::string("Nothing to compile.");
530 // If they are asking for linking and didn't provide an output
531 // file then its an error (no way for us to "make up" a meaningful
532 // file name based on the various linker input files).
533 if (finalPhase == LINKING && Output.is_empty())
535 "An output file name must be specified for linker output");
537 // If they are not asking for linking, provided an output file and
538 // there is more than one input file, its an error
539 if (finalPhase != LINKING && !Output.is_empty() && InpList.size() > 1)
540 throw std::string("An output file name cannot be specified ") +
541 "with more than one input file name when not linking";
543 // This vector holds all the resulting actions of the following loop.
544 std::vector<Action*> actions;
546 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
547 // for each input item
548 SetVector<sys::Path> LinkageItems;
549 std::vector<std::string> LibFiles;
550 InputList::const_iterator I = InpList.begin();
551 while ( I != InpList.end() ) {
552 // Get the suffix of the file name
553 const std::string& ftype = I->second;
555 // If its a library, bytecode file, or object file, save
556 // it for linking below and short circuit the
557 // pre-processing/translation/assembly phases
558 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
559 // We shouldn't get any of these types of files unless we're
560 // later going to link. Enforce this limit now.
561 if (finalPhase != LINKING) {
563 "Pre-compiled objects found but linking not requested");
566 LibFiles.push_back(I->first.get());
568 LinkageItems.insert(I->first);
569 ++I; continue; // short circuit remainder of loop
572 // At this point, we know its something we need to translate
573 // and/or optimize. See if we can get the configuration data
574 // for this kind of file.
575 ConfigData* cd = cdp->ProvideConfigData(I->second);
577 throw std::string("Files of type '") + I->second +
578 "' are not recognized.";
579 if (isSet(DEBUG_FLAG))
580 DumpConfigData(cd,I->second);
582 // Initialize the input and output files
583 sys::Path InFile(I->first);
584 sys::Path OutFile(I->first.get_basename());
586 // PRE-PROCESSING PHASE
587 Action& action = cd->PreProcessor;
589 // Get the preprocessing action, if needed, or error if appropriate
590 if (!action.program.is_empty()) {
591 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
592 if (finalPhase == PREPROCESSING) {
593 OutFile.append_suffix("E");
594 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
596 sys::Path TempFile(MakeTempFile(I->first.get_basename(),"E"));
597 actions.push_back(GetAction(cd,InFile,TempFile,
602 } else if (finalPhase == PREPROCESSING) {
603 throw cd->langName + " does not support pre-processing";
604 } else if (action.isSet(REQUIRED_FLAG)) {
605 throw std::string("Don't know how to pre-process ") +
606 cd->langName + " files";
609 // Short-circuit remaining actions if all they want is
611 if (finalPhase == PREPROCESSING) { ++I; continue; };
613 /// TRANSLATION PHASE
614 action = cd->Translator;
616 // Get the translation action, if needed, or error if appropriate
617 if (!action.program.is_empty()) {
618 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
619 if (finalPhase == TRANSLATION) {
620 OutFile.append_suffix("o");
621 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
623 sys::Path TempFile(MakeTempFile(I->first.get_basename(),"trans"));
624 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
629 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
630 /// The output of the translator is an LLVM Assembly program
631 /// We need to translate it to bytecode
632 Action* action = new Action();
633 action->program.set_file("llvm-as");
634 action->args.push_back(InFile.get());
635 action->args.push_back("-o");
636 InFile.append_suffix("bc");
637 action->args.push_back(InFile.get());
638 actions.push_back(action);
641 } else if (finalPhase == TRANSLATION) {
642 throw cd->langName + " does not support translation";
643 } else if (action.isSet(REQUIRED_FLAG)) {
644 throw std::string("Don't know how to translate ") +
645 cd->langName + " files";
648 // Short-circuit remaining actions if all they want is translation
649 if (finalPhase == TRANSLATION) { ++I; continue; }
651 /// OPTIMIZATION PHASE
652 action = cd->Optimizer;
654 // Get the optimization action, if needed, or error if appropriate
655 if (!isSet(EMIT_RAW_FLAG)) {
656 if (!action.program.is_empty()) {
657 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
658 if (finalPhase == OPTIMIZATION) {
659 OutFile.append_suffix("o");
660 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
662 sys::Path TempFile(MakeTempFile(I->first.get_basename(),"opt"));
663 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
667 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
668 /// The output of the optimizer is an LLVM Assembly program
669 /// We need to translate it to bytecode with llvm-as
670 Action* action = new Action();
671 action->program.set_file("llvm-as");
672 action->args.push_back(InFile.get());
673 action->args.push_back("-f");
674 action->args.push_back("-o");
675 InFile.append_suffix("bc");
676 action->args.push_back(InFile.get());
677 actions.push_back(action);
680 } else if (finalPhase == OPTIMIZATION) {
681 throw cd->langName + " does not support optimization";
682 } else if (action.isSet(REQUIRED_FLAG)) {
683 throw std::string("Don't know how to optimize ") +
684 cd->langName + " files";
688 // Short-circuit remaining actions if all they want is optimization
689 if (finalPhase == OPTIMIZATION) { ++I; continue; }
692 action = cd->Assembler;
694 if (finalPhase == ASSEMBLY) {
695 if (isSet(EMIT_NATIVE_FLAG)) {
696 // Use llc to get the native assembly file
697 Action* action = new Action();
698 action->program.set_file("llc");
699 action->args.push_back(InFile.get());
700 action->args.push_back("-f");
701 action->args.push_back("-o");
702 OutFile.append_suffix("s");
703 action->args.push_back(OutFile.get());
705 // Just convert back to llvm assembly with llvm-dis
706 Action* action = new Action();
707 action->program.set_file("llvm-dis");
708 action->args.push_back(InFile.get());
709 action->args.push_back("-f");
710 action->args.push_back("-o");
711 OutFile.append_suffix("ll");
712 action->args.push_back(OutFile.get());
713 actions.push_back(action);
716 // Short circuit the rest of the loop, we don't want to link
721 // Register the result of the actions as a link candidate
722 LinkageItems.insert(InFile);
724 // Go to next file to be processed
726 } // end while loop over each input file
728 /// RUN THE COMPILATION ACTIONS
729 std::vector<Action*>::iterator AI = actions.begin();
730 std::vector<Action*>::iterator AE = actions.end();
733 throw std::string("Action failed");
738 if (finalPhase == LINKING) {
740 // Insert the platform-specific system libraries to the path list
741 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath1());
742 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath2());
744 // Set up the linking action with llvm-ld
745 Action* link = new Action();
746 link->program.set_file("llvm-ld");
748 // Add in the optimization level requested
750 case OPT_FAST_COMPILE:
751 link->args.push_back("-O1");
754 link->args.push_back("-O2");
757 link->args.push_back("-O3");
760 link->args.push_back("-O4");
762 case OPT_AGGRESSIVE_LINK_TIME:
763 link->args.push_back("-O5");
769 // Add in all the linkage items we generated. This includes the
770 // output from the translation/optimization phases as well as any
771 // -l arguments specified.
772 for (PathVector::const_iterator I=LinkageItems.begin(),
773 E=LinkageItems.end(); I != E; ++I )
774 link->args.push_back(I->get());
776 // Add in all the libraries we found.
777 for (std::vector<std::string>::const_iterator I=LibFiles.begin(),
778 E=LibFiles.end(); I != E; ++I )
779 link->args.push_back(std::string("-l")+*I);
781 // Add in all the library paths to the command line
782 for (PathVector::const_iterator I=LibraryPaths.begin(),
783 E=LibraryPaths.end(); I != E; ++I)
784 link->args.push_back( std::string("-L") + I->get());
786 // Add in the additional linker arguments requested
787 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
788 E=AdditionalArgs[LINKING].end(); I != E; ++I)
789 link->args.push_back( *I );
791 // Add in other optional flags
792 if (isSet(EMIT_NATIVE_FLAG))
793 link->args.push_back("-native");
794 if (isSet(VERBOSE_FLAG))
795 link->args.push_back("-v");
796 if (isSet(TIME_PASSES_FLAG))
797 link->args.push_back("-time-passes");
798 if (isSet(SHOW_STATS_FLAG))
799 link->args.push_back("-stats");
800 if (isSet(STRIP_OUTPUT_FLAG))
801 link->args.push_back("-s");
802 if (isSet(DEBUG_FLAG)) {
803 link->args.push_back("-debug");
804 link->args.push_back("-debug-pass=Details");
807 // Add in mandatory flags
808 link->args.push_back("-o");
809 link->args.push_back(Output.get());
813 throw std::string("Action failed");
815 } catch (std::string& msg) {
820 throw std::string("Unspecified error");
830 ConfigDataProvider* cdp; ///< Where we get configuration data from
831 Phases finalPhase; ///< The final phase of compilation
832 OptimizationLevels optLevel; ///< The optimization level to apply
833 unsigned Flags; ///< The driver flags
834 std::string machine; ///< Target machine name
835 PathVector LibraryPaths; ///< -L options
836 PathVector IncludePaths; ///< -I options
837 StringVector Defines; ///< -D options
838 sys::Path TempDir; ///< Name of the temporary directory.
839 StringTable AdditionalArgs; ///< The -Txyz options
840 StringVector fOptions; ///< -f options
841 StringVector MOptions; ///< -M options
842 StringVector WOptions; ///< -W options
848 CompilerDriver::~CompilerDriver() {
852 CompilerDriver::Get(ConfigDataProvider& CDP) {
853 return new CompilerDriverImpl(CDP);
856 CompilerDriver::ConfigData::ConfigData()
864 StringVector emptyVec;
865 for (unsigned i = 0; i < NUM_PHASES; ++i)
866 opts.push_back(emptyVec);
869 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab