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/AsmParser/Parser.h" 19 #include "llvm/Bitcode/BitcodeWriter.h" 20 #include "llvm/IR/LLVMContext.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/IR/Verifier.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/ManagedStatic.h" 26 #include "llvm/Support/PrettyStackTrace.h" 27 #include "llvm/Support/Signals.h" 28 #include "llvm/Support/SourceMgr.h" 29 #include "llvm/Support/SystemUtils.h" 30 #include "llvm/Support/ToolOutputFile.h" 31 #include <memory> 32 using namespace llvm; 33 34 static cl::opt<std::string> InputFilename(cl::Positional, 35 cl::desc("<input .llvm file>"), 36 cl::init("-")); 37 38 static cl::opt<std::string> OutputFilename("o", 39 cl::desc("Override output filename"), 40 cl::value_desc("filename")); 41 42 static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals")); 43 44 static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"), 45 cl::init(false)); 46 47 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), 48 cl::init(false)); 49 50 static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"), 51 cl::Hidden); 52 53 static cl::opt<bool> 54 DisableVerify("disable-verify", cl::Hidden, 55 cl::desc("Do not run verifier on input LLVM (dangerous!)")); 56 57 static cl::opt<bool> PreserveBitcodeUseListOrder( 58 "preserve-bc-uselistorder", 59 cl::desc("Preserve use-list order when writing LLVM bitcode."), 60 cl::init(true), cl::Hidden); 61 62 static cl::opt<std::string> ClDataLayout("data-layout", 63 cl::desc("data layout string to use"), 64 cl::value_desc("layout-string"), 65 cl::init("")); 66 67 static void WriteOutputFile(const Module *M) { 68 // Infer the output filename if needed. 69 if (OutputFilename.empty()) { 70 if (InputFilename == "-") { 71 OutputFilename = "-"; 72 } else { 73 StringRef IFN = InputFilename; 74 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str(); 75 OutputFilename += ".bc"; 76 } 77 } 78 79 std::error_code EC; 80 std::unique_ptr<ToolOutputFile> Out( 81 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); 82 if (EC) { 83 errs() << EC.message() << '\n'; 84 exit(1); 85 } 86 87 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) 88 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder, nullptr, 89 EmitModuleHash); 90 91 // Declare success. 92 Out->keep(); 93 } 94 95 int main(int argc, char **argv) { 96 // Print a stack trace if we signal out. 97 sys::PrintStackTraceOnErrorSignal(argv[0]); 98 PrettyStackTraceProgram X(argc, argv); 99 LLVMContext Context; 100 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 101 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); 102 103 // Parse the file now... 104 SMDiagnostic Err; 105 std::unique_ptr<Module> M = parseAssemblyFile( 106 InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout); 107 if (!M.get()) { 108 Err.print(argv[0], errs()); 109 return 1; 110 } 111 112 if (!DisableVerify) { 113 std::string ErrorStr; 114 raw_string_ostream OS(ErrorStr); 115 if (verifyModule(*M.get(), &OS)) { 116 errs() << argv[0] 117 << ": assembly parsed, but does not verify as correct!\n"; 118 errs() << OS.str(); 119 return 1; 120 } 121 } 122 123 if (DumpAsm) 124 errs() << "Here's the assembly:\n" << *M.get(); 125 126 if (!DisableOutput) 127 WriteOutputFile(M.get()); 128 129 return 0; 130 } 131