1 //===- BitstreamWriter.h - Low-level bitstream writer interface -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitstreamWriter class. This class can be used to
11 // write an arbitrary bitstream, regardless of its contents.
13 //===----------------------------------------------------------------------===//
15 #ifndef BITSTREAM_WRITER_H
16 #define BITSTREAM_WRITER_H
18 #include "llvm/Bitcode/BitCodes.h"
23 class BitstreamWriter {
24 std::vector<unsigned char> &Out;
26 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
29 /// CurValue - The current value. Only bits < CurBit are valid.
32 // CurCodeSize - This is the declared size of code values used for the current
37 unsigned PrevCodeSize;
38 unsigned StartSizeWord;
39 std::vector<BitCodeAbbrev*> PrevAbbrevs;
40 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
43 /// BlockScope - This tracks the current blocks that we have entered.
44 std::vector<Block> BlockScope;
46 std::vector<BitCodeAbbrev*> CurAbbrevs;
48 BitstreamWriter(std::vector<unsigned char> &O)
49 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
52 assert(CurBit == 0 && "Unflused data remaining");
53 assert(BlockScope.empty() && "Block imbalance");
55 //===--------------------------------------------------------------------===//
56 // Basic Primitives for emitting bits to the stream.
57 //===--------------------------------------------------------------------===//
59 void Emit(uint32_t Val, unsigned NumBits) {
60 assert(NumBits <= 32 && "Invalid value size!");
61 assert((Val & ~(~0U >> (32-NumBits))) == 0 && "High bits set!");
62 CurValue |= Val << CurBit;
63 if (CurBit + NumBits < 32) {
68 // Add the current word.
69 unsigned V = CurValue;
70 Out.push_back((unsigned char)(V >> 0));
71 Out.push_back((unsigned char)(V >> 8));
72 Out.push_back((unsigned char)(V >> 16));
73 Out.push_back((unsigned char)(V >> 24));
76 CurValue = Val >> (32-CurBit);
79 CurBit = (CurBit+NumBits) & 31;
82 void Emit64(uint64_t Val, unsigned NumBits) {
84 Emit((uint32_t)Val, NumBits);
86 Emit((uint32_t)Val, 32);
87 Emit((uint32_t)(Val >> 32), NumBits-32);
93 unsigned V = CurValue;
94 Out.push_back((unsigned char)(V >> 0));
95 Out.push_back((unsigned char)(V >> 8));
96 Out.push_back((unsigned char)(V >> 16));
97 Out.push_back((unsigned char)(V >> 24));
103 void EmitVBR(uint32_t Val, unsigned NumBits) {
104 uint32_t Threshold = 1U << (NumBits-1);
106 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
107 while (Val >= Threshold) {
108 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
115 void EmitVBR64(uint64_t Val, unsigned NumBits) {
116 if ((uint32_t)Val == Val)
117 return EmitVBR((uint32_t)Val, NumBits);
119 uint64_t Threshold = 1U << (NumBits-1);
121 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
122 while (Val >= Threshold) {
123 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
124 (1 << (NumBits-1)), NumBits);
128 Emit((uint32_t)Val, NumBits);
131 /// EmitCode - Emit the specified code.
132 void EmitCode(unsigned Val) {
133 Emit(Val, CurCodeSize);
136 //===--------------------------------------------------------------------===//
137 // Block Manipulation
138 //===--------------------------------------------------------------------===//
140 void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
142 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
143 EmitCode(bitc::ENTER_SUBBLOCK);
144 EmitVBR(BlockID, bitc::BlockIDWidth);
145 EmitVBR(CodeLen, bitc::CodeLenWidth);
147 BlockScope.push_back(Block(CurCodeSize, Out.size()/4));
149 // Delete all abbrevs.
150 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
151 delete CurAbbrevs[i];
153 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
154 // Emit a placeholder, which will be replaced when the block is popped.
155 Emit(0, bitc::BlockSizeWidth);
157 CurCodeSize = CodeLen;
161 assert(!BlockScope.empty() && "Block scope imbalance!");
162 const Block &B = BlockScope.back();
165 // [END_BLOCK, <align4bytes>]
166 EmitCode(bitc::END_BLOCK);
169 // Compute the size of the block, in words, not counting the size field.
170 unsigned SizeInWords = Out.size()/4-B.StartSizeWord - 1;
171 unsigned ByteNo = B.StartSizeWord*4;
173 // Update the block size field in the header of this sub-block.
174 Out[ByteNo++] = (unsigned char)(SizeInWords >> 0);
175 Out[ByteNo++] = (unsigned char)(SizeInWords >> 8);
176 Out[ByteNo++] = (unsigned char)(SizeInWords >> 16);
177 Out[ByteNo++] = (unsigned char)(SizeInWords >> 24);
179 // Restore the inner block's code size and abbrev table.
180 CurCodeSize = B.PrevCodeSize;
181 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
182 BlockScope.pop_back();
185 //===--------------------------------------------------------------------===//
187 //===--------------------------------------------------------------------===//
189 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
190 /// we have one to compress the output.
191 void EmitRecord(unsigned Code, SmallVectorImpl<uint64_t> &Vals,
192 unsigned Abbrev = 0) {
194 unsigned AbbrevNo = Abbrev-bitc::FIRST_ABBREV;
195 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
196 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
198 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
203 // If we don't have an abbrev to use, emit this in its fully unabbreviated
205 EmitCode(bitc::UNABBREV_RECORD);
207 EmitVBR(Vals.size(), 6);
208 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
209 EmitVBR64(Vals[i], 6);
213 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
214 /// we have one to compress the output.
215 void EmitRecord(unsigned Code, SmallVectorImpl<unsigned> &Vals,
216 unsigned Abbrev = 0) {
218 assert(0 && "abbrevs not implemented yet!");
220 // If we don't have an abbrev to use, emit this in its fully unabbreviated
222 EmitCode(bitc::UNABBREV_RECORD);
224 EmitVBR(Vals.size(), 6);
225 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
230 //===--------------------------------------------------------------------===//
232 //===--------------------------------------------------------------------===//
234 /// EmitAbbrev - This emits an abbreviation to the stream. Note that this
235 /// method takes ownership of the specified abbrev.
236 unsigned EmitAbbrev(BitCodeAbbrev *Abbv) {
237 // Emit the abbreviation as a record.
238 EmitCode(bitc::DEFINE_ABBREV);
239 EmitVBR(Abbv->getNumOperandInfos(), 5);
240 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
241 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
242 Emit(Op.isLiteral(), 1);
243 if (Op.isLiteral()) {
244 EmitVBR64(Op.getLiteralValue(), 8);
246 Emit(Op.getEncoding(), 3);
247 if (Op.hasEncodingData())
248 EmitVBR64(Op.getEncodingData(), 5);
252 CurAbbrevs.push_back(Abbv);
253 return CurAbbrevs.size()-1+bitc::FIRST_ABBREV;
258 } // End llvm namespace