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