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/System/Signals.h"
20 #include "Support/FileUtilities.h"
21 #include "Support/SetVector.h"
22 #include "Support/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() {
98 AdditionalArgs.clear();
105 virtual void setFinalPhase( Phases phase ) {
109 virtual void setOptimization( OptimizationLevels level ) {
113 virtual void setDriverFlags( unsigned flags ) {
114 Flags = flags & DRIVER_FLAGS_MASK;
117 virtual void setOutputMachine( const std::string& machineName ) {
118 machine = machineName;
121 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
122 assert(phase <= LINKING && phase >= PREPROCESSING);
123 AdditionalArgs[phase] = opts;
126 virtual void setIncludePaths(const StringVector& paths) {
127 StringVector::const_iterator I = paths.begin();
128 StringVector::const_iterator E = paths.end();
131 tmp.set_directory(*I);
132 IncludePaths.push_back(tmp);
137 virtual void setSymbolDefines(const StringVector& defs) {
141 virtual void setLibraryPaths(const StringVector& paths) {
142 StringVector::const_iterator I = paths.begin();
143 StringVector::const_iterator E = paths.end();
146 tmp.set_directory(*I);
147 LibraryPaths.push_back(tmp);
152 virtual void addLibraryPath( const sys::Path& libPath ) {
153 LibraryPaths.push_back(libPath);
160 bool isSet(DriverFlags flag) {
161 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
165 if (!isSet(KEEP_TEMPS_FLAG)) {
166 if (TempDir.is_directory() && TempDir.writable())
167 TempDir.destroy_directory(/*remove_contents=*/true);
169 std::cout << "Temporary files are in " << TempDir.get() << "\n";
173 sys::Path MakeTempFile(const std::string& basename, const std::string& suffix ) {
174 sys::Path result(TempDir);
175 if (!result.append_file(basename))
176 throw basename + ": can't use this file name";
177 if (!result.append_suffix(suffix))
178 throw suffix + ": can't use this file suffix";
182 Action* GetAction(ConfigData* cd,
183 const sys::Path& input,
184 const sys::Path& output,
187 Action* pat = 0; ///< The pattern/template for the action
188 Action* action = new Action; ///< The actual action to execute
190 // Get the action pattern
192 case PREPROCESSING: pat = &cd->PreProcessor; break;
193 case TRANSLATION: pat = &cd->Translator; break;
194 case OPTIMIZATION: pat = &cd->Optimizer; break;
195 case ASSEMBLY: pat = &cd->Assembler; break;
196 case LINKING: pat = &cd->Linker; break;
198 assert(!"Invalid driver phase!");
201 assert(pat != 0 && "Invalid command pattern");
203 // Copy over some pattern things that don't need to change
204 action->program = pat->program;
205 action->flags = pat->flags;
207 // Do the substitutions from the pattern to the actual
208 StringVector::iterator PI = pat->args.begin();
209 StringVector::iterator PE = pat->args.end();
211 if ((*PI)[0] == '%' && PI->length() >2) {
215 if (*PI == "%args%") {
216 if (AdditionalArgs.size() > unsigned(phase))
217 if (!AdditionalArgs[phase].empty()) {
218 // Get specific options for each kind of action type
219 StringVector& addargs = AdditionalArgs[phase];
220 // Add specific options for each kind of action type
221 action->args.insert(action->args.end(), addargs.begin(), addargs.end());
227 if (*PI == "%defs%") {
228 StringVector::iterator I = Defines.begin();
229 StringVector::iterator E = Defines.end();
231 action->args.push_back( std::string("-D") + *I);
238 if (*PI == "%force%") {
239 if (isSet(FORCE_FLAG))
240 action->args.push_back("-f");
246 action->args.push_back(input.get());
247 } else if (*PI == "%incls%") {
248 PathVector::iterator I = IncludePaths.begin();
249 PathVector::iterator E = IncludePaths.end();
251 action->args.push_back( std::string("-I") + I->get() );
258 if (*PI == "%libs%") {
259 PathVector::iterator I = LibraryPaths.begin();
260 PathVector::iterator E = LibraryPaths.end();
262 action->args.push_back( std::string("-L") + I->get() );
269 if (*PI == "%out%") {
270 action->args.push_back(output.get());
271 } else if (*PI == "%opt%") {
272 if (!isSet(EMIT_RAW_FLAG)) {
273 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
274 !cd->opts[optLevel].empty())
275 action->args.insert(action->args.end(), cd->opts[optLevel].begin(),
276 cd->opts[optLevel].end());
278 throw std::string("Optimization options for level ") +
279 utostr(unsigned(optLevel)) + " were not specified";
285 if (*PI == "%stats%") {
286 if (isSet(SHOW_STATS_FLAG))
287 action->args.push_back("-stats");
292 if (*PI == "%target%") {
293 action->args.push_back(std::string("-march=") + machine);
294 } else if (*PI == "%time%") {
295 if (isSet(TIME_PASSES_FLAG))
296 action->args.push_back("-time-passes");
301 if (*PI == "%verbose%") {
302 if (isSet(VERBOSE_FLAG))
303 action->args.push_back("-v");
312 // Did it even look like a substitution?
313 if (PI->length()>1 && (*PI)[0] == '%' &&
314 (*PI)[PI->length()-1] == '%') {
315 throw std::string("Invalid substitution token: '") + *PI +
316 "' for command '" + pat->program.get() + "'";
318 // It's not a legal substitution, just pass it through
319 action->args.push_back(*PI);
323 // Its not a substitution, just put it in the action
324 action->args.push_back(*PI);
329 // Finally, we're done
333 bool DoAction(Action*action) {
334 assert(action != 0 && "Invalid Action!");
335 if (isSet(VERBOSE_FLAG))
337 if (!isSet(DRY_RUN_FLAG)) {
338 action->program = sys::Program::FindProgramByName(action->program.get());
339 if (action->program.is_empty())
340 throw std::string("Can't find program '") + action->program.get() + "'";
342 // Invoke the program
343 return 0 == action->program.ExecuteAndWait(action->args);
348 /// This method tries various variants of a linkage item's file
349 /// name to see if it can find an appropriate file to link with
350 /// in the directory specified.
351 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
352 const sys::Path& dir) {
353 sys::Path fullpath(dir);
354 fullpath.append_file(link_item);
355 fullpath.append_suffix("bc");
356 if (fullpath.readable())
358 fullpath.elide_suffix();
359 fullpath.append_suffix("o");
360 if (fullpath.readable())
363 fullpath.append_file(std::string("lib") + link_item);
364 fullpath.append_suffix("a");
365 if (fullpath.readable())
367 fullpath.elide_suffix();
368 fullpath.append_suffix("so");
369 if (fullpath.readable())
377 /// This method processes a linkage item. The item could be a
378 /// Bytecode file needing translation to native code and that is
379 /// dependent on other bytecode libraries, or a native code
380 /// library that should just be linked into the program.
381 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
382 SetVector<sys::Path>& set,
384 // First, see if the unadorned file name is not readable. If so,
385 // we must track down the file in the lib search path.
387 if (!link_item.readable()) {
388 // First, look for the library using the -L arguments specified
389 // on the command line.
390 PathVector::iterator PI = LibraryPaths.begin();
391 PathVector::iterator PE = LibraryPaths.end();
392 while (PI != PE && fullpath.is_empty()) {
393 fullpath = GetPathForLinkageItem(link_item.get(),*PI);
397 // If we didn't find the file in any of the library search paths
398 // so we have to bail. No where else to look.
399 if (fullpath.is_empty()) {
400 err = std::string("Can't find linkage item '") + link_item.get() + "'";
404 fullpath = link_item;
407 // If we got here fullpath is the path to the file, and its readable.
408 set.insert(fullpath);
410 // If its an LLVM bytecode file ...
411 if (CheckMagic(fullpath.get(), "llvm")) {
412 // Process the dependent libraries recursively
413 Module::LibraryListType modlibs;
414 if (GetBytecodeDependentLibraries(fullpath.get(),modlibs)) {
415 // Traverse the dependent libraries list
416 Module::lib_iterator LI = modlibs.begin();
417 Module::lib_iterator LE = modlibs.end();
419 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
421 err = std::string("Library '") + *LI +
422 "' is not valid for linking but is required by file '" +
423 fullpath.get() + "'";
425 err += " which is required by file '" + fullpath.get() + "'";
431 } else if (err.empty()) {
432 err = std::string("The dependent libraries could not be extracted from '")
444 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
446 // Echo the configuration of options if we're running verbose
447 if (isSet(DEBUG_FLAG)) {
448 std::cerr << "Compiler Driver Options:\n";
449 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
450 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
451 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
452 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
453 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
454 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
455 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
456 std::cerr << "ForceOutput = " << isSet(FORCE_FLAG) << "\n";
457 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
458 std::cerr << "OutputMachine = " << machine << "\n";
459 InputList::const_iterator I = InpList.begin();
460 while ( I != InpList.end() ) {
461 std::cerr << "Input: " << I->first.get() << "(" << I->second << ")\n";
464 std::cerr << "Output: " << Output.get() << "\n";
467 // If there's no input, we're done.
469 throw std::string("Nothing to compile.");
471 // If they are asking for linking and didn't provide an output
472 // file then its an error (no way for us to "make up" a meaningful
473 // file name based on the various linker input files).
474 if (finalPhase == LINKING && Output.is_empty())
476 "An output file name must be specified for linker output");
478 // This vector holds all the resulting actions of the following loop.
479 std::vector<Action*> actions;
481 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
482 // for each input item
483 SetVector<sys::Path> LinkageItems;
484 sys::Path OutFile(Output);
485 InputList::const_iterator I = InpList.begin();
486 while ( I != InpList.end() ) {
487 // Get the suffix of the file name
488 const std::string& ftype = I->second;
490 // If its a library, bytecode file, or object file, save
491 // it for linking below and short circuit the
492 // pre-processing/translation/assembly phases
493 if (ftype.empty() || ftype == "o" || ftype == "bc") {
494 // We shouldn't get any of these types of files unless we're
495 // later going to link. Enforce this limit now.
496 if (finalPhase != LINKING) {
498 "Pre-compiled objects found but linking not requested");
500 LinkageItems.insert(I->first);
501 ++I; continue; // short circuit remainder of loop
504 // At this point, we know its something we need to translate
505 // and/or optimize. See if we can get the configuration data
506 // for this kind of file.
507 ConfigData* cd = cdp->ProvideConfigData(I->second);
509 throw std::string("Files of type '") + I->second +
510 "' are not recognized.";
511 if (isSet(DEBUG_FLAG))
512 DumpConfigData(cd,I->second);
514 // Initialize the input file
515 sys::Path InFile(I->first);
517 // PRE-PROCESSING PHASE
518 Action& action = cd->PreProcessor;
520 // Get the preprocessing action, if needed, or error if appropriate
521 if (!action.program.is_empty()) {
522 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
523 if (finalPhase == PREPROCESSING)
524 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
526 sys::Path TempFile(MakeTempFile(I->first.get(),"E"));
527 actions.push_back(GetAction(cd,InFile,TempFile,PREPROCESSING));
531 } else if (finalPhase == PREPROCESSING) {
532 throw cd->langName + " does not support pre-processing";
533 } else if (action.isSet(REQUIRED_FLAG)) {
534 throw std::string("Don't know how to pre-process ") +
535 cd->langName + " files";
538 // Short-circuit remaining actions if all they want is pre-processing
539 if (finalPhase == PREPROCESSING) { ++I; continue; };
541 /// TRANSLATION PHASE
542 action = cd->Translator;
544 // Get the translation action, if needed, or error if appropriate
545 if (!action.program.is_empty()) {
546 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
547 if (finalPhase == TRANSLATION)
548 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
550 sys::Path TempFile(MakeTempFile(I->first.get(),"trans"));
551 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
556 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
557 /// The output of the translator is an LLVM Assembly program
558 /// We need to translate it to bytecode
559 Action* action = new Action();
560 action->program.set_file("llvm-as");
561 action->args.push_back(InFile.get());
562 action->args.push_back("-o");
563 InFile.append_suffix("bc");
564 action->args.push_back(InFile.get());
565 actions.push_back(action);
568 } else if (finalPhase == TRANSLATION) {
569 throw cd->langName + " does not support translation";
570 } else if (action.isSet(REQUIRED_FLAG)) {
571 throw std::string("Don't know how to translate ") +
572 cd->langName + " files";
575 // Short-circuit remaining actions if all they want is translation
576 if (finalPhase == TRANSLATION) { ++I; continue; }
578 /// OPTIMIZATION PHASE
579 action = cd->Optimizer;
581 // Get the optimization action, if needed, or error if appropriate
582 if (!isSet(EMIT_RAW_FLAG)) {
583 if (!action.program.is_empty()) {
584 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
585 if (finalPhase == OPTIMIZATION)
586 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
588 sys::Path TempFile(MakeTempFile(I->first.get(),"opt"));
589 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
593 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
594 /// The output of the translator is an LLVM Assembly program
595 /// We need to translate it to bytecode
596 Action* action = new Action();
597 action->program.set_file("llvm-as");
598 action->args.push_back(InFile.get());
599 action->args.push_back("-f");
600 action->args.push_back("-o");
601 InFile.append_suffix("bc");
602 action->args.push_back(InFile.get());
603 actions.push_back(action);
606 } else if (finalPhase == OPTIMIZATION) {
607 throw cd->langName + " does not support optimization";
608 } else if (action.isSet(REQUIRED_FLAG)) {
609 throw std::string("Don't know how to optimize ") +
610 cd->langName + " files";
614 // Short-circuit remaining actions if all they want is optimization
615 if (finalPhase == OPTIMIZATION) { ++I; continue; }
618 action = cd->Assembler;
620 if (finalPhase == ASSEMBLY || isSet(EMIT_NATIVE_FLAG)) {
621 if (isSet(EMIT_NATIVE_FLAG)) {
622 if (action.program.is_empty()) {
623 throw std::string("Native Assembler not specified for ") +
624 cd->langName + " files";
625 } else if (finalPhase == ASSEMBLY) {
626 actions.push_back(GetAction(cd,InFile,OutFile,ASSEMBLY));
628 sys::Path TempFile(MakeTempFile(I->first.get(),"S"));
629 actions.push_back(GetAction(cd,InFile,TempFile,ASSEMBLY));
633 // Just convert back to llvm assembly with llvm-dis
634 Action* action = new Action();
635 action->program.set_file("llvm-dis");
636 action->args.push_back(InFile.get());
637 action->args.push_back("-f");
638 action->args.push_back("-o");
639 action->args.push_back(OutFile.get());
640 actions.push_back(action);
644 // Short-circuit remaining actions if all they want is assembly output
645 if (finalPhase == ASSEMBLY) { ++I; continue; }
647 // Register the OutFile as a link candidate
648 LinkageItems.insert(InFile);
650 // Go to next file to be processed
654 /// RUN THE COMPILATION ACTIONS
655 std::vector<Action*>::iterator AI = actions.begin();
656 std::vector<Action*>::iterator AE = actions.end();
659 throw std::string("Action failed");
665 if (finalPhase == LINKING) {
666 if (isSet(EMIT_NATIVE_FLAG)) {
668 "llvmc doesn't know how to link native code yet");
670 // First, we need to examine the files to ensure that they all contain
671 // bytecode files. Since the final output is bytecode, we can only
673 SetVector<sys::Path>::const_iterator I = LinkageItems.begin();
674 SetVector<sys::Path>::const_iterator E = LinkageItems.end();
677 while (I != E && ProcessLinkageItem(*I,LinkageItems,errmsg))
683 // Insert the system libraries.
684 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath1());
685 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath2());
687 // We're emitting bytecode so let's build an llvm-link Action
688 Action* link = new Action();
689 link->program.set_file("llvm-link");
690 for (PathVector::const_iterator I=LinkageItems.begin(),
691 E=LinkageItems.end(); I != E; ++I )
692 link->args.push_back(I->get());
693 if (isSet(VERBOSE_FLAG))
694 link->args.push_back("-v");
695 link->args.push_back("-f");
696 link->args.push_back("-o");
697 link->args.push_back(OutFile.get());
698 if (isSet(TIME_PASSES_FLAG))
699 link->args.push_back("-time-passes");
700 if (isSet(SHOW_STATS_FLAG))
701 link->args.push_back("-stats");
702 actions.push_back(link);
706 /// RUN THE LINKING ACTIONS
707 AI = actions.begin();
711 throw std::string("Action failed");
714 } catch (std::string& msg) {
719 throw std::string("Unspecified error");
729 ConfigDataProvider* cdp; ///< Where we get configuration data from
730 Phases finalPhase; ///< The final phase of compilation
731 OptimizationLevels optLevel; ///< The optimization level to apply
732 unsigned Flags; ///< The driver flags
733 std::string machine; ///< Target machine name
734 PathVector LibraryPaths; ///< -L options
735 PathVector IncludePaths; ///< -I options
736 StringVector Defines; ///< -D options
737 sys::Path TempDir; ///< Name of the temporary directory.
738 StringTable AdditionalArgs; ///< The -Txyz options
744 CompilerDriver::~CompilerDriver() {
748 CompilerDriver::Get(ConfigDataProvider& CDP) {
749 return new CompilerDriverImpl(CDP);
752 CompilerDriver::ConfigData::ConfigData()
760 StringVector emptyVec;
761 for (unsigned i = 0; i < NUM_PHASES; ++i)
762 opts.push_back(emptyVec);
765 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab