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 |
11 // write an arbitrary bitstream, regardless of its contents. | 11 // write an arbitrary bitstream, regardless of its contents. |
12 // | 12 // |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H | 15 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H |
16 #define LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H | 16 #define LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H |
17 | 17 |
18 #include "llvm/ADT/SmallVector.h" | 18 #include "llvm/ADT/SmallVector.h" |
19 #include "llvm/ADT/StringRef.h" | 19 #include "llvm/ADT/StringRef.h" |
20 #include "llvm/Bitcode/NaCl/NaClBitCodes.h" | 20 #include "llvm/Bitcode/NaCl/NaClBitCodes.h" |
21 #include <vector> | 21 #include <vector> |
22 | 22 |
23 #include "llvm/Support/raw_ostream.h" | |
jvoung (off chromium)
2013/05/14 23:47:14
what is this used for?
Karl
2013/05/17 20:52:18
Removed.
| |
24 | |
23 namespace llvm { | 25 namespace llvm { |
24 | 26 |
25 class NaClBitstreamWriter { | 27 class NaClBitstreamWriter { |
26 SmallVectorImpl<char> &Out; | 28 SmallVectorImpl<char> &Out; |
27 | 29 |
28 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use. | 30 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use. |
29 unsigned CurBit; | 31 unsigned CurBit; |
30 | 32 |
31 /// CurValue - The current value. Only bits < CurBit are valid. | 33 /// CurValue - The current value. Only bits < CurBit are valid. |
32 uint32_t CurValue; | 34 uint32_t CurValue; |
33 | 35 |
34 /// CurCodeSize - This is the declared size of code values used for the | 36 /// CurCodeSize - This is the declared size of code values used for the |
35 /// current block, in bits. | 37 /// current block, in bits. |
36 unsigned CurCodeSize; | 38 NaClBitcodeSelectorAbbrev CurCodeSize; |
37 | 39 |
38 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently | 40 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently |
39 /// selected BLOCK ID. | 41 /// selected BLOCK ID. |
40 unsigned BlockInfoCurBID; | 42 unsigned BlockInfoCurBID; |
41 | 43 |
42 /// CurAbbrevs - Abbrevs installed at in this block. | 44 /// CurAbbrevs - Abbrevs installed at in this block. |
43 std::vector<NaClBitCodeAbbrev*> CurAbbrevs; | 45 std::vector<NaClBitCodeAbbrev*> CurAbbrevs; |
44 | 46 |
45 struct Block { | 47 struct Block { |
46 unsigned PrevCodeSize; | 48 NaClBitcodeSelectorAbbrev PrevCodeSize; |
47 unsigned StartSizeWord; | 49 unsigned StartSizeWord; |
48 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs; | 50 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs; |
49 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {} | 51 Block(const NaClBitcodeSelectorAbbrev& PCS, unsigned SSW) |
52 : PrevCodeSize(PCS), StartSizeWord(SSW) {} | |
50 }; | 53 }; |
51 | 54 |
52 /// BlockScope - This tracks the current blocks that we have entered. | 55 /// BlockScope - This tracks the current blocks that we have entered. |
53 std::vector<Block> BlockScope; | 56 std::vector<Block> BlockScope; |
54 | 57 |
55 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks. | 58 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks. |
56 /// These describe abbreviations that all blocks of the specified ID inherit. | 59 /// These describe abbreviations that all blocks of the specified ID inherit. |
57 struct BlockInfo { | 60 struct BlockInfo { |
58 unsigned BlockID; | 61 unsigned BlockID; |
59 std::vector<NaClBitCodeAbbrev*> Abbrevs; | 62 std::vector<NaClBitCodeAbbrev*> Abbrevs; |
(...skipping 25 matching lines...) Expand all Loading... | |
85 unsigned GetBufferOffset() const { | 88 unsigned GetBufferOffset() const { |
86 return Out.size(); | 89 return Out.size(); |
87 } | 90 } |
88 | 91 |
89 unsigned GetWordIndex() const { | 92 unsigned GetWordIndex() const { |
90 unsigned Offset = GetBufferOffset(); | 93 unsigned Offset = GetBufferOffset(); |
91 assert((Offset & 3) == 0 && "Not 32-bit aligned"); | 94 assert((Offset & 3) == 0 && "Not 32-bit aligned"); |
92 return Offset / 4; | 95 return Offset / 4; |
93 } | 96 } |
94 | 97 |
98 // Helper to EmitIntVBR to print out first chunk. One bit is for the sign, | |
99 // and the remaining bits is to encode Val. | |
100 void EmitIntVBRChunk(uint32_t Val, bool IsNegative, unsigned NumBits, | |
Karl
2013/05/17 20:52:18
Removed. No longer used. Uses NaClEncodeSignRotate
| |
101 bool IsFirstIteration) { | |
102 if (IsFirstIteration) { | |
103 if (IsNegative) Val &= (1 << NumBits); | |
104 Emit(Val, NumBits+1); | |
105 } else { | |
106 Emit(Val, NumBits); | |
107 } | |
108 } | |
109 | |
95 public: | 110 public: |
96 explicit NaClBitstreamWriter(SmallVectorImpl<char> &O) | 111 explicit NaClBitstreamWriter(SmallVectorImpl<char> &O) |
97 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {} | 112 : Out(O), CurBit(0), CurValue(0), CurCodeSize() {} |
98 | 113 |
99 ~NaClBitstreamWriter() { | 114 ~NaClBitstreamWriter() { |
100 assert(CurBit == 0 && "Unflused data remaining"); | 115 assert(CurBit == 0 && "Unflused data remaining"); |
101 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance"); | 116 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance"); |
102 | 117 |
103 // Free the BlockInfoRecords. | 118 // Free the BlockInfoRecords. |
104 while (!BlockInfoRecords.empty()) { | 119 while (!BlockInfoRecords.empty()) { |
105 BlockInfo &Info = BlockInfoRecords.back(); | 120 BlockInfo &Info = BlockInfoRecords.back(); |
106 // Free blockinfo abbrev info. | 121 // Free blockinfo abbrev info. |
107 for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size()); | 122 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() { | 164 void FlushToWord() { |
150 if (CurBit) { | 165 if (CurBit) { |
151 WriteWord(CurValue); | 166 WriteWord(CurValue); |
152 CurBit = 0; | 167 CurBit = 0; |
153 CurValue = 0; | 168 CurValue = 0; |
154 } | 169 } |
155 } | 170 } |
156 | 171 |
157 void EmitVBR(uint32_t Val, unsigned NumBits) { | 172 void EmitVBR(uint32_t Val, unsigned NumBits) { |
158 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!"); | |
159 uint32_t Threshold = 1U << (NumBits-1); | 175 uint32_t Threshold = 1U << (NumBits-1); |
160 | 176 |
161 // Emit the bits with VBR encoding, NumBits-1 bits at a time. | 177 // Emit the bits with VBR encoding, NumBits-1 bits at a time. |
162 while (Val >= Threshold) { | 178 while (Val >= Threshold) { |
163 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits); | 179 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits); |
164 Val >>= NumBits-1; | 180 Val >>= NumBits-1; |
165 } | 181 } |
166 | 182 |
167 Emit(Val, NumBits); | 183 Emit(Val, NumBits); |
168 } | 184 } |
169 | 185 |
170 void EmitVBR64(uint64_t Val, unsigned NumBits) { | 186 void EmitVBR64(uint64_t Val, unsigned NumBits) { |
171 assert(NumBits <= 32 && "Too many bits to emit!"); | 187 assert(NumBits <= 32 && "Too many bits to emit!"); |
188 assert(NumBits > 1 && "Too few bits to emit!"); | |
172 if ((uint32_t)Val == Val) | 189 if ((uint32_t)Val == Val) |
173 return EmitVBR((uint32_t)Val, NumBits); | 190 return EmitVBR((uint32_t)Val, NumBits); |
174 | 191 |
175 uint32_t Threshold = 1U << (NumBits-1); | 192 uint32_t Threshold = 1U << (NumBits-1); |
176 | 193 |
177 // Emit the bits with VBR encoding, NumBits-1 bits at a time. | 194 // Emit the bits with VBR encoding, NumBits-1 bits at a time. |
178 while (Val >= Threshold) { | 195 while (Val >= Threshold) { |
179 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) | | 196 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) | |
180 (1 << (NumBits-1)), NumBits); | 197 (1 << (NumBits-1)), NumBits); |
181 Val >>= NumBits-1; | 198 Val >>= NumBits-1; |
182 } | 199 } |
183 | 200 |
184 Emit((uint32_t)Val, NumBits); | 201 Emit((uint32_t)Val, NumBits); |
185 } | 202 } |
186 | 203 |
204 void EmitIntVBR(int32_t Val, unsigned NumBits) { | |
jvoung (off chromium)
2013/05/14 23:47:14
There is static void emitSignedInt64(SmallVectorIm
Karl
2013/05/17 20:52:18
Removed. No longer used. Uses NaClEncodeSignRotate
| |
205 // Same as unsigned version, except that the sign is captured | |
206 // by prepending a sign bit. | |
207 assert(NumBits < 32 && "Too many bits to emit!"); | |
208 assert(NumBits > 1 && "Too few bits to emit!"); | |
209 | |
210 // Capture sign. | |
211 bool IsNegative = false; | |
212 uint32_t Value = static_cast<uint32_t>(Val); | |
213 if (Val < 0) { | |
214 IsNegative = true; | |
215 Value = static_cast<uint32_t>(-Val); | |
216 } | |
217 | |
218 uint32_t Threshold = 1U << (NumBits-1); | |
219 bool IsFirstIteration = true; | |
220 | |
221 // Emit the bits with VBR encoding, NumBits-1 bits at a time. | |
222 while (Value >= Threshold) { | |
223 uint32_t Chunk = (Value & (Threshold-1)) | Threshold; | |
224 EmitIntVBRChunk(Chunk, IsNegative, NumBits, IsFirstIteration); | |
225 if (IsFirstIteration) IsFirstIteration = false; | |
226 Value >>= NumBits-1; | |
227 } | |
228 EmitIntVBRChunk(Value, IsNegative, NumBits, IsFirstIteration); | |
229 } | |
230 | |
187 /// EmitCode - Emit the specified code. | 231 /// EmitCode - Emit the specified code. |
188 void EmitCode(unsigned Val) { | 232 void EmitCode(unsigned Val) { |
189 Emit(Val, CurCodeSize); | 233 if (CurCodeSize.UseFixed()) |
Karl
2013/05/17 20:52:18
Modified code to use fields since methods UseFixed
| |
234 Emit(Val, CurCodeSize.UseNumBits()); | |
235 else | |
236 EmitVBR(Val, CurCodeSize.UseNumBits()); | |
190 } | 237 } |
191 | 238 |
192 //===--------------------------------------------------------------------===// | 239 //===--------------------------------------------------------------------===// |
193 // Block Manipulation | 240 // Block Manipulation |
194 //===--------------------------------------------------------------------===// | 241 //===--------------------------------------------------------------------===// |
195 | 242 |
196 /// getBlockInfo - If there is block info for the specified ID, return it, | 243 /// getBlockInfo - If there is block info for the specified ID, return it, |
197 /// otherwise return null. | 244 /// otherwise return null. |
198 BlockInfo *getBlockInfo(unsigned BlockID) { | 245 BlockInfo *getBlockInfo(unsigned BlockID) { |
199 // Common case, the most recent entry matches BlockID. | 246 // Common case, the most recent entry matches BlockID. |
200 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) | 247 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) |
201 return &BlockInfoRecords.back(); | 248 return &BlockInfoRecords.back(); |
202 | 249 |
203 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); | 250 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); |
204 i != e; ++i) | 251 i != e; ++i) |
205 if (BlockInfoRecords[i].BlockID == BlockID) | 252 if (BlockInfoRecords[i].BlockID == BlockID) |
206 return &BlockInfoRecords[i]; | 253 return &BlockInfoRecords[i]; |
207 return 0; | 254 return 0; |
208 } | 255 } |
209 | 256 |
210 void EnterSubblock(unsigned BlockID, unsigned CodeLen) { | 257 // Enter block using CodeLen bits to read the size of the code selector |
258 // associated with the block. | |
259 void EnterSubblock(unsigned BlockID, | |
260 const NaClBitcodeSelectorAbbrev& CodeLen) { | |
211 // Block header: | 261 // Block header: |
212 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | 262 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] |
213 EmitCode(naclbitc::ENTER_SUBBLOCK); | 263 EmitCode(naclbitc::ENTER_SUBBLOCK); |
214 EmitVBR(BlockID, naclbitc::BlockIDWidth); | 264 EmitVBR(BlockID, naclbitc::BlockIDWidth); |
215 EmitVBR(CodeLen, naclbitc::CodeLenWidth); | 265 EmitIntVBR((CodeLen.UseFixed() |
jvoung (off chromium)
2013/05/14 23:47:14
How come this can be a negative value: -CodeLen.Us
Karl
2013/05/17 20:52:18
I modified this to be more explicit using:
Emit
| |
266 ? CodeLen.UseNumBits() : -CodeLen.UseNumBits()), | |
267 naclbitc::CodeLenWidth); | |
216 FlushToWord(); | 268 FlushToWord(); |
217 | 269 |
218 unsigned BlockSizeWordIndex = GetWordIndex(); | 270 unsigned BlockSizeWordIndex = GetWordIndex(); |
219 unsigned OldCodeSize = CurCodeSize; | 271 NaClBitcodeSelectorAbbrev OldCodeSize(CurCodeSize); |
220 | 272 |
221 // Emit a placeholder, which will be replaced when the block is popped. | 273 // Emit a placeholder, which will be replaced when the block is popped. |
222 Emit(0, naclbitc::BlockSizeWidth); | 274 Emit(0, naclbitc::BlockSizeWidth); |
223 | 275 |
224 CurCodeSize = CodeLen; | 276 CurCodeSize = CodeLen; |
225 | 277 |
226 // Push the outer block's abbrev set onto the stack, start out with an | 278 // Push the outer block's abbrev set onto the stack, start out with an |
227 // empty abbrev set. | 279 // empty abbrev set. |
228 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); | 280 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); |
229 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); | 281 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); |
230 | 282 |
231 // If there is a blockinfo for this BlockID, add all the predefined abbrevs | 283 // If there is a blockinfo for this BlockID, add all the predefined abbrevs |
232 // to the abbrev list. | 284 // to the abbrev list. |
233 if (BlockInfo *Info = getBlockInfo(BlockID)) { | 285 if (BlockInfo *Info = getBlockInfo(BlockID)) { |
234 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size()); | 286 for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size()); |
235 i != e; ++i) { | 287 i != e; ++i) { |
236 CurAbbrevs.push_back(Info->Abbrevs[i]); | 288 CurAbbrevs.push_back(Info->Abbrevs[i]); |
237 Info->Abbrevs[i]->addRef(); | 289 Info->Abbrevs[i]->addRef(); |
238 } | 290 } |
239 } | 291 } |
240 } | 292 } |
241 | 293 |
294 // Enter block with no abbreviations. | |
295 void EnterSubblock(unsigned BlockID) { | |
296 NaClBitcodeSelectorAbbrev DefaultCodeLen; | |
297 EnterSubblock(BlockID, DefaultCodeLen); | |
298 } | |
299 | |
300 // Enter block with the given number of abbreviations. | |
301 void EnterSubblock(unsigned BlockID, unsigned NumAbbrev) { | |
302 NaClBitcodeSelectorAbbrev CodeLenAbbrev(NumAbbrev); | |
303 EnterSubblock(BlockID, CodeLenAbbrev); | |
304 } | |
305 | |
306 // Enter block with the given number of abbreviations NumAbbrev, | |
307 // with the constraint that all abbreviations < FixedAbbrevCutoff will | |
308 // be read using a single read. | |
309 void EnterSubblock(unsigned BlockId, | |
310 unsigned NumAbbrev, | |
311 unsigned FixedAbbrevCutoff) { | |
312 NaClBitcodeSelectorAbbrev CodeLenAbbrev(FixedAbbrevCutoff, NumAbbrev); | |
313 EnterSubblock(BlockId, CodeLenAbbrev); | |
314 } | |
315 | |
242 void ExitBlock() { | 316 void ExitBlock() { |
243 assert(!BlockScope.empty() && "Block scope imbalance!"); | 317 assert(!BlockScope.empty() && "Block scope imbalance!"); |
244 | 318 |
245 // Delete all abbrevs. | 319 // Delete all abbrevs. |
246 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); | 320 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); |
247 i != e; ++i) | 321 i != e; ++i) |
248 CurAbbrevs[i]->dropRef(); | 322 CurAbbrevs[i]->dropRef(); |
249 | 323 |
250 const Block &B = BlockScope.back(); | 324 const Block &B = BlockScope.back(); |
251 | 325 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 CurAbbrevs.push_back(Abbv); | 568 CurAbbrevs.push_back(Abbv); |
495 return static_cast<unsigned>(CurAbbrevs.size())-1 + | 569 return static_cast<unsigned>(CurAbbrevs.size())-1 + |
496 naclbitc::FIRST_APPLICATION_ABBREV; | 570 naclbitc::FIRST_APPLICATION_ABBREV; |
497 } | 571 } |
498 | 572 |
499 //===--------------------------------------------------------------------===// | 573 //===--------------------------------------------------------------------===// |
500 // BlockInfo Block Emission | 574 // BlockInfo Block Emission |
501 //===--------------------------------------------------------------------===// | 575 //===--------------------------------------------------------------------===// |
502 | 576 |
503 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. | 577 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. |
504 void EnterBlockInfoBlock(unsigned CodeWidth) { | 578 void EnterBlockInfoBlock() { |
505 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID, CodeWidth); | 579 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID); |
506 BlockInfoCurBID = ~0U; | 580 BlockInfoCurBID = ~0U; |
507 } | 581 } |
508 private: | 582 private: |
509 /// SwitchToBlockID - If we aren't already talking about the specified block | 583 /// SwitchToBlockID - If we aren't already talking about the specified block |
510 /// ID, emit a BLOCKINFO_CODE_SETBID record. | 584 /// ID, emit a BLOCKINFO_CODE_SETBID record. |
511 void SwitchToBlockID(unsigned BlockID) { | 585 void SwitchToBlockID(unsigned BlockID) { |
512 if (BlockInfoCurBID == BlockID) return; | 586 if (BlockInfoCurBID == BlockID) return; |
513 SmallVector<unsigned, 2> V; | 587 SmallVector<unsigned, 2> V; |
514 V.push_back(BlockID); | 588 V.push_back(BlockID); |
515 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); | 589 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); |
(...skipping 23 matching lines...) Expand all Loading... | |
539 Info.Abbrevs.push_back(Abbv); | 613 Info.Abbrevs.push_back(Abbv); |
540 | 614 |
541 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; | 615 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; |
542 } | 616 } |
543 }; | 617 }; |
544 | 618 |
545 | 619 |
546 } // End llvm namespace | 620 } // End llvm namespace |
547 | 621 |
548 #endif | 622 #endif |
OLD | NEW |