1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
3 // This is the llc compiler driver.
5 //===----------------------------------------------------------------------===//
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"
20 //------------------------------------------------------------------------------
21 // Option declarations for LLC.
22 //------------------------------------------------------------------------------
24 // General options for llc. Other pass-specific options are specified
25 // within the corresponding llc passes, and target-specific options
26 // and back-end code generation options are specified with the target machine.
28 static cl::opt<std::string>
29 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
31 static cl::opt<std::string>
32 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
34 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
36 enum ArchName { noarch, x86, Sparc };
38 static cl::opt<ArchName>
39 Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
40 cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"),
41 clEnumValN(Sparc, "sparc", " SPARC V9"),
45 // GetFileNameRoot - Helper function to get the basename of a filename...
46 static inline std::string
47 GetFileNameRoot(const std::string &InputFilename)
49 std::string IFN = InputFilename;
50 std::string outputFilename;
51 int Len = IFN.length();
52 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
53 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
57 return outputFilename;
61 //===---------------------------------------------------------------------===//
64 // Entry point for the llc compiler.
65 //===---------------------------------------------------------------------===//
68 main(int argc, char **argv)
70 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
72 // Load the module to be compiled...
73 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
76 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
79 Module &mod = *M.get();
81 // Allocate target machine. First, check whether the user has
82 // explicitly specified an architecture to compile for.
83 unsigned Config = (mod.isLittleEndian() ? TM::LittleEndian : TM::BigEndian) |
84 (mod.has32BitPointers() ? TM::PtrSize32 : TM::PtrSize64);
85 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
88 TargetMachineAllocator = allocateX86TargetMachine;
91 TargetMachineAllocator = allocateSparcTargetMachine;
94 // Decide what the default target machine should be, by looking at
95 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
96 // SPARCV9) is kind of gross, but it will work until we have more
97 // sophisticated target information to work from.
98 if (mod.isLittleEndian() && mod.has32BitPointers()) {
99 TargetMachineAllocator = allocateX86TargetMachine;
100 } else if (mod.isBigEndian() && mod.has64BitPointers()) {
101 TargetMachineAllocator = allocateSparcTargetMachine;
103 assert(0 && "You must specify -march; I could not guess the default");
107 std::auto_ptr<TargetMachine> target((*TargetMachineAllocator)(Config));
108 assert(target.get() && "Could not allocate target machine!");
109 TargetMachine &Target = *target.get();
110 const TargetData &TD = Target.getTargetData();
112 // Build up all of the passes that we want to do to the module...
115 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
116 TD.getPointerAlignment(), TD.getDoubleAlignment()));
118 // Figure out where we are going to send the output...
119 std::ostream *Out = 0;
120 if (OutputFilename != "") {
121 // Specified an output filename?
122 if (!Force && std::ifstream(OutputFilename.c_str())) {
123 // If force is not specified, make sure not to overwrite a file!
124 std::cerr << argv[0] << ": error opening '" << OutputFilename
125 << "': file exists!\n"
126 << "Use -f command line argument to force output\n";
129 Out = new std::ofstream(OutputFilename.c_str());
131 // Make sure that the Out file gets unlink'd from the disk if we get a
133 RemoveFileOnSignal(OutputFilename);
135 if (InputFilename == "-") {
136 OutputFilename = "-";
139 OutputFilename = GetFileNameRoot(InputFilename);
140 OutputFilename += ".s";
142 if (!Force && std::ifstream(OutputFilename.c_str())) {
143 // If force is not specified, make sure not to overwrite a file!
144 std::cerr << argv[0] << ": error opening '" << OutputFilename
145 << "': file exists!\n"
146 << "Use -f command line argument to force output\n";
150 Out = new std::ofstream(OutputFilename.c_str());
152 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
157 // Make sure that the Out file gets unlink'd from the disk if we get a
159 RemoveFileOnSignal(OutputFilename);
163 // Ask the target to add backend passes as necessary
164 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
165 std::cerr << argv[0] << ": target '" << Target.getName()
166 << "' does not support static compilation!\n";
167 if (Out != &std::cout) delete Out;
168 // And the Out file is empty and useless, so remove it now.
169 std::remove(OutputFilename.c_str());
172 // Run our queue of passes all at once now, efficiently.
173 Passes.run(*M.get());
176 // Delete the ostream if it's not a stdout stream
177 if (Out != &std::cout) delete Out;