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 //===---------------------------------------------------------------------===//
74 // EmitMethodAssembly Pass
76 // Write assembly code for each method to specified output stream
77 //===---------------------------------------------------------------------===//
79 class EmitMethodAssembly : public MethodPass {
80 TargetMachine &Target; // Target to compile for
81 std::ostream *Out; // Stream to print on
83 inline EmitMethodAssembly(TargetMachine &T, std::ostream *O)
84 : Target(T), Out(O) {}
86 virtual bool runOnMethod(Method *M) {
87 Target.emitAssembly(M, *Out);
88 Target.freeCompiledMethod(M); // Release memory for the method
94 //===---------------------------------------------------------------------===//
95 // EmitGlobalsAssembly Pass
97 // Write assembly code for global values to specified output stream
98 //===---------------------------------------------------------------------===//
100 class EmitGlobalsAssembly : public Pass {
101 const TargetMachine &Target; // Target to compile for
102 std::ostream *Out; // Stream to print on
103 bool DeleteStream; // Delete stream in dtor?
105 inline EmitGlobalsAssembly(const TargetMachine &T, std::ostream *O, bool D)
106 : Target(T), Out(O), DeleteStream(D) {}
108 virtual bool run(Module *M) {
109 Target.emitAssembly(M, *Out);
110 if (DeleteStream) delete Out;
117 //===---------------------------------------------------------------------===//
120 // Entry point for the llc compiler.
121 //===---------------------------------------------------------------------===//
123 int main(int argc, char **argv) {
124 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
126 // Allocate a target... in the future this will be controllable on the
128 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
129 assert(target.get() && "Could not allocate target machine!");
131 TargetMachine &Target = *target.get();
133 // Load the module to be compiled...
134 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
136 cerr << "bytecode didn't read correctly.\n";
140 // Build up all of the passes that we want to do to the module...
143 // Hoist constants out of PHI nodes into predecessor BB's
144 Passes.add(new HoistPHIConstants());
146 if (TraceBBValues || TraceMethodValues) { // If tracing enabled...
147 // Insert trace code in all methods in the module
148 Passes.add(new InsertTraceCode(TraceBBValues,
149 TraceBBValues ||TraceMethodValues));
151 // Eliminate duplication in constant pool
152 Passes.add(new DynamicConstantMerge());
154 // Then write out the module with tracing code before code generation
155 assert(InputFilename != "-" &&
156 "files on stdin not supported with tracing");
157 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
159 if (!Force && std::ifstream(OutputFilename.c_str())) {
160 // If force is not specified, make sure not to overwrite a file!
161 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
162 << "Use -f command line argument to force output\n";
166 std::ostream *os = new std::ofstream(traceFileName.c_str());
168 cerr << "Error opening " << traceFileName
169 << "! SKIPPING OUTPUT OF TRACE CODE\n";
174 Passes.add(new WriteBytecodePass(os, true));
177 // Replace malloc and free instructions with library calls.
178 // Do this after tracing until lli implements these lib calls.
179 // For now, it will emulate malloc and free internally.
180 Passes.add(new LowerAllocations(Target.DataLayout));
182 // If LLVM dumping after transformations is requested, add it to the pipeline
184 Passes.add(new PrintMethodPass("Code after xformations: \n",&cerr));
186 // Generate Target code...
187 Passes.add(new GenerateCodeForTarget(Target));
189 if (!DoNotEmitAssembly) { // If asm output is enabled...
190 // Figure out where we are going to send the output...
191 std::ostream *Out = 0;
192 if (OutputFilename != "") { // Specified an output filename?
193 if (!Force && std::ifstream(OutputFilename.c_str())) {
194 // If force is not specified, make sure not to overwrite a file!
195 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
196 << "Use -f command line argument to force output\n";
199 Out = new std::ofstream(OutputFilename.c_str());
201 if (InputFilename == "-") {
202 OutputFilename = "-";
205 string OutputFilename = GetFileNameRoot(InputFilename);
206 OutputFilename += ".s";
208 if (!Force && std::ifstream(OutputFilename.c_str())) {
209 // If force is not specified, make sure not to overwrite a file!
210 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
211 << "Use -f command line argument to force output\n";
215 Out = new std::ofstream(OutputFilename.c_str());
217 cerr << "Error opening " << OutputFilename << "!\n";
224 // Output assembly language to the .s file. Assembly emission is split into
225 // two parts: Method output and Global value output. This is because method
226 // output is pipelined with all of the rest of code generation stuff,
227 // allowing machine code representations for methods to be free'd after the
228 // method has been emitted.
230 Passes.add(new EmitMethodAssembly(Target, Out)); // for methods
232 // For global values...
233 Passes.add(new EmitGlobalsAssembly(Target, Out, Out != &std::cout));
236 // Run our queue of passes all at once now, efficiently.