Implement many new features for llvmc. This is the first version that will
[oota-llvm.git] / tools / llvmc / llvmc.cpp
1 //===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===//
2 // 
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 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
17 #include "CompilerDriver.h"
18 #include "ConfigData.h"
19 #include "llvm/System/Signals.h"
20 #include "Support/CommandLine.h"
21 #include <iostream>
22
23 using namespace llvm;
24
25 namespace {
26 //===------------------------------------------------------------------------===
27 //===          PHASE OPTIONS
28 //===------------------------------------------------------------------------===
29 cl::opt<CompilerDriver::Phases> FinalPhase(
30   cl::desc("Choose final phase of compilation:"), 
31   cl::init(CompilerDriver::LINKING),
32   cl::values(
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"),
41     clEnumValEnd
42   )
43 );
44
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),
51   cl::values(
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"),
64     clEnumValEnd
65   )
66 );
67
68 //===------------------------------------------------------------------------===
69 //===          TOOL OPTIONS
70 //===------------------------------------------------------------------------===
71
72 cl::list<std::string> PreprocessorToolOpts("Tpre", cl::ZeroOrMore,
73   cl::desc("Pass specific options to the pre-processor"), 
74   cl::value_desc("option"));
75
76 cl::list<std::string> TranslatorToolOpts("Ttrn", cl::ZeroOrMore,
77   cl::desc("Pass specific options to the assembler"),
78   cl::value_desc("option"));
79
80 cl::list<std::string> AssemblerToolOpts("Tasm", cl::ZeroOrMore,
81   cl::desc("Pass specific options to the assembler"),
82   cl::value_desc("option"));
83
84 cl::list<std::string> OptimizerToolOpts("Topt", cl::ZeroOrMore,
85   cl::desc("Pass specific options to the optimizer"),
86   cl::value_desc("option"));
87
88 cl::list<std::string> LinkerToolOpts("Tlnk", cl::ZeroOrMore,
89   cl::desc("Pass specific options to the linker"),
90   cl::value_desc("option"));
91
92 //===------------------------------------------------------------------------===
93 //===          INPUT OPTIONS
94 //===------------------------------------------------------------------------===
95
96 cl::list<std::string> LibPaths("L", cl::Prefix,
97   cl::desc("Specify a library search path"), cl::value_desc("directory"));
98                                                                                                                                             
99 cl::list<std::string> Libraries("l", cl::Prefix,
100   cl::desc("Specify libraries to link to"), cl::value_desc("library prefix"));
101
102
103 //===------------------------------------------------------------------------===
104 //===          OUTPUT OPTIONS
105 //===------------------------------------------------------------------------===
106
107 cl::opt<std::string> OutputFilename("o", 
108   cl::desc("Override output filename"), cl::value_desc("filename"));
109
110 cl::opt<std::string> OutputMachine("m", cl::Prefix,
111   cl::desc("Specify a target machine"), cl::value_desc("machine"));
112                                                                                                                                             
113 cl::opt<bool> Native("native", cl::init(false),
114   cl::desc("Generative native object and executables instead of bytecode"));
115
116 //===------------------------------------------------------------------------===
117 //===          INFORMATION OPTIONS
118 //===------------------------------------------------------------------------===
119
120 cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false),
121   cl::desc("Do everything but perform the compilation actions"));
122
123 cl::alias DryRunAlias("y", cl::Optional,
124   cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun));
125
126 cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false),
127   cl::desc("Print out each action taken"));
128
129 cl::alias VerboseAlias("v", cl::Optional, 
130   cl::desc("Alias for -verbose"), cl::aliasopt(Verbose));
131
132 cl::opt<bool> Debug("debug", cl::Optional, cl::init(false), 
133   cl::Hidden, cl::desc("Print out debugging information"));
134
135 cl::alias DebugAlias("d", cl::Optional,
136   cl::desc("Alias for -debug"), cl::aliasopt(Debug));
137
138 cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false),
139   cl::desc("Print execution time for each action taken"));
140
141 cl::opt<bool> TimePasses("time-passes", cl::Optional, cl::init(false),
142   cl::desc("Print execution time for each optimization pass"));
143
144 cl::opt<bool> ShowStats("stats", cl::Optional, cl::init(false),
145   cl::desc("Print statistics accumulated during optimization"));
146
147 //===------------------------------------------------------------------------===
148 //===          ADVANCED OPTIONS
149 //===------------------------------------------------------------------------===
150
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"));
154
155 static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, cl::Optional,
156   cl::desc("Emit raw, unoptimized code"));
157
158 static cl::opt<bool> PipeCommands("pipe", cl::Optional,
159   cl::desc("Invoke sub-commands by linking input/output with pipes"));
160
161 static cl::opt<bool> KeepTemporaries("keep-temps", cl::Optional,
162   cl::desc("Don't delete the temporary files created during compilation"));
163
164 //===------------------------------------------------------------------------===
165 //===          POSITIONAL OPTIONS
166 //===------------------------------------------------------------------------===
167
168 static cl::list<std::string> Files(cl::Positional, cl::OneOrMore,
169   cl::desc("[Sources/objects/libraries]"));
170
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"));
174
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 = "";
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
200
201
202 /// @brief The main program for llvmc
203 int main(int argc, char **argv) {
204   try {
205     // Make sure we print stack trace if we get bad signals
206     PrintStackTraceOnErrorSignal();
207
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"
213     );
214
215     // Deal with unimplemented options.
216     if (PipeCommands)
217       std::cerr << argv[0] << ": Not implemented yet: -pipe";
218
219     // Default the output file, only if we're going to try to link
220     if (OutputFilename.empty() && OptLevel == CompilerDriver::LINKING)
221       OutputFilename = "a.out";
222
223     // Construct the ConfigDataProvider object
224     LLVMC_ConfigDataProvider Provider;
225     Provider.setConfigDir(ConfigDir);
226
227     // Construct the CompilerDriver object
228     CompilerDriver CD(Provider);
229
230     // 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);
254
255     // 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;
269
270       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
281     }
282
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;
288     }
289
290     // 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";
296   }
297 }