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/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"
31 #include <memory>
32 using namespace llvm;
33 
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
36 
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39                cl::value_desc("filename"));
40 
41 static cl::opt<bool>
42 Force("f", cl::desc("Enable binary output on terminals"));
43 
44 static cl::opt<bool>
45 DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
46 
47 static cl::opt<bool>
48 EmitFunctionSummary("function-summary", cl::desc("Emit function summary index"),
49                     cl::init(false));
50 
51 static cl::opt<bool>
52 DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
53 
54 static cl::opt<bool>
55 DisableVerify("disable-verify", cl::Hidden,
56               cl::desc("Do not run verifier on input LLVM (dangerous!)"));
57 
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);
62 
WriteOutputFile(const Module * M)63 static void WriteOutputFile(const Module *M) {
64   // Infer the output filename if needed.
65   if (OutputFilename.empty()) {
66     if (InputFilename == "-") {
67       OutputFilename = "-";
68     } else {
69       StringRef IFN = InputFilename;
70       OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
71       OutputFilename += ".bc";
72     }
73   }
74 
75   std::error_code EC;
76   std::unique_ptr<tool_output_file> Out(
77       new tool_output_file(OutputFilename, EC, sys::fs::F_None));
78   if (EC) {
79     errs() << EC.message() << '\n';
80     exit(1);
81   }
82 
83   if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
84     WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder,
85                        EmitFunctionSummary);
86 
87   // Declare success.
88   Out->keep();
89 }
90 
main(int argc,char ** argv)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");
98 
99   // Parse the file now...
100   SMDiagnostic Err;
101   std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
102   if (!M.get()) {
103     Err.print(argv[0], errs());
104     return 1;
105   }
106 
107   if (!DisableVerify) {
108     std::string ErrorStr;
109     raw_string_ostream OS(ErrorStr);
110     if (verifyModule(*M.get(), &OS)) {
111       errs() << argv[0]
112              << ": assembly parsed, but does not verify as correct!\n";
113       errs() << OS.str();
114       return 1;
115     }
116   }
117 
118   if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
119 
120   if (!DisableOutput)
121     WriteOutputFile(M.get());
122 
123   return 0;
124 }
125