blob: f49a780ba5eec4d203b0c6811546edbd6a0f02d3 [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 Spencerabf1ce32004-08-10 16:29:18 +000032 enum Phases {
33 PREPROCESSING, ///< Source language combining, filtering, substitution
34 TRANSLATION, ///< Translate source -> LLVM bytecode/assembly
35 OPTIMIZATION, ///< Optimize translation result
36 LINKING, ///< Link bytecode and native code
37 ASSEMBLY, ///< Convert program to executable
38 };
39
40 enum OptimizationLevels {
Reid Spencer5c56dc12004-08-13 20:22:43 +000041 OPT_NONE, ///< Zippo optimizations, nada, nil, none.
42 OPT_FAST_COMPILE, ///< Optimize to make >compile< go faster
43 OPT_SIMPLE, ///< Standard/simple optimizations
44 OPT_AGGRESSIVE, ///< Aggressive optimizations
45 OPT_LINK_TIME, ///< Aggressive + LinkTime optimizations
46 OPT_AGGRESSIVE_LINK_TIME ///< Make it go way fast!
47 };
48
49 /// This type is the input list to the CompilerDriver. It provides
50 /// a vector of filename/filetype pairs. The filetype is used to look up
51 /// the configuration of the actions to be taken by the driver.
52 /// @brief The Input Data to the execute method
53 typedef std::vector<std::pair<std::string,std::string> > InputList;
54
55 /// This type is read from configuration files or otherwise provided to
56 /// the CompilerDriver through a "ConfigDataProvider". It serves as both
57 /// the template of what to do and the actual Action to be executed.
58 /// @brief A structure to hold the action data for a given source
59 /// language.
60 struct Action {
Reid Spencer68fb37a2004-08-14 09:37:15 +000061 Action() : inputAt(0) , outputAt(0) {}
Reid Spencer5c56dc12004-08-13 20:22:43 +000062 std::string program; ///< The program to execve
63 std::vector<std::string> args; ///< Arguments to the program
64 size_t inputAt; ///< Argument index to insert input file
65 size_t outputAt; ///< Argument index to insert output file
66 };
67
68 struct ConfigData {
Reid Spencer68fb37a2004-08-14 09:37:15 +000069 ConfigData() : TranslatorPreprocesses(false),
70 TranslatorOptimizes(false),
71 TranslatorGroksDashO(false),
72 PreprocessorNeeded(false) {}
Reid Spencer5c56dc12004-08-13 20:22:43 +000073 std::string langName; ///< The name of the source language
74 bool TranslatorPreprocesses;///< Translator program will pre-process
75 bool TranslatorOptimizes; ///< Translator program will optimize too
76 bool TranslatorGroksDashO; ///< Translator understands -O arguments
77 bool PreprocessorNeeded; ///< Preprocessor is needed for translation
78 Action PreProcessor; ///< PreProcessor command line
79 Action Translator; ///< Translator command line
80 Action Optimizer; ///< Optimizer command line
81 Action Assembler; ///< Assembler command line
82 Action Linker; ///< Linker command line
83 };
84
85 /// This pure virtual interface class defines the interface between the
86 /// CompilerDriver and other software that provides ConfigData objects to
87 /// it. The CompilerDriver must be configured to use an object of this
88 /// type so it can obtain the configuration data.
89 /// @see setConfigDataProvider
90 /// @brief Configuration Data Provider interface
91 class ConfigDataProvider {
92 public:
93 virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
94 virtual void setConfigDir(const std::string& dirName) = 0;
Reid Spencerabf1ce32004-08-10 16:29:18 +000095 };
96
97 /// @}
98 /// @name Constructors
99 /// @{
100 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000101 CompilerDriver(ConfigDataProvider& cdp );
102 virtual ~CompilerDriver();
Reid Spencerabf1ce32004-08-10 16:29:18 +0000103
104 /// @}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000105 /// @name Methods
Reid Spencerabf1ce32004-08-10 16:29:18 +0000106 /// @{
107 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000108 /// @brief Handle an error
109 virtual void error(const std::string& errmsg);
110
111 /// @brief Execute the actions requested for the given input list.
112 virtual int execute(const InputList& list, const std::string& output);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000113
114 /// @}
115 /// @name Mutators
116 /// @{
117 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000118 /// @brief Set the final phase at which compilation terminates
119 void setFinalPhase( Phases phase ) { finalPhase = phase; }
120
Reid Spencerabf1ce32004-08-10 16:29:18 +0000121 /// @brief Set the optimization level for the compilation
Reid Spencer5c56dc12004-08-13 20:22:43 +0000122 void setOptimization( OptimizationLevels level ) { optLevel = level; }
123
124 /// @brief Prevent the CompilerDriver from taking any actions
125 void setDryRun( bool TF ) { isDryRun = TF; }
126
127 /// @brief Cause the CompilerDriver to print to stderr all the
128 /// actions it is taking.
129 void setVerbose( bool TF ) { isVerbose = TF; }
130
131 /// @brief Cause the CompilerDriver to print to stderr very verbose
132 /// information that might be useful in debugging the driver's actions
133 void setDebug( bool TF ) { isDebug = TF; }
134
135 /// @brief Cause the CompilerDriver to print to stderr the
136 /// execution time of each action taken.
137 void setTimeActions( bool TF ) { timeActions = TF; }
138
139 /// @brief Indicate that native code is to be generated instead
140 /// of LLVM bytecode.
141 void setEmitNativeCode( bool TF ) { emitNativeCode = TF; }
142
143 /// @brief Indicate that raw, unoptimized code is to be generated.
144 void setEmitRawCode(bool TF ) { emitRawCode = TF; }
145
146 /// @brief Set the output machine name.
147 void setOutputMachine( const std::string& machineName ) {
148 machine = machineName;
149 }
150
151 /// @brief Set the list of library paths to be searched for
152 /// libraries.
153 void addLibraryPath( const std::string& libPath ) {
154 libPaths.push_back(libPath);
155 }
156
157 /// @}
158 /// @name Functions
159 /// @{
160 private:
161 Action* GetAction(ConfigData* cd, const std::string& input,
162 const std::string& output, Phases phase );
Reid Spencer5c56dc12004-08-13 20:22:43 +0000163 void DoAction(Action* a);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000164
165 /// @}
166 /// @name Data
167 /// @{
Reid Spencer5c56dc12004-08-13 20:22:43 +0000168 private:
169 ConfigDataProvider* cdp; ///< Where we get configuration data from
170 Phases finalPhase; ///< The final phase of compilation
171 OptimizationLevels optLevel; ///< The optimization level to apply
172 bool isDryRun; ///< Prevent actions ?
173 bool isVerbose; ///< Print actions?
174 bool isDebug; ///< Print lotsa debug info?
175 bool timeActions; ///< Time the actions executed ?
176 bool emitRawCode; ///< Emit Raw (unoptimized) code?
177 bool emitNativeCode; ///< Emit native code instead of bytecode?
178 std::string machine; ///< Target machine name
179 std::vector<std::string> libPaths; ///< list of dirs to find libraries
Reid Spencerabf1ce32004-08-10 16:29:18 +0000180
181 /// @}
182
183 };
184}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000185
186// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
187#endif