Fix some compiler warnings.
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This utility may be invoked in the following manner:
11 //  llvm-dis [options]      - Read LLVM bitcode from stdin, write asm to stdout
12 //  llvm-dis [options] x.bc - Read LLVM bitcode from the x.bc file, write asm
13 //                            to the x.ll file.
14 //  Options:
15 //      --help   - Output information about command line switches
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Type.h"
22 #include "llvm/Bitcode/ReaderWriter.h"
23 #include "llvm/Assembly/AssemblyAnnotationWriter.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/FormattedStream.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/PrettyStackTrace.h"
29 #include "llvm/Support/ToolOutputFile.h"
30 #include "llvm/Support/Signals.h"
31 #include "llvm/Support/system_error.h"
32 using namespace llvm;
33
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
36
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39                cl::value_desc("filename"));
40
41 static cl::opt<bool>
42 Force("f", cl::desc("Enable binary output on terminals"));
43
44 static cl::opt<bool>
45 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
46
47 static cl::opt<bool>
48 ShowAnnotations("show-annotations",
49                 cl::desc("Add informational comments to the .ll file"));
50
51 namespace {
52   
53 class CommentWriter : public AssemblyAnnotationWriter {
54 public:
55   void emitFunctionAnnot(const Function *F,
56                          formatted_raw_ostream &OS) {
57     OS << "; [#uses=" << F->getNumUses() << ']';  // Output # uses
58     OS << '\n';
59   }
60   void printInfoComment(const Value &V, formatted_raw_ostream &OS) {
61     if (V.getType()->isVoidTy()) return;
62       
63     OS.PadToColumn(50);
64     OS << "; [#uses=" << V.getNumUses() << ']';  // Output # uses
65   }
66 };
67   
68 } // end anon namespace
69
70 int main(int argc, char **argv) {
71   // Print a stack trace if we signal out.
72   sys::PrintStackTraceOnErrorSignal();
73   PrettyStackTraceProgram X(argc, argv);
74   
75   LLVMContext &Context = getGlobalContext();
76   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
77   
78   
79   cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
80
81   std::string ErrorMessage;
82   error_code ec;
83   std::auto_ptr<Module> M;
84   OwningPtr<MemoryBuffer> BufferPtr;
85
86   if ((ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)))
87     ErrorMessage = ec.message();
88   else
89     M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
90   (void) BufferPtr.take();
91
92   if (M.get() == 0) {
93     errs() << argv[0] << ": ";
94     if (ErrorMessage.size())
95       errs() << ErrorMessage << "\n";
96     else
97       errs() << "bitcode didn't read correctly.\n";
98     return 1;
99   }
100   
101   // Just use stdout.  We won't actually print anything on it.
102   if (DontPrint)
103     OutputFilename = "-";
104   
105   if (OutputFilename.empty()) { // Unspecified output, infer it.
106     if (InputFilename == "-") {
107       OutputFilename = "-";
108     } else {
109       const std::string &IFN = InputFilename;
110       int Len = IFN.length();
111       // If the source ends in .bc, strip it off.
112       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
113         OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
114       else
115         OutputFilename = IFN+".ll";
116     }
117   }
118
119   std::string ErrorInfo;
120   OwningPtr<tool_output_file> 
121   Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
122                            raw_fd_ostream::F_Binary));
123   if (!ErrorInfo.empty()) {
124     errs() << ErrorInfo << '\n';
125     return 1;
126   }
127
128   OwningPtr<AssemblyAnnotationWriter> Annotator;
129   if (ShowAnnotations)
130     Annotator.reset(new CommentWriter());
131   
132   // All that llvm-dis does is write the assembly to a file.
133   if (!DontPrint)
134     M->print(Out->os(), Annotator.get());
135
136   // Declare success.
137   Out->keep();
138
139   return 0;
140 }
141