blob: 1bf1e8c0b7b741d662940c97040172a5533f5dc3 [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"
Cameron Buschardt0732c702002-03-27 23:29:23 +000020#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000021#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/CommandLine.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000023#include <memory>
24#include <fstream>
25#include <string>
26
27cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
28 cl::Required, "");
29cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
Chris Lattner081c4092002-03-21 21:21:50 +000030cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
31 cl::Hidden);
Chris Lattnerecbde332001-10-31 04:28:11 +000032
33int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000034 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000035
Chris Lattnerecbde332001-10-31 04:28:11 +000036 std::auto_ptr<Module> M;
37 try {
38 // Parse the file now...
39 M.reset(ParseAssemblyFile(InputFilename));
40 } catch (const ParseException &E) {
41 cerr << E.getMessage() << endl;
42 return 1;
43 }
44
45 if (M.get() == 0) {
46 cerr << "assembly didn't read correctly.\n";
47 return 1;
48 }
49
50 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +000051 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +000052 int Len = IFN.length();
53 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +000054 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +000055 } else {
56 OutputFilename = IFN; // Append a .o to it
57 }
58 OutputFilename += ".o";
59 }
60
Chris Lattner3dc67dd2002-01-22 03:30:46 +000061 std::ofstream Out(OutputFilename.c_str(), ios::out);
62 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +000063 cerr << "Error opening " << OutputFilename << "!\n";
64 return 1;
65 }
66
67 // In addition to just parsing the input from GCC, we also want to spiff it up
68 // a little bit. Do this now.
69 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +000070 PassManager Passes;
Chris Lattner6a1f6942002-04-10 20:33:32 +000071 Passes.add(createFunctionResolvingPass()); // Resolve (...) functions
Chris Lattner417fbe52002-02-26 21:47:29 +000072 Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
73 Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
74 Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
75 Passes.add(createIndVarSimplifyPass()); // Simplify indvars
Chris Lattner081c4092002-03-21 21:21:50 +000076 if (!StopAtLevelRaise) {
77 Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
Chris Lattner69cd2b12002-04-01 19:45:11 +000078 Passes.add(createPromoteMemoryToRegister()); // Promote alloca's to regs
Chris Lattner081c4092002-03-21 21:21:50 +000079 Passes.add(createConstantMergePass()); // Merge dup global consts
80 Passes.add(createInstructionCombiningPass()); // Combine silly seq's
81 Passes.add(createDeadCodeEliminationPass()); // Remove Dead code/vars
82 }
Chris Lattner417fbe52002-02-26 21:47:29 +000083 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnerecbde332001-10-31 04:28:11 +000084
Chris Lattner3dc67dd2002-01-22 03:30:46 +000085 // Run our queue of passes all at once now, efficiently.
Chris Lattnerf4de63f2002-01-21 07:31:50 +000086 Passes.run(M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +000087 return 0;
88}
89