1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This utility may be invoked in the following manner: 11 // llvm-as --help - Output information about command line switches 12 // llvm-as [options] - Read LLVM asm from stdin, write bytecode to stdout 13 // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bytecode 14 // to the x.bc file. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/Module.h" 19 #include "llvm/Assembly/Parser.h" 20 #include "llvm/Bytecode/Writer.h" 21 #include "llvm/Analysis/Verifier.h" 22 #include "llvm/Support/CommandLine.h" 23 #include "llvm/Support/ManagedStatic.h" 24 #include "llvm/Support/Streams.h" 25 #include "llvm/Support/SystemUtils.h" 26 #include "llvm/System/Signals.h" 27 #include <fstream> 28 #include <iostream> 29 #include <memory> 30 using namespace llvm; 31 32 static cl::opt<std::string> 33 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); 34 35 static cl::opt<std::string> 36 OutputFilename("o", cl::desc("Override output filename"), 37 cl::value_desc("filename")); 38 39 static cl::opt<bool> 40 Force("f", cl::desc("Overwrite output files")); 41 42 static cl::opt<bool> 43 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); 44 45 static cl::opt<bool> 46 NoCompress("disable-compression", cl::init(false), 47 cl::desc("Don't compress the generated bytecode")); 48 49 static cl::opt<bool> 50 DisableVerify("disable-verify", cl::Hidden, 51 cl::desc("Do not run verifier on input LLVM (dangerous!)")); 52 53 int main(int argc, char **argv) { 54 llvm_shutdown_obj X; // Call llvm_shutdown() on exit. 55 cl::ParseCommandLineOptions(argc, argv, " llvm .ll -> .bc assembler\n"); 56 sys::PrintStackTraceOnErrorSignal(); 57 58 int exitCode = 0; 59 std::ostream *Out = 0; 60 try { 61 // Parse the file now... 62 ParseError Err; 63 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename,&Err)); 64 if (M.get() == 0) { 65 cerr << argv[0] << ": " << Err.getMessage() << "\n"; 66 return 1; 67 } 68 69 if (!DisableVerify) { 70 std::string Err; 71 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) { 72 cerr << argv[0] 73 << ": assembly parsed, but does not verify as correct!\n"; 74 cerr << Err; 75 return 1; 76 } 77 } 78 79 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get(); 80 81 if (OutputFilename != "") { // Specified an output filename? 82 if (OutputFilename != "-") { // Not stdout? 83 if (!Force && std::ifstream(OutputFilename.c_str())) { 84 // If force is not specified, make sure not to overwrite a file! 85 cerr << argv[0] << ": error opening '" << OutputFilename 86 << "': file exists!\n" 87 << "Use -f command line argument to force output\n"; 88 return 1; 89 } 90 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | 91 std::ios::trunc | std::ios::binary); 92 } else { // Specified stdout 93 // FIXME: cout is not binary! 94 Out = &std::cout; 95 } 96 } else { 97 if (InputFilename == "-") { 98 OutputFilename = "-"; 99 Out = &std::cout; 100 } else { 101 std::string IFN = InputFilename; 102 int Len = IFN.length(); 103 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { 104 // Source ends in .ll 105 OutputFilename = std::string(IFN.begin(), IFN.end()-3); 106 } else { 107 OutputFilename = IFN; // Append a .bc to it 108 } 109 OutputFilename += ".bc"; 110 111 if (!Force && std::ifstream(OutputFilename.c_str())) { 112 // If force is not specified, make sure not to overwrite a file! 113 cerr << argv[0] << ": error opening '" << OutputFilename 114 << "': file exists!\n" 115 << "Use -f command line argument to force output\n"; 116 return 1; 117 } 118 119 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | 120 std::ios::trunc | std::ios::binary); 121 // Make sure that the Out file gets unlinked from the disk if we get a 122 // SIGINT 123 sys::RemoveFileOnSignal(sys::Path(OutputFilename)); 124 } 125 } 126 127 if (!Out->good()) { 128 cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; 129 return 1; 130 } 131 132 if (Force || !CheckBytecodeOutputToConsole(Out,true)) { 133 OStream L(*Out); 134 WriteBytecodeToFile(M.get(), L, !NoCompress); 135 } 136 } catch (const std::string& msg) { 137 cerr << argv[0] << ": " << msg << "\n"; 138 exitCode = 1; 139 } catch (...) { 140 cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; 141 exitCode = 1; 142 } 143 144 if (Out != &std::cout) delete Out; 145 return exitCode; 146 } 147 148