blob: 6321551446a11417ad477b82d5dd433487c4ebc9 [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 Lattnere2244462007-04-22 06:31:02 +000021#include "llvm/Bitcode/ReaderWriter.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"
Chris Lattner6694f602007-04-29 07:54:31 +000025#include "llvm/Support/MemoryBuffer.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 Lattnere2244462007-04-22 06:31:02 +000055 std::auto_ptr<Module> M;
Chris Lattner8ca41bf2007-04-22 06:35:20 +000056
Chris Lattner6d80e212007-05-06 09:29:57 +000057 MemoryBuffer *Buffer
58 = MemoryBuffer::getFileOrSTDIN(&InputFilename[0], InputFilename.size());
Chris Lattner6694f602007-04-29 07:54:31 +000059
Chris Lattner6d80e212007-05-06 09:29:57 +000060 if (Buffer == 0)
61 ErrorMessage = "Error reading file '" + InputFilename + "'";
62 else
63 M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
64 delete Buffer;
Chris Lattner6694f602007-04-29 07:54:31 +000065
Reid Spencer996ec722004-12-30 05:36:08 +000066 if (M.get() == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000067 cerr << argv[0] << ": ";
Reid Spencer996ec722004-12-30 05:36:08 +000068 if (ErrorMessage.size())
Bill Wendlingf3baad32006-12-07 01:30:32 +000069 cerr << ErrorMessage << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +000070 else
Bill Wendlingf3baad32006-12-07 01:30:32 +000071 cerr << "bytecode didn't read correctly.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000072 return 1;
73 }
Chris Lattner6694f602007-04-29 07:54:31 +000074
Chris Lattner216835d2007-02-07 04:39:35 +000075 if (DontPrint) {
76 // Just use stdout. We won't actually print anything on it.
77 } else if (OutputFilename != "") { // Specified an output filename?
Reid Spencer996ec722004-12-30 05:36:08 +000078 if (OutputFilename != "-") { // Not stdout?
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
Bill Wendlingf3baad32006-12-07 01:30:32 +000081 cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists! Sending to standard output.\n";
Reid Spencer996ec722004-12-30 05:36:08 +000083 } else {
84 Out = new std::ofstream(OutputFilename.c_str());
85 }
86 }
Chris Lattner0af24642001-07-23 02:35:57 +000087 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000088 if (InputFilename == "-") {
89 OutputFilename = "-";
Chris Lattner0af24642001-07-23 02:35:57 +000090 } else {
Reid Spencer996ec722004-12-30 05:36:08 +000091 std::string IFN = InputFilename;
92 int Len = IFN.length();
93 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
94 // Source ends in .bc
95 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
96 } else {
97 OutputFilename = IFN+".ll";
98 }
Chris Lattner7f74a562002-01-20 22:54:45 +000099
Reid Spencer996ec722004-12-30 05:36:08 +0000100 if (!Force && std::ifstream(OutputFilename.c_str())) {
101 // If force is not specified, make sure not to overwrite a file!
Bill Wendlingf3baad32006-12-07 01:30:32 +0000102 cerr << argv[0] << ": error opening '" << OutputFilename
103 << "': file exists! Sending to standard output.\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000104 } else {
105 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnerc065ad82002-04-18 19:55:25 +0000106
Reid Spencer996ec722004-12-30 05:36:08 +0000107 // Make sure that the Out file gets unlinked from the disk if we get a
108 // SIGINT
109 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
110 }
Chris Lattner7f74a562002-01-20 22:54:45 +0000111 }
Chris Lattner0af24642001-07-23 02:35:57 +0000112 }
Chris Lattner0af24642001-07-23 02:35:57 +0000113
Reid Spencer996ec722004-12-30 05:36:08 +0000114 if (!Out->good()) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000115 cerr << argv[0] << ": error opening " << OutputFilename
116 << ": sending to stdout instead!\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000117 Out = &std::cout;
118 }
Chris Lattner0af24642001-07-23 02:35:57 +0000119
Chris Lattner0174b522005-01-29 17:29:05 +0000120 // All that llvm-dis does is write the assembly to a file.
Chris Lattner216835d2007-02-07 04:39:35 +0000121 if (!DontPrint) {
122 PassManager Passes;
123 OStream L(*Out);
124 Passes.add(new PrintModulePass(&L));
125 Passes.run(*M.get());
126 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127
Reid Spencer996ec722004-12-30 05:36:08 +0000128 if (Out != &std::cout) {
129 ((std::ofstream*)Out)->close();
130 delete Out;
131 }
132 return 0;
133 } catch (const std::string& msg) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000134 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer996ec722004-12-30 05:36:08 +0000135 } catch (...) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000136 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner47c2e3e2002-09-24 00:09:48 +0000137 }
Chris Lattner76d46322006-12-06 01:18:01 +0000138
Reid Spencer996ec722004-12-30 05:36:08 +0000139 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000140}
Sumant Kowshikcf3afd92002-05-08 18:09:58 +0000141