1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // 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 bitcode to stdout 13 // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode 14 // to the x.bc file. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/IR/LLVMContext.h" 19 #include "llvm/Analysis/Verifier.h" 20 #include "llvm/AsmParser/Parser.h" 21 #include "llvm/Bitcode/ReaderWriter.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/ManagedStatic.h" 25 #include "llvm/Support/PrettyStackTrace.h" 26 #include "llvm/Support/Signals.h" 27 #include "llvm/Support/SourceMgr.h" 28 #include "llvm/Support/SystemUtils.h" 29 #include "llvm/Support/ToolOutputFile.h" 30 #include <memory> 31 using namespace llvm; 32 33 static cl::opt<std::string> 34 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-")); 35 36 static cl::opt<std::string> 37 OutputFilename("o", cl::desc("Override output filename"), 38 cl::value_desc("filename")); 39 40 static cl::opt<bool> 41 Force("f", cl::desc("Enable binary output on terminals")); 42 43 static cl::opt<bool> 44 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false)); 45 46 static cl::opt<bool> 47 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden); 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 static void WriteOutputFile(const Module *M) { 54 // Infer the output filename if needed. 55 if (OutputFilename.empty()) { 56 if (InputFilename == "-") { 57 OutputFilename = "-"; 58 } else { 59 std::string IFN = InputFilename; 60 int Len = IFN.length(); 61 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') { 62 // Source ends in .ll 63 OutputFilename = std::string(IFN.begin(), IFN.end()-3); 64 } else { 65 OutputFilename = IFN; // Append a .bc to it 66 } 67 OutputFilename += ".bc"; 68 } 69 } 70 71 std::string ErrorInfo; 72 OwningPtr<tool_output_file> Out(new tool_output_file( 73 OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary)); 74 if (!ErrorInfo.empty()) { 75 errs() << ErrorInfo << '\n'; 76 exit(1); 77 } 78 79 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) 80 WriteBitcodeToFile(M, Out->os()); 81 82 // Declare success. 83 Out->keep(); 84 } 85 86 int main(int argc, char **argv) { 87 // Print a stack trace if we signal out. 88 sys::PrintStackTraceOnErrorSignal(); 89 PrettyStackTraceProgram X(argc, argv); 90 LLVMContext &Context = getGlobalContext(); 91 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 92 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); 93 94 // Parse the file now... 95 SMDiagnostic Err; 96 OwningPtr<Module> M(ParseAssemblyFile(InputFilename, Err, Context)); 97 if (M.get() == 0) { 98 Err.print(argv[0], errs()); 99 return 1; 100 } 101 102 if (!DisableVerify) { 103 std::string Err; 104 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) { 105 errs() << argv[0] 106 << ": assembly parsed, but does not verify as correct!\n"; 107 errs() << Err; 108 return 1; 109 } 110 } 111 112 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get(); 113 114 if (!DisableOutput) 115 WriteOutputFile(M.get()); 116 117 return 0; 118 } 119