blob: d6587b0aa92425b785410bd3afbc8de1a44a48ed [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 {
61 std::string program; ///< The program to execve
62 std::vector<std::string> args; ///< Arguments to the program
63 size_t inputAt; ///< Argument index to insert input file
64 size_t outputAt; ///< Argument index to insert output file
65 };
66
67 struct ConfigData {
68 std::string langName; ///< The name of the source language
69 bool TranslatorPreprocesses;///< Translator program will pre-process
70 bool TranslatorOptimizes; ///< Translator program will optimize too
71 bool TranslatorGroksDashO; ///< Translator understands -O arguments
72 bool PreprocessorNeeded; ///< Preprocessor is needed for translation
73 Action PreProcessor; ///< PreProcessor command line
74 Action Translator; ///< Translator command line
75 Action Optimizer; ///< Optimizer command line
76 Action Assembler; ///< Assembler command line
77 Action Linker; ///< Linker command line
78 };
79
80 /// This pure virtual interface class defines the interface between the
81 /// CompilerDriver and other software that provides ConfigData objects to
82 /// it. The CompilerDriver must be configured to use an object of this
83 /// type so it can obtain the configuration data.
84 /// @see setConfigDataProvider
85 /// @brief Configuration Data Provider interface
86 class ConfigDataProvider {
87 public:
88 virtual ConfigData* ProvideConfigData(const std::string& filetype) = 0;
89 virtual void setConfigDir(const std::string& dirName) = 0;
Reid Spencerabf1ce32004-08-10 16:29:18 +000090 };
91
92 /// @}
93 /// @name Constructors
94 /// @{
95 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +000096 CompilerDriver(ConfigDataProvider& cdp );
97 virtual ~CompilerDriver();
Reid Spencerabf1ce32004-08-10 16:29:18 +000098
99 /// @}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000100 /// @name Methods
Reid Spencerabf1ce32004-08-10 16:29:18 +0000101 /// @{
102 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000103 /// @brief Handle an error
104 virtual void error(const std::string& errmsg);
105
106 /// @brief Execute the actions requested for the given input list.
107 virtual int execute(const InputList& list, const std::string& output);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000108
109 /// @}
110 /// @name Mutators
111 /// @{
112 public:
Reid Spencer5c56dc12004-08-13 20:22:43 +0000113 /// @brief Set the final phase at which compilation terminates
114 void setFinalPhase( Phases phase ) { finalPhase = phase; }
115
Reid Spencerabf1ce32004-08-10 16:29:18 +0000116 /// @brief Set the optimization level for the compilation
Reid Spencer5c56dc12004-08-13 20:22:43 +0000117 void setOptimization( OptimizationLevels level ) { optLevel = level; }
118
119 /// @brief Prevent the CompilerDriver from taking any actions
120 void setDryRun( bool TF ) { isDryRun = TF; }
121
122 /// @brief Cause the CompilerDriver to print to stderr all the
123 /// actions it is taking.
124 void setVerbose( bool TF ) { isVerbose = TF; }
125
126 /// @brief Cause the CompilerDriver to print to stderr very verbose
127 /// information that might be useful in debugging the driver's actions
128 void setDebug( bool TF ) { isDebug = TF; }
129
130 /// @brief Cause the CompilerDriver to print to stderr the
131 /// execution time of each action taken.
132 void setTimeActions( bool TF ) { timeActions = TF; }
133
134 /// @brief Indicate that native code is to be generated instead
135 /// of LLVM bytecode.
136 void setEmitNativeCode( bool TF ) { emitNativeCode = TF; }
137
138 /// @brief Indicate that raw, unoptimized code is to be generated.
139 void setEmitRawCode(bool TF ) { emitRawCode = TF; }
140
141 /// @brief Set the output machine name.
142 void setOutputMachine( const std::string& machineName ) {
143 machine = machineName;
144 }
145
146 /// @brief Set the list of library paths to be searched for
147 /// libraries.
148 void addLibraryPath( const std::string& libPath ) {
149 libPaths.push_back(libPath);
150 }
151
152 /// @}
153 /// @name Functions
154 /// @{
155 private:
156 Action* GetAction(ConfigData* cd, const std::string& input,
157 const std::string& output, Phases phase );
158 void WriteAction(Action* a);
159 void DoAction(Action* a);
Reid Spencerabf1ce32004-08-10 16:29:18 +0000160
161 /// @}
162 /// @name Data
163 /// @{
Reid Spencer5c56dc12004-08-13 20:22:43 +0000164 private:
165 ConfigDataProvider* cdp; ///< Where we get configuration data from
166 Phases finalPhase; ///< The final phase of compilation
167 OptimizationLevels optLevel; ///< The optimization level to apply
168 bool isDryRun; ///< Prevent actions ?
169 bool isVerbose; ///< Print actions?
170 bool isDebug; ///< Print lotsa debug info?
171 bool timeActions; ///< Time the actions executed ?
172 bool emitRawCode; ///< Emit Raw (unoptimized) code?
173 bool emitNativeCode; ///< Emit native code instead of bytecode?
174 std::string machine; ///< Target machine name
175 std::vector<std::string> libPaths; ///< list of dirs to find libraries
Reid Spencerabf1ce32004-08-10 16:29:18 +0000176
177 /// @}
178
179 };
180}
Reid Spencer5c56dc12004-08-13 20:22:43 +0000181
182// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
183#endif