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