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