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 | 93426ba | 2004-08-29 20:02:28 +0000 | [diff] [blame^] | 19 | #include "llvm/System/Signals.h" |
Reid Spencer | a01439a | 2004-08-24 13:55:17 +0000 | [diff] [blame] | 20 | #include "Support/FileUtilities.h" |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 21 | #include "Support/SetVector.h" |
Reid Spencer | a01439a | 2004-08-24 13:55:17 +0000 | [diff] [blame] | 22 | #include "Support/StringExtras.h" |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 23 | #include <iostream> |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 28 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 29 | void WriteAction(CompilerDriver::Action* action ) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 30 | std::cerr << action->program.c_str(); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 31 | std::vector<std::string>::iterator I = action->args.begin(); |
| 32 | while (I != action->args.end()) { |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 33 | std::cerr << " " + *I; |
| 34 | ++I; |
| 35 | } |
| 36 | std::cerr << "\n"; |
| 37 | } |
| 38 | |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 39 | void DumpAction(CompilerDriver::Action* action) { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 40 | std::cerr << "command = " << action->program.c_str(); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 41 | std::vector<std::string>::iterator I = action->args.begin(); |
| 42 | while (I != action->args.end()) { |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 43 | std::cerr << " " + *I; |
| 44 | ++I; |
| 45 | } |
| 46 | std::cerr << "\n"; |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 47 | std::cerr << "flags = " << action->flags << "\n"; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 50 | void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){ |
| 51 | std::cerr << "Configuration Data For '" << cd->langName << "' (" << type |
| 52 | << ")\n"; |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 53 | std::cerr << "PreProcessor: "; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 54 | DumpAction(&cd->PreProcessor); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 55 | std::cerr << "Translator: "; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 56 | DumpAction(&cd->Translator); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 57 | std::cerr << "Optimizer: "; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 58 | DumpAction(&cd->Optimizer); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 59 | std::cerr << "Assembler: "; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 60 | DumpAction(&cd->Assembler); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 61 | std::cerr << "Linker: "; |
Reid Spencer | bf43772 | 2004-08-15 08:19:46 +0000 | [diff] [blame] | 62 | DumpAction(&cd->Linker); |
Reid Spencer | 68fb37a | 2004-08-14 09:37:15 +0000 | [diff] [blame] | 63 | } |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 64 | |
Reid Spencer | 2a069fa | 2004-08-16 07:06:38 +0000 | [diff] [blame] | 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. |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 68 | static const char* DefaultFastCompileOptimizations[] = { |
| 69 | "-simplifycfg", "-mem2reg", "-instcombine" |
Reid Spencer | 2a069fa | 2004-08-16 07:06:38 +0000 | [diff] [blame] | 70 | }; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 71 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 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); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 92 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 93 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 94 | virtual ~CompilerDriverImpl() { |
| 95 | cleanup(); |
| 96 | cdp = 0; |
| 97 | LibraryPaths.clear(); |
| 98 | AdditionalArgs.clear(); |
| 99 | } |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 100 | |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 101 | /// @} |
| 102 | /// @name Methods |
| 103 | /// @{ |
| 104 | public: |
| 105 | virtual void setFinalPhase( Phases phase ) { |
| 106 | finalPhase = phase; |
| 107 | } |
| 108 | |
| 109 | virtual void setOptimization( OptimizationLevels level ) { |
| 110 | optLevel = level; |
| 111 | } |
| 112 | |
| 113 | virtual void setDriverFlags( unsigned flags ) { |
| 114 | Flags = flags & DRIVER_FLAGS_MASK; |
| 115 | } |
| 116 | |
| 117 | virtual void setOutputMachine( const std::string& machineName ) { |
| 118 | machine = machineName; |
| 119 | } |
| 120 | |
| 121 | virtual void setPhaseArgs(Phases phase, const StringVector& opts) { |
| 122 | assert(phase <= LINKING && phase >= PREPROCESSING); |
| 123 | AdditionalArgs[phase] = opts; |
| 124 | } |
| 125 | |
| 126 | virtual void setLibraryPaths(const StringVector& paths) { |
| 127 | StringVector::const_iterator I = paths.begin(); |
| 128 | StringVector::const_iterator E = paths.end(); |
| 129 | while (I != E) { |
| 130 | sys::Path tmp; |
| 131 | tmp.set_directory(*I); |
| 132 | LibraryPaths.push_back(tmp); |
| 133 | ++I; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | virtual void addLibraryPath( const sys::Path& libPath ) { |
| 138 | LibraryPaths.push_back(libPath); |
| 139 | } |
| 140 | |
| 141 | /// @} |
| 142 | /// @name Functions |
| 143 | /// @{ |
| 144 | private: |
| 145 | bool isSet(DriverFlags flag) { |
| 146 | return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags); |
| 147 | } |
| 148 | |
| 149 | void cleanup() { |
| 150 | if (!isSet(KEEP_TEMPS_FLAG)) { |
| 151 | if (TempDir.is_directory() && TempDir.writable()) |
| 152 | TempDir.destroy_directory(/*remove_contents=*/true); |
| 153 | } else { |
| 154 | std::cout << "Temporary files are in " << TempDir.get() << "\n"; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | sys::Path MakeTempFile(const std::string& basename, const std::string& suffix ) { |
| 159 | sys::Path result(TempDir); |
| 160 | if (!result.append_file(basename)) |
| 161 | throw basename + ": can't use this file name"; |
| 162 | if (!result.append_suffix(suffix)) |
| 163 | throw suffix + ": can't use this file suffix"; |
| 164 | return result; |
| 165 | } |
| 166 | |
| 167 | Action* GetAction(ConfigData* cd, |
| 168 | const sys::Path& input, |
| 169 | const sys::Path& output, |
| 170 | Phases phase) |
| 171 | { |
| 172 | Action* pat = 0; ///< The pattern/template for the action |
| 173 | Action* action = new Action; ///< The actual action to execute |
| 174 | |
| 175 | // Get the action pattern |
| 176 | switch (phase) { |
| 177 | case PREPROCESSING: pat = &cd->PreProcessor; break; |
| 178 | case TRANSLATION: pat = &cd->Translator; break; |
| 179 | case OPTIMIZATION: pat = &cd->Optimizer; break; |
| 180 | case ASSEMBLY: pat = &cd->Assembler; break; |
| 181 | case LINKING: pat = &cd->Linker; break; |
| 182 | default: |
| 183 | assert(!"Invalid driver phase!"); |
| 184 | break; |
| 185 | } |
| 186 | assert(pat != 0 && "Invalid command pattern"); |
| 187 | |
| 188 | // Copy over some pattern things that don't need to change |
| 189 | action->program = pat->program; |
| 190 | action->flags = pat->flags; |
| 191 | |
| 192 | // Do the substitutions from the pattern to the actual |
| 193 | StringVector::iterator PI = pat->args.begin(); |
| 194 | StringVector::iterator PE = pat->args.end(); |
| 195 | while (PI != PE) { |
| 196 | if ((*PI)[0] == '%') { |
| 197 | if (*PI == "%in%") { |
| 198 | action->args.push_back(input.get()); |
| 199 | } else if (*PI == "%out%") { |
| 200 | action->args.push_back(output.get()); |
| 201 | } else if (*PI == "%time%") { |
| 202 | if (isSet(TIME_PASSES_FLAG)) |
| 203 | action->args.push_back("-time-passes"); |
| 204 | } else if (*PI == "%stats%") { |
| 205 | if (isSet(SHOW_STATS_FLAG)) |
| 206 | action->args.push_back("-stats"); |
| 207 | } else if (*PI == "%force%") { |
| 208 | if (isSet(FORCE_FLAG)) |
| 209 | action->args.push_back("-f"); |
| 210 | } else if (*PI == "%verbose%") { |
| 211 | if (isSet(VERBOSE_FLAG)) |
| 212 | action->args.push_back("-v"); |
| 213 | } else if (*PI == "%target%") { |
| 214 | // FIXME: Ignore for now |
| 215 | } else if (*PI == "%opt%") { |
| 216 | if (!isSet(EMIT_RAW_FLAG)) { |
| 217 | if (cd->opts.size() > static_cast<unsigned>(optLevel) && |
| 218 | !cd->opts[optLevel].empty()) |
| 219 | action->args.insert(action->args.end(), cd->opts[optLevel].begin(), |
| 220 | cd->opts[optLevel].end()); |
| 221 | else |
| 222 | throw std::string("Optimization options for level ") + |
| 223 | utostr(unsigned(optLevel)) + " were not specified"; |
| 224 | } |
| 225 | } else if (*PI == "%args%") { |
| 226 | if (AdditionalArgs.size() > unsigned(phase)) |
| 227 | if (!AdditionalArgs[phase].empty()) { |
| 228 | // Get specific options for each kind of action type |
| 229 | StringVector& addargs = AdditionalArgs[phase]; |
| 230 | // Add specific options for each kind of action type |
| 231 | action->args.insert(action->args.end(), addargs.begin(), addargs.end()); |
| 232 | } |
| 233 | } else { |
| 234 | throw "Invalid substitution name" + *PI; |
| 235 | } |
| 236 | } else { |
| 237 | // Its not a substitution, just put it in the action |
| 238 | action->args.push_back(*PI); |
| 239 | } |
| 240 | PI++; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | // Finally, we're done |
| 245 | return action; |
| 246 | } |
| 247 | |
| 248 | bool DoAction(Action*action) { |
| 249 | assert(action != 0 && "Invalid Action!"); |
| 250 | if (isSet(VERBOSE_FLAG)) |
| 251 | WriteAction(action); |
| 252 | if (!isSet(DRY_RUN_FLAG)) { |
| 253 | action->program = sys::Program::FindProgramByName(action->program.get()); |
| 254 | if (action->program.is_empty()) |
| 255 | throw "Can't find program '" + action->program.get() + "'"; |
| 256 | |
| 257 | // Invoke the program |
| 258 | return 0 == action->program.ExecuteAndWait(action->args); |
| 259 | } |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | /// This method tries various variants of a linkage item's file |
| 264 | /// name to see if it can find an appropriate file to link with |
| 265 | /// in the directory specified. |
| 266 | llvm::sys::Path GetPathForLinkageItem(const std::string& link_item, |
| 267 | const sys::Path& dir) { |
| 268 | sys::Path fullpath(dir); |
| 269 | fullpath.append_file(link_item); |
| 270 | fullpath.append_suffix("bc"); |
| 271 | if (fullpath.readable()) |
| 272 | return fullpath; |
| 273 | fullpath.elide_suffix(); |
| 274 | fullpath.append_suffix("o"); |
| 275 | if (fullpath.readable()) |
| 276 | return fullpath; |
| 277 | fullpath = dir; |
| 278 | fullpath.append_file(std::string("lib") + link_item); |
| 279 | fullpath.append_suffix("a"); |
| 280 | if (fullpath.readable()) |
| 281 | return fullpath; |
| 282 | fullpath.elide_suffix(); |
| 283 | fullpath.append_suffix("so"); |
| 284 | if (fullpath.readable()) |
| 285 | return fullpath; |
| 286 | |
| 287 | // Didn't find one. |
| 288 | fullpath.clear(); |
| 289 | return fullpath; |
| 290 | } |
| 291 | |
| 292 | /// This method processes a linkage item. The item could be a |
| 293 | /// Bytecode file needing translation to native code and that is |
| 294 | /// dependent on other bytecode libraries, or a native code |
| 295 | /// library that should just be linked into the program. |
| 296 | bool ProcessLinkageItem(const llvm::sys::Path& link_item, |
| 297 | SetVector<sys::Path>& set, |
| 298 | std::string& err) { |
| 299 | // First, see if the unadorned file name is not readable. If so, |
| 300 | // we must track down the file in the lib search path. |
| 301 | sys::Path fullpath; |
| 302 | if (!link_item.readable()) { |
| 303 | // First, look for the library using the -L arguments specified |
| 304 | // on the command line. |
| 305 | PathVector::iterator PI = LibraryPaths.begin(); |
| 306 | PathVector::iterator PE = LibraryPaths.end(); |
| 307 | while (PI != PE && fullpath.is_empty()) { |
| 308 | fullpath = GetPathForLinkageItem(link_item.get(),*PI); |
| 309 | ++PI; |
| 310 | } |
| 311 | |
| 312 | // If we didn't find the file in any of the library search paths |
| 313 | // so we have to bail. No where else to look. |
| 314 | if (fullpath.is_empty()) { |
| 315 | err = std::string("Can't find linkage item '") + link_item.get() + "'"; |
| 316 | return false; |
| 317 | } |
| 318 | } else { |
| 319 | fullpath = link_item; |
| 320 | } |
| 321 | |
| 322 | // If we got here fullpath is the path to the file, and its readable. |
| 323 | set.insert(fullpath); |
| 324 | |
| 325 | // If its an LLVM bytecode file ... |
| 326 | if (CheckMagic(fullpath.get(), "llvm")) { |
| 327 | // Process the dependent libraries recursively |
| 328 | Module::LibraryListType modlibs; |
| 329 | if (GetBytecodeDependentLibraries(fullpath.get(),modlibs)) { |
| 330 | // Traverse the dependent libraries list |
| 331 | Module::lib_iterator LI = modlibs.begin(); |
| 332 | Module::lib_iterator LE = modlibs.end(); |
| 333 | while ( LI != LE ) { |
| 334 | if (!ProcessLinkageItem(sys::Path(*LI),set,err)) { |
| 335 | if (err.empty()) { |
| 336 | err = std::string("Library '") + *LI + |
| 337 | "' is not valid for linking but is required by file '" + |
| 338 | fullpath.get() + "'"; |
| 339 | } else { |
| 340 | err += " which is required by file '" + fullpath.get() + "'"; |
| 341 | } |
| 342 | return false; |
| 343 | } |
| 344 | ++LI; |
| 345 | } |
| 346 | } else if (err.empty()) { |
| 347 | err = std::string("The dependent libraries could not be extracted from '") |
| 348 | + fullpath.get(); |
| 349 | return false; |
| 350 | } |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | /// @} |
| 356 | /// @name Methods |
| 357 | /// @{ |
| 358 | public: |
| 359 | virtual int execute(const InputList& InpList, const sys::Path& Output ) { |
| 360 | try { |
| 361 | // Echo the configuration of options if we're running verbose |
| 362 | if (isSet(DEBUG_FLAG)) { |
| 363 | std::cerr << "Compiler Driver Options:\n"; |
| 364 | std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n"; |
| 365 | std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n"; |
| 366 | std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n"; |
| 367 | std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n"; |
| 368 | std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n"; |
| 369 | std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n"; |
| 370 | std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n"; |
| 371 | std::cerr << "ForceOutput = " << isSet(FORCE_FLAG) << "\n"; |
| 372 | std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n"; |
| 373 | std::cerr << "OutputMachine = " << machine << "\n"; |
| 374 | InputList::const_iterator I = InpList.begin(); |
| 375 | while ( I != InpList.end() ) { |
| 376 | std::cerr << "Input: " << I->first.get() << "(" << I->second << ")\n"; |
| 377 | ++I; |
| 378 | } |
| 379 | std::cerr << "Output: " << Output.get() << "\n"; |
| 380 | } |
| 381 | |
| 382 | // If there's no input, we're done. |
| 383 | if (InpList.empty()) |
| 384 | throw std::string("Nothing to compile."); |
| 385 | |
| 386 | // If they are asking for linking and didn't provide an output |
| 387 | // file then its an error (no way for us to "make up" a meaningful |
| 388 | // file name based on the various linker input files). |
| 389 | if (finalPhase == LINKING && Output.is_empty()) |
| 390 | throw std::string( |
| 391 | "An output file name must be specified for linker output"); |
| 392 | |
| 393 | // This vector holds all the resulting actions of the following loop. |
| 394 | std::vector<Action*> actions; |
| 395 | |
| 396 | /// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases |
| 397 | // for each input item |
| 398 | SetVector<sys::Path> LinkageItems; |
| 399 | sys::Path OutFile(Output); |
| 400 | InputList::const_iterator I = InpList.begin(); |
| 401 | while ( I != InpList.end() ) { |
| 402 | // Get the suffix of the file name |
| 403 | const std::string& ftype = I->second; |
| 404 | |
| 405 | // If its a library, bytecode file, or object file, save |
| 406 | // it for linking below and short circuit the |
| 407 | // pre-processing/translation/assembly phases |
| 408 | if (ftype.empty() || ftype == "o" || ftype == "bc") { |
| 409 | // We shouldn't get any of these types of files unless we're |
| 410 | // later going to link. Enforce this limit now. |
| 411 | if (finalPhase != LINKING) { |
| 412 | throw std::string( |
| 413 | "Pre-compiled objects found but linking not requested"); |
| 414 | } |
| 415 | LinkageItems.insert(I->first); |
| 416 | ++I; continue; // short circuit remainder of loop |
| 417 | } |
| 418 | |
| 419 | // At this point, we know its something we need to translate |
| 420 | // and/or optimize. See if we can get the configuration data |
| 421 | // for this kind of file. |
| 422 | ConfigData* cd = cdp->ProvideConfigData(I->second); |
| 423 | if (cd == 0) |
| 424 | throw std::string("Files of type '") + I->second + |
| 425 | "' are not recognized."; |
| 426 | if (isSet(DEBUG_FLAG)) |
| 427 | DumpConfigData(cd,I->second); |
| 428 | |
| 429 | // Initialize the input file |
| 430 | sys::Path InFile(I->first); |
| 431 | |
| 432 | // PRE-PROCESSING PHASE |
| 433 | Action& action = cd->PreProcessor; |
| 434 | |
| 435 | // Get the preprocessing action, if needed, or error if appropriate |
| 436 | if (!action.program.is_empty()) { |
| 437 | if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) { |
| 438 | if (finalPhase == PREPROCESSING) |
| 439 | actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING)); |
| 440 | else { |
| 441 | sys::Path TempFile(MakeTempFile(I->first.get(),"E")); |
| 442 | actions.push_back(GetAction(cd,InFile,TempFile,PREPROCESSING)); |
| 443 | InFile = TempFile; |
| 444 | } |
| 445 | } |
| 446 | } else if (finalPhase == PREPROCESSING) { |
| 447 | throw cd->langName + " does not support pre-processing"; |
| 448 | } else if (action.isSet(REQUIRED_FLAG)) { |
| 449 | throw std::string("Don't know how to pre-process ") + |
| 450 | cd->langName + " files"; |
| 451 | } |
| 452 | |
| 453 | // Short-circuit remaining actions if all they want is pre-processing |
| 454 | if (finalPhase == PREPROCESSING) { ++I; continue; }; |
| 455 | |
| 456 | /// TRANSLATION PHASE |
| 457 | action = cd->Translator; |
| 458 | |
| 459 | // Get the translation action, if needed, or error if appropriate |
| 460 | if (!action.program.is_empty()) { |
| 461 | if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) { |
| 462 | if (finalPhase == TRANSLATION) |
| 463 | actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION)); |
| 464 | else { |
| 465 | sys::Path TempFile(MakeTempFile(I->first.get(),"trans")); |
| 466 | actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION)); |
| 467 | InFile = TempFile; |
| 468 | } |
| 469 | |
| 470 | // ll -> bc Helper |
| 471 | if (action.isSet(OUTPUT_IS_ASM_FLAG)) { |
| 472 | /// The output of the translator is an LLVM Assembly program |
| 473 | /// We need to translate it to bytecode |
| 474 | Action* action = new Action(); |
| 475 | action->program.set_file("llvm-as"); |
| 476 | action->args.push_back(InFile.get()); |
| 477 | action->args.push_back("-o"); |
| 478 | InFile.append_suffix("bc"); |
| 479 | action->args.push_back(InFile.get()); |
| 480 | actions.push_back(action); |
| 481 | } |
| 482 | } |
| 483 | } else if (finalPhase == TRANSLATION) { |
| 484 | throw cd->langName + " does not support translation"; |
| 485 | } else if (action.isSet(REQUIRED_FLAG)) { |
| 486 | throw std::string("Don't know how to translate ") + |
| 487 | cd->langName + " files"; |
| 488 | } |
| 489 | |
| 490 | // Short-circuit remaining actions if all they want is translation |
| 491 | if (finalPhase == TRANSLATION) { ++I; continue; } |
| 492 | |
| 493 | /// OPTIMIZATION PHASE |
| 494 | action = cd->Optimizer; |
| 495 | |
| 496 | // Get the optimization action, if needed, or error if appropriate |
| 497 | if (!isSet(EMIT_RAW_FLAG)) { |
| 498 | if (!action.program.is_empty()) { |
| 499 | if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) { |
| 500 | if (finalPhase == OPTIMIZATION) |
| 501 | actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION)); |
| 502 | else { |
| 503 | sys::Path TempFile(MakeTempFile(I->first.get(),"opt")); |
| 504 | actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION)); |
| 505 | InFile = TempFile; |
| 506 | } |
| 507 | // ll -> bc Helper |
| 508 | if (action.isSet(OUTPUT_IS_ASM_FLAG)) { |
| 509 | /// The output of the translator is an LLVM Assembly program |
| 510 | /// We need to translate it to bytecode |
| 511 | Action* action = new Action(); |
| 512 | action->program.set_file("llvm-as"); |
| 513 | action->args.push_back(InFile.get()); |
| 514 | action->args.push_back("-f"); |
| 515 | action->args.push_back("-o"); |
| 516 | InFile.append_suffix("bc"); |
| 517 | action->args.push_back(InFile.get()); |
| 518 | actions.push_back(action); |
| 519 | } |
| 520 | } |
| 521 | } else if (finalPhase == OPTIMIZATION) { |
| 522 | throw cd->langName + " does not support optimization"; |
| 523 | } else if (action.isSet(REQUIRED_FLAG)) { |
| 524 | throw std::string("Don't know how to optimize ") + |
| 525 | cd->langName + " files"; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Short-circuit remaining actions if all they want is optimization |
| 530 | if (finalPhase == OPTIMIZATION) { ++I; continue; } |
| 531 | |
| 532 | /// ASSEMBLY PHASE |
| 533 | action = cd->Assembler; |
| 534 | |
| 535 | if (finalPhase == ASSEMBLY || isSet(EMIT_NATIVE_FLAG)) { |
| 536 | if (isSet(EMIT_NATIVE_FLAG)) { |
| 537 | if (action.program.is_empty()) { |
| 538 | throw std::string("Native Assembler not specified for ") + |
| 539 | cd->langName + " files"; |
| 540 | } else if (finalPhase == ASSEMBLY) { |
| 541 | actions.push_back(GetAction(cd,InFile,OutFile,ASSEMBLY)); |
| 542 | } else { |
| 543 | sys::Path TempFile(MakeTempFile(I->first.get(),"S")); |
| 544 | actions.push_back(GetAction(cd,InFile,TempFile,ASSEMBLY)); |
| 545 | InFile = TempFile; |
| 546 | } |
| 547 | } else { |
| 548 | // Just convert back to llvm assembly with llvm-dis |
| 549 | Action* action = new Action(); |
| 550 | action->program.set_file("llvm-dis"); |
| 551 | action->args.push_back(InFile.get()); |
| 552 | action->args.push_back("-f"); |
| 553 | action->args.push_back("-o"); |
| 554 | action->args.push_back(OutFile.get()); |
| 555 | actions.push_back(action); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // Short-circuit remaining actions if all they want is assembly output |
| 560 | if (finalPhase == ASSEMBLY) { ++I; continue; } |
| 561 | |
| 562 | // Register the OutFile as a link candidate |
| 563 | LinkageItems.insert(InFile); |
| 564 | |
| 565 | // Go to next file to be processed |
| 566 | ++I; |
| 567 | } |
| 568 | |
| 569 | /// RUN THE COMPILATION ACTIONS |
| 570 | std::vector<Action*>::iterator AI = actions.begin(); |
| 571 | std::vector<Action*>::iterator AE = actions.end(); |
| 572 | while (AI != AE) { |
| 573 | if (!DoAction(*AI)) |
| 574 | throw "Action failed"; |
| 575 | AI++; |
| 576 | } |
| 577 | |
| 578 | /// LINKING PHASE |
| 579 | actions.clear(); |
| 580 | if (finalPhase == LINKING) { |
| 581 | if (isSet(EMIT_NATIVE_FLAG)) { |
| 582 | throw "llvmc doesn't know how to link native code yet"; |
| 583 | } else { |
| 584 | // First, we need to examine the files to ensure that they all contain |
| 585 | // bytecode files. Since the final output is bytecode, we can only |
| 586 | // link bytecode. |
| 587 | SetVector<sys::Path>::const_iterator I = LinkageItems.begin(); |
| 588 | SetVector<sys::Path>::const_iterator E = LinkageItems.end(); |
| 589 | std::string errmsg; |
| 590 | |
| 591 | while (I != E && ProcessLinkageItem(*I,LinkageItems,errmsg)) |
| 592 | ++I; |
| 593 | |
| 594 | if (!errmsg.empty()) |
| 595 | throw errmsg; |
| 596 | |
| 597 | // Insert the system libraries. |
| 598 | LibraryPaths.push_back(sys::Path::GetSystemLibraryPath1()); |
| 599 | LibraryPaths.push_back(sys::Path::GetSystemLibraryPath2()); |
| 600 | |
| 601 | // We're emitting bytecode so let's build an llvm-link Action |
| 602 | Action* link = new Action(); |
| 603 | link->program.set_file("llvm-link"); |
| 604 | for (PathVector::const_iterator I=LinkageItems.begin(), |
| 605 | E=LinkageItems.end(); I != E; ++I ) |
| 606 | link->args.push_back(I->get()); |
| 607 | if (isSet(VERBOSE_FLAG)) |
| 608 | link->args.push_back("-v"); |
| 609 | link->args.push_back("-f"); |
| 610 | link->args.push_back("-o"); |
| 611 | link->args.push_back(OutFile.get()); |
| 612 | if (isSet(TIME_PASSES_FLAG)) |
| 613 | link->args.push_back("-time-passes"); |
| 614 | if (isSet(SHOW_STATS_FLAG)) |
| 615 | link->args.push_back("-stats"); |
| 616 | actions.push_back(link); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /// RUN THE LINKING ACTIONS |
| 621 | AI = actions.begin(); |
| 622 | AE = actions.end(); |
| 623 | while (AI != AE) { |
| 624 | if (!DoAction(*AI)) |
| 625 | throw std::string("Action failed"); |
| 626 | AI++; |
| 627 | } |
| 628 | } catch (std::string& msg) { |
| 629 | cleanup(); |
| 630 | throw; |
| 631 | } catch (...) { |
| 632 | cleanup(); |
| 633 | throw std::string("Unspecified error"); |
| 634 | } |
| 635 | cleanup(); |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | /// @} |
| 640 | /// @name Data |
| 641 | /// @{ |
| 642 | private: |
| 643 | ConfigDataProvider* cdp; ///< Where we get configuration data from |
| 644 | Phases finalPhase; ///< The final phase of compilation |
| 645 | OptimizationLevels optLevel; ///< The optimization level to apply |
| 646 | unsigned Flags; ///< The driver flags |
| 647 | std::string machine; ///< Target machine name |
| 648 | PathVector LibraryPaths; ///< -L options |
| 649 | sys::Path TempDir; ///< Name of the temporary directory. |
| 650 | StringTable AdditionalArgs; ///< The -Txyz options |
| 651 | |
| 652 | /// @} |
| 653 | }; |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | CompilerDriver::~CompilerDriver() { |
Reid Spencer | 52c2dc1 | 2004-08-29 19:26:56 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | CompilerDriver* |
| 660 | CompilerDriver::Get(ConfigDataProvider& CDP) { |
| 661 | return new CompilerDriverImpl(CDP); |
Reid Spencer | bae6825 | 2004-08-19 04:49:47 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | CompilerDriver::ConfigData::ConfigData() |
| 665 | : langName() |
| 666 | , PreProcessor() |
| 667 | , Translator() |
| 668 | , Optimizer() |
| 669 | , Assembler() |
| 670 | , Linker() |
| 671 | { |
| 672 | StringVector emptyVec; |
| 673 | for (unsigned i = 0; i < NUM_PHASES; ++i) |
| 674 | opts.push_back(emptyVec); |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame] | 677 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |