1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
3 // This is the llc code generator.
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"
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.
23 static cl::opt<std::string>
24 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
26 static cl::opt<std::string>
27 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
29 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
31 enum ArchName { noarch, x86, Sparc };
33 static cl::opt<ArchName>
34 Arch("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"),
40 // GetFileNameRoot - Helper function to get the basename of a filename...
41 static inline std::string
42 GetFileNameRoot(const std::string &InputFilename)
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/
52 return outputFilename;
56 // main - Entry point for the llc compiler.
58 int main(int argc, char **argv) {
59 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
61 // Load the module to be compiled...
62 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
64 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
67 Module &mod = *M.get();
69 // Allocate target machine. First, check whether the user has
70 // explicitly specified an architecture to compile for.
71 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
74 TargetMachineAllocator = allocateX86TargetMachine;
77 TargetMachineAllocator = allocateSparcTargetMachine;
80 // Decide what the default target machine should be, by looking at
81 // the module. This heuristic (ILP32, LE -> IA32; LP64, BE ->
82 // SPARCV9) is kind of gross, but it will work until we have more
83 // sophisticated target information to work from.
84 if (mod.getEndianness() == Module::LittleEndian &&
85 mod.getPointerSize() == Module::Pointer32) {
86 TargetMachineAllocator = allocateX86TargetMachine;
87 } else if (mod.getEndianness() == Module::BigEndian &&
88 mod.getPointerSize() == Module::Pointer64) {
89 TargetMachineAllocator = allocateSparcTargetMachine;
91 // If the module is target independent, favor a target which matches the
92 // current build system.
93 #if defined(i386) || defined(__i386__) || defined(__x86__)
94 TargetMachineAllocator = allocateX86TargetMachine;
95 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
96 TargetMachineAllocator = allocateSparcTargetMachine;
98 std::cerr << argv[0] << ": module does not specify a target to use. "
99 << "You must use the -march option.\n";
105 std::auto_ptr<TargetMachine> target((*TargetMachineAllocator)(0));
106 assert(target.get() && "Could not allocate target machine!");
107 TargetMachine &Target = *target.get();
108 const TargetData &TD = Target.getTargetData();
110 // Build up all of the passes that we want to do to the module...
113 Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
114 TD.getPointerAlignment(), TD.getDoubleAlignment()));
116 // Figure out where we are going to send the output...
117 std::ostream *Out = 0;
118 if (OutputFilename != "") {
119 if (OutputFilename != "-") {
120 // Specified an output filename?
121 if (!Force && std::ifstream(OutputFilename.c_str())) {
122 // If force is not specified, make sure not to overwrite a file!
123 std::cerr << argv[0] << ": error opening '" << OutputFilename
124 << "': file exists!\n"
125 << "Use -f command line argument to force output\n";
128 Out = new std::ofstream(OutputFilename.c_str());
130 // Make sure that the Out file gets unlink'd from the disk if we get a
132 RemoveFileOnSignal(OutputFilename);
137 if (InputFilename == "-") {
138 OutputFilename = "-";
141 OutputFilename = GetFileNameRoot(InputFilename);
142 OutputFilename += ".s";
144 if (!Force && std::ifstream(OutputFilename.c_str())) {
145 // If force is not specified, make sure not to overwrite a file!
146 std::cerr << argv[0] << ": error opening '" << OutputFilename
147 << "': file exists!\n"
148 << "Use -f command line argument to force output\n";
152 Out = new std::ofstream(OutputFilename.c_str());
154 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
159 // Make sure that the Out file gets unlink'd from the disk if we get a
161 RemoveFileOnSignal(OutputFilename);
165 // Ask the target to add backend passes as necessary
166 if (Target.addPassesToEmitAssembly(Passes, *Out)) {
167 std::cerr << argv[0] << ": target '" << Target.getName()
168 << "' does not support static compilation!\n";
169 if (Out != &std::cout) delete Out;
170 // And the Out file is empty and useless, so remove it now.
171 std::remove(OutputFilename.c_str());
174 // Run our queue of passes all at once now, efficiently.
175 Passes.run(*M.get());
178 // Delete the ostream if it's not a stdout stream
179 if (Out != &std::cout) delete Out;