blob: 97ec4c7306165f3929d66ac438351163445c103f [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Brukmanad0bf0f2003-08-28 21:34:13 +000011// llvm-dis [options] - Read LLVM bytecode from stdin, write asm to stdout
12// llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
13// 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"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000026#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000028#include <iostream>
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029#include <fstream>
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>
Chris Lattner5ff62e92002-07-22 02:10:13 +000034InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
35
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 Lattnerc30598b2006-12-06 01:18:01 +000047 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000048 try {
49 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
50 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000051
Chris Lattner5ea56e52005-01-29 17:29:05 +000052 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000053 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000054
Chris Lattnerb3ca3622007-04-22 06:31:02 +000055 std::auto_ptr<Module> M;
Chris Lattner197ccea2007-04-22 06:35:20 +000056
Chris Lattner065344d2007-05-06 23:45:49 +000057 if (MemoryBuffer *Buffer
58 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
Chris Lattner44dadff2007-05-06 09:29:57 +000059 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
Chris Lattner065344d2007-05-06 23:45:49 +000060 delete Buffer;
61 }
Chris Lattnerc453f762007-04-29 07:54:31 +000062
Reid Spencer1ef8bda2004-12-30 05:36:08 +000063 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000064 cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000065 if (ErrorMessage.size())
Bill Wendlinge8156192006-12-07 01:30:32 +000066 cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000067 else
Bill Wendlinge8156192006-12-07 01:30:32 +000068 cerr << "bytecode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000069 return 1;
70 }
Chris Lattnerc453f762007-04-29 07:54:31 +000071
Chris Lattner4d544cd2007-02-07 04:39:35 +000072 if (DontPrint) {
73 // Just use stdout. We won't actually print anything on it.
74 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer1ef8bda2004-12-30 05:36:08 +000075 if (OutputFilename != "-") { // Not stdout?
76 if (!Force && std::ifstream(OutputFilename.c_str())) {
77 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000078 cerr << argv[0] << ": error opening '" << OutputFilename
79 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000080 } else {
81 Out = new std::ofstream(OutputFilename.c_str());
82 }
83 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000084 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000085 if (InputFilename == "-") {
86 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000087 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000088 std::string IFN = InputFilename;
89 int Len = IFN.length();
90 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
91 // Source ends in .bc
92 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
93 } else {
94 OutputFilename = IFN+".ll";
95 }
Chris Lattner697954c2002-01-20 22:54:45 +000096
Reid Spencer1ef8bda2004-12-30 05:36:08 +000097 if (!Force && std::ifstream(OutputFilename.c_str())) {
98 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000099 cerr << argv[0] << ": error opening '" << OutputFilename
100 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000101 } else {
102 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000103
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000104 // Make sure that the Out file gets unlinked from the disk if we get a
105 // SIGINT
106 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
107 }
Chris Lattner697954c2002-01-20 22:54:45 +0000108 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000109 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000110
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000111 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000112 cerr << argv[0] << ": error opening " << OutputFilename
113 << ": sending to stdout instead!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 Out = &std::cout;
115 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000116
Chris Lattner5ea56e52005-01-29 17:29:05 +0000117 // All that llvm-dis does is write the assembly to a file.
Chris Lattner4d544cd2007-02-07 04:39:35 +0000118 if (!DontPrint) {
119 PassManager Passes;
120 OStream L(*Out);
121 Passes.add(new PrintModulePass(&L));
122 Passes.run(*M.get());
123 }
Chris Lattner00950542001-06-06 20:29:01 +0000124
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000125 if (Out != &std::cout) {
126 ((std::ofstream*)Out)->close();
127 delete Out;
128 }
129 return 0;
130 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000131 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000132 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000133 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000134 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000135
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000136 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000137}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000138