blob: 8c2b3fe2e789b4800fb5f88d265bf66bc69c4e01 [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"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000024#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000026#include <iostream>
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000028#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Chris Lattnerc7a09852002-07-25 16:31:09 +000031static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000032InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
33
Chris Lattnerc7a09852002-07-25 16:31:09 +000034static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000035OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000036 cl::value_desc("filename"));
37
38static cl::opt<bool>
39Force("f", cl::desc("Overwrite output files"));
40
Chris Lattner00950542001-06-06 20:29:01 +000041int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000042 try {
43 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
44 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000045
Chris Lattner5ea56e52005-01-29 17:29:05 +000046 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000047 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000048
Reid Spencer1ef8bda2004-12-30 05:36:08 +000049 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
50 if (M.get() == 0) {
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000051 llvm_cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000052 if (ErrorMessage.size())
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000053 llvm_cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000054 else
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000055 llvm_cerr << "bytecode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000056 return 1;
57 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000058
Reid Spencer1ef8bda2004-12-30 05:36:08 +000059 if (OutputFilename != "") { // Specified an output filename?
60 if (OutputFilename != "-") { // Not stdout?
61 if (!Force && std::ifstream(OutputFilename.c_str())) {
62 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000063 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
Reid Spencer1ef8bda2004-12-30 05:36:08 +000064 << "': file exists! Sending to standard output.\n";
65 } else {
66 Out = new std::ofstream(OutputFilename.c_str());
67 }
68 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000069 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000070 if (InputFilename == "-") {
71 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000072 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000073 std::string IFN = InputFilename;
74 int Len = IFN.length();
75 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
76 // Source ends in .bc
77 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
78 } else {
79 OutputFilename = IFN+".ll";
80 }
Chris Lattner697954c2002-01-20 22:54:45 +000081
Reid Spencer1ef8bda2004-12-30 05:36:08 +000082 if (!Force && std::ifstream(OutputFilename.c_str())) {
83 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000084 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
Reid Spencer1ef8bda2004-12-30 05:36:08 +000085 << "': file exists! Sending to standard output.\n";
86 } else {
87 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000088
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 // Make sure that the Out file gets unlinked from the disk if we get a
90 // SIGINT
91 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
92 }
Chris Lattner697954c2002-01-20 22:54:45 +000093 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000094 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000095
Reid Spencer1ef8bda2004-12-30 05:36:08 +000096 if (!Out->good()) {
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000097 llvm_cerr << argv[0] << ": error opening " << OutputFilename
Reid Spencer1ef8bda2004-12-30 05:36:08 +000098 << ": sending to stdout instead!\n";
99 Out = &std::cout;
100 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000101
Chris Lattner5ea56e52005-01-29 17:29:05 +0000102 // All that llvm-dis does is write the assembly to a file.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000103 PassManager Passes;
Bill Wendlinga5b31ca2006-11-28 23:33:06 +0000104 llvm_ostream L(*Out);
105 Passes.add(new PrintModulePass(&L));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000106 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000107
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000108 if (Out != &std::cout) {
109 ((std::ofstream*)Out)->close();
110 delete Out;
111 }
112 return 0;
113 } catch (const std::string& msg) {
Bill Wendlinga5b31ca2006-11-28 23:33:06 +0000114 llvm_cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000115 } catch (...) {
Bill Wendlinga5b31ca2006-11-28 23:33:06 +0000116 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000117 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000118 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000119}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000120