blob: 38b863c20c8276fe5d073696a2d8b468b7ae0f3b [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"
Chris Lattnerf2e292c2007-02-07 21:41:02 +000023#include "llvm/Support/Compressor.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000026#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000028#include <iostream>
Chris Lattnercee8f9a2001-11-27 00:03:19 +000029#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000030#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattnerc7a09852002-07-25 16:31:09 +000033static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000034InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
35
Chris Lattnerc7a09852002-07-25 16:31:09 +000036static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000037OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000038 cl::value_desc("filename"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
42
Chris Lattner4d544cd2007-02-07 04:39:35 +000043static cl::opt<bool>
44DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
45
Chris Lattner00950542001-06-06 20:29:01 +000046int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000047 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000048 try {
49 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
50 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000051
Chris Lattner5ea56e52005-01-29 17:29:05 +000052 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000053 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000054
Chris Lattnerf2e292c2007-02-07 21:41:02 +000055 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename,
56 Compressor::decompressToNewBuffer,
57 &ErrorMessage));
Reid Spencer1ef8bda2004-12-30 05:36:08 +000058 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000059 cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000060 if (ErrorMessage.size())
Bill Wendlinge8156192006-12-07 01:30:32 +000061 cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000062 else
Bill Wendlinge8156192006-12-07 01:30:32 +000063 cerr << "bytecode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000064 return 1;
65 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000066
Chris Lattner4d544cd2007-02-07 04:39:35 +000067 if (DontPrint) {
68 // Just use stdout. We won't actually print anything on it.
69 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer1ef8bda2004-12-30 05:36:08 +000070 if (OutputFilename != "-") { // Not stdout?
71 if (!Force && std::ifstream(OutputFilename.c_str())) {
72 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000073 cerr << argv[0] << ": error opening '" << OutputFilename
74 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000075 } else {
76 Out = new std::ofstream(OutputFilename.c_str());
77 }
78 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000079 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000080 if (InputFilename == "-") {
81 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000082 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000083 std::string IFN = InputFilename;
84 int Len = IFN.length();
85 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
86 // Source ends in .bc
87 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
88 } else {
89 OutputFilename = IFN+".ll";
90 }
Chris Lattner697954c2002-01-20 22:54:45 +000091
Reid Spencer1ef8bda2004-12-30 05:36:08 +000092 if (!Force && std::ifstream(OutputFilename.c_str())) {
93 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000094 cerr << argv[0] << ": error opening '" << OutputFilename
95 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000096 } else {
97 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000098
Reid Spencer1ef8bda2004-12-30 05:36:08 +000099 // Make sure that the Out file gets unlinked from the disk if we get a
100 // SIGINT
101 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
102 }
Chris Lattner697954c2002-01-20 22:54:45 +0000103 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000104 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000105
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000106 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000107 cerr << argv[0] << ": error opening " << OutputFilename
108 << ": sending to stdout instead!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000109 Out = &std::cout;
110 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000111
Chris Lattner5ea56e52005-01-29 17:29:05 +0000112 // All that llvm-dis does is write the assembly to a file.
Chris Lattner4d544cd2007-02-07 04:39:35 +0000113 if (!DontPrint) {
114 PassManager Passes;
115 OStream L(*Out);
116 Passes.add(new PrintModulePass(&L));
117 Passes.run(*M.get());
118 }
Chris Lattner00950542001-06-06 20:29:01 +0000119
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000120 if (Out != &std::cout) {
121 ((std::ofstream*)Out)->close();
122 delete Out;
123 }
124 return 0;
125 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000126 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000127 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000128 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000129 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000130
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000131 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000132}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000133