Targets now configure themselves with the module, not flags
[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 (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
48     outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
49   } else {
50     outputFilename = IFN;
51   }
52   return outputFilename;
53 }
54
55
56 // main - Entry point for the llc compiler.
57 //
58 int main(int argc, char **argv) {
59   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
60   
61   // Load the module to be compiled...
62   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
63   if (M.get() == 0) {
64     std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
65     return 1;
66   }
67   Module &mod = *M.get();
68
69   // Allocate target machine.  First, check whether the user has
70   // explicitly specified an architecture to compile for.
71   TargetMachine* (*TargetMachineAllocator)(const Module&) = 0;
72   switch (Arch) {
73   case x86:
74     TargetMachineAllocator = allocateX86TargetMachine;
75     break;
76   case Sparc:
77     TargetMachineAllocator = allocateSparcTargetMachine;
78     break;
79   default:
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;
90     } else {
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;
97 #else
98       std::cerr << argv[0] << ": module does not specify a target to use.  "
99                 << "You must use the -march option.\n";
100       return 1;
101 #endif
102     } 
103     break;
104   }
105   std::auto_ptr<TargetMachine> target(TargetMachineAllocator(mod));
106   assert(target.get() && "Could not allocate target machine!");
107   TargetMachine &Target = *target.get();
108   const TargetData &TD = Target.getTargetData();
109
110   // Build up all of the passes that we want to do to the module...
111   PassManager Passes;
112
113   Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
114                             TD.getPointerAlignment(), TD.getDoubleAlignment()));
115
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";
126         return 1;
127       }
128       Out = new std::ofstream(OutputFilename.c_str());
129
130       // Make sure that the Out file gets unlink'd from the disk if we get a
131       // SIGINT
132       RemoveFileOnSignal(OutputFilename);
133     } else {
134       Out = &std::cout;
135     }
136   } else {
137     if (InputFilename == "-") {
138       OutputFilename = "-";
139       Out = &std::cout;
140     } else {
141       OutputFilename = GetFileNameRoot(InputFilename); 
142       OutputFilename += ".s";
143       
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";
149         return 1;
150       }
151       
152       Out = new std::ofstream(OutputFilename.c_str());
153       if (!Out->good()) {
154         std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
155         delete Out;
156         return 1;
157       }
158       
159       // Make sure that the Out file gets unlink'd from the disk if we get a
160       // SIGINT
161       RemoveFileOnSignal(OutputFilename);
162     }
163   }
164
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());
172     return 1;
173   } else {
174     // Run our queue of passes all at once now, efficiently.
175     Passes.run(*M.get());
176   }
177
178   // Delete the ostream if it's not a stdout stream
179   if (Out != &std::cout) delete Out;
180
181   return 0;
182 }