~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/include/llvm/Bitcode/BitstreamWriter.h

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- BitstreamWriter.h - Low-level bitstream writer interface -*- C++ -*-===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
//
 
8
//===----------------------------------------------------------------------===//
 
9
//
 
10
// This header defines the BitstreamWriter class.  This class can be used to
 
11
// write an arbitrary bitstream, regardless of its contents.
 
12
//
 
13
//===----------------------------------------------------------------------===//
 
14
 
 
15
#ifndef BITSTREAM_WRITER_H
 
16
#define BITSTREAM_WRITER_H
 
17
 
 
18
#include "llvm/ADT/StringRef.h"
 
19
#include "llvm/Bitcode/BitCodes.h"
 
20
#include <vector>
 
21
 
 
22
namespace llvm {
 
23
 
 
24
class BitstreamWriter {
 
25
  std::vector<unsigned char> &Out;
 
26
 
 
27
  /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
 
28
  unsigned CurBit;
 
29
 
 
30
  /// CurValue - The current value.  Only bits < CurBit are valid.
 
31
  uint32_t CurValue;
 
32
 
 
33
  /// CurCodeSize - This is the declared size of code values used for the
 
34
  /// current block, in bits.
 
35
  unsigned CurCodeSize;
 
36
 
 
37
  /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently
 
38
  /// selected BLOCK ID.
 
39
  unsigned BlockInfoCurBID;
 
40
 
 
41
  /// CurAbbrevs - Abbrevs installed at in this block.
 
42
  std::vector<BitCodeAbbrev*> CurAbbrevs;
 
43
 
 
44
  struct Block {
 
45
    unsigned PrevCodeSize;
 
46
    unsigned StartSizeWord;
 
47
    std::vector<BitCodeAbbrev*> PrevAbbrevs;
 
48
    Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
 
49
  };
 
50
 
 
51
  /// BlockScope - This tracks the current blocks that we have entered.
 
52
  std::vector<Block> BlockScope;
 
53
 
 
54
  /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
 
55
  /// These describe abbreviations that all blocks of the specified ID inherit.
 
56
  struct BlockInfo {
 
57
    unsigned BlockID;
 
58
    std::vector<BitCodeAbbrev*> Abbrevs;
 
59
  };
 
60
  std::vector<BlockInfo> BlockInfoRecords;
 
61
 
 
62
public:
 
63
  explicit BitstreamWriter(std::vector<unsigned char> &O)
 
64
    : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
 
65
 
 
66
  ~BitstreamWriter() {
 
67
    assert(CurBit == 0 && "Unflused data remaining");
 
68
    assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
 
69
 
 
70
    // Free the BlockInfoRecords.
 
71
    while (!BlockInfoRecords.empty()) {
 
72
      BlockInfo &Info = BlockInfoRecords.back();
 
73
      // Free blockinfo abbrev info.
 
74
      for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
 
75
           i != e; ++i)
 
76
        Info.Abbrevs[i]->dropRef();
 
77
      BlockInfoRecords.pop_back();
 
78
    }
 
79
  }
 
80
 
 
81
  std::vector<unsigned char> &getBuffer() { return Out; }
 
82
 
 
83
  /// \brief Retrieve the current position in the stream, in bits.
 
84
  uint64_t GetCurrentBitNo() const { return Out.size() * 8 + CurBit; }
 
85
 
 
86
  //===--------------------------------------------------------------------===//
 
87
  // Basic Primitives for emitting bits to the stream.
 
88
  //===--------------------------------------------------------------------===//
 
89
 
 
90
  void Emit(uint32_t Val, unsigned NumBits) {
 
91
    assert(NumBits <= 32 && "Invalid value size!");
 
92
    assert((Val & ~(~0U >> (32-NumBits))) == 0 && "High bits set!");
 
93
    CurValue |= Val << CurBit;
 
94
    if (CurBit + NumBits < 32) {
 
95
      CurBit += NumBits;
 
96
      return;
 
97
    }
 
98
 
 
99
    // Add the current word.
 
100
    unsigned V = CurValue;
 
101
    Out.push_back((unsigned char)(V >>  0));
 
102
    Out.push_back((unsigned char)(V >>  8));
 
103
    Out.push_back((unsigned char)(V >> 16));
 
104
    Out.push_back((unsigned char)(V >> 24));
 
105
 
 
106
    if (CurBit)
 
107
      CurValue = Val >> (32-CurBit);
 
108
    else
 
109
      CurValue = 0;
 
110
    CurBit = (CurBit+NumBits) & 31;
 
111
  }
 
112
 
 
113
  void Emit64(uint64_t Val, unsigned NumBits) {
 
114
    if (NumBits <= 32)
 
115
      Emit((uint32_t)Val, NumBits);
 
116
    else {
 
117
      Emit((uint32_t)Val, 32);
 
118
      Emit((uint32_t)(Val >> 32), NumBits-32);
 
119
    }
 
120
  }
 
121
 
 
122
  void FlushToWord() {
 
123
    if (CurBit) {
 
124
      unsigned V = CurValue;
 
125
      Out.push_back((unsigned char)(V >>  0));
 
126
      Out.push_back((unsigned char)(V >>  8));
 
127
      Out.push_back((unsigned char)(V >> 16));
 
128
      Out.push_back((unsigned char)(V >> 24));
 
129
      CurBit = 0;
 
130
      CurValue = 0;
 
131
    }
 
132
  }
 
133
 
 
134
  void EmitVBR(uint32_t Val, unsigned NumBits) {
 
135
    uint32_t Threshold = 1U << (NumBits-1);
 
136
 
 
137
    // Emit the bits with VBR encoding, NumBits-1 bits at a time.
 
138
    while (Val >= Threshold) {
 
139
      Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
 
140
      Val >>= NumBits-1;
 
141
    }
 
142
 
 
143
    Emit(Val, NumBits);
 
144
  }
 
145
 
 
146
  void EmitVBR64(uint64_t Val, unsigned NumBits) {
 
147
    if ((uint32_t)Val == Val)
 
148
      return EmitVBR((uint32_t)Val, NumBits);
 
149
 
 
150
    uint64_t Threshold = 1U << (NumBits-1);
 
151
 
 
152
    // Emit the bits with VBR encoding, NumBits-1 bits at a time.
 
153
    while (Val >= Threshold) {
 
154
      Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
 
155
           (1 << (NumBits-1)), NumBits);
 
156
      Val >>= NumBits-1;
 
157
    }
 
158
 
 
159
    Emit((uint32_t)Val, NumBits);
 
160
  }
 
