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