2002-01-22 03:30:46 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 04:28:11 +00:00
|
|
|
// LLVM 'GCCAS' UTILITY
|
|
|
|
//
|
|
|
|
// This utility is designed to be used by the GCC frontend for creating
|
|
|
|
// bytecode files from it's intermediate llvm assembly. The requirements for
|
|
|
|
// this utility are thus slightly different than that of the standard as util.
|
|
|
|
//
|
2002-01-22 03:30:46 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 04:28:11 +00:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 00:46:22 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2001-10-31 04:28:11 +00:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 02:41:09 +00:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2001-10-31 06:36:48 +00:00
|
|
|
#include "llvm/Transforms/ConstantMerge.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"
|
2002-01-22 03:30:46 +00:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.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-10-31 04:28:11 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
|
|
|
|
cl::Required, "");
|
|
|
|
cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
|
2002-03-21 21:21:50 +00:00
|
|
|
cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
|
|
|
|
cl::Hidden);
|
2001-10-31 04:28:11 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2001-10-31 04:33:33 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
|
2001-10-31 04:28:11 +00:00
|
|
|
|
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
try {
|
|
|
|
// Parse the file now...
|
|
|
|
M.reset(ParseAssemblyFile(InputFilename));
|
|
|
|
} catch (const ParseException &E) {
|
|
|
|
cerr << E.getMessage() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (M.get() == 0) {
|
|
|
|
cerr << "assembly didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OutputFilename == "") { // Didn't specify an output filename?
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string IFN = InputFilename;
|
2001-10-31 04:28:11 +00:00
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
|
2002-01-20 22:54:45 +00:00
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-2);
|
2001-10-31 04:28:11 +00:00
|
|
|
} else {
|
|
|
|
OutputFilename = IFN; // Append a .o to it
|
|
|
|
}
|
|
|
|
OutputFilename += ".o";
|
|
|
|
}
|
|
|
|
|
2002-01-22 03:30:46 +00:00
|
|
|
std::ofstream Out(OutputFilename.c_str(), ios::out);
|
|
|
|
if (!Out.good()) {
|
2001-10-31 04:28:11 +00:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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-31 04:28:11 +00:00
|
|
|
// In addition to just parsing the input from GCC, we also want to spiff it up
|
|
|
|
// a little bit. Do this now.
|
|
|
|
//
|
2002-01-21 07:31:50 +00:00
|
|
|
PassManager Passes;
|
2002-04-10 20:33:32 +00:00
|
|
|
Passes.add(createFunctionResolvingPass()); // Resolve (...) functions
|
2002-04-29 20:11:38 +00:00
|
|
|
Passes.add(createConstantMergePass()); // Merge dup global constants
|
2002-02-26 21:47:29 +00:00
|
|
|
Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
|
|
|
|
Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
|
|
|
|
Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
|
|
|
|
Passes.add(createIndVarSimplifyPass()); // Simplify indvars
|
2002-03-21 21:21:50 +00:00
|
|
|
if (!StopAtLevelRaise) {
|
|
|
|
Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
|
2002-04-01 19:45:11 +00:00
|
|
|
Passes.add(createPromoteMemoryToRegister()); // Promote alloca's to regs
|
2002-05-08 22:18:20 +00:00
|
|
|
Passes.add(createReassociatePass()); // Reassociate expressions
|
2002-03-21 21:21:50 +00:00
|
|
|
Passes.add(createInstructionCombiningPass()); // Combine silly seq's
|
2002-05-06 16:52:15 +00:00
|
|
|
Passes.add(createDeadInstEliminationPass()); // Kill InstCombine remnants
|
2002-05-10 22:44:31 +00:00
|
|
|
Passes.add(createLICMPass()); // Hoist loop invariants
|
2002-04-28 01:00:15 +00:00
|
|
|
Passes.add(createGCSEPass()); // Remove common subexprs
|
2002-05-06 18:54:12 +00:00
|
|
|
Passes.add(createSCCPPass()); // Constant prop with SCCP
|
2002-05-14 16:23:14 +00:00
|
|
|
|
|
|
|
// Run instcombine after redundancy elimination to exploit opportunities
|
|
|
|
// opened up by them.
|
|
|
|
Passes.add(createInstructionCombiningPass());
|
2002-05-06 03:04:17 +00:00
|
|
|
Passes.add(createDeadCodeEliminationPass()); // Remove Dead code/vars
|
2002-03-21 21:21:50 +00:00
|
|
|
}
|
2002-02-26 21:47:29 +00:00
|
|
|
Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
|
2001-10-31 04:28:11 +00:00
|
|
|
|
2002-01-22 03:30:46 +00:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
2002-01-21 07:31:50 +00:00
|
|
|
Passes.run(M.get());
|
2001-10-31 04:28:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|