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