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 //===----------------------------------------------------------------------===//
10 // This is the llc code generator.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bytecode/Reader.h"
15 #include "llvm/Target/TargetMachineImpls.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Pass.h"
21 #include "Support/CommandLine.h"
22 #include "Support/Signals.h"
28 // General options for llc. Other pass-specific options are specified
29 // within the corresponding llc passes, and target-specific options
30 // and back-end code generation options are specified with the target machine.
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
35 static cl::opt<std::string>
36 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
38 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
40 enum ArchName { noarch, x86, Sparc };
42 static cl::opt<ArchName>
43 Arch("march", cl::desc("Architecture to generate assembly for:"), cl::Prefix,
44 cl::values(clEnumVal(x86, " IA-32 (Pentium and above)"),
45 clEnumValN(Sparc, "sparc", " SPARC V9"),
49 // GetFileNameRoot - Helper function to get the basename of a filename...
50 static inline std::string
51 GetFileNameRoot(const std::string &InputFilename)
53 std::string IFN = InputFilename;
54 std::string outputFilename;
55 int Len = IFN.length();
57 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
58 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
62 return outputFilename;
66 // main - Entry point for the llc compiler.
68 int main(int argc, char **argv) {
69 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
71 // Load the module to be compiled...
72 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
74 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
77 Module &mod = *M.get();
79 // Allocate target machine. First, check whether the user has
80 // explicitly specified an architecture to compile for.
81 TargetMachine* (*TargetMachineAllocator)(const Module&,
82 IntrinsicLowering *) = 0;
85 TargetMachineAllocator = allocateX86TargetMachine;
88 TargetMachineAllocator = allocateSparcTargetMachine;
91 // Decide what the default target machine should be, by looking at
92 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
93 // SPARCV9) is kind of gross, but it will work until we have more
94 // sophisticated target information to work from.
95 if (mod.getEndianness() == Module::LittleEndian &&
96 mod.getPointerSize() == Module::Pointer32) {
97 TargetMachineAllocator = allocateX86TargetMachine;
98 } else if (mod.getEndianness() == Module::BigEndian &&
99 mod.getPointerSize() == Module::Pointer64) {
100 TargetMachineAllocator = allocateSparcTargetMachine;
102 // If the module is target independent, favor a target which matches the
103 // current build system.
104 #if defined(i386) || defined(__i386__) || defined(__x86__)
105 TargetMachineAllocator = allocateX86TargetMachine;
106 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
107 TargetMachineAllocator = allocateSparcTargetMachine;
109 std::cerr << argv[0] << ": module does not specify a target to use. "
110 << "You must use the -march option.\n";
116 std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod, 0));
117 assert(target.get() && "Could not allocate target machine!");
118 TargetMachine &Target = *target.get();
119 const TargetData &TD = Target.getTargetData();
121 // Build up all of the passes that we want to do to the module...
124 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
125 TD.getPointerAlignment(), TD.getDoubleAlignment()));
127 // Figure out where we are going to send the output...
128 std::ostream *Out = 0;
129 if (OutputFilename != "") {
130 if (OutputFilename != "-") {
131 // Specified an output filename?
132 if (!Force && std::ifstream(OutputFilename.c_str())) {
133 // If force is not specified, make sure not to overwrite a file!
134 std::cerr << argv[0] << ": error opening '" << OutputFilename
135 << "': file exists!\n"
136 << "Use -f command line argument to force output\n";
139 Out = new std::ofstream(OutputFilename.c_str());
141 // Make sure that the Out file gets unlinked from the disk if we get a
143 RemoveFileOnSignal(OutputFilename);
148 if (InputFilename == "-") {
149 OutputFilename = "-";
152 OutputFilename = GetFileNameRoot(InputFilename);
153 OutputFilename += ".s";
155 if (!Force && std::ifstream(OutputFilename.c_str())) {
156 // If force is not specified, make sure not to overwrite a file!
157 std::cerr << argv[0] << ": error opening '" << OutputFilename
158 << "': file exists!\n"
159 << "Use -f command line argument to force output\n";
163 Out = new std::ofstream(OutputFilename.c_str());
165 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
170 // Make sure that the Out file gets unlinked from the disk if we get a
172 RemoveFileOnSignal(OutputFilename);
176 // Ask the target to add backend passes as necessary
177 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
178 std::cerr << argv[0] << ": target '" << Target.getName()
179 << "' does not support static compilation!\n";
180 if (Out != &std::cout) delete Out;
181 // And the Out file is empty and useless, so remove it now.
182 std::remove(OutputFilename.c_str());
185 // Run our queue of passes all at once now, efficiently.
186 Passes.run(*M.get());
189 // Delete the ostream if it's not a stdout stream
190 if (Out != &std::cout) delete Out;