161
 
 
162
  /// EmitCode - Emit the specified code.
 
163
  void EmitCode(unsigned Val) {
 
164
    Emit(Val, CurCodeSize);
 
165
  }
 
166
 
 
167
  // BackpatchWord - Backpatch a 32-bit word in the output with the specified
 
168
  // value.
 
169
  void BackpatchWord(unsigned ByteNo, unsigned NewWord) {
 
170
    Out[ByteNo++] = (unsigned char)(NewWord >>  0);
 
171
    Out[ByteNo++] = (unsigned char)(NewWord >>  8);
 
172
    Out[ByteNo++] = (unsigned char)(NewWord >> 16);
 
173
    Out[ByteNo  ] = (unsigned char)(NewWord >> 24);
 
174
  }
 
175
 
 
176
  //===--------------------------------------------------------------------===//
 
177
  // Block Manipulation
 
178
  //===--------------------------------------------------------------------===//
 
179
 
 
180
  /// getBlockInfo - If there is block info for the specified ID, return it,
 
181
  /// otherwise return null.
 
182
  BlockInfo *getBlockInfo(unsigned BlockID) {
 
183
    // Common case, the most recent entry matches BlockID.
 
184
    if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
 
185
      return &BlockInfoRecords.back();
 
186
 
 
187
    for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
 
188
         i != e; ++i)
 
189
      if (BlockInfoRecords[i].BlockID == BlockID)
 
