1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This utility may be invoked in the following manner:
10 // llvm-as --help - Output information about command line switches
11 // llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout
12 // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
15 //===----------------------------------------------------------------------===//
17 #include "llvm/AsmParser/Parser.h"
18 #include "llvm/Bitcode/BitcodeWriter.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ModuleSummaryIndex.h"
22 #include "llvm/IR/Verifier.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/InitLLVM.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/SystemUtils.h"
29 #include "llvm/Support/ToolOutputFile.h"
33 cl::OptionCategory
AsCat("llvm-as Options");
35 static cl::opt
<std::string
> InputFilename(cl::Positional
,
36 cl::desc("<input .llvm file>"),
39 static cl::opt
<std::string
> OutputFilename("o",
40 cl::desc("Override output filename"),
41 cl::value_desc("filename"),
44 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"),
47 static cl::opt
<bool> DisableOutput("disable-output", cl::desc("Disable output"),
48 cl::init(false), cl::cat(AsCat
));
50 static cl::opt
<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
51 cl::init(false), cl::cat(AsCat
));
53 static cl::opt
<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
54 cl::Hidden
, cl::cat(AsCat
));
57 DisableVerify("disable-verify", cl::Hidden
,
58 cl::desc("Do not run verifier on input LLVM (dangerous!)"),
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
, cl::cat(AsCat
));
66 static cl::opt
<std::string
> ClDataLayout("data-layout",
67 cl::desc("data layout string to use"),
68 cl::value_desc("layout-string"),
69 cl::init(""), cl::cat(AsCat
));
71 static void WriteOutputFile(const Module
*M
, const ModuleSummaryIndex
*Index
) {
72 // Infer the output filename if needed.
73 if (OutputFilename
.empty()) {
74 if (InputFilename
== "-") {
77 StringRef IFN
= InputFilename
;
78 OutputFilename
= (IFN
.endswith(".ll") ? IFN
.drop_back(3) : IFN
).str();
79 OutputFilename
+= ".bc";
84 std::unique_ptr
<ToolOutputFile
> Out(
85 new ToolOutputFile(OutputFilename
, EC
, sys::fs::OF_None
));
87 errs() << EC
.message() << '\n';
91 if (Force
|| !CheckBitcodeOutputToConsole(Out
->os(), true)) {
92 const ModuleSummaryIndex
*IndexToWrite
= nullptr;
93 // Don't attempt to write a summary index unless it contains any entries.
94 // Otherwise we get an empty summary section.
95 if (Index
&& Index
->begin() != Index
->end())
97 if (!IndexToWrite
|| (M
&& (!M
->empty() || !M
->global_empty())))
98 // If we have a non-empty Module, then we write the Module plus
99 // any non-null Index along with it as a per-module Index.
100 // If both are empty, this will give an empty module block, which is
101 // the expected behavior.
102 WriteBitcodeToFile(*M
, Out
->os(), PreserveBitcodeUseListOrder
,
103 IndexToWrite
, EmitModuleHash
);
105 // Otherwise, with an empty Module but non-empty Index, we write a
107 WriteIndexToFile(*IndexToWrite
, Out
->os());
114 int main(int argc
, char **argv
) {
115 InitLLVM
X(argc
, argv
);
117 cl::HideUnrelatedOptions(AsCat
);
118 cl::ParseCommandLineOptions(argc
, argv
, "llvm .ll -> .bc assembler\n");
120 // Parse the file now...
122 auto ModuleAndIndex
= parseAssemblyFileWithIndex(
123 InputFilename
, Err
, Context
, nullptr, !DisableVerify
, ClDataLayout
);
124 std::unique_ptr
<Module
> M
= std::move(ModuleAndIndex
.Mod
);
126 Err
.print(argv
[0], errs());
129 std::unique_ptr
<ModuleSummaryIndex
> Index
= std::move(ModuleAndIndex
.Index
);
131 if (!DisableVerify
) {
132 std::string ErrorStr
;
133 raw_string_ostream
OS(ErrorStr
);
134 if (verifyModule(*M
.get(), &OS
)) {
136 << ": assembly parsed, but does not verify as correct!\n";
140 // TODO: Implement and call summary index verifier.
144 errs() << "Here's the assembly:\n" << *M
.get();
145 if (Index
.get() && Index
->begin() != Index
->end())
146 Index
->print(errs());
150 WriteOutputFile(M
.get(), Index
.get());