blob: 78d10ee531b36911f16e00e4dfdbcd21a4886b28 [file] [log] [blame]
Chris Lattner8f71f042003-10-20 17:58:43 +00001//===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
John Criswell09344dc2003-10-20 17:47:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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>
Chris Lattner306f8b42003-05-31 21:47:16 +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 Lattnerbbc373b2004-06-07 18:10:01 +000040static cl::opt<bool>
41CWriteMode("c", cl::desc("Obsolete option, do not use"), cl::ReallyHidden);
Chris Lattner0af24642001-07-23 02:35:57 +000042
Chris Lattner2f7c9632001-06-06 20:29:01 +000043int main(int argc, char **argv) {
Chris Lattner0af24642001-07-23 02:35:57 +000044 cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
Reid Spencere3263ec2004-08-29 19:28:55 +000045 sys::PrintStackTraceOnErrorSignal();
Chris Lattner12439ff2004-02-19 20:32:12 +000046
Chris Lattner7f74a562002-01-20 22:54:45 +000047 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattnerf46a02c2003-04-16 20:51:36 +000048 std::string ErrorMessage;
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
Chris Lattnerbbc373b2004-06-07 18:10:01 +000050 if (CWriteMode) {
Misha Brukman7c437d32004-02-13 23:46:47 +000051 std::cerr << "ERROR: llvm-dis no longer contains the C backend. "
52 << "Use 'llc -march=c' instead!\n";
Chris Lattner92849b7dc2004-02-13 23:22:40 +000053 exit(1);
54 }
55
Chris Lattnerf46a02c2003-04-16 20:51:36 +000056 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner874a9e42002-08-31 00:30:15 +000057 if (M.get() == 0) {
Chris Lattner02a16832003-05-22 20:13:16 +000058 std::cerr << argv[0] << ": ";
Chris Lattnerf46a02c2003-04-16 20:51:36 +000059 if (ErrorMessage.size())
Chris Lattner02a16832003-05-22 20:13:16 +000060 std::cerr << ErrorMessage << "\n";
Chris Lattnerf46a02c2003-04-16 20:51:36 +000061 else
Chris Lattner02a16832003-05-22 20:13:16 +000062 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000063 return 1;
64 }
65
Chris Lattnerab0cc402001-07-23 19:27:24 +000066 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner306f8b42003-05-31 21:47:16 +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!
70 std::cerr << argv[0] << ": error opening '" << OutputFilename
71 << "': file exists! Sending to standard output.\n";
72 } else {
73 Out = new std::ofstream(OutputFilename.c_str());
74 }
Chris Lattner7f74a562002-01-20 22:54:45 +000075 }
Chris Lattner0af24642001-07-23 02:35:57 +000076 } else {
Chris Lattnerab0cc402001-07-23 19:27:24 +000077 if (InputFilename == "-") {
78 OutputFilename = "-";
Chris Lattner0af24642001-07-23 02:35:57 +000079 } else {
Chris Lattner7f74a562002-01-20 22:54:45 +000080 std::string IFN = InputFilename;
Chris Lattner0af24642001-07-23 02:35:57 +000081 int Len = IFN.length();
82 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
83 // Source ends in .bc
Chris Lattner92849b7dc2004-02-13 23:22:40 +000084 OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
Chris Lattner0af24642001-07-23 02:35:57 +000085 } else {
Chris Lattner92849b7dc2004-02-13 23:22:40 +000086 OutputFilename = IFN+".ll";
Chris Lattner2f7c9632001-06-06 20:29:01 +000087 }
Chris Lattner7f74a562002-01-20 22:54:45 +000088
Chris Lattner0e11e542002-01-22 21:07:24 +000089 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +000090 // If force is not specified, make sure not to overwrite a file!
Chris Lattner02a16832003-05-22 20:13:16 +000091 std::cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists! Sending to standard output.\n";
Chris Lattner7f74a562002-01-20 22:54:45 +000093 } else {
94 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnerc065ad82002-04-18 19:55:25 +000095
Misha Brukmand6769742003-10-10 17:56:49 +000096 // Make sure that the Out file gets unlinked from the disk if we get a
Chris Lattnerc065ad82002-04-18 19:55:25 +000097 // SIGINT
Reid Spencerb2d0fa02004-11-14 22:30:54 +000098 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner7f74a562002-01-20 22:54:45 +000099 }
Chris Lattner0af24642001-07-23 02:35:57 +0000100 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101 }
Chris Lattner0af24642001-07-23 02:35:57 +0000102
103 if (!Out->good()) {
Chris Lattner02a16832003-05-22 20:13:16 +0000104 std::cerr << argv[0] << ": error opening " << OutputFilename
105 << ": sending to stdout instead!\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000106 Out = &std::cout;
Chris Lattner0af24642001-07-23 02:35:57 +0000107 }
108
Chris Lattnere693f332002-05-09 01:25:55 +0000109 // All that dis does is write the assembly or C out to a file...
110 //
Chris Lattner874a9e42002-08-31 00:30:15 +0000111 PassManager Passes;
Chris Lattner92849b7dc2004-02-13 23:22:40 +0000112 Passes.add(new PrintModulePass(Out));
Chris Lattner874a9e42002-08-31 00:30:15 +0000113 Passes.run(*M.get());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000114
Chris Lattner47c2e3e2002-09-24 00:09:48 +0000115 if (Out != &std::cout) {
116 ((std::ofstream*)Out)->close();
117 delete Out;
118 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000119 return 0;
120}
Sumant Kowshikcf3afd92002-05-08 18:09:58 +0000121