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