[llvm] r200613 - MC: Add AsmLexer::BigNum token for integers greater than 64 bits

David Woodhouse dwmw2 at infradead.org
Sat Feb 1 08:20:55 PST 2014


Author: dwmw2
Date: Sat Feb  1 10:20:54 2014
New Revision: 200613

URL: http://llvm.org/viewvc/llvm-project?rev=200613&view=rev
Log:
MC: Add AsmLexer::BigNum token for integers greater than 64 bits

This will be needed for .octa support, but we don't want to just use the
existing AsmLexer::Integer for it and then have to litter all its users
with explicit checks for the size, and make them use the new get APIntVal()
method.

So let the lexer produce an AsmLexer::Integer as before for numbers which
are small enough — which appears to cover what was previously a nasty
special case handling of numbers which don't fit in int64_t but *do* fit
in uint64_t.

Where the number is too large even for that, produce an AsmLexer::BigNum
instead. We do nothing with these except complain about them for now,
but that will be changed shortly...

Based on a patch from PaX Team <pageexec at freemail.hu>

Modified:
    llvm/trunk/include/llvm/MC/MCParser/MCAsmLexer.h
    llvm/trunk/lib/MC/MCParser/AsmLexer.cpp
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp

Modified: llvm/trunk/include/llvm/MC/MCParser/MCAsmLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCParser/MCAsmLexer.h?rev=200613&r1=200612&r2=200613&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCParser/MCAsmLexer.h (original)
+++ llvm/trunk/include/llvm/MC/MCParser/MCAsmLexer.h Sat Feb  1 10:20:54 2014
@@ -10,6 +10,7 @@
 #ifndef LLVM_MC_MCPARSER_MCASMLEXER_H
 #define LLVM_MC_MCPARSER_MCASMLEXER_H
 
+#include "llvm/ADT/APInt.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/DataTypes.h"
@@ -30,6 +31,7 @@ public:
 
     // Integer values.
     Integer,
+    BigNum, // larger than 64 bits
 
     // Real values.
     Real,
@@ -57,12 +59,14 @@ private:
   /// a memory buffer owned by the source manager.
   StringRef Str;
 
-  int64_t IntVal;
+  APInt IntVal;
 
 public:
   AsmToken() {}
-  AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
+  AsmToken(TokenKind _Kind, StringRef _Str, APInt _IntVal)
     : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
+  AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
+    : Kind(_Kind), Str(_Str), IntVal(64, _IntVal, true) {}
 
   TokenKind getKind() const { return Kind; }
   bool is(TokenKind K) const { return Kind == K; }
@@ -99,6 +103,12 @@ public:
   // as a single token, then diagnose as an invalid number).
   int64_t getIntVal() const {
     assert(Kind == Integer && "This token isn't an integer!");
+    return IntVal.getZExtValue();
+  }
+
+  APInt getAPIntVal() const {
+    assert((Kind == Integer || Kind == BigNum) &&
+           "This token isn't an integer!");
     return IntVal;
   }
 };

Modified: llvm/trunk/lib/MC/MCParser/AsmLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmLexer.cpp?rev=200613&r1=200612&r2=200613&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmLexer.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmLexer.cpp Sat Feb  1 10:20:54 2014
@@ -238,6 +238,13 @@ static unsigned doLookAhead(const char *
   return DefaultRadix;
 }
 
+static AsmToken intToken(StringRef Ref, APInt &Value)
+{
+  if (Value.isIntN(64))
+    return AsmToken(AsmToken::Integer, Ref, Value);
+  return AsmToken(AsmToken::BigNum, Ref, Value);
+}
+
 /// LexDigit: First character is [0-9].
 ///   Local Label: [0-9][:]
 ///   Forward/Backward Label: [0-9][fb]
@@ -258,16 +265,10 @@ AsmToken AsmLexer::LexDigit() {
 
     StringRef Result(TokStart, CurPtr - TokStart);
 
-    long long Value;
-    if (Result.getAsInteger(Radix, Value)) {
-      // Allow positive values that are too large to fit into a signed 64-bit
-      // integer, but that do fit in an unsigned one, we just convert them over.
-      unsigned long long UValue;
-      if (Result.getAsInteger(Radix, UValue))
-        return ReturnError(TokStart, !isHex ? "invalid decimal number" :
+    APInt Value(128, 0, true);
+    if (Result.getAsInteger(Radix, Value))
+      return ReturnError(TokStart, !isHex ? "invalid decimal number" :
                            "invalid hexdecimal number");
-      Value = (long long)UValue;
-    }
 
     // Consume the [bB][hH].
     if (Radix == 2 || Radix == 16)
@@ -277,7 +278,7 @@ AsmToken AsmLexer::LexDigit() {
     // suffices on integer literals.
     SkipIgnoredIntegerSuffix(CurPtr);
 
-    return AsmToken(AsmToken::Integer, Result, Value);
+    return intToken(Result, Value);
   }
 
   if (*CurPtr == 'b') {
@@ -298,7 +299,7 @@ AsmToken AsmLexer::LexDigit() {
 
     StringRef Result(TokStart, CurPtr - TokStart);
 
-    long long Value;
+    APInt Value(128, 0, true);
     if (Result.substr(2).getAsInteger(2, Value))
       return ReturnError(TokStart, "invalid binary number");
 
@@ -306,7 +307,7 @@ AsmToken AsmLexer::LexDigit() {
     // suffixes on integer literals.
     SkipIgnoredIntegerSuffix(CurPtr);
 
-    return AsmToken(AsmToken::Integer, Result, Value);
+    return intToken(Result, Value);
   }
 
   if (*CurPtr == 'x') {
@@ -324,7 +325,7 @@ AsmToken AsmLexer::LexDigit() {
     if (CurPtr == NumStart)
       return ReturnError(CurPtr-2, "invalid hexadecimal number");
 
-    unsigned long long Result;
+    APInt Result(128, 0);
     if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
       return ReturnError(TokStart, "invalid hexadecimal number");
 
@@ -336,12 +337,11 @@ AsmToken AsmLexer::LexDigit() {
     // suffixes on integer literals.
     SkipIgnoredIntegerSuffix(CurPtr);
 
-    return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
-                    (int64_t)Result);
+    return intToken(StringRef(TokStart, CurPtr - TokStart), Result);
   }
 
   // Either octal or hexadecimal.
-  long long Value;
+  APInt Value(128, 0, true);
   unsigned Radix = doLookAhead(CurPtr, 8);
   bool isHex = Radix == 16;
   StringRef Result(TokStart, CurPtr - TokStart);
@@ -357,7 +357,7 @@ AsmToken AsmLexer::LexDigit() {
   // suffixes on integer literals.
   SkipIgnoredIntegerSuffix(CurPtr);
 
-  return AsmToken(AsmToken::Integer, Result, Value);
+  return intToken(Result, Value);
 }
 
 /// LexSingleQuote: Integer: 'b'

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=200613&r1=200612&r2=200613&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Sat Feb  1 10:20:54 2014
@@ -854,6 +854,8 @@ bool AsmParser::parsePrimaryExpr(const M
     Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
     return false;
   }
+  case AsmToken::BigNum:
+    return TokError("literal value out of range for directive");
   case AsmToken::Integer: {
     SMLoc Loc = getTok().getLoc();
     int64_t IntVal = getTok().getIntVal();





More information about the llvm-commits mailing list