blob: bb91ad6fce6ed2b3efb7f4a5c5576089b2265281 [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 Lattnerb3ca3622007-04-22 06:31:02 +000021#include "llvm/Bitcode/ReaderWriter.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 Lattnerf2e292c2007-02-07 21:41:02 +000024#include "llvm/Support/Compressor.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000027#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000028#include "llvm/System/Signals.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000029#include <iostream>
Chris Lattnercee8f9a2001-11-27 00:03:19 +000030#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000031#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000032using namespace llvm;
33
Chris Lattnerc7a09852002-07-25 16:31:09 +000034static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000035InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
36
Chris Lattnerc7a09852002-07-25 16:31:09 +000037static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000038OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000039 cl::value_desc("filename"));
40
41static cl::opt<bool>
42Force("f", cl::desc("Overwrite output files"));
43
Chris Lattner4d544cd2007-02-07 04:39:35 +000044static cl::opt<bool>
45DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
46
Chris Lattner00950542001-06-06 20:29:01 +000047int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000048 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000049 try {
50 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
51 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000052
Chris Lattner5ea56e52005-01-29 17:29:05 +000053 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000054 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattnerb3ca3622007-04-22 06:31:02 +000056 std::auto_ptr<Module> M;
Chris Lattner197ccea2007-04-22 06:35:20 +000057
58 if (InputFilename != "-")
59 M.reset(ParseBitcodeFile(InputFilename, &ErrorMessage));
Chris Lattnerb3ca3622007-04-22 06:31:02 +000060
61 if (M.get() == 0)
62 M.reset(ParseBytecodeFile(InputFilename,Compressor::decompressToNewBuffer,
63 &ErrorMessage));
Reid Spencer1ef8bda2004-12-30 05:36:08 +000064 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000065 cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000066 if (ErrorMessage.size())
Bill Wendlinge8156192006-12-07 01:30:32 +000067 cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000068 else
Bill Wendlinge8156192006-12-07 01:30:32 +000069 cerr << "bytecode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000070 return 1;
71 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000072
Chris Lattner4d544cd2007-02-07 04:39:35 +000073 if (DontPrint) {
74 // Just use stdout. We won't actually print anything on it.
75 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer1ef8bda2004-12-30 05:36:08 +000076 if (OutputFilename != "-") { // Not stdout?
77 if (!Force && std::ifstream(OutputFilename.c_str())) {
78 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000079 cerr << argv[0] << ": error opening '" << OutputFilename
80 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000081 } else {
82 Out = new std::ofstream(OutputFilename.c_str());
83 }
84 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000085 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086 if (InputFilename == "-") {
87 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000088 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 std::string IFN = InputFilename;
90 int Len = IFN.length();
91 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
92 // Source ends in .bc
93 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
94 } else {
95 OutputFilename = IFN+".ll";
96 }
Chris Lattner697954c2002-01-20 22:54:45 +000097
Reid Spencer1ef8bda2004-12-30 05:36:08 +000098 if (!Force && std::ifstream(OutputFilename.c_str())) {
99 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +0000100 cerr << argv[0] << ": error opening '" << OutputFilename
101 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000102 } else {
103 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000104
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000105 // Make sure that the Out file gets unlinked from the disk if we get a
106 // SIGINT
107 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
108 }
Chris Lattner697954c2002-01-20 22:54:45 +0000109 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000110 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000111
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000112 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000113 cerr << argv[0] << ": error opening " << OutputFilename
114 << ": sending to stdout instead!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000115 Out = &std::cout;
116 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000117
Chris Lattner5ea56e52005-01-29 17:29:05 +0000118 // All that llvm-dis does is write the assembly to a file.
Chris Lattner4d544cd2007-02-07 04:39:35 +0000119 if (!DontPrint) {
120 PassManager Passes;
121 OStream L(*Out);
122 Passes.add(new PrintModulePass(&L));
123 Passes.run(*M.get());
124 }
Chris Lattner00950542001-06-06 20:29:01 +0000125
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000126 if (Out != &std::cout) {
127 ((std::ofstream*)Out)->close();
128 delete Out;
129 }
130 return 0;
131 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000132 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000133 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000134 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000135 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000136
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000137 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000138}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000139