Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===-- NaClCompress.cpp - Bitcode (abbrev) compression -----------------===// | 1 //===-- NaClCompress.cpp - Bitcode (abbrev) compression -----------------===// |
| 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 // Analyzes the data in memory buffer, and determines what | 10 // Analyzes the data in memory buffer, and determines what |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 | 41 |
| 42 #include "llvm/Bitcode/NaCl/NaClCompress.h" | 42 #include "llvm/Bitcode/NaCl/NaClCompress.h" |
| 43 | 43 |
| 44 #include "llvm/ADT/DenseMap.h" | 44 #include "llvm/ADT/DenseMap.h" |
| 45 #include "llvm/Bitcode/NaCl/AbbrevTrieNode.h" | 45 #include "llvm/Bitcode/NaCl/AbbrevTrieNode.h" |
| 46 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" | 46 #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" |
| 47 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h" | 47 #include "llvm/Bitcode/NaCl/NaClBitstreamWriter.h" |
| 48 #include "llvm/Bitcode/NaCl/NaClCompressBlockDist.h" | 48 #include "llvm/Bitcode/NaCl/NaClCompressBlockDist.h" |
| 49 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 49 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 50 | 50 |
| 51 using namespace llvm; | |
| 52 | |
| 53 namespace { | 51 namespace { |
| 54 | 52 |
| 55 using namespace llvm; | 53 using namespace llvm; |
| 56 | 54 |
| 57 /// Error - All bitcode analysis errors go through this function, | 55 // Generates an error message when outside parsing, and no |
| 58 /// making this a good place to breakpoint if debugging. | 56 // corresponding bit position is known. |
| 59 static bool Error(const std::string &Err) { | 57 static bool Error(const std::string &Err) { |
| 60 errs() << Err << "\n"; | 58 errs() << Err << "\n"; |
| 61 return true; | 59 return true; |
| 62 } | 60 } |
| 63 | 61 |
| 64 // Prints out the abbreviation in readable form to the given Stream. | 62 // Prints out the abbreviation in readable form to the given Stream. |
| 65 static void PrintAbbrev(raw_ostream &Stream, | 63 static void PrintAbbrev(raw_ostream &Stream, |
| 66 unsigned BlockID, const NaClBitCodeAbbrev *Abbrev) { | 64 unsigned BlockID, const NaClBitCodeAbbrev *Abbrev) { |
| 67 Stream << "Abbrev(block " << BlockID << "): "; | 65 Stream << "Abbrev(block " << BlockID << "): "; |
| 68 Abbrev->Print(Stream); | 66 Abbrev->Print(Stream); |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 : NaClBitcodeParser(Cursor), Flags(Flags), | 469 : NaClBitcodeParser(Cursor), Flags(Flags), |
| 472 BlockAbbrevsMap(BlockAbbrevsMap), | 470 BlockAbbrevsMap(BlockAbbrevsMap), |
| 473 BlockDist(&NaClCompressBlockDistElement::Sentinel), | 471 BlockDist(&NaClCompressBlockDistElement::Sentinel), |
| 474 AbbrevListener(this) | 472 AbbrevListener(this) |
| 475 { | 473 { |
| 476 SetListener(&AbbrevListener); | 474 SetListener(&AbbrevListener); |
| 477 } | 475 } |
| 478 | 476 |
| 479 virtual ~NaClAnalyzeParser() {} | 477 virtual ~NaClAnalyzeParser() {} |
| 480 | 478 |
| 481 virtual bool Error(const std::string &Message) { | |
| 482 // Use local error routine so that all errors are treated uniformly. | |
| 483 return ::Error(Message); | |
| 484 } | |
| 485 | |
| 486 virtual bool ParseBlock(unsigned BlockID); | 479 virtual bool ParseBlock(unsigned BlockID); |
| 487 | 480 |
| 488 const NaClBitcodeCompressor::CompressFlags &Flags; | 481 const NaClBitcodeCompressor::CompressFlags &Flags; |
| 489 | 482 |
| 490 // Mapping from block ID's to the corresponding list of abbreviations | 483 // Mapping from block ID's to the corresponding list of abbreviations |
| 491 // associated with that block. | 484 // associated with that block. |
| 492 BlockAbbrevsMapType &BlockAbbrevsMap; | 485 BlockAbbrevsMapType &BlockAbbrevsMap; |
| 493 | 486 |
| 494 // Nested distribution capturing distribution of records in bitcode file. | 487 // Nested distribution capturing distribution of records in bitcode file. |
| 495 NaClBitcodeBlockDist BlockDist; | 488 NaClBitcodeBlockDist BlockDist; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 522 /// block parser to parse a block with the given BlockID, and | 515 /// block parser to parse a block with the given BlockID, and |
| 523 /// collect data (for compression) in that block. | 516 /// collect data (for compression) in that block. |
| 524 NaClBlockAnalyzeParser(unsigned BlockID, | 517 NaClBlockAnalyzeParser(unsigned BlockID, |
| 525 NaClBlockAnalyzeParser *EnclosingParser) | 518 NaClBlockAnalyzeParser *EnclosingParser) |
| 526 : NaClBitcodeParser(BlockID, EnclosingParser), | 519 : NaClBitcodeParser(BlockID, EnclosingParser), |
| 527 Context(EnclosingParser->Context) { | 520 Context(EnclosingParser->Context) { |
| 528 Init(); | 521 Init(); |
| 529 } | 522 } |
| 530 | 523 |
| 531 public: | 524 public: |
| 532 virtual bool Error(const std::string &Message) { | |
| 533 // Use local error routine so that all errors are treated uniformly. | |
| 534 return ::Error(Message); | |
| 535 } | |
| 536 | |
| 537 virtual bool ParseBlock(unsigned BlockID) { | 525 virtual bool ParseBlock(unsigned BlockID) { |
| 538 NaClBlockAnalyzeParser Parser(BlockID, this); | 526 NaClBlockAnalyzeParser Parser(BlockID, this); |
| 539 return Parser.ParseThisBlock(); | 527 return Parser.ParseThisBlock(); |
| 540 } | 528 } |
| 541 | 529 |
| 542 virtual void ProcessRecord() { | 530 virtual void ProcessRecord() { |
| 543 // Before processing the record, we need to rename the abbreviation | 531 // Before processing the record, we need to rename the abbreviation |
| 544 // index, so that we can look it up in the set of block abbreviations | 532 // index, so that we can look it up in the set of block abbreviations |
| 545 // being defined. | 533 // being defined. |
| 546 if (Record.UsedAnAbbreviation()) { | 534 if (Record.UsedAnAbbreviation()) { |
| (...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1205 NaClBitstreamCursor &Cursor, | 1193 NaClBitstreamCursor &Cursor, |
| 1206 BlockAbbrevsMapType &BlockAbbrevsMap, | 1194 BlockAbbrevsMapType &BlockAbbrevsMap, |
| 1207 NaClBitstreamWriter &Writer) | 1195 NaClBitstreamWriter &Writer) |
| 1208 : NaClBitcodeParser(Cursor), | 1196 : NaClBitcodeParser(Cursor), |
| 1209 Flags(Flags), | 1197 Flags(Flags), |
| 1210 BlockAbbrevsMap(BlockAbbrevsMap), | 1198 BlockAbbrevsMap(BlockAbbrevsMap), |
| 1211 Writer(Writer) {} | 1199 Writer(Writer) {} |
| 1212 | 1200 |
| 1213 virtual ~NaClBitcodeCopyParser() {} | 1201 virtual ~NaClBitcodeCopyParser() {} |
| 1214 | 1202 |
| 1215 virtual bool Error(const std::string &Message) { | |
| 1216 // Use local error routine so that all errors are treated uniformly. | |
| 1217 return ::Error(Message); | |
| 1218 } | |
| 1219 | |
| 1220 virtual bool ParseBlock(unsigned BlockID); | 1203 virtual bool ParseBlock(unsigned BlockID); |
| 1221 | 1204 |
| 1222 const NaClBitcodeCompressor::CompressFlags &Flags; | 1205 const NaClBitcodeCompressor::CompressFlags &Flags; |
| 1223 | 1206 |
| 1224 // The abbreviations to use for the copied bitcode. | 1207 // The abbreviations to use for the copied bitcode. |
| 1225 BlockAbbrevsMapType &BlockAbbrevsMap; | 1208 BlockAbbrevsMapType &BlockAbbrevsMap; |
| 1226 | 1209 |
| 1227 // The bitstream to copy the compressed bitcode into. | 1210 // The bitstream to copy the compressed bitcode into. |
| 1228 NaClBitstreamWriter &Writer; | 1211 NaClBitstreamWriter &Writer; |
| 1229 }; | 1212 }; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 1250 | 1233 |
| 1251 /// Constructor to parse nested blocks. Creates a block parser to | 1234 /// Constructor to parse nested blocks. Creates a block parser to |
| 1252 /// parse in a block with the given BlockID, and write the block | 1235 /// parse in a block with the given BlockID, and write the block |
| 1253 /// back out using the abbreviations in BlockAbbrevsMap. | 1236 /// back out using the abbreviations in BlockAbbrevsMap. |
| 1254 NaClBlockCopyParser(unsigned BlockID, | 1237 NaClBlockCopyParser(unsigned BlockID, |
| 1255 NaClBlockCopyParser *EnclosingParser) | 1238 NaClBlockCopyParser *EnclosingParser) |
| 1256 : NaClBitcodeParser(BlockID, EnclosingParser), | 1239 : NaClBitcodeParser(BlockID, EnclosingParser), |
| 1257 Context(EnclosingParser->Context) | 1240 Context(EnclosingParser->Context) |
| 1258 {} | 1241 {} |
| 1259 | 1242 |
| 1260 virtual bool Error(const std::string &Message) { | |
| 1261 // Use local error routine so that all errors are treated uniformly. | |
| 1262 return ::Error(Message); | |
| 1263 } | |
| 1264 | |
|
jvoung (off chromium)
2015/03/06 22:30:28
wow so many =)
Karl
2015/03/06 22:54:20
Acknowledged.
| |
| 1265 /// Returns the set of (global) block abbreviations defined for the | 1243 /// Returns the set of (global) block abbreviations defined for the |
| 1266 /// given block ID. | 1244 /// given block ID. |
| 1267 BlockAbbrevs *GetGlobalAbbrevs(unsigned BlockID) { | 1245 BlockAbbrevs *GetGlobalAbbrevs(unsigned BlockID) { |
| 1268 BlockAbbrevs *Abbrevs = Context->BlockAbbrevsMap[BlockID]; | 1246 BlockAbbrevs *Abbrevs = Context->BlockAbbrevsMap[BlockID]; |
| 1269 if (Abbrevs == 0) { | 1247 if (Abbrevs == 0) { |
| 1270 Abbrevs = new BlockAbbrevs(BlockID); | 1248 Abbrevs = new BlockAbbrevs(BlockID); |
| 1271 Context->BlockAbbrevsMap[BlockID] = Abbrevs; | 1249 Context->BlockAbbrevsMap[BlockID] = Abbrevs; |
| 1272 } | 1250 } |
| 1273 return Abbrevs; | 1251 return Abbrevs; |
| 1274 } | 1252 } |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1425 | 1403 |
| 1426 bool NaClBitcodeCompressor::compress(MemoryBuffer *MemBuf, | 1404 bool NaClBitcodeCompressor::compress(MemoryBuffer *MemBuf, |
| 1427 raw_ostream &BitcodeOutput, | 1405 raw_ostream &BitcodeOutput, |
| 1428 raw_ostream &ShowOutput) { | 1406 raw_ostream &ShowOutput) { |
| 1429 BlockAbbrevsMapType BlockAbbrevsMap; | 1407 BlockAbbrevsMapType BlockAbbrevsMap; |
| 1430 if (AnalyzeBitcode(Flags, MemBuf, ShowOutput, BlockAbbrevsMap)) return false; | 1408 if (AnalyzeBitcode(Flags, MemBuf, ShowOutput, BlockAbbrevsMap)) return false; |
| 1431 BuildAbbrevLookupMaps(Flags, BlockAbbrevsMap); | 1409 BuildAbbrevLookupMaps(Flags, BlockAbbrevsMap); |
| 1432 if (CopyBitcode(Flags, MemBuf, BitcodeOutput, BlockAbbrevsMap)) return false; | 1410 if (CopyBitcode(Flags, MemBuf, BitcodeOutput, BlockAbbrevsMap)) return false; |
| 1433 return true; | 1411 return true; |
| 1434 } | 1412 } |
| OLD | NEW |