1 //===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tool provides a single point of access to the LLVM compilation tools.
11 // It has many options. To discover the options supported please refer to the
12 // tools' manual page (docs/CommandGuide/html/llvmc.html) or run the tool with
15 //===------------------------------------------------------------------------===
17 #include "CompilerDriver.h"
18 #include "Configuration.h"
19 #include "llvm/System/Signals.h"
20 #include "Support/CommandLine.h"
26 //===------------------------------------------------------------------------===
28 //===------------------------------------------------------------------------===
29 cl::opt<CompilerDriver::Phases> FinalPhase(
30 cl::desc("Choose final phase of compilation:"),
31 cl::init(CompilerDriver::LINKING),
33 clEnumValN(CompilerDriver::PREPROCESSING,"E",
34 "Stop compilation after pre-processing phase"),
35 clEnumValN(CompilerDriver::TRANSLATION, "t",
36 "Stop compilation after translation phase"),
37 clEnumValN(CompilerDriver::OPTIMIZATION,"c",
38 "Stop compilation after optimization phase"),
39 clEnumValN(CompilerDriver::ASSEMBLY,"S",
40 "Stop compilation after assembly phase"),
45 //===------------------------------------------------------------------------===
46 //=== OPTIMIZATION OPTIONS
47 //===------------------------------------------------------------------------===
48 cl::opt<CompilerDriver::OptimizationLevels> OptLevel(
49 cl::desc("Choose level of optimization to apply:"),
50 cl::init(CompilerDriver::OPT_FAST_COMPILE),
52 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0",
53 "An alias for the -O1 option."),
54 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O1",
55 "Optimize for compilation speed, not execution speed."),
56 clEnumValN(CompilerDriver::OPT_SIMPLE,"O2",
57 "Perform simple translation time optimizations"),
58 clEnumValN(CompilerDriver::OPT_AGGRESSIVE,"O3",
59 "Perform aggressive translation time optimizations"),
60 clEnumValN(CompilerDriver::OPT_LINK_TIME,"O4",
61 "Perform link time optimizations"),
62 clEnumValN(CompilerDriver::OPT_AGGRESSIVE_LINK_TIME,"O5",
63 "Perform aggressive link time optimizations"),
68 //===------------------------------------------------------------------------===
70 //===------------------------------------------------------------------------===
72 cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore,
73 cl::desc("Pass specific options to the pre-processor"),
74 cl::value_desc("option"));
76 cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
77 cl::desc("Pass specific options to the assembler"),
78 cl::value_desc("option"));
80 cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
81 cl::desc("Pass specific options to the assembler"),
82 cl::value_desc("option"));
84 cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
85 cl::desc("Pass specific options to the optimizer"),
86 cl::value_desc("option"));
88 cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
89 cl::desc("Pass specific options to the linker"),
90 cl::value_desc("option"));
92 //===------------------------------------------------------------------------===
94 //===------------------------------------------------------------------------===
96 cl::list<std::string> LibPaths("L", cl::Prefix,
97 cl::desc("Specify a library search path"), cl::value_desc("directory"));
99 cl::list<std::string> Libraries("l", cl::Prefix,
100 cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
103 //===------------------------------------------------------------------------===
105 //===------------------------------------------------------------------------===
107 cl::opt<std::string> OutputFilename("o",
108 cl::desc("Override output filename"), cl::value_desc("filename"));
110 cl::opt<std::string> OutputMachine("m", cl::Prefix,
111 cl::desc("Specify a target machine"), cl::value_desc("machine"));
113 cl::opt<bool> Native("native", cl::init(false),
114 cl::desc("Generative native object and executables instead of bytecode"));
116 //===------------------------------------------------------------------------===
117 //=== INFORMATION OPTIONS
118 //===------------------------------------------------------------------------===
120 cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
121 cl::desc("Do everything but perform the compilation actions"));
123 cl::alias DryRunAlias("y", cl::Optional,
124 cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
126 cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false),
127 cl::desc("Print out each action taken"));
129 cl::alias VerboseAlias("v", cl::Optional,
130 cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
132 cl::opt<bool> Debug("debug", cl::Optional, cl::init(false),
133 cl::Hidden, cl::desc("Print out debugging information"));
135 cl::alias DebugAlias("d", cl::Optional,
136 cl::desc("Alias for -debug"), cl::aliasopt(Debug));
138 cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false),
139 cl::desc("Print execution time for each action taken"));
141 cl::opt<bool> TimePasses("time-passes", cl::Optional, cl::init(false),
142 cl::desc("Print execution time for each optimization pass"));
144 cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
145 cl::desc("Print statistics accumulated during optimization"));
147 //===------------------------------------------------------------------------===
148 //=== ADVANCED OPTIONS
149 //===------------------------------------------------------------------------===
151 static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
152 cl::desc("Specify a configuration directory to override defaults"),
153 cl::value_desc("directory"));
155 static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
156 cl::desc("Emit raw, unoptimized code"));
158 static cl::opt<bool> PipeCommands("pipe", cl::Optional,
159 cl::desc("Invoke sub-commands by linking input/output with pipes"));
161 static cl::opt<bool> KeepTemporaries("keep-temps", cl::Optional,
162 cl::desc("Don't delete the temporary files created during compilation"));
164 //===------------------------------------------------------------------------===
165 //=== POSITIONAL OPTIONS
166 //===------------------------------------------------------------------------===
168 static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
169 cl::desc("[Sources/objects/libraries]"));
171 static cl::list<std::string> Languages("x", cl::ZeroOrMore,
172 cl::desc("Specify the source language for subsequent files"),
173 cl::value_desc("language"));
175 //===------------------------------------------------------------------------===
176 //=== GetFileType - determine type of a file
177 //===------------------------------------------------------------------------===
178 const std::string GetFileType(const std::string& fname, unsigned pos ) {
179 static std::vector<std::string>::iterator langIt = Languages.begin();
180 static std::string CurrLang = "";
182 // If a -x LANG option has been specified ..
183 if ( langIt != Languages.end() )
184 // If the -x LANG option came before the current file on command line
185 if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) {
187 CurrLang = *langIt++;
191 // If there's a current language in effect
192 if (!CurrLang.empty())
193 return CurrLang; // use that language
195 // otherwise just determine lang from the filename's suffix
196 return fname.substr( fname.rfind('.',fname.size()) + 1 );
199 } // end anonymous namespace
202 /// @brief The main program for llvmc
203 int main(int argc, char **argv) {
205 // Make sure we print stack trace if we get bad signals
206 PrintStackTraceOnErrorSignal();
208 // Parse the command line options
209 cl::ParseCommandLineOptions(argc, argv,
210 " LLVM Compilation Driver (llvmc)\n\n"
211 " This program provides easy invocation of the LLVM tool set\n"
212 " and source language compiler tools.\n"
215 // Deal with unimplemented options.
217 throw "Not implemented yet: -pipe";
219 if (OutputFilename.empty())
220 if (OptLevel == CompilerDriver::LINKING)
221 OutputFilename = "a.out";
223 throw "An output file must be specified. Please use the -o option";
226 // Construct the ConfigDataProvider object
227 LLVMC_ConfigDataProvider Provider;
228 Provider.setConfigDir(ConfigDir);
230 // Construct the CompilerDriver object
231 CompilerDriver CD(Provider);
233 // Configure the driver based on options
234 CD.setVerbose(Verbose);
236 CD.setDryRun(DryRun);
237 CD.setFinalPhase(FinalPhase);
238 CD.setOptimization(OptLevel);
239 CD.setOutputMachine(OutputMachine);
240 CD.setEmitNativeCode(Native);
241 CD.setEmitRawCode(EmitRawCode);
242 CD.setTimeActions(TimeActions);
243 CD.setTimePasses(TimePasses);
244 CD.setShowStats(ShowStats);
245 CD.setKeepTemporaries(KeepTemporaries);
246 CD.setLibraryPaths(LibPaths);
247 if (!PreprocessorToolOpts.empty())
248 CD.setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
249 if (!TranslatorToolOpts.empty())
250 CD.setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
251 if (!OptimizerToolOpts.empty())
252 CD.setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
253 if (!AssemblerToolOpts.empty())
254 CD.setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
255 if (!LinkerToolOpts.empty())
256 CD.setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
258 // Prepare the list of files to be compiled by the CompilerDriver.
259 CompilerDriver::InputList InpList;
260 std::vector<std::string>::iterator fileIt = Files.begin();
261 std::vector<std::string>::iterator libIt = Libraries.begin();
262 unsigned libPos = 0, filePos = 0;
264 if ( libIt != Libraries.end() )
265 libPos = Libraries.getPosition( libIt - Libraries.begin() );
268 if ( fileIt != Files.end() )
269 filePos = Files.getPosition( fileIt - Files.begin() );
273 if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
275 InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos)));
278 else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
280 InpList.push_back( std::make_pair(*libIt++,""));
283 break; // we're done with the list
286 // Tell the driver to do its thing
287 int result = CD.execute(InpList,OutputFilename);
289 throw "Error executing actions. Terminated.\n";
293 // All is good, return success
295 } catch (std::string& msg) {
296 std::cerr << argv[0] << ": " << msg << "\n";
298 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";