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 type
*unop_result_type (enum exp_opcode op
, struct type
*type1
);
43 static struct type
*binop_result_type (enum exp_opcode op
, struct type
*type1
,
46 void _initialize_valarith (void);
49 /* Given a pointer, return the size of its target.
50 If the pointer type is void *, then return 1.
51 If the target type is incomplete, then error out.
52 This isn't a general purpose function, but just a
53 helper for value_sub & value_add.
57 find_size_for_pointer_math (struct type
*ptr_type
)
60 struct type
*ptr_target
;
62 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
64 sz
= TYPE_LENGTH (ptr_target
);
67 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
73 name
= TYPE_NAME (ptr_target
);
75 name
= TYPE_TAG_NAME (ptr_target
);
77 error (_("Cannot perform pointer math on incomplete types, "
78 "try casting to a known type, or void *."));
80 error (_("Cannot perform pointer math on incomplete type \"%s\", "
81 "try casting to a known type, or void *."), name
);
88 value_add (struct value
*arg1
, struct value
*arg2
)
93 struct type
*type1
, *type2
, *valptrtype
;
95 arg1
= coerce_array (arg1
);
96 arg2
= coerce_array (arg2
);
97 type1
= check_typedef (value_type (arg1
));
98 type2
= check_typedef (value_type (arg2
));
100 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
101 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
103 (is_integral_type (type1
) || is_integral_type (type2
)))
104 /* Exactly one argument is a pointer, and one is an integer. */
106 struct value
*retval
;
108 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
121 sz
= find_size_for_pointer_math (valptrtype
);
123 retval
= value_from_pointer (valptrtype
,
124 value_as_address (valptr
)
125 + (sz
* value_as_long (valint
)));
129 return value_binop (arg1
, arg2
, BINOP_ADD
);
133 value_sub (struct value
*arg1
, struct value
*arg2
)
135 struct type
*type1
, *type2
;
136 arg1
= coerce_array (arg1
);
137 arg2
= coerce_array (arg2
);
138 type1
= check_typedef (value_type (arg1
));
139 type2
= check_typedef (value_type (arg2
));
141 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
143 if (is_integral_type (type2
))
145 /* pointer - integer. */
146 LONGEST sz
= find_size_for_pointer_math (type1
);
148 return value_from_pointer (type1
,
149 (value_as_address (arg1
)
150 - (sz
* value_as_long (arg2
))));
152 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
153 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
154 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
156 /* pointer to <type x> - pointer to <type x>. */
157 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
158 return value_from_longest
159 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
160 (value_as_long (arg1
) - value_as_long (arg2
)) / sz
);
165 First argument of `-' is a pointer and second argument is neither\n\
166 an integer nor a pointer of the same type."));
170 return value_binop (arg1
, arg2
, BINOP_SUB
);
173 /* Return the value of ARRAY[IDX].
175 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
176 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
177 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
179 See comments in value_coerce_array() for rationale for reason for
180 doing lower bounds adjustment here rather than there.
181 FIXME: Perhaps we should validate that the index is valid and if
182 verbosity is set, warn about invalid indices (but still use them). */
185 value_subscript (struct value
*array
, struct value
*idx
)
188 int c_style
= current_language
->c_style_arrays
;
191 array
= coerce_ref (array
);
192 tarray
= check_typedef (value_type (array
));
194 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
195 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
197 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
198 LONGEST lowerbound
, upperbound
;
199 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
201 if (VALUE_LVAL (array
) != lval_memory
)
202 return value_subscripted_rvalue (array
, idx
, lowerbound
);
206 LONGEST index
= value_as_long (idx
);
207 if (index
>= lowerbound
&& index
<= upperbound
)
208 return value_subscripted_rvalue (array
, idx
, lowerbound
);
209 /* Emit warning unless we have an array of unknown size.
210 An array of unknown size has lowerbound 0 and upperbound -1. */
212 warning (_("array or string index out of range"));
213 /* fall doing C stuff */
219 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
220 idx
= value_sub (idx
, bound
);
223 array
= value_coerce_array (array
);
227 return value_ind (value_add (array
, idx
));
229 error (_("not an array or string"));
232 /* Return the value of EXPR[IDX], expr an aggregate rvalue
233 (eg, a vector register). This routine used to promote floats
234 to doubles, but no longer does. */
237 value_subscripted_rvalue (struct value
*array
, struct value
*idx
, int lowerbound
)
239 struct type
*array_type
= check_typedef (value_type (array
));
240 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
241 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
242 LONGEST index
= value_as_long (idx
);
243 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
246 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
247 error (_("no such vector element"));
249 v
= allocate_value (elt_type
);
250 if (VALUE_LVAL (array
) == lval_memory
&& value_lazy (array
))
251 set_value_lazy (v
, 1);
253 memcpy (value_contents_writeable (v
),
254 value_contents (array
) + elt_offs
, elt_size
);
256 if (VALUE_LVAL (array
) == lval_internalvar
)
257 VALUE_LVAL (v
) = lval_internalvar_component
;
259 VALUE_LVAL (v
) = VALUE_LVAL (array
);
260 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
261 VALUE_REGNUM (v
) = VALUE_REGNUM (array
);
262 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
263 set_value_offset (v
, value_offset (array
) + elt_offs
);
267 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
270 value_bitstring_subscript (struct type
*type
,
271 struct value
*bitstring
, struct value
*idx
)
274 struct type
*bitstring_type
, *range_type
;
275 LONGEST index
= value_as_long (idx
);
277 int offset
, byte
, bit_index
;
278 LONGEST lowerbound
, upperbound
;
280 bitstring_type
= check_typedef (value_type (bitstring
));
281 gdb_assert (TYPE_CODE (bitstring_type
) == TYPE_CODE_BITSTRING
);
283 range_type
= TYPE_INDEX_TYPE (bitstring_type
);
284 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
285 if (index
< lowerbound
|| index
> upperbound
)
286 error (_("bitstring index out of range"));
289 offset
= index
/ TARGET_CHAR_BIT
;
290 byte
= *((char *) value_contents (bitstring
) + offset
);
292 bit_index
= index
% TARGET_CHAR_BIT
;
293 byte
>>= (gdbarch_bits_big_endian (current_gdbarch
) ?
294 TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
296 v
= value_from_longest (type
, byte
& 1);
298 set_value_bitpos (v
, bit_index
);
299 set_value_bitsize (v
, 1);
301 VALUE_LVAL (v
) = VALUE_LVAL (bitstring
);
302 if (VALUE_LVAL (bitstring
) == lval_internalvar
)
303 VALUE_LVAL (v
) = lval_internalvar_component
;
304 VALUE_ADDRESS (v
) = VALUE_ADDRESS (bitstring
);
305 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (bitstring
);
307 set_value_offset (v
, offset
+ value_offset (bitstring
));
313 /* Check to see if either argument is a structure, or a reference to
314 one. This is called so we know whether to go ahead with the normal
315 binop or look for a user defined function instead.
317 For now, we do not overload the `=' operator. */
320 binop_user_defined_p (enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
322 struct type
*type1
, *type2
;
323 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
326 type1
= check_typedef (value_type (arg1
));
327 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
328 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
330 type2
= check_typedef (value_type (arg2
));
331 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
332 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
334 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
335 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
338 /* Check to see if argument is a structure. This is called so
339 we know whether to go ahead with the normal unop or look for a
340 user defined function instead.
342 For now, we do not overload the `&' operator. */
345 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
350 type1
= check_typedef (value_type (arg1
));
353 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
355 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
356 type1
= TYPE_TARGET_TYPE (type1
);
362 /* We know either arg1 or arg2 is a structure, so try to find the right
363 user defined function. Create an argument vector that calls
364 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
365 binary operator which is legal for GNU C++).
367 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
368 is the opcode saying how to modify it. Otherwise, OTHEROP is
372 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
373 enum exp_opcode otherop
, enum noside noside
)
375 struct value
**argvec
;
380 arg1
= coerce_ref (arg1
);
381 arg2
= coerce_ref (arg2
);
382 arg1
= coerce_enum (arg1
);
383 arg2
= coerce_enum (arg2
);
385 /* now we know that what we have to do is construct our
386 arg vector and find the right function to call it with. */
388 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
389 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
391 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
392 argvec
[1] = value_addr (arg1
);
396 /* make the right function name up */
397 strcpy (tstr
, "operator__");
422 case BINOP_BITWISE_AND
:
425 case BINOP_BITWISE_IOR
:
428 case BINOP_BITWISE_XOR
:
431 case BINOP_LOGICAL_AND
:
434 case BINOP_LOGICAL_OR
:
446 case BINOP_ASSIGN_MODIFY
:
464 case BINOP_BITWISE_AND
:
467 case BINOP_BITWISE_IOR
:
470 case BINOP_BITWISE_XOR
:
473 case BINOP_MOD
: /* invalid */
475 error (_("Invalid binary operation specified."));
478 case BINOP_SUBSCRIPT
:
499 case BINOP_MOD
: /* invalid */
501 error (_("Invalid binary operation specified."));
504 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
510 argvec
[1] = argvec
[0];
513 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
515 struct type
*return_type
;
517 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
518 return value_zero (return_type
, VALUE_LVAL (arg1
));
520 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
522 error (_("member function %s not found"), tstr
);
524 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
528 /* We know that arg1 is a structure, so try to find a unary user
529 defined operator that matches the operator in question.
530 Create an argument vector that calls arg1.operator @ (arg1)
531 and return that value (where '@' is (almost) any unary operator which
532 is legal for GNU C++). */
535 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
537 struct value
**argvec
;
538 char *ptr
, *mangle_ptr
;
539 char tstr
[13], mangle_tstr
[13];
540 int static_memfuncp
, nargs
;
542 arg1
= coerce_ref (arg1
);
543 arg1
= coerce_enum (arg1
);
545 /* now we know that what we have to do is construct our
546 arg vector and find the right function to call it with. */
548 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
549 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
551 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
552 argvec
[1] = value_addr (arg1
);
557 /* make the right function name up */
558 strcpy (tstr
, "operator__");
560 strcpy (mangle_tstr
, "__");
561 mangle_ptr
= mangle_tstr
+ 2;
564 case UNOP_PREINCREMENT
:
567 case UNOP_PREDECREMENT
:
570 case UNOP_POSTINCREMENT
:
572 argvec
[2] = value_from_longest (builtin_type_int
, 0);
576 case UNOP_POSTDECREMENT
:
578 argvec
[2] = value_from_longest (builtin_type_int
, 0);
582 case UNOP_LOGICAL_NOT
:
585 case UNOP_COMPLEMENT
:
598 error (_("Invalid unary operation specified."));
601 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
607 argvec
[1] = argvec
[0];
611 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
613 struct type
*return_type
;
615 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
616 return value_zero (return_type
, VALUE_LVAL (arg1
));
618 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
620 error (_("member function %s not found"), tstr
);
621 return 0; /* For lint -- never reached */
625 /* Concatenate two values with the following conditions:
627 (1) Both values must be either bitstring values or character string
628 values and the resulting value consists of the concatenation of
629 ARG1 followed by ARG2.
633 One value must be an integer value and the other value must be
634 either a bitstring value or character string value, which is
635 to be repeated by the number of times specified by the integer
639 (2) Boolean values are also allowed and are treated as bit string
642 (3) Character values are also allowed and are treated as character
643 string values of length 1.
647 value_concat (struct value
*arg1
, struct value
*arg2
)
649 struct value
*inval1
;
650 struct value
*inval2
;
651 struct value
*outval
= NULL
;
652 int inval1len
, inval2len
;
656 struct type
*type1
= check_typedef (value_type (arg1
));
657 struct type
*type2
= check_typedef (value_type (arg2
));
659 /* First figure out if we are dealing with two values to be concatenated
660 or a repeat count and a value to be repeated. INVAL1 is set to the
661 first of two concatenated values, or the repeat count. INVAL2 is set
662 to the second of the two concatenated values or the value to be
665 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
667 struct type
*tmp
= type1
;
679 /* Now process the input values. */
681 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
683 /* We have a repeat count. Validate the second value and then
684 construct a value repeated that many times. */
685 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
686 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
688 count
= longest_to_int (value_as_long (inval1
));
689 inval2len
= TYPE_LENGTH (type2
);
690 ptr
= (char *) alloca (count
* inval2len
);
691 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
693 inchar
= (char) unpack_long (type2
,
694 value_contents (inval2
));
695 for (idx
= 0; idx
< count
; idx
++)
697 *(ptr
+ idx
) = inchar
;
702 for (idx
= 0; idx
< count
; idx
++)
704 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
708 outval
= value_string (ptr
, count
* inval2len
);
710 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
711 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
713 error (_("unimplemented support for bitstring/boolean repeats"));
717 error (_("can't repeat values of that type"));
720 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
721 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
723 /* We have two character strings to concatenate. */
724 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
725 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
727 error (_("Strings can only be concatenated with other strings."));
729 inval1len
= TYPE_LENGTH (type1
);
730 inval2len
= TYPE_LENGTH (type2
);
731 ptr
= (char *) alloca (inval1len
+ inval2len
);
732 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
734 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
738 memcpy (ptr
, value_contents (inval1
), inval1len
);
740 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
743 (char) unpack_long (type2
, value_contents (inval2
));
747 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
749 outval
= value_string (ptr
, inval1len
+ inval2len
);
751 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
752 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
754 /* We have two bitstrings to concatenate. */
755 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
756 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
758 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
760 error (_("unimplemented support for bitstring/boolean concatenation."));
764 /* We don't know how to concatenate these operands. */
765 error (_("illegal operands for concatenation."));
770 /* Return result type of OP performed on TYPE1.
771 The result type follows ANSI C rules.
772 If the result is not appropropriate for any particular language then it
773 needs to patch this function to return the correct type. */
776 unop_result_type (enum exp_opcode op
, struct type
*type1
)
778 struct type
*result_type
;
780 type1
= check_typedef (type1
);
788 case UNOP_COMPLEMENT
:
789 /* Reject floats and decimal floats. */
790 if (!is_integral_type (type1
))
791 error (_("Argument to complement operation not an integer or boolean."));
794 error (_("Invalid unary operation on numbers."));
797 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
798 || TYPE_CODE (type1
) == TYPE_CODE_FLT
)
802 else if (is_integral_type (type1
))
804 /* Perform integral promotion for ANSI C/C++.
805 If not appropropriate for any particular language it needs to
806 modify this function to return the correct result for it. */
807 if (TYPE_LENGTH (type1
) < TYPE_LENGTH (builtin_type_int
))
808 result_type
= builtin_type_int
;
814 error (_("Argument to unary operation not a number."));
815 return 0; /* For lint -- never reached */
819 /* Return result type of OP performed on TYPE1, TYPE2.
820 If the result is not appropropriate for any particular language then it
821 needs to patch this function to return the correct type. */
824 binop_result_type (enum exp_opcode op
, struct type
*type1
, struct type
*type2
)
826 type1
= check_typedef (type1
);
827 type2
= check_typedef (type2
);
829 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
830 && TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
831 && !is_integral_type (type1
))
833 (TYPE_CODE (type2
) != TYPE_CODE_FLT
834 && TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
835 && !is_integral_type (type2
)))
836 error (_("Argument to arithmetic operation not a number or boolean."));
838 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
839 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
850 error (_("Operation not valid for decimal floating point number."));
853 if (TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
)
854 /* If type1 is not a decimal float, the type of the result is the type
855 of the decimal float argument, type2. */
857 else if (TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
)
858 /* Same logic, for the case where type2 is not a decimal float. */
861 /* Both are decimal floats, the type of the result is the bigger
863 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)) ? type1
: type2
;
865 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
866 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
879 error (_("Integer-only operation on floating point number."));
882 switch (current_language
->la_language
)
888 /* Perform ANSI/ISO-C promotions.
889 If only one type is float, use its type.
890 Otherwise use the bigger type. */
891 if (TYPE_CODE (type1
) != TYPE_CODE_FLT
)
893 else if (TYPE_CODE (type2
) != TYPE_CODE_FLT
)
896 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)) ? type1
: type2
;
899 /* For other languages the result type is unchanged from gdb
900 version 6.7 for backward compatibility.
901 If either arg was long double, make sure that value is also long
902 double. Otherwise use double. */
903 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (current_gdbarch
)
904 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (current_gdbarch
))
905 return builtin_type_long_double
;
907 return builtin_type_double
;
910 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
911 && TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
915 case BINOP_BITWISE_AND
:
916 case BINOP_BITWISE_IOR
:
917 case BINOP_BITWISE_XOR
:
922 error (_("Invalid operation on booleans."));
928 /* Integral operations here. */
929 /* FIXME: Also mixed integral/booleans, with result an integer. */
931 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
932 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
933 int is_unsigned1
= TYPE_UNSIGNED (type1
);
934 int is_unsigned2
= TYPE_UNSIGNED (type2
);
935 unsigned int result_len
;
936 int unsigned_operation
;
938 /* Determine type length and signedness after promotion for
940 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
943 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
945 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
948 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
951 /* Determine type length of the result, and if the operation should
952 be done unsigned. For exponentiation and shift operators,
953 use the length and type of the left operand. Otherwise,
954 use the signedness of the operand with the greater length.
955 If both operands are of equal length, use unsigned operation
956 if one of the operands is unsigned. */
957 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
959 /* In case of the shift operators and exponentiation the type of
960 the result only depends on the type of the left operand. */
961 unsigned_operation
= is_unsigned1
;
962 result_len
= promoted_len1
;
964 else if (promoted_len1
> promoted_len2
)
966 unsigned_operation
= is_unsigned1
;
967 result_len
= promoted_len1
;
969 else if (promoted_len2
> promoted_len1
)
971 unsigned_operation
= is_unsigned2
;
972 result_len
= promoted_len2
;
976 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
977 result_len
= promoted_len1
;
992 case BINOP_BITWISE_AND
:
993 case BINOP_BITWISE_IOR
:
994 case BINOP_BITWISE_XOR
:
995 case BINOP_LOGICAL_AND
:
996 case BINOP_LOGICAL_OR
:
1000 case BINOP_NOTEQUAL
:
1005 error (_("Invalid binary operation on numbers."));
1008 switch (current_language
->la_language
)
1011 case language_cplus
:
1014 if (result_len
<= TYPE_LENGTH (builtin_type_int
))
1016 return (unsigned_operation
1017 ? builtin_type_unsigned_int
1018 : builtin_type_int
);
1020 else if (result_len
<= TYPE_LENGTH (builtin_type_long
))
1022 return (unsigned_operation
1023 ? builtin_type_unsigned_long
1024 : builtin_type_long
);
1028 return (unsigned_operation
1029 ? builtin_type_unsigned_long_long
1030 : builtin_type_long_long
);
1034 /* For other languages the result type is unchanged from gdb
1035 version 6.7 for backward compatibility.
1036 If either arg was long long, make sure that value is also long
1037 long. Otherwise use long. */
1038 if (unsigned_operation
)
1040 if (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
)
1041 return builtin_type_unsigned_long_long
;
1043 return builtin_type_unsigned_long
;
1047 if (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
)
1048 return builtin_type_long_long
;
1050 return builtin_type_long
;
1055 return NULL
; /* avoid -Wall warning */
1058 /* Integer exponentiation: V1**V2, where both arguments are
1059 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1061 integer_pow (LONGEST v1
, LONGEST v2
)
1066 error (_("Attempt to raise 0 to negative power."));
1072 /* The Russian Peasant's Algorithm */
1088 /* Integer exponentiation: V1**V2, where both arguments are
1089 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1091 uinteger_pow (ULONGEST v1
, LONGEST v2
)
1096 error (_("Attempt to raise 0 to negative power."));
1102 /* The Russian Peasant's Algorithm */
1118 /* Obtain decimal value of arguments for binary operation, converting from
1119 other types if one of them is not decimal floating point. */
1121 value_args_as_decimal (struct value
*arg1
, struct value
*arg2
,
1122 gdb_byte
*x
, int *len_x
, gdb_byte
*y
, int *len_y
)
1124 struct type
*type1
, *type2
;
1126 type1
= check_typedef (value_type (arg1
));
1127 type2
= check_typedef (value_type (arg2
));
1129 /* At least one of the arguments must be of decimal float type. */
1130 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
1131 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
);
1133 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
1134 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
1135 /* The DFP extension to the C language does not allow mixing of
1136 * decimal float types with other float types in expressions
1137 * (see WDTR 24732, page 12). */
1138 error (_("Mixing decimal floating types with other floating types is not allowed."));
1140 /* Obtain decimal value of arg1, converting from other types
1143 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1145 *len_x
= TYPE_LENGTH (type1
);
1146 memcpy (x
, value_contents (arg1
), *len_x
);
1148 else if (is_integral_type (type1
))
1150 *len_x
= TYPE_LENGTH (type2
);
1151 decimal_from_integral (arg1
, x
, *len_x
);
1154 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
1157 /* Obtain decimal value of arg2, converting from other types
1160 if (TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
1162 *len_y
= TYPE_LENGTH (type2
);
1163 memcpy (y
, value_contents (arg2
), *len_y
);
1165 else if (is_integral_type (type2
))
1167 *len_y
= TYPE_LENGTH (type1
);
1168 decimal_from_integral (arg2
, y
, *len_y
);
1171 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
1175 /* Perform a binary operation on two operands which have reasonable
1176 representations as integers or floats. This includes booleans,
1177 characters, integers, or floats.
1178 Does not support addition and subtraction on pointers;
1179 use value_add or value_sub if you want to handle those possibilities. */
1182 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1185 struct type
*result_type
;
1187 arg1
= coerce_ref (arg1
);
1188 arg2
= coerce_ref (arg2
);
1190 result_type
= binop_result_type (op
, value_type (arg1
), value_type (arg2
));
1192 if (TYPE_CODE (result_type
) == TYPE_CODE_DECFLOAT
)
1194 struct type
*v_type
;
1195 int len_v1
, len_v2
, len_v
;
1196 gdb_byte v1
[16], v2
[16];
1199 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1208 decimal_binop (op
, v1
, len_v1
, v2
, len_v2
, v
, &len_v
);
1212 error (_("Operation not valid for decimal floating point number."));
1215 val
= value_from_decfloat (result_type
, v
);
1217 else if (TYPE_CODE (result_type
) == TYPE_CODE_FLT
)
1219 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1220 in target format. real.c in GCC probably has the necessary
1222 DOUBLEST v1
, v2
, v
= 0;
1223 v1
= value_as_double (arg1
);
1224 v2
= value_as_double (arg2
);
1248 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
1252 v
= v1
< v2
? v1
: v2
;
1256 v
= v1
> v2
? v1
: v2
;
1260 error (_("Integer-only operation on floating point number."));
1263 val
= allocate_value (result_type
);
1264 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
1266 else if (TYPE_CODE (result_type
) == TYPE_CODE_BOOL
)
1268 LONGEST v1
, v2
, v
= 0;
1269 v1
= value_as_long (arg1
);
1270 v2
= value_as_long (arg2
);
1274 case BINOP_BITWISE_AND
:
1278 case BINOP_BITWISE_IOR
:
1282 case BINOP_BITWISE_XOR
:
1290 case BINOP_NOTEQUAL
:
1295 error (_("Invalid operation on booleans."));
1298 val
= allocate_value (result_type
);
1299 store_signed_integer (value_contents_raw (val
),
1300 TYPE_LENGTH (result_type
),
1304 /* Integral operations here. */
1306 int unsigned_operation
= TYPE_UNSIGNED (result_type
);
1308 if (unsigned_operation
)
1310 unsigned int len1
, len2
, result_len
;
1311 LONGEST v2_signed
= value_as_long (arg2
);
1312 ULONGEST v1
, v2
, v
= 0;
1313 v1
= (ULONGEST
) value_as_long (arg1
);
1314 v2
= (ULONGEST
) v2_signed
;
1316 /* Truncate values to the type length of the result.
1317 Things are mildly tricky because binop_result_type may
1318 return a long which on amd64 is 8 bytes, and that's a problem if
1319 ARG1, ARG2 are both <= 4 bytes: we need to truncate the values
1320 at 4 bytes not 8. So fetch the lengths of the original types
1321 and truncate at the larger of the two. */
1322 len1
= TYPE_LENGTH (value_type (arg1
));
1323 len2
= TYPE_LENGTH (value_type (arg1
));
1324 result_len
= len1
> len2
? len1
: len2
;
1325 if (result_len
< sizeof (ULONGEST
))
1327 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1328 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1350 error (_("Division by zero"));
1354 v
= uinteger_pow (v1
, v2_signed
);
1361 error (_("Division by zero"));
1365 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1366 v1 mod 0 has a defined value, v1. */
1374 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1387 case BINOP_BITWISE_AND
:
1391 case BINOP_BITWISE_IOR
:
1395 case BINOP_BITWISE_XOR
:
1399 case BINOP_LOGICAL_AND
:
1403 case BINOP_LOGICAL_OR
:
1408 v
= v1
< v2
? v1
: v2
;
1412 v
= v1
> v2
? v1
: v2
;
1419 case BINOP_NOTEQUAL
:
1428 error (_("Invalid binary operation on numbers."));
1431 val
= allocate_value (result_type
);
1432 store_unsigned_integer (value_contents_raw (val
),
1433 TYPE_LENGTH (value_type (val
)),
1438 LONGEST v1
, v2
, v
= 0;
1439 v1
= value_as_long (arg1
);
1440 v2
= value_as_long (arg2
);
1461 error (_("Division by zero"));
1465 v
= integer_pow (v1
, v2
);
1472 error (_("Division by zero"));
1476 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1477 X mod 0 has a defined value, X. */
1485 /* Compute floor. */
1486 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1502 case BINOP_BITWISE_AND
:
1506 case BINOP_BITWISE_IOR
:
1510 case BINOP_BITWISE_XOR
:
1514 case BINOP_LOGICAL_AND
:
1518 case BINOP_LOGICAL_OR
:
1523 v
= v1
< v2
? v1
: v2
;
1527 v
= v1
> v2
? v1
: v2
;
1539 error (_("Invalid binary operation on numbers."));
1542 val
= allocate_value (result_type
);
1543 store_signed_integer (value_contents_raw (val
),
1544 TYPE_LENGTH (value_type (val
)),
1552 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1555 value_logical_not (struct value
*arg1
)
1561 arg1
= coerce_number (arg1
);
1562 type1
= check_typedef (value_type (arg1
));
1564 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1565 return 0 == value_as_double (arg1
);
1566 else if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1567 return decimal_is_zero (value_contents (arg1
), TYPE_LENGTH (type1
));
1569 len
= TYPE_LENGTH (type1
);
1570 p
= value_contents (arg1
);
1581 /* Perform a comparison on two string values (whose content are not
1582 necessarily null terminated) based on their length */
1585 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1587 int len1
= TYPE_LENGTH (value_type (arg1
));
1588 int len2
= TYPE_LENGTH (value_type (arg2
));
1589 const gdb_byte
*s1
= value_contents (arg1
);
1590 const gdb_byte
*s2
= value_contents (arg2
);
1591 int i
, len
= len1
< len2
? len1
: len2
;
1593 for (i
= 0; i
< len
; i
++)
1597 else if (s1
[i
] > s2
[i
])
1605 else if (len1
> len2
)
1611 /* Simulate the C operator == by returning a 1
1612 iff ARG1 and ARG2 have equal contents. */
1615 value_equal (struct value
*arg1
, struct value
*arg2
)
1620 struct type
*type1
, *type2
;
1621 enum type_code code1
;
1622 enum type_code code2
;
1623 int is_int1
, is_int2
;
1625 arg1
= coerce_array (arg1
);
1626 arg2
= coerce_array (arg2
);
1628 type1
= check_typedef (value_type (arg1
));
1629 type2
= check_typedef (value_type (arg2
));
1630 code1
= TYPE_CODE (type1
);
1631 code2
= TYPE_CODE (type2
);
1632 is_int1
= is_integral_type (type1
);
1633 is_int2
= is_integral_type (type2
);
1635 if (is_int1
&& is_int2
)
1636 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1638 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1639 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1641 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1642 `long double' values are returned in static storage (m68k). */
1643 DOUBLEST d
= value_as_double (arg1
);
1644 return d
== value_as_double (arg2
);
1646 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1647 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1649 gdb_byte v1
[16], v2
[16];
1652 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1654 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == 0;
1657 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1659 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1660 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1661 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1662 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1664 else if (code1
== code2
1665 && ((len
= (int) TYPE_LENGTH (type1
))
1666 == (int) TYPE_LENGTH (type2
)))
1668 p1
= value_contents (arg1
);
1669 p2
= value_contents (arg2
);
1677 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1679 return value_strcmp (arg1
, arg2
) == 0;
1683 error (_("Invalid type combination in equality test."));
1684 return 0; /* For lint -- never reached */
1688 /* Simulate the C operator < by returning 1
1689 iff ARG1's contents are less than ARG2's. */
1692 value_less (struct value
*arg1
, struct value
*arg2
)
1694 enum type_code code1
;
1695 enum type_code code2
;
1696 struct type
*type1
, *type2
;
1697 int is_int1
, is_int2
;
1699 arg1
= coerce_array (arg1
);
1700 arg2
= coerce_array (arg2
);
1702 type1
= check_typedef (value_type (arg1
));
1703 type2
= check_typedef (value_type (arg2
));
1704 code1
= TYPE_CODE (type1
);
1705 code2
= TYPE_CODE (type2
);
1706 is_int1
= is_integral_type (type1
);
1707 is_int2
= is_integral_type (type2
);
1709 if (is_int1
&& is_int2
)
1710 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1712 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1713 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1715 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1716 `long double' values are returned in static storage (m68k). */
1717 DOUBLEST d
= value_as_double (arg1
);
1718 return d
< value_as_double (arg2
);
1720 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1721 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1723 gdb_byte v1
[16], v2
[16];
1726 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1728 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == -1;
1730 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1731 return value_as_address (arg1
) < value_as_address (arg2
);
1733 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1735 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1736 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1737 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1738 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1739 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1740 return value_strcmp (arg1
, arg2
) < 0;
1743 error (_("Invalid type combination in ordering comparison."));
1748 /* The unary operators +, - and ~. They free the argument ARG1. */
1751 value_pos (struct value
*arg1
)
1754 struct type
*result_type
;
1756 arg1
= coerce_ref (arg1
);
1757 type
= check_typedef (value_type (arg1
));
1758 result_type
= unop_result_type (UNOP_PLUS
, value_type (arg1
));
1760 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1761 return value_from_double (result_type
, value_as_double (arg1
));
1762 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1763 return value_from_decfloat (result_type
, value_contents (arg1
));
1764 else if (is_integral_type (type
))
1766 return value_from_longest (result_type
, value_as_long (arg1
));
1770 error ("Argument to positive operation not a number.");
1771 return 0; /* For lint -- never reached */
1776 value_neg (struct value
*arg1
)
1779 struct type
*result_type
;
1781 arg1
= coerce_ref (arg1
);
1782 type
= check_typedef (value_type (arg1
));
1783 result_type
= unop_result_type (UNOP_NEG
, value_type (arg1
));
1785 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1787 struct value
*val
= allocate_value (result_type
);
1788 int len
= TYPE_LENGTH (type
);
1789 gdb_byte decbytes
[16]; /* a decfloat is at most 128 bits long */
1791 memcpy (decbytes
, value_contents (arg1
), len
);
1793 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_LITTLE
)
1794 decbytes
[len
-1] = decbytes
[len
- 1] | 0x80;
1796 decbytes
[0] = decbytes
[0] | 0x80;
1798 memcpy (value_contents_raw (val
), decbytes
, len
);
1801 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1802 return value_from_double (result_type
, -value_as_double (arg1
));
1803 else if (is_integral_type (type
))
1805 return value_from_longest (result_type
, -value_as_long (arg1
));
1809 error (_("Argument to negate operation not a number."));
1810 return 0; /* For lint -- never reached */
1815 value_complement (struct value
*arg1
)
1818 struct type
*result_type
;
1820 arg1
= coerce_ref (arg1
);
1821 type
= check_typedef (value_type (arg1
));
1822 result_type
= unop_result_type (UNOP_COMPLEMENT
, value_type (arg1
));
1824 if (!is_integral_type (type
))
1825 error (_("Argument to complement operation not an integer or boolean."));
1827 return value_from_longest (result_type
, ~value_as_long (arg1
));
1830 /* The INDEX'th bit of SET value whose value_type is TYPE,
1831 and whose value_contents is valaddr.
1832 Return -1 if out of range, -2 other error. */
1835 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1837 LONGEST low_bound
, high_bound
;
1840 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1841 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1843 if (index
< low_bound
|| index
> high_bound
)
1845 rel_index
= index
- low_bound
;
1846 word
= unpack_long (builtin_type_unsigned_char
,
1847 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1848 rel_index
%= TARGET_CHAR_BIT
;
1849 if (gdbarch_bits_big_endian (current_gdbarch
))
1850 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1851 return (word
>> rel_index
) & 1;
1855 value_in (struct value
*element
, struct value
*set
)
1858 struct type
*settype
= check_typedef (value_type (set
));
1859 struct type
*eltype
= check_typedef (value_type (element
));
1860 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1861 eltype
= TYPE_TARGET_TYPE (eltype
);
1862 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1863 error (_("Second argument of 'IN' has wrong type"));
1864 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1865 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1866 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1867 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1868 error (_("First argument of 'IN' has wrong type"));
1869 member
= value_bit_index (settype
, value_contents (set
),
1870 value_as_long (element
));
1872 error (_("First argument of 'IN' not in range"));
1873 return value_from_longest (LA_BOOL_TYPE
, member
);
1877 _initialize_valarith (void)