Reid Spencer | a3f1855 | 2004-08-13 20:25:54 +0000 | [diff] [blame] | 1 | //===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===// |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 2 | // |
| 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 Spencer | a3f1855 | 2004-08-13 20:25:54 +0000 | [diff] [blame] | 11 | // This file implements the bulk of the LLVM Compiler Driver (llvmc). |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 12 | // |
| 13 | //===------------------------------------------------------------------------=== |
| 14 | |
| 15 | #include "CompilerDriver.h" |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 16 | #include "ConfigLexer.h" |
Reid Spencer | a01439a | 2004-08-24 13:55:17 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 18 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Timer.h" |
Reid Spencer | 93426ba | 2004-08-29 20:02:28 +0000 | [diff] [blame] | 20 | #include "llvm/System/Signals.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SetVector.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Brian Gaeke | fabf41f | 2004-12-20 04:02:01 +0000 | [diff] [blame] | 23 | #include "llvm/Config/alloca.h" |
Misha Brukman | baec07c | 2005-04-20 04:51:29 +0000 | [diff] [blame^] | 24 | #include <iostream> |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 28 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 29 | void WriteAction(CompilerDriver::Action* action ) { |
| 30 | std::cerr << action->program.c_str(); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 31 | std::vector<std::string>::const_iterator I = action->args.begin(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 32 | while (I != action->args.end()) { |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 33 | std::cerr << " " << *I; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 34 | ++I; |
| 35 | } |
| 36 | std::cerr << "\n"; |
| 37 | } |
| 38 | |
| 39 | void DumpAction(CompilerDriver::Action* action) { |
| 40 | std::cerr << "command = " << action->program.c_str(); |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 41 | std::vector<std::string>::const_iterator I = action->args.begin(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 42 | while (I != action->args.end()) { |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 43 | std::cerr << " " << *I; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 44 | ++I; |
| 45 | } |
| 46 | std::cerr << "\n"; |
| 47 | std::cerr << "flags = " << action->flags << "\n"; |
| 48 | } |
| 49 | |
| 50 | void 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. |
| 68 | static const char* DefaultFastCompileOptimizations[] = { |
| 69 | "-simplifycfg", "-mem2reg", "-instcombine" |
| 70 | }; |
| 71 | |
| 72 | class CompilerDriverImpl : public CompilerDriver { |
| 73 | /// @name Constructors |
| 74 | /// @{ |
| 75 | public: |
| 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 | /// @{ |
| 110 | public: |
| 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 Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 137 | tmp.setDirectory(*I); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 138 | IncludePaths.push_back(tmp); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 139 | ++I; |
| 140 | } |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 143 | 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 Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 152 | tmp.setDirectory(*I); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 153 | LibraryPaths.push_back(tmp); |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 154 | ++I; |
| 155 | } |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 158 | virtual void addLibraryPath( const sys::Path& libPath ) { |
| 159 | LibraryPaths.push_back(libPath); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 160 | } |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 161 | |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 162 | virtual void addToolPath( const sys::Path& toolPath ) { |
| 163 | ToolPaths.push_back(toolPath); |
| 164 | } |
| 165 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 166 | virtual void setfPassThrough(const StringVector& fOpts) { |
| 167 | fOptions = fOpts; |
| 168 | } |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 169 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 170 | /// @brief Set the list of -M options to be passed through |
| 171 | virtual void setMPassThrough(const StringVector& MOpts) { |
| 172 | MOptions = MOpts; |
| 173 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 174 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 175 | /// @brief Set the list of -W options to be passed through |
| 176 | virtual void setWPassThrough(const StringVector& WOpts) { |
| 177 | WOptions = WOpts; |
| 178 | } |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 179 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 180 | /// @} |
| 181 | /// @name Functions |
| 182 | /// @{ |
| 183 | private: |
| 184 | bool isSet(DriverFlags flag) { |
| 185 | return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags); |
| 186 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 187 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 188 | void cleanup() { |
| 189 | if (!isSet(KEEP_TEMPS_FLAG)) { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 190 | if (TempDir.isDirectory() && TempDir.writable()) |
| 191 | TempDir.destroyDirectory(/*remove_contents=*/true); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 192 | } else { |
Reid Spencer | 12786d5 | 2004-12-13 08:53:36 +0000 | [diff] [blame] | 193 | std::cout << "Temporary files are in " << TempDir << "\n"; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 196 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 197 | sys::Path MakeTempFile(const std::string& basename, |
| 198 | const std::string& suffix ) { |
| 199 | sys::Path result(TempDir); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 200 | if (!result.appendFile(basename)) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 201 | throw basename + ": can't use this file name"; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 202 | if (!result.appendSuffix(suffix)) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 203 | throw suffix + ": can't use this file suffix"; |
| 204 | return result; |
| 205 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 206 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 207 | 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 Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 214 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 215 | // 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 Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 227 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 228 | // Copy over some pattern things that don't need to change |
| 229 | action->program = pat->program; |
| 230 | action->flags = pat->flags; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 231 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 232 | // 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 Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 265 | if (!fOptions.empty()) |
| 266 | action->args.insert(action->args.end(), fOptions.begin(), |
| 267 | fOptions.end()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 268 | } else |
| 269 | found = false; |
| 270 | break; |
| 271 | case 'i': |
| 272 | if (*PI == "%in%") { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 273 | action->args.push_back(input.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 274 | } else if (*PI == "%incls%") { |
| 275 | PathVector::iterator I = IncludePaths.begin(); |
| 276 | PathVector::iterator E = IncludePaths.end(); |
| 277 | while (I != E) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 278 | action->args.push_back( std::string("-I") + I->toString() ); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 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) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 289 | action->args.push_back( std::string("-L") + I->toString() ); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 290 | ++I; |
| 291 | } |
| 292 | } else |
| 293 | found = false; |
| 294 | break; |
| 295 | case 'o': |
| 296 | if (*PI == "%out%") { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 297 | action->args.push_back(output.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 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 Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 336 | if (*PI == "%Mopts%") { |
| 337 | if (!MOptions.empty()) |
| 338 | action->args.insert(action->args.end(), MOptions.begin(), |
| 339 | MOptions.end()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 340 | } else |
| 341 | found = false; |
| 342 | break; |
| 343 | case 'W': |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 344 | 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 Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 349 | } else |
| 350 | found = false; |
| 351 | break; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 352 | default: |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 353 | found = false; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 354 | break; |
| 355 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 356 | 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 + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 361 | "' for command '" + pat->program.toString() + "'"; |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 362 | } else if (!PI->empty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 363 | // It's not a legal substitution, just pass it through |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 364 | action->args.push_back(*PI); |
| 365 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 366 | } |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 367 | } else if (!PI->empty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 368 | // Its not a substitution, just put it in the action |
| 369 | action->args.push_back(*PI); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 370 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 371 | PI++; |
| 372 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 373 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 374 | // Finally, we're done |
| 375 | return action; |
| 376 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 377 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 378 | 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( |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 384 | action->program.toString()); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 385 | if (progpath.isEmpty()) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 386 | throw std::string("Can't find program '" + |
| 387 | action->program.toString()+"'"); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 388 | else if (progpath.executable()) |
| 389 | action->program = progpath; |
| 390 | else |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 391 | throw std::string("Program '"+action->program.toString()+ |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 392 | "' is not executable."); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 393 | |
| 394 | // Invoke the program |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 395 | const char** Args = (const char**) |
Reid Spencer | c30088f | 2005-04-11 05:48:04 +0000 | [diff] [blame] | 396 | alloca(sizeof(const char*)*(action->args.size()+2)); |
| 397 | Args[0] = action->program.toString().c_str(); |
| 398 | for (unsigned i = 1; i != action->args.size(); ++i) |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 399 | Args[i] = action->args[i].c_str(); |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 400 | Args[action->args.size()] = 0; // null terminate list. |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 401 | if (isSet(TIME_ACTIONS_FLAG)) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 402 | Timer timer(action->program.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 403 | timer.startTimer(); |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 404 | int resultCode = sys::Program::ExecuteAndWait(action->program, Args); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 405 | timer.stopTimer(); |
| 406 | timer.print(timer,std::cerr); |
| 407 | return resultCode == 0; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 408 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 409 | else |
Chris Lattner | 7456e3c | 2005-02-13 23:10:45 +0000 | [diff] [blame] | 410 | return 0 == sys::Program::ExecuteAndWait(action->program, Args); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 411 | } |
| 412 | return true; |
| 413 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 414 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 415 | /// This method tries various variants of a linkage item's file |
| 416 | /// name to see if it can find an appropriate file to link with |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 417 | /// in the directories of the LibraryPaths. |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 418 | llvm::sys::Path GetPathForLinkageItem(const std::string& link_item, |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 419 | bool native = false) { |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 420 | sys::Path fullpath; |
| 421 | fullpath.setFile(link_item); |
| 422 | if (fullpath.readable()) |
| 423 | return fullpath; |
| 424 | for (PathVector::iterator PI = LibraryPaths.begin(), |
| 425 | PE = LibraryPaths.end(); PI != PE; ++PI) { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 426 | fullpath.setDirectory(PI->toString()); |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 427 | fullpath.appendFile(link_item); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 428 | if (fullpath.readable()) |
| 429 | return fullpath; |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 430 | if (native) { |
| 431 | fullpath.appendSuffix("a"); |
| 432 | } else { |
| 433 | fullpath.appendSuffix("bc"); |
| 434 | if (fullpath.readable()) |
| 435 | return fullpath; |
| 436 | fullpath.elideSuffix(); |
| 437 | fullpath.appendSuffix("o"); |
| 438 | if (fullpath.readable()) |
| 439 | return fullpath; |
| 440 | fullpath = *PI; |
| 441 | fullpath.appendFile(std::string("lib") + link_item); |
| 442 | fullpath.appendSuffix("a"); |
| 443 | if (fullpath.readable()) |
| 444 | return fullpath; |
| 445 | fullpath.elideSuffix(); |
| 446 | fullpath.appendSuffix("so"); |
| 447 | if (fullpath.readable()) |
| 448 | return fullpath; |
| 449 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Didn't find one. |
| 453 | fullpath.clear(); |
| 454 | return fullpath; |
| 455 | } |
| 456 | |
| 457 | /// This method processes a linkage item. The item could be a |
| 458 | /// Bytecode file needing translation to native code and that is |
| 459 | /// dependent on other bytecode libraries, or a native code |
| 460 | /// library that should just be linked into the program. |
| 461 | bool ProcessLinkageItem(const llvm::sys::Path& link_item, |
| 462 | SetVector<sys::Path>& set, |
| 463 | std::string& err) { |
| 464 | // First, see if the unadorned file name is not readable. If so, |
| 465 | // we must track down the file in the lib search path. |
| 466 | sys::Path fullpath; |
| 467 | if (!link_item.readable()) { |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 468 | // look for the library using the -L arguments specified |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 469 | // on the command line. |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 470 | fullpath = GetPathForLinkageItem(link_item.toString()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 471 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 472 | // If we didn't find the file in any of the library search paths |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 473 | // we have to bail. No where else to look. |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 474 | if (fullpath.isEmpty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 475 | err = |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 476 | std::string("Can't find linkage item '") + link_item.toString() + "'"; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 477 | return false; |
| 478 | } |
| 479 | } else { |
| 480 | fullpath = link_item; |
| 481 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 482 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 483 | // If we got here fullpath is the path to the file, and its readable. |
| 484 | set.insert(fullpath); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 485 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 486 | // If its an LLVM bytecode file ... |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 487 | if (fullpath.isBytecodeFile()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 488 | // Process the dependent libraries recursively |
| 489 | Module::LibraryListType modlibs; |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 490 | if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs)) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 491 | // Traverse the dependent libraries list |
| 492 | Module::lib_iterator LI = modlibs.begin(); |
| 493 | Module::lib_iterator LE = modlibs.end(); |
| 494 | while ( LI != LE ) { |
| 495 | if (!ProcessLinkageItem(sys::Path(*LI),set,err)) { |
| 496 | if (err.empty()) { |
| 497 | err = std::string("Library '") + *LI + |
| 498 | "' is not valid for linking but is required by file '" + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 499 | fullpath.toString() + "'"; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 500 | } else { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 501 | err += " which is required by file '" + fullpath.toString() + "'"; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 502 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 503 | return false; |
| 504 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 505 | ++LI; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 506 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 507 | } else if (err.empty()) { |
| 508 | err = std::string( |
| 509 | "The dependent libraries could not be extracted from '") + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 510 | fullpath.toString(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | /// @} |
| 518 | /// @name Methods |
| 519 | /// @{ |
| 520 | public: |
| 521 | virtual int execute(const InputList& InpList, const sys::Path& Output ) { |
| 522 | try { |
| 523 | // Echo the configuration of options if we're running verbose |
| 524 | if (isSet(DEBUG_FLAG)) { |
| 525 | std::cerr << "Compiler Driver Options:\n"; |
| 526 | std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n"; |
| 527 | std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n"; |
| 528 | std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n"; |
| 529 | std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n"; |
| 530 | std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n"; |
| 531 | std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n"; |
| 532 | std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n"; |
| 533 | std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n"; |
| 534 | std::cerr << "OutputMachine = " << machine << "\n"; |
| 535 | InputList::const_iterator I = InpList.begin(); |
| 536 | while ( I != InpList.end() ) { |
Reid Spencer | 12786d5 | 2004-12-13 08:53:36 +0000 | [diff] [blame] | 537 | std::cerr << "Input: " << I->first << "(" << I->second |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 538 | << ")\n"; |
| 539 | ++I; |
| 540 | } |
Reid Spencer | 12786d5 | 2004-12-13 08:53:36 +0000 | [diff] [blame] | 541 | std::cerr << "Output: " << Output << "\n"; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 544 | // If there's no input, we're done. |
| 545 | if (InpList.empty()) |
| 546 | throw std::string("Nothing to compile."); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 547 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 548 | // If they are asking for linking and didn't provide an output |
| 549 | // file then its an error (no way for us to "make up" a meaningful |
| 550 | // file name based on the various linker input files). |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 551 | if (finalPhase == LINKING && Output.isEmpty()) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 552 | throw std::string( |
| 553 | "An output file name must be specified for linker output"); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 554 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 555 | // If they are not asking for linking, provided an output file and |
| 556 | // there is more than one input file, its an error |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 557 | if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1) |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 558 | throw std::string("An output file name cannot be specified ") + |
| 559 | "with more than one input file name when not linking"; |
| 560 | |
| 561 | // This vector holds all the resulting actions of the following loop. |
| 562 | std::vector<Action*> actions; |
| 563 | |
| 564 | /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases |
| 565 | // for each input item |
| 566 | SetVector<sys::Path> LinkageItems; |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 567 | StringVector LibFiles; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 568 | InputList::const_iterator I = InpList.begin(); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 569 | for (InputList::const_iterator I = InpList.begin(), E = InpList.end(); |
| 570 | I != E; ++I ) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 571 | // Get the suffix of the file name |
| 572 | const std::string& ftype = I->second; |
| 573 | |
| 574 | // If its a library, bytecode file, or object file, save |
| 575 | // it for linking below and short circuit the |
| 576 | // pre-processing/translation/assembly phases |
| 577 | if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") { |
| 578 | // We shouldn't get any of these types of files unless we're |
| 579 | // later going to link. Enforce this limit now. |
| 580 | if (finalPhase != LINKING) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 581 | throw std::string( |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 582 | "Pre-compiled objects found but linking not requested"); |
| 583 | } |
| 584 | if (ftype.empty()) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 585 | LibFiles.push_back(I->first.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 586 | else |
| 587 | LinkageItems.insert(I->first); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 588 | continue; // short circuit remainder of loop |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 589 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 590 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 591 | // At this point, we know its something we need to translate |
| 592 | // and/or optimize. See if we can get the configuration data |
| 593 | // for this kind of file. |
| 594 | ConfigData* cd = cdp->ProvideConfigData(I->second); |
| 595 | if (cd == 0) |
| 596 | throw std::string("Files of type '") + I->second + |
| 597 | "' are not recognized."; |
| 598 | if (isSet(DEBUG_FLAG)) |
| 599 | DumpConfigData(cd,I->second); |
Reid Spencer | 54fafe4 | 2004-09-14 01:58:45 +0000 | [diff] [blame] | 600 | |
Reid Spencer | 0b3c7d0 | 2004-11-23 23:45:49 +0000 | [diff] [blame] | 601 | // Add the config data's library paths to the end of the list |
| 602 | for (StringVector::iterator LPI = cd->libpaths.begin(), |
| 603 | LPE = cd->libpaths.end(); LPI != LPE; ++LPI){ |
| 604 | LibraryPaths.push_back(sys::Path(*LPI)); |
| 605 | } |
| 606 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 607 | // Initialize the input and output files |
| 608 | sys::Path InFile(I->first); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 609 | sys::Path OutFile(I->first.getBasename()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 610 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 611 | // PRE-PROCESSING PHASE |
| 612 | Action& action = cd->PreProcessor; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 613 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 614 | // Get the preprocessing action, if needed, or error if appropriate |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 615 | if (!action.program.isEmpty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 616 | if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) { |
| 617 | if (finalPhase == PREPROCESSING) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 618 | if (Output.isEmpty()) { |
| 619 | OutFile.appendSuffix("E"); |
| 620 | actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING)); |
| 621 | } else { |
| 622 | actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING)); |
| 623 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 624 | } else { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 625 | sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E")); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 626 | actions.push_back(GetAction(cd,InFile,TempFile, |
| 627 | PREPROCESSING)); |
| 628 | InFile = TempFile; |
| 629 | } |
| 630 | } |
| 631 | } else if (finalPhase == PREPROCESSING) { |
| 632 | throw cd->langName + " does not support pre-processing"; |
| 633 | } else if (action.isSet(REQUIRED_FLAG)) { |
| 634 | throw std::string("Don't know how to pre-process ") + |
| 635 | cd->langName + " files"; |
| 636 | } |
| 637 | |
| 638 | // Short-circuit remaining actions if all they want is |
| 639 | // pre-processing |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 640 | if (finalPhase == PREPROCESSING) { continue; }; |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 641 | |
| 642 | /// TRANSLATION PHASE |
| 643 | action = cd->Translator; |
| 644 | |
| 645 | // Get the translation action, if needed, or error if appropriate |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 646 | if (!action.program.isEmpty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 647 | if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) { |
| 648 | if (finalPhase == TRANSLATION) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 649 | if (Output.isEmpty()) { |
| 650 | OutFile.appendSuffix("o"); |
| 651 | actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION)); |
| 652 | } else { |
| 653 | actions.push_back(GetAction(cd,InFile,Output,TRANSLATION)); |
| 654 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 655 | } else { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 656 | sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans")); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 657 | actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION)); |
| 658 | InFile = TempFile; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 661 | // ll -> bc Helper |
| 662 | if (action.isSet(OUTPUT_IS_ASM_FLAG)) { |
| 663 | /// The output of the translator is an LLVM Assembly program |
| 664 | /// We need to translate it to bytecode |
| 665 | Action* action = new Action(); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 666 | action->program.setFile("llvm-as"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 667 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 668 | action->args.push_back("-o"); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 669 | InFile.appendSuffix("bc"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 670 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 671 | actions.push_back(action); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 672 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 673 | } |
| 674 | } else if (finalPhase == TRANSLATION) { |
| 675 | throw cd->langName + " does not support translation"; |
| 676 | } else if (action.isSet(REQUIRED_FLAG)) { |
| 677 | throw std::string("Don't know how to translate ") + |
| 678 | cd->langName + " files"; |
| 679 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 680 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 681 | // Short-circuit remaining actions if all they want is translation |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 682 | if (finalPhase == TRANSLATION) { continue; } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 683 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 684 | /// OPTIMIZATION PHASE |
| 685 | action = cd->Optimizer; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 686 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 687 | // Get the optimization action, if needed, or error if appropriate |
| 688 | if (!isSet(EMIT_RAW_FLAG)) { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 689 | if (!action.program.isEmpty()) { |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 690 | if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) { |
| 691 | if (finalPhase == OPTIMIZATION) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 692 | if (Output.isEmpty()) { |
| 693 | OutFile.appendSuffix("o"); |
| 694 | actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION)); |
| 695 | } else { |
| 696 | actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION)); |
| 697 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 698 | } else { |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 699 | sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt")); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 700 | actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION)); |
| 701 | InFile = TempFile; |
| 702 | } |
| 703 | // ll -> bc Helper |
| 704 | if (action.isSet(OUTPUT_IS_ASM_FLAG)) { |
| 705 | /// The output of the optimizer is an LLVM Assembly program |
| 706 | /// We need to translate it to bytecode with llvm-as |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 707 | Action* action = new Action(); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 708 | action->program.setFile("llvm-as"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 709 | action->args.push_back(InFile.toString()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 710 | action->args.push_back("-f"); |
| 711 | action->args.push_back("-o"); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 712 | InFile.appendSuffix("bc"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 713 | action->args.push_back(InFile.toString()); |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 714 | actions.push_back(action); |
| 715 | } |
| 716 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 717 | } else if (finalPhase == OPTIMIZATION) { |
| 718 | throw cd->langName + " does not support optimization"; |
| 719 | } else if (action.isSet(REQUIRED_FLAG)) { |
| 720 | throw std::string("Don't know how to optimize ") + |
| 721 | cd->langName + " files"; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 722 | } |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 723 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 724 | |
| 725 | // Short-circuit remaining actions if all they want is optimization |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 726 | if (finalPhase == OPTIMIZATION) { continue; } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 727 | |
| 728 | /// ASSEMBLY PHASE |
| 729 | action = cd->Assembler; |
| 730 | |
| 731 | if (finalPhase == ASSEMBLY) { |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 732 | |
| 733 | // Build either a native compilation action or a disassembly action |
| 734 | Action* action = new Action(); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 735 | if (isSet(EMIT_NATIVE_FLAG)) { |
| 736 | // Use llc to get the native assembly file |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 737 | action->program.setFile("llc"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 738 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 739 | action->args.push_back("-f"); |
| 740 | action->args.push_back("-o"); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 741 | if (Output.isEmpty()) { |
| 742 | OutFile.appendSuffix("o"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 743 | action->args.push_back(OutFile.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 744 | } else { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 745 | action->args.push_back(Output.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 746 | } |
| 747 | actions.push_back(action); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 748 | } else { |
| 749 | // Just convert back to llvm assembly with llvm-dis |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 750 | action->program.setFile("llvm-dis"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 751 | action->args.push_back(InFile.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 752 | action->args.push_back("-f"); |
| 753 | action->args.push_back("-o"); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 754 | if (Output.isEmpty()) { |
| 755 | OutFile.appendSuffix("ll"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 756 | action->args.push_back(OutFile.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 757 | } else { |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 758 | action->args.push_back(Output.toString()); |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 759 | } |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Reid Spencer | 679a723 | 2004-11-20 20:39:33 +0000 | [diff] [blame] | 762 | // Put the action on the list |
| 763 | actions.push_back(action); |
| 764 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 765 | // Short circuit the rest of the loop, we don't want to link |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 766 | continue; |
| 767 | } |
| 768 | |
| 769 | // Register the result of the actions as a link candidate |
| 770 | LinkageItems.insert(InFile); |
| 771 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 772 | } // end while loop over each input file |
| 773 | |
| 774 | /// RUN THE COMPILATION ACTIONS |
| 775 | std::vector<Action*>::iterator AI = actions.begin(); |
| 776 | std::vector<Action*>::iterator AE = actions.end(); |
| 777 | while (AI != AE) { |
| 778 | if (!DoAction(*AI)) |
| 779 | throw std::string("Action failed"); |
| 780 | AI++; |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 783 | /// LINKING PHASE |
| 784 | if (finalPhase == LINKING) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 785 | |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 786 | // Insert the platform-specific system libraries to the path list |
Reid Spencer | 11db4b8 | 2004-12-13 03:01:26 +0000 | [diff] [blame] | 787 | std::vector<sys::Path> SysLibs; |
| 788 | sys::Path::GetSystemLibraryPaths(SysLibs); |
| 789 | LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 790 | |
| 791 | // Set up the linking action with llvm-ld |
| 792 | Action* link = new Action(); |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 793 | link->program.setFile("llvm-ld"); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 794 | |
| 795 | // Add in the optimization level requested |
| 796 | switch (optLevel) { |
| 797 | case OPT_FAST_COMPILE: |
| 798 | link->args.push_back("-O1"); |
| 799 | break; |
| 800 | case OPT_SIMPLE: |
| 801 | link->args.push_back("-O2"); |
| 802 | break; |
| 803 | case OPT_AGGRESSIVE: |
| 804 | link->args.push_back("-O3"); |
| 805 | break; |
| 806 | case OPT_LINK_TIME: |
| 807 | link->args.push_back("-O4"); |
| 808 | break; |
| 809 | case OPT_AGGRESSIVE_LINK_TIME: |
| 810 | link->args.push_back("-O5"); |
| 811 | break; |
| 812 | case OPT_NONE: |
| 813 | break; |
| 814 | } |
| 815 | |
| 816 | // Add in all the linkage items we generated. This includes the |
| 817 | // output from the translation/optimization phases as well as any |
| 818 | // -l arguments specified. |
| 819 | for (PathVector::const_iterator I=LinkageItems.begin(), |
| 820 | E=LinkageItems.end(); I != E; ++I ) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 821 | link->args.push_back(I->toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 822 | |
| 823 | // Add in all the libraries we found. |
Reid Spencer | f6358c7 | 2004-12-19 18:00:56 +0000 | [diff] [blame] | 824 | for (StringVector::const_iterator I=LibFiles.begin(), |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 825 | E=LibFiles.end(); I != E; ++I ) |
| 826 | link->args.push_back(std::string("-l")+*I); |
| 827 | |
| 828 | // Add in all the library paths to the command line |
| 829 | for (PathVector::const_iterator I=LibraryPaths.begin(), |
| 830 | E=LibraryPaths.end(); I != E; ++I) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 831 | link->args.push_back( std::string("-L") + I->toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 832 | |
| 833 | // Add in the additional linker arguments requested |
| 834 | for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(), |
| 835 | E=AdditionalArgs[LINKING].end(); I != E; ++I) |
| 836 | link->args.push_back( *I ); |
| 837 | |
| 838 | // Add in other optional flags |
| 839 | if (isSet(EMIT_NATIVE_FLAG)) |
| 840 | link->args.push_back("-native"); |
| 841 | if (isSet(VERBOSE_FLAG)) |
| 842 | link->args.push_back("-v"); |
| 843 | if (isSet(TIME_PASSES_FLAG)) |
| 844 | link->args.push_back("-time-passes"); |
| 845 | if (isSet(SHOW_STATS_FLAG)) |
| 846 | link->args.push_back("-stats"); |
| 847 | if (isSet(STRIP_OUTPUT_FLAG)) |
| 848 | link->args.push_back("-s"); |
| 849 | if (isSet(DEBUG_FLAG)) { |
| 850 | link->args.push_back("-debug"); |
| 851 | link->args.push_back("-debug-pass=Details"); |
| 852 | } |
| 853 | |
| 854 | // Add in mandatory flags |
| 855 | link->args.push_back("-o"); |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 856 | link->args.push_back(Output.toString()); |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 857 | |
| 858 | // Execute the link |
| 859 | if (!DoAction(link)) |
| 860 | throw std::string("Action failed"); |
| 861 | } |
| 862 | } catch (std::string& msg) { |
| 863 | cleanup(); |
| 864 | throw; |
| 865 | } catch (...) { |
| 866 | cleanup(); |
| 867 | throw std::string("Unspecified error"); |
| 868 | } |
| 869 | cleanup(); |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | /// @} |
| 874 | /// @name Data |
| 875 | /// @{ |
| 876 | private: |
| 877 | ConfigDataProvider* cdp; ///< Where we get configuration data from |
| 878 | Phases finalPhase; ///< The final phase of compilation |
| 879 | OptimizationLevels optLevel; ///< The optimization level to apply |
| 880 | unsigned Flags; ///< The driver flags |
| 881 | std::string machine; ///< Target machine name |
| 882 | PathVector LibraryPaths; ///< -L options |
| 883 | PathVector IncludePaths; ///< -I options |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 884 | PathVector ToolPaths; ///< -B options |
Reid Spencer | af77d74 | 2004-10-28 04:05:06 +0000 | [diff] [blame] | 885 | StringVector Defines; ///< -D options |
| 886 | sys::Path TempDir; ///< Name of the temporary directory. |
| 887 | StringTable AdditionalArgs; ///< The -Txyz options |
| 888 | StringVector fOptions; ///< -f options |
| 889 | StringVector MOptions; ///< -M options |
| 890 | StringVector WOptions; ///< -W options |
| 891 | |
| 892 | /// @} |
| 893 | }; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | CompilerDriver::~CompilerDriver() { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | CompilerDriver* |
| 900 | CompilerDriver::Get(ConfigDataProvider& CDP) { |
| 901 | return new CompilerDriverImpl(CDP); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | CompilerDriver::ConfigData::ConfigData() |
| 905 | : langName() |
| 906 | , PreProcessor() |
| 907 | , Translator() |
| 908 | , Optimizer() |
| 909 | , Assembler() |
| 910 | , Linker() |
| 911 | { |
| 912 | StringVector emptyVec; |
| 913 | for (unsigned i = 0; i < NUM_PHASES; ++i) |
| 914 | opts.push_back(emptyVec); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 917 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |