BitstreamReader.cpp 13 KB

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