[PATCH] D22112: Disambiguate a constant with both 0B prefix and H suffix.

Yunzhong Gao via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 7 14:29:02 PDT 2016


ygao created this revision.
ygao added reviewers: sidneym, colinl.
ygao added a subscriber: llvm-commits.

Fixing PR27884: allow llvm-mc to accept numbers like 0BEDh as a valid hexadecimal number.

Does this look like a reasonable approach?

http://reviews.llvm.org/D22112

Files:
  lib/MC/MCParser/AsmLexer.cpp
  test/MC/AsmParser/uppercase-hex.s

Index: test/MC/AsmParser/uppercase-hex.s
===================================================================
--- test/MC/AsmParser/uppercase-hex.s
+++ test/MC/AsmParser/uppercase-hex.s
@@ -5,3 +5,7 @@
 # CHECK: .short 1
 .short 	0B1
 # CHECK: .short 1
+.short  0BH
+# CHECK: .short 11
+.short  0BEDH
+# CHECK: .short 3053
Index: lib/MC/MCParser/AsmLexer.cpp
===================================================================
--- lib/MC/MCParser/AsmLexer.cpp
+++ lib/MC/MCParser/AsmLexer.cpp
@@ -251,6 +251,7 @@
 ///   Local Label: [0-9][:]
 ///   Forward/Backward Label: [0-9][fb]
 ///   Binary integer: 0b[01]+
+///     However, 0b[0-9a-fA-F]*[hH], with the [hH] suffix, is hexadecimal.
 ///   Octal integer: 0[0-7]+
 ///   Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
 ///   Decimal integer: [1-9][0-9]*
@@ -286,7 +287,7 @@
   if ((*CurPtr == 'b') || (*CurPtr == 'B')) {
     ++CurPtr;
     // See if we actually have "0b" as part of something like "jmp 0b\n"
-    if (!isdigit(CurPtr[0])) {
+    if (!isxdigit(CurPtr[0]) && *CurPtr != 'h' && *CurPtr != 'H') {
       --CurPtr;
       StringRef Result(TokStart, CurPtr - TokStart);
       return AsmToken(AsmToken::Integer, Result, 0);
@@ -295,16 +296,31 @@
     while (CurPtr[0] == '0' || CurPtr[0] == '1')
       ++CurPtr;
 
+    // Special case: check for the [hH] suffix and treat 0bedh as hexadecimal.
+    const char *NonBinStart = CurPtr;
+    while (isxdigit(*CurPtr))
+      ++CurPtr;
+    unsigned Radix = 2;
+    if (*CurPtr == 'h' || *CurPtr == 'H') {
+      Radix = 16;
+      --NumStart; // the "b" in "0bedh" also counts
+    } else
+      CurPtr = NonBinStart;
+
     // Requires at least one binary digit.
     if (CurPtr == NumStart)
       return ReturnError(TokStart, "invalid binary number");
 
-    StringRef Result(TokStart, CurPtr - TokStart);
+    StringRef Result(NumStart, CurPtr - NumStart);
 
     APInt Value(128, 0, true);
-    if (Result.substr(2).getAsInteger(2, Value))
+    if (Result.getAsInteger(Radix, Value))
       return ReturnError(TokStart, "invalid binary number");
 
+    // Consume the [hH] suffix
+    if (Radix == 16)
+      ++CurPtr;
+
     // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
     // suffixes on integer literals.
     SkipIgnoredIntegerSuffix(CurPtr);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22112.63136.patch
Type: text/x-patch
Size: 2305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160707/a561f0b4/attachment.bin>


More information about the llvm-commits mailing list