blob: f2be5cc8cfc7d5dbd234fbd77be8349560928206 [file] [log] [blame]
Reid Spencer5c56dc12004-08-13 20:22:43 +00001//===- CompilerDriver.h - Compiler Driver -----------------------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerabf1ce32004-08-10 16:29:18 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman3da94ae2005-04-22 00:00:37 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerabf1ce32004-08-10 16:29:18 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencerabf1ce32004-08-10 16:29:18 +00008//===----------------------------------------------------------------------===//
9//
10// This file declares the CompilerDriver class which implements the bulk of the
11// LLVM Compiler Driver program (llvmc).
12//
Chris Lattner74f48d12006-05-29 18:52:05 +000013//===----------------------------------------------------------------------===//
14
Reid Spencer5c56dc12004-08-13 20:22:43 +000015#ifndef LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
16#define LLVM_TOOLS_LLVMC_COMPILERDRIVER_H
17
18#include <string>
19#include <vector>
Reid Spencer52c2dc12004-08-29 19:26:56 +000020#include "llvm/System/Program.h"
Reid Spencerabf1ce32004-08-10 16:29:18 +000021
22namespace llvm {
23 /// This class provides the high level interface to the LLVM Compiler Driver.
24 /// The driver's purpose is to make it easier for compiler writers and users
25 /// of LLVM to utilize the compiler toolkits and LLVM toolset by learning only
26 /// the interface of one program (llvmc).
Misha Brukman3da94ae2005-04-22 00:00:37 +000027 ///
Reid Spencerabf1ce32004-08-10 16:29:18 +000028 /// @see llvmc.cpp
29 /// @brief The interface to the LLVM Compiler Driver.
30 class CompilerDriver {
31 /// @name Types
32 /// @{
33 public:
Reid Spencer52c2dc12004-08-29 19:26:56 +000034 /// @brief A vector of strings, used for argument lists
Reid Spencerbf437722004-08-15 08:19:46 +000035 typedef std::vector<std::string> StringVector;
36
Reid Spencer52c2dc12004-08-29 19:26:56 +000037 /// @brief A vector of sys::Path, used for path lists
38 typedef std::vector<sys::Path> PathVector;
39
Reid Spencerbae68252004-08-19 04:49:47 +000040 /// @brief A table of strings, indexed typically by Phases
41 typedef std::vector<StringVector> StringTable;
42
Reid Spencerbf437722004-08-15 08:19:46 +000043 /// @brief The phases of processing that llvmc understands
Reid Spencerabf1ce32004-08-10 16:29:18 +000044 enum Phases {
45 PREPROCESSING, ///< Source language combining, filtering, substitution
Gabor Greifa99be512007-07-05 17:07:56 +000046 TRANSLATION, ///< Translate source -> LLVM bitcode/assembly
Misha Brukman3da94ae2005-04-22 00:00:37 +000047 OPTIMIZATION, ///< Optimize translation result
Reid Spencerabf1ce32004-08-10 16:29:18 +000048 ASSEMBLY, ///< Convert program to executable
Gabor Greifa99be512007-07-05 17:07:56 +000049 LINKING, ///< Link bitcode and native code
Reid Spencerbae68252004-08-19 04:49:47 +000050 NUM_PHASES ///< Always last!
Reid Spencerabf1ce32004-08-10 16:29:18 +000051 };
52
Reid Spencerbf437722004-08-15 08:19:46 +000053 /// @brief The levels of optimization llvmc understands
Reid Spencerabf1ce32004-08-10 16:29:18 +000054 enum OptimizationLevels {
Reid Spencer5c56dc12004-08-13 20:22:43 +000055 OPT_FAST_COMPILE, ///< Optimize to make >compile< go faster
56 OPT_SIMPLE, ///< Standard/simple optimizations
57 OPT_AGGRESSIVE, ///< Aggressive optimizations
58 OPT_LINK_TIME, ///< Aggressive + LinkTime optimizations
Reid Spencerbf437722004-08-15 08:19:46 +000059 OPT_AGGRESSIVE_LINK_TIME, ///< Make it go way fast!
60 OPT_NONE ///< No optimizations. Keep this at the end!
61 };
62
63 /// @brief Action specific flags
64 enum ConfigurationFlags {
65 REQUIRED_FLAG = 0x0001, ///< Should the action always be run?
Reid Spencerfd2f7282004-08-24 13:49:57 +000066 PREPROCESSES_FLAG = 0x0002, ///< Does this action preprocess?
67 TRANSLATES_FLAG = 0x0004, ///< Does this action translate?
68 OUTPUT_IS_ASM_FLAG = 0x0008, ///< Action produces .ll files?
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000069 FLAGS_MASK = 0x000F ///< Union of all flags
Reid Spencer5c56dc12004-08-13 20:22:43 +000070 };
71
72 /// This type is the input list to the CompilerDriver. It provides
Reid Spencer52c2dc12004-08-29 19:26:56 +000073 /// a vector of pathname/filetype pairs. The filetype is used to look up
Reid Spencer5c56dc12004-08-13 20:22:43 +000074 /// the configuration of the actions to be taken by the driver.
75 /// @brief The Input Data to the execute method
Reid Spencer52c2dc12004-08-29 19:26:56 +000076 typedef std::vector<std::pair<sys::Path,std::string> > InputList;
Reid Spencer5c56dc12004-08-13 20:22:43 +000077
78 /// This type is read from configuration files or otherwise provided to
79 /// the CompilerDriver through a "ConfigDataProvider". It serves as both
80 /// the template of what to do and the actual Action to be executed.
81 /// @brief A structure to hold the action data for a given source
82 /// language.
83 struct Action {
Reid Spencerbae68252004-08-19 04:49:47 +000084 Action() : flags(0) {}
Reid Spencerf6358c72004-12-19 18:00:56 +000085 sys::Path program; ///< The program to execve
86 StringVector args; ///< Arguments to the program
87 unsigned flags; ///< Action specific flags
Reid Spencerbf437722004-08-15 08:19:46 +000088 void set(unsigned fl ) { flags |= fl; }
89 void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); }
Reid Spencerbae68252004-08-19 04:49:47 +000090 bool isSet(unsigned fl) { return (flags&fl) != 0; }
Reid Spencer5c56dc12004-08-13 20:22:43 +000091 };
92
93 struct ConfigData {
Reid Spencerbae68252004-08-19 04:49:47 +000094 ConfigData();
Reid Spencer59a745a2004-08-22 18:03:25 +000095 std::string version; ///< The version number.
Misha Brukman3da94ae2005-04-22 00:00:37 +000096 std::string langName; ///< The name of the source language
Reid Spencerbae68252004-08-19 04:49:47 +000097 StringTable opts; ///< The o10n options for each level
Reid Spencer2d3ee4e2004-11-23 23:40:06 +000098 StringVector libpaths; ///< The library paths
Reid Spencerbae68252004-08-19 04:49:47 +000099 Action PreProcessor; ///< PreProcessor command line
100 Action Translator; ///< Translator command line
101 Action Optimizer; ///< Optimizer command line
102 Action Assembler; ///< Assembler command line
103 Action Linker; ///< Linker command line
Reid Spencer5c56dc12004-08-13 20:22:43 +0000104 };
105
106 /// This pure virtual interface class defines the interface between the
107 /// CompilerDriver and other software that provides ConfigData objects to
108 /// it. The CompilerDriver must be configured to use an object of this
Misha Brukman3da94ae2005-04-22 00:00:37 +0000109 /// type so it can obtain the configuration data.
Reid Spencer5c56dc12004-08-13 20:22:43 +0000110 /// @see setConfigDataProvider
111 /// @brief Configuration Data Provider interface
112 class ConfigDataProvider {
113 public:
Reid Spencercc97cfc2005-05-19 00:52:28 +0000114 virtual ~ConfigDataProvider();
Reid Spencer5c56dc12004-08-13 20:22:43 +0000115 virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000116 virtual void setConfigDir(const sys::Path& dirName) = 0;
Reid Spencerabf1ce32004-08-10 16:29:18 +0000117 };
118
Reid Spencer54fafe42004-09-14 01:58:45 +0000119 /// These flags control various actions of the compiler driver. They are
120 /// used by adding the needed flag values together and passing them to the
Misha Brukman3da94ae2005-04-22 00:00:37 +0000121 /// compiler driver's setDriverFlags method.
Reid Spencer54fafe42004-09-14 01:58:45 +0000122 /// @see setDriverFlags
123 /// @brief Driver specific flags
124 enum DriverFlags {
125 DRY_RUN_FLAG = 0x0001, ///< Do everything but execute actions
Reid Spencer3a9b2222004-10-28 04:04:38 +0000126 VERBOSE_FLAG = 0x0002, ///< Print each action
127 DEBUG_FLAG = 0x0004, ///< Print debug information
128 TIME_PASSES_FLAG = 0x0008, ///< Time the passes as they execute
129 TIME_ACTIONS_FLAG = 0x0010, ///< Time the actions as they execute
130 SHOW_STATS_FLAG = 0x0020, ///< Show pass statistics
131 EMIT_NATIVE_FLAG = 0x0040, ///< Emit native code instead of bc
Gabor Greifa99be512007-07-05 17:07:56 +0000132 EMIT_RAW_FLAG = 0x0080, ///< Emit raw, unoptimized bitcode
Reid Spencer3a9b2222004-10-28 04:04:38 +0000133 KEEP_TEMPS_FLAG = 0x0100, ///< Don't delete temporary files
134 STRIP_OUTPUT_FLAG = 0x0200, ///< Strip symbols from linked output
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000135 DRIVER_FLAGS_MASK = 0x03FF ///< Union of the above flags
Reid Spencer54fafe42004-09-14 01:58:45 +0000136 };
137
Reid Spencerabf1ce32004-08-10 16:29:18 +0000138 /// @}
139 /// @name Constructors
140 /// @{
141 public:
Reid Spencer52c2dc12004-08-29 19:26:56 +0000142 /// @brief Static Constructor
143 static CompilerDriver* Get(ConfigDataProvider& CDP);
144
145 /// @brief Virtual destructor
Reid Spencer5c56dc12004-08-13 20:22:43 +0000146 virtual ~CompilerDriver();
Reid Spencerabf1ce32004-08-10 16:29:18 +0000147
148 /// @}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000149 /// @name Methods
Reid Spencerabf1ce32004-08-10 16:29:18 +0000150 /// @{
151 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000152 /// @brief Execute the actions requested for the given input list.
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000153 virtual int execute(
154 const InputList& list, const sys::Path& output, std::string& ErrMsg) =0;
Reid Spencerabf1ce32004-08-10 16:29:18 +0000155
Reid Spencer5c56dc12004-08-13 20:22:43 +0000156 /// @brief Set the final phase at which compilation terminates
Reid Spencer52c2dc12004-08-29 19:26:56 +0000157 virtual void setFinalPhase(Phases phase) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000158
Reid Spencerabf1ce32004-08-10 16:29:18 +0000159 /// @brief Set the optimization level for the compilation
Reid Spencer52c2dc12004-08-29 19:26:56 +0000160 virtual void setOptimization(OptimizationLevels level) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000161
Reid Spencer52c2dc12004-08-29 19:26:56 +0000162 /// @brief Set the driver flags.
163 virtual void setDriverFlags(unsigned flags) = 0;
Reid Spencerbae68252004-08-19 04:49:47 +0000164
Reid Spencer5c56dc12004-08-13 20:22:43 +0000165 /// @brief Set the output machine name.
Reid Spencer52c2dc12004-08-29 19:26:56 +0000166 virtual void setOutputMachine(const std::string& machineName) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000167
Reid Spencer07adb282004-11-05 22:15:36 +0000168 /// @brief Set the options for a given phase.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000169 virtual void setPhaseArgs(Phases phase, const StringVector& opts) = 0;
Reid Spencerbf437722004-08-15 08:19:46 +0000170
171 /// @brief Set Library Paths
Reid Spencerca01f9b2004-08-30 06:29:06 +0000172 virtual void setIncludePaths(const StringVector& paths) = 0;
173
174 /// @brief Set Library Paths
175 virtual void setSymbolDefines(const StringVector& paths) = 0;
176
177 /// @brief Set Library Paths
Reid Spencer52c2dc12004-08-29 19:26:56 +0000178 virtual void setLibraryPaths(const StringVector& paths) = 0;
Reid Spencerbf437722004-08-15 08:19:46 +0000179
Reid Spencer07adb282004-11-05 22:15:36 +0000180 /// @brief Add a path to the list of library paths
Reid Spencer52c2dc12004-08-29 19:26:56 +0000181 virtual void addLibraryPath( const sys::Path& libPath ) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000182
Reid Spencer07adb282004-11-05 22:15:36 +0000183 /// @brief Add a path to the list of paths in which to find tools
184 virtual void addToolPath( const sys::Path& toolPath) = 0;
185
Reid Spencer54fafe42004-09-14 01:58:45 +0000186 /// @brief Set the list of -f options to be passed through
187 virtual void setfPassThrough(const StringVector& fOpts) = 0;
188
189 /// @brief Set the list of -M options to be passed through
190 virtual void setMPassThrough(const StringVector& fOpts) = 0;
191
192 /// @brief Set the list of -W options to be passed through
193 virtual void setWPassThrough(const StringVector& fOpts) = 0;
194
Reid Spencer2d3ee4e2004-11-23 23:40:06 +0000195 /// @brief Determine where a linkage file is located in the file system
196 virtual sys::Path GetPathForLinkageItem(
197 const std::string& link_item, ///< Item to be sought
198 bool native = false ///< Looking for native?
199 ) = 0;
200
Reid Spencer5c56dc12004-08-13 20:22:43 +0000201 /// @}
Reid Spencerabf1ce32004-08-10 16:29:18 +0000202 };
203}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000204
205// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
206#endif