Don't use 'using std::error_code' in include/llvm.
[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/IR/LLVMContext.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/IR/AssemblyAnnotationWriter.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/DataStream.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/PrettyStackTrace.h"
33 #include "llvm/Support/Signals.h"
34 #include "llvm/Support/ToolOutputFile.h"
35 #include <system_error>
36 using namespace llvm;
37 using std::error_code;
38
39 static cl::opt<std::string>
40 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
41
42 static cl::opt<std::string>
43 OutputFilename("o", cl::desc("Override output filename"),
44                cl::value_desc("filename"));
45
46 static cl::opt<bool>
47 Force("f", cl::desc("Enable binary output on terminals"));
48
49 static cl::opt<bool>
50 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
51
52 static cl::opt<bool>
53 ShowAnnotations("show-annotations",
54                 cl::desc("Add informational comments to the .ll file"));
55
56 namespace {
57
58 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
59   OS << DL.getLine() << ":" << DL.getCol();
60   if (MDNode *N = DL.getInlinedAt(getGlobalContext())) {
61     DebugLoc IDL = DebugLoc::getFromDILocation(N);
62     if (!IDL.isUnknown()) {
63       OS << "@";
64       printDebugLoc(IDL,OS);
65     }
66   }
67 }
68 class CommentWriter : public AssemblyAnnotationWriter {
69 public:
70   void emitFunctionAnnot(const Function *F,
71                          formatted_raw_ostream &OS) override {
72     OS << "; [#uses=" << F->getNumUses() << ']';  // Output # uses
73     OS << '\n';
74   }
75   void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
76     bool Padded = false;
77     if (!V.getType()->isVoidTy()) {
78       OS.PadToColumn(50);
79       Padded = true;
80       OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";  // Output # uses and type
81     }
82     if (const Instruction *I = dyn_cast<Instruction>(&V)) {
83       const DebugLoc &DL = I->getDebugLoc();
84       if (!DL.isUnknown()) {
85         if (!Padded) {
86           OS.PadToColumn(50);
87           Padded = true;
88           OS << ";";
89         }
90         OS << " [debug line = ";
91         printDebugLoc(DL,OS);
92         OS << "]";
93       }
94       if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
95         DIVariable Var(DDI->getVariable());
96         if (!Padded) {
97           OS.PadToColumn(50);
98           OS << ";";
99         }
100         OS << " [debug variable = " << Var.getName() << "]";
101       }
102       else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
103         DIVariable Var(DVI->getVariable());
104         if (!Padded) {
105           OS.PadToColumn(50);
106           OS << ";";
107         }
108         OS << " [debug variable = " << Var.getName() << "]";
109       }
110     }
111   }
112 };
113
114 } // end anon namespace
115
116 int main(int argc, char **argv) {
117   // Print a stack trace if we signal out.
118   sys::PrintStackTraceOnErrorSignal();
119   PrettyStackTraceProgram X(argc, argv);
120
121   LLVMContext &Context = getGlobalContext();
122   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
123
124
125   cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
126
127   std::string ErrorMessage;
128   std::unique_ptr<Module> M;
129
130   // Use the bitcode streaming interface
131   DataStreamer *streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
132   if (streamer) {
133     std::string DisplayFilename;
134     if (InputFilename == "-")
135       DisplayFilename = "<stdin>";
136     else
137       DisplayFilename = InputFilename;
138     M.reset(getStreamedBitcodeModule(DisplayFilename, streamer, Context,
139                                      &ErrorMessage));
140     if(M.get()) {
141       if (error_code EC = M->materializeAllPermanently()) {
142         ErrorMessage = EC.message();
143         M.reset();
144       }
145     }
146   }
147
148   if (!M.get()) {
149     errs() << argv[0] << ": ";
150     if (ErrorMessage.size())
151       errs() << ErrorMessage << "\n";
152     else
153       errs() << "bitcode didn't read correctly.\n";
154     return 1;
155   }
156
157   // Just use stdout.  We won't actually print anything on it.
158   if (DontPrint)
159     OutputFilename = "-";
160
161   if (OutputFilename.empty()) { // Unspecified output, infer it.
162     if (InputFilename == "-") {
163       OutputFilename = "-";
164     } else {
165       const std::string &IFN = InputFilename;
166       int Len = IFN.length();
167       // If the source ends in .bc, strip it off.
168       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
169         OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
170       else
171         OutputFilename = IFN+".ll";
172     }
173   }
174
175   std::string ErrorInfo;
176   std::unique_ptr<tool_output_file> Out(
177       new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
178   if (!ErrorInfo.empty()) {
179     errs() << ErrorInfo << '\n';
180     return 1;
181   }
182
183   std::unique_ptr<AssemblyAnnotationWriter> Annotator;
184   if (ShowAnnotations)
185     Annotator.reset(new CommentWriter());
186
187   // All that llvm-dis does is write the assembly to a file.
188   if (!DontPrint)
189     M->print(Out->os(), Annotator.get());
190
191   // Declare success.
192   Out->keep();
193
194   return 0;
195 }