[llvm-commits] [llvm] r114861 - in /llvm/trunk: include/llvm/MC/MCParser/AsmLexer.h lib/MC/MCParser/AsmLexer.cpp test/MC/AsmParser/floating-literals.s

Daniel Dunbar daniel at zuster.org
Mon Sep 27 13:12:52 PDT 2010


Author: ddunbar
Date: Mon Sep 27 15:12:52 2010
New Revision: 114861

URL: http://llvm.org/viewvc/llvm-project?rev=114861&view=rev
Log:
MC/AsmParser: Handle exponents in floating point literals.

Modified:
    llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h
    llvm/trunk/lib/MC/MCParser/AsmLexer.cpp
    llvm/trunk/test/MC/AsmParser/floating-literals.s

Modified: llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h?rev=114861&r1=114860&r2=114861&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h (original)
+++ llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h Mon Sep 27 15:12:52 2010
@@ -61,6 +61,7 @@
   AsmToken LexLineComment();
   AsmToken LexDigit();
   AsmToken LexQuote();
+  AsmToken LexFloatLiteral();
 };
   
 } // end namespace llvm

Modified: llvm/trunk/lib/MC/MCParser/AsmLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmLexer.cpp?rev=114861&r1=114860&r2=114861&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmLexer.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmLexer.cpp Mon Sep 27 15:12:52 2010
@@ -64,6 +64,31 @@
   }
 }
 
+/// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
+///
+/// The leading integral digit sequence and dot should have already been
+/// consumed, some or all of the fractional digit sequence *can* have been
+/// consumed.
+AsmToken AsmLexer::LexFloatLiteral() {
+  // Skip the fractional digit sequence.
+  while (isdigit(*CurPtr))
+    ++CurPtr;
+
+  // Check for exponent; we intentionally accept a slighlty wider set of
+  // literals here and rely on the upstream client to reject invalid ones (e.g.,
+  // "1e+").
+  if (*CurPtr == 'e' || *CurPtr == 'E') {
+    ++CurPtr;
+    if (*CurPtr == '-' || *CurPtr == '+')
+      ++CurPtr;
+    while (isdigit(*CurPtr))
+      ++CurPtr;
+  }
+
+  return AsmToken(AsmToken::Real,
+                  StringRef(TokStart, CurPtr - TokStart));
+}
+
 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
 static bool IsIdentifierChar(char c) {
   return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@';
@@ -71,12 +96,11 @@
 AsmToken AsmLexer::LexIdentifier() {
   // Check for floating point literals.
   if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
+    // Disambiguate a .1243foo identifier from a floating literal.
     while (isdigit(*CurPtr))
       ++CurPtr;
-    if (!IsIdentifierChar(*CurPtr)) {
-      return AsmToken(AsmToken::Real,
-                      StringRef(TokStart, CurPtr - TokStart));
-    }
+    if (*CurPtr == 'e' || *CurPtr == 'E' || !IsIdentifierChar(*CurPtr))
+      return LexFloatLiteral();
   }
 
   while (IsIdentifierChar(*CurPtr))
@@ -150,12 +174,9 @@
       ++CurPtr;
 
     // Check for floating point literals.
-    if (*CurPtr == '.') {
+    if (*CurPtr == '.' || *CurPtr == 'e') {
       ++CurPtr;
-      while (isdigit(*CurPtr))
-        ++CurPtr;
-
-      return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
+      return LexFloatLiteral();
     }
 
     StringRef Result(TokStart, CurPtr - TokStart);

Modified: llvm/trunk/test/MC/AsmParser/floating-literals.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/floating-literals.s?rev=114861&r1=114860&r2=114861&view=diff
==============================================================================
--- llvm/trunk/test/MC/AsmParser/floating-literals.s (original)
+++ llvm/trunk/test/MC/AsmParser/floating-literals.s Mon Sep 27 15:12:52 2010
@@ -13,3 +13,20 @@
 
 # CHECK: .quad  0
 .double 0.0
+
+# CHECK: .quad  -4570379565595099136
+.double -1.2e3
+# CHECK: .quad  -4690170861623122860
+.double -1.2e-5
+# CHECK: .quad  -4465782973978902528
+.double -1.2e+10
+# CHECK: .quad  4681608360884174848
+.double 1e5
+# CHECK: .quad  4681608360884174848
+.double 1.e5
+# CHECK: .quad  4611686018427387904
+.double 2.
+
+// APFloat should reject these with an error, not crash:
+//.double -1.2e+
+//.double -1.2e





More information about the llvm-commits mailing list