Read and write NamedMDNode.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77517 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel 2009-07-29 22:34:41 +00:00
parent debcb01b0f
commit aa99314251
5 changed files with 78 additions and 3 deletions

View File

@ -108,8 +108,10 @@ namespace bitc {
};
enum MetadataCodes {
METADATA_STRING = 1, // MDString: [values]
METADATA_NODE = 2 // MDNODE: [n x (type num, value num)]
METADATA_STRING = 1, // MDSTRING: [values]
METADATA_NODE = 2, // MDNODE: [n x (type num, value num)]
METADATA_NAME = 3, // STRING: [values]
METADATA_NAMED_NODE = 4 // NAMEDMDNODE: [n x mdnodes]
};
// The constants block (CONSTANTS_BLOCK_ID) describes emission for each
// constant and maintains an implicit current type value.

View File

@ -734,6 +734,33 @@ bool BitcodeReader::ParseMetadata() {
switch (Stream.ReadRecord(Code, Record)) {
default: // Default behavior: ignore.
break;
case bitc::METADATA_NAME: {
// Read named of the named metadata.
unsigned NameLength = Record.size();
SmallString<8> Name;
Name.resize(NameLength);
for (unsigned i = 0; i != NameLength; ++i)
Name[i] = Record[i];
Record.clear();
Code = Stream.ReadCode();
// METADATA_NAME is always followed by METADATA_NAMED_NODE.
if (Stream.ReadRecord(Code, Record) != bitc::METADATA_NAMED_NODE)
assert ( 0 && "Inavlid Named Metadata record");
// Read named metadata elements.
unsigned Size = Record.size();
SmallVector<MetadataBase*, 8> Elts;
for (unsigned i = 0; i != Size; ++i) {
Value *MD = ValueList.getValueFwdRef(Record[i], Type::MetadataTy);
if (MetadataBase *B = dyn_cast<MetadataBase>(MD))
Elts.push_back(B);
}
Value *V = NamedMDNode::Create(Name.c_str(), Elts.data(), Elts.size(),
TheModule);
ValueList.AssignValue(V, NextValueNo++);
break;
}
case bitc::METADATA_NODE: {
if (Record.empty() || Record.size() % 2 == 1)
return Error("Invalid METADATA_NODE record");

View File

@ -495,6 +495,7 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
const ValueEnumerator::ValueList &Vals = VE.getValues();
bool StartedMetadataBlock = false;
unsigned MDSAbbrev = 0;
unsigned String8Abbrev = 0;
SmallVector<uint64_t, 64> Record;
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
@ -525,6 +526,33 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
// Emit the finished record.
Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
Record.clear();
} else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(Vals[i].first)) {
if (!StartedMetadataBlock) {
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
StartedMetadataBlock = true;
BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
String8Abbrev = Stream.EmitAbbrev(Abbv);
}
// Write name.
const char *StrBegin = NMD->getName().data();
for (unsigned i = 0, e = NMD->getName().size(); i != e; ++i)
Record.push_back(StrBegin[i]);
Stream.EmitRecord(bitc::METADATA_NAME, Record, String8Abbrev);
Record.clear();
// Write named metadata elements.
for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
if (NMD->getElement(i))
Record.push_back(VE.getValueID(NMD->getElement(i)));
else
Record.push_back(0);
}
Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Record.clear();
}
}
@ -578,7 +606,7 @@ 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) || isa<MDNode>(V))
if (isa<MetadataBase>(V))
continue;
// If we need to switch types, do so now.
if (V->getType() != LastTy) {

View File

@ -221,6 +221,18 @@ void ValueEnumerator::EnumerateValue(const Value *V) {
return;
}
if (const NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
Values.push_back(std::make_pair(V, 1U));
ValueMap[V] = Values.size();
ValueID = Values.size();
for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
E = N->elem_end(); I != E; ++I) {
MetadataBase *M = *I;
EnumerateValue(M);
}
return;
}
// Add the value.
Values.push_back(std::make_pair(V, 1U));
ValueID = Values.size();

View File

@ -0,0 +1,6 @@
; RUN: llvm-as < %s | llvm-dis | grep "llvm.stuff = "
;; Simple NamedMDNode
!0 = metadata !{i32 42}
!1 = metadata !{metadata !"foo"}
!llvm.stuff = !{!0, !1}