blob: 7e9500a667233a6fea7fb648216e384da72a7fdb [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//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Misha Brukman98930272003-08-28 21:32:57 +000011// llvm-as --help - Output information about command line switches
Chris Lattner6d80e212007-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 Brukman98930272003-08-28 21:32:57 +000014// to the x.bc file.
Misha Brukman650ba8e2005-04-22 00:00:37 +000015//
Chris Lattnerd6592892006-05-29 18:52:52 +000016//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000017
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/LLVMContext.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000019#include "llvm/AsmParser/Parser.h"
Chris Lattner52e1f0f2007-04-22 06:28:58 +000020#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Module.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"
Chris Lattner76d46322006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000027#include "llvm/Support/Signals.h"
Chris Lattnera76611a2009-07-02 22:46:18 +000028#include "llvm/Support/SourceMgr.h"
Reid Spencer742af2f2005-01-02 00:08:46 +000029#include "llvm/Support/SystemUtils.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000030#include "llvm/Support/ToolOutputFile.h"
Chris Lattner7f74a562002-01-20 22:54:45 +000031#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000032using namespace llvm;
33
Misha Brukman650ba8e2005-04-22 00:00:37 +000034static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000035InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
36
Chris Lattner02a16832003-05-22 20:13:16 +000037static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000038OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
40
41static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000042Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000043
44static cl::opt<bool>
Devang Patelc6f915e2008-02-21 01:41:25 +000045DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
46
Teresa Johnson26ab5772016-03-15 00:04:37 +000047static cl::opt<bool> EmitSummaryIndex("module-summary",
48 cl::desc("Emit module summary index"),
49 cl::init(false));
Teresa Johnson403a7872015-10-04 14:33:43 +000050
Mehdi Aminid7ad2212016-04-01 05:33:11 +000051static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
52 cl::init(false));
53
Teresa Johnson403a7872015-10-04 14:33:43 +000054static cl::opt<bool>
Chris Lattnerf5cad152002-07-22 02:10:13 +000055DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner2f7c9632001-06-06 20:29:01 +000056
Misha Brukman650ba8e2005-04-22 00:00:37 +000057static cl::opt<bool>
Chris Lattner016ccdf2003-08-18 20:47:13 +000058DisableVerify("disable-verify", cl::Hidden,
Reid Spencera9e83602004-07-11 23:20:54 +000059 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner016ccdf2003-08-18 20:47:13 +000060
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +000061static cl::opt<bool> PreserveBitcodeUseListOrder(
62 "preserve-bc-uselistorder",
63 cl::desc("Preserve use-list order when writing LLVM bitcode."),
64 cl::init(true), cl::Hidden);
65
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000066static void WriteOutputFile(const Module *M) {
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;
Ahmed Charles56440fd2014-03-06 05:51:42 +000079 std::unique_ptr<tool_output_file> Out(
Rafael Espindola3fd1e992014-08-25 18:16:47 +000080 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
81 if (EC) {
82 errs() << EC.message() << '\n';
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000083 exit(1);
84 }
85
Dan Gohmana2233f22010-09-01 14:20:41 +000086 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
Teresa Johnson403a7872015-10-04 14:33:43 +000087 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder,
Mehdi Aminid7ad2212016-04-01 05:33:11 +000088 EmitSummaryIndex, EmitModuleHash);
Dan Gohman268b0f42010-08-20 01:07:01 +000089
90 // Declare success.
91 Out->keep();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000092}
93
94int main(int argc, char **argv) {
95 // Print a stack trace if we signal out.
96 sys::PrintStackTraceOnErrorSignal();
97 PrettyStackTraceProgram X(argc, argv);
98 LLVMContext &Context = getGlobalContext();
99 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
100 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
101
102 // Parse the file now...
103 SMDiagnostic Err;
Rafael Espindola11c07d72014-08-19 16:58:54 +0000104 std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000105 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000106 Err.print(argv[0], errs());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000107 return 1;
108 }
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000109
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000110 if (!DisableVerify) {
Chandler Carruth043949d2014-01-19 02:22:18 +0000111 std::string ErrorStr;
112 raw_string_ostream OS(ErrorStr);
113 if (verifyModule(*M.get(), &OS)) {
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000114 errs() << argv[0]
115 << ": assembly parsed, but does not verify as correct!\n";
Chandler Carruth043949d2014-01-19 02:22:18 +0000116 errs() << OS.str();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000117 return 1;
118 }
119 }
120
121 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
122
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000123 if (!DisableOutput)
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000124 WriteOutputFile(M.get());
125
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000126 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127}