2001-10-04 01:40:53 +00:00
|
|
|
//===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
|
2001-09-07 22:20:50 +00:00
|
|
|
//
|
|
|
|
// This is the llc compiler driver.
|
|
|
|
//
|
2001-10-04 01:40:53 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-21 12:42:29 +00:00
|
|
|
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2001-09-14 05:34:53 +00:00
|
|
|
#include "llvm/Target/Sparc.h"
|
2001-09-18 13:10:45 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2001-10-14 23:29:28 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2002-01-22 01:04:08 +00:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2001-10-15 17:30:47 +00:00
|
|
|
#include "llvm/Transforms/HoistPHIConstants.h"
|
2001-10-18 20:32:07 +00:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-10-18 20:06:31 +00:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2001-09-07 21:26:31 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Method.h"
|
2002-01-31 00:46:45 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2001-09-18 17:04:18 +00:00
|
|
|
#include <memory>
|
2001-10-14 23:29:28 +00:00
|
|
|
#include <string>
|
2001-09-19 16:52:09 +00:00
|
|
|
#include <fstream>
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::string;
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2001-07-23 19:27:24 +00:00
|
|
|
cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
|
2001-10-15 17:41:24 +00:00
|
|
|
cl::Flag Force ("f", "Overwrite output files");
|
|
|
|
cl::Flag DumpAsm ("d", "Print bytecode before native code generation",
|
|
|
|
cl::Hidden);
|
|
|
|
cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
|
2001-10-14 23:29:28 +00:00
|
|
|
cl::Flag TraceBBValues ("trace",
|
2001-10-15 17:41:24 +00:00
|
|
|
"Trace values at basic block and method exits");
|
|
|
|
cl::Flag TraceMethodValues("tracem", "Trace values only at method exits");
|
2001-10-04 01:40:53 +00:00
|
|
|
|
|
|
|
|
2001-10-15 17:30:47 +00:00
|
|
|
// GetFileNameRoot - Helper function to get the basename of a filename...
|
|
|
|
static inline string GetFileNameRoot(const string &InputFilename) {
|
2001-10-14 23:29:28 +00:00
|
|
|
string IFN = InputFilename;
|
|
|
|
string outputFilename;
|
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
|
|
|
|
outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
|
|
|
|
} else {
|
2001-10-15 17:30:47 +00:00
|
|
|
outputFilename = IFN;
|
2001-10-14 23:29:28 +00:00
|
|
|
}
|
|
|
|
return outputFilename;
|
|
|
|
}
|
|
|
|
|
2001-09-18 17:04:18 +00:00
|
|
|
|
2001-10-14 23:29:28 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
2001-10-15 17:30:47 +00:00
|
|
|
// GenerateCodeForTarget Pass
|
2001-10-14 23:29:28 +00:00
|
|
|
//
|
|
|
|
// Native code generation for a specified target.
|
|
|
|
//===---------------------------------------------------------------------===//
|
2001-09-18 17:04:18 +00:00
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
class GenerateCodeForTarget : public MethodPass {
|
2001-10-15 17:30:47 +00:00
|
|
|
TargetMachine &Target;
|
|
|
|
public:
|
|
|
|
inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
|
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
// runOnMethod - This method does the actual work of generating code for
|
2001-10-15 17:30:47 +00:00
|
|
|
// the specified method.
|
|
|
|
//
|
2002-01-21 07:31:50 +00:00
|
|
|
bool runOnMethod(Method *M) {
|
2002-01-31 00:46:45 +00:00
|
|
|
if (Target.compileMethod(M))
|
2001-10-15 17:30:47 +00:00
|
|
|
cerr << "Error compiling " << InputFilename << "!\n";
|
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
return true;
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
|
|
|
};
|
2001-10-14 23:29:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
2002-02-03 07:54:10 +00:00
|
|
|
// EmitMethodAssembly Pass
|
2001-10-14 23:29:28 +00:00
|
|
|
//
|
2002-02-03 07:54:10 +00:00
|
|
|
// Write assembly code for each method to specified output stream
|
2001-10-14 23:29:28 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2002-02-03 07:54:10 +00:00
|
|
|
class EmitMethodAssembly : public MethodPass {
|
|
|
|
TargetMachine &Target; // Target to compile for
|
|
|
|
std::ostream *Out; // Stream to print on
|
|
|
|
public:
|
|
|
|
inline EmitMethodAssembly(TargetMachine &T, std::ostream *O)
|
|
|
|
: Target(T), Out(O) {}
|
|
|
|
|
|
|
|
virtual bool runOnMethod(Method *M) {
|
|
|
|
Target.emitAssembly(M, *Out);
|
|
|
|
Target.freeCompiledMethod(M); // Release memory for the method
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// EmitGlobalsAssembly Pass
|
|
|
|
//
|
|
|
|
// Write assembly code for global values to specified output stream
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class EmitGlobalsAssembly : public Pass {
|
2001-10-15 17:30:47 +00:00
|
|
|
const TargetMachine &Target; // Target to compile for
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream *Out; // Stream to print on
|
2001-10-15 17:30:47 +00:00
|
|
|
bool DeleteStream; // Delete stream in dtor?
|
|
|
|
public:
|
2002-02-03 07:54:10 +00:00
|
|
|
inline EmitGlobalsAssembly(const TargetMachine &T, std::ostream *O, bool D)
|
2001-10-15 17:30:47 +00:00
|
|
|
: Target(T), Out(O), DeleteStream(D) {}
|
2001-09-19 16:52:09 +00:00
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
virtual bool run(Module *M) {
|
2001-10-18 05:28:44 +00:00
|
|
|
Target.emitAssembly(M, *Out);
|
2001-10-15 17:30:47 +00:00
|
|
|
if (DeleteStream) delete Out;
|
2001-10-18 05:28:44 +00:00
|
|
|
return false;
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
|
|
|
};
|
2001-09-14 03:37:52 +00:00
|
|
|
|
2001-09-18 13:10:45 +00:00
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
|
2001-10-14 23:29:28 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// Function main()
|
|
|
|
//
|
|
|
|
// Entry point for the llc compiler.
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2001-10-15 17:41:24 +00:00
|
|
|
int main(int argc, char **argv) {
|
2001-10-14 23:29:28 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
|
|
|
|
|
|
|
|
// Allocate a target... in the future this will be controllable on the
|
|
|
|
// command line.
|
2002-01-20 22:54:45 +00:00
|
|
|
std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
|
2001-10-15 17:30:47 +00:00
|
|
|
assert(target.get() && "Could not allocate target machine!");
|
|
|
|
|
|
|
|
TargetMachine &Target = *target.get();
|
2001-10-14 23:29:28 +00:00
|
|
|
|
|
|
|
// Load the module to be compiled...
|
2002-01-20 22:54:45 +00:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
2001-10-14 23:29:28 +00:00
|
|
|
if (M.get() == 0) {
|
|
|
|
cerr << "bytecode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-15 17:30:47 +00:00
|
|
|
|
|
|
|
// Build up all of the passes that we want to do to the module...
|
2002-01-21 07:31:50 +00:00
|
|
|
PassManager Passes;
|
2001-10-15 17:30:47 +00:00
|
|
|
|
|
|
|
// Hoist constants out of PHI nodes into predecessor BB's
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.add(new HoistPHIConstants());
|
2001-10-15 17:30:47 +00:00
|
|
|
|
2001-10-18 20:06:31 +00:00
|
|
|
if (TraceBBValues || TraceMethodValues) { // If tracing enabled...
|
|
|
|
// Insert trace code in all methods in the module
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.add(new InsertTraceCode(TraceBBValues,
|
|
|
|
TraceBBValues ||TraceMethodValues));
|
2001-10-18 20:06:31 +00:00
|
|
|
|
|
|
|
// Eliminate duplication in constant pool
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.add(new DynamicConstantMerge());
|
2001-10-18 13:51:20 +00:00
|
|
|
|
2001-10-18 20:06:31 +00:00
|
|
|
// Then write out the module with tracing code before code generation
|
|
|
|
assert(InputFilename != "-" &&
|
|
|
|
"files on stdin not supported with tracing");
|
|
|
|
string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
|
2002-01-20 22:54:45 +00:00
|
|
|
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream *os = new std::ofstream(traceFileName.c_str());
|
2001-10-18 20:06:31 +00:00
|
|
|
if (!os->good()) {
|
|
|
|
cerr << "Error opening " << traceFileName
|
|
|
|
<< "! SKIPPING OUTPUT OF TRACE CODE\n";
|
|
|
|
delete os;
|
|
|
|
return 1;
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
2001-10-18 20:06:31 +00:00
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.add(new WriteBytecodePass(os, true));
|
2001-10-18 20:06:31 +00:00
|
|
|
}
|
2001-10-18 13:51:20 +00:00
|
|
|
|
2001-10-18 18:20:20 +00:00
|
|
|
// Replace malloc and free instructions with library calls.
|
|
|
|
// Do this after tracing until lli implements these lib calls.
|
|
|
|
// For now, it will emulate malloc and free internally.
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.add(new LowerAllocations(Target.DataLayout));
|
2001-10-18 18:20:20 +00:00
|
|
|
|
2001-10-15 17:30:47 +00:00
|
|
|
// If LLVM dumping after transformations is requested, add it to the pipeline
|
|
|
|
if (DumpAsm)
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.add(new PrintMethodPass("Code after xformations: \n",&cerr));
|
2001-10-15 17:30:47 +00:00
|
|
|
|
|
|
|
// Generate Target code...
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.add(new GenerateCodeForTarget(Target));
|
2001-10-15 17:30:47 +00:00
|
|
|
|
|
|
|
if (!DoNotEmitAssembly) { // If asm output is enabled...
|
|
|
|
// Figure out where we are going to send the output...
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream *Out = 0;
|
2001-10-15 17:30:47 +00:00
|
|
|
if (OutputFilename != "") { // Specified an output filename?
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2001-10-15 17:30:47 +00:00
|
|
|
} else {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2002-01-20 22:54:45 +00:00
|
|
|
Out = &std::cout;
|
2001-10-15 17:30:47 +00:00
|
|
|
} else {
|
|
|
|
string OutputFilename = GetFileNameRoot(InputFilename);
|
|
|
|
OutputFilename += ".s";
|
2002-01-20 22:54:45 +00:00
|
|
|
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2001-10-15 17:30:47 +00:00
|
|
|
if (!Out->good()) {
|
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
|
|
|
delete Out;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-03 07:54:10 +00:00
|
|
|
// Output assembly language to the .s file. Assembly emission is split into
|
|
|
|
// two parts: Method output and Global value output. This is because method
|
|
|
|
// output is pipelined with all of the rest of code generation stuff,
|
|
|
|
// allowing machine code representations for methods to be free'd after the
|
|
|
|
// method has been emitted.
|
|
|
|
//
|
|
|
|
Passes.add(new EmitMethodAssembly(Target, Out)); // for methods
|
|
|
|
|
|
|
|
// For global values...
|
|
|
|
Passes.add(new EmitGlobalsAssembly(Target, Out, Out != &std::cout));
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
2001-10-14 23:29:28 +00:00
|
|
|
|
2002-01-21 07:31:50 +00:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
Passes.run(M.get());
|
2001-10-18 01:31:22 +00:00
|
|
|
|
2001-10-18 20:33:21 +00:00
|
|
|
return 0;
|
2001-10-14 23:29:28 +00:00
|
|
|
}
|
|
|
|
|
2001-10-15 17:30:47 +00:00
|
|
|
|