190
        return &BlockInfoRecords[i];
 
191
    return 0;
 
192
  }
 
193
 
 
194
  void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
 
195
    // Block header:
 
196
    //    [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
 
197
    EmitCode(bitc::ENTER_SUBBLOCK);
 
198
    EmitVBR(BlockID, bitc::BlockIDWidth);
 
199
    EmitVBR(CodeLen, bitc::CodeLenWidth);
 
200
    FlushToWord();
 
201
 
 
202
    unsigned BlockSizeWordLoc = static_cast<unsigned>(Out.size());
 
203
    unsigned OldCodeSize = CurCodeSize;
 
204
 
 
205
    // Emit a placeholder, which will be replaced when the block is popped.
 
206
    Emit(0, bitc::BlockSizeWidth);
 
207
 
 
208
    CurCodeSize = CodeLen;
 
209
 
 
210
    // Push the outer block's abbrev set onto the stack, start out with an
 
211
    // empty abbrev set.
 
212
    BlockScope.push_back(Block(OldCodeSize, BlockSizeWordLoc/4));
 
213
    BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
 
214
 
 
215
    // If there is a blockinfo for this BlockID, add all the predefined abbrevs
 
216
    // to the abbrev list.
 
217
    if (BlockInfo *Info = getBlockInfo(BlockID)) {
 
218
      for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
 
219
           i != e; ++i) {
 
220
        CurAbbrevs.push_back(Info->Abbrevs[i]);
 
221
        Info->Abbrevs[i]->addRef();
 
222
      }
 
223
    }
 
224
  }
 
225
 
 
226
  void ExitBlock() {
 
227
    assert(!BlockScope.empty() && "Block scope imbalance!");
 
228
 
 
229
    // Delete all abbrevs.
 
230
    for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
 
231
         i != e; ++i)
 
232
      CurAbbrevs[i]->dropRef();
 
233
 
 
234
    const Block &B = BlockScope.back();
 
235
 
 
236
    // Block tail:
 
237
    //    [END_BLOCK, <align4bytes>]
 
238
    EmitCode(bitc::END_BLOCK);
 
239
    FlushToWord();
 
240
 
 
241
    // Compute the size of the block, in words, not counting the size field.
 
242
    unsigned SizeInWords= static_cast<unsigned>(Out.size())/4-B.StartSizeWord-1;
 
243
    unsigned ByteNo = B.StartSizeWord*4;
 
244
 
 
245
    // Update the block size field in the header of this sub-block.
 
246
    BackpatchWord(ByteNo, SizeInWords);
 
247
 
 
248
    // Restore the inner block's code size and abbrev table.
 
249
    CurCodeSize = B.PrevCodeSize;
 
250
    BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
 
251
    BlockScope.pop_back();
 
252
  }
 
253
 
 
254
  //===--------------------------------------------------------------------===//
 
255
  // Record Emission
 
256
  //===--------------------------------------------------------------------===//
 
257
 
 
258
private:
 
259
  /// EmitAbbreviatedLiteral - Emit a literal value according to its abbrev
 
260
  /// record.  This is a no-op, since the abbrev specifies the literal to use. 
 
261
  template<typename uintty>
 
262
  void EmitAbbreviatedLiteral(const BitCodeAbbrevOp &Op, uintty V) {
 
263
    assert(Op.isLiteral() && "Not a literal");
 
264
    // If the abbrev specifies the literal value to use, don't emit
 
265
    // anything.
 
266
    assert(V == Op.getLiteralValue() &&
 
267
           "Invalid abbrev for record!");
 
268
  }
 
269
  
 
270
  /// EmitAbbreviatedField - Emit a single scalar field value with the specified
 
271
  /// encoding.
 
272
  template<typename uintty>
 
