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/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/ModuleSummaryIndex.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/InitLLVM.h"
27 #include "llvm/Support/ManagedStatic.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
> InputFilename(cl::Positional
,
35 cl::desc("<input .llvm file>"),
38 static cl::opt
<std::string
> OutputFilename("o",
39 cl::desc("Override output filename"),
40 cl::value_desc("filename"));
42 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"));
44 static cl::opt
<bool> DisableOutput("disable-output", cl::desc("Disable output"),
47 static cl::opt
<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
50 static cl::opt
<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
54 DisableVerify("disable-verify", cl::Hidden
,
55 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
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
);
62 static cl::opt
<std::string
> ClDataLayout("data-layout",
63 cl::desc("data layout string to use"),
64 cl::value_desc("layout-string"),
67 static void WriteOutputFile(const Module
*M
, const ModuleSummaryIndex
*Index
) {
68 // Infer the output filename if needed.
69 if (OutputFilename
.empty()) {
70 if (InputFilename
== "-") {
73 StringRef IFN
= InputFilename
;
74 OutputFilename
= (IFN
.endswith(".ll") ? IFN
.drop_back(3) : IFN
).str();
75 OutputFilename
+= ".bc";
80 std::unique_ptr
<ToolOutputFile
> Out(
81 new ToolOutputFile(OutputFilename
, EC
, sys::fs::F_None
));
83 errs() << EC
.message() << '\n';
87 if (Force
|| !CheckBitcodeOutputToConsole(Out
->os(), true)) {
88 const ModuleSummaryIndex
*IndexToWrite
= nullptr;
89 // Don't attempt to write a summary index unless it contains any entries.
90 // Otherwise we get an empty summary section.
91 if (Index
&& Index
->begin() != Index
->end())
93 if (!IndexToWrite
|| (M
&& (!M
->empty() || !M
->global_empty())))
94 // If we have a non-empty Module, then we write the Module plus
95 // any non-null Index along with it as a per-module Index.
96 // If both are empty, this will give an empty module block, which is
97 // the expected behavior.
98 WriteBitcodeToFile(*M
, Out
->os(), PreserveBitcodeUseListOrder
,
99 IndexToWrite
, EmitModuleHash
);
101 // Otherwise, with an empty Module but non-empty Index, we write a
103 WriteIndexToFile(*IndexToWrite
, Out
->os());
110 int main(int argc
, char **argv
) {
111 InitLLVM
X(argc
, argv
);
113 cl::ParseCommandLineOptions(argc
, argv
, "llvm .ll -> .bc assembler\n");
115 // Parse the file now...
117 auto ModuleAndIndex
= parseAssemblyFileWithIndex(
118 InputFilename
, Err
, Context
, nullptr, !DisableVerify
, ClDataLayout
);
119 std::unique_ptr
<Module
> M
= std::move(ModuleAndIndex
.Mod
);
121 Err
.print(argv
[0], errs());
124 std::unique_ptr
<ModuleSummaryIndex
> Index
= std::move(ModuleAndIndex
.Index
);
126 if (!DisableVerify
) {
127 std::string ErrorStr
;
128 raw_string_ostream
OS(ErrorStr
);
129 if (verifyModule(*M
.get(), &OS
)) {
131 << ": assembly parsed, but does not verify as correct!\n";
135 // TODO: Implement and call summary index verifier.
139 errs() << "Here's the assembly:\n" << *M
.get();
140 if (Index
.get() && Index
->begin() != Index
->end())
141 Index
->print(errs());
145 WriteOutputFile(M
.get(), Index
.get());