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