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"
|
2002-05-07 20:03:27 +00:00
|
|
|
#include "llvm/Transforms/Scalar.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"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.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"
|
2002-04-18 19:55:25 +00:00
|
|
|
#include "Support/Signals.h"
|
2001-09-18 17:04:18 +00:00
|
|
|
#include <memory>
|
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
|
|
|
|
2002-02-26 21:36:53 +00:00
|
|
|
static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
|
|
|
|
static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
|
|
|
|
static cl::Flag Force ("f", "Overwrite output files");
|
|
|
|
static cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden);
|
|
|
|
|
|
|
|
enum TraceLevel {
|
2002-04-07 20:49:59 +00:00
|
|
|
TraceOff, TraceFunctions, TraceBasicBlocks
|
2002-02-26 21:36:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
|
2002-04-07 20:49:59 +00:00
|
|
|
"Trace values through functions or basic blocks",
|
2002-02-26 21:36:53 +00:00
|
|
|
clEnumValN(TraceOff , "off", "Disable trace code"),
|
2002-04-07 20:49:59 +00:00
|
|
|
clEnumValN(TraceFunctions , "function", "Trace each function"),
|
2002-02-26 21:36:53 +00:00
|
|
|
clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
|
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
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// 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
|
|
|
|
2002-02-26 21:36:53 +00:00
|
|
|
if (TraceValues != TraceOff) { // If tracing enabled...
|
2002-04-07 20:49:59 +00:00
|
|
|
// Insert trace code in all functions in the module
|
2002-02-26 21:36:53 +00:00
|
|
|
if (TraceValues == TraceBasicBlocks)
|
|
|
|
Passes.add(createTraceValuesPassForBasicBlocks());
|
2002-04-07 20:49:59 +00:00
|
|
|
else if (TraceValues == TraceFunctions)
|
2002-04-27 06:56:12 +00:00
|
|
|
Passes.add(createTraceValuesPassForFunction());
|
2002-02-26 21:36:53 +00:00
|
|
|
else
|
|
|
|
assert(0 && "Bad value for TraceValues!");
|
2001-10-18 20:06:31 +00:00
|
|
|
|
|
|
|
// Eliminate duplication in constant pool
|
2002-02-26 21:36:53 +00:00
|
|
|
Passes.add(createDynamicConstantMergePass());
|
2002-03-24 03:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decompose multi-dimensional refs into a sequence of 1D refs
|
|
|
|
Passes.add(createDecomposeMultiDimRefsPass());
|
|
|
|
|
|
|
|
// Write out the module with tracing code just before code generation
|
|
|
|
if (TraceValues != TraceOff) { // If tracing enabled...
|
2001-10-18 20:06:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2002-03-24 03:19:54 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
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-02-26 21:36:53 +00:00
|
|
|
Passes.add(createLowerAllocationsPass(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-04-08 22:05:01 +00:00
|
|
|
Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
|
2001-10-15 17:30:47 +00:00
|
|
|
|
2002-02-03 23:43:19 +00:00
|
|
|
// Figure out where we are going to send the output...
|
|
|
|
std::ostream *Out = 0;
|
|
|
|
if (OutputFilename != "") { // Specified an output filename?
|
|
|
|
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());
|
2002-04-18 19:55:25 +00:00
|
|
|
|
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2002-02-03 23:43:19 +00:00
|
|
|
} else {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
|
|
|
Out = &std::cout;
|
|
|
|
} else {
|
|
|
|
string OutputFilename = GetFileNameRoot(InputFilename);
|
|
|
|
OutputFilename += ".s";
|
2001-10-15 17:30:47 +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;
|
|
|
|
}
|
2002-02-03 23:43:19 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2002-02-03 23:43:19 +00:00
|
|
|
if (!Out->good()) {
|
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
|
|
|
delete Out;
|
|
|
|
return 1;
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-10-15 17:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-14 23:29:28 +00:00
|
|
|
|
2002-02-03 23:43:19 +00:00
|
|
|
Target.addPassesToEmitAssembly(Passes, *Out);
|
|
|
|
|
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
|
|
|
|
2002-02-03 23:43:19 +00:00
|
|
|
if (Out != &std::cout) delete Out;
|
|
|
|
|
2001-10-18 20:33:21 +00:00
|
|
|
return 0;
|
2001-10-14 23:29:28 +00:00
|
|
|
}
|