1 //===------------------------------------------------------------------------===
2 // LLVM 'GCCAS' UTILITY
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.
8 //===------------------------------------------------------------------------===
10 #include "llvm/Module.h"
11 #include "llvm/Assembly/Parser.h"
12 #include "llvm/Transforms/CleanupGCCOutput.h"
13 #include "llvm/Transforms/LevelChange.h"
14 #include "llvm/Optimizations/ConstantProp.h"
15 #include "llvm/Optimizations/DCE.h"
16 #include "llvm/Transforms/ConstantMerge.h"
17 #include "llvm/Bytecode/Writer.h"
18 #include "Support/CommandLine.h"
23 cl::String InputFilename ("", "Parse <arg> file, compile to bytecode",
25 cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
27 int main(int argc, char **argv) {
28 cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n");
31 std::auto_ptr<Module> M;
33 // Parse the file now...
34 M.reset(ParseAssemblyFile(InputFilename));
35 } catch (const ParseException &E) {
36 cerr << E.getMessage() << endl;
41 cerr << "assembly didn't read correctly.\n";
45 if (OutputFilename == "") { // Didn't specify an output filename?
46 string IFN = InputFilename;
47 int Len = IFN.length();
48 if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s?
49 OutputFilename = string(IFN.begin(), IFN.end()-2);
51 OutputFilename = IFN; // Append a .o to it
53 OutputFilename += ".o";
56 Out = new ofstream(OutputFilename.c_str(), ios::out);
58 cerr << "Error opening " << OutputFilename << "!\n";
62 // In addition to just parsing the input from GCC, we also want to spiff it up
63 // a little bit. Do this now.
66 Passes.push_back(new CleanupGCCOutput()); // Fix gccisms
67 Passes.push_back(new RaisePointerReferences()); // Fix general low level code
68 Passes.push_back(new ConstantMerge()); // Merge dup global constants
70 // Run our queue of passes all at once now, efficiently. This form of
71 // runAllPasses frees the Pass objects after runAllPasses completes.
73 Pass::runAllPassesAndFree(M.get(), Passes);
75 WriteBytecodeToFile(M.get(), *Out);