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