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