blob: 613f51c8f39c65dd34123ed30a2acf6fd7c1e9f3 [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 Lattner598d21c2002-04-28 01:00:15 +000018#include "llvm/Transforms/Scalar/GCSE.h"
Chris Lattner9c6f2ac2001-12-05 06:34:58 +000019#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Chris Lattnerd584dcc2001-12-14 16:48:30 +000020#include "llvm/Transforms/Scalar/InstructionCombining.h"
Cameron Buschardt0732c702002-03-27 23:29:23 +000021#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000022#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000024#include "Support/Signals.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000025#include <memory>
26#include <fstream>
27#include <string>
28
29cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
30 cl::Required, "");
31cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
Chris Lattner081c4092002-03-21 21:21:50 +000032cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
33 cl::Hidden);
Chris Lattnerecbde332001-10-31 04:28:11 +000034
35int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000036 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000037
Chris Lattnerecbde332001-10-31 04:28:11 +000038 std::auto_ptr<Module> M;
39 try {
40 // Parse the file now...
41 M.reset(ParseAssemblyFile(InputFilename));
42 } catch (const ParseException &E) {
43 cerr << E.getMessage() << endl;
44 return 1;
45 }
46
47 if (M.get() == 0) {
48 cerr << "assembly didn't read correctly.\n";
49 return 1;
50 }
51
52 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +000053 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +000054 int Len = IFN.length();
55 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +000056 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +000057 } else {
58 OutputFilename = IFN; // Append a .o to it
59 }
60 OutputFilename += ".o";
61 }
62
Chris Lattner3dc67dd2002-01-22 03:30:46 +000063 std::ofstream Out(OutputFilename.c_str(), ios::out);
64 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +000065 cerr << "Error opening " << OutputFilename << "!\n";
66 return 1;
67 }
68
Chris Lattner76d12292002-04-18 19:55:25 +000069 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
70 RemoveFileOnSignal(OutputFilename);
71
Chris Lattnerecbde332001-10-31 04:28:11 +000072 // In addition to just parsing the input from GCC, we also want to spiff it up
73 // a little bit. Do this now.
74 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +000075 PassManager Passes;
Chris Lattner6a1f6942002-04-10 20:33:32 +000076 Passes.add(createFunctionResolvingPass()); // Resolve (...) functions
Chris Lattner417fbe52002-02-26 21:47:29 +000077 Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
78 Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
79 Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
80 Passes.add(createIndVarSimplifyPass()); // Simplify indvars
Chris Lattner081c4092002-03-21 21:21:50 +000081 if (!StopAtLevelRaise) {
82 Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
Chris Lattner69cd2b12002-04-01 19:45:11 +000083 Passes.add(createPromoteMemoryToRegister()); // Promote alloca's to regs
Chris Lattner081c4092002-03-21 21:21:50 +000084 Passes.add(createConstantMergePass()); // Merge dup global consts
85 Passes.add(createInstructionCombiningPass()); // Combine silly seq's
86 Passes.add(createDeadCodeEliminationPass()); // Remove Dead code/vars
Chris Lattner598d21c2002-04-28 01:00:15 +000087 Passes.add(createGCSEPass()); // Remove common subexprs
Chris Lattner081c4092002-03-21 21:21:50 +000088 }
Chris Lattner417fbe52002-02-26 21:47:29 +000089 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnerecbde332001-10-31 04:28:11 +000090
Chris Lattner3dc67dd2002-01-22 03:30:46 +000091 // Run our queue of passes all at once now, efficiently.
Chris Lattnerf4de63f2002-01-21 07:31:50 +000092 Passes.run(M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +000093 return 0;
94}
95