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