blob: c355434b4445787a3483d0028822b17899cc7fba [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/PrintModulePass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000025#include "Support/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000027#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner8f367bd2001-07-23 02:35:57 +000029// OutputMode - The different orderings to print basic blocks in...
30enum OutputMode {
Brian Gaeked0fde302003-11-11 22:41:34 +000031 LLVM = 0, // Generate LLVM assembly (the default)
Chris Lattneraa3904f2002-05-09 01:25:55 +000032 c, // Generate C code
Chris Lattner8f367bd2001-07-23 02:35:57 +000033};
34
Brian Gaeked0fde302003-11-11 22:41:34 +000035using namespace llvm;
36
Chris Lattnerc7a09852002-07-25 16:31:09 +000037static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000038InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
39
Chris Lattnerc7a09852002-07-25 16:31:09 +000040static cl::opt<std::string>
Chris Lattner3ce0ea82003-05-31 21:47:16 +000041OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000042 cl::value_desc("filename"));
43
44static cl::opt<bool>
45Force("f", cl::desc("Overwrite output files"));
46
47static cl::opt<enum OutputMode>
48WriteMode(cl::desc("Specify the output format:"),
Chris Lattner6a45da02003-11-12 04:59:59 +000049 cl::values(clEnumValN(LLVM, "llvm", "Output LLVM assembly"),
Brian Gaeked0fde302003-11-11 22:41:34 +000050 clEnumVal(c, "Output C code for program"),
Chris Lattnere8e7a182004-02-13 23:24:46 +000051 0),
52 cl::ReallyHidden);
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 Lattner2f1f8e02004-02-13 23:22:40 +000059 if (WriteMode == c) {
60 std::cerr << "ERROR: llvm-dis no longer contains the C backend. Use 'llc -march=c' instead!\n";
61 exit(1);
62 }
63
Chris Lattner56620da2003-04-16 20:51:36 +000064 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner01794502002-08-31 00:30:15 +000065 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000066 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000067 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000068 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000069 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000070 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000071 return 1;
72 }
73
Chris Lattner1e78f362001-07-23 19:27:24 +000074 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000075 if (OutputFilename != "-") { // Not stdout?
76 if (!Force && std::ifstream(OutputFilename.c_str())) {
77 // If force is not specified, make sure not to overwrite a file!
78 std::cerr << argv[0] << ": error opening '" << OutputFilename
79 << "': file exists! Sending to standard output.\n";
80 } else {
81 Out = new std::ofstream(OutputFilename.c_str());
82 }
Chris Lattner697954c2002-01-20 22:54:45 +000083 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000084 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000085 if (InputFilename == "-") {
86 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000087 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000088 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000089 int Len = IFN.length();
90 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
91 // Source ends in .bc
Chris Lattner2f1f8e02004-02-13 23:22:40 +000092 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
Chris Lattner8f367bd2001-07-23 02:35:57 +000093 } else {
Chris Lattner2f1f8e02004-02-13 23:22:40 +000094 OutputFilename = IFN+".ll";
Chris Lattner00950542001-06-06 20:29:01 +000095 }
Chris Lattner697954c2002-01-20 22:54:45 +000096
Chris Lattner888912d2002-01-22 21:07:24 +000097 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000098 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000099 std::cerr << argv[0] << ": error opening '" << OutputFilename
100 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000101 } else {
102 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000103
Misha Brukman452fea92003-10-10 17:56:49 +0000104 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000105 // SIGINT
106 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +0000107 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000108 }
Chris Lattner00950542001-06-06 20:29:01 +0000109 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000110
111 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000112 std::cerr << argv[0] << ": error opening " << OutputFilename
113 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000114 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000115 }
116
Chris Lattneraa3904f2002-05-09 01:25:55 +0000117 // All that dis does is write the assembly or C out to a file...
118 //
Chris Lattner01794502002-08-31 00:30:15 +0000119 PassManager Passes;
Chris Lattner2f1f8e02004-02-13 23:22:40 +0000120 Passes.add(new PrintModulePass(Out));
Chris Lattner01794502002-08-31 00:30:15 +0000121 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