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 | static cl::OptionCategory AsCat("llvm-as Options" ); |
34 | |
35 | static cl::opt<std::string> |
36 | InputFilename(cl::Positional, cl::desc("<input .ll file>" ), cl::init(Val: "-" )); |
37 | |
38 | static cl::opt<std::string> OutputFilename("o" , |
39 | cl::desc("Override output filename" ), |
40 | cl::value_desc("filename" ), |
41 | cl::cat(AsCat)); |
42 | |
43 | static cl::opt<bool> Force("f" , cl::desc("Enable binary output on terminals" ), |
44 | cl::cat(AsCat)); |
45 | |
46 | static cl::opt<bool> DisableOutput("disable-output" , cl::desc("Disable output" ), |
47 | cl::init(Val: false), cl::cat(AsCat)); |
48 | |
49 | static cl::opt<bool> EmitModuleHash("module-hash" , cl::desc("Emit module hash" ), |
50 | cl::init(Val: false), cl::cat(AsCat)); |
51 | |
52 | static cl::opt<bool> DumpAsm("d" , cl::desc("Print assembly as parsed" ), |
53 | cl::Hidden, cl::cat(AsCat)); |
54 | |
55 | static cl::opt<bool> |
56 | DisableVerify("disable-verify" , cl::Hidden, |
57 | cl::desc("Do not run verifier on input LLVM (dangerous!)" ), |
58 | cl::cat(AsCat)); |
59 | |
60 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
61 | "preserve-bc-uselistorder" , |
62 | cl::desc("Preserve use-list order when writing LLVM bitcode." ), |
63 | cl::init(Val: true), cl::Hidden, cl::cat(AsCat)); |
64 | |
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(Val: "" ), cl::cat(AsCat)); |
69 | |
70 | static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) { |
71 | // Infer the output filename if needed. |
72 | if (OutputFilename.empty()) { |
73 | if (InputFilename == "-" ) { |
74 | OutputFilename = "-" ; |
75 | } else { |
76 | StringRef IFN = InputFilename; |
77 | OutputFilename = (IFN.ends_with(Suffix: ".ll" ) ? IFN.drop_back(N: 3) : IFN).str(); |
78 | OutputFilename += ".bc" ; |
79 | } |
80 | } |
81 | |
82 | std::error_code EC; |
83 | std::unique_ptr<ToolOutputFile> Out( |
84 | new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None)); |
85 | if (EC) { |
86 | errs() << EC.message() << '\n'; |
87 | exit(status: 1); |
88 | } |
89 | |
90 | if (Force || !CheckBitcodeOutputToConsole(stream_to_check&: Out->os())) { |
91 | const ModuleSummaryIndex *IndexToWrite = nullptr; |
92 | // Don't attempt to write a summary index unless it contains any entries or |
93 | // has non-zero flags. The latter is used to assemble dummy index files for |
94 | // skipping modules by distributed ThinLTO backends. Otherwise we get an empty |
95 | // summary section. |
96 | if (Index && (Index->begin() != Index->end() || Index->getFlags())) |
97 | IndexToWrite = Index; |
98 | if (!IndexToWrite || (M && (!M->empty() || !M->global_empty()))) |
99 | // If we have a non-empty Module, then we write the Module plus |
100 | // any non-null Index along with it as a per-module Index. |
101 | // If both are empty, this will give an empty module block, which is |
102 | // the expected behavior. |
103 | WriteBitcodeToFile(M: *M, Out&: Out->os(), ShouldPreserveUseListOrder: PreserveBitcodeUseListOrder, |
104 | Index: IndexToWrite, GenerateHash: EmitModuleHash); |
105 | else |
106 | // Otherwise, with an empty Module but non-empty Index, we write a |
107 | // combined index. |
108 | writeIndexToFile(Index: *IndexToWrite, Out&: Out->os()); |
109 | } |
110 | |
111 | // Declare success. |
112 | Out->keep(); |
113 | } |
114 | |
115 | int main(int argc, char **argv) { |
116 | InitLLVM X(argc, argv); |
117 | cl::HideUnrelatedOptions(Category&: AsCat); |
118 | cl::ParseCommandLineOptions(argc, argv, Overview: "llvm .ll -> .bc assembler\n" ); |
119 | LLVMContext Context; |
120 | |
121 | // Parse the file now... |
122 | SMDiagnostic Err; |
123 | auto SetDataLayout = [](StringRef, StringRef) -> std::optional<std::string> { |
124 | if (ClDataLayout.empty()) |
125 | return std::nullopt; |
126 | return ClDataLayout; |
127 | }; |
128 | ParsedModuleAndIndex ModuleAndIndex; |
129 | if (DisableVerify) { |
130 | ModuleAndIndex = parseAssemblyFileWithIndexNoUpgradeDebugInfo( |
131 | Filename: InputFilename, Err, Context, Slots: nullptr, DataLayoutCallback: SetDataLayout); |
132 | } else { |
133 | ModuleAndIndex = parseAssemblyFileWithIndex(Filename: InputFilename, Err, Context, |
134 | Slots: nullptr, DataLayoutCallback: SetDataLayout); |
135 | } |
136 | std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod); |
137 | if (!M) { |
138 | Err.print(ProgName: argv[0], S&: errs()); |
139 | return 1; |
140 | } |
141 | |
142 | M->removeDebugIntrinsicDeclarations(); |
143 | |
144 | std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index); |
145 | |
146 | if (!DisableVerify) { |
147 | std::string ErrorStr; |
148 | raw_string_ostream OS(ErrorStr); |
149 | if (verifyModule(M: *M, OS: &OS)) { |
150 | errs() << argv[0] |
151 | << ": assembly parsed, but does not verify as correct!\n" ; |
152 | errs() << OS.str(); |
153 | return 1; |
154 | } |
155 | // TODO: Implement and call summary index verifier. |
156 | } |
157 | |
158 | if (DumpAsm) { |
159 | errs() << "Here's the assembly:\n" << *M; |
160 | if (Index.get() && Index->begin() != Index->end()) |
161 | Index->print(OS&: errs()); |
162 | } |
163 | |
164 | if (!DisableOutput) |
165 | WriteOutputFile(M: M.get(), Index: Index.get()); |
166 | |
167 | return 0; |
168 | } |
169 | |