blob: 38b863c20c8276fe5d073696a2d8b468b7ae0f3b [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"
Chris Lattnera0e49f22007-02-07 21:41:02 +000023#include "llvm/Support/Compressor.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattner76d46322006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Bill Wendlinga531ac22006-11-28 23:33:06 +000026#include "llvm/Support/Streams.h"
Chris Lattner278f5152004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Bill Wendlinga531ac22006-11-28 23:33:06 +000028#include <iostream>
Chris Lattner5de22042001-11-27 00:03:19 +000029#include <fstream>
Chris Lattner874a9e42002-08-31 00:30:15 +000030#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattner64a67272002-07-25 16:31:09 +000033static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000034InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
35
Chris Lattner64a67272002-07-25 16:31:09 +000036static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000037OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000038 cl::value_desc("filename"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
42
Chris Lattner216835d2007-02-07 04:39:35 +000043static cl::opt<bool>
44DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
45
Chris Lattner2f7c9632001-06-06 20:29:01 +000046int main(int argc, char **argv) {
Chris Lattner76d46322006-12-06 01:18:01 +000047 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer996ec722004-12-30 05:36:08 +000048 try {
49 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
50 sys::PrintStackTraceOnErrorSignal();
Chris Lattner12439ff2004-02-19 20:32:12 +000051
Chris Lattner0174b522005-01-29 17:29:05 +000052 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer996ec722004-12-30 05:36:08 +000053 std::string ErrorMessage;
Chris Lattner2f7c9632001-06-06 20:29:01 +000054
Chris Lattnera0e49f22007-02-07 21:41:02 +000055 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename,
56 Compressor::decompressToNewBuffer,
57 &ErrorMessage));
Reid Spencer996ec722004-12-30 05:36:08 +000058 if (M.get() == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000059 cerr << argv[0] << ": ";
Reid Spencer996ec722004-12-30 05:36:08 +000060 if (ErrorMessage.size())
Bill Wendlingf3baad32006-12-07 01:30:32 +000061 cerr << ErrorMessage << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +000062 else
Bill Wendlingf3baad32006-12-07 01:30:32 +000063 cerr << "bytecode didn't read correctly.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000064 return 1;
65 }
Misha Brukman650ba8e2005-04-22 00:00:37 +000066
Chris Lattner216835d2007-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 Spencer996ec722004-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 Wendlingf3baad32006-12-07 01:30:32 +000073 cerr << argv[0] << ": error opening '" << OutputFilename
74 << "': file exists! Sending to standard output.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000075 } else {
76 Out = new std::ofstream(OutputFilename.c_str());
77 }
78 }
Chris Lattner0af24642001-07-23 02:35:57 +000079 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000080 if (InputFilename == "-") {
81 OutputFilename = "-";
Chris Lattner0af24642001-07-23 02:35:57 +000082 } else {
Reid Spencer996ec722004-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 Lattner7f74a562002-01-20 22:54:45 +000091
Reid Spencer996ec722004-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 Wendlingf3baad32006-12-07 01:30:32 +000094 cerr << argv[0] << ": error opening '" << OutputFilename
95 << "': file exists! Sending to standard output.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000096 } else {
97 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnerc065ad82002-04-18 19:55:25 +000098
Reid Spencer996ec722004-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 Lattner7f74a562002-01-20 22:54:45 +0000103 }
Chris Lattner0af24642001-07-23 02:35:57 +0000104 }
Chris Lattner0af24642001-07-23 02:35:57 +0000105
Reid Spencer996ec722004-12-30 05:36:08 +0000106 if (!Out->good()) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000107 cerr << argv[0] << ": error opening " << OutputFilename
108 << ": sending to stdout instead!\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000109 Out = &std::cout;
110 }
Chris Lattner0af24642001-07-23 02:35:57 +0000111
Chris Lattner0174b522005-01-29 17:29:05 +0000112 // All that llvm-dis does is write the assembly to a file.
Chris Lattner216835d2007-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 Lattner2f7c9632001-06-06 20:29:01 +0000119
Reid Spencer996ec722004-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 Wendlingf3baad32006-12-07 01:30:32 +0000126 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000127 } catch (...) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000128 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner47c2e3e2002-09-24 00:09:48 +0000129 }
Chris Lattner76d46322006-12-06 01:18:01 +0000130
Reid Spencer996ec722004-12-30 05:36:08 +0000131 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132}
Sumant Kowshikcf3afd92002-05-08 18:09:58 +0000133