blob: eccabd5d14a4b3b67eb5a49abd5a58435dd42b9e [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"
Bill Wendling68fe61d2006-11-29 00:19:40 +000027#include "llvm/Support/Streams.h"
Reid Spencerb687c0c2005-01-02 00:08:46 +000028#include "llvm/Support/SystemUtils.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000030#include "llvm/System/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000031#include <fstream>
Reid Spencer86f42bd2004-07-04 12:20:55 +000032#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000033#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000034using namespace llvm;
35
Misha Brukman3da94ae2005-04-22 00:00:37 +000036static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000037InputFilename(cl::Positional, cl::desc("<input .llvm file>"), cl::init("-"));
38
Chris Lattner6c8103f2003-05-22 20:13:16 +000039static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000040OutputFilename("o", cl::desc("Override output filename"),
41 cl::value_desc("filename"));
42
43static cl::opt<bool>
44Force("f", cl::desc("Overwrite output files"));
45
46static cl::opt<bool>
Devang Patelef00f9d2008-02-21 01:41:25 +000047DisableOutput("disable-output", cl::desc("Disable output"), cl::init(false));
48
49static cl::opt<bool>
Chris Lattner5ff62e92002-07-22 02:10:13 +000050DumpAsm("d", cl::desc("Print assembly as parsed"), cl::Hidden);
Chris Lattner00950542001-06-06 20:29:01 +000051
Misha Brukman3da94ae2005-04-22 00:00:37 +000052static cl::opt<bool>
Chris Lattner04aa29d2003-08-18 20:47:13 +000053DisableVerify("disable-verify", cl::Hidden,
Reid Spencer0d886162004-07-11 23:20:54 +000054 cl::desc("Do not run verifier on input LLVM (dangerous!)"));
Chris Lattner04aa29d2003-08-18 20:47:13 +000055
Chris Lattner00950542001-06-06 20:29:01 +000056int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000057 // Print a stack trace if we signal out.
Reid Spencer9de7b332004-08-29 19:28:55 +000058 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +000059 PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +000060 LLVMContext Context;
Chris Lattnercc14d252009-03-06 05:34:10 +000061 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
62 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
Chris Lattner00950542001-06-06 20:29:01 +000063
Reid Spencer1ef8bda2004-12-30 05:36:08 +000064 int exitCode = 0;
Anand Shukla63aaa112002-06-25 21:43:28 +000065 std::ostream *Out = 0;
Chris Lattner00950542001-06-06 20:29:01 +000066 try {
67 // Parse the file now...
Chris Lattner92bcb422009-07-02 22:46:18 +000068 SMDiagnostic Err;
Owen Anderson31895e72009-07-01 21:22:36 +000069 std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
Chris Lattner06272dc2002-04-07 22:34:19 +000070 if (M.get() == 0) {
Chris Lattner92bcb422009-07-02 22:46:18 +000071 Err.Print(argv[0], errs());
Chris Lattner00950542001-06-06 20:29:01 +000072 return 1;
73 }
Chris Lattner1acbea12002-08-30 22:54:41 +000074
Chris Lattner05ac92c2006-07-06 18:02:27 +000075 if (!DisableVerify) {
76 std::string Err;
77 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
Bill Wendlinge8156192006-12-07 01:30:32 +000078 cerr << argv[0]
79 << ": assembly parsed, but does not verify as correct!\n";
80 cerr << Err;
Chris Lattner05ac92c2006-07-06 18:02:27 +000081 return 1;
82 }
Chris Lattner1acbea12002-08-30 22:54:41 +000083 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000084
Bill Wendlinge8156192006-12-07 01:30:32 +000085 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
Chris Lattner8f367bd2001-07-23 02:35:57 +000086
Chris Lattner1e78f362001-07-23 19:27:24 +000087 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000088 if (OutputFilename != "-") { // Not stdout?
89 if (!Force && std::ifstream(OutputFilename.c_str())) {
90 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000091 cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists!\n"
93 << "Use -f command line argument to force output\n";
Chris Lattner3ce0ea82003-05-31 21:47:16 +000094 return 1;
95 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000096 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000097 std::ios::trunc | std::ios::binary);
Chris Lattner3ce0ea82003-05-31 21:47:16 +000098 } else { // Specified stdout
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000099 // FIXME: cout is not binary!
100 Out = &std::cout;
Chris Lattner697954c2002-01-20 22:54:45 +0000101 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000102 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +0000103 if (InputFilename == "-") {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000104 OutputFilename = "-";
105 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000106 } else {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000107 std::string IFN = InputFilename;
108 int Len = IFN.length();
109 if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
110 // Source ends in .ll
111 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +0000112 } else {
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000113 OutputFilename = IFN; // Append a .bc to it
114 }
115 OutputFilename += ".bc";
Chris Lattner697954c2002-01-20 22:54:45 +0000116
Chris Lattner888912d2002-01-22 21:07:24 +0000117 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000118 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +0000119 cerr << argv[0] << ": error opening '" << OutputFilename
120 << "': file exists!\n"
121 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000122 return 1;
123 }
124
Misha Brukman3da94ae2005-04-22 00:00:37 +0000125 Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000126 std::ios::trunc | std::ios::binary);
Misha Brukman452fea92003-10-10 17:56:49 +0000127 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000128 // SIGINT
Reid Spencer227b6d02004-11-14 22:30:54 +0000129 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner00950542001-06-06 20:29:01 +0000130 }
131 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000132
Chris Lattner697954c2002-01-20 22:54:45 +0000133 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000134 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000135 return 1;
136 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000137
Devang Patelef00f9d2008-02-21 01:41:25 +0000138 if (!DisableOutput)
139 if (Force || !CheckBitcodeOutputToConsole(Out,true))
140 WriteBitcodeToFile(M.get(), *Out);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000141 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000142 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000143 exitCode = 1;
144 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000145 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000146 exitCode = 1;
Chris Lattner00950542001-06-06 20:29:01 +0000147 }
148
Anand Shukla63aaa112002-06-25 21:43:28 +0000149 if (Out != &std::cout) delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000150 return exitCode;
Chris Lattner00950542001-06-06 20:29:01 +0000151}
152