blob: 5ccf505923f2d7f19071613a0aea45cade34b8e6 [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
47static cl::opt<bool>
Chris Lattnerf5cad152002-07-22 02:10:13 +000048DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
Misha Brukman650ba8e2005-04-22 00:00:37 +000050static cl::opt<bool>
Chris Lattner016ccdf2003-08-18 20:47:13 +000051DisableVerify("disable-verify", cl::Hidden,
Reid Spencera9e83602004-07-11 23:20:54 +000052 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner016ccdf2003-08-18 20:47:13 +000053
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000054static void WriteOutputFile(const Module *M) {
Chris Lattner9e6f1f12009-08-23 02:51:22 +000055 // Infer the output filename if needed.
56 if (OutputFilename.empty()) {
57 if (InputFilename == "-") {
58 OutputFilename = "-";
59 } else {
60 std::string IFN = InputFilename;
61 int Len = IFN.length();
62 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
63 // Source ends in .ll
64 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
65 } else {
66 OutputFilename = IFN; // Append a .bc to it
67 }
68 OutputFilename += ".bc";
69 }
70 }
Dan Gohmane5929232009-09-11 20:46:33 +000071
Rafael Espindola3fd1e992014-08-25 18:16:47 +000072 std::error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +000073 std::unique_ptr<tool_output_file> Out(
Rafael Espindola3fd1e992014-08-25 18:16:47 +000074 new tool_output_file(OutputFilename, EC, sys::fs::F_None));
75 if (EC) {
76 errs() << EC.message() << '\n';
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000077 exit(1);
78 }
79
Dan Gohmana2233f22010-09-01 14:20:41 +000080 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
81 WriteBitcodeToFile(M, Out->os());
Dan Gohman268b0f42010-08-20 01:07:01 +000082
83 // Declare success.
84 Out->keep();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +000085}
86
87int main(int argc, char **argv) {
88 // Print a stack trace if we signal out.
89 sys::PrintStackTraceOnErrorSignal();
90 PrettyStackTraceProgram X(argc, argv);
91 LLVMContext &Context = getGlobalContext();
92 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
93 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
94
95 // Parse the file now...
96 SMDiagnostic Err;
Rafael Espindola11c07d72014-08-19 16:58:54 +000097 std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
Craig Toppere6cb63e2014-04-25 04:24:47 +000098 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +000099 Err.print(argv[0], errs());
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000100 return 1;
101 }
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000102
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000103 if (!DisableVerify) {
Chandler Carruth043949d2014-01-19 02:22:18 +0000104 std::string ErrorStr;
105 raw_string_ostream OS(ErrorStr);
106 if (verifyModule(*M.get(), &OS)) {
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000107 errs() << argv[0]
108 << ": assembly parsed, but does not verify as correct!\n";
Chandler Carruth043949d2014-01-19 02:22:18 +0000109 errs() << OS.str();
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000110 return 1;
111 }
112 }
113
114 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
115
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000116 if (!DisableOutput)
Daniel Dunbar2bdc1a52009-10-17 03:28:28 +0000117 WriteOutputFile(M.get());
118
Chris Lattner9e6f1f12009-08-23 02:51:22 +0000119 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120}