aboutsummaryrefslogtreecommitdiff
path: root/gdb/parse.c
diff options
context:
space:
mode:
authorUlrich Weigand <ulrich.weigand@de.ibm.com>2017-10-25 15:32:23 +0200
committerUlrich Weigand <ulrich.weigand@de.ibm.com>2017-10-25 15:32:23 +0200
commitedd079d9f6ca2f9ad21322b742269aec5de61190 (patch)
treece27c78e6d4f1b598e0e351ba69ba55e1281d649 /gdb/parse.c
parente5d70d6b5a5c2832ad199ac1b91f68324b4a12c9 (diff)
downloadgdb-edd079d9f6ca2f9ad21322b742269aec5de61190.zip
gdb-edd079d9f6ca2f9ad21322b742269aec5de61190.tar.gz
gdb-edd079d9f6ca2f9ad21322b742269aec5de61190.tar.bz2
Target FP: Use target format throughout expression parsing
When parsing floating-point literals, the language parsers currently use parse_float or some equivalent routine to parse the input string into a DOUBLEST, which is then stored within a OP_DOUBLE expression node. When evaluating the expression, the OP_DOUBLE is finally converted into a value in target format. On the other hand, *decimal* floating-point literals are parsed directly into target format and stored that way in a OP_DECFLOAT expression node. In order to eliminate the DOUBLEST, this patch therefore unifies the handling of binary and decimal floating- point literals and stores them both in target format within a new OP_FLOAT expression node, replacing both OP_DOUBLE and OP_DECFLOAT. In order to store literals in target format, the parse_float routine needs to know the type of the literal. All parsers therefore need to be changed to determine the appropriate type (e.g. by detecting suffixes) *before* calling parse_float, instead of after it as today. However, this change is mostly straightforward -- again, this is already done for decimal FP today. The core of the literal parsing is moved into a new routine floatformat_from_string, mirroring floatformat_to_string. The parse_float routine now calls either floatformat_from_string or decimal_from_sting, allowing it to handle any type of FP literal. All language parsers need to be updated. Some notes on specific changes to the various languages: - C: Decimal FP is now handled in parse_float, and no longer needs to be handled specially. - D: Straightforward. - Fortran: Still used a hard-coded "atof", also replaced by parse_float now. Continues to always use builtin_real_s8 as the type of literal, even though this is probably wrong. - Go: This used to handle "f" and "l" suffixes, even though the Go language actually doesn't support those. I kept this support for now -- maybe revisit later. Note the the GDB test suite for some reason actually *verifies* that GDB supports those unsupported suffixes ... - Pascal: Likewise -- this handles suffixes that are not supported in the language standard. - Modula-2: Like Fortran, used to use "atof". - Rust: Mostly straightforward, except for a unit-testing hitch. The code use to set a special "unit_testing" flag which would cause "rust_type" to always return NULL. This makes it not possible to encode a literal into target format (which type?). The reason for this flag appears to have been that during unit testing, there is no "rust_parser" context set up, which means no "gdbarch" is available to use its types. To fix this, I removed the unit_testing flag, and instead simply just set up a dummy rust_parser context during unit testing. - Ada: This used to check sizeof (DOUBLEST) to determine which type to use for floating-point literal. This seems questionable to begin with (since DOUBLEST is quite unrelated to target formats), and in any case we need to get rid of DOUBLEST. I'm now simply always using the largest type (builtin_long_double). gdb/ChangeLog: 2017-10-25 Ulrich Weigand <uweigand@de.ibm.com> * doublest.c (floatformat_from_string): New function. * doublest.h (floatformat_from_string): Add prototype. * std-operator.def (OP_DOUBLE, OP_DECFLOAT): Remove, replace by ... (OP_FLOAT): ... this. * expression.h: Do not include "doublest.h". (union exp_element): Replace doubleconst and decfloatconst by new element floatconst. * ada-lang.c (resolve_subexp): Handle OP_FLOAT instead of OP_DOUBLE. (ada_evaluate_subexp): Likewise. * eval.c (evaluate_subexp_standard): Handle OP_FLOAT instead of OP_DOUBLE and OP_DECFLOAT. * expprint.c (print_subexp_standard): Likewise. (dump_subexp_body_standard): Likewise. * breakpoint.c (watchpoint_exp_is_const): Likewise. * parse.c: Include "dfp.h". (write_exp_elt_dblcst, write_exp_elt_decfloatcst): Remove. (write_exp_elt_floatcst): New function. (operator_length_standard): Handle OP_FLOAT instead of OP_DOUBLE and OP_DECFLOAT. (operator_check_standard): Likewise. (parse_float): Do not accept suffix. Take type as input. Return bool. Return target format buffer instead of host DOUBLEST. Use floatformat_from_string and decimal_from_string to parse either binary or decimal floating-point types. (parse_c_float): Remove. * parser-defs.h: Do not include "doublest.h". (write_exp_elt_dblcst, write_exp_elt_decfloatcst): Remove. (write_exp_elt_floatcst): Add prototype. (parse_float): Update prototype. (parse_c_float): Remove. * c-exp.y: Do not include "dfp.h". (typed_val_float): Use byte buffer instead of DOUBLEST. (typed_val_decfloat): Remove. (DECFLOAT): Remove. (FLOAT): Use OP_FLOAT and write_exp_elt_floatcst. (parse_number): Update to new parse_float interface. Parse suffixes and determine type before calling parse_float. Handle decimal and binary FP types the same way. * d-exp.y (typed_val_float): Use byte buffer instead of DOUBLEST. (FLOAT_LITERAL): Use OP_FLOAT and write_exp_elt_floatcst. (parse_number): Update to new parse_float interface. Parse suffixes and determine type before calling parse_float. * f-exp.y: Replace dval by typed_val_float. (FLOAT): Use OP_FLOAT and write_exp_elt_floatcst. (parse_number): Use parse_float instead of atof. * go-exp.y (typed_val_float): Use byte buffer instead of DOUBLEST. (parse_go_float): Remove. (FLOAT): Use OP_FLOAT and write_exp_elt_floatcst. (parse_number): Call parse_float instead of parse_go_float. Parse suffixes and determine type before calling parse_float. * p-exp.y (typed_val_float): Use byte buffer instead of DOUBLEST. (FLOAT): Use OP_FLOAT and write_exp_elt_floatcst. (parse_number): Update to new parse_float interface. Parse suffixes and determine type before calling parse_float. * m2-exp.y: Replace dval by byte buffer val. (FLOAT): Use OP_FLOAT and write_exp_elt_floatcst. (parse_number): Call parse_float instead of atof. * rust-exp.y (typed_val_float): Use byte buffer instead of DOUBLEST. (lex_number): Call parse_float instead of strtod. (ast_dliteral): Use OP_FLOAT instead of OP_DOUBLE. (convert_ast_to_expression): Handle OP_FLOAT instead of OP_DOUBLE. Use write_exp_elt_floatcst. (unit_testing): Remove static variable. (rust_type): Do not check unit_testing. (rust_lex_tests): Do not set uint_testing. Set up dummy rust_parser. * ada-exp.y (type_float, type_double): Remove. (typed_val_float): Use byte buffer instead of DOUBLEST. (FLOAT): Use OP_FLOAT and write_exp_elt_floatcst. * ada-lex.l (processReal): Use parse_float instead of sscanf.
Diffstat (limited to 'gdb/parse.c')
-rw-r--r--gdb/parse.c91
1 files changed, 17 insertions, 74 deletions
diff --git a/gdb/parse.c b/gdb/parse.c
index 6bbf25f..a4058c3 100644
--- a/gdb/parse.c
+++ b/gdb/parse.c
@@ -45,6 +45,7 @@
#include "symfile.h" /* for overlay functions */
#include "inferior.h"
#include "doublest.h"
+#include "dfp.h"
#include "block.h"
#include "source.h"
#include "objfiles.h"
@@ -264,23 +265,13 @@ write_exp_elt_longcst (struct parser_state *ps, LONGEST expelt)
}
void
-write_exp_elt_dblcst (struct parser_state *ps, DOUBLEST expelt)
-{
- union exp_element tmp;
-
- memset (&tmp, 0, sizeof (union exp_element));
- tmp.doubleconst = expelt;
- write_exp_elt (ps, &tmp);
-}
-
-void
-write_exp_elt_decfloatcst (struct parser_state *ps, gdb_byte expelt[16])
+write_exp_elt_floatcst (struct parser_state *ps, const gdb_byte expelt[16])
{
union exp_element tmp;
int index;
for (index = 0; index < 16; index++)
- tmp.decfloatconst[index] = expelt[index];
+ tmp.floatconst[index] = expelt[index];
write_exp_elt (ps, &tmp);
}
@@ -870,8 +861,7 @@ operator_length_standard (const struct expression *expr, int endpos,
break;
case OP_LONG:
- case OP_DOUBLE:
- case OP_DECFLOAT:
+ case OP_FLOAT:
case OP_VAR_VALUE:
case OP_VAR_MSYM_VALUE:
oplen = 4;
@@ -1338,69 +1328,23 @@ null_post_parser (struct expression **exp, int void_context_p)
}
/* Parse floating point value P of length LEN.
- Return 0 (false) if invalid, 1 (true) if valid.
- The successfully parsed number is stored in D.
- *SUFFIX points to the suffix of the number in P.
+ Return false if invalid, true if valid.
+ The successfully parsed number is stored in DATA in
+ target format for floating-point type TYPE.
NOTE: This accepts the floating point syntax that sscanf accepts. */
-int
-parse_float (const char *p, int len, DOUBLEST *d, const char **suffix)
-{
- char *copy;
- int n, num;
-
- copy = (char *) xmalloc (len + 1);
- memcpy (copy, p, len);
- copy[len] = 0;
-
- num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
- xfree (copy);
-
- /* The sscanf man page suggests not making any assumptions on the effect
- of %n on the result, so we don't.
- That is why we simply test num == 0. */
- if (num == 0)
- return 0;
-
- *suffix = p + n;
- return 1;
-}
-
-/* Parse floating point value P of length LEN, using the C syntax for floats.
- Return 0 (false) if invalid, 1 (true) if valid.
- The successfully parsed number is stored in *D.
- Its type is taken from builtin_type (gdbarch) and is stored in *T. */
-
-int
-parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
- DOUBLEST *d, struct type **t)
+bool
+parse_float (const char *p, int len,
+ const struct type *type, gdb_byte *data)
{
- const char *suffix;
- int suffix_len;
- const struct builtin_type *builtin_types = builtin_type (gdbarch);
-
- if (! parse_float (p, len, d, &suffix))
- return 0;
-
- suffix_len = p + len - suffix;
-
- if (suffix_len == 0)
- *t = builtin_types->builtin_double;
- else if (suffix_len == 1)
- {
- /* Handle suffixes: 'f' for float, 'l' for long double. */
- if (tolower (*suffix) == 'f')
- *t = builtin_types->builtin_float;
- else if (tolower (*suffix) == 'l')
- *t = builtin_types->builtin_long_double;
- else
- return 0;
- }
+ if (TYPE_CODE (type) == TYPE_CODE_FLT)
+ return floatformat_from_string (floatformat_from_type (type),
+ data, std::string (p, len));
else
- return 0;
-
- return 1;
+ return decimal_from_string (data, TYPE_LENGTH (type),
+ gdbarch_byte_order (get_type_arch (type)),
+ std::string (p, len));
}
/* Stuff for maintaining a stack of types. Currently just used by C, but
@@ -1808,8 +1752,7 @@ operator_check_standard (struct expression *exp, int pos,
{
case BINOP_VAL:
case OP_COMPLEX:
- case OP_DECFLOAT:
- case OP_DOUBLE:
+ case OP_FLOAT:
case OP_LONG:
case OP_SCOPE:
case OP_TYPE: