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
|
|
|
|
// -dfo - Print basic blocks in depth first order
|
|
|
|
// -rdfo - Print basic blocks in reverse depth first order
|
|
|
|
// -po - Print basic blocks in post order
|
|
|
|
// -rpo - Print basic blocks in reverse post order
|
2002-05-08 18:09:58 +00:00
|
|
|
//
|
|
|
|
// -c - Print C code
|
2001-06-13 19:55:41 +00:00
|
|
|
//
|
|
|
|
// TODO: add -vcg which prints VCG compatible output.
|
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/DepthFirstIterator.h"
|
|
|
|
#include "Support/PostOrderIterator.h"
|
|
|
|
#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-04-07 20:49:59 +00:00
|
|
|
Default = 0, // Function Order (list order)
|
2001-07-23 02:35:57 +00:00
|
|
|
dfo, // Depth First ordering
|
|
|
|
rdfo, // Reverse Depth First ordering
|
|
|
|
po, // Post Order
|
|
|
|
rpo, // Reverse Post Order
|
2002-05-08 18:09:58 +00:00
|
|
|
|
|
|
|
c, // Generate C code
|
2001-07-23 02:35:57 +00:00
|
|
|
};
|
2001-06-13 19:55:41 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
|
2001-07-23 19:27:24 +00:00
|
|
|
cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
|
|
|
|
cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
|
2001-07-23 02:54:25 +00:00
|
|
|
clEnumVal(Default, "Write basic blocks in bytecode order"),
|
|
|
|
clEnumVal(dfo , "Write basic blocks in depth first order"),
|
|
|
|
clEnumVal(rdfo , "Write basic blocks in reverse DFO"),
|
|
|
|
clEnumVal(po , "Write basic blocks in postorder"),
|
|
|
|
clEnumVal(rpo , "Write basic blocks in reverse postorder"),
|
2002-05-08 18:09:58 +00:00
|
|
|
|
|
|
|
clEnumVal(c , "Write corresponding C code"),
|
2001-07-23 02:54:25 +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...
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
Module *M = ParseBytecodeFile(InputFilename);
|
|
|
|
if (M == 0) {
|
2001-06-06 20:29:01 +00:00
|
|
|
cerr << "bytecode didn't read correctly.\n";
|
|
|
|
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!
|
|
|
|
cerr << "Error opening '" << OutputFilename
|
|
|
|
<< "': File exists! Sending to standard output.\n";
|
|
|
|
} 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)
|
|
|
|
OutputFilename += ".c";
|
|
|
|
else
|
|
|
|
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!
|
|
|
|
cerr << "Error opening '" << OutputFilename
|
|
|
|
<< "': File exists! Sending to standard output.\n";
|
|
|
|
} 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()) {
|
2001-07-23 19:27:24 +00:00
|
|
|
cerr << "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-08 18:09:58 +00:00
|
|
|
// All that dis does is write the assembly or C out to a file... which is
|
|
|
|
// exactly what the writer or cwriter library is supposed to do...
|
2001-07-23 19:27:24 +00:00
|
|
|
if (WriteMode == Default) {
|
2002-04-07 20:49:59 +00:00
|
|
|
(*Out) << M; // Print out in list order
|
2002-05-08 18:09:58 +00:00
|
|
|
} else if (WriteMode == c) {
|
|
|
|
WriteToC(M, *Out);
|
2001-06-13 19:55:41 +00:00
|
|
|
} else {
|
|
|
|
// TODO: This does not print anything other than the basic blocks in the
|
2002-04-07 20:49:59 +00:00
|
|
|
// functions... more should definately be printed. It should be valid
|
|
|
|
// output consumable by the assembler.
|
2001-06-13 19:55:41 +00:00
|
|
|
//
|
2002-04-07 20:49:59 +00:00
|
|
|
for (Module::iterator I = M->begin(), End = M->end(); I != End; ++I) {
|
|
|
|
Function *F = *I;
|
|
|
|
(*Out) << "-------------- Method: " << F->getName() << " -------------\n";
|
2001-06-13 19:55:41 +00:00
|
|
|
|
2001-07-23 19:27:24 +00:00
|
|
|
switch (WriteMode) {
|
2001-07-23 02:35:57 +00:00
|
|
|
case dfo: // Depth First ordering
|
2002-04-07 20:49:59 +00:00
|
|
|
copy(df_begin(F), df_end(F),
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream_iterator<BasicBlock*>(*Out, "\n"));
|
2001-06-13 19:55:41 +00:00
|
|
|
break;
|
2001-07-23 02:35:57 +00:00
|
|
|
case rdfo: // Reverse Depth First ordering
|
2002-04-07 20:49:59 +00:00
|
|
|
copy(df_begin(F, true), df_end(F),
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream_iterator<BasicBlock*>(*Out, "\n"));
|
2001-06-13 19:55:41 +00:00
|
|
|
break;
|
2001-07-23 02:35:57 +00:00
|
|
|
case po: // Post Order
|
2002-04-07 20:49:59 +00:00
|
|
|
copy(po_begin(F), po_end(F),
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream_iterator<BasicBlock*>(*Out, "\n"));
|
2001-06-13 19:55:41 +00:00
|
|
|
break;
|
2001-07-23 02:35:57 +00:00
|
|
|
case rpo: { // Reverse Post Order
|
2002-02-24 23:03:37 +00:00
|
|
|
#if 0 // FIXME, GCC 3.0.4 bug
|
2002-04-07 20:49:59 +00:00
|
|
|
ReversePostOrderTraversal<Function*> RPOT(F);
|
2001-06-13 19:55:41 +00:00
|
|
|
copy(RPOT.begin(), RPOT.end(),
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream_iterator<BasicBlock*>(*Out, "\n"));
|
2002-02-24 23:03:37 +00:00
|
|
|
#endif
|
2001-06-13 19:55:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|