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
>>= (BITS_BIG_ENDIAN
? TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
234 v
= value_from_longest (LA_BOOL_TYPE
, byte
& 1);
235 set_value_bitpos (v
, bit_index
);
236 set_value_bitsize (v
, 1);
237 VALUE_LVAL (v
) = VALUE_LVAL (array
);
238 if (VALUE_LVAL (array
) == lval_internalvar
)
239 VALUE_LVAL (v
) = lval_internalvar_component
;
240 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
241 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
242 set_value_offset (v
, offset
+ value_offset (array
));
247 return value_ind (value_add (array
, idx
));
249 error (_("not an array or string"));
252 /* Return the value of EXPR[IDX], expr an aggregate rvalue
253 (eg, a vector register). This routine used to promote floats
254 to doubles, but no longer does. */
256 static struct value
*
257 value_subscripted_rvalue (struct value
*array
, struct value
*idx
, int lowerbound
)
259 struct type
*array_type
= check_typedef (value_type (array
));
260 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
261 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
262 LONGEST index
= value_as_long (idx
);
263 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
266 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
267 error (_("no such vector element"));
269 v
= allocate_value (elt_type
);
270 if (value_lazy (array
))
271 set_value_lazy (v
, 1);
273 memcpy (value_contents_writeable (v
),
274 value_contents (array
) + elt_offs
, elt_size
);
276 if (VALUE_LVAL (array
) == lval_internalvar
)
277 VALUE_LVAL (v
) = lval_internalvar_component
;
279 VALUE_LVAL (v
) = VALUE_LVAL (array
);
280 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
281 VALUE_REGNUM (v
) = VALUE_REGNUM (array
);
282 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
283 set_value_offset (v
, value_offset (array
) + elt_offs
);
287 /* Check to see if either argument is a structure, or a reference to
288 one. This is called so we know whether to go ahead with the normal
289 binop or look for a user defined function instead.
291 For now, we do not overload the `=' operator. */
294 binop_user_defined_p (enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
296 struct type
*type1
, *type2
;
297 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
300 type1
= check_typedef (value_type (arg1
));
301 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
302 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
304 type2
= check_typedef (value_type (arg2
));
305 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
306 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
308 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
309 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
312 /* Check to see if argument is a structure. This is called so
313 we know whether to go ahead with the normal unop or look for a
314 user defined function instead.
316 For now, we do not overload the `&' operator. */
319 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
324 type1
= check_typedef (value_type (arg1
));
327 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
329 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
330 type1
= TYPE_TARGET_TYPE (type1
);
336 /* We know either arg1 or arg2 is a structure, so try to find the right
337 user defined function. Create an argument vector that calls
338 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
339 binary operator which is legal for GNU C++).
341 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
342 is the opcode saying how to modify it. Otherwise, OTHEROP is
346 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
347 enum exp_opcode otherop
, enum noside noside
)
349 struct value
**argvec
;
354 arg1
= coerce_ref (arg1
);
355 arg2
= coerce_ref (arg2
);
356 arg1
= coerce_enum (arg1
);
357 arg2
= coerce_enum (arg2
);
359 /* now we know that what we have to do is construct our
360 arg vector and find the right function to call it with. */
362 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
363 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
365 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
366 argvec
[1] = value_addr (arg1
);
370 /* make the right function name up */
371 strcpy (tstr
, "operator__");
396 case BINOP_BITWISE_AND
:
399 case BINOP_BITWISE_IOR
:
402 case BINOP_BITWISE_XOR
:
405 case BINOP_LOGICAL_AND
:
408 case BINOP_LOGICAL_OR
:
420 case BINOP_ASSIGN_MODIFY
:
438 case BINOP_BITWISE_AND
:
441 case BINOP_BITWISE_IOR
:
444 case BINOP_BITWISE_XOR
:
447 case BINOP_MOD
: /* invalid */
449 error (_("Invalid binary operation specified."));
452 case BINOP_SUBSCRIPT
:
473 case BINOP_MOD
: /* invalid */
475 error (_("Invalid binary operation specified."));
478 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
484 argvec
[1] = argvec
[0];
487 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
489 struct type
*return_type
;
491 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
492 return value_zero (return_type
, VALUE_LVAL (arg1
));
494 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
496 error (_("member function %s not found"), tstr
);
498 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
502 /* We know that arg1 is a structure, so try to find a unary user
503 defined operator that matches the operator in question.
504 Create an argument vector that calls arg1.operator @ (arg1)
505 and return that value (where '@' is (almost) any unary operator which
506 is legal for GNU C++). */
509 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
511 struct value
**argvec
;
512 char *ptr
, *mangle_ptr
;
513 char tstr
[13], mangle_tstr
[13];
514 int static_memfuncp
, nargs
;
516 arg1
= coerce_ref (arg1
);
517 arg1
= coerce_enum (arg1
);
519 /* now we know that what we have to do is construct our
520 arg vector and find the right function to call it with. */
522 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
523 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
525 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
526 argvec
[1] = value_addr (arg1
);
531 /* make the right function name up */
532 strcpy (tstr
, "operator__");
534 strcpy (mangle_tstr
, "__");
535 mangle_ptr
= mangle_tstr
+ 2;
538 case UNOP_PREINCREMENT
:
541 case UNOP_PREDECREMENT
:
544 case UNOP_POSTINCREMENT
:
546 argvec
[2] = value_from_longest (builtin_type_int
, 0);
550 case UNOP_POSTDECREMENT
:
552 argvec
[2] = value_from_longest (builtin_type_int
, 0);
556 case UNOP_LOGICAL_NOT
:
559 case UNOP_COMPLEMENT
:
572 error (_("Invalid unary operation specified."));
575 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
581 argvec
[1] = argvec
[0];
585 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
587 struct type
*return_type
;
589 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
590 return value_zero (return_type
, VALUE_LVAL (arg1
));
592 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
594 error (_("member function %s not found"), tstr
);
595 return 0; /* For lint -- never reached */
599 /* Concatenate two values with the following conditions:
601 (1) Both values must be either bitstring values or character string
602 values and the resulting value consists of the concatenation of
603 ARG1 followed by ARG2.
607 One value must be an integer value and the other value must be
608 either a bitstring value or character string value, which is
609 to be repeated by the number of times specified by the integer
613 (2) Boolean values are also allowed and are treated as bit string
616 (3) Character values are also allowed and are treated as character
617 string values of length 1.
621 value_concat (struct value
*arg1
, struct value
*arg2
)
623 struct value
*inval1
;
624 struct value
*inval2
;
625 struct value
*outval
= NULL
;
626 int inval1len
, inval2len
;
630 struct type
*type1
= check_typedef (value_type (arg1
));
631 struct type
*type2
= check_typedef (value_type (arg2
));
633 /* First figure out if we are dealing with two values to be concatenated
634 or a repeat count and a value to be repeated. INVAL1 is set to the
635 first of two concatenated values, or the repeat count. INVAL2 is set
636 to the second of the two concatenated values or the value to be
639 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
641 struct type
*tmp
= type1
;
653 /* Now process the input values. */
655 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
657 /* We have a repeat count. Validate the second value and then
658 construct a value repeated that many times. */
659 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
660 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
662 count
= longest_to_int (value_as_long (inval1
));
663 inval2len
= TYPE_LENGTH (type2
);
664 ptr
= (char *) alloca (count
* inval2len
);
665 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
667 inchar
= (char) unpack_long (type2
,
668 value_contents (inval2
));
669 for (idx
= 0; idx
< count
; idx
++)
671 *(ptr
+ idx
) = inchar
;
676 for (idx
= 0; idx
< count
; idx
++)
678 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
682 outval
= value_string (ptr
, count
* inval2len
);
684 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
685 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
687 error (_("unimplemented support for bitstring/boolean repeats"));
691 error (_("can't repeat values of that type"));
694 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
695 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
697 /* We have two character strings to concatenate. */
698 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
699 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
701 error (_("Strings can only be concatenated with other strings."));
703 inval1len
= TYPE_LENGTH (type1
);
704 inval2len
= TYPE_LENGTH (type2
);
705 ptr
= (char *) alloca (inval1len
+ inval2len
);
706 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
708 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
712 memcpy (ptr
, value_contents (inval1
), inval1len
);
714 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
717 (char) unpack_long (type2
, value_contents (inval2
));
721 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
723 outval
= value_string (ptr
, inval1len
+ inval2len
);
725 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
726 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
728 /* We have two bitstrings to concatenate. */
729 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
730 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
732 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
734 error (_("unimplemented support for bitstring/boolean concatenation."));
738 /* We don't know how to concatenate these operands. */
739 error (_("illegal operands for concatenation."));
745 /* Obtain decimal value of arguments for binary operation, converting from
746 other types if one of them is not decimal floating point. */
748 value_args_as_decimal (struct value
*arg1
, struct value
*arg2
,
749 gdb_byte
*x
, int *len_x
, gdb_byte
*y
, int *len_y
)
751 struct type
*type1
, *type2
;
753 type1
= check_typedef (value_type (arg1
));
754 type2
= check_typedef (value_type (arg2
));
756 /* At least one of the arguments must be of decimal float type. */
757 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
758 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
);
760 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
761 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
762 /* The DFP extension to the C language does not allow mixing of
763 * decimal float types with other float types in expressions
764 * (see WDTR 24732, page 12). */
765 error (_("Mixing decimal floating types with other floating types is not allowed."));
767 /* Obtain decimal value of arg1, converting from other types
770 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
772 *len_x
= TYPE_LENGTH (type1
);
773 memcpy (x
, value_contents (arg1
), *len_x
);
775 else if (is_integral_type (type1
))
777 *len_x
= TYPE_LENGTH (type2
);
778 decimal_from_integral (arg1
, x
, *len_x
);
781 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
784 /* Obtain decimal value of arg2, converting from other types
787 if (TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
789 *len_y
= TYPE_LENGTH (type2
);
790 memcpy (y
, value_contents (arg2
), *len_y
);
792 else if (is_integral_type (type2
))
794 *len_y
= TYPE_LENGTH (type1
);
795 decimal_from_integral (arg2
, y
, *len_y
);
798 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
802 /* Perform a binary operation on two operands which have reasonable
803 representations as integers or floats. This includes booleans,
804 characters, integers, or floats.
805 Does not support addition and subtraction on pointers;
806 use value_add or value_sub if you want to handle those possibilities. */
809 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
812 struct type
*type1
, *type2
;
814 arg1
= coerce_ref (arg1
);
815 arg2
= coerce_ref (arg2
);
816 type1
= check_typedef (value_type (arg1
));
817 type2
= check_typedef (value_type (arg2
));
819 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
820 && TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
&& !is_integral_type (type1
))
822 (TYPE_CODE (type2
) != TYPE_CODE_FLT
823 && TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
&& !is_integral_type (type2
)))
824 error (_("Argument to arithmetic operation not a number or boolean."));
826 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
828 TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
831 int len_v1
, len_v2
, len_v
;
832 gdb_byte v1
[16], v2
[16];
835 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
844 decimal_binop (op
, v1
, len_v1
, v2
, len_v2
, v
, &len_v
);
848 error (_("Operation not valid for decimal floating point number."));
851 if (TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
)
852 /* If arg1 is not a decimal float, the type of the result is the type
853 of the decimal float argument, arg2. */
855 else if (TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
)
856 /* Same logic, for the case where arg2 is not a decimal float. */
859 /* len_v is equal either to len_v1 or to len_v2. the type of the
860 result is the type of the argument with the same length as v. */
861 v_type
= (len_v
== len_v1
)? type1
: type2
;
863 val
= value_from_decfloat (v_type
, v
);
865 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
867 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
869 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
870 in target format. real.c in GCC probably has the necessary
872 DOUBLEST v1
, v2
, v
= 0;
873 v1
= value_as_double (arg1
);
874 v2
= value_as_double (arg2
);
897 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
901 error (_("Integer-only operation on floating point number."));
904 /* If either arg was long double, make sure that value is also long
907 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (current_gdbarch
)
908 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (current_gdbarch
))
909 val
= allocate_value (builtin_type_long_double
);
911 val
= allocate_value (builtin_type_double
);
913 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
915 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
917 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
919 LONGEST v1
, v2
, v
= 0;
920 v1
= value_as_long (arg1
);
921 v2
= value_as_long (arg2
);
925 case BINOP_BITWISE_AND
:
929 case BINOP_BITWISE_IOR
:
933 case BINOP_BITWISE_XOR
:
946 error (_("Invalid operation on booleans."));
949 val
= allocate_value (type1
);
950 store_signed_integer (value_contents_raw (val
),
955 /* Integral operations here. */
956 /* FIXME: Also mixed integral/booleans, with result an integer. */
957 /* FIXME: This implements ANSI C rules (also correct for C++).
958 What about FORTRAN and (the deleted) chill ? */
960 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
961 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
962 int is_unsigned1
= TYPE_UNSIGNED (type1
);
963 int is_unsigned2
= TYPE_UNSIGNED (type2
);
964 unsigned int result_len
;
965 int unsigned_operation
;
967 /* Determine type length and signedness after promotion for
969 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
972 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
974 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
977 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
980 /* Determine type length of the result, and if the operation should
982 Use the signedness of the operand with the greater length.
983 If both operands are of equal length, use unsigned operation
984 if one of the operands is unsigned. */
985 if (op
== BINOP_RSH
|| op
== BINOP_LSH
)
987 /* In case of the shift operators the type of the result only
988 depends on the type of the left operand. */
989 unsigned_operation
= is_unsigned1
;
990 result_len
= promoted_len1
;
992 else if (promoted_len1
> promoted_len2
)
994 unsigned_operation
= is_unsigned1
;
995 result_len
= promoted_len1
;
997 else if (promoted_len2
> promoted_len1
)
999 unsigned_operation
= is_unsigned2
;
1000 result_len
= promoted_len2
;
1004 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
1005 result_len
= promoted_len1
;
1008 if (unsigned_operation
)
1010 ULONGEST v1
, v2
, v
= 0;
1011 v1
= (ULONGEST
) value_as_long (arg1
);
1012 v2
= (ULONGEST
) value_as_long (arg2
);
1014 /* Truncate values to the type length of the result. */
1015 if (result_len
< sizeof (ULONGEST
))
1017 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1018 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1043 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
1051 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1052 v1 mod 0 has a defined value, v1. */
1060 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1073 case BINOP_BITWISE_AND
:
1077 case BINOP_BITWISE_IOR
:
1081 case BINOP_BITWISE_XOR
:
1085 case BINOP_LOGICAL_AND
:
1089 case BINOP_LOGICAL_OR
:
1094 v
= v1
< v2
? v1
: v2
;
1098 v
= v1
> v2
? v1
: v2
;
1105 case BINOP_NOTEQUAL
:
1114 error (_("Invalid binary operation on numbers."));
1117 /* This is a kludge to get around the fact that we don't
1118 know how to determine the result type from the types of
1119 the operands. (I'm not really sure how much we feel the
1120 need to duplicate the exact rules of the current
1121 language. They can get really hairy. But not to do so
1122 makes it hard to document just what we *do* do). */
1124 /* Can't just call init_type because we wouldn't know what
1125 name to give the type. */
1126 val
= allocate_value
1127 (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
1128 ? builtin_type_unsigned_long_long
1129 : builtin_type_unsigned_long
);
1130 store_unsigned_integer (value_contents_raw (val
),
1131 TYPE_LENGTH (value_type (val
)),
1136 LONGEST v1
, v2
, v
= 0;
1137 v1
= value_as_long (arg1
);
1138 v2
= value_as_long (arg2
);
1158 error (_("Division by zero"));
1165 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
1172 error (_("Division by zero"));
1176 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1177 X mod 0 has a defined value, X. */
1185 /* Compute floor. */
1186 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1202 case BINOP_BITWISE_AND
:
1206 case BINOP_BITWISE_IOR
:
1210 case BINOP_BITWISE_XOR
:
1214 case BINOP_LOGICAL_AND
:
1218 case BINOP_LOGICAL_OR
:
1223 v
= v1
< v2
? v1
: v2
;
1227 v
= v1
> v2
? v1
: v2
;
1239 error (_("Invalid binary operation on numbers."));
1242 /* This is a kludge to get around the fact that we don't
1243 know how to determine the result type from the types of
1244 the operands. (I'm not really sure how much we feel the
1245 need to duplicate the exact rules of the current
1246 language. They can get really hairy. But not to do so
1247 makes it hard to document just what we *do* do). */
1249 /* Can't just call init_type because we wouldn't know what
1250 name to give the type. */
1251 val
= allocate_value
1252 (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
1253 ? builtin_type_long_long
1254 : builtin_type_long
);
1255 store_signed_integer (value_contents_raw (val
),
1256 TYPE_LENGTH (value_type (val
)),
1264 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1267 value_logical_not (struct value
*arg1
)
1273 arg1
= coerce_number (arg1
);
1274 type1
= check_typedef (value_type (arg1
));
1276 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1277 return 0 == value_as_double (arg1
);
1278 else if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1279 return decimal_is_zero (value_contents (arg1
), TYPE_LENGTH (type1
));
1281 len
= TYPE_LENGTH (type1
);
1282 p
= value_contents (arg1
);
1293 /* Perform a comparison on two string values (whose content are not
1294 necessarily null terminated) based on their length */
1297 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1299 int len1
= TYPE_LENGTH (value_type (arg1
));
1300 int len2
= TYPE_LENGTH (value_type (arg2
));
1301 const gdb_byte
*s1
= value_contents (arg1
);
1302 const gdb_byte
*s2
= value_contents (arg2
);
1303 int i
, len
= len1
< len2
? len1
: len2
;
1305 for (i
= 0; i
< len
; i
++)
1309 else if (s1
[i
] > s2
[i
])
1317 else if (len1
> len2
)
1323 /* Simulate the C operator == by returning a 1
1324 iff ARG1 and ARG2 have equal contents. */
1327 value_equal (struct value
*arg1
, struct value
*arg2
)
1332 struct type
*type1
, *type2
;
1333 enum type_code code1
;
1334 enum type_code code2
;
1335 int is_int1
, is_int2
;
1337 arg1
= coerce_array (arg1
);
1338 arg2
= coerce_array (arg2
);
1340 type1
= check_typedef (value_type (arg1
));
1341 type2
= check_typedef (value_type (arg2
));
1342 code1
= TYPE_CODE (type1
);
1343 code2
= TYPE_CODE (type2
);
1344 is_int1
= is_integral_type (type1
);
1345 is_int2
= is_integral_type (type2
);
1347 if (is_int1
&& is_int2
)
1348 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1350 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1351 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1353 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1354 `long double' values are returned in static storage (m68k). */
1355 DOUBLEST d
= value_as_double (arg1
);
1356 return d
== value_as_double (arg2
);
1358 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1359 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1361 gdb_byte v1
[16], v2
[16];
1364 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1366 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == 0;
1369 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1371 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1372 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1373 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1374 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1376 else if (code1
== code2
1377 && ((len
= (int) TYPE_LENGTH (type1
))
1378 == (int) TYPE_LENGTH (type2
)))
1380 p1
= value_contents (arg1
);
1381 p2
= value_contents (arg2
);
1389 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1391 return value_strcmp (arg1
, arg2
) == 0;
1395 error (_("Invalid type combination in equality test."));
1396 return 0; /* For lint -- never reached */
1400 /* Simulate the C operator < by returning 1
1401 iff ARG1's contents are less than ARG2's. */
1404 value_less (struct value
*arg1
, struct value
*arg2
)
1406 enum type_code code1
;
1407 enum type_code code2
;
1408 struct type
*type1
, *type2
;
1409 int is_int1
, is_int2
;
1411 arg1
= coerce_array (arg1
);
1412 arg2
= coerce_array (arg2
);
1414 type1
= check_typedef (value_type (arg1
));
1415 type2
= check_typedef (value_type (arg2
));
1416 code1
= TYPE_CODE (type1
);
1417 code2
= TYPE_CODE (type2
);
1418 is_int1
= is_integral_type (type1
);
1419 is_int2
= is_integral_type (type2
);
1421 if (is_int1
&& is_int2
)
1422 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1424 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1425 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1427 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1428 `long double' values are returned in static storage (m68k). */
1429 DOUBLEST d
= value_as_double (arg1
);
1430 return d
< value_as_double (arg2
);
1432 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1433 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1435 gdb_byte v1
[16], v2
[16];
1438 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1440 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == -1;
1442 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1443 return value_as_address (arg1
) < value_as_address (arg2
);
1445 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1447 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1448 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1449 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1450 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1451 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1452 return value_strcmp (arg1
, arg2
) < 0;
1455 error (_("Invalid type combination in ordering comparison."));
1460 /* The unary operators +, - and ~. They free the argument ARG1. */
1463 value_pos (struct value
*arg1
)
1467 arg1
= coerce_ref (arg1
);
1469 type
= check_typedef (value_type (arg1
));
1471 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1472 return value_from_double (type
, value_as_double (arg1
));
1473 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1474 return value_from_decfloat (type
, value_contents (arg1
));
1475 else if (is_integral_type (type
))
1477 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1478 FORTRAN and (the deleted) chill ? */
1479 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1480 type
= builtin_type_int
;
1482 return value_from_longest (type
, value_as_long (arg1
));
1486 error ("Argument to positive operation not a number.");
1487 return 0; /* For lint -- never reached */
1492 value_neg (struct value
*arg1
)
1495 struct type
*result_type
= value_type (arg1
);
1497 arg1
= coerce_ref (arg1
);
1499 type
= check_typedef (value_type (arg1
));
1501 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1503 struct value
*val
= allocate_value (result_type
);
1504 int len
= TYPE_LENGTH (type
);
1505 gdb_byte decbytes
[16]; /* a decfloat is at most 128 bits long */
1507 memcpy (decbytes
, value_contents (arg1
), len
);
1509 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_LITTLE
)
1510 decbytes
[len
-1] = decbytes
[len
- 1] | 0x80;
1512 decbytes
[0] = decbytes
[0] | 0x80;
1514 memcpy (value_contents_raw (val
), decbytes
, len
);
1518 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1519 return value_from_double (result_type
, -value_as_double (arg1
));
1520 else if (is_integral_type (type
))
1522 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1523 FORTRAN and (the deleted) chill ? */
1524 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1525 result_type
= builtin_type_int
;
1527 return value_from_longest (result_type
, -value_as_long (arg1
));
1531 error (_("Argument to negate operation not a number."));
1532 return 0; /* For lint -- never reached */
1537 value_complement (struct value
*arg1
)
1540 struct type
*result_type
= value_type (arg1
);
1542 arg1
= coerce_ref (arg1
);
1544 type
= check_typedef (value_type (arg1
));
1546 if (!is_integral_type (type
))
1547 error (_("Argument to complement operation not an integer or boolean."));
1549 /* Perform integral promotion for ANSI C/C++.
1550 FIXME: What about FORTRAN ? */
1551 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1552 result_type
= builtin_type_int
;
1554 return value_from_longest (result_type
, ~value_as_long (arg1
));
1557 /* The INDEX'th bit of SET value whose value_type is TYPE,
1558 and whose value_contents is valaddr.
1559 Return -1 if out of range, -2 other error. */
1562 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1564 LONGEST low_bound
, high_bound
;
1567 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1568 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1570 if (index
< low_bound
|| index
> high_bound
)
1572 rel_index
= index
- low_bound
;
1573 word
= unpack_long (builtin_type_unsigned_char
,
1574 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1575 rel_index
%= TARGET_CHAR_BIT
;
1576 if (BITS_BIG_ENDIAN
)
1577 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1578 return (word
>> rel_index
) & 1;
1582 value_in (struct value
*element
, struct value
*set
)
1585 struct type
*settype
= check_typedef (value_type (set
));
1586 struct type
*eltype
= check_typedef (value_type (element
));
1587 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1588 eltype
= TYPE_TARGET_TYPE (eltype
);
1589 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1590 error (_("Second argument of 'IN' has wrong type"));
1591 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1592 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1593 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1594 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1595 error (_("First argument of 'IN' has wrong type"));
1596 member
= value_bit_index (settype
, value_contents (set
),
1597 value_as_long (element
));
1599 error (_("First argument of 'IN' not in range"));
1600 return value_from_longest (LA_BOOL_TYPE
, member
);
1604 _initialize_valarith (void)