blob: 258f5141e11813bea5074e92766b63aa2471e567 [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"
Teresa Johnsonad176792016-11-11 05:34:58 +000019#include "llvm/Bitcode/BitcodeWriter.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"
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
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;
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
Mehdi Amini68da4262016-04-12 21:35:18 +000086 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
Rafael Espindola6a86e252018-02-14 19:11:32 +000087 WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder, nullptr,
Teresa Johnson2d5487c2016-04-11 13:58:45 +000088 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) {
Rui Ueyama197194b2018-04-13 18:26:06 +000095 InitLLVM X(argc, argv);
Mehdi Amini03b42e42016-04-14 21:59:01 +000096 LLVMContext Context;
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000097 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
98
99 // Parse the file now...
100 SMDiagnostic Err;
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000101 std::unique_ptr<Module> M = parseAssemblyFile(
102 InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000103 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000104 Err.print(argv[0], errs());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000105 return 1;
106 }
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000107
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000108 if (!DisableVerify) {
Chandler Carruth043949d2014-01-19 02:22:18 +0000109 std::string ErrorStr;
110 raw_string_ostream OS(ErrorStr);
111 if (verifyModule(*M.get(), &OS)) {
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000112 errs() << argv[0]
113 << ": assembly parsed, but does not verify as correct!\n";
Chandler Carruth043949d2014-01-19 02:22:18 +0000114 errs() << OS.str();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000115 return 1;
116 }
117 }
118
Teresa Johnson7ddec632016-04-04 21:06:17 +0000119 if (DumpAsm)
120 errs() << "Here's the assembly:\n" << *M.get();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000121
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000122 if (!DisableOutput)
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000123 WriteOutputFile(M.get());
124
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000125 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000126}