Chris Lattner | 10cf8fa | 2003-10-20 17:52:11 +0000 | [diff] [blame] | 1 | //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This utility may be invoked in the following manner: |
Misha Brukman | cbb62dd | 2003-08-28 21:32:57 +0000 | [diff] [blame] | 11 | // llvm-as --help - Output information about command line switches |
Chris Lattner | 44dadff | 2007-05-06 09:29:57 +0000 | [diff] [blame] | 12 | // llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout |
| 13 | // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode |
Misha Brukman | cbb62dd | 2003-08-28 21:32:57 +0000 | [diff] [blame] | 14 | // to the x.bc file. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 15 | // |
Chris Lattner | 6d1727c | 2006-05-29 18:52:52 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | bc65a8d | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 18 | #include "llvm/AsmParser/Parser.h" |
Teresa Johnson | a547919 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/BitcodeWriter.h" |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 22 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chandler Carruth | 56e1394 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Verifier.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 7259f14 | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 26 | #include "llvm/Support/InitLLVM.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 92bcb42 | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 28 | #include "llvm/Support/SourceMgr.h" |
Reid Spencer | b687c0c | 2005-01-02 00:08:46 +0000 | [diff] [blame] | 29 | #include "llvm/Support/SystemUtils.h" |
Dan Gohman | e4f1a9b | 2010-10-07 20:32:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ToolOutputFile.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 31 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 34 | static cl::opt<std::string> InputFilename(cl::Positional, |
| 35 | cl::desc("<input .llvm file>"), |
| 36 | cl::init("-")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 37 | |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 38 | static cl::opt<std::string> OutputFilename("o", |
| 39 | cl::desc("Override output filename"), |
| 40 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 41 | |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 42 | static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 43 | |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 44 | static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"), |
| 45 | cl::init(false)); |
Devang Patel | ef00f9d | 2008-02-21 01:41:25 +0000 | [diff] [blame] | 46 | |
Mehdi Amini | d2f4701 | 2016-04-01 05:33:11 +0000 | [diff] [blame] | 47 | static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), |
| 48 | cl::init(false)); |
| 49 | |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 50 | static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"), |
| 51 | cl::Hidden); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 53 | static cl::opt<bool> |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 54 | DisableVerify("disable-verify", cl::Hidden, |
| 55 | cl::desc("Do not run verifier on input LLVM (dangerous!)")); |
Chris Lattner | 04aa29d | 2003-08-18 20:47:13 +0000 | [diff] [blame] | 56 | |
Duncan P. N. Exon Smith | 8d61ee9 | 2015-04-15 03:14:06 +0000 | [diff] [blame] | 57 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 58 | "preserve-bc-uselistorder", |
| 59 | cl::desc("Preserve use-list order when writing LLVM bitcode."), |
| 60 | cl::init(true), cl::Hidden); |
| 61 | |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 62 | static cl::opt<std::string> ClDataLayout("data-layout", |
| 63 | cl::desc("data layout string to use"), |
| 64 | cl::value_desc("layout-string"), |
| 65 | cl::init("")); |
| 66 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 67 | static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) { |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 68 | // Infer the output filename if needed. |
| 69 | if (OutputFilename.empty()) { |
| 70 | if (InputFilename == "-") { |
| 71 | OutputFilename = "-"; |
| 72 | } else { |
Alexey Samsonov | 4e5be5a | 2015-05-11 21:20:20 +0000 | [diff] [blame] | 73 | StringRef IFN = InputFilename; |
| 74 | OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str(); |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 75 | OutputFilename += ".bc"; |
| 76 | } |
| 77 | } |
Dan Gohman | ec08046 | 2009-09-11 20:46:33 +0000 | [diff] [blame] | 78 | |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 79 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 80 | std::unique_ptr<ToolOutputFile> Out( |
| 81 | new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); |
Rafael Espindola | 8c96862 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 82 | if (EC) { |
| 83 | errs() << EC.message() << '\n'; |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 84 | exit(1); |
| 85 | } |
| 86 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 87 | if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) { |
| 88 | const ModuleSummaryIndex *IndexToWrite = nullptr; |
| 89 | // Don't attempt to write a summary index unless it contains any entries. |
| 90 | // Otherwise we get an empty summary section. |
| 91 | if (Index && Index->begin() != Index->end()) |
| 92 | IndexToWrite = Index; |
| 93 | if (!IndexToWrite || (M && (!M->empty() || !M->global_empty()))) |
| 94 | // If we have a non-empty Module, then we write the Module plus |
| 95 | // any non-null Index along with it as a per-module Index. |
| 96 | // If both are empty, this will give an empty module block, which is |
| 97 | // the expected behavior. |
| 98 | WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder, |
| 99 | IndexToWrite, EmitModuleHash); |
| 100 | else |
| 101 | // Otherwise, with an empty Module but non-empty Index, we write a |
| 102 | // combined index. |
| 103 | WriteIndexToFile(*IndexToWrite, Out->os()); |
| 104 | } |
Dan Gohman | d5826a3 | 2010-08-20 01:07:01 +0000 | [diff] [blame] | 105 | |
| 106 | // Declare success. |
| 107 | Out->keep(); |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 111 | InitLLVM X(argc, argv); |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 112 | LLVMContext Context; |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 113 | cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); |
| 114 | |
| 115 | // Parse the file now... |
| 116 | SMDiagnostic Err; |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 117 | auto ModuleAndIndex = parseAssemblyFileWithIndex( |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 118 | InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout); |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 119 | std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod); |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 120 | if (!M.get()) { |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 121 | Err.print(argv[0], errs()); |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 122 | return 1; |
| 123 | } |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 124 | std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index); |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 126 | if (!DisableVerify) { |
Chandler Carruth | e608d69 | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 127 | std::string ErrorStr; |
| 128 | raw_string_ostream OS(ErrorStr); |
| 129 | if (verifyModule(*M.get(), &OS)) { |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 130 | errs() << argv[0] |
| 131 | << ": assembly parsed, but does not verify as correct!\n"; |
Chandler Carruth | e608d69 | 2014-01-19 02:22:18 +0000 | [diff] [blame] | 132 | errs() << OS.str(); |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 133 | return 1; |
| 134 | } |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 135 | // TODO: Implement and call summary index verifier. |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 138 | if (DumpAsm) { |
Teresa Johnson | 566a35a | 2016-04-04 21:06:17 +0000 | [diff] [blame] | 139 | errs() << "Here's the assembly:\n" << *M.get(); |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 140 | if (Index.get() && Index->begin() != Index->end()) |
| 141 | Index->print(errs()); |
| 142 | } |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 144 | if (!DisableOutput) |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 145 | WriteOutputFile(M.get(), Index.get()); |
Daniel Dunbar | 79ec717 | 2009-10-17 03:28:28 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 147 | return 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 148 | } |