* [PATCH] Unary plus
@ 2005-02-09 13:37 Nathan Sidwell
2005-02-27 4:22 ` Daniel Jacobowitz
0 siblings, 1 reply; 4+ messages in thread
From: Nathan Sidwell @ 2005-02-09 13:37 UTC (permalink / raw)
To: gdb-patches
[-- Attachment #1: Type: text/plain, Size: 448 bytes --]
Hi,
gdb doesn't parse or evaluate the unary plus operator (but does have one).
gdb.trace/collection.exp contains such an expression -- 'a[+b]'. This patch
adds the necessary parsing and evaluation machinery.
built & tested on i686-px-linux-gnu and an unreleased architecture. ok?
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk
[-- Attachment #2: unop_plus.patch --]
[-- Type: text/plain, Size: 4363 bytes --]
2005-02-09 Nathan Sidwell <nathan@codesourcery.com>
* ax-gdb.c (gen_expr): Add UNOP_PLUS case.
* c-exp.y (exp): Add unary plus.
* eval.c (evaluate_subexp_standard): Add UNOP_PLUS case.
* valarith.c (value_pos): New.
* value.h (value_pos): Declare.
Index: ax-gdb.c
===================================================================
RCS file: /cvs/src/src/gdb/ax-gdb.c,v
retrieving revision 1.27
diff -c -3 -p -r1.27 ax-gdb.c
*** ax-gdb.c 29 Jan 2005 17:53:25 -0000 1.27
--- ax-gdb.c 9 Feb 2005 09:55:10 -0000
*************** gen_expr (union exp_element **pc, struct
*** 1642,1647 ****
--- 1642,1654 ----
}
break;
+ case UNOP_PLUS:
+ (*pc)++;
+ /* + FOO is equivalent to 0 + FOO, which can be optimized. */
+ gen_expr (pc, ax, value);
+ gen_usual_unary (ax, value);
+ break;
+
case UNOP_NEG:
(*pc)++;
/* -FOO is equivalent to 0 - FOO. */
Index: c-exp.y
===================================================================
RCS file: /cvs/src/src/gdb/c-exp.y,v
retrieving revision 1.28
diff -c -3 -p -r1.28 c-exp.y
*** c-exp.y 7 Aug 2004 19:45:45 -0000 1.28
--- c-exp.y 9 Feb 2005 09:55:11 -0000
*************** exp : '-' exp %prec UNARY
*** 256,261 ****
--- 256,265 ----
{ write_exp_elt_opcode (UNOP_NEG); }
;
+ exp : '+' exp %prec UNARY
+ { write_exp_elt_opcode (UNOP_PLUS); }
+ ;
+
exp : '!' exp %prec UNARY
{ write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
;
Index: eval.c
===================================================================
RCS file: /cvs/src/src/gdb/eval.c,v
retrieving revision 1.49
diff -c -3 -p -r1.49 eval.c
*** eval.c 7 Feb 2005 00:09:53 -0000 1.49
--- eval.c 9 Feb 2005 09:55:16 -0000
*************** evaluate_subexp_standard (struct type *e
*** 1854,1859 ****
--- 1854,1868 ----
evaluate_subexp (NULL_TYPE, exp, pos, noside);
return evaluate_subexp (NULL_TYPE, exp, pos, noside);
+ case UNOP_PLUS:
+ arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
+ if (noside == EVAL_SKIP)
+ goto nosideret;
+ if (unop_user_defined_p (op, arg1))
+ return value_x_unop (arg1, op, noside);
+ else
+ return value_pos (arg1);
+
case UNOP_NEG:
arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
if (noside == EVAL_SKIP)
Index: valarith.c
===================================================================
RCS file: /cvs/src/src/gdb/valarith.c,v
retrieving revision 1.35
diff -c -3 -p -r1.35 valarith.c
*** valarith.c 7 Feb 2005 15:04:43 -0000 1.35
--- valarith.c 9 Feb 2005 09:55:25 -0000
*************** value_less (struct value *arg1, struct v
*** 1313,1319 ****
}
}
\f
! /* The unary operators - and ~. Both free the argument ARG1. */
struct value *
value_neg (struct value *arg1)
--- 1313,1349 ----
}
}
\f
! /* The unary operators +, - and ~. They free the argument ARG1
! (unless it is returned). */
!
! struct value *
! value_pos (struct value *arg1)
! {
! struct type *type;
!
! arg1 = coerce_ref (arg1);
!
! type = check_typedef (value_type (arg1));
!
! if (TYPE_CODE (type) == TYPE_CODE_FLT)
! return arg1;
! else if (is_integral_type (type))
! {
! /* Perform integral promotion for ANSI C/C++. FIXME: What about
! FORTRAN and (the deleted) chill ? */
! if (TYPE_LENGTH (type) >= TYPE_LENGTH (builtin_type_int))
! return arg1;
!
! type = builtin_type_int;
!
! return value_from_longest (type, value_as_long (arg1));
! }
! else
! {
! error ("Argument to positive operation not a number.");
! return 0; /* For lint -- never reached */
! }
! }
struct value *
value_neg (struct value *arg1)
Index: value.h
===================================================================
RCS file: /cvs/src/src/gdb/value.h,v
retrieving revision 1.72
diff -c -3 -p -r1.72 value.h
*** value.h 7 Feb 2005 15:04:43 -0000 1.72
--- value.h 9 Feb 2005 09:55:26 -0000
*************** extern struct value *value_addr (struct
*** 332,337 ****
--- 332,339 ----
extern struct value *value_assign (struct value *toval,
struct value *fromval);
+ extern struct value *value_pos (struct value *arg1);
+
extern struct value *value_neg (struct value *arg1);
extern struct value *value_complement (struct value *arg1);
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Unary plus
2005-02-09 13:37 [PATCH] Unary plus Nathan Sidwell
@ 2005-02-27 4:22 ` Daniel Jacobowitz
2005-03-08 10:51 ` Nathan Sidwell
0 siblings, 1 reply; 4+ messages in thread
From: Daniel Jacobowitz @ 2005-02-27 4:22 UTC (permalink / raw)
To: Nathan Sidwell; +Cc: gdb-patches
On Wed, Feb 09, 2005 at 10:16:17AM +0000, Nathan Sidwell wrote:
> Hi,
> gdb doesn't parse or evaluate the unary plus operator (but does have one).
> gdb.trace/collection.exp contains such an expression -- 'a[+b]'. This patch
> adds the necessary parsing and evaluation machinery.
>
> built & tested on i686-px-linux-gnu and an unreleased architecture. ok?
Two comments:
- Could you add a non-tracepoint testcase for this? I think one good
place would be gdb.cp/userdef.exp.
> ! /* The unary operators +, - and ~. They free the argument ARG1
> ! (unless it is returned). */
> !
> ! struct value *
> ! value_pos (struct value *arg1)
> ! {
> ! struct type *type;
> !
> ! arg1 = coerce_ref (arg1);
> !
> ! type = check_typedef (value_type (arg1));
> !
> ! if (TYPE_CODE (type) == TYPE_CODE_FLT)
> ! return arg1;
- IIUC, that's not quite right - we need an rvalue here.
--
Daniel Jacobowitz
CodeSourcery, LLC
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Unary plus
2005-02-27 4:22 ` Daniel Jacobowitz
@ 2005-03-08 10:51 ` Nathan Sidwell
2005-03-08 13:40 ` Daniel Jacobowitz
0 siblings, 1 reply; 4+ messages in thread
From: Nathan Sidwell @ 2005-03-08 10:51 UTC (permalink / raw)
To: Daniel Jacobowitz; +Cc: gdb-patches
[-- Attachment #1: Type: text/plain, Size: 595 bytes --]
Daniel Jacobowitz wrote:
> Two comments:
>
> - Could you add a non-tracepoint testcase for this? I think one good
> place would be gdb.cp/userdef.exp.
done. As this overloads operator+, I introduced a new class for
the breakpoint test.
>>! if (TYPE_CODE (type) == TYPE_CODE_FLT)
>>! return arg1;
>
>
> - IIUC, that's not quite right - we need an rvalue here.
Like this?
built and tested on i686-pc-linux-gnu, ok?
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk
[-- Attachment #2: unop_plus-2.patch --]
[-- Type: text/plain, Size: 7600 bytes --]
2005-03-08 Nathan Sidwell <nathan@codesourcery.com>
* ax-gdb.c (gen_expr): Add UNOP_PLUS case.
* c-exp.y (exp): Add unary plus.
* eval.c (evaluate_subexp_standard): Add UNOP_PLUS case.
* valarith.c (value_x_unop): Add UNOP_PLUS case.
(value_pos): New.
* value.h (value_pos): Declare.
* gdb.cp/userdef.cc (A1::operator+): New unary plus.
(A2): New class.
(main): Test operator+.
* gdb.cp/userdef.exp: Test unary plus. Use A2::operator+ for
breakpoint test.
Index: ax-gdb.c
===================================================================
RCS file: /cvs/src/src/gdb/ax-gdb.c,v
retrieving revision 1.28
diff -c -3 -p -r1.28 ax-gdb.c
*** ax-gdb.c 7 Feb 2005 23:51:02 -0000 1.28
--- ax-gdb.c 8 Mar 2005 10:40:15 -0000
*************** gen_expr (union exp_element **pc, struct
*** 1642,1647 ****
--- 1642,1654 ----
}
break;
+ case UNOP_PLUS:
+ (*pc)++;
+ /* + FOO is equivalent to 0 + FOO, which can be optimized. */
+ gen_expr (pc, ax, value);
+ gen_usual_unary (ax, value);
+ break;
+
case UNOP_NEG:
(*pc)++;
/* -FOO is equivalent to 0 - FOO. */
Index: c-exp.y
===================================================================
RCS file: /cvs/src/src/gdb/c-exp.y,v
retrieving revision 1.28
diff -c -3 -p -r1.28 c-exp.y
*** c-exp.y 7 Aug 2004 19:45:45 -0000 1.28
--- c-exp.y 8 Mar 2005 10:40:20 -0000
*************** exp : '-' exp %prec UNARY
*** 256,261 ****
--- 256,265 ----
{ write_exp_elt_opcode (UNOP_NEG); }
;
+ exp : '+' exp %prec UNARY
+ { write_exp_elt_opcode (UNOP_PLUS); }
+ ;
+
exp : '!' exp %prec UNARY
{ write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
;
Index: eval.c
===================================================================
RCS file: /cvs/src/src/gdb/eval.c,v
retrieving revision 1.53
diff -c -3 -p -r1.53 eval.c
*** eval.c 11 Feb 2005 04:05:46 -0000 1.53
--- eval.c 8 Mar 2005 10:40:25 -0000
*************** evaluate_subexp_standard (struct type *e
*** 1855,1860 ****
--- 1855,1869 ----
evaluate_subexp (NULL_TYPE, exp, pos, noside);
return evaluate_subexp (NULL_TYPE, exp, pos, noside);
+ case UNOP_PLUS:
+ arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
+ if (noside == EVAL_SKIP)
+ goto nosideret;
+ if (unop_user_defined_p (op, arg1))
+ return value_x_unop (arg1, op, noside);
+ else
+ return value_pos (arg1);
+
case UNOP_NEG:
arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
if (noside == EVAL_SKIP)
Index: valarith.c
===================================================================
RCS file: /cvs/src/src/gdb/valarith.c,v
retrieving revision 1.38
diff -c -3 -p -r1.38 valarith.c
*** valarith.c 11 Feb 2005 04:06:08 -0000 1.38
--- valarith.c 8 Mar 2005 10:40:29 -0000
*************** value_x_unop (struct value *arg1, enum e
*** 560,565 ****
--- 560,568 ----
case UNOP_NEG:
strcpy (ptr, "-");
break;
+ case UNOP_PLUS:
+ strcpy (ptr, "+");
+ break;
case UNOP_IND:
strcpy (ptr, "*");
break;
*************** value_less (struct value *arg1, struct v
*** 1313,1319 ****
}
}
\f
! /* The unary operators - and ~. Both free the argument ARG1. */
struct value *
value_neg (struct value *arg1)
--- 1316,1350 ----
}
}
\f
! /* The unary operators +, - and ~. They free the argument ARG1
! (unless it is returned). */
!
! struct value *
! value_pos (struct value *arg1)
! {
! struct type *type;
!
! arg1 = coerce_ref (arg1);
!
! type = check_typedef (value_type (arg1));
!
! if (TYPE_CODE (type) == TYPE_CODE_FLT)
! return value_from_double (type, value_as_double (arg1));
! else if (is_integral_type (type))
! {
! /* Perform integral promotion for ANSI C/C++. FIXME: What about
! FORTRAN and (the deleted) chill ? */
! if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
! type = builtin_type_int;
!
! return value_from_longest (type, value_as_long (arg1));
! }
! else
! {
! error ("Argument to positive operation not a number.");
! return 0; /* For lint -- never reached */
! }
! }
struct value *
value_neg (struct value *arg1)
Index: value.h
===================================================================
RCS file: /cvs/src/src/gdb/value.h,v
retrieving revision 1.84
diff -c -3 -p -r1.84 value.h
*** value.h 28 Feb 2005 17:00:49 -0000 1.84
--- value.h 8 Mar 2005 10:40:30 -0000
*************** extern struct value *value_addr (struct
*** 326,331 ****
--- 326,333 ----
extern struct value *value_assign (struct value *toval,
struct value *fromval);
+ extern struct value *value_pos (struct value *arg1);
+
extern struct value *value_neg (struct value *arg1);
extern struct value *value_complement (struct value *arg1);
Index: testsuite/gdb.cp/userdef.cc
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.cp/userdef.cc,v
retrieving revision 1.2
diff -c -3 -p -r1.2 userdef.cc
*** testsuite/gdb.cp/userdef.cc 12 Jun 2004 16:45:49 -0000 1.2
--- testsuite/gdb.cp/userdef.cc 8 Mar 2005 10:40:35 -0000
*************** A1 operator/(const A1&);
*** 63,68 ****
--- 63,69 ----
A1 operator=(const A1&);
A1 operator~();
+ A1 operator+();
A1 operator-();
int operator!();
A1 operator++();
*************** A1 A1::operator-(void)
*** 225,230 ****
--- 226,240 ----
return (neg);
}
+ A1 A1::operator+(void)
+ {
+ A1 pos(0,0);
+ pos.x = +x;
+ pos.y = +y;
+
+ return (pos);
+ }
+
A1 A1::operator~(void)
{
A1 acompl(0,0);
*************** ostream& operator<<(ostream& outs, A1 on
*** 286,291 ****
--- 296,312 ----
return (outs << endl << "x = " << one.x << endl << "y = " << one.y << endl << "-------" << endl);
}
+ class A2 {
+ public:
+ A2 operator+();
+ };
+
+ A2 A2::operator+()
+ {
+ return A2 ();
+ }
+
+
int main (void)
{
A1 one(2,3);
*************** int main (void)
*** 342,347 ****
--- 363,370 ----
val = (!one);
cout << "! " << val << endl << "-----"<<endl;
+ three = (+one);
+ cout << "+ " << three;
three = (-one);
cout << "- " << three;
three = (~one);
Index: testsuite/gdb.cp/userdef.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.cp/userdef.exp,v
retrieving revision 1.2
diff -c -3 -p -r1.2 userdef.exp
*** testsuite/gdb.cp/userdef.exp 11 Feb 2004 14:01:25 -0000 1.2
--- testsuite/gdb.cp/userdef.exp 8 Mar 2005 10:40:35 -0000
*************** gdb_test "print one >> 31" "\\\$\[0-9\]*
*** 121,126 ****
--- 121,128 ----
gdb_test "print !one" "\\\$\[0-9\]* = 0\[\r\n\]"
# Assumes 2's complement. So does everything...
+ gdb_test "print +one" "\\\$\[0-9\]* = {x = 2, y = 3}"
+
gdb_test "print ~one" "\\\$\[0-9\]* = {x = -3, y = -4}"
gdb_test "print -one" "\\\$\[0-9\]* = {x = -2, y = -3}"
*************** gdb_test "print one += 7" "\\\$\[0-9\]*
*** 138,145 ****
gdb_test "print two = one" "\\\$\[0-9\]* = {x = 9, y = 10}"
# Check that GDB tolerates whitespace in operator names.
! gdb_test "break A1::'operator+'" ".*Breakpoint $decimal at.*"
! gdb_test "break A1::'operator +'" ".*Breakpoint $decimal at.*"
gdb_exit
return 0
--- 140,147 ----
gdb_test "print two = one" "\\\$\[0-9\]* = {x = 9, y = 10}"
# Check that GDB tolerates whitespace in operator names.
! gdb_test "break A2::'operator+'" ".*Breakpoint $decimal at.*"
! gdb_test "break A2::'operator +'" ".*Breakpoint $decimal at.*"
gdb_exit
return 0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Unary plus
2005-03-08 10:51 ` Nathan Sidwell
@ 2005-03-08 13:40 ` Daniel Jacobowitz
0 siblings, 0 replies; 4+ messages in thread
From: Daniel Jacobowitz @ 2005-03-08 13:40 UTC (permalink / raw)
To: Nathan Sidwell; +Cc: gdb-patches
On Tue, Mar 08, 2005 at 10:50:53AM +0000, Nathan Sidwell wrote:
> Daniel Jacobowitz wrote:
> >Two comments:
> >
> >- Could you add a non-tracepoint testcase for this? I think one good
> >place would be gdb.cp/userdef.exp.
> done. As this overloads operator+, I introduced a new class for
> the breakpoint test.
>
>
> >>! if (TYPE_CODE (type) == TYPE_CODE_FLT)
> >>! return arg1;
> >
> >
> >- IIUC, that's not quite right - we need an rvalue here.
>
> Like this?
>
> built and tested on i686-pc-linux-gnu, ok?
OK. This comment isn't quite right after the rvalue fix:
> ! /* The unary operators - and ~. Both free the argument ARG1. */
>
> struct value *
> value_neg (struct value *arg1)
> --- 1316,1350 ----
> }
> }
> \f
> ! /* The unary operators +, - and ~. They free the argument ARG1
> ! (unless it is returned). */
--
Daniel Jacobowitz
CodeSourcery, LLC
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2005-03-08 13:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-09 13:37 [PATCH] Unary plus Nathan Sidwell
2005-02-27 4:22 ` Daniel Jacobowitz
2005-03-08 10:51 ` Nathan Sidwell
2005-03-08 13:40 ` Daniel Jacobowitz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox