BitstreamReader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. //===- BitstreamReader.cpp - BitstreamReader implementation ---------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Bitcode/BitstreamReader.h"
  10. using namespace llvm;
  11. //===----------------------------------------------------------------------===//
  12. // BitstreamCursor implementation
  13. //===----------------------------------------------------------------------===//
  14. void BitstreamCursor::operator=(const BitstreamCursor &RHS) {
  15. freeState();
  16. BitStream = RHS.BitStream;
  17. NextChar = RHS.NextChar;
  18. CurWord = RHS.CurWord;
  19. BitsInCurWord = RHS.BitsInCurWord;
  20. CurCodeSize = RHS.CurCodeSize;
  21. // Copy abbreviations, and bump ref counts.
  22. CurAbbrevs = RHS.CurAbbrevs;
  23. for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
  24. CurAbbrevs[i]->addRef();
  25. // Copy block scope and bump ref counts.
  26. BlockScope = RHS.BlockScope;
  27. for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
  28. std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
  29. for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
  30. Abbrevs[i]->addRef();
  31. }
  32. }
  33. void BitstreamCursor::freeState() {
  34. // Free all the Abbrevs.
  35. for (size_t i = 0, e = CurAbbrevs.size(); i != e; ++i)
  36. CurAbbrevs[i]->dropRef();
  37. CurAbbrevs.clear();
  38. // Free all the Abbrevs in the block scope.
  39. for (size_t S = 0, e = BlockScope.size(); S != e; ++S) {
  40. std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
  41. for (size_t i = 0, e = Abbrevs.size(); i != e; ++i)
  42. Abbrevs[i]->dropRef();
  43. }
  44. BlockScope.clear();
  45. }
  46. /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, enter
  47. /// the block, and return true if the block has an error.
  48. bool BitstreamCursor::EnterSubBlock(unsigned BlockID, unsigned *NumWordsP) {
  49. // Save the current block's state on BlockScope.
  50. BlockScope.push_back(Block(CurCodeSize));
  51. BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
  52. // Add the abbrevs specific to this block to the CurAbbrevs list.
  53. if (const BitstreamReader::BlockInfo *Info =
  54. BitStream->getBlockInfo(BlockID)) {
  55. for (size_t i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
  56. CurAbbrevs.push_back(Info->Abbrevs[i]);
  57. CurAbbrevs.back()->addRef();
  58. }
  59. }
  60. // Get the codesize of this block.
  61. CurCodeSize = ReadVBR(bitc::CodeLenWidth);
  62. SkipToFourByteBoundary();
  63. unsigned NumWords = Read(bitc::BlockSizeWidth);
  64. if (NumWordsP) *NumWordsP = NumWords;
  65. // Validate that this block is sane.
  66. if (CurCodeSize == 0 || AtEndOfStream())
  67. return true;
  68. return false;
  69. }
  70. void BitstreamCursor::readAbbreviatedLiteral(const BitCodeAbbrevOp &Op,
  71. SmallVectorImpl<uint64_t> &Vals) {
  72. assert(Op.isLiteral() && "Not a literal");
  73. // If the abbrev specifies the literal value to use, use it.
  74. Vals.push_back(Op.getLiteralValue());
  75. }
  76. void BitstreamCursor::readAbbreviatedField(const BitCodeAbbrevOp &Op,
  77. SmallVectorImpl<uint64_t> &Vals) {
  78. assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
  79. // Decode the value as we are commanded.
  80. switch (Op.getEncoding()) {
  81. case BitCodeAbbrevOp::Array:
  82. case BitCodeAbbrevOp::Blob:
  83. assert(0 && "Should not reach here");
  84. case BitCodeAbbrevOp::Fixed:
  85. Vals.push_back(Read((unsigned)Op.getEncodingData()));
  86. break;
  87. case BitCodeAbbrevOp::VBR:
  88. Vals.push_back(ReadVBR64((unsigned)Op.getEncodingData()));
  89. break;
  90. case BitCodeAbbrevOp::Char6:
  91. Vals.push_back(BitCodeAbbrevOp::DecodeChar6(Read(6)));
  92. break;
  93. }
  94. }
  95. void BitstreamCursor::skipAbbreviatedField(const BitCodeAbbrevOp &Op) {
  96. assert(!Op.isLiteral() && "Use ReadAbbreviatedLiteral for literals!");
  97. // Decode the value as we are commanded.
  98. switch (Op.getEncoding()) {
  99. case BitCodeAbbrevOp::Array:
  100. case BitCodeAbbrevOp::Blob:
  101. assert(0 && "Should not reach here");
  102. case BitCodeAbbrevOp::Fixed:
  103. (void)Read((unsigned)Op.getEncodingData());
  104. break;
  105. case BitCodeAbbrevOp::VBR:
  106. (void)ReadVBR64((unsigned)Op.getEncodingData());
  107. break;
  108. case BitCodeAbbrevOp::Char6:
  109. (void)Read(6);
  110. break;
  111. }
  112. }
  113. /// skipRecord - Read the current record and discard it.
  114. void BitstreamCursor::skipRecord(unsigned AbbrevID) {
  115. // Skip unabbreviated records by reading past their entries.
  116. if (AbbrevID == bitc::UNABBREV_RECORD) {
  117. unsigned Code = ReadVBR(6);
  118. (void)Code;
  119. unsigned NumElts = ReadVBR(6);
  120. for (unsigned i = 0; i != NumElts; ++i)
  121. (void)ReadVBR64(6);
  122. return;
  123. }
  124. const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
  125. for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
  126. const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
  127. if (Op.isLiteral())
  128. continue;
  129. if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
  130. Op.getEncoding() != BitCodeAbbrevOp::Blob) {
  131. skipAbbreviatedField(Op);
  132. continue;
  133. }
  134. if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
  135. // Array case. Read the number of elements as a vbr6.
  136. unsigned NumElts = ReadVBR(6);
  137. // Get the element encoding.
  138. assert(i+2 == e && "array op not second to last?");
  139. const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
  140. // Read all the elements.
  141. for (; NumElts; --NumElts)
  142. skipAbbreviatedField(EltEnc);
  143. continue;
  144. }
  145. assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
  146. // Blob case. Read the number of bytes as a vbr6.
  147. unsigned NumElts = ReadVBR(6);
  148. SkipToFourByteBoundary(); // 32-bit alignment
  149. // Figure out where the end of this blob will be including tail padding.
  150. size_t NewEnd = GetCurrentBitNo()+((NumElts+3)&~3)*8;
  151. // If this would read off the end of the bitcode file, just set the
  152. // record to empty and return.
  153. if (!canSkipToPos(NewEnd/8)) {
  154. NextChar = BitStream->getBitcodeBytes().getExtent();
  155. break;
  156. }
  157. // Skip over the blob.
  158. JumpToBit(NewEnd);
  159. }
  160. }
  161. unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
  162. SmallVectorImpl<uint64_t> &Vals,
  163. StringRef *Blob) {
  164. if (AbbrevID == bitc::UNABBREV_RECORD) {
  165. unsigned Code = ReadVBR(6);
  166. unsigned NumElts = ReadVBR(6);
  167. for (unsigned i = 0; i != NumElts; ++i)
  168. Vals.push_back(ReadVBR64(6));
  169. return Code;
  170. }
  171. const BitCodeAbbrev *Abbv = getAbbrev(AbbrevID);
  172. // Read the record code first.
  173. assert(Abbv->getNumOperandInfos() != 0 && "no record code in abbreviation?");
  174. const BitCodeAbbrevOp &CodeOp = Abbv->getOperandInfo(0);
  175. if (CodeOp.isLiteral())
  176. readAbbreviatedLiteral(CodeOp, Vals);
  177. else
  178. readAbbreviatedField(CodeOp, Vals);
  179. unsigned Code = (unsigned)Vals.pop_back_val();
  180. for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
  181. const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
  182. if (Op.isLiteral()) {
  183. readAbbreviatedLiteral(Op, Vals);
  184. continue;
  185. }
  186. if (Op.getEncoding() != BitCodeAbbrevOp::Array &&
  187. Op.getEncoding() != BitCodeAbbrevOp::Blob) {
  188. readAbbreviatedField(Op, Vals);
  189. continue;
  190. }
  191. if (Op.getEncoding() == BitCodeAbbrevOp::Array) {
  192. // Array case. Read the number of elements as a vbr6.
  193. unsigned NumElts = ReadVBR(6);
  194. // Get the element encoding.
  195. assert(i+2 == e && "array op not second to last?");
  196. const BitCodeAbbrevOp &EltEnc = Abbv->getOperandInfo(++i);
  197. // Read all the elements.
  198. for (; NumElts; --NumElts)
  199. readAbbreviatedField(EltEnc, Vals);
  200. continue;
  201. }
  202. assert(Op.getEncoding() == BitCodeAbbrevOp::Blob);
  203. // Blob case. Read the number of bytes as a vbr6.
  204. unsigned NumElts = ReadVBR(6);
  205. SkipToFourByteBoundary(); // 32-bit alignment
  206. // Figure out where the end of this blob will be including tail padding.
  207. size_t CurBitPos = GetCurrentBitNo();
  208. size_t NewEnd = CurBitPos+((NumElts+3)&~3)*8;
  209. // If this would read off the end of the bitcode file, just set the
  210. // record to empty and return.
  211. if (!canSkipToPos(NewEnd/8)) {
  212. Vals.append(NumElts, 0);
  213. NextChar = BitStream->getBitcodeBytes().getExtent();
  214. break;
  215. }
  216. // Otherwise, inform the streamer that we need these bytes in memory.
  217. const char *Ptr = (const char*)
  218. BitStream->getBitcodeBytes().getPointer(CurBitPos/8, NumElts);
  219. // If we can return a reference to the data, do so to avoid copying it.
  220. if (Blob) {
  221. *Blob = StringRef(Ptr, NumElts);
  222. } else {
  223. // Otherwise, unpack into Vals with zero extension.
  224. for (; NumElts; --NumElts)
  225. Vals.push_back((unsigned char)*Ptr++);
  226. }
  227. // Skip over tail padding.
  228. JumpToBit(NewEnd);
  229. }
  230. return Code;
  231. }
  232. void BitstreamCursor::ReadAbbrevRecord() {
  233. BitCodeAbbrev *Abbv = new BitCodeAbbrev();
  234. unsigned NumOpInfo = ReadVBR(5);
  235. for (unsigned i = 0; i != NumOpInfo; ++i) {
  236. bool IsLiteral = Read(1) ? true : false;
  237. if (IsLiteral) {
  238. Abbv->Add(BitCodeAbbrevOp(ReadVBR64(8)));
  239. continue;
  240. }
  241. BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3);
  242. if (BitCodeAbbrevOp::hasEncodingData(E)) {
  243. unsigned Data = ReadVBR64(5);
  244. // As a special case, handle fixed(0) (i.e., a fixed field with zero bits)
  245. // and vbr(0) as a literal zero. This is decoded the same way, and avoids
  246. // a slow path in Read() to have to handle reading zero bits.
  247. if ((E == BitCodeAbbrevOp::Fixed || E == BitCodeAbbrevOp::VBR) &&
  248. Data == 0) {
  249. Abbv->Add(BitCodeAbbrevOp(0));
  250. continue;
  251. }
  252. Abbv->Add(BitCodeAbbrevOp(E, Data));
  253. } else
  254. Abbv->Add(BitCodeAbbrevOp(E));
  255. }
  256. CurAbbrevs.push_back(Abbv);
  257. }
  258. bool BitstreamCursor::ReadBlockInfoBlock() {
  259. // If this is the second stream to get to the block info block, skip it.
  260. if (BitStream->hasBlockInfoRecords())
  261. return SkipBlock();
  262. if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
  263. SmallVector<uint64_t, 64> Record;
  264. BitstreamReader::BlockInfo *CurBlockInfo = nullptr;
  265. // Read all the records for this module.
  266. while (1) {
  267. BitstreamEntry Entry = advanceSkippingSubblocks(AF_DontAutoprocessAbbrevs);
  268. switch (Entry.Kind) {
  269. case llvm::BitstreamEntry::SubBlock: // Handled for us already.
  270. case llvm::BitstreamEntry::Error:
  271. return true;
  272. case llvm::BitstreamEntry::EndBlock:
  273. return false;
  274. case llvm::BitstreamEntry::Record:
  275. // The interesting case.
  276. break;
  277. }
  278. // Read abbrev records, associate them with CurBID.
  279. if (Entry.ID == bitc::DEFINE_ABBREV) {
  280. if (!CurBlockInfo) return true;
  281. ReadAbbrevRecord();
  282. // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
  283. // appropriate BlockInfo.
  284. BitCodeAbbrev *Abbv = CurAbbrevs.back();
  285. CurAbbrevs.pop_back();
  286. CurBlockInfo->Abbrevs.push_back(Abbv);
  287. continue;
  288. }
  289. // Read a record.
  290. Record.clear();
  291. switch (readRecord(Entry.ID, Record)) {
  292. default: break; // Default behavior, ignore unknown content.
  293. case bitc::BLOCKINFO_CODE_SETBID:
  294. if (Record.size() < 1) return true;
  295. CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
  296. break;
  297. case bitc::BLOCKINFO_CODE_BLOCKNAME: {
  298. if (!CurBlockInfo) return true;
  299. if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
  300. std::string Name;
  301. for (unsigned i = 0, e = Record.size(); i != e; ++i)
  302. Name += (char)Record[i];
  303. CurBlockInfo->Name = Name;
  304. break;
  305. }
  306. case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
  307. if (!CurBlockInfo) return true;
  308. if (BitStream->isIgnoringBlockInfoNames()) break; // Ignore name.
  309. std::string Name;
  310. for (unsigned i = 1, e = Record.size(); i != e; ++i)
  311. Name += (char)Record[i];
  312. CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
  313. Name));
  314. break;
  315. }
  316. }
  317. }
  318. }