1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free
5 Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
28 #include "expression.h"
31 #include "gdb_string.h"
36 /* Define whether or not the C operator '/' truncates towards zero for
37 differently signed operands (truncation direction is undefined in C). */
39 #ifndef TRUNCATION_TOWARDS_ZERO
40 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
43 static struct value
*value_subscripted_rvalue (struct value
*, struct value
*, int);
45 void _initialize_valarith (void);
48 /* Given a pointer, return the size of its target.
49 If the pointer type is void *, then return 1.
50 If the target type is incomplete, then error out.
51 This isn't a general purpose function, but just a
52 helper for value_sub & value_add.
56 find_size_for_pointer_math (struct type
*ptr_type
)
59 struct type
*ptr_target
;
61 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
63 sz
= TYPE_LENGTH (ptr_target
);
66 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
72 name
= TYPE_NAME (ptr_target
);
74 name
= TYPE_TAG_NAME (ptr_target
);
76 error (_("Cannot perform pointer math on incomplete types, "
77 "try casting to a known type, or void *."));
79 error (_("Cannot perform pointer math on incomplete type \"%s\", "
80 "try casting to a known type, or void *."), name
);
87 value_add (struct value
*arg1
, struct value
*arg2
)
92 struct type
*type1
, *type2
, *valptrtype
;
94 arg1
= coerce_array (arg1
);
95 arg2
= coerce_array (arg2
);
96 type1
= check_typedef (value_type (arg1
));
97 type2
= check_typedef (value_type (arg2
));
99 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
100 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
102 (is_integral_type (type1
) || is_integral_type (type2
)))
103 /* Exactly one argument is a pointer, and one is an integer. */
105 struct value
*retval
;
107 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
120 sz
= find_size_for_pointer_math (valptrtype
);
122 retval
= value_from_pointer (valptrtype
,
123 value_as_address (valptr
)
124 + (sz
* value_as_long (valint
)));
128 return value_binop (arg1
, arg2
, BINOP_ADD
);
132 value_sub (struct value
*arg1
, struct value
*arg2
)
134 struct type
*type1
, *type2
;
135 arg1
= coerce_array (arg1
);
136 arg2
= coerce_array (arg2
);
137 type1
= check_typedef (value_type (arg1
));
138 type2
= check_typedef (value_type (arg2
));
140 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
142 if (is_integral_type (type2
))
144 /* pointer - integer. */
145 LONGEST sz
= find_size_for_pointer_math (type1
);
147 return value_from_pointer (type1
,
148 (value_as_address (arg1
)
149 - (sz
* value_as_long (arg2
))));
151 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
152 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
153 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
155 /* pointer to <type x> - pointer to <type x>. */
156 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
157 return value_from_longest
158 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
159 (value_as_long (arg1
) - value_as_long (arg2
)) / sz
);
164 First argument of `-' is a pointer and second argument is neither\n\
165 an integer nor a pointer of the same type."));
169 return value_binop (arg1
, arg2
, BINOP_SUB
);
172 /* Return the value of ARRAY[IDX].
173 See comments in value_coerce_array() for rationale for reason for
174 doing lower bounds adjustment here rather than there.
175 FIXME: Perhaps we should validate that the index is valid and if
176 verbosity is set, warn about invalid indices (but still use them). */
179 value_subscript (struct value
*array
, struct value
*idx
)
182 int c_style
= current_language
->c_style_arrays
;
185 array
= coerce_ref (array
);
186 tarray
= check_typedef (value_type (array
));
188 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
189 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
191 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
192 LONGEST lowerbound
, upperbound
;
193 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
195 if (VALUE_LVAL (array
) != lval_memory
)
196 return value_subscripted_rvalue (array
, idx
, lowerbound
);
200 LONGEST index
= value_as_long (idx
);
201 if (index
>= lowerbound
&& index
<= upperbound
)
202 return value_subscripted_rvalue (array
, idx
, lowerbound
);
203 /* Emit warning unless we have an array of unknown size.
204 An array of unknown size has lowerbound 0 and upperbound -1. */
206 warning (_("array or string index out of range"));
207 /* fall doing C stuff */
213 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
214 idx
= value_sub (idx
, bound
);
217 array
= value_coerce_array (array
);
220 if (TYPE_CODE (tarray
) == TYPE_CODE_BITSTRING
)
222 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
223 LONGEST index
= value_as_long (idx
);
225 int offset
, byte
, bit_index
;
226 LONGEST lowerbound
, upperbound
;
227 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
228 if (index
< lowerbound
|| index
> upperbound
)
229 error (_("bitstring index out of range"));
231 offset
= index
/ TARGET_CHAR_BIT
;
232 byte
= *((char *) value_contents (array
) + offset
);
233 bit_index
= index
% TARGET_CHAR_BIT
;
234 byte
>>= (BITS_BIG_ENDIAN
? TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
235 v
= value_from_longest (LA_BOOL_TYPE
, byte
& 1);
236 set_value_bitpos (v
, bit_index
);
237 set_value_bitsize (v
, 1);
238 VALUE_LVAL (v
) = VALUE_LVAL (array
);
239 if (VALUE_LVAL (array
) == lval_internalvar
)
240 VALUE_LVAL (v
) = lval_internalvar_component
;
241 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
242 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
243 set_value_offset (v
, offset
+ value_offset (array
));
248 return value_ind (value_add (array
, idx
));
250 error (_("not an array or string"));
253 /* Return the value of EXPR[IDX], expr an aggregate rvalue
254 (eg, a vector register). This routine used to promote floats
255 to doubles, but no longer does. */
257 static struct value
*
258 value_subscripted_rvalue (struct value
*array
, struct value
*idx
, int lowerbound
)
260 struct type
*array_type
= check_typedef (value_type (array
));
261 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
262 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
263 LONGEST index
= value_as_long (idx
);
264 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
267 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
268 error (_("no such vector element"));
270 v
= allocate_value (elt_type
);
271 if (value_lazy (array
))
272 set_value_lazy (v
, 1);
274 memcpy (value_contents_writeable (v
),
275 value_contents (array
) + elt_offs
, elt_size
);
277 if (VALUE_LVAL (array
) == lval_internalvar
)
278 VALUE_LVAL (v
) = lval_internalvar_component
;
280 VALUE_LVAL (v
) = VALUE_LVAL (array
);
281 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
282 VALUE_REGNUM (v
) = VALUE_REGNUM (array
);
283 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
284 set_value_offset (v
, value_offset (array
) + elt_offs
);
288 /* Check to see if either argument is a structure. This is called so
289 we know whether to go ahead with the normal binop or look for a
290 user defined function instead.
292 For now, we do not overload the `=' operator. */
295 binop_user_defined_p (enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
297 struct type
*type1
, *type2
;
298 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
300 type1
= check_typedef (value_type (arg1
));
301 type2
= check_typedef (value_type (arg2
));
302 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
303 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
304 || (TYPE_CODE (type1
) == TYPE_CODE_REF
305 && TYPE_CODE (TYPE_TARGET_TYPE (type1
)) == TYPE_CODE_STRUCT
)
306 || (TYPE_CODE (type2
) == TYPE_CODE_REF
307 && TYPE_CODE (TYPE_TARGET_TYPE (type2
)) == TYPE_CODE_STRUCT
));
310 /* Check to see if argument is a structure. This is called so
311 we know whether to go ahead with the normal unop or look for a
312 user defined function instead.
314 For now, we do not overload the `&' operator. */
317 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
322 type1
= check_typedef (value_type (arg1
));
325 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
327 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
328 type1
= TYPE_TARGET_TYPE (type1
);
334 /* We know either arg1 or arg2 is a structure, so try to find the right
335 user defined function. Create an argument vector that calls
336 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
337 binary operator which is legal for GNU C++).
339 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
340 is the opcode saying how to modify it. Otherwise, OTHEROP is
344 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
345 enum exp_opcode otherop
, enum noside noside
)
347 struct value
**argvec
;
352 arg1
= coerce_ref (arg1
);
353 arg2
= coerce_ref (arg2
);
354 arg1
= coerce_enum (arg1
);
355 arg2
= coerce_enum (arg2
);
357 /* now we know that what we have to do is construct our
358 arg vector and find the right function to call it with. */
360 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
361 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
363 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
364 argvec
[1] = value_addr (arg1
);
368 /* make the right function name up */
369 strcpy (tstr
, "operator__");
394 case BINOP_BITWISE_AND
:
397 case BINOP_BITWISE_IOR
:
400 case BINOP_BITWISE_XOR
:
403 case BINOP_LOGICAL_AND
:
406 case BINOP_LOGICAL_OR
:
418 case BINOP_ASSIGN_MODIFY
:
436 case BINOP_BITWISE_AND
:
439 case BINOP_BITWISE_IOR
:
442 case BINOP_BITWISE_XOR
:
445 case BINOP_MOD
: /* invalid */
447 error (_("Invalid binary operation specified."));
450 case BINOP_SUBSCRIPT
:
471 case BINOP_MOD
: /* invalid */
473 error (_("Invalid binary operation specified."));
476 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
482 argvec
[1] = argvec
[0];
485 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
487 struct type
*return_type
;
489 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
490 return value_zero (return_type
, VALUE_LVAL (arg1
));
492 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
494 error (_("member function %s not found"), tstr
);
496 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
500 /* We know that arg1 is a structure, so try to find a unary user
501 defined operator that matches the operator in question.
502 Create an argument vector that calls arg1.operator @ (arg1)
503 and return that value (where '@' is (almost) any unary operator which
504 is legal for GNU C++). */
507 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
509 struct value
**argvec
;
510 char *ptr
, *mangle_ptr
;
511 char tstr
[13], mangle_tstr
[13];
512 int static_memfuncp
, nargs
;
514 arg1
= coerce_ref (arg1
);
515 arg1
= coerce_enum (arg1
);
517 /* now we know that what we have to do is construct our
518 arg vector and find the right function to call it with. */
520 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
521 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
523 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
524 argvec
[1] = value_addr (arg1
);
529 /* make the right function name up */
530 strcpy (tstr
, "operator__");
532 strcpy (mangle_tstr
, "__");
533 mangle_ptr
= mangle_tstr
+ 2;
536 case UNOP_PREINCREMENT
:
539 case UNOP_PREDECREMENT
:
542 case UNOP_POSTINCREMENT
:
544 argvec
[2] = value_from_longest (builtin_type_int
, 0);
548 case UNOP_POSTDECREMENT
:
550 argvec
[2] = value_from_longest (builtin_type_int
, 0);
554 case UNOP_LOGICAL_NOT
:
557 case UNOP_COMPLEMENT
:
570 error (_("Invalid unary operation specified."));
573 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
579 argvec
[1] = argvec
[0];
583 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
585 struct type
*return_type
;
587 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
588 return value_zero (return_type
, VALUE_LVAL (arg1
));
590 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
592 error (_("member function %s not found"), tstr
);
593 return 0; /* For lint -- never reached */
597 /* Concatenate two values with the following conditions:
599 (1) Both values must be either bitstring values or character string
600 values and the resulting value consists of the concatenation of
601 ARG1 followed by ARG2.
605 One value must be an integer value and the other value must be
606 either a bitstring value or character string value, which is
607 to be repeated by the number of times specified by the integer
611 (2) Boolean values are also allowed and are treated as bit string
614 (3) Character values are also allowed and are treated as character
615 string values of length 1.
619 value_concat (struct value
*arg1
, struct value
*arg2
)
621 struct value
*inval1
;
622 struct value
*inval2
;
623 struct value
*outval
= NULL
;
624 int inval1len
, inval2len
;
628 struct type
*type1
= check_typedef (value_type (arg1
));
629 struct type
*type2
= check_typedef (value_type (arg2
));
631 /* First figure out if we are dealing with two values to be concatenated
632 or a repeat count and a value to be repeated. INVAL1 is set to the
633 first of two concatenated values, or the repeat count. INVAL2 is set
634 to the second of the two concatenated values or the value to be
637 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
639 struct type
*tmp
= type1
;
651 /* Now process the input values. */
653 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
655 /* We have a repeat count. Validate the second value and then
656 construct a value repeated that many times. */
657 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
658 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
660 count
= longest_to_int (value_as_long (inval1
));
661 inval2len
= TYPE_LENGTH (type2
);
662 ptr
= (char *) alloca (count
* inval2len
);
663 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
665 inchar
= (char) unpack_long (type2
,
666 value_contents (inval2
));
667 for (idx
= 0; idx
< count
; idx
++)
669 *(ptr
+ idx
) = inchar
;
674 for (idx
= 0; idx
< count
; idx
++)
676 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
680 outval
= value_string (ptr
, count
* inval2len
);
682 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
683 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
685 error (_("unimplemented support for bitstring/boolean repeats"));
689 error (_("can't repeat values of that type"));
692 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
693 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
695 /* We have two character strings to concatenate. */
696 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
697 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
699 error (_("Strings can only be concatenated with other strings."));
701 inval1len
= TYPE_LENGTH (type1
);
702 inval2len
= TYPE_LENGTH (type2
);
703 ptr
= (char *) alloca (inval1len
+ inval2len
);
704 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
706 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
710 memcpy (ptr
, value_contents (inval1
), inval1len
);
712 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
715 (char) unpack_long (type2
, value_contents (inval2
));
719 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
721 outval
= value_string (ptr
, inval1len
+ inval2len
);
723 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
724 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
726 /* We have two bitstrings to concatenate. */
727 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
728 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
730 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
732 error (_("unimplemented support for bitstring/boolean concatenation."));
736 /* We don't know how to concatenate these operands. */
737 error (_("illegal operands for concatenation."));
744 /* Perform a binary operation on two operands which have reasonable
745 representations as integers or floats. This includes booleans,
746 characters, integers, or floats.
747 Does not support addition and subtraction on pointers;
748 use value_add or value_sub if you want to handle those possibilities. */
751 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
754 struct type
*type1
, *type2
;
756 arg1
= coerce_ref (arg1
);
757 arg2
= coerce_ref (arg2
);
758 type1
= check_typedef (value_type (arg1
));
759 type2
= check_typedef (value_type (arg2
));
761 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
&& !is_integral_type (type1
))
763 (TYPE_CODE (type2
) != TYPE_CODE_FLT
&& !is_integral_type (type2
)))
764 error (_("Argument to arithmetic operation not a number or boolean."));
766 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
768 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
770 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
771 in target format. real.c in GCC probably has the necessary
773 DOUBLEST v1
, v2
, v
= 0;
774 v1
= value_as_double (arg1
);
775 v2
= value_as_double (arg2
);
798 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
802 error (_("Integer-only operation on floating point number."));
805 /* If either arg was long double, make sure that value is also long
808 if (TYPE_LENGTH (type1
) * 8 > TARGET_DOUBLE_BIT
809 || TYPE_LENGTH (type2
) * 8 > TARGET_DOUBLE_BIT
)
810 val
= allocate_value (builtin_type_long_double
);
812 val
= allocate_value (builtin_type_double
);
814 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
816 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
818 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
820 LONGEST v1
, v2
, v
= 0;
821 v1
= value_as_long (arg1
);
822 v2
= value_as_long (arg2
);
826 case BINOP_BITWISE_AND
:
830 case BINOP_BITWISE_IOR
:
834 case BINOP_BITWISE_XOR
:
847 error (_("Invalid operation on booleans."));
850 val
= allocate_value (type1
);
851 store_signed_integer (value_contents_raw (val
),
856 /* Integral operations here. */
857 /* FIXME: Also mixed integral/booleans, with result an integer. */
858 /* FIXME: This implements ANSI C rules (also correct for C++).
859 What about FORTRAN and (the deleted) chill ? */
861 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
862 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
863 int is_unsigned1
= TYPE_UNSIGNED (type1
);
864 int is_unsigned2
= TYPE_UNSIGNED (type2
);
865 unsigned int result_len
;
866 int unsigned_operation
;
868 /* Determine type length and signedness after promotion for
870 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
873 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
875 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
878 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
881 /* Determine type length of the result, and if the operation should
883 Use the signedness of the operand with the greater length.
884 If both operands are of equal length, use unsigned operation
885 if one of the operands is unsigned. */
886 if (promoted_len1
> promoted_len2
)
888 unsigned_operation
= is_unsigned1
;
889 result_len
= promoted_len1
;
891 else if (promoted_len2
> promoted_len1
)
893 unsigned_operation
= is_unsigned2
;
894 result_len
= promoted_len2
;
898 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
899 result_len
= promoted_len1
;
902 if (unsigned_operation
)
904 ULONGEST v1
, v2
, v
= 0;
905 v1
= (ULONGEST
) value_as_long (arg1
);
906 v2
= (ULONGEST
) value_as_long (arg2
);
908 /* Truncate values to the type length of the result. */
909 if (result_len
< sizeof (ULONGEST
))
911 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
912 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
937 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
945 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
946 v1 mod 0 has a defined value, v1. */
954 /* Note floor(v1/v2) == v1/v2 for unsigned. */
967 case BINOP_BITWISE_AND
:
971 case BINOP_BITWISE_IOR
:
975 case BINOP_BITWISE_XOR
:
979 case BINOP_LOGICAL_AND
:
983 case BINOP_LOGICAL_OR
:
988 v
= v1
< v2
? v1
: v2
;
992 v
= v1
> v2
? v1
: v2
;
1008 error (_("Invalid binary operation on numbers."));
1011 /* This is a kludge to get around the fact that we don't
1012 know how to determine the result type from the types of
1013 the operands. (I'm not really sure how much we feel the
1014 need to duplicate the exact rules of the current
1015 language. They can get really hairy. But not to do so
1016 makes it hard to document just what we *do* do). */
1018 /* Can't just call init_type because we wouldn't know what
1019 name to give the type. */
1020 val
= allocate_value
1021 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1022 ? builtin_type_unsigned_long_long
1023 : builtin_type_unsigned_long
);
1024 store_unsigned_integer (value_contents_raw (val
),
1025 TYPE_LENGTH (value_type (val
)),
1030 LONGEST v1
, v2
, v
= 0;
1031 v1
= value_as_long (arg1
);
1032 v2
= value_as_long (arg2
);
1052 error (_("Division by zero"));
1059 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
1066 error (_("Division by zero"));
1070 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1071 X mod 0 has a defined value, X. */
1079 /* Compute floor. */
1080 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1096 case BINOP_BITWISE_AND
:
1100 case BINOP_BITWISE_IOR
:
1104 case BINOP_BITWISE_XOR
:
1108 case BINOP_LOGICAL_AND
:
1112 case BINOP_LOGICAL_OR
:
1117 v
= v1
< v2
? v1
: v2
;
1121 v
= v1
> v2
? v1
: v2
;
1133 error (_("Invalid binary operation on numbers."));
1136 /* This is a kludge to get around the fact that we don't
1137 know how to determine the result type from the types of
1138 the operands. (I'm not really sure how much we feel the
1139 need to duplicate the exact rules of the current
1140 language. They can get really hairy. But not to do so
1141 makes it hard to document just what we *do* do). */
1143 /* Can't just call init_type because we wouldn't know what
1144 name to give the type. */
1145 val
= allocate_value
1146 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1147 ? builtin_type_long_long
1148 : builtin_type_long
);
1149 store_signed_integer (value_contents_raw (val
),
1150 TYPE_LENGTH (value_type (val
)),
1158 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1161 value_logical_not (struct value
*arg1
)
1167 arg1
= coerce_number (arg1
);
1168 type1
= check_typedef (value_type (arg1
));
1170 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1171 return 0 == value_as_double (arg1
);
1173 len
= TYPE_LENGTH (type1
);
1174 p
= value_contents (arg1
);
1185 /* Perform a comparison on two string values (whose content are not
1186 necessarily null terminated) based on their length */
1189 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1191 int len1
= TYPE_LENGTH (value_type (arg1
));
1192 int len2
= TYPE_LENGTH (value_type (arg2
));
1193 const gdb_byte
*s1
= value_contents (arg1
);
1194 const gdb_byte
*s2
= value_contents (arg2
);
1195 int i
, len
= len1
< len2
? len1
: len2
;
1197 for (i
= 0; i
< len
; i
++)
1201 else if (s1
[i
] > s2
[i
])
1209 else if (len1
> len2
)
1215 /* Simulate the C operator == by returning a 1
1216 iff ARG1 and ARG2 have equal contents. */
1219 value_equal (struct value
*arg1
, struct value
*arg2
)
1224 struct type
*type1
, *type2
;
1225 enum type_code code1
;
1226 enum type_code code2
;
1227 int is_int1
, is_int2
;
1229 arg1
= coerce_array (arg1
);
1230 arg2
= coerce_array (arg2
);
1232 type1
= check_typedef (value_type (arg1
));
1233 type2
= check_typedef (value_type (arg2
));
1234 code1
= TYPE_CODE (type1
);
1235 code2
= TYPE_CODE (type2
);
1236 is_int1
= is_integral_type (type1
);
1237 is_int2
= is_integral_type (type2
);
1239 if (is_int1
&& is_int2
)
1240 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1242 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1243 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1244 return value_as_double (arg1
) == value_as_double (arg2
);
1246 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1248 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1249 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1250 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1251 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1253 else if (code1
== code2
1254 && ((len
= (int) TYPE_LENGTH (type1
))
1255 == (int) TYPE_LENGTH (type2
)))
1257 p1
= value_contents (arg1
);
1258 p2
= value_contents (arg2
);
1266 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1268 return value_strcmp (arg1
, arg2
) == 0;
1272 error (_("Invalid type combination in equality test."));
1273 return 0; /* For lint -- never reached */
1277 /* Simulate the C operator < by returning 1
1278 iff ARG1's contents are less than ARG2's. */
1281 value_less (struct value
*arg1
, struct value
*arg2
)
1283 enum type_code code1
;
1284 enum type_code code2
;
1285 struct type
*type1
, *type2
;
1286 int is_int1
, is_int2
;
1288 arg1
= coerce_array (arg1
);
1289 arg2
= coerce_array (arg2
);
1291 type1
= check_typedef (value_type (arg1
));
1292 type2
= check_typedef (value_type (arg2
));
1293 code1
= TYPE_CODE (type1
);
1294 code2
= TYPE_CODE (type2
);
1295 is_int1
= is_integral_type (type1
);
1296 is_int2
= is_integral_type (type2
);
1298 if (is_int1
&& is_int2
)
1299 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1301 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1302 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1303 return value_as_double (arg1
) < value_as_double (arg2
);
1304 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1305 return value_as_address (arg1
) < value_as_address (arg2
);
1307 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1309 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1310 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1311 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1312 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1313 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1314 return value_strcmp (arg1
, arg2
) < 0;
1317 error (_("Invalid type combination in ordering comparison."));
1322 /* The unary operators +, - and ~. They free the argument ARG1. */
1325 value_pos (struct value
*arg1
)
1329 arg1
= coerce_ref (arg1
);
1331 type
= check_typedef (value_type (arg1
));
1333 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1334 return value_from_double (type
, value_as_double (arg1
));
1335 else if (is_integral_type (type
))
1337 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1338 FORTRAN and (the deleted) chill ? */
1339 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1340 type
= builtin_type_int
;
1342 return value_from_longest (type
, value_as_long (arg1
));
1346 error ("Argument to positive operation not a number.");
1347 return 0; /* For lint -- never reached */
1352 value_neg (struct value
*arg1
)
1355 struct type
*result_type
= value_type (arg1
);
1357 arg1
= coerce_ref (arg1
);
1359 type
= check_typedef (value_type (arg1
));
1361 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1362 return value_from_double (result_type
, -value_as_double (arg1
));
1363 else if (is_integral_type (type
))
1365 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1366 FORTRAN and (the deleted) chill ? */
1367 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1368 result_type
= builtin_type_int
;
1370 return value_from_longest (result_type
, -value_as_long (arg1
));
1374 error (_("Argument to negate operation not a number."));
1375 return 0; /* For lint -- never reached */
1380 value_complement (struct value
*arg1
)
1383 struct type
*result_type
= value_type (arg1
);
1385 arg1
= coerce_ref (arg1
);
1387 type
= check_typedef (value_type (arg1
));
1389 if (!is_integral_type (type
))
1390 error (_("Argument to complement operation not an integer or boolean."));
1392 /* Perform integral promotion for ANSI C/C++.
1393 FIXME: What about FORTRAN ? */
1394 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1395 result_type
= builtin_type_int
;
1397 return value_from_longest (result_type
, ~value_as_long (arg1
));
1400 /* The INDEX'th bit of SET value whose value_type is TYPE,
1401 and whose value_contents is valaddr.
1402 Return -1 if out of range, -2 other error. */
1405 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1407 LONGEST low_bound
, high_bound
;
1410 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1411 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1413 if (index
< low_bound
|| index
> high_bound
)
1415 rel_index
= index
- low_bound
;
1416 word
= unpack_long (builtin_type_unsigned_char
,
1417 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1418 rel_index
%= TARGET_CHAR_BIT
;
1419 if (BITS_BIG_ENDIAN
)
1420 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1421 return (word
>> rel_index
) & 1;
1425 value_in (struct value
*element
, struct value
*set
)
1428 struct type
*settype
= check_typedef (value_type (set
));
1429 struct type
*eltype
= check_typedef (value_type (element
));
1430 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1431 eltype
= TYPE_TARGET_TYPE (eltype
);
1432 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1433 error (_("Second argument of 'IN' has wrong type"));
1434 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1435 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1436 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1437 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1438 error (_("First argument of 'IN' has wrong type"));
1439 member
= value_bit_index (settype
, value_contents (set
),
1440 value_as_long (element
));
1442 error (_("First argument of 'IN' not in range"));
1443 return value_from_longest (LA_BOOL_TYPE
, member
);
1447 _initialize_valarith (void)