blob: bb4233aa9ba0f831a2a28ac8d868c70bef729790 [file] [log] [blame]
Chris Lattner10cf8fa2003-10-20 17:52:11 +00001//===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Misha Brukmancbb62dd2003-08-28 21:32:57 +000011// llvm-as --help - Output information about command line switches
Chris Lattner44dadff2007-05-06 09:29:57 +000012// 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 Brukmancbb62dd2003-08-28 21:32:57 +000014// to the x.bc file.
Misha Brukman3da94ae2005-04-22 00:00:37 +000015//
Chris Lattner6d1727c2006-05-29 18:52:52 +000016//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000017
Chandler Carruthbc65a8d2014-01-07 12:34:26 +000018#include "llvm/AsmParser/Parser.h"
Teresa Johnsona5479192016-11-11 05:34:58 +000019#include "llvm/Bitcode/BitcodeWriter.h"
Teresa Johnson566a35a2016-04-04 21:06:17 +000020#include "llvm/IR/LLVMContext.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
Teresa Johnsonc6dda902018-06-26 13:56:49 +000022#include "llvm/IR/ModuleSummaryIndex.h"
Chandler Carruth56e13942014-01-13 09:26:24 +000023#include "llvm/IR/Verifier.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Benjamin Kramer7259f142014-04-29 23:26:49 +000025#include "llvm/Support/FileSystem.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000026#include "llvm/Support/InitLLVM.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/ManagedStatic.h"
Chris Lattner92bcb422009-07-02 22:46:18 +000028#include "llvm/Support/SourceMgr.h"
Reid Spencerb687c0c2005-01-02 00:08:46 +000029#include "llvm/Support/SystemUtils.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000030#include "llvm/Support/ToolOutputFile.h"
Chris Lattner697954c2002-01-20 22:54:45 +000031#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000032using namespace llvm;
33
Teresa Johnson566a35a2016-04-04 21:06:17 +000034static cl::opt<std::string> InputFilename(cl::Positional,
35 cl::desc("<input .llvm file>"),
36 cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000037
Teresa Johnson566a35a2016-04-04 21:06:17 +000038static cl::opt<std::string> OutputFilename("o",
39 cl::desc("Override output filename"),
40 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Teresa Johnson566a35a2016-04-04 21:06:17 +000042static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000043
Teresa Johnson566a35a2016-04-04 21:06:17 +000044static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),
45 cl::init(false));
Devang Patelef00f9d2008-02-21 01:41:25 +000046
Mehdi Aminid2f47012016-04-01 05:33:11 +000047static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
48 cl::init(false));
49
Teresa Johnson566a35a2016-04-04 21:06:17 +000050static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
51 cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000052
Misha Brukman3da94ae2005-04-22 00:00:37 +000053static cl::opt<bool>
Teresa Johnson566a35a2016-04-04 21:06:17 +000054 DisableVerify("disable-verify", cl::Hidden,
55 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner04aa29d2003-08-18 20:47:13 +000056
Duncan P. N. Exon Smith8d61ee92015-04-15 03:14:06 +000057static 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 Liu7c2d0492018-01-30 22:32:39 +000062static 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 Johnsonc6dda902018-06-26 13:56:49 +000067static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) {
Chris Lattner17e9edc2009-08-23 02:51:22 +000068 // Infer the output filename if needed.
69 if (OutputFilename.empty()) {
70 if (InputFilename == "-") {
71 OutputFilename = "-";
72 } else {
Alexey Samsonov4e5be5a2015-05-11 21:20:20 +000073 StringRef IFN = InputFilename;
74 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
Chris Lattner17e9edc2009-08-23 02:51:22 +000075 OutputFilename += ".bc";
76 }
77 }
Dan Gohmanec080462009-09-11 20:46:33 +000078
Rafael Espindola8c968622014-08-25 18:16:47 +000079 std::error_code EC;
Reid Kleckner97ca9642017-09-23 01:03:17 +000080 std::unique_ptr<ToolOutputFile> Out(
81 new ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
Rafael Espindola8c968622014-08-25 18:16:47 +000082 if (EC) {
83 errs() << EC.message() << '\n';
Daniel Dunbar79ec7172009-10-17 03:28:28 +000084 exit(1);
85 }
86
Teresa Johnsonc6dda902018-06-26 13:56:49 +000087 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 Gohmand5826a32010-08-20 01:07:01 +0000105
106 // Declare success.
107 Out->keep();
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000108}
109
110int main(int argc, char **argv) {
Rui Ueyama0b9d56a2018-04-13 18:26:06 +0000111 InitLLVM X(argc, argv);
Mehdi Amini8be77072016-04-14 21:59:01 +0000112 LLVMContext Context;
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000113 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
114
115 // Parse the file now...
116 SMDiagnostic Err;
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000117 auto ModuleAndIndex = parseAssemblyFileWithIndex(
Yaxun Liu7c2d0492018-01-30 22:32:39 +0000118 InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout);
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000119 std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod);
Craig Topper573faec2014-04-25 04:24:47 +0000120 if (!M.get()) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000121 Err.print(argv[0], errs());
Chris Lattner17e9edc2009-08-23 02:51:22 +0000122 return 1;
123 }
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000124 std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index);
Chris Lattner17e9edc2009-08-23 02:51:22 +0000125
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000126 if (!DisableVerify) {
Chandler Carruthe608d692014-01-19 02:22:18 +0000127 std::string ErrorStr;
128 raw_string_ostream OS(ErrorStr);
129 if (verifyModule(*M.get(), &OS)) {
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000130 errs() << argv[0]
131 << ": assembly parsed, but does not verify as correct!\n";
Chandler Carruthe608d692014-01-19 02:22:18 +0000132 errs() << OS.str();
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000133 return 1;
134 }
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000135 // TODO: Implement and call summary index verifier.
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000136 }
137
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000138 if (DumpAsm) {
Teresa Johnson566a35a2016-04-04 21:06:17 +0000139 errs() << "Here's the assembly:\n" << *M.get();
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000140 if (Index.get() && Index->begin() != Index->end())
141 Index->print(errs());
142 }
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000143
Chris Lattner17e9edc2009-08-23 02:51:22 +0000144 if (!DisableOutput)
Teresa Johnsonc6dda902018-06-26 13:56:49 +0000145 WriteOutputFile(M.get(), Index.get());
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000146
Chris Lattner17e9edc2009-08-23 02:51:22 +0000147 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000148}