This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

GASP patch


I would like to submit the following patch, which corrects GASP's
handling of floating point constants (`flonums' in GAS terminology).
Previously, a floating point constant such as 12.12 would be converted
from the currently specified base into decimal.  Worse, the fractional
component of a float would be scanned as a separate integer, leading
to strange transformations such as 12.001 => 12.1.

This patch clarifies GASP's position on flonums and restricts their
intepretation to base 10.  Thinking that this is probably newsworthy,
I've added a note to the NEWS file.  New test case files are MIME
attached at the end of this message.

Okay to commit?

[gas ChangeLog]
2002-02-08  Ben Elliston  <bje@redhat.com>

	* NEWS: Document floating point number handling in gasp.
	* gasp.c: Include <assert.h> and "xregex.h".
	(is_flonum): New function.
	(chew_flownum): Likewise.
	(change_base): Consume flonums from the input, where possible.
	* doc/gasp.texi (Constants): Document floating point numbers.

[gas/testsuite/ChangeLog]
2002-02-08  Ben Elliston  <bje@redhat.com>

	* gasp/flonums.asm: New test.
	* gasp/flonums.err: New result.
	* gasp/flonums.out: Likewise.

Index: NEWS
===================================================================
RCS file: /cvs/src/src/gas/NEWS,v
retrieving revision 1.23
diff -u -r1.23 NEWS
--- NEWS	2002/01/31 17:32:59	1.23
+++ NEWS	2002/02/07 22:17:24
@@ -1,4 +1,8 @@
 -*- text -*-
+GASP now correctly parses floating point numbers. Unless the base is explicitly
+specified, they are interpreted as decimal numbers regardless of the currently
+specified base.
+
 Support for the OpenRISC 32-bit embedded processor by OpenCores.
 
 The ARM assembler now accepts -march=..., -mcpu=... and -mfpu=... for
Index: gasp.c
===================================================================
RCS file: /cvs/src/src/gas/gasp.c,v
retrieving revision 1.14
diff -u -r1.14 gasp.c
--- gasp.c	2002/01/17 11:28:49	1.14
+++ gasp.c	2002/02/07 22:17:28
@@ -48,6 +48,7 @@
 #include "config.h"
 #include "bin-bugs.h"
 
+#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 #include <getopt.h>
@@ -66,6 +67,7 @@
 #include "sb.h"
 #include "macro.h"
 #include "asintl.h"
+#include "xregex.h"
 
 char *program_version = "1.2";
 
@@ -227,6 +229,8 @@
 static void hash_add_to_int_table PARAMS ((hash_table *, sb *, int));
 static hash_entry *hash_lookup PARAMS ((hash_table *, sb *));
 static void checkconst PARAMS ((int, exp_t *));
+static int is_flonum PARAMS ((int, sb *));
+static int chew_flonum PARAMS ((int, sb *, sb *));
 static int sb_strtol PARAMS ((int, sb *, int, int *));
 static int level_0 PARAMS ((int, sb *, exp_t *));
 static int level_1 PARAMS ((int, sb *, exp_t *));
@@ -522,6 +526,59 @@
     }
 }
 
+/* Chew the flonum from the string starting at idx.  Adjust idx to
+   point to the next character after the flonum.  */
+
+static int
+chew_flonum (idx, string, out)
+     int idx;
+     sb *string;
+     sb *out;
+{
+  sb buf;
+  regex_t reg;
+  regmatch_t match;
+
+  /* Duplicate and null terminate `string'.  */
+  sb_new (&buf);
+  sb_add_sb (&buf, string);
+  sb_add_char (&buf, '\0');
+
+  assert (regcomp (&reg, "([0-9]*\\.[0-9]+([eE][+-]?[0-9]+)?)", REG_EXTENDED) == 0);
+  if (regexec (&reg, &buf.ptr[idx], 1, &match, 0) != 0)
+    return idx;
+
+  /* Copy the match to the output.  */
+  assert (match.rm_eo >= match.rm_so);
+  sb_add_buffer (out, &buf.ptr[idx], match.rm_eo - match.rm_so);
+
+  sb_kill (&buf);
+  regfree (&reg);
+  idx += match.rm_eo;
+  return idx;
+}
+
+static int
+is_flonum (idx, string)
+     int idx;
+     sb *string;
+{
+  sb buf;
+  regex_t reg;
+  int rc;
+
+  /* Duplicate and null terminate `string'.  */
+  sb_new (&buf);
+  sb_add_sb (&buf, string);
+  sb_add_char (&buf, '\0');
+
+  assert (regcomp (&reg, "^[0-9]*\\.[0-9]+([eE][+-]?[0-9]+)?", REG_EXTENDED) == 0);
+  rc = regexec (&reg, &buf.ptr[idx], 0, NULL, 0);
+  sb_kill (&buf);
+  regfree (&reg);
+  return (rc == 0);
+}
+
 /* Turn the number in string at idx into a number of base, fill in
    ptr, and return the index of the first character not in the number.  */
 
@@ -1131,6 +1188,10 @@
 	      sb_add_char (out, in->ptr[idx]);
 	      idx++;
 	    }
+	}
+      else if (is_flonum (idx, in))
+	{
+	  idx = chew_flonum (idx, in, out);
 	}
       else if (ISDIGIT (in->ptr[idx]))
 	{
Index: doc/gasp.texi
===================================================================
RCS file: /cvs/src/src/gas/doc/gasp.texi,v
retrieving revision 1.3
diff -u -r1.3 gasp.texi
--- gasp.texi	2001/03/08 23:24:26	1.3
+++ gasp.texi	2002/02/07 22:17:31
@@ -943,6 +943,11 @@
 base 2, @samp{Q} for base 8, @samp{D} for base 10, and @samp{H} for base
 16.  (You can write this character in lower case if you prefer.)
 
+You can write floating point constants using the same syntax recognised
+by GAS @ref{Flonums,,Flonums,as,The GNU Assembler.}.  A constraint is
+that these constants will be interpreted as decimal values irrespective
+of the currently selected base.
+
 @c FIXME! What are rules for recognizing number in deflt base?  Whatever
 @c        is left over after parsing other things??

Attachment: flonums.asm
Description: Binary data

Attachment: flonums.out
Description: Binary data

Attachment: flonums.err
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]