diff options
Diffstat (limited to 'lib/Bitcode')
-rw-r--r-- | lib/Bitcode/NaCl/Reader/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/Bitcode/NaCl/Reader/NaClBitcodeParser.cpp | 95 |
2 files changed, 96 insertions, 0 deletions
diff --git a/lib/Bitcode/NaCl/Reader/CMakeLists.txt b/lib/Bitcode/NaCl/Reader/CMakeLists.txt index 9e4de723c1..d2eb93b00a 100644 --- a/lib/Bitcode/NaCl/Reader/CMakeLists.txt +++ b/lib/Bitcode/NaCl/Reader/CMakeLists.txt @@ -2,6 +2,7 @@ add_llvm_library(LLVMNaClBitReader NaClBitcodeHeader.cpp NaClBitcodeReader.cpp NaClBitstreamReader.cpp + NaClBitcodeParser.cpp ) add_dependencies(LLVMNaClBitReader intrinsics_gen) diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeParser.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeParser.cpp new file mode 100644 index 0000000000..642c8cfd34 --- /dev/null +++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeParser.cpp @@ -0,0 +1,95 @@ +//===- NaClBitcodeParser.cpp ----------------------------------------------===// +// Low-level bitcode driver to parse PNaCl bitcode files. +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "NaClBitcodeParser" + +#include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" +#include "llvm/Support/Debug.h" + +void NaClBitcodeRecord::Print(raw_ostream& os) const { + DEBUG(os << "Block " << GetBlockID() << ", Code " << Code + << ", EntryID " << Entry.ID << ", <"; + for (unsigned i = 0, e = Values.size(); i != e; ++i) { + if (i > 0) os << " "; + os << Values[i]; + } + os << ">"); +} + +NaClBitcodeParser::~NaClBitcodeParser() {} + +bool NaClBitcodeParser::Parse() { + Record.ReadEntry(); + + if (Record.GetEntryKind() != NaClBitstreamEntry::SubBlock) + return Error("Expected block, but not found"); + + return ParseBlock(Record.GetEntryID()); +} + +bool NaClBitcodeParser::ParseThisBlock() { + if (GetBlockID() == naclbitc::BLOCKINFO_BLOCK_ID) { + // BLOCKINFO is a special part of the stream. Let the bitstream + // reader process this block. + // + // TODO(kschimpf): Move this out of the bitstream reader, so that + // we have simplier API's for this class. + EnterBlockInfo(); + if (Record.GetCursor().ReadBlockInfoBlock()) + return Error("Malformed BlockInfoBlock"); + RemoveBlockBitsFromEnclosingBlock(); + ExitBlockInfo(); + return false; + } + + // Regular block. Enter subblock. + unsigned NumWords; + if (Record.GetCursor().EnterSubBlock(GetBlockID(), &NumWords)) { + return Error("Malformed block record"); + } + + EnterBlock(NumWords); + + // Process records. + while (1) { + if (Record.GetCursor().AtEndOfStream()) + return Error("Premature end of bitstream"); + + // Read entry defining type of entry. + Record.ReadEntry(); + + switch (Record.GetEntryKind()) { + case NaClBitstreamEntry::Error: + return Error("malformed bitcode file"); + case NaClBitstreamEntry::EndBlock: { + ExitBlock(); + RemoveBlockBitsFromEnclosingBlock(); + return false; + } + case NaClBitstreamEntry::SubBlock: { + if (ParseBlock(Record.GetEntryID())) return true; + break; + } + case NaClBitstreamEntry::Record: + // The interesting case. + if (Record.GetEntryID() == naclbitc::DEFINE_ABBREV) { + //Process any block-local abbreviation definitions. + Record.GetCursor().ReadAbbrevRecord(); + ProcessRecordAbbrev(); + } else { + // Read in a record. + Record.ReadValues(); + ProcessRecord(); + } + break; + } + } + return false; +} |