[PATCH] D68483: [IA] Recognize hexadecimal escape sequences

Bill Wendling via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 4 13:43:30 PDT 2019


void created this revision.
void added a reviewer: nickdesaulniers.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D68483

Files:
  llvm/lib/MC/MCParser/AsmParser.cpp
  llvm/test/MC/AsmParser/directive_ascii.s


Index: llvm/test/MC/AsmParser/directive_ascii.s
===================================================================
--- llvm/test/MC/AsmParser/directive_ascii.s
+++ llvm/test/MC/AsmParser/directive_ascii.s
@@ -39,3 +39,8 @@
 # CHECK: .byte 0
 TEST6:
         .string "B", "C"
+
+# CHECK: TEST7:
+# CHECK: .byte 100
+TEST7:
+        .ascii "\x64"
Index: llvm/lib/MC/MCParser/AsmParser.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmParser.cpp
+++ llvm/lib/MC/MCParser/AsmParser.cpp
@@ -2914,11 +2914,26 @@
     }
 
     // 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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68483.223283.patch
Type: text/x-patch
Size: 1521 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191004/62cee369/attachment.bin>


More information about the llvm-commits mailing list