blob: 4cb5d15630d879fbf9eab8760e37fd0997b23ce8 [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"
Chris Lattner0f3bfff2002-01-31 00:46:22 +000011#include "llvm/PassManager.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000012#include "llvm/Assembly/Parser.h"
13#include "llvm/Transforms/CleanupGCCOutput.h"
Chris Lattner068f4872001-11-01 02:41:09 +000014#include "llvm/Transforms/LevelChange.h"
Chris Lattnerdbe05142001-10-31 06:36:48 +000015#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000016#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner59b6b8e2002-01-21 23:17:48 +000017#include "llvm/Transforms/Scalar/DCE.h"
Chris Lattner9c6f2ac2001-12-05 06:34:58 +000018#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattnerd584dcc2001-12-14 16:48:30 +000019#include "llvm/Transforms/Scalar/InstructionCombining.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000020#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021#include "Support/CommandLine.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000022#include <memory>
23#include <fstream>
24#include <string>
25
26cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
27 cl::Required, "");
28cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
29
30int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000031 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000032
Chris Lattnerecbde332001-10-31 04:28:11 +000033 std::auto_ptr<Module> M;
34 try {
35 // Parse the file now...
36 M.reset(ParseAssemblyFile(InputFilename));
37 } catch (const ParseException &E) {
38 cerr << E.getMessage() << endl;
39 return 1;
40 }
41
42 if (M.get() == 0) {
43 cerr << "assembly didn't read correctly.\n";
44 return 1;
45 }
46
47 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +000048 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +000049 int Len = IFN.length();
50 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +000051 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +000052 } else {
53 OutputFilename = IFN; // Append a .o to it
54 }
55 OutputFilename += ".o";
56 }
57
Chris Lattner3dc67dd2002-01-22 03:30:46 +000058 std::ofstream Out(OutputFilename.c_str(), ios::out);
59 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +000060 cerr << "Error opening " << OutputFilename << "!\n";
61 return 1;
62 }
63
64 // In addition to just parsing the input from GCC, we also want to spiff it up
65 // a little bit. Do this now.
66 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +000067 PassManager Passes;
Chris Lattnere9452552002-01-22 21:06:44 +000068 Passes.add(new DeadInstElimination()); // Remove Dead code/vars
Chris Lattner5048c3b2002-01-22 00:13:51 +000069 Passes.add(new RaiseAllocations()); // call %malloc -> malloc inst
Chris Lattnerf4de63f2002-01-21 07:31:50 +000070 Passes.add(new CleanupGCCOutput()); // Fix gccisms
71 Passes.add(new InductionVariableSimplify()); // Simplify indvars
72 Passes.add(new RaisePointerReferences()); // Eliminate casts
73 Passes.add(new ConstantMerge()); // Merge dup global consts
74 Passes.add(new InstructionCombining()); // Combine silly seq's
Chris Lattner59b6b8e2002-01-21 23:17:48 +000075 Passes.add(new DeadCodeElimination()); // Remove Dead code/vars
Chris Lattner3dc67dd2002-01-22 03:30:46 +000076 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnerecbde332001-10-31 04:28:11 +000077
Chris Lattner3dc67dd2002-01-22 03:30:46 +000078 // Run our queue of passes all at once now, efficiently.
Chris Lattnerf4de63f2002-01-21 07:31:50 +000079 Passes.run(M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +000080 return 0;
81}
82