llc.cpp revision 839615a510c582ddcdb09a8e2934f30775daa032
1//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This is the llc code generator driver. It provides a convenient 11// command-line interface for generating native assembly-language code 12// or C code, given LLVM bytecode. 13// 14//===----------------------------------------------------------------------===// 15 16#include "llvm/Bytecode/Reader.h" 17#include "llvm/Target/SubtargetFeature.h" 18#include "llvm/Target/TargetMachine.h" 19#include "llvm/Target/TargetMachineRegistry.h" 20#include "llvm/Transforms/Scalar.h" 21#include "llvm/Module.h" 22#include "llvm/PassManager.h" 23#include "llvm/Pass.h" 24#include "llvm/Support/CommandLine.h" 25#include "llvm/Support/PluginLoader.h" 26#include "llvm/Support/PassNameParser.h" 27#include "llvm/Analysis/Verifier.h" 28#include "llvm/System/Signals.h" 29#include "llvm/Config/config.h" 30#include <fstream> 31#include <iostream> 32#include <memory> 33 34using namespace llvm; 35 36// General options for llc. Other pass-specific options are specified 37// within the corresponding llc passes, and target-specific options 38// and back-end code generation options are specified with the target machine. 39// 40static cl::opt<std::string> 41InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); 42 43static cl::opt<std::string> 44OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); 45 46static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); 47 48static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser> 49MArch("march", cl::desc("Architecture to generate code for:")); 50 51static cl::opt<std::string> 52MCPU("mcpu", 53 cl::desc("Target a specific cpu type (-mcpu=help for list of choices)"), 54 cl::value_desc("cpu-name"), 55 cl::init("")); 56 57static cl::list<std::string> 58MAttrs("mattr", 59 cl::CommaSeparated, 60 cl::desc("Target specific attributes (-mattr=help for list of choices)"), 61 cl::value_desc("attr1,+attr2, ..., -attrN")); 62 63cl::opt<TargetMachine::CodeGenFileType> 64FileType("filetype", cl::init(TargetMachine::AssemblyFile), 65 cl::desc("Choose a file type (not all types are supported by all targets):"), 66 cl::values( 67 clEnumValN(TargetMachine::AssemblyFile, "asm", 68 " Emit an assembly ('.s') file"), 69 clEnumValN(TargetMachine::ObjectFile, "obj", 70 " Emit a native object ('.o') file"), 71 clEnumValN(TargetMachine::DynamicLibrary, "dynlib", 72 " Emit a native dynamic library ('.so') file"), 73 clEnumValEnd)); 74 75// The LLCPassList is populated with passes that were registered using 76// PassInfo::LLC by the FilteredPassNameParser: 77cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::LLC> > 78LLCPassList(cl::desc("Passes Available")); 79 80cl::opt<bool> NoVerify("disable-verify", cl::Hidden, 81 cl::desc("Do not verify input module")); 82 83 84// GetFileNameRoot - Helper function to get the basename of a filename. 85static inline std::string 86GetFileNameRoot(const std::string &InputFilename) { 87 std::string IFN = InputFilename; 88 std::string outputFilename; 89 int Len = IFN.length(); 90 if ((Len > 2) && 91 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { 92 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ 93 } else { 94 outputFilename = IFN; 95 } 96 return outputFilename; 97} 98 99 100// main - Entry point for the llc compiler. 101// 102int main(int argc, char **argv) { 103 try { 104 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); 105 sys::PrintStackTraceOnErrorSignal(); 106 107 // Load the module to be compiled... 108 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); 109 if (M.get() == 0) { 110 std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; 111 return 1; 112 } 113 Module &mod = *M.get(); 114 115 // Allocate target machine. First, check whether the user has 116 // explicitly specified an architecture to compile for. 117 TargetMachine* (*TargetMachineAllocator)(const Module&, 118 IntrinsicLowering *) = 0; 119 if (MArch == 0) { 120 std::string Err; 121 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err); 122 if (MArch == 0) { 123 std::cerr << argv[0] << ": error auto-selecting target for module '" 124 << Err << "'. Please use the -march option to explicitly " 125 << "pick a target.\n"; 126 return 1; 127 } 128 } 129 130 // Package up features to be passed to target/subtarget 131 std::string FeaturesStr; 132 if (MCPU.size() || MAttrs.size()) { 133 SubtargetFeatures Features; 134 Features.setCPU(MCPU); 135 for (unsigned i = 0; i != MAttrs.size(); ++i) 136 Features.AddFeature(MAttrs[i]); 137 FeaturesStr = Features.getString(); 138 } 139 140 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0, FeaturesStr)); 141 assert(target.get() && "Could not allocate target machine!"); 142 TargetMachine &Target = *target.get(); 143 const TargetData &TD = Target.getTargetData(); 144 145 // Build up all of the passes that we want to do to the module... 146 PassManager Passes; 147 Passes.add(new TargetData(TD)); 148 149 // Create a new pass for each one specified on the command line 150 for (unsigned i = 0; i < LLCPassList.size(); ++i) { 151 const PassInfo *aPass = LLCPassList[i]; 152 153 if (aPass->getNormalCtor()) { 154 Pass *P = aPass->getNormalCtor()(); 155 Passes.add(P); 156 } else { 157 std::cerr << argv[0] << ": cannot create pass: " 158 << aPass->getPassName() << "\n"; 159 } 160 } 161 162#ifndef NDEBUG 163 if(!NoVerify) 164 Passes.add(createVerifierPass()); 165#endif 166 167 // Figure out where we are going to send the output... 168 std::ostream *Out = 0; 169 if (OutputFilename != "") { 170 if (OutputFilename != "-") { 171 // Specified an output filename? 172 if (!Force && std::ifstream(OutputFilename.c_str())) { 173 // If force is not specified, make sure not to overwrite a file! 174 std::cerr << argv[0] << ": error opening '" << OutputFilename 175 << "': file exists!\n" 176 << "Use -f command line argument to force output\n"; 177 return 1; 178 } 179 Out = new std::ofstream(OutputFilename.c_str()); 180 181 // Make sure that the Out file gets unlinked from the disk if we get a 182 // SIGINT 183 sys::RemoveFileOnSignal(sys::Path(OutputFilename)); 184 } else { 185 Out = &std::cout; 186 } 187 } else { 188 if (InputFilename == "-") { 189 OutputFilename = "-"; 190 Out = &std::cout; 191 } else { 192 OutputFilename = GetFileNameRoot(InputFilename); 193 194 switch (FileType) { 195 case TargetMachine::AssemblyFile: 196 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE 197 OutputFilename += ".s"; 198 else 199 OutputFilename += ".cbe.c"; 200 break; 201 case TargetMachine::ObjectFile: 202 OutputFilename += ".o"; 203 break; 204 case TargetMachine::DynamicLibrary: 205 OutputFilename += LTDL_SHLIB_EXT; 206 break; 207 } 208 209 if (!Force && std::ifstream(OutputFilename.c_str())) { 210 // If force is not specified, make sure not to overwrite a file! 211 std::cerr << argv[0] << ": error opening '" << OutputFilename 212 << "': file exists!\n" 213 << "Use -f command line argument to force output\n"; 214 return 1; 215 } 216 217 Out = new std::ofstream(OutputFilename.c_str()); 218 if (!Out->good()) { 219 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; 220 delete Out; 221 return 1; 222 } 223 224 // Make sure that the Out file gets unlinked from the disk if we get a 225 // SIGINT 226 sys::RemoveFileOnSignal(sys::Path(OutputFilename)); 227 } 228 } 229 230 // Ask the target to add backend passes as necessary. 231 if (Target.addPassesToEmitFile(Passes, *Out, FileType)) { 232 std::cerr << argv[0] << ": target '" << Target.getName() 233 << "' does not support generation of this file type!\n"; 234 if (Out != &std::cout) delete Out; 235 // And the Out file is empty and useless, so remove it now. 236 std::remove(OutputFilename.c_str()); 237 return 1; 238 } else { 239 // Run our queue of passes all at once now, efficiently. 240 Passes.run(*M.get()); 241 } 242 243 // Delete the ostream if it's not a stdout stream 244 if (Out != &std::cout) delete Out; 245 246 return 0; 247 } catch (const std::string& msg) { 248 std::cerr << argv[0] << ": " << msg << "\n"; 249 } catch (...) { 250 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; 251 } 252 return 1; 253} 254