Skip to content

Commit

Permalink
AsmParser: Recognize DW_TAG_* constants
Browse files Browse the repository at this point in the history
Recognize `DW_TAG_` constants in assembly, and output it by default for
`GenericDebugNode`.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228042 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dexonsmith committed Feb 3, 2015
1 parent 6adbfa3 commit 1602e58
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/AsmParser/LLLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,12 @@ lltok::Kind LLLexer::LexIdentifier() {
INSTKEYWORD(landingpad, LandingPad);
#undef INSTKEYWORD

if (Len >= strlen("DW_TAG_") &&
!memcmp(StartChar, "DW_TAG_", strlen("DW_TAG_"))) {
StrVal.assign(StartChar, CurPtr);
return lltok::DwarfTag;
}

// Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
// the CFE to avoid forcing it to deal with 64-bit numbers.
if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
Expand Down
25 changes: 24 additions & 1 deletion lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -2936,6 +2937,28 @@ bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
return false;
}

bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
if (Lex.getKind() == lltok::APSInt)
return ParseMDField(Loc, Name,
static_cast<MDUnsignedField<uint32_t> &>(Result));

if (Result.Seen)
return Error(Loc,
"field '" + Name + "' cannot be specified more than once");

if (Lex.getKind() != lltok::DwarfTag)
return TokError("expected DWARF tag");

unsigned Tag = dwarf::getTag(Lex.getStrVal());
if (Tag == dwarf::DW_TAG_invalid)
return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
assert(Tag < 1u << 16 && "Expected valid DWARF tag");

Result.assign(Tag);
Lex.Lex();
return false;
}

bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
Metadata *MD;
if (ParseMetadata(MD, nullptr))
Expand Down Expand Up @@ -3056,7 +3079,7 @@ bool LLParser::ParseMDLocation(MDNode *&Result, bool IsDistinct) {
/// ::= !GenericDebugNode(tag: 15, header: "...", operands: {...})
bool LLParser::ParseGenericDebugNode(MDNode *&Result, bool IsDistinct) {
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
REQUIRED(tag, MDUnsignedField<uint32_t>, (0, ~0u >> 16)); \
REQUIRED(tag, DwarfTagField, ); \
OPTIONAL(header, MDStringField, ); \
OPTIONAL(operands, MDFieldList, );
PARSE_MD_FIELDS();
Expand Down
4 changes: 4 additions & 0 deletions lib/AsmParser/LLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ namespace llvm {
NumTy Max = std::numeric_limits<NumTy>::max())
: ImplTy(Default), Max(Max) {}
};
struct DwarfTagField : public MDUnsignedField<uint32_t> {
DwarfTagField() : MDUnsignedField<uint32_t>(0, ~0u >> 16) {}
};
struct MDField : public MDFieldImpl<Metadata *> {
MDField() : ImplTy(nullptr) {}
};
Expand Down Expand Up @@ -427,6 +430,7 @@ namespace llvm {

bool ParseMDField(LocTy Loc, StringRef Name,
MDUnsignedField<uint32_t> &Result);
bool ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result);
bool ParseMDField(LocTy Loc, StringRef Name, MDField &Result);
bool ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result);
bool ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result);
Expand Down
1 change: 1 addition & 0 deletions lib/AsmParser/LLToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ namespace lltok {
LocalVar, // %foo %"foo"
MetadataVar, // !foo
StringConstant, // "foo"
DwarfTag, // DW_TAG_foo (includes "DW_TAG_")

// Type valued tokens (TyVal).
Type,
Expand Down
7 changes: 5 additions & 2 deletions lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1291,8 +1291,11 @@ static void writeGenericDebugNode(raw_ostream &Out, const GenericDebugNode *N,
SlotTracker *Machine, const Module *Context) {
Out << "!GenericDebugNode(";
FieldSeparator FS;
// Always output the line, since 0 is a relevant and important value for it.
Out << FS << "tag: " << N->getTag();
Out << FS << "tag: ";
if (const char *Tag = dwarf::TagString(N->getTag()))
Out << Tag;
else
Out << N->getTag();
if (!N->getHeader().empty()) {
Out << FS << "header: \"";
PrintEscapedString(N->getHeader(), Out);
Expand Down
13 changes: 8 additions & 5 deletions test/Assembler/generic-debug-node.ll
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
; RUN: llvm-as < %s | llvm-dis | FileCheck %s
; RUN: verify-uselistorder %s

; CHECK: !named = !{!0, !1, !1, !2, !2, !2, !2, !3, !4}
!named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
; CHECK: !named = !{!0, !1, !1, !2, !2, !2, !2, !3, !4, !2}
!named = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}

; CHECK: !0 = !{}
!0 = !{}

; CHECK-NEXT: !1 = !GenericDebugNode(tag: 3, header: "some\00header", operands: {!0, !2, !2})
; CHECK-NEXT: !1 = !GenericDebugNode(tag: DW_TAG_entry_point, header: "some\00header", operands: {!0, !2, !2})
!1 = !GenericDebugNode(tag: 3, header: "some\00header", operands: {!0, !3, !4})
!2 = !GenericDebugNode(tag: 3, header: "some\00header", operands: {!{}, !3, !4})

; CHECK-NEXT: !2 = !GenericDebugNode(tag: 3)
; CHECK-NEXT: !2 = !GenericDebugNode(tag: DW_TAG_entry_point)
!3 = !GenericDebugNode(tag: 3)
!4 = !GenericDebugNode(tag: 3, header: "")
!5 = !GenericDebugNode(tag: 3, operands: {})
!6 = !GenericDebugNode(tag: 3, header: "", operands: {})

; CHECK-NEXT: !3 = distinct !GenericDebugNode(tag: 3)
; CHECK-NEXT: !3 = distinct !GenericDebugNode(tag: DW_TAG_entry_point)
!7 = distinct !GenericDebugNode(tag: 3)

; CHECK-NEXT: !4 = !GenericDebugNode(tag: 65535)
!8 = !GenericDebugNode(tag: 65535)

; CHECK-NOT: !
!9 = !GenericDebugNode(tag: DW_TAG_entry_point)
4 changes: 4 additions & 0 deletions test/Assembler/invalid-generic-debug-node-tag-bad.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s

; CHECK: <stdin>:[[@LINE+1]]:29: error: invalid DWARF tag 'DW_TAG_badtag'
!0 = !GenericDebugNode(tag: DW_TAG_badtag)
4 changes: 4 additions & 0 deletions test/Assembler/invalid-generic-debug-node-tag-wrong-type.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s

; CHECK: <stdin>:[[@LINE+1]]:29: error: expected DWARF tag
!0 = !GenericDebugNode(tag: "string")
1 change: 1 addition & 0 deletions utils/vim/llvm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ syn match llvmIdentifier /[%@][-a-zA-Z$._][-a-zA-Z$._0-9]*/
syn match llvmIdentifier /![-a-zA-Z$._][-a-zA-Z$._0-9]*\ze\s*$/
syn match llvmIdentifier /![-a-zA-Z$._][-a-zA-Z$._0-9]*\ze\s*[=!]/
syn match llvmType /!\zs\a\+\ze\s*(/
syn match llvmConstant /\<DW_TAG_[a-z_]\+\>/

" Syntax-highlight dejagnu test commands.
syn match llvmSpecialComment /;\s*RUN:.*$/
Expand Down

0 comments on commit 1602e58

Please sign in to comment.