blob: d9c175166bd1304c841f946516648484ab2785f3 [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 Lattnerf73b4ca2004-02-19 20:32:12 +000056 PrintStackTraceOnErrorSignal();
57
Chris Lattner697954c2002-01-20 22:54:45 +000058 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner56620da2003-04-16 20:51:36 +000059 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000060
Chris Lattner2f1f8e02004-02-13 23:22:40 +000061 if (WriteMode == c) {
Misha Brukmandc5feab2004-02-13 23:46:47 +000062 std::cerr << "ERROR: llvm-dis no longer contains the C backend. "
63 << "Use 'llc -march=c' instead!\n";
Chris Lattner2f1f8e02004-02-13 23:22:40 +000064 exit(1);
65 }
66
Chris Lattner56620da2003-04-16 20:51:36 +000067 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner01794502002-08-31 00:30:15 +000068 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000069 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000070 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000071 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000072 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000073 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000074 return 1;
75 }
76
Chris Lattner1e78f362001-07-23 19:27:24 +000077 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner3ce0ea82003-05-31 21:47:16 +000078 if (OutputFilename != "-") { // Not stdout?
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
81 std::cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists! Sending to standard output.\n";
83 } else {
84 Out = new std::ofstream(OutputFilename.c_str());
85 }
Chris Lattner697954c2002-01-20 22:54:45 +000086 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000087 } else {
Chris Lattner1e78f362001-07-23 19:27:24 +000088 if (InputFilename == "-") {
89 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000090 } else {
Chris Lattner697954c2002-01-20 22:54:45 +000091 std::string IFN = InputFilename;
Chris Lattner8f367bd2001-07-23 02:35:57 +000092 int Len = IFN.length();
93 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
94 // Source ends in .bc
Chris Lattner2f1f8e02004-02-13 23:22:40 +000095 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
Chris Lattner8f367bd2001-07-23 02:35:57 +000096 } else {
Chris Lattner2f1f8e02004-02-13 23:22:40 +000097 OutputFilename = IFN+".ll";
Chris Lattner00950542001-06-06 20:29:01 +000098 }
Chris Lattner697954c2002-01-20 22:54:45 +000099
Chris Lattner888912d2002-01-22 21:07:24 +0000100 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000101 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000102 std::cerr << argv[0] << ": error opening '" << OutputFilename
103 << "': file exists! Sending to standard output.\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000104 } else {
105 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000106
Misha Brukman452fea92003-10-10 17:56:49 +0000107 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000108 // SIGINT
109 RemoveFileOnSignal(OutputFilename);
Chris Lattner697954c2002-01-20 22:54:45 +0000110 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000111 }
Chris Lattner00950542001-06-06 20:29:01 +0000112 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000113
114 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000115 std::cerr << argv[0] << ": error opening " << OutputFilename
116 << ": sending to stdout instead!\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000117 Out = &std::cout;
Chris Lattner8f367bd2001-07-23 02:35:57 +0000118 }
119
Chris Lattneraa3904f2002-05-09 01:25:55 +0000120 // All that dis does is write the assembly or C out to a file...
121 //
Chris Lattner01794502002-08-31 00:30:15 +0000122 PassManager Passes;
Chris Lattner2f1f8e02004-02-13 23:22:40 +0000123 Passes.add(new PrintModulePass(Out));
Chris Lattner01794502002-08-31 00:30:15 +0000124 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000125
Chris Lattner93c26682002-09-24 00:09:48 +0000126 if (Out != &std::cout) {
127 ((std::ofstream*)Out)->close();
128 delete Out;
129 }
Chris Lattner00950542001-06-06 20:29:01 +0000130 return 0;
131}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000132