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

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: Fix code based on Jan's suggestions. 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 // Enter block using CodeLen bits to read the size of the code selector
217 // associated with the block.
218 void EnterSubblock(unsigned BlockID,
219 const NaClBitcodeSelectorAbbrev& CodeLen) {
Karl 2013/05/21 23:06:52 Refactored the private version of this function to
211 // Block header: 220 // Block header:
212 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] 221 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
213 EmitCode(naclbitc::ENTER_SUBBLOCK); 222 EmitCode(naclbitc::ENTER_SUBBLOCK);
214 EmitVBR(BlockID, naclbitc::BlockIDWidth); 223 EmitVBR(BlockID, naclbitc::BlockIDWidth);
215 EmitVBR(CodeLen, naclbitc::CodeLenWidth); 224 assert(CodeLen.IsFixed && "Block codelens must be fixed");
225 EmitVBR(CodeLen.NumBits, naclbitc::CodeLenWidth);
216 FlushToWord(); 226 FlushToWord();
217 227
218 unsigned BlockSizeWordIndex = GetWordIndex(); 228 unsigned BlockSizeWordIndex = GetWordIndex();
219 unsigned OldCodeSize = CurCodeSize; 229 NaClBitcodeSelectorAbbrev OldCodeSize(CurCodeSize);
220 230
221 // Emit a placeholder, which will be replaced when the block is popped. 231 // Emit a placeholder, which will be replaced when the block is popped.
222 Emit(0, naclbitc::BlockSizeWidth); 232 Emit(0, naclbitc::BlockSizeWidth);
223 233
224 CurCodeSize = CodeLen; 234 CurCodeSize = CodeLen;
225 235
226 // Push the outer block's abbrev set onto the stack, start out with an 236 // Push the outer block's abbrev set onto the stack, start out with an
227 // empty abbrev set. 237 // empty abbrev set.
228 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); 238 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex));
229 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); 239 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
230 240
231 // If there is a blockinfo for this BlockID, add all the predefined abbrevs 241 // If there is a blockinfo for this BlockID, add all the predefined abbrevs
232 // to the abbrev list. 242 // to the abbrev list.
233 if (BlockInfo *Info = getBlockInfo(BlockID)) { 243 if (BlockInfo *Info = getBlockInfo(BlockID)) {
234 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size()); 244 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
235 i != e; ++i) { 245 i != e; ++i) {
236 CurAbbrevs.push_back(Info->Abbrevs[i]); 246 CurAbbrevs.push_back(Info->Abbrevs[i]);
237 Info->Abbrevs[i]->addRef(); 247 Info->Abbrevs[i]->addRef();
238 } 248 }
239 } 249 }
240 } 250 }
241 251
252 // Enter block with no abbreviations.
253 void EnterSubblock(unsigned BlockID) {
Karl 2013/05/21 23:06:52 Modified this function to calculate the code lengt
254 NaClBitcodeSelectorAbbrev DefaultCodeLen;
255 EnterSubblock(BlockID, DefaultCodeLen);
256 }
257
258 // Enter block with the given number of abbreviations.
259 void EnterSubblock(unsigned BlockID, unsigned NumAbbrev) {
260 NaClBitcodeSelectorAbbrev CodeLenAbbrev(NumAbbrev);
261 EnterSubblock(BlockID, CodeLenAbbrev);
262 }
263
242 void ExitBlock() { 264 void ExitBlock() {
243 assert(!BlockScope.empty() && "Block scope imbalance!"); 265 assert(!BlockScope.empty() && "Block scope imbalance!");
244 266
245 // Delete all abbrevs. 267 // Delete all abbrevs.
246 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); 268 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
247 i != e; ++i) 269 i != e; ++i)
248 CurAbbrevs[i]->dropRef(); 270 CurAbbrevs[i]->dropRef();
249 271
250 const Block &B = BlockScope.back(); 272 const Block &B = BlockScope.back();
251 273
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 CurAbbrevs.push_back(Abbv); 516 CurAbbrevs.push_back(Abbv);
495 return static_cast<unsigned>(CurAbbrevs.size())-1 + 517 return static_cast<unsigned>(CurAbbrevs.size())-1 +
496 naclbitc::FIRST_APPLICATION_ABBREV; 518 naclbitc::FIRST_APPLICATION_ABBREV;
497 } 519 }
498 520
499 //===--------------------------------------------------------------------===// 521 //===--------------------------------------------------------------------===//
500 // BlockInfo Block Emission 522 // BlockInfo Block Emission
501 //===--------------------------------------------------------------------===// 523 //===--------------------------------------------------------------------===//
502 524
503 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. 525 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
504 void EnterBlockInfoBlock(unsigned CodeWidth) { 526 void EnterBlockInfoBlock() {
505 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID, CodeWidth); 527 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID);
506 BlockInfoCurBID = ~0U; 528 BlockInfoCurBID = ~0U;
507 } 529 }
508 private: 530 private:
509 /// SwitchToBlockID - If we aren't already talking about the specified block 531 /// SwitchToBlockID - If we aren't already talking about the specified block
510 /// ID, emit a BLOCKINFO_CODE_SETBID record. 532 /// ID, emit a BLOCKINFO_CODE_SETBID record.
511 void SwitchToBlockID(unsigned BlockID) { 533 void SwitchToBlockID(unsigned BlockID) {
512 if (BlockInfoCurBID == BlockID) return; 534 if (BlockInfoCurBID == BlockID) return;
513 SmallVector<unsigned, 2> V; 535 SmallVector<unsigned, 2> V;
514 V.push_back(BlockID); 536 V.push_back(BlockID);
515 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); 537 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V);
(...skipping 23 matching lines...) Expand all
539 Info.Abbrevs.push_back(Abbv); 561 Info.Abbrevs.push_back(Abbv);
540 562
541 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; 563 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV;
542 } 564 }
543 }; 565 };
544 566
545 567
546 } // End llvm namespace 568 } // End llvm namespace
547 569
548 #endif 570 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698