OLD | NEW |
1 //===- NaClBitstreamWriter.h - NaCl bitstream writer ------------*- C++ -*-===// | 1 //===- NaClBitstreamWriter.h - NaCl bitstream writer ------------*- C++ -*-===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This header defines the BitstreamWriter class. This class can be used to | 10 // This header defines the BitstreamWriter class. This class can be used to |
11 // write an arbitrary bitstream, regardless of its contents. | 11 // write an arbitrary bitstream, regardless of its contents. |
12 // | 12 // |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H | 15 #ifndef LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H |
16 #define LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H | 16 #define LLVM_BITCODE_NACL_NACLBITSTREAMWRITER_H |
17 | 17 |
18 #include "llvm/ADT/SmallVector.h" | 18 #include "llvm/ADT/SmallVector.h" |
19 #include "llvm/ADT/StringRef.h" | 19 #include "llvm/ADT/StringRef.h" |
20 #include "llvm/Bitcode/BitCodes.h" | 20 #include "llvm/Bitcode/NaCl/NaClBitCodes.h" |
21 #include <vector> | 21 #include <vector> |
22 | 22 |
23 namespace llvm { | 23 namespace llvm { |
24 | 24 |
25 class NaClBitstreamWriter { | 25 class NaClBitstreamWriter { |
26 SmallVectorImpl<char> &Out; | 26 SmallVectorImpl<char> &Out; |
27 | 27 |
28 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use. | 28 /// CurBit - Always between 0 and 31 inclusive, specifies the next bit to use. |
29 unsigned CurBit; | 29 unsigned CurBit; |
30 | 30 |
31 /// CurValue - The current value. Only bits < CurBit are valid. | 31 /// CurValue - The current value. Only bits < CurBit are valid. |
32 uint32_t CurValue; | 32 uint32_t CurValue; |
33 | 33 |
34 /// CurCodeSize - This is the declared size of code values used for the | 34 /// CurCodeSize - This is the declared size of code values used for the |
35 /// current block, in bits. | 35 /// current block, in bits. |
36 unsigned CurCodeSize; | 36 unsigned CurCodeSize; |
37 | 37 |
38 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently | 38 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently |
39 /// selected BLOCK ID. | 39 /// selected BLOCK ID. |
40 unsigned BlockInfoCurBID; | 40 unsigned BlockInfoCurBID; |
41 | 41 |
42 /// CurAbbrevs - Abbrevs installed at in this block. | 42 /// CurAbbrevs - Abbrevs installed at in this block. |
43 std::vector<BitCodeAbbrev*> CurAbbrevs; | 43 std::vector<NaClBitCodeAbbrev*> CurAbbrevs; |
44 | 44 |
45 struct Block { | 45 struct Block { |
46 unsigned PrevCodeSize; | 46 unsigned PrevCodeSize; |
47 unsigned StartSizeWord; | 47 unsigned StartSizeWord; |
48 std::vector<BitCodeAbbrev*> PrevAbbrevs; | 48 std::vector<NaClBitCodeAbbrev*> PrevAbbrevs; |
49 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {} | 49 Block(unsigned PCS, unsigned SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {} |
50 }; | 50 }; |
51 | 51 |
52 /// BlockScope - This tracks the current blocks that we have entered. | 52 /// BlockScope - This tracks the current blocks that we have entered. |
53 std::vector<Block> BlockScope; | 53 std::vector<Block> BlockScope; |
54 | 54 |
55 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks. | 55 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks. |
56 /// These describe abbreviations that all blocks of the specified ID inherit. | 56 /// These describe abbreviations that all blocks of the specified ID inherit. |
57 struct BlockInfo { | 57 struct BlockInfo { |
58 unsigned BlockID; | 58 unsigned BlockID; |
59 std::vector<BitCodeAbbrev*> Abbrevs; | 59 std::vector<NaClBitCodeAbbrev*> Abbrevs; |
60 }; | 60 }; |
61 std::vector<BlockInfo> BlockInfoRecords; | 61 std::vector<BlockInfo> BlockInfoRecords; |
62 | 62 |
63 // BackpatchWord - Backpatch a 32-bit word in the output with the specified | 63 // BackpatchWord - Backpatch a 32-bit word in the output with the specified |
64 // value. | 64 // value. |
65 void BackpatchWord(unsigned ByteNo, unsigned NewWord) { | 65 void BackpatchWord(unsigned ByteNo, unsigned NewWord) { |
66 Out[ByteNo++] = (unsigned char)(NewWord >> 0); | 66 Out[ByteNo++] = (unsigned char)(NewWord >> 0); |
67 Out[ByteNo++] = (unsigned char)(NewWord >> 8); | 67 Out[ByteNo++] = (unsigned char)(NewWord >> 8); |
68 Out[ByteNo++] = (unsigned char)(NewWord >> 16); | 68 Out[ByteNo++] = (unsigned char)(NewWord >> 16); |
69 Out[ByteNo ] = (unsigned char)(NewWord >> 24); | 69 Out[ByteNo ] = (unsigned char)(NewWord >> 24); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); | 203 for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); |
204 i != e; ++i) | 204 i != e; ++i) |
205 if (BlockInfoRecords[i].BlockID == BlockID) | 205 if (BlockInfoRecords[i].BlockID == BlockID) |
206 return &BlockInfoRecords[i]; | 206 return &BlockInfoRecords[i]; |
207 return 0; | 207 return 0; |
208 } | 208 } |
209 | 209 |
210 void EnterSubblock(unsigned BlockID, unsigned CodeLen) { | 210 void EnterSubblock(unsigned BlockID, unsigned CodeLen) { |
211 // Block header: | 211 // Block header: |
212 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | 212 // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] |
213 EmitCode(bitc::ENTER_SUBBLOCK); | 213 EmitCode(naclbitc::ENTER_SUBBLOCK); |
214 EmitVBR(BlockID, bitc::BlockIDWidth); | 214 EmitVBR(BlockID, naclbitc::BlockIDWidth); |
215 EmitVBR(CodeLen, bitc::CodeLenWidth); | 215 EmitVBR(CodeLen, naclbitc::CodeLenWidth); |
216 FlushToWord(); | 216 FlushToWord(); |
217 | 217 |
218 unsigned BlockSizeWordIndex = GetWordIndex(); | 218 unsigned BlockSizeWordIndex = GetWordIndex(); |
219 unsigned OldCodeSize = CurCodeSize; | 219 unsigned OldCodeSize = CurCodeSize; |
220 | 220 |
221 // Emit a placeholder, which will be replaced when the block is popped. | 221 // Emit a placeholder, which will be replaced when the block is popped. |
222 Emit(0, bitc::BlockSizeWidth); | 222 Emit(0, naclbitc::BlockSizeWidth); |
223 | 223 |
224 CurCodeSize = CodeLen; | 224 CurCodeSize = CodeLen; |
225 | 225 |
226 // Push the outer block's abbrev set onto the stack, start out with an | 226 // Push the outer block's abbrev set onto the stack, start out with an |
227 // empty abbrev set. | 227 // empty abbrev set. |
228 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); | 228 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordIndex)); |
229 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); | 229 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); |
230 | 230 |
231 // If there is a blockinfo for this BlockID, add all the predefined abbrevs | 231 // If there is a blockinfo for this BlockID, add all the predefined abbrevs |
232 // to the abbrev list. | 232 // to the abbrev list. |
(...skipping 11 matching lines...) Expand all Loading... |
244 | 244 |
245 // Delete all abbrevs. | 245 // Delete all abbrevs. |
246 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); | 246 for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size()); |
247 i != e; ++i) | 247 i != e; ++i) |
248 CurAbbrevs[i]->dropRef(); | 248 CurAbbrevs[i]->dropRef(); |
249 | 249 |
250 const Block &B = BlockScope.back(); | 250 const Block &B = BlockScope.back(); |
251 | 251 |
252 // Block tail: | 252 // Block tail: |
253 // [END_BLOCK, <align4bytes>] | 253 // [END_BLOCK, <align4bytes>] |
254 EmitCode(bitc::END_BLOCK); | 254 EmitCode(naclbitc::END_BLOCK); |
255 FlushToWord(); | 255 FlushToWord(); |
256 | 256 |
257 // Compute the size of the block, in words, not counting the size field. | 257 // Compute the size of the block, in words, not counting the size field. |
258 unsigned SizeInWords = GetWordIndex() - B.StartSizeWord - 1; | 258 unsigned SizeInWords = GetWordIndex() - B.StartSizeWord - 1; |
259 unsigned ByteNo = B.StartSizeWord*4; | 259 unsigned ByteNo = B.StartSizeWord*4; |
260 | 260 |
261 // Update the block size field in the header of this sub-block. | 261 // Update the block size field in the header of this sub-block. |
262 BackpatchWord(ByteNo, SizeInWords); | 262 BackpatchWord(ByteNo, SizeInWords); |
263 | 263 |
264 // Restore the inner block's code size and abbrev table. | 264 // Restore the inner block's code size and abbrev table. |
265 CurCodeSize = B.PrevCodeSize; | 265 CurCodeSize = B.PrevCodeSize; |
266 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); | 266 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs); |
267 BlockScope.pop_back(); | 267 BlockScope.pop_back(); |
268 } | 268 } |
269 | 269 |
270 //===--------------------------------------------------------------------===// | 270 //===--------------------------------------------------------------------===// |
271 // Record Emission | 271 // Record Emission |
272 //===--------------------------------------------------------------------===// | 272 //===--------------------------------------------------------------------===// |
273 | 273 |
274 private: | 274 private: |
275 /// EmitAbbreviatedLiteral - Emit a literal value according to its abbrev | 275 /// EmitAbbreviatedLiteral - Emit a literal value according to its abbrev |
276 /// record. This is a no-op, since the abbrev specifies the literal to use. | 276 /// record. This is a no-op, since the abbrev specifies the literal to use. |
277 template<typename uintty> | 277 template<typename uintty> |
278 void EmitAbbreviatedLiteral(const BitCodeAbbrevOp &Op, uintty V) { | 278 void EmitAbbreviatedLiteral(const NaClBitCodeAbbrevOp &Op, uintty V) { |
279 assert(Op.isLiteral() && "Not a literal"); | 279 assert(Op.isLiteral() && "Not a literal"); |
280 // If the abbrev specifies the literal value to use, don't emit | 280 // If the abbrev specifies the literal value to use, don't emit |
281 // anything. | 281 // anything. |
282 assert(V == Op.getLiteralValue() && | 282 assert(V == Op.getLiteralValue() && |
283 "Invalid abbrev for record!"); | 283 "Invalid abbrev for record!"); |
284 } | 284 } |
285 | 285 |
286 /// EmitAbbreviatedField - Emit a single scalar field value with the specified | 286 /// EmitAbbreviatedField - Emit a single scalar field value with the specified |
287 /// encoding. | 287 /// encoding. |
288 template<typename uintty> | 288 template<typename uintty> |
289 void EmitAbbreviatedField(const BitCodeAbbrevOp &Op, uintty V) { | 289 void EmitAbbreviatedField(const NaClBitCodeAbbrevOp &Op, uintty V) { |
290 assert(!Op.isLiteral() && "Literals should use EmitAbbreviatedLiteral!"); | 290 assert(!Op.isLiteral() && "Literals should use EmitAbbreviatedLiteral!"); |
291 | 291 |
292 // Encode the value as we are commanded. | 292 // Encode the value as we are commanded. |
293 switch (Op.getEncoding()) { | 293 switch (Op.getEncoding()) { |
294 default: llvm_unreachable("Unknown encoding!"); | 294 default: llvm_unreachable("Unknown encoding!"); |
295 case BitCodeAbbrevOp::Fixed: | 295 case NaClBitCodeAbbrevOp::Fixed: |
296 if (Op.getEncodingData()) | 296 if (Op.getEncodingData()) |
297 Emit((unsigned)V, (unsigned)Op.getEncodingData()); | 297 Emit((unsigned)V, (unsigned)Op.getEncodingData()); |
298 break; | 298 break; |
299 case BitCodeAbbrevOp::VBR: | 299 case NaClBitCodeAbbrevOp::VBR: |
300 if (Op.getEncodingData()) | 300 if (Op.getEncodingData()) |
301 EmitVBR64(V, (unsigned)Op.getEncodingData()); | 301 EmitVBR64(V, (unsigned)Op.getEncodingData()); |
302 break; | 302 break; |
303 case BitCodeAbbrevOp::Char6: | 303 case NaClBitCodeAbbrevOp::Char6: |
304 Emit(BitCodeAbbrevOp::EncodeChar6((char)V), 6); | 304 Emit(NaClBitCodeAbbrevOp::EncodeChar6((char)V), 6); |
305 break; | 305 break; |
306 } | 306 } |
307 } | 307 } |
308 | 308 |
309 /// EmitRecordWithAbbrevImpl - This is the core implementation of the record | 309 /// EmitRecordWithAbbrevImpl - This is the core implementation of the record |
310 /// emission code. If BlobData is non-null, then it specifies an array of | 310 /// emission code. If BlobData is non-null, then it specifies an array of |
311 /// data that should be emitted as part of the Blob or Array operand that is | 311 /// data that should be emitted as part of the Blob or Array operand that is |
312 /// known to exist at the end of the record. | 312 /// known to exist at the end of the record. |
313 template<typename uintty> | 313 template<typename uintty> |
314 void EmitRecordWithAbbrevImpl(unsigned Abbrev, SmallVectorImpl<uintty> &Vals, | 314 void EmitRecordWithAbbrevImpl(unsigned Abbrev, SmallVectorImpl<uintty> &Vals, |
315 StringRef Blob) { | 315 StringRef Blob) { |
316 const char *BlobData = Blob.data(); | 316 const char *BlobData = Blob.data(); |
317 unsigned BlobLen = (unsigned) Blob.size(); | 317 unsigned BlobLen = (unsigned) Blob.size(); |
318 unsigned AbbrevNo = Abbrev-bitc::FIRST_APPLICATION_ABBREV; | 318 unsigned AbbrevNo = Abbrev-naclbitc::FIRST_APPLICATION_ABBREV; |
319 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!"); | 319 assert(AbbrevNo < CurAbbrevs.size() && "Invalid abbrev #!"); |
320 BitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo]; | 320 NaClBitCodeAbbrev *Abbv = CurAbbrevs[AbbrevNo]; |
321 | 321 |
322 EmitCode(Abbrev); | 322 EmitCode(Abbrev); |
323 | 323 |
324 unsigned RecordIdx = 0; | 324 unsigned RecordIdx = 0; |
325 for (unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos()); | 325 for (unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos()); |
326 i != e; ++i) { | 326 i != e; ++i) { |
327 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); | 327 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); |
328 if (Op.isLiteral()) { | 328 if (Op.isLiteral()) { |
329 assert(RecordIdx < Vals.size() && "Invalid abbrev/record"); | 329 assert(RecordIdx < Vals.size() && "Invalid abbrev/record"); |
330 EmitAbbreviatedLiteral(Op, Vals[RecordIdx]); | 330 EmitAbbreviatedLiteral(Op, Vals[RecordIdx]); |
331 ++RecordIdx; | 331 ++RecordIdx; |
332 } else if (Op.getEncoding() == BitCodeAbbrevOp::Array) { | 332 } else if (Op.getEncoding() == NaClBitCodeAbbrevOp::Array) { |
333 // Array case. | 333 // Array case. |
334 assert(i+2 == e && "array op not second to last?"); | 334 assert(i+2 == e && "array op not second to last?"); |
335 const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); | 335 const NaClBitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i); |
336 | 336 |
337 // If this record has blob data, emit it, otherwise we must have record | 337 // If this record has blob data, emit it, otherwise we must have record |
338 // entries to encode this way. | 338 // entries to encode this way. |
339 if (BlobData) { | 339 if (BlobData) { |
340 assert(RecordIdx == Vals.size() && | 340 assert(RecordIdx == Vals.size() && |
341 "Blob data and record entries specified for array!"); | 341 "Blob data and record entries specified for array!"); |
342 // Emit a vbr6 to indicate the number of elements present. | 342 // Emit a vbr6 to indicate the number of elements present. |
343 EmitVBR(static_cast<uint32_t>(BlobLen), 6); | 343 EmitVBR(static_cast<uint32_t>(BlobLen), 6); |
344 | 344 |
345 // Emit each field. | 345 // Emit each field. |
346 for (unsigned i = 0; i != BlobLen; ++i) | 346 for (unsigned i = 0; i != BlobLen; ++i) |
347 EmitAbbreviatedField(EltEnc, (unsigned char)BlobData[i]); | 347 EmitAbbreviatedField(EltEnc, (unsigned char)BlobData[i]); |
348 | 348 |
349 // Know that blob data is consumed for assertion below. | 349 // Know that blob data is consumed for assertion below. |
350 BlobData = 0; | 350 BlobData = 0; |
351 } else { | 351 } else { |
352 // Emit a vbr6 to indicate the number of elements present. | 352 // Emit a vbr6 to indicate the number of elements present. |
353 EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6); | 353 EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6); |
354 | 354 |
355 // Emit each field. | 355 // Emit each field. |
356 for (unsigned e = Vals.size(); RecordIdx != e; ++RecordIdx) | 356 for (unsigned e = Vals.size(); RecordIdx != e; ++RecordIdx) |
357 EmitAbbreviatedField(EltEnc, Vals[RecordIdx]); | 357 EmitAbbreviatedField(EltEnc, Vals[RecordIdx]); |
358 } | 358 } |
359 } else if (Op.getEncoding() == BitCodeAbbrevOp::Blob) { | 359 } else if (Op.getEncoding() == NaClBitCodeAbbrevOp::Blob) { |
360 // If this record has blob data, emit it, otherwise we must have record | 360 // If this record has blob data, emit it, otherwise we must have record |
361 // entries to encode this way. | 361 // entries to encode this way. |
362 | 362 |
363 // Emit a vbr6 to indicate the number of elements present. | 363 // Emit a vbr6 to indicate the number of elements present. |
364 if (BlobData) { | 364 if (BlobData) { |
365 EmitVBR(static_cast<uint32_t>(BlobLen), 6); | 365 EmitVBR(static_cast<uint32_t>(BlobLen), 6); |
366 assert(RecordIdx == Vals.size() && | 366 assert(RecordIdx == Vals.size() && |
367 "Blob data and record entries specified for blob operand!"); | 367 "Blob data and record entries specified for blob operand!"); |
368 } else { | 368 } else { |
369 EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6); | 369 EmitVBR(static_cast<uint32_t>(Vals.size()-RecordIdx), 6); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 public: | 403 public: |
404 | 404 |
405 /// EmitRecord - Emit the specified record to the stream, using an abbrev if | 405 /// EmitRecord - Emit the specified record to the stream, using an abbrev if |
406 /// we have one to compress the output. | 406 /// we have one to compress the output. |
407 template<typename uintty> | 407 template<typename uintty> |
408 void EmitRecord(unsigned Code, SmallVectorImpl<uintty> &Vals, | 408 void EmitRecord(unsigned Code, SmallVectorImpl<uintty> &Vals, |
409 unsigned Abbrev = 0) { | 409 unsigned Abbrev = 0) { |
410 if (!Abbrev) { | 410 if (!Abbrev) { |
411 // If we don't have an abbrev to use, emit this in its fully unabbreviated | 411 // If we don't have an abbrev to use, emit this in its fully unabbreviated |
412 // form. | 412 // form. |
413 EmitCode(bitc::UNABBREV_RECORD); | 413 EmitCode(naclbitc::UNABBREV_RECORD); |
414 EmitVBR(Code, 6); | 414 EmitVBR(Code, 6); |
415 EmitVBR(static_cast<uint32_t>(Vals.size()), 6); | 415 EmitVBR(static_cast<uint32_t>(Vals.size()), 6); |
416 for (unsigned i = 0, e = static_cast<unsigned>(Vals.size()); i != e; ++i) | 416 for (unsigned i = 0, e = static_cast<unsigned>(Vals.size()); i != e; ++i) |
417 EmitVBR64(Vals[i], 6); | 417 EmitVBR64(Vals[i], 6); |
418 return; | 418 return; |
419 } | 419 } |
420 | 420 |
421 // Insert the code into Vals to treat it uniformly. | 421 // Insert the code into Vals to treat it uniformly. |
422 Vals.insert(Vals.begin(), Code); | 422 Vals.insert(Vals.begin(), Code); |
423 | 423 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 return EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(ArrayData, | 461 return EmitRecordWithAbbrevImpl(Abbrev, Vals, StringRef(ArrayData, |
462 ArrayLen)); | 462 ArrayLen)); |
463 } | 463 } |
464 | 464 |
465 //===--------------------------------------------------------------------===// | 465 //===--------------------------------------------------------------------===// |
466 // Abbrev Emission | 466 // Abbrev Emission |
467 //===--------------------------------------------------------------------===// | 467 //===--------------------------------------------------------------------===// |
468 | 468 |
469 private: | 469 private: |
470 // Emit the abbreviation as a DEFINE_ABBREV record. | 470 // Emit the abbreviation as a DEFINE_ABBREV record. |
471 void EncodeAbbrev(BitCodeAbbrev *Abbv) { | 471 void EncodeAbbrev(NaClBitCodeAbbrev *Abbv) { |
472 EmitCode(bitc::DEFINE_ABBREV); | 472 EmitCode(naclbitc::DEFINE_ABBREV); |
473 EmitVBR(Abbv->getNumOperandInfos(), 5); | 473 EmitVBR(Abbv->getNumOperandInfos(), 5); |
474 for (unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos()); | 474 for (unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos()); |
475 i != e; ++i) { | 475 i != e; ++i) { |
476 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); | 476 const NaClBitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); |
477 Emit(Op.isLiteral(), 1); | 477 Emit(Op.isLiteral(), 1); |
478 if (Op.isLiteral()) { | 478 if (Op.isLiteral()) { |
479 EmitVBR64(Op.getLiteralValue(), 8); | 479 EmitVBR64(Op.getLiteralValue(), 8); |
480 } else { | 480 } else { |
481 Emit(Op.getEncoding(), 3); | 481 Emit(Op.getEncoding(), 3); |
482 if (Op.hasEncodingData()) | 482 if (Op.hasEncodingData()) |
483 EmitVBR64(Op.getEncodingData(), 5); | 483 EmitVBR64(Op.getEncodingData(), 5); |
484 } | 484 } |
485 } | 485 } |
486 } | 486 } |
487 public: | 487 public: |
488 | 488 |
489 /// EmitAbbrev - This emits an abbreviation to the stream. Note that this | 489 /// EmitAbbrev - This emits an abbreviation to the stream. Note that this |
490 /// method takes ownership of the specified abbrev. | 490 /// method takes ownership of the specified abbrev. |
491 unsigned EmitAbbrev(BitCodeAbbrev *Abbv) { | 491 unsigned EmitAbbrev(NaClBitCodeAbbrev *Abbv) { |
492 // Emit the abbreviation as a record. | 492 // Emit the abbreviation as a record. |
493 EncodeAbbrev(Abbv); | 493 EncodeAbbrev(Abbv); |
494 CurAbbrevs.push_back(Abbv); | 494 CurAbbrevs.push_back(Abbv); |
495 return static_cast<unsigned>(CurAbbrevs.size())-1 + | 495 return static_cast<unsigned>(CurAbbrevs.size())-1 + |
496 bitc::FIRST_APPLICATION_ABBREV; | 496 naclbitc::FIRST_APPLICATION_ABBREV; |
497 } | 497 } |
498 | 498 |
499 //===--------------------------------------------------------------------===// | 499 //===--------------------------------------------------------------------===// |
500 // BlockInfo Block Emission | 500 // BlockInfo Block Emission |
501 //===--------------------------------------------------------------------===// | 501 //===--------------------------------------------------------------------===// |
502 | 502 |
503 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. | 503 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK. |
504 void EnterBlockInfoBlock(unsigned CodeWidth) { | 504 void EnterBlockInfoBlock(unsigned CodeWidth) { |
505 EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, CodeWidth); | 505 EnterSubblock(naclbitc::BLOCKINFO_BLOCK_ID, CodeWidth); |
506 BlockInfoCurBID = ~0U; | 506 BlockInfoCurBID = ~0U; |
507 } | 507 } |
508 private: | 508 private: |
509 /// SwitchToBlockID - If we aren't already talking about the specified block | 509 /// SwitchToBlockID - If we aren't already talking about the specified block |
510 /// ID, emit a BLOCKINFO_CODE_SETBID record. | 510 /// ID, emit a BLOCKINFO_CODE_SETBID record. |
511 void SwitchToBlockID(unsigned BlockID) { | 511 void SwitchToBlockID(unsigned BlockID) { |
512 if (BlockInfoCurBID == BlockID) return; | 512 if (BlockInfoCurBID == BlockID) return; |
513 SmallVector<unsigned, 2> V; | 513 SmallVector<unsigned, 2> V; |
514 V.push_back(BlockID); | 514 V.push_back(BlockID); |
515 EmitRecord(bitc::BLOCKINFO_CODE_SETBID, V); | 515 EmitRecord(naclbitc::BLOCKINFO_CODE_SETBID, V); |
516 BlockInfoCurBID = BlockID; | 516 BlockInfoCurBID = BlockID; |
517 } | 517 } |
518 | 518 |
519 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) { | 519 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) { |
520 if (BlockInfo *BI = getBlockInfo(BlockID)) | 520 if (BlockInfo *BI = getBlockInfo(BlockID)) |
521 return *BI; | 521 return *BI; |
522 | 522 |
523 // Otherwise, add a new record. | 523 // Otherwise, add a new record. |
524 BlockInfoRecords.push_back(BlockInfo()); | 524 BlockInfoRecords.push_back(BlockInfo()); |
525 BlockInfoRecords.back().BlockID = BlockID; | 525 BlockInfoRecords.back().BlockID = BlockID; |
526 return BlockInfoRecords.back(); | 526 return BlockInfoRecords.back(); |
527 } | 527 } |
528 | 528 |
529 public: | 529 public: |
530 | 530 |
531 /// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified | 531 /// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified |
532 /// BlockID. | 532 /// BlockID. |
533 unsigned EmitBlockInfoAbbrev(unsigned BlockID, BitCodeAbbrev *Abbv) { | 533 unsigned EmitBlockInfoAbbrev(unsigned BlockID, NaClBitCodeAbbrev *Abbv) { |
534 SwitchToBlockID(BlockID); | 534 SwitchToBlockID(BlockID); |
535 EncodeAbbrev(Abbv); | 535 EncodeAbbrev(Abbv); |
536 | 536 |
537 // Add the abbrev to the specified block record. | 537 // Add the abbrev to the specified block record. |
538 BlockInfo &Info = getOrCreateBlockInfo(BlockID); | 538 BlockInfo &Info = getOrCreateBlockInfo(BlockID); |
539 Info.Abbrevs.push_back(Abbv); | 539 Info.Abbrevs.push_back(Abbv); |
540 | 540 |
541 return Info.Abbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV; | 541 return Info.Abbrevs.size()-1+naclbitc::FIRST_APPLICATION_ABBREV; |
542 } | 542 } |
543 }; | 543 }; |
544 | 544 |
545 | 545 |
546 } // End llvm namespace | 546 } // End llvm namespace |
547 | 547 |
548 #endif | 548 #endif |
OLD | NEW |