Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: include/llvm/Bitcode/NaCl/NaClBitstreamWriter.h

Issue 14813032: Make abbreviations explicit in pnacl-freeze/thaw. (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Small cleanups suggested by Jan in CL. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- NaClBitstreamWriter.h - NaCl bitstream writer ------------*- C++ -*-===// 1 //===- NaClBitstreamWriter.h - NaCl bitstream writer ------------*- C++ -*-===//
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 // This header defines the BitstreamWriter class. This class can be used to 10 // This header defines the BitstreamWriter class. This class can be used to
(...skipping 15 matching lines...) Expand all
26 SmallVectorImpl<char> &Out; 26 SmallVectorImpl<char> &Out;
27 27
28 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use. 28 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
29 unsigned CurBit; 29 unsigned CurBit;
30 30
31 /// CurValue - The current value. Only bits < CurBit are valid. 31 /// CurValue - The current value. Only bits < CurBit are valid.
32 uint32_t CurValue; 32 uint32_t CurValue;
33 33
34 /// CurCodeSize - This is the declared size of code values used for the 34 /// CurCodeSize - This is the declared size of code values used for the
35 /// current block, in bits. 35 /// current block, in bits.
36 unsigned CurCodeSize; 36 NaClBitcodeSelectorAbbrev CurCodeSize;
37 37
38 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently 38 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently
39 /// selected BLOCK ID. 39 /// selected BLOCK ID.
40 unsigned BlockInfoCurBID; 40 unsigned BlockInfoCurBID;
41 41
42 /// CurAbbrevs - Abbrevs installed at in this block. 42 /// CurAbbrevs - Abbrevs installed at in this block.
43 std::vector<NaClBitCodeAbbrev*> CurAbbrevs; 43 std::vector<NaClBitCodeAbbrev*> CurAbbrevs;
44 44
45 struct Block { 45 struct Block {
46 unsigned PrevCodeSize; 46 NaClBitcodeSelectorAbbrev PrevCodeSize;
47 unsigned StartSizeWord; 47 unsigned StartSizeWord;
48 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs; 48 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs;
49 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {} 49 Block(const NaClBitcodeSelectorAbbrev& PCS, unsigned SSW)
50 : PrevCodeSize(PCS), StartSizeWord(SSW) {}
50 }; 51 };
51 52
52 /// BlockScope - This tracks the current blocks that we have entered. 53 /// BlockScope - This tracks the current blocks that we have entered.
53 std::vector<Block> BlockScope; 54 std::vector<Block> BlockScope;
54 55
55 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks. 56 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
56 /// These describe abbreviations that all blocks of the specified ID inherit. 57 /// These describe abbreviations that all blocks of the specified ID inherit.
57 struct BlockInfo { 58 struct BlockInfo {
58 unsigned BlockID; 59 unsigned BlockID;
59 std::vector<NaClBitCodeAbbrev*> Abbrevs; 60 std::vector<NaClBitCodeAbbrev*> Abbrevs;
(...skipping 27 matching lines...) Expand all
87 } 88 }
88 89
89 unsigned GetWordIndex() const { 90 unsigned GetWordIndex() const {
90 unsigned Offset = GetBufferOffset(); 91 unsigned Offset = GetBufferOffset();
91 assert((Offset & 3) == 0 && "Not 32-bit aligned"); 92 assert((Offset & 3) == 0 && "Not 32-bit aligned");
92 return Offset / 4; 93 return Offset / 4;
93 } 94 }
94 95
95 public: 96 public:
96 explicit NaClBitstreamWriter(SmallVectorImpl<char> &O) 97 explicit NaClBitstreamWriter(SmallVectorImpl<char> &O)
97 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {} 98 : Out(O), CurBit(0), CurValue(0), CurCodeSize() {}
98 99
99 ~NaClBitstreamWriter() { 100 ~NaClBitstreamWriter() {
100 assert(CurBit == 0 && "Unflused data remaining"); 101 assert(CurBit == 0 && "Unflused data remaining");
101 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance"); 102 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
102 103
103 // Free the BlockInfoRecords. 104 // Free the BlockInfoRecords.
104 while (!BlockInfoRecords.empty()) { 105 while (!BlockInfoRecords.empty()) {
105 BlockInfo &Info = BlockInfoRecords.back(); 106 BlockInfo &Info = BlockInfoRecords.back();
106 // Free blockinfo abbrev info. 107 // Free blockinfo abbrev info.
107 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); 108 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 void FlushToWord() { 150 void FlushToWord() {
150 if (CurBit) { 151 if (CurBit) {
151 WriteWord(CurValue); 152 WriteWord(CurValue);
152 CurBit = 0; 153 CurBit = 0;
153 CurValue = 0; 154 CurValue = 0;
154 } 155 }
155 } 156 }
156 157
157 void EmitVBR(uint32_t Val, unsigned NumBits) { 158 void EmitVBR(uint32_t Val, unsigned NumBits) {
158 assert(NumBits <= 32 && "Too many bits to emit!"); 159 assert(NumBits <= 32 && "Too many bits to emit!");
160 assert(NumBits > 1 && "Too few bits to emit!");
159 uint32_t Threshold = 1U << (NumBits-1); 161 uint32_t Threshold = 1U << (NumBits-1);
160 162
161 // Emit the bits with VBR encoding, NumBits-1 bits at a time. 163 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
162 while (Val >= Threshold) { 164 while (Val >= Threshold) {
163 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits); 165 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
164 Val >>= NumBits-1; 166 Val >>= NumBits-1;
165 } 167 }
166 168
167 Emit(Val, NumBits); 169 Emit(Val, NumBits);
168 } 170 }
169 171
170 void EmitVBR64(uint64_t Val, unsigned NumBits) { 172 void EmitVBR64(uint64_t Val, unsigned NumBits) {
171 assert(NumBits <= 32 && "Too many bits to emit!"); 173 assert(NumBits <= 32 && "Too many bits to emit!");
174 assert(NumBits > 1 && "Too few bits to emit!");
172 if ((uint32_t)Val == Val) 175 if ((uint32_t)Val == Val)
173 return EmitVBR((uint32_t)Val, NumBits); 176 return EmitVBR((uint32_t)Val, NumBits);
174 177
175 uint32_t Threshold = 1U << (NumBits-1); 178 uint32_t Threshold = 1U << (NumBits-1);
176 179
177 // Emit the bits with VBR encoding, NumBits-1 bits at a time. 180 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
178 while (Val >= Threshold) { 181 while (Val >= Threshold) {
179 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) | 182 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
180 (1 << (NumBits-1)), NumBits); 183 (1 << (NumBits-1)), NumBits);
181 Val >>= NumBits-1; 184 Val >>= NumBits-1;
182 } 185 }
183 186
184 Emit((uint32_t)Val, NumBits); 187 Emit((uint32_t)Val, NumBits);
185 } 188 }
186 189
187 /// EmitCode - Emit the specified code. 190 /// EmitCode - Emit the specified code.
188 void EmitCode(unsigned Val) { 191 void EmitCode(unsigned Val) {
189 Emit(Val, CurCodeSize); 192 if (CurCodeSize.IsFixed)
193 Emit(Val, CurCodeSize.NumBits);
194 else
195 EmitVBR(Val, CurCodeSize.NumBits);
190 } 196 }
191 197
192 //===--------------------------------------------------------------------===// 198 //===--------------------------------------------------------------------===//
193 // Block Manipulation 199 // Block Manipulation
194 //===--------------------------------------------------------------------===// 200 //===--------------------------------------------------------------------===//
195 201
196 /// getBlockInfo - If there is block info for the specified ID, return it, 202 /// getBlockInfo - If there is block info for the specified ID, return it,
197 /// otherwise return null. 203 /// otherwise return null.
198 BlockInfo *getBlockInfo(unsigned BlockID) { 204 BlockInfo *getBlockInfo(unsigned BlockID) {
199 // Common case, the most recent entry matches BlockID. 205 // Common case, the most recent entry matches BlockID.
200 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) 206 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
201 return &BlockInfoRecords.back(); 207 return &BlockInfoRecords.back();
202 208
203 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); 209 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
204 i != e; ++i) 210 i != e; ++i)
205 if (BlockInfoRecords[i].BlockID == BlockID) 211 if (BlockInfoRecords[i].BlockID == BlockID)
206 return &BlockInfoRecords[i]; 212 return &BlockInfoRecords[i];
207 return 0; 213 return 0;
208 } 214 }
209 215
210 void EnterSubblock(unsigned BlockID, unsigned CodeLen) { 216 private:
217 // Enter block using CodeLen bits to read the size of the code
218 // selector associated with the block.
219 void EnterSubblock(unsigned BlockID,
220 const NaClBitcodeSelectorAbbrev& CodeLen,
221 BlockInfo *Info) {
211 // Block header: 222 // Block header:
212 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] 223 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
213 EmitCode(naclbitc::ENTER_SUBBLOCK); 224 EmitCode(naclbitc::ENTER_SUBBLOCK);
214 EmitVBR(BlockID, naclbitc::BlockIDWidth); 225 EmitVBR(BlockID, naclbitc::BlockIDWidth);
215 EmitVBR(CodeLen, naclbitc::CodeLenWidth); 226 assert(CodeLen.IsFixed && "Block codelens must be fixed");
227 EmitVBR(CodeLen.NumBits, naclbitc::CodeLenWidth);
216 FlushToWord(); 228 FlushToWord();
217 229
218 unsigned BlockSizeWordIndex = GetWordIndex(); 230 unsigned BlockSizeWordIndex = GetWordIndex();
219 unsigned OldCodeSize = CurCodeSize; 231 NaClBitcodeSelectorAbbrev OldCodeSize(CurCodeSize);
220 232
221 // Emit a placeholder, which will be replaced when the block is popped. 233 // Emit a placeholder, which will be replaced when the block is popped.
222 Emit(0, naclbitc::BlockSizeWidth); 234 Emit(0, naclbitc::BlockSizeWidth);
223 235
224 CurCodeSize = CodeLen; 236 CurCodeSize = CodeLen;
225 237
226 // Push the outer block's abbrev set onto the stack, start out with an 238 // Push the outer block's abbrev set onto the stack, start out with an
227 // empty abbrev set. 239 // empty abbrev set.
228 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); 240 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex));
229 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); 241 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
230 242
231 // If there is a blockinfo for this BlockID, add all the predefined abbrevs 243 // If there is a blockinfo for this BlockID, add all the predefined abbrevs
232 // to the abbrev list. 244 // to the abbrev list.
233 if (BlockInfo *Info = getBlockInfo(BlockID)) { 245 if (Info) {
234 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size()); 246 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
235 i != e; ++i) { 247 i != e; ++i) {
236 CurAbbrevs.push_back(Info->Abbrevs[i]); 248 CurAbbrevs.push_back(Info->Abbrevs[i]);
237 Info->Abbrevs[i]->addRef(); 249 Info->Abbrevs[i]->addRef();
238 } 250 }
239 } 251 }
240 } 252 }
241 253
254 public:
255 /// \brief Enter block using CodeLen bits to read the size of the code
256 /// selector associated with the block.
257 void EnterSubblock(unsigned BlockID,
258 const NaClBitcodeSelectorAbbrev& CodeLen) {
259 EnterSubblock(BlockID, CodeLen, getBlockInfo(BlockID));
260 }
261
262 /// \brief Enter block, using a code length based on the number of
263 /// (global) BlockInfo entries defined for the block. Note: This
264 /// should be used only if the block doesn't define any local abbreviations.
265 void EnterSubblock(unsigned BlockID) {
266 BlockInfo *Info = getBlockInfo(BlockID);
267 size_t NumAbbrevs = Info ? Info->Abbrevs.size() : 0;
268 NaClBitcodeSelectorAbbrev DefaultCodeLen(
269 naclbitc::DEFAULT_MAX_ABBREV+NumAbbrevs);
270 EnterSubblock(BlockID, DefaultCodeLen, Info);
271 }
272
273 /// \brief Enter block with the given number of abbreviations.
274 void EnterSubblock(unsigned BlockID, unsigned NumAbbrev) {
275 NaClBitcodeSelectorAbbrev CodeLenAbbrev(NumAbbrev);
276 EnterSubblock(BlockID, CodeLenAbbrev);
277 }
278
242 void ExitBlock() { 279 void ExitBlock() {
243 assert(!BlockScope.empty() && "Block scope imbalance!"); 280 assert(!BlockScope.empty() && "Block scope imbalance!");
244 281
245 // Delete all abbrevs. 282 // Delete all abbrevs.
246 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); 283 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
247 i != e; ++i) 284 i != e; ++i)
248 CurAbbrevs[i]->dropRef(); 285 CurAbbrevs[i]->dropRef();
249 286
250 const Block &B = BlockScope.back(); 287 const Block &B = BlockScope.back();
251 288
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 CurAbbrevs.push_back(Abbv); 531 CurAbbrevs.push_back(Abbv);
495 return static_cast<unsigned>(CurAbbrevs.size())-1 + 532 return static_cast<unsigned>(CurAbbrevs.size())-1 +
496 naclbitc::FIRST_APPLICATION_ABBREV; 533 naclbitc::FIRST_APPLICATION_ABBREV;
497 } 534 }
498 535
499 //===--------------------------------------------------------------------===// 536 //===--------------------------------------------------------------------===//
500 // BlockInfo Block Emission 537 // BlockInfo Block Emission
501 //===--------------------------------------------------------------------===// 538 //===--------------------------------------------------------------------===//
502 539
503 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. 540 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
504 void EnterBlockInfoBlock(unsigned CodeWidth) { 541 void EnterBlockInfoBlock() {
505 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID, CodeWidth); 542 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID);
506 BlockInfoCurBID = ~0U; 543 BlockInfoCurBID = ~0U;
507 } 544 }
508 private: 545 private:
509 /// SwitchToBlockID - If we aren't already talking about the specified block 546 /// SwitchToBlockID - If we aren't already talking about the specified block
510 /// ID, emit a BLOCKINFO_CODE_SETBID record. 547 /// ID, emit a BLOCKINFO_CODE_SETBID record.
511 void SwitchToBlockID(unsigned BlockID) { 548 void SwitchToBlockID(unsigned BlockID) {
512 if (BlockInfoCurBID == BlockID) return; 549 if (BlockInfoCurBID == BlockID) return;
513 SmallVector<unsigned, 2> V; 550 SmallVector<unsigned, 2> V;
514 V.push_back(BlockID); 551 V.push_back(BlockID);
515 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); 552 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V);
(...skipping 23 matching lines...) Expand all
539 Info.Abbrevs.push_back(Abbv); 576 Info.Abbrevs.push_back(Abbv);
540 577
541 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; 578 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV;
542 } 579 }
543 }; 580 };
544 581
545 582
546 } // End llvm namespace 583 } // End llvm namespace
547 584
548 #endif 585 #endif
OLDNEW
« no previous file with comments | « include/llvm/Bitcode/NaCl/NaClBitstreamReader.h ('k') | lib/Bitcode/NaCl/Reader/NaClBitcodeReader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698