blob: 0914feca4c5e2c8211a8b371a3a8b6a774da6e67 [file] [log] [blame]
Reid Spencer5c56dc12004-08-13 20:22:43 +00001//===- CompilerDriver.h - Compiler Driver -----------------------*- C++ -*-===//
Reid Spencerabf1ce32004-08-10 16:29:18 +00002//
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 Spencer5c56dc12004-08-13 20:22:43 +000014#ifndef LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
15#define LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
16
17#include <string>
18#include <vector>
Reid Spencerabf1ce32004-08-10 16:29:18 +000019
20namespace 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 Spencerbf437722004-08-15 08:19:46 +000032 /// @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 Spencerabf1ce32004-08-10 16:29:18 +000036 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 Spencerbf437722004-08-15 08:19:46 +000044 /// @brief The levels of optimization llvmc understands
Reid Spencerabf1ce32004-08-10 16:29:18 +000045 enum OptimizationLevels {
Reid Spencer5c56dc12004-08-13 20:22:43 +000046 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 Spencerbf437722004-08-15 08:19:46 +000050 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 Spencer5c56dc12004-08-13 20:22:43 +000062 };
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 Spencer2a069fa2004-08-16 07:06:38 +000068 typedef std::vector<std::pair<std::string,std::string> > InputList;
Reid Spencer5c56dc12004-08-13 20:22:43 +000069
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 Spencerbf437722004-08-15 08:19:46 +000076 Action() : inputAt(0) , outputAt(0), flags(0) {}
Reid Spencer2a069fa2004-08-16 07:06:38 +000077 std::string program; ///< The program to execve
Reid Spencerbf437722004-08-15 08:19:46 +000078 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 Spencer5c56dc12004-08-13 20:22:43 +000085 };
86
87 struct ConfigData {
Reid Spencerbf437722004-08-15 08:19:46 +000088 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 Spencer5c56dc12004-08-13 20:22:43 +000095 };
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 Spencerabf1ce32004-08-10 16:29:18 +0000107 };
108
109 /// @}
110 /// @name Constructors
111 /// @{
112 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000113 CompilerDriver(ConfigDataProvider& cdp );
114 virtual ~CompilerDriver();
Reid Spencerabf1ce32004-08-10 16:29:18 +0000115
116 /// @}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000117 /// @name Methods
Reid Spencerabf1ce32004-08-10 16:29:18 +0000118 /// @{
119 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000120 /// @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 Spencer2a069fa2004-08-16 07:06:38 +0000124 virtual int execute(const InputList& list, const std::string& output);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000125
126 /// @}
127 /// @name Mutators
128 /// @{
129 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000130 /// @brief Set the final phase at which compilation terminates
131 void setFinalPhase( Phases phase ) { finalPhase = phase; }
132
Reid Spencerabf1ce32004-08-10 16:29:18 +0000133 /// @brief Set the optimization level for the compilation
Reid Spencer5c56dc12004-08-13 20:22:43 +0000134 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 Spencerbf437722004-08-15 08:19:46 +0000163 /// @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 Spencer5c56dc12004-08-13 20:22:43 +0000193 /// @brief Set the list of library paths to be searched for
194 /// libraries.
195 void addLibraryPath( const std::string& libPath ) {
Reid Spencerbf437722004-08-15 08:19:46 +0000196 LibraryPaths.push_back(libPath);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000197 }
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 Spencer5c56dc12004-08-13 20:22:43 +0000205 void DoAction(Action* a);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000206
207 /// @}
208 /// @name Data
209 /// @{
Reid Spencer5c56dc12004-08-13 20:22:43 +0000210 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 Spencerbf437722004-08-15 08:19:46 +0000221 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 Spencerabf1ce32004-08-10 16:29:18 +0000227
228 /// @}
229
230 };
231}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000232
233// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
234#endif