blob: b8b1a39384cd766dcd7057130e07ce6d48cd5abe [file] [log] [blame]
Chris Lattner1dd27b12003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
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:
Gabor Greifa99be512007-07-05 17:07:56 +000011// llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout
12// llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
Misha Brukmanad0bf0f2003-08-28 21:34:13 +000013// to the x.ll file.
Chris Lattner9bff2e92001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattner00950542001-06-06 20:29:01 +000016//
Chris Lattner1d7b50b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000018
Owen Anderson8b477ed2009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Module.h"
Chris Lattnerb3ca3622007-04-22 06:31:02 +000021#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar3b0da262008-10-22 03:25:22 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Chris Lattner01794502002-08-31 00:30:15 +000028#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Chris Lattnerc7a09852002-07-25 16:31:09 +000031static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000032InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000033
Chris Lattnerc7a09852002-07-25 16:31:09 +000034static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000035OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000036 cl::value_desc("filename"));
37
38static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000039Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000040
Chris Lattner4d544cd2007-02-07 04:39:35 +000041static cl::opt<bool>
42DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
43
Chris Lattner00950542001-06-06 20:29:01 +000044int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000045 // Print a stack trace if we signal out.
46 sys::PrintStackTraceOnErrorSignal();
47 PrettyStackTraceProgram X(argc, argv);
48
Owen Anderson0d7c6952009-07-15 22:16:10 +000049 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +000050 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner17e9edc2009-08-23 02:51:22 +000051
52
53 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000054
Chris Lattner17e9edc2009-08-23 02:51:22 +000055 std::string ErrorMessage;
56 std::auto_ptr<Module> M;
57
58 if (MemoryBuffer *Buffer
59 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
60 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
61 delete Buffer;
Chris Lattner93c26682002-09-24 00:09:48 +000062 }
Chris Lattnerc30598b2006-12-06 01:18:01 +000063
Chris Lattner17e9edc2009-08-23 02:51:22 +000064 if (M.get() == 0) {
65 errs() << argv[0] << ": ";
66 if (ErrorMessage.size())
67 errs() << ErrorMessage << "\n";
68 else
69 errs() << "bitcode didn't read correctly.\n";
70 return 1;
71 }
72
73 // Just use stdout. We won't actually print anything on it.
74 if (DontPrint)
75 OutputFilename = "-";
76
77 if (OutputFilename.empty()) { // Unspecified output, infer it.
78 if (InputFilename == "-") {
79 OutputFilename = "-";
80 } else {
81 const std::string &IFN = InputFilename;
82 int Len = IFN.length();
83 // If the source ends in .bc, strip it off.
84 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
85 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
86 else
87 OutputFilename = IFN+".ll";
88 }
89 }
Dan Gohmanec080462009-09-11 20:46:33 +000090
91 // Make sure that the Out file gets unlinked from the disk if we get a
92 // SIGINT.
93 if (OutputFilename != "-")
94 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
95
Chris Lattner17e9edc2009-08-23 02:51:22 +000096 std::string ErrorInfo;
97 std::auto_ptr<raw_fd_ostream>
98 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
Chris Lattner17e9edc2009-08-23 02:51:22 +000099 raw_fd_ostream::F_Binary));
100 if (!ErrorInfo.empty()) {
101 errs() << ErrorInfo << '\n';
Chris Lattner17e9edc2009-08-23 02:51:22 +0000102 return 1;
103 }
104
Chris Lattner17e9edc2009-08-23 02:51:22 +0000105 // All that llvm-dis does is write the assembly to a file.
Dan Gohman04fc8c82009-09-15 15:33:42 +0000106 if (!DontPrint)
107 *Out << *M;
Chris Lattner17e9edc2009-08-23 02:51:22 +0000108
109 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000110}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000111