blob: b52354e7127ad08ca261b8247ef777b39bf19a34 [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 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 Lattnerc7a09852002-07-25 16:31:09 +000030static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000031InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
32
Chris Lattnerc7a09852002-07-25 16:31:09 +000033static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000034OutputFilename("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 Lattner56620da2003-04-16 20:51:36 +000050 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000051
Chris Lattner56620da2003-04-16 20:51:36 +000052 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner01794502002-08-31 00:30:15 +000053 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000054 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000055 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000056 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000057 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000058 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000059 return 1;
60 }
61
Chris Lattner1e78f362001-07-23 19:27:24 +000062 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner888912d2002-01-22 21:07:24 +000063 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000064 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000065 std::cerr << argv[0] << ": error opening '" << OutputFilename
66 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +000067 } else {
68 Out = new std::ofstream(OutputFilename.c_str());
69 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000070 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000071 if (InputFilename == "-") {
72 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000073 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000074 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000075 int Len = IFN.length();
76 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
77 // Source ends in .bc
Chris Lattner697954c2002-01-20 22:54:45 +000078 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000079 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000080 OutputFilename = IFN; // Append a .ll to it
Chris Lattner00950542001-06-06 20:29:01 +000081 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000082 if (WriteMode == c)
Chris Lattneraa3904f2002-05-09 01:25:55 +000083 OutputFilename += ".c";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000084 else
Chris Lattneraa3904f2002-05-09 01:25:55 +000085 OutputFilename += ".ll";
Chris Lattner697954c2002-01-20 22:54:45 +000086
Chris Lattner888912d2002-01-22 21:07:24 +000087 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000088 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000089 std::cerr << argv[0] << ": error opening '" << OutputFilename
90 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +000091 } else {
92 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000093
94 // Make sure that the Out file gets unlink'd from the disk if we get a
95 // SIGINT
96 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +000097 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000098 }
Chris Lattner00950542001-06-06 20:29:01 +000099 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000100
101 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000102 std::cerr << argv[0] << ": error opening " << OutputFilename
103 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000104 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000105 }
106
Chris Lattneraa3904f2002-05-09 01:25:55 +0000107 // All that dis does is write the assembly or C out to a file...
108 //
Chris Lattner01794502002-08-31 00:30:15 +0000109 PassManager Passes;
110
Chris Lattneraa3904f2002-05-09 01:25:55 +0000111 switch (WriteMode) {
Chris Lattner01794502002-08-31 00:30:15 +0000112 case llvm: // Output LLVM assembly
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000113 Passes.add(new PrintModulePass(Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000114 break;
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000115 case c: // Convert LLVM to C
Chris Lattner01794502002-08-31 00:30:15 +0000116 Passes.add(createWriteToCPass(*Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000117 break;
Chris Lattner9bff2e92001-06-13 19:55:41 +0000118 }
Chris Lattner01794502002-08-31 00:30:15 +0000119
120 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000121
Chris Lattner93c26682002-09-24 00:09:48 +0000122 if (Out != &std::cout) {
123 ((std::ofstream*)Out)->close();
124 delete Out;
125 }
Chris Lattner00950542001-06-06 20:29:01 +0000126 return 0;
127}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000128