1 | //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
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 |
13 | // to the x.bc file. |
14 | // |
15 | //===----------------------------------------------------------------------===// |
16 | |
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" |
29 | #include <memory> |
30 | #include <optional> |
31 | using namespace llvm; |
32 | |
33 | cl::OptionCategory AsCat("llvm-as Options" ); |
34 | |
35 | static cl::opt<std::string> InputFilename(cl::Positional, |
36 | cl::desc("<input .llvm file>" ), |
37 | cl::init(Val: "-" )); |
38 | |
39 | static cl::opt<std::string> OutputFilename("o" , |
40 | cl::desc("Override output filename" ), |
41 | cl::value_desc("filename" ), |
42 | cl::cat(AsCat)); |
43 | |
44 | static cl::opt<bool> Force("f" , cl::desc("Enable binary output on terminals" ), |
45 | cl::cat(AsCat)); |
46 | |
47 | static cl::opt<bool> DisableOutput("disable-output" , cl::desc("Disable output" ), |
48 | cl::init(Val: false), cl::cat(AsCat)); |
49 | |
50 | static cl::opt<bool> EmitModuleHash("module-hash" , cl::desc("Emit module hash" ), |
51 | cl::init(Val: false), cl::cat(AsCat)); |
52 | |
53 | static cl::opt<bool> DumpAsm("d" , cl::desc("Print assembly as parsed" ), |
54 | cl::Hidden, cl::cat(AsCat)); |
55 | |
56 | static cl::opt<bool> |
57 | DisableVerify("disable-verify" , cl::Hidden, |
58 | cl::desc("Do not run verifier on input LLVM (dangerous!)" ), |
59 | cl::cat(AsCat)); |
60 | |
61 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
62 | "preserve-bc-uselistorder" , |
63 | cl::desc("Preserve use-list order when writing LLVM bitcode." ), |
64 | cl::init(Val: true), cl::Hidden, cl::cat(AsCat)); |
65 | |
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(Val: "" ), cl::cat(AsCat)); |
70 | extern bool WriteNewDbgInfoFormatToBitcode; |
71 | |
72 | static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) { |
73 | // Infer the output filename if needed. |
74 | if (OutputFilename.empty()) { |
75 | if (InputFilename == "-" ) { |
76 | OutputFilename = "-" ; |
77 | } else { |
78 | StringRef IFN = InputFilename; |
79 | OutputFilename = (IFN.ends_with(Suffix: ".ll" ) ? IFN.drop_back(N: 3) : IFN).str(); |
80 | OutputFilename += ".bc" ; |
81 | } |
82 | } |
83 | |
84 | std::error_code EC; |
85 | std::unique_ptr<ToolOutputFile> Out( |
86 | new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None)); |
87 | if (EC) { |
88 | errs() << EC.message() << '\n'; |
89 | exit(status: 1); |
90 | } |
91 | |
92 | if (Force || !CheckBitcodeOutputToConsole(stream_to_check&: 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 |
97 | // summary section. |
98 | if (Index && (Index->begin() != Index->end() || Index->getFlags())) |
99 | IndexToWrite = Index; |
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: *M, Out&: Out->os(), ShouldPreserveUseListOrder: PreserveBitcodeUseListOrder, |
106 | Index: IndexToWrite, GenerateHash: EmitModuleHash); |
107 | else |
108 | // Otherwise, with an empty Module but non-empty Index, we write a |
109 | // combined index. |
110 | writeIndexToFile(Index: *IndexToWrite, Out&: Out->os()); |
111 | } |
112 | |
113 | // Declare success. |
114 | Out->keep(); |
115 | } |
116 | |
117 | int main(int argc, char **argv) { |
118 | InitLLVM X(argc, argv); |
119 | cl::HideUnrelatedOptions(Category&: AsCat); |
120 | cl::ParseCommandLineOptions(argc, argv, Overview: "llvm .ll -> .bc assembler\n" ); |
121 | LLVMContext Context; |
122 | |
123 | // Parse the file now... |
124 | SMDiagnostic Err; |
125 | auto SetDataLayout = [](StringRef, StringRef) -> std::optional<std::string> { |
126 | if (ClDataLayout.empty()) |
127 | return std::nullopt; |
128 | return ClDataLayout; |
129 | }; |
130 | ParsedModuleAndIndex ModuleAndIndex; |
131 | if (DisableVerify) { |
132 | ModuleAndIndex = parseAssemblyFileWithIndexNoUpgradeDebugInfo( |
133 | Filename: InputFilename, Err, Context, Slots: nullptr, DataLayoutCallback: SetDataLayout); |
134 | } else { |
135 | ModuleAndIndex = parseAssemblyFileWithIndex(Filename: InputFilename, Err, Context, |
136 | Slots: nullptr, DataLayoutCallback: SetDataLayout); |
137 | } |
138 | std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod); |
139 | if (!M.get()) { |
140 | Err.print(ProgName: argv[0], S&: errs()); |
141 | return 1; |
142 | } |
143 | |
144 | // Convert to new debug format if requested. |
145 | assert(!M->IsNewDbgInfoFormat && "Unexpectedly in new debug mode" ); |
146 | if (UseNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode) { |
147 | M->convertToNewDbgValues(); |
148 | M->removeDebugIntrinsicDeclarations(); |
149 | } |
150 | |
151 | std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index); |
152 | |
153 | if (!DisableVerify) { |
154 | std::string ErrorStr; |
155 | raw_string_ostream OS(ErrorStr); |
156 | if (verifyModule(M: *M.get(), OS: &OS)) { |
157 | errs() << argv[0] |
158 | << ": assembly parsed, but does not verify as correct!\n" ; |
159 | errs() << OS.str(); |
160 | return 1; |
161 | } |
162 | // TODO: Implement and call summary index verifier. |
163 | } |
164 | |
165 | if (DumpAsm) { |
166 | errs() << "Here's the assembly:\n" << *M.get(); |
167 | if (Index.get() && Index->begin() != Index->end()) |
168 | Index->print(OS&: errs()); |
169 | } |
170 | |
171 | if (!DisableOutput) |
172 | WriteOutputFile(M: M.get(), Index: Index.get()); |
173 | |
174 | return 0; |
175 | } |
176 | |