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
36 /// CurAbbrevs - Abbrevs installed at in this block.
37 std::vector<BitCodeAbbrev*> CurAbbrevs;
40 unsigned PrevCodeSize;
41 unsigned StartSizeWord;
42 std::vector<BitCodeAbbrev*> PrevAbbrevs;
43 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
46 /// BlockScope - This tracks the current blocks that we have entered.
47 std::vector<Block> BlockScope;
50 BitstreamWriter(std::vector<unsigned char> &O)
51 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
54 assert(CurBit == 0 && "Unflused data remaining");
55 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
57 //===--------------------------------------------------------------------===//
58 // Basic Primitives for emitting bits to the stream.
59 //===--------------------------------------------------------------------===//
61 void Emit(uint32_t Val, unsigned NumBits) {
62 assert(NumBits <= 32 && "Invalid value size!");
63 assert((Val & ~(~0U >> (32-NumBits))) == 0 && "High bits set!");
64 CurValue |= Val << CurBit;
65 if (CurBit + NumBits < 32) {
70 // Add the current word.
71 unsigned V = CurValue;
72 Out.push_back((unsigned char)(V >> 0));
73 Out.push_back((unsigned char)(V >> 8));
74 Out.push_back((unsigned char)(V >> 16));
75 Out.push_back((unsigned char)(V >> 24));
78 CurValue = Val >> (32-CurBit);
81 CurBit = (CurBit+NumBits) & 31;
84 void Emit64(uint64_t Val, unsigned NumBits) {
86 Emit((uint32_t)Val, NumBits);
88 Emit((uint32_t)Val, 32);
89 Emit((uint32_t)(Val >> 32), NumBits-32);
95 unsigned V = CurValue;
96 Out.push_back((unsigned char)(V >> 0));
97 Out.push_back((unsigned char)(V >> 8));
98 Out.push_back((unsigned char)(V >> 16));
99 Out.push_back((unsigned char)(V >> 24));
105 void EmitVBR(uint32_t Val, unsigned NumBits) {
106 uint32_t Threshold = 1U << (NumBits-1);
108 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
109 while (Val >= Threshold) {
110 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
117 void EmitVBR64(uint64_t Val, unsigned NumBits) {
118 if ((uint32_t)Val == Val)
119 return EmitVBR((uint32_t)Val, NumBits);
121 uint64_t Threshold = 1U << (NumBits-1);
123 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
124 while (Val >= Threshold) {
125 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
126 (1 << (NumBits-1)), NumBits);
130 Emit((uint32_t)Val, NumBits);
133 /// EmitCode - Emit the specified code.
134 void EmitCode(unsigned Val) {
135 Emit(Val, CurCodeSize);
138 //===--------------------------------------------------------------------===//
139 // Block Manipulation
140 //===--------------------------------------------------------------------===//
142 void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
144 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
145 EmitCode(bitc::ENTER_SUBBLOCK);
146 EmitVBR(BlockID, bitc::BlockIDWidth);
147 EmitVBR(CodeLen, bitc::CodeLenWidth);
149 BlockScope.push_back(Block(CurCodeSize, Out.size()/4));
150 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
152 // Emit a placeholder, which will be replaced when the block is popped.
153 Emit(0, bitc::BlockSizeWidth);
155 CurCodeSize = CodeLen;
159 assert(!BlockScope.empty() && "Block scope imbalance!");
161 // Delete all abbrevs.
162 for (unsigned i = 0, e = CurAbbrevs.size(); i != e; ++i)
163 delete CurAbbrevs[i];
165 const Block &B = BlockScope.back();
168 // [END_BLOCK, <align4bytes>]
169 EmitCode(bitc::END_BLOCK);
172 // Compute the size of the block, in words, not counting the size field.
173 unsigned SizeInWords = Out.size()/4-B.StartSizeWord - 1;
174 unsigned ByteNo = B.StartSizeWord*4;
176 // Update the block size field in the header of this sub-block.
177 Out[ByteNo++] = (unsigned char)(SizeInWords >> 0);
178 Out[ByteNo++] = (unsigned char)(SizeInWords >> 8);
179 Out[ByteNo++] = (unsigned char)(SizeInWords >> 16);
180 Out[ByteNo++] = (unsigned char)(SizeInWords >> 24);
182 // Restore the inner block's code size and abbrev table.
183 CurCodeSize = B.PrevCodeSize;
184 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
185 BlockScope.pop_back();
188 //===--------------------------------------------------------------------===//
190 //===--------------------------------------------------------------------===//
192 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
193 /// we have one to compress the output.
194 void EmitRecord(unsigned Code, SmallVectorImpl<uint64_t> &Vals,
195 unsigned Abbrev = 0) {
197 unsigned AbbrevNo = Abbrev-bitc::FIRST_ABBREV;
198 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
199 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
203 // Insert the code into Vals to treat it uniformly.
204 Vals.insert(Vals.begin(), Code);
206 unsigned RecordIdx = 0;
207 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
208 assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
209 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
210 uint64_t RecordVal = Vals[RecordIdx];
212 if (Op.isLiteral()) {
213 // If the abbrev specifies the literal value to use, don't emit
215 assert(RecordVal == Op.getLiteralValue() &&
216 "Invalid abbrev for record!");
219 // Encode the value as we are commanded.
220 switch (Op.getEncoding()) {
221 default: assert(0 && "Unknown encoding!");
222 case BitCodeAbbrevOp::FixedWidth:
223 Emit64(RecordVal, Op.getEncodingData());
226 case BitCodeAbbrevOp::VBR:
227 EmitVBR64(RecordVal, Op.getEncodingData());
233 assert(RecordIdx == Vals.size() && "Not all record operands emitted!");
235 // If we don't have an abbrev to use, emit this in its fully unabbreviated
237 EmitCode(bitc::UNABBREV_RECORD);
239 EmitVBR(Vals.size(), 6);
240 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
241 EmitVBR64(Vals[i], 6);
245 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
246 /// we have one to compress the output.
247 void EmitRecord(unsigned Code, SmallVectorImpl<unsigned> &Vals,
248 unsigned Abbrev = 0) {
250 unsigned AbbrevNo = Abbrev-bitc::FIRST_ABBREV;
251 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!");
252 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo];
256 // Insert the code into Vals to treat it uniformly.
257 Vals.insert(Vals.begin(), Code);
259 unsigned RecordIdx = 0;
260 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
261 assert(RecordIdx < Vals.size() && "Invalid abbrev/record");
262 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
263 unsigned RecordVal = Vals[RecordIdx];
265 if (Op.isLiteral()) {
266 // If the abbrev specifies the literal value to use, don't emit
268 assert(RecordVal == Op.getLiteralValue() &&
269 "Invalid abbrev for record!");
272 // Encode the value as we are commanded.
273 switch (Op.getEncoding()) {
274 default: assert(0 && "Unknown encoding!");
275 case BitCodeAbbrevOp::FixedWidth:
276 Emit(RecordVal, Op.getEncodingData());
279 case BitCodeAbbrevOp::VBR:
280 EmitVBR(RecordVal, Op.getEncodingData());
286 assert(RecordIdx == Vals.size() && "Not all record operands emitted!");
288 // If we don't have an abbrev to use, emit this in its fully unabbreviated
290 EmitCode(bitc::UNABBREV_RECORD);
292 EmitVBR(Vals.size(), 6);
293 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
298 //===--------------------------------------------------------------------===//
300 //===--------------------------------------------------------------------===//
302 /// EmitAbbrev - This emits an abbreviation to the stream. Note that this
303 /// method takes ownership of the specified abbrev.
304 unsigned EmitAbbrev(BitCodeAbbrev *Abbv) {
305 // Emit the abbreviation as a record.
306 EmitCode(bitc::DEFINE_ABBREV);
307 EmitVBR(Abbv->getNumOperandInfos(), 5);
308 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
309 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
310 Emit(Op.isLiteral(), 1);
311 if (Op.isLiteral()) {
312 EmitVBR64(Op.getLiteralValue(), 8);
314 Emit(Op.getEncoding(), 3);
315 if (Op.hasEncodingData())
316 EmitVBR64(Op.getEncodingData(), 5);
320 CurAbbrevs.push_back(Abbv);
321 return CurAbbrevs.size()-1+bitc::FIRST_ABBREV;
326 } // End llvm namespace