1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2019 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 "gdbsupport/byte-vector.h"
32 /* Define whether or not the C operator '/' truncates towards zero for
33 differently signed operands (truncation direction is undefined in C). */
35 #ifndef TRUNCATION_TOWARDS_ZERO
36 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
39 /* Given a pointer, return the size of its target.
40 If the pointer type is void *, then return 1.
41 If the target type is incomplete, then error out.
42 This isn't a general purpose function, but just a
43 helper for value_ptradd. */
46 find_size_for_pointer_math (struct type
*ptr_type
)
49 struct type
*ptr_target
;
51 gdb_assert (TYPE_CODE (ptr_type
) == TYPE_CODE_PTR
);
52 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
54 sz
= type_length_units (ptr_target
);
57 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
63 name
= TYPE_NAME (ptr_target
);
65 error (_("Cannot perform pointer math on incomplete types, "
66 "try casting to a known type, or void *."));
68 error (_("Cannot perform pointer math on incomplete type \"%s\", "
69 "try casting to a known type, or void *."), name
);
75 /* Given a pointer ARG1 and an integral value ARG2, return the
76 result of C-style pointer arithmetic ARG1 + ARG2. */
79 value_ptradd (struct value
*arg1
, LONGEST arg2
)
81 struct type
*valptrtype
;
85 arg1
= coerce_array (arg1
);
86 valptrtype
= check_typedef (value_type (arg1
));
87 sz
= find_size_for_pointer_math (valptrtype
);
89 result
= value_from_pointer (valptrtype
,
90 value_as_address (arg1
) + sz
* arg2
);
91 if (VALUE_LVAL (result
) != lval_internalvar
)
92 set_value_component_location (result
, arg1
);
96 /* Given two compatible pointer values ARG1 and ARG2, return the
97 result of C-style pointer arithmetic ARG1 - ARG2. */
100 value_ptrdiff (struct value
*arg1
, struct value
*arg2
)
102 struct type
*type1
, *type2
;
105 arg1
= coerce_array (arg1
);
106 arg2
= coerce_array (arg2
);
107 type1
= check_typedef (value_type (arg1
));
108 type2
= check_typedef (value_type (arg2
));
110 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_PTR
);
111 gdb_assert (TYPE_CODE (type2
) == TYPE_CODE_PTR
);
113 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
114 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
115 error (_("First argument of `-' is a pointer and "
116 "second argument is neither\n"
117 "an integer nor a pointer of the same type."));
119 sz
= type_length_units (check_typedef (TYPE_TARGET_TYPE (type1
)));
122 warning (_("Type size unknown, assuming 1. "
123 "Try casting to a known type, or void *."));
127 return (value_as_long (arg1
) - value_as_long (arg2
)) / sz
;
130 /* Return the value of ARRAY[IDX].
132 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
133 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
135 See comments in value_coerce_array() for rationale for reason for
136 doing lower bounds adjustment here rather than there.
137 FIXME: Perhaps we should validate that the index is valid and if
138 verbosity is set, warn about invalid indices (but still use them). */
141 value_subscript (struct value
*array
, LONGEST index
)
143 int c_style
= current_language
->c_style_arrays
;
146 array
= coerce_ref (array
);
147 tarray
= check_typedef (value_type (array
));
149 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
150 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
152 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
153 LONGEST lowerbound
, upperbound
;
155 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
156 if (VALUE_LVAL (array
) != lval_memory
)
157 return value_subscripted_rvalue (array
, index
, lowerbound
);
161 if (index
>= lowerbound
&& index
<= upperbound
)
162 return value_subscripted_rvalue (array
, index
, lowerbound
);
163 /* Emit warning unless we have an array of unknown size.
164 An array of unknown size has lowerbound 0 and upperbound -1. */
166 warning (_("array or string index out of range"));
167 /* fall doing C stuff */
172 array
= value_coerce_array (array
);
176 return value_ind (value_ptradd (array
, index
));
178 error (_("not an array or string"));
181 /* Return the value of EXPR[IDX], expr an aggregate rvalue
182 (eg, a vector register). This routine used to promote floats
183 to doubles, but no longer does. */
186 value_subscripted_rvalue (struct value
*array
, LONGEST index
, LONGEST lowerbound
)
188 struct type
*array_type
= check_typedef (value_type (array
));
189 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
190 ULONGEST elt_size
= type_length_units (elt_type
);
191 ULONGEST elt_offs
= elt_size
* (index
- lowerbound
);
193 if (index
< lowerbound
194 || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)
195 && elt_offs
>= type_length_units (array_type
))
196 || (VALUE_LVAL (array
) != lval_memory
197 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)))
199 if (type_not_associated (array_type
))
200 error (_("no such vector element (vector not associated)"));
201 else if (type_not_allocated (array_type
))
202 error (_("no such vector element (vector not allocated)"));
204 error (_("no such vector element"));
207 if (is_dynamic_type (elt_type
))
211 address
= value_address (array
) + elt_offs
;
212 elt_type
= resolve_dynamic_type (elt_type
, NULL
, address
);
215 return value_from_component (array
, elt_type
, elt_offs
);
219 /* Check to see if either argument is a structure, or a reference to
220 one. This is called so we know whether to go ahead with the normal
221 binop or look for a user defined function instead.
223 For now, we do not overload the `=' operator. */
226 binop_types_user_defined_p (enum exp_opcode op
,
227 struct type
*type1
, struct type
*type2
)
229 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
232 type1
= check_typedef (type1
);
233 if (TYPE_IS_REFERENCE (type1
))
234 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
236 type2
= check_typedef (type2
);
237 if (TYPE_IS_REFERENCE (type2
))
238 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
240 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
241 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
244 /* Check to see if either argument is a structure, or a reference to
245 one. This is called so we know whether to go ahead with the normal
246 binop or look for a user defined function instead.
248 For now, we do not overload the `=' operator. */
251 binop_user_defined_p (enum exp_opcode op
,
252 struct value
*arg1
, struct value
*arg2
)
254 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
257 /* Check to see if argument is a structure. This is called so
258 we know whether to go ahead with the normal unop or look for a
259 user defined function instead.
261 For now, we do not overload the `&' operator. */
264 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
270 type1
= check_typedef (value_type (arg1
));
271 if (TYPE_IS_REFERENCE (type1
))
272 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
273 return TYPE_CODE (type1
) == TYPE_CODE_STRUCT
;
276 /* Try to find an operator named OPERATOR which takes NARGS arguments
277 specified in ARGS. If the operator found is a static member operator
278 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
279 The search if performed through find_overload_match which will handle
280 member operators, non member operators, operators imported implicitly or
281 explicitly, and perform correct overload resolution in all of the above
282 situations or combinations thereof. */
284 static struct value
*
285 value_user_defined_cpp_op (gdb::array_view
<value
*> args
, char *oper
,
286 int *static_memfuncp
, enum noside noside
)
289 struct symbol
*symp
= NULL
;
290 struct value
*valp
= NULL
;
292 find_overload_match (args
, oper
, BOTH
/* could be method */,
294 NULL
/* pass NULL symbol since symbol is unknown */,
295 &valp
, &symp
, static_memfuncp
, 0, noside
);
302 /* This is a non member function and does not
303 expect a reference as its first argument
304 rather the explicit structure. */
305 args
[0] = value_ind (args
[0]);
306 return value_of_variable (symp
, 0);
309 error (_("Could not find %s."), oper
);
312 /* Lookup user defined operator NAME. Return a value representing the
313 function, otherwise return NULL. */
315 static struct value
*
316 value_user_defined_op (struct value
**argp
, gdb::array_view
<value
*> args
,
317 char *name
, int *static_memfuncp
, enum noside noside
)
319 struct value
*result
= NULL
;
321 if (current_language
->la_language
== language_cplus
)
323 result
= value_user_defined_cpp_op (args
, name
, static_memfuncp
,
327 result
= value_struct_elt (argp
, args
.data (), name
, static_memfuncp
,
333 /* We know either arg1 or arg2 is a structure, so try to find the right
334 user defined function. Create an argument vector that calls
335 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
336 binary operator which is legal for GNU C++).
338 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
339 is the opcode saying how to modify it. Otherwise, OTHEROP is
343 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
344 enum exp_opcode otherop
, enum noside noside
)
350 arg1
= coerce_ref (arg1
);
351 arg2
= coerce_ref (arg2
);
353 /* now we know that what we have to do is construct our
354 arg vector and find the right function to call it with. */
356 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
357 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
359 value
*argvec_storage
[3];
360 gdb::array_view
<value
*> argvec
= argvec_storage
;
362 argvec
[1] = value_addr (arg1
);
365 /* Make the right function name up. */
366 strcpy (tstr
, "operator__");
391 case BINOP_BITWISE_AND
:
394 case BINOP_BITWISE_IOR
:
397 case BINOP_BITWISE_XOR
:
400 case BINOP_LOGICAL_AND
:
403 case BINOP_LOGICAL_OR
:
415 case BINOP_ASSIGN_MODIFY
:
433 case BINOP_BITWISE_AND
:
436 case BINOP_BITWISE_IOR
:
439 case BINOP_BITWISE_XOR
:
442 case BINOP_MOD
: /* invalid */
444 error (_("Invalid binary operation specified."));
447 case BINOP_SUBSCRIPT
:
468 case BINOP_MOD
: /* invalid */
470 error (_("Invalid binary operation specified."));
473 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1), tstr
,
474 &static_memfuncp
, noside
);
480 argvec
[1] = argvec
[0];
481 argvec
= argvec
.slice (1);
483 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
485 /* Static xmethods are not supported yet. */
486 gdb_assert (static_memfuncp
== 0);
487 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
489 struct type
*return_type
490 = result_type_of_xmethod (argvec
[0], argvec
.slice (1));
492 if (return_type
== NULL
)
493 error (_("Xmethod is missing return type."));
494 return value_zero (return_type
, VALUE_LVAL (arg1
));
496 return call_xmethod (argvec
[0], argvec
.slice (1));
498 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
500 struct type
*return_type
;
503 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
504 return value_zero (return_type
, VALUE_LVAL (arg1
));
506 return call_function_by_hand (argvec
[0], NULL
,
507 argvec
.slice (1, 2 - static_memfuncp
));
509 throw_error (NOT_FOUND_ERROR
,
510 _("member function %s not found"), tstr
);
513 /* We know that arg1 is a structure, so try to find a unary user
514 defined operator that matches the operator in question.
515 Create an argument vector that calls arg1.operator @ (arg1)
516 and return that value (where '@' is (almost) any unary operator which
517 is legal for GNU C++). */
520 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
522 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
524 char tstr
[13], mangle_tstr
[13];
525 int static_memfuncp
, nargs
;
527 arg1
= coerce_ref (arg1
);
529 /* now we know that what we have to do is construct our
530 arg vector and find the right function to call it with. */
532 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
533 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
535 value
*argvec_storage
[3];
536 gdb::array_view
<value
*> argvec
= argvec_storage
;
538 argvec
[1] = value_addr (arg1
);
543 /* Make the right function name up. */
544 strcpy (tstr
, "operator__");
546 strcpy (mangle_tstr
, "__");
549 case UNOP_PREINCREMENT
:
552 case UNOP_PREDECREMENT
:
555 case UNOP_POSTINCREMENT
:
557 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
560 case UNOP_POSTDECREMENT
:
562 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
565 case UNOP_LOGICAL_NOT
:
568 case UNOP_COMPLEMENT
:
584 error (_("Invalid unary operation specified."));
587 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1, nargs
), tstr
,
588 &static_memfuncp
, noside
);
594 argvec
[1] = argvec
[0];
595 argvec
= argvec
.slice (1);
597 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
599 /* Static xmethods are not supported yet. */
600 gdb_assert (static_memfuncp
== 0);
601 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
603 struct type
*return_type
604 = result_type_of_xmethod (argvec
[0], argvec
[1]);
606 if (return_type
== NULL
)
607 error (_("Xmethod is missing return type."));
608 return value_zero (return_type
, VALUE_LVAL (arg1
));
610 return call_xmethod (argvec
[0], argvec
[1]);
612 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
614 struct type
*return_type
;
617 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
618 return value_zero (return_type
, VALUE_LVAL (arg1
));
620 return call_function_by_hand (argvec
[0], NULL
,
621 argvec
.slice (1, nargs
));
623 throw_error (NOT_FOUND_ERROR
,
624 _("member function %s not found"), tstr
);
628 /* Concatenate two values with the following conditions:
630 (1) Both values must be either bitstring values or character string
631 values and the resulting value consists of the concatenation of
632 ARG1 followed by ARG2.
636 One value must be an integer value and the other value must be
637 either a bitstring value or character string value, which is
638 to be repeated by the number of times specified by the integer
642 (2) Boolean values are also allowed and are treated as bit string
645 (3) Character values are also allowed and are treated as character
646 string values of length 1. */
649 value_concat (struct value
*arg1
, struct value
*arg2
)
651 struct value
*inval1
;
652 struct value
*inval2
;
653 struct value
*outval
= NULL
;
654 int inval1len
, inval2len
;
657 struct type
*type1
= check_typedef (value_type (arg1
));
658 struct type
*type2
= check_typedef (value_type (arg2
));
659 struct type
*char_type
;
661 /* First figure out if we are dealing with two values to be concatenated
662 or a repeat count and a value to be repeated. INVAL1 is set to the
663 first of two concatenated values, or the repeat count. INVAL2 is set
664 to the second of the two concatenated values or the value to be
667 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
669 struct type
*tmp
= type1
;
682 /* Now process the input values. */
684 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
686 /* We have a repeat count. Validate the second value and then
687 construct a value repeated that many times. */
688 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
689 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
691 count
= longest_to_int (value_as_long (inval1
));
692 inval2len
= TYPE_LENGTH (type2
);
693 std::vector
<char> ptr (count
* inval2len
);
694 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
698 inchar
= (char) unpack_long (type2
,
699 value_contents (inval2
));
700 for (idx
= 0; idx
< count
; idx
++)
707 char_type
= TYPE_TARGET_TYPE (type2
);
709 for (idx
= 0; idx
< count
; idx
++)
711 memcpy (&ptr
[idx
* inval2len
], value_contents (inval2
),
715 outval
= value_string (ptr
.data (), count
* inval2len
, char_type
);
717 else if (TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
719 error (_("unimplemented support for boolean repeats"));
723 error (_("can't repeat values of that type"));
726 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
727 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
729 /* We have two character strings to concatenate. */
730 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
731 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
733 error (_("Strings can only be concatenated with other strings."));
735 inval1len
= TYPE_LENGTH (type1
);
736 inval2len
= TYPE_LENGTH (type2
);
737 std::vector
<char> ptr (inval1len
+ inval2len
);
738 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
742 ptr
[0] = (char) unpack_long (type1
, value_contents (inval1
));
746 char_type
= TYPE_TARGET_TYPE (type1
);
748 memcpy (ptr
.data (), value_contents (inval1
), inval1len
);
750 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
753 (char) unpack_long (type2
, value_contents (inval2
));
757 memcpy (&ptr
[inval1len
], value_contents (inval2
), inval2len
);
759 outval
= value_string (ptr
.data (), inval1len
+ inval2len
, char_type
);
761 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
763 /* We have two bitstrings to concatenate. */
764 if (TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
766 error (_("Booleans can only be concatenated "
767 "with other bitstrings or booleans."));
769 error (_("unimplemented support for boolean concatenation."));
773 /* We don't know how to concatenate these operands. */
774 error (_("illegal operands for concatenation."));
779 /* Integer exponentiation: V1**V2, where both arguments are
780 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
783 integer_pow (LONGEST v1
, LONGEST v2
)
788 error (_("Attempt to raise 0 to negative power."));
794 /* The Russian Peasant's Algorithm. */
810 /* Integer exponentiation: V1**V2, where both arguments are
811 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
814 uinteger_pow (ULONGEST v1
, LONGEST v2
)
819 error (_("Attempt to raise 0 to negative power."));
825 /* The Russian Peasant's Algorithm. */
841 /* Obtain argument values for binary operation, converting from
842 other types if one of them is not floating point. */
844 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
845 gdb_byte
*x
, struct type
**eff_type_x
,
846 gdb_byte
*y
, struct type
**eff_type_y
)
848 struct type
*type1
, *type2
;
850 type1
= check_typedef (value_type (arg1
));
851 type2
= check_typedef (value_type (arg2
));
853 /* At least one of the arguments must be of floating-point type. */
854 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
856 if (is_floating_type (type1
) && is_floating_type (type2
)
857 && TYPE_CODE (type1
) != TYPE_CODE (type2
))
858 /* The DFP extension to the C language does not allow mixing of
859 * decimal float types with other float types in expressions
860 * (see WDTR 24732, page 12). */
861 error (_("Mixing decimal floating types with "
862 "other floating types is not allowed."));
864 /* Obtain value of arg1, converting from other types if necessary. */
866 if (is_floating_type (type1
))
869 memcpy (x
, value_contents (arg1
), TYPE_LENGTH (type1
));
871 else if (is_integral_type (type1
))
874 if (TYPE_UNSIGNED (type1
))
875 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
877 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
880 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
883 /* Obtain value of arg2, converting from other types if necessary. */
885 if (is_floating_type (type2
))
888 memcpy (y
, value_contents (arg2
), TYPE_LENGTH (type2
));
890 else if (is_integral_type (type2
))
893 if (TYPE_UNSIGNED (type2
))
894 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
896 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
899 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
903 /* Perform a binary operation on two operands which have reasonable
904 representations as integers or floats. This includes booleans,
905 characters, integers, or floats.
906 Does not support addition and subtraction on pointers;
907 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
909 static struct value
*
910 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
913 struct type
*type1
, *type2
, *result_type
;
915 arg1
= coerce_ref (arg1
);
916 arg2
= coerce_ref (arg2
);
918 type1
= check_typedef (value_type (arg1
));
919 type2
= check_typedef (value_type (arg2
));
921 if ((!is_floating_value (arg1
) && !is_integral_type (type1
))
922 || (!is_floating_value (arg2
) && !is_integral_type (type2
)))
923 error (_("Argument to arithmetic operation not a number or boolean."));
925 if (is_floating_type (type1
) || is_floating_type (type2
))
927 /* If only one type is floating-point, use its type.
928 Otherwise use the bigger type. */
929 if (!is_floating_type (type1
))
931 else if (!is_floating_type (type2
))
933 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
938 val
= allocate_value (result_type
);
940 struct type
*eff_type_v1
, *eff_type_v2
;
941 gdb::byte_vector v1
, v2
;
942 v1
.resize (TYPE_LENGTH (result_type
));
943 v2
.resize (TYPE_LENGTH (result_type
));
945 value_args_as_target_float (arg1
, arg2
,
946 v1
.data (), &eff_type_v1
,
947 v2
.data (), &eff_type_v2
);
948 target_float_binop (op
, v1
.data (), eff_type_v1
,
949 v2
.data (), eff_type_v2
,
950 value_contents_raw (val
), result_type
);
952 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
953 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
955 LONGEST v1
, v2
, v
= 0;
957 v1
= value_as_long (arg1
);
958 v2
= value_as_long (arg2
);
962 case BINOP_BITWISE_AND
:
966 case BINOP_BITWISE_IOR
:
970 case BINOP_BITWISE_XOR
:
983 error (_("Invalid operation on booleans."));
988 val
= allocate_value (result_type
);
989 store_signed_integer (value_contents_raw (val
),
990 TYPE_LENGTH (result_type
),
991 type_byte_order (result_type
),
995 /* Integral operations here. */
997 /* Determine type length of the result, and if the operation should
998 be done unsigned. For exponentiation and shift operators,
999 use the length and type of the left operand. Otherwise,
1000 use the signedness of the operand with the greater length.
1001 If both operands are of equal length, use unsigned operation
1002 if one of the operands is unsigned. */
1003 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1004 result_type
= type1
;
1005 else if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1006 result_type
= type1
;
1007 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1008 result_type
= type2
;
1009 else if (TYPE_UNSIGNED (type1
))
1010 result_type
= type1
;
1011 else if (TYPE_UNSIGNED (type2
))
1012 result_type
= type2
;
1014 result_type
= type1
;
1016 if (TYPE_UNSIGNED (result_type
))
1018 LONGEST v2_signed
= value_as_long (arg2
);
1019 ULONGEST v1
, v2
, v
= 0;
1021 v1
= (ULONGEST
) value_as_long (arg1
);
1022 v2
= (ULONGEST
) v2_signed
;
1043 error (_("Division by zero"));
1047 v
= uinteger_pow (v1
, v2_signed
);
1054 error (_("Division by zero"));
1058 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1059 v1 mod 0 has a defined value, v1. */
1067 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1080 case BINOP_BITWISE_AND
:
1084 case BINOP_BITWISE_IOR
:
1088 case BINOP_BITWISE_XOR
:
1092 case BINOP_LOGICAL_AND
:
1096 case BINOP_LOGICAL_OR
:
1101 v
= v1
< v2
? v1
: v2
;
1105 v
= v1
> v2
? v1
: v2
;
1112 case BINOP_NOTEQUAL
:
1133 error (_("Invalid binary operation on numbers."));
1136 val
= allocate_value (result_type
);
1137 store_unsigned_integer (value_contents_raw (val
),
1138 TYPE_LENGTH (value_type (val
)),
1139 type_byte_order (result_type
),
1144 LONGEST v1
, v2
, v
= 0;
1146 v1
= value_as_long (arg1
);
1147 v2
= value_as_long (arg2
);
1168 error (_("Division by zero"));
1172 v
= integer_pow (v1
, v2
);
1179 error (_("Division by zero"));
1183 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1184 X mod 0 has a defined value, X. */
1192 /* Compute floor. */
1193 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1209 case BINOP_BITWISE_AND
:
1213 case BINOP_BITWISE_IOR
:
1217 case BINOP_BITWISE_XOR
:
1221 case BINOP_LOGICAL_AND
:
1225 case BINOP_LOGICAL_OR
:
1230 v
= v1
< v2
? v1
: v2
;
1234 v
= v1
> v2
? v1
: v2
;
1241 case BINOP_NOTEQUAL
:
1262 error (_("Invalid binary operation on numbers."));
1265 val
= allocate_value (result_type
);
1266 store_signed_integer (value_contents_raw (val
),
1267 TYPE_LENGTH (value_type (val
)),
1268 type_byte_order (result_type
),
1276 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1277 replicating SCALAR_VALUE for each element of the vector. Only scalar
1278 types that can be cast to the type of one element of the vector are
1279 acceptable. The newly created vector value is returned upon success,
1280 otherwise an error is thrown. */
1283 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1285 /* Widen the scalar to a vector. */
1286 struct type
*eltype
, *scalar_type
;
1287 struct value
*val
, *elval
;
1288 LONGEST low_bound
, high_bound
;
1291 vector_type
= check_typedef (vector_type
);
1293 gdb_assert (TYPE_CODE (vector_type
) == TYPE_CODE_ARRAY
1294 && TYPE_VECTOR (vector_type
));
1296 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1297 error (_("Could not determine the vector bounds"));
1299 eltype
= check_typedef (TYPE_TARGET_TYPE (vector_type
));
1300 elval
= value_cast (eltype
, scalar_value
);
1302 scalar_type
= check_typedef (value_type (scalar_value
));
1304 /* If we reduced the length of the scalar then check we didn't loose any
1306 if (TYPE_LENGTH (eltype
) < TYPE_LENGTH (scalar_type
)
1307 && !value_equal (elval
, scalar_value
))
1308 error (_("conversion of scalar to vector involves truncation"));
1310 val
= allocate_value (vector_type
);
1311 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1312 /* Duplicate the contents of elval into the destination vector. */
1313 memcpy (value_contents_writeable (val
) + (i
* TYPE_LENGTH (eltype
)),
1314 value_contents_all (elval
), TYPE_LENGTH (eltype
));
1319 /* Performs a binary operation on two vector operands by calling scalar_binop
1320 for each pair of vector components. */
1322 static struct value
*
1323 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1325 struct value
*val
, *tmp
, *mark
;
1326 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1327 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1328 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1330 type1
= check_typedef (value_type (val1
));
1331 type2
= check_typedef (value_type (val2
));
1333 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1334 && TYPE_VECTOR (type1
)) ? 1 : 0;
1335 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1336 && TYPE_VECTOR (type2
)) ? 1 : 0;
1338 if (!t1_is_vec
|| !t2_is_vec
)
1339 error (_("Vector operations are only supported among vectors"));
1341 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1342 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1343 error (_("Could not determine the vector bounds"));
1345 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1346 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1347 elsize
= TYPE_LENGTH (eltype1
);
1349 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
1350 || elsize
!= TYPE_LENGTH (eltype2
)
1351 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
1352 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1353 error (_("Cannot perform operation on vectors with different types"));
1355 val
= allocate_value (type1
);
1356 mark
= value_mark ();
1357 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1359 tmp
= value_binop (value_subscript (val1
, i
),
1360 value_subscript (val2
, i
), op
);
1361 memcpy (value_contents_writeable (val
) + i
* elsize
,
1362 value_contents_all (tmp
),
1365 value_free_to_mark (mark
);
1370 /* Perform a binary operation on two operands. */
1373 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1376 struct type
*type1
= check_typedef (value_type (arg1
));
1377 struct type
*type2
= check_typedef (value_type (arg2
));
1378 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1379 && TYPE_VECTOR (type1
));
1380 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1381 && TYPE_VECTOR (type2
));
1383 if (!t1_is_vec
&& !t2_is_vec
)
1384 val
= scalar_binop (arg1
, arg2
, op
);
1385 else if (t1_is_vec
&& t2_is_vec
)
1386 val
= vector_binop (arg1
, arg2
, op
);
1389 /* Widen the scalar operand to a vector. */
1390 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1391 struct type
*t
= t1_is_vec
? type2
: type1
;
1393 if (TYPE_CODE (t
) != TYPE_CODE_FLT
1394 && TYPE_CODE (t
) != TYPE_CODE_DECFLOAT
1395 && !is_integral_type (t
))
1396 error (_("Argument to operation not a number or boolean."));
1398 /* Replicate the scalar value to make a vector value. */
1399 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1401 val
= vector_binop (arg1
, arg2
, op
);
1407 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1410 value_logical_not (struct value
*arg1
)
1416 arg1
= coerce_array (arg1
);
1417 type1
= check_typedef (value_type (arg1
));
1419 if (is_floating_value (arg1
))
1420 return target_float_is_zero (value_contents (arg1
), type1
);
1422 len
= TYPE_LENGTH (type1
);
1423 p
= value_contents (arg1
);
1434 /* Perform a comparison on two string values (whose content are not
1435 necessarily null terminated) based on their length. */
1438 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1440 int len1
= TYPE_LENGTH (value_type (arg1
));
1441 int len2
= TYPE_LENGTH (value_type (arg2
));
1442 const gdb_byte
*s1
= value_contents (arg1
);
1443 const gdb_byte
*s2
= value_contents (arg2
);
1444 int i
, len
= len1
< len2
? len1
: len2
;
1446 for (i
= 0; i
< len
; i
++)
1450 else if (s1
[i
] > s2
[i
])
1458 else if (len1
> len2
)
1464 /* Simulate the C operator == by returning a 1
1465 iff ARG1 and ARG2 have equal contents. */
1468 value_equal (struct value
*arg1
, struct value
*arg2
)
1473 struct type
*type1
, *type2
;
1474 enum type_code code1
;
1475 enum type_code code2
;
1476 int is_int1
, is_int2
;
1478 arg1
= coerce_array (arg1
);
1479 arg2
= coerce_array (arg2
);
1481 type1
= check_typedef (value_type (arg1
));
1482 type2
= check_typedef (value_type (arg2
));
1483 code1
= TYPE_CODE (type1
);
1484 code2
= TYPE_CODE (type2
);
1485 is_int1
= is_integral_type (type1
);
1486 is_int2
= is_integral_type (type2
);
1488 if (is_int1
&& is_int2
)
1489 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1491 else if ((is_floating_value (arg1
) || is_int1
)
1492 && (is_floating_value (arg2
) || is_int2
))
1494 struct type
*eff_type_v1
, *eff_type_v2
;
1495 gdb::byte_vector v1
, v2
;
1496 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1497 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1499 value_args_as_target_float (arg1
, arg2
,
1500 v1
.data (), &eff_type_v1
,
1501 v2
.data (), &eff_type_v2
);
1503 return target_float_compare (v1
.data (), eff_type_v1
,
1504 v2
.data (), eff_type_v2
) == 0;
1507 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1509 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1510 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1511 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1512 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1514 else if (code1
== code2
1515 && ((len
= (int) TYPE_LENGTH (type1
))
1516 == (int) TYPE_LENGTH (type2
)))
1518 p1
= value_contents (arg1
);
1519 p2
= value_contents (arg2
);
1527 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1529 return value_strcmp (arg1
, arg2
) == 0;
1532 error (_("Invalid type combination in equality test."));
1535 /* Compare values based on their raw contents. Useful for arrays since
1536 value_equal coerces them to pointers, thus comparing just the address
1537 of the array instead of its contents. */
1540 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1542 struct type
*type1
, *type2
;
1544 type1
= check_typedef (value_type (arg1
));
1545 type2
= check_typedef (value_type (arg2
));
1547 return (TYPE_CODE (type1
) == TYPE_CODE (type2
)
1548 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1549 && memcmp (value_contents (arg1
), value_contents (arg2
),
1550 TYPE_LENGTH (type1
)) == 0);
1553 /* Simulate the C operator < by returning 1
1554 iff ARG1's contents are less than ARG2's. */
1557 value_less (struct value
*arg1
, struct value
*arg2
)
1559 enum type_code code1
;
1560 enum type_code code2
;
1561 struct type
*type1
, *type2
;
1562 int is_int1
, is_int2
;
1564 arg1
= coerce_array (arg1
);
1565 arg2
= coerce_array (arg2
);
1567 type1
= check_typedef (value_type (arg1
));
1568 type2
= check_typedef (value_type (arg2
));
1569 code1
= TYPE_CODE (type1
);
1570 code2
= TYPE_CODE (type2
);
1571 is_int1
= is_integral_type (type1
);
1572 is_int2
= is_integral_type (type2
);
1574 if (is_int1
&& is_int2
)
1575 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1577 else if ((is_floating_value (arg1
) || is_int1
)
1578 && (is_floating_value (arg2
) || is_int2
))
1580 struct type
*eff_type_v1
, *eff_type_v2
;
1581 gdb::byte_vector v1
, v2
;
1582 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1583 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1585 value_args_as_target_float (arg1
, arg2
,
1586 v1
.data (), &eff_type_v1
,
1587 v2
.data (), &eff_type_v2
);
1589 return target_float_compare (v1
.data (), eff_type_v1
,
1590 v2
.data (), eff_type_v2
) == -1;
1592 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1593 return value_as_address (arg1
) < value_as_address (arg2
);
1595 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1597 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1598 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1599 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1600 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1601 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1602 return value_strcmp (arg1
, arg2
) < 0;
1605 error (_("Invalid type combination in ordering comparison."));
1610 /* The unary operators +, - and ~. They free the argument ARG1. */
1613 value_pos (struct value
*arg1
)
1617 arg1
= coerce_ref (arg1
);
1618 type
= check_typedef (value_type (arg1
));
1620 if (is_integral_type (type
) || is_floating_value (arg1
)
1621 || (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
)))
1622 return value_from_contents (type
, value_contents (arg1
));
1624 error (_("Argument to positive operation not a number."));
1628 value_neg (struct value
*arg1
)
1632 arg1
= coerce_ref (arg1
);
1633 type
= check_typedef (value_type (arg1
));
1635 if (is_integral_type (type
) || is_floating_type (type
))
1636 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1637 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1639 struct value
*tmp
, *val
= allocate_value (type
);
1640 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1642 LONGEST low_bound
, high_bound
;
1644 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1645 error (_("Could not determine the vector bounds"));
1647 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1649 tmp
= value_neg (value_subscript (arg1
, i
));
1650 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1651 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1656 error (_("Argument to negate operation not a number."));
1660 value_complement (struct value
*arg1
)
1665 arg1
= coerce_ref (arg1
);
1666 type
= check_typedef (value_type (arg1
));
1668 if (is_integral_type (type
))
1669 val
= value_from_longest (type
, ~value_as_long (arg1
));
1670 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1673 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1675 LONGEST low_bound
, high_bound
;
1677 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1678 error (_("Could not determine the vector bounds"));
1680 val
= allocate_value (type
);
1681 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1683 tmp
= value_complement (value_subscript (arg1
, i
));
1684 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1685 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1689 error (_("Argument to complement operation not an integer, boolean."));
1694 /* The INDEX'th bit of SET value whose value_type is TYPE,
1695 and whose value_contents is valaddr.
1696 Return -1 if out of range, -2 other error. */
1699 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1701 struct gdbarch
*gdbarch
= get_type_arch (type
);
1702 LONGEST low_bound
, high_bound
;
1705 struct type
*range
= TYPE_INDEX_TYPE (type
);
1707 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1709 if (index
< low_bound
|| index
> high_bound
)
1711 rel_index
= index
- low_bound
;
1712 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1713 type_byte_order (type
));
1714 rel_index
%= TARGET_CHAR_BIT
;
1715 if (gdbarch_bits_big_endian (gdbarch
))
1716 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1717 return (word
>> rel_index
) & 1;
1721 value_in (struct value
*element
, struct value
*set
)
1724 struct type
*settype
= check_typedef (value_type (set
));
1725 struct type
*eltype
= check_typedef (value_type (element
));
1727 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1728 eltype
= TYPE_TARGET_TYPE (eltype
);
1729 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1730 error (_("Second argument of 'IN' has wrong type"));
1731 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1732 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1733 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1734 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1735 error (_("First argument of 'IN' has wrong type"));
1736 member
= value_bit_index (settype
, value_contents (set
),
1737 value_as_long (element
));
1739 error (_("First argument of 'IN' not in range"));
This page took 0.067922 seconds and 4 git commands to generate.