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