blob: 18aab21604b553d27145a48b014b060af963229f [file] [log] [blame]
Reid Spencera3f18552004-08-13 20:25:54 +00001//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
Reid Spencer5c56dc12004-08-13 20:22:43 +00002//
Misha Brukman3da94ae2005-04-22 00:00:37 +00003//
Reid Spencer5c56dc12004-08-13 20:22:43 +00004// The LLVM Compiler Infrastructure
5//
Misha Brukman3da94ae2005-04-22 00:00:37 +00006// This file was developed by Reid Spencer and is distributed under the
Reid Spencer5c56dc12004-08-13 20:22:43 +00007// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00008//
Reid Spencer5c56dc12004-08-13 20:22:43 +00009//===----------------------------------------------------------------------===//
10//
Reid Spencera3f18552004-08-13 20:25:54 +000011// This file implements the bulk of the LLVM Compiler Driver (llvmc).
Reid Spencer5c56dc12004-08-13 20:22:43 +000012//
Chris Lattner74f48d12006-05-29 18:52:05 +000013//===----------------------------------------------------------------------===//
Reid Spencer5c56dc12004-08-13 20:22:43 +000014
15#include "CompilerDriver.h"
Reid Spencerbf437722004-08-15 08:19:46 +000016#include "ConfigLexer.h"
Reid Spencera01439a2004-08-24 13:55:17 +000017#include "llvm/Module.h"
Reid Spencer52c2dc12004-08-29 19:26:56 +000018#include "llvm/Bytecode/Reader.h"
Reid Spencer54fafe42004-09-14 01:58:45 +000019#include "llvm/Support/Timer.h"
Reid Spencer93426ba2004-08-29 20:02:28 +000020#include "llvm/System/Signals.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/SetVector.h"
22#include "llvm/ADT/StringExtras.h"
Brian Gaekefabf41f2004-12-20 04:02:01 +000023#include "llvm/Config/alloca.h"
Misha Brukmanbaec07c2005-04-20 04:51:29 +000024#include <iostream>
Reid Spencer5c56dc12004-08-13 20:22:43 +000025using namespace llvm;
26
27namespace {
Reid Spencer5c56dc12004-08-13 20:22:43 +000028
Reid Spenceraf77d742004-10-28 04:05:06 +000029void WriteAction(CompilerDriver::Action* action ) {
30 std::cerr << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000031 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000032 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000033 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000034 ++I;
35 }
Misha Brukman0b861482005-05-03 20:30:34 +000036 std::cerr << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000037}
38
39void DumpAction(CompilerDriver::Action* action) {
40 std::cerr << "command = " << action->program.c_str();
Reid Spencerf6358c72004-12-19 18:00:56 +000041 std::vector<std::string>::const_iterator I = action->args.begin();
Reid Spenceraf77d742004-10-28 04:05:06 +000042 while (I != action->args.end()) {
Misha Brukman0b861482005-05-03 20:30:34 +000043 std::cerr << ' ' << *I;
Reid Spenceraf77d742004-10-28 04:05:06 +000044 ++I;
45 }
Misha Brukman0b861482005-05-03 20:30:34 +000046 std::cerr << '\n';
47 std::cerr << "flags = " << action->flags << '\n';
Reid Spenceraf77d742004-10-28 04:05:06 +000048}
49
50void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
Misha Brukman3da94ae2005-04-22 00:00:37 +000051 std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
Reid Spenceraf77d742004-10-28 04:05:06 +000052 << ")\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
Reid Spenceraf77d742004-10-28 04:05:06 +000065class CompilerDriverImpl : public CompilerDriver {
66/// @name Constructors
67/// @{
68public:
69 CompilerDriverImpl(ConfigDataProvider& confDatProv )
70 : cdp(&confDatProv)
71 , finalPhase(LINKING)
Misha Brukman3da94ae2005-04-22 00:00:37 +000072 , optLevel(OPT_FAST_COMPILE)
Reid Spenceraf77d742004-10-28 04:05:06 +000073 , Flags(0)
74 , machine()
75 , LibraryPaths()
76 , TempDir()
77 , AdditionalArgs()
78 {
Reid Spenceraf77d742004-10-28 04:05:06 +000079 AdditionalArgs.reserve(NUM_PHASES);
80 StringVector emptyVec;
81 for (unsigned i = 0; i < NUM_PHASES; ++i)
82 AdditionalArgs.push_back(emptyVec);
83 }
84
85 virtual ~CompilerDriverImpl() {
86 cleanup();
87 cdp = 0;
88 LibraryPaths.clear();
89 IncludePaths.clear();
90 Defines.clear();
91 TempDir.clear();
92 AdditionalArgs.clear();
93 fOptions.clear();
94 MOptions.clear();
95 WOptions.clear();
96 }
97
98/// @}
99/// @name Methods
100/// @{
101public:
Misha Brukman0b861482005-05-03 20:30:34 +0000102 virtual void setFinalPhase(Phases phase) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000103 finalPhase = phase;
Reid Spenceraf77d742004-10-28 04:05:06 +0000104 }
105
Misha Brukman0b861482005-05-03 20:30:34 +0000106 virtual void setOptimization(OptimizationLevels level) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000107 optLevel = level;
Reid Spenceraf77d742004-10-28 04:05:06 +0000108 }
109
Misha Brukman0b861482005-05-03 20:30:34 +0000110 virtual void setDriverFlags(unsigned flags) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000111 Flags = flags & DRIVER_FLAGS_MASK;
Reid Spenceraf77d742004-10-28 04:05:06 +0000112 }
113
Misha Brukman0b861482005-05-03 20:30:34 +0000114 virtual void setOutputMachine(const std::string& machineName) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000115 machine = machineName;
116 }
117
118 virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
119 assert(phase <= LINKING && phase >= PREPROCESSING);
120 AdditionalArgs[phase] = opts;
121 }
122
123 virtual void setIncludePaths(const StringVector& paths) {
124 StringVector::const_iterator I = paths.begin();
125 StringVector::const_iterator E = paths.end();
126 while (I != E) {
127 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000128 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000129 IncludePaths.push_back(tmp);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000130 ++I;
131 }
Reid Spencer68fb37a2004-08-14 09:37:15 +0000132 }
133
Reid Spenceraf77d742004-10-28 04:05:06 +0000134 virtual void setSymbolDefines(const StringVector& defs) {
135 Defines = defs;
136 }
137
138 virtual void setLibraryPaths(const StringVector& paths) {
139 StringVector::const_iterator I = paths.begin();
140 StringVector::const_iterator E = paths.end();
141 while (I != E) {
142 sys::Path tmp;
Reid Spencerdd04df02005-07-07 23:21:43 +0000143 tmp.set(*I);
Reid Spenceraf77d742004-10-28 04:05:06 +0000144 LibraryPaths.push_back(tmp);
Reid Spencerbf437722004-08-15 08:19:46 +0000145 ++I;
146 }
Reid Spencerbf437722004-08-15 08:19:46 +0000147 }
148
Misha Brukman0b861482005-05-03 20:30:34 +0000149 virtual void addLibraryPath(const sys::Path& libPath) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000150 LibraryPaths.push_back(libPath);
Reid Spencer68fb37a2004-08-14 09:37:15 +0000151 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000152
Misha Brukman0b861482005-05-03 20:30:34 +0000153 virtual void addToolPath(const sys::Path& toolPath) {
Reid Spencer07adb282004-11-05 22:15:36 +0000154 ToolPaths.push_back(toolPath);
155 }
156
Reid Spenceraf77d742004-10-28 04:05:06 +0000157 virtual void setfPassThrough(const StringVector& fOpts) {
158 fOptions = fOpts;
159 }
Reid Spencer5c56dc12004-08-13 20:22:43 +0000160
Reid Spenceraf77d742004-10-28 04:05:06 +0000161 /// @brief Set the list of -M options to be passed through
162 virtual void setMPassThrough(const StringVector& MOpts) {
163 MOptions = MOpts;
164 }
Reid Spencerbae68252004-08-19 04:49:47 +0000165
Reid Spenceraf77d742004-10-28 04:05:06 +0000166 /// @brief Set the list of -W options to be passed through
167 virtual void setWPassThrough(const StringVector& WOpts) {
168 WOptions = WOpts;
169 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000170
Reid Spenceraf77d742004-10-28 04:05:06 +0000171/// @}
172/// @name Functions
173/// @{
174private:
175 bool isSet(DriverFlags flag) {
176 return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
177 }
Reid Spencerbae68252004-08-19 04:49:47 +0000178
Reid Spenceraf77d742004-10-28 04:05:06 +0000179 void cleanup() {
180 if (!isSet(KEEP_TEMPS_FLAG)) {
Chris Lattner33b0e9c2006-08-01 18:12:29 +0000181 sys::FileStatus Status;
182 if (!TempDir.getFileStatus(Status) && Status.isDir)
Reid Spencera229c5c2005-07-08 03:08:58 +0000183 TempDir.eraseFromDisk(/*remove_contents=*/true);
Reid Spenceraf77d742004-10-28 04:05:06 +0000184 } else {
Reid Spencer12786d52004-12-13 08:53:36 +0000185 std::cout << "Temporary files are in " << TempDir << "\n";
Reid Spenceraf77d742004-10-28 04:05:06 +0000186 }
187 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000188
Misha Brukman3da94ae2005-04-22 00:00:37 +0000189 sys::Path MakeTempFile(const std::string& basename,
Reid Spencer48744762006-08-22 19:01:30 +0000190 const std::string& suffix,
191 std::string* ErrMsg) {
192 if (TempDir.isEmpty()) {
193 TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
194 if (TempDir.isEmpty())
195 return sys::Path();
196 sys::RemoveDirectoryOnSignal(TempDir);
197 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000198 sys::Path result(TempDir);
Reid Spencer48744762006-08-22 19:01:30 +0000199 if (!result.appendComponent(basename)) {
200 if (ErrMsg)
201 *ErrMsg = basename + ": can't use this file name";
202 return sys::Path();
203 }
204 if (!result.appendSuffix(suffix)) {
205 if (ErrMsg)
206 *ErrMsg = suffix + ": can't use this file suffix";
207 return sys::Path();
208 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000209 return result;
210 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000211
Misha Brukman3da94ae2005-04-22 00:00:37 +0000212 Action* GetAction(ConfigData* cd,
213 const sys::Path& input,
Reid Spenceraf77d742004-10-28 04:05:06 +0000214 const sys::Path& output,
215 Phases phase)
216 {
217 Action* pat = 0; ///< The pattern/template for the action
218 Action* action = new Action; ///< The actual action to execute
Reid Spencer52c2dc12004-08-29 19:26:56 +0000219
Reid Spenceraf77d742004-10-28 04:05:06 +0000220 // Get the action pattern
221 switch (phase) {
222 case PREPROCESSING: pat = &cd->PreProcessor; break;
223 case TRANSLATION: pat = &cd->Translator; break;
224 case OPTIMIZATION: pat = &cd->Optimizer; break;
225 case ASSEMBLY: pat = &cd->Assembler; break;
226 case LINKING: pat = &cd->Linker; break;
227 default:
228 assert(!"Invalid driver phase!");
229 break;
230 }
231 assert(pat != 0 && "Invalid command pattern");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000232
Reid Spenceraf77d742004-10-28 04:05:06 +0000233 // Copy over some pattern things that don't need to change
Reid Spenceraf77d742004-10-28 04:05:06 +0000234 action->flags = pat->flags;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000235
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000236 // See if program starts with wildcard...
237 std::string programName=pat->program.toString();
238 if (programName[0] == '%' && programName.length() >2) {
239 switch(programName[1]){
240 case 'b':
241 if (programName.substr(0,8) == "%bindir%") {
242 std::string tmp(LLVM_BINDIR);
243 tmp.append(programName.substr(8));
244 pat->program.set(tmp);
245 }
246 break;
247 case 'l':
248 if (programName.substr(0,12) == "%llvmgccdir%"){
249 std::string tmp(LLVMGCCDIR);
250 tmp.append(programName.substr(12));
251 pat->program.set(tmp);
252 }else if (programName.substr(0,13) == "%llvmgccarch%"){
253 std::string tmp(LLVMGCCARCH);
254 tmp.append(programName.substr(13));
255 pat->program.set(tmp);
256 }else if (programName.substr(0,9) == "%llvmgcc%"){
257 std::string tmp(LLVMGCC);
258 tmp.append(programName.substr(9));
259 pat->program.set(tmp);
260 }else if (programName.substr(0,9) == "%llvmgxx%"){
261 std::string tmp(LLVMGXX);
262 tmp.append(programName.substr(9));
263 pat->program.set(tmp);
264 }else if (programName.substr(0,9) == "%llvmcc1%"){
265 std::string tmp(LLVMCC1);
266 tmp.append(programName.substr(9));
267 pat->program.set(tmp);
268 }else if (programName.substr(0,13) == "%llvmcc1plus%"){
269 std::string tmp(LLVMCC1PLUS);
270 tmp.append(programName.substr(13));
271 pat->program.set(tmp);
272 }else if (programName.substr(0,8) == "%libdir%") {
273 std::string tmp(LLVM_LIBDIR);
274 tmp.append(programName.substr(8));
275 pat->program.set(tmp);
276 }
277 break;
278 }
279 }
280 action->program = pat->program;
281
Reid Spenceraf77d742004-10-28 04:05:06 +0000282 // Do the substitutions from the pattern to the actual
283 StringVector::iterator PI = pat->args.begin();
284 StringVector::iterator PE = pat->args.end();
285 while (PI != PE) {
286 if ((*PI)[0] == '%' && PI->length() >2) {
287 bool found = true;
288 switch ((*PI)[1]) {
289 case 'a':
290 if (*PI == "%args%") {
291 if (AdditionalArgs.size() > unsigned(phase))
292 if (!AdditionalArgs[phase].empty()) {
293 // Get specific options for each kind of action type
294 StringVector& addargs = AdditionalArgs[phase];
295 // Add specific options for each kind of action type
Misha Brukman3da94ae2005-04-22 00:00:37 +0000296 action->args.insert(action->args.end(), addargs.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000297 addargs.end());
298 }
299 } else
300 found = false;
301 break;
Reid Spencercc97cfc2005-05-19 00:52:28 +0000302 case 'b':
303 if (*PI == "%bindir%") {
304 std::string tmp(*PI);
305 tmp.replace(0,8,LLVM_BINDIR);
306 action->args.push_back(tmp);
307 } else
308 found = false;
309 break;
Reid Spenceraf77d742004-10-28 04:05:06 +0000310 case 'd':
311 if (*PI == "%defs%") {
312 StringVector::iterator I = Defines.begin();
313 StringVector::iterator E = Defines.end();
314 while (I != E) {
315 action->args.push_back( std::string("-D") + *I);
316 ++I;
317 }
318 } else
319 found = false;
320 break;
321 case 'f':
322 if (*PI == "%fOpts%") {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000323 if (!fOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000324 action->args.insert(action->args.end(), fOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000325 fOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000326 } else
327 found = false;
328 break;
329 case 'i':
330 if (*PI == "%in%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000331 action->args.push_back(input.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000332 } else if (*PI == "%incls%") {
333 PathVector::iterator I = IncludePaths.begin();
334 PathVector::iterator E = IncludePaths.end();
335 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000336 action->args.push_back( std::string("-I") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000337 ++I;
338 }
339 } else
340 found = false;
341 break;
342 case 'l':
Reid Spencercc97cfc2005-05-19 00:52:28 +0000343 if ((*PI)[1] == 'l') {
344 std::string tmp(*PI);
345 if (*PI == "%llvmgccdir%")
346 tmp.replace(0,12,LLVMGCCDIR);
347 else if (*PI == "%llvmgccarch%")
348 tmp.replace(0,13,LLVMGCCARCH);
349 else if (*PI == "%llvmgcc%")
350 tmp.replace(0,9,LLVMGCC);
351 else if (*PI == "%llvmgxx%")
352 tmp.replace(0,9,LLVMGXX);
353 else if (*PI == "%llvmcc1%")
354 tmp.replace(0,9,LLVMCC1);
Jeff Cohen00b168892005-07-27 06:12:32 +0000355 else if (*PI == "%llvmcc1plus%")
Reid Spencercc97cfc2005-05-19 00:52:28 +0000356 tmp.replace(0,9,LLVMCC1);
357 else
358 found = false;
359 if (found)
360 action->args.push_back(tmp);
361 } else if (*PI == "%libs%") {
Reid Spenceraf77d742004-10-28 04:05:06 +0000362 PathVector::iterator I = LibraryPaths.begin();
363 PathVector::iterator E = LibraryPaths.end();
364 while (I != E) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000365 action->args.push_back( std::string("-L") + I->toString() );
Reid Spenceraf77d742004-10-28 04:05:06 +0000366 ++I;
367 }
Reid Spencercc97cfc2005-05-19 00:52:28 +0000368 } else if (*PI == "%libdir%") {
369 std::string tmp(*PI);
370 tmp.replace(0,8,LLVM_LIBDIR);
371 action->args.push_back(tmp);
Reid Spenceraf77d742004-10-28 04:05:06 +0000372 } else
373 found = false;
374 break;
375 case 'o':
376 if (*PI == "%out%") {
Reid Spencer1fce0912004-12-11 00:14:15 +0000377 action->args.push_back(output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000378 } else if (*PI == "%opt%") {
379 if (!isSet(EMIT_RAW_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000380 if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000381 !cd->opts[optLevel].empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000382 action->args.insert(action->args.end(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000383 cd->opts[optLevel].begin(),
384 cd->opts[optLevel].end());
385 else
Misha Brukman3da94ae2005-04-22 00:00:37 +0000386 throw std::string("Optimization options for level ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000387 utostr(unsigned(optLevel)) + " were not specified";
388 }
389 } else
390 found = false;
391 break;
392 case 's':
393 if (*PI == "%stats%") {
394 if (isSet(SHOW_STATS_FLAG))
395 action->args.push_back("-stats");
396 } else
397 found = false;
398 break;
399 case 't':
400 if (*PI == "%target%") {
401 action->args.push_back(std::string("-march=") + machine);
402 } else if (*PI == "%time%") {
403 if (isSet(TIME_PASSES_FLAG))
404 action->args.push_back("-time-passes");
405 } else
406 found = false;
407 break;
408 case 'v':
409 if (*PI == "%verbose%") {
410 if (isSet(VERBOSE_FLAG))
411 action->args.push_back("-v");
412 } else
413 found = false;
414 break;
415 case 'M':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000416 if (*PI == "%Mopts%") {
417 if (!MOptions.empty())
Misha Brukman3da94ae2005-04-22 00:00:37 +0000418 action->args.insert(action->args.end(), MOptions.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000419 MOptions.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000420 } else
421 found = false;
422 break;
423 case 'W':
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000424 if (*PI == "%Wopts%") {
425 for (StringVector::iterator I = WOptions.begin(),
426 E = WOptions.end(); I != E ; ++I ) {
Misha Brukman0b861482005-05-03 20:30:34 +0000427 action->args.push_back(std::string("-W") + *I);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000428 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000429 } else
430 found = false;
431 break;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000432 default:
Reid Spenceraf77d742004-10-28 04:05:06 +0000433 found = false;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000434 break;
435 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000436 if (!found) {
437 // Did it even look like a substitution?
Misha Brukman3da94ae2005-04-22 00:00:37 +0000438 if (PI->length()>1 && (*PI)[0] == '%' &&
Reid Spenceraf77d742004-10-28 04:05:06 +0000439 (*PI)[PI->length()-1] == '%') {
440 throw std::string("Invalid substitution token: '") + *PI +
Reid Spencer1fce0912004-12-11 00:14:15 +0000441 "' for command '" + pat->program.toString() + "'";
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000442 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000443 // It's not a legal substitution, just pass it through
Reid Spencer52c2dc12004-08-29 19:26:56 +0000444 action->args.push_back(*PI);
445 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000446 }
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000447 } else if (!PI->empty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000448 // Its not a substitution, just put it in the action
449 action->args.push_back(*PI);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000450 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000451 PI++;
452 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000453
Reid Spenceraf77d742004-10-28 04:05:06 +0000454 // Finally, we're done
455 return action;
456 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000457
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000458 int DoAction(Action*action, std::string& ErrMsg) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000459 assert(action != 0 && "Invalid Action!");
460 if (isSet(VERBOSE_FLAG))
461 WriteAction(action);
462 if (!isSet(DRY_RUN_FLAG)) {
463 sys::Path progpath = sys::Program::FindProgramByName(
Reid Spencer1fce0912004-12-11 00:14:15 +0000464 action->program.toString());
Reid Spencer07adb282004-11-05 22:15:36 +0000465 if (progpath.isEmpty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000466 throw std::string("Can't find program '" +
467 action->program.toString()+"'");
Reid Spencerc7f08322005-07-07 18:21:42 +0000468 else if (progpath.canExecute())
Reid Spenceraf77d742004-10-28 04:05:06 +0000469 action->program = progpath;
470 else
Reid Spencer1fce0912004-12-11 00:14:15 +0000471 throw std::string("Program '"+action->program.toString()+
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000472 "' is not executable.");
Reid Spenceraf77d742004-10-28 04:05:06 +0000473
474 // Invoke the program
Misha Brukman3da94ae2005-04-22 00:00:37 +0000475 const char** Args = (const char**)
Reid Spencerc30088f2005-04-11 05:48:04 +0000476 alloca(sizeof(const char*)*(action->args.size()+2));
477 Args[0] = action->program.toString().c_str();
Reid Spencer3b4c5d72006-08-16 20:31:44 +0000478 for (unsigned i = 1; i <= action->args.size(); ++i)
479 Args[i] = action->args[i-1].c_str();
480 Args[action->args.size()+1] = 0; // null terminate list.
Reid Spenceraf77d742004-10-28 04:05:06 +0000481 if (isSet(TIME_ACTIONS_FLAG)) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000482 Timer timer(action->program.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000483 timer.startTimer();
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000484 int resultCode =
485 sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000486 timer.stopTimer();
487 timer.print(timer,std::cerr);
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000488 return resultCode;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000489 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000490 else
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000491 return
492 sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg);
Reid Spenceraf77d742004-10-28 04:05:06 +0000493 }
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000494 return 0;
Reid Spenceraf77d742004-10-28 04:05:06 +0000495 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000496
Reid Spenceraf77d742004-10-28 04:05:06 +0000497 /// This method tries various variants of a linkage item's file
498 /// name to see if it can find an appropriate file to link with
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000499 /// in the directories of the LibraryPaths.
Reid Spenceraf77d742004-10-28 04:05:06 +0000500 llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
Reid Spenceraf77d742004-10-28 04:05:06 +0000501 bool native = false) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000502 sys::Path fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000503 fullpath.set(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000504 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000505 return fullpath;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000506 for (PathVector::iterator PI = LibraryPaths.begin(),
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000507 PE = LibraryPaths.end(); PI != PE; ++PI) {
Reid Spencerdd04df02005-07-07 23:21:43 +0000508 fullpath.set(PI->toString());
509 fullpath.appendComponent(link_item);
Reid Spencerc7f08322005-07-07 18:21:42 +0000510 if (fullpath.canRead())
Reid Spenceraf77d742004-10-28 04:05:06 +0000511 return fullpath;
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000512 if (native) {
513 fullpath.appendSuffix("a");
514 } else {
515 fullpath.appendSuffix("bc");
Reid Spencerc7f08322005-07-07 18:21:42 +0000516 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000517 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000518 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000519 fullpath.appendSuffix("o");
Reid Spencerc7f08322005-07-07 18:21:42 +0000520 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000521 return fullpath;
522 fullpath = *PI;
Reid Spencerdd04df02005-07-07 23:21:43 +0000523 fullpath.appendComponent(std::string("lib") + link_item);
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000524 fullpath.appendSuffix("a");
Reid Spencerc7f08322005-07-07 18:21:42 +0000525 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000526 return fullpath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000527 fullpath.eraseSuffix();
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000528 fullpath.appendSuffix("so");
Reid Spencerc7f08322005-07-07 18:21:42 +0000529 if (fullpath.canRead())
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000530 return fullpath;
531 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000532 }
533
534 // Didn't find one.
535 fullpath.clear();
536 return fullpath;
537 }
538
539 /// This method processes a linkage item. The item could be a
540 /// Bytecode file needing translation to native code and that is
541 /// dependent on other bytecode libraries, or a native code
542 /// library that should just be linked into the program.
543 bool ProcessLinkageItem(const llvm::sys::Path& link_item,
544 SetVector<sys::Path>& set,
545 std::string& err) {
546 // First, see if the unadorned file name is not readable. If so,
547 // we must track down the file in the lib search path.
548 sys::Path fullpath;
Reid Spencerc7f08322005-07-07 18:21:42 +0000549 if (!link_item.canRead()) {
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000550 // look for the library using the -L arguments specified
Reid Spenceraf77d742004-10-28 04:05:06 +0000551 // on the command line.
Reid Spencer1fce0912004-12-11 00:14:15 +0000552 fullpath = GetPathForLinkageItem(link_item.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000553
Reid Spenceraf77d742004-10-28 04:05:06 +0000554 // If we didn't find the file in any of the library search paths
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000555 // we have to bail. No where else to look.
Reid Spencer07adb282004-11-05 22:15:36 +0000556 if (fullpath.isEmpty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000557 err =
Reid Spencer1fce0912004-12-11 00:14:15 +0000558 std::string("Can't find linkage item '") + link_item.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000559 return false;
560 }
561 } else {
562 fullpath = link_item;
563 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000564
Reid Spenceraf77d742004-10-28 04:05:06 +0000565 // If we got here fullpath is the path to the file, and its readable.
566 set.insert(fullpath);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000567
Reid Spenceraf77d742004-10-28 04:05:06 +0000568 // If its an LLVM bytecode file ...
Reid Spencer07adb282004-11-05 22:15:36 +0000569 if (fullpath.isBytecodeFile()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000570 // Process the dependent libraries recursively
571 Module::LibraryListType modlibs;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000572 if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,&err)) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000573 // Traverse the dependent libraries list
574 Module::lib_iterator LI = modlibs.begin();
575 Module::lib_iterator LE = modlibs.end();
576 while ( LI != LE ) {
577 if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
578 if (err.empty()) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000579 err = std::string("Library '") + *LI +
Reid Spenceraf77d742004-10-28 04:05:06 +0000580 "' is not valid for linking but is required by file '" +
Reid Spencer1fce0912004-12-11 00:14:15 +0000581 fullpath.toString() + "'";
Reid Spenceraf77d742004-10-28 04:05:06 +0000582 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000583 err += " which is required by file '" + fullpath.toString() + "'";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000584 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000585 return false;
586 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000587 ++LI;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000588 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000589 } else if (err.empty()) {
590 err = std::string(
Misha Brukman3da94ae2005-04-22 00:00:37 +0000591 "The dependent libraries could not be extracted from '") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000592 fullpath.toString();
Reid Spenceraf77d742004-10-28 04:05:06 +0000593 return false;
Reid Spencer0b5a5042006-08-25 17:43:11 +0000594 } else
595 return false;
Reid Spenceraf77d742004-10-28 04:05:06 +0000596 }
597 return true;
598 }
599
600/// @}
601/// @name Methods
602/// @{
603public:
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000604 virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000605 try {
606 // Echo the configuration of options if we're running verbose
607 if (isSet(DEBUG_FLAG)) {
608 std::cerr << "Compiler Driver Options:\n";
609 std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
610 std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
611 std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
612 std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
613 std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
614 std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
615 std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
616 std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
617 std::cerr << "OutputMachine = " << machine << "\n";
618 InputList::const_iterator I = InpList.begin();
619 while ( I != InpList.end() ) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000620 std::cerr << "Input: " << I->first << "(" << I->second
Reid Spenceraf77d742004-10-28 04:05:06 +0000621 << ")\n";
622 ++I;
623 }
Reid Spencer12786d52004-12-13 08:53:36 +0000624 std::cerr << "Output: " << Output << "\n";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000625 }
626
Reid Spenceraf77d742004-10-28 04:05:06 +0000627 // If there's no input, we're done.
628 if (InpList.empty())
629 throw std::string("Nothing to compile.");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000630
Reid Spenceraf77d742004-10-28 04:05:06 +0000631 // If they are asking for linking and didn't provide an output
632 // file then its an error (no way for us to "make up" a meaningful
633 // file name based on the various linker input files).
Reid Spencer07adb282004-11-05 22:15:36 +0000634 if (finalPhase == LINKING && Output.isEmpty())
Reid Spenceraf77d742004-10-28 04:05:06 +0000635 throw std::string(
636 "An output file name must be specified for linker output");
Reid Spencer52c2dc12004-08-29 19:26:56 +0000637
Reid Spenceraf77d742004-10-28 04:05:06 +0000638 // If they are not asking for linking, provided an output file and
639 // there is more than one input file, its an error
Reid Spencer07adb282004-11-05 22:15:36 +0000640 if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
Reid Spenceraf77d742004-10-28 04:05:06 +0000641 throw std::string("An output file name cannot be specified ") +
642 "with more than one input file name when not linking";
643
644 // This vector holds all the resulting actions of the following loop.
645 std::vector<Action*> actions;
646
647 /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
648 // for each input item
649 SetVector<sys::Path> LinkageItems;
Reid Spencerf6358c72004-12-19 18:00:56 +0000650 StringVector LibFiles;
Reid Spenceraf77d742004-10-28 04:05:06 +0000651 InputList::const_iterator I = InpList.begin();
Reid Spencer679a7232004-11-20 20:39:33 +0000652 for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
653 I != E; ++I ) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000654 // Get the suffix of the file name
655 const std::string& ftype = I->second;
656
Misha Brukman3da94ae2005-04-22 00:00:37 +0000657 // If its a library, bytecode file, or object file, save
658 // it for linking below and short circuit the
Reid Spenceraf77d742004-10-28 04:05:06 +0000659 // pre-processing/translation/assembly phases
660 if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000661 // We shouldn't get any of these types of files unless we're
Reid Spenceraf77d742004-10-28 04:05:06 +0000662 // later going to link. Enforce this limit now.
663 if (finalPhase != LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000664 throw std::string(
Reid Spenceraf77d742004-10-28 04:05:06 +0000665 "Pre-compiled objects found but linking not requested");
666 }
667 if (ftype.empty())
Reid Spencer1fce0912004-12-11 00:14:15 +0000668 LibFiles.push_back(I->first.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000669 else
670 LinkageItems.insert(I->first);
Reid Spencer679a7232004-11-20 20:39:33 +0000671 continue; // short circuit remainder of loop
Reid Spenceraf77d742004-10-28 04:05:06 +0000672 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000673
Reid Spenceraf77d742004-10-28 04:05:06 +0000674 // At this point, we know its something we need to translate
675 // and/or optimize. See if we can get the configuration data
676 // for this kind of file.
677 ConfigData* cd = cdp->ProvideConfigData(I->second);
678 if (cd == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +0000679 throw std::string("Files of type '") + I->second +
680 "' are not recognized.";
Reid Spenceraf77d742004-10-28 04:05:06 +0000681 if (isSet(DEBUG_FLAG))
682 DumpConfigData(cd,I->second);
Reid Spencer54fafe42004-09-14 01:58:45 +0000683
Reid Spencer0b3c7d02004-11-23 23:45:49 +0000684 // Add the config data's library paths to the end of the list
685 for (StringVector::iterator LPI = cd->libpaths.begin(),
686 LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
687 LibraryPaths.push_back(sys::Path(*LPI));
688 }
689
Reid Spenceraf77d742004-10-28 04:05:06 +0000690 // Initialize the input and output files
691 sys::Path InFile(I->first);
Reid Spencer07adb282004-11-05 22:15:36 +0000692 sys::Path OutFile(I->first.getBasename());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000693
Reid Spenceraf77d742004-10-28 04:05:06 +0000694 // PRE-PROCESSING PHASE
695 Action& action = cd->PreProcessor;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000696
Reid Spenceraf77d742004-10-28 04:05:06 +0000697 // Get the preprocessing action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000698 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000699 if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
700 if (finalPhase == PREPROCESSING) {
Reid Spencer679a7232004-11-20 20:39:33 +0000701 if (Output.isEmpty()) {
702 OutFile.appendSuffix("E");
703 actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
704 } else {
705 actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
706 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000707 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000708 sys::Path TempFile(
709 MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
710 if (TempFile.isEmpty())
711 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000712 actions.push_back(GetAction(cd,InFile,TempFile,
713 PREPROCESSING));
714 InFile = TempFile;
715 }
716 }
717 } else if (finalPhase == PREPROCESSING) {
718 throw cd->langName + " does not support pre-processing";
719 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000720 throw std::string("Don't know how to pre-process ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000721 cd->langName + " files";
722 }
723
Misha Brukman3da94ae2005-04-22 00:00:37 +0000724 // Short-circuit remaining actions if all they want is
Reid Spenceraf77d742004-10-28 04:05:06 +0000725 // pre-processing
Reid Spencer679a7232004-11-20 20:39:33 +0000726 if (finalPhase == PREPROCESSING) { continue; };
Reid Spenceraf77d742004-10-28 04:05:06 +0000727
728 /// TRANSLATION PHASE
729 action = cd->Translator;
730
731 // Get the translation action, if needed, or error if appropriate
Reid Spencer07adb282004-11-05 22:15:36 +0000732 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000733 if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
734 if (finalPhase == TRANSLATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000735 if (Output.isEmpty()) {
736 OutFile.appendSuffix("o");
737 actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
738 } else {
739 actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
740 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000741 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000742 sys::Path TempFile(
743 MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
744 if (TempFile.isEmpty())
745 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000746 actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
747 InFile = TempFile;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000748 }
749
Reid Spenceraf77d742004-10-28 04:05:06 +0000750 // ll -> bc Helper
751 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
752 /// The output of the translator is an LLVM Assembly program
753 /// We need to translate it to bytecode
754 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000755 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000756 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000757 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000758 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000759 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000760 actions.push_back(action);
Reid Spencer52c2dc12004-08-29 19:26:56 +0000761 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000762 }
763 } else if (finalPhase == TRANSLATION) {
764 throw cd->langName + " does not support translation";
765 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000766 throw std::string("Don't know how to translate ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000767 cd->langName + " files";
768 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000769
Reid Spenceraf77d742004-10-28 04:05:06 +0000770 // Short-circuit remaining actions if all they want is translation
Reid Spencer679a7232004-11-20 20:39:33 +0000771 if (finalPhase == TRANSLATION) { continue; }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000772
Reid Spenceraf77d742004-10-28 04:05:06 +0000773 /// OPTIMIZATION PHASE
774 action = cd->Optimizer;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000775
Reid Spenceraf77d742004-10-28 04:05:06 +0000776 // Get the optimization action, if needed, or error if appropriate
777 if (!isSet(EMIT_RAW_FLAG)) {
Reid Spencer07adb282004-11-05 22:15:36 +0000778 if (!action.program.isEmpty()) {
Reid Spenceraf77d742004-10-28 04:05:06 +0000779 if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
780 if (finalPhase == OPTIMIZATION) {
Reid Spencer679a7232004-11-20 20:39:33 +0000781 if (Output.isEmpty()) {
782 OutFile.appendSuffix("o");
783 actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
784 } else {
785 actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
786 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000787 } else {
Reid Spencer48744762006-08-22 19:01:30 +0000788 sys::Path TempFile(
789 MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
790 if (TempFile.isEmpty())
791 return 1;
Reid Spenceraf77d742004-10-28 04:05:06 +0000792 actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
793 InFile = TempFile;
794 }
795 // ll -> bc Helper
796 if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
797 /// The output of the optimizer is an LLVM Assembly program
798 /// We need to translate it to bytecode with llvm-as
Reid Spencer52c2dc12004-08-29 19:26:56 +0000799 Action* action = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000800 action->program.set("llvm-as");
Reid Spencer1fce0912004-12-11 00:14:15 +0000801 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000802 action->args.push_back("-f");
803 action->args.push_back("-o");
Reid Spencer07adb282004-11-05 22:15:36 +0000804 InFile.appendSuffix("bc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000805 action->args.push_back(InFile.toString());
Reid Spencer52c2dc12004-08-29 19:26:56 +0000806 actions.push_back(action);
807 }
808 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000809 } else if (finalPhase == OPTIMIZATION) {
810 throw cd->langName + " does not support optimization";
811 } else if (action.isSet(REQUIRED_FLAG)) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000812 throw std::string("Don't know how to optimize ") +
Reid Spenceraf77d742004-10-28 04:05:06 +0000813 cd->langName + " files";
Reid Spencer52c2dc12004-08-29 19:26:56 +0000814 }
Reid Spencer52c2dc12004-08-29 19:26:56 +0000815 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000816
817 // Short-circuit remaining actions if all they want is optimization
Reid Spencer679a7232004-11-20 20:39:33 +0000818 if (finalPhase == OPTIMIZATION) { continue; }
Reid Spenceraf77d742004-10-28 04:05:06 +0000819
820 /// ASSEMBLY PHASE
821 action = cd->Assembler;
822
823 if (finalPhase == ASSEMBLY) {
Reid Spencer679a7232004-11-20 20:39:33 +0000824
825 // Build either a native compilation action or a disassembly action
826 Action* action = new Action();
Reid Spenceraf77d742004-10-28 04:05:06 +0000827 if (isSet(EMIT_NATIVE_FLAG)) {
828 // Use llc to get the native assembly file
Reid Spencerdd04df02005-07-07 23:21:43 +0000829 action->program.set("llc");
Reid Spencer1fce0912004-12-11 00:14:15 +0000830 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000831 action->args.push_back("-f");
832 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000833 if (Output.isEmpty()) {
834 OutFile.appendSuffix("o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000835 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000836 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000837 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000838 }
839 actions.push_back(action);
Reid Spenceraf77d742004-10-28 04:05:06 +0000840 } else {
841 // Just convert back to llvm assembly with llvm-dis
Reid Spencerdd04df02005-07-07 23:21:43 +0000842 action->program.set("llvm-dis");
Reid Spencer1fce0912004-12-11 00:14:15 +0000843 action->args.push_back(InFile.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000844 action->args.push_back("-f");
845 action->args.push_back("-o");
Reid Spencer679a7232004-11-20 20:39:33 +0000846 if (Output.isEmpty()) {
847 OutFile.appendSuffix("ll");
Reid Spencer1fce0912004-12-11 00:14:15 +0000848 action->args.push_back(OutFile.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000849 } else {
Reid Spencer1fce0912004-12-11 00:14:15 +0000850 action->args.push_back(Output.toString());
Reid Spencer679a7232004-11-20 20:39:33 +0000851 }
Reid Spenceraf77d742004-10-28 04:05:06 +0000852 }
853
Reid Spencer679a7232004-11-20 20:39:33 +0000854 // Put the action on the list
855 actions.push_back(action);
856
Misha Brukman3da94ae2005-04-22 00:00:37 +0000857 // Short circuit the rest of the loop, we don't want to link
Reid Spenceraf77d742004-10-28 04:05:06 +0000858 continue;
859 }
860
861 // Register the result of the actions as a link candidate
862 LinkageItems.insert(InFile);
863
Reid Spenceraf77d742004-10-28 04:05:06 +0000864 } // end while loop over each input file
865
866 /// RUN THE COMPILATION ACTIONS
867 std::vector<Action*>::iterator AI = actions.begin();
868 std::vector<Action*>::iterator AE = actions.end();
869 while (AI != AE) {
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000870 int ActionResult = DoAction(*AI, ErrMsg);
871 if (ActionResult != 0)
872 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000873 AI++;
Reid Spencer52c2dc12004-08-29 19:26:56 +0000874 }
875
Reid Spenceraf77d742004-10-28 04:05:06 +0000876 /// LINKING PHASE
877 if (finalPhase == LINKING) {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000878
Reid Spenceraf77d742004-10-28 04:05:06 +0000879 // Insert the platform-specific system libraries to the path list
Reid Spencer11db4b82004-12-13 03:01:26 +0000880 std::vector<sys::Path> SysLibs;
881 sys::Path::GetSystemLibraryPaths(SysLibs);
882 LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
Reid Spenceraf77d742004-10-28 04:05:06 +0000883
884 // Set up the linking action with llvm-ld
885 Action* link = new Action();
Reid Spencerdd04df02005-07-07 23:21:43 +0000886 link->program.set("llvm-ld");
Reid Spenceraf77d742004-10-28 04:05:06 +0000887
888 // Add in the optimization level requested
889 switch (optLevel) {
890 case OPT_FAST_COMPILE:
891 link->args.push_back("-O1");
892 break;
893 case OPT_SIMPLE:
894 link->args.push_back("-O2");
895 break;
896 case OPT_AGGRESSIVE:
897 link->args.push_back("-O3");
898 break;
899 case OPT_LINK_TIME:
900 link->args.push_back("-O4");
901 break;
902 case OPT_AGGRESSIVE_LINK_TIME:
903 link->args.push_back("-O5");
904 break;
905 case OPT_NONE:
906 break;
907 }
908
909 // Add in all the linkage items we generated. This includes the
910 // output from the translation/optimization phases as well as any
911 // -l arguments specified.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000912 for (PathVector::const_iterator I=LinkageItems.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000913 E=LinkageItems.end(); I != E; ++I )
Reid Spencer1fce0912004-12-11 00:14:15 +0000914 link->args.push_back(I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000915
916 // Add in all the libraries we found.
Reid Spencerf6358c72004-12-19 18:00:56 +0000917 for (StringVector::const_iterator I=LibFiles.begin(),
Reid Spenceraf77d742004-10-28 04:05:06 +0000918 E=LibFiles.end(); I != E; ++I )
919 link->args.push_back(std::string("-l")+*I);
920
921 // Add in all the library paths to the command line
922 for (PathVector::const_iterator I=LibraryPaths.begin(),
923 E=LibraryPaths.end(); I != E; ++I)
Reid Spencer1fce0912004-12-11 00:14:15 +0000924 link->args.push_back( std::string("-L") + I->toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000925
926 // Add in the additional linker arguments requested
927 for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
928 E=AdditionalArgs[LINKING].end(); I != E; ++I)
929 link->args.push_back( *I );
930
931 // Add in other optional flags
932 if (isSet(EMIT_NATIVE_FLAG))
933 link->args.push_back("-native");
934 if (isSet(VERBOSE_FLAG))
935 link->args.push_back("-v");
936 if (isSet(TIME_PASSES_FLAG))
937 link->args.push_back("-time-passes");
938 if (isSet(SHOW_STATS_FLAG))
939 link->args.push_back("-stats");
940 if (isSet(STRIP_OUTPUT_FLAG))
941 link->args.push_back("-s");
942 if (isSet(DEBUG_FLAG)) {
943 link->args.push_back("-debug");
944 link->args.push_back("-debug-pass=Details");
945 }
946
947 // Add in mandatory flags
948 link->args.push_back("-o");
Reid Spencer1fce0912004-12-11 00:14:15 +0000949 link->args.push_back(Output.toString());
Reid Spenceraf77d742004-10-28 04:05:06 +0000950
951 // Execute the link
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000952 int ActionResult = DoAction(link, ErrMsg);
953 if (ActionResult != 0)
954 return ActionResult;
Reid Spenceraf77d742004-10-28 04:05:06 +0000955 }
956 } catch (std::string& msg) {
957 cleanup();
958 throw;
959 } catch (...) {
960 cleanup();
961 throw std::string("Unspecified error");
962 }
963 cleanup();
964 return 0;
965 }
966
967/// @}
968/// @name Data
969/// @{
970private:
971 ConfigDataProvider* cdp; ///< Where we get configuration data from
972 Phases finalPhase; ///< The final phase of compilation
973 OptimizationLevels optLevel; ///< The optimization level to apply
974 unsigned Flags; ///< The driver flags
975 std::string machine; ///< Target machine name
976 PathVector LibraryPaths; ///< -L options
977 PathVector IncludePaths; ///< -I options
Reid Spencer07adb282004-11-05 22:15:36 +0000978 PathVector ToolPaths; ///< -B options
Reid Spenceraf77d742004-10-28 04:05:06 +0000979 StringVector Defines; ///< -D options
980 sys::Path TempDir; ///< Name of the temporary directory.
981 StringTable AdditionalArgs; ///< The -Txyz options
982 StringVector fOptions; ///< -f options
983 StringVector MOptions; ///< -M options
984 StringVector WOptions; ///< -W options
985
986/// @}
987};
Reid Spencer5c56dc12004-08-13 20:22:43 +0000988}
989
990CompilerDriver::~CompilerDriver() {
Reid Spencer52c2dc12004-08-29 19:26:56 +0000991}
992
Reid Spencercc97cfc2005-05-19 00:52:28 +0000993CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
994
Reid Spencer52c2dc12004-08-29 19:26:56 +0000995CompilerDriver*
996CompilerDriver::Get(ConfigDataProvider& CDP) {
997 return new CompilerDriverImpl(CDP);
Reid Spencerbae68252004-08-19 04:49:47 +0000998}
999
1000CompilerDriver::ConfigData::ConfigData()
1001 : langName()
1002 , PreProcessor()
1003 , Translator()
1004 , Optimizer()
1005 , Assembler()
1006 , Linker()
1007{
1008 StringVector emptyVec;
1009 for (unsigned i = 0; i < NUM_PHASES; ++i)
1010 opts.push_back(emptyVec);
Reid Spencer5c56dc12004-08-13 20:22:43 +00001011}