blob: cd7c0ab5d2727078e8d3b88f644f4c66b35024e2 [file] [log] [blame]
Reid Spencer5c56dc12004-08-13 20:22:43 +00001//===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
Reid Spencer034a5442004-08-10 16:26:01 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer5c56dc12004-08-13 20:22:43 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer034a5442004-08-10 16:26:01 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
13// the --help option.
14//
15//===------------------------------------------------------------------------===
16
Reid Spencerabf1ce32004-08-10 16:29:18 +000017#include "CompilerDriver.h"
Reid Spencerf51a87c2004-08-19 21:52:49 +000018#include "Configuration.h"
Reid Spencer034a5442004-08-10 16:26:01 +000019#include "llvm/System/Signals.h"
20#include "Support/CommandLine.h"
21#include <iostream>
22
23using namespace llvm;
24
Reid Spencer5c56dc12004-08-13 20:22:43 +000025namespace {
Reid Spencer034a5442004-08-10 16:26:01 +000026//===------------------------------------------------------------------------===
27//=== PHASE OPTIONS
28//===------------------------------------------------------------------------===
Reid Spencerbae68252004-08-19 04:49:47 +000029cl::opt<CompilerDriver::Phases> FinalPhase(
Reid Spencer034a5442004-08-10 16:26:01 +000030 cl::desc("Choose final phase of compilation:"),
Reid Spencer5c56dc12004-08-13 20:22:43 +000031 cl::init(CompilerDriver::LINKING),
Reid Spencer034a5442004-08-10 16:26:01 +000032 cl::values(
33 clEnumValN(CompilerDriver::PREPROCESSING,"E",
Reid Spencer5c56dc12004-08-13 20:22:43 +000034 "Stop compilation after pre-processing phase"),
35 clEnumValN(CompilerDriver::TRANSLATION, "t",
36 "Stop compilation after translation phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000037 clEnumValN(CompilerDriver::OPTIMIZATION,"c",
Reid Spencer5c56dc12004-08-13 20:22:43 +000038 "Stop compilation after optimization phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000039 clEnumValN(CompilerDriver::ASSEMBLY,"S",
Reid Spencer5c56dc12004-08-13 20:22:43 +000040 "Stop compilation after assembly phase"),
Reid Spencer034a5442004-08-10 16:26:01 +000041 clEnumValEnd
42 )
43);
44
45//===------------------------------------------------------------------------===
46//=== OPTIMIZATION OPTIONS
47//===------------------------------------------------------------------------===
Reid Spencerbae68252004-08-19 04:49:47 +000048cl::opt<CompilerDriver::OptimizationLevels> OptLevel(
Reid Spencer034a5442004-08-10 16:26:01 +000049 cl::desc("Choose level of optimization to apply:"),
Reid Spencer5c56dc12004-08-13 20:22:43 +000050 cl::init(CompilerDriver::OPT_FAST_COMPILE),
Reid Spencer034a5442004-08-10 16:26:01 +000051 cl::values(
52 clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0",
Reid Spencer5c56dc12004-08-13 20:22:43 +000053 "An alias for the -O1 option."),
Reid Spencer034a5442004-08-10 16:26:01 +000054 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"),
64 clEnumValEnd
65 )
66);
67
68//===------------------------------------------------------------------------===
69//=== TOOL OPTIONS
70//===------------------------------------------------------------------------===
71
Reid Spencerbae68252004-08-19 04:49:47 +000072cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000073 cl::desc("Pass specific options to the pre-processor"),
74 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000075
Reid Spencerbae68252004-08-19 04:49:47 +000076cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000077 cl::desc("Pass specific options to the assembler"),
78 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000079
Reid Spencerbae68252004-08-19 04:49:47 +000080cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
Reid Spencerbf437722004-08-15 08:19:46 +000081 cl::desc("Pass specific options to the assembler"),
82 cl::value_desc("option"));
83
Reid Spencerbae68252004-08-19 04:49:47 +000084cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000085 cl::desc("Pass specific options to the optimizer"),
86 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000087
Reid Spencerbae68252004-08-19 04:49:47 +000088cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +000089 cl::desc("Pass specific options to the linker"),
90 cl::value_desc("option"));
Reid Spencer034a5442004-08-10 16:26:01 +000091
92//===------------------------------------------------------------------------===
93//=== INPUT OPTIONS
94//===------------------------------------------------------------------------===
95
Reid Spencerbae68252004-08-19 04:49:47 +000096cl::list<std::string> LibPaths("L", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +000097 cl::desc("Specify a library search path"), cl::value_desc("directory"));
98
Reid Spencerbae68252004-08-19 04:49:47 +000099cl::list<std::string> Libraries("l", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +0000100 cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
101
102
103//===------------------------------------------------------------------------===
104//=== OUTPUT OPTIONS
105//===------------------------------------------------------------------------===
106
Reid Spencerbae68252004-08-19 04:49:47 +0000107cl::opt<std::string> OutputFilename("o",
Reid Spencer034a5442004-08-10 16:26:01 +0000108 cl::desc("Override output filename"), cl::value_desc("filename"));
109
Reid Spencerbae68252004-08-19 04:49:47 +0000110cl::opt<std::string> OutputMachine("m", cl::Prefix,
Reid Spencer034a5442004-08-10 16:26:01 +0000111 cl::desc("Specify a target machine"), cl::value_desc("machine"));
112
Reid Spencerbae68252004-08-19 04:49:47 +0000113cl::opt<bool> Native("native", cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000114 cl::desc("Generative native object and executables instead of bytecode"));
115
116//===------------------------------------------------------------------------===
117//=== INFORMATION OPTIONS
118//===------------------------------------------------------------------------===
119
Reid Spencerbae68252004-08-19 04:49:47 +0000120cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000121 cl::desc("Do everything but perform the compilation actions"));
Reid Spencer034a5442004-08-10 16:26:01 +0000122
Reid Spencerbae68252004-08-19 04:49:47 +0000123cl::alias DryRunAlias("y", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000124 cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
Reid Spencer034a5442004-08-10 16:26:01 +0000125
Reid Spencerbae68252004-08-19 04:49:47 +0000126cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000127 cl::desc("Print out each action taken"));
128
Reid Spencerbae68252004-08-19 04:49:47 +0000129cl::alias VerboseAlias("v", cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000130 cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
131
Reid Spencerbae68252004-08-19 04:49:47 +0000132cl::opt<bool> Debug("debug", cl::Optional, cl::init(false),
Reid Spencer5c56dc12004-08-13 20:22:43 +0000133 cl::Hidden, cl::desc("Print out debugging information"));
134
Reid Spencerbae68252004-08-19 04:49:47 +0000135cl::alias DebugAlias("d", cl::Optional,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000136 cl::desc("Alias for -debug"), cl::aliasopt(Debug));
137
Reid Spencerbae68252004-08-19 04:49:47 +0000138cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false),
Reid Spencer034a5442004-08-10 16:26:01 +0000139 cl::desc("Print execution time for each action taken"));
140
Reid Spencerbae68252004-08-19 04:49:47 +0000141cl::opt<bool> TimePasses("time-passes", cl::Optional, cl::init(false),
142 cl::desc("Print execution time for each optimization pass"));
143
144cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
145 cl::desc("Print statistics accumulated during optimization"));
146
Reid Spencer034a5442004-08-10 16:26:01 +0000147//===------------------------------------------------------------------------===
148//=== ADVANCED OPTIONS
149//===------------------------------------------------------------------------===
150
Reid Spencer5c56dc12004-08-13 20:22:43 +0000151static cl::opt<std::string> ConfigDir("config-dir", cl::Optional,
152 cl::desc("Specify a configuration directory to override defaults"),
153 cl::value_desc("directory"));
Reid Spencer034a5442004-08-10 16:26:01 +0000154
Reid Spencerbf437722004-08-15 08:19:46 +0000155static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
Reid Spencer034a5442004-08-10 16:26:01 +0000156 cl::desc("Emit raw, unoptimized code"));
157
Reid Spencerbf437722004-08-15 08:19:46 +0000158static cl::opt<bool> PipeCommands("pipe", cl::Optional,
159 cl::desc("Invoke sub-commands by linking input/output with pipes"));
160
Reid Spencerbae68252004-08-19 04:49:47 +0000161static cl::opt<bool> KeepTemporaries("keep-temps", cl::Optional,
162 cl::desc("Don't delete the temporary files created during compilation"));
163
Reid Spencer034a5442004-08-10 16:26:01 +0000164//===------------------------------------------------------------------------===
165//=== POSITIONAL OPTIONS
166//===------------------------------------------------------------------------===
167
168static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
Reid Spencer5c56dc12004-08-13 20:22:43 +0000169 cl::desc("[Sources/objects/libraries]"));
170
171static cl::list<std::string> Languages("x", cl::ZeroOrMore,
172 cl::desc("Specify the source language for subsequent files"),
173 cl::value_desc("language"));
174
175//===------------------------------------------------------------------------===
176//=== GetFileType - determine type of a file
177//===------------------------------------------------------------------------===
178const std::string GetFileType(const std::string& fname, unsigned pos ) {
179 static std::vector<std::string>::iterator langIt = Languages.begin();
180 static std::string CurrLang = "";
181
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 ) {
186 // use that language
187 CurrLang = *langIt++;
188 return CurrLang;
189 }
190
191 // If there's a current language in effect
192 if (!CurrLang.empty())
193 return CurrLang; // use that language
194
195 // otherwise just determine lang from the filename's suffix
196 return fname.substr( fname.rfind('.',fname.size()) + 1 );
197}
198
199} // end anonymous namespace
Reid Spencer034a5442004-08-10 16:26:01 +0000200
201
202/// @brief The main program for llvmc
203int main(int argc, char **argv) {
Reid Spencerbae68252004-08-19 04:49:47 +0000204 try {
205 // Make sure we print stack trace if we get bad signals
206 PrintStackTraceOnErrorSignal();
Reid Spencer034a5442004-08-10 16:26:01 +0000207
Reid Spencerbae68252004-08-19 04:49:47 +0000208 // 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"
213 );
Reid Spencer034a5442004-08-10 16:26:01 +0000214
Reid Spencerbae68252004-08-19 04:49:47 +0000215 // Deal with unimplemented options.
216 if (PipeCommands)
217 std::cerr << argv[0] << ": Not implemented yet: -pipe";
Reid Spencer034a5442004-08-10 16:26:01 +0000218
Reid Spencerbae68252004-08-19 04:49:47 +0000219 // Default the output file, only if we're going to try to link
220 if (OutputFilename.empty() && OptLevel == CompilerDriver::LINKING)
221 OutputFilename = "a.out";
Reid Spencer5c56dc12004-08-13 20:22:43 +0000222
Reid Spencerbae68252004-08-19 04:49:47 +0000223 // Construct the ConfigDataProvider object
224 LLVMC_ConfigDataProvider Provider;
225 Provider.setConfigDir(ConfigDir);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000226
Reid Spencerbae68252004-08-19 04:49:47 +0000227 // Construct the CompilerDriver object
228 CompilerDriver CD(Provider);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000229
Reid Spencerbae68252004-08-19 04:49:47 +0000230 // Configure the driver based on options
231 CD.setVerbose(Verbose);
232 CD.setDebug(Debug);
233 CD.setDryRun(DryRun);
234 CD.setFinalPhase(FinalPhase);
235 CD.setOptimization(OptLevel);
236 CD.setOutputMachine(OutputMachine);
237 CD.setEmitNativeCode(Native);
238 CD.setEmitRawCode(EmitRawCode);
239 CD.setTimeActions(TimeActions);
240 CD.setTimePasses(TimePasses);
241 CD.setShowStats(ShowStats);
242 CD.setKeepTemporaries(KeepTemporaries);
243 CD.setLibraryPaths(LibPaths);
244 if (!PreprocessorToolOpts.empty())
245 CD.setPhaseArgs(CompilerDriver::PREPROCESSING, PreprocessorToolOpts);
246 if (!TranslatorToolOpts.empty())
247 CD.setPhaseArgs(CompilerDriver::TRANSLATION, TranslatorToolOpts);
248 if (!OptimizerToolOpts.empty())
249 CD.setPhaseArgs(CompilerDriver::OPTIMIZATION, OptimizerToolOpts);
250 if (!AssemblerToolOpts.empty())
251 CD.setPhaseArgs(CompilerDriver::ASSEMBLY,AssemblerToolOpts);
252 if (!LinkerToolOpts.empty())
253 CD.setPhaseArgs(CompilerDriver::LINKING, LinkerToolOpts);
Reid Spencer5c56dc12004-08-13 20:22:43 +0000254
Reid Spencerbae68252004-08-19 04:49:47 +0000255 // Prepare the list of files to be compiled by the CompilerDriver.
256 CompilerDriver::InputList InpList;
257 std::vector<std::string>::iterator fileIt = Files.begin();
258 std::vector<std::string>::iterator libIt = Libraries.begin();
259 unsigned libPos = 0, filePos = 0;
260 while ( 1 ) {
261 if ( libIt != Libraries.end() )
262 libPos = Libraries.getPosition( libIt - Libraries.begin() );
263 else
264 libPos = 0;
265 if ( fileIt != Files.end() )
266 filePos = Files.getPosition( fileIt - Files.begin() );
267 else
268 filePos = 0;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000269
Reid Spencerbae68252004-08-19 04:49:47 +0000270 if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) {
271 // Add a source file
272 InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos)));
273 ++fileIt;
274 }
275 else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) {
276 // Add a library
277 InpList.push_back( std::make_pair(*libIt++,""));
278 }
279 else
280 break; // we're done with the list
Reid Spencer5c56dc12004-08-13 20:22:43 +0000281 }
Reid Spencerbae68252004-08-19 04:49:47 +0000282
283 // Tell the driver to do its thing
284 int result = CD.execute(InpList,OutputFilename);
285 if (result != 0) {
286 std::cerr << argv[0] << ": Error executing actions. Terminated.\n";
287 return result;
Reid Spencer5c56dc12004-08-13 20:22:43 +0000288 }
Reid Spencer034a5442004-08-10 16:26:01 +0000289
Reid Spencerbae68252004-08-19 04:49:47 +0000290 // All is good, return success
291 return 0;
292 } catch (std::string& msg) {
293 std::cerr << msg << "\n";
294 } catch (...) {
295 std::cerr << "Unexpected unknown exception occurred.\n";
Reid Spencer034a5442004-08-10 16:26:01 +0000296 }
Reid Spencer034a5442004-08-10 16:26:01 +0000297}