blob: 092685f53bc9ae1817352bee6c97da96f8ab4336 [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?
Reid Spencerfd2f7282004-08-24 13:49:57 +000061 PREPROCESSES_FLAG = 0x0002, ///< Does this action preprocess?
62 TRANSLATES_FLAG = 0x0004, ///< Does this action translate?
63 OUTPUT_IS_ASM_FLAG = 0x0008, ///< Action produces .ll files?
64 FLAGS_MASK = 0x000F, ///< Union of all flags
Reid Spencer5c56dc12004-08-13 20:22:43 +000065 };
66
67 /// This type is the input list to the CompilerDriver. It provides
68 /// a vector of filename/filetype pairs. The filetype is used to look up
69 /// the configuration of the actions to be taken by the driver.
70 /// @brief The Input Data to the execute method
Reid Spencer2a069fa2004-08-16 07:06:38 +000071 typedef std::vector<std::pair<std::string,std::string> > InputList;
Reid Spencer5c56dc12004-08-13 20:22:43 +000072
73 /// This type is read from configuration files or otherwise provided to
74 /// the CompilerDriver through a "ConfigDataProvider". It serves as both
75 /// the template of what to do and the actual Action to be executed.
76 /// @brief A structure to hold the action data for a given source
77 /// language.
78 struct Action {
Reid Spencerbae68252004-08-19 04:49:47 +000079 Action() : flags(0) {}
Reid Spencer2a069fa2004-08-16 07:06:38 +000080 std::string program; ///< The program to execve
Reid Spencerbf437722004-08-15 08:19:46 +000081 StringVector args; ///< Arguments to the program
Reid Spencerbf437722004-08-15 08:19:46 +000082 unsigned flags; ///< Action specific flags
83 void set(unsigned fl ) { flags |= fl; }
84 void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); }
Reid Spencerbae68252004-08-19 04:49:47 +000085 bool isSet(unsigned fl) { return (flags&fl) != 0; }
Reid Spencer5c56dc12004-08-13 20:22:43 +000086 };
87
88 struct ConfigData {
Reid Spencerbae68252004-08-19 04:49:47 +000089 ConfigData();
Reid Spencer59a745a2004-08-22 18:03:25 +000090 std::string version; ///< The version number.
Reid Spencerbae68252004-08-19 04:49:47 +000091 std::string langName; ///< The name of the source language
92 StringTable opts; ///< The o10n options for each level
93 Action PreProcessor; ///< PreProcessor command line
94 Action Translator; ///< Translator command line
95 Action Optimizer; ///< Optimizer command line
96 Action Assembler; ///< Assembler command line
97 Action Linker; ///< Linker command line
Reid Spencer5c56dc12004-08-13 20:22:43 +000098 };
99
100 /// This pure virtual interface class defines the interface between the
101 /// CompilerDriver and other software that provides ConfigData objects to
102 /// it. The CompilerDriver must be configured to use an object of this
103 /// type so it can obtain the configuration data.
104 /// @see setConfigDataProvider
105 /// @brief Configuration Data Provider interface
106 class ConfigDataProvider {
107 public:
108 virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
109 virtual void setConfigDir(const std::string& dirName) = 0;
Reid Spencerabf1ce32004-08-10 16:29:18 +0000110 };
111
112 /// @}
113 /// @name Constructors
114 /// @{
115 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000116 CompilerDriver(ConfigDataProvider& cdp );
117 virtual ~CompilerDriver();
Reid Spencerabf1ce32004-08-10 16:29:18 +0000118
119 /// @}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000120 /// @name Methods
Reid Spencerabf1ce32004-08-10 16:29:18 +0000121 /// @{
122 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000123 /// @brief Handle an error
124 virtual void error(const std::string& errmsg);
125
126 /// @brief Execute the actions requested for the given input list.
Reid Spencer2a069fa2004-08-16 07:06:38 +0000127 virtual int execute(const InputList& list, const std::string& output);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000128
129 /// @}
130 /// @name Mutators
131 /// @{
132 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000133 /// @brief Set the final phase at which compilation terminates
134 void setFinalPhase( Phases phase ) { finalPhase = phase; }
135
Reid Spencerabf1ce32004-08-10 16:29:18 +0000136 /// @brief Set the optimization level for the compilation
Reid Spencer5c56dc12004-08-13 20:22:43 +0000137 void setOptimization( OptimizationLevels level ) { optLevel = level; }
138
139 /// @brief Prevent the CompilerDriver from taking any actions
140 void setDryRun( bool TF ) { isDryRun = TF; }
141
142 /// @brief Cause the CompilerDriver to print to stderr all the
143 /// actions it is taking.
144 void setVerbose( bool TF ) { isVerbose = TF; }
145
146 /// @brief Cause the CompilerDriver to print to stderr very verbose
147 /// information that might be useful in debugging the driver's actions
148 void setDebug( bool TF ) { isDebug = TF; }
149
150 /// @brief Cause the CompilerDriver to print to stderr the
151 /// execution time of each action taken.
152 void setTimeActions( bool TF ) { timeActions = TF; }
153
Reid Spencerbae68252004-08-19 04:49:47 +0000154 /// @brief Cause the CompilerDriver to print timings for each pass.
155 void setTimePasses( bool TF ) { timePasses = TF; }
156
157 /// @brief Cause the CompilerDriver to show statistics gathered
158 void setShowStats( bool TF ) { showStats = TF; }
159
Reid Spencer5c56dc12004-08-13 20:22:43 +0000160 /// @brief Indicate that native code is to be generated instead
161 /// of LLVM bytecode.
162 void setEmitNativeCode( bool TF ) { emitNativeCode = TF; }
163
164 /// @brief Indicate that raw, unoptimized code is to be generated.
165 void setEmitRawCode(bool TF ) { emitRawCode = TF; }
166
Reid Spencerbae68252004-08-19 04:49:47 +0000167 void setKeepTemporaries(bool TF) { keepTemps = TF; }
168
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169 /// @brief Set the output machine name.
170 void setOutputMachine( const std::string& machineName ) {
171 machine = machineName;
172 }
173
Reid Spencerbf437722004-08-15 08:19:46 +0000174 /// @brief Set Preprocessor specific options
Reid Spencerbae68252004-08-19 04:49:47 +0000175 void setPhaseArgs(Phases phase, const std::vector<std::string>& opts) {
176 assert(phase <= LINKING && phase >= PREPROCESSING);
177 AdditionalArgs[phase] = opts;
Reid Spencerbf437722004-08-15 08:19:46 +0000178 }
179
180 /// @brief Set Library Paths
181 void setLibraryPaths(const std::vector<std::string>& paths) {
182 LibraryPaths = paths;
183 }
184
Reid Spencer5c56dc12004-08-13 20:22:43 +0000185 /// @brief Set the list of library paths to be searched for
186 /// libraries.
187 void addLibraryPath( const std::string& libPath ) {
Reid Spencerbf437722004-08-15 08:19:46 +0000188 LibraryPaths.push_back(libPath);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000189 }
190
191 /// @}
192 /// @name Functions
193 /// @{
194 private:
195 Action* GetAction(ConfigData* cd, const std::string& input,
196 const std::string& output, Phases phase );
Reid Spencerbae68252004-08-19 04:49:47 +0000197 bool DoAction(Action* a);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000198
199 /// @}
200 /// @name Data
201 /// @{
Reid Spencer5c56dc12004-08-13 20:22:43 +0000202 private:
203 ConfigDataProvider* cdp; ///< Where we get configuration data from
204 Phases finalPhase; ///< The final phase of compilation
205 OptimizationLevels optLevel; ///< The optimization level to apply
206 bool isDryRun; ///< Prevent actions ?
207 bool isVerbose; ///< Print actions?
208 bool isDebug; ///< Print lotsa debug info?
209 bool timeActions; ///< Time the actions executed ?
Reid Spencerbae68252004-08-19 04:49:47 +0000210 bool timePasses; ///< Time each pass and print timing ?
211 bool showStats; ///< Show gathered statistics ?
Reid Spencer5c56dc12004-08-13 20:22:43 +0000212 bool emitRawCode; ///< Emit Raw (unoptimized) code?
213 bool emitNativeCode; ///< Emit native code instead of bytecode?
Reid Spencerbae68252004-08-19 04:49:47 +0000214 bool keepTemps; ///< Keep temporary files?
Reid Spencer5c56dc12004-08-13 20:22:43 +0000215 std::string machine; ///< Target machine name
Reid Spencerbae68252004-08-19 04:49:47 +0000216 StringVector LibraryPaths; ///< -L options
217 StringTable AdditionalArgs; ///< The -Txyz options
218 std::string TempDir; ///< Name of the temporary directory.
Reid Spencerabf1ce32004-08-10 16:29:18 +0000219
220 /// @}
221
222 };
223}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000224
225// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
226#endif