cbb51521121911a796355b11fa12c9bc27feaa89
[oota-llvm.git] / tools / llvm-bcanalyzer / llvm-bcanalyzer.cpp
1 //===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tool may be invoked in the following manner:
11 //  llvm-bcanalyzer [options]      - Read LLVM bytecode from stdin
12 //  llvm-bcanalyzer [options] x.bc - Read LLVM bytecode from the x.bc file
13 //
14 //  Options:
15 //      --help      - Output information about command line switches
16 //      --nodetails - Don't print out detailed informaton about individual
17 //                    blocks and functions
18 //      --dump      - Dump low-level bytecode structure in readable format
19 //
20 // This tool provides analytical information about a bytecode file. It is
21 // intended as an aid to developers of bytecode reading and writing software. It
22 // produces on std::out a summary of the bytecode file that shows various
23 // statistics about the contents of the file. By default this information is
24 // detailed and contains information about individual bytecode blocks and the
25 // functions in the module. To avoid this more detailed output, use the
26 // -nodetails option to limit the output to just module level information.
27 // The tool is also able to print a bytecode file in a straight forward text
28 // format that shows the containment and relationships of the information in
29 // the bytecode file (-dump option).
30 //===----------------------------------------------------------------------===//
31
32 #include "llvm/Analysis/Verifier.h"
33 #include "llvm/Bytecode/Analyzer.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Compressor.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/System/Signals.h"
38 #include <fstream>
39 #include <iostream>
40
41 using namespace llvm;
42
43 static cl::opt<std::string>
44   InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
45
46 static cl::opt<std::string>
47   OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
48
49 static cl::opt<bool> NoDetails ("nodetails", cl::desc("Skip detailed output"));
50 static cl::opt<bool> Dump      ("dump", cl::desc("Dump low level bytecode trace"));
51 static cl::opt<bool> Verify    ("verify", cl::desc("Progressively verify module"));
52
53 int main(int argc, char **argv) {
54   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
55   try {
56     cl::ParseCommandLineOptions(argc, argv,
57       " llvm-bcanalyzer Analysis of ByteCode Dumper\n");
58
59     sys::PrintStackTraceOnErrorSignal();
60
61     std::ostream* Out = &std::cout;  // Default to printing to stdout...
62     std::string ErrorMessage;
63     BytecodeAnalysis bca;
64
65     /// Determine what to generate
66     bca.detailedResults = !NoDetails;
67     bca.progressiveVerify = Verify;
68
69     /// Analyze the bytecode file
70     Module* M = AnalyzeBytecodeFile(InputFilename, bca, 
71                                     Compressor::decompressToNewBuffer,
72                                     &ErrorMessage, (Dump?Out:0));
73
74     // All that bcanalyzer does is write the gathered statistics to the output
75     PrintBytecodeAnalysis(bca,*Out);
76
77     if ( M && Verify ) {
78       std::string verificationMsg;
79       if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
80         std::cerr << "Final Verification Message: " << verificationMsg << "\n";
81     }
82
83     if (Out != &std::cout) {
84       ((std::ofstream*)Out)->close();
85       delete Out;
86     }
87     return 0;
88   } catch (const std::string& msg) {
89     std::cerr << argv[0] << ": " << msg << "\n";
90   } catch (...) {
91     std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
92   }
93   return 1;
94 }
95
96 // vim: sw=2