BitstreamReader.cpp 13 KB

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