1 //===- CompilerDriver.h - Compiler Driver -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the CompilerDriver class which implements the bulk of the
11 // LLVM Compiler Driver program (llvmc).
13 //===------------------------------------------------------------------------===
14 #ifndef LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
15 #define LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
17 #include "llvm/System/Path.h"
22 /// This class provides the high level interface to the LLVM Compiler Driver.
23 /// The driver's purpose is to make it easier for compiler writers and users
24 /// of LLVM to utilize the compiler toolkits and LLVM toolset by learning only
25 /// the interface of one program (llvmc).
28 /// @brief The interface to the LLVM Compiler Driver.
29 class CompilerDriver {
33 /// @brief A vector of strings, commonly used
34 typedef std::vector<std::string> StringVector;
36 /// @brief The phases of processing that llvmc understands
38 PREPROCESSING, ///< Source language combining, filtering, substitution
39 TRANSLATION, ///< Translate source -> LLVM bytecode/assembly
40 OPTIMIZATION, ///< Optimize translation result
41 LINKING, ///< Link bytecode and native code
42 ASSEMBLY, ///< Convert program to executable
45 /// @brief The levels of optimization llvmc understands
46 enum OptimizationLevels {
47 OPT_FAST_COMPILE, ///< Optimize to make >compile< go faster
48 OPT_SIMPLE, ///< Standard/simple optimizations
49 OPT_AGGRESSIVE, ///< Aggressive optimizations
50 OPT_LINK_TIME, ///< Aggressive + LinkTime optimizations
51 OPT_AGGRESSIVE_LINK_TIME, ///< Make it go way fast!
52 OPT_NONE ///< No optimizations. Keep this at the end!
55 /// @brief Action specific flags
56 enum ConfigurationFlags {
57 REQUIRED_FLAG = 0x0001, ///< Should the action always be run?
58 GROKS_DASH_O_FLAG = 0x0002, ///< Understands the -On options?
59 PREPROCESSES_FLAG = 0x0004, ///< Does this action preprocess?
60 OPTIMIZES_FLAG = 0x0008, ///< Does this action optimize?
61 GROKS_O10N_FLAG = 0x0010, ///< Understands optimization options?
62 FLAGS_MASK = 0x001F, ///< Union of all flags
65 /// This type is the input list to the CompilerDriver. It provides
66 /// a vector of filename/filetype pairs. The filetype is used to look up
67 /// the configuration of the actions to be taken by the driver.
68 /// @brief The Input Data to the execute method
69 typedef std::vector<std::pair<sys::Path,std::string> > InputList;
71 /// This type is read from configuration files or otherwise provided to
72 /// the CompilerDriver through a "ConfigDataProvider". It serves as both
73 /// the template of what to do and the actual Action to be executed.
74 /// @brief A structure to hold the action data for a given source
77 Action() : inputAt(0) , outputAt(0), flags(0) {}
78 sys::Path program; ///< The program to execve
79 StringVector args; ///< Arguments to the program
80 size_t inputAt; ///< Argument index to insert input file
81 size_t outputAt; ///< Argument index to insert output file
82 unsigned flags; ///< Action specific flags
83 void set(unsigned fl ) { flags |= fl; }
84 void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); }
85 bool isSet(unsigned fl) { return flags&fl != 0; }
89 std::string langName; ///< The name of the source language
90 std::vector<StringVector> opts; ///< The o10n options for each level
91 Action PreProcessor; ///< PreProcessor command line
92 Action Translator; ///< Translator command line
93 Action Optimizer; ///< Optimizer command line
94 Action Assembler; ///< Assembler command line
95 Action Linker; ///< Linker command line
98 /// This pure virtual interface class defines the interface between the
99 /// CompilerDriver and other software that provides ConfigData objects to
100 /// it. The CompilerDriver must be configured to use an object of this
101 /// type so it can obtain the configuration data.
102 /// @see setConfigDataProvider
103 /// @brief Configuration Data Provider interface
104 class ConfigDataProvider {
106 virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
107 virtual void setConfigDir(const std::string& dirName) = 0;
111 /// @name Constructors
114 CompilerDriver(ConfigDataProvider& cdp );
115 virtual ~CompilerDriver();
121 /// @brief Handle an error
122 virtual void error(const std::string& errmsg);
124 /// @brief Execute the actions requested for the given input list.
125 virtual int execute(const InputList& list, const sys::Path& output);
131 /// @brief Set the final phase at which compilation terminates
132 void setFinalPhase( Phases phase ) { finalPhase = phase; }
134 /// @brief Set the optimization level for the compilation
135 void setOptimization( OptimizationLevels level ) { optLevel = level; }
137 /// @brief Prevent the CompilerDriver from taking any actions
138 void setDryRun( bool TF ) { isDryRun = TF; }
140 /// @brief Cause the CompilerDriver to print to stderr all the
141 /// actions it is taking.
142 void setVerbose( bool TF ) { isVerbose = TF; }
144 /// @brief Cause the CompilerDriver to print to stderr very verbose
145 /// information that might be useful in debugging the driver's actions
146 void setDebug( bool TF ) { isDebug = TF; }
148 /// @brief Cause the CompilerDriver to print to stderr the
149 /// execution time of each action taken.
150 void setTimeActions( bool TF ) { timeActions = TF; }
152 /// @brief Indicate that native code is to be generated instead
153 /// of LLVM bytecode.
154 void setEmitNativeCode( bool TF ) { emitNativeCode = TF; }
156 /// @brief Indicate that raw, unoptimized code is to be generated.
157 void setEmitRawCode(bool TF ) { emitRawCode = TF; }
159 /// @brief Set the output machine name.
160 void setOutputMachine( const std::string& machineName ) {
161 machine = machineName;
164 /// @brief Set Preprocessor specific options
165 void setPreprocessorOptions(const std::vector<std::string>& opts) {
166 PreprocessorOptions = opts;
169 /// @brief Set Translator specific options
170 void setTranslatorOptions(const std::vector<std::string>& opts) {
171 TranslatorOptions = opts;
174 /// @brief Set Optimizer specific options
175 void setOptimizerOptions(const std::vector<std::string>& opts) {
176 OptimizerOptions = opts;
179 /// @brief Set Assembler specific options
180 void setAssemblerOptions(const std::vector<std::string>& opts) {
181 AssemblerOptions = opts;
184 /// @brief Set Linker specific options
185 void setLinkerOptions(const std::vector<std::string>& opts) {
186 LinkerOptions = opts;
189 /// @brief Set Library Paths
190 void setLibraryPaths(const std::vector<std::string>& paths) {
191 LibraryPaths = paths;
194 /// @brief Set the list of library paths to be searched for
196 void addLibraryPath( const std::string& libPath ) {
197 LibraryPaths.push_back(libPath);
204 Action* GetAction(ConfigData* cd, const std::string& input,
205 const std::string& output, Phases phase );
206 void DoAction(Action* a);
212 ConfigDataProvider* cdp; ///< Where we get configuration data from
213 Phases finalPhase; ///< The final phase of compilation
214 OptimizationLevels optLevel; ///< The optimization level to apply
215 bool isDryRun; ///< Prevent actions ?
216 bool isVerbose; ///< Print actions?
217 bool isDebug; ///< Print lotsa debug info?
218 bool timeActions; ///< Time the actions executed ?
219 bool emitRawCode; ///< Emit Raw (unoptimized) code?
220 bool emitNativeCode; ///< Emit native code instead of bytecode?
221 std::string machine; ///< Target machine name
222 std::vector<std::string> LibraryPaths;
223 std::vector<std::string> PreprocessorOptions;
224 std::vector<std::string> TranslatorOptions;
225 std::vector<std::string> OptimizerOptions;
226 std::vector<std::string> AssemblerOptions;
227 std::vector<std::string> LinkerOptions;
234 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab