diff mbox

[RFC,10/19] Target FP: Add string routines to target-float.{c,h}

Message ID 20170905182120.48E93D8086F@oc3748833570.ibm.com
State New
Headers show

Commit Message

Ulrich Weigand Sept. 5, 2017, 6:21 p.m. UTC
[RFC][10/19] Target FP: Add string routines to target-float.{c,h}

This adds target_float_to_string and target_float_from_string,
which dispatch to the corresponding floatformat_ or decimal_ routines.

Existing users of those routines are changed to use the new
target-float routines instead (those places already handle both
binary and decimal FP).

In addition, two other places are changes to use target_float_from_string:

- define_symbol in stabsread.c, when parsing a floating-point literal
  from stabs debug info

- gdbarch-selftest.c when initializing a target format values (to
  eliminate use of DOUBLEST there).

Bye,
Ulrich

ChangeLog:

	* target-float.c (target_float_to_string): New function.
	(target_float_from_string): New function.
	* target-float.h (target_float_to_string): Add prototype.
	(target_float_from_string): Add prototype.

	* valprint.c: Include "target-float.h".  Do not include
	"doublest.h" and "dfp.h".
	(print_floating): Use target_float_to_string.
	* printcmd.c: Include "target-float.h".  Do not include "dfp.h".
	(printf_floating): Use target_float_to_string.

	* parse.c: Include "target-float.h".  Do not include
	"doublest.h" and "dfp.h".
	(parse_float): Use target_float_from_string.
	* stabsread.c: Include "target-float.h".  Do not include "doublest.h".
	(define_symbol): Use target_float_from_string.
	* gdbarch-selftests.c: Include "target-float.h".
	(register_to_value_test): Use target_float_from_string.
diff mbox

Patch

Index: binutils-gdb/gdb/target-float.c
===================================================================
--- binutils-gdb.orig/gdb/target-float.c
+++ binutils-gdb/gdb/target-float.c
@@ -60,3 +60,40 @@  target_float_is_zero (const gdb_byte *ad
   gdb_assert_not_reached ("unexpected type code");
 }
 
+/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
+   to a string, optionally using the print format FORMAT.  */
+std::string
+target_float_to_string (const gdb_byte *addr, const struct type *type,
+			const char *format)
+{
+  if (TYPE_CODE (type) == TYPE_CODE_FLT)
+    return floatformat_to_string (floatformat_from_type (type), addr, format);
+
+  if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
+    return decimal_to_string (addr, TYPE_LENGTH (type),
+			      gdbarch_byte_order (get_type_arch (type)),
+			      format);
+
+  gdb_assert_not_reached ("unexpected type code");
+}
+
+/* Parse string STRING into a target floating-number of type TYPE and
+   store it as byte-stream ADDR.  Return whether parsing succeeded.  */
+bool
+target_float_from_string (gdb_byte *addr, const struct type *type,
+			  std::string string)
+{
+  /* Ensure possible padding bytes in the target buffer are zeroed out.  */
+  memset (addr, 0, TYPE_LENGTH (type));
+
+  if (TYPE_CODE (type) == TYPE_CODE_FLT)
+    return floatformat_from_string (floatformat_from_type (type), addr,
+				    string);
+
+  if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
+    return decimal_from_string (addr, TYPE_LENGTH (type),
+				gdbarch_byte_order (get_type_arch (type)),
+				string);
+
+  gdb_assert_not_reached ("unexpected type code");
+}
Index: binutils-gdb/gdb/target-float.h
===================================================================
--- binutils-gdb.orig/gdb/target-float.h
+++ binutils-gdb/gdb/target-float.h
@@ -25,4 +25,11 @@  extern bool target_float_is_valid (const
 extern bool target_float_is_zero (const gdb_byte *addr,
 				  const struct type *type);
 
+extern std::string target_float_to_string (const gdb_byte *addr,
+					   const struct type *type,
+					   const char *format = nullptr);
+extern bool target_float_from_string (gdb_byte *addr,
+				      const struct type *type,
+				      std::string string);
+
 #endif
Index: binutils-gdb/gdb/valprint.c
===================================================================
--- binutils-gdb.orig/gdb/valprint.c
+++ binutils-gdb/gdb/valprint.c
@@ -27,8 +27,7 @@ 
 #include "language.h"
 #include "annotate.h"
 #include "valprint.h"
-#include "doublest.h"
-#include "dfp.h"
+#include "target-float.h"
 #include "extension.h"
 #include "ada-lang.h"
 #include "gdb_obstack.h"
@@ -1376,18 +1375,7 @@  void
 print_floating (const gdb_byte *valaddr, struct type *type,
 		struct ui_file *stream)
 {
-  std::string str;
-  if (TYPE_CODE (type) == TYPE_CODE_FLT)
-    {
-      const struct floatformat *fmt = floatformat_from_type (type);
-      str = floatformat_to_string (fmt, valaddr);
-    }
-  else
-    {
-      enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
-      unsigned len = TYPE_LENGTH (type);
-      std::string str = decimal_to_string (valaddr, len, byte_order);
-    }
+  std::string str = target_float_to_string (valaddr, type);
   fputs_filtered (str.c_str (), stream);
 }
 
Index: binutils-gdb/gdb/printcmd.c
===================================================================
--- binutils-gdb.orig/gdb/printcmd.c
+++ binutils-gdb/gdb/printcmd.c
@@ -38,7 +38,7 @@ 
 #include "ui-out.h"
 #include "block.h"
 #include "disasm.h"
-#include "dfp.h"
+#include "target-float.h"
 #include "observer.h"
 #include "solist.h"
 #include "parser-defs.h"
@@ -2359,13 +2359,8 @@  printf_floating (struct ui_file *stream,
   value = value_cast (fmt_type, value);
 
   /* Convert the value to a string and print it.  */
-  std::string str;
-  if (TYPE_CODE (fmt_type) == TYPE_CODE_FLT)
-    str = floatformat_to_string (floatformat_from_type (fmt_type),
-				 value_contents (value), format);
-  else
-    str = decimal_to_string (value_contents (value),
-			     TYPE_LENGTH (fmt_type), byte_order, format);
+  std::string str
+    = target_float_to_string (value_contents (value), fmt_type, format);
   fputs_filtered (str.c_str (), stream);
 }
 
Index: binutils-gdb/gdb/parse.c
===================================================================
--- binutils-gdb.orig/gdb/parse.c
+++ binutils-gdb/gdb/parse.c
@@ -44,8 +44,7 @@ 
 #include "gdbcmd.h"
 #include "symfile.h"		/* for overlay functions */
 #include "inferior.h"
-#include "doublest.h"
-#include "dfp.h"
+#include "target-float.h"
 #include "block.h"
 #include "source.h"
 #include "objfiles.h"
@@ -1364,13 +1363,7 @@  bool
 parse_float (const char *p, int len,
 	     const struct type *type, gdb_byte *data)
 {
-  if (TYPE_CODE (type) == TYPE_CODE_FLT)
-    return floatformat_from_string (floatformat_from_type (type),
-				    data, std::string (p, len));
-  else
-    return decimal_from_string (data, TYPE_LENGTH (type),
-				gdbarch_byte_order (get_type_arch (type)),
-				std::string (p, len));
+  return target_float_from_string (data, type, std::string (p, len));
 }
 
 /* Stuff for maintaining a stack of types.  Currently just used by C, but
Index: binutils-gdb/gdb/stabsread.c
===================================================================
--- binutils-gdb.orig/gdb/stabsread.c
+++ binutils-gdb/gdb/stabsread.c
@@ -41,7 +41,7 @@ 
 #include "demangle.h"
 #include "gdb-demangle.h"
 #include "language.h"
-#include "doublest.h"
+#include "target-float.h"
 #include "cp-abi.h"
 #include "cp-support.h"
 #include <ctype.h>
@@ -800,19 +800,15 @@  define_symbol (CORE_ADDR valu, const cha
 	{
 	case 'r':
 	  {
-	    double d = atof (p);
 	    gdb_byte *dbl_valu;
 	    struct type *dbl_type;
 
-	    /* FIXME-if-picky-about-floating-accuracy: Should be using
-	       target arithmetic to get the value.  real.c in GCC
-	       probably has the necessary code.  */
-
 	    dbl_type = objfile_type (objfile)->builtin_double;
 	    dbl_valu
 	      = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
 					    TYPE_LENGTH (dbl_type));
-	    store_typed_floating (dbl_valu, dbl_type, d);
+
+	    target_float_from_string (dbl_valu, dbl_type, std::string (p));
 
 	    SYMBOL_TYPE (sym) = dbl_type;
 	    SYMBOL_VALUE_BYTES (sym) = dbl_valu;
Index: binutils-gdb/gdb/gdbarch-selftests.c
===================================================================
--- binutils-gdb.orig/gdb/gdbarch-selftests.c
+++ binutils-gdb/gdb/gdbarch-selftests.c
@@ -22,6 +22,7 @@ 
 #include "selftest.h"
 #include "selftest-arch.h"
 #include "inferior.h"
+#include "target-float.h"
 
 namespace selftests {
 
@@ -105,11 +106,8 @@  register_to_value_test (struct gdbarch *
 
 	      if (TYPE_CODE (type) == TYPE_CODE_FLT)
 		{
-		  DOUBLEST d = 1.25;
-
 		  /* Generate valid float format.  */
-		  floatformat_from_doublest (floatformat_from_type (type),
-					     &d, expected.data ());
+		  target_float_from_string (expected.data (), type, "1.25");
 		}
 	      else
 		{