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

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

index 492d7c588b3d7d4424b473c3fd5bf4e21a8b201e..dff1c28591d72f1be1378bf579a22686ca78f24d 100644 (file)
@@ -162,7 +162,8 @@ namespace bitc {
     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_GLOBAL_VAR    = 27   // [distinct, ...]
+    METADATA_GLOBAL_VAR    = 27,  // [distinct, ...]
+    METADATA_LOCAL_VAR     = 28   // [distinct, ...]
   };
 
   // The constants block (CONSTANTS_BLOCK_ID) describes emission for each
index 9c557258a4aa75ff34a7d98d510ad53865fd51ec..48dbaeeb76ae68a4cb0e0a57824c5c77a82dca13 100644 (file)
@@ -3575,9 +3575,30 @@ bool LLParser::ParseMDGlobalVariable(MDNode *&Result, bool IsDistinct) {
   return false;
 }
 
+/// ParseMDLocalVariable:
+///   ::= !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !0, name: "foo",
+///                        file: !1, line: 7, type: !2, arg: 2, flags: 7,
+///                        inlinedAt: !3)
 bool LLParser::ParseMDLocalVariable(MDNode *&Result, bool IsDistinct) {
-  return TokError("unimplemented parser");
+#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
+  REQUIRED(tag, DwarfTagField, );                                              \
+  OPTIONAL(scope, MDField, );                                                  \
+  OPTIONAL(name, MDStringField, );                                             \
+  OPTIONAL(file, MDField, );                                                   \
+  OPTIONAL(line, LineField, );                                                 \
+  OPTIONAL(type, MDField, );                                                   \
+  OPTIONAL(arg, MDUnsignedField, (0, UINT8_MAX));                              \
+  OPTIONAL(flags, MDUnsignedField, (0, UINT32_MAX));                           \
+  OPTIONAL(inlinedAt, MDField, );
+  PARSE_MD_FIELDS();
+#undef VISIT_MD_FIELDS
+
+  Result = GET_OR_DISTINCT(
+      MDLocalVariable, (Context, tag.Val, scope.Val, name.Val, file.Val,
+                        line.Val, type.Val, arg.Val, flags.Val, inlinedAt.Val));
+  return false;
 }
+
 bool LLParser::ParseMDExpression(MDNode *&Result, bool IsDistinct) {
   return TokError("unimplemented parser");
 }
index 56855ebb81b4c171de7c8715967e433481fe384a..5dee2f906777967ade7b02595bd44de519ade0a9 100644 (file)
@@ -1539,6 +1539,19 @@ std::error_code BitcodeReader::ParseMetadata() {
           NextMDValueNo++);
       break;
     }
