blob: df0737ad6cef3f13ffa440ba0c8412632e5e8069 [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 Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000026#include <memory>
Chris Lattner00950542001-06-06 20:29:01 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattnerc7a09852002-07-25 16:31:09 +000030static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000031InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
32
Chris Lattnerc7a09852002-07-25 16:31:09 +000033static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000034OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000035 cl::value_desc("filename"));
36
37static cl::opt<bool>
38Force("f", cl::desc("Overwrite output files"));
39
Chris Lattner00950542001-06-06 20:29:01 +000040int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000041 try {
42 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
43 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000044
Chris Lattner5ea56e52005-01-29 17:29:05 +000045 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000046 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000047
Reid Spencer1ef8bda2004-12-30 05:36:08 +000048 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
49 if (M.get() == 0) {
50 std::cerr << argv[0] << ": ";
51 if (ErrorMessage.size())
52 std::cerr << ErrorMessage << "\n";
53 else
54 std::cerr << "bytecode didn't read correctly.\n";
55 return 1;
56 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000057
Reid Spencer1ef8bda2004-12-30 05:36:08 +000058 if (OutputFilename != "") { // Specified an output filename?
59 if (OutputFilename != "-") { // Not stdout?
60 if (!Force && std::ifstream(OutputFilename.c_str())) {
61 // If force is not specified, make sure not to overwrite a file!
62 std::cerr << argv[0] << ": error opening '" << OutputFilename
63 << "': file exists! Sending to standard output.\n";
64 } else {
65 Out = new std::ofstream(OutputFilename.c_str());
66 }
67 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000068 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000069 if (InputFilename == "-") {
70 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000071 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000072 std::string IFN = InputFilename;
73 int Len = IFN.length();
74 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
75 // Source ends in .bc
76 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
77 } else {
78 OutputFilename = IFN+".ll";
79 }
Chris Lattner697954c2002-01-20 22:54:45 +000080
Reid Spencer1ef8bda2004-12-30 05:36:08 +000081 if (!Force && std::ifstream(OutputFilename.c_str())) {
82 // If force is not specified, make sure not to overwrite a file!
83 std::cerr << argv[0] << ": error opening '" << OutputFilename
84 << "': file exists! Sending to standard output.\n";
85 } else {
86 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000087
Reid Spencer1ef8bda2004-12-30 05:36:08 +000088 // Make sure that the Out file gets unlinked from the disk if we get a
89 // SIGINT
90 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
91 }
Chris Lattner697954c2002-01-20 22:54:45 +000092 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000093 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000094
Reid Spencer1ef8bda2004-12-30 05:36:08 +000095 if (!Out->good()) {
96 std::cerr << argv[0] << ": error opening " << OutputFilename
97 << ": sending to stdout instead!\n";
98 Out = &std::cout;
99 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000100
Chris Lattner5ea56e52005-01-29 17:29:05 +0000101 // All that llvm-dis does is write the assembly to a file.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000102 PassManager Passes;
103 Passes.add(new PrintModulePass(Out));
104 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000105
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000106 if (Out != &std::cout) {
107 ((std::ofstream*)Out)->close();
108 delete Out;
109 }
110 return 0;
111 } catch (const std::string& msg) {
112 std::cerr << argv[0] << ": " << msg << "\n";
113 } catch (...) {
114 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000115 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000116 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000117}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000118