blob: 01053751cd27f1babe91b9c9d616529dd388ea2b [file] [log] [blame]
Reid Spencer8a542ae2004-07-02 03:22:53 +00001//===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===//
Reid Spencerdac69c82004-06-07 17:53:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer23d46e72004-06-10 18:38:44 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer96684ef2004-06-08 05:56:58 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencerdac69c82004-06-07 17:53:43 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer96684ef2004-06-08 05:56:58 +000010// This tool may be invoked in the following manner:
Reid Spencer8a542ae2004-07-02 03:22:53 +000011// llvm-bcanalyzer [options] - Read LLVM bytecode from stdin
12// llvm-bcanalyzer [options] x.bc - Read LLVM bytecode from the x.bc file
Reid Spencerdac69c82004-06-07 17:53:43 +000013//
Reid Spencer96684ef2004-06-08 05:56:58 +000014// Options:
Reid Spencer23d46e72004-06-10 18:38:44 +000015// --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
Reid Spencer96684ef2004-06-08 05:56:58 +000019//
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
Reid Spencer23d46e72004-06-10 18:38:44 +000023// 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).
Reid Spencerdac69c82004-06-07 17:53:43 +000030//===----------------------------------------------------------------------===//
31
Reid Spencer86a9a7a2004-06-29 23:34:27 +000032#include "llvm/Analysis/Verifier.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000033#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
Reid Spencer75937452004-08-21 21:00:24 +000044static cl::opt<std::string>
45 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
46
Reid Spencer23d46e72004-06-10 18:38:44 +000047static cl::opt<bool> NoDetails ("nodetails", cl::desc("Skip detailed output"));
Reid Spencer86a9a7a2004-06-29 23:34:27 +000048static cl::opt<bool> Dump ("dump", cl::desc("Dump low level bytecode trace"));
49static cl::opt<bool> Verify ("verify", cl::desc("Progressively verify module"));
Reid Spencerdac69c82004-06-07 17:53:43 +000050
51int
52main(int argc, char **argv)
53{
54 cl::ParseCommandLineOptions(argc, argv,
Reid Spencer8a542ae2004-07-02 03:22:53 +000055 " llvm-bcanalyzer Analysis of ByteCode Dumper\n");
Reid Spencerdac69c82004-06-07 17:53:43 +000056
57 PrintStackTraceOnErrorSignal();
58
59 std::ostream* Out = &std::cout; // Default to printing to stdout...
60 std::istream* In = &std::cin; // Default to reading stdin
61 std::string ErrorMessage;
62 BytecodeAnalysis bca;
63
Reid Spencer96684ef2004-06-08 05:56:58 +000064 /// Determine what to generate
Reid Spencer23d46e72004-06-10 18:38:44 +000065 bca.detailedResults = !NoDetails;
Reid Spencer86a9a7a2004-06-29 23:34:27 +000066 bca.progressiveVerify = Verify;
Reid Spencer96684ef2004-06-08 05:56:58 +000067
Reid Spencerdac69c82004-06-07 17:53:43 +000068 /// Analyze the bytecode file
Reid Spencer75937452004-08-21 21:00:24 +000069 Module* M = AnalyzeBytecodeFile(InputFilename, bca, &ErrorMessage, (Dump?Out:0));
Reid Spencer86a9a7a2004-06-29 23:34:27 +000070
Reid Spencer8a542ae2004-07-02 03:22:53 +000071 // All that bcanalyzer does is write the gathered statistics to the output
Reid Spencer86a9a7a2004-06-29 23:34:27 +000072 PrintBytecodeAnalysis(bca,*Out);
73
74 if ( M && Verify ) {
75 std::string verificationMsg;
76 try {
77 verifyModule( *M, ThrowExceptionAction );
78 } catch (std::string& errmsg ) {
79 verificationMsg = errmsg;
80 }
81 if ( verificationMsg.length() > 0 )
82 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
83 }
84
Reid Spencerdac69c82004-06-07 17:53:43 +000085
86 // If there was an error, print it and stop.
87 if ( ErrorMessage.size() ) {
88 std::cerr << argv[0] << ": " << ErrorMessage << "\n";
89 return 1;
90 }
91
Reid Spencerdac69c82004-06-07 17:53:43 +000092
93 if (Out != &std::cout) {
94 ((std::ofstream*)Out)->close();
95 delete Out;
96 }
97 return 0;
98}
99
100// vim: sw=2