llc.cpp revision 86f42bdad93677fa0ca33b27afb0f493028376cb
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/TargetMachineImpls.h" 18#include "llvm/Target/TargetMachine.h" 19#include "llvm/Transforms/Scalar.h" 20#include "llvm/Module.h" 21#include "llvm/PassManager.h" 22#include "llvm/Pass.h" 23#include "Support/CommandLine.h" 24#include "llvm/System/Signals.h" 25#include <fstream> 26#include <iostream> 27#include <memory> 28 29using namespace llvm; 30 31// General options for llc. Other pass-specific options are specified 32// within the corresponding llc passes, and target-specific options 33// and back-end code generation options are specified with the target machine. 34// 35static cl::opt<std::string> 36InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); 37 38static cl::opt<std::string> 39OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); 40 41static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); 42 43enum ArchName { noarch, X86, SparcV9, PowerPC, CBackend }; 44 45static cl::opt<ArchName> 46Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix, 47 cl::values(clEnumValN(X86, "x86", " IA-32 (Pentium and above)"), 48 clEnumValN(SparcV9, "sparcv9", " SPARC V9"), 49 clEnumValN(PowerPC, "powerpc", " PowerPC (experimental)"), 50 clEnumValN(CBackend, "c", " C backend"), 51 0), 52 cl::init(noarch)); 53 54// GetFileNameRoot - Helper function to get the basename of a filename... 55static inline std::string 56GetFileNameRoot(const std::string &InputFilename) 57{ 58 std::string IFN = InputFilename; 59 std::string outputFilename; 60 int Len = IFN.length(); 61 if ((Len > 2) && 62 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { 63 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ 64 } else { 65 outputFilename = IFN; 66 } 67 return outputFilename; 68} 69 70 71// main - Entry point for the llc compiler. 72// 73int main(int argc, char **argv) { 74 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); 75 PrintStackTraceOnErrorSignal(); 76 77 // Load the module to be compiled... 78 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); 79 if (M.get() == 0) { 80 std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; 81 return 1; 82 } 83 Module &mod = *M.get(); 84 85 // Allocate target machine. First, check whether the user has 86 // explicitly specified an architecture to compile for. 87 TargetMachine* (*TargetMachineAllocator)(const Module&, 88 IntrinsicLowering *) = 0; 89 switch (Arch) { 90 case CBackend: 91 TargetMachineAllocator = allocateCTargetMachine; 92 break; 93 case X86: 94 TargetMachineAllocator = allocateX86TargetMachine; 95 break; 96 case SparcV9: 97 TargetMachineAllocator = allocateSparcV9TargetMachine; 98 break; 99 case PowerPC: 100 TargetMachineAllocator = allocatePowerPCTargetMachine; 101 break; 102 default: 103 // Decide what the default target machine should be, by looking at 104 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE -> 105 // SPARCV9) is kind of gross, but it will work until we have more 106 // sophisticated target information to work from. 107 if (mod.getEndianness() == Module::LittleEndian && 108 mod.getPointerSize() == Module::Pointer32) { 109 TargetMachineAllocator = allocateX86TargetMachine; 110 } else if (mod.getEndianness() == Module::BigEndian && 111 mod.getPointerSize() == Module::Pointer32) { 112 TargetMachineAllocator = allocatePowerPCTargetMachine; 113 } else if (mod.getEndianness() == Module::BigEndian && 114 mod.getPointerSize() == Module::Pointer64) { 115 TargetMachineAllocator = allocateSparcV9TargetMachine; 116 } else { 117 // If the module is target independent, favor a target which matches the 118 // current build system. 119#if defined(i386) || defined(__i386__) || defined(__x86__) 120 TargetMachineAllocator = allocateX86TargetMachine; 121#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9) 122 TargetMachineAllocator = allocateSparcV9TargetMachine; 123#elif defined(__POWERPC__) || defined(__ppc__) || defined(__APPLE__) 124 TargetMachineAllocator = allocatePowerPCTargetMachine; 125#else 126 std::cerr << argv[0] << ": module does not specify a target to use. " 127 << "You must use the -march option. If no native target is " 128 << "available, use -march=c to emit C code.\n"; 129 return 1; 130#endif 131 } 132 break; 133 } 134 std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod, 0)); 135 assert(target.get() && "Could not allocate target machine!"); 136 TargetMachine &Target = *target.get(); 137 const TargetData &TD = Target.getTargetData(); 138 139 // Build up all of the passes that we want to do to the module... 140 PassManager Passes; 141 142 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(), 143 TD.getPointerAlignment(), TD.getDoubleAlignment())); 144 145 // Figure out where we are going to send the output... 146 std::ostream *Out = 0; 147 if (OutputFilename != "") { 148 if (OutputFilename != "-") { 149 // Specified an output filename? 150 if (!Force && std::ifstream(OutputFilename.c_str())) { 151 // If force is not specified, make sure not to overwrite a file! 152 std::cerr << argv[0] << ": error opening '" << OutputFilename 153 << "': file exists!\n" 154 << "Use -f command line argument to force output\n"; 155 return 1; 156 } 157 Out = new std::ofstream(OutputFilename.c_str()); 158 159 // Make sure that the Out file gets unlinked from the disk if we get a 160 // SIGINT 161 RemoveFileOnSignal(OutputFilename); 162 } else { 163 Out = &std::cout; 164 } 165 } else { 166 if (InputFilename == "-") { 167 OutputFilename = "-"; 168 Out = &std::cout; 169 } else { 170 OutputFilename = GetFileNameRoot(InputFilename); 171 172 if (Arch != CBackend) 173 OutputFilename += ".s"; 174 else 175 OutputFilename += ".cbe.c"; 176 177 if (!Force && std::ifstream(OutputFilename.c_str())) { 178 // If force is not specified, make sure not to overwrite a file! 179 std::cerr << argv[0] << ": error opening '" << OutputFilename 180 << "': file exists!\n" 181 << "Use -f command line argument to force output\n"; 182 return 1; 183 } 184 185 Out = new std::ofstream(OutputFilename.c_str()); 186 if (!Out->good()) { 187 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; 188 delete Out; 189 return 1; 190 } 191 192 // Make sure that the Out file gets unlinked from the disk if we get a 193 // SIGINT 194 RemoveFileOnSignal(OutputFilename); 195 } 196 } 197 198 // Ask the target to add backend passes as necessary 199 if (Target.addPassesToEmitAssembly(Passes, *Out)) { 200 std::cerr << argv[0] << ": target '" << Target.getName() 201 << "' does not support static compilation!\n"; 202 if (Out != &std::cout) delete Out; 203 // And the Out file is empty and useless, so remove it now. 204 std::remove(OutputFilename.c_str()); 205 return 1; 206 } else { 207 // Run our queue of passes all at once now, efficiently. 208 Passes.run(*M.get()); 209 } 210 211 // Delete the ostream if it's not a stdout stream 212 if (Out != &std::cout) delete Out; 213 214 return 0; 215} 216