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 22 matching lines...) Expand all Loading... | |
130 // Add the current word. | 131 // Add the current word. |
131 WriteWord(CurValue); | 132 WriteWord(CurValue); |
132 | 133 |
133 if (CurBit) | 134 if (CurBit) |
134 CurValue = Val >> (32-CurBit); | 135 CurValue = Val >> (32-CurBit); |
135 else | 136 else |
136 CurValue = 0; | 137 CurValue = 0; |
137 CurBit = (CurBit+NumBits) & 31; | 138 CurBit = (CurBit+NumBits) & 31; |
138 } | 139 } |
139 | 140 |
141 void EmitBool(bool b) { | |
Karl
2013/05/20 22:59:20
Deleting this, since it is no longer needed.
| |
142 Emit((uint32_t) b, 1); | |
143 } | |
144 | |
140 void Emit64(uint64_t Val, unsigned NumBits) { | 145 void Emit64(uint64_t Val, unsigned NumBits) { |
141 if (NumBits <= 32) | 146 if (NumBits <= 32) |
142 Emit((uint32_t)Val, NumBits); | 147 Emit((uint32_t)Val, NumBits); |
143 else { | 148 else { |
144 Emit((uint32_t)Val, 32); | 149 Emit((uint32_t)Val, 32); |
145 Emit((uint32_t)(Val >> 32), NumBits-32); | 150 Emit((uint32_t)(Val >> 32), NumBits-32); |
146 } | 151 } |
147 } | 152 } |
148 | 153 |
149 void FlushToWord() { | 154 void FlushToWord() { |
150 if (CurBit) { | 155 if (CurBit) { |
151 WriteWord(CurValue); | 156 WriteWord(CurValue); |
152 CurBit = 0; | 157 CurBit = 0; |
153 CurValue = 0; | 158 CurValue = 0; |
154 } | 159 } |
155 } | 160 } |
156 | 161 |
157 void EmitVBR(uint32_t Val, unsigned NumBits) { | 162 void EmitVBR(uint32_t Val, unsigned NumBits) { |
158 assert(NumBits <= 32 && "Too many bits to emit!"); | 163 assert(NumBits <= 32 && "Too many bits to emit!"); |
164 assert(NumBits > 1 && "Too few bits to emit!"); | |
159 uint32_t Threshold = 1U << (NumBits-1); | 165 uint32_t Threshold = 1U << (NumBits-1); |
160 | 166 |
161 // Emit the bits with VBR encoding, NumBits-1 bits at a time. | 167 // Emit the bits with VBR encoding, NumBits-1 bits at a time. |
162 while (Val >= Threshold) { | 168 while (Val >= Threshold) { |
163 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits); | 169 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits); |
164 Val >>= NumBits-1; | 170 Val >>= NumBits-1; |
165 } | 171 } |
166 | 172 |
167 Emit(Val, NumBits); | 173 Emit(Val, NumBits); |
168 } | 174 } |
169 | 175 |
170 void EmitVBR64(uint64_t Val, unsigned NumBits) { | 176 void EmitVBR64(uint64_t Val, unsigned NumBits) { |
171 assert(NumBits <= 32 && "Too many bits to emit!"); | 177 assert(NumBits <= 32 && "Too many bits to emit!"); |
178 assert(NumBits > 1 && "Too few bits to emit!"); | |
172 if ((uint32_t)Val == Val) | 179 if ((uint32_t)Val == Val) |
173 return EmitVBR((uint32_t)Val, NumBits); | 180 return EmitVBR((uint32_t)Val, NumBits); |
174 | 181 |
175 uint32_t Threshold = 1U << (NumBits-1); | 182 uint32_t Threshold = 1U << (NumBits-1); |
176 | 183 |
177 // Emit the bits with VBR encoding, NumBits-1 bits at a time. | 184 // Emit the bits with VBR encoding, NumBits-1 bits at a time. |
178 while (Val >= Threshold) { | 185 while (Val >= Threshold) { |
179 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) | | 186 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) | |
180 (1 << (NumBits-1)), NumBits); | 187 (1 << (NumBits-1)), NumBits); |
181 Val >>= NumBits-1; | 188 Val >>= NumBits-1; |
182 } | 189 } |
183 | 190 |
184 Emit((uint32_t)Val, NumBits); | 191 Emit((uint32_t)Val, NumBits); |
185 } | 192 } |
186 | 193 |
187 /// EmitCode - Emit the specified code. | 194 /// EmitCode - Emit the specified code. |
188 void EmitCode(unsigned Val) { | 195 void EmitCode(unsigned Val) { |
189 Emit(Val, CurCodeSize); | 196 if (CurCodeSize.IsFixed) |
197 Emit(Val, CurCodeSize.NumBits); | |
198 else | |
199 EmitVBR(Val, CurCodeSize.NumBits); | |
190 } | 200 } |
191 | 201 |
192 //===--------------------------------------------------------------------===// | 202 //===--------------------------------------------------------------------===// |
193 // Block Manipulation | 203 // Block Manipulation |
194 //===--------------------------------------------------------------------===// | 204 //===--------------------------------------------------------------------===// |
195 | 205 |
196 /// getBlockInfo - If there is block info for the specified ID, return it, | 206 /// getBlockInfo - If there is block info for the specified ID, return it, |
197 /// otherwise return null. | 207 /// otherwise return null. |
198 BlockInfo *getBlockInfo(unsigned BlockID) { | 208 BlockInfo *getBlockInfo(unsigned BlockID) { |
199 // Common case, the most recent entry matches BlockID. | 209 // Common case, the most recent entry matches BlockID. |
200 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) | 210 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) |
201 return &BlockInfoRecords.back(); | 211 return &BlockInfoRecords.back(); |
202 | 212 |
203 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); | 213 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); |
204 i != e; ++i) | 214 i != e; ++i) |
205 if (BlockInfoRecords[i].BlockID == BlockID) | 215 if (BlockInfoRecords[i].BlockID == BlockID) |
206 return &BlockInfoRecords[i]; | 216 return &BlockInfoRecords[i]; |
207 return 0; | 217 return 0; |
208 } | 218 } |
209 | 219 |
210 void EnterSubblock(unsigned BlockID, unsigned CodeLen) { | 220 // Enter block using CodeLen bits to read the size of the code selector |
221 // associated with the block. | |
222 void EnterSubblock(unsigned BlockID, | |
223 const NaClBitcodeSelectorAbbrev& CodeLen) { | |
211 // Block header: | 224 // Block header: |
212 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | 225 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] |
213 EmitCode(naclbitc::ENTER_SUBBLOCK); | 226 EmitCode(naclbitc::ENTER_SUBBLOCK); |
214 EmitVBR(BlockID, naclbitc::BlockIDWidth); | 227 EmitVBR(BlockID, naclbitc::BlockIDWidth); |
215 EmitVBR(CodeLen, naclbitc::CodeLenWidth); | 228 EmitBool(CodeLen.IsFixed); |
jvoung (off chromium)
2013/05/17 23:40:48
See below: if the fixedabbrevcutoff variant isn't
Karl
2013/05/20 22:59:20
Deleted.
| |
229 EmitVBR(CodeLen.NumBits, naclbitc::CodeLenWidth); | |
216 FlushToWord(); | 230 FlushToWord(); |
217 | 231 |
218 unsigned BlockSizeWordIndex = GetWordIndex(); | 232 unsigned BlockSizeWordIndex = GetWordIndex(); |
219 unsigned OldCodeSize = CurCodeSize; | 233 NaClBitcodeSelectorAbbrev OldCodeSize(CurCodeSize); |
220 | 234 |
221 // Emit a placeholder, which will be replaced when the block is popped. | 235 // Emit a placeholder, which will be replaced when the block is popped. |
222 Emit(0, naclbitc::BlockSizeWidth); | 236 Emit(0, naclbitc::BlockSizeWidth); |
223 | 237 |
224 CurCodeSize = CodeLen; | 238 CurCodeSize = CodeLen; |
225 | 239 |
226 // Push the outer block's abbrev set onto the stack, start out with an | 240 // Push the outer block's abbrev set onto the stack, start out with an |
227 // empty abbrev set. | 241 // empty abbrev set. |
228 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); | 242 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); |
229 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); | 243 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); |
230 | 244 |
231 // If there is a blockinfo for this BlockID, add all the predefined abbrevs | 245 // If there is a blockinfo for this BlockID, add all the predefined abbrevs |
232 // to the abbrev list. | 246 // to the abbrev list. |
233 if (BlockInfo *Info = getBlockInfo(BlockID)) { | 247 if (BlockInfo *Info = getBlockInfo(BlockID)) { |
234 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size()); | 248 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size()); |
235 i != e; ++i) { | 249 i != e; ++i) { |
236 CurAbbrevs.push_back(Info->Abbrevs[i]); | 250 CurAbbrevs.push_back(Info->Abbrevs[i]); |
237 Info->Abbrevs[i]->addRef(); | 251 Info->Abbrevs[i]->addRef(); |
238 } | 252 } |
239 } | 253 } |
240 } | 254 } |
241 | 255 |
256 // Enter block with no abbreviations. | |
257 void EnterSubblock(unsigned BlockID) { | |
258 NaClBitcodeSelectorAbbrev DefaultCodeLen; | |
259 EnterSubblock(BlockID, DefaultCodeLen); | |
260 } | |
261 | |
262 // Enter block with the given number of abbreviations. | |
263 void EnterSubblock(unsigned BlockID, unsigned NumAbbrev) { | |
264 NaClBitcodeSelectorAbbrev CodeLenAbbrev(NumAbbrev); | |
265 EnterSubblock(BlockID, CodeLenAbbrev); | |
266 } | |
267 | |
268 // Enter block with the given number of abbreviations NumAbbrev, | |
269 // with the constraint that all abbreviations < FixedAbbrevCutoff will | |
270 // be read using a single read. | |
271 void EnterSubblock(unsigned BlockId, | |
jvoung (off chromium)
2013/05/17 23:40:48
Is this actually used?
Karl
2013/05/20 22:59:20
Again, I'm over planning changes. Removing this ca
| |
272 unsigned NumAbbrev, | |
273 unsigned FixedAbbrevCutoff) { | |
274 NaClBitcodeSelectorAbbrev CodeLenAbbrev(FixedAbbrevCutoff, NumAbbrev); | |
275 EnterSubblock(BlockId, CodeLenAbbrev); | |
276 } | |
277 | |
242 void ExitBlock() { | 278 void ExitBlock() { |
243 assert(!BlockScope.empty() && "Block scope imbalance!"); | 279 assert(!BlockScope.empty() && "Block scope imbalance!"); |
244 | 280 |
245 // Delete all abbrevs. | 281 // Delete all abbrevs. |
246 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); | 282 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); |
247 i != e; ++i) | 283 i != e; ++i) |
248 CurAbbrevs[i]->dropRef(); | 284 CurAbbrevs[i]->dropRef(); |
249 | 285 |
250 const Block &B = BlockScope.back(); | 286 const Block &B = BlockScope.back(); |
251 | 287 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 CurAbbrevs.push_back(Abbv); | 530 CurAbbrevs.push_back(Abbv); |
495 return static_cast<unsigned>(CurAbbrevs.size())-1 + | 531 return static_cast<unsigned>(CurAbbrevs.size())-1 + |
496 naclbitc::FIRST_APPLICATION_ABBREV; | 532 naclbitc::FIRST_APPLICATION_ABBREV; |
497 } | 533 } |
498 | 534 |
499 //===--------------------------------------------------------------------===// | 535 //===--------------------------------------------------------------------===// |
500 // BlockInfo Block Emission | 536 // BlockInfo Block Emission |
501 //===--------------------------------------------------------------------===// | 537 //===--------------------------------------------------------------------===// |
502 | 538 |
503 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. | 539 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. |
504 void EnterBlockInfoBlock(unsigned CodeWidth) { | 540 void EnterBlockInfoBlock() { |
505 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID, CodeWidth); | 541 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID); |
506 BlockInfoCurBID = ~0U; | 542 BlockInfoCurBID = ~0U; |
507 } | 543 } |
508 private: | 544 private: |
509 /// SwitchToBlockID - If we aren't already talking about the specified block | 545 /// SwitchToBlockID - If we aren't already talking about the specified block |
510 /// ID, emit a BLOCKINFO_CODE_SETBID record. | 546 /// ID, emit a BLOCKINFO_CODE_SETBID record. |
511 void SwitchToBlockID(unsigned BlockID) { | 547 void SwitchToBlockID(unsigned BlockID) { |
512 if (BlockInfoCurBID == BlockID) return; | 548 if (BlockInfoCurBID == BlockID) return; |
513 SmallVector<unsigned, 2> V; | 549 SmallVector<unsigned, 2> V; |
514 V.push_back(BlockID); | 550 V.push_back(BlockID); |
515 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); | 551 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); |
(...skipping 23 matching lines...) Expand all Loading... | |
539 Info.Abbrevs.push_back(Abbv); | 575 Info.Abbrevs.push_back(Abbv); |
540 | 576 |
541 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; | 577 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; |
542 } | 578 } |
543 }; | 579 }; |
544 | 580 |
545 | 581 |
546 } // End llvm namespace | 582 } // End llvm namespace |
547 | 583 |
548 #endif | 584 #endif |
OLD | NEW |