2003-10-20 17:58:43 +00:00
|
|
|
//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2007-07-05 17:07:56 +00:00
|
|
|
// llvm-dis [options] - Read LLVM bitcode from stdin, write asm to stdout
|
|
|
|
// llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
|
2003-08-28 21:34:13 +00:00
|
|
|
// to the x.ll file.
|
2001-06-13 19:55:41 +00:00
|
|
|
// Options:
|
|
|
|
// --help - Output information about command line switches
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2001-10-13 07:06:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-08-31 00:30:15 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2007-04-22 06:31:02 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2002-08-31 00:30:15 +00:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-04-29 07:54:31 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2006-11-28 23:33:06 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2008-10-22 03:25:22 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2006-11-28 23:33:06 +00:00
|
|
|
#include <iostream>
|
2001-11-27 00:03:19 +00:00
|
|
|
#include <fstream>
|
2002-08-31 00:30:15 +00:00
|
|
|
#include <memory>
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2007-07-05 17:07:56 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2005-04-22 00:00:37 +00:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
2002-07-22 02:10:13 +00:00
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
2007-02-07 04:39:35 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
|
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
int main(int argc, char **argv) {
|
2006-12-06 01:18:01 +00:00
|
|
|
llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
|
2004-12-30 05:36:08 +00:00
|
|
|
try {
|
2007-10-08 15:45:12 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
|
2004-12-30 05:36:08 +00:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2004-02-19 20:32:12 +00:00
|
|
|
|
2005-01-29 17:29:05 +00:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout.
|
2004-12-30 05:36:08 +00:00
|
|
|
std::string ErrorMessage;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2007-04-22 06:31:02 +00:00
|
|
|
std::auto_ptr<Module> M;
|
2007-04-22 06:35:20 +00:00
|
|
|
|
2007-05-06 23:45:49 +00:00
|
|
|
if (MemoryBuffer *Buffer
|
|
|
|
= MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
|
2007-05-06 09:29:57 +00:00
|
|
|
M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
|
2007-05-06 23:45:49 +00:00
|
|
|
delete Buffer;
|
|
|
|
}
|
2007-04-29 07:54:31 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (M.get() == 0) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": ";
|
2004-12-30 05:36:08 +00:00
|
|
|
if (ErrorMessage.size())
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << ErrorMessage << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
else
|
2007-07-05 17:07:56 +00:00
|
|
|
cerr << "bitcode didn't read correctly.\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
2002-01-20 22:54:45 +00:00
|
|
|
}
|
2007-04-29 07:54:31 +00:00
|
|
|
|
2007-02-07 04:39:35 +00:00
|
|
|
if (DontPrint) {
|
|
|
|
// Just use stdout. We won't actually print anything on it.
|
|
|
|
} else if (OutputFilename != "") { // Specified an output filename?
|
2004-12-30 05:36:08 +00:00
|
|
|
if (OutputFilename != "-") { // Not stdout?
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists! Sending to standard output.\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
} else {
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2004-12-30 05:36:08 +00:00
|
|
|
} else {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2002-01-20 22:54:45 +00:00
|
|
|
} else {
|
2004-12-30 05:36:08 +00:00
|
|
|
std::string IFN = InputFilename;
|
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
|
|
|
|
// Source ends in .bc
|
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
|
|
|
|
} else {
|
|
|
|
OutputFilename = IFN+".ll";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists! Sending to standard output.\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
} else {
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
|
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
|
|
|
}
|
2002-01-20 22:54:45 +00:00
|
|
|
}
|
2001-07-23 02:35:57 +00:00
|
|
|
}
|
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (!Out->good()) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": error opening " << OutputFilename
|
|
|
|
<< ": sending to stdout instead!\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
Out = &std::cout;
|
|
|
|
}
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2005-01-29 17:29:05 +00:00
|
|
|
// All that llvm-dis does is write the assembly to a file.
|
2007-02-07 04:39:35 +00:00
|
|
|
if (!DontPrint) {
|
|
|
|
PassManager Passes;
|
2008-10-22 03:25:22 +00:00
|
|
|
raw_os_ostream L(*Out);
|
2008-10-21 23:33:38 +00:00
|
|
|
Passes.add(createPrintModulePass(&L));
|
2007-02-07 04:39:35 +00:00
|
|
|
Passes.run(*M.get());
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (Out != &std::cout) {
|
|
|
|
((std::ofstream*)Out)->close();
|
|
|
|
delete Out;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} catch (const std::string& msg) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": " << msg << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
} catch (...) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2002-09-24 00:09:48 +00:00
|
|
|
}
|
2006-12-06 01:18:01 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2002-05-08 18:09:58 +00:00
|
|
|
|