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 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | #include "llvm/Module.h" |
| 13 | #include "llvm/Assembly/Parser.h" |
| 14 | #include "llvm/Assembly/Writer.h" |
| 15 | #include "llvm/Bytecode/Writer.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 16 | #include "Support/CommandLine.h" |
| 17 | #include <fstream> |
| 18 | #include <string> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 19 | #include <memory> |
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, "-"); |
Chris Lattner | 1e78f36 | 2001-07-23 19:27:24 +0000 | [diff] [blame] | 22 | cl::String OutputFilename("o", "Override output filename", cl::NoFlags, ""); |
| 23 | cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false); |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 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 | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 32 | std::auto_ptr<Module> C(ParseAssemblyFile(InputFilename)); |
| 33 | if (C.get() == 0) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | cerr << "assembly didn't read correctly.\n"; |
| 35 | return 1; |
| 36 | } |
| 37 | |
Chris Lattner | 1e78f36 | 2001-07-23 19:27:24 +0000 | [diff] [blame] | 38 | if (DumpAsm) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 39 | cerr << "Here's the assembly:\n" << C.get(); |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 1e78f36 | 2001-07-23 19:27:24 +0000 | [diff] [blame] | 41 | if (OutputFilename != "") { // Specified an output filename? |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 42 | if (!Force && !std::ifstream(OutputFilename.c_str())) { |
| 43 | // If force is not specified, make sure not to overwrite a file! |
| 44 | cerr << "Error opening '" << OutputFilename << "': File exists!\n" |
| 45 | << "Use -f command line argument to force output\n"; |
| 46 | return 1; |
| 47 | } |
| 48 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 49 | } else { |
Chris Lattner | 1e78f36 | 2001-07-23 19:27:24 +0000 | [diff] [blame] | 50 | if (InputFilename == "-") { |
| 51 | OutputFilename = "-"; |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 52 | Out = &cout; |
| 53 | } else { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 54 | std::string IFN = InputFilename; |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 55 | int Len = IFN.length(); |
| 56 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { |
| 57 | // Source ends in .ll |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 58 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 59 | } else { |
Chris Lattner | 1e78f36 | 2001-07-23 19:27:24 +0000 | [diff] [blame] | 60 | OutputFilename = IFN; // Append a .bc to it |
Chris Lattner | 8f367bd | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 1e78f36 | 2001-07-23 19:27:24 +0000 | [diff] [blame] | 62 | OutputFilename += ".bc"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 63 | |
| 64 | if (!Force && !std::ifstream(OutputFilename.c_str())) { |
| 65 | // If force is not specified, make sure not to overwrite a file! |
| 66 | cerr << "Error opening '" << OutputFilename << "': File exists!\n" |
| 67 | << "Use -f command line argument to force output\n"; |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | Out = new std::ofstream(OutputFilename.c_str()); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 74 | |
| 75 | if (!Out->good()) { |
| 76 | cerr << "Error opening " << OutputFilename << "!\n"; |
| 77 | return 1; |
| 78 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame^] | 80 | WriteBytecodeToFile(C.get(), *Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 81 | } catch (const ParseException &E) { |
| 82 | cerr << E.getMessage() << endl; |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | if (Out != &cout) delete Out; |
| 87 | return 0; |
| 88 | } |
| 89 | |