Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 1 | //===------------------------------------------------------------------------=== |
| 2 | // 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 | // |
| 8 | //===------------------------------------------------------------------------=== |
| 9 | |
| 10 | #include "llvm/Module.h" |
| 11 | #include "llvm/Assembly/Parser.h" |
| 12 | #include "llvm/Transforms/CleanupGCCOutput.h" |
| 13 | #include "llvm/Optimizations/DCE.h" |
| 14 | #include "llvm/Bytecode/Writer.h" |
| 15 | #include "llvm/Support/CommandLine.h" |
| 16 | #include <memory> |
| 17 | #include <fstream> |
| 18 | #include <string> |
| 19 | |
| 20 | cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", |
| 21 | cl::Required, ""); |
| 22 | cl::String OutputFilename("o", "Override output filename", cl::NoFlags, ""); |
| 23 | |
| 24 | int main(int argc, char **argv) { |
Chris Lattner | 11c862c | 2001-10-31 04:33:33 +0000 | [diff] [blame^] | 25 | cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n"); |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 26 | |
| 27 | ostream *Out = 0; |
| 28 | std::auto_ptr<Module> M; |
| 29 | try { |
| 30 | // Parse the file now... |
| 31 | M.reset(ParseAssemblyFile(InputFilename)); |
| 32 | } catch (const ParseException &E) { |
| 33 | cerr << E.getMessage() << endl; |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | if (M.get() == 0) { |
| 38 | cerr << "assembly didn't read correctly.\n"; |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | if (OutputFilename == "") { // Didn't specify an output filename? |
| 43 | string IFN = InputFilename; |
| 44 | int Len = IFN.length(); |
| 45 | if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s? |
| 46 | OutputFilename = string(IFN.begin(), IFN.end()-2); |
| 47 | } else { |
| 48 | OutputFilename = IFN; // Append a .o to it |
| 49 | } |
| 50 | OutputFilename += ".o"; |
| 51 | } |
| 52 | |
| 53 | Out = new ofstream(OutputFilename.c_str(), ios::out); |
| 54 | if (!Out->good()) { |
| 55 | cerr << "Error opening " << OutputFilename << "!\n"; |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | // In addition to just parsing the input from GCC, we also want to spiff it up |
| 60 | // a little bit. Do this now. |
| 61 | // |
| 62 | vector<Pass*> Passes; |
| 63 | Passes.push_back(new CleanupGCCOutput()); |
| 64 | Passes.push_back(new opt::DeadCodeElimination()); |
| 65 | |
| 66 | // Run our queue of passes all at once now, efficiently. This form of |
| 67 | // runAllPasses frees the Pass objects after runAllPasses completes. |
| 68 | // |
| 69 | Pass::runAllPassesAndFree(M.get(), Passes); |
| 70 | |
| 71 | WriteBytecodeToFile(M.get(), *Out); |
| 72 | return 0; |
| 73 | } |
| 74 | |