Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(538)

Unified Diff: lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp

Issue 14314016: Copy LLVM bitcode reader to generate a PNaCl wire format reader. (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
similarity index 96%
copy from lib/Bitcode/Reader/BitcodeReader.cpp
copy to lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
index 5878b47376bf93c0cf82e6b09ef7ff356a669c99..0e2c003c73572a07ac84f182e13b6eba8c7c84a5 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
@@ -1,4 +1,5 @@
-//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
+//===- NaClBitcodeReader.cpp ----------------------------------------------===//
+// Internal NaClBitcodeReader implementation
//
// The LLVM Compiler Infrastructure
//
@@ -7,8 +8,8 @@
//
//===----------------------------------------------------------------------===//
-#include "llvm/Bitcode/ReaderWriter.h"
-#include "BitcodeReader.h"
+#include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
+#include "NaClBitcodeReader.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/AutoUpgrade.h"
@@ -28,14 +29,14 @@ enum {
SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
};
-void BitcodeReader::materializeForwardReferencedFunctions() {
+void NaClBitcodeReader::materializeForwardReferencedFunctions() {
while (!BlockAddrFwdRefs.empty()) {
Function *F = BlockAddrFwdRefs.begin()->first;
F->Materialize();
}
}
-void BitcodeReader::FreeState() {
+void NaClBitcodeReader::FreeState() {
if (BufferOwned)
delete Buffer;
Buffer = 0;
@@ -226,7 +227,7 @@ struct OperandTraits<ConstantPlaceHolder> :
}
-void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
+void NaClBitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
if (Idx == size()) {
push_back(V);
return;
@@ -255,7 +256,7 @@ void BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
}
-Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
+Constant *NaClBitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
Type *Ty) {
if (Idx >= size())
resize(Idx + 1);
@@ -271,7 +272,7 @@ Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
return C;
}
-Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
+Value *NaClBitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
if (Idx >= size())
resize(Idx + 1);
@@ -296,7 +297,7 @@ Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
/// building/reuniquing the constant. Instead of doing this, we look at all the
/// uses and rewrite all the place holders at once for any constant that uses
/// a placeholder.
-void BitcodeReaderValueList::ResolveConstantForwardRefs() {
+void NaClBitcodeReaderValueList::ResolveConstantForwardRefs() {
// Sort the values by-pointer so that they are efficient to look up with a
// binary search.
std::sort(ResolveConstants.begin(), ResolveConstants.end());
@@ -371,7 +372,7 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() {
}
}
-void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
+void NaClBitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
if (Idx == size()) {
push_back(V);
return;
@@ -395,7 +396,7 @@ void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
MDValuePtrs[Idx] = V;
}
-Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
+Value *NaClBitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
if (Idx >= size())
resize(Idx + 1);
@@ -410,7 +411,7 @@ Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
return V;
}
-Type *BitcodeReader::getTypeByID(unsigned ID) {
+Type *NaClBitcodeReader::getTypeByID(unsigned ID) {
// The type table size is always specified correctly.
if (ID >= TypeList.size())
return 0;
@@ -448,7 +449,7 @@ static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
(EncodedAttrs & 0xffff));
}
-bool BitcodeReader::ParseAttributeBlock() {
+bool NaClBitcodeReader::ParseAttributeBlock() {
if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
return Error("Malformed block record");
@@ -506,7 +507,7 @@ bool BitcodeReader::ParseAttributeBlock() {
}
}
-bool BitcodeReader::ParseAttributeGroupBlock() {
+bool NaClBitcodeReader::ParseAttributeGroupBlock() {
if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
return Error("Malformed block record");
@@ -581,14 +582,14 @@ bool BitcodeReader::ParseAttributeGroupBlock() {
}
}
-bool BitcodeReader::ParseTypeTable() {
+bool NaClBitcodeReader::ParseTypeTable() {
if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
return Error("Malformed block record");
return ParseTypeTableBody();
}
-bool BitcodeReader::ParseTypeTableBody() {
+bool NaClBitcodeReader::ParseTypeTableBody() {
if (!TypeList.empty())
return Error("Multiple TYPE_BLOCKs found!");
@@ -807,7 +808,7 @@ bool BitcodeReader::ParseTypeTableBody() {
}
}
-bool BitcodeReader::ParseValueSymbolTable() {
+bool NaClBitcodeReader::ParseValueSymbolTable() {
if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
return Error("Malformed block record");
@@ -861,7 +862,7 @@ bool BitcodeReader::ParseValueSymbolTable() {
}
}
-bool BitcodeReader::ParseMetadata() {
+bool NaClBitcodeReader::ParseMetadata() {
unsigned NextMDValueNo = MDValueList.size();
if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
@@ -961,7 +962,7 @@ bool BitcodeReader::ParseMetadata() {
/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
/// the LSB for dense VBR encoding.
-uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
+uint64_t NaClBitcodeReader::decodeSignRotatedValue(uint64_t V) {
if ((V & 1) == 0)
return V >> 1;
if (V != 1)
@@ -972,7 +973,7 @@ uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
/// values and aliases that we can.
-bool BitcodeReader::ResolveGlobalAndAliasInits() {
+bool NaClBitcodeReader::ResolveGlobalAndAliasInits() {
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
@@ -1011,12 +1012,12 @@ bool BitcodeReader::ResolveGlobalAndAliasInits() {
static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
SmallVector<uint64_t, 8> Words(Vals.size());
std::transform(Vals.begin(), Vals.end(), Words.begin(),
- BitcodeReader::decodeSignRotatedValue);
+ NaClBitcodeReader::decodeSignRotatedValue);
return APInt(TypeBits, Words);
}
-bool BitcodeReader::ParseConstants() {
+bool NaClBitcodeReader::ParseConstants() {
if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
return Error("Malformed block record");
@@ -1411,7 +1412,7 @@ bool BitcodeReader::ParseConstants() {
}
}
-bool BitcodeReader::ParseUseLists() {
+bool NaClBitcodeReader::ParseUseLists() {
if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
return Error("Malformed block record");
@@ -1451,7 +1452,7 @@ bool BitcodeReader::ParseUseLists() {
/// RememberAndSkipFunctionBody - When we see the block for a function body,
/// remember where it is and then skip it. This lets us lazily deserialize the
/// functions.
-bool BitcodeReader::RememberAndSkipFunctionBody() {
+bool NaClBitcodeReader::RememberAndSkipFunctionBody() {
// Get the function we are talking about.
if (FunctionsWithBodies.empty())
return Error("Insufficient function protos");
@@ -1469,7 +1470,7 @@ bool BitcodeReader::RememberAndSkipFunctionBody() {
return false;
}
-bool BitcodeReader::GlobalCleanup() {
+bool NaClBitcodeReader::GlobalCleanup() {
// Patch the initializers for globals and aliases up.
ResolveGlobalAndAliasInits();
if (!GlobalInits.empty() || !AliasInits.empty())
@@ -1495,7 +1496,7 @@ bool BitcodeReader::GlobalCleanup() {
return false;
}
-bool BitcodeReader::ParseModule(bool Resume) {
+bool NaClBitcodeReader::ParseModule(bool Resume) {
if (Resume)
Stream.JumpToBit(NextUnreadBit);
else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
@@ -1790,7 +1791,7 @@ bool BitcodeReader::ParseModule(bool Resume) {
}
}
-bool BitcodeReader::ParseBitcodeInto(Module *M) {
+bool NaClBitcodeReader::ParseBitcodeInto(Module *M) {
TheModule = 0;
if (InitStream()) return true;
@@ -1811,7 +1812,7 @@ bool BitcodeReader::ParseBitcodeInto(Module *M) {
return false;
BitstreamEntry Entry =
- Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
+ Stream.advance(NaClBitstreamCursor::AF_DontAutoprocessAbbrevs);
switch (Entry.Kind) {
case BitstreamEntry::Error:
@@ -1857,7 +1858,7 @@ bool BitcodeReader::ParseBitcodeInto(Module *M) {
}
}
-bool BitcodeReader::ParseModuleTriple(std::string &Triple) {
+bool NaClBitcodeReader::ParseModuleTriple(std::string &Triple) {
if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
return Error("Malformed block record");
@@ -1893,7 +1894,7 @@ bool BitcodeReader::ParseModuleTriple(std::string &Triple) {
}
}
-bool BitcodeReader::ParseTriple(std::string &Triple) {
+bool NaClBitcodeReader::ParseTriple(std::string &Triple) {
if (InitStream()) return true;
// Sniff for the signature.
@@ -1936,7 +1937,7 @@ bool BitcodeReader::ParseTriple(std::string &Triple) {
}
/// ParseMetadataAttachment - Parse metadata attachments.
-bool BitcodeReader::ParseMetadataAttachment() {
+bool NaClBitcodeReader::ParseMetadataAttachment() {
if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
return Error("Malformed block record");
@@ -1981,7 +1982,7 @@ bool BitcodeReader::ParseMetadataAttachment() {
}
/// ParseFunctionBody - Lazily parse the specified function body block.
-bool BitcodeReader::ParseFunctionBody(Function *F) {
+bool NaClBitcodeReader::ParseFunctionBody(Function *F) {
if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
return Error("Malformed block record");
@@ -2863,7 +2864,7 @@ OutOfRecordLoop:
}
/// FindFunctionInStream - Find the function body in the bitcode stream
-bool BitcodeReader::FindFunctionInStream(Function *F,
+bool NaClBitcodeReader::FindFunctionInStream(Function *F,
DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
while (DeferredFunctionInfoIterator->second == 0) {
if (Stream.AtEndOfStream())
@@ -2880,7 +2881,7 @@ bool BitcodeReader::FindFunctionInStream(Function *F,
//===----------------------------------------------------------------------===//
-bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
+bool NaClBitcodeReader::isMaterializable(const GlobalValue *GV) const {
if (const Function *F = dyn_cast<Function>(GV)) {
return F->isDeclaration() &&
DeferredFunctionInfo.count(const_cast<Function*>(F));
@@ -2888,7 +2889,7 @@ bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
return false;
}
-bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
+bool NaClBitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
Function *F = dyn_cast<Function>(GV);
// If it's not a function or is already material, ignore the request.
if (!F || !F->isMaterializable()) return false;
@@ -2923,7 +2924,7 @@ bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
return false;
}
-bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
+bool NaClBitcodeReader::isDematerializable(const GlobalValue *GV) const {
const Function *F = dyn_cast<Function>(GV);
if (!F || F->isDeclaration())
return false;
@@ -2940,7 +2941,7 @@ bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
return DeferredFunctionInfo.count(const_cast<Function*>(F));
}
-void BitcodeReader::Dematerialize(GlobalValue *GV) {
+void NaClBitcodeReader::Dematerialize(GlobalValue *GV) {
Function *F = dyn_cast<Function>(GV);
// If this function isn't dematerializable, this is a noop.
if (!F || !isDematerializable(F))
@@ -2953,9 +2954,9 @@ void BitcodeReader::Dematerialize(GlobalValue *GV) {
}
-bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
+bool NaClBitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
assert(M == TheModule &&
- "Can only Materialize the Module this BitcodeReader is attached to.");
+ "Can only Materialize the Module this NaClBitcodeReader is attached to.");
// Iterate over the module, deserializing any functions that are still on
// disk.
for (Module::iterator F = TheModule->begin(), E = TheModule->end();
@@ -2992,17 +2993,18 @@ bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
return false;
}
-bool BitcodeReader::InitStream() {
+bool NaClBitcodeReader::InitStream() {
if (LazyStreamer) return InitLazyStream();
return InitStreamFromBuffer();
}
-bool BitcodeReader::InitStreamFromBuffer() {
+bool NaClBitcodeReader::InitStreamFromBuffer() {
const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
if (Buffer->getBufferSize() & 3) {
- if (!isRawBitcode(BufPtr, BufEnd) && !isBitcodeWrapper(BufPtr, BufEnd))
+ if (!isNaClRawBitcode(BufPtr, BufEnd) &&
+ !isNaClBitcodeWrapper(BufPtr, BufEnd))
return Error("Invalid bitcode signature");
else
return Error("Bitcode stream should be a multiple of 4 bytes in length");
@@ -3010,34 +3012,34 @@ bool BitcodeReader::InitStreamFromBuffer() {
// If we have a wrapper header, parse it and ignore the non-bc file contents.
// The magic number is 0x0B17C0DE stored in little endian.
- if (isBitcodeWrapper(BufPtr, BufEnd))
- if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
+ if (isNaClBitcodeWrapper(BufPtr, BufEnd))
+ if (SkipNaClBitcodeWrapperHeader(BufPtr, BufEnd, true))
return Error("Invalid bitcode wrapper header");
- StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
+ StreamFile.reset(new NaClBitstreamReader(BufPtr, BufEnd));
Stream.init(*StreamFile);
return false;
}
-bool BitcodeReader::InitLazyStream() {
- // Check and strip off the bitcode wrapper; BitstreamReader expects never to
- // see it.
+bool NaClBitcodeReader::InitLazyStream() {
+ // Check and strip off the bitcode wrapper; NaClBitstreamReader expects
+ // never to see it.
StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer);
- StreamFile.reset(new BitstreamReader(Bytes));
+ StreamFile.reset(new NaClBitstreamReader(Bytes));
Stream.init(*StreamFile);
unsigned char buf[16];
if (Bytes->readBytes(0, 16, buf, NULL) == -1)
return Error("Bitcode stream must be at least 16 bytes in length");
- if (!isBitcode(buf, buf + 16))
+ if (!isNaClBitcode(buf, buf + 16))
return Error("Invalid bitcode signature");
- if (isBitcodeWrapper(buf, buf + 4)) {
+ if (isNaClBitcodeWrapper(buf, buf + 4)) {
const unsigned char *bitcodeStart = buf;
const unsigned char *bitcodeEnd = buf + 16;
- SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
+ SkipNaClBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
Bytes->dropLeadingBytes(bitcodeStart - buf);
Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart);
}
@@ -3048,13 +3050,13 @@ bool BitcodeReader::InitLazyStream() {
// External interface
//===----------------------------------------------------------------------===//
-/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
+/// getNaClLazyBitcodeModule - lazy function-at-a-time loading from a file.
///
-Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
- LLVMContext& Context,
- std::string *ErrMsg) {
+Module *llvm::getNaClLazyBitcodeModule(MemoryBuffer *Buffer,
+ LLVMContext& Context,
jvoung (off chromium) 2013/04/29 18:02:08 tabs
Karl 2013/04/29 20:44:37 Done.
+ std::string *ErrMsg) {
Module *M = new Module(Buffer->getBufferIdentifier(), Context);
- BitcodeReader *R = new BitcodeReader(Buffer, Context);
+ NaClBitcodeReader *R = new NaClBitcodeReader(Buffer, Context);
M->setMaterializer(R);
if (R->ParseBitcodeInto(M)) {
if (ErrMsg)
@@ -3063,7 +3065,7 @@ Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
delete M; // Also deletes R.
return 0;
}
- // Have the BitcodeReader dtor delete 'Buffer'.
+ // Have the NaClBitcodeReader dtor delete 'Buffer'.
R->setBufferOwned(true);
R->materializeForwardReferencedFunctions();
@@ -3074,12 +3076,12 @@ Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
}
-Module *llvm::getStreamedBitcodeModule(const std::string &name,
- DataStreamer *streamer,
- LLVMContext &Context,
- std::string *ErrMsg) {
+Module *llvm::getNaClStreamedBitcodeModule(const std::string &name,
+ DataStreamer *streamer,
jvoung (off chromium) 2013/04/29 18:02:08 tabs!
Karl 2013/04/29 20:44:37 Done.
+ LLVMContext &Context,
+ std::string *ErrMsg) {
Module *M = new Module(name, Context);
- BitcodeReader *R = new BitcodeReader(streamer, Context);
+ NaClBitcodeReader *R = new NaClBitcodeReader(streamer, Context);
M->setMaterializer(R);
if (R->ParseBitcodeInto(M)) {
if (ErrMsg)
@@ -3096,18 +3098,18 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name,
return M;
}
-/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
+/// NaClParseBitcodeFile - Read the specified bitcode file, returning the module.
/// If an error occurs, return null and fill in *ErrMsg if non-null.
-Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
- std::string *ErrMsg){
- Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
+Module *llvm::NaClParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
+ std::string *ErrMsg){
jvoung (off chromium) 2013/04/29 18:02:08 tabs
Karl 2013/04/29 20:44:37 Done.
+ Module *M = getNaClLazyBitcodeModule(Buffer, Context, ErrMsg);
if (!M) return 0;
- // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
+ // Don't let the NaClBitcodeReader dtor delete 'Buffer', regardless of whether
// there was an error.
- static_cast<BitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
+ static_cast<NaClBitcodeReader*>(M->getMaterializer())->setBufferOwned(false);
- // Read in the entire module, and destroy the BitcodeReader.
+ // Read in the entire module, and destroy the NaClBitcodeReader.
if (M->MaterializeAllPermanently(ErrMsg)) {
delete M;
return 0;
@@ -3118,19 +3120,3 @@ Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
return M;
}
-
-std::string llvm::getBitcodeTargetTriple(MemoryBuffer *Buffer,
- LLVMContext& Context,
- std::string *ErrMsg) {
- BitcodeReader *R = new BitcodeReader(Buffer, Context);
- // Don't let the BitcodeReader dtor delete 'Buffer'.
- R->setBufferOwned(false);
-
- std::string Triple("");
- if (R->ParseTriple(Triple))
jvoung (off chromium) 2013/04/29 18:02:08 Any reason this is removed? The NaClBitcodeReader
Karl 2013/04/29 20:44:37 As far as I can tell, only lto uses it, so thought
jvoung (off chromium) 2013/04/29 21:49:34 Would have been a bit cleaner to do that separatel
- if (ErrMsg)
- *ErrMsg = R->getErrorString();
-
- delete R;
- return Triple;
-}

Powered by Google App Engine
This is Rietveld 408576698