Subject: libdecnumber support This patch provides functions to interact with libdecnumber and changes the Makefile to compile it. 2007-10-15 Wu Zhou Thiago Jung Bauermann * Makefile.in (LIBDECNUMBER_DIR, LIBDECNUMBER, LIBDECNUMBER_SRC LIBDECNUMBER_CFLAGS): New macros for libdecnumber. (INTERNAL_CFLAGS_BASE): Add LIBDECNUMBER_CFLAGS in. (INSTALLED_LIBS): Add -ldecnumber in. (CLIBS): Add LIBDECNUMBER in. (decimal128_h, decimal64_h, decimal32_h): New macros for decimal headers. (dfp_h): New macros for decimal floating point. (dfp.o): New target. (COMMON_OBS): Add dfp.o in. (c-exp.o): Add dfp_h as dependency. (valprint.o): Add dfp_h as dependency. (value.o): Add dfp_h as dependency. * dfp.h: New header file for decimal floating point support in GDB. * dfp.c: New source file for decimal floating point support in GDB. Implement decimal_from_string and decimal_to_string based on libdecnumber API. * configure.ac: Add AC_C_BIGENDIAN test. Index: gdb/Makefile.in =================================================================== --- gdb/Makefile.in.orig 2007-10-15 14:25:14.000000000 -0200 +++ gdb/Makefile.in 2007-10-15 14:35:06.000000000 -0200 @@ -125,6 +125,12 @@ BFD = $(BFD_DIR)/libbfd.a BFD_SRC = $(srcdir)/$(BFD_DIR) BFD_CFLAGS = -I$(BFD_DIR) -I$(BFD_SRC) +# Where is the decnumber library? Typically in ../libdecnumber. +LIBDECNUMBER_DIR = ../libdecnumber +LIBDECNUMBER = $(LIBDECNUMBER_DIR)/libdecnumber.a +LIBDECNUMBER_SRC = $(srcdir)/$(LIBDECNUMBER_DIR) +LIBDECNUMBER_CFLAGS = -I$(LIBDECNUMBER_DIR) -I$(LIBDECNUMBER_SRC) + # Where is the READLINE library? Typically in ../readline. READLINE_DIR = ../readline READLINE_SRC = $(srcdir)/$(READLINE_DIR) @@ -358,7 +364,7 @@ CXXFLAGS = -g -O INTERNAL_CFLAGS_BASE = \ $(CFLAGS) $(GLOBAL_CFLAGS) $(PROFILE_CFLAGS) \ $(GDB_CFLAGS) $(OPCODES_CFLAGS) $(READLINE_CFLAGS) \ - $(BFD_CFLAGS) $(INCLUDE_CFLAGS) \ + $(BFD_CFLAGS) $(INCLUDE_CFLAGS) $(LIBDECNUMBER_CFLAGS) \ $(INTL_CFLAGS) $(ENABLE_CFLAGS) $(INTERNAL_CPPFLAGS) INTERNAL_WARN_CFLAGS = $(INTERNAL_CFLAGS_BASE) $(GDB_WARN_CFLAGS) INTERNAL_CFLAGS = $(INTERNAL_WARN_CFLAGS) $(GDB_WERROR_CFLAGS) @@ -381,10 +387,10 @@ INTERNAL_LDFLAGS = $(CFLAGS) $(GLOBAL_CF # LIBIBERTY appears twice on purpose. # If you have the Cygnus libraries installed, # you can use 'CLIBS=$(INSTALLED_LIBS)' 'CDEPS=' -INSTALLED_LIBS=-lbfd -lreadline -lopcodes -liberty \ +INSTALLED_LIBS=-lbfd -lreadline -lopcodes -liberty -ldecnumber \ $(XM_CLIBS) $(TM_CLIBS) $(NAT_CLIBS) $(GDBTKLIBS) @LIBS@ \ -lintl -liberty -CLIBS = $(SIM) $(READLINE) $(OPCODES) $(BFD) $(INTL) $(LIBIBERTY) \ +CLIBS = $(SIM) $(READLINE) $(OPCODES) $(BFD) $(INTL) $(LIBIBERTY) $(LIBDECNUMBER) \ $(XM_CLIBS) $(TM_CLIBS) $(NAT_CLIBS) $(GDBTKLIBS) @LIBS@ \ $(LIBICONV) $(LIBEXPAT) \ $(LIBIBERTY) $(WIN32LIBS) @@ -624,6 +630,10 @@ safe_ctype_h = $(INCLUDE_DIR)/safe-ctyp hashtab_h = $(INCLUDE_DIR)/hashtab.h filenames_h = $(INCLUDE_DIR)/filenames.h +decimal128_h = $(LIBDECNUMBER_DIR)/dpd/decimal128.h +decimal64_h = $(LIBDECNUMBER_DIR)/dpd/decimal64.h +decimal32_h = $(LIBDECNUMBER_DIR)/dpd/decimal32.h + # # $BUILD/ headers # @@ -690,6 +700,7 @@ dictionary_h = dictionary.h disasm_h = disasm.h doublest_h = doublest.h $(floatformat_h) dummy_frame_h = dummy-frame.h +dfp_h = dfp.h dwarf2expr_h = dwarf2expr.h dwarf2_frame_h = dwarf2-frame.h dwarf2loc_h = dwarf2loc.h @@ -938,7 +949,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $ auxv.o \ bfd-target.o \ blockframe.o breakpoint.o findvar.o regcache.o \ - charset.o disasm.o dummy-frame.o \ + charset.o disasm.o dummy-frame.o dfp.o \ source.o value.o eval.o valops.o valarith.o valprint.o printcmd.o \ block.o symtab.o symfile.o symmisc.o linespec.o dictionary.o \ infcall.o \ @@ -1871,7 +1882,7 @@ buildsym.o: buildsym.c $(defs_h) $(bfd_h $(cp_support_h) $(dictionary_h) $(buildsym_h) $(stabsread_h) c-exp.o: c-exp.c $(defs_h) $(gdb_string_h) $(expression_h) $(value_h) \ $(parser_defs_h) $(language_h) $(c_lang_h) $(bfd_h) $(symfile_h) \ - $(objfiles_h) $(charset_h) $(block_h) $(cp_support_h) + $(objfiles_h) $(charset_h) $(block_h) $(cp_support_h) $(dfp_h) charset.o: charset.c $(defs_h) $(charset_h) $(gdbcmd_h) $(gdb_assert_h) \ $(gdb_string_h) c-lang.o: c-lang.c $(defs_h) $(symtab_h) $(gdbtypes_h) $(expression_h) \ @@ -1954,6 +1965,7 @@ dsrec.o: dsrec.c $(defs_h) $(serial_h) $ dummy-frame.o: dummy-frame.c $(defs_h) $(dummy_frame_h) $(regcache_h) \ $(frame_h) $(inferior_h) $(gdb_assert_h) $(frame_unwind_h) \ $(command_h) $(gdbcmd_h) $(gdb_string_h) +dfp.o: dfp.c $(defs_h) $(dfp_h) $(decimal128_h) $(decimal64_h) $(decimal32_h) dwarf2expr.o: dwarf2expr.c $(defs_h) $(symtab_h) $(gdbtypes_h) $(value_h) \ $(gdbcore_h) $(elf_dwarf2_h) $(dwarf2expr_h) dwarf2-frame.o: dwarf2-frame.c $(defs_h) $(dwarf2expr_h) $(elf_dwarf2_h) \ @@ -2815,11 +2827,11 @@ valops.o: valops.c $(defs_h) $(symtab_h) valprint.o: valprint.c $(defs_h) $(gdb_string_h) $(symtab_h) $(gdbtypes_h) \ $(value_h) $(gdbcore_h) $(gdbcmd_h) $(target_h) $(language_h) \ $(annotate_h) $(valprint_h) $(floatformat_h) $(doublest_h) \ - $(exceptions_h) + $(exceptions_h) $(dfp_h) value.o: value.c $(defs_h) $(gdb_string_h) $(symtab_h) $(gdbtypes_h) \ $(value_h) $(gdbcore_h) $(command_h) $(gdbcmd_h) $(target_h) \ $(language_h) $(demangle_h) $(doublest_h) \ - $(gdb_assert_h) $(regcache_h) $(block_h) + $(gdb_assert_h) $(regcache_h) $(block_h) $(dfp_h) varobj.o: varobj.c $(defs_h) $(exceptions_h) $(value_h) $(expression_h) \ $(frame_h) $(language_h) $(wrapper_h) $(gdbcmd_h) $(block_h) \ $(gdb_assert_h) $(gdb_string_h) $(varobj_h) $(vec_h) Index: gdb/dfp.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ gdb/dfp.c 2007-10-15 14:35:06.000000000 -0200 @@ -0,0 +1,113 @@ +/* Decimal floating point support for GDB. + + Copyright 2007 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "defs.h" + +/* The order of the following headers is important for making sure + decNumber structure is large enough to hold decimal128 digits. */ + +#include "dpd/decimal128.h" +#include "dpd/decimal64.h" +#include "dpd/decimal32.h" + +/* 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) +{ + int i; + +#if WORDS_BIGENDIAN +#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE +#else +#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG +#endif + + if (gdbarch_byte_order (current_gdbarch) == OPPOSITE_BYTE_ORDER) + for (i = 0; i < len; i++) + to[i] = from[len - i - 1]; + else + for (i = 0; i < len; i++) + to[i] = from[i]; + + return; +} + +/* 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) +{ + gdb_byte dec[16]; + + match_endianness (decbytes, len, dec); + switch (len) + { + case 4: + decimal32ToString ((decimal32 *) dec, s); + break; + case 8: + decimal64ToString ((decimal64 *) dec, s); + break; + case 16: + decimal128ToString ((decimal128 *) dec, s); + break; + default: + error (_("Unknown decimal floating point type.\n")); + break; + } +} + +/* 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) +{ + decContext set; + gdb_byte dec[16]; + + switch (len) + { + case 4: + decContextDefault (&set, DEC_INIT_DECIMAL32); + set.traps = 0; + decimal32FromString ((decimal32 *) dec, string, &set); + break; + case 8: + decContextDefault (&set, DEC_INIT_DECIMAL64); + set.traps = 0; + decimal64FromString ((decimal64 *) dec, string, &set); + break; + case 16: + decContextDefault (&set, DEC_INIT_DECIMAL128); + set.traps = 0; + decimal128FromString ((decimal128 *) dec, string, &set); + break; + default: + error (_("Unknown decimal floating point type.\n")); + break; + } + + match_endianness (dec, len, decbytes); + + return 1; +} Index: gdb/dfp.h =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ gdb/dfp.h 2007-10-15 14:35:06.000000000 -0200 @@ -0,0 +1,35 @@ +/* Decimal floating point support for GDB. + + Copyright 2007 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Decimal floating point is one of the extension to IEEE 754, which is + described in http://grouper.ieee.org/groups/754/revision.html and + http://www2.hursley.ibm.com/decimal/. It completes binary floating + point by representing floating point more exactly. */ + +#ifndef DFP_H +#define DFP_H + +/* When using decimal128, this is the maximum string length + 1 + * (value comes from libdecnumber's DECIMAL128_String constant). */ +#define MAX_DECIMAL_STRING 43 + +extern void decimal_to_string (const gdb_byte *, int, char *); +extern int decimal_from_string (gdb_byte *, int, const char *); + +#endif Index: gdb/configure.ac =================================================================== --- gdb/configure.ac.orig 2007-10-01 14:20:02.000000000 -0300 +++ gdb/configure.ac 2007-10-15 14:35:06.000000000 -0200 @@ -496,6 +496,7 @@ AC_CHECK_TYPES(uintptr_t, [], [], [#incl AC_C_CONST AC_C_INLINE +AC_C_BIGENDIAN # ------------------------------ # # Checks for library functions. #