blob: 361564c8e54e9674bf8b2e21892df1ea10dfe21b [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
Chris Lattner5ff62e92002-07-22 02:10:13 +000030static cl::opt<string>
31InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
32
33static cl::opt<string>
34OutputFilename("o", cl::desc("Override output filename"),
35 cl::value_desc("filename"));
36
37static cl::opt<bool>
38Force("f", cl::desc("Overwrite output files"));
39
40static cl::opt<enum OutputMode>
41WriteMode(cl::desc("Specify the output format:"),
42 cl::values(
43 clEnumVal(llvm, "Output LLVM assembly"),
44 clEnumVal(c , "Output C code for program"),
45 0));
Chris Lattner8f367bd2001-07-23 02:35:57 +000046
Chris Lattner00950542001-06-06 20:29:01 +000047int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000048 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
Chris Lattner697954c2002-01-20 22:54:45 +000049 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner00950542001-06-06 20:29:01 +000050
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000051 Module *M = ParseBytecodeFile(InputFilename);
52 if (M == 0) {
Chris Lattner00950542001-06-06 20:29:01 +000053 cerr << "bytecode didn't read correctly.\n";
54 return 1;
55 }
56
Chris Lattner1e78f362001-07-23 19:27:24 +000057 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000058 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000059 // If force is not specified, make sure not to overwrite a file!
60 cerr << "Error opening '" << OutputFilename
61 << "': File exists! Sending to standard output.\n";
62 } else {
63 Out = new std::ofstream(OutputFilename.c_str());
64 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000065 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000066 if (InputFilename == "-") {
67 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000068 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000069 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000070 int Len = IFN.length();
71 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
72 // Source ends in .bc
Chris Lattner697954c2002-01-20 22:54:45 +000073 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000074 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000075 OutputFilename = IFN; // Append a .ll to it
Chris Lattner00950542001-06-06 20:29:01 +000076 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000077 if (WriteMode == c)
Chris Lattneraa3904f2002-05-09 01:25:55 +000078 OutputFilename += ".c";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000079 else
Chris Lattneraa3904f2002-05-09 01:25:55 +000080 OutputFilename += ".ll";
Chris Lattner697954c2002-01-20 22:54:45 +000081
Chris Lattner888912d2002-01-22 21:07:24 +000082 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000083 // If force is not specified, make sure not to overwrite a file!
84 cerr << "Error opening '" << OutputFilename
85 << "': File exists! Sending to standard output.\n";
86 } else {
87 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000088
89 // Make sure that the Out file gets unlink'd from the disk if we get a
90 // SIGINT
91 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +000092 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000093 }
Chris Lattner00950542001-06-06 20:29:01 +000094 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000095
96 if (!Out->good()) {
Chris Lattner1e78f362001-07-23 19:27:24 +000097 cerr << "Error opening " << OutputFilename
Chris Lattner8f367bd2001-07-23 02:35:57 +000098 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +000099 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000100 }
101
Chris Lattneraa3904f2002-05-09 01:25:55 +0000102 // All that dis does is write the assembly or C out to a file...
103 //
104 switch (WriteMode) {
105 case llvm:
106 (*Out) << M; // Output LLVM assembly
107 break;
108 case c:
109 WriteToC(M, *Out); // Convert LLVM to C
110 break;
Chris Lattner9bff2e92001-06-13 19:55:41 +0000111 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000112 delete M;
Chris Lattner00950542001-06-06 20:29:01 +0000113
Chris Lattner697954c2002-01-20 22:54:45 +0000114 if (Out != &std::cout) delete Out;
Chris Lattner00950542001-06-06 20:29:01 +0000115 return 0;
116}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000117