blob: 0d081ca526a9578a1e21f94decea775910713034 [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
Owen Anderson8b477ed2009-07-01 16:58:40 +000018#include "llvm/LLVMContext.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
20#include "llvm/Assembly/Parser.h"
Chris Lattner1acbea12002-08-30 22:54:41 +000021#include "llvm/Analysis/Verifier.h"
Chris Lattner27bb3af2007-04-22 06:28:58 +000022#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner92bcb422009-07-02 22:46:18 +000026#include "llvm/Support/SourceMgr.h"
Reid Spencerb687c0c2005-01-02 00:08:46 +000027#include "llvm/Support/SystemUtils.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000028#include "llvm/Support/ToolOutputFile.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000029#include "llvm/System/Signals.h"
Chris Lattner697954c2002-01-20 22:54:45 +000030#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Misha Brukman3da94ae2005-04-22 00:00:37 +000033static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000034InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
35
Chris Lattner6c8103f2003-05-22 20:13:16 +000036static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000037OutputFilename("o", cl::desc("Override output filename"),
38 cl::value_desc("filename"));
39
40static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000041Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000042
43static cl::opt<bool>
Devang Patelef00f9d2008-02-21 01:41:25 +000044DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
45
46static cl::opt<bool>
Chris Lattner5ff62e92002-07-22 02:10:13 +000047DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000048
Misha Brukman3da94ae2005-04-22 00:00:37 +000049static cl::opt<bool>
Chris Lattner04aa29d2003-08-18 20:47:13 +000050DisableVerify("disable-verify", cl::Hidden,
Reid Spencer0d886162004-07-11 23:20:54 +000051 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner04aa29d2003-08-18 20:47:13 +000052
Daniel Dunbar79ec7172009-10-17 03:28:28 +000053static void WriteOutputFile(const Module *M) {
Chris Lattner17e9edc2009-08-23 02:51:22 +000054 // Infer the output filename if needed.
55 if (OutputFilename.empty()) {
56 if (InputFilename == "-") {
57 OutputFilename = "-";
58 } else {
59 std::string IFN = InputFilename;
60 int Len = IFN.length();
61 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
62 // Source ends in .ll
63 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
64 } else {
65 OutputFilename = IFN; // Append a .bc to it
66 }
67 OutputFilename += ".bc";
68 }
69 }
Dan Gohmanec080462009-09-11 20:46:33 +000070
Chris Lattner17e9edc2009-08-23 02:51:22 +000071 std::string ErrorInfo;
Dan Gohmand5826a32010-08-20 01:07:01 +000072 OwningPtr<tool_output_file> Out
73 (new tool_output_file(OutputFilename.c_str(), ErrorInfo,
74 raw_fd_ostream::F_Binary));
Chris Lattner17e9edc2009-08-23 02:51:22 +000075 if (!ErrorInfo.empty()) {
76 errs() << ErrorInfo << '\n';
Daniel Dunbar79ec7172009-10-17 03:28:28 +000077 exit(1);
78 }
79
Dan Gohmand4c45432010-09-01 14:20:41 +000080 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true))
81 WriteBitcodeToFile(M, Out->os());
Dan Gohmand5826a32010-08-20 01:07:01 +000082
83 // Declare success.
84 Out->keep();
Daniel Dunbar79ec7172009-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;
97 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
98 if (M.get() == 0) {
99 Err.Print(argv[0], errs());
Chris Lattner17e9edc2009-08-23 02:51:22 +0000100 return 1;
101 }
Chris Lattner17e9edc2009-08-23 02:51:22 +0000102
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000103 if (!DisableVerify) {
104 std::string Err;
105 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
106 errs() << argv[0]
107 << ": assembly parsed, but does not verify as correct!\n";
108 errs() << Err;
109 return 1;
110 }
111 }
112
113 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
114
Chris Lattner17e9edc2009-08-23 02:51:22 +0000115 if (!DisableOutput)
Daniel Dunbar79ec7172009-10-17 03:28:28 +0000116 WriteOutputFile(M.get());
117
Chris Lattner17e9edc2009-08-23 02:51:22 +0000118 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000119}