@@ -33,7 +33,7 @@ class NaClBitstreamWriter {
33
33
34
34
// / CurCodeSize - This is the declared size of code values used for the
35
35
// / current block, in bits.
36
- unsigned CurCodeSize;
36
+ NaClBitcodeSelectorAbbrev CurCodeSize;
37
37
38
38
// / BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently
39
39
// / selected BLOCK ID.
@@ -43,10 +43,11 @@ class NaClBitstreamWriter {
43
43
std::vector<NaClBitCodeAbbrev*> CurAbbrevs;
44
44
45
45
struct Block {
46
- unsigned PrevCodeSize;
46
+ NaClBitcodeSelectorAbbrev PrevCodeSize;
47
47
unsigned StartSizeWord;
48
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
53
// / BlockScope - This tracks the current blocks that we have entered.
@@ -94,7 +95,7 @@ class NaClBitstreamWriter {
94
95
95
96
public:
96
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
100
~NaClBitstreamWriter () {
100
101
assert (CurBit == 0 && " Unflused data remaining" );
@@ -156,6 +157,7 @@ class NaClBitstreamWriter {
156
157
157
158
void EmitVBR (uint32_t Val, unsigned NumBits) {
158
159
assert (NumBits <= 32 && " Too many bits to emit!" );
160
+ assert (NumBits > 1 && " Too few bits to emit!" );
159
161
uint32_t Threshold = 1U << (NumBits-1 );
160
162
161
163
// Emit the bits with VBR encoding, NumBits-1 bits at a time.
@@ -169,6 +171,7 @@ class NaClBitstreamWriter {
169
171
170
172
void EmitVBR64 (uint64_t Val, unsigned NumBits) {
171
173
assert (NumBits <= 32 && " Too many bits to emit!" );
174
+ assert (NumBits > 1 && " Too few bits to emit!" );
172
175
if ((uint32_t )Val == Val)
173
176
return EmitVBR ((uint32_t )Val, NumBits);
174
177
@@ -186,7 +189,10 @@ class NaClBitstreamWriter {
186
189
187
190
// / EmitCode - Emit the specified code.
188
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
// ===--------------------------------------------------------------------===//
@@ -207,16 +213,22 @@ class NaClBitstreamWriter {
207
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
222
// Block header:
212
223
// [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
213
224
EmitCode (naclbitc::ENTER_SUBBLOCK);
214
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
228
FlushToWord ();
217
229
218
230
unsigned BlockSizeWordIndex = GetWordIndex ();
219
- unsigned OldCodeSize = CurCodeSize;
231
+ NaClBitcodeSelectorAbbrev OldCodeSize ( CurCodeSize) ;
220
232
221
233
// Emit a placeholder, which will be replaced when the block is popped.
222
234
Emit (0 , naclbitc::BlockSizeWidth);
@@ -230,7 +242,7 @@ class NaClBitstreamWriter {
230
242
231
243
// If there is a blockinfo for this BlockID, add all the predefined abbrevs
232
244
// to the abbrev list.
233
- if (BlockInfo * Info = getBlockInfo (BlockID) ) {
245
+ if (Info) {
234
246
for (unsigned i = 0 , e = static_cast <unsigned >(Info->Abbrevs .size ());
235
247
i != e; ++i) {
236
248
CurAbbrevs.push_back (Info->Abbrevs [i]);
@@ -239,6 +251,31 @@ class NaClBitstreamWriter {
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
279
void ExitBlock () {
243
280
assert (!BlockScope.empty () && " Block scope imbalance!" );
244
281
@@ -501,8 +538,8 @@ class NaClBitstreamWriter {
501
538
// ===--------------------------------------------------------------------===//
502
539
503
540
// / EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
504
- void EnterBlockInfoBlock (unsigned CodeWidth ) {
505
- EnterSubblock (naclbitc::BLOCKINFO_BLOCK_ID, CodeWidth );
541
+ void EnterBlockInfoBlock () {
542
+ EnterSubblock (naclbitc::BLOCKINFO_BLOCK_ID);
506
543
BlockInfoCurBID = ~0U ;
507
544
}
508
545
private:
0 commit comments