Skip to content

Commit 6942327

Browse files
committed
[IA] Recognize hexadecimal escape sequences
Summary: Implement support for hexadecimal escape sequences to match how GNU 'as' handles them. I.e., read all hexadecimal characters and truncate to the lower 16 bits. Reviewers: nickdesaulniers Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68483 llvm-svn: 373888
1 parent 32b47dd commit 6942327

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2914,11 +2914,26 @@ bool AsmParser::parseEscapedString(std::string &Data) {
29142914
}
29152915

29162916
// Recognize escaped characters. Note that this escape semantics currently
2917-
// loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2917+
// loosely follows Darwin 'as'.
29182918
++i;
29192919
if (i == e)
29202920
return TokError("unexpected backslash at end of string");
29212921

2922+
// Recognize hex sequences similarly to GNU 'as'.
2923+
if (Str[i] == 'x' || Str[i] == 'X') {
2924+
if (!isHexDigit(Str[i + 1]))
2925+
return TokError("invalid hexadecimal escape sequence");
2926+
2927+
// Consume hex characters. GNU 'as' reads all hexadecimal characters and
2928+
// then truncates to the lower 16 bits. Seems reasonable.
2929+
unsigned Value = 0;
2930+
while (isHexDigit(Str[i + 1]))
2931+
Value = Value * 16 + hexDigitValue(Str[++i]);
2932+
2933+
Data += (unsigned char)(Value & 0xFF);
2934+
continue;
2935+
}
2936+
29222937
// Recognize octal sequences.
29232938
if ((unsigned)(Str[i] - '0') <= 7) {
29242939
// Consume up to three octal characters.

llvm/test/MC/AsmParser/directive_ascii.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ TEST5:
3939
# CHECK: .byte 0
4040
TEST6:
4141
.string "B", "C"
42+
43+
# CHECK: TEST7:
44+
# CHECK: .ascii "dk"
45+
TEST7:
46+
.ascii "\x64\Xa6B"

0 commit comments

Comments
 (0)