blob: 59c12713fb5186d83760896ffd8533f770413ab7 [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 Lattner65f1b892002-05-07 20:03:27 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner3dc67dd2002-01-22 03:30:46 +000018#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000020#include "Support/Signals.h"
Chris Lattnerecbde332001-10-31 04:28:11 +000021#include <memory>
22#include <fstream>
Chris Lattnerecbde332001-10-31 04:28:11 +000023
24cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
25 cl::Required, "");
26cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
Chris Lattner081c4092002-03-21 21:21:50 +000027cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before level raise",
28 cl::Hidden);
Chris Lattnerecbde332001-10-31 04:28:11 +000029
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
Chris Lattner76d12292002-04-18 19:55:25 +000064 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
65 RemoveFileOnSignal(OutputFilename);
66
Chris Lattnerecbde332001-10-31 04:28:11 +000067 // 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 Lattner8ac58a82002-04-29 20:11:38 +000072 Passes.add(createConstantMergePass()); // Merge dup global constants
Chris Lattner417fbe52002-02-26 21:47:29 +000073 Passes.add(createDeadInstEliminationPass()); // Remove Dead code/vars
74 Passes.add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
75 Passes.add(createCleanupGCCOutputPass()); // Fix gccisms
76 Passes.add(createIndVarSimplifyPass()); // Simplify indvars
Chris Lattner081c4092002-03-21 21:21:50 +000077 if (!StopAtLevelRaise) {
78 Passes.add(createRaisePointerReferencesPass()); // Eliminate casts
Chris Lattner69cd2b12002-04-01 19:45:11 +000079 Passes.add(createPromoteMemoryToRegister()); // Promote alloca's to regs
Chris Lattner081c4092002-03-21 21:21:50 +000080 Passes.add(createInstructionCombiningPass()); // Combine silly seq's
Chris Lattnereadd32c2002-05-06 16:52:15 +000081 Passes.add(createDeadInstEliminationPass()); // Kill InstCombine remnants
Chris Lattner598d21c2002-04-28 01:00:15 +000082 Passes.add(createGCSEPass()); // Remove common subexprs
Chris Lattner5f06e102002-05-06 18:54:12 +000083 Passes.add(createSCCPPass()); // Constant prop with SCCP
Chris Lattner9c1291c2002-05-06 03:04:17 +000084 Passes.add(createDeadCodeEliminationPass()); // Remove Dead code/vars
Chris Lattner081c4092002-03-21 21:21:50 +000085 }
Chris Lattner417fbe52002-02-26 21:47:29 +000086 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnerecbde332001-10-31 04:28:11 +000087
Chris Lattner3dc67dd2002-01-22 03:30:46 +000088 // Run our queue of passes all at once now, efficiently.
Chris Lattnerf4de63f2002-01-21 07:31:50 +000089 Passes.run(M.get());
Chris Lattnerecbde332001-10-31 04:28:11 +000090 return 0;
91}
92