blob: 02d7cfe827f8904f8bf3019d186960d1a419f0a3 [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 Lattner76d12292002-04-18 19:55:25 +000023#include "Support/Signals.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000024#include <memory>
25#include <fstream>
26#include <string>
27
28cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
29 cl::Required, "");
30cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
Chris Lattner081c4092002-03-21 21:21:50 +000031cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
32 cl::Hidden);
Chris Lattnerecbde332001-10-31 04:28:11 +000033
34int main(int argc, char **argv) {
Chris Lattner11c862c2001-10-31 04:33:33 +000035 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
Chris Lattnerecbde332001-10-31 04:28:11 +000036
Chris Lattnerecbde332001-10-31 04:28:11 +000037 std::auto_ptr<Module> M;
38 try {
39 // Parse the file now...
40 M.reset(ParseAssemblyFile(InputFilename));
41 } catch (const ParseException &E) {
42 cerr << E.getMessage() << endl;
43 return 1;
44 }
45
46 if (M.get() == 0) {
47 cerr << "assembly didn't read correctly.\n";
48 return 1;
49 }
50
51 if (OutputFilename == "") { // Didn't specify an output filename?
Chris Lattner697954c2002-01-20 22:54:45 +000052 std::string IFN = InputFilename;
Chris Lattnerecbde332001-10-31 04:28:11 +000053 int Len = IFN.length();
54 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
Chris Lattner697954c2002-01-20 22:54:45 +000055 OutputFilename = std::string(IFN.begin(), IFN.end()-2);
Chris Lattnerecbde332001-10-31 04:28:11 +000056 } else {
57 OutputFilename = IFN; // Append a .o to it
58 }
59 OutputFilename += ".o";
60 }
61
Chris Lattner3dc67dd2002-01-22 03:30:46 +000062 std::ofstream Out(OutputFilename.c_str(), ios::out);
63 if (!Out.good()) {
Chris Lattnerecbde332001-10-31 04:28:11 +000064 cerr << "Error opening " << OutputFilename << "!\n";
65 return 1;
66 }
67
Chris Lattner76d12292002-04-18 19:55:25 +000068 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
69 RemoveFileOnSignal(OutputFilename);
70
Chris Lattnerecbde332001-10-31 04:28:11 +000071 // In addition to just parsing the input from GCC, we also want to spiff it up
72 // a little bit. Do this now.
73 //
Chris Lattnerf4de63f2002-01-21 07:31:50 +000074 PassManager Passes;
Chris Lattner6a1f6942002-04-10 20:33:32 +000075 Passes.add(createFunctionResolvingPass()); // Resolve (...) functions
Chris Lattner417fbe52002-02-26 21:47:29 +000076 Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
77 Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
78 Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
79 Passes.add(createIndVarSimplifyPass()); // Simplify indvars
Chris Lattner081c4092002-03-21 21:21:50 +000080 if (!StopAtLevelRaise) {
81 Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
Chris Lattner69cd2b12002-04-01 19:45:11 +000082 Passes.add(createPromoteMemoryToRegister()); // Promote alloca's to regs
Chris Lattner081c4092002-03-21 21:21:50 +000083 Passes.add(createConstantMergePass()); // Merge dup global consts
84 Passes.add(createInstructionCombiningPass()); // Combine silly seq's
85 Passes.add(createDeadCodeEliminationPass()); // Remove Dead code/vars
86 }
Chris Lattner417fbe52002-02-26 21:47:29 +000087 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnerecbde332001-10-31 04:28:11 +000088
Chris Lattner3dc67dd2002-01-22 03:30:46 +000089 // Run our queue of passes all at once now, efficiently.
Chris Lattnerf4de63f2002-01-21 07:31:50 +000090 Passes.run(M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +000091 return 0;
92}
93