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"
20 #include "llvm/Support/DataTypes.h"
26 class BitstreamWriter {
27 std::vector<unsigned char> &Out;
29 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use.
32 /// CurValue - The current value. Only bits < CurBit are valid.
35 // CurCodeSize - This is the declared size of code values used for the current
40 unsigned PrevCodeSize;
41 unsigned StartSizeWord;
42 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
45 /// BlockScope - This tracks the current blocks that we have entered.
46 std::vector<Block> BlockScope;
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));
148 // Emit a placeholder, which will be replaced when the block is popped.
149 Emit(0, bitc::BlockSizeWidth);
151 CurCodeSize = CodeLen;
155 assert(!BlockScope.empty() && "Block scope imbalance!");
156 Block B = BlockScope.back();
157 BlockScope.pop_back();
160 // [END_BLOCK, <align4bytes>]
161 EmitCode(bitc::END_BLOCK);
164 // Compute the size of the block, in words, not counting the size field.
165 unsigned SizeInWords = Out.size()/4-B.StartSizeWord - 1;
166 unsigned ByteNo = B.StartSizeWord*4;
168 // Update the block size field in the header of this sub-block.
169 Out[ByteNo++] = (unsigned char)(SizeInWords >> 0);
170 Out[ByteNo++] = (unsigned char)(SizeInWords >> 8);
171 Out[ByteNo++] = (unsigned char)(SizeInWords >> 16);
172 Out[ByteNo++] = (unsigned char)(SizeInWords >> 24);
174 // Restore the outer block's code size.
175 CurCodeSize = B.PrevCodeSize;
178 //===--------------------------------------------------------------------===//
180 //===--------------------------------------------------------------------===//
182 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
183 /// we have one to compress the output.
184 void EmitRecord(unsigned Code, SmallVectorImpl<uint64_t> &Vals,
185 unsigned Abbrev = 0) {
187 assert(0 && "abbrevs not implemented yet!");
189 // If we don't have an abbrev to use, emit this in its fully unabbreviated
191 EmitCode(bitc::UNABBREV_RECORD);
193 EmitVBR(Vals.size(), 6);
194 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
195 EmitVBR64(Vals[i], 6);
199 /// EmitRecord - Emit the specified record to the stream, using an abbrev if
200 /// we have one to compress the output.
201 void EmitRecord(unsigned Code, SmallVectorImpl<unsigned> &Vals,
202 unsigned Abbrev = 0) {
204 assert(0 && "abbrevs not implemented yet!");
206 // If we don't have an abbrev to use, emit this in its fully unabbreviated
208 EmitCode(bitc::UNABBREV_RECORD);
210 EmitVBR(Vals.size(), 6);
211 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
218 } // End llvm namespace