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