7e2b6f3e46c98b88bdd83067b6e00fe8fdf56916
[oota-llvm.git] / tools / gccas / gccas.cpp
1 //===------------------------------------------------------------------------===
2 // LLVM 'GCCAS' UTILITY 
3 //
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.
7 //
8 //===------------------------------------------------------------------------===
9
10 #include "llvm/Module.h"
11 #include "llvm/Assembly/Parser.h"
12 #include "llvm/Transforms/CleanupGCCOutput.h"
13 #include "llvm/Transforms/LevelChange.h"
14 #include "llvm/Optimizations/DCE.h"
15 #include "llvm/Transforms/ConstantMerge.h"
16 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
17 #include "llvm/Transforms/Scalar/InstructionCombining.h"
18 #include "llvm/Bytecode/Writer.h"
19 #include "Support/CommandLine.h"
20 #include <memory>
21 #include <fstream>
22 #include <string>
23
24 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
25                           cl::Required, "");
26 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
27
28 int main(int argc, char **argv) {
29   cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
30
31   ostream *Out = 0;
32   std::auto_ptr<Module> M;
33   try {
34     // Parse the file now...
35     M.reset(ParseAssemblyFile(InputFilename));
36   } catch (const ParseException &E) {
37     cerr << E.getMessage() << endl;
38     return 1;
39   }
40
41   if (M.get() == 0) {
42     cerr << "assembly didn't read correctly.\n";
43     return 1;
44   }
45   
46   if (OutputFilename == "") {   // Didn't specify an output filename?
47     std::string IFN = InputFilename;
48     int Len = IFN.length();
49     if (IFN[Len-2] == '.' && IFN[Len-1] == 's') {   // Source ends in .s?
50       OutputFilename = std::string(IFN.begin(), IFN.end()-2);
51     } else {
52       OutputFilename = IFN;   // Append a .o to it
53     }
54     OutputFilename += ".o";
55   }
56
57   Out = new std::ofstream(OutputFilename.c_str(), ios::out);
58   if (!Out->good()) {
59     cerr << "Error opening " << OutputFilename << "!\n";
60     return 1;
61   }
62
63   // In addition to just parsing the input from GCC, we also want to spiff it up
64   // a little bit.  Do this now.
65   //
66   PassManager Passes;
67   Passes.add(new opt::DeadCodeElimination());  // Remove Dead code/vars
68   Passes.add(new CleanupGCCOutput());          // Fix gccisms
69   Passes.add(new InductionVariableSimplify()); // Simplify indvars
70   Passes.add(new RaisePointerReferences());    // Eliminate casts
71   Passes.add(new ConstantMerge());             // Merge dup global consts
72   Passes.add(new InstructionCombining());      // Combine silly seq's
73   Passes.add(new opt::DeadCodeElimination());  // Remove Dead code/vars
74
75   // Run our queue of passes all at once now, efficiently.  This form of
76   // runAllPasses frees the Pass objects after runAllPasses completes.
77   //
78   Passes.run(M.get());
79
80   WriteBytecodeToFile(M.get(), *Out);
81   return 0;
82 }
83