1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
11 // This is the llc code generator.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Bytecode/Reader.h"
16 #include "llvm/Target/TargetMachineImpls.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Pass.h"
22 #include "Support/CommandLine.h"
23 #include "Support/Signals.h"
27 // General options for llc. Other pass-specific options are specified
28 // within the corresponding llc passes, and target-specific options
29 // and back-end code generation options are specified with the target machine.
31 static cl::opt<std::string>
32 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
34 static cl::opt<std::string>
35 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
37 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
39 enum ArchName { noarch, x86, Sparc };
41 static cl::opt<ArchName>
42 Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
43 cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"),
44 clEnumValN(Sparc, "sparc", " SPARC V9"),
48 // GetFileNameRoot - Helper function to get the basename of a filename...
49 static inline std::string
50 GetFileNameRoot(const std::string &InputFilename)
52 std::string IFN = InputFilename;
53 std::string outputFilename;
54 int Len = IFN.length();
56 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
57 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
61 return outputFilename;
65 // main - Entry point for the llc compiler.
67 int main(int argc, char **argv) {
68 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
70 // Load the module to be compiled...
71 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
73 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
76 Module &mod = *M.get();
78 // Allocate target machine. First, check whether the user has
79 // explicitly specified an architecture to compile for.
80 TargetMachine* (*TargetMachineAllocator)(const Module&) = 0;
83 TargetMachineAllocator = allocateX86TargetMachine;
86 TargetMachineAllocator = allocateSparcTargetMachine;
89 // Decide what the default target machine should be, by looking at
90 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
91 // SPARCV9) is kind of gross, but it will work until we have more
92 // sophisticated target information to work from.
93 if (mod.getEndianness() == Module::LittleEndian &&
94 mod.getPointerSize() == Module::Pointer32) {
95 TargetMachineAllocator = allocateX86TargetMachine;
96 } else if (mod.getEndianness() == Module::BigEndian &&
97 mod.getPointerSize() == Module::Pointer64) {
98 TargetMachineAllocator = allocateSparcTargetMachine;
100 // If the module is target independent, favor a target which matches the
101 // current build system.
102 #if defined(i386) || defined(__i386__) || defined(__x86__)
103 TargetMachineAllocator = allocateX86TargetMachine;
104 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
105 TargetMachineAllocator = allocateSparcTargetMachine;
107 std::cerr << argv[0] << ": module does not specify a target to use. "
108 << "You must use the -march option.\n";
114 std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod));
115 assert(target.get() && "Could not allocate target machine!");
116 TargetMachine &Target = *target.get();
117 const TargetData &TD = Target.getTargetData();
119 // Build up all of the passes that we want to do to the module...
122 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
123 TD.getPointerAlignment(), TD.getDoubleAlignment()));
125 // Figure out where we are going to send the output...
126 std::ostream *Out = 0;
127 if (OutputFilename != "") {
128 if (OutputFilename != "-") {
129 // Specified an output filename?
130 if (!Force && std::ifstream(OutputFilename.c_str())) {
131 // If force is not specified, make sure not to overwrite a file!
132 std::cerr << argv[0] << ": error opening '" << OutputFilename
133 << "': file exists!\n"
134 << "Use -f command line argument to force output\n";
137 Out = new std::ofstream(OutputFilename.c_str());
139 // Make sure that the Out file gets unlinked from the disk if we get a
141 RemoveFileOnSignal(OutputFilename);
146 if (InputFilename == "-") {
147 OutputFilename = "-";
150 OutputFilename = GetFileNameRoot(InputFilename);
151 OutputFilename += ".s";
153 if (!Force && std::ifstream(OutputFilename.c_str())) {
154 // If force is not specified, make sure not to overwrite a file!
155 std::cerr << argv[0] << ": error opening '" << OutputFilename
156 << "': file exists!\n"
157 << "Use -f command line argument to force output\n";
161 Out = new std::ofstream(OutputFilename.c_str());
163 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
168 // Make sure that the Out file gets unlinked from the disk if we get a
170 RemoveFileOnSignal(OutputFilename);
174 // Ask the target to add backend passes as necessary
175 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
176 std::cerr << argv[0] << ": target '" << Target.getName()
177 << "' does not support static compilation!\n";
178 if (Out != &std::cout) delete Out;
179 // And the Out file is empty and useless, so remove it now.
180 std::remove(OutputFilename.c_str());
183 // Run our queue of passes all at once now, efficiently.
184 Passes.run(*M.get());
187 // Delete the ostream if it's not a stdout stream
188 if (Out != &std::cout) delete Out;