1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright (C) 1986, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GDB is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "expression.h"
28 value
value_x_binop ();
29 value
value_subscripted_rvalue ();
32 value_add (arg1
, arg2
)
35 register value val
, valint
, valptr
;
41 if ((TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
42 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_PTR
)
44 (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
45 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
))
46 /* Exactly one argument is a pointer, and one is an integer. */
48 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
58 len
= TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr
)));
59 if (len
== 0) len
= 1; /* For (void *) */
60 val
= value_from_long (builtin_type_long
,
61 value_as_long (valptr
)
62 + (len
* value_as_long (valint
)));
63 VALUE_TYPE (val
) = VALUE_TYPE (valptr
);
67 return value_binop (arg1
, arg2
, BINOP_ADD
);
71 value_sub (arg1
, arg2
)
79 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
81 if (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
)
83 /* pointer - integer. */
87 - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)))
88 * value_as_long (arg2
)));
89 VALUE_TYPE (val
) = VALUE_TYPE (arg1
);
92 else if (VALUE_TYPE (arg1
) == VALUE_TYPE (arg2
))
94 /* pointer to <type x> - pointer to <type x>. */
97 (value_as_long (arg1
) - value_as_long (arg2
))
98 / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))));
104 First argument of `-' is a pointer and second argument is neither\n\
105 an integer nor a pointer of the same type.");
109 return value_binop (arg1
, arg2
, BINOP_SUB
);
112 /* Return the value of ARRAY[IDX]. */
115 value_subscript (array
, idx
)
118 if (TYPE_CODE (VALUE_TYPE (array
)) == TYPE_CODE_ARRAY
119 && VALUE_LVAL (array
) != lval_memory
)
120 return value_subscripted_rvalue (array
, idx
);
122 return value_ind (value_add (array
, idx
));
125 /* Return the value of EXPR[IDX], expr an aggregate rvalue
126 (eg, a vector register). This routine used to promote floats
127 to doubles, but no longer does. */
130 value_subscripted_rvalue (array
, idx
)
133 struct type
*elt_type
= TYPE_TARGET_TYPE (VALUE_TYPE (array
));
134 int elt_size
= TYPE_LENGTH (elt_type
);
135 int elt_offs
= elt_size
* value_as_long (idx
);
138 if (elt_offs
>= TYPE_LENGTH (VALUE_TYPE (array
)))
139 error ("no such vector element");
141 v
= allocate_value (elt_type
);
142 bcopy (VALUE_CONTENTS (array
) + elt_offs
, VALUE_CONTENTS (v
), elt_size
);
144 if (VALUE_LVAL (array
) == lval_internalvar
)
145 VALUE_LVAL (v
) = lval_internalvar_component
;
147 VALUE_LVAL (v
) = not_lval
;
148 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
149 VALUE_OFFSET (v
) = VALUE_OFFSET (array
) + elt_offs
;
150 VALUE_BITSIZE (v
) = elt_size
* 8;
154 /* Check to see if either argument is a structure. This is called so
155 we know whether to go ahead with the normal binop or look for a
156 user defined function instead.
158 For now, we do not overload the `=' operator. */
161 binop_user_defined_p (op
, arg1
, arg2
)
165 if (op
== BINOP_ASSIGN
)
167 return (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRUCT
168 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_STRUCT
169 || (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
170 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) == TYPE_CODE_STRUCT
)
171 || (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_REF
172 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) == TYPE_CODE_STRUCT
));
175 /* Check to see if argument is a structure. This is called so
176 we know whether to go ahead with the normal unop or look for a
177 user defined function instead.
179 For now, we do not overload the `&' operator. */
181 int unop_user_defined_p (op
, arg1
)
187 return (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRUCT
188 || (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
189 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) == TYPE_CODE_STRUCT
));
192 /* We know either arg1 or arg2 is a structure, so try to find the right
193 user defined function. Create an argument vector that calls
194 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
195 binary operator which is legal for GNU C++). */
198 value_x_binop (arg1
, arg2
, op
, otherop
)
200 enum exp_opcode op
, otherop
;
210 /* now we know that what we have to do is construct our
211 arg vector and find the right function to call it with. */
213 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_STRUCT
)
214 error ("Can't do that binary op on that type"); /* FIXME be explicit */
216 argvec
= (value
*) alloca (sizeof (value
) * 4);
217 argvec
[1] = value_addr (arg1
);
221 /* make the right function name up */
222 strcpy(tstr
, "operator__");
226 case BINOP_ADD
: strcpy(ptr
,"+"); break;
227 case BINOP_SUB
: strcpy(ptr
,"-"); break;
228 case BINOP_MUL
: strcpy(ptr
,"*"); break;
229 case BINOP_DIV
: strcpy(ptr
,"/"); break;
230 case BINOP_REM
: strcpy(ptr
,"%"); break;
231 case BINOP_LSH
: strcpy(ptr
,"<<"); break;
232 case BINOP_RSH
: strcpy(ptr
,">>"); break;
233 case BINOP_LOGAND
: strcpy(ptr
,"&"); break;
234 case BINOP_LOGIOR
: strcpy(ptr
,"|"); break;
235 case BINOP_LOGXOR
: strcpy(ptr
,"^"); break;
236 case BINOP_AND
: strcpy(ptr
,"&&"); break;
237 case BINOP_OR
: strcpy(ptr
,"||"); break;
238 case BINOP_MIN
: strcpy(ptr
,"<?"); break;
239 case BINOP_MAX
: strcpy(ptr
,">?"); break;
240 case BINOP_ASSIGN
: strcpy(ptr
,"="); break;
241 case BINOP_ASSIGN_MODIFY
:
244 case BINOP_ADD
: strcpy(ptr
,"+="); break;
245 case BINOP_SUB
: strcpy(ptr
,"-="); break;
246 case BINOP_MUL
: strcpy(ptr
,"*="); break;
247 case BINOP_DIV
: strcpy(ptr
,"/="); break;
248 case BINOP_REM
: strcpy(ptr
,"%="); break;
249 case BINOP_LOGAND
: strcpy(ptr
,"&="); break;
250 case BINOP_LOGIOR
: strcpy(ptr
,"|="); break;
251 case BINOP_LOGXOR
: strcpy(ptr
,"^="); break;
253 error ("Invalid binary operation specified.");
256 case BINOP_SUBSCRIPT
: strcpy(ptr
,"[]"); break;
257 case BINOP_EQUAL
: strcpy(ptr
,"=="); break;
258 case BINOP_NOTEQUAL
: strcpy(ptr
,"!="); break;
259 case BINOP_LESS
: strcpy(ptr
,"<"); break;
260 case BINOP_GTR
: strcpy(ptr
,">"); break;
261 case BINOP_GEQ
: strcpy(ptr
,">="); break;
262 case BINOP_LEQ
: strcpy(ptr
,"<="); break;
264 error ("Invalid binary operation specified.");
266 argvec
[0] = value_struct_elt (&arg1
, argvec
+1, tstr
, &static_memfuncp
, "structure");
271 argvec
[1] = argvec
[0];
274 return target_call_function (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
276 error ("member function %s not found", tstr
);
278 return target_call_function (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
282 /* We know that arg1 is a structure, so try to find a unary user
283 defined operator that matches the operator in question.
284 Create an argument vector that calls arg1.operator @ (arg1)
285 and return that value (where '@' is (almost) any unary operator which
286 is legal for GNU C++). */
289 value_x_unop (arg1
, op
)
300 /* now we know that what we have to do is construct our
301 arg vector and find the right function to call it with. */
303 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_STRUCT
)
304 error ("Can't do that unary op on that type"); /* FIXME be explicit */
306 argvec
= (value
*) alloca (sizeof (value
) * 3);
307 argvec
[1] = value_addr (arg1
);
310 /* make the right function name up */
311 strcpy(tstr
,"operator__");
315 case UNOP_PREINCREMENT
: strcpy(ptr
,"++"); break;
316 case UNOP_PREDECREMENT
: strcpy(ptr
,"++"); break;
317 case UNOP_POSTINCREMENT
: strcpy(ptr
,"++"); break;
318 case UNOP_POSTDECREMENT
: strcpy(ptr
,"++"); break;
319 case UNOP_ZEROP
: strcpy(ptr
,"!"); break;
320 case UNOP_LOGNOT
: strcpy(ptr
,"~"); break;
321 case UNOP_NEG
: strcpy(ptr
,"-"); break;
323 error ("Invalid binary operation specified.");
325 argvec
[0] = value_struct_elt (&arg1
, argvec
+1, tstr
, &static_memfuncp
, "structure");
330 argvec
[1] = argvec
[0];
333 return target_call_function (argvec
[0], 1 - static_memfuncp
, argvec
+ 1);
335 error ("member function %s not found", tstr
);
336 return 0; /* For lint -- never reached */
339 /* Perform a binary operation on two integers or two floats.
340 Does not support addition and subtraction on pointers;
341 use value_add or value_sub if you want to handle those possibilities. */
344 value_binop (arg1
, arg2
, op
)
353 if ((TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_FLT
355 TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
)
357 (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_FLT
359 TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
))
360 error ("Argument to arithmetic operation not a number.");
362 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_FLT
364 TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_FLT
)
367 v1
= value_as_double (arg1
);
368 v2
= value_as_double (arg2
);
388 error ("Integer-only operation on floating point number.");
391 val
= allocate_value (builtin_type_double
);
392 SWAP_TARGET_AND_HOST (&v
, sizeof (v
));
393 *(double *) VALUE_CONTENTS_RAW (val
) = v
;
396 /* Integral operations here. */
398 /* Should we promote to unsigned longest? */
399 if ((TYPE_UNSIGNED (VALUE_TYPE (arg1
))
400 || TYPE_UNSIGNED (VALUE_TYPE (arg2
)))
401 && (TYPE_LENGTH (VALUE_TYPE (arg1
)) >= sizeof (unsigned LONGEST
)
402 || TYPE_LENGTH (VALUE_TYPE (arg1
)) >= sizeof (unsigned LONGEST
)))
404 unsigned LONGEST v1
, v2
, v
;
405 v1
= (unsigned LONGEST
) value_as_long (arg1
);
406 v2
= (unsigned LONGEST
) value_as_long (arg2
);
459 v
= v1
< v2
? v1
: v2
;
463 v
= v1
> v2
? v1
: v2
;
467 error ("Invalid binary operation on numbers.");
470 val
= allocate_value (BUILTIN_TYPE_UNSIGNED_LONGEST
);
471 SWAP_TARGET_AND_HOST (&v
, sizeof (v
));
472 *(unsigned LONGEST
*) VALUE_CONTENTS_RAW (val
) = v
;
477 v1
= value_as_long (arg1
);
478 v2
= value_as_long (arg2
);
531 v
= v1
< v2
? v1
: v2
;
535 v
= v1
> v2
? v1
: v2
;
539 error ("Invalid binary operation on numbers.");
542 val
= allocate_value (BUILTIN_TYPE_LONGEST
);
543 SWAP_TARGET_AND_HOST (&v
, sizeof (v
));
544 *(LONGEST
*) VALUE_CONTENTS_RAW (val
) = v
;
551 /* Simulate the C operator ! -- return 1 if ARG1 contains zeros. */
562 len
= TYPE_LENGTH (VALUE_TYPE (arg1
));
563 p
= VALUE_CONTENTS (arg1
);
574 /* Simulate the C operator == by returning a 1
575 iff ARG1 and ARG2 have equal contents. */
578 value_equal (arg1
, arg2
)
579 register value arg1
, arg2
;
583 register char *p1
, *p2
;
584 enum type_code code1
;
585 enum type_code code2
;
590 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
591 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
593 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
594 return value_as_long (arg1
) == value_as_long (arg2
);
595 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
596 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
597 return value_as_double (arg1
) == value_as_double (arg2
);
598 else if ((code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_INT
)
599 || (code2
== TYPE_CODE_PTR
&& code1
== TYPE_CODE_INT
))
600 return (char *) value_as_long (arg1
) == (char *) value_as_long (arg2
);
601 else if (code1
== code2
602 && ((len
= TYPE_LENGTH (VALUE_TYPE (arg1
)))
603 == TYPE_LENGTH (VALUE_TYPE (arg2
))))
605 p1
= VALUE_CONTENTS (arg1
);
606 p2
= VALUE_CONTENTS (arg2
);
616 error ("Invalid type combination in equality test.");
617 return 0; /* For lint -- never reached */
621 /* Simulate the C operator < by returning 1
622 iff ARG1's contents are less than ARG2's. */
625 value_less (arg1
, arg2
)
626 register value arg1
, arg2
;
628 register enum type_code code1
;
629 register enum type_code code2
;
634 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
635 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
637 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
639 if (TYPE_UNSIGNED (VALUE_TYPE (arg1
))
640 || TYPE_UNSIGNED (VALUE_TYPE (arg2
)))
641 return (unsigned)value_as_long (arg1
) < (unsigned)value_as_long (arg2
);
643 return value_as_long (arg1
) < value_as_long (arg2
);
645 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
646 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
647 return value_as_double (arg1
) < value_as_double (arg2
);
648 else if ((code1
== TYPE_CODE_PTR
|| code1
== TYPE_CODE_INT
)
649 && (code2
== TYPE_CODE_PTR
|| code2
== TYPE_CODE_INT
))
651 /* FIXME, this assumes that host and target char *'s are the same! */
652 return (char *) value_as_long (arg1
) < (char *) value_as_long (arg2
);
656 error ("Invalid type combination in ordering comparison.");
661 /* The unary operators - and ~. Both free the argument ARG1. */
667 register struct type
*type
;
671 type
= VALUE_TYPE (arg1
);
673 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
674 return value_from_double (type
, - value_as_double (arg1
));
675 else if (TYPE_CODE (type
) == TYPE_CODE_INT
)
676 return value_from_long (type
, - value_as_long (arg1
));
678 error ("Argument to negate operation not a number.");
679 return 0; /* For lint -- never reached */
689 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
)
690 error ("Argument to complement operation not an integer.");
692 return value_from_long (VALUE_TYPE (arg1
), ~ value_as_long (arg1
));