blob: 89a6ee5edfd890a394086200e2a933aeb225c942 [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 Carruth9aca9182014-01-07 12:34:26 +000018#include "llvm/AsmParser/Parser.h"
Chris Lattner52e1f0f2007-04-22 06:28:58 +000019#include "llvm/Bitcode/ReaderWriter.h"
Teresa Johnson7ddec632016-04-04 21:06:17 +000020#include "llvm/IR/LLVMContext.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
Teresa Johnson7ddec632016-04-04 21:06:17 +000034static cl::opt<std::string> InputFilename(cl::Positional,
35 cl::desc("<input .llvm file>"),
36 cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000037
Teresa Johnson7ddec632016-04-04 21:06:17 +000038static cl::opt<std::string> OutputFilename("o",
39 cl::desc("Override output filename"),
40 cl::value_desc("filename"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000041
Teresa Johnson7ddec632016-04-04 21:06:17 +000042static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000043
Teresa Johnson7ddec632016-04-04 21:06:17 +000044static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),
45 cl::init(false));
Devang Patelc6f915e2008-02-21 01:41:25 +000046
Mehdi Aminid7ad2212016-04-01 05:33:11 +000047static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
48 cl::init(false));
49
Teresa Johnson7ddec632016-04-04 21:06:17 +000050static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
51 cl::Hidden);
Chris Lattner2f7c9632001-06-06 20:29:01 +000052
Misha Brukman650ba8e2005-04-22 00:00:37 +000053static cl::opt<bool>
Teresa Johnson7ddec632016-04-04 21:06:17 +000054 DisableVerify("disable-verify", cl::Hidden,
55 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner016ccdf2003-08-18 20:47:13 +000056
Duncan P. N. Exon Smith8a7b84b2015-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
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000062static void WriteOutputFile(const Module *M) {
Chris Lattner9e6f1f12009-08-23 02:51:22 +000063 // Infer the output filename if needed.
64 if (OutputFilename.empty()) {
65 if (InputFilename == "-") {
66 OutputFilename = "-";
67 } else {
Alexey Samsonovd9235082015-05-11 21:20:20 +000068 StringRef IFN = InputFilename;
69 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
Chris Lattner9e6f1f12009-08-23 02:51:22 +000070 OutputFilename += ".bc";
71 }
72 }
Dan Gohmane5929232009-09-11 20:46:33 +000073
Rafael Espindola3fd1e992014-08-25 18:16:47 +000074 std::error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +000075 std::unique_ptr<tool_output_file> Out(
Rafael Espindola3fd1e992014-08-25 18:16:47 +000076 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
77 if (EC) {
78 errs() << EC.message() << '\n';
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000079 exit(1);
80 }
81
Mehdi Amini68da4262016-04-12 21:35:18 +000082 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
83 WriteBitcodeToFile(M, Out->os(), PreserveBitcodeUseListOrder, nullptr,
Teresa Johnson2d5487c2016-04-11 13:58:45 +000084 EmitModuleHash);
Dan Gohman268b0f42010-08-20 01:07:01 +000085
86 // Declare success.
87 Out->keep();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000088}
89
90int main(int argc, char **argv) {
91 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +000092 sys::PrintStackTraceOnErrorSignal(argv[0]);
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000093 PrettyStackTraceProgram X(argc, argv);
Mehdi Amini03b42e42016-04-14 21:59:01 +000094 LLVMContext Context;
Teresa Johnson7ddec632016-04-04 21:06:17 +000095 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000096 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
97
98 // Parse the file now...
99 SMDiagnostic Err;
Rafael Espindola11c07d72014-08-19 16:58:54 +0000100 std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000101 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000102 Err.print(argv[0], errs());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000103 return 1;
104 }
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000105
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000106 if (!DisableVerify) {
Chandler Carruth043949d2014-01-19 02:22:18 +0000107 std::string ErrorStr;
108 raw_string_ostream OS(ErrorStr);
109 if (verifyModule(*M.get(), &OS)) {
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000110 errs() << argv[0]
111 << ": assembly parsed, but does not verify as correct!\n";
Chandler Carruth043949d2014-01-19 02:22:18 +0000112 errs() << OS.str();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000113 return 1;
114 }
115 }
116
Teresa Johnson7ddec632016-04-04 21:06:17 +0000117 if (DumpAsm)
118 errs() << "Here's the assembly:\n" << *M.get();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000119
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000120 if (!DisableOutput)
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000121 WriteOutputFile(M.get());
122
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000123 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000124}