2002-01-22 11:30:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 12:28:11 +08: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 11:30:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-31 12:28:11 +08:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 08:46:22 +08:00
|
|
|
#include "llvm/PassManager.h"
|
2001-10-31 12:28:11 +08:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 10:41:09 +08:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2001-10-31 14:36:48 +08:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2002-01-22 09:04:08 +08:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-01-22 07:17:48 +08:00
|
|
|
#include "llvm/Transforms/Scalar/DCE.h"
|
2001-12-05 14:34:58 +08:00
|
|
|
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
|
2001-12-15 00:48:30 +08:00
|
|
|
#include "llvm/Transforms/Scalar/InstructionCombining.h"
|
2002-03-28 07:29:23 +08:00
|
|
|
#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
|
2002-01-22 11:30:46 +08:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2001-10-31 12:28:11 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
|
|
|
|
cl::Required, "");
|
|
|
|
cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
|
2002-03-22 05:21:50 +08:00
|
|
|
cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
|
|
|
|
cl::Hidden);
|
2001-10-31 12:28:11 +08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2001-10-31 12:33:33 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
|
2001-10-31 12:28:11 +08: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-21 06:54:45 +08:00
|
|
|
std::string IFN = InputFilename;
|
2001-10-31 12:28:11 +08:00
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
|
2002-01-21 06:54:45 +08:00
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-2);
|
2001-10-31 12:28:11 +08:00
|
|
|
} else {
|
|
|
|
OutputFilename = IFN; // Append a .o to it
|
|
|
|
}
|
|
|
|
OutputFilename += ".o";
|
|
|
|
}
|
|
|
|
|
2002-01-22 11:30:46 +08:00
|
|
|
std::ofstream Out(OutputFilename.c_str(), ios::out);
|
|
|
|
if (!Out.good()) {
|
2001-10-31 12:28:11 +08:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 15:31:50 +08:00
|
|
|
PassManager Passes;
|
2002-02-27 05:47:29 +08:00
|
|
|
Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
|
|
|
|
Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
|
2002-03-28 07:29:23 +08:00
|
|
|
Passes.add(createPromoteMemoryToRegister()); // memory->register promotion
|
2002-02-27 05:47:29 +08:00
|
|
|
Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
|
|
|
|
Passes.add(createIndVarSimplifyPass()); // Simplify indvars
|
2002-03-22 05:21:50 +08:00
|
|
|
if (!StopAtLevelRaise) {
|
|
|
|
Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
|
|
|
|
Passes.add(createConstantMergePass()); // Merge dup global consts
|
|
|
|
Passes.add(createInstructionCombiningPass()); // Combine silly seq's
|
|
|
|
Passes.add(createDeadCodeEliminationPass()); // Remove Dead code/vars
|
|
|
|
}
|
2002-02-27 05:47:29 +08:00
|
|
|
Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
|
2001-10-31 12:28:11 +08:00
|
|
|
|
2002-01-22 11:30:46 +08:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
2002-01-21 15:31:50 +08:00
|
|
|
Passes.run(M.get());
|
2001-10-31 12:28:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|