llc.cpp revision 5b836c4a06c584544647d5b013b193510ac5ced5
1//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// 2// 3// This is the llc code generator. 4// 5//===----------------------------------------------------------------------===// 6 7#include "llvm/Bytecode/Reader.h" 8#include "llvm/Target/TargetMachineImpls.h" 9#include "llvm/Target/TargetMachine.h" 10#include "llvm/Transforms/Scalar.h" 11#include "llvm/Module.h" 12#include "llvm/PassManager.h" 13#include "llvm/Pass.h" 14#include "Support/CommandLine.h" 15#include "Support/Signals.h" 16#include <memory> 17#include <fstream> 18 19// General options for llc. Other pass-specific options are specified 20// within the corresponding llc passes, and target-specific options 21// and back-end code generation options are specified with the target machine. 22// 23static cl::opt<std::string> 24InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); 25 26static cl::opt<std::string> 27OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); 28 29static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); 30 31enum ArchName { noarch, x86, Sparc }; 32 33static cl::opt<ArchName> 34Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix, 35 cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"), 36 clEnumValN(Sparc, "sparc", " SPARC V9"), 37 0), 38 cl::init(noarch)); 39 40// GetFileNameRoot - Helper function to get the basename of a filename... 41static inline std::string 42GetFileNameRoot(const std::string &InputFilename) 43{ 44 std::string IFN = InputFilename; 45 std::string outputFilename; 46 int Len = IFN.length(); 47 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { 48 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ 49 } else { 50 outputFilename = IFN; 51 } 52 return outputFilename; 53} 54 55 56// main - Entry point for the llc compiler. 57// 58int main(int argc, char **argv) { 59 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); 60 61 // Load the module to be compiled... 62 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); 63 if (M.get() == 0) { 64 std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; 65 return 1; 66 } 67 Module &mod = *M.get(); 68 69 // Allocate target machine. First, check whether the user has 70 // explicitly specified an architecture to compile for. 71 unsigned Config = (mod.isLittleEndian() ? TM::LittleEndian : TM::BigEndian)| 72 (mod.has32BitPointers() ? TM::PtrSize32 : TM::PtrSize64); 73 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0; 74 switch (Arch) { 75 case x86: 76 TargetMachineAllocator = allocateX86TargetMachine; 77 break; 78 case Sparc: 79 TargetMachineAllocator = allocateSparcTargetMachine; 80 break; 81 default: 82 // Decide what the default target machine should be, by looking at 83 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE -> 84 // SPARCV9) is kind of gross, but it will work until we have more 85 // sophisticated target information to work from. 86 if (mod.isLittleEndian() && mod.has32BitPointers()) { 87 TargetMachineAllocator = allocateX86TargetMachine; 88 } else if (mod.isBigEndian() && mod.has64BitPointers()) { 89 TargetMachineAllocator = allocateSparcTargetMachine; 90 } else { 91 assert(0 && "You must specify -march; I could not guess the default"); 92 } 93 break; 94 } 95 std::auto_ptr<TargetMachine> target((*TargetMachineAllocator)(Config)); 96 assert(target.get() && "Could not allocate target machine!"); 97 TargetMachine &Target = *target.get(); 98 const TargetData &TD = Target.getTargetData(); 99 100 // Build up all of the passes that we want to do to the module... 101 PassManager Passes; 102 103 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(), 104 TD.getPointerAlignment(), TD.getDoubleAlignment())); 105 106 // Figure out where we are going to send the output... 107 std::ostream *Out = 0; 108 if (OutputFilename != "") { 109 if (OutputFilename != "-") { 110 // Specified an output filename? 111 if (!Force && std::ifstream(OutputFilename.c_str())) { 112 // If force is not specified, make sure not to overwrite a file! 113 std::cerr << argv[0] << ": error opening '" << OutputFilename 114 << "': file exists!\n" 115 << "Use -f command line argument to force output\n"; 116 return 1; 117 } 118 Out = new std::ofstream(OutputFilename.c_str()); 119 120 // Make sure that the Out file gets unlink'd from the disk if we get a 121 // SIGINT 122 RemoveFileOnSignal(OutputFilename); 123 } else { 124 Out = &std::cout; 125 } 126 } else { 127 if (InputFilename == "-") { 128 OutputFilename = "-"; 129 Out = &std::cout; 130 } else { 131 OutputFilename = GetFileNameRoot(InputFilename); 132 OutputFilename += ".s"; 133 134 if (!Force && std::ifstream(OutputFilename.c_str())) { 135 // If force is not specified, make sure not to overwrite a file! 136 std::cerr << argv[0] << ": error opening '" << OutputFilename 137 << "': file exists!\n" 138 << "Use -f command line argument to force output\n"; 139 return 1; 140 } 141 142 Out = new std::ofstream(OutputFilename.c_str()); 143 if (!Out->good()) { 144 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; 145 delete Out; 146 return 1; 147 } 148 149 // Make sure that the Out file gets unlink'd from the disk if we get a 150 // SIGINT 151 RemoveFileOnSignal(OutputFilename); 152 } 153 } 154 155 // Ask the target to add backend passes as necessary 156 if (Target.addPassesToEmitAssembly(Passes, *Out)) { 157 std::cerr << argv[0] << ": target '" << Target.getName() 158 << "' does not support static compilation!\n"; 159 if (Out != &std::cout) delete Out; 160 // And the Out file is empty and useless, so remove it now. 161 std::remove(OutputFilename.c_str()); 162 return 1; 163 } else { 164 // Run our queue of passes all at once now, efficiently. 165 Passes.run(*M.get()); 166 } 167 168 // Delete the ostream if it's not a stdout stream 169 if (Out != &std::cout) delete Out; 170 171 return 0; 172} 173