blob: 70f8f5103b4df83f0a0595ae78c54dab386eced6 [file] [log] [blame]
Chris Lattner1d7b50b2001-10-13 07:06:57 +00001//===----------------------------------------------------------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner00950542001-06-06 20:29:01 +000010// LLVM 'DIS' UTILITY
11//
12// This utility may be invoked in the following manner:
Misha Brukmanad0bf0f2003-08-28 21:34:13 +000013// llvm-dis [options] - Read LLVM bytecode from stdin, write asm to stdout
14// llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
15// to the x.ll file.
Chris Lattner9bff2e92001-06-13 19:55:41 +000016// Options:
17// --help - Output information about command line switches
Chris Lattneraa3904f2002-05-09 01:25:55 +000018// -c - Print C code instead of LLVM assembly
Chris Lattner00950542001-06-06 20:29:01 +000019//
Chris Lattner1d7b50b2001-10-13 07:06:57 +000020//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Module.h"
Chris Lattner01794502002-08-31 00:30:15 +000023#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/Bytecode/Reader.h"
Chris Lattner01794502002-08-31 00:30:15 +000025#include "llvm/Assembly/CWriter.h"
26#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000028#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000030#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000031
Chris Lattner8f367bd2001-07-23 02:35:57 +000032// OutputMode - The different orderings to print basic blocks in...
33enum OutputMode {
Chris Lattneraa3904f2002-05-09 01:25:55 +000034 llvm = 0, // Generate LLVM assembly (the default)
35 c, // Generate C code
Chris Lattner8f367bd2001-07-23 02:35:57 +000036};
37
Chris Lattnerc7a09852002-07-25 16:31:09 +000038static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000039InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
40
Chris Lattnerc7a09852002-07-25 16:31:09 +000041static cl::opt<std::string>
Chris Lattner3ce0ea82003-05-31 21:47:16 +000042OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000043 cl::value_desc("filename"));
44
45static cl::opt<bool>
46Force("f", cl::desc("Overwrite output files"));
47
48static cl::opt<enum OutputMode>
49WriteMode(cl::desc("Specify the output format:"),
Misha Brukmanad0bf0f2003-08-28 21:34:13 +000050 cl::values(clEnumVal(llvm, "Output LLVM assembly"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000051 clEnumVal(c , "Output C code for program"),
52 0));
Chris Lattner8f367bd2001-07-23 02:35:57 +000053
Chris Lattner00950542001-06-06 20:29:01 +000054int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000055 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
Chris Lattner697954c2002-01-20 22:54:45 +000056 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner56620da2003-04-16 20:51:36 +000057 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000058
Chris Lattner56620da2003-04-16 20:51:36 +000059 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner01794502002-08-31 00:30:15 +000060 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000061 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000062 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000063 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000064 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000065 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000066 return 1;
67 }
68
Chris Lattner1e78f362001-07-23 19:27:24 +000069 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000070 if (OutputFilename != "-") { // Not stdout?
71 if (!Force && std::ifstream(OutputFilename.c_str())) {
72 // If force is not specified, make sure not to overwrite a file!
73 std::cerr << argv[0] << ": error opening '" << OutputFilename
74 << "': file exists! Sending to standard output.\n";
75 } else {
76 Out = new std::ofstream(OutputFilename.c_str());
77 }
Chris Lattner697954c2002-01-20 22:54:45 +000078 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000079 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000080 if (InputFilename == "-") {
81 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000082 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000083 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000084 int Len = IFN.length();
85 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
86 // Source ends in .bc
Chris Lattner697954c2002-01-20 22:54:45 +000087 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000088 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000089 OutputFilename = IFN; // Append a .ll to it
Chris Lattner00950542001-06-06 20:29:01 +000090 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000091 if (WriteMode == c)
Chris Lattneraa3904f2002-05-09 01:25:55 +000092 OutputFilename += ".c";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000093 else
Chris Lattneraa3904f2002-05-09 01:25:55 +000094 OutputFilename += ".ll";
Chris Lattner697954c2002-01-20 22:54:45 +000095
Chris Lattner888912d2002-01-22 21:07:24 +000096 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000097 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000098 std::cerr << argv[0] << ": error opening '" << OutputFilename
99 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000100 } else {
101 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000102
Misha Brukman452fea92003-10-10 17:56:49 +0000103 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000104 // SIGINT
105 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +0000106 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000107 }
Chris Lattner00950542001-06-06 20:29:01 +0000108 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000109
110 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000111 std::cerr << argv[0] << ": error opening " << OutputFilename
112 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000113 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000114 }
115
Chris Lattneraa3904f2002-05-09 01:25:55 +0000116 // All that dis does is write the assembly or C out to a file...
117 //
Chris Lattner01794502002-08-31 00:30:15 +0000118 PassManager Passes;
119
Chris Lattneraa3904f2002-05-09 01:25:55 +0000120 switch (WriteMode) {
Chris Lattner01794502002-08-31 00:30:15 +0000121 case llvm: // Output LLVM assembly
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000122 Passes.add(new PrintModulePass(Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000123 break;
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000124 case c: // Convert LLVM to C
Chris Lattner01794502002-08-31 00:30:15 +0000125 Passes.add(createWriteToCPass(*Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000126 break;
Chris Lattner9bff2e92001-06-13 19:55:41 +0000127 }
Chris Lattner01794502002-08-31 00:30:15 +0000128
129 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000130
Chris Lattner93c26682002-09-24 00:09:48 +0000131 if (Out != &std::cout) {
132 ((std::ofstream*)Out)->close();
133 delete Out;
134 }
Chris Lattner00950542001-06-06 20:29:01 +0000135 return 0;
136}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000137