blob: b9e3a6207db4978bc9e277f3238cbc875cbdc66f [file] [log] [blame]
Chris Lattnerecbde332001-10-31 04:28:11 +00001//===------------------------------------------------------------------------===
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"
Chris Lattner068f4872001-11-01 02:41:09 +000013#include "llvm/Transforms/LevelChange.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000014#include "llvm/Optimizations/DCE.h"
Chris Lattnerdbe05142001-10-31 06:36:48 +000015#include "llvm/Transforms/ConstantMerge.h"
Chris Lattner9c6f2ac2001-12-05 06:34:58 +000016#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattnerd584dcc2001-12-14 16:48:30 +000017#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000018#include "llvm/Bytecode/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000020#include <memory>
21#include <fstream>
22#include <string>
23
24cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
25 cl::Required, "");
26cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
27
28int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000029 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000030
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 string IFN = InputFilename;
48 int Len = IFN.length();
49 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
50 OutputFilename = 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 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 vector<Pass*> Passes;
Chris Lattner0d7a4742001-12-07 04:25:36 +000067 Passes.push_back(new opt::DeadCodeElimination()); // Remove Dead code/vars
68 Passes.push_back(new CleanupGCCOutput()); // Fix gccisms
Chris Lattner9c6f2ac2001-12-05 06:34:58 +000069 Passes.push_back(new InductionVariableSimplify()); // Simplify indvars
Chris Lattnerd584dcc2001-12-14 16:48:30 +000070 Passes.push_back(new RaisePointerReferences()); // Eliminate casts
71 Passes.push_back(new ConstantMerge()); // Merge dup global consts
72 Passes.push_back(new InstructionCombining()); // Combine silly seq's
73 Passes.push_back(new opt::DeadCodeElimination()); // Remove Dead code/vars
Chris Lattnerecbde332001-10-31 04:28:11 +000074
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 Pass::runAllPassesAndFree(M.get(), Passes);
79
80 WriteBytecodeToFile(M.get(), *Out);
81 return 0;
82}
83