62e53e2702ca46f427de26c8f06ee5d8566193d0
[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 <string>
18 #include <vector>
19 #include "llvm/System/Program.h"
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, used for argument lists
34       typedef std::vector<std::string> StringVector;
35
36       /// @brief A vector of sys::Path, used for path lists
37       typedef std::vector<sys::Path> PathVector;
38
39       /// @brief A table of strings, indexed typically by Phases
40       typedef std::vector<StringVector> StringTable;
41
42       /// @brief The phases of processing that llvmc understands
43       enum Phases {
44         PREPROCESSING, ///< Source language combining, filtering, substitution
45         TRANSLATION,   ///< Translate source -> LLVM bytecode/assembly
46         OPTIMIZATION,  ///< Optimize translation result 
47         ASSEMBLY,      ///< Convert program to executable
48         LINKING,       ///< Link bytecode and native code
49         NUM_PHASES     ///< Always last!
50       };
51
52       /// @brief The levels of optimization llvmc understands
53       enum OptimizationLevels {
54         OPT_FAST_COMPILE,         ///< Optimize to make >compile< go faster
55         OPT_SIMPLE,               ///< Standard/simple optimizations
56         OPT_AGGRESSIVE,           ///< Aggressive optimizations
57         OPT_LINK_TIME,            ///< Aggressive + LinkTime optimizations
58         OPT_AGGRESSIVE_LINK_TIME, ///< Make it go way fast!
59         OPT_NONE                  ///< No optimizations. Keep this at the end!
60       };
61
62       /// @brief Action specific flags
63       enum ConfigurationFlags {
64         REQUIRED_FLAG        = 0x0001, ///< Should the action always be run?
65         PREPROCESSES_FLAG    = 0x0002, ///< Does this action preprocess?
66         TRANSLATES_FLAG      = 0x0004, ///< Does this action translate?
67         OUTPUT_IS_ASM_FLAG   = 0x0008, ///< Action produces .ll files?
68         FLAGS_MASK           = 0x000F, ///< Union of all flags
69       };
70
71       /// This type is the input list to the CompilerDriver. It provides
72       /// a vector of pathname/filetype pairs. The filetype is used to look up
73       /// the configuration of the actions to be taken by the driver.
74       /// @brief The Input Data to the execute method
75       typedef std::vector<std::pair<sys::Path,std::string> > InputList;
76
77       /// This type is read from configuration files or otherwise provided to
78       /// the CompilerDriver through a "ConfigDataProvider". It serves as both
79       /// the template of what to do and the actual Action to be executed.
80       /// @brief A structure to hold the action data for a given source
81       /// language.
82       struct Action {
83         Action() : flags(0) {}
84         sys::Path program;     ///< The program to execve
85         StringVector args;     ///< Arguments to the program
86         unsigned flags;        ///< Action specific flags
87         void set(unsigned fl ) { flags |= fl; }
88         void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); }
89         bool isSet(unsigned fl) { return (flags&fl) != 0; }
90       };
91
92       struct ConfigData {
93         ConfigData();
94         std::string version;    ///< The version number.
95         std::string langName;   ///< The name of the source language 
96         StringTable opts;       ///< The o10n options for each level
97         Action PreProcessor;    ///< PreProcessor command line
98         Action Translator;      ///< Translator command line
99         Action Optimizer;       ///< Optimizer command line
100         Action Assembler;       ///< Assembler command line
101         Action Linker;          ///< Linker command line
102       };
103
104       /// This pure virtual interface class defines the interface between the
105       /// CompilerDriver and other software that provides ConfigData objects to
106       /// it. The CompilerDriver must be configured to use an object of this
107       /// type so it can obtain the configuration data. 
108       /// @see setConfigDataProvider
109       /// @brief Configuration Data Provider interface
110       class ConfigDataProvider {
111       public:
112         virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
113         virtual void setConfigDir(const sys::Path& dirName) = 0;
114       };
115
116       /// These flags control various actions of the compiler driver. They are
117       /// used by adding the needed flag values together and passing them to the
118       /// compiler driver's setDriverFlags method. 
119       /// @see setDriverFlags
120       /// @brief Driver specific flags
121       enum DriverFlags {
122         DRY_RUN_FLAG         = 0x0001, ///< Do everything but execute actions
123         VERBOSE_FLAG         = 0x0002, ///< Print each action
124         DEBUG_FLAG           = 0x0004, ///< Print debug information
125         TIME_PASSES_FLAG     = 0x0008, ///< Time the passes as they execute
126         TIME_ACTIONS_FLAG    = 0x0010, ///< Time the actions as they execute
127         SHOW_STATS_FLAG      = 0x0020, ///< Show pass statistics
128         EMIT_NATIVE_FLAG     = 0x0040, ///< Emit native code instead of bc
129         EMIT_RAW_FLAG        = 0x0080, ///< Emit raw, unoptimized bytecode
130         KEEP_TEMPS_FLAG      = 0x0100, ///< Don't delete temporary files
131         STRIP_OUTPUT_FLAG    = 0x0200, ///< Strip symbols from linked output
132         DRIVER_FLAGS_MASK    = 0x03FF, ///< Union of the above flags
133       };
134
135     /// @}
136     /// @name Constructors
137     /// @{
138     public:
139       /// @brief Static Constructor
140       static CompilerDriver* Get(ConfigDataProvider& CDP);
141
142       /// @brief Virtual destructor
143       virtual ~CompilerDriver();
144
145     /// @}
146     /// @name Methods
147     /// @{
148     public:
149       /// @brief Execute the actions requested for the given input list.
150       virtual int execute(const InputList& list, const sys::Path& output) = 0;
151
152       /// @brief Set the final phase at which compilation terminates
153       virtual void setFinalPhase(Phases phase) = 0;
154
155       /// @brief Set the optimization level for the compilation
156       virtual void setOptimization(OptimizationLevels level) = 0;
157
158       /// @brief Set the driver flags.
159       virtual void setDriverFlags(unsigned flags) = 0;
160
161       /// @brief Set the output machine name.
162       virtual void setOutputMachine(const std::string& machineName) = 0;
163
164       /// @brief Set the options for a given phase.
165       virtual void setPhaseArgs(Phases phase, const StringVector& opts) = 0; 
166
167       /// @brief Set Library Paths
168       virtual void setIncludePaths(const StringVector& paths) = 0;
169
170       /// @brief Set Library Paths
171       virtual void setSymbolDefines(const StringVector& paths) = 0;
172
173       /// @brief Set Library Paths
174       virtual void setLibraryPaths(const StringVector& paths) = 0;
175
176       /// @brief Add a path to the list of library paths
177       virtual void addLibraryPath( const sys::Path& libPath )  = 0;
178
179       /// @brief Add a path to the list of paths in which to find tools
180       virtual void addToolPath( const sys::Path& toolPath) = 0;
181
182       /// @brief Set the list of -f options to be passed through
183       virtual void setfPassThrough(const StringVector& fOpts) = 0;
184
185       /// @brief Set the list of -M options to be passed through
186       virtual void setMPassThrough(const StringVector& fOpts) = 0;
187
188       /// @brief Set the list of -W options to be passed through
189       virtual void setWPassThrough(const StringVector& fOpts) = 0;
190
191     /// @}
192   };
193 }
194
195 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
196 #endif