1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
5 Free 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 3 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, see <http://www.gnu.org/licenses/>. */
26 #include "expression.h"
29 #include "gdb_string.h"
35 /* Define whether or not the C operator '/' truncates towards zero for
36 differently signed operands (truncation direction is undefined in C). */
38 #ifndef TRUNCATION_TOWARDS_ZERO
39 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
42 static struct value
*value_subscripted_rvalue (struct value
*, struct value
*, int);
44 void _initialize_valarith (void);
47 /* Given a pointer, return the size of its target.
48 If the pointer type is void *, then return 1.
49 If the target type is incomplete, then error out.
50 This isn't a general purpose function, but just a
51 helper for value_sub & value_add.
55 find_size_for_pointer_math (struct type
*ptr_type
)
58 struct type
*ptr_target
;
60 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
62 sz
= TYPE_LENGTH (ptr_target
);
65 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
71 name
= TYPE_NAME (ptr_target
);
73 name
= TYPE_TAG_NAME (ptr_target
);
75 error (_("Cannot perform pointer math on incomplete types, "
76 "try casting to a known type, or void *."));
78 error (_("Cannot perform pointer math on incomplete type \"%s\", "
79 "try casting to a known type, or void *."), name
);
86 value_add (struct value
*arg1
, struct value
*arg2
)
91 struct type
*type1
, *type2
, *valptrtype
;
93 arg1
= coerce_array (arg1
);
94 arg2
= coerce_array (arg2
);
95 type1
= check_typedef (value_type (arg1
));
96 type2
= check_typedef (value_type (arg2
));
98 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
99 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
101 (is_integral_type (type1
) || is_integral_type (type2
)))
102 /* Exactly one argument is a pointer, and one is an integer. */
104 struct value
*retval
;
106 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
119 sz
= find_size_for_pointer_math (valptrtype
);
121 retval
= value_from_pointer (valptrtype
,
122 value_as_address (valptr
)
123 + (sz
* value_as_long (valint
)));
127 return value_binop (arg1
, arg2
, BINOP_ADD
);
131 value_sub (struct value
*arg1
, struct value
*arg2
)
133 struct type
*type1
, *type2
;
134 arg1
= coerce_array (arg1
);
135 arg2
= coerce_array (arg2
);
136 type1
= check_typedef (value_type (arg1
));
137 type2
= check_typedef (value_type (arg2
));
139 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
141 if (is_integral_type (type2
))
143 /* pointer - integer. */
144 LONGEST sz
= find_size_for_pointer_math (type1
);
146 return value_from_pointer (type1
,
147 (value_as_address (arg1
)
148 - (sz
* value_as_long (arg2
))));
150 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
151 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
152 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
154 /* pointer to <type x> - pointer to <type x>. */
155 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
156 return value_from_longest
157 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
158 (value_as_long (arg1
) - value_as_long (arg2
)) / sz
);
163 First argument of `-' is a pointer and second argument is neither\n\
164 an integer nor a pointer of the same type."));
168 return value_binop (arg1
, arg2
, BINOP_SUB
);
171 /* Return the value of ARRAY[IDX].
172 See comments in value_coerce_array() for rationale for reason for
173 doing lower bounds adjustment here rather than there.
174 FIXME: Perhaps we should validate that the index is valid and if
175 verbosity is set, warn about invalid indices (but still use them). */
178 value_subscript (struct value
*array
, struct value
*idx
)
181 int c_style
= current_language
->c_style_arrays
;
184 array
= coerce_ref (array
);
185 tarray
= check_typedef (value_type (array
));
187 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
188 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
190 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
191 LONGEST lowerbound
, upperbound
;
192 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
194 if (VALUE_LVAL (array
) != lval_memory
)
195 return value_subscripted_rvalue (array
, idx
, lowerbound
);
199 LONGEST index
= value_as_long (idx
);
200 if (index
>= lowerbound
&& index
<= upperbound
)
201 return value_subscripted_rvalue (array
, idx
, lowerbound
);
202 /* Emit warning unless we have an array of unknown size.
203 An array of unknown size has lowerbound 0 and upperbound -1. */
205 warning (_("array or string index out of range"));
206 /* fall doing C stuff */
212 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
213 idx
= value_sub (idx
, bound
);
216 array
= value_coerce_array (array
);
219 if (TYPE_CODE (tarray
) == TYPE_CODE_BITSTRING
)
221 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
222 LONGEST index
= value_as_long (idx
);
224 int offset
, byte
, bit_index
;
225 LONGEST lowerbound
, upperbound
;
226 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
227 if (index
< lowerbound
|| index
> upperbound
)
228 error (_("bitstring index out of range"));
230 offset
= index
/ TARGET_CHAR_BIT
;
231 byte
= *((char *) value_contents (array
) + offset
);
232 bit_index
= index
% TARGET_CHAR_BIT
;
233 byte
>>= (gdbarch_bits_big_endian (current_gdbarch
) ?
234 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, or a reference to
289 one. This is called so we know whether to go ahead with the normal
290 binop or look for a 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
)
301 type1
= check_typedef (value_type (arg1
));
302 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
303 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
305 type2
= check_typedef (value_type (arg2
));
306 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
307 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
309 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
310 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
313 /* Check to see if argument is a structure. This is called so
314 we know whether to go ahead with the normal unop or look for a
315 user defined function instead.
317 For now, we do not overload the `&' operator. */
320 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
325 type1
= check_typedef (value_type (arg1
));
328 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
330 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
331 type1
= TYPE_TARGET_TYPE (type1
);
337 /* We know either arg1 or arg2 is a structure, so try to find the right
338 user defined function. Create an argument vector that calls
339 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
340 binary operator which is legal for GNU C++).
342 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
343 is the opcode saying how to modify it. Otherwise, OTHEROP is
347 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
348 enum exp_opcode otherop
, enum noside noside
)
350 struct value
**argvec
;
355 arg1
= coerce_ref (arg1
);
356 arg2
= coerce_ref (arg2
);
357 arg1
= coerce_enum (arg1
);
358 arg2
= coerce_enum (arg2
);
360 /* now we know that what we have to do is construct our
361 arg vector and find the right function to call it with. */
363 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
364 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
366 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
367 argvec
[1] = value_addr (arg1
);
371 /* make the right function name up */
372 strcpy (tstr
, "operator__");
397 case BINOP_BITWISE_AND
:
400 case BINOP_BITWISE_IOR
:
403 case BINOP_BITWISE_XOR
:
406 case BINOP_LOGICAL_AND
:
409 case BINOP_LOGICAL_OR
:
421 case BINOP_ASSIGN_MODIFY
:
439 case BINOP_BITWISE_AND
:
442 case BINOP_BITWISE_IOR
:
445 case BINOP_BITWISE_XOR
:
448 case BINOP_MOD
: /* invalid */
450 error (_("Invalid binary operation specified."));
453 case BINOP_SUBSCRIPT
:
474 case BINOP_MOD
: /* invalid */
476 error (_("Invalid binary operation specified."));
479 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
485 argvec
[1] = argvec
[0];
488 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
490 struct type
*return_type
;
492 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
493 return value_zero (return_type
, VALUE_LVAL (arg1
));
495 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
497 error (_("member function %s not found"), tstr
);
499 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
503 /* We know that arg1 is a structure, so try to find a unary user
504 defined operator that matches the operator in question.
505 Create an argument vector that calls arg1.operator @ (arg1)
506 and return that value (where '@' is (almost) any unary operator which
507 is legal for GNU C++). */
510 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
512 struct value
**argvec
;
513 char *ptr
, *mangle_ptr
;
514 char tstr
[13], mangle_tstr
[13];
515 int static_memfuncp
, nargs
;
517 arg1
= coerce_ref (arg1
);
518 arg1
= coerce_enum (arg1
);
520 /* now we know that what we have to do is construct our
521 arg vector and find the right function to call it with. */
523 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
524 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
526 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
527 argvec
[1] = value_addr (arg1
);
532 /* make the right function name up */
533 strcpy (tstr
, "operator__");
535 strcpy (mangle_tstr
, "__");
536 mangle_ptr
= mangle_tstr
+ 2;
539 case UNOP_PREINCREMENT
:
542 case UNOP_PREDECREMENT
:
545 case UNOP_POSTINCREMENT
:
547 argvec
[2] = value_from_longest (builtin_type_int
, 0);
551 case UNOP_POSTDECREMENT
:
553 argvec
[2] = value_from_longest (builtin_type_int
, 0);
557 case UNOP_LOGICAL_NOT
:
560 case UNOP_COMPLEMENT
:
573 error (_("Invalid unary operation specified."));
576 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
582 argvec
[1] = argvec
[0];
586 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
588 struct type
*return_type
;
590 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
591 return value_zero (return_type
, VALUE_LVAL (arg1
));
593 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
595 error (_("member function %s not found"), tstr
);
596 return 0; /* For lint -- never reached */
600 /* Concatenate two values with the following conditions:
602 (1) Both values must be either bitstring values or character string
603 values and the resulting value consists of the concatenation of
604 ARG1 followed by ARG2.
608 One value must be an integer value and the other value must be
609 either a bitstring value or character string value, which is
610 to be repeated by the number of times specified by the integer
614 (2) Boolean values are also allowed and are treated as bit string
617 (3) Character values are also allowed and are treated as character
618 string values of length 1.
622 value_concat (struct value
*arg1
, struct value
*arg2
)
624 struct value
*inval1
;
625 struct value
*inval2
;
626 struct value
*outval
= NULL
;
627 int inval1len
, inval2len
;
631 struct type
*type1
= check_typedef (value_type (arg1
));
632 struct type
*type2
= check_typedef (value_type (arg2
));
634 /* First figure out if we are dealing with two values to be concatenated
635 or a repeat count and a value to be repeated. INVAL1 is set to the
636 first of two concatenated values, or the repeat count. INVAL2 is set
637 to the second of the two concatenated values or the value to be
640 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
642 struct type
*tmp
= type1
;
654 /* Now process the input values. */
656 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
658 /* We have a repeat count. Validate the second value and then
659 construct a value repeated that many times. */
660 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
661 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
663 count
= longest_to_int (value_as_long (inval1
));
664 inval2len
= TYPE_LENGTH (type2
);
665 ptr
= (char *) alloca (count
* inval2len
);
666 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
668 inchar
= (char) unpack_long (type2
,
669 value_contents (inval2
));
670 for (idx
= 0; idx
< count
; idx
++)
672 *(ptr
+ idx
) = inchar
;
677 for (idx
= 0; idx
< count
; idx
++)
679 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
683 outval
= value_string (ptr
, count
* inval2len
);
685 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
686 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
688 error (_("unimplemented support for bitstring/boolean repeats"));
692 error (_("can't repeat values of that type"));
695 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
696 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
698 /* We have two character strings to concatenate. */
699 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
700 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
702 error (_("Strings can only be concatenated with other strings."));
704 inval1len
= TYPE_LENGTH (type1
);
705 inval2len
= TYPE_LENGTH (type2
);
706 ptr
= (char *) alloca (inval1len
+ inval2len
);
707 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
709 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
713 memcpy (ptr
, value_contents (inval1
), inval1len
);
715 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
718 (char) unpack_long (type2
, value_contents (inval2
));
722 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
724 outval
= value_string (ptr
, inval1len
+ inval2len
);
726 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
727 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
729 /* We have two bitstrings to concatenate. */
730 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
731 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
733 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
735 error (_("unimplemented support for bitstring/boolean concatenation."));
739 /* We don't know how to concatenate these operands. */
740 error (_("illegal operands for concatenation."));
746 /* Integer exponentiation: V1**V2, where both arguments are
747 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
749 integer_pow (LONGEST v1
, LONGEST v2
)
754 error (_("Attempt to raise 0 to negative power."));
760 /* The Russian Peasant's Algorithm */
776 /* Integer exponentiation: V1**V2, where both arguments are
777 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
779 uinteger_pow (ULONGEST v1
, LONGEST v2
)
784 error (_("Attempt to raise 0 to negative power."));
790 /* The Russian Peasant's Algorithm */
806 /* Obtain decimal value of arguments for binary operation, converting from
807 other types if one of them is not decimal floating point. */
809 value_args_as_decimal (struct value
*arg1
, struct value
*arg2
,
810 gdb_byte
*x
, int *len_x
, gdb_byte
*y
, int *len_y
)
812 struct type
*type1
, *type2
;
814 type1
= check_typedef (value_type (arg1
));
815 type2
= check_typedef (value_type (arg2
));
817 /* At least one of the arguments must be of decimal float type. */
818 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
819 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
);
821 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
822 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
823 /* The DFP extension to the C language does not allow mixing of
824 * decimal float types with other float types in expressions
825 * (see WDTR 24732, page 12). */
826 error (_("Mixing decimal floating types with other floating types is not allowed."));
828 /* Obtain decimal value of arg1, converting from other types
831 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
833 *len_x
= TYPE_LENGTH (type1
);
834 memcpy (x
, value_contents (arg1
), *len_x
);
836 else if (is_integral_type (type1
))
838 *len_x
= TYPE_LENGTH (type2
);
839 decimal_from_integral (arg1
, x
, *len_x
);
842 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
845 /* Obtain decimal value of arg2, converting from other types
848 if (TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
850 *len_y
= TYPE_LENGTH (type2
);
851 memcpy (y
, value_contents (arg2
), *len_y
);
853 else if (is_integral_type (type2
))
855 *len_y
= TYPE_LENGTH (type1
);
856 decimal_from_integral (arg2
, y
, *len_y
);
859 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
863 /* Perform a binary operation on two operands which have reasonable
864 representations as integers or floats. This includes booleans,
865 characters, integers, or floats.
866 Does not support addition and subtraction on pointers;
867 use value_add or value_sub if you want to handle those possibilities. */
870 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
873 struct type
*type1
, *type2
;
875 arg1
= coerce_ref (arg1
);
876 arg2
= coerce_ref (arg2
);
877 type1
= check_typedef (value_type (arg1
));
878 type2
= check_typedef (value_type (arg2
));
880 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
881 && TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
&& !is_integral_type (type1
))
883 (TYPE_CODE (type2
) != TYPE_CODE_FLT
884 && TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
&& !is_integral_type (type2
)))
885 error (_("Argument to arithmetic operation not a number or boolean."));
887 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
889 TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
892 int len_v1
, len_v2
, len_v
;
893 gdb_byte v1
[16], v2
[16];
896 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
905 decimal_binop (op
, v1
, len_v1
, v2
, len_v2
, v
, &len_v
);
909 error (_("Operation not valid for decimal floating point number."));
912 if (TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
)
913 /* If arg1 is not a decimal float, the type of the result is the type
914 of the decimal float argument, arg2. */
916 else if (TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
)
917 /* Same logic, for the case where arg2 is not a decimal float. */
920 /* len_v is equal either to len_v1 or to len_v2. the type of the
921 result is the type of the argument with the same length as v. */
922 v_type
= (len_v
== len_v1
)? type1
: type2
;
924 val
= value_from_decfloat (v_type
, v
);
926 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
928 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
930 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
931 in target format. real.c in GCC probably has the necessary
933 DOUBLEST v1
, v2
, v
= 0;
934 v1
= value_as_double (arg1
);
935 v2
= value_as_double (arg2
);
958 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
962 v
= v1
< v2
? v1
: v2
;
966 v
= v1
> v2
? v1
: v2
;
970 error (_("Integer-only operation on floating point number."));
973 /* If either arg was long double, make sure that value is also long
976 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (current_gdbarch
)
977 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (current_gdbarch
))
978 val
= allocate_value (builtin_type_long_double
);
980 val
= allocate_value (builtin_type_double
);
982 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
984 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
986 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
988 LONGEST v1
, v2
, v
= 0;
989 v1
= value_as_long (arg1
);
990 v2
= value_as_long (arg2
);
994 case BINOP_BITWISE_AND
:
998 case BINOP_BITWISE_IOR
:
1002 case BINOP_BITWISE_XOR
:
1010 case BINOP_NOTEQUAL
:
1015 error (_("Invalid operation on booleans."));
1018 val
= allocate_value (type1
);
1019 store_signed_integer (value_contents_raw (val
),
1020 TYPE_LENGTH (type1
),
1024 /* Integral operations here. */
1025 /* FIXME: Also mixed integral/booleans, with result an integer. */
1026 /* FIXME: This implements ANSI C rules (also correct for C++).
1027 What about FORTRAN and (the deleted) chill ? */
1029 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
1030 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
1031 int is_unsigned1
= TYPE_UNSIGNED (type1
);
1032 int is_unsigned2
= TYPE_UNSIGNED (type2
);
1033 unsigned int result_len
;
1034 int unsigned_operation
;
1036 /* Determine type length and signedness after promotion for
1038 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
1041 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
1043 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
1046 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
1049 /* Determine type length of the result, and if the operation should
1050 be done unsigned. For exponentiation and shift operators,
1051 use the length and type of the left operand. Otherwise,
1052 use the signedness of the operand with the greater length.
1053 If both operands are of equal length, use unsigned operation
1054 if one of the operands is unsigned. */
1055 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1057 /* In case of the shift operators and exponentiation the type of
1058 the result only depends on the type of the left operand. */
1059 unsigned_operation
= is_unsigned1
;
1060 result_len
= promoted_len1
;
1062 else if (promoted_len1
> promoted_len2
)
1064 unsigned_operation
= is_unsigned1
;
1065 result_len
= promoted_len1
;
1067 else if (promoted_len2
> promoted_len1
)
1069 unsigned_operation
= is_unsigned2
;
1070 result_len
= promoted_len2
;
1074 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
1075 result_len
= promoted_len1
;
1078 if (unsigned_operation
)
1080 LONGEST v2_signed
= value_as_long (arg2
);
1081 ULONGEST v1
, v2
, v
= 0;
1082 v1
= (ULONGEST
) value_as_long (arg1
);
1083 v2
= (ULONGEST
) v2_signed
;
1085 /* Truncate values to the type length of the result. */
1086 if (result_len
< sizeof (ULONGEST
))
1088 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1089 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1111 error (_("Division by zero"));
1115 v
= uinteger_pow (v1
, v2_signed
);
1122 error (_("Division by zero"));
1126 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1127 v1 mod 0 has a defined value, v1. */
1135 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1148 case BINOP_BITWISE_AND
:
1152 case BINOP_BITWISE_IOR
:
1156 case BINOP_BITWISE_XOR
:
1160 case BINOP_LOGICAL_AND
:
1164 case BINOP_LOGICAL_OR
:
1169 v
= v1
< v2
? v1
: v2
;
1173 v
= v1
> v2
? v1
: v2
;
1180 case BINOP_NOTEQUAL
:
1189 error (_("Invalid binary operation on numbers."));
1192 /* This is a kludge to get around the fact that we don't
1193 know how to determine the result type from the types of
1194 the operands. (I'm not really sure how much we feel the
1195 need to duplicate the exact rules of the current
1196 language. They can get really hairy. But not to do so
1197 makes it hard to document just what we *do* do). */
1199 /* Can't just call init_type because we wouldn't know what
1200 name to give the type. */
1201 val
= allocate_value
1202 (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
1203 ? builtin_type_unsigned_long_long
1204 : builtin_type_unsigned_long
);
1205 store_unsigned_integer (value_contents_raw (val
),
1206 TYPE_LENGTH (value_type (val
)),
1211 LONGEST v1
, v2
, v
= 0;
1212 v1
= value_as_long (arg1
);
1213 v2
= value_as_long (arg2
);
1234 error (_("Division by zero"));
1238 v
= integer_pow (v1
, v2
);
1245 error (_("Division by zero"));
1249 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1250 X mod 0 has a defined value, X. */
1258 /* Compute floor. */
1259 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1275 case BINOP_BITWISE_AND
:
1279 case BINOP_BITWISE_IOR
:
1283 case BINOP_BITWISE_XOR
:
1287 case BINOP_LOGICAL_AND
:
1291 case BINOP_LOGICAL_OR
:
1296 v
= v1
< v2
? v1
: v2
;
1300 v
= v1
> v2
? v1
: v2
;
1312 error (_("Invalid binary operation on numbers."));
1315 /* This is a kludge to get around the fact that we don't
1316 know how to determine the result type from the types of
1317 the operands. (I'm not really sure how much we feel the
1318 need to duplicate the exact rules of the current
1319 language. They can get really hairy. But not to do so
1320 makes it hard to document just what we *do* do). */
1322 /* Can't just call init_type because we wouldn't know what
1323 name to give the type. */
1324 val
= allocate_value
1325 (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
1326 ? builtin_type_long_long
1327 : builtin_type_long
);
1328 store_signed_integer (value_contents_raw (val
),
1329 TYPE_LENGTH (value_type (val
)),
1337 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1340 value_logical_not (struct value
*arg1
)
1346 arg1
= coerce_number (arg1
);
1347 type1
= check_typedef (value_type (arg1
));
1349 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1350 return 0 == value_as_double (arg1
);
1351 else if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1352 return decimal_is_zero (value_contents (arg1
), TYPE_LENGTH (type1
));
1354 len
= TYPE_LENGTH (type1
);
1355 p
= value_contents (arg1
);
1366 /* Perform a comparison on two string values (whose content are not
1367 necessarily null terminated) based on their length */
1370 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1372 int len1
= TYPE_LENGTH (value_type (arg1
));
1373 int len2
= TYPE_LENGTH (value_type (arg2
));
1374 const gdb_byte
*s1
= value_contents (arg1
);
1375 const gdb_byte
*s2
= value_contents (arg2
);
1376 int i
, len
= len1
< len2
? len1
: len2
;
1378 for (i
= 0; i
< len
; i
++)
1382 else if (s1
[i
] > s2
[i
])
1390 else if (len1
> len2
)
1396 /* Simulate the C operator == by returning a 1
1397 iff ARG1 and ARG2 have equal contents. */
1400 value_equal (struct value
*arg1
, struct value
*arg2
)
1405 struct type
*type1
, *type2
;
1406 enum type_code code1
;
1407 enum type_code code2
;
1408 int is_int1
, is_int2
;
1410 arg1
= coerce_array (arg1
);
1411 arg2
= coerce_array (arg2
);
1413 type1
= check_typedef (value_type (arg1
));
1414 type2
= check_typedef (value_type (arg2
));
1415 code1
= TYPE_CODE (type1
);
1416 code2
= TYPE_CODE (type2
);
1417 is_int1
= is_integral_type (type1
);
1418 is_int2
= is_integral_type (type2
);
1420 if (is_int1
&& is_int2
)
1421 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1423 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1424 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1426 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1427 `long double' values are returned in static storage (m68k). */
1428 DOUBLEST d
= value_as_double (arg1
);
1429 return d
== value_as_double (arg2
);
1431 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1432 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1434 gdb_byte v1
[16], v2
[16];
1437 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1439 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == 0;
1442 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1444 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1445 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1446 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1447 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1449 else if (code1
== code2
1450 && ((len
= (int) TYPE_LENGTH (type1
))
1451 == (int) TYPE_LENGTH (type2
)))
1453 p1
= value_contents (arg1
);
1454 p2
= value_contents (arg2
);
1462 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1464 return value_strcmp (arg1
, arg2
) == 0;
1468 error (_("Invalid type combination in equality test."));
1469 return 0; /* For lint -- never reached */
1473 /* Simulate the C operator < by returning 1
1474 iff ARG1's contents are less than ARG2's. */
1477 value_less (struct value
*arg1
, struct value
*arg2
)
1479 enum type_code code1
;
1480 enum type_code code2
;
1481 struct type
*type1
, *type2
;
1482 int is_int1
, is_int2
;
1484 arg1
= coerce_array (arg1
);
1485 arg2
= coerce_array (arg2
);
1487 type1
= check_typedef (value_type (arg1
));
1488 type2
= check_typedef (value_type (arg2
));
1489 code1
= TYPE_CODE (type1
);
1490 code2
= TYPE_CODE (type2
);
1491 is_int1
= is_integral_type (type1
);
1492 is_int2
= is_integral_type (type2
);
1494 if (is_int1
&& is_int2
)
1495 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1497 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1498 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1500 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1501 `long double' values are returned in static storage (m68k). */
1502 DOUBLEST d
= value_as_double (arg1
);
1503 return d
< value_as_double (arg2
);
1505 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1506 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1508 gdb_byte v1
[16], v2
[16];
1511 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1513 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == -1;
1515 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1516 return value_as_address (arg1
) < value_as_address (arg2
);
1518 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1520 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1521 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1522 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1523 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1524 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1525 return value_strcmp (arg1
, arg2
) < 0;
1528 error (_("Invalid type combination in ordering comparison."));
1533 /* The unary operators +, - and ~. They free the argument ARG1. */
1536 value_pos (struct value
*arg1
)
1540 arg1
= coerce_ref (arg1
);
1542 type
= check_typedef (value_type (arg1
));
1544 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1545 return value_from_double (type
, value_as_double (arg1
));
1546 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1547 return value_from_decfloat (type
, value_contents (arg1
));
1548 else if (is_integral_type (type
))
1550 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1551 FORTRAN and (the deleted) chill ? */
1552 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1553 type
= builtin_type_int
;
1555 return value_from_longest (type
, value_as_long (arg1
));
1559 error ("Argument to positive operation not a number.");
1560 return 0; /* For lint -- never reached */
1565 value_neg (struct value
*arg1
)
1568 struct type
*result_type
= value_type (arg1
);
1570 arg1
= coerce_ref (arg1
);
1572 type
= check_typedef (value_type (arg1
));
1574 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1576 struct value
*val
= allocate_value (result_type
);
1577 int len
= TYPE_LENGTH (type
);
1578 gdb_byte decbytes
[16]; /* a decfloat is at most 128 bits long */
1580 memcpy (decbytes
, value_contents (arg1
), len
);
1582 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_LITTLE
)
1583 decbytes
[len
-1] = decbytes
[len
- 1] | 0x80;
1585 decbytes
[0] = decbytes
[0] | 0x80;
1587 memcpy (value_contents_raw (val
), decbytes
, len
);
1591 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1592 return value_from_double (result_type
, -value_as_double (arg1
));
1593 else if (is_integral_type (type
))
1595 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1596 FORTRAN and (the deleted) chill ? */
1597 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1598 result_type
= builtin_type_int
;
1600 return value_from_longest (result_type
, -value_as_long (arg1
));
1604 error (_("Argument to negate operation not a number."));
1605 return 0; /* For lint -- never reached */
1610 value_complement (struct value
*arg1
)
1613 struct type
*result_type
= value_type (arg1
);
1615 arg1
= coerce_ref (arg1
);
1617 type
= check_typedef (value_type (arg1
));
1619 if (!is_integral_type (type
))
1620 error (_("Argument to complement operation not an integer or boolean."));
1622 /* Perform integral promotion for ANSI C/C++.
1623 FIXME: What about FORTRAN ? */
1624 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1625 result_type
= builtin_type_int
;
1627 return value_from_longest (result_type
, ~value_as_long (arg1
));
1630 /* The INDEX'th bit of SET value whose value_type is TYPE,
1631 and whose value_contents is valaddr.
1632 Return -1 if out of range, -2 other error. */
1635 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1637 LONGEST low_bound
, high_bound
;
1640 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1641 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1643 if (index
< low_bound
|| index
> high_bound
)
1645 rel_index
= index
- low_bound
;
1646 word
= unpack_long (builtin_type_unsigned_char
,
1647 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1648 rel_index
%= TARGET_CHAR_BIT
;
1649 if (gdbarch_bits_big_endian (current_gdbarch
))
1650 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1651 return (word
>> rel_index
) & 1;
1655 value_in (struct value
*element
, struct value
*set
)
1658 struct type
*settype
= check_typedef (value_type (set
));
1659 struct type
*eltype
= check_typedef (value_type (element
));
1660 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1661 eltype
= TYPE_TARGET_TYPE (eltype
);
1662 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1663 error (_("Second argument of 'IN' has wrong type"));
1664 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1665 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1666 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1667 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1668 error (_("First argument of 'IN' has wrong type"));
1669 member
= value_bit_index (settype
, value_contents (set
),
1670 value_as_long (element
));
1672 error (_("First argument of 'IN' not in range"));
1673 return value_from_longest (LA_BOOL_TYPE
, member
);
1677 _initialize_valarith (void)