blob: e5bf42fda8faad224927ece9383c956518f9b0a5 [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
Reid Spencerbae68252004-08-19 04:49:47 +000035 /// @brief A table of strings, indexed typically by Phases
36 typedef std::vector<StringVector> StringTable;
37
Reid Spencerbf437722004-08-15 08:19:46 +000038 /// @brief The phases of processing that llvmc understands
Reid Spencerabf1ce32004-08-10 16:29:18 +000039 enum Phases {
40 PREPROCESSING, ///< Source language combining, filtering, substitution
41 TRANSLATION, ///< Translate source -> LLVM bytecode/assembly
42 OPTIMIZATION, ///< Optimize translation result
Reid Spencerabf1ce32004-08-10 16:29:18 +000043 ASSEMBLY, ///< Convert program to executable
Reid Spencerbae68252004-08-19 04:49:47 +000044 LINKING, ///< Link bytecode and native code
45 NUM_PHASES ///< Always last!
Reid Spencerabf1ce32004-08-10 16:29:18 +000046 };
47
Reid Spencerbf437722004-08-15 08:19:46 +000048 /// @brief The levels of optimization llvmc understands
Reid Spencerabf1ce32004-08-10 16:29:18 +000049 enum OptimizationLevels {
Reid Spencer5c56dc12004-08-13 20:22:43 +000050 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 Spencerbf437722004-08-15 08:19:46 +000054 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?
61 GROKS_DASH_O_FLAG = 0x0002, ///< Understands the -On options?
62 PREPROCESSES_FLAG = 0x0004, ///< Does this action preprocess?
Reid Spencerbae68252004-08-19 04:49:47 +000063 TRANSLATES_FLAG = 0x0008, ///< Does this action translate?
64 OPTIMIZES_FLAG = 0x0010, ///< Does this action optimize?
65 OUTPUT_IS_ASM_FLAG = 0x0020, ///< Action produces .ll files?
66 FLAGS_MASK = 0x003F, ///< Union of all flags
Reid Spencer5c56dc12004-08-13 20:22:43 +000067 };
68
69 /// This type is the input list to the CompilerDriver. It provides
70 /// a vector of filename/filetype pairs. The filetype is used to look up
71 /// the configuration of the actions to be taken by the driver.
72 /// @brief The Input Data to the execute method
Reid Spencer2a069fa2004-08-16 07:06:38 +000073 typedef std::vector<std::pair<std::string,std::string> > InputList;
Reid Spencer5c56dc12004-08-13 20:22:43 +000074
75 /// This type is read from configuration files or otherwise provided to
76 /// the CompilerDriver through a "ConfigDataProvider". It serves as both
77 /// the template of what to do and the actual Action to be executed.
78 /// @brief A structure to hold the action data for a given source
79 /// language.
80 struct Action {
Reid Spencerbae68252004-08-19 04:49:47 +000081 Action() : flags(0) {}
Reid Spencer2a069fa2004-08-16 07:06:38 +000082 std::string program; ///< The program to execve
Reid Spencerbf437722004-08-15 08:19:46 +000083 StringVector args; ///< Arguments to the program
Reid Spencerbf437722004-08-15 08:19:46 +000084 unsigned flags; ///< Action specific flags
85 void set(unsigned fl ) { flags |= fl; }
86 void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); }
Reid Spencerbae68252004-08-19 04:49:47 +000087 bool isSet(unsigned fl) { return (flags&fl) != 0; }
Reid Spencer5c56dc12004-08-13 20:22:43 +000088 };
89
90 struct ConfigData {
Reid Spencerbae68252004-08-19 04:49:47 +000091 ConfigData();
Reid Spencer59a745a2004-08-22 18:03:25 +000092 std::string version; ///< The version number.
Reid Spencerbae68252004-08-19 04:49:47 +000093 std::string langName; ///< The name of the source language
94 StringTable opts; ///< The o10n options for each level
95 Action PreProcessor; ///< PreProcessor command line
96 Action Translator; ///< Translator command line
97 Action Optimizer; ///< Optimizer command line
98 Action Assembler; ///< Assembler command line
99 Action Linker; ///< Linker command line
Reid Spencer5c56dc12004-08-13 20:22:43 +0000100 };
101
102 /// This pure virtual interface class defines the interface between the
103 /// CompilerDriver and other software that provides ConfigData objects to
104 /// it. The CompilerDriver must be configured to use an object of this
105 /// type so it can obtain the configuration data.
106 /// @see setConfigDataProvider
107 /// @brief Configuration Data Provider interface
108 class ConfigDataProvider {
109 public:
110 virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
111 virtual void setConfigDir(const std::string& dirName) = 0;
Reid Spencerabf1ce32004-08-10 16:29:18 +0000112 };
113
114 /// @}
115 /// @name Constructors
116 /// @{
117 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000118 CompilerDriver(ConfigDataProvider& cdp );
119 virtual ~CompilerDriver();
Reid Spencerabf1ce32004-08-10 16:29:18 +0000120
121 /// @}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000122 /// @name Methods
Reid Spencerabf1ce32004-08-10 16:29:18 +0000123 /// @{
124 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000125 /// @brief Handle an error
126 virtual void error(const std::string& errmsg);
127
128 /// @brief Execute the actions requested for the given input list.
Reid Spencer2a069fa2004-08-16 07:06:38 +0000129 virtual int execute(const InputList& list, const std::string& output);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000130
131 /// @}
132 /// @name Mutators
133 /// @{
134 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000135 /// @brief Set the final phase at which compilation terminates
136 void setFinalPhase( Phases phase ) { finalPhase = phase; }
137
Reid Spencerabf1ce32004-08-10 16:29:18 +0000138 /// @brief Set the optimization level for the compilation
Reid Spencer5c56dc12004-08-13 20:22:43 +0000139 void setOptimization( OptimizationLevels level ) { optLevel = level; }
140
141 /// @brief Prevent the CompilerDriver from taking any actions
142 void setDryRun( bool TF ) { isDryRun = TF; }
143
144 /// @brief Cause the CompilerDriver to print to stderr all the
145 /// actions it is taking.
146 void setVerbose( bool TF ) { isVerbose = TF; }
147
148 /// @brief Cause the CompilerDriver to print to stderr very verbose
149 /// information that might be useful in debugging the driver's actions
150 void setDebug( bool TF ) { isDebug = TF; }
151
152 /// @brief Cause the CompilerDriver to print to stderr the
153 /// execution time of each action taken.
154 void setTimeActions( bool TF ) { timeActions = TF; }
155
Reid Spencerbae68252004-08-19 04:49:47 +0000156 /// @brief Cause the CompilerDriver to print timings for each pass.
157 void setTimePasses( bool TF ) { timePasses = TF; }
158
159 /// @brief Cause the CompilerDriver to show statistics gathered
160 void setShowStats( bool TF ) { showStats = TF; }
161
Reid Spencer5c56dc12004-08-13 20:22:43 +0000162 /// @brief Indicate that native code is to be generated instead
163 /// of LLVM bytecode.
164 void setEmitNativeCode( bool TF ) { emitNativeCode = TF; }
165
166 /// @brief Indicate that raw, unoptimized code is to be generated.
167 void setEmitRawCode(bool TF ) { emitRawCode = TF; }
168
Reid Spencerbae68252004-08-19 04:49:47 +0000169 void setKeepTemporaries(bool TF) { keepTemps = TF; }
170
Reid Spencer5c56dc12004-08-13 20:22:43 +0000171 /// @brief Set the output machine name.
172 void setOutputMachine( const std::string& machineName ) {
173 machine = machineName;
174 }
175
Reid Spencerbf437722004-08-15 08:19:46 +0000176 /// @brief Set Preprocessor specific options
Reid Spencerbae68252004-08-19 04:49:47 +0000177 void setPhaseArgs(Phases phase, const std::vector<std::string>& opts) {
178 assert(phase <= LINKING && phase >= PREPROCESSING);
179 AdditionalArgs[phase] = opts;
Reid Spencerbf437722004-08-15 08:19:46 +0000180 }
181
182 /// @brief Set Library Paths
183 void setLibraryPaths(const std::vector<std::string>& paths) {
184 LibraryPaths = paths;
185 }
186
Reid Spencer5c56dc12004-08-13 20:22:43 +0000187 /// @brief Set the list of library paths to be searched for
188 /// libraries.
189 void addLibraryPath( const std::string& libPath ) {
Reid Spencerbf437722004-08-15 08:19:46 +0000190 LibraryPaths.push_back(libPath);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000191 }
192
193 /// @}
194 /// @name Functions
195 /// @{
196 private:
197 Action* GetAction(ConfigData* cd, const std::string& input,
198 const std::string& output, Phases phase );
Reid Spencerbae68252004-08-19 04:49:47 +0000199 bool DoAction(Action* a);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000200
201 /// @}
202 /// @name Data
203 /// @{
Reid Spencer5c56dc12004-08-13 20:22:43 +0000204 private:
205 ConfigDataProvider* cdp; ///< Where we get configuration data from
206 Phases finalPhase; ///< The final phase of compilation
207 OptimizationLevels optLevel; ///< The optimization level to apply
208 bool isDryRun; ///< Prevent actions ?
209 bool isVerbose; ///< Print actions?
210 bool isDebug; ///< Print lotsa debug info?
211 bool timeActions; ///< Time the actions executed ?
Reid Spencerbae68252004-08-19 04:49:47 +0000212 bool timePasses; ///< Time each pass and print timing ?
213 bool showStats; ///< Show gathered statistics ?
Reid Spencer5c56dc12004-08-13 20:22:43 +0000214 bool emitRawCode; ///< Emit Raw (unoptimized) code?
215 bool emitNativeCode; ///< Emit native code instead of bytecode?
Reid Spencerbae68252004-08-19 04:49:47 +0000216 bool keepTemps; ///< Keep temporary files?
Reid Spencer5c56dc12004-08-13 20:22:43 +0000217 std::string machine; ///< Target machine name
Reid Spencerbae68252004-08-19 04:49:47 +0000218 StringVector LibraryPaths; ///< -L options
219 StringTable AdditionalArgs; ///< The -Txyz options
220 std::string TempDir; ///< Name of the temporary directory.
Reid Spencerabf1ce32004-08-10 16:29:18 +0000221
222 /// @}
223
224 };
225}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000226
227// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
228#endif