#ifndef BITCODE_READER_H
#define BITCODE_READER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/Bitcode/BitstreamReader.h"
#include "llvm/Bitcode/LLVMBitCodes.h"
#include "llvm/GVMaterializer.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/OperandTraits.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/ValueHandle.h"
#include "llvm/Support/system_error.h"
#include <vector>
namespace llvm {
class MemoryBuffer;
class LLVMContext;
class BitcodeReaderValueList {
std::vector<WeakVH> ValuePtrs;
typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
ResolveConstantsTy ResolveConstants;
LLVMContext &Context;
public:
BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
~BitcodeReaderValueList() {
assert(ResolveConstants.empty() && "Constants not resolved?");
}
unsigned size() const { return ValuePtrs.size(); }
void resize(unsigned N) { ValuePtrs.resize(N); }
void push_back(Value *V) {
ValuePtrs.push_back(V);
}
void clear() {
assert(ResolveConstants.empty() && "Constants not resolved?");
ValuePtrs.clear();
}
Value *operator[](unsigned i) const {
assert(i < ValuePtrs.size());
return ValuePtrs[i];
}
Value *back() const { return ValuePtrs.back(); }
void pop_back() { ValuePtrs.pop_back(); }
bool empty() const { return ValuePtrs.empty(); }
void shrinkTo(unsigned N) {
assert(N <= size() && "Invalid shrinkTo request!");
ValuePtrs.resize(N);
}
Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
Value *getValueFwdRef(unsigned Idx, Type *Ty);
void AssignValue(Value *V, unsigned Idx);
void ResolveConstantForwardRefs();
};
class BitcodeReaderMDValueList {
std::vector<WeakVH> MDValuePtrs;
LLVMContext &Context;
public:
BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
unsigned size() const { return MDValuePtrs.size(); }
void resize(unsigned N) { MDValuePtrs.resize(N); }
void push_back(Value *V) { MDValuePtrs.push_back(V); }
void clear() { MDValuePtrs.clear(); }
Value *back() const { return MDValuePtrs.back(); }
void pop_back() { MDValuePtrs.pop_back(); }
bool empty() const { return MDValuePtrs.empty(); }
Value *operator[](unsigned i) const {
assert(i < MDValuePtrs.size());
return MDValuePtrs[i];
}
void shrinkTo(unsigned N) {
assert(N <= size() && "Invalid shrinkTo request!");
MDValuePtrs.resize(N);
}
Value *getValueFwdRef(unsigned Idx);
void AssignValue(Value *V, unsigned Idx);
};
class BitcodeReader : public GVMaterializer {
LLVMContext &Context;
Module *TheModule;
MemoryBuffer *Buffer;
bool BufferOwned;
OwningPtr<BitstreamReader> StreamFile;
BitstreamCursor Stream;
DataStreamer *LazyStreamer;
uint64_t NextUnreadBit;
bool SeenValueSymbolTable;
std::vector<Type*> TypeList;
BitcodeReaderValueList ValueList;
BitcodeReaderMDValueList MDValueList;
SmallVector<Instruction *, 64> InstructionList;
SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords;
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
SmallVector<Instruction*, 64> InstsWithTBAATag;
std::vector<AttributeSet> MAttributes;
std::map<unsigned, AttributeSet> MAttributeGroups;
std::vector<BasicBlock*> FunctionBBs;
std::vector<Function*> FunctionsWithBodies;
typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
UpgradedIntrinsicMap UpgradedIntrinsics;
DenseMap<unsigned, unsigned> MDKindMap;
bool SeenFirstFunctionBody;
DenseMap<Function*, uint64_t> DeferredFunctionInfo;
typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
bool UseRelativeIDs;
static const error_category &BitcodeErrorCategory();
public:
enum ErrorType {
BitcodeStreamInvalidSize,
ConflictingMETADATA_KINDRecords,
CouldNotFindFunctionInStream,
ExpectedConstant,
InsufficientFunctionProtos,
InvalidBitcodeSignature,
InvalidBitcodeWrapperHeader,
InvalidConstantReference,
InvalidID, InvalidInstructionWithNoBB,
InvalidRecord, InvalidTypeForValue, InvalidTYPETable,
InvalidType, MalformedBlock, MalformedGlobalInitializerSet,
InvalidMultipleBlocks, NeverResolvedValueFoundInFunction,
InvalidValue };
error_code Error(ErrorType E) {
return error_code(E, BitcodeErrorCategory());
}
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
: Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false),
ValueList(C), MDValueList(C),
SeenFirstFunctionBody(false), UseRelativeIDs(false) {
}
explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
: Context(C), TheModule(0), Buffer(0), BufferOwned(false),
LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false),
ValueList(C), MDValueList(C),
SeenFirstFunctionBody(false), UseRelativeIDs(false) {
}
~BitcodeReader() {
FreeState();
}
void materializeForwardReferencedFunctions();
void FreeState();
void setBufferOwned(bool Owned) { BufferOwned = Owned; }
virtual bool isMaterializable(const GlobalValue *GV) const;
virtual bool isDematerializable(const GlobalValue *GV) const;
virtual error_code Materialize(GlobalValue *GV);
virtual error_code MaterializeModule(Module *M);
virtual void Dematerialize(GlobalValue *GV);
error_code ParseBitcodeInto(Module *M);
error_code ParseTriple(std::string &Triple);
static uint64_t decodeSignRotatedValue(uint64_t V);
private:
Type *getTypeByID(unsigned ID);
Value *getFnValueByID(unsigned ID, Type *Ty) {
if (Ty && Ty->isMetadataTy())
return MDValueList.getValueFwdRef(ID);
return ValueList.getValueFwdRef(ID, Ty);
}
BasicBlock *getBasicBlock(unsigned ID) const {
if (ID >= FunctionBBs.size()) return 0; return FunctionBBs[ID];
}
AttributeSet getAttributes(unsigned i) const {
if (i-1 < MAttributes.size())
return MAttributes[i-1];
return AttributeSet();
}
bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
unsigned InstNum, Value *&ResVal) {
if (Slot == Record.size()) return true;
unsigned ValNo = (unsigned)Record[Slot++];
if (UseRelativeIDs)
ValNo = InstNum - ValNo;
if (ValNo < InstNum) {
ResVal = getFnValueByID(ValNo, 0);
return ResVal == 0;
} else if (Slot == Record.size()) {
return true;
}
unsigned TypeNo = (unsigned)Record[Slot++];
ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
return ResVal == 0;
}
bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
unsigned InstNum, Type *Ty, Value *&ResVal) {
if (getValue(Record, Slot, InstNum, Ty, ResVal))
return true;
++Slot;
return false;
}
bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
unsigned InstNum, Type *Ty, Value *&ResVal) {
ResVal = getValue(Record, Slot, InstNum, Ty);
return ResVal == 0;
}
Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
unsigned InstNum, Type *Ty) {
if (Slot == Record.size()) return 0;
unsigned ValNo = (unsigned)Record[Slot];
if (UseRelativeIDs)
ValNo = InstNum - ValNo;
return getFnValueByID(ValNo, Ty);
}
Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
unsigned InstNum, Type *Ty) {
if (Slot == Record.size()) return 0;
unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
if (UseRelativeIDs)
ValNo = InstNum - ValNo;
return getFnValueByID(ValNo, Ty);
}
error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
error_code ParseModule(bool Resume);
error_code ParseAttributeBlock();
error_code ParseAttributeGroupBlock();
error_code ParseTypeTable();
error_code ParseTypeTableBody();
error_code ParseValueSymbolTable();
error_code ParseConstants();
error_code RememberAndSkipFunctionBody();
error_code ParseFunctionBody(Function *F);
error_code GlobalCleanup();
error_code ResolveGlobalAndAliasInits();
error_code ParseMetadata();
error_code ParseMetadataAttachment();
error_code ParseModuleTriple(std::string &Triple);
error_code ParseUseLists();
error_code InitStream();
error_code InitStreamFromBuffer();
error_code InitLazyStream();
error_code FindFunctionInStream(Function *F,
DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator);
};
}
#endif