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/ReaderWriter.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> EmitSummaryIndex("module-summary", 48 cl::desc("Emit module summary index"), 49 cl::init(false)); 50 51 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), 52 cl::init(false)); 53 54 static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"), 55 cl::Hidden); 56 57 static cl::opt<bool> 58 DisableVerify("disable-verify", cl::Hidden, 59 cl::desc("Do not run verifier on input LLVM (dangerous!)")); 60 61 static cl::opt<bool> PreserveBitcodeUseListOrder( 62 "preserve-bc-uselistorder", 63 cl::desc("Preserve use-list order when writing LLVM bitcode."), 64 cl::init(true), cl::Hidden); 65 66 static void WriteOutputFile(const Module *M) { 67 // Infer the output filename if needed. 68 if (OutputFilename.empty()) { 69 if (InputFilename == "-") { 70 OutputFilename = "-"; 71 } else { 72 StringRef IFN = InputFilename; 73 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str(); 74 OutputFilename += ".bc"; 75 } 76 } 77 78 std::error_code EC; 79 std::unique_ptr<tool_output_file> Out( 80 new tool_output_file(OutputFilename, EC, sys::fs::F_None)); 81 if (EC) { 82 errs() << EC.message() << '\n'; 83 exit(1); 84 } 85 86 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) 87 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder, 88 EmitSummaryIndex, EmitModuleHash); 89 90 // Declare success. 91 Out->keep(); 92 } 93 94 int main(int argc, char **argv) { 95 // Print a stack trace if we signal out. 96 sys::PrintStackTraceOnErrorSignal(); 97 PrettyStackTraceProgram X(argc, argv); 98 LLVMContext &Context = getGlobalContext(); 99 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 100 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); 101 102 // Parse the file now... 103 SMDiagnostic Err; 104 std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context); 105 if (!M.get()) { 106 Err.print(argv[0], errs()); 107 return 1; 108 } 109 110 if (!DisableVerify) { 111 std::string ErrorStr; 112 raw_string_ostream OS(ErrorStr); 113 if (verifyModule(*M.get(), &OS)) { 114 errs() << argv[0] 115 << ": assembly parsed, but does not verify as correct!\n"; 116 errs() << OS.str(); 117 return 1; 118 } 119 } 120 121 if (DumpAsm) 122 errs() << "Here's the assembly:\n" << *M.get(); 123 124 if (!DisableOutput) 125 WriteOutputFile(M.get()); 126 127 return 0; 128 } 129