blob: e93d6d4e0992e187ae570ad0e6b2a66abc05151d [file] [log] [blame]
Chris Lattner8f71f042003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
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:
Gabor Greife16561c2007-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 Brukmanf12549d2003-08-28 21:34:13 +000013// to the x.ll file.
Chris Lattnerf284ac52001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattner2f7c9632001-06-06 20:29:01 +000016//
Chris Lattner6d56c6b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000018
Owen Anderson6773d382009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/Module.h"
Chris Lattner874a9e42002-08-31 00:30:15 +000021#include "llvm/PassManager.h"
Chris Lattnere2244462007-04-22 06:31:02 +000022#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner874a9e42002-08-31 00:30:15 +000023#include "llvm/Assembly/PrintModulePass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner76d46322006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Chris Lattner6694f602007-04-29 07:54:31 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000027#include "llvm/Support/PrettyStackTrace.h"
Bill Wendlinga531ac22006-11-28 23:33:06 +000028#include "llvm/Support/Streams.h"
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattner278f5152004-05-27 05:41:36 +000030#include "llvm/System/Signals.h"
Chris Lattner874a9e42002-08-31 00:30:15 +000031#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000032using namespace llvm;
33
Chris Lattner64a67272002-07-25 16:31:09 +000034static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000035InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000036
Chris Lattner64a67272002-07-25 16:31:09 +000037static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000038OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000039 cl::value_desc("filename"));
40
41static cl::opt<bool>
42Force("f", cl::desc("Overwrite output files"));
43
Chris Lattner216835d2007-02-07 04:39:35 +000044static cl::opt<bool>
45DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
46
Chris Lattner2f7c9632001-06-06 20:29:01 +000047int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000048 // Print a stack trace if we signal out.
49 sys::PrintStackTraceOnErrorSignal();
50 PrettyStackTraceProgram X(argc, argv);
51
Owen Anderson19251ec2009-07-15 22:16:10 +000052 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000053 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer996ec722004-12-30 05:36:08 +000054 try {
Dan Gohman2c6a8212007-10-08 15:45:12 +000055 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattner12439ff2004-02-19 20:32:12 +000056
Dan Gohman607818a2009-07-15 17:29:42 +000057 raw_ostream *Out = &outs(); // Default to printing to stdout.
Reid Spencer996ec722004-12-30 05:36:08 +000058 std::string ErrorMessage;
Chris Lattner2f7c9632001-06-06 20:29:01 +000059
Chris Lattnere2244462007-04-22 06:31:02 +000060 std::auto_ptr<Module> M;
Chris Lattner8ca41bf2007-04-22 06:35:20 +000061
Chris Lattner9e9a34c2007-05-06 23:45:49 +000062 if (MemoryBuffer *Buffer
63 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
Owen Anderson1cf085d2009-07-01 21:22:36 +000064 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
Chris Lattner9e9a34c2007-05-06 23:45:49 +000065 delete Buffer;
66 }
Chris Lattner6694f602007-04-29 07:54:31 +000067
Reid Spencer996ec722004-12-30 05:36:08 +000068 if (M.get() == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000069 cerr << argv[0] << ": ";
Reid Spencer996ec722004-12-30 05:36:08 +000070 if (ErrorMessage.size())
Bill Wendlingf3baad32006-12-07 01:30:32 +000071 cerr << ErrorMessage << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +000072 else
Gabor Greife16561c2007-07-05 17:07:56 +000073 cerr << "bitcode didn't read correctly.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000074 return 1;
75 }
Chris Lattner6694f602007-04-29 07:54:31 +000076
Chris Lattner216835d2007-02-07 04:39:35 +000077 if (DontPrint) {
78 // Just use stdout. We won't actually print anything on it.
79 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer996ec722004-12-30 05:36:08 +000080 if (OutputFilename != "-") { // Not stdout?
Dan Gohman607818a2009-07-15 17:29:42 +000081 std::string ErrorInfo;
82 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
83 Force, ErrorInfo);
84 if (!ErrorInfo.empty()) {
85 errs() << ErrorInfo << '\n';
86 if (!Force)
87 errs() << "Use -f command line argument to force output\n";
88 delete Out;
89 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +000090 }
91 }
Chris Lattner0af24642001-07-23 02:35:57 +000092 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000093 if (InputFilename == "-") {
94 OutputFilename = "-";
Chris Lattner0af24642001-07-23 02:35:57 +000095 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000096 std::string IFN = InputFilename;
97 int Len = IFN.length();
98 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
99 // Source ends in .bc
100 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
101 } else {
102 OutputFilename = IFN+".ll";
103 }
Chris Lattner7f74a562002-01-20 22:54:45 +0000104
Dan Gohman607818a2009-07-15 17:29:42 +0000105 std::string ErrorInfo;
106 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
107 Force, ErrorInfo);
108 if (!ErrorInfo.empty()) {
109 errs() << ErrorInfo << '\n';
110 if (!Force)
111 errs() << "Use -f command line argument to force output\n";
112 delete Out;
113 return 1;
Reid Spencer996ec722004-12-30 05:36:08 +0000114 }
Chris Lattner0af24642001-07-23 02:35:57 +0000115
Dan Gohman607818a2009-07-15 17:29:42 +0000116 // Make sure that the Out file gets unlinked from the disk if we get a
117 // SIGINT
118 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
119 }
Reid Spencer996ec722004-12-30 05:36:08 +0000120 }
Chris Lattner0af24642001-07-23 02:35:57 +0000121
Chris Lattner0174b522005-01-29 17:29:05 +0000122 // All that llvm-dis does is write the assembly to a file.
Chris Lattner216835d2007-02-07 04:39:35 +0000123 if (!DontPrint) {
124 PassManager Passes;
Dan Gohman607818a2009-07-15 17:29:42 +0000125 Passes.add(createPrintModulePass(Out));
Chris Lattner216835d2007-02-07 04:39:35 +0000126 Passes.run(*M.get());
127 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128
Dan Gohman607818a2009-07-15 17:29:42 +0000129 if (Out != &outs())
Reid Spencer996ec722004-12-30 05:36:08 +0000130 delete Out;
Reid Spencer996ec722004-12-30 05:36:08 +0000131 return 0;
132 } catch (const std::string& msg) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000133 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000134 } catch (...) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000135 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner47c2e3e2002-09-24 00:09:48 +0000136 }
Chris Lattner76d46322006-12-06 01:18:01 +0000137
Reid Spencer996ec722004-12-30 05:36:08 +0000138 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000139}
Sumant Kowshikcf3afd92002-05-08 18:09:58 +0000140