273
  void EmitAbbreviatedField(const BitCodeAbbrevOp &Op, uintty V) {
 
274
    assert(!Op.isLiteral() && "Literals should use EmitAbbreviatedLiteral!");
 
275
    
 
276
    // Encode the value as we are commanded.
 
277
    switch (Op.getEncoding()) {
 
278
    default: assert(0 && "Unknown encoding!");
 
279
    case BitCodeAbbrevOp::Fixed:
 
280
      Emit((unsigned)V, (unsigned)Op.getEncodingData());
 
281
      break;
 
282
    case BitCodeAbbrevOp::VBR:
 
283
      EmitVBR64(V, (unsigned)Op.getEncodingData());
 
284
      break;
 
285
    case BitCodeAbbrevOp::Char6:
 
286
      Emit(BitCodeAbbrevOp::EncodeChar6((char)V), 6);
 
287
      break;
 
288
    }
 
289
  }
 
290
  
 
291
  /// EmitRecordWithAbbrevImpl - This is the core implementation of the record
 
292
  /// emission code.  If BlobData is non-null, then it specifies an array of
 
293
  /// data that should be emitted as part of the Blob or Array operand that is
 
294
  /// known to exist at the end of the record.
 
295
  template<typename uintty>
 
296
  void EmitRecordWithAbbrevImpl(unsigned Abbrev, SmallVectorImpl<uintty> &Vals,
 
297
                                StringRef Blob) {
 
298
    const char *BlobData = Blob.data();
 
299
    unsigned BlobLen = (unsigned) Blob.size();
 
300
    unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV;
 
301
    assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
 
302
    BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
 
303
 
 
304
    EmitCode(Abbrev);
 
305
 
 
306
    unsigned RecordIdx = 0;
 
307
    for (unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos());
 
308
         i != e; ++i) {
 
309
      const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
 
310
      if (Op.isLiteral()) {
 
311
        assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
 
312
        EmitAbbreviatedLiteral(Op, Vals[RecordIdx]);
 
313
        ++RecordIdx;
 
314
      } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
 
315
        // Array case.
 
316
        assert(i+2 == e && "array op not second to last?");
 
317
        const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
 
318
 
 
319
        // If this record has blob data, emit it, otherwise we must have record
 
320
        // entries to encode this way.
 
321
        if (BlobData) {
 
322
          assert(RecordIdx == Vals.size() &&
 
323
                 "Blob data and record entries specified for array!");
 
324
          // Emit a vbr6 to indicate the number of elements present.
 
325
          EmitVBR(static_cast<uint32_t>(BlobLen), 6);
 
326
          
 
327
          // Emit each field.
 
328
          for (unsigned i = 0; i != BlobLen; ++i)
 
329
            EmitAbbreviatedField(EltEnc, (unsigned char)BlobData[i]);
 
330
          
 
331
          // Know that blob data is consumed for assertion below.
 
332
          BlobData = 0;
 
333
        } else {
 
334
          // Emit a vbr6 to indicate the number of elements present.
 
335
          EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6);
 
336
 
 
337
          // Emit each field.
 
338
          for (unsigned e = Vals.size(); RecordIdx != e; ++RecordIdx)
 
339
            EmitAbbreviatedField(EltEnc, Vals[RecordIdx]);
 
340
        }
 
341
      } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) {
 
342
        // If this record has blob data, emit it, otherwise we must have record
 
343
        // entries to encode this way.
 
344
        
 
345
        // Emit a vbr6 to indicate the number of elements present.
 
346
        if (BlobData) {
 
347
          EmitVBR(static_cast<uint32_t>(BlobLen), 6);
 
348
          assert(RecordIdx == Vals.size() &&
 
349
                 "Blob data and record entries specified for blob operand!");
 
350
        } else {
 
351
          EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6);
 
352
        }
 
353
        
 
354
        // Flush to a 32-bit alignment boundary.
 
355
        FlushToWord();
 
356
        assert((Out.size() & 3) == 0 && "Not 32-bit aligned");
 
357
 
 
358
        // Emit each field as a literal byte.
 
