blob: a48dd7eb1a0fe0e5dcb168ec338775f615513962 [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 Lattner56620da2003-04-16 20:51:36 +000051 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000052
Chris Lattner56620da2003-04-16 20:51:36 +000053 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner01794502002-08-31 00:30:15 +000054 if (M.get() == 0) {
Chris Lattner56620da2003-04-16 20:51:36 +000055 cerr << argv[0] << ": ";
56 if (ErrorMessage.size())
57 cerr << ErrorMessage << "\n";
58 else
59 cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000060 return 1;
61 }
62
Chris Lattner1e78f362001-07-23 19:27:24 +000063 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000064 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000065 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerfa03cc72002-07-30 19:52:14 +000066 cerr << argv[0] << ": error opening '" << OutputFilename
67 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +000068 } else {
69 Out = new std::ofstream(OutputFilename.c_str());
70 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000071 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000072 if (InputFilename == "-") {
73 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000074 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000075 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000076 int Len = IFN.length();
77 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
78 // Source ends in .bc
Chris Lattner697954c2002-01-20 22:54:45 +000079 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000080 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000081 OutputFilename = IFN; // Append a .ll to it
Chris Lattner00950542001-06-06 20:29:01 +000082 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000083 if (WriteMode == c)
Chris Lattneraa3904f2002-05-09 01:25:55 +000084 OutputFilename += ".c";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000085 else
Chris Lattneraa3904f2002-05-09 01:25:55 +000086 OutputFilename += ".ll";
Chris Lattner697954c2002-01-20 22:54:45 +000087
Chris Lattner888912d2002-01-22 21:07:24 +000088 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000089 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerfa03cc72002-07-30 19:52:14 +000090 cerr << argv[0] << ": error opening '" << OutputFilename
91 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +000092 } else {
93 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000094
95 // Make sure that the Out file gets unlink'd from the disk if we get a
96 // SIGINT
97 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +000098 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000099 }
Chris Lattner00950542001-06-06 20:29:01 +0000100 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000101
102 if (!Out->good()) {
Chris Lattnerfa03cc72002-07-30 19:52:14 +0000103 cerr << argv[0] << ": error opening " << OutputFilename
Chris Lattner8f367bd2001-07-23 02:35:57 +0000104 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000105 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000106 }
107
Chris Lattneraa3904f2002-05-09 01:25:55 +0000108 // All that dis does is write the assembly or C out to a file...
109 //
Chris Lattner01794502002-08-31 00:30:15 +0000110 PassManager Passes;
111
Chris Lattneraa3904f2002-05-09 01:25:55 +0000112 switch (WriteMode) {
Chris Lattner01794502002-08-31 00:30:15 +0000113 case llvm: // Output LLVM assembly
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000114 Passes.add(new PrintModulePass(Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000115 break;
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000116 case c: // Convert LLVM to C
Chris Lattner01794502002-08-31 00:30:15 +0000117 Passes.add(createWriteToCPass(*Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000118 break;
Chris Lattner9bff2e92001-06-13 19:55:41 +0000119 }
Chris Lattner01794502002-08-31 00:30:15 +0000120
121 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000122
Chris Lattner93c26682002-09-24 00:09:48 +0000123 if (Out != &std::cout) {
124 ((std::ofstream*)Out)->close();
125 delete Out;
126 }
Chris Lattner00950542001-06-06 20:29:01 +0000127 return 0;
128}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000129