Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===------------------------------------------------------------------------=== |
| 2 | // LLVM 'AS' UTILITY |
| 3 | // |
| 4 | // This utility may be invoked in the following manner: |
| 5 | // as --help - Output information about command line switches |
| 6 | // as [options] - Read LLVM assembly from stdin, write bytecode to stdout |
| 7 | // as [options] x.ll - Read LLVM assembly from the x.ll file, write bytecode |
| 8 | // to the x.bc file. |
| 9 | // |
| 10 | //===------------------------------------------------------------------------=== |
| 11 | |
| 12 | #include <iostream.h> |
| 13 | #include <fstream.h> |
| 14 | #include <string> |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Assembly/Parser.h" |
| 17 | #include "llvm/Assembly/Writer.h" |
| 18 | #include "llvm/Bytecode/Writer.h" |
| 19 | #include "llvm/Tools/CommandLine.h" |
| 20 | |
| 21 | |
| 22 | int main(int argc, char **argv) { |
| 23 | ToolCommandLine Opts(argc, argv); |
| 24 | bool DumpAsm = false; |
| 25 | |
| 26 | for (int i = 1; i < argc; i++) { |
| 27 | if (string(argv[i]) == string("-d")) { |
| 28 | argv[i] = 0; DumpAsm = true; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | bool PrintMessage = false; |
| 33 | for (int i = 1; i < argc; i++) { |
| 34 | if (argv[i] == 0) continue; |
| 35 | |
| 36 | if (string(argv[i]) == string("--help")) { |
| 37 | PrintMessage = true; |
| 38 | } else { |
| 39 | cerr << argv[0] << ": argument not recognized: '" << argv[i] << "'!\n"; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if (PrintMessage) { |
| 44 | cerr << argv[0] << " usage:\n" |
| 45 | << " " << argv[0] << " --help - Print this usage information\n" |
| 46 | << " " << argv[0] << " x.ll - Parse <x.ll> file and output " |
| 47 | << "bytecodes to x.bc\n" |
| 48 | << " " << argv[0] << " - Parse stdin and write to stdout.\n"; |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | ostream *Out = &cout; // Default to output to stdout... |
| 53 | try { |
| 54 | // Parse the file now... |
| 55 | Module *C = ParseAssemblyFile(Opts); |
| 56 | if (C == 0) { |
| 57 | cerr << "assembly didn't read correctly.\n"; |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | if (DumpAsm) |
| 62 | cerr << "Here's the assembly:\n" << C; |
| 63 | |
| 64 | if (Opts.getOutputFilename() != "-") { |
| 65 | Out = new ofstream(Opts.getOutputFilename().c_str(), |
| 66 | (Opts.getForce() ? 0 : ios::noreplace)|ios::out); |
| 67 | if (!Out->good()) { |
| 68 | cerr << "Error opening " << Opts.getOutputFilename() << "!\n"; |
| 69 | delete C; |
| 70 | return 1; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | WriteBytecodeToFile(C, *Out); |
| 75 | |
| 76 | delete C; |
| 77 | } catch (const ParseException &E) { |
| 78 | cerr << E.getMessage() << endl; |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | if (Out != &cout) delete Out; |
| 83 | return 0; |
| 84 | } |
| 85 | |