/* Decimal floating point support for GDB.
- Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
+ Copyright (C) 2007-2017 Free Software Foundation, Inc.
This file is part of GDB.
#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
#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. */
static void
-match_endianness (const gdb_byte *from, int len, gdb_byte *to)
+match_endianness (const gdb_byte *from, int len, enum bfd_endian byte_order,
+ gdb_byte *to)
{
int i;
#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
#endif
- if (gdbarch_byte_order (current_gdbarch) == OPPOSITE_BYTE_ORDER)
+ if (byte_order == OPPOSITE_BYTE_ORDER)
for (i = 0; i < len; i++)
to[i] = from[len - i - 1];
else
{
/* Leave only the error bits in the status flags. */
ctx->status &= DEC_IEEE_854_Invalid_operation;
- error (_("Cannot perform operation: %s"), decContextStatusToString (ctx));
+ error (_("Cannot perform operation: %s"),
+ decContextStatusToString (ctx));
}
}
decimal128ToNumber ((decimal128 *) from, to);
break;
default:
- error (_("Unknown decimal floating point type.\n"));
+ error (_("Unknown decimal floating point type."));
break;
}
}
/* 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
-decimal_to_string (const gdb_byte *decbytes, int len, char *s)
+std::string
+decimal_to_string (const gdb_byte *decbytes, int len,
+ enum bfd_endian byte_order, const char *format)
{
gdb_byte dec[16];
- match_endianness (decbytes, len, dec);
+ match_endianness (decbytes, len, byte_order, dec);
+
+ if (format != nullptr)
+ {
+ /* We don't handle format strings (yet). If the host printf supports
+ decimal floating point types, just use this. Otherwise, fall back
+ to printing the number while ignoring the format string. */
+#if defined (PRINTF_HAS_DECFLOAT)
+ /* FIXME: This makes unwarranted assumptions about the host ABI! */
+ return string_printf (format, dec);
+#endif
+ }
+
+ 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
-decimal_from_string (gdb_byte *decbytes, int len, const char *string)
+bool
+decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order,
+ const std::string &string)
{
decContext set;
gdb_byte dec[16];
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."));
break;
}
- match_endianness (dec, len, decbytes);
+ match_endianness (dec, len, byte_order, decbytes);
/* 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)
+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."));
- type = check_typedef (value_type (from));
+ decNumberFromInt32 (&number, (int32_t) from);
- if (TYPE_LENGTH (type) > 4)
- /* libdecnumber can convert only 32-bit integers. */
- error (_("Conversion of large integer to a decimal floating type is not supported."));
+ decimal_from_number (&number, dec, len);
+ match_endianness (dec, len, byte_order, to);
+}
- l = value_as_long (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_UNSIGNED (type))
- decNumberFromUInt32 (&number, (unsigned int) l);
- else
- decNumberFromInt32 (&number, (int) l);
+ 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."));
+
+ decNumberFromUInt32 (&number, (uint32_t) from);
decimal_from_number (&number, dec, len);
- match_endianness (dec, len, to);
+ 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
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, gdb_byte *to, int len)
+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, 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)
+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, 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
- and store value in RESULT with size LEN_RESULT. */
+ and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
+ RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
void
-decimal_binop (enum exp_opcode op, const gdb_byte *x, int len_x,
- const gdb_byte *y, int len_y, gdb_byte *result, int len_result)
+decimal_binop (enum exp_opcode op,
+ const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
+ const gdb_byte *y, int len_y, enum bfd_endian byte_order_y,
+ gdb_byte *result, int len_result,
+ enum bfd_endian byte_order_result)
{
decContext set;
decNumber number1, number2, number3;
gdb_byte dec1[16], dec2[16], dec3[16];
- match_endianness (x, len_x, dec1);
- match_endianness (y, len_y, dec2);
+ match_endianness (x, len_x, byte_order_x, dec1);
+ match_endianness (y, len_y, byte_order_y, dec2);
decimal_to_number (dec1, len_x, &number1);
decimal_to_number (dec2, len_y, &number2);
decimal_from_number (&number3, dec3, len_result);
- match_endianness (dec3, len_result, result);
+ match_endianness (dec3, len_result, byte_order_result, result);
}
/* Returns true if X (which is LEN bytes wide) is the number zero. */
int
-decimal_is_zero (const gdb_byte *x, int len)
+decimal_is_zero (const gdb_byte *x, int len, enum bfd_endian byte_order)
{
decNumber number;
gdb_byte dec[16];
- match_endianness (x, len, dec);
+ match_endianness (x, len, byte_order, dec);
decimal_to_number (dec, len, &number);
return decNumberIsZero (&number);
will be -1. If they are equal, then the return value will be 0. If X is
greater than the Y then the return value will be 1. */
int
-decimal_compare (const gdb_byte *x, int len_x, const gdb_byte *y, int len_y)
+decimal_compare (const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
+ const gdb_byte *y, int len_y, enum bfd_endian byte_order_y)
{
decNumber number1, number2, result;
decContext set;
gdb_byte dec1[16], dec2[16];
int len_result;
- match_endianness (x, len_x, dec1);
- match_endianness (y, len_y, dec2);
+ match_endianness (x, len_x, byte_order_x, dec1);
+ match_endianness (y, len_y, byte_order_y, dec2);
decimal_to_number (dec1, len_x, &number1);
decimal_to_number (dec2, len_y, &number2);
/* Convert a decimal value from a decimal type with LEN_FROM bytes to a
decimal type with LEN_TO bytes. */
void
-decimal_convert (const gdb_byte *from, int len_from, gdb_byte *to,
- int len_to)
+decimal_convert (const gdb_byte *from, int len_from,
+ enum bfd_endian byte_order_from, gdb_byte *to, int len_to,
+ enum bfd_endian byte_order_to)
{
decNumber number;
gdb_byte dec[16];
- match_endianness (from, len_from, dec);
+ match_endianness (from, len_from, byte_order_from, dec);
decimal_to_number (dec, len_from, &number);
decimal_from_number (&number, dec, len_to);
- match_endianness (dec, len_to, to);
+ match_endianness (dec, len_to, byte_order_to, to);
}