blob: ae0320c27163554b45def4b371af92ade29d3b8a [file] [log] [blame]
Chris Lattner3dc67dd2002-01-22 03:30:46 +00001//===----------------------------------------------------------------------===//
Chris Lattnerecbde332001-10-31 04:28:11 +00002// 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//
Chris Lattner3dc67dd2002-01-22 03:30:46 +00008//===----------------------------------------------------------------------===//
Chris Lattnerecbde332001-10-31 04:28:11 +00009
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 Lattnerdbe05142001-10-31 06:36:48 +000014#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000015#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000016#include "llvm/Transforms/Scalar/DCE.h"
Chris Lattner9c6f2ac2001-12-05 06:34:58 +000017#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattnerd584dcc2001-12-14 16:48:30 +000018#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000019#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/CommandLine.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000021#include <memory>
22#include <fstream>
23#include <string>
24
25cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
26 cl::Required, "");
27cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
28
29int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000030 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000031
Chris Lattnerecbde332001-10-31 04:28:11 +000032 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?
Chris Lattner697954c2002-01-20 22:54:45 +000047 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +000048 int Len = IFN.length();
49 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +000050 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +000051 } else {
52 OutputFilename = IFN; // Append a .o to it
53 }
54 OutputFilename += ".o";
55 }
56
Chris Lattner3dc67dd2002-01-22 03:30:46 +000057 std::ofstream Out(OutputFilename.c_str(), ios::out);
58 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +000059 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 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +000066 PassManager Passes;
Chris Lattner59b6b8e2002-01-21 23:17:48 +000067 Passes.add(new DeadCodeElimination()); // Remove Dead code/vars
Chris Lattner5048c3b2002-01-22 00:13:51 +000068 Passes.add(new RaiseAllocations()); // call %malloc -> malloc inst
Chris Lattnerf4de63f2002-01-21 07:31:50 +000069 Passes.add(new CleanupGCCOutput()); // Fix gccisms
70 Passes.add(new InductionVariableSimplify()); // Simplify indvars
71 Passes.add(new RaisePointerReferences()); // Eliminate casts
72 Passes.add(new ConstantMerge()); // Merge dup global consts
73 Passes.add(new InstructionCombining()); // Combine silly seq's
Chris Lattner59b6b8e2002-01-21 23:17:48 +000074 Passes.add(new DeadCodeElimination()); // Remove Dead code/vars
Chris Lattner3dc67dd2002-01-22 03:30:46 +000075 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnerecbde332001-10-31 04:28:11 +000076
Chris Lattner3dc67dd2002-01-22 03:30:46 +000077 // Run our queue of passes all at once now, efficiently.
Chris Lattnerf4de63f2002-01-21 07:31:50 +000078 Passes.run(M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +000079 return 0;
80}
81