Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 1 | //===--- llvmc.cpp - The LLVM Compiler Driver -------------------*- C++ -*-===// |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tool provides a single point of access to the LLVM compilation tools. |
| 11 | // It has many options. To discover the options supported please refer to the |
| 12 | // tools' manual page (docs/CommandGuide/html/llvmc.html) or run the tool with |
| 13 | // the --help option. |
| 14 | // |
| 15 | //===------------------------------------------------------------------------=== |
| 16 | |
Reid Spencer | abf1ce3 | 2004-08-10 16:29:18 +0000 | [diff] [blame] | 17 | #include "CompilerDriver.h" |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 18 | #include "ConfigData.h" |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 19 | #include "llvm/System/Signals.h" |
| 20 | #include "Support/CommandLine.h" |
| 21 | #include <iostream> |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 25 | namespace { |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 26 | //===------------------------------------------------------------------------=== |
| 27 | //=== PHASE OPTIONS |
| 28 | //===------------------------------------------------------------------------=== |
| 29 | static cl::opt<CompilerDriver::Phases> FinalPhase( |
| 30 | cl::desc("Choose final phase of compilation:"), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 31 | cl::init(CompilerDriver::LINKING), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 32 | cl::values( |
| 33 | clEnumValN(CompilerDriver::PREPROCESSING,"E", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 34 | "Stop compilation after pre-processing phase"), |
| 35 | clEnumValN(CompilerDriver::TRANSLATION, "t", |
| 36 | "Stop compilation after translation phase"), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 37 | clEnumValN(CompilerDriver::OPTIMIZATION,"c", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 38 | "Stop compilation after optimization phase"), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 39 | clEnumValN(CompilerDriver::ASSEMBLY,"S", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 40 | "Stop compilation after assembly phase"), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 41 | clEnumValEnd |
| 42 | ) |
| 43 | ); |
| 44 | |
| 45 | //===------------------------------------------------------------------------=== |
| 46 | //=== OPTIMIZATION OPTIONS |
| 47 | //===------------------------------------------------------------------------=== |
| 48 | static cl::opt<CompilerDriver::OptimizationLevels> OptLevel( |
| 49 | cl::desc("Choose level of optimization to apply:"), |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 50 | cl::init(CompilerDriver::OPT_FAST_COMPILE), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 51 | cl::values( |
| 52 | clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O0", |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 53 | "An alias for the -O1 option."), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 54 | clEnumValN(CompilerDriver::OPT_FAST_COMPILE,"O1", |
| 55 | "Optimize for compilation speed, not execution speed."), |
| 56 | clEnumValN(CompilerDriver::OPT_SIMPLE,"O2", |
| 57 | "Perform simple translation time optimizations"), |
| 58 | clEnumValN(CompilerDriver::OPT_AGGRESSIVE,"O3", |
| 59 | "Perform aggressive translation time optimizations"), |
| 60 | clEnumValN(CompilerDriver::OPT_LINK_TIME,"O4", |
| 61 | "Perform link time optimizations"), |
| 62 | clEnumValN(CompilerDriver::OPT_AGGRESSIVE_LINK_TIME,"O5", |
| 63 | "Perform aggressive link time optimizations"), |
| 64 | clEnumValEnd |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | //===------------------------------------------------------------------------=== |
| 69 | //=== TOOL OPTIONS |
| 70 | //===------------------------------------------------------------------------=== |
| 71 | |
| 72 | static cl::opt<std::string> PPToolOpts("Tpp", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 73 | cl::desc("Pass specific options to the pre-processor"), |
| 74 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 75 | |
| 76 | static cl::opt<std::string> AsmToolOpts("Tasm", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 77 | cl::desc("Pass specific options to the assembler"), |
| 78 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 79 | |
| 80 | static cl::opt<std::string> OptToolOpts("Topt", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 81 | cl::desc("Pass specific options to the optimizer"), |
| 82 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 83 | |
| 84 | static cl::opt<std::string> LinkToolOpts("Tlink", cl::ZeroOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 85 | cl::desc("Pass specific options to the linker"), |
| 86 | cl::value_desc("option")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 87 | |
| 88 | //===------------------------------------------------------------------------=== |
| 89 | //=== INPUT OPTIONS |
| 90 | //===------------------------------------------------------------------------=== |
| 91 | |
| 92 | static cl::list<std::string> LibPaths("L", cl::Prefix, |
| 93 | cl::desc("Specify a library search path"), cl::value_desc("directory")); |
| 94 | |
| 95 | static cl::list<std::string> Libraries("l", cl::Prefix, |
| 96 | cl::desc("Specify libraries to link to"), cl::value_desc("library prefix")); |
| 97 | |
| 98 | |
| 99 | //===------------------------------------------------------------------------=== |
| 100 | //=== OUTPUT OPTIONS |
| 101 | //===------------------------------------------------------------------------=== |
| 102 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 103 | static cl::opt<std::string> OutputFilename("o", |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 104 | cl::desc("Override output filename"), cl::value_desc("filename")); |
| 105 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 106 | static cl::opt<std::string> OutputMachine("m", cl::Prefix, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 107 | cl::desc("Specify a target machine"), cl::value_desc("machine")); |
| 108 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 109 | static cl::opt<bool> Native("native", cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 110 | cl::desc("Generative native object and executables instead of bytecode")); |
| 111 | |
| 112 | //===------------------------------------------------------------------------=== |
| 113 | //=== INFORMATION OPTIONS |
| 114 | //===------------------------------------------------------------------------=== |
| 115 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 116 | static cl::opt<bool> DryRun("dry-run", cl::Optional, cl::init(false), |
| 117 | cl::desc("Do everything but perform the compilation actions")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 118 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 119 | static cl::alias DryRunAlias("y", cl::Optional, |
| 120 | cl::desc("Alias for -dry-run"), cl::aliasopt(DryRun)); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 121 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 122 | static cl::opt<bool> Verbose("verbose", cl::Optional, cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 123 | cl::desc("Print out each action taken")); |
| 124 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 125 | static cl::alias VerboseAlias("v", cl::Optional, |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 126 | cl::desc("Alias for -verbose"), cl::aliasopt(Verbose)); |
| 127 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 128 | static cl::opt<bool> Debug("debug", cl::Optional, cl::init(false), |
| 129 | cl::Hidden, cl::desc("Print out debugging information")); |
| 130 | |
| 131 | static cl::alias DebugAlias("d", cl::Optional, |
| 132 | cl::desc("Alias for -debug"), cl::aliasopt(Debug)); |
| 133 | |
| 134 | static cl::opt<bool> TimeActions("time-actions", cl::Optional, cl::init(false), |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 135 | cl::desc("Print execution time for each action taken")); |
| 136 | |
| 137 | //===------------------------------------------------------------------------=== |
| 138 | //=== ADVANCED OPTIONS |
| 139 | //===------------------------------------------------------------------------=== |
| 140 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 141 | static cl::opt<std::string> ConfigDir("config-dir", cl::Optional, |
| 142 | cl::desc("Specify a configuration directory to override defaults"), |
| 143 | cl::value_desc("directory")); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 144 | |
| 145 | static cl::opt<bool> EmitRawCode("emit-raw-code", cl::Hidden, |
| 146 | cl::desc("Emit raw, unoptimized code")); |
| 147 | |
| 148 | //===------------------------------------------------------------------------=== |
| 149 | //=== POSITIONAL OPTIONS |
| 150 | //===------------------------------------------------------------------------=== |
| 151 | |
| 152 | static cl::list<std::string> Files(cl::Positional, cl::OneOrMore, |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 153 | cl::desc("[Sources/objects/libraries]")); |
| 154 | |
| 155 | static cl::list<std::string> Languages("x", cl::ZeroOrMore, |
| 156 | cl::desc("Specify the source language for subsequent files"), |
| 157 | cl::value_desc("language")); |
| 158 | |
| 159 | //===------------------------------------------------------------------------=== |
| 160 | //=== GetFileType - determine type of a file |
| 161 | //===------------------------------------------------------------------------=== |
| 162 | const std::string GetFileType(const std::string& fname, unsigned pos ) { |
| 163 | static std::vector<std::string>::iterator langIt = Languages.begin(); |
| 164 | static std::string CurrLang = ""; |
| 165 | |
| 166 | // If a -x LANG option has been specified .. |
| 167 | if ( langIt != Languages.end() ) |
| 168 | // If the -x LANG option came before the current file on command line |
| 169 | if ( Languages.getPosition( langIt - Languages.begin() ) < pos ) { |
| 170 | // use that language |
| 171 | CurrLang = *langIt++; |
| 172 | return CurrLang; |
| 173 | } |
| 174 | |
| 175 | // If there's a current language in effect |
| 176 | if (!CurrLang.empty()) |
| 177 | return CurrLang; // use that language |
| 178 | |
| 179 | // otherwise just determine lang from the filename's suffix |
| 180 | return fname.substr( fname.rfind('.',fname.size()) + 1 ); |
| 181 | } |
| 182 | |
| 183 | } // end anonymous namespace |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | /// @brief The main program for llvmc |
| 187 | int main(int argc, char **argv) { |
| 188 | // Make sure we print stack trace if we get bad signals |
| 189 | PrintStackTraceOnErrorSignal(); |
| 190 | |
| 191 | // Parse the command line options |
| 192 | cl::ParseCommandLineOptions(argc, argv, |
| 193 | " LLVM Compilation Driver (llvmc)\n\n" |
| 194 | " This program provides easy invocation of the LLVM tool set\n" |
| 195 | " and source language compiler tools.\n" |
| 196 | ); |
| 197 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 198 | // Deal with unimplemented options. |
| 199 | if (Native) |
| 200 | std::cerr << argv[0] << ": Not implemented yet: -native"; |
| 201 | if (EmitRawCode) |
| 202 | std::cerr << argv[0] << ": Not implemented yet: -emit-raw-code"; |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 203 | |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 204 | // Default the output file, only if we're going to try to link |
| 205 | if (OutputFilename.empty() && OptLevel == CompilerDriver::LINKING) |
| 206 | OutputFilename = "a.out"; |
| 207 | |
| 208 | // Construct the ConfigDataProvider object |
| 209 | LLVMC_ConfigDataProvider Provider; |
| 210 | |
| 211 | // Construct the CompilerDriver object |
| 212 | CompilerDriver CD(Provider); |
| 213 | |
| 214 | // Configure the driver based on options |
| 215 | CD.setVerbose(Verbose); |
| 216 | CD.setDebug(Debug); |
| 217 | CD.setDryRun(DryRun); |
| 218 | CD.setFinalPhase(FinalPhase); |
| 219 | CD.setOptimization(OptLevel); |
| 220 | CD.setOutputMachine(OutputMachine); |
| 221 | CD.setEmitNativeCode(Native); |
| 222 | CD.setEmitRawCode(EmitRawCode); |
| 223 | std::vector<std::string>::iterator pathIt = LibPaths.begin(); |
| 224 | while ( pathIt != LibPaths.end() ) { |
| 225 | CD.addLibraryPath( *pathIt++ ); |
| 226 | } |
| 227 | |
| 228 | // Prepare the list of files to be compiled by the CompilerDriver. |
| 229 | CompilerDriver::InputList InpList; |
| 230 | std::vector<std::string>::iterator fileIt = Files.begin(); |
| 231 | std::vector<std::string>::iterator libIt = Libraries.begin(); |
| 232 | unsigned libPos = 0, filePos = 0; |
| 233 | while ( 1 ) { |
| 234 | if ( libIt != Libraries.end() ) |
| 235 | libPos = Libraries.getPosition( libIt - Libraries.begin() ); |
| 236 | else |
| 237 | libPos = 0; |
| 238 | if ( fileIt != Files.end() ) |
| 239 | filePos = Files.getPosition( fileIt - Files.begin() ); |
| 240 | else |
| 241 | filePos = 0; |
| 242 | |
| 243 | if ( filePos != 0 && (libPos == 0 || filePos < libPos) ) { |
| 244 | // Add a source file |
| 245 | InpList.push_back( std::make_pair(*fileIt, GetFileType(*fileIt,filePos))); |
| 246 | ++fileIt; |
| 247 | } |
| 248 | else if ( libPos != 0 && (filePos == 0 || libPos < filePos) ) { |
| 249 | // Add a library |
| 250 | InpList.push_back( std::make_pair(*libIt++,"")); |
| 251 | } |
| 252 | else |
| 253 | break; // we're done with the list |
| 254 | } |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 255 | |
| 256 | // Tell the driver to do its thing |
Reid Spencer | 5c56dc1 | 2004-08-13 20:22:43 +0000 | [diff] [blame^] | 257 | int result = CD.execute(InpList,OutputFilename); |
Reid Spencer | 034a544 | 2004-08-10 16:26:01 +0000 | [diff] [blame] | 258 | if (result != 0) { |
| 259 | std::cerr << argv[0] << ": Error executing actions. Terminated.\n"; |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | // All is good, return success |
| 264 | return 0; |
| 265 | } |