mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2026-04-20 16:17:38 +00:00
Introduce MetadataBase, a base class for MDString and MDNode.
Derive MDString directly from MetadataBase. Introduce new bitcode block to hold metadata. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76759 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -699,6 +699,56 @@ bool BitcodeReader::ParseValueSymbolTable() {
|
||||
}
|
||||
}
|
||||
|
||||
bool BitcodeReader::ParseMetadata() {
|
||||
unsigned NextValueNo = ValueList.size();
|
||||
|
||||
if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
|
||||
return Error("Malformed block record");
|
||||
|
||||
SmallVector<uint64_t, 64> Record;
|
||||
|
||||
// Read all the records.
|
||||
while (1) {
|
||||
unsigned Code = Stream.ReadCode();
|
||||
if (Code == bitc::END_BLOCK) {
|
||||
if (Stream.ReadBlockEnd())
|
||||
return Error("Error at end of PARAMATTR block");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Code == bitc::ENTER_SUBBLOCK) {
|
||||
// No known subblocks, always skip them.
|
||||
Stream.ReadSubBlockID();
|
||||
if (Stream.SkipBlock())
|
||||
return Error("Malformed block record");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Code == bitc::DEFINE_ABBREV) {
|
||||
Stream.ReadAbbrevRecord();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read a record.
|
||||
Record.clear();
|
||||
switch (Stream.ReadRecord(Code, Record)) {
|
||||
default: // Default behavior: ignore.
|
||||
break;
|
||||
case bitc::METADATA_STRING: {
|
||||
unsigned MDStringLength = Record.size();
|
||||
SmallString<8> String;
|
||||
String.resize(MDStringLength);
|
||||
for (unsigned i = 0; i != MDStringLength; ++i)
|
||||
String[i] = Record[i];
|
||||
Value *V =
|
||||
Context.getMDString(String.c_str(), String.c_str() + MDStringLength);
|
||||
ValueList.AssignValue(V, NextValueNo++);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// DecodeSignRotatedValue - Decode a signed value stored with the sign bit in
|
||||
/// the LSB for dense VBR encoding.
|
||||
static uint64_t DecodeSignRotatedValue(uint64_t V) {
|
||||
@@ -1028,15 +1078,6 @@ bool BitcodeReader::ParseConstants() {
|
||||
AsmStr, ConstrStr, HasSideEffects);
|
||||
break;
|
||||
}
|
||||
case bitc::CST_CODE_MDSTRING: {
|
||||
unsigned MDStringLength = Record.size();
|
||||
SmallString<8> String;
|
||||
String.resize(MDStringLength);
|
||||
for (unsigned i = 0; i != MDStringLength; ++i)
|
||||
String[i] = Record[i];
|
||||
V = Context.getMDString(String.c_str(), String.c_str() + MDStringLength);
|
||||
break;
|
||||
}
|
||||
case bitc::CST_CODE_MDNODE: {
|
||||
if (Record.empty() || Record.size() % 2 == 1)
|
||||
return Error("Invalid CST_MDNODE record");
|
||||
@@ -1171,6 +1212,10 @@ bool BitcodeReader::ParseModule(const std::string &ModuleID) {
|
||||
if (ParseConstants() || ResolveGlobalAndAliasInits())
|
||||
return true;
|
||||
break;
|
||||
case bitc::METADATA_BLOCK_ID:
|
||||
if (ParseMetadata())
|
||||
return true;
|
||||
break;
|
||||
case bitc::FUNCTION_BLOCK_ID:
|
||||
// If this is the first function body we've seen, reverse the
|
||||
// FunctionsWithBodies list.
|
||||
|
||||
@@ -210,6 +210,7 @@ private:
|
||||
bool RememberAndSkipFunctionBody();
|
||||
bool ParseFunctionBody(Function *F);
|
||||
bool ResolveGlobalAndAliasInits();
|
||||
bool ParseMetadata();
|
||||
};
|
||||
|
||||
} // End llvm namespace
|
||||
|
||||
@@ -473,6 +473,42 @@ static uint64_t GetOptimizationFlags(const Value *V) {
|
||||
return Flags;
|
||||
}
|
||||
|
||||
static void WriteModuleMetadata(const ValueEnumerator &VE,
|
||||
BitstreamWriter &Stream) {
|
||||
const ValueEnumerator::ValueList &Vals = VE.getValues();
|
||||
bool StartedMetadataBlock = false;
|
||||
unsigned MDSAbbrev = 0;
|
||||
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
|
||||
if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
|
||||
if (!StartedMetadataBlock) {
|
||||
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
|
||||
|
||||
// Abbrev for CST_CODE_STRING.
|
||||
BitCodeAbbrev *Abbv = new BitCodeAbbrev();
|
||||
Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
|
||||
MDSAbbrev = Stream.EmitAbbrev(Abbv);
|
||||
StartedMetadataBlock = true;
|
||||
}
|
||||
|
||||
SmallVector<unsigned, 64> StrVals;
|
||||
StrVals.clear();
|
||||
// Code: [strchar x N]
|
||||
const char *StrBegin = MDS->begin();
|
||||
for (unsigned i = 0, e = MDS->size(); i != e; ++i)
|
||||
StrVals.push_back(StrBegin[i]);
|
||||
|
||||
// Emit the finished record.
|
||||
Stream.EmitRecord(bitc::METADATA_STRING, StrVals, MDSAbbrev);
|
||||
}
|
||||
}
|
||||
|
||||
if (StartedMetadataBlock)
|
||||
Stream.ExitBlock();
|
||||
}
|
||||
|
||||
|
||||
static void WriteConstants(unsigned FirstVal, unsigned LastVal,
|
||||
const ValueEnumerator &VE,
|
||||
BitstreamWriter &Stream, bool isGlobal) {
|
||||
@@ -484,8 +520,6 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
|
||||
unsigned String8Abbrev = 0;
|
||||
unsigned CString7Abbrev = 0;
|
||||
unsigned CString6Abbrev = 0;
|
||||
unsigned MDString8Abbrev = 0;
|
||||
unsigned MDString6Abbrev = 0;
|
||||
// If this is a constant pool for the module, emit module-specific abbrevs.
|
||||
if (isGlobal) {
|
||||
// Abbrev for CST_CODE_AGGREGATE.
|
||||
@@ -513,19 +547,6 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
|
||||
CString6Abbrev = Stream.EmitAbbrev(Abbv);
|
||||
|
||||
// Abbrev for CST_CODE_MDSTRING.
|
||||
Abbv = new BitCodeAbbrev();
|
||||
Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_MDSTRING));
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
|
||||
MDString8Abbrev = Stream.EmitAbbrev(Abbv);
|
||||
// Abbrev for CST_CODE_MDSTRING.
|
||||
Abbv = new BitCodeAbbrev();
|
||||
Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_MDSTRING));
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
|
||||
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
|
||||
MDString6Abbrev = Stream.EmitAbbrev(Abbv);
|
||||
}
|
||||
|
||||
SmallVector<uint64_t, 64> Record;
|
||||
@@ -534,6 +555,8 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
|
||||
const Type *LastTy = 0;
|
||||
for (unsigned i = FirstVal; i != LastVal; ++i) {
|
||||
const Value *V = Vals[i].first;
|
||||
if (isa<MDString>(V))
|
||||
continue;
|
||||
// If we need to switch types, do so now.
|
||||
if (V->getType() != LastTy) {
|
||||
LastTy = V->getType();
|
||||
@@ -713,16 +736,6 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
|
||||
Record.push_back(CE->getPredicate());
|
||||
break;
|
||||
}
|
||||
} else if (const MDString *S = dyn_cast<MDString>(C)) {
|
||||
Code = bitc::CST_CODE_MDSTRING;
|
||||
AbbrevToUse = MDString6Abbrev;
|
||||
for (unsigned i = 0, e = S->size(); i != e; ++i) {
|
||||
char V = S->begin()[i];
|
||||
Record.push_back(V);
|
||||
|
||||
if (!BitCodeAbbrevOp::isChar6(V))
|
||||
AbbrevToUse = MDString8Abbrev;
|
||||
}
|
||||
} else if (const MDNode *N = dyn_cast<MDNode>(C)) {
|
||||
Code = bitc::CST_CODE_MDNODE;
|
||||
for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
|
||||
@@ -1328,7 +1341,10 @@ static void WriteModule(const Module *M, BitstreamWriter &Stream) {
|
||||
// Emit top-level description of module, including target triple, inline asm,
|
||||
// descriptors for global variables, and function prototype info.
|
||||
WriteModuleInfo(M, VE, Stream);
|
||||
|
||||
|
||||
// Emit metadata.
|
||||
WriteModuleMetadata(VE, Stream);
|
||||
|
||||
// Emit constants.
|
||||
WriteModuleConstants(VE, Stream);
|
||||
|
||||
|
||||
@@ -259,10 +259,14 @@ void ValueEnumerator::EnumerateOperandType(const Value *V) {
|
||||
EnumerateOperandType(C->getOperand(i));
|
||||
|
||||
if (const MDNode *N = dyn_cast<MDNode>(V)) {
|
||||
for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
|
||||
EnumerateOperandType(N->getElement(i));
|
||||
for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
|
||||
Value *Elem = N->getElement(i);
|
||||
if (Elem)
|
||||
EnumerateOperandType(Elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (const MDString *MDS = dyn_cast<MDString>(V))
|
||||
EnumerateValue(V);
|
||||
}
|
||||
|
||||
void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
|
||||
|
||||
Reference in New Issue
Block a user