blob: 471e5e2f5d2c8c8a2f64f5320f849db65172124c [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
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Chris Lattner01794502002-08-31 00:30:15 +000020#include "llvm/PassManager.h"
Chris Lattnerb3ca3622007-04-22 06:31:02 +000021#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner01794502002-08-31 00:30:15 +000022#include "llvm/Assembly/PrintModulePass.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 Lattnerc453f762007-04-29 07:54:31 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000027#include "llvm/Support/Streams.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"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000030#include <iostream>
Chris Lattnercee8f9a2001-11-27 00:03:19 +000031#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000032#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000033using namespace llvm;
34
Chris Lattnerc7a09852002-07-25 16:31:09 +000035static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000036InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000037
Chris Lattnerc7a09852002-07-25 16:31:09 +000038static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000039OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000040 cl::value_desc("filename"));
41
42static cl::opt<bool>
43Force("f", cl::desc("Overwrite output files"));
44
Chris Lattner4d544cd2007-02-07 04:39:35 +000045static cl::opt<bool>
46DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
47
Chris Lattner00950542001-06-06 20:29:01 +000048int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000049 // Print a stack trace if we signal out.
50 sys::PrintStackTraceOnErrorSignal();
51 PrettyStackTraceProgram X(argc, argv);
52
53 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000054 try {
Dan Gohman82a13c92007-10-08 15:45:12 +000055 cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000056
Chris Lattner5ea56e52005-01-29 17:29:05 +000057 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000058 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000059
Chris Lattnerb3ca3622007-04-22 06:31:02 +000060 std::auto_ptr<Module> M;
Chris Lattner197ccea2007-04-22 06:35:20 +000061
Chris Lattner065344d2007-05-06 23:45:49 +000062 if (MemoryBuffer *Buffer
63 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
Chris Lattner44dadff2007-05-06 09:29:57 +000064 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
Chris Lattner065344d2007-05-06 23:45:49 +000065 delete Buffer;
66 }
Chris Lattnerc453f762007-04-29 07:54:31 +000067
Reid Spencer1ef8bda2004-12-30 05:36:08 +000068 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000069 cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000070 if (ErrorMessage.size())
Bill Wendlinge8156192006-12-07 01:30:32 +000071 cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000072 else
Gabor Greifa99be512007-07-05 17:07:56 +000073 cerr << "bitcode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000074 return 1;
75 }
Chris Lattnerc453f762007-04-29 07:54:31 +000076
Chris Lattner4d544cd2007-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 Spencer1ef8bda2004-12-30 05:36:08 +000080 if (OutputFilename != "-") { // Not stdout?
81 if (!Force && std::ifstream(OutputFilename.c_str())) {
82 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000083 cerr << argv[0] << ": error opening '" << OutputFilename
84 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000085 } else {
86 Out = new std::ofstream(OutputFilename.c_str());
87 }
88 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000089 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000090 if (InputFilename == "-") {
91 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000092 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000093 std::string IFN = InputFilename;
94 int Len = IFN.length();
95 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
96 // Source ends in .bc
97 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
98 } else {
99 OutputFilename = IFN+".ll";
100 }
Chris Lattner697954c2002-01-20 22:54:45 +0000101
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000102 if (!Force && std::ifstream(OutputFilename.c_str())) {
103 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +0000104 cerr << argv[0] << ": error opening '" << OutputFilename
105 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000106 } else {
107 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000108
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000109 // Make sure that the Out file gets unlinked from the disk if we get a
110 // SIGINT
111 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
112 }
Chris Lattner697954c2002-01-20 22:54:45 +0000113 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000114 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000115
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000117 cerr << argv[0] << ": error opening " << OutputFilename
118 << ": sending to stdout instead!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000119 Out = &std::cout;
120 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000121
Chris Lattner5ea56e52005-01-29 17:29:05 +0000122 // All that llvm-dis does is write the assembly to a file.
Chris Lattner4d544cd2007-02-07 04:39:35 +0000123 if (!DontPrint) {
124 PassManager Passes;
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000125 raw_os_ostream L(*Out);
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000126 Passes.add(createPrintModulePass(&L));
Chris Lattner4d544cd2007-02-07 04:39:35 +0000127 Passes.run(*M.get());
128 }
Chris Lattner00950542001-06-06 20:29:01 +0000129
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 if (Out != &std::cout) {
131 ((std::ofstream*)Out)->close();
132 delete Out;
133 }
134 return 0;
135 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000136 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000137 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000138 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000139 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000140
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000141 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000142}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000143