blob: 6b1d33a5c98a3af0757de1b57eadbd0566151b6e [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 Lattnerdbe05142001-10-31 06:36:48 +000013#include "llvm/Optimizations/LevelChange.h"
14#include "llvm/Optimizations/ConstantProp.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000015#include "llvm/Optimizations/DCE.h"
Chris Lattnerdbe05142001-10-31 06:36:48 +000016#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000017#include "llvm/Bytecode/Writer.h"
18#include "llvm/Support/CommandLine.h"
19#include <memory>
20#include <fstream>
21#include <string>
22
23cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
24 cl::Required, "");
25cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
26
27int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000028 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000029
30 ostream *Out = 0;
31 std::auto_ptr<Module> M;
32 try {
33 // Parse the file now...
34 M.reset(ParseAssemblyFile(InputFilename));
35 } catch (const ParseException &E) {
36 cerr << E.getMessage() << endl;
37 return 1;
38 }
39
40 if (M.get() == 0) {
41 cerr << "assembly didn't read correctly.\n";
42 return 1;
43 }
44
45 if (OutputFilename == "") { // Didn't specify an output filename?
46 string IFN = InputFilename;
47 int Len = IFN.length();
48 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
49 OutputFilename = string(IFN.begin(), IFN.end()-2);
50 } else {
51 OutputFilename = IFN; // Append a .o to it
52 }
53 OutputFilename += ".o";
54 }
55
56 Out = new ofstream(OutputFilename.c_str(), ios::out);
57 if (!Out->good()) {
58 cerr << "Error opening " << OutputFilename << "!\n";
59 return 1;
60 }
61
62 // In addition to just parsing the input from GCC, we also want to spiff it up
63 // a little bit. Do this now.
64 //
65 vector<Pass*> Passes;
Chris Lattnerdbe05142001-10-31 06:36:48 +000066 Passes.push_back(new CleanupGCCOutput()); // Fix gccisms
67 Passes.push_back(new opt::RaiseRepresentation());// Fix general low level code
68 Passes.push_back(new opt::ConstantPropogation());// Trivial const prop
69 Passes.push_back(new opt::DeadCodeElimination());// Trivial DCE
70 Passes.push_back(new ConstantMerge()); // Merge dup global constants
Chris Lattnerecbde332001-10-31 04:28:11 +000071
72 // Run our queue of passes all at once now, efficiently. This form of
73 // runAllPasses frees the Pass objects after runAllPasses completes.
74 //
75 Pass::runAllPassesAndFree(M.get(), Passes);
76
77 WriteBytecodeToFile(M.get(), *Out);
78 return 0;
79}
80