More Functionality:
[oota-llvm.git] / tools / llvmc / CompilerDriver.h
1 //===- CompilerDriver.h - Compiler Driver -----------------------*- C++ -*-===//
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 //===------------------------------------------------------------------------===
14 #ifndef LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
15 #define LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
16
17 #include "llvm/System/Path.h"
18 #include <string>
19 #include <vector>
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:
33       /// @brief A vector of strings, commonly used
34       typedef std::vector<std::string> StringVector;
35
36       /// @brief The phases of processing that llvmc understands
37       enum Phases {
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
43       };
44
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!
53       };
54
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
63       };
64
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;
70
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
75       /// language.
76       struct Action {
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; }
86       };
87
88       struct ConfigData {
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
96       };
97
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 {
105       public:
106         virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
107         virtual void setConfigDir(const std::string& dirName) = 0;
108       };
109
110     /// @}
111     /// @name Constructors
112     /// @{
113     public:
114       CompilerDriver(ConfigDataProvider& cdp );
115       virtual ~CompilerDriver();
116
117     /// @}
118     /// @name Methods
119     /// @{
120     public:
121       /// @brief Handle an error
122       virtual void error(const std::string& errmsg);
123
124       /// @brief Execute the actions requested for the given input list.
125       virtual int execute(const InputList& list, const sys::Path& output);
126
127     /// @}
128     /// @name Mutators
129     /// @{
130     public:
131       /// @brief Set the final phase at which compilation terminates
132       void setFinalPhase( Phases phase ) { finalPhase = phase; }
133
134       /// @brief Set the optimization level for the compilation
135       void setOptimization( OptimizationLevels level ) { optLevel = level; }
136
137       /// @brief Prevent the CompilerDriver from taking any actions
138       void setDryRun( bool TF ) { isDryRun = TF; }
139
140       /// @brief Cause the CompilerDriver to print to stderr all the
141       /// actions it is taking.
142       void setVerbose( bool TF ) { isVerbose = TF; }
143
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; }
147
148       /// @brief Cause the CompilerDriver to print to stderr the 
149       /// execution time of each action taken.
150       void setTimeActions( bool TF ) { timeActions = TF; }
151
152       /// @brief Indicate that native code is to be generated instead
153       /// of LLVM bytecode.
154       void setEmitNativeCode( bool TF ) { emitNativeCode = TF; }
155
156       /// @brief Indicate that raw, unoptimized code is to be generated.
157       void setEmitRawCode(bool TF ) { emitRawCode = TF; }
158
159       /// @brief Set the output machine name.
160       void setOutputMachine( const std::string& machineName ) {
161         machine = machineName;
162       }
163
164       /// @brief Set Preprocessor specific options
165       void setPreprocessorOptions(const std::vector<std::string>& opts) {
166         PreprocessorOptions = opts;
167       }
168
169       /// @brief Set Translator specific options
170       void setTranslatorOptions(const std::vector<std::string>& opts) {
171         TranslatorOptions = opts;
172       }
173
174       /// @brief Set Optimizer specific options
175       void setOptimizerOptions(const std::vector<std::string>& opts) {
176         OptimizerOptions = opts;
177       }
178
179       /// @brief Set Assembler specific options
180       void setAssemblerOptions(const std::vector<std::string>& opts) {
181         AssemblerOptions = opts;
182       }
183
184       /// @brief Set Linker specific options
185       void setLinkerOptions(const std::vector<std::string>& opts) {
186         LinkerOptions = opts;
187       }
188
189       /// @brief Set Library Paths
190       void setLibraryPaths(const std::vector<std::string>& paths) {
191         LibraryPaths = paths;
192       }
193
194       /// @brief Set the list of library paths to be searched for
195       /// libraries.
196       void addLibraryPath( const std::string& libPath ) {
197         LibraryPaths.push_back(libPath);
198       }
199
200     /// @}
201     /// @name Functions
202     /// @{
203     private:
204       Action* GetAction(ConfigData* cd, const std::string& input, 
205                        const std::string& output, Phases phase );
206       void DoAction(Action* a);
207
208     /// @}
209     /// @name Data
210     /// @{
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 ?
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;
228
229     /// @}
230
231   };
232 }
233
234 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
235 #endif