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