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/SourceMgr.h"
27 #include "llvm/Support/SystemUtils.h"
28 #include "llvm/Support/ToolOutputFile.h"
33 cl::OptionCategory
AsCat("llvm-as Options");
35 static cl::opt
<std::string
>
36 InputFilename(cl::Positional
, cl::desc("<input .ll file>"), cl::init("-"));
38 static cl::opt
<std::string
> OutputFilename("o",
39 cl::desc("Override output filename"),
40 cl::value_desc("filename"),
43 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"),
46 static cl::opt
<bool> DisableOutput("disable-output", cl::desc("Disable output"),
47 cl::init(false), cl::cat(AsCat
));
49 static cl::opt
<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
50 cl::init(false), cl::cat(AsCat
));
52 static cl::opt
<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
53 cl::Hidden
, cl::cat(AsCat
));
56 DisableVerify("disable-verify", cl::Hidden
,
57 cl::desc("Do not run verifier on input LLVM (dangerous!)"),
60 static cl::opt
<bool> PreserveBitcodeUseListOrder(
61 "preserve-bc-uselistorder",
62 cl::desc("Preserve use-list order when writing LLVM bitcode."),
63 cl::init(true), cl::Hidden
, cl::cat(AsCat
));
65 static cl::opt
<std::string
> ClDataLayout("data-layout",
66 cl::desc("data layout string to use"),
67 cl::value_desc("layout-string"),
68 cl::init(""), cl::cat(AsCat
));
69 extern cl::opt
<bool> UseNewDbgInfoFormat
;
70 extern bool WriteNewDbgInfoFormatToBitcode
;
72 static void WriteOutputFile(const Module
*M
, const ModuleSummaryIndex
*Index
) {
73 // Infer the output filename if needed.
74 if (OutputFilename
.empty()) {
75 if (InputFilename
== "-") {
78 StringRef IFN
= InputFilename
;
79 OutputFilename
= (IFN
.ends_with(".ll") ? IFN
.drop_back(3) : IFN
).str();
80 OutputFilename
+= ".bc";
85 std::unique_ptr
<ToolOutputFile
> Out(
86 new ToolOutputFile(OutputFilename
, EC
, sys::fs::OF_None
));
88 errs() << EC
.message() << '\n';
92 if (Force
|| !CheckBitcodeOutputToConsole(Out
->os())) {
93 const ModuleSummaryIndex
*IndexToWrite
= nullptr;
94 // Don't attempt to write a summary index unless it contains any entries or
95 // has non-zero flags. The latter is used to assemble dummy index files for
96 // skipping modules by distributed ThinLTO backends. Otherwise we get an empty
98 if (Index
&& (Index
->begin() != Index
->end() || Index
->getFlags()))
100 if (!IndexToWrite
|| (M
&& (!M
->empty() || !M
->global_empty())))
101 // If we have a non-empty Module, then we write the Module plus
102 // any non-null Index along with it as a per-module Index.
103 // If both are empty, this will give an empty module block, which is
104 // the expected behavior.
105 WriteBitcodeToFile(*M
, Out
->os(), PreserveBitcodeUseListOrder
,
106 IndexToWrite
, EmitModuleHash
);
108 // Otherwise, with an empty Module but non-empty Index, we write a
110 writeIndexToFile(*IndexToWrite
, Out
->os());
117 int main(int argc
, char **argv
) {
118 InitLLVM
X(argc
, argv
);
119 cl::HideUnrelatedOptions(AsCat
);
120 cl::ParseCommandLineOptions(argc
, argv
, "llvm .ll -> .bc assembler\n");
123 // Parse the file now...
125 auto SetDataLayout
= [](StringRef
, StringRef
) -> std::optional
<std::string
> {
126 if (ClDataLayout
.empty())
130 ParsedModuleAndIndex ModuleAndIndex
;
132 ModuleAndIndex
= parseAssemblyFileWithIndexNoUpgradeDebugInfo(
133 InputFilename
, Err
, Context
, nullptr, SetDataLayout
);
135 ModuleAndIndex
= parseAssemblyFileWithIndex(InputFilename
, Err
, Context
,
136 nullptr, SetDataLayout
);
138 std::unique_ptr
<Module
> M
= std::move(ModuleAndIndex
.Mod
);
140 Err
.print(argv
[0], errs());
144 // Convert to new debug format if requested.
145 M
->setIsNewDbgInfoFormat(UseNewDbgInfoFormat
&&
146 WriteNewDbgInfoFormatToBitcode
);
147 if (M
->IsNewDbgInfoFormat
)
148 M
->removeDebugIntrinsicDeclarations();
150 std::unique_ptr
<ModuleSummaryIndex
> Index
= std::move(ModuleAndIndex
.Index
);
152 if (!DisableVerify
) {
153 std::string ErrorStr
;
154 raw_string_ostream
OS(ErrorStr
);
155 if (verifyModule(*M
, &OS
)) {
157 << ": assembly parsed, but does not verify as correct!\n";
161 // TODO: Implement and call summary index verifier.
165 errs() << "Here's the assembly:\n" << *M
;
166 if (Index
.get() && Index
->begin() != Index
->end())
167 Index
->print(errs());
171 WriteOutputFile(M
.get(), Index
.get());