diff options
author | Ulrich Weigand <ulrich.weigand@de.ibm.com> | 2017-10-05 19:14:08 +0200 |
---|---|---|
committer | Ulrich Weigand <ulrich.weigand@de.ibm.com> | 2017-10-05 19:14:08 +0200 |
commit | 3b4b2f160d288b85a1379d24fd0f4de19062f3fd (patch) | |
tree | a2e1e9b581fe59bae11bf476291b127713777be8 /gdb/dfp.c | |
parent | 1841ee5d0300cf00022c8aadfe16725c5e86fe1d (diff) | |
download | gdb-3b4b2f160d288b85a1379d24fd0f4de19062f3fd.zip gdb-3b4b2f160d288b85a1379d24fd0f4de19062f3fd.tar.gz gdb-3b4b2f160d288b85a1379d24fd0f4de19062f3fd.tar.bz2 |
Clean up some DFP interfaces
This cleans up a number of interfaces in dfp.c / dfp.h. Specifically:
- The decimal_from_string / decimal_to_string routines are C++-ified
to operate on std::string instead of character buffers. In the
decimal_from_string, the boolean return value now actually is bool
instead of an int.
- The decimal_from_integral and decimal_from_doublest routines take
an struct value as input. This is not really appropriate at the low
level the DFP routines sit, so this replaced them with new routines
decimal_from_longest / decimal_from_ulongest / decimal_from_doublest
that operate on contents instead.
- To mirror the decimal_from_[u]longest, a new decimal_to_longest
routine is added as well, which can be used in unpack_long to
avoid an unnecessary conversion via DOUBLEST.
Note that the decimal_from_longest / decimal_from_ulongest routines
are actually more powerful than decimal_from_integral: the old routine
would only accept integer *types* of at most four bytes size, while
the new routines accept all integer *values* that fit in an [u]int32_t,
no matter which type they came from. The DFP tests are updated to
allow for this larger range of integers that can be converted.
gdb/ChangeLog:
2017-10-05 Ulrich Weigand <uweigand@de.ibm.com>
* dfp.h (MAX_DECIMAL_STRING): Move to dfp.c.
(decimal_to_string): Return std::string object.
(decimal_from_string): Accept std::string object. Return bool.
(decimal_from_integral, decimal_from_doublest): Remove.
(decimal_from_longest): Add prototype.
(decimal_from_ulongest): Likewise.
(decimal_to_longest): Likewise.
(decimal_from_doublest): Likewise.
* dfp.c: Do not include "gdbtypes.h" or "value.h".
(MAX_DECIMAL_STRING): Move here.
(decimal_to_string): Return std::string object.
(decimal_from_string): Accept std::string object. Return bool.
(decimal_from_integral): Remove, replace by ...
(decimal_from_longest, decimal_from_ulongest): ... these new functions.
(decimal_to_longest): New function.
(decimal_from_floating): Remove, replace by ...
(decimal_from_doublest): ... this new function.
(decimal_to_doublest): Update to new decimal_to_string interface.
* value.c (unpack_long): Use decimal_to_longest.
* valops.c (value_cast): Use decimal_from_doublest instead of
decimal_from_floating. Use decimal_from_[u]longest isntead of
decimal_from_integral.
* valarith.c (value_args_as_decimal): Likewise.
* valprint.c (print_decimal_floating): Update to new
decimal_to_string interface.
* printcmd.c (printf_decfloat): Likewise.
* c-exp.y (parse_number): Update to new decimal_from_string interface.
gdb/testsuite/ChangeLog:
2017-10-05 Ulrich Weigand <uweigand@de.ibm.com>
* gdb.base/dfp-exprs.exp: Update tests to larger range of supported
integer-to-dfp conversion.
* gdb.base/dfp-test.exp: Likewise.
Diffstat (limited to 'gdb/dfp.c')
-rw-r--r-- | gdb/dfp.c | 96 |
1 files changed, 57 insertions, 39 deletions
@@ -19,8 +19,6 @@ #include "defs.h" #include "expression.h" -#include "gdbtypes.h" -#include "value.h" #include "dfp.h" /* The order of the following headers is important for making sure @@ -30,6 +28,10 @@ #include "dpd/decimal64.h" #include "dpd/decimal32.h" +/* When using decimal128, this is the maximum string length + 1 + (value comes from libdecnumber's DECIMAL128_String constant). */ +#define MAX_DECIMAL_STRING 43 + /* In GDB, we are using an array of gdb_byte to represent decimal values. They are stored in host byte order. This routine does the conversion if the target byte order is different. */ @@ -142,37 +144,42 @@ decimal_to_number (const gdb_byte *from, int len, decNumber *to) /* Convert decimal type to its string representation. LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and 16 bytes for decimal128. */ -void +std::string decimal_to_string (const gdb_byte *decbytes, int len, - enum bfd_endian byte_order, char *s) + enum bfd_endian byte_order) { gdb_byte dec[16]; match_endianness (decbytes, len, byte_order, dec); + std::string result; + result.resize (MAX_DECIMAL_STRING); + switch (len) { case 4: - decimal32ToString ((decimal32 *) dec, s); + decimal32ToString ((decimal32 *) dec, &result[0]); break; case 8: - decimal64ToString ((decimal64 *) dec, s); + decimal64ToString ((decimal64 *) dec, &result[0]); break; case 16: - decimal128ToString ((decimal128 *) dec, s); + decimal128ToString ((decimal128 *) dec, &result[0]); break; default: error (_("Unknown decimal floating point type.")); break; } + + return result; } /* Convert the string form of a decimal value to its decimal representation. LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and 16 bytes for decimal128. */ -int +bool decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order, - const char *string) + std::string string) { decContext set; gdb_byte dec[16]; @@ -182,13 +189,13 @@ decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order, switch (len) { case 4: - decimal32FromString ((decimal32 *) dec, string, &set); + decimal32FromString ((decimal32 *) dec, string.c_str (), &set); break; case 8: - decimal64FromString ((decimal64 *) dec, string, &set); + decimal64FromString ((decimal64 *) dec, string.c_str (), &set); break; case 16: - decimal128FromString ((decimal128 *) dec, string, &set); + decimal128FromString ((decimal128 *) dec, string.c_str (), &set); break; default: error (_("Unknown decimal floating point type.")); @@ -200,66 +207,77 @@ decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order, /* Check for errors in the DFP operation. */ decimal_check_errors (&set); - return 1; + return true; } -/* Converts a value of an integral type to a decimal float of - specified LEN bytes. */ +/* Converts a LONGEST to a decimal float of specified LEN bytes. */ void -decimal_from_integral (struct value *from, - gdb_byte *to, int len, enum bfd_endian byte_order) +decimal_from_longest (LONGEST from, + gdb_byte *to, int len, enum bfd_endian byte_order) { - LONGEST l; gdb_byte dec[16]; decNumber number; - struct type *type; + if ((int32_t) from != from) + /* libdecnumber can convert only 32-bit integers. */ + error (_("Conversion of large integer to a " + "decimal floating type is not supported.")); + + decNumberFromInt32 (&number, (int32_t) from); + + decimal_from_number (&number, dec, len); + match_endianness (dec, len, byte_order, to); +} - type = check_typedef (value_type (from)); +/* Converts a ULONGEST to a decimal float of specified LEN bytes. */ +void +decimal_from_ulongest (ULONGEST from, + gdb_byte *to, int len, enum bfd_endian byte_order) +{ + gdb_byte dec[16]; + decNumber number; - if (TYPE_LENGTH (type) > 4) + if ((uint32_t) from != from) /* libdecnumber can convert only 32-bit integers. */ error (_("Conversion of large integer to a " "decimal floating type is not supported.")); - l = value_as_long (from); - - if (TYPE_UNSIGNED (type)) - decNumberFromUInt32 (&number, (unsigned int) l); - else - decNumberFromInt32 (&number, (int) l); + decNumberFromUInt32 (&number, (uint32_t) from); decimal_from_number (&number, dec, len); match_endianness (dec, len, byte_order, to); } +/* Converts a decimal float of LEN bytes to a LONGEST. */ +LONGEST +decimal_to_longest (const gdb_byte *from, int len, enum bfd_endian byte_order) +{ + /* libdecnumber has a function to convert from decimal to integer, but + it doesn't work when the decimal number has a fractional part. */ + std::string str = decimal_to_string (from, len, byte_order); + return strtoll (str.c_str (), NULL, 10); +} + /* Converts a value of a float type to a decimal float of specified LEN bytes. This is an ugly way to do the conversion, but libdecnumber does not offer a direct way to do it. */ void -decimal_from_floating (struct value *from, +decimal_from_doublest (DOUBLEST from, gdb_byte *to, int len, enum bfd_endian byte_order) { - char *buffer; - - buffer = xstrprintf ("%.30" DOUBLEST_PRINT_FORMAT, value_as_double (from)); - - decimal_from_string (to, len, byte_order, buffer); - - xfree (buffer); + std::string str = string_printf ("%.30" DOUBLEST_PRINT_FORMAT, from); + decimal_from_string (to, len, byte_order, str); } /* Converts a decimal float of LEN bytes to a double value. */ DOUBLEST decimal_to_doublest (const gdb_byte *from, int len, enum bfd_endian byte_order) { - char buffer[MAX_DECIMAL_STRING]; - /* This is an ugly way to do the conversion, but libdecnumber does not offer a direct way to do it. */ - decimal_to_string (from, len, byte_order, buffer); - return strtod (buffer, NULL); + std::string str = decimal_to_string (from, len, byte_order); + return strtod (str.c_str (), NULL); } /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y |