blob: e4ed2b32ca94edb5e7bbedb42ea8f5ec580e9a36 [file] [log] [blame]
Chris Lattner1dd27b12003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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 Lattner00950542001-06-06 20:29:01 +000016//
Chris Lattner1d7b50b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000018
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Chris Lattner01794502002-08-31 00:30:15 +000020#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/Bytecode/Reader.h"
Chris Lattner01794502002-08-31 00:30:15 +000022#include "llvm/Assembly/PrintModulePass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000025#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000026#include "llvm/System/Signals.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000027#include <iostream>
Chris Lattnercee8f9a2001-11-27 00:03:19 +000028#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000029#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000030using namespace llvm;
31
Chris Lattnerc7a09852002-07-25 16:31:09 +000032static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000033InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
34
Chris Lattnerc7a09852002-07-25 16:31:09 +000035static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000036OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000037 cl::value_desc("filename"));
38
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
41
Chris Lattner4d544cd2007-02-07 04:39:35 +000042static cl::opt<bool>
43DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
44
Chris Lattner00950542001-06-06 20:29:01 +000045int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000046 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000047 try {
48 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
49 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000050
Chris Lattner5ea56e52005-01-29 17:29:05 +000051 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000052 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000053
Reid Spencer1ef8bda2004-12-30 05:36:08 +000054 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
55 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000056 cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000057 if (ErrorMessage.size())
Bill Wendlinge8156192006-12-07 01:30:32 +000058 cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000059 else
Bill Wendlinge8156192006-12-07 01:30:32 +000060 cerr << "bytecode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000061 return 1;
62 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000063
Chris Lattner4d544cd2007-02-07 04:39:35 +000064 if (DontPrint) {
65 // Just use stdout. We won't actually print anything on it.
66 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer1ef8bda2004-12-30 05:36:08 +000067 if (OutputFilename != "-") { // Not stdout?
68 if (!Force && std::ifstream(OutputFilename.c_str())) {
69 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000070 cerr << argv[0] << ": error opening '" << OutputFilename
71 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000072 } else {
73 Out = new std::ofstream(OutputFilename.c_str());
74 }
75 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000076 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000077 if (InputFilename == "-") {
78 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000079 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000080 std::string IFN = InputFilename;
81 int Len = IFN.length();
82 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
83 // Source ends in .bc
84 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
85 } else {
86 OutputFilename = IFN+".ll";
87 }
Chris Lattner697954c2002-01-20 22:54:45 +000088
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 if (!Force && std::ifstream(OutputFilename.c_str())) {
90 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000091 cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000093 } else {
94 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000095
Reid Spencer1ef8bda2004-12-30 05:36:08 +000096 // Make sure that the Out file gets unlinked from the disk if we get a
97 // SIGINT
98 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
99 }
Chris Lattner697954c2002-01-20 22:54:45 +0000100 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000101 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000102
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000103 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000104 cerr << argv[0] << ": error opening " << OutputFilename
105 << ": sending to stdout instead!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000106 Out = &std::cout;
107 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000108
Chris Lattner5ea56e52005-01-29 17:29:05 +0000109 // All that llvm-dis does is write the assembly to a file.
Chris Lattner4d544cd2007-02-07 04:39:35 +0000110 if (!DontPrint) {
111 PassManager Passes;
112 OStream L(*Out);
113 Passes.add(new PrintModulePass(&L));
114 Passes.run(*M.get());
115 }
Chris Lattner00950542001-06-06 20:29:01 +0000116
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117 if (Out != &std::cout) {
118 ((std::ofstream*)Out)->close();
119 delete Out;
120 }
121 return 0;
122 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000123 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000124 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000125 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000126 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000127
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000128 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000129}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000130