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/Bytecode/Writer.h" 15 #include "Support/CommandLine.h" 16 #include "Support/Signals.h" 17 #include <fstream> 18 #include <memory> 19 using std::cerr; 20 using std::string; 21 22 static cl::opt<string> 23 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); 24 25 static cl::opt<string> 26 OutputFilename("o", cl::desc("Override output filename"), 27 cl::value_desc("filename")); 28 29 static cl::opt<bool> 30 Force("f", cl::desc("Overwrite output files")); 31 32 static cl::opt<bool> 33 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); 34 35 int main(int argc, char **argv) { 36 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n"); 37 38 std::ostream *Out = 0; 39 try { 40 // Parse the file now... 41 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename)); 42 if (M.get() == 0) { 43 cerr << "assembly didn't read correctly.\n"; 44 return 1; 45 } 46 47 if (DumpAsm) cerr << "Here's the assembly:\n" << M.get(); 48 49 if (OutputFilename != "") { // Specified an output filename? 50 if (!Force && std::ifstream(OutputFilename.c_str())) { 51 // If force is not specified, make sure not to overwrite a file! 52 cerr << "Error opening '" << OutputFilename << "': File exists!\n" 53 << "Use -f command line argument to force output\n"; 54 return 1; 55 } 56 Out = new std::ofstream(OutputFilename.c_str()); 57 } else { 58 if (InputFilename == "-") { 59 OutputFilename = "-"; 60 Out = &std::cout; 61 } else { 62 std::string IFN = InputFilename; 63 int Len = IFN.length(); 64 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { 65 // Source ends in .ll 66 OutputFilename = std::string(IFN.begin(), IFN.end()-3); 67 } else { 68 OutputFilename = IFN; // Append a .bc to it 69 } 70 OutputFilename += ".bc"; 71 72 if (!Force && std::ifstream(OutputFilename.c_str())) { 73 // If force is not specified, make sure not to overwrite a file! 74 cerr << "Error opening '" << OutputFilename << "': File exists!\n" 75 << "Use -f command line argument to force output\n"; 76 return 1; 77 } 78 79 Out = new std::ofstream(OutputFilename.c_str()); 80 // Make sure that the Out file gets unlink'd from the disk if we get a 81 // SIGINT 82 RemoveFileOnSignal(OutputFilename); 83 } 84 } 85 86 if (!Out->good()) { 87 cerr << "Error opening " << OutputFilename << "!\n"; 88 return 1; 89 } 90 91 WriteBytecodeToFile(M.get(), *Out); 92 } catch (const ParseException &E) { 93 cerr << E.getMessage() << std::endl; 94 return 1; 95 } 96 97 if (Out != &std::cout) delete Out; 98 return 0; 99 } 100 101