blob: eb9a3a39ed067c898027762762a9ec84e910a414 [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 Lattner00950542001-06-06 20:29:01 +000015#include "llvm/Bytecode/Reader.h"
Chris Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000017#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000018#include "Support/Signals.h"
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000019#include "llvm/Assembly/CWriter.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include <fstream>
Chris Lattner697954c2002-01-20 22:54:45 +000021#include <iostream>
22using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000023
Chris Lattner8f367bd2001-07-23 02:35:57 +000024// OutputMode - The different orderings to print basic blocks in...
25enum OutputMode {
Chris Lattneraa3904f2002-05-09 01:25:55 +000026 llvm = 0, // Generate LLVM assembly (the default)
27 c, // Generate C code
Chris Lattner8f367bd2001-07-23 02:35:57 +000028};
29
30cl::String InputFilename ("", "Load <arg> file, print as assembly", 0, "-");
Chris Lattner1e78f362001-07-23 19:27:24 +000031cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
32cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
Chris Lattner8f367bd2001-07-23 02:35:57 +000033cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
Chris Lattneraa3904f2002-05-09 01:25:55 +000034 clEnumVal(llvm, "Output LLVM assembly"),
35 clEnumVal(c , "Output C code for program"),
Chris Lattner0f683682001-07-23 02:54:25 +000036 0);
Chris Lattner8f367bd2001-07-23 02:35:57 +000037
Chris Lattner00950542001-06-06 20:29:01 +000038int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000039 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
Chris Lattner697954c2002-01-20 22:54:45 +000040 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000042 Module *M = ParseBytecodeFile(InputFilename);
43 if (M == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000044 cerr << "bytecode didn't read correctly.\n";
45 return 1;
46 }
47
Chris Lattner1e78f362001-07-23 19:27:24 +000048 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000049 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000050 // If force is not specified, make sure not to overwrite a file!
51 cerr << "Error opening '" << OutputFilename
52 << "': File exists! Sending to standard output.\n";
53 } else {
54 Out = new std::ofstream(OutputFilename.c_str());
55 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000056 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000057 if (InputFilename == "-") {
58 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000059 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000060 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000061 int Len = IFN.length();
62 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
63 // Source ends in .bc
Chris Lattner697954c2002-01-20 22:54:45 +000064 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000065 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000066 OutputFilename = IFN; // Append a .ll to it
Chris Lattner00950542001-06-06 20:29:01 +000067 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000068 if (WriteMode == c)
Chris Lattneraa3904f2002-05-09 01:25:55 +000069 OutputFilename += ".c";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000070 else
Chris Lattneraa3904f2002-05-09 01:25:55 +000071 OutputFilename += ".ll";
Chris Lattner697954c2002-01-20 22:54:45 +000072
Chris Lattner888912d2002-01-22 21:07:24 +000073 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000074 // If force is not specified, make sure not to overwrite a file!
75 cerr << "Error opening '" << OutputFilename
76 << "': File exists! Sending to standard output.\n";
77 } else {
78 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000079
80 // Make sure that the Out file gets unlink'd from the disk if we get a
81 // SIGINT
82 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +000083 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000084 }
Chris Lattner00950542001-06-06 20:29:01 +000085 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000086
87 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +000088 cerr << "Error opening " << OutputFilename
Chris Lattner8f367bd2001-07-23 02:35:57 +000089 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +000090 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +000091 }
92
Chris Lattneraa3904f2002-05-09 01:25:55 +000093 // All that dis does is write the assembly or C out to a file...
94 //
95 switch (WriteMode) {
96 case llvm:
97 (*Out) << M; // Output LLVM assembly
98 break;
99 case c:
100 WriteToC(M, *Out); // Convert LLVM to C
101 break;
Chris Lattner9bff2e92001-06-13 19:55:41 +0000102 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000103 delete M;
Chris Lattner00950542001-06-06 20:29:01 +0000104
Chris Lattner697954c2002-01-20 22:54:45 +0000105 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000106 return 0;
107}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000108