1 //===----------------------------------------------------------------------===//
2 // LLVM 'GCCAS' UTILITY
4 // This utility is designed to be used by the GCC frontend for creating
5 // bytecode files from it's intermediate llvm assembly. The requirements for
6 // this utility are thus slightly different than that of the standard as util.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Module.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/Assembly/Parser.h"
13 #include "llvm/Transforms/CleanupGCCOutput.h"
14 #include "llvm/Transforms/LevelChange.h"
15 #include "llvm/Transforms/ConstantMerge.h"
16 #include "llvm/Transforms/ChangeAllocations.h"
17 #include "llvm/Transforms/Scalar/DCE.h"
18 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
19 #include "llvm/Transforms/Scalar/InstructionCombining.h"
20 #include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
21 #include "llvm/Bytecode/WriteBytecodePass.h"
22 #include "Support/CommandLine.h"
27 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
29 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
30 cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
33 int main(int argc, char **argv) {
34 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
36 std::auto_ptr<Module> M;
38 // Parse the file now...
39 M.reset(ParseAssemblyFile(InputFilename));
40 } catch (const ParseException &E) {
41 cerr << E.getMessage() << endl;
46 cerr << "assembly didn't read correctly.\n";
50 if (OutputFilename == "") { // Didn't specify an output filename?
51 std::string IFN = InputFilename;
52 int Len = IFN.length();
53 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
54 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
56 OutputFilename = IFN; // Append a .o to it
58 OutputFilename += ".o";
61 std::ofstream Out(OutputFilename.c_str(), ios::out);
63 cerr << "Error opening " << OutputFilename << "!\n";
67 // In addition to just parsing the input from GCC, we also want to spiff it up
68 // a little bit. Do this now.
71 Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
72 Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
73 Passes.add(createPromoteMemoryToRegister()); // memory->register promotion
74 Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
75 Passes.add(createIndVarSimplifyPass()); // Simplify indvars
76 if (!StopAtLevelRaise) {
77 Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
78 Passes.add(createConstantMergePass()); // Merge dup global consts
79 Passes.add(createInstructionCombiningPass()); // Combine silly seq's
80 Passes.add(createDeadCodeEliminationPass()); // Remove Dead code/vars
82 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
84 // Run our queue of passes all at once now, efficiently.