1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
16 //===----------------------------------------------------------------------===//
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/AsmParser/Parser.h"
20 #include "llvm/Bitcode/ReaderWriter.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"
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
42 Force("f", cl::desc("Enable binary output on terminals"));
45 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
48 EmitFunctionSummary("function-summary", cl::desc("Emit function summary index"),
52 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
55 DisableVerify("disable-verify", cl::Hidden,
56 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
58 static cl::opt<bool> PreserveBitcodeUseListOrder(
59 "preserve-bc-uselistorder",
60 cl::desc("Preserve use-list order when writing LLVM bitcode."),
61 cl::init(true), cl::Hidden);
63 static void WriteOutputFile(const Module *M) {
64 // Infer the output filename if needed.
65 if (OutputFilename.empty()) {
66 if (InputFilename == "-") {
69 StringRef IFN = InputFilename;
70 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
71 OutputFilename += ".bc";
76 std::unique_ptr<tool_output_file> Out(
77 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
79 errs() << EC.message() << '\n';
83 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
84 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder,
91 int main(int argc, char **argv) {
92 // Print a stack trace if we signal out.
93 sys::PrintStackTraceOnErrorSignal();
94 PrettyStackTraceProgram X(argc, argv);
95 LLVMContext &Context = getGlobalContext();
96 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
97 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
99 // Parse the file now...
101 std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
103 Err.print(argv[0], errs());
107 if (!DisableVerify) {
108 std::string ErrorStr;
109 raw_string_ostream OS(ErrorStr);
110 if (verifyModule(*M.get(), &OS)) {
112 << ": assembly parsed, but does not verify as correct!\n";
118 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
121 WriteOutputFile(M.get());