blob: ea999b44d999f557ae639990fa82cc06bc65e0b3 [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 Spencer52c2dc12004-08-29 19:26:56 +000019#include "llvm/System/Program.h"
Reid Spencerabf1ce32004-08-10 16:29:18 +000020
21namespace 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:
Reid Spencer52c2dc12004-08-29 19:26:56 +000033 /// @brief A vector of strings, used for argument lists
Reid Spencerbf437722004-08-15 08:19:46 +000034 typedef std::vector<std::string> StringVector;
35
Reid Spencer52c2dc12004-08-29 19:26:56 +000036 /// @brief A vector of sys::Path, used for path lists
37 typedef std::vector<sys::Path> PathVector;
38
Reid Spencerbae68252004-08-19 04:49:47 +000039 /// @brief A table of strings, indexed typically by Phases
40 typedef std::vector<StringVector> StringTable;
41
Reid Spencerbf437722004-08-15 08:19:46 +000042 /// @brief The phases of processing that llvmc understands
Reid Spencerabf1ce32004-08-10 16:29:18 +000043 enum Phases {
44 PREPROCESSING, ///< Source language combining, filtering, substitution
45 TRANSLATION, ///< Translate source -> LLVM bytecode/assembly
46 OPTIMIZATION, ///< Optimize translation result
Reid Spencerabf1ce32004-08-10 16:29:18 +000047 ASSEMBLY, ///< Convert program to executable
Reid Spencerbae68252004-08-19 04:49:47 +000048 LINKING, ///< Link bytecode and native code
49 NUM_PHASES ///< Always last!
Reid Spencerabf1ce32004-08-10 16:29:18 +000050 };
51
Reid Spencerbf437722004-08-15 08:19:46 +000052 /// @brief The levels of optimization llvmc understands
Reid Spencerabf1ce32004-08-10 16:29:18 +000053 enum OptimizationLevels {
Reid Spencer5c56dc12004-08-13 20:22:43 +000054 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
Reid Spencerbf437722004-08-15 08:19:46 +000058 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?
Reid Spencerfd2f7282004-08-24 13:49:57 +000065 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
Reid Spencer5c56dc12004-08-13 20:22:43 +000069 };
70
Reid Spencer52c2dc12004-08-29 19:26:56 +000071 /// @brief Driver specific flags
72 enum DriverFlags {
73 DRY_RUN_FLAG = 0x0001, ///< Do everything but execute actions
74 FORCE_FLAG = 0x0002, ///< Force overwrite of output files
75 VERBOSE_FLAG = 0x0004, ///< Print each action
76 DEBUG_FLAG = 0x0008, ///< Print debug information
77 TIME_PASSES_FLAG = 0x0010, ///< Time the passes as they execute
78 TIME_ACTIONS_FLAG = 0x0020, ///< Time the actions as they execute
79 SHOW_STATS_FLAG = 0x0040, ///< Show pass statistics
80 EMIT_NATIVE_FLAG = 0x0080, ///< Emit native code instead of bc
81 EMIT_RAW_FLAG = 0x0100, ///< Emit raw, unoptimized bytecode
82 KEEP_TEMPS_FLAG = 0x0200, ///< Don't delete temporary files
83 DRIVER_FLAGS_MASK = 0x02FF, ///< Union of the above flags
84 };
85
Reid Spencer5c56dc12004-08-13 20:22:43 +000086 /// This type is the input list to the CompilerDriver. It provides
Reid Spencer52c2dc12004-08-29 19:26:56 +000087 /// a vector of pathname/filetype pairs. The filetype is used to look up
Reid Spencer5c56dc12004-08-13 20:22:43 +000088 /// the configuration of the actions to be taken by the driver.
89 /// @brief The Input Data to the execute method
Reid Spencer52c2dc12004-08-29 19:26:56 +000090 typedef std::vector<std::pair<sys::Path,std::string> > InputList;
Reid Spencer5c56dc12004-08-13 20:22:43 +000091
92 /// This type is read from configuration files or otherwise provided to
93 /// the CompilerDriver through a "ConfigDataProvider". It serves as both
94 /// the template of what to do and the actual Action to be executed.
95 /// @brief A structure to hold the action data for a given source
96 /// language.
97 struct Action {
Reid Spencerbae68252004-08-19 04:49:47 +000098 Action() : flags(0) {}
Reid Spencer52c2dc12004-08-29 19:26:56 +000099 sys::Program program; ///< The program to execve
Reid Spencerbf437722004-08-15 08:19:46 +0000100 StringVector args; ///< Arguments to the program
Reid Spencerbf437722004-08-15 08:19:46 +0000101 unsigned flags; ///< Action specific flags
102 void set(unsigned fl ) { flags |= fl; }
103 void clear(unsigned fl) { flags &= (FLAGS_MASK ^ fl); }
Reid Spencerbae68252004-08-19 04:49:47 +0000104 bool isSet(unsigned fl) { return (flags&fl) != 0; }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000105 };
106
107 struct ConfigData {
Reid Spencerbae68252004-08-19 04:49:47 +0000108 ConfigData();
Reid Spencer59a745a2004-08-22 18:03:25 +0000109 std::string version; ///< The version number.
Reid Spencerbae68252004-08-19 04:49:47 +0000110 std::string langName; ///< The name of the source language
111 StringTable opts; ///< The o10n options for each level
112 Action PreProcessor; ///< PreProcessor command line
113 Action Translator; ///< Translator command line
114 Action Optimizer; ///< Optimizer command line
115 Action Assembler; ///< Assembler command line
116 Action Linker; ///< Linker command line
Reid Spencer5c56dc12004-08-13 20:22:43 +0000117 };
118
119 /// This pure virtual interface class defines the interface between the
120 /// CompilerDriver and other software that provides ConfigData objects to
121 /// it. The CompilerDriver must be configured to use an object of this
122 /// type so it can obtain the configuration data.
123 /// @see setConfigDataProvider
124 /// @brief Configuration Data Provider interface
125 class ConfigDataProvider {
126 public:
127 virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000128 virtual void setConfigDir(const sys::Path& dirName) = 0;
Reid Spencerabf1ce32004-08-10 16:29:18 +0000129 };
130
131 /// @}
132 /// @name Constructors
133 /// @{
134 public:
Reid Spencer52c2dc12004-08-29 19:26:56 +0000135 /// @brief Static Constructor
136 static CompilerDriver* Get(ConfigDataProvider& CDP);
137
138 /// @brief Virtual destructor
Reid Spencer5c56dc12004-08-13 20:22:43 +0000139 virtual ~CompilerDriver();
Reid Spencerabf1ce32004-08-10 16:29:18 +0000140
141 /// @}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000142 /// @name Methods
Reid Spencerabf1ce32004-08-10 16:29:18 +0000143 /// @{
144 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000145 /// @brief Execute the actions requested for the given input list.
Reid Spencer52c2dc12004-08-29 19:26:56 +0000146 virtual int execute(const InputList& list, const sys::Path& output) = 0;
Reid Spencerabf1ce32004-08-10 16:29:18 +0000147
Reid Spencer5c56dc12004-08-13 20:22:43 +0000148 /// @brief Set the final phase at which compilation terminates
Reid Spencer52c2dc12004-08-29 19:26:56 +0000149 virtual void setFinalPhase(Phases phase) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000150
Reid Spencerabf1ce32004-08-10 16:29:18 +0000151 /// @brief Set the optimization level for the compilation
Reid Spencer52c2dc12004-08-29 19:26:56 +0000152 virtual void setOptimization(OptimizationLevels level) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000153
Reid Spencer52c2dc12004-08-29 19:26:56 +0000154 /// @brief Set the driver flags.
155 virtual void setDriverFlags(unsigned flags) = 0;
Reid Spencerbae68252004-08-19 04:49:47 +0000156
Reid Spencer5c56dc12004-08-13 20:22:43 +0000157 /// @brief Set the output machine name.
Reid Spencer52c2dc12004-08-29 19:26:56 +0000158 virtual void setOutputMachine(const std::string& machineName) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000159
Reid Spencerbf437722004-08-15 08:19:46 +0000160 /// @brief Set Preprocessor specific options
Reid Spencer52c2dc12004-08-29 19:26:56 +0000161 virtual void setPhaseArgs(Phases phase, const StringVector& opts) = 0;
Reid Spencerbf437722004-08-15 08:19:46 +0000162
163 /// @brief Set Library Paths
Reid Spencer52c2dc12004-08-29 19:26:56 +0000164 virtual void setLibraryPaths(const StringVector& paths) = 0;
Reid Spencerbf437722004-08-15 08:19:46 +0000165
Reid Spencer5c56dc12004-08-13 20:22:43 +0000166 /// @brief Set the list of library paths to be searched for
167 /// libraries.
Reid Spencer52c2dc12004-08-29 19:26:56 +0000168 virtual void addLibraryPath( const sys::Path& libPath ) = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169
170 /// @}
Reid Spencerabf1ce32004-08-10 16:29:18 +0000171 };
172}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000173
174// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
175#endif