359
        if (BlobData) {
 
360
          for (unsigned i = 0; i != BlobLen; ++i)
 
361
            Out.push_back((unsigned char)BlobData[i]);
 
362
          
 
363
          // Know that blob data is consumed for assertion below.
 
364
          BlobData = 0;
 
365
        } else {
 
366
          for (unsigned e = Vals.size(); RecordIdx != e; ++RecordIdx) {
 
367
            assert(Vals[RecordIdx] < 256 && "Value too large to emit as blob");
 
368
            Out.push_back((unsigned char)Vals[RecordIdx]);
 
369
          }
 
370
        }
 
371
        // Align end to 32-bits.
 
372
        while (Out.size() & 3)
 
373
          Out.push_back(0);
 
374
        
 
375
      } else {  // Single scalar field.
 
376
        assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
 
377
        EmitAbbreviatedField(Op, Vals[RecordIdx]);
 
378
        ++RecordIdx;
 
379
      }
 
380
    }
 
381
    assert(RecordIdx == Vals.size() && "Not all record operands emitted!");
 
382
    assert(BlobData == 0 &&
 
383
           "Blob data specified for record that doesn't use it!");
 
384
  }
 
385
  
 
386
public:
 
387
 
 
388
  /// EmitRecord - Emit the specified record to the stream, using an abbrev if
 
389
  /// we have one to compress the output.
 
390
  template<typename uintty>
 
391
  void EmitRecord(unsigned Code, SmallVectorImpl<uintty> &Vals,
 
392
                  unsigned Abbrev = 0) {
 
393
    if (!Abbrev) {
 
394
      // If we don't have an abbrev to use, emit this in its fully unabbreviated
 
395
      // form.
 
396
      EmitCode(bitc::UNABBREV_RECORD);
 
397
      EmitVBR(Code, 6);
 
398
      EmitVBR(static_cast<uint32_t>(Vals.size()), 6);
 
399
      for (unsigned i = 0, e = static_cast<unsigned>(Vals.size()); i != e; ++i)
 
400
        EmitVBR64(Vals[i], 6);
 
401
      return;
 
402
    }
 
403
 
 
404
    // Insert the code into Vals to treat it uniformly.
 
405
    Vals.insert(Vals.begin(), Code);
 
406
    
 
407
    EmitRecordWithAbbrev(Abbrev, Vals);
 
408
  }
 
409
  
 
410
  /// EmitRecordWithAbbrev - Emit a record with the specified abbreviation.
 
411
  /// Unlike EmitRecord, the code for the record should be included in Vals as
 
412
  /// the first entry.
 
413
  template<typename uintty>
 
414
  void EmitRecordWithAbbrev(unsigned Abbrev, SmallVectorImpl<uintty> &Vals) {
 
415
    EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef());
 
416
  }
 
417
  
 
418
  /// EmitRecordWithBlob - Emit the specified record to the stream, using an
 
419
  /// abbrev that includes a blob at the end.  The blob data to emit is
 
420
  /// specified by the pointer and length specified at the end.  In contrast to
 
421
  /// EmitRecord, this routine expects that the first entry in Vals is the code
 
422
  /// of the record.
 
423
  template<typename uintty>
 
424
  void EmitRecordWithBlob(unsigned Abbrev, SmallVectorImpl<uintty> &Vals,
 
425
                          StringRef Blob) {
 
426
    EmitRecordWithAbbrevImpl(Abbrev, Vals, Blob);
 
427
  }
 
428
  template<typename uintty>
 
429
  void EmitRecordWithBlob(unsigned Abbrev, SmallVectorImpl<uintty> &Vals,
 
430
                          const char *BlobData, unsigned BlobLen) {
 
431
    return EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(BlobData, BlobLen));
 
432
  }
 
433
 
 
434
  /// EmitRecordWithArray - Just like EmitRecordWithBlob, works with records
 
435
  /// that end with an array.
 
436
  template<typename uintty>
 
437
  void EmitRecordWithArray(unsigned Abbrev, SmallVectorImpl<uintty> &Vals,
 
438
                          StringRef Array) {
 
439
    EmitRecordWithAbbrevImpl(Abbrev, Vals, Array);
 
440
  }
 
441
  template<typename uintty>
 
