blob: 5f7bc4524b73d34f7b5087ce56f8b3d6d0d99db0 [file] [log] [blame]
Reid Spencere37345e2004-08-13 20:25:54 +00001//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
Reid Spencercf7c2fe2004-08-13 20:22:43 +00002//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file was developed by Reid Spencer and is distributed under the
7// University of Illinois Open Source License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
Reid Spencere37345e2004-08-13 20:25:54 +000011// This file implements the bulk of the LLVM Compiler Driver (llvmc).
Reid Spencercf7c2fe2004-08-13 20:22:43 +000012//
13//===------------------------------------------------------------------------===
14
15#include "CompilerDriver.h"
Reid Spencerf58e8d32004-08-15 08:19:46 +000016#include "ConfigLexer.h"
Reid Spencerf62f89b2004-08-24 13:55:17 +000017#include "llvm/Module.h"
Reid Spencer1b5b24f2004-08-29 19:26:56 +000018#include "llvm/Bytecode/Reader.h"
Reid Spencer4de872f2004-09-14 01:58:45 +000019#include "llvm/Support/Timer.h"
Reid Spencera62e5a82004-08-29 20:02:28 +000020#include "llvm/System/Signals.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/StringExtras.h"
Reid Spencercf7c2fe2004-08-13 20:22:43 +000023#include <iostream>
24
25using namespace llvm;
26
27namespace {
Reid Spencercf7c2fe2004-08-13 20:22:43 +000028
Reid Spenceraec50b92004-10-28 04:05:06 +000029void WriteAction(CompilerDriver::Action* action ) {
30 std::cerr << action->program.c_str();
31 std::vector<std::string>::iterator I = action->args.begin();
32 while (I != action->args.end()) {
33 std::cerr << " " + *I;
34 ++I;
35 }
36 std::cerr << "\n";
37}
38
39void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
41 std::vector<std::string>::iterator I = action->args.begin();
42 while (I != action->args.end()) {
43 std::cerr << " " + *I;
44 ++I;
45 }
46 std::cerr << "\n";
47 std::cerr << "flags = " << action->flags << "\n";
48}
49
50void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
51 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
52 << ")\n";
53 std::cerr << "PreProcessor: ";
54 DumpAction(&cd->PreProcessor);
55 std::cerr << "Translator: ";
56 DumpAction(&cd->Translator);
57 std::cerr << "Optimizer: ";
58 DumpAction(&cd->Optimizer);
59 std::cerr << "Assembler: ";
60 DumpAction(&cd->Assembler);
61 std::cerr << "Linker: ";
62 DumpAction(&cd->Linker);
63}
64
65/// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
66/// which should reduce the volume of code and make compilation
67/// faster. This is also safe on any llvm module.
68static const char* DefaultFastCompileOptimizations[] = {
69 "-simplifycfg", "-mem2reg", "-instcombine"
70};
71
72class CompilerDriverImpl : public CompilerDriver {
73/// @name Constructors
74/// @{
75public:
76 CompilerDriverImpl(ConfigDataProvider& confDatProv )
77 : cdp(&confDatProv)
78 , finalPhase(LINKING)
79 , optLevel(OPT_FAST_COMPILE)
80 , Flags(0)
81 , machine()
82 , LibraryPaths()
83 , TempDir()
84 , AdditionalArgs()
85 {
86 TempDir = sys::Path::GetTemporaryDirectory();
87 sys::RemoveDirectoryOnSignal(TempDir);
88 AdditionalArgs.reserve(NUM_PHASES);
89 StringVector emptyVec;
90 for (unsigned i = 0; i < NUM_PHASES; ++i)
91 AdditionalArgs.push_back(emptyVec);
92 }
93
94 virtual ~CompilerDriverImpl() {
95 cleanup();
96 cdp = 0;
97 LibraryPaths.clear();
98 IncludePaths.clear();
99 Defines.clear();
100 TempDir.clear();
101 AdditionalArgs.clear();
102 fOptions.clear();
103 MOptions.clear();
104 WOptions.clear();
105 }
106
107/// @}
108/// @name Methods
109/// @{
110public:
111 virtual void setFinalPhase( Phases phase ) {
112 finalPhase = phase;
113 }
114
115 virtual void setOptimization( OptimizationLevels level ) {
116 optLevel = level;
117 }
118
119 virtual void setDriverFlags( unsigned flags ) {
120 Flags = flags & DRIVER_FLAGS_MASK;
121 }
122
123 virtual void setOutputMachine( const std::string& machineName ) {
124 machine = machineName;
125 }
126
127 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
128 assert(phase <= LINKING && phase >= PREPROCESSING);
129 AdditionalArgs[phase] = opts;
130 }
131
132 virtual void setIncludePaths(const StringVector& paths) {
133 StringVector::const_iterator I = paths.begin();
134 StringVector::const_iterator E = paths.end();
135 while (I != E) {
136 sys::Path tmp;
Reid Spencer0c6a2832004-11-05 22:15:36 +0000137 tmp.setDirectory(*I);
Reid Spenceraec50b92004-10-28 04:05:06 +0000138 IncludePaths.push_back(tmp);
Reid Spencer9d68ff62004-08-14 09:37:15 +0000139 ++I;
140 }
Reid Spencer9d68ff62004-08-14 09:37:15 +0000141 }
142
Reid Spenceraec50b92004-10-28 04:05:06 +0000143 virtual void setSymbolDefines(const StringVector& defs) {
144 Defines = defs;
145 }
146
147 virtual void setLibraryPaths(const StringVector& paths) {
148 StringVector::const_iterator I = paths.begin();
149 StringVector::const_iterator E = paths.end();
150 while (I != E) {
151 sys::Path tmp;
Reid Spencer0c6a2832004-11-05 22:15:36 +0000152 tmp.setDirectory(*I);
Reid Spenceraec50b92004-10-28 04:05:06 +0000153 LibraryPaths.push_back(tmp);
Reid Spencerf58e8d32004-08-15 08:19:46 +0000154 ++I;
155 }
Reid Spencerf58e8d32004-08-15 08:19:46 +0000156 }
157
Reid Spenceraec50b92004-10-28 04:05:06 +0000158 virtual void addLibraryPath( const sys::Path& libPath ) {
159 LibraryPaths.push_back(libPath);
Reid Spencer9d68ff62004-08-14 09:37:15 +0000160 }
Reid Spencercf7c2fe2004-08-13 20:22:43 +0000161
Reid Spencer0c6a2832004-11-05 22:15:36 +0000162 virtual void addToolPath( const sys::Path& toolPath ) {
163 ToolPaths.push_back(toolPath);
164 }
165
Reid Spenceraec50b92004-10-28 04:05:06 +0000166 virtual void setfPassThrough(const StringVector& fOpts) {
167 fOptions = fOpts;
168 }
Reid Spencercf7c2fe2004-08-13 20:22:43 +0000169
Reid Spenceraec50b92004-10-28 04:05:06 +0000170 /// @brief Set the list of -M options to be passed through
171 virtual void setMPassThrough(const StringVector& MOpts) {
172 MOptions = MOpts;
173 }
Reid Spencerdc203892004-08-19 04:49:47 +0000174
Reid Spenceraec50b92004-10-28 04:05:06 +0000175 /// @brief Set the list of -W options to be passed through
176 virtual void setWPassThrough(const StringVector& WOpts) {
177 WOptions = WOpts;
178 }
Reid Spencercd893a92004-11-23 23:45:49 +0000179
Reid Spenceraec50b92004-10-28 04:05:06 +0000180/// @}
181/// @name Functions
182/// @{
183private:
184 bool isSet(DriverFlags flag) {
185 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
186 }
Reid Spencerdc203892004-08-19 04:49:47 +0000187
Reid Spenceraec50b92004-10-28 04:05:06 +0000188 void cleanup() {
189 if (!isSet(KEEP_TEMPS_FLAG)) {
Reid Spencer0c6a2832004-11-05 22:15:36 +0000190 if (TempDir.isDirectory() && TempDir.writable())
191 TempDir.destroyDirectory(/*remove_contents=*/true);
Reid Spenceraec50b92004-10-28 04:05:06 +0000192 } else {
193 std::cout << "Temporary files are in " << TempDir.get() << "\n";
194 }
195 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000196
Reid Spenceraec50b92004-10-28 04:05:06 +0000197 sys::Path MakeTempFile(const std::string& basename,
198 const std::string& suffix ) {
199 sys::Path result(TempDir);
Reid Spencer0c6a2832004-11-05 22:15:36 +0000200 if (!result.appendFile(basename))
Reid Spenceraec50b92004-10-28 04:05:06 +0000201 throw basename + ": can't use this file name";
Reid Spencer0c6a2832004-11-05 22:15:36 +0000202 if (!result.appendSuffix(suffix))
Reid Spenceraec50b92004-10-28 04:05:06 +0000203 throw suffix + ": can't use this file suffix";
204 return result;
205 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000206
Reid Spenceraec50b92004-10-28 04:05:06 +0000207 Action* GetAction(ConfigData* cd,
208 const sys::Path& input,
209 const sys::Path& output,
210 Phases phase)
211 {
212 Action* pat = 0; ///< The pattern/template for the action
213 Action* action = new Action; ///< The actual action to execute
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000214
Reid Spenceraec50b92004-10-28 04:05:06 +0000215 // Get the action pattern
216 switch (phase) {
217 case PREPROCESSING: pat = &cd->PreProcessor; break;
218 case TRANSLATION: pat = &cd->Translator; break;
219 case OPTIMIZATION: pat = &cd->Optimizer; break;
220 case ASSEMBLY: pat = &cd->Assembler; break;
221 case LINKING: pat = &cd->Linker; break;
222 default:
223 assert(!"Invalid driver phase!");
224 break;
225 }
226 assert(pat != 0 && "Invalid command pattern");
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000227
Reid Spenceraec50b92004-10-28 04:05:06 +0000228 // Copy over some pattern things that don't need to change
229 action->program = pat->program;
230 action->flags = pat->flags;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000231
Reid Spenceraec50b92004-10-28 04:05:06 +0000232 // Do the substitutions from the pattern to the actual
233 StringVector::iterator PI = pat->args.begin();
234 StringVector::iterator PE = pat->args.end();
235 while (PI != PE) {
236 if ((*PI)[0] == '%' && PI->length() >2) {
237 bool found = true;
238 switch ((*PI)[1]) {
239 case 'a':
240 if (*PI == "%args%") {
241 if (AdditionalArgs.size() > unsigned(phase))
242 if (!AdditionalArgs[phase].empty()) {
243 // Get specific options for each kind of action type
244 StringVector& addargs = AdditionalArgs[phase];
245 // Add specific options for each kind of action type
246 action->args.insert(action->args.end(), addargs.begin(),
247 addargs.end());
248 }
249 } else
250 found = false;
251 break;
252 case 'd':
253 if (*PI == "%defs%") {
254 StringVector::iterator I = Defines.begin();
255 StringVector::iterator E = Defines.end();
256 while (I != E) {
257 action->args.push_back( std::string("-D") + *I);
258 ++I;
259 }
260 } else
261 found = false;
262 break;
263 case 'f':
264 if (*PI == "%fOpts%") {
Reid Spencercd893a92004-11-23 23:45:49 +0000265 if (!fOptions.empty())
266 action->args.insert(action->args.end(), fOptions.begin(),
267 fOptions.end());
Reid Spenceraec50b92004-10-28 04:05:06 +0000268 } else
269 found = false;
270 break;
271 case 'i':
272 if (*PI == "%in%") {
273 action->args.push_back(input.get());
274 } else if (*PI == "%incls%") {
275 PathVector::iterator I = IncludePaths.begin();
276 PathVector::iterator E = IncludePaths.end();
277 while (I != E) {
278 action->args.push_back( std::string("-I") + I->get() );
279 ++I;
280 }
281 } else
282 found = false;
283 break;
284 case 'l':
285 if (*PI == "%libs%") {
286 PathVector::iterator I = LibraryPaths.begin();
287 PathVector::iterator E = LibraryPaths.end();
288 while (I != E) {
289 action->args.push_back( std::string("-L") + I->get() );
290 ++I;
291 }
292 } else
293 found = false;
294 break;
295 case 'o':
296 if (*PI == "%out%") {
297 action->args.push_back(output.get());
298 } else if (*PI == "%opt%") {
299 if (!isSet(EMIT_RAW_FLAG)) {
300 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
301 !cd->opts[optLevel].empty())
302 action->args.insert(action->args.end(),
303 cd->opts[optLevel].begin(),
304 cd->opts[optLevel].end());
305 else
306 throw std::string("Optimization options for level ") +
307 utostr(unsigned(optLevel)) + " were not specified";
308 }
309 } else
310 found = false;
311 break;
312 case 's':
313 if (*PI == "%stats%") {
314 if (isSet(SHOW_STATS_FLAG))
315 action->args.push_back("-stats");
316 } else
317 found = false;
318 break;
319 case 't':
320 if (*PI == "%target%") {
321 action->args.push_back(std::string("-march=") + machine);
322 } else if (*PI == "%time%") {
323 if (isSet(TIME_PASSES_FLAG))
324 action->args.push_back("-time-passes");
325 } else
326 found = false;
327 break;
328 case 'v':
329 if (*PI == "%verbose%") {
330 if (isSet(VERBOSE_FLAG))
331 action->args.push_back("-v");
332 } else
333 found = false;
334 break;
335 case 'M':
Reid Spencercd893a92004-11-23 23:45:49 +0000336 if (*PI == "%Mopts%") {
337 if (!MOptions.empty())
338 action->args.insert(action->args.end(), MOptions.begin(),
339 MOptions.end());
Reid Spenceraec50b92004-10-28 04:05:06 +0000340 } else
341 found = false;
342 break;
343 case 'W':
Reid Spencercd893a92004-11-23 23:45:49 +0000344 if (*PI == "%Wopts%") {
345 for (StringVector::iterator I = WOptions.begin(),
346 E = WOptions.end(); I != E ; ++I ) {
347 action->args.push_back( std::string("-W") + *I );
348 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000349 } else
350 found = false;
351 break;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000352 default:
Reid Spenceraec50b92004-10-28 04:05:06 +0000353 found = false;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000354 break;
355 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000356 if (!found) {
357 // Did it even look like a substitution?
358 if (PI->length()>1 && (*PI)[0] == '%' &&
359 (*PI)[PI->length()-1] == '%') {
360 throw std::string("Invalid substitution token: '") + *PI +
361 "' for command '" + pat->program.get() + "'";
Reid Spencercd893a92004-11-23 23:45:49 +0000362 } else if (!PI->empty()) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000363 // It's not a legal substitution, just pass it through
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000364 action->args.push_back(*PI);
365 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000366 }
Reid Spencercd893a92004-11-23 23:45:49 +0000367 } else if (!PI->empty()) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000368 // Its not a substitution, just put it in the action
369 action->args.push_back(*PI);
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000370 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000371 PI++;
372 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000373
Reid Spenceraec50b92004-10-28 04:05:06 +0000374 // Finally, we're done
375 return action;
376 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000377
Reid Spenceraec50b92004-10-28 04:05:06 +0000378 bool DoAction(Action*action) {
379 assert(action != 0 && "Invalid Action!");
380 if (isSet(VERBOSE_FLAG))
381 WriteAction(action);
382 if (!isSet(DRY_RUN_FLAG)) {
383 sys::Path progpath = sys::Program::FindProgramByName(
384 action->program.get());
Reid Spencer0c6a2832004-11-05 22:15:36 +0000385 if (progpath.isEmpty())
Reid Spencercd893a92004-11-23 23:45:49 +0000386 throw std::string("Can't find program '"+action->program.get()+"'");
Reid Spenceraec50b92004-10-28 04:05:06 +0000387 else if (progpath.executable())
388 action->program = progpath;
389 else
Reid Spencercd893a92004-11-23 23:45:49 +0000390 throw std::string("Program '"+action->program.get()+
391 "' is not executable.");
Reid Spenceraec50b92004-10-28 04:05:06 +0000392
393 // Invoke the program
394 if (isSet(TIME_ACTIONS_FLAG)) {
395 Timer timer(action->program.get());
396 timer.startTimer();
397 int resultCode =
398 sys::Program::ExecuteAndWait(action->program,action->args);
399 timer.stopTimer();
400 timer.print(timer,std::cerr);
401 return resultCode == 0;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000402 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000403 else
404 return 0 ==
405 sys::Program::ExecuteAndWait(action->program, action->args);
406 }
407 return true;
408 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000409
Reid Spenceraec50b92004-10-28 04:05:06 +0000410 /// This method tries various variants of a linkage item's file
411 /// name to see if it can find an appropriate file to link with
Reid Spencercd893a92004-11-23 23:45:49 +0000412 /// in the directories of the LibraryPaths.
Reid Spenceraec50b92004-10-28 04:05:06 +0000413 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraec50b92004-10-28 04:05:06 +0000414 bool native = false) {
Reid Spencercd893a92004-11-23 23:45:49 +0000415 sys::Path fullpath;
416 fullpath.setFile(link_item);
417 if (fullpath.readable())
418 return fullpath;
419 for (PathVector::iterator PI = LibraryPaths.begin(),
420 PE = LibraryPaths.end(); PI != PE; ++PI) {
421 fullpath.setDirectory(PI->get());
422 fullpath.appendFile(link_item);
Reid Spenceraec50b92004-10-28 04:05:06 +0000423 if (fullpath.readable())
424 return fullpath;
Reid Spencercd893a92004-11-23 23:45:49 +0000425 if (native) {
426 fullpath.appendSuffix("a");
427 } else {
428 fullpath.appendSuffix("bc");
429 if (fullpath.readable())
430 return fullpath;
431 fullpath.elideSuffix();
432 fullpath.appendSuffix("o");
433 if (fullpath.readable())
434 return fullpath;
435 fullpath = *PI;
436 fullpath.appendFile(std::string("lib") + link_item);
437 fullpath.appendSuffix("a");
438 if (fullpath.readable())
439 return fullpath;
440 fullpath.elideSuffix();
441 fullpath.appendSuffix("so");
442 if (fullpath.readable())
443 return fullpath;
444 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000445 }
446
447 // Didn't find one.
448 fullpath.clear();
449 return fullpath;
450 }
451
452 /// This method processes a linkage item. The item could be a
453 /// Bytecode file needing translation to native code and that is
454 /// dependent on other bytecode libraries, or a native code
455 /// library that should just be linked into the program.
456 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
457 SetVector<sys::Path>& set,
458 std::string& err) {
459 // First, see if the unadorned file name is not readable. If so,
460 // we must track down the file in the lib search path.
461 sys::Path fullpath;
462 if (!link_item.readable()) {
Reid Spencercd893a92004-11-23 23:45:49 +0000463 // look for the library using the -L arguments specified
Reid Spenceraec50b92004-10-28 04:05:06 +0000464 // on the command line.
Reid Spencercd893a92004-11-23 23:45:49 +0000465 fullpath = GetPathForLinkageItem(link_item.get());
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000466
Reid Spenceraec50b92004-10-28 04:05:06 +0000467 // If we didn't find the file in any of the library search paths
Reid Spencercd893a92004-11-23 23:45:49 +0000468 // we have to bail. No where else to look.
Reid Spencer0c6a2832004-11-05 22:15:36 +0000469 if (fullpath.isEmpty()) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000470 err =
471 std::string("Can't find linkage item '") + link_item.get() + "'";
472 return false;
473 }
474 } else {
475 fullpath = link_item;
476 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000477
Reid Spenceraec50b92004-10-28 04:05:06 +0000478 // If we got here fullpath is the path to the file, and its readable.
479 set.insert(fullpath);
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000480
Reid Spenceraec50b92004-10-28 04:05:06 +0000481 // If its an LLVM bytecode file ...
Reid Spencer0c6a2832004-11-05 22:15:36 +0000482 if (fullpath.isBytecodeFile()) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000483 // Process the dependent libraries recursively
484 Module::LibraryListType modlibs;
485 if (GetBytecodeDependentLibraries(fullpath.get(),modlibs)) {
486 // Traverse the dependent libraries list
487 Module::lib_iterator LI = modlibs.begin();
488 Module::lib_iterator LE = modlibs.end();
489 while ( LI != LE ) {
490 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
491 if (err.empty()) {
492 err = std::string("Library '") + *LI +
493 "' is not valid for linking but is required by file '" +
494 fullpath.get() + "'";
495 } else {
496 err += " which is required by file '" + fullpath.get() + "'";
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000497 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000498 return false;
499 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000500 ++LI;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000501 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000502 } else if (err.empty()) {
503 err = std::string(
504 "The dependent libraries could not be extracted from '") +
505 fullpath.get();
506 return false;
507 }
508 }
509 return true;
510 }
511
512/// @}
513/// @name Methods
514/// @{
515public:
516 virtual int execute(const InputList& InpList, const sys::Path& Output ) {
517 try {
518 // Echo the configuration of options if we're running verbose
519 if (isSet(DEBUG_FLAG)) {
520 std::cerr << "Compiler Driver Options:\n";
521 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
522 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
523 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
524 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
525 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
526 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
527 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
528 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
529 std::cerr << "OutputMachine = " << machine << "\n";
530 InputList::const_iterator I = InpList.begin();
531 while ( I != InpList.end() ) {
532 std::cerr << "Input: " << I->first.get() << "(" << I->second
533 << ")\n";
534 ++I;
535 }
536 std::cerr << "Output: " << Output.get() << "\n";
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000537 }
538
Reid Spenceraec50b92004-10-28 04:05:06 +0000539 // If there's no input, we're done.
540 if (InpList.empty())
541 throw std::string("Nothing to compile.");
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000542
Reid Spenceraec50b92004-10-28 04:05:06 +0000543 // If they are asking for linking and didn't provide an output
544 // file then its an error (no way for us to "make up" a meaningful
545 // file name based on the various linker input files).
Reid Spencer0c6a2832004-11-05 22:15:36 +0000546 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraec50b92004-10-28 04:05:06 +0000547 throw std::string(
548 "An output file name must be specified for linker output");
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000549
Reid Spenceraec50b92004-10-28 04:05:06 +0000550 // If they are not asking for linking, provided an output file and
551 // there is more than one input file, its an error
Reid Spencer0c6a2832004-11-05 22:15:36 +0000552 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraec50b92004-10-28 04:05:06 +0000553 throw std::string("An output file name cannot be specified ") +
554 "with more than one input file name when not linking";
555
556 // This vector holds all the resulting actions of the following loop.
557 std::vector<Action*> actions;
558
559 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
560 // for each input item
561 SetVector<sys::Path> LinkageItems;
562 std::vector<std::string> LibFiles;
563 InputList::const_iterator I = InpList.begin();
Reid Spencer83506092004-11-20 20:39:33 +0000564 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
565 I != E; ++I ) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000566 // Get the suffix of the file name
567 const std::string& ftype = I->second;
568
569 // If its a library, bytecode file, or object file, save
570 // it for linking below and short circuit the
571 // pre-processing/translation/assembly phases
572 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
573 // We shouldn't get any of these types of files unless we're
574 // later going to link. Enforce this limit now.
575 if (finalPhase != LINKING) {
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000576 throw std::string(
Reid Spenceraec50b92004-10-28 04:05:06 +0000577 "Pre-compiled objects found but linking not requested");
578 }
579 if (ftype.empty())
580 LibFiles.push_back(I->first.get());
581 else
582 LinkageItems.insert(I->first);
Reid Spencer83506092004-11-20 20:39:33 +0000583 continue; // short circuit remainder of loop
Reid Spenceraec50b92004-10-28 04:05:06 +0000584 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000585
Reid Spenceraec50b92004-10-28 04:05:06 +0000586 // At this point, we know its something we need to translate
587 // and/or optimize. See if we can get the configuration data
588 // for this kind of file.
589 ConfigData* cd = cdp->ProvideConfigData(I->second);
590 if (cd == 0)
591 throw std::string("Files of type '") + I->second +
592 "' are not recognized.";
593 if (isSet(DEBUG_FLAG))
594 DumpConfigData(cd,I->second);
Reid Spencer4de872f2004-09-14 01:58:45 +0000595
Reid Spencercd893a92004-11-23 23:45:49 +0000596 // Add the config data's library paths to the end of the list
597 for (StringVector::iterator LPI = cd->libpaths.begin(),
598 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
599 LibraryPaths.push_back(sys::Path(*LPI));
600 }
601
Reid Spenceraec50b92004-10-28 04:05:06 +0000602 // Initialize the input and output files
603 sys::Path InFile(I->first);
Reid Spencer0c6a2832004-11-05 22:15:36 +0000604 sys::Path OutFile(I->first.getBasename());
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000605
Reid Spenceraec50b92004-10-28 04:05:06 +0000606 // PRE-PROCESSING PHASE
607 Action& action = cd->PreProcessor;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000608
Reid Spenceraec50b92004-10-28 04:05:06 +0000609 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer0c6a2832004-11-05 22:15:36 +0000610 if (!action.program.isEmpty()) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000611 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
612 if (finalPhase == PREPROCESSING) {
Reid Spencer83506092004-11-20 20:39:33 +0000613 if (Output.isEmpty()) {
614 OutFile.appendSuffix("E");
615 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
616 } else {
617 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
618 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000619 } else {
Reid Spencer0c6a2832004-11-05 22:15:36 +0000620 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
Reid Spenceraec50b92004-10-28 04:05:06 +0000621 actions.push_back(GetAction(cd,InFile,TempFile,
622 PREPROCESSING));
623 InFile = TempFile;
624 }
625 }
626 } else if (finalPhase == PREPROCESSING) {
627 throw cd->langName + " does not support pre-processing";
628 } else if (action.isSet(REQUIRED_FLAG)) {
629 throw std::string("Don't know how to pre-process ") +
630 cd->langName + " files";
631 }
632
633 // Short-circuit remaining actions if all they want is
634 // pre-processing
Reid Spencer83506092004-11-20 20:39:33 +0000635 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraec50b92004-10-28 04:05:06 +0000636
637 /// TRANSLATION PHASE
638 action = cd->Translator;
639
640 // Get the translation action, if needed, or error if appropriate
Reid Spencer0c6a2832004-11-05 22:15:36 +0000641 if (!action.program.isEmpty()) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000642 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
643 if (finalPhase == TRANSLATION) {
Reid Spencer83506092004-11-20 20:39:33 +0000644 if (Output.isEmpty()) {
645 OutFile.appendSuffix("o");
646 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
647 } else {
648 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
649 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000650 } else {
Reid Spencer0c6a2832004-11-05 22:15:36 +0000651 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
Reid Spenceraec50b92004-10-28 04:05:06 +0000652 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
653 InFile = TempFile;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000654 }
655
Reid Spenceraec50b92004-10-28 04:05:06 +0000656 // ll -> bc Helper
657 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
658 /// The output of the translator is an LLVM Assembly program
659 /// We need to translate it to bytecode
660 Action* action = new Action();
Reid Spencer0c6a2832004-11-05 22:15:36 +0000661 action->program.setFile("llvm-as");
Reid Spenceraec50b92004-10-28 04:05:06 +0000662 action->args.push_back(InFile.get());
663 action->args.push_back("-o");
Reid Spencer0c6a2832004-11-05 22:15:36 +0000664 InFile.appendSuffix("bc");
Reid Spenceraec50b92004-10-28 04:05:06 +0000665 action->args.push_back(InFile.get());
666 actions.push_back(action);
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000667 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000668 }
669 } else if (finalPhase == TRANSLATION) {
670 throw cd->langName + " does not support translation";
671 } else if (action.isSet(REQUIRED_FLAG)) {
672 throw std::string("Don't know how to translate ") +
673 cd->langName + " files";
674 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000675
Reid Spenceraec50b92004-10-28 04:05:06 +0000676 // Short-circuit remaining actions if all they want is translation
Reid Spencer83506092004-11-20 20:39:33 +0000677 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000678
Reid Spenceraec50b92004-10-28 04:05:06 +0000679 /// OPTIMIZATION PHASE
680 action = cd->Optimizer;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000681
Reid Spenceraec50b92004-10-28 04:05:06 +0000682 // Get the optimization action, if needed, or error if appropriate
683 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer0c6a2832004-11-05 22:15:36 +0000684 if (!action.program.isEmpty()) {
Reid Spenceraec50b92004-10-28 04:05:06 +0000685 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
686 if (finalPhase == OPTIMIZATION) {
Reid Spencer83506092004-11-20 20:39:33 +0000687 if (Output.isEmpty()) {
688 OutFile.appendSuffix("o");
689 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
690 } else {
691 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
692 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000693 } else {
Reid Spencer0c6a2832004-11-05 22:15:36 +0000694 sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
Reid Spenceraec50b92004-10-28 04:05:06 +0000695 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
696 InFile = TempFile;
697 }
698 // ll -> bc Helper
699 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
700 /// The output of the optimizer is an LLVM Assembly program
701 /// We need to translate it to bytecode with llvm-as
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000702 Action* action = new Action();
Reid Spencer0c6a2832004-11-05 22:15:36 +0000703 action->program.setFile("llvm-as");
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000704 action->args.push_back(InFile.get());
705 action->args.push_back("-f");
706 action->args.push_back("-o");
Reid Spencer0c6a2832004-11-05 22:15:36 +0000707 InFile.appendSuffix("bc");
Reid Spenceraec50b92004-10-28 04:05:06 +0000708 action->args.push_back(InFile.get());
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000709 actions.push_back(action);
710 }
711 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000712 } else if (finalPhase == OPTIMIZATION) {
713 throw cd->langName + " does not support optimization";
714 } else if (action.isSet(REQUIRED_FLAG)) {
715 throw std::string("Don't know how to optimize ") +
716 cd->langName + " files";
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000717 }
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000718 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000719
720 // Short-circuit remaining actions if all they want is optimization
Reid Spencer83506092004-11-20 20:39:33 +0000721 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraec50b92004-10-28 04:05:06 +0000722
723 /// ASSEMBLY PHASE
724 action = cd->Assembler;
725
726 if (finalPhase == ASSEMBLY) {
Reid Spencer83506092004-11-20 20:39:33 +0000727
728 // Build either a native compilation action or a disassembly action
729 Action* action = new Action();
Reid Spenceraec50b92004-10-28 04:05:06 +0000730 if (isSet(EMIT_NATIVE_FLAG)) {
731 // Use llc to get the native assembly file
Reid Spencer0c6a2832004-11-05 22:15:36 +0000732 action->program.setFile("llc");
Reid Spenceraec50b92004-10-28 04:05:06 +0000733 action->args.push_back(InFile.get());
734 action->args.push_back("-f");
735 action->args.push_back("-o");
Reid Spencer83506092004-11-20 20:39:33 +0000736 if (Output.isEmpty()) {
737 OutFile.appendSuffix("o");
738 action->args.push_back(OutFile.get());
739 } else {
740 action->args.push_back(Output.get());
741 }
742 actions.push_back(action);
Reid Spenceraec50b92004-10-28 04:05:06 +0000743 } else {
744 // Just convert back to llvm assembly with llvm-dis
Reid Spencer0c6a2832004-11-05 22:15:36 +0000745 action->program.setFile("llvm-dis");
Reid Spenceraec50b92004-10-28 04:05:06 +0000746 action->args.push_back(InFile.get());
747 action->args.push_back("-f");
748 action->args.push_back("-o");
Reid Spencer83506092004-11-20 20:39:33 +0000749 if (Output.isEmpty()) {
750 OutFile.appendSuffix("ll");
751 action->args.push_back(OutFile.get());
752 } else {
753 action->args.push_back(Output.get());
754 }
Reid Spenceraec50b92004-10-28 04:05:06 +0000755 }
756
Reid Spencer83506092004-11-20 20:39:33 +0000757 // Put the action on the list
758 actions.push_back(action);
759
Reid Spenceraec50b92004-10-28 04:05:06 +0000760 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraec50b92004-10-28 04:05:06 +0000761 continue;
762 }
763
764 // Register the result of the actions as a link candidate
765 LinkageItems.insert(InFile);
766
Reid Spenceraec50b92004-10-28 04:05:06 +0000767 } // end while loop over each input file
768
769 /// RUN THE COMPILATION ACTIONS
770 std::vector<Action*>::iterator AI = actions.begin();
771 std::vector<Action*>::iterator AE = actions.end();
772 while (AI != AE) {
773 if (!DoAction(*AI))
774 throw std::string("Action failed");
775 AI++;
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000776 }
777
Reid Spenceraec50b92004-10-28 04:05:06 +0000778 /// LINKING PHASE
779 if (finalPhase == LINKING) {
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000780
Reid Spenceraec50b92004-10-28 04:05:06 +0000781 // Insert the platform-specific system libraries to the path list
782 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath1());
783 LibraryPaths.push_back(sys::Path::GetSystemLibraryPath2());
784
785 // Set up the linking action with llvm-ld
786 Action* link = new Action();
Reid Spencer0c6a2832004-11-05 22:15:36 +0000787 link->program.setFile("llvm-ld");
Reid Spenceraec50b92004-10-28 04:05:06 +0000788
789 // Add in the optimization level requested
790 switch (optLevel) {
791 case OPT_FAST_COMPILE:
792 link->args.push_back("-O1");
793 break;
794 case OPT_SIMPLE:
795 link->args.push_back("-O2");
796 break;
797 case OPT_AGGRESSIVE:
798 link->args.push_back("-O3");
799 break;
800 case OPT_LINK_TIME:
801 link->args.push_back("-O4");
802 break;
803 case OPT_AGGRESSIVE_LINK_TIME:
804 link->args.push_back("-O5");
805 break;
806 case OPT_NONE:
807 break;
808 }
809
810 // Add in all the linkage items we generated. This includes the
811 // output from the translation/optimization phases as well as any
812 // -l arguments specified.
813 for (PathVector::const_iterator I=LinkageItems.begin(),
814 E=LinkageItems.end(); I != E; ++I )
815 link->args.push_back(I->get());
816
817 // Add in all the libraries we found.
818 for (std::vector<std::string>::const_iterator I=LibFiles.begin(),
819 E=LibFiles.end(); I != E; ++I )
820 link->args.push_back(std::string("-l")+*I);
821
822 // Add in all the library paths to the command line
823 for (PathVector::const_iterator I=LibraryPaths.begin(),
824 E=LibraryPaths.end(); I != E; ++I)
825 link->args.push_back( std::string("-L") + I->get());
826
827 // Add in the additional linker arguments requested
828 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
829 E=AdditionalArgs[LINKING].end(); I != E; ++I)
830 link->args.push_back( *I );
831
832 // Add in other optional flags
833 if (isSet(EMIT_NATIVE_FLAG))
834 link->args.push_back("-native");
835 if (isSet(VERBOSE_FLAG))
836 link->args.push_back("-v");
837 if (isSet(TIME_PASSES_FLAG))
838 link->args.push_back("-time-passes");
839 if (isSet(SHOW_STATS_FLAG))
840 link->args.push_back("-stats");
841 if (isSet(STRIP_OUTPUT_FLAG))
842 link->args.push_back("-s");
843 if (isSet(DEBUG_FLAG)) {
844 link->args.push_back("-debug");
845 link->args.push_back("-debug-pass=Details");
846 }
847
848 // Add in mandatory flags
849 link->args.push_back("-o");
850 link->args.push_back(Output.get());
851
852 // Execute the link
853 if (!DoAction(link))
854 throw std::string("Action failed");
855 }
856 } catch (std::string& msg) {
857 cleanup();
858 throw;
859 } catch (...) {
860 cleanup();
861 throw std::string("Unspecified error");
862 }
863 cleanup();
864 return 0;
865 }
866
867/// @}
868/// @name Data
869/// @{
870private:
871 ConfigDataProvider* cdp; ///< Where we get configuration data from
872 Phases finalPhase; ///< The final phase of compilation
873 OptimizationLevels optLevel; ///< The optimization level to apply
874 unsigned Flags; ///< The driver flags
875 std::string machine; ///< Target machine name
876 PathVector LibraryPaths; ///< -L options
877 PathVector IncludePaths; ///< -I options
Reid Spencer0c6a2832004-11-05 22:15:36 +0000878 PathVector ToolPaths; ///< -B options
Reid Spenceraec50b92004-10-28 04:05:06 +0000879 StringVector Defines; ///< -D options
880 sys::Path TempDir; ///< Name of the temporary directory.
881 StringTable AdditionalArgs; ///< The -Txyz options
882 StringVector fOptions; ///< -f options
883 StringVector MOptions; ///< -M options
884 StringVector WOptions; ///< -W options
885
886/// @}
887};
Reid Spencercf7c2fe2004-08-13 20:22:43 +0000888}
889
890CompilerDriver::~CompilerDriver() {
Reid Spencer1b5b24f2004-08-29 19:26:56 +0000891}
892
893CompilerDriver*
894CompilerDriver::Get(ConfigDataProvider& CDP) {
895 return new CompilerDriverImpl(CDP);
Reid Spencerdc203892004-08-19 04:49:47 +0000896}
897
898CompilerDriver::ConfigData::ConfigData()
899 : langName()
900 , PreProcessor()
901 , Translator()
902 , Optimizer()
903 , Assembler()
904 , Linker()
905{
906 StringVector emptyVec;
907 for (unsigned i = 0; i < NUM_PHASES; ++i)
908 opts.push_back(emptyVec);
Reid Spencercf7c2fe2004-08-13 20:22:43 +0000909}
910
Reid Spencercf7c2fe2004-08-13 20:22:43 +0000911// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab