1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
3 // This is the llc compiler driver.
5 //===----------------------------------------------------------------------===//
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/ChangeAllocations.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 "llvm/PassManager.h"
19 #include "Support/CommandLine.h"
25 cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
26 cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
27 cl::Flag Force ("f", "Overwrite output files");
28 cl::Flag DumpAsm ("d", "Print bytecode before native code generation",
30 cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
31 cl::Flag TraceBBValues ("trace",
32 "Trace values at basic block and method exits");
33 cl::Flag TraceMethodValues("tracem", "Trace values only at method exits");
36 // GetFileNameRoot - Helper function to get the basename of a filename...
37 static inline string GetFileNameRoot(const string &InputFilename) {
38 string IFN = InputFilename;
39 string outputFilename;
40 int Len = IFN.length();
41 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
42 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
46 return outputFilename;
50 //===---------------------------------------------------------------------===//
51 // GenerateCodeForTarget Pass
53 // Native code generation for a specified target.
54 //===---------------------------------------------------------------------===//
56 class GenerateCodeForTarget : public MethodPass {
57 TargetMachine &Target;
59 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
61 // runOnMethod - This method does the actual work of generating code for
62 // the specified method.
64 bool runOnMethod(Method *M) {
65 if (Target.compileMethod(M))
66 cerr << "Error compiling " << InputFilename << "!\n";
73 //===---------------------------------------------------------------------===//
76 // Write assembly code to specified output stream
77 //===---------------------------------------------------------------------===//
79 class EmitAssembly : public Pass {
80 const TargetMachine &Target; // Target to compile for
81 std::ostream *Out; // Stream to print on
82 bool DeleteStream; // Delete stream in dtor?
84 inline EmitAssembly(const TargetMachine &T, std::ostream *O, bool D)
85 : Target(T), Out(O), DeleteStream(D) {}
87 virtual bool run(Module *M) {
88 Target.emitAssembly(M, *Out);
90 if (DeleteStream) delete Out;
97 //===---------------------------------------------------------------------===//
100 // Entry point for the llc compiler.
101 //===---------------------------------------------------------------------===//
103 int main(int argc, char **argv) {
104 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
106 // Allocate a target... in the future this will be controllable on the
108 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
109 assert(target.get() && "Could not allocate target machine!");
111 TargetMachine &Target = *target.get();
113 // Load the module to be compiled...
114 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
116 cerr << "bytecode didn't read correctly.\n";
120 // Build up all of the passes that we want to do to the module...
123 // Hoist constants out of PHI nodes into predecessor BB's
124 Passes.add(new HoistPHIConstants());
126 if (TraceBBValues || TraceMethodValues) { // If tracing enabled...
127 // Insert trace code in all methods in the module
128 Passes.add(new InsertTraceCode(TraceBBValues,
129 TraceBBValues ||TraceMethodValues));
131 // Eliminate duplication in constant pool
132 Passes.add(new DynamicConstantMerge());
134 // Then write out the module with tracing code before code generation
135 assert(InputFilename != "-" &&
136 "files on stdin not supported with tracing");
137 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
139 if (!Force && std::ifstream(OutputFilename.c_str())) {
140 // If force is not specified, make sure not to overwrite a file!
141 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
142 << "Use -f command line argument to force output\n";
146 std::ostream *os = new std::ofstream(traceFileName.c_str());
148 cerr << "Error opening " << traceFileName
149 << "! SKIPPING OUTPUT OF TRACE CODE\n";
154 Passes.add(new WriteBytecodePass(os, true));
157 // Replace malloc and free instructions with library calls.
158 // Do this after tracing until lli implements these lib calls.
159 // For now, it will emulate malloc and free internally.
160 Passes.add(new LowerAllocations(Target.DataLayout));
162 // If LLVM dumping after transformations is requested, add it to the pipeline
164 Passes.add(new PrintMethodPass("Code after xformations: \n",&cerr));
166 // Generate Target code...
167 Passes.add(new GenerateCodeForTarget(Target));
169 if (!DoNotEmitAssembly) { // If asm output is enabled...
170 // Figure out where we are going to send the output...
171 std::ostream *Out = 0;
172 if (OutputFilename != "") { // Specified an output filename?
173 if (!Force && std::ifstream(OutputFilename.c_str())) {
174 // If force is not specified, make sure not to overwrite a file!
175 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
176 << "Use -f command line argument to force output\n";
179 Out = new std::ofstream(OutputFilename.c_str());
181 if (InputFilename == "-") {
182 OutputFilename = "-";
185 string OutputFilename = GetFileNameRoot(InputFilename);
186 OutputFilename += ".s";
188 if (!Force && std::ifstream(OutputFilename.c_str())) {
189 // If force is not specified, make sure not to overwrite a file!
190 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
191 << "Use -f command line argument to force output\n";
195 Out = new std::ofstream(OutputFilename.c_str());
197 cerr << "Error opening " << OutputFilename << "!\n";
204 // Output assembly language to the .s file
205 Passes.add(new EmitAssembly(Target, Out, Out != &std::cout));
208 // Run our queue of passes all at once now, efficiently.