blob: d2022fd9f849516479d2d8158b1a17d9553fbd9c [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 Lattnerc30598b2006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000025#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000026#include "llvm/System/Signals.h"
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000027#include <iostream>
Chris Lattnercee8f9a2001-11-27 00:03:19 +000028#include <fstream>
Chris Lattner01794502002-08-31 00:30:15 +000029#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000030using namespace llvm;
31
Chris Lattnerc7a09852002-07-25 16:31:09 +000032static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000033InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
34
Chris Lattnerc7a09852002-07-25 16:31:09 +000035static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000036OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000037 cl::value_desc("filename"));
38
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
41
Chris Lattner00950542001-06-06 20:29:01 +000042int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000043 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000044 try {
45 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
46 sys::PrintStackTraceOnErrorSignal();
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000047
Chris Lattner5ea56e52005-01-29 17:29:05 +000048 std::ostream *Out = &std::cout; // Default to printing to stdout.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000049 std::string ErrorMessage;
Chris Lattner00950542001-06-06 20:29:01 +000050
Reid Spencer1ef8bda2004-12-30 05:36:08 +000051 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
52 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000053 cerr << argv[0] << ": ";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000054 if (ErrorMessage.size())
Bill Wendlinge8156192006-12-07 01:30:32 +000055 cerr << ErrorMessage << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000056 else
Bill Wendlinge8156192006-12-07 01:30:32 +000057 cerr << "bytecode didn't read correctly.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000058 return 1;
59 }
Misha Brukman3da94ae2005-04-22 00:00:37 +000060
Reid Spencer1ef8bda2004-12-30 05:36:08 +000061 if (OutputFilename != "") { // Specified an output filename?
62 if (OutputFilename != "-") { // Not stdout?
63 if (!Force && std::ifstream(OutputFilename.c_str())) {
64 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000065 cerr << argv[0] << ": error opening '" << OutputFilename
66 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000067 } else {
68 Out = new std::ofstream(OutputFilename.c_str());
69 }
70 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000071 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000072 if (InputFilename == "-") {
73 OutputFilename = "-";
Chris Lattner8f367bd2001-07-23 02:35:57 +000074 } else {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000075 std::string IFN = InputFilename;
76 int Len = IFN.length();
77 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
78 // Source ends in .bc
79 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
80 } else {
81 OutputFilename = IFN+".ll";
82 }
Chris Lattner697954c2002-01-20 22:54:45 +000083
Reid Spencer1ef8bda2004-12-30 05:36:08 +000084 if (!Force && std::ifstream(OutputFilename.c_str())) {
85 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000086 cerr << argv[0] << ": error opening '" << OutputFilename
87 << "': file exists! Sending to standard output.\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000088 } else {
89 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +000090
Reid Spencer1ef8bda2004-12-30 05:36:08 +000091 // Make sure that the Out file gets unlinked from the disk if we get a
92 // SIGINT
93 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
94 }
Chris Lattner697954c2002-01-20 22:54:45 +000095 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000096 }
Chris Lattner8f367bd2001-07-23 02:35:57 +000097
Reid Spencer1ef8bda2004-12-30 05:36:08 +000098 if (!Out->good()) {
Bill Wendlinge8156192006-12-07 01:30:32 +000099 cerr << argv[0] << ": error opening " << OutputFilename
100 << ": sending to stdout instead!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000101 Out = &std::cout;
102 }
Chris Lattner8f367bd2001-07-23 02:35:57 +0000103
Chris Lattner5ea56e52005-01-29 17:29:05 +0000104 // All that llvm-dis does is write the assembly to a file.
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000105 PassManager Passes;
Bill Wendlinge8156192006-12-07 01:30:32 +0000106 OStream L(*Out);
Bill Wendlinga5b31ca2006-11-28 23:33:06 +0000107 Passes.add(new PrintModulePass(&L));
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000108 Passes.run(*M.get());
Chris Lattner00950542001-06-06 20:29:01 +0000109
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000110 if (Out != &std::cout) {
111 ((std::ofstream*)Out)->close();
112 delete Out;
113 }
114 return 0;
115 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000116 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000118 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattner93c26682002-09-24 00:09:48 +0000119 }
Chris Lattnerc30598b2006-12-06 01:18:01 +0000120
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000121 return 1;
Chris Lattner00950542001-06-06 20:29:01 +0000122}
Sumant Kowshik9ddc86c2002-05-08 18:09:58 +0000123