Changes to build successfully with GCC 3.02
[oota-llvm.git] / tools / llc / llc.cpp
1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
2 //
3 // This is the llc compiler driver.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Bytecode/Reader.h"
8 #include "llvm/Target/Sparc.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Instrumentation/TraceValues.h"
11 #include "llvm/Transforms/LowerAllocations.h"
12 #include "llvm/Transforms/HoistPHIConstants.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Bytecode/WriteBytecodePass.h"
15 #include "llvm/Transforms/ConstantMerge.h"
16 #include "llvm/Module.h"
17 #include "llvm/Method.h"
18 #include "Support/CommandLine.h"
19 #include <memory>
20 #include <string>
21 #include <fstream>
22 using std::string;
23
24 cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
25 cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
26 cl::Flag   Force         ("f", "Overwrite output files");
27 cl::Flag   DumpAsm       ("d", "Print bytecode before native code generation",
28                           cl::Hidden);
29 cl::Flag   DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
30 cl::Flag   TraceBBValues ("trace",
31                           "Trace values at basic block and method exits");
32 cl::Flag   TraceMethodValues("tracem", "Trace values only at method exits");
33
34
35 // GetFileNameRoot - Helper function to get the basename of a filename...
36 static inline string GetFileNameRoot(const string &InputFilename) {
37   string IFN = InputFilename;
38   string outputFilename;
39   int Len = IFN.length();
40   if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
41     outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
42   } else {
43     outputFilename = IFN;
44   }
45   return outputFilename;
46 }
47
48
49 //===---------------------------------------------------------------------===//
50 // GenerateCodeForTarget Pass
51 // 
52 // Native code generation for a specified target.
53 //===---------------------------------------------------------------------===//
54
55 class GenerateCodeForTarget : public Pass {
56   TargetMachine &Target;
57 public:
58   inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
59
60   // doPerMethodWork - This method does the actual work of generating code for
61   // the specified method.
62   //
63   bool doPerMethodWork(Method *M) {
64     if (!M->isExternal() && Target.compileMethod(M)) {
65       cerr << "Error compiling " << InputFilename << "!\n";
66       return true;
67     }
68     
69     return false;
70   }
71 };
72
73
74 //===---------------------------------------------------------------------===//
75 // EmitAssembly Pass
76 // 
77 // Write assembly code to specified output stream
78 //===---------------------------------------------------------------------===//
79
80 class EmitAssembly : public Pass {
81   const TargetMachine &Target;   // Target to compile for
82   std::ostream *Out;             // Stream to print on
83   bool DeleteStream;             // Delete stream in dtor?
84 public:
85   inline EmitAssembly(const TargetMachine &T, std::ostream *O, bool D)
86     : Target(T), Out(O), DeleteStream(D) {}
87
88
89   virtual bool doPassFinalization(Module *M) {
90     Target.emitAssembly(M, *Out);
91
92     if (DeleteStream) delete Out;
93     return false;
94   }
95 };
96
97
98 //===---------------------------------------------------------------------===//
99 // Function main()
100 // 
101 // Entry point for the llc compiler.
102 //===---------------------------------------------------------------------===//
103
104 int main(int argc, char **argv) {
105   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
106   
107   // Allocate a target... in the future this will be controllable on the
108   // command line.
109   std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
110   assert(target.get() && "Could not allocate target machine!");
111
112   TargetMachine &Target = *target.get();
113   
114   // Load the module to be compiled...
115   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
116   if (M.get() == 0) {
117     cerr << "bytecode didn't read correctly.\n";
118     return 1;
119   }
120
121   // Build up all of the passes that we want to do to the module...
122   std::vector<Pass*> Passes;
123
124   // Hoist constants out of PHI nodes into predecessor BB's
125   Passes.push_back(new HoistPHIConstants());
126
127   if (TraceBBValues || TraceMethodValues) {   // If tracing enabled...
128     // Insert trace code in all methods in the module
129     Passes.push_back(new InsertTraceCode(TraceBBValues, 
130                                          TraceBBValues ||TraceMethodValues));
131
132     // Eliminate duplication in constant pool
133     Passes.push_back(new DynamicConstantMerge());
134       
135     // Then write out the module with tracing code before code generation 
136     assert(InputFilename != "-" &&
137            "files on stdin not supported with tracing");
138     string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
139
140     if (!Force && !std::ifstream(OutputFilename.c_str())) {
141       // If force is not specified, make sure not to overwrite a file!
142       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
143            << "Use -f command line argument to force output\n";
144       return 1;
145     }
146
147     std::ostream *os = new std::ofstream(traceFileName.c_str());
148     if (!os->good()) {
149       cerr << "Error opening " << traceFileName
150            << "! SKIPPING OUTPUT OF TRACE CODE\n";
151       delete os;
152       return 1;
153     }
154     
155     Passes.push_back(new WriteBytecodePass(os, true));
156   }
157   
158   // Replace malloc and free instructions with library calls.
159   // Do this after tracing until lli implements these lib calls.
160   // For now, it will emulate malloc and free internally.
161   Passes.push_back(new LowerAllocations(Target.DataLayout));
162   
163   // If LLVM dumping after transformations is requested, add it to the pipeline
164   if (DumpAsm)
165     Passes.push_back(new PrintModulePass("Code after xformations: \n",&cerr));
166
167   // Generate Target code...
168   Passes.push_back(new GenerateCodeForTarget(Target));
169
170   if (!DoNotEmitAssembly) {                // If asm output is enabled...
171     // Figure out where we are going to send the output...
172     std::ostream *Out = 0;
173     if (OutputFilename != "") {   // Specified an output filename?
174       if (!Force && !std::ifstream(OutputFilename.c_str())) {
175         // If force is not specified, make sure not to overwrite a file!
176         cerr << "Error opening '" << OutputFilename << "': File exists!\n"
177              << "Use -f command line argument to force output\n";
178         return 1;
179       }
180       Out = new std::ofstream(OutputFilename.c_str());
181     } else {
182       if (InputFilename == "-") {
183         OutputFilename = "-";
184         Out = &std::cout;
185       } else {
186         string OutputFilename = GetFileNameRoot(InputFilename); 
187         OutputFilename += ".s";
188
189         if (!Force && !std::ifstream(OutputFilename.c_str())) {
190           // If force is not specified, make sure not to overwrite a file!
191           cerr << "Error opening '" << OutputFilename << "': File exists!\n"
192                << "Use -f command line argument to force output\n";
193           return 1;
194         }
195
196         Out = new std::ofstream(OutputFilename.c_str());
197         if (!Out->good()) {
198           cerr << "Error opening " << OutputFilename << "!\n";
199           delete Out;
200           return 1;
201         }
202       }
203     }
204     
205     // Output assembly language to the .s file
206     Passes.push_back(new EmitAssembly(Target, Out, Out != &std::cout));
207   }
208   
209   // Run our queue of passes all at once now, efficiently.  This form of
210   // runAllPasses frees the Pass objects after runAllPasses completes.
211   Pass::runAllPassesAndFree(M.get(), Passes);
212
213   return 0;
214 }
215
216