+    case bitc::METADATA_LOCAL_VAR: {
+      if (Record.size() != 10)
+        return Error("Invalid record");
+
+      MDValueList.AssignValue(
+          GET_OR_DISTINCT(MDLocalVariable, Record[0],
+                          (Context, Record[1], getMDOrNull(Record[2]),
+                           getMDString(Record[3]), getMDOrNull(Record[4]),
+                           Record[5], getMDOrNull(Record[6]), Record[7],
+                           Record[8], getMDOrNull(Record[9]))),
+          NextMDValueNo++);
+      break;
+    }
     case bitc::METADATA_STRING: {
       std::string String(Record.begin(), Record.end());
       llvm::UpgradeMDStringConstant(String);
index a86e26cc3624ad137c87a4d93e02fba2e78a644f..9feb4ffda7f01b7cb6288f2c4ca5b23e4c459ab6 100644 (file)
@@ -1072,11 +1072,26 @@ static void WriteMDGlobalVariable(const MDGlobalVariable *N,
   Record.clear();
 }
 
-static void WriteMDLocalVariable(const MDLocalVariable *,
-                                 const ValueEnumerator &, BitstreamWriter &,
-                                 SmallVectorImpl<uint64_t> &, unsigned) {
-  llvm_unreachable("write not implemented");
+static void WriteMDLocalVariable(const MDLocalVariable *N,
+                                 const ValueEnumerator &VE,
+                                 BitstreamWriter &Stream,
+                                 SmallVectorImpl<uint64_t> &Record,
+                                 unsigned Abbrev) {
+  Record.push_back(N->isDistinct());
+  Record.push_back(N->getTag());
+  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
+  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
+  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
+  Record.push_back(N->getLine());
+  Record.push_back(VE.getMetadataOrNullID(N->getType()));
+  Record.push_back(N->getArg());
+  Record.push_back(N->getFlags());
+  Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
+
+  Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
+  Record.clear();
 }
+
 static void WriteMDExpression(const MDExpression *, const ValueEnumerator &,
                               BitstreamWriter &, SmallVectorImpl<uint64_t> &,
                               unsigned) {
index b00d52ca7444593058d25be864e5892c7ecb881c..ae9a50c94da17509eb89fa50de6413e638df2ec4 100644 (file)
@@ -1748,11 +1748,39 @@ static void writeMDGlobalVariable(raw_ostream &Out, const MDGlobalVariable *N,
   Out << ")";
 }
 
-static void writeMDLocalVariable(raw_ostream &, const MDLocalVariable *,
-                                 TypePrinting *, SlotTracker *,
-                                 const Module *) {
-  llvm_unreachable("write not implemented");
+static void writeMDLocalVariable(raw_ostream &Out, const MDLocalVariable *N,
+                                 TypePrinting *TypePrinter,
+                                 SlotTracker *Machine, const Module *Context) {
+  Out << "!MDLocalVariable(";
+  FieldSeparator FS;
+  writeTag(Out, FS, N);
+  Out << FS << "scope: ";
+  writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context);
+  Out << FS << "name: \"" << N->getName() << "\"";
+  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);
+  }
+  if (N->getTag() == dwarf::DW_TAG_arg_variable || N->getArg())
+    Out << FS << "arg: " << N->getArg();
+  if (N->getFlags())
+    Out << FS << "flags: " << N->getFlags();
+  if (N->getInlinedAt()) {
+    Out << FS << "inlinedAt: ";
+    writeMetadataAsOperand(Out, N->getInlinedAt(), TypePrinter, Machine,
+                           Context);
+  }
+  Out << ")";
 }
+
 static void writeMDExpression(raw_ostream &, const MDExpression *,
                               TypePrinting *, SlotTracker *, const Module *) {
   llvm_unreachable("write not implemented");
index 46ac98a7d0cf419a1026cb2b15cba8bb55406794..3d6ebbce1b4efa5148e300956f34979e0092151a 100644 (file)
@@ -336,6 +336,12 @@ MDLocalVariable *MDLocalVariable::getImpl(
     LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
     Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
     Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
+  // Truncate Arg to 8 bits.
+  //
+  // FIXME: This is gross (and should be changed to an assert or removed), but
+  // it matches historical behaviour for now.
+  Arg &= (1u << 8) - 1;
+
   assert(isCanonical(Name) && "Expected canonical MDString");
   DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
                                           Line, Type, Arg, Flags, InlinedAt));
diff --git a/test/Assembler/invalid-mdlocalvariable-missing-name.ll b/test/Assembler/invalid-mdlocalvariable-missing-name.ll
new file mode 100644 (file)
index 0000000..5b23600
--- /dev/null
@@ -0,0 +1,4 @@
+; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
+
+; CHECK: <stdin>:[[@LINE+1]]:29: error: missing required field 'tag'
+!0 = !MDLocalVariable(arg: 7)
diff --git a/test/Assembler/mdlocalvariable.ll b/test/Assembler/mdlocalvariable.ll
new file mode 100644 (file)
index 0000000..6a7e530
--- /dev/null
@@ -0,0 +1,26 @@
+; 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, !7, !8}
+!named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
+
+!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 = !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !0, name: "foo", file: !1, line: 7, type: !3, arg: 3, flags: 8, inlinedAt: !4)
+; CHECK: !6 = !MDLocalVariable(tag: DW_TAG_auto_variable, scope: !0, name: "foo", file: !1, line: 7, type: !3, flags: 8, inlinedAt: !4)
+!5 = !MDLocalVariable(tag: DW_TAG_arg_variable, scope: !0, name: "foo",
+                      file: !1, line: 7, type: !3, arg: 3,
+                      flags: 8, inlinedAt: !4)
+!6 = !MDLocalVariable(tag: DW_TAG_auto_variable, scope: !0, name: "foo",
+                      file: !1, line: 7, type: !3, flags: 8, inlinedAt: !4)
+
+; CHECK: !7 = !MDLocalVariable(tag: DW_TAG_arg_variable, scope: null, name: "", arg: 0)
+; CHECK: !8 = !MDLocalVariable(tag: DW_TAG_auto_variable, scope: null, name: "")
+!7 = !MDLocalVariable(tag: DW_TAG_arg_variable)
+!8 = !MDLocalVariable(tag: DW_TAG_auto_variable)