Chris Lattner | 22feb17 | 2003-10-20 17:52:11 +0000 | [diff] [blame] | 1 | //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 6 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 8 | // |
| 9 | // This utility may be invoked in the following manner: |
Misha Brukman | 9893027 | 2003-08-28 21:32:57 +0000 | [diff] [blame] | 10 | // llvm-as --help - Output information about command line switches |
Chris Lattner | 6d80e21 | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 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 |
Misha Brukman | 9893027 | 2003-08-28 21:32:57 +0000 | [diff] [blame] | 13 | // to the x.bc file. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | d659289 | 2006-05-29 18:52:52 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | |
Chandler Carruth | 9aca918 | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 17 | #include "llvm/AsmParser/Parser.h" |
Teresa Johnson | ad17679 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/BitcodeWriter.h" |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 21 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Verifier.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | 76d4632 | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | a76611a | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SourceMgr.h" |
Reid Spencer | 742af2f | 2005-01-02 00:08:46 +0000 | [diff] [blame] | 28 | #include "llvm/Support/SystemUtils.h" |
Dan Gohman | 0df7ea4 | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ToolOutputFile.h" |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 30 | #include <memory> |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 33 | static cl::opt<std::string> InputFilename(cl::Positional, |
| 34 | cl::desc("<input .llvm file>"), |
| 35 | cl::init("-")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 36 | |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 37 | static cl::opt<std::string> OutputFilename("o", |
| 38 | cl::desc("Override output filename"), |
| 39 | cl::value_desc("filename")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 40 | |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 41 | static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 42 | |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 43 | static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"), |
| 44 | cl::init(false)); |
Devang Patel | c6f915e | 2008-02-21 01:41:25 +0000 | [diff] [blame] | 45 | |
Mehdi Amini | d7ad221 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 46 | static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), |
| 47 | cl::init(false)); |
| 48 | |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 49 | static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"), |
| 50 | cl::Hidden); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 53 | DisableVerify("disable-verify", cl::Hidden, |
| 54 | cl::desc("Do not run verifier on input LLVM (dangerous!)")); |
Chris Lattner | 016ccdf | 2003-08-18 20:47:13 +0000 | [diff] [blame] | 55 | |
Duncan P. N. Exon Smith | 8a7b84b | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 56 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 57 | "preserve-bc-uselistorder", |
| 58 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 59 | cl::init(true), cl::Hidden); |
| 60 | |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 61 | static cl::opt<std::string> ClDataLayout("data-layout", |
| 62 | cl::desc("data layout string to use"), |
| 63 | cl::value_desc("layout-string"), |
| 64 | cl::init("")); |
| 65 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 66 | static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) { |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 67 | // Infer the output filename if needed. |
| 68 | if (OutputFilename.empty()) { |
| 69 | if (InputFilename == "-") { |
| 70 | OutputFilename = "-"; |
| 71 | } else { |
Alexey Samsonov | d923508 | 2015-05-11 21:20:20 +0000 | [diff] [blame] | 72 | StringRef IFN = InputFilename; |
| 73 | OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str(); |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 74 | OutputFilename += ".bc"; |
| 75 | } |
| 76 | } |
Dan Gohman | e592923 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 77 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 78 | std::error_code EC; |
Reid Kleckner | 3fc649c | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 79 | std::unique_ptr<ToolOutputFile> Out( |
| 80 | new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 81 | if (EC) { |
| 82 | errs() << EC.message() << '\n'; |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 83 | exit(1); |
| 84 | } |
| 85 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 86 | if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) { |
| 87 | const ModuleSummaryIndex *IndexToWrite = nullptr; |
| 88 | // Don't attempt to write a summary index unless it contains any entries. |
| 89 | // Otherwise we get an empty summary section. |
| 90 | if (Index && Index->begin() != Index->end()) |
| 91 | IndexToWrite = Index; |
| 92 | if (!IndexToWrite || (M && (!M->empty() || !M->global_empty()))) |
| 93 | // If we have a non-empty Module, then we write the Module plus |
| 94 | // any non-null Index along with it as a per-module Index. |
| 95 | // If both are empty, this will give an empty module block, which is |
| 96 | // the expected behavior. |
| 97 | WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder, |
| 98 | IndexToWrite, EmitModuleHash); |
| 99 | else |
| 100 | // Otherwise, with an empty Module but non-empty Index, we write a |
| 101 | // combined index. |
| 102 | WriteIndexToFile(*IndexToWrite, Out->os()); |
| 103 | } |
Dan Gohman | 268b0f4 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 104 | |
| 105 | // Declare success. |
| 106 | Out->keep(); |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 110 | InitLLVM X(argc, argv); |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 111 | LLVMContext Context; |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 112 | cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); |
| 113 | |
| 114 | // Parse the file now... |
| 115 | SMDiagnostic Err; |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 116 | auto ModuleAndIndex = parseAssemblyFileWithIndex( |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 117 | InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout); |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 118 | std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 119 | if (!M.get()) { |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 120 | Err.print(argv[0], errs()); |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 121 | return 1; |
| 122 | } |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 123 | std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index); |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 124 | |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 125 | if (!DisableVerify) { |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 126 | std::string ErrorStr; |
| 127 | raw_string_ostream OS(ErrorStr); |
| 128 | if (verifyModule(*M.get(), &OS)) { |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 129 | errs() << argv[0] |
| 130 | << ": assembly parsed, but does not verify as correct!\n"; |
Chandler Carruth | 043949d | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 131 | errs() << OS.str(); |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 132 | return 1; |
| 133 | } |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 134 | // TODO: Implement and call summary index verifier. |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 137 | if (DumpAsm) { |
Teresa Johnson | 7ddec63 | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 138 | errs() << "Here's the assembly:\n" << *M.get(); |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 139 | if (Index.get() && Index->begin() != Index->end()) |
| 140 | Index->print(errs()); |
| 141 | } |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 143 | if (!DisableOutput) |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 144 | WriteOutputFile(M.get(), Index.get()); |
Daniel Dunbar | 2bdc1a5 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 9e6f1f1 | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 146 | return 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 147 | } |