442
  void EmitRecordWithArray(unsigned Abbrev, SmallVectorImpl<uintty> &Vals,
 
443
                          const char *ArrayData, unsigned ArrayLen) {
 
444
    return EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(ArrayData, 
 
445
                                                            ArrayLen));
 
446
  }
 
447
  
 
448
  //===--------------------------------------------------------------------===//
 
449
  // Abbrev Emission
 
450
  //===--------------------------------------------------------------------===//
 
451
 
 
452
private:
 
453
  // Emit the abbreviation as a DEFINE_ABBREV record.
 
454
  void EncodeAbbrev(BitCodeAbbrev *Abbv) {
 
455
    EmitCode(bitc::DEFINE_ABBREV);
 
456
    EmitVBR(Abbv->getNumOperandInfos(), 5);
 
457
    for (unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos());
 
458
         i != e; ++i) {
 
459
      const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
 
460
      Emit(Op.isLiteral(), 1);
 
461
      if (Op.isLiteral()) {
 
462
        EmitVBR64(Op.getLiteralValue(), 8);
 
463
      } else {
 
464
        Emit(Op.getEncoding(), 3);
 
465
        if (Op.hasEncodingData())
 
466
          EmitVBR64(Op.getEncodingData(), 5);
 
467
      }
 
468
    }
 
469
  }
 
470
public:
 
471
 
 
472
  /// EmitAbbrev - This emits an abbreviation to the stream.  Note that this
 
473
  /// method takes ownership of the specified abbrev.
 
474
  unsigned EmitAbbrev(BitCodeAbbrev *Abbv) {
 
475
    // Emit the abbreviation as a record.
 
476
    EncodeAbbrev(Abbv);
 
477
    CurAbbrevs.push_back(Abbv);
 
478
    return static_cast<unsigned>(CurAbbrevs.size())-1 +
 
479
      bitc::FIRST_APPLICATION_ABBREV;
 
480
  }
 
481
 
 
482
  //===--------------------------------------------------------------------===//
 
483
  // BlockInfo Block Emission
 
484
  //===--------------------------------------------------------------------===//
 
485
 
 
486
  /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
 
487
  void EnterBlockInfoBlock(unsigned CodeWidth) {
 
488
    EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, CodeWidth);
 
489
    BlockInfoCurBID = -1U;
 
490
  }
 
491
private:
 
492
  /// SwitchToBlockID - If we aren't already talking about the specified block
 
493
  /// ID, emit a BLOCKINFO_CODE_SETBID record.
 
494
  void SwitchToBlockID(unsigned BlockID) {
 
495
    if (BlockInfoCurBID == BlockID) return;
 
496
    SmallVector<unsigned, 2> V;
 
497
    V.push_back(BlockID);
 
498
    EmitRecord(bitc::BLOCKINFO_CODE_SETBID, V);
 
499
    BlockInfoCurBID = BlockID;
 
500
  }
 
501
 
 
502
  BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
 
503
    if (BlockInfo *BI = getBlockInfo(BlockID))
 
504
      return *BI;
 
505
 
 
506
    // Otherwise, add a new record.
 
507
    BlockInfoRecords.push_back(BlockInfo());
 
508
    BlockInfoRecords.back().BlockID = BlockID;
 
509
    return BlockInfoRecords.back();
 
510
  }
 
511
 
 
512
public:
 
513
 
 
514
  /// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified
 
515
  /// BlockID.
 
516
  unsigned EmitBlockInfoAbbrev(unsigned BlockID, BitCodeAbbrev *Abbv) {
 
517
    SwitchToBlockID(BlockID);
 
518
    EncodeAbbrev(Abbv);
 
519
 
 
520
    // Add the abbrev to the specified block record.
 
521
    BlockInfo &Info = getOrCreateBlockInfo(BlockID);
 
522
    Info.Abbrevs.push_back(Abbv);
 
523
 
 
524
    return Info.Abbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV;
 
525
  }
 
526
};
 
527
 
 
528
 
 
529
} // End llvm namespace
 
530
 
 
531
#endif