Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 1 | //===- CompilerDriver.h - Compiler Driver -----------------------*- C++ -*-===// |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the CompilerDriver class which implements the bulk of the |
| 11 | // LLVM Compiler Driver program (llvmc). |
| 12 | // |
| 13 | //===------------------------------------------------------------------------=== |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 14 | #ifndef LLVM_TOOLS_LLVMC_COMPILERDRIVER_H |
| 15 | #define LLVM_TOOLS_LLVMC_COMPILERDRIVER_H |
| 16 | |
| 17 | #include <string> |
| 18 | #include <vector> |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 19 | |
| 20 | namespace llvm { |
| 21 | /// This class provides the high level interface to the LLVM Compiler Driver. |
| 22 | /// The driver's purpose is to make it easier for compiler writers and users |
| 23 | /// of LLVM to utilize the compiler toolkits and LLVM toolset by learning only |
| 24 | /// the interface of one program (llvmc). |
| 25 | /// |
| 26 | /// @see llvmc.cpp |
| 27 | /// @brief The interface to the LLVM Compiler Driver. |
| 28 | class CompilerDriver { |
| 29 | /// @name Types |
| 30 | /// @{ |
| 31 | public: |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 32 | /// @brief A vector of strings, commonly used |
| 33 | typedef std::vector<std::string> StringVector; |
| 34 | |
| 35 | /// @brief The phases of processing that llvmc understands |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 36 | enum Phases { |
| 37 | PREPROCESSING, ///< Source language combining, filtering, substitution |
| 38 | TRANSLATION, ///< Translate source -> LLVM bytecode/assembly |
| 39 | OPTIMIZATION, ///< Optimize translation result |
| 40 | LINKING, ///< Link bytecode and native code |
| 41 | ASSEMBLY, ///< Convert program to executable |
| 42 | }; |
| 43 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 44 | /// @brief The levels of optimization llvmc understands |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 45 | enum OptimizationLevels { |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 46 | OPT_FAST_COMPILE, ///< Optimize to make >compile< go faster |
| 47 | OPT_SIMPLE, ///< Standard/simple optimizations |
| 48 | OPT_AGGRESSIVE, ///< Aggressive optimizations |
| 49 | OPT_LINK_TIME, ///< Aggressive + LinkTime optimizations |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 50 | OPT_AGGRESSIVE_LINK_TIME, ///< Make it go way fast! |
| 51 | OPT_NONE ///< No optimizations. Keep this at the end! |
| 52 | }; |
| 53 | |
| 54 | /// @brief Action specific flags |
| 55 | enum ConfigurationFlags { |
| 56 | REQUIRED_FLAG = 0x0001, ///< Should the action always be run? |
| 57 | GROKS_DASH_O_FLAG = 0x0002, ///< Understands the -On options? |
| 58 | PREPROCESSES_FLAG = 0x0004, ///< Does this action preprocess? |
| 59 | OPTIMIZES_FLAG = 0x0008, ///< Does this action optimize? |
| 60 | GROKS_O10N_FLAG = 0x0010, ///< Understands optimization options? |
| 61 | FLAGS_MASK = 0x001F, ///< Union of all flags |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /// This type is the input list to the CompilerDriver. It provides |
| 65 | /// a vector of filename/filetype pairs. The filetype is used to look up |
| 66 | /// the configuration of the actions to be taken by the driver. |
| 67 | /// @brief The Input Data to the execute method |
Reid Spencer | 2a069fa | 2004-08-16 07:06:38 +0000 | [diff] [blame^] | 68 | typedef std::vector<std::pair<std::string,std::string> > InputList; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 69 | |
| 70 | /// This type is read from configuration files or otherwise provided to |
| 71 | /// the CompilerDriver through a "ConfigDataProvider". It serves as both |
| 72 | /// the template of what to do and the actual Action to be executed. |
| 73 | /// @brief A structure to hold the action data for a given source |
| 74 | /// language. |
| 75 | struct Action { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 76 | Action() : inputAt(0) , outputAt(0), flags(0) {} |
Reid Spencer | 2a069fa | 2004-08-16 07:06:38 +0000 | [diff] [blame^] | 77 | std::string program; ///< The program to execve |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 78 | StringVector args; ///< Arguments to the program |
| 79 | size_t inputAt; ///< Argument index to insert input file |
| 80 | size_t outputAt; ///< Argument index to insert output file |
| 81 | unsigned flags; ///< Action specific flags |
| 82 | void set(unsigned fl ) { flags |= fl; } |
| 83 | void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); } |
| 84 | bool isSet(unsigned fl) { return flags&fl != 0; } |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct ConfigData { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 88 | std::string langName; ///< The name of the source language |
| 89 | std::vector<StringVector> opts; ///< The o10n options for each level |
| 90 | Action PreProcessor; ///< PreProcessor command line |
| 91 | Action Translator; ///< Translator command line |
| 92 | Action Optimizer; ///< Optimizer command line |
| 93 | Action Assembler; ///< Assembler command line |
| 94 | Action Linker; ///< Linker command line |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /// This pure virtual interface class defines the interface between the |
| 98 | /// CompilerDriver and other software that provides ConfigData objects to |
| 99 | /// it. The CompilerDriver must be configured to use an object of this |
| 100 | /// type so it can obtain the configuration data. |
| 101 | /// @see setConfigDataProvider |
| 102 | /// @brief Configuration Data Provider interface |
| 103 | class ConfigDataProvider { |
| 104 | public: |
| 105 | virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0; |
| 106 | virtual void setConfigDir(const std::string& dirName) = 0; |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /// @} |
| 110 | /// @name Constructors |
| 111 | /// @{ |
| 112 | public: |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 113 | CompilerDriver(ConfigDataProvider& cdp ); |
| 114 | virtual ~CompilerDriver(); |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 115 | |
| 116 | /// @} |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 117 | /// @name Methods |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 118 | /// @{ |
| 119 | public: |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 120 | /// @brief Handle an error |
| 121 | virtual void error(const std::string& errmsg); |
| 122 | |
| 123 | /// @brief Execute the actions requested for the given input list. |
Reid Spencer | 2a069fa | 2004-08-16 07:06:38 +0000 | [diff] [blame^] | 124 | virtual int execute(const InputList& list, const std::string& output); |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 125 | |
| 126 | /// @} |
| 127 | /// @name Mutators |
| 128 | /// @{ |
| 129 | public: |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 130 | /// @brief Set the final phase at which compilation terminates |
| 131 | void setFinalPhase( Phases phase ) { finalPhase = phase; } |
| 132 | |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 133 | /// @brief Set the optimization level for the compilation |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 134 | void setOptimization( OptimizationLevels level ) { optLevel = level; } |
| 135 | |
| 136 | /// @brief Prevent the CompilerDriver from taking any actions |
| 137 | void setDryRun( bool TF ) { isDryRun = TF; } |
| 138 | |
| 139 | /// @brief Cause the CompilerDriver to print to stderr all the |
| 140 | /// actions it is taking. |
| 141 | void setVerbose( bool TF ) { isVerbose = TF; } |
| 142 | |
| 143 | /// @brief Cause the CompilerDriver to print to stderr very verbose |
| 144 | /// information that might be useful in debugging the driver's actions |
| 145 | void setDebug( bool TF ) { isDebug = TF; } |
| 146 | |
| 147 | /// @brief Cause the CompilerDriver to print to stderr the |
| 148 | /// execution time of each action taken. |
| 149 | void setTimeActions( bool TF ) { timeActions = TF; } |
| 150 | |
| 151 | /// @brief Indicate that native code is to be generated instead |
| 152 | /// of LLVM bytecode. |
| 153 | void setEmitNativeCode( bool TF ) { emitNativeCode = TF; } |
| 154 | |
| 155 | /// @brief Indicate that raw, unoptimized code is to be generated. |
| 156 | void setEmitRawCode(bool TF ) { emitRawCode = TF; } |
| 157 | |
| 158 | /// @brief Set the output machine name. |
| 159 | void setOutputMachine( const std::string& machineName ) { |
| 160 | machine = machineName; |
| 161 | } |
| 162 | |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 163 | /// @brief Set Preprocessor specific options |
| 164 | void setPreprocessorOptions(const std::vector<std::string>& opts) { |
| 165 | PreprocessorOptions = opts; |
| 166 | } |
| 167 | |
| 168 | /// @brief Set Translator specific options |
| 169 | void setTranslatorOptions(const std::vector<std::string>& opts) { |
| 170 | TranslatorOptions = opts; |
| 171 | } |
| 172 | |
| 173 | /// @brief Set Optimizer specific options |
| 174 | void setOptimizerOptions(const std::vector<std::string>& opts) { |
| 175 | OptimizerOptions = opts; |
| 176 | } |
| 177 | |
| 178 | /// @brief Set Assembler specific options |
| 179 | void setAssemblerOptions(const std::vector<std::string>& opts) { |
| 180 | AssemblerOptions = opts; |
| 181 | } |
| 182 | |
| 183 | /// @brief Set Linker specific options |
| 184 | void setLinkerOptions(const std::vector<std::string>& opts) { |
| 185 | LinkerOptions = opts; |
| 186 | } |
| 187 | |
| 188 | /// @brief Set Library Paths |
| 189 | void setLibraryPaths(const std::vector<std::string>& paths) { |
| 190 | LibraryPaths = paths; |
| 191 | } |
| 192 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 193 | /// @brief Set the list of library paths to be searched for |
| 194 | /// libraries. |
| 195 | void addLibraryPath( const std::string& libPath ) { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 196 | LibraryPaths.push_back(libPath); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /// @} |
| 200 | /// @name Functions |
| 201 | /// @{ |
| 202 | private: |
| 203 | Action* GetAction(ConfigData* cd, const std::string& input, |
| 204 | const std::string& output, Phases phase ); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 205 | void DoAction(Action* a); |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 206 | |
| 207 | /// @} |
| 208 | /// @name Data |
| 209 | /// @{ |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 210 | private: |
| 211 | ConfigDataProvider* cdp; ///< Where we get configuration data from |
| 212 | Phases finalPhase; ///< The final phase of compilation |
| 213 | OptimizationLevels optLevel; ///< The optimization level to apply |
| 214 | bool isDryRun; ///< Prevent actions ? |
| 215 | bool isVerbose; ///< Print actions? |
| 216 | bool isDebug; ///< Print lotsa debug info? |
| 217 | bool timeActions; ///< Time the actions executed ? |
| 218 | bool emitRawCode; ///< Emit Raw (unoptimized) code? |
| 219 | bool emitNativeCode; ///< Emit native code instead of bytecode? |
| 220 | std::string machine; ///< Target machine name |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 221 | std::vector<std::string> LibraryPaths; |
| 222 | std::vector<std::string> PreprocessorOptions; |
| 223 | std::vector<std::string> TranslatorOptions; |
| 224 | std::vector<std::string> OptimizerOptions; |
| 225 | std::vector<std::string> AssemblerOptions; |
| 226 | std::vector<std::string> LinkerOptions; |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 227 | |
| 228 | /// @} |
| 229 | |
| 230 | }; |
| 231 | } |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 232 | |
| 233 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |
| 234 | #endif |