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" |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 21 | cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", 0, "-"); |
| 22 | cl::String OutputFilename("o", "Override output filename", 0, ""); |
| 23 | cl::Flag Force ("f", "Overwrite output files", 0, false); |
| 24 | cl::Flag DumpAsm ("d", "Print assembly as parsed", cl::Hidden, false); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | |
| 26 | int main(int argc, char **argv) { |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 27 | cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 29 | ostream *Out = 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 30 | try { |
| 31 | // Parse the file now... |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 32 | Module *C = ParseAssemblyFile(InputFilename.getValue()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | if (C == 0) { |
| 34 | cerr << "assembly didn't read correctly.\n"; |
| 35 | return 1; |
| 36 | } |
| 37 | |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 38 | if (DumpAsm.getValue()) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | cerr << "Here's the assembly:\n" << C; |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 40 | |
| 41 | if (OutputFilename.getValue() != "") { // Specified an output filename? |
| 42 | Out = new ofstream(OutputFilename.getValue().c_str(), |
| 43 | (Force.getValue() ? 0 : ios::noreplace)|ios::out); |
| 44 | } else { |
| 45 | if (InputFilename.getValue() == "-") { |
| 46 | OutputFilename.setValue("-"); |
| 47 | Out = &cout; |
| 48 | } else { |
| 49 | string IFN = InputFilename.getValue(); |
| 50 | int Len = IFN.length(); |
| 51 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 52 | // Source ends in .ll |
| 53 | OutputFilename.setValue(string(IFN.begin(), IFN.end()-3)); |
| 54 | } else { |
| 55 | OutputFilename.setValue(IFN); // Append a .bc to it |
| 56 | } |
| 57 | OutputFilename.setValue(OutputFilename.getValue() + ".bc"); |
| 58 | Out = new ofstream(OutputFilename.getValue().c_str(), |
| 59 | (Force.getValue() ? 0 : ios::noreplace)|ios::out); |
| 60 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 62 | if (!Out->good()) { |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 63 | cerr << "Error opening " << OutputFilename.getValue() << "!\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 64 | delete C; |
| 65 | return 1; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | WriteBytecodeToFile(C, *Out); |
| 70 | |
| 71 | delete C; |
| 72 | } catch (const ParseException &E) { |
| 73 | cerr << E.getMessage() << endl; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | if (Out != &cout) delete Out; |
| 78 | return 0; |
| 79 | } |
| 80 | |