blob: 8d1749c3d14012d4d9efb1311e0b1214c8f68b18 [file] [log] [blame]
Chris Lattner1d7b50b2001-10-13 07:06:57 +00001//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002// LLVM 'DIS' UTILITY
3//
4// This utility may be invoked in the following manner:
Chris Lattner00950542001-06-06 20:29:01 +00005// dis [options] - Read LLVM bytecode from stdin, write assembly to stdout
6// dis [options] x.bc - Read LLVM bytecode from the x.bc file, write assembly
7// to the x.ll file.
Chris Lattner9bff2e92001-06-13 19:55:41 +00008// Options:
9// --help - Output information about command line switches
Chris Lattneraa3904f2002-05-09 01:25:55 +000010// -c - Print C code instead of LLVM assembly
Chris Lattner00950542001-06-06 20:29:01 +000011//
Chris Lattner1d7b50b2001-10-13 07:06:57 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner01794502002-08-31 00:30:15 +000015#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Bytecode/Reader.h"
Chris Lattner01794502002-08-31 00:30:15 +000017#include "llvm/Assembly/CWriter.h"
18#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000020#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000022#include <memory>
Chris Lattner697954c2002-01-20 22:54:45 +000023using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000024
Chris Lattner8f367bd2001-07-23 02:35:57 +000025// OutputMode - The different orderings to print basic blocks in...
26enum OutputMode {
Chris Lattneraa3904f2002-05-09 01:25:55 +000027 llvm = 0, // Generate LLVM assembly (the default)
28 c, // Generate C code
Chris Lattner8f367bd2001-07-23 02:35:57 +000029};
30
Chris Lattnerc7a09852002-07-25 16:31:09 +000031static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000032InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
33
Chris Lattnerc7a09852002-07-25 16:31:09 +000034static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000035OutputFilename("o", cl::desc("Override output filename"),
36 cl::value_desc("filename"));
37
38static cl::opt<bool>
39Force("f", cl::desc("Overwrite output files"));
40
41static cl::opt<enum OutputMode>
42WriteMode(cl::desc("Specify the output format:"),
43 cl::values(
44 clEnumVal(llvm, "Output LLVM assembly"),
45 clEnumVal(c , "Output C code for program"),
46 0));
Chris Lattner8f367bd2001-07-23 02:35:57 +000047
Chris Lattner00950542001-06-06 20:29:01 +000048int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000049 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
Chris Lattner697954c2002-01-20 22:54:45 +000050 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner00950542001-06-06 20:29:01 +000051
Chris Lattner01794502002-08-31 00:30:15 +000052 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
53 if (M.get() == 0) {
Chris Lattnerfa03cc72002-07-30 19:52:14 +000054 cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000055 return 1;
56 }
57
Chris Lattner1e78f362001-07-23 19:27:24 +000058 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000059 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000060 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerfa03cc72002-07-30 19:52:14 +000061 cerr << argv[0] << ": error opening '" << OutputFilename
62 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +000063 } else {
64 Out = new std::ofstream(OutputFilename.c_str());
65 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000066 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000067 if (InputFilename == "-") {
68 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000069 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000070 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000071 int Len = IFN.length();
72 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
73 // Source ends in .bc
Chris Lattner697954c2002-01-20 22:54:45 +000074 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000075 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000076 OutputFilename = IFN; // Append a .ll to it
Chris Lattner00950542001-06-06 20:29:01 +000077 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000078 if (WriteMode == c)
Chris Lattneraa3904f2002-05-09 01:25:55 +000079 OutputFilename += ".c";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000080 else
Chris Lattneraa3904f2002-05-09 01:25:55 +000081 OutputFilename += ".ll";
Chris Lattner697954c2002-01-20 22:54:45 +000082
Chris Lattner888912d2002-01-22 21:07:24 +000083 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000084 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerfa03cc72002-07-30 19:52:14 +000085 cerr << argv[0] << ": error opening '" << OutputFilename
86 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +000087 } else {
88 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000089
90 // Make sure that the Out file gets unlink'd from the disk if we get a
91 // SIGINT
92 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +000093 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000094 }
Chris Lattner00950542001-06-06 20:29:01 +000095 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000096
97 if (!Out->good()) {
Chris Lattnerfa03cc72002-07-30 19:52:14 +000098 cerr << argv[0] << ": error opening " << OutputFilename
Chris Lattner8f367bd2001-07-23 02:35:57 +000099 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000100 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000101 }
102
Chris Lattneraa3904f2002-05-09 01:25:55 +0000103 // All that dis does is write the assembly or C out to a file...
104 //
Chris Lattner01794502002-08-31 00:30:15 +0000105 PassManager Passes;
106
Chris Lattneraa3904f2002-05-09 01:25:55 +0000107 switch (WriteMode) {
Chris Lattner01794502002-08-31 00:30:15 +0000108 case llvm: // Output LLVM assembly
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000109 Passes.add(new PrintModulePass(Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000110 break;
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000111 case c: // Convert LLVM to C
Chris Lattner01794502002-08-31 00:30:15 +0000112 Passes.add(createWriteToCPass(*Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000113 break;
Chris Lattner9bff2e92001-06-13 19:55:41 +0000114 }
Chris Lattner01794502002-08-31 00:30:15 +0000115
116 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000117
Chris Lattner697954c2002-01-20 22:54:45 +0000118 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000119 return 0;
120}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000121