From aa7c94359cf7a8f5781e63f53934628a0fe672da Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Fri, 13 Feb 2015 01:14:11 +0000 Subject: [PATCH] AsmWriter/Bitcode: MDEnumerator git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229004 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Bitcode/LLVMBitCodes.h | 3 ++- include/llvm/IR/DebugInfoMetadata.h | 2 ++ lib/AsmParser/LLParser.cpp | 12 +++++++++++- lib/Bitcode/Reader/BitcodeReader.cpp | 10 ++++++++++ lib/Bitcode/Writer/BitcodeWriter.cpp | 15 +++++++++++---- lib/IR/AsmWriter.cpp | 9 +++++++-- test/Assembler/debug-info.ll | 11 +++++++++-- .../invalid-mdenumerator-missing-name.ll | 4 ++++ .../invalid-mdenumerator-missing-value.ll | 4 ++++ 9 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 test/Assembler/invalid-mdenumerator-missing-name.ll create mode 100644 test/Assembler/invalid-mdenumerator-missing-value.ll diff --git a/include/llvm/Bitcode/LLVMBitCodes.h b/include/llvm/Bitcode/LLVMBitCodes.h index 4ccf8943d6d..ac0545fe400 100644 --- a/include/llvm/Bitcode/LLVMBitCodes.h +++ b/include/llvm/Bitcode/LLVMBitCodes.h @@ -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 diff --git a/include/llvm/IR/DebugInfoMetadata.h b/include/llvm/IR/DebugInfoMetadata.h index f4a7ae9b79e..c2f8785f82b 100644 --- a/include/llvm/IR/DebugInfoMetadata.h +++ b/include/llvm/IR/DebugInfoMetadata.h @@ -309,6 +309,8 @@ public: int64_t getValue() const { return Value; } StringRef getName() const { return getStringOperand(0); } + MDString *getRawName() const { return getOperandAs(0); } + static bool classof(const Metadata *MD) { return MD->getMetadataID() == MDEnumeratorKind; } diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 735cf494cb2..191b6f99557 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -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"); } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index f7815810401..f7f0db27285 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -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); diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 3d1e82d8f8c..dfbfc9c0bea 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -826,11 +826,18 @@ static void WriteMDSubrange(const MDSubrange *N, const ValueEnumerator &, Record.clear(); } -static void WriteMDEnumerator(const MDEnumerator *, const ValueEnumerator &, - BitstreamWriter &, SmallVectorImpl &, - unsigned) { - llvm_unreachable("write not implemented"); +static void WriteMDEnumerator(const MDEnumerator *N, const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl &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 &, unsigned) { diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index aeed2af7376..b728b22df99 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -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"); diff --git a/test/Assembler/debug-info.ll b/test/Assembler/debug-info.ll index e92606d2d80..8ab69544433 100644 --- a/test/Assembler/debug-info.ll +++ b/test/Assembler/debug-info.ll @@ -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) @@ -12,3 +12,10 @@ !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 index 00000000000..709c6a556e6 --- /dev/null +++ b/test/Assembler/invalid-mdenumerator-missing-name.ll @@ -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 index 00000000000..a8501680688 --- /dev/null +++ b/test/Assembler/invalid-mdenumerator-missing-value.ll @@ -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")