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"
19 #include "llvm/ADT/SmallVector.h"
25 class BitstreamWriter {
26 std::vector<unsigned char> &Out;
28 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
31 /// CurValue - The current value. Only bits < CurBit are valid.
34 // CurCodeSize - This is the declared size of code values used for the current
39 unsigned PrevCodeSize;
40 unsigned StartSizeWord;
41 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
44 /// BlockScope - This tracks the current blocks that we have entered.
45 std::vector<Block> BlockScope;
47 BitstreamWriter(std::vector<unsigned char> &O)
48 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
51 assert(CurBit == 0 && "Unflused data remaining");
52 assert(BlockScope.empty() && "Block imbalance");
54 //===--------------------------------------------------------------------===//
55 // Basic Primitives for emitting bits to the stream.
56 //===--------------------------------------------------------------------===//
58 void Emit(uint32_t Val, unsigned NumBits) {
59 assert(NumBits <= 32 && "Invalid value size!");
60 assert((Val & ~(~0U >> (32-NumBits))) == 0 && "High bits set!");
61 CurValue |= Val << CurBit;
62 if (CurBit + NumBits < 32) {
67 // Add the current word.
68 unsigned V = CurValue;
69 Out.push_back((unsigned char)(V >> 0));
70 Out.push_back((unsigned char)(V >> 8));
71 Out.push_back((unsigned char)(V >> 16));
72 Out.push_back((unsigned char)(V >> 24));
75 CurValue = Val >> 32-CurBit;
78 CurBit = (CurBit+NumBits) & 31;
81 void Emit64(uint64_t Val, unsigned NumBits) {
83 Emit((uint32_t)Val, NumBits);
85 Emit((uint32_t)Val, 32);
86 Emit((uint32_t)(Val >> 32), NumBits-32);
92 unsigned V = CurValue;
93 Out.push_back((unsigned char)(V >> 0));
94 Out.push_back((unsigned char)(V >> 8));
95 Out.push_back((unsigned char)(V >> 16));
96 Out.push_back((unsigned char)(V >> 24));
102 void EmitVBR(uint32_t Val, unsigned NumBits) {
103 uint32_t Threshold = 1U << (NumBits-1);
105 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
106 while (Val >= Threshold) {
107 Emit((Val & ((1 << (NumBits-1))-1)) | (1 << (NumBits-1)), NumBits);
114 void EmitVBR64(uint64_t Val, unsigned NumBits) {
115 if ((uint32_t)Val == Val)
116 return EmitVBR((uint32_t)Val, NumBits);
118 uint64_t Threshold = 1U << (NumBits-1);
120 // Emit the bits with VBR encoding, NumBits-1 bits at a time.
121 while (Val >= Threshold) {
122 Emit(((uint32_t)Val & ((1 << (NumBits-1))-1)) |
123 (1 << (NumBits-1)), NumBits);
127 Emit((uint32_t)Val, NumBits);
130 /// EmitCode - Emit the specified code.
131 void EmitCode(unsigned Val) {
132 Emit(Val, CurCodeSize);
135 //===--------------------------------------------------------------------===//
136 // Block Manipulation
137 //===--------------------------------------------------------------------===//
139 void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
141 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
142 EmitCode(bitc::ENTER_SUBBLOCK);
143 EmitVBR(BlockID, bitc::BlockIDWidth);
144 EmitVBR(CodeLen, bitc::CodeLenWidth);
146 BlockScope.push_back(Block(CurCodeSize, Out.size()/4));
147 // Emit a placeholder, which will be replaced when the block is popped.
148 Emit(0, bitc::BlockSizeWidth);
150 CurCodeSize = CodeLen;
154 assert(!BlockScope.empty() && "Block scope imbalance!");
155 Block B = BlockScope.back();
156 BlockScope.pop_back();
159 // [END_BLOCK, <align4bytes>]
160 EmitCode(bitc::END_BLOCK);
163 // Compute the size of the block, in words, not counting the size field.
164 unsigned SizeInWords = Out.size()/4-B.StartSizeWord - 1;
165 unsigned ByteNo = B.StartSizeWord*4;
167 // Update the block size field in the header of this sub-block.
168 Out[ByteNo++] = (unsigned char)(SizeInWords >> 0);
169 Out[ByteNo++] = (unsigned char)(SizeInWords >> 8);
170 Out[ByteNo++] = (unsigned char)(SizeInWords >> 16);
171 Out[ByteNo++] = (unsigned char)(SizeInWords >> 24);
173 // Restore the outer block's code size.
174 CurCodeSize = B.PrevCodeSize;
177 //===--------------------------------------------------------------------===//
179 //===--------------------------------------------------------------------===//
181 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
182 /// we have one to compress the output.
183 void EmitRecord(unsigned Code, SmallVectorImpl<uint64_t> &Vals,
184 unsigned Abbrev = 0) {
186 assert(0 && "abbrevs not implemented yet!");
188 // If we don't have an abbrev to use, emit this in its fully unabbreviated
190 EmitCode(bitc::UNABBREV_RECORD);
192 EmitVBR(Vals.size(), 6);
193 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
194 EmitVBR64(Vals[i], 6);
198 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
199 /// we have one to compress the output.
200 void EmitRecord(unsigned Code, SmallVectorImpl<unsigned> &Vals,
201 unsigned Abbrev = 0) {
203 assert(0 && "abbrevs not implemented yet!");
205 // If we don't have an abbrev to use, emit this in its fully unabbreviated
207 EmitCode(bitc::UNABBREV_RECORD);
209 EmitVBR(Vals.size(), 6);
210 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
217 } // End llvm namespace