2001-10-13 07:06:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
// LLVM 'DIS' UTILITY
|
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
|
|
|
// dis [options] - Read LLVM bytecode from stdin, write assembly to stdout
|
|
|
|
// dis [options] x.bc - Read LLVM bytecode from the x.bc file, write assembly
|
|
|
|
// 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 {
|
2002-05-09 01:25:55 +00:00
|
|
|
llvm = 0, // Generate LLVM assembly (the default)
|
|
|
|
c, // Generate C code
|
2001-07-23 02:35:57 +00:00
|
|
|
};
|
2001-06-13 19:55:41 +00:00
|
|
|
|
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:"),
|
|
|
|
cl::values(
|
|
|
|
clEnumVal(llvm, "Output LLVM assembly"),
|
|
|
|
clEnumVal(c , "Output C code for program"),
|
|
|
|
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
|
|
|
|
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// 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) {
|
2002-08-31 00:30:15 +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
|
2002-08-31 00:30:15 +00:00
|
|
|
Passes.add(createWriteToCPass(*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
|
|
|
|