]> granicus.if.org Git - llvm/commitdiff
[IA] Recognize hexadecimal escape sequences
authorBill Wendling <isanbard@gmail.com>
Mon, 7 Oct 2019 09:54:53 +0000 (09:54 +0000)
committerBill Wendling <isanbard@gmail.com>
Mon, 7 Oct 2019 09:54:53 +0000 (09:54 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373888 91177308-0d34-0410-b5e6-96231b3b80d8

lib/MC/MCParser/AsmParser.cpp
test/MC/AsmParser/directive_ascii.s

index 381bf96416166e69c266a7eb712633383d266f94..b25959b102d6d4256daa5dbfa3042ec8989257b1 100644 (file)
@@ -2914,11 +2914,26 @@ bool AsmParser::parseEscapedString(std::string &Data) {
     }
 
     // Recognize escaped characters. Note that this escape semantics currently
-    // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
+    // loosely follows Darwin 'as'.
     ++i;
     if (i == e)
       return TokError("unexpected backslash at end of string");
 
+    // Recognize hex sequences similarly to GNU 'as'.
+    if (Str[i] == 'x' || Str[i] == 'X') {
+      if (!isHexDigit(Str[i + 1]))
+        return TokError("invalid hexadecimal escape sequence");
+
+      // Consume hex characters. GNU 'as' reads all hexadecimal characters and
+      // then truncates to the lower 16 bits. Seems reasonable.
+      unsigned Value = 0;
+      while (isHexDigit(Str[i + 1]))
+        Value = Value * 16 + hexDigitValue(Str[++i]);
+
+      Data += (unsigned char)(Value & 0xFF);
+      continue;
+    }
+
     // Recognize octal sequences.
     if ((unsigned)(Str[i] - '0') <= 7) {
       // Consume up to three octal characters.
index a7ba7bbd5da132453572fad707c6a5dece406cf6..604f9721bcca979e59826e741b88dbe5b21d0876 100644 (file)
@@ -39,3 +39,8 @@ TEST5:
 # CHECK: .byte 0
 TEST6:
         .string "B", "C"
+
+# CHECK: TEST7:
+# CHECK: .ascii "dk"
+TEST7:
+        .ascii "\x64\Xa6B"