llvm-bcanalyzer.cpp revision 8a542aeb8478d53df8dc985972bdc479560ff72f
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 "Support/CommandLine.h"
35#include "llvm/System/Signals.h"
36#include <fstream>
37#include <iostream>
38
39using namespace llvm;
40
41static cl::opt<std::string>
42  InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
43
44static cl::opt<bool> NoDetails ("nodetails", cl::desc("Skip detailed output"));
45static cl::opt<bool> Dump      ("dump", cl::desc("Dump low level bytecode trace"));
46static cl::opt<bool> Verify    ("verify", cl::desc("Progressively verify module"));
47
48int
49main(int argc, char **argv)
50{
51  cl::ParseCommandLineOptions(argc, argv,
52    " llvm-bcanalyzer Analysis of ByteCode Dumper\n");
53
54  PrintStackTraceOnErrorSignal();
55
56  std::ostream* Out = &std::cout;  // Default to printing to stdout...
57  std::istream* In  = &std::cin;   // Default to reading stdin
58  std::string ErrorMessage;
59  BytecodeAnalysis bca;
60
61  /// Determine what to generate
62  bca.dumpBytecode = Dump;
63  bca.detailedResults = !NoDetails;
64  bca.progressiveVerify = Verify;
65
66  /// Analyze the bytecode file
67  Module* M = AnalyzeBytecodeFile(InputFilename, bca, &ErrorMessage);
68
69  // All that bcanalyzer does is write the gathered statistics to the output
70  PrintBytecodeAnalysis(bca,*Out);
71
72  if ( M && Verify ) {
73    std::string verificationMsg;
74    try {
75      verifyModule( *M, ThrowExceptionAction );
76    } catch (std::string& errmsg ) {
77      verificationMsg = errmsg;
78    }
79    if ( verificationMsg.length() > 0 )
80      std::cerr << "Final Verification Message: " << verificationMsg << "\n";
81  }
82
83
84  // If there was an error, print it and stop.
85  if ( ErrorMessage.size() ) {
86    std::cerr << argv[0] << ": " << ErrorMessage << "\n";
87    return 1;
88  }
89
90
91  if (Out != &std::cout) {
92    ((std::ofstream*)Out)->close();
93    delete Out;
94  }
95  return 0;
96}
97
98// vim: sw=2
99