OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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. | |
jvoung (off chromium)
2013/05/21 23:56:45
This should only be used if there are no local abb
Karl
2013/05/24 16:55:33
Done.
| |
264 void EnterSubblock(unsigned BlockID) { | |
265 BlockInfo *Info = getBlockInfo(BlockID); | |
266 std::vector<NaClBitCodeAbbrev*>::size_type NumAbbrevs = | |
jvoung (off chromium)
2013/05/21 23:56:45
nit: Wow, this type is a bit hard to read =) This
Karl
2013/05/24 16:55:33
Simplified to size_t.
| |
267 Info | |
268 ? Info->Abbrevs.size() | |
269 : (std::vector<NaClBitCodeAbbrev*>::size_type) 0; | |
270 NaClBitcodeSelectorAbbrev DefaultCodeLen( | |
271 naclbitc::DEFAULT_MAX_ABBREV+NumAbbrevs); | |
272 EnterSubblock(BlockID, DefaultCodeLen, Info); | |
273 } | |
274 | |
275 /// \brief Enter block with the given number of abbreviations. | |
276 void EnterSubblock(unsigned BlockID, unsigned NumAbbrev) { | |
277 NaClBitcodeSelectorAbbrev CodeLenAbbrev(NumAbbrev); | |
278 EnterSubblock(BlockID, CodeLenAbbrev); | |
279 } | |
280 | |
242 void ExitBlock() { | 281 void ExitBlock() { |
243 assert(!BlockScope.empty() && "Block scope imbalance!"); | 282 assert(!BlockScope.empty() && "Block scope imbalance!"); |
244 | 283 |
245 // Delete all abbrevs. | 284 // Delete all abbrevs. |
246 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); | 285 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); |
247 i != e; ++i) | 286 i != e; ++i) |
248 CurAbbrevs[i]->dropRef(); | 287 CurAbbrevs[i]->dropRef(); |
249 | 288 |
250 const Block &B = BlockScope.back(); | 289 const Block &B = BlockScope.back(); |
251 | 290 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 CurAbbrevs.push_back(Abbv); | 533 CurAbbrevs.push_back(Abbv); |
495 return static_cast<unsigned>(CurAbbrevs.size())-1 + | 534 return static_cast<unsigned>(CurAbbrevs.size())-1 + |
496 naclbitc::FIRST_APPLICATION_ABBREV; | 535 naclbitc::FIRST_APPLICATION_ABBREV; |
497 } | 536 } |
498 | 537 |
499 //===--------------------------------------------------------------------===// | 538 //===--------------------------------------------------------------------===// |
500 // BlockInfo Block Emission | 539 // BlockInfo Block Emission |
501 //===--------------------------------------------------------------------===// | 540 //===--------------------------------------------------------------------===// |
502 | 541 |
503 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. | 542 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. |
504 void EnterBlockInfoBlock(unsigned CodeWidth) { | 543 void EnterBlockInfoBlock() { |
505 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID, CodeWidth); | 544 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID); |
506 BlockInfoCurBID = ~0U; | 545 BlockInfoCurBID = ~0U; |
507 } | 546 } |
508 private: | 547 private: |
509 /// SwitchToBlockID - If we aren't already talking about the specified block | 548 /// SwitchToBlockID - If we aren't already talking about the specified block |
510 /// ID, emit a BLOCKINFO_CODE_SETBID record. | 549 /// ID, emit a BLOCKINFO_CODE_SETBID record. |
511 void SwitchToBlockID(unsigned BlockID) { | 550 void SwitchToBlockID(unsigned BlockID) { |
512 if (BlockInfoCurBID == BlockID) return; | 551 if (BlockInfoCurBID == BlockID) return; |
513 SmallVector<unsigned, 2> V; | 552 SmallVector<unsigned, 2> V; |
514 V.push_back(BlockID); | 553 V.push_back(BlockID); |
515 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); | 554 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); |
(...skipping 23 matching lines...) Expand all Loading... | |
539 Info.Abbrevs.push_back(Abbv); | 578 Info.Abbrevs.push_back(Abbv); |
540 | 579 |
541 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; | 580 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; |
542 } | 581 } |
543 }; | 582 }; |
544 | 583 |
545 | 584 |
546 } // End llvm namespace | 585 } // End llvm namespace |
547 | 586 |
548 #endif | 587 #endif |
OLD | NEW |