blob: 915755903bf560c3ccd58e5a64a7f33ea52b91dc [file] [log] [blame]
Chris Lattner1dd27b12003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
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//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Misha Brukmanad0bf0f2003-08-28 21:34:13 +000011// llvm-dis [options] - Read LLVM bytecode from stdin, write asm to stdout
12// llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
13// to the x.ll file.
Chris Lattner9bff2e92001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattneraa3904f2002-05-09 01:25:55 +000016// -c - Print C code instead of LLVM assembly
Chris Lattner00950542001-06-06 20:29:01 +000017//
Chris Lattner1d7b50b2001-10-13 07:06:57 +000018//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000019
Chris Lattner00950542001-06-06 20:29:01 +000020#include "llvm/Module.h"
Chris Lattner01794502002-08-31 00:30:15 +000021#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Bytecode/Reader.h"
Chris Lattner01794502002-08-31 00:30:15 +000023#include "llvm/Assembly/CWriter.h"
24#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000026#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000028#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000029
Chris Lattner8f367bd2001-07-23 02:35:57 +000030// OutputMode - The different orderings to print basic blocks in...
31enum OutputMode {
Chris Lattneraa3904f2002-05-09 01:25:55 +000032 llvm = 0, // Generate LLVM assembly (the default)
33 c, // Generate C code
Chris Lattner8f367bd2001-07-23 02:35:57 +000034};
35
Chris Lattnerc7a09852002-07-25 16:31:09 +000036static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000037InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
38
Chris Lattnerc7a09852002-07-25 16:31:09 +000039static cl::opt<std::string>
Chris Lattner3ce0ea82003-05-31 21:47:16 +000040OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000041 cl::value_desc("filename"));
42
43static cl::opt<bool>
44Force("f", cl::desc("Overwrite output files"));
45
46static cl::opt<enum OutputMode>
47WriteMode(cl::desc("Specify the output format:"),
Misha Brukmanad0bf0f2003-08-28 21:34:13 +000048 cl::values(clEnumVal(llvm, "Output LLVM assembly"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000049 clEnumVal(c , "Output C code for program"),
50 0));
Chris Lattner8f367bd2001-07-23 02:35:57 +000051
Chris Lattner00950542001-06-06 20:29:01 +000052int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000053 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
Chris Lattner697954c2002-01-20 22:54:45 +000054 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner56620da2003-04-16 20:51:36 +000055 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000056
Chris Lattner56620da2003-04-16 20:51:36 +000057 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner01794502002-08-31 00:30:15 +000058 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000059 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000060 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000061 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000062 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000063 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000064 return 1;
65 }
66
Chris Lattner1e78f362001-07-23 19:27:24 +000067 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000068 if (OutputFilename != "-") { // Not stdout?
69 if (!Force && std::ifstream(OutputFilename.c_str())) {
70 // If force is not specified, make sure not to overwrite a file!
71 std::cerr << argv[0] << ": error opening '" << OutputFilename
72 << "': file exists! Sending to standard output.\n";
73 } else {
74 Out = new std::ofstream(OutputFilename.c_str());
75 }
Chris Lattner697954c2002-01-20 22:54:45 +000076 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000077 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000078 if (InputFilename == "-") {
79 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000080 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000081 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000082 int Len = IFN.length();
83 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
84 // Source ends in .bc
Chris Lattner697954c2002-01-20 22:54:45 +000085 OutputFilename = std::string(IFN.begin(), IFN.end()-3);
Chris Lattner8f367bd2001-07-23 02:35:57 +000086 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000087 OutputFilename = IFN; // Append a .ll to it
Chris Lattner00950542001-06-06 20:29:01 +000088 }
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000089 if (WriteMode == c)
Chris Lattneraa3904f2002-05-09 01:25:55 +000090 OutputFilename += ".c";
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +000091 else
Chris Lattneraa3904f2002-05-09 01:25:55 +000092 OutputFilename += ".ll";
Chris Lattner697954c2002-01-20 22:54:45 +000093
Chris Lattner888912d2002-01-22 21:07:24 +000094 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000095 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000096 std::cerr << argv[0] << ": error opening '" << OutputFilename
97 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +000098 } else {
99 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000100
Misha Brukman452fea92003-10-10 17:56:49 +0000101 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000102 // SIGINT
103 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +0000104 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000105 }
Chris Lattner00950542001-06-06 20:29:01 +0000106 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000107
108 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000109 std::cerr << argv[0] << ": error opening " << OutputFilename
110 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000111 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000112 }
113
Chris Lattneraa3904f2002-05-09 01:25:55 +0000114 // All that dis does is write the assembly or C out to a file...
115 //
Chris Lattner01794502002-08-31 00:30:15 +0000116 PassManager Passes;
117
Chris Lattneraa3904f2002-05-09 01:25:55 +0000118 switch (WriteMode) {
Chris Lattner01794502002-08-31 00:30:15 +0000119 case llvm: // Output LLVM assembly
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000120 Passes.add(new PrintModulePass(Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000121 break;
Chris Lattnerb4ee4f92002-09-19 20:48:48 +0000122 case c: // Convert LLVM to C
Chris Lattner01794502002-08-31 00:30:15 +0000123 Passes.add(createWriteToCPass(*Out));
Chris Lattneraa3904f2002-05-09 01:25:55 +0000124 break;
Chris Lattner9bff2e92001-06-13 19:55:41 +0000125 }
Chris Lattner01794502002-08-31 00:30:15 +0000126
127 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000128
Chris Lattner93c26682002-09-24 00:09:48 +0000129 if (Out != &std::cout) {
130 ((std::ofstream*)Out)->close();
131 delete Out;
132 }
Chris Lattner00950542001-06-06 20:29:01 +0000133 return 0;
134}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000135