1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
27 #include "target-float.h"
29 #include "common/byte-vector.h"
31 /* Define whether or not the C operator '/' truncates towards zero for
32 differently signed operands (truncation direction is undefined in C). */
34 #ifndef TRUNCATION_TOWARDS_ZERO
35 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
38 /* Given a pointer, return the size of its target.
39 If the pointer type is void *, then return 1.
40 If the target type is incomplete, then error out.
41 This isn't a general purpose function, but just a
42 helper for value_ptradd. */
45 find_size_for_pointer_math (struct type
*ptr_type
)
48 struct type
*ptr_target
;
50 gdb_assert (TYPE_CODE (ptr_type
) == TYPE_CODE_PTR
);
51 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
53 sz
= type_length_units (ptr_target
);
56 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
62 name
= TYPE_NAME (ptr_target
);
64 error (_("Cannot perform pointer math on incomplete types, "
65 "try casting to a known type, or void *."));
67 error (_("Cannot perform pointer math on incomplete type \"%s\", "
68 "try casting to a known type, or void *."), name
);
74 /* Given a pointer ARG1 and an integral value ARG2, return the
75 result of C-style pointer arithmetic ARG1 + ARG2. */
78 value_ptradd (struct value
*arg1
, LONGEST arg2
)
80 struct type
*valptrtype
;
84 arg1
= coerce_array (arg1
);
85 valptrtype
= check_typedef (value_type (arg1
));
86 sz
= find_size_for_pointer_math (valptrtype
);
88 result
= value_from_pointer (valptrtype
,
89 value_as_address (arg1
) + sz
* arg2
);
90 if (VALUE_LVAL (result
) != lval_internalvar
)
91 set_value_component_location (result
, arg1
);
95 /* Given two compatible pointer values ARG1 and ARG2, return the
96 result of C-style pointer arithmetic ARG1 - ARG2. */
99 value_ptrdiff (struct value
*arg1
, struct value
*arg2
)
101 struct type
*type1
, *type2
;
104 arg1
= coerce_array (arg1
);
105 arg2
= coerce_array (arg2
);
106 type1
= check_typedef (value_type (arg1
));
107 type2
= check_typedef (value_type (arg2
));
109 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_PTR
);
110 gdb_assert (TYPE_CODE (type2
) == TYPE_CODE_PTR
);
112 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
113 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
114 error (_("First argument of `-' is a pointer and "
115 "second argument is neither\n"
116 "an integer nor a pointer of the same type."));
118 sz
= type_length_units (check_typedef (TYPE_TARGET_TYPE (type1
)));
121 warning (_("Type size unknown, assuming 1. "
122 "Try casting to a known type, or void *."));
126 return (value_as_long (arg1
) - value_as_long (arg2
)) / sz
;
129 /* Return the value of ARRAY[IDX].
131 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
132 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
134 See comments in value_coerce_array() for rationale for reason for
135 doing lower bounds adjustment here rather than there.
136 FIXME: Perhaps we should validate that the index is valid and if
137 verbosity is set, warn about invalid indices (but still use them). */
140 value_subscript (struct value
*array
, LONGEST index
)
142 int c_style
= current_language
->c_style_arrays
;
145 array
= coerce_ref (array
);
146 tarray
= check_typedef (value_type (array
));
148 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
149 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
151 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
152 LONGEST lowerbound
, upperbound
;
154 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
155 if (VALUE_LVAL (array
) != lval_memory
)
156 return value_subscripted_rvalue (array
, index
, lowerbound
);
160 if (index
>= lowerbound
&& index
<= upperbound
)
161 return value_subscripted_rvalue (array
, index
, lowerbound
);
162 /* Emit warning unless we have an array of unknown size.
163 An array of unknown size has lowerbound 0 and upperbound -1. */
165 warning (_("array or string index out of range"));
166 /* fall doing C stuff */
171 array
= value_coerce_array (array
);
175 return value_ind (value_ptradd (array
, index
));
177 error (_("not an array or string"));
180 /* Return the value of EXPR[IDX], expr an aggregate rvalue
181 (eg, a vector register). This routine used to promote floats
182 to doubles, but no longer does. */
185 value_subscripted_rvalue (struct value
*array
, LONGEST index
, int lowerbound
)
187 struct type
*array_type
= check_typedef (value_type (array
));
188 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
189 ULONGEST elt_size
= type_length_units (elt_type
);
190 ULONGEST elt_offs
= elt_size
* (index
- lowerbound
);
192 if (index
< lowerbound
|| (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)
193 && elt_offs
>= type_length_units (array_type
)))
195 if (type_not_associated (array_type
))
196 error (_("no such vector element (vector not associated)"));
197 else if (type_not_allocated (array_type
))
198 error (_("no such vector element (vector not allocated)"));
200 error (_("no such vector element"));
203 if (is_dynamic_type (elt_type
))
207 address
= value_address (array
) + elt_offs
;
208 elt_type
= resolve_dynamic_type (elt_type
, NULL
, address
);
211 return value_from_component (array
, elt_type
, elt_offs
);
215 /* Check to see if either argument is a structure, or a reference to
216 one. This is called so we know whether to go ahead with the normal
217 binop or look for a user defined function instead.
219 For now, we do not overload the `=' operator. */
222 binop_types_user_defined_p (enum exp_opcode op
,
223 struct type
*type1
, struct type
*type2
)
225 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
228 type1
= check_typedef (type1
);
229 if (TYPE_IS_REFERENCE (type1
))
230 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
232 type2
= check_typedef (type2
);
233 if (TYPE_IS_REFERENCE (type2
))
234 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
236 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
237 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
240 /* Check to see if either argument is a structure, or a reference to
241 one. This is called so we know whether to go ahead with the normal
242 binop or look for a user defined function instead.
244 For now, we do not overload the `=' operator. */
247 binop_user_defined_p (enum exp_opcode op
,
248 struct value
*arg1
, struct value
*arg2
)
250 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
253 /* Check to see if argument is a structure. This is called so
254 we know whether to go ahead with the normal unop or look for a
255 user defined function instead.
257 For now, we do not overload the `&' operator. */
260 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
266 type1
= check_typedef (value_type (arg1
));
267 if (TYPE_IS_REFERENCE (type1
))
268 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
269 return TYPE_CODE (type1
) == TYPE_CODE_STRUCT
;
272 /* Try to find an operator named OPERATOR which takes NARGS arguments
273 specified in ARGS. If the operator found is a static member operator
274 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
275 The search if performed through find_overload_match which will handle
276 member operators, non member operators, operators imported implicitly or
277 explicitly, and perform correct overload resolution in all of the above
278 situations or combinations thereof. */
280 static struct value
*
281 value_user_defined_cpp_op (struct value
**args
, int nargs
, char *oper
,
282 int *static_memfuncp
, enum noside noside
)
285 struct symbol
*symp
= NULL
;
286 struct value
*valp
= NULL
;
288 find_overload_match (args
, nargs
, oper
, BOTH
/* could be method */,
290 NULL
/* pass NULL symbol since symbol is unknown */,
291 &valp
, &symp
, static_memfuncp
, 0, noside
);
298 /* This is a non member function and does not
299 expect a reference as its first argument
300 rather the explicit structure. */
301 args
[0] = value_ind (args
[0]);
302 return value_of_variable (symp
, 0);
305 error (_("Could not find %s."), oper
);
308 /* Lookup user defined operator NAME. Return a value representing the
309 function, otherwise return NULL. */
311 static struct value
*
312 value_user_defined_op (struct value
**argp
, struct value
**args
, char *name
,
313 int *static_memfuncp
, int nargs
, enum noside noside
)
315 struct value
*result
= NULL
;
317 if (current_language
->la_language
== language_cplus
)
319 result
= value_user_defined_cpp_op (args
, nargs
, name
, static_memfuncp
,
323 result
= value_struct_elt (argp
, args
, name
, static_memfuncp
,
329 /* We know either arg1 or arg2 is a structure, so try to find the right
330 user defined function. Create an argument vector that calls
331 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
332 binary operator which is legal for GNU C++).
334 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
335 is the opcode saying how to modify it. Otherwise, OTHEROP is
339 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
340 enum exp_opcode otherop
, enum noside noside
)
342 struct value
**argvec
;
347 arg1
= coerce_ref (arg1
);
348 arg2
= coerce_ref (arg2
);
350 /* now we know that what we have to do is construct our
351 arg vector and find the right function to call it with. */
353 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
354 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
356 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
357 argvec
[1] = value_addr (arg1
);
361 /* Make the right function name up. */
362 strcpy (tstr
, "operator__");
387 case BINOP_BITWISE_AND
:
390 case BINOP_BITWISE_IOR
:
393 case BINOP_BITWISE_XOR
:
396 case BINOP_LOGICAL_AND
:
399 case BINOP_LOGICAL_OR
:
411 case BINOP_ASSIGN_MODIFY
:
429 case BINOP_BITWISE_AND
:
432 case BINOP_BITWISE_IOR
:
435 case BINOP_BITWISE_XOR
:
438 case BINOP_MOD
: /* invalid */
440 error (_("Invalid binary operation specified."));
443 case BINOP_SUBSCRIPT
:
464 case BINOP_MOD
: /* invalid */
466 error (_("Invalid binary operation specified."));
469 argvec
[0] = value_user_defined_op (&arg1
, argvec
+ 1, tstr
,
470 &static_memfuncp
, 2, noside
);
476 argvec
[1] = argvec
[0];
479 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
481 /* Static xmethods are not supported yet. */
482 gdb_assert (static_memfuncp
== 0);
483 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
485 struct type
*return_type
486 = result_type_of_xmethod (argvec
[0], 2, argvec
+ 1);
488 if (return_type
== NULL
)
489 error (_("Xmethod is missing return type."));
490 return value_zero (return_type
, VALUE_LVAL (arg1
));
492 return call_xmethod (argvec
[0], 2, argvec
+ 1);
494 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
496 struct type
*return_type
;
499 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
500 return value_zero (return_type
, VALUE_LVAL (arg1
));
502 return call_function_by_hand (argvec
[0], NULL
, 2 - static_memfuncp
,
505 throw_error (NOT_FOUND_ERROR
,
506 _("member function %s not found"), tstr
);
509 /* We know that arg1 is a structure, so try to find a unary user
510 defined operator that matches the operator in question.
511 Create an argument vector that calls arg1.operator @ (arg1)
512 and return that value (where '@' is (almost) any unary operator which
513 is legal for GNU C++). */
516 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
518 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
519 struct value
**argvec
;
521 char tstr
[13], mangle_tstr
[13];
522 int static_memfuncp
, nargs
;
524 arg1
= coerce_ref (arg1
);
526 /* now we know that what we have to do is construct our
527 arg vector and find the right function to call it with. */
529 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
530 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
532 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
533 argvec
[1] = value_addr (arg1
);
538 /* Make the right function name up. */
539 strcpy (tstr
, "operator__");
541 strcpy (mangle_tstr
, "__");
544 case UNOP_PREINCREMENT
:
547 case UNOP_PREDECREMENT
:
550 case UNOP_POSTINCREMENT
:
552 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
556 case UNOP_POSTDECREMENT
:
558 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
562 case UNOP_LOGICAL_NOT
:
565 case UNOP_COMPLEMENT
:
581 error (_("Invalid unary operation specified."));
584 argvec
[0] = value_user_defined_op (&arg1
, argvec
+ 1, tstr
,
585 &static_memfuncp
, nargs
, noside
);
591 argvec
[1] = argvec
[0];
595 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
597 /* Static xmethods are not supported yet. */
598 gdb_assert (static_memfuncp
== 0);
599 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
601 struct type
*return_type
602 = result_type_of_xmethod (argvec
[0], 1, argvec
+ 1);
604 if (return_type
== NULL
)
605 error (_("Xmethod is missing return type."));
606 return value_zero (return_type
, VALUE_LVAL (arg1
));
608 return call_xmethod (argvec
[0], 1, argvec
+ 1);
610 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
612 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], NULL
, nargs
, argvec
+ 1);
620 throw_error (NOT_FOUND_ERROR
,
621 _("member function %s not found"), tstr
);
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. */
646 value_concat (struct value
*arg1
, struct value
*arg2
)
648 struct value
*inval1
;
649 struct value
*inval2
;
650 struct value
*outval
= NULL
;
651 int inval1len
, inval2len
;
654 struct type
*type1
= check_typedef (value_type (arg1
));
655 struct type
*type2
= check_typedef (value_type (arg2
));
656 struct type
*char_type
;
658 /* First figure out if we are dealing with two values to be concatenated
659 or a repeat count and a value to be repeated. INVAL1 is set to the
660 first of two concatenated values, or the repeat count. INVAL2 is set
661 to the second of the two concatenated values or the value to be
664 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
666 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 std::vector
<char> ptr (count
* inval2len
);
691 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
695 inchar
= (char) unpack_long (type2
,
696 value_contents (inval2
));
697 for (idx
= 0; idx
< count
; idx
++)
704 char_type
= TYPE_TARGET_TYPE (type2
);
706 for (idx
= 0; idx
< count
; idx
++)
708 memcpy (&ptr
[idx
* inval2len
], value_contents (inval2
),
712 outval
= value_string (ptr
.data (), count
* inval2len
, char_type
);
714 else if (TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
716 error (_("unimplemented support for boolean repeats"));
720 error (_("can't repeat values of that type"));
723 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
724 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
726 /* We have two character strings to concatenate. */
727 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
728 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
730 error (_("Strings can only be concatenated with other strings."));
732 inval1len
= TYPE_LENGTH (type1
);
733 inval2len
= TYPE_LENGTH (type2
);
734 std::vector
<char> ptr (inval1len
+ inval2len
);
735 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
739 ptr
[0] = (char) unpack_long (type1
, value_contents (inval1
));
743 char_type
= TYPE_TARGET_TYPE (type1
);
745 memcpy (ptr
.data (), value_contents (inval1
), inval1len
);
747 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
750 (char) unpack_long (type2
, value_contents (inval2
));
754 memcpy (&ptr
[inval1len
], value_contents (inval2
), inval2len
);
756 outval
= value_string (ptr
.data (), inval1len
+ inval2len
, char_type
);
758 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
760 /* We have two bitstrings to concatenate. */
761 if (TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
763 error (_("Booleans can only be concatenated "
764 "with other bitstrings or booleans."));
766 error (_("unimplemented support for boolean concatenation."));
770 /* We don't know how to concatenate these operands. */
771 error (_("illegal operands for concatenation."));
776 /* Integer exponentiation: V1**V2, where both arguments are
777 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
780 integer_pow (LONGEST v1
, LONGEST v2
)
785 error (_("Attempt to raise 0 to negative power."));
791 /* The Russian Peasant's Algorithm. */
807 /* Integer exponentiation: V1**V2, where both arguments are
808 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
811 uinteger_pow (ULONGEST v1
, LONGEST v2
)
816 error (_("Attempt to raise 0 to negative power."));
822 /* The Russian Peasant's Algorithm. */
838 /* Obtain argument values for binary operation, converting from
839 other types if one of them is not floating point. */
841 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
842 gdb_byte
*x
, struct type
**eff_type_x
,
843 gdb_byte
*y
, struct type
**eff_type_y
)
845 struct type
*type1
, *type2
;
847 type1
= check_typedef (value_type (arg1
));
848 type2
= check_typedef (value_type (arg2
));
850 /* At least one of the arguments must be of floating-point type. */
851 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
853 if (is_floating_type (type1
) && is_floating_type (type2
)
854 && TYPE_CODE (type1
) != TYPE_CODE (type2
))
855 /* The DFP extension to the C language does not allow mixing of
856 * decimal float types with other float types in expressions
857 * (see WDTR 24732, page 12). */
858 error (_("Mixing decimal floating types with "
859 "other floating types is not allowed."));
861 /* Obtain value of arg1, converting from other types if necessary. */
863 if (is_floating_type (type1
))
866 memcpy (x
, value_contents (arg1
), TYPE_LENGTH (type1
));
868 else if (is_integral_type (type1
))
871 if (TYPE_UNSIGNED (type1
))
872 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
874 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
877 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
880 /* Obtain value of arg2, converting from other types if necessary. */
882 if (is_floating_type (type2
))
885 memcpy (y
, value_contents (arg2
), TYPE_LENGTH (type2
));
887 else if (is_integral_type (type2
))
890 if (TYPE_UNSIGNED (type2
))
891 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
893 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
896 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
900 /* Perform a binary operation on two operands which have reasonable
901 representations as integers or floats. This includes booleans,
902 characters, integers, or floats.
903 Does not support addition and subtraction on pointers;
904 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
906 static struct value
*
907 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
910 struct type
*type1
, *type2
, *result_type
;
912 arg1
= coerce_ref (arg1
);
913 arg2
= coerce_ref (arg2
);
915 type1
= check_typedef (value_type (arg1
));
916 type2
= check_typedef (value_type (arg2
));
918 if ((!is_floating_value (arg1
) && !is_integral_type (type1
))
919 || (!is_floating_value (arg2
) && !is_integral_type (type2
)))
920 error (_("Argument to arithmetic operation not a number or boolean."));
922 if (is_floating_type (type1
) || is_floating_type (type2
))
924 /* If only one type is floating-point, use its type.
925 Otherwise use the bigger type. */
926 if (!is_floating_type (type1
))
928 else if (!is_floating_type (type2
))
930 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
935 val
= allocate_value (result_type
);
937 struct type
*eff_type_v1
, *eff_type_v2
;
938 gdb::byte_vector v1
, v2
;
939 v1
.resize (TYPE_LENGTH (result_type
));
940 v2
.resize (TYPE_LENGTH (result_type
));
942 value_args_as_target_float (arg1
, arg2
,
943 v1
.data (), &eff_type_v1
,
944 v2
.data (), &eff_type_v2
);
945 target_float_binop (op
, v1
.data (), eff_type_v1
,
946 v2
.data (), eff_type_v2
,
947 value_contents_raw (val
), result_type
);
949 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
950 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
952 LONGEST v1
, v2
, v
= 0;
954 v1
= value_as_long (arg1
);
955 v2
= value_as_long (arg2
);
959 case BINOP_BITWISE_AND
:
963 case BINOP_BITWISE_IOR
:
967 case BINOP_BITWISE_XOR
:
980 error (_("Invalid operation on booleans."));
985 val
= allocate_value (result_type
);
986 store_signed_integer (value_contents_raw (val
),
987 TYPE_LENGTH (result_type
),
988 gdbarch_byte_order (get_type_arch (result_type
)),
992 /* Integral operations here. */
994 /* Determine type length of the result, and if the operation should
995 be done unsigned. For exponentiation and shift operators,
996 use the length and type of the left operand. Otherwise,
997 use the signedness of the operand with the greater length.
998 If both operands are of equal length, use unsigned operation
999 if one of the operands is unsigned. */
1000 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1001 result_type
= type1
;
1002 else if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1003 result_type
= type1
;
1004 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1005 result_type
= type2
;
1006 else if (TYPE_UNSIGNED (type1
))
1007 result_type
= type1
;
1008 else if (TYPE_UNSIGNED (type2
))
1009 result_type
= type2
;
1011 result_type
= type1
;
1013 if (TYPE_UNSIGNED (result_type
))
1015 LONGEST v2_signed
= value_as_long (arg2
);
1016 ULONGEST v1
, v2
, v
= 0;
1018 v1
= (ULONGEST
) value_as_long (arg1
);
1019 v2
= (ULONGEST
) v2_signed
;
1040 error (_("Division by zero"));
1044 v
= uinteger_pow (v1
, v2_signed
);
1051 error (_("Division by zero"));
1055 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1056 v1 mod 0 has a defined value, v1. */
1064 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1077 case BINOP_BITWISE_AND
:
1081 case BINOP_BITWISE_IOR
:
1085 case BINOP_BITWISE_XOR
:
1089 case BINOP_LOGICAL_AND
:
1093 case BINOP_LOGICAL_OR
:
1098 v
= v1
< v2
? v1
: v2
;
1102 v
= v1
> v2
? v1
: v2
;
1109 case BINOP_NOTEQUAL
:
1130 error (_("Invalid binary operation on numbers."));
1133 val
= allocate_value (result_type
);
1134 store_unsigned_integer (value_contents_raw (val
),
1135 TYPE_LENGTH (value_type (val
)),
1137 (get_type_arch (result_type
)),
1142 LONGEST v1
, v2
, v
= 0;
1144 v1
= value_as_long (arg1
);
1145 v2
= value_as_long (arg2
);
1166 error (_("Division by zero"));
1170 v
= integer_pow (v1
, v2
);
1177 error (_("Division by zero"));
1181 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1182 X mod 0 has a defined value, X. */
1190 /* Compute floor. */
1191 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1207 case BINOP_BITWISE_AND
:
1211 case BINOP_BITWISE_IOR
:
1215 case BINOP_BITWISE_XOR
:
1219 case BINOP_LOGICAL_AND
:
1223 case BINOP_LOGICAL_OR
:
1228 v
= v1
< v2
? v1
: v2
;
1232 v
= v1
> v2
? v1
: v2
;
1239 case BINOP_NOTEQUAL
:
1260 error (_("Invalid binary operation on numbers."));
1263 val
= allocate_value (result_type
);
1264 store_signed_integer (value_contents_raw (val
),
1265 TYPE_LENGTH (value_type (val
)),
1267 (get_type_arch (result_type
)),
1275 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1276 replicating SCALAR_VALUE for each element of the vector. Only scalar
1277 types that can be cast to the type of one element of the vector are
1278 acceptable. The newly created vector value is returned upon success,
1279 otherwise an error is thrown. */
1282 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1284 /* Widen the scalar to a vector. */
1285 struct type
*eltype
, *scalar_type
;
1286 struct value
*val
, *elval
;
1287 LONGEST low_bound
, high_bound
;
1290 vector_type
= check_typedef (vector_type
);
1292 gdb_assert (TYPE_CODE (vector_type
) == TYPE_CODE_ARRAY
1293 && TYPE_VECTOR (vector_type
));
1295 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1296 error (_("Could not determine the vector bounds"));
1298 eltype
= check_typedef (TYPE_TARGET_TYPE (vector_type
));
1299 elval
= value_cast (eltype
, scalar_value
);
1301 scalar_type
= check_typedef (value_type (scalar_value
));
1303 /* If we reduced the length of the scalar then check we didn't loose any
1305 if (TYPE_LENGTH (eltype
) < TYPE_LENGTH (scalar_type
)
1306 && !value_equal (elval
, scalar_value
))
1307 error (_("conversion of scalar to vector involves truncation"));
1309 val
= allocate_value (vector_type
);
1310 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1311 /* Duplicate the contents of elval into the destination vector. */
1312 memcpy (value_contents_writeable (val
) + (i
* TYPE_LENGTH (eltype
)),
1313 value_contents_all (elval
), TYPE_LENGTH (eltype
));
1318 /* Performs a binary operation on two vector operands by calling scalar_binop
1319 for each pair of vector components. */
1321 static struct value
*
1322 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1324 struct value
*val
, *tmp
, *mark
;
1325 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1326 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1327 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1329 type1
= check_typedef (value_type (val1
));
1330 type2
= check_typedef (value_type (val2
));
1332 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1333 && TYPE_VECTOR (type1
)) ? 1 : 0;
1334 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1335 && TYPE_VECTOR (type2
)) ? 1 : 0;
1337 if (!t1_is_vec
|| !t2_is_vec
)
1338 error (_("Vector operations are only supported among vectors"));
1340 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1341 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1342 error (_("Could not determine the vector bounds"));
1344 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1345 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1346 elsize
= TYPE_LENGTH (eltype1
);
1348 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
1349 || elsize
!= TYPE_LENGTH (eltype2
)
1350 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
1351 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1352 error (_("Cannot perform operation on vectors with different types"));
1354 val
= allocate_value (type1
);
1355 mark
= value_mark ();
1356 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1358 tmp
= value_binop (value_subscript (val1
, i
),
1359 value_subscript (val2
, i
), op
);
1360 memcpy (value_contents_writeable (val
) + i
* elsize
,
1361 value_contents_all (tmp
),
1364 value_free_to_mark (mark
);
1369 /* Perform a binary operation on two operands. */
1372 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1375 struct type
*type1
= check_typedef (value_type (arg1
));
1376 struct type
*type2
= check_typedef (value_type (arg2
));
1377 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1378 && TYPE_VECTOR (type1
));
1379 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1380 && TYPE_VECTOR (type2
));
1382 if (!t1_is_vec
&& !t2_is_vec
)
1383 val
= scalar_binop (arg1
, arg2
, op
);
1384 else if (t1_is_vec
&& t2_is_vec
)
1385 val
= vector_binop (arg1
, arg2
, op
);
1388 /* Widen the scalar operand to a vector. */
1389 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1390 struct type
*t
= t1_is_vec
? type2
: type1
;
1392 if (TYPE_CODE (t
) != TYPE_CODE_FLT
1393 && TYPE_CODE (t
) != TYPE_CODE_DECFLOAT
1394 && !is_integral_type (t
))
1395 error (_("Argument to operation not a number or boolean."));
1397 /* Replicate the scalar value to make a vector value. */
1398 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1400 val
= vector_binop (arg1
, arg2
, op
);
1406 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1409 value_logical_not (struct value
*arg1
)
1415 arg1
= coerce_array (arg1
);
1416 type1
= check_typedef (value_type (arg1
));
1418 if (is_floating_value (arg1
))
1419 return target_float_is_zero (value_contents (arg1
), type1
);
1421 len
= TYPE_LENGTH (type1
);
1422 p
= value_contents (arg1
);
1433 /* Perform a comparison on two string values (whose content are not
1434 necessarily null terminated) based on their length. */
1437 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1439 int len1
= TYPE_LENGTH (value_type (arg1
));
1440 int len2
= TYPE_LENGTH (value_type (arg2
));
1441 const gdb_byte
*s1
= value_contents (arg1
);
1442 const gdb_byte
*s2
= value_contents (arg2
);
1443 int i
, len
= len1
< len2
? len1
: len2
;
1445 for (i
= 0; i
< len
; i
++)
1449 else if (s1
[i
] > s2
[i
])
1457 else if (len1
> len2
)
1463 /* Simulate the C operator == by returning a 1
1464 iff ARG1 and ARG2 have equal contents. */
1467 value_equal (struct value
*arg1
, struct value
*arg2
)
1472 struct type
*type1
, *type2
;
1473 enum type_code code1
;
1474 enum type_code code2
;
1475 int is_int1
, is_int2
;
1477 arg1
= coerce_array (arg1
);
1478 arg2
= coerce_array (arg2
);
1480 type1
= check_typedef (value_type (arg1
));
1481 type2
= check_typedef (value_type (arg2
));
1482 code1
= TYPE_CODE (type1
);
1483 code2
= TYPE_CODE (type2
);
1484 is_int1
= is_integral_type (type1
);
1485 is_int2
= is_integral_type (type2
);
1487 if (is_int1
&& is_int2
)
1488 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1490 else if ((is_floating_value (arg1
) || is_int1
)
1491 && (is_floating_value (arg2
) || is_int2
))
1493 struct type
*eff_type_v1
, *eff_type_v2
;
1494 gdb::byte_vector v1
, v2
;
1495 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1496 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1498 value_args_as_target_float (arg1
, arg2
,
1499 v1
.data (), &eff_type_v1
,
1500 v2
.data (), &eff_type_v2
);
1502 return target_float_compare (v1
.data (), eff_type_v1
,
1503 v2
.data (), eff_type_v2
) == 0;
1506 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1508 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1509 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1510 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1511 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1513 else if (code1
== code2
1514 && ((len
= (int) TYPE_LENGTH (type1
))
1515 == (int) TYPE_LENGTH (type2
)))
1517 p1
= value_contents (arg1
);
1518 p2
= value_contents (arg2
);
1526 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1528 return value_strcmp (arg1
, arg2
) == 0;
1531 error (_("Invalid type combination in equality test."));
1534 /* Compare values based on their raw contents. Useful for arrays since
1535 value_equal coerces them to pointers, thus comparing just the address
1536 of the array instead of its contents. */
1539 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1541 struct type
*type1
, *type2
;
1543 type1
= check_typedef (value_type (arg1
));
1544 type2
= check_typedef (value_type (arg2
));
1546 return (TYPE_CODE (type1
) == TYPE_CODE (type2
)
1547 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1548 && memcmp (value_contents (arg1
), value_contents (arg2
),
1549 TYPE_LENGTH (type1
)) == 0);
1552 /* Simulate the C operator < by returning 1
1553 iff ARG1's contents are less than ARG2's. */
1556 value_less (struct value
*arg1
, struct value
*arg2
)
1558 enum type_code code1
;
1559 enum type_code code2
;
1560 struct type
*type1
, *type2
;
1561 int is_int1
, is_int2
;
1563 arg1
= coerce_array (arg1
);
1564 arg2
= coerce_array (arg2
);
1566 type1
= check_typedef (value_type (arg1
));
1567 type2
= check_typedef (value_type (arg2
));
1568 code1
= TYPE_CODE (type1
);
1569 code2
= TYPE_CODE (type2
);
1570 is_int1
= is_integral_type (type1
);
1571 is_int2
= is_integral_type (type2
);
1573 if (is_int1
&& is_int2
)
1574 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1576 else if ((is_floating_value (arg1
) || is_int1
)
1577 && (is_floating_value (arg2
) || is_int2
))
1579 struct type
*eff_type_v1
, *eff_type_v2
;
1580 gdb::byte_vector v1
, v2
;
1581 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1582 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1584 value_args_as_target_float (arg1
, arg2
,
1585 v1
.data (), &eff_type_v1
,
1586 v2
.data (), &eff_type_v2
);
1588 return target_float_compare (v1
.data (), eff_type_v1
,
1589 v2
.data (), eff_type_v2
) == -1;
1591 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1592 return value_as_address (arg1
) < value_as_address (arg2
);
1594 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1596 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1597 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1598 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1599 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1600 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1601 return value_strcmp (arg1
, arg2
) < 0;
1604 error (_("Invalid type combination in ordering comparison."));
1609 /* The unary operators +, - and ~. They free the argument ARG1. */
1612 value_pos (struct value
*arg1
)
1616 arg1
= coerce_ref (arg1
);
1617 type
= check_typedef (value_type (arg1
));
1619 if (is_integral_type (type
) || is_floating_value (arg1
)
1620 || (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
)))
1621 return value_from_contents (type
, value_contents (arg1
));
1623 error (_("Argument to positive operation not a number."));
1627 value_neg (struct value
*arg1
)
1631 arg1
= coerce_ref (arg1
);
1632 type
= check_typedef (value_type (arg1
));
1634 if (is_integral_type (type
) || is_floating_type (type
))
1635 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1636 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1638 struct value
*tmp
, *val
= allocate_value (type
);
1639 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1641 LONGEST low_bound
, high_bound
;
1643 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1644 error (_("Could not determine the vector bounds"));
1646 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1648 tmp
= value_neg (value_subscript (arg1
, i
));
1649 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1650 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1655 error (_("Argument to negate operation not a number."));
1659 value_complement (struct value
*arg1
)
1664 arg1
= coerce_ref (arg1
);
1665 type
= check_typedef (value_type (arg1
));
1667 if (is_integral_type (type
))
1668 val
= value_from_longest (type
, ~value_as_long (arg1
));
1669 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1672 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1674 LONGEST low_bound
, high_bound
;
1676 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1677 error (_("Could not determine the vector bounds"));
1679 val
= allocate_value (type
);
1680 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1682 tmp
= value_complement (value_subscript (arg1
, i
));
1683 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1684 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1688 error (_("Argument to complement operation not an integer, boolean."));
1693 /* The INDEX'th bit of SET value whose value_type is TYPE,
1694 and whose value_contents is valaddr.
1695 Return -1 if out of range, -2 other error. */
1698 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1700 struct gdbarch
*gdbarch
= get_type_arch (type
);
1701 LONGEST low_bound
, high_bound
;
1704 struct type
*range
= TYPE_INDEX_TYPE (type
);
1706 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1708 if (index
< low_bound
|| index
> high_bound
)
1710 rel_index
= index
- low_bound
;
1711 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1712 gdbarch_byte_order (gdbarch
));
1713 rel_index
%= TARGET_CHAR_BIT
;
1714 if (gdbarch_bits_big_endian (gdbarch
))
1715 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1716 return (word
>> rel_index
) & 1;
1720 value_in (struct value
*element
, struct value
*set
)
1723 struct type
*settype
= check_typedef (value_type (set
));
1724 struct type
*eltype
= check_typedef (value_type (element
));
1726 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1727 eltype
= TYPE_TARGET_TYPE (eltype
);
1728 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1729 error (_("Second argument of 'IN' has wrong type"));
1730 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1731 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1732 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1733 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1734 error (_("First argument of 'IN' has wrong type"));
1735 member
= value_bit_index (settype
, value_contents (set
),
1736 value_as_long (element
));
1738 error (_("First argument of 'IN' not in range"));
1743 _initialize_valarith (void)
This page took 0.120879 seconds and 4 git commands to generate.