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);
43 static struct type
*unop_result_type (enum exp_opcode op
, struct type
*type1
);
44 static struct type
*binop_result_type (enum exp_opcode op
, struct type
*type1
,
47 void _initialize_valarith (void);
50 /* Given a pointer, return the size of its target.
51 If the pointer type is void *, then return 1.
52 If the target type is incomplete, then error out.
53 This isn't a general purpose function, but just a
54 helper for value_sub & value_add.
58 find_size_for_pointer_math (struct type
*ptr_type
)
61 struct type
*ptr_target
;
63 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
65 sz
= TYPE_LENGTH (ptr_target
);
68 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
74 name
= TYPE_NAME (ptr_target
);
76 name
= TYPE_TAG_NAME (ptr_target
);
78 error (_("Cannot perform pointer math on incomplete types, "
79 "try casting to a known type, or void *."));
81 error (_("Cannot perform pointer math on incomplete type \"%s\", "
82 "try casting to a known type, or void *."), name
);
89 value_add (struct value
*arg1
, struct value
*arg2
)
94 struct type
*type1
, *type2
, *valptrtype
;
96 arg1
= coerce_array (arg1
);
97 arg2
= coerce_array (arg2
);
98 type1
= check_typedef (value_type (arg1
));
99 type2
= check_typedef (value_type (arg2
));
101 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
102 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
104 (is_integral_type (type1
) || is_integral_type (type2
)))
105 /* Exactly one argument is a pointer, and one is an integer. */
107 struct value
*retval
;
109 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
122 sz
= find_size_for_pointer_math (valptrtype
);
124 retval
= value_from_pointer (valptrtype
,
125 value_as_address (valptr
)
126 + (sz
* value_as_long (valint
)));
130 return value_binop (arg1
, arg2
, BINOP_ADD
);
134 value_sub (struct value
*arg1
, struct value
*arg2
)
136 struct type
*type1
, *type2
;
137 arg1
= coerce_array (arg1
);
138 arg2
= coerce_array (arg2
);
139 type1
= check_typedef (value_type (arg1
));
140 type2
= check_typedef (value_type (arg2
));
142 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
144 if (is_integral_type (type2
))
146 /* pointer - integer. */
147 LONGEST sz
= find_size_for_pointer_math (type1
);
149 return value_from_pointer (type1
,
150 (value_as_address (arg1
)
151 - (sz
* value_as_long (arg2
))));
153 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
154 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
155 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
157 /* pointer to <type x> - pointer to <type x>. */
158 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
159 return value_from_longest
160 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
161 (value_as_long (arg1
) - value_as_long (arg2
)) / sz
);
166 First argument of `-' is a pointer and second argument is neither\n\
167 an integer nor a pointer of the same type."));
171 return value_binop (arg1
, arg2
, BINOP_SUB
);
174 /* Return the value of ARRAY[IDX].
175 See comments in value_coerce_array() for rationale for reason for
176 doing lower bounds adjustment here rather than there.
177 FIXME: Perhaps we should validate that the index is valid and if
178 verbosity is set, warn about invalid indices (but still use them). */
181 value_subscript (struct value
*array
, struct value
*idx
)
184 int c_style
= current_language
->c_style_arrays
;
187 array
= coerce_ref (array
);
188 tarray
= check_typedef (value_type (array
));
190 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
191 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
193 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
194 LONGEST lowerbound
, upperbound
;
195 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
197 if (VALUE_LVAL (array
) != lval_memory
)
198 return value_subscripted_rvalue (array
, idx
, lowerbound
);
202 LONGEST index
= value_as_long (idx
);
203 if (index
>= lowerbound
&& index
<= upperbound
)
204 return value_subscripted_rvalue (array
, idx
, lowerbound
);
205 /* Emit warning unless we have an array of unknown size.
206 An array of unknown size has lowerbound 0 and upperbound -1. */
208 warning (_("array or string index out of range"));
209 /* fall doing C stuff */
215 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
216 idx
= value_sub (idx
, bound
);
219 array
= value_coerce_array (array
);
222 if (TYPE_CODE (tarray
) == TYPE_CODE_BITSTRING
)
224 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
225 LONGEST index
= value_as_long (idx
);
227 int offset
, byte
, bit_index
;
228 LONGEST lowerbound
, upperbound
;
229 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
230 if (index
< lowerbound
|| index
> upperbound
)
231 error (_("bitstring index out of range"));
233 offset
= index
/ TARGET_CHAR_BIT
;
234 byte
= *((char *) value_contents (array
) + offset
);
235 bit_index
= index
% TARGET_CHAR_BIT
;
236 byte
>>= (gdbarch_bits_big_endian (current_gdbarch
) ?
237 TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
238 v
= value_from_longest (LA_BOOL_TYPE
, byte
& 1);
239 set_value_bitpos (v
, bit_index
);
240 set_value_bitsize (v
, 1);
241 VALUE_LVAL (v
) = VALUE_LVAL (array
);
242 if (VALUE_LVAL (array
) == lval_internalvar
)
243 VALUE_LVAL (v
) = lval_internalvar_component
;
244 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
245 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
246 set_value_offset (v
, offset
+ value_offset (array
));
251 return value_ind (value_add (array
, idx
));
253 error (_("not an array or string"));
256 /* Return the value of EXPR[IDX], expr an aggregate rvalue
257 (eg, a vector register). This routine used to promote floats
258 to doubles, but no longer does. */
260 static struct value
*
261 value_subscripted_rvalue (struct value
*array
, struct value
*idx
, int lowerbound
)
263 struct type
*array_type
= check_typedef (value_type (array
));
264 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
265 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
266 LONGEST index
= value_as_long (idx
);
267 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
270 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
271 error (_("no such vector element"));
273 v
= allocate_value (elt_type
);
274 if (value_lazy (array
))
275 set_value_lazy (v
, 1);
277 memcpy (value_contents_writeable (v
),
278 value_contents (array
) + elt_offs
, elt_size
);
280 if (VALUE_LVAL (array
) == lval_internalvar
)
281 VALUE_LVAL (v
) = lval_internalvar_component
;
283 VALUE_LVAL (v
) = VALUE_LVAL (array
);
284 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
285 VALUE_REGNUM (v
) = VALUE_REGNUM (array
);
286 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
287 set_value_offset (v
, value_offset (array
) + elt_offs
);
291 /* Check to see if either argument is a structure, or a reference to
292 one. This is called so we know whether to go ahead with the normal
293 binop or look for a user defined function instead.
295 For now, we do not overload the `=' operator. */
298 binop_user_defined_p (enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
300 struct type
*type1
, *type2
;
301 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
304 type1
= check_typedef (value_type (arg1
));
305 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
306 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
308 type2
= check_typedef (value_type (arg2
));
309 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
310 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
312 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
313 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
316 /* Check to see if argument is a structure. This is called so
317 we know whether to go ahead with the normal unop or look for a
318 user defined function instead.
320 For now, we do not overload the `&' operator. */
323 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
328 type1
= check_typedef (value_type (arg1
));
331 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
333 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
334 type1
= TYPE_TARGET_TYPE (type1
);
340 /* We know either arg1 or arg2 is a structure, so try to find the right
341 user defined function. Create an argument vector that calls
342 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
343 binary operator which is legal for GNU C++).
345 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
346 is the opcode saying how to modify it. Otherwise, OTHEROP is
350 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
351 enum exp_opcode otherop
, enum noside noside
)
353 struct value
**argvec
;
358 arg1
= coerce_ref (arg1
);
359 arg2
= coerce_ref (arg2
);
360 arg1
= coerce_enum (arg1
);
361 arg2
= coerce_enum (arg2
);
363 /* now we know that what we have to do is construct our
364 arg vector and find the right function to call it with. */
366 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
367 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
369 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
370 argvec
[1] = value_addr (arg1
);
374 /* make the right function name up */
375 strcpy (tstr
, "operator__");
400 case BINOP_BITWISE_AND
:
403 case BINOP_BITWISE_IOR
:
406 case BINOP_BITWISE_XOR
:
409 case BINOP_LOGICAL_AND
:
412 case BINOP_LOGICAL_OR
:
424 case BINOP_ASSIGN_MODIFY
:
442 case BINOP_BITWISE_AND
:
445 case BINOP_BITWISE_IOR
:
448 case BINOP_BITWISE_XOR
:
451 case BINOP_MOD
: /* invalid */
453 error (_("Invalid binary operation specified."));
456 case BINOP_SUBSCRIPT
:
477 case BINOP_MOD
: /* invalid */
479 error (_("Invalid binary operation specified."));
482 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
488 argvec
[1] = argvec
[0];
491 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
493 struct type
*return_type
;
495 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
496 return value_zero (return_type
, VALUE_LVAL (arg1
));
498 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
500 error (_("member function %s not found"), tstr
);
502 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
506 /* We know that arg1 is a structure, so try to find a unary user
507 defined operator that matches the operator in question.
508 Create an argument vector that calls arg1.operator @ (arg1)
509 and return that value (where '@' is (almost) any unary operator which
510 is legal for GNU C++). */
513 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
515 struct value
**argvec
;
516 char *ptr
, *mangle_ptr
;
517 char tstr
[13], mangle_tstr
[13];
518 int static_memfuncp
, nargs
;
520 arg1
= coerce_ref (arg1
);
521 arg1
= coerce_enum (arg1
);
523 /* now we know that what we have to do is construct our
524 arg vector and find the right function to call it with. */
526 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
527 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
529 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
530 argvec
[1] = value_addr (arg1
);
535 /* make the right function name up */
536 strcpy (tstr
, "operator__");
538 strcpy (mangle_tstr
, "__");
539 mangle_ptr
= mangle_tstr
+ 2;
542 case UNOP_PREINCREMENT
:
545 case UNOP_PREDECREMENT
:
548 case UNOP_POSTINCREMENT
:
550 argvec
[2] = value_from_longest (builtin_type_int
, 0);
554 case UNOP_POSTDECREMENT
:
556 argvec
[2] = value_from_longest (builtin_type_int
, 0);
560 case UNOP_LOGICAL_NOT
:
563 case UNOP_COMPLEMENT
:
576 error (_("Invalid unary operation specified."));
579 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
585 argvec
[1] = argvec
[0];
589 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
591 struct type
*return_type
;
593 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
594 return value_zero (return_type
, VALUE_LVAL (arg1
));
596 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
598 error (_("member function %s not found"), tstr
);
599 return 0; /* For lint -- never reached */
603 /* Concatenate two values with the following conditions:
605 (1) Both values must be either bitstring values or character string
606 values and the resulting value consists of the concatenation of
607 ARG1 followed by ARG2.
611 One value must be an integer value and the other value must be
612 either a bitstring value or character string value, which is
613 to be repeated by the number of times specified by the integer
617 (2) Boolean values are also allowed and are treated as bit string
620 (3) Character values are also allowed and are treated as character
621 string values of length 1.
625 value_concat (struct value
*arg1
, struct value
*arg2
)
627 struct value
*inval1
;
628 struct value
*inval2
;
629 struct value
*outval
= NULL
;
630 int inval1len
, inval2len
;
634 struct type
*type1
= check_typedef (value_type (arg1
));
635 struct type
*type2
= check_typedef (value_type (arg2
));
637 /* First figure out if we are dealing with two values to be concatenated
638 or a repeat count and a value to be repeated. INVAL1 is set to the
639 first of two concatenated values, or the repeat count. INVAL2 is set
640 to the second of the two concatenated values or the value to be
643 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
645 struct type
*tmp
= type1
;
657 /* Now process the input values. */
659 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
661 /* We have a repeat count. Validate the second value and then
662 construct a value repeated that many times. */
663 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
664 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
666 count
= longest_to_int (value_as_long (inval1
));
667 inval2len
= TYPE_LENGTH (type2
);
668 ptr
= (char *) alloca (count
* inval2len
);
669 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
671 inchar
= (char) unpack_long (type2
,
672 value_contents (inval2
));
673 for (idx
= 0; idx
< count
; idx
++)
675 *(ptr
+ idx
) = inchar
;
680 for (idx
= 0; idx
< count
; idx
++)
682 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
686 outval
= value_string (ptr
, count
* inval2len
);
688 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
689 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
691 error (_("unimplemented support for bitstring/boolean repeats"));
695 error (_("can't repeat values of that type"));
698 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
699 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
701 /* We have two character strings to concatenate. */
702 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
703 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
705 error (_("Strings can only be concatenated with other strings."));
707 inval1len
= TYPE_LENGTH (type1
);
708 inval2len
= TYPE_LENGTH (type2
);
709 ptr
= (char *) alloca (inval1len
+ inval2len
);
710 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
712 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
716 memcpy (ptr
, value_contents (inval1
), inval1len
);
718 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
721 (char) unpack_long (type2
, value_contents (inval2
));
725 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
727 outval
= value_string (ptr
, inval1len
+ inval2len
);
729 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
730 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
732 /* We have two bitstrings to concatenate. */
733 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
734 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
736 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
738 error (_("unimplemented support for bitstring/boolean concatenation."));
742 /* We don't know how to concatenate these operands. */
743 error (_("illegal operands for concatenation."));
748 /* Return result type of OP performed on TYPE1.
749 The result type follows ANSI C rules.
750 If the result is not appropropriate for any particular language then it
751 needs to patch this function to return the correct type. */
754 unop_result_type (enum exp_opcode op
, struct type
*type1
)
756 struct type
*result_type
;
758 type1
= check_typedef (type1
);
766 case UNOP_COMPLEMENT
:
767 /* Reject floats and decimal floats. */
768 if (!is_integral_type (type1
))
769 error (_("Argument to complement operation not an integer or boolean."));
772 error (_("Invalid unary operation on numbers."));
775 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
776 || TYPE_CODE (type1
) == TYPE_CODE_FLT
)
780 else if (is_integral_type (type1
))
782 /* Perform integral promotion for ANSI C/C++.
783 If not appropropriate for any particular language it needs to
784 modify this function to return the correct result for it. */
785 if (TYPE_LENGTH (type1
) < TYPE_LENGTH (builtin_type_int
))
786 result_type
= builtin_type_int
;
792 error (_("Argument to unary operation not a number."));
793 return 0; /* For lint -- never reached */
797 /* Return result type of OP performed on TYPE1, TYPE2.
798 If the result is not appropropriate for any particular language then it
799 needs to patch this function to return the correct type. */
802 binop_result_type (enum exp_opcode op
, struct type
*type1
, struct type
*type2
)
804 type1
= check_typedef (type1
);
805 type2
= check_typedef (type2
);
807 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
808 && TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
809 && !is_integral_type (type1
))
811 (TYPE_CODE (type2
) != TYPE_CODE_FLT
812 && TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
813 && !is_integral_type (type2
)))
814 error (_("Argument to arithmetic operation not a number or boolean."));
816 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
817 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
828 error (_("Operation not valid for decimal floating point number."));
831 if (TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
)
832 /* If type1 is not a decimal float, the type of the result is the type
833 of the decimal float argument, type2. */
835 else if (TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
)
836 /* Same logic, for the case where type2 is not a decimal float. */
839 /* Both are decimal floats, the type of the result is the bigger
841 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)) ? type1
: type2
;
843 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
844 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
857 error (_("Integer-only operation on floating point number."));
860 switch (current_language
->la_language
)
866 /* Perform ANSI/ISO-C promotions.
867 If only one type is float, use its type.
868 Otherwise use the bigger type. */
869 if (TYPE_CODE (type1
) != TYPE_CODE_FLT
)
871 else if (TYPE_CODE (type2
) != TYPE_CODE_FLT
)
874 return (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
)) ? type1
: type2
;
877 /* For other languages the result type is unchanged from gdb
878 version 6.7 for backward compatibility.
879 If either arg was long double, make sure that value is also long
880 double. Otherwise use double. */
881 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (current_gdbarch
)
882 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (current_gdbarch
))
883 return builtin_type_long_double
;
885 return builtin_type_double
;
888 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
889 && TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
893 case BINOP_BITWISE_AND
:
894 case BINOP_BITWISE_IOR
:
895 case BINOP_BITWISE_XOR
:
900 error (_("Invalid operation on booleans."));
906 /* Integral operations here. */
907 /* FIXME: Also mixed integral/booleans, with result an integer. */
909 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
910 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
911 int is_unsigned1
= TYPE_UNSIGNED (type1
);
912 int is_unsigned2
= TYPE_UNSIGNED (type2
);
913 unsigned int result_len
;
914 int unsigned_operation
;
916 /* Determine type length and signedness after promotion for
918 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
921 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
923 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
926 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
929 /* Determine type length of the result, and if the operation should
930 be done unsigned. For exponentiation and shift operators,
931 use the length and type of the left operand. Otherwise,
932 use the signedness of the operand with the greater length.
933 If both operands are of equal length, use unsigned operation
934 if one of the operands is unsigned. */
935 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
937 /* In case of the shift operators and exponentiation the type of
938 the result only depends on the type of the left operand. */
939 unsigned_operation
= is_unsigned1
;
940 result_len
= promoted_len1
;
942 else if (promoted_len1
> promoted_len2
)
944 unsigned_operation
= is_unsigned1
;
945 result_len
= promoted_len1
;
947 else if (promoted_len2
> promoted_len1
)
949 unsigned_operation
= is_unsigned2
;
950 result_len
= promoted_len2
;
954 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
955 result_len
= promoted_len1
;
970 case BINOP_BITWISE_AND
:
971 case BINOP_BITWISE_IOR
:
972 case BINOP_BITWISE_XOR
:
973 case BINOP_LOGICAL_AND
:
974 case BINOP_LOGICAL_OR
:
983 error (_("Invalid binary operation on numbers."));
986 switch (current_language
->la_language
)
992 if (result_len
<= TYPE_LENGTH (builtin_type_int
))
994 return (unsigned_operation
995 ? builtin_type_unsigned_int
998 else if (result_len
<= TYPE_LENGTH (builtin_type_long
))
1000 return (unsigned_operation
1001 ? builtin_type_unsigned_long
1002 : builtin_type_long
);
1006 return (unsigned_operation
1007 ? builtin_type_unsigned_long_long
1008 : builtin_type_long_long
);
1012 /* For other languages the result type is unchanged from gdb
1013 version 6.7 for backward compatibility.
1014 If either arg was long long, make sure that value is also long
1015 long. Otherwise use long. */
1016 if (unsigned_operation
)
1018 if (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
)
1019 return builtin_type_unsigned_long_long
;
1021 return builtin_type_unsigned_long
;
1025 if (result_len
> gdbarch_long_bit (current_gdbarch
) / HOST_CHAR_BIT
)
1026 return builtin_type_long_long
;
1028 return builtin_type_long
;
1033 return NULL
; /* avoid -Wall warning */
1036 /* Integer exponentiation: V1**V2, where both arguments are
1037 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1039 integer_pow (LONGEST v1
, LONGEST v2
)
1044 error (_("Attempt to raise 0 to negative power."));
1050 /* The Russian Peasant's Algorithm */
1066 /* Integer exponentiation: V1**V2, where both arguments are
1067 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1069 uinteger_pow (ULONGEST v1
, LONGEST v2
)
1074 error (_("Attempt to raise 0 to negative power."));
1080 /* The Russian Peasant's Algorithm */
1096 /* Obtain decimal value of arguments for binary operation, converting from
1097 other types if one of them is not decimal floating point. */
1099 value_args_as_decimal (struct value
*arg1
, struct value
*arg2
,
1100 gdb_byte
*x
, int *len_x
, gdb_byte
*y
, int *len_y
)
1102 struct type
*type1
, *type2
;
1104 type1
= check_typedef (value_type (arg1
));
1105 type2
= check_typedef (value_type (arg2
));
1107 /* At least one of the arguments must be of decimal float type. */
1108 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
1109 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
);
1111 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
1112 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
1113 /* The DFP extension to the C language does not allow mixing of
1114 * decimal float types with other float types in expressions
1115 * (see WDTR 24732, page 12). */
1116 error (_("Mixing decimal floating types with other floating types is not allowed."));
1118 /* Obtain decimal value of arg1, converting from other types
1121 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1123 *len_x
= TYPE_LENGTH (type1
);
1124 memcpy (x
, value_contents (arg1
), *len_x
);
1126 else if (is_integral_type (type1
))
1128 *len_x
= TYPE_LENGTH (type2
);
1129 decimal_from_integral (arg1
, x
, *len_x
);
1132 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
1135 /* Obtain decimal value of arg2, converting from other types
1138 if (TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
1140 *len_y
= TYPE_LENGTH (type2
);
1141 memcpy (y
, value_contents (arg2
), *len_y
);
1143 else if (is_integral_type (type2
))
1145 *len_y
= TYPE_LENGTH (type1
);
1146 decimal_from_integral (arg2
, y
, *len_y
);
1149 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
1153 /* Perform a binary operation on two operands which have reasonable
1154 representations as integers or floats. This includes booleans,
1155 characters, integers, or floats.
1156 Does not support addition and subtraction on pointers;
1157 use value_add or value_sub if you want to handle those possibilities. */
1160 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1163 struct type
*result_type
;
1165 arg1
= coerce_ref (arg1
);
1166 arg2
= coerce_ref (arg2
);
1168 result_type
= binop_result_type (op
, value_type (arg1
), value_type (arg2
));
1170 if (TYPE_CODE (result_type
) == TYPE_CODE_DECFLOAT
)
1172 struct type
*v_type
;
1173 int len_v1
, len_v2
, len_v
;
1174 gdb_byte v1
[16], v2
[16];
1177 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1186 decimal_binop (op
, v1
, len_v1
, v2
, len_v2
, v
, &len_v
);
1190 error (_("Operation not valid for decimal floating point number."));
1193 val
= value_from_decfloat (result_type
, v
);
1195 else if (TYPE_CODE (result_type
) == TYPE_CODE_FLT
)
1197 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1198 in target format. real.c in GCC probably has the necessary
1200 DOUBLEST v1
, v2
, v
= 0;
1201 v1
= value_as_double (arg1
);
1202 v2
= value_as_double (arg2
);
1226 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
1230 v
= v1
< v2
? v1
: v2
;
1234 v
= v1
> v2
? v1
: v2
;
1238 error (_("Integer-only operation on floating point number."));
1241 val
= allocate_value (result_type
);
1242 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
1244 else if (TYPE_CODE (result_type
) == TYPE_CODE_BOOL
)
1246 LONGEST v1
, v2
, v
= 0;
1247 v1
= value_as_long (arg1
);
1248 v2
= value_as_long (arg2
);
1252 case BINOP_BITWISE_AND
:
1256 case BINOP_BITWISE_IOR
:
1260 case BINOP_BITWISE_XOR
:
1268 case BINOP_NOTEQUAL
:
1273 error (_("Invalid operation on booleans."));
1276 val
= allocate_value (result_type
);
1277 store_signed_integer (value_contents_raw (val
),
1278 TYPE_LENGTH (result_type
),
1282 /* Integral operations here. */
1284 int unsigned_operation
= TYPE_UNSIGNED (result_type
);
1286 if (unsigned_operation
)
1288 unsigned int len1
, len2
, result_len
;
1289 LONGEST v2_signed
= value_as_long (arg2
);
1290 ULONGEST v1
, v2
, v
= 0;
1291 v1
= (ULONGEST
) value_as_long (arg1
);
1292 v2
= (ULONGEST
) v2_signed
;
1294 /* Truncate values to the type length of the result.
1295 Things are mildly tricky because binop_result_type may
1296 return a long which on amd64 is 8 bytes, and that's a problem if
1297 ARG1, ARG2 are both <= 4 bytes: we need to truncate the values
1298 at 4 bytes not 8. So fetch the lengths of the original types
1299 and truncate at the larger of the two. */
1300 len1
= TYPE_LENGTH (value_type (arg1
));
1301 len2
= TYPE_LENGTH (value_type (arg1
));
1302 result_len
= len1
> len2
? len1
: len2
;
1303 if (result_len
< sizeof (ULONGEST
))
1305 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1306 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
1328 error (_("Division by zero"));
1332 v
= uinteger_pow (v1
, v2_signed
);
1339 error (_("Division by zero"));
1343 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1344 v1 mod 0 has a defined value, v1. */
1352 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1365 case BINOP_BITWISE_AND
:
1369 case BINOP_BITWISE_IOR
:
1373 case BINOP_BITWISE_XOR
:
1377 case BINOP_LOGICAL_AND
:
1381 case BINOP_LOGICAL_OR
:
1386 v
= v1
< v2
? v1
: v2
;
1390 v
= v1
> v2
? v1
: v2
;
1397 case BINOP_NOTEQUAL
:
1406 error (_("Invalid binary operation on numbers."));
1409 val
= allocate_value (result_type
);
1410 store_unsigned_integer (value_contents_raw (val
),
1411 TYPE_LENGTH (value_type (val
)),
1416 LONGEST v1
, v2
, v
= 0;
1417 v1
= value_as_long (arg1
);
1418 v2
= value_as_long (arg2
);
1439 error (_("Division by zero"));
1443 v
= integer_pow (v1
, v2
);
1450 error (_("Division by zero"));
1454 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1455 X mod 0 has a defined value, X. */
1463 /* Compute floor. */
1464 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1480 case BINOP_BITWISE_AND
:
1484 case BINOP_BITWISE_IOR
:
1488 case BINOP_BITWISE_XOR
:
1492 case BINOP_LOGICAL_AND
:
1496 case BINOP_LOGICAL_OR
:
1501 v
= v1
< v2
? v1
: v2
;
1505 v
= v1
> v2
? v1
: v2
;
1517 error (_("Invalid binary operation on numbers."));
1520 val
= allocate_value (result_type
);
1521 store_signed_integer (value_contents_raw (val
),
1522 TYPE_LENGTH (value_type (val
)),
1530 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1533 value_logical_not (struct value
*arg1
)
1539 arg1
= coerce_number (arg1
);
1540 type1
= check_typedef (value_type (arg1
));
1542 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1543 return 0 == value_as_double (arg1
);
1544 else if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1545 return decimal_is_zero (value_contents (arg1
), TYPE_LENGTH (type1
));
1547 len
= TYPE_LENGTH (type1
);
1548 p
= value_contents (arg1
);
1559 /* Perform a comparison on two string values (whose content are not
1560 necessarily null terminated) based on their length */
1563 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1565 int len1
= TYPE_LENGTH (value_type (arg1
));
1566 int len2
= TYPE_LENGTH (value_type (arg2
));
1567 const gdb_byte
*s1
= value_contents (arg1
);
1568 const gdb_byte
*s2
= value_contents (arg2
);
1569 int i
, len
= len1
< len2
? len1
: len2
;
1571 for (i
= 0; i
< len
; i
++)
1575 else if (s1
[i
] > s2
[i
])
1583 else if (len1
> len2
)
1589 /* Simulate the C operator == by returning a 1
1590 iff ARG1 and ARG2 have equal contents. */
1593 value_equal (struct value
*arg1
, struct value
*arg2
)
1598 struct type
*type1
, *type2
;
1599 enum type_code code1
;
1600 enum type_code code2
;
1601 int is_int1
, is_int2
;
1603 arg1
= coerce_array (arg1
);
1604 arg2
= coerce_array (arg2
);
1606 type1
= check_typedef (value_type (arg1
));
1607 type2
= check_typedef (value_type (arg2
));
1608 code1
= TYPE_CODE (type1
);
1609 code2
= TYPE_CODE (type2
);
1610 is_int1
= is_integral_type (type1
);
1611 is_int2
= is_integral_type (type2
);
1613 if (is_int1
&& is_int2
)
1614 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1616 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1617 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1619 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1620 `long double' values are returned in static storage (m68k). */
1621 DOUBLEST d
= value_as_double (arg1
);
1622 return d
== value_as_double (arg2
);
1624 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1625 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1627 gdb_byte v1
[16], v2
[16];
1630 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1632 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == 0;
1635 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1637 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1638 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1639 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1640 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1642 else if (code1
== code2
1643 && ((len
= (int) TYPE_LENGTH (type1
))
1644 == (int) TYPE_LENGTH (type2
)))
1646 p1
= value_contents (arg1
);
1647 p2
= value_contents (arg2
);
1655 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1657 return value_strcmp (arg1
, arg2
) == 0;
1661 error (_("Invalid type combination in equality test."));
1662 return 0; /* For lint -- never reached */
1666 /* Simulate the C operator < by returning 1
1667 iff ARG1's contents are less than ARG2's. */
1670 value_less (struct value
*arg1
, struct value
*arg2
)
1672 enum type_code code1
;
1673 enum type_code code2
;
1674 struct type
*type1
, *type2
;
1675 int is_int1
, is_int2
;
1677 arg1
= coerce_array (arg1
);
1678 arg2
= coerce_array (arg2
);
1680 type1
= check_typedef (value_type (arg1
));
1681 type2
= check_typedef (value_type (arg2
));
1682 code1
= TYPE_CODE (type1
);
1683 code2
= TYPE_CODE (type2
);
1684 is_int1
= is_integral_type (type1
);
1685 is_int2
= is_integral_type (type2
);
1687 if (is_int1
&& is_int2
)
1688 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1690 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1691 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1693 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1694 `long double' values are returned in static storage (m68k). */
1695 DOUBLEST d
= value_as_double (arg1
);
1696 return d
< value_as_double (arg2
);
1698 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1699 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1701 gdb_byte v1
[16], v2
[16];
1704 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, v2
, &len_v2
);
1706 return decimal_compare (v1
, len_v1
, v2
, len_v2
) == -1;
1708 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1709 return value_as_address (arg1
) < value_as_address (arg2
);
1711 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1713 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1714 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1715 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1716 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1717 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1718 return value_strcmp (arg1
, arg2
) < 0;
1721 error (_("Invalid type combination in ordering comparison."));
1726 /* The unary operators +, - and ~. They free the argument ARG1. */
1729 value_pos (struct value
*arg1
)
1732 struct type
*result_type
;
1734 arg1
= coerce_ref (arg1
);
1735 type
= check_typedef (value_type (arg1
));
1736 result_type
= unop_result_type (UNOP_PLUS
, value_type (arg1
));
1738 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1739 return value_from_double (result_type
, value_as_double (arg1
));
1740 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1741 return value_from_decfloat (result_type
, value_contents (arg1
));
1742 else if (is_integral_type (type
))
1744 return value_from_longest (result_type
, value_as_long (arg1
));
1748 error ("Argument to positive operation not a number.");
1749 return 0; /* For lint -- never reached */
1754 value_neg (struct value
*arg1
)
1757 struct type
*result_type
;
1759 arg1
= coerce_ref (arg1
);
1760 type
= check_typedef (value_type (arg1
));
1761 result_type
= unop_result_type (UNOP_NEG
, value_type (arg1
));
1763 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1765 struct value
*val
= allocate_value (result_type
);
1766 int len
= TYPE_LENGTH (type
);
1767 gdb_byte decbytes
[16]; /* a decfloat is at most 128 bits long */
1769 memcpy (decbytes
, value_contents (arg1
), len
);
1771 if (gdbarch_byte_order (current_gdbarch
) == BFD_ENDIAN_LITTLE
)
1772 decbytes
[len
-1] = decbytes
[len
- 1] | 0x80;
1774 decbytes
[0] = decbytes
[0] | 0x80;
1776 memcpy (value_contents_raw (val
), decbytes
, len
);
1779 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1780 return value_from_double (result_type
, -value_as_double (arg1
));
1781 else if (is_integral_type (type
))
1783 return value_from_longest (result_type
, -value_as_long (arg1
));
1787 error (_("Argument to negate operation not a number."));
1788 return 0; /* For lint -- never reached */
1793 value_complement (struct value
*arg1
)
1796 struct type
*result_type
;
1798 arg1
= coerce_ref (arg1
);
1799 type
= check_typedef (value_type (arg1
));
1800 result_type
= unop_result_type (UNOP_COMPLEMENT
, value_type (arg1
));
1802 if (!is_integral_type (type
))
1803 error (_("Argument to complement operation not an integer or boolean."));
1805 return value_from_longest (result_type
, ~value_as_long (arg1
));
1808 /* The INDEX'th bit of SET value whose value_type is TYPE,
1809 and whose value_contents is valaddr.
1810 Return -1 if out of range, -2 other error. */
1813 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1815 LONGEST low_bound
, high_bound
;
1818 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1819 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1821 if (index
< low_bound
|| index
> high_bound
)
1823 rel_index
= index
- low_bound
;
1824 word
= unpack_long (builtin_type_unsigned_char
,
1825 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1826 rel_index
%= TARGET_CHAR_BIT
;
1827 if (gdbarch_bits_big_endian (current_gdbarch
))
1828 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1829 return (word
>> rel_index
) & 1;
1833 value_in (struct value
*element
, struct value
*set
)
1836 struct type
*settype
= check_typedef (value_type (set
));
1837 struct type
*eltype
= check_typedef (value_type (element
));
1838 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1839 eltype
= TYPE_TARGET_TYPE (eltype
);
1840 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1841 error (_("Second argument of 'IN' has wrong type"));
1842 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1843 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1844 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1845 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1846 error (_("First argument of 'IN' has wrong type"));
1847 member
= value_bit_index (settype
, value_contents (set
),
1848 value_as_long (element
));
1850 error (_("First argument of 'IN' not in range"));
1851 return value_from_longest (LA_BOOL_TYPE
, member
);
1855 _initialize_valarith (void)