blob: e4ed2b32ca94edb5e7bbedb42ea8f5ec580e9a36 [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 Lattner76d46322006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Bill Wendlinga531ac22006-11-28 23:33:06 +000025#include "llvm/Support/Streams.h"
Chris Lattner278f5152004-05-27 05:41:36 +000026#include "llvm/System/Signals.h"
Bill Wendlinga531ac22006-11-28 23:33:06 +000027#include <iostream>
Chris Lattner5de22042001-11-27 00:03:19 +000028#include <fstream>
Chris Lattner874a9e42002-08-31 00:30:15 +000029#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000030using namespace llvm;
31
Chris Lattner64a67272002-07-25 16:31:09 +000032static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000033InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
34
Chris Lattner64a67272002-07-25 16:31:09 +000035static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000036OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000037 cl::value_desc("filename"));
38
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
41
Chris Lattner216835d2007-02-07 04:39:35 +000042static cl::opt<bool>
43DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
44
Chris Lattner2f7c9632001-06-06 20:29:01 +000045int main(int argc, char **argv) {
Chris Lattner76d46322006-12-06 01:18:01 +000046 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer996ec722004-12-30 05:36:08 +000047 try {
48 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
49 sys::PrintStackTraceOnErrorSignal();
Chris Lattner12439ff2004-02-19 20:32:12 +000050
Chris Lattner0174b522005-01-29 17:29:05 +000051 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer996ec722004-12-30 05:36:08 +000052 std::string ErrorMessage;
Chris Lattner2f7c9632001-06-06 20:29:01 +000053
Reid Spencer996ec722004-12-30 05:36:08 +000054 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
55 if (M.get() == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000056 cerr << argv[0] << ": ";
Reid Spencer996ec722004-12-30 05:36:08 +000057 if (ErrorMessage.size())
Bill Wendlingf3baad32006-12-07 01:30:32 +000058 cerr << ErrorMessage << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +000059 else
Bill Wendlingf3baad32006-12-07 01:30:32 +000060 cerr << "bytecode didn't read correctly.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000061 return 1;
62 }
Misha Brukman650ba8e2005-04-22 00:00:37 +000063
Chris Lattner216835d2007-02-07 04:39:35 +000064 if (DontPrint) {
65 // Just use stdout. We won't actually print anything on it.
66 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer996ec722004-12-30 05:36:08 +000067 if (OutputFilename != "-") { // Not stdout?
68 if (!Force && std::ifstream(OutputFilename.c_str())) {
69 // If force is not specified, make sure not to overwrite a file!
Bill Wendlingf3baad32006-12-07 01:30:32 +000070 cerr << argv[0] << ": error opening '" << OutputFilename
71 << "': file exists! Sending to standard output.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000072 } else {
73 Out = new std::ofstream(OutputFilename.c_str());
74 }
75 }
Chris Lattner0af24642001-07-23 02:35:57 +000076 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000077 if (InputFilename == "-") {
78 OutputFilename = "-";
Chris Lattner0af24642001-07-23 02:35:57 +000079 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000080 std::string IFN = InputFilename;
81 int Len = IFN.length();
82 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
83 // Source ends in .bc
84 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
85 } else {
86 OutputFilename = IFN+".ll";
87 }
Chris Lattner7f74a562002-01-20 22:54:45 +000088
Reid Spencer996ec722004-12-30 05:36:08 +000089 if (!Force && std::ifstream(OutputFilename.c_str())) {
90 // If force is not specified, make sure not to overwrite a file!
Bill Wendlingf3baad32006-12-07 01:30:32 +000091 cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists! Sending to standard output.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000093 } else {
94 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnerc065ad82002-04-18 19:55:25 +000095
Reid Spencer996ec722004-12-30 05:36:08 +000096 // Make sure that the Out file gets unlinked from the disk if we get a
97 // SIGINT
98 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
99 }
Chris Lattner7f74a562002-01-20 22:54:45 +0000100 }
Chris Lattner0af24642001-07-23 02:35:57 +0000101 }
Chris Lattner0af24642001-07-23 02:35:57 +0000102
Reid Spencer996ec722004-12-30 05:36:08 +0000103 if (!Out->good()) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000104 cerr << argv[0] << ": error opening " << OutputFilename
105 << ": sending to stdout instead!\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000106 Out = &std::cout;
107 }
Chris Lattner0af24642001-07-23 02:35:57 +0000108
Chris Lattner0174b522005-01-29 17:29:05 +0000109 // All that llvm-dis does is write the assembly to a file.
Chris Lattner216835d2007-02-07 04:39:35 +0000110 if (!DontPrint) {
111 PassManager Passes;
112 OStream L(*Out);
113 Passes.add(new PrintModulePass(&L));
114 Passes.run(*M.get());
115 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116
Reid Spencer996ec722004-12-30 05:36:08 +0000117 if (Out != &std::cout) {
118 ((std::ofstream*)Out)->close();
119 delete Out;
120 }
121 return 0;
122 } catch (const std::string& msg) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000123 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000124 } catch (...) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000125 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner47c2e3e2002-09-24 00:09:48 +0000126 }
Chris Lattner76d46322006-12-06 01:18:01 +0000127
Reid Spencer996ec722004-12-30 05:36:08 +0000128 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000129}
Sumant Kowshikcf3afd92002-05-08 18:09:58 +0000130