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