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"
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-02-12 21:07:25 +00:00
|
|
|
#include "llvm/Support/CFG.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"
|
2002-05-08 18:09:58 +00:00
|
|
|
#include "llvm/Assembly/CWriter.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include <fstream>
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
using std::cerr;
|
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>
|
2002-07-22 02:10:13 +00:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
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...
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
Module *M = ParseBytecodeFile(InputFilename);
|
|
|
|
if (M == 0) {
|
2002-07-30 19:52:14 +00:00
|
|
|
cerr << argv[0] << ": 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?
|
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!
|
2002-07-30 19:52:14 +00:00
|
|
|
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());
|
|
|
|
}
|
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!
|
2002-07-30 19:52:14 +00:00
|
|
|
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()) {
|
2002-07-30 19:52:14 +00:00
|
|
|
cerr << argv[0] << ": error opening " << OutputFilename
|
2001-07-23 02:35:57 +00:00
|
|
|
<< ": 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...
|
|
|
|
//
|
|
|
|
switch (WriteMode) {
|
|
|
|
case llvm:
|
|
|
|
(*Out) << M; // Output LLVM assembly
|
|
|
|
break;
|
|
|
|
case c:
|
|
|
|
WriteToC(M, *Out); // Convert LLVM to C
|
|
|
|
break;
|
2001-06-13 19:55:41 +00:00
|
|
|
}
|
2002-04-07 20:49:59 +00:00
|
|
|
delete M;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
if (Out != &std::cout) delete Out;
|
2001-06-06 20:29:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-05-08 18:09:58 +00:00
|
|
|
|