Add support for hexadecimal FP constants!
authorChris Lattner <sabre@nondot.org>
Sun, 7 Apr 2002 08:10:41 +0000 (08:10 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 7 Apr 2002 08:10:41 +0000 (08:10 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2135 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AsmParser/Lexer.l

index 8992cdc63ded2399e81bf780e22945092c3113d0..678697a7df606ae4008eef1fc623e2356002a0f8 100644 (file)
@@ -19,9 +19,6 @@
 
 %{
 #include "ParserInternals.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/Method.h"
-#include "llvm/Module.h"
 #include <list>
 #include "llvmAsmParser.h"
 #include <ctype.h>
 // long representation... this does not have to do input error checking, 
 // because we know that the input will be matched by a suitable regex...
 //
-uint64_t atoull(const char *Buffer) {
+static uint64_t atoull(const char *Buffer) {
   uint64_t Result = 0;
   for (; *Buffer; Buffer++) {
     uint64_t OldRes = Result;
     Result *= 10;
     Result += *Buffer-'0';
-    if (Result < OldRes) {  // Uh, oh, overflow detected!!!
+    if (Result < OldRes)   // Uh, oh, overflow detected!!!
       ThrowException("constant bigger than 64 bits detected!");
-    }
   }
   return Result;
 }
 
+// HexToFP - Convert the ascii string in hexidecimal format to the floating
+// point representation of it.
+//
+static double HexToFP(const char *Buffer) {
+  uint64_t Result = 0;
+  for (; *Buffer; ++Buffer) {
+    uint64_t OldRes = Result;
+    Result *= 16;
+    char C = *Buffer;
+    if (C >= '0' && C <= '9')
+      Result += C-'0';
+    else if (C >= 'A' && C <= 'F')
+      Result += C-'A'+10;
+    else if (C >= 'a' && C <= 'f')
+      Result += C-'a'+10;
+
+    if (Result < OldRes)   // Uh, oh, overflow detected!!!
+      ThrowException("constant bigger than 64 bits detected!");
+  }
+
+  assert(sizeof(double) == sizeof(Result) &&
+         "Data sizes incompatible on this target!");
+  void *ProxyPointer = &Result;  // Break TBAA correctly
+  cerr << "VALUE: " << *(double*)ProxyPointer << "\n";
+  return *(double*)ProxyPointer;   // Cast Hex constant to double
+}
+
 
 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
 // appropriate character.  If AllowNull is set to false, a \00 value will cause
@@ -110,9 +133,13 @@ PInteger   [0-9]+
 NInteger  -[0-9]+
 
 /* FPConstant - A Floating point constant.
  TODO: Expand lexer to support 10e50 FP constant notation */
+ */
 FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
 
+/* HexFPConstant - Floating point constant represented in IEEE format as a
+ *  hexadecimal number for when exponential notation is not precise enough.
+ */
+HexFPConstant 0x[0-9A-Fa-f]+
 %%
 
 {Comment}       { /* Ignore comments for now */ }
@@ -233,6 +260,7 @@ getelementptr   { RET_TOK(MemOpVal, GetElementPtr, GETELEMENTPTR); }
                 }
 
 {FPConstant}    { llvmAsmlval.FPVal = atof(yytext); return FPVAL; }
+{HexFPConstant} { llvmAsmlval.FPVal = HexToFP(yytext); return FPVAL; }
 
 [ \t\n]         { /* Ignore whitespace */ }
 .               { return yytext[0]; }