From patchwork Tue Feb 12 16:11:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 31419 Received: (qmail 68848 invoked by alias); 12 Feb 2019 16:11:27 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 68766 invoked by uid 89); 12 Feb 2019 16:11:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=vary, mathh, math.h, UD:math.h X-HELO: mail-wr1-f68.google.com Received: from mail-wr1-f68.google.com (HELO mail-wr1-f68.google.com) (209.85.221.68) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 12 Feb 2019 16:11:24 +0000 Received: by mail-wr1-f68.google.com with SMTP id t27so3296515wra.6 for ; Tue, 12 Feb 2019 08:11:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=jZWpJarIXDnmR7zBDgMQylgmxdyMcjOfZwVIzXg3W9I=; b=dBblv8GjRlIxDZAg1oHpV7oZXqZ6JXSoAdRGWrDcAEbW9UKl8CbXatQEI7Jc/62xg1 tX7YptmSWea+yiPegzVgwRojTZ1VmfNt5r+aH3x1OEDkhg8/vncP9+rtkMAAAVZhZywv 3qpOO4jhkU7v4pR3WuLBtNBzrgMyqdiRM2dxmccR66PK7jllavIOT/kkRjpIqn+JUEDx nQ9T3dIasraeK2w+tSCX9KrrCPtwlFQAipcRBPA6HYGon5MLUBjZ44AF+CkytaYPajnX yJeoOBynJnt6y7iPFYPPYjCz85kPIViZolzt5BtzK65HMReD/Ncnsk6DRKxbM6pLN2R6 Oikg== Return-Path: Received: from localhost (host81-151-161-9.range81-151.btcentralplus.com. [81.151.161.9]) by smtp.gmail.com with ESMTPSA id t199sm6106400wmt.1.2019.02.12.08.11.20 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 12 Feb 2019 08:11:20 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Richard Bunt , Andrew Burgess Subject: [PATCH 10/11] gdb/fortran: Add support for the ABS intrinsic function Date: Tue, 12 Feb 2019 16:11:01 +0000 Message-Id: <1083edb8a3e483558635f172e37e88eeae210149.1549986233.git.andrew.burgess@embecosm.com> In-Reply-To: References: In-Reply-To: References: X-IsSubscribed: yes Adds support for the abs intrinsic function, this requires adding a new pattern to the Fortran parser. Currently only float and integer argument types are supported to ABS, complex is still not supported, this can be added later if needed. gdb/ChangeLog: * f-exp.y: New token, UNOP_INTRINSIC. (exp): New pattern using UNOP_INTRINSIC token. (f77_keywords): Add 'abs' keyword. * f-lang.c: Add 'target-float.h' and 'math.h' includes. (value_from_host_double): New function. (evaluate_subexp_f): Support UNOP_ABS. gdb/testsuite/ChangeLog: * gdb.fortran/intrinsics.exp: Extend to cover ABS. --- gdb/ChangeLog | 11 +++++++++ gdb/f-exp.y | 8 ++++++- gdb/f-lang.c | 39 ++++++++++++++++++++++++++++++++ gdb/testsuite/ChangeLog | 4 ++++ gdb/testsuite/gdb.fortran/intrinsics.exp | 9 ++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/gdb/f-exp.y b/gdb/f-exp.y index d256ff14c1e..88c685a0af3 100644 --- a/gdb/f-exp.y +++ b/gdb/f-exp.y @@ -168,6 +168,7 @@ static int parse_number (struct parser_state *, const char *, int, %token DOLLAR_VARIABLE %token ASSIGN_MODIFY +%token UNOP_INTRINSIC %left ',' %left ABOVE_COMMA @@ -252,6 +253,10 @@ exp : exp '(' OP_F77_UNDETERMINED_ARGLIST); } ; +exp : UNOP_INTRINSIC '(' exp ')' + { write_exp_elt_opcode (pstate, $1); } + ; + arglist : ; @@ -945,7 +950,8 @@ static const struct token f77_keywords[] = { "real", REAL_KEYWORD, BINOP_END, true }, /* The following correspond to actual functions in Fortran and are case insensitive. */ - { "kind", KIND, BINOP_END, false } + { "kind", KIND, BINOP_END, false }, + { "abs", UNOP_INTRINSIC, UNOP_ABS, false } }; /* Implementation of a dynamically expandable buffer for processing input diff --git a/gdb/f-lang.c b/gdb/f-lang.c index cbb3ad0f8a6..7aced533bce 100644 --- a/gdb/f-lang.c +++ b/gdb/f-lang.c @@ -33,7 +33,9 @@ #include "cp-support.h" #include "charset.h" #include "c-lang.h" +#include "target-float.h" +#include /* Local functions */ @@ -238,6 +240,20 @@ f_collect_symbol_completion_matches (completion_tracker &tracker, text, word, ":", code); } +/* Create and return a value object of TYPE containing the value D. The + TYPE must be of TYPE_CODE_FLT, and must be large enough to hold D once + it is converted to target format. */ + +static struct value * +value_from_host_double (struct type *type, double d) +{ + struct value *value = allocate_value (type); + gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT); + target_float_from_host_double (value_contents_raw (value), + value_type (value), d); + return value; +} + /* Special expression evaluation cases for Fortran. */ struct value * evaluate_subexp_f (struct type *expect_type, struct expression *exp, @@ -258,6 +274,29 @@ evaluate_subexp_f (struct type *expect_type, struct expression *exp, *pos -= 1; return evaluate_subexp_standard (expect_type, exp, pos, noside); + case UNOP_ABS: + arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside); + if (noside == EVAL_SKIP) + return eval_skip_value (exp); + type = value_type (arg1); + switch (TYPE_CODE (type)) + { + case TYPE_CODE_FLT: + { + double d + = fabs (target_float_to_host_double (value_contents (arg1), + value_type (arg1))); + return value_from_host_double (type, d); + } + case TYPE_CODE_INT: + { + LONGEST l = value_as_long (arg1); + l = llabs (l); + return value_from_longest (type, l); + } + } + error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type)); + case UNOP_KIND: arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS); type = value_type (arg1); diff --git a/gdb/testsuite/gdb.fortran/intrinsics.exp b/gdb/testsuite/gdb.fortran/intrinsics.exp index 674f299c428..00396c74c2f 100644 --- a/gdb/testsuite/gdb.fortran/intrinsics.exp +++ b/gdb/testsuite/gdb.fortran/intrinsics.exp @@ -40,3 +40,12 @@ gdb_test "p kind (l2)" " = 2" gdb_test "p kind (l4)" " = 4" gdb_test "p kind (l8)" " = 8" gdb_test "p kind (s1)" "argument to kind must be an intrinsic type" + +# Test ABS + +gdb_test "p abs (-11)" " = 11" +gdb_test "p abs (11)" " = 11" +# Use `$decimal` to match here as we depend on host floating point +# rounding, which can vary. +gdb_test "p abs (-9.1)" " = 9.$decimal" +gdb_test "p abs (9.1)" " = 9.$decimal"