blob: df0737ad6cef3f13ffa440ba0c8412632e5e8069 [file] [log] [blame]
Chris Lattner8f71f042003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This utility may be invoked in the following manner:
Misha Brukmanf12549d2003-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 Lattnerf284ac52001-06-13 19:55:41 +000014// Options:
15// --help - Output information about command line switches
Chris Lattner2f7c9632001-06-06 20:29:01 +000016//
Chris Lattner6d56c6b2001-10-13 07:06:57 +000017//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000018
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/Module.h"
Chris Lattner874a9e42002-08-31 00:30:15 +000020#include "llvm/PassManager.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000021#include "llvm/Bytecode/Reader.h"
Chris Lattner874a9e42002-08-31 00:30:15 +000022#include "llvm/Assembly/PrintModulePass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
Chris Lattner278f5152004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Chris Lattner5de22042001-11-27 00:03:19 +000025#include <fstream>
Chris Lattner874a9e42002-08-31 00:30:15 +000026#include <memory>
Chris Lattner2f7c9632001-06-06 20:29:01 +000027
Brian Gaeke960707c2003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattner64a67272002-07-25 16:31:09 +000030static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000031InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
32
Chris Lattner64a67272002-07-25 16:31:09 +000033static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000034OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000035 cl::value_desc("filename"));
36
37static cl::opt<bool>
38Force("f", cl::desc("Overwrite output files"));
39
Chris Lattner2f7c9632001-06-06 20:29:01 +000040int main(int argc, char **argv) {
Reid Spencer996ec722004-12-30 05:36:08 +000041 try {
42 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
43 sys::PrintStackTraceOnErrorSignal();
Chris Lattner12439ff2004-02-19 20:32:12 +000044
Chris Lattner0174b522005-01-29 17:29:05 +000045 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer996ec722004-12-30 05:36:08 +000046 std::string ErrorMessage;
Chris Lattner2f7c9632001-06-06 20:29:01 +000047
Reid Spencer996ec722004-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 Brukman650ba8e2005-04-22 00:00:37 +000057
Reid Spencer996ec722004-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 Lattner0af24642001-07-23 02:35:57 +000068 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000069 if (InputFilename == "-") {
70 OutputFilename = "-";
Chris Lattner0af24642001-07-23 02:35:57 +000071 } else {
Reid Spencer996ec722004-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 Lattner7f74a562002-01-20 22:54:45 +000080
Reid Spencer996ec722004-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 Lattnerc065ad82002-04-18 19:55:25 +000087
Reid Spencer996ec722004-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 Lattner7f74a562002-01-20 22:54:45 +000092 }
Chris Lattner0af24642001-07-23 02:35:57 +000093 }
Chris Lattner0af24642001-07-23 02:35:57 +000094
Reid Spencer996ec722004-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 Lattner0af24642001-07-23 02:35:57 +0000100
Chris Lattner0174b522005-01-29 17:29:05 +0000101 // All that llvm-dis does is write the assembly to a file.
Reid Spencer996ec722004-12-30 05:36:08 +0000102 PassManager Passes;
103 Passes.add(new PrintModulePass(Out));
104 Passes.run(*M.get());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105
Reid Spencer996ec722004-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 Lattner47c2e3e2002-09-24 00:09:48 +0000115 }
Reid Spencer996ec722004-12-30 05:36:08 +0000116 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000117}
Sumant Kowshikcf3afd92002-05-08 18:09:58 +0000118