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"
|
2001-10-15 17:30:47 +00:00
|
|
|
#include "llvm/Transforms/LowerAllocations.h"
|
|
|
|
#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"
|
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
|
|
|
|
2001-10-18 05:28:44 +00:00
|
|
|
class GenerateCodeForTarget : public Pass {
|
2001-10-15 17:30:47 +00:00
|
|
|
TargetMachine &Target;
|
|
|
|
public:
|
|
|
|
inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
|
|
|
|
|
|
|
|
// doPerMethodWork - This method does the actual work of generating code for
|
|
|
|
// the specified method.
|
|
|
|
//
|
2001-10-18 05:28:44 +00:00
|
|
|
bool doPerMethodWork(Method *M) {
|
2001-10-15 17:30:47 +00:00
|
|
|
if (!M->isExternal() && Target.compileMethod(M)) {
|
|
|
|
cerr << "Error compiling " << InputFilename << "!\n";
|
|
|
|
return true;
|
2001-09-14 05:34:53 +00:00
|
|
|
}
|
2001-10-15 17:30:47 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2001-10-14 23:29:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
2001-10-15 17:30:47 +00:00
|
|
|
// EmitAssembly Pass
|
2001-10-14 23:29:28 +00:00
|
|
|
//
|
2001-10-15 17:30:47 +00:00
|
|
|
// Write assembly code to specified output stream
|
2001-10-14 23:29:28 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2001-10-18 05:28:44 +00:00
|
|
|
class EmitAssembly : 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-01-20 22:54:45 +00:00
|
|
|
inline EmitAssembly(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
|
|
|
|
2001-10-14 23:29:28 +00:00
|
|
|
|
2001-10-18 05:28:44 +00:00
|
|
|
virtual bool doPassFinalization(Module *M) {
|
|
|
|
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
|
|
|
|
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-20 22:54:45 +00:00
|
|
|
std::vector<Pass*> Passes;
|
2001-10-15 17:30:47 +00:00
|
|
|
|
|
|
|
// Hoist constants out of PHI nodes into predecessor BB's
|
|
|
|
Passes.push_back(new HoistPHIConstants());
|
|
|
|
|
2001-10-18 20:06:31 +00:00
|
|
|
if (TraceBBValues || TraceMethodValues) { // If tracing enabled...
|
|
|
|
// Insert trace code in all methods in the module
|
|
|
|
Passes.push_back(new InsertTraceCode(TraceBBValues,
|
|
|
|
TraceBBValues ||TraceMethodValues));
|
|
|
|
|
|
|
|
// Eliminate duplication in constant pool
|
|
|
|
Passes.push_back(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
|
|
|
|
|
|
|
if (!Force && !std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// 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
|
|
|
|
2001-10-18 20:32:07 +00:00
|
|
|
Passes.push_back(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.
|
|
|
|
Passes.push_back(new LowerAllocations(Target.DataLayout));
|
|
|
|
|
2001-10-15 17:30:47 +00:00
|
|
|
// If LLVM dumping after transformations is requested, add it to the pipeline
|
|
|
|
if (DumpAsm)
|
2001-10-18 13:51:20 +00:00
|
|
|
Passes.push_back(new PrintModulePass("Code after xformations: \n",&cerr));
|
2001-10-15 17:30:47 +00:00
|
|
|
|
|
|
|
// Generate Target code...
|
|
|
|
Passes.push_back(new GenerateCodeForTarget(Target));
|
|
|
|
|
|
|
|
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-20 22:54:45 +00:00
|
|
|
if (!Force && !std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// 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
|
|
|
|
|
|
|
if (!Force && !std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output assembly language to the .s file
|
2002-01-20 22:54:45 +00:00
|
|
|
Passes.push_back(new EmitAssembly(Target, Out, Out != &std::cout));
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
2001-10-14 23:29:28 +00:00
|
|
|
|
2001-10-18 01:31:22 +00:00
|
|
|
// Run our queue of passes all at once now, efficiently. This form of
|
|
|
|
// runAllPasses frees the Pass objects after runAllPasses completes.
|
|
|
|
Pass::runAllPassesAndFree(M.get(), Passes);
|
|
|
|
|
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
|
|
|
|