AsmWriter/Bitcode: MDEnumerator
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Fri, 13 Feb 2015 01:14:11 +0000 (01:14 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Fri, 13 Feb 2015 01:14:11 +0000 (01:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229004 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Bitcode/LLVMBitCodes.h
include/llvm/IR/DebugInfoMetadata.h
lib/AsmParser/LLParser.cpp
lib/Bitcode/Reader/BitcodeReader.cpp
lib/Bitcode/Writer/BitcodeWriter.cpp
lib/IR/AsmWriter.cpp
test/Assembler/debug-info.ll
test/Assembler/invalid-mdenumerator-missing-name.ll [new file with mode: 0644]
test/Assembler/invalid-mdenumerator-missing-value.ll [new file with mode: 0644]

index 4ccf894..ac0545f 100644 (file)
@@ -148,7 +148,8 @@ namespace bitc {
     METADATA_NAMED_NODE    = 10,  // NAMED_NODE:    [n x mdnodes]
     METADATA_ATTACHMENT    = 11,  // [m x [value, [n x [id, mdnode]]]
     METADATA_GENERIC_DEBUG = 12,  // [distinct, tag, vers, header, n x md num]
-    METADATA_SUBRANGE      = 13   // [distinct, count, lo]
+    METADATA_SUBRANGE      = 13,  // [distinct, count, lo]
+    METADATA_ENUMERATOR    = 14   // [distinct, value, name?]
   };
 
   // The constants block (CONSTANTS_BLOCK_ID) describes emission for each
index f4a7ae9..c2f8785 100644 (file)
@@ -309,6 +309,8 @@ public:
   int64_t getValue() const { return Value; }
   StringRef getName() const { return getStringOperand(0); }
 
+  MDString *getRawName() const { return getOperandAs<MDString>(0); }
+
   static bool classof(const Metadata *MD) {
     return MD->getMetadataID() == MDEnumeratorKind;
   }
index 735cf49..191b6f9 100644 (file)
@@ -3181,9 +3181,19 @@ bool LLParser::ParseMDSubrange(MDNode *&Result, bool IsDistinct) {
   return false;
 }
 
+/// ParseMDEnumerator:
+///   ::= !MDEnumerator(value: 30, name: "SomeKind")
 bool LLParser::ParseMDEnumerator(MDNode *&Result, bool IsDistinct) {
-  return TokError("unimplemented parser");
+#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
+  REQUIRED(value, MDSignedField, );                                            \
+  REQUIRED(name, MDStringField, );
+  PARSE_MD_FIELDS();
+#undef VISIT_MD_FIELDS
+
+  Result = GET_OR_DISTINCT(MDEnumerator, (Context, value.Val, name.Val));
+  return false;
 }
+
 bool LLParser::ParseMDBasicType(MDNode *&Result, bool IsDistinct) {
   return TokError("unimplemented parser");
 }
index f781581..f7f0db2 100644 (file)
@@ -1361,6 +1361,16 @@ std::error_code BitcodeReader::ParseMetadata() {
           NextMDValueNo++);
       break;
     }
+    case bitc::METADATA_ENUMERATOR: {
+      if (Record.size() != 3)
+        return Error("Invalid record");
+
+      MDValueList.AssignValue(GET_OR_DISTINCT(MDEnumerator, Record[0],
+                                              (Context, unrotateSign(Record[1]),
+                                               getMDString(Record[2]))),
+                              NextMDValueNo++);
+      break;
+    }
     case bitc::METADATA_STRING: {
       std::string String(Record.begin(), Record.end());
       llvm::UpgradeMDStringConstant(String);
index 3d1e82d..dfbfc9c 100644 (file)
@@ -826,11 +826,18 @@ static void WriteMDSubrange(const MDSubrange *N, const ValueEnumerator &,
   Record.clear();
 }
 
-static void WriteMDEnumerator(const MDEnumerator *, const ValueEnumerator &,
-                              BitstreamWriter &, SmallVectorImpl<uint64_t> &,
-                              unsigned) {
-  llvm_unreachable("write not implemented");
+static void WriteMDEnumerator(const MDEnumerator *N, const ValueEnumerator &VE,
+                              BitstreamWriter &Stream,
+                              SmallVectorImpl<uint64_t> &Record,
+                              unsigned Abbrev) {
+  Record.push_back(N->isDistinct());
+  Record.push_back(rotateSign(N->getValue()));
+  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
+
+  Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
+  Record.clear();
 }
+
 static void WriteMDBasicType(const MDBasicType *, const ValueEnumerator &,
                              BitstreamWriter &, SmallVectorImpl<uint64_t> &,
                              unsigned) {
index aeed2af..b728b22 100644 (file)
@@ -1357,10 +1357,15 @@ static void writeMDSubrange(raw_ostream &Out, const MDSubrange *N,
   Out << ")";
 }
 
-static void writeMDEnumerator(raw_ostream &, const MDEnumerator *,
+static void writeMDEnumerator(raw_ostream &Out, const MDEnumerator *N,
                               TypePrinting *, SlotTracker *, const Module *) {
-  llvm_unreachable("write not implemented");
+  Out << "!MDEnumerator(";
+  FieldSeparator FS;
+  Out << FS << "value: " << N->getValue();
+  Out << FS << "name: \"" << N->getName() << "\"";
+  Out << ")";
 }
+
 static void writeMDBasicType(raw_ostream &, const MDBasicType *, TypePrinting *,
                              SlotTracker *, const Module *) {
   llvm_unreachable("write not implemented");
index e92606d..8ab6954 100644 (file)
@@ -1,8 +1,8 @@
 ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
 ; RUN: verify-uselistorder %s
 
-; CHECK: !named = !{!0, !0, !1, !2}
-!named = !{!0, !1, !2, !3}
+; CHECK: !named = !{!0, !0, !1, !2, !3, !4, !5}
+!named = !{!0, !1, !2, !3, !4, !5, !6}
 
 ; CHECK:      !0 = !MDSubrange(count: 3)
 ; CHECK-NEXT: !1 = !MDSubrange(count: 3, lowerBound: 4)
 
 !2 = !MDSubrange(count: 3, lowerBound: 4)
 !3 = !MDSubrange(count: 3, lowerBound: -5)
+
+; CHECK-NEXT: !3 = !MDEnumerator(value: 7, name: "seven")
+; CHECK-NEXT: !4 = !MDEnumerator(value: -8, name: "negeight")
+; CHECK-NEXT: !5 = !MDEnumerator(value: 0, name: "")
+!4 = !MDEnumerator(value: 7, name: "seven")
+!5 = !MDEnumerator(value: -8, name: "negeight")
+!6 = !MDEnumerator(value: 0, name: "")
diff --git a/test/Assembler/invalid-mdenumerator-missing-name.ll b/test/Assembler/invalid-mdenumerator-missing-name.ll
new file mode 100644 (file)
index 0000000..709c6a5
--- /dev/null
@@ -0,0 +1,4 @@
+; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
+
+; CHECK: [[@LINE+1]]:28: error: missing required field 'name'
+!0 = !MDEnumerator(value: 7)
diff --git a/test/Assembler/invalid-mdenumerator-missing-value.ll b/test/Assembler/invalid-mdenumerator-missing-value.ll
new file mode 100644 (file)
index 0000000..a850168
--- /dev/null
@@ -0,0 +1,4 @@
+; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
+
+; CHECK: [[@LINE+1]]:32: error: missing required field 'value'
+!0 = !MDEnumerator(name: "name")