blob: ad130633d2b07403d48b97d63a030fb51bf4dd91 [file] [log] [blame]
Reid Spencer8a542ae2004-07-02 03:22:53 +00001//===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerdac69c82004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman3da94ae2005-04-22 00:00:37 +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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencerdac69c82004-06-07 17:53:43 +00008//===----------------------------------------------------------------------===//
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
Misha Brukman3da94ae2005-04-22 00:00:37 +000016// --nodetails - Don't print out detailed informaton about individual
Reid Spencer23d46e72004-06-10 18:38:44 +000017// 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
Misha Brukman3da94ae2005-04-22 00:00:37 +000022// 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
Misha Brukman3da94ae2005-04-22 00:00:37 +000025// functions in the module. To avoid this more detailed output, use the
Reid Spencer23d46e72004-06-10 18:38:44 +000026// -nodetails option to limit the output to just module level information.
Misha Brukman3da94ae2005-04-22 00:00:37 +000027// 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).
Chris Lattner63db4852007-04-29 05:51:00 +000030//
Reid Spencerdac69c82004-06-07 17:53:43 +000031//===----------------------------------------------------------------------===//
32
Reid Spencer86a9a7a2004-06-29 23:34:27 +000033#include "llvm/Analysis/Verifier.h"
Chris Lattner45e0f892007-04-29 08:12:22 +000034#include "llvm/Bitcode/BitstreamReader.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000035#include "llvm/Bytecode/Analyzer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000036#include "llvm/Support/CommandLine.h"
Chris Lattnerf2e292c2007-02-07 21:41:02 +000037#include "llvm/Support/Compressor.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner45e0f892007-04-29 08:12:22 +000039#include "llvm/Support/MemoryBuffer.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000040#include "llvm/System/Signals.h"
41#include <fstream>
42#include <iostream>
Reid Spencerdac69c82004-06-07 17:53:43 +000043using namespace llvm;
44
45static cl::opt<std::string>
46 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
47
Reid Spencer75937452004-08-21 21:00:24 +000048static cl::opt<std::string>
49 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
50
Chris Lattner63db4852007-04-29 05:51:00 +000051static cl::opt<bool> NoDetails("nodetails", cl::desc("Skip detailed output"));
52static cl::opt<bool> Dump("dump", cl::desc("Dump low level bytecode trace"));
53static cl::opt<bool> Verify("verify", cl::desc("Progressively verify module"));
Chris Lattner45e0f892007-04-29 08:12:22 +000054static cl::opt<bool> Bitcode("bitcode", cl::desc("Read a bitcode file"));
55
56/// CurStreamType - If we can sniff the flavor of this stream, we can produce
57/// better dump info.
58static enum {
59 UnknownBitstream,
60 LLVMIRBitstream
61} CurStreamType;
62
63/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
64static int AnalyzeBitcode() {
65 // Read the input file.
66 MemoryBuffer *Buffer;
67 if (InputFilename == "-")
68 Buffer = MemoryBuffer::getSTDIN();
69 else
70 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
71
72 if (Buffer == 0) {
73 std::cerr << "Error reading '" << InputFilename << "'.\n";
74 return 1;
75 }
76
77 if (Buffer->getBufferSize() & 3) {
78 std::cerr << "Bitcode stream should be a multiple of 4 bytes in length\n";
79 return 1;
80 }
81
82 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
83 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
84
85
86 // Read the stream signature.
87 char Signature[6];
88 Signature[0] = Stream.Read(8);
89 Signature[1] = Stream.Read(8);
90 Signature[2] = Stream.Read(4);
91 Signature[3] = Stream.Read(4);
92 Signature[4] = Stream.Read(4);
93 Signature[5] = Stream.Read(4);
94
95 CurStreamType = UnknownBitstream;
96 if (Signature[0] == 'B' && Signature[1] == 'C' &&
97 Signature[2] == 0x0 && Signature[3] == 0xC &&
98 Signature[4] == 0xE && Signature[5] == 0xD)
99 CurStreamType = LLVMIRBitstream;
100
101 std::cerr << "Summary of " << InputFilename << ":\n";
102 std::cerr << " Stream type: ";
103 switch (CurStreamType) {
104 default: assert(0 && "Unknown bitstream type");
105 case UnknownBitstream: std::cerr << "unknown\n"; break;
106 case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break;
107 }
108
109 return 0;
110}
Reid Spencerdac69c82004-06-07 17:53:43 +0000111
Chris Lattnerc30598b2006-12-06 01:18:01 +0000112int main(int argc, char **argv) {
113 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner45e0f892007-04-29 08:12:22 +0000114 cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n");
115
116 sys::PrintStackTraceOnErrorSignal();
117
118 if (Bitcode)
119 return AnalyzeBitcode();
120
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000121 try {
Chris Lattner63db4852007-04-29 05:51:00 +0000122 std::ostream *Out = &std::cout; // Default to printing to stdout...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000123 std::string ErrorMessage;
124 BytecodeAnalysis bca;
Reid Spencerdac69c82004-06-07 17:53:43 +0000125
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000126 /// Determine what to generate
127 bca.detailedResults = !NoDetails;
128 bca.progressiveVerify = Verify;
Reid Spencer96684ef2004-06-08 05:56:58 +0000129
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000130 /// Analyze the bytecode file
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000131 Module* M = AnalyzeBytecodeFile(InputFilename, bca,
132 Compressor::decompressToNewBuffer,
133 &ErrorMessage, (Dump?Out:0));
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000134
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000135 // All that bcanalyzer does is write the gathered statistics to the output
136 PrintBytecodeAnalysis(bca,*Out);
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000137
Chris Lattner45e0f892007-04-29 08:12:22 +0000138 if (M && Verify) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000139 std::string verificationMsg;
Chris Lattner05ac92c2006-07-06 18:02:27 +0000140 if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000141 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000142 }
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000143
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000144 if (Out != &std::cout) {
145 ((std::ofstream*)Out)->close();
146 delete Out;
147 }
148 return 0;
149 } catch (const std::string& msg) {
150 std::cerr << argv[0] << ": " << msg << "\n";
151 } catch (...) {
152 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000153 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000154 return 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000155}