From: Duncan P. N. Exon Smith Date: Fri, 13 Feb 2015 01:35:40 +0000 (+0000) Subject: AsmWriter/Bitcode: MDGlobalVariable X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=fbc547da8195481feeed745f7179c593db304602;hp=8921bbca59c0ab2bc63988558a38c9546c638755;p=oota-llvm.git AsmWriter/Bitcode: MDGlobalVariable git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229020 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Bitcode/LLVMBitCodes.h b/include/llvm/Bitcode/LLVMBitCodes.h index 64d4bf98a5f..492d7c588b3 100644 --- a/include/llvm/Bitcode/LLVMBitCodes.h +++ b/include/llvm/Bitcode/LLVMBitCodes.h @@ -161,7 +161,8 @@ namespace bitc { METADATA_LEXICAL_BLOCK_FILE=23,//[distinct, scope, file, discriminator] METADATA_NAMESPACE = 24, // [distinct, scope, file, name, line] METADATA_TEMPLATE_TYPE = 25, // [distinct, scope, name, type, ...] - METADATA_TEMPLATE_VALUE= 26 // [distinct, scope, name, type, value, ...] + METADATA_TEMPLATE_VALUE= 26, // [distinct, scope, name, type, value, ...] + METADATA_GLOBAL_VAR = 27 // [distinct, ...] }; // 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 071f2706eb8..c9b0da9a26d 100644 --- a/include/llvm/IR/DebugInfoMetadata.h +++ b/include/llvm/IR/DebugInfoMetadata.h @@ -1243,6 +1243,8 @@ public: Metadata *getFile() const { return getOperand(2); } Metadata *getType() const { return getOperand(3); } + MDString *getRawName() const { return getOperandAs(1); } + static bool classof(const Metadata *MD) { return MD->getMetadataID() == MDLocalVariableKind || MD->getMetadataID() == MDGlobalVariableKind; @@ -1315,6 +1317,8 @@ public: Metadata *getVariable() const { return getOperand(6); } Metadata *getStaticDataMemberDeclaration() const { return getOperand(7); } + MDString *getRawLinkageName() const { return getOperandAs(5); } + static bool classof(const Metadata *MD) { return MD->getMetadataID() == MDGlobalVariableKind; } diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 59e3c0ec8cf..9c557258a4a 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -3548,9 +3548,33 @@ bool LLParser::ParseMDTemplateValueParameter(MDNode *&Result, bool IsDistinct) { return false; } +/// ParseMDGlobalVariable: +/// ::= !MDGlobalVariable(scope: !0, name: "foo", linkageName: "foo", +/// file: !1, line: 7, type: !2, isLocal: false, +/// isDefinition: true, variable: i32* @foo, +/// declaration: !3) bool LLParser::ParseMDGlobalVariable(MDNode *&Result, bool IsDistinct) { - return TokError("unimplemented parser"); +#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ + OPTIONAL(scope, MDField, ); \ + REQUIRED(name, MDStringField, ); \ + OPTIONAL(linkageName, MDStringField, ); \ + OPTIONAL(file, MDField, ); \ + OPTIONAL(line, LineField, ); \ + OPTIONAL(type, MDField, ); \ + OPTIONAL(isLocal, MDBoolField, ); \ + OPTIONAL(isDefinition, MDBoolField, (true)); \ + OPTIONAL(variable, MDConstant, ); \ + OPTIONAL(declaration, MDField, ); + PARSE_MD_FIELDS(); +#undef VISIT_MD_FIELDS + + Result = GET_OR_DISTINCT(MDGlobalVariable, + (Context, scope.Val, name.Val, linkageName.Val, + file.Val, line.Val, type.Val, isLocal.Val, + isDefinition.Val, variable.Val, declaration.Val)); + return false; } + bool LLParser::ParseMDLocalVariable(MDNode *&Result, bool IsDistinct) { return TokError("unimplemented parser"); } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 9731e8c0e03..56855ebb81b 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1525,6 +1525,20 @@ std::error_code BitcodeReader::ParseMetadata() { NextMDValueNo++); break; } + case bitc::METADATA_GLOBAL_VAR: { + if (Record.size() != 11) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDGlobalVariable, Record[0], + (Context, getMDOrNull(Record[1]), + getMDString(Record[2]), getMDString(Record[3]), + getMDOrNull(Record[4]), Record[5], + getMDOrNull(Record[6]), Record[7], Record[8], + getMDOrNull(Record[9]), getMDOrNull(Record[10]))), + 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 b1b61e717e6..a86e26cc362 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -1051,11 +1051,27 @@ static void WriteMDTemplateValueParameter(const MDTemplateValueParameter *N, Record.clear(); } -static void WriteMDGlobalVariable(const MDGlobalVariable *, - const ValueEnumerator &, BitstreamWriter &, - SmallVectorImpl &, unsigned) { - llvm_unreachable("write not implemented"); +static void WriteMDGlobalVariable(const MDGlobalVariable *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getType())); + Record.push_back(N->isLocalToUnit()); + Record.push_back(N->isDefinition()); + Record.push_back(VE.getMetadataOrNullID(N->getVariable())); + Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration())); + + Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev); + Record.clear(); } + static void WriteMDLocalVariable(const MDLocalVariable *, const ValueEnumerator &, BitstreamWriter &, SmallVectorImpl &, unsigned) { diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index 4e7bc8ded0c..b00d52ca744 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -1711,11 +1711,43 @@ static void writeMDTemplateValueParameter(raw_ostream &Out, Out << ")"; } -static void writeMDGlobalVariable(raw_ostream &, const MDGlobalVariable *, - TypePrinting *, SlotTracker *, - const Module *) { - llvm_unreachable("write not implemented"); +static void writeMDGlobalVariable(raw_ostream &Out, const MDGlobalVariable *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context) { + Out << "!MDGlobalVariable("; + FieldSeparator FS; + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + Out << FS << "name: \"" << N->getName() << "\""; + if (!N->getLinkageName().empty()) + Out << FS << "linkageName: \"" << N->getLinkageName() << "\""; + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getType()) { + Out << FS << "type: "; + writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, + Context); + } + Out << FS << "isLocal: " << (N->isLocalToUnit() ? "true" : "false"); + Out << FS << "isDefinition: " << (N->isDefinition() ? "true" : "false"); + if (N->getVariable()) { + Out << FS << "variable: "; + writeMetadataAsOperand(Out, N->getVariable(), TypePrinter, Machine, + Context); + } + if (N->getStaticDataMemberDeclaration()) { + Out << FS << "declaration: "; + writeMetadataAsOperand(Out, N->getStaticDataMemberDeclaration(), + TypePrinter, Machine, Context); + } + Out << ")"; } + static void writeMDLocalVariable(raw_ostream &, const MDLocalVariable *, TypePrinting *, SlotTracker *, const Module *) { diff --git a/test/Assembler/invalid-mdglobalvariable-missing-name.ll b/test/Assembler/invalid-mdglobalvariable-missing-name.ll new file mode 100644 index 00000000000..bc0f72434a5 --- /dev/null +++ b/test/Assembler/invalid-mdglobalvariable-missing-name.ll @@ -0,0 +1,4 @@ +; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s + +; CHECK: :[[@LINE+1]]:42: error: missing required field 'name' +!0 = !MDGlobalVariable(linkageName: "foo") diff --git a/test/Assembler/mdglobalvariable.ll b/test/Assembler/mdglobalvariable.ll new file mode 100644 index 00000000000..f52eaa710ed --- /dev/null +++ b/test/Assembler/mdglobalvariable.ll @@ -0,0 +1,22 @@ +; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s +; RUN: verify-uselistorder %s + +@foo = global i32 0 + +; CHECK: !named = !{!0, !1, !2, !3, !4, !5, !6} +!named = !{!0, !1, !2, !3, !4, !5, !6} + +!0 = distinct !{} +!1 = !{!"path/to/file", !"/path/to/dir"} +!2 = !MDFile(filename: "path/to/file", directory: "/path/to/dir") +!3 = distinct !{} +!4 = distinct !{} + +; CHECK: !5 = !MDGlobalVariable(scope: !0, name: "foo", linkageName: "foo", file: !1, line: 7, type: !3, isLocal: true, isDefinition: false, variable: i32* @foo, declaration: !4) +!5 = !MDGlobalVariable(scope: !0, name: "foo", linkageName: "foo", + file: !1, line: 7, type: !3, isLocal: true, + isDefinition: false, variable: i32* @foo, + declaration: !4) + +; CHECK: !6 = !MDGlobalVariable(scope: null, name: "bar", isLocal: false, isDefinition: true) +!6 = !MDGlobalVariable(name: "bar")