2003-10-20 17:58:43 +00:00
|
|
|
//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
|
2003-10-20 17:47:21 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-08-28 21:34:13 +00: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 19:55:41 +00:00
|
|
|
// Options:
|
|
|
|
// --help - Output information about command line switches
|
2002-05-09 01:25:55 +00:00
|
|
|
// -c - Print C code instead of LLVM assembly
|
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"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-08-31 00:30:15 +00:00
|
|
|
#include "llvm/Assembly/CWriter.h"
|
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-04-18 19:55:25 +00:00
|
|
|
#include "Support/Signals.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include <fstream>
|
2002-08-31 00:30:15 +00:00
|
|
|
#include <memory>
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
// OutputMode - The different orderings to print basic blocks in...
|
|
|
|
enum OutputMode {
|
2003-11-11 22:41:34 +00:00
|
|
|
LLVM = 0, // Generate LLVM assembly (the default)
|
2002-05-09 01:25:55 +00:00
|
|
|
c, // Generate C code
|
2001-07-23 02:35:57 +00:00
|
|
|
};
|
2001-06-13 19:55:41 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 02:10:13 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
|
|
|
|
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2003-05-31 21:47:16 +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"));
|
|
|
|
|
|
|
|
static cl::opt<enum OutputMode>
|
|
|
|
WriteMode(cl::desc("Specify the output format:"),
|
2003-11-12 04:59:59 +00:00
|
|
|
cl::values(clEnumValN(LLVM, "llvm", "Output LLVM assembly"),
|
2003-11-11 22:41:34 +00:00
|
|
|
clEnumVal(c, "Output C code for program"),
|
2002-07-22 02:10:13 +00:00
|
|
|
0));
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
2003-04-16 20:51:36 +00:00
|
|
|
std::string ErrorMessage;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-04-16 20:51:36 +00:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
|
2002-08-31 00:30:15 +00:00
|
|
|
if (M.get() == 0) {
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << argv[0] << ": ";
|
2003-04-16 20:51:36 +00:00
|
|
|
if (ErrorMessage.size())
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << ErrorMessage << "\n";
|
2003-04-16 20:51:36 +00:00
|
|
|
else
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << "bytecode didn't read correctly.\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-07-23 19:27:24 +00:00
|
|
|
if (OutputFilename != "") { // Specified an output filename?
|
2003-05-31 21:47:16 +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!
|
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists! Sending to standard output.\n";
|
|
|
|
} else {
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
}
|
2002-01-20 22:54:45 +00:00
|
|
|
}
|
2001-07-23 02:35:57 +00:00
|
|
|
} else {
|
2001-07-23 19:27:24 +00:00
|
|
|
if (InputFilename == "-") {
|
|
|
|
OutputFilename = "-";
|
2001-07-23 02:35:57 +00:00
|
|
|
} else {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string IFN = InputFilename;
|
2001-07-23 02:35:57 +00:00
|
|
|
int Len = IFN.length();
|
|
|
|
if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
|
|
|
|
// Source ends in .bc
|
2002-01-20 22:54:45 +00:00
|
|
|
OutputFilename = std::string(IFN.begin(), IFN.end()-3);
|
2001-07-23 02:35:57 +00:00
|
|
|
} else {
|
2001-07-23 19:27:24 +00:00
|
|
|
OutputFilename = IFN; // Append a .ll to it
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2002-05-08 18:09:58 +00:00
|
|
|
if (WriteMode == c)
|
2002-05-09 01:25:55 +00:00
|
|
|
OutputFilename += ".c";
|
2002-05-08 18:09:58 +00:00
|
|
|
else
|
2002-05-09 01:25:55 +00:00
|
|
|
OutputFilename += ".ll";
|
2002-01-20 22:54:45 +00:00
|
|
|
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists! Sending to standard output.\n";
|
2002-01-20 22:54:45 +00:00
|
|
|
} else {
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2002-04-18 19:55:25 +00:00
|
|
|
|
2003-10-10 17:56:49 +00:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
2002-04-18 19:55:25 +00:00
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2002-01-20 22:54:45 +00:00
|
|
|
}
|
2001-07-23 02:35:57 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-07-23 02:35:57 +00:00
|
|
|
|
|
|
|
if (!Out->good()) {
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << argv[0] << ": error opening " << OutputFilename
|
|
|
|
<< ": sending to stdout instead!\n";
|
2002-01-20 22:54:45 +00:00
|
|
|
Out = &std::cout;
|
2001-07-23 02:35:57 +00:00
|
|
|
}
|
|
|
|
|
2002-05-09 01:25:55 +00:00
|
|
|
// All that dis does is write the assembly or C out to a file...
|
|
|
|
//
|
2002-08-31 00:30:15 +00:00
|
|
|
PassManager Passes;
|
|
|
|
|
2002-05-09 01:25:55 +00:00
|
|
|
switch (WriteMode) {
|
2003-11-11 22:41:34 +00:00
|
|
|
case LLVM: // Output LLVM assembly
|
2002-09-19 20:48:48 +00:00
|
|
|
Passes.add(new PrintModulePass(Out));
|
2002-05-09 01:25:55 +00:00
|
|
|
break;
|
2002-09-19 20:48:48 +00:00
|
|
|
case c: // Convert LLVM to C
|
2004-02-13 23:01:14 +00:00
|
|
|
AddPassesToWriteC(Passes, *Out);
|
2002-05-09 01:25:55 +00:00
|
|
|
break;
|
2001-06-13 19:55:41 +00:00
|
|
|
}
|
2002-08-31 00:30:15 +00:00
|
|
|
|
|
|
|
Passes.run(*M.get());
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-09-24 00:09:48 +00:00
|
|
|
if (Out != &std::cout) {
|
|
|
|
((std::ofstream*)Out)->close();
|
|
|
|
delete Out;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-08 18:09:58 +00:00
|
|
|
|