OLD | NEW |
---|---|
1 //===-- NaClBitcodeAnalyzer.cpp - Bitcode Analyzer ------------------------===// | 1 //===-- NaClBitcodeAnalyzer.cpp - Bitcode Analyzer ------------------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 | 9 |
10 #define DEBUG_TYPE "nacl-bitcode-analyzer" | 10 #define DEBUG_TYPE "nacl-bitcode-analyzer" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "llvm/Support/MemoryBuffer.h" | 23 #include "llvm/Support/MemoryBuffer.h" |
24 #include "llvm/Support/raw_ostream.h" | 24 #include "llvm/Support/raw_ostream.h" |
25 #include <algorithm> | 25 #include <algorithm> |
26 #include <map> | 26 #include <map> |
27 #include <system_error> | 27 #include <system_error> |
28 | 28 |
29 // TODO(kschimpf): Separate out into two bitcode parsers, one for | 29 // TODO(kschimpf): Separate out into two bitcode parsers, one for |
30 // dumping records, and one for collecting distribution stats for | 30 // dumping records, and one for collecting distribution stats for |
31 // printing. This should simplify the code. | 31 // printing. This should simplify the code. |
32 | 32 |
33 /// Error - All bitcode analysis errors go through this function, making this a | 33 namespace { |
34 /// good place to breakpoint if debugging. | 34 |
35 static bool Error(const llvm::Twine &Err) { | 35 // Generates an error message when outside parsing, and no |
36 // corresponding bit position is known. | |
37 bool Error(const llvm::Twine &Err) { | |
36 llvm::errs() << Err << "\n"; | 38 llvm::errs() << Err << "\n"; |
37 return true; | 39 return true; |
40 } // End of anonymous namespace. | |
41 | |
38 } | 42 } |
jvoung (off chromium)
2015/03/06 22:30:27
namespace ends down here instead?
Karl
2015/03/06 22:54:20
Oops, fixing.
| |
39 | 43 |
40 namespace llvm { | 44 namespace llvm { |
41 | 45 |
42 // Parses all bitcode blocks, and collects distribution of records in | 46 // Parses all bitcode blocks, and collects distribution of records in |
43 // each block. Also dumps bitcode structure if specified (via global | 47 // each block. Also dumps bitcode structure if specified (via global |
44 // variables). | 48 // variables). |
45 class PNaClBitcodeAnalyzerParser : public NaClBitcodeParser { | 49 class PNaClBitcodeAnalyzerParser : public NaClBitcodeParser { |
46 public: | 50 public: |
47 PNaClBitcodeAnalyzerParser(NaClBitstreamCursor &Cursor, | 51 PNaClBitcodeAnalyzerParser(NaClBitstreamCursor &Cursor, |
48 raw_ostream &OS, | 52 raw_ostream &OS, |
49 const AnalysisDumpOptions &DumpOptions, | 53 const AnalysisDumpOptions &DumpOptions, |
50 NaClBitcodeDist *Dist) | 54 NaClBitcodeDist *Dist) |
51 : NaClBitcodeParser(Cursor), | 55 : NaClBitcodeParser(Cursor), |
52 IndentLevel(0), | 56 IndentLevel(0), |
53 OS(OS), | 57 OS(OS), |
54 DumpOptions(DumpOptions), | 58 DumpOptions(DumpOptions), |
55 Dist(Dist), | 59 Dist(Dist), |
56 AbbrevListener(this) | 60 AbbrevListener(this) |
57 { | 61 { |
58 SetListener(&AbbrevListener); | 62 SetListener(&AbbrevListener); |
59 } | 63 } |
60 | 64 |
61 virtual ~PNaClBitcodeAnalyzerParser() {} | 65 virtual ~PNaClBitcodeAnalyzerParser() {} |
62 | 66 |
63 virtual bool Error(const std::string &Message) { | |
64 // Use local error routine so that all errors are treated uniformly. | |
65 return ::Error(Message); | |
66 } | |
67 | |
68 virtual bool ParseBlock(unsigned BlockID); | 67 virtual bool ParseBlock(unsigned BlockID); |
69 | 68 |
70 // Returns the string defining the indentation to use with respect | 69 // Returns the string defining the indentation to use with respect |
71 // to the current indent level. | 70 // to the current indent level. |
72 const std::string &GetIndentation() { | 71 const std::string &GetIndentation() { |
73 size_t Size = IndentationCache.size(); | 72 size_t Size = IndentationCache.size(); |
74 if (IndentLevel >= Size) { | 73 if (IndentLevel >= Size) { |
75 IndentationCache.resize(IndentLevel+1); | 74 IndentationCache.resize(IndentLevel+1); |
76 for (size_t i = Size; i <= IndentLevel; ++i) { | 75 for (size_t i = Size; i <= IndentLevel; ++i) { |
77 IndentationCache[i] = std::string(i*2, ' '); | 76 IndentationCache[i] = std::string(i*2, ' '); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 Context->IndentLevel++; | 239 Context->IndentLevel++; |
241 Indent = Context->GetIndentation(); | 240 Indent = Context->GetIndentation(); |
242 } | 241 } |
243 | 242 |
244 // Increment the indentation level for dumping. | 243 // Increment the indentation level for dumping. |
245 void DecrementIndent() { | 244 void DecrementIndent() { |
246 Context->IndentLevel--; | 245 Context->IndentLevel--; |
247 Indent = Context->GetIndentation(); | 246 Indent = Context->GetIndentation(); |
248 } | 247 } |
249 | 248 |
250 virtual bool Error(const std::string &Message) { | |
251 // Use local error routine so that all errors are treated uniformly. | |
252 return ::Error(Message); | |
253 } | |
254 | |
255 // Called once the block has been entered by the bitstream reader. | 249 // Called once the block has been entered by the bitstream reader. |
256 // Argument NumWords is set to the number of words in the | 250 // Argument NumWords is set to the number of words in the |
257 // corresponding block. | 251 // corresponding block. |
258 virtual void EnterBlock(unsigned NumberWords) { | 252 virtual void EnterBlock(unsigned NumberWords) { |
259 NumWords = NumberWords; | 253 NumWords = NumberWords; |
260 if (Context->DumpOptions.DumpRecords) { | 254 if (Context->DumpOptions.DumpRecords) { |
261 unsigned BlockID = GetBlockID(); | 255 unsigned BlockID = GetBlockID(); |
262 EmitBeginStartTag(); | 256 EmitBeginStartTag(); |
263 EmitEnterBlockTagName(BlockID); | 257 EmitEnterBlockTagName(BlockID); |
264 if (Context->DumpOptions.DumpDetails) { | 258 if (Context->DumpOptions.DumpDetails) { |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
403 OS << format("%lub/%.2fB/%luW", (unsigned long)Bits, | 397 OS << format("%lub/%.2fB/%luW", (unsigned long)Bits, |
404 (double)Bits/8, (unsigned long)(Bits/32)); | 398 (double)Bits/8, (unsigned long)(Bits/32)); |
405 } | 399 } |
406 | 400 |
407 int AnalyzeBitcodeInBuffer(const std::unique_ptr<MemoryBuffer> &Buf, | 401 int AnalyzeBitcodeInBuffer(const std::unique_ptr<MemoryBuffer> &Buf, |
408 raw_ostream &OS, | 402 raw_ostream &OS, |
409 const AnalysisDumpOptions &DumpOptions) { | 403 const AnalysisDumpOptions &DumpOptions) { |
410 DEBUG(dbgs() << "-> AnalyzeBitcodeInBuffer\n"); | 404 DEBUG(dbgs() << "-> AnalyzeBitcodeInBuffer\n"); |
411 | 405 |
412 if (Buf->getBufferSize() & 3) | 406 if (Buf->getBufferSize() & 3) |
413 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | 407 return ::Error("Bitcode stream should be a multiple of 4 bytes in length"); |
Karl
2015/03/06 22:54:20
Also removed the "::" because we aren't inside a b
| |
414 | 408 |
415 const unsigned char *BufPtr = (const unsigned char *)Buf->getBufferStart(); | 409 const unsigned char *BufPtr = (const unsigned char *)Buf->getBufferStart(); |
416 const unsigned char *EndBufPtr = BufPtr + Buf->getBufferSize(); | 410 const unsigned char *EndBufPtr = BufPtr + Buf->getBufferSize(); |
417 | 411 |
418 NaClBitcodeHeader Header; | 412 NaClBitcodeHeader Header; |
419 if (Header.Read(BufPtr, EndBufPtr)) | 413 if (Header.Read(BufPtr, EndBufPtr)) |
420 return Error("Invalid PNaCl bitcode header"); | 414 return ::Error("Invalid PNaCl bitcode header"); |
421 | 415 |
422 if (!Header.IsSupported()) | 416 if (!Header.IsSupported()) |
423 errs() << "Warning: " << Header.Unsupported() << "\n"; | 417 errs() << "Warning: " << Header.Unsupported() << "\n"; |
424 | 418 |
425 if (!Header.IsReadable()) | 419 if (!Header.IsReadable()) |
426 Error("Bitcode file is not readable"); | 420 ::Error("Bitcode file is not readable"); |
427 | 421 |
428 NaClBitstreamReader StreamFile(BufPtr, EndBufPtr); | 422 NaClBitstreamReader StreamFile(BufPtr, EndBufPtr); |
429 NaClBitstreamCursor Stream(StreamFile); | 423 NaClBitstreamCursor Stream(StreamFile); |
430 | 424 |
431 unsigned NumTopBlocks = 0; | 425 unsigned NumTopBlocks = 0; |
432 | 426 |
433 // Print out header information. | 427 // Print out header information. |
434 for (size_t i = 0, limit = Header.NumberFields(); i < limit; ++i) { | 428 for (size_t i = 0, limit = Header.NumberFields(); i < limit; ++i) { |
435 OS << Header.GetField(i)->Contents() << "\n"; | 429 OS << Header.GetField(i)->Contents() << "\n"; |
436 } | 430 } |
(...skipping 22 matching lines...) Expand all Loading... | |
459 | 453 |
460 DEBUG(dbgs() << "<- AnalyzeBitcode\n"); | 454 DEBUG(dbgs() << "<- AnalyzeBitcode\n"); |
461 return 0; | 455 return 0; |
462 } | 456 } |
463 | 457 |
464 int AnalyzeBitcodeInFile(const StringRef &InputFilename, raw_ostream &OS, | 458 int AnalyzeBitcodeInFile(const StringRef &InputFilename, raw_ostream &OS, |
465 const AnalysisDumpOptions &DumpOptions) { | 459 const AnalysisDumpOptions &DumpOptions) { |
466 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = | 460 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrOrFile = |
467 MemoryBuffer::getFileOrSTDIN(InputFilename); | 461 MemoryBuffer::getFileOrSTDIN(InputFilename); |
468 if (std::error_code EC = ErrOrFile.getError()) | 462 if (std::error_code EC = ErrOrFile.getError()) |
469 return Error(Twine("Error reading '") + InputFilename + "': " + | 463 return ::Error(Twine("Error reading '") + InputFilename + "': " + |
470 EC.message()); | 464 EC.message()); |
471 | 465 |
472 return AnalyzeBitcodeInBuffer(ErrOrFile.get(), OS, DumpOptions); | 466 return AnalyzeBitcodeInBuffer(ErrOrFile.get(), OS, DumpOptions); |
473 } | 467 } |
474 | 468 |
475 } // namespace llvm | 469 } // namespace llvm |
OLD | NEW |