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/LowerAllocations.h"
12 #include "llvm/Transforms/HoistPHIConstants.h"
13 #include "llvm/Transforms/PrintModulePass.h"
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Module.h"
16 #include "llvm/Method.h"
21 cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
22 cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
23 cl::Flag Force ("f", "Overwrite output files");
24 cl::Flag DumpAsm ("d", "Print bytecode before native code generation",
26 cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
27 cl::Flag TraceBBValues ("trace",
28 "Trace values at basic block and method exits");
29 cl::Flag TraceMethodValues("tracem", "Trace values only at method exits");
30 cl::Flag DebugTrace ("dumptrace",
31 "output trace code to a <fn>.trace.ll file",
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/
45 return outputFilename;
49 //===---------------------------------------------------------------------===//
50 // GenerateCodeForTarget Pass
52 // Native code generation for a specified target.
53 //===---------------------------------------------------------------------===//
55 class GenerateCodeForTarget : public ConcretePass<GenerateCodeForTarget> {
56 TargetMachine &Target;
58 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
60 // doPerMethodWork - This method does the actual work of generating code for
61 // the specified method.
63 bool doPerMethodWorkVirt(Method *M) {
64 if (!M->isExternal() && Target.compileMethod(M)) {
65 cerr << "Error compiling " << InputFilename << "!\n";
74 //===---------------------------------------------------------------------===//
77 // Write assembly code to specified output stream
78 //===---------------------------------------------------------------------===//
80 class EmitAssembly : public ConcretePass<EmitAssembly> {
81 const TargetMachine &Target; // Target to compile for
82 ostream *Out; // Stream to print on
83 bool DeleteStream; // Delete stream in dtor?
87 inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
88 : Target(T), Out(O), DeleteStream(D) {}
90 virtual bool doPassInitializationVirt(Module *M) {
96 // TODO: This should be performed as a moduleCleanup function, but we don't
98 Target.emitAssembly(TheMod, *Out);
100 if (DeleteStream) delete Out;
105 //===---------------------------------------------------------------------===//
108 // Entry point for the llc compiler.
109 //===---------------------------------------------------------------------===//
111 int main(int argc, char **argv) {
112 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
114 // Allocate a target... in the future this will be controllable on the
116 auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
117 assert(target.get() && "Could not allocate target machine!");
119 TargetMachine &Target = *target.get();
121 // Load the module to be compiled...
122 auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
124 cerr << "bytecode didn't read correctly.\n";
128 // Build up all of the passes that we want to do to the module...
129 vector<Pass*> Passes;
131 // Replace malloc and free instructions with library calls
132 Passes.push_back(new LowerAllocations(Target.DataLayout));
134 // Hoist constants out of PHI nodes into predecessor BB's
135 Passes.push_back(new HoistPHIConstants());
137 if (TraceBBValues || TraceMethodValues) // If tracing enabled...
138 // Insert trace code in all methods in the module
139 Passes.push_back(new InsertTraceCode(TraceBBValues,
140 TraceBBValues || TraceMethodValues));
143 if (DebugTrace) { // If Trace Debugging is enabled...
144 // Then write the module with tracing code out in assembly form
145 assert(InputFilename != "-" && "files on stdin not supported with tracing");
146 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.ll";
148 ostream *os = new ofstream(traceFileName.c_str(),
149 (Force ? 0 : ios::noreplace)|ios::out);
151 cerr << "Error opening " << traceFileName << "!\n";
156 Passes.push_back(new PrintModulePass("", os, true));
159 // If LLVM dumping after transformations is requested, add it to the pipeline
161 Passes.push_back(new PrintModulePass("Method after xformations: \n",&cerr));
163 // Generate Target code...
164 Passes.push_back(new GenerateCodeForTarget(Target));
166 if (!DoNotEmitAssembly) { // If asm output is enabled...
167 // Figure out where we are going to send the output...
169 if (OutputFilename != "") { // Specified an output filename?
170 Out = new ofstream(OutputFilename.c_str(),
171 (Force ? 0 : ios::noreplace)|ios::out);
173 if (InputFilename == "-") {
174 OutputFilename = "-";
177 string OutputFilename = GetFileNameRoot(InputFilename);
178 OutputFilename += ".s";
179 Out = new ofstream(OutputFilename.c_str(),
180 (Force ? 0 : ios::noreplace)|ios::out);
182 cerr << "Error opening " << OutputFilename << "!\n";
189 // Output assembly language to the .s file
190 Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
193 // Run our queue of passes all at once now, efficiently.
194 return Pass::runAllPassesAndFree(M.get(), Passes);