blob: 65e97c37dfcb5776813ffe5bde2e1f9143a765a3 [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 Lattner01794502002-08-31 00:30:15 +000021#include "llvm/PassManager.h"
Chris Lattnerb3ca3622007-04-22 06:31:02 +000022#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner01794502002-08-31 00:30:15 +000023#include "llvm/Assembly/PrintModulePass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000027#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar3b0da262008-10-22 03:25:22 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000029#include "llvm/System/Signals.h"
Chris Lattner01794502002-08-31 00:30:15 +000030#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattnerc7a09852002-07-25 16:31:09 +000033static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000034InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000035
Chris Lattnerc7a09852002-07-25 16:31:09 +000036static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000037OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000038 cl::value_desc("filename"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
42
Chris Lattner4d544cd2007-02-07 04:39:35 +000043static cl::opt<bool>
44DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
45
Chris Lattner00950542001-06-06 20:29:01 +000046int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000047 // Print a stack trace if we signal out.
48 sys::PrintStackTraceOnErrorSignal();
49 PrettyStackTraceProgram X(argc, argv);
50
Owen Anderson0d7c6952009-07-15 22:16:10 +000051 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +000052 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000053 try {
Dan Gohman82a13c92007-10-08 15:45:12 +000054 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000055
Dan Gohmana1bdced2009-07-15 17:29:42 +000056 raw_ostream *Out = &outs(); // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000057 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000058
Chris Lattnerb3ca3622007-04-22 06:31:02 +000059 std::auto_ptr<Module> M;
Chris Lattner197ccea2007-04-22 06:35:20 +000060
Chris Lattner065344d2007-05-06 23:45:49 +000061 if (MemoryBuffer *Buffer
62 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
Owen Anderson31895e72009-07-01 21:22:36 +000063 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
Chris Lattner065344d2007-05-06 23:45:49 +000064 delete Buffer;
65 }
Chris Lattnerc453f762007-04-29 07:54:31 +000066
Reid Spencer1ef8bda2004-12-30 05:36:08 +000067 if (M.get() == 0) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000068 errs() << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000069 if (ErrorMessage.size())
Dan Gohmanac95cc72009-07-16 15:30:09 +000070 errs() << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000071 else
Dan Gohmanac95cc72009-07-16 15:30:09 +000072 errs() << "bitcode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000073 return 1;
74 }
Chris Lattnerc453f762007-04-29 07:54:31 +000075
Chris Lattner4d544cd2007-02-07 04:39:35 +000076 if (DontPrint) {
77 // Just use stdout. We won't actually print anything on it.
78 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer1ef8bda2004-12-30 05:36:08 +000079 if (OutputFilename != "-") { // Not stdout?
Dan Gohmana1bdced2009-07-15 17:29:42 +000080 std::string ErrorInfo;
81 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
82 Force, ErrorInfo);
83 if (!ErrorInfo.empty()) {
84 errs() << ErrorInfo << '\n';
85 if (!Force)
86 errs() << "Use -f command line argument to force output\n";
87 delete Out;
88 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 }
90 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000091 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000092 if (InputFilename == "-") {
93 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000094 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000095 std::string IFN = InputFilename;
96 int Len = IFN.length();
97 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
98 // Source ends in .bc
99 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
100 } else {
101 OutputFilename = IFN+".ll";
102 }
Chris Lattner697954c2002-01-20 22:54:45 +0000103
Dan Gohmana1bdced2009-07-15 17:29:42 +0000104 std::string ErrorInfo;
105 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
106 Force, ErrorInfo);
107 if (!ErrorInfo.empty()) {
108 errs() << ErrorInfo << '\n';
109 if (!Force)
110 errs() << "Use -f command line argument to force output\n";
111 delete Out;
112 return 1;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000113 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000114
Dan Gohmana1bdced2009-07-15 17:29:42 +0000115 // Make sure that the Out file gets unlinked from the disk if we get a
116 // SIGINT
117 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
118 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000119 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000120
Chris Lattner5ea56e52005-01-29 17:29:05 +0000121 // All that llvm-dis does is write the assembly to a file.
Chris Lattner4d544cd2007-02-07 04:39:35 +0000122 if (!DontPrint) {
123 PassManager Passes;
Dan Gohmana1bdced2009-07-15 17:29:42 +0000124 Passes.add(createPrintModulePass(Out));
Chris Lattner4d544cd2007-02-07 04:39:35 +0000125 Passes.run(*M.get());
126 }
Chris Lattner00950542001-06-06 20:29:01 +0000127
Dan Gohmana1bdced2009-07-15 17:29:42 +0000128 if (Out != &outs())
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000129 delete Out;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 return 0;
131 } catch (const std::string& msg) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000132 errs() << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 } catch (...) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000134 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000135 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000136
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000137 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000138}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000139