blob: 8f8d10ff572807db9b88ad77eebf4f6580f85e4b [file] [log] [blame]
Chris Lattner22feb172003-10-20 17:52:11 +00001//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswell09344dc2003-10-20 17:47:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00008//
9// This utility may be invoked in the following manner:
Misha Brukman98930272003-08-28 21:32:57 +000010// llvm-as --help - Output information about command line switches
Chris Lattner6d80e212007-05-06 09:29:57 +000011// 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 Brukman98930272003-08-28 21:32:57 +000013// to the x.bc file.
Misha Brukman650ba8e2005-04-22 00:00:37 +000014//
Chris Lattnerd6592892006-05-29 18:52:52 +000015//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000016
Chandler Carruth9aca9182014-01-07 12:34:26 +000017#include "llvm/AsmParser/Parser.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000018#include "llvm/Bitcode/BitcodeWriter.h"
Teresa Johnson7ddec632016-04-04 21:06:17 +000019#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Module.h"
Teresa Johnson63ee0e72018-06-26 13:56:49 +000021#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000022#include "llvm/IR/Verifier.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000024#include "llvm/Support/FileSystem.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000025#include "llvm/Support/InitLLVM.h"
Chris Lattner76d46322006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Chris Lattnera76611a2009-07-02 22:46:18 +000027#include "llvm/Support/SourceMgr.h"
Reid Spencer742af2f2005-01-02 00:08:46 +000028#include "llvm/Support/SystemUtils.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000029#include "llvm/Support/ToolOutputFile.h"
Chris Lattner7f74a562002-01-20 22:54:45 +000030#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000031using namespace llvm;
32
Teresa Johnson7ddec632016-04-04 21:06:17 +000033static cl::opt<std::string> InputFilename(cl::Positional,
34 cl::desc("<input .llvm file>"),
35 cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000036
Teresa Johnson7ddec632016-04-04 21:06:17 +000037static cl::opt<std::string> OutputFilename("o",
38 cl::desc("Override output filename"),
39 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000040
Teresa Johnson7ddec632016-04-04 21:06:17 +000041static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000042
Teresa Johnson7ddec632016-04-04 21:06:17 +000043static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),
44 cl::init(false));
Devang Patelc6f915e2008-02-21 01:41:25 +000045
Mehdi Aminid7ad2212016-04-01 05:33:11 +000046static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
47 cl::init(false));
48
Teresa Johnson7ddec632016-04-04 21:06:17 +000049static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
50 cl::Hidden);
Chris Lattner2f7c9632001-06-06 20:29:01 +000051
Misha Brukman650ba8e2005-04-22 00:00:37 +000052static cl::opt<bool>
Teresa Johnson7ddec632016-04-04 21:06:17 +000053 DisableVerify("disable-verify", cl::Hidden,
54 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner016ccdf2003-08-18 20:47:13 +000055
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +000056static 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 Liuc00d81e2018-01-30 22:32:39 +000061static 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 Johnson63ee0e72018-06-26 13:56:49 +000066static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) {
Chris Lattner9e6f1f12009-08-23 02:51:22 +000067 // Infer the output filename if needed.
68 if (OutputFilename.empty()) {
69 if (InputFilename == "-") {
70 OutputFilename = "-";
71 } else {
Alexey Samsonovd9235082015-05-11 21:20:20 +000072 StringRef IFN = InputFilename;
73 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
Chris Lattner9e6f1f12009-08-23 02:51:22 +000074 OutputFilename += ".bc";
75 }
76 }
Dan Gohmane5929232009-09-11 20:46:33 +000077
Rafael Espindola3fd1e992014-08-25 18:16:47 +000078 std::error_code EC;
Reid Kleckner3fc649c2017-09-23 01:03:17 +000079 std::unique_ptr<ToolOutputFile> Out(
80 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
Rafael Espindola3fd1e992014-08-25 18:16:47 +000081 if (EC) {
82 errs() << EC.message() << '\n';
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000083 exit(1);
84 }
85
Teresa Johnson63ee0e72018-06-26 13:56:49 +000086 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 Gohman268b0f42010-08-20 01:07:01 +0000104
105 // Declare success.
106 Out->keep();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000107}
108
109int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000110 InitLLVM X(argc, argv);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000111 LLVMContext Context;
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000112 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
113
114 // Parse the file now...
115 SMDiagnostic Err;
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000116 auto ModuleAndIndex = parseAssemblyFileWithIndex(
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000117 InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout);
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000118 std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000119 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000120 Err.print(argv[0], errs());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000121 return 1;
122 }
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000123 std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index);
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000124
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000125 if (!DisableVerify) {
Chandler Carruth043949d2014-01-19 02:22:18 +0000126 std::string ErrorStr;
127 raw_string_ostream OS(ErrorStr);
128 if (verifyModule(*M.get(), &OS)) {
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000129 errs() << argv[0]
130 << ": assembly parsed, but does not verify as correct!\n";
Chandler Carruth043949d2014-01-19 02:22:18 +0000131 errs() << OS.str();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000132 return 1;
133 }
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000134 // TODO: Implement and call summary index verifier.
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000135 }
136
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000137 if (DumpAsm) {
Teresa Johnson7ddec632016-04-04 21:06:17 +0000138 errs() << "Here's the assembly:\n" << *M.get();
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000139 if (Index.get() && Index->begin() != Index->end())
140 Index->print(errs());
141 }
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000142
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000143 if (!DisableOutput)
Teresa Johnson63ee0e72018-06-26 13:56:49 +0000144 WriteOutputFile(M.get(), Index.get());
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000145
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000146 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147}