2003-10-20 19:58:43 +02:00
|
|
|
//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-08-28 23:34:13 +02:00
|
|
|
// llvm-dis [options] - Read LLVM bytecode from stdin, write asm to stdout
|
|
|
|
// llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
|
|
|
|
// to the x.ll file.
|
2001-06-13 21:55:41 +02:00
|
|
|
// Options:
|
|
|
|
// --help - Output information about command line switches
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2001-10-13 09:06:57 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-08-31 02:30:15 +02:00
|
|
|
#include "llvm/PassManager.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-08-31 02:30:15 +02:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-05-27 07:41:36 +02:00
|
|
|
#include "llvm/System/Signals.h"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include <fstream>
|
2002-08-31 02:30:15 +02:00
|
|
|
#include <memory>
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-25 18:31:09 +02:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 04:10:13 +02:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
|
|
|
|
|
2002-07-25 18:31:09 +02:00
|
|
|
static cl::opt<std::string>
|
2005-04-22 02:00:37 +02:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
2002-07-22 04:10:13 +02:00
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
2001-07-23 04:35:57 +02:00
|
|
|
int main(int argc, char **argv) {
|
2004-12-30 06:36:08 +01:00
|
|
|
try {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2004-02-19 21:32:12 +01:00
|
|
|
|
2005-01-29 18:29:05 +01:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout.
|
2004-12-30 06:36:08 +01:00
|
|
|
std::string ErrorMessage;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
|
|
|
|
if (M.get() == 0) {
|
|
|
|
std::cerr << argv[0] << ": ";
|
|
|
|
if (ErrorMessage.size())
|
|
|
|
std::cerr << ErrorMessage << "\n";
|
|
|
|
else
|
|
|
|
std::cerr << "bytecode didn't read correctly.\n";
|
|
|
|
return 1;
|
2002-01-20 23:54:45 +01:00
|
|
|
}
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
if (OutputFilename != "") { // Specified an output filename?
|
|
|
|
if (OutputFilename != "-") { // Not stdout?
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists! Sending to standard output.\n";
|
|
|
|
} else {
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2004-12-30 06:36:08 +01:00
|
|
|
} else {
|
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2002-01-20 23:54:45 +01:00
|
|
|
} else {
|
2004-12-30 06:36:08 +01: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!
|
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists! Sending to standard output.\n";
|
|
|
|
} 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 23:54:45 +01:00
|
|
|
}
|
2001-07-23 04:35:57 +02:00
|
|
|
}
|
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
if (!Out->good()) {
|
|
|
|
std::cerr << argv[0] << ": error opening " << OutputFilename
|
|
|
|
<< ": sending to stdout instead!\n";
|
|
|
|
Out = &std::cout;
|
|
|
|
}
|
2001-07-23 04:35:57 +02:00
|
|
|
|
2005-01-29 18:29:05 +01:00
|
|
|
// All that llvm-dis does is write the assembly to a file.
|
2004-12-30 06:36:08 +01:00
|
|
|
PassManager Passes;
|
|
|
|
Passes.add(new PrintModulePass(Out));
|
|
|
|
Passes.run(*M.get());
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2004-12-30 06:36:08 +01:00
|
|
|
if (Out != &std::cout) {
|
|
|
|
((std::ofstream*)Out)->close();
|
|
|
|
delete Out;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} catch (const std::string& msg) {
|
|
|
|
std::cerr << argv[0] << ": " << msg << "\n";
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2002-09-24 02:09:48 +02:00
|
|
|
}
|
2004-12-30 06:36:08 +01:00
|
|
|
return 1;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2002-05-08 20:09:58 +02:00
|
|
|
|