blob: 6065e9ba5e768ca5b65380c296a02de5f86c04cd [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 Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Bytecode/Reader.h"
Chris Lattner01794502002-08-31 00:30:15 +000023#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerf2e292c2007-02-07 21:41:02 +000024#include "llvm/Support/Compressor.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Chris Lattnerc453f762007-04-29 07:54:31 +000027#include "llvm/Support/MemoryBuffer.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000028#include "llvm/Support/Streams.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>
Chris Lattner5ff62e92002-07-22 02:10:13 +000036InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
37
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 Lattnerc453f762007-04-29 07:54:31 +000048static cl::opt<bool>
49Bitcode("bitcode", cl::desc("Read a bitcode file"));
50
Chris Lattner00950542001-06-06 20:29:01 +000051int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000052 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000053 try {
54 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
55 sys::PrintStackTraceOnErrorSignal();
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 Lattnerc453f762007-04-29 07:54:31 +000062 if (Bitcode) {
63 MemoryBuffer *Buffer;
64 if (InputFilename == "-") {
65 Buffer = MemoryBuffer::getSTDIN();
66 } else {
67 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
68 }
69
70 if (Buffer == 0)
71 ErrorMessage = "Error reading file '" + InputFilename + "'";
72 else
73 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
74
75 delete Buffer;
76 } else {
77 M.reset(ParseBytecodeFile(InputFilename,
78 Compressor::decompressToNewBuffer,
Chris Lattnerb3ca3622007-04-22 06:31:02 +000079 &ErrorMessage));
Chris Lattnerc453f762007-04-29 07:54:31 +000080 }
81
Reid Spencer1ef8bda2004-12-30 05:36:08 +000082 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000083 cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000084 if (ErrorMessage.size())
Bill Wendlinge8156192006-12-07 01:30:32 +000085 cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086 else
Bill Wendlinge8156192006-12-07 01:30:32 +000087 cerr << "bytecode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000088 return 1;
89 }
Chris Lattnerc453f762007-04-29 07:54:31 +000090
Chris Lattner4d544cd2007-02-07 04:39:35 +000091 if (DontPrint) {
92 // Just use stdout. We won't actually print anything on it.
93 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer1ef8bda2004-12-30 05:36:08 +000094 if (OutputFilename != "-") { // Not stdout?
95 if (!Force && std::ifstream(OutputFilename.c_str())) {
96 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000097 cerr << argv[0] << ": error opening '" << OutputFilename
98 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000099 } else {
100 Out = new std::ofstream(OutputFilename.c_str());
101 }
102 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000103 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000104 if (InputFilename == "-") {
105 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +0000106 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000107 std::string IFN = InputFilename;
108 int Len = IFN.length();
109 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
110 // Source ends in .bc
111 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
112 } else {
113 OutputFilename = IFN+".ll";
114 }
Chris Lattner697954c2002-01-20 22:54:45 +0000115
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116 if (!Force && std::ifstream(OutputFilename.c_str())) {
117 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +0000118 cerr << argv[0] << ": error opening '" << OutputFilename
119 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000120 } else {
121 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000122
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000123 // Make sure that the Out file gets unlinked from the disk if we get a
124 // SIGINT
125 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
126 }
Chris Lattner697954c2002-01-20 22:54:45 +0000127 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000128 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000129
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000131 cerr << argv[0] << ": error opening " << OutputFilename
132 << ": sending to stdout instead!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 Out = &std::cout;
134 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000135
Chris Lattner5ea56e52005-01-29 17:29:05 +0000136 // All that llvm-dis does is write the assembly to a file.
Chris Lattner4d544cd2007-02-07 04:39:35 +0000137 if (!DontPrint) {
138 PassManager Passes;
139 OStream L(*Out);
140 Passes.add(new PrintModulePass(&L));
141 Passes.run(*M.get());
142 }
Chris Lattner00950542001-06-06 20:29:01 +0000143
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000144 if (Out != &std::cout) {
145 ((std::ofstream*)Out)->close();
146 delete Out;
147 }
148 return 0;
149 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000150 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000151 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000152 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000153 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000154
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000155 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000156}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000157