1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2020 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 (ptr_type
->code () == TYPE_CODE_PTR
);
52 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
54 sz
= type_length_units (ptr_target
);
57 if (ptr_type
->code () == TYPE_CODE_VOID
)
63 name
= ptr_target
->name ();
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 (type1
->code () == TYPE_CODE_PTR
);
111 gdb_assert (type2
->code () == 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 bool c_style
= current_language
->c_style_arrays_p ();
146 array
= coerce_ref (array
);
147 tarray
= check_typedef (value_type (array
));
149 if (tarray
->code () == TYPE_CODE_ARRAY
150 || tarray
->code () == TYPE_CODE_STRING
)
152 struct type
*range_type
= tarray
->index_type ();
153 gdb::optional
<LONGEST
> lowerbound
= get_discrete_low_bound (range_type
);
154 if (!lowerbound
.has_value ())
157 if (VALUE_LVAL (array
) != lval_memory
)
158 return value_subscripted_rvalue (array
, index
, *lowerbound
);
162 gdb::optional
<LONGEST
> upperbound
163 = get_discrete_high_bound (range_type
);
165 if (!upperbound
.has_value ())
168 if (index
>= *lowerbound
&& index
<= *upperbound
)
169 return value_subscripted_rvalue (array
, index
, *lowerbound
);
171 /* Emit warning unless we have an array of unknown size.
172 An array of unknown size has lowerbound 0 and upperbound -1. */
173 if (*upperbound
> -1)
174 warning (_("array or string index out of range"));
175 /* fall doing C stuff */
179 index
-= *lowerbound
;
180 array
= value_coerce_array (array
);
184 return value_ind (value_ptradd (array
, index
));
186 error (_("not an array or string"));
189 /* Return the value of EXPR[IDX], expr an aggregate rvalue
190 (eg, a vector register). This routine used to promote floats
191 to doubles, but no longer does. */
194 value_subscripted_rvalue (struct value
*array
, LONGEST index
, LONGEST lowerbound
)
196 struct type
*array_type
= check_typedef (value_type (array
));
197 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
198 LONGEST elt_size
= type_length_units (elt_type
);
200 /* Fetch the bit stride and convert it to a byte stride, assuming 8 bits
202 LONGEST stride
= array_type
->bit_stride ();
205 struct gdbarch
*arch
= get_type_arch (elt_type
);
206 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
207 elt_size
= stride
/ (unit_size
* 8);
210 LONGEST elt_offs
= elt_size
* (index
- lowerbound
);
211 bool array_upper_bound_undefined
212 = array_type
->bounds ()->high
.kind () == PROP_UNDEFINED
;
214 if (index
< lowerbound
215 || (!array_upper_bound_undefined
216 && elt_offs
>= type_length_units (array_type
))
217 || (VALUE_LVAL (array
) != lval_memory
&& array_upper_bound_undefined
))
219 if (type_not_associated (array_type
))
220 error (_("no such vector element (vector not associated)"));
221 else if (type_not_allocated (array_type
))
222 error (_("no such vector element (vector not allocated)"));
224 error (_("no such vector element"));
227 if (is_dynamic_type (elt_type
))
231 address
= value_address (array
) + elt_offs
;
232 elt_type
= resolve_dynamic_type (elt_type
, {}, address
);
235 return value_from_component (array
, elt_type
, elt_offs
);
239 /* Check to see if either argument is a structure, or a reference to
240 one. This is called so we know whether to go ahead with the normal
241 binop or look for a user defined function instead.
243 For now, we do not overload the `=' operator. */
246 binop_types_user_defined_p (enum exp_opcode op
,
247 struct type
*type1
, struct type
*type2
)
249 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
252 type1
= check_typedef (type1
);
253 if (TYPE_IS_REFERENCE (type1
))
254 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
256 type2
= check_typedef (type2
);
257 if (TYPE_IS_REFERENCE (type2
))
258 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
260 return (type1
->code () == TYPE_CODE_STRUCT
261 || type2
->code () == TYPE_CODE_STRUCT
);
264 /* Check to see if either argument is a structure, or a reference to
265 one. This is called so we know whether to go ahead with the normal
266 binop or look for a user defined function instead.
268 For now, we do not overload the `=' operator. */
271 binop_user_defined_p (enum exp_opcode op
,
272 struct value
*arg1
, struct value
*arg2
)
274 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
277 /* Check to see if argument is a structure. This is called so
278 we know whether to go ahead with the normal unop or look for a
279 user defined function instead.
281 For now, we do not overload the `&' operator. */
284 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
290 type1
= check_typedef (value_type (arg1
));
291 if (TYPE_IS_REFERENCE (type1
))
292 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
293 return type1
->code () == TYPE_CODE_STRUCT
;
296 /* Try to find an operator named OPERATOR which takes NARGS arguments
297 specified in ARGS. If the operator found is a static member operator
298 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
299 The search if performed through find_overload_match which will handle
300 member operators, non member operators, operators imported implicitly or
301 explicitly, and perform correct overload resolution in all of the above
302 situations or combinations thereof. */
304 static struct value
*
305 value_user_defined_cpp_op (gdb::array_view
<value
*> args
, char *oper
,
306 int *static_memfuncp
, enum noside noside
)
309 struct symbol
*symp
= NULL
;
310 struct value
*valp
= NULL
;
312 find_overload_match (args
, oper
, BOTH
/* could be method */,
314 NULL
/* pass NULL symbol since symbol is unknown */,
315 &valp
, &symp
, static_memfuncp
, 0, noside
);
322 /* This is a non member function and does not
323 expect a reference as its first argument
324 rather the explicit structure. */
325 args
[0] = value_ind (args
[0]);
326 return value_of_variable (symp
, 0);
329 error (_("Could not find %s."), oper
);
332 /* Lookup user defined operator NAME. Return a value representing the
333 function, otherwise return NULL. */
335 static struct value
*
336 value_user_defined_op (struct value
**argp
, gdb::array_view
<value
*> args
,
337 char *name
, int *static_memfuncp
, enum noside noside
)
339 struct value
*result
= NULL
;
341 if (current_language
->la_language
== language_cplus
)
343 result
= value_user_defined_cpp_op (args
, name
, static_memfuncp
,
347 result
= value_struct_elt (argp
, args
.data (), name
, static_memfuncp
,
353 /* We know either arg1 or arg2 is a structure, so try to find the right
354 user defined function. Create an argument vector that calls
355 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
356 binary operator which is legal for GNU C++).
358 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
359 is the opcode saying how to modify it. Otherwise, OTHEROP is
363 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
364 enum exp_opcode otherop
, enum noside noside
)
370 arg1
= coerce_ref (arg1
);
371 arg2
= coerce_ref (arg2
);
373 /* now we know that what we have to do is construct our
374 arg vector and find the right function to call it with. */
376 if (check_typedef (value_type (arg1
))->code () != TYPE_CODE_STRUCT
)
377 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
379 value
*argvec_storage
[3];
380 gdb::array_view
<value
*> argvec
= argvec_storage
;
382 argvec
[1] = value_addr (arg1
);
385 /* Make the right function name up. */
386 strcpy (tstr
, "operator__");
411 case BINOP_BITWISE_AND
:
414 case BINOP_BITWISE_IOR
:
417 case BINOP_BITWISE_XOR
:
420 case BINOP_LOGICAL_AND
:
423 case BINOP_LOGICAL_OR
:
435 case BINOP_ASSIGN_MODIFY
:
453 case BINOP_BITWISE_AND
:
456 case BINOP_BITWISE_IOR
:
459 case BINOP_BITWISE_XOR
:
462 case BINOP_MOD
: /* invalid */
464 error (_("Invalid binary operation specified."));
467 case BINOP_SUBSCRIPT
:
488 case BINOP_MOD
: /* invalid */
490 error (_("Invalid binary operation specified."));
493 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1), tstr
,
494 &static_memfuncp
, noside
);
500 argvec
[1] = argvec
[0];
501 argvec
= argvec
.slice (1);
503 if (value_type (argvec
[0])->code () == TYPE_CODE_XMETHOD
)
505 /* Static xmethods are not supported yet. */
506 gdb_assert (static_memfuncp
== 0);
507 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
509 struct type
*return_type
510 = result_type_of_xmethod (argvec
[0], argvec
.slice (1));
512 if (return_type
== NULL
)
513 error (_("Xmethod is missing return type."));
514 return value_zero (return_type
, VALUE_LVAL (arg1
));
516 return call_xmethod (argvec
[0], argvec
.slice (1));
518 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
520 struct type
*return_type
;
523 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
524 return value_zero (return_type
, VALUE_LVAL (arg1
));
526 return call_function_by_hand (argvec
[0], NULL
,
527 argvec
.slice (1, 2 - static_memfuncp
));
529 throw_error (NOT_FOUND_ERROR
,
530 _("member function %s not found"), tstr
);
533 /* We know that arg1 is a structure, so try to find a unary user
534 defined operator that matches the operator in question.
535 Create an argument vector that calls arg1.operator @ (arg1)
536 and return that value (where '@' is (almost) any unary operator which
537 is legal for GNU C++). */
540 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
542 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
544 char tstr
[13], mangle_tstr
[13];
545 int static_memfuncp
, nargs
;
547 arg1
= coerce_ref (arg1
);
549 /* now we know that what we have to do is construct our
550 arg vector and find the right function to call it with. */
552 if (check_typedef (value_type (arg1
))->code () != TYPE_CODE_STRUCT
)
553 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
555 value
*argvec_storage
[3];
556 gdb::array_view
<value
*> argvec
= argvec_storage
;
558 argvec
[1] = value_addr (arg1
);
563 /* Make the right function name up. */
564 strcpy (tstr
, "operator__");
566 strcpy (mangle_tstr
, "__");
569 case UNOP_PREINCREMENT
:
572 case UNOP_PREDECREMENT
:
575 case UNOP_POSTINCREMENT
:
577 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
580 case UNOP_POSTDECREMENT
:
582 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
585 case UNOP_LOGICAL_NOT
:
588 case UNOP_COMPLEMENT
:
604 error (_("Invalid unary operation specified."));
607 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1, nargs
), tstr
,
608 &static_memfuncp
, noside
);
614 argvec
[1] = argvec
[0];
615 argvec
= argvec
.slice (1);
617 if (value_type (argvec
[0])->code () == TYPE_CODE_XMETHOD
)
619 /* Static xmethods are not supported yet. */
620 gdb_assert (static_memfuncp
== 0);
621 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
623 struct type
*return_type
624 = result_type_of_xmethod (argvec
[0], argvec
[1]);
626 if (return_type
== NULL
)
627 error (_("Xmethod is missing return type."));
628 return value_zero (return_type
, VALUE_LVAL (arg1
));
630 return call_xmethod (argvec
[0], argvec
[1]);
632 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
634 struct type
*return_type
;
637 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
638 return value_zero (return_type
, VALUE_LVAL (arg1
));
640 return call_function_by_hand (argvec
[0], NULL
,
641 argvec
.slice (1, nargs
));
643 throw_error (NOT_FOUND_ERROR
,
644 _("member function %s not found"), tstr
);
648 /* Concatenate two values with the following conditions:
650 (1) Both values must be either bitstring values or character string
651 values and the resulting value consists of the concatenation of
652 ARG1 followed by ARG2.
656 One value must be an integer value and the other value must be
657 either a bitstring value or character string value, which is
658 to be repeated by the number of times specified by the integer
662 (2) Boolean values are also allowed and are treated as bit string
665 (3) Character values are also allowed and are treated as character
666 string values of length 1. */
669 value_concat (struct value
*arg1
, struct value
*arg2
)
671 struct value
*inval1
;
672 struct value
*inval2
;
673 struct value
*outval
= NULL
;
674 int inval1len
, inval2len
;
677 struct type
*type1
= check_typedef (value_type (arg1
));
678 struct type
*type2
= check_typedef (value_type (arg2
));
679 struct type
*char_type
;
681 /* First figure out if we are dealing with two values to be concatenated
682 or a repeat count and a value to be repeated. INVAL1 is set to the
683 first of two concatenated values, or the repeat count. INVAL2 is set
684 to the second of the two concatenated values or the value to be
687 if (type2
->code () == TYPE_CODE_INT
)
689 struct type
*tmp
= type1
;
702 /* Now process the input values. */
704 if (type1
->code () == TYPE_CODE_INT
)
706 /* We have a repeat count. Validate the second value and then
707 construct a value repeated that many times. */
708 if (type2
->code () == TYPE_CODE_STRING
709 || type2
->code () == TYPE_CODE_CHAR
)
711 count
= longest_to_int (value_as_long (inval1
));
712 inval2len
= TYPE_LENGTH (type2
);
713 std::vector
<char> ptr (count
* inval2len
);
714 if (type2
->code () == TYPE_CODE_CHAR
)
718 inchar
= (char) unpack_long (type2
,
719 value_contents (inval2
));
720 for (idx
= 0; idx
< count
; idx
++)
727 char_type
= TYPE_TARGET_TYPE (type2
);
729 for (idx
= 0; idx
< count
; idx
++)
731 memcpy (&ptr
[idx
* inval2len
], value_contents (inval2
),
735 outval
= value_string (ptr
.data (), count
* inval2len
, char_type
);
737 else if (type2
->code () == TYPE_CODE_BOOL
)
739 error (_("unimplemented support for boolean repeats"));
743 error (_("can't repeat values of that type"));
746 else if (type1
->code () == TYPE_CODE_STRING
747 || type1
->code () == TYPE_CODE_CHAR
)
749 /* We have two character strings to concatenate. */
750 if (type2
->code () != TYPE_CODE_STRING
751 && type2
->code () != TYPE_CODE_CHAR
)
753 error (_("Strings can only be concatenated with other strings."));
755 inval1len
= TYPE_LENGTH (type1
);
756 inval2len
= TYPE_LENGTH (type2
);
757 std::vector
<char> ptr (inval1len
+ inval2len
);
758 if (type1
->code () == TYPE_CODE_CHAR
)
762 ptr
[0] = (char) unpack_long (type1
, value_contents (inval1
));
766 char_type
= TYPE_TARGET_TYPE (type1
);
768 memcpy (ptr
.data (), value_contents (inval1
), inval1len
);
770 if (type2
->code () == TYPE_CODE_CHAR
)
773 (char) unpack_long (type2
, value_contents (inval2
));
777 memcpy (&ptr
[inval1len
], value_contents (inval2
), inval2len
);
779 outval
= value_string (ptr
.data (), inval1len
+ inval2len
, char_type
);
781 else if (type1
->code () == TYPE_CODE_BOOL
)
783 /* We have two bitstrings to concatenate. */
784 if (type2
->code () != TYPE_CODE_BOOL
)
786 error (_("Booleans can only be concatenated "
787 "with other bitstrings or booleans."));
789 error (_("unimplemented support for boolean concatenation."));
793 /* We don't know how to concatenate these operands. */
794 error (_("illegal operands for concatenation."));
799 /* Integer exponentiation: V1**V2, where both arguments are
800 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
803 integer_pow (LONGEST v1
, LONGEST v2
)
808 error (_("Attempt to raise 0 to negative power."));
814 /* The Russian Peasant's Algorithm. */
830 /* Obtain argument values for binary operation, converting from
831 other types if one of them is not floating point. */
833 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
834 gdb_byte
*x
, struct type
**eff_type_x
,
835 gdb_byte
*y
, struct type
**eff_type_y
)
837 struct type
*type1
, *type2
;
839 type1
= check_typedef (value_type (arg1
));
840 type2
= check_typedef (value_type (arg2
));
842 /* At least one of the arguments must be of floating-point type. */
843 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
845 if (is_floating_type (type1
) && is_floating_type (type2
)
846 && type1
->code () != type2
->code ())
847 /* The DFP extension to the C language does not allow mixing of
848 * decimal float types with other float types in expressions
849 * (see WDTR 24732, page 12). */
850 error (_("Mixing decimal floating types with "
851 "other floating types is not allowed."));
853 /* Obtain value of arg1, converting from other types if necessary. */
855 if (is_floating_type (type1
))
858 memcpy (x
, value_contents (arg1
), TYPE_LENGTH (type1
));
860 else if (is_integral_type (type1
))
863 if (type1
->is_unsigned ())
864 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
866 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
869 error (_("Don't know how to convert from %s to %s."), type1
->name (),
872 /* Obtain value of arg2, converting from other types if necessary. */
874 if (is_floating_type (type2
))
877 memcpy (y
, value_contents (arg2
), TYPE_LENGTH (type2
));
879 else if (is_integral_type (type2
))
882 if (type2
->is_unsigned ())
883 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
885 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
888 error (_("Don't know how to convert from %s to %s."), type1
->name (),
892 /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
893 perform the binary operation OP on these two operands, and return
894 the resulting value (also as a fixed point). */
896 static struct value
*
897 fixed_point_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
899 struct type
*type1
= check_typedef (value_type (arg1
));
900 struct type
*type2
= check_typedef (value_type (arg2
));
901 const struct language_defn
*language
= current_language
;
903 struct gdbarch
*gdbarch
= get_type_arch (type1
);
906 gdb_assert (is_fixed_point_type (type1
) || is_fixed_point_type (type2
));
907 if (!is_fixed_point_type (type1
))
909 arg1
= value_cast (type2
, arg1
);
912 if (!is_fixed_point_type (type2
))
914 arg2
= value_cast (type1
, arg2
);
919 v1
.read_fixed_point (gdb::make_array_view (value_contents (arg1
),
920 TYPE_LENGTH (type1
)),
921 type_byte_order (type1
), type1
->is_unsigned (),
922 type1
->fixed_point_scaling_factor ());
923 v2
.read_fixed_point (gdb::make_array_view (value_contents (arg2
),
924 TYPE_LENGTH (type2
)),
925 type_byte_order (type2
), type2
->is_unsigned (),
926 type2
->fixed_point_scaling_factor ());
928 auto fixed_point_to_value
= [type1
] (const gdb_mpq
&fp
)
930 value
*fp_val
= allocate_value (type1
);
933 (gdb::make_array_view (value_contents_raw (fp_val
),
934 TYPE_LENGTH (type1
)),
935 type_byte_order (type1
),
936 type1
->is_unsigned (),
937 type1
->fixed_point_scaling_factor ());
945 mpq_add (res
.val
, v1
.val
, v2
.val
);
946 val
= fixed_point_to_value (res
);
950 mpq_sub (res
.val
, v1
.val
, v2
.val
);
951 val
= fixed_point_to_value (res
);
955 val
= fixed_point_to_value (mpq_cmp (v1
.val
, v2
.val
) < 0 ? v1
: v2
);
959 val
= fixed_point_to_value (mpq_cmp (v1
.val
, v2
.val
) > 0 ? v1
: v2
);
963 mpq_mul (res
.val
, v1
.val
, v2
.val
);
964 val
= fixed_point_to_value (res
);
968 mpq_div (res
.val
, v1
.val
, v2
.val
);
969 val
= fixed_point_to_value (res
);
973 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
974 mpq_cmp (v1
.val
, v2
.val
) == 0 ? 1 : 0);
978 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
979 mpq_cmp (v1
.val
, v2
.val
) < 0 ? 1 : 0);
983 error (_("Integer-only operation on fixed point number."));
989 /* A helper function that finds the type to use for a binary operation
990 involving TYPE1 and TYPE2. */
993 promotion_type (struct type
*type1
, struct type
*type2
)
995 struct type
*result_type
;
997 if (is_floating_type (type1
) || is_floating_type (type2
))
999 /* If only one type is floating-point, use its type.
1000 Otherwise use the bigger type. */
1001 if (!is_floating_type (type1
))
1002 result_type
= type2
;
1003 else if (!is_floating_type (type2
))
1004 result_type
= type1
;
1005 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1006 result_type
= type2
;
1008 result_type
= type1
;
1012 /* Integer types. */
1013 if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1014 result_type
= type1
;
1015 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1016 result_type
= type2
;
1017 else if (type1
->is_unsigned ())
1018 result_type
= type1
;
1019 else if (type2
->is_unsigned ())
1020 result_type
= type2
;
1022 result_type
= type1
;
1028 static struct value
*scalar_binop (struct value
*arg1
, struct value
*arg2
,
1029 enum exp_opcode op
);
1031 /* Perform a binary operation on complex operands. */
1033 static struct value
*
1034 complex_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1036 struct type
*arg1_type
= check_typedef (value_type (arg1
));
1037 struct type
*arg2_type
= check_typedef (value_type (arg2
));
1039 struct value
*arg1_real
, *arg1_imag
, *arg2_real
, *arg2_imag
;
1040 if (arg1_type
->code () == TYPE_CODE_COMPLEX
)
1042 arg1_real
= value_real_part (arg1
);
1043 arg1_imag
= value_imaginary_part (arg1
);
1048 arg1_imag
= value_zero (arg1_type
, not_lval
);
1050 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1052 arg2_real
= value_real_part (arg2
);
1053 arg2_imag
= value_imaginary_part (arg2
);
1058 arg2_imag
= value_zero (arg2_type
, not_lval
);
1061 struct type
*comp_type
= promotion_type (value_type (arg1_real
),
1062 value_type (arg2_real
));
1063 arg1_real
= value_cast (comp_type
, arg1_real
);
1064 arg1_imag
= value_cast (comp_type
, arg1_imag
);
1065 arg2_real
= value_cast (comp_type
, arg2_real
);
1066 arg2_imag
= value_cast (comp_type
, arg2_imag
);
1068 struct type
*result_type
= init_complex_type (nullptr, comp_type
);
1070 struct value
*result_real
, *result_imag
;
1075 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1076 result_imag
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1081 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1082 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1083 result_real
= scalar_binop (x1
, x2
, BINOP_SUB
);
1085 x1
= scalar_binop (arg1_real
, arg2_imag
, op
);
1086 x2
= scalar_binop (arg1_imag
, arg2_real
, op
);
1087 result_imag
= scalar_binop (x1
, x2
, BINOP_ADD
);
1093 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1095 struct value
*conjugate
= value_complement (arg2
);
1096 /* We have to reconstruct ARG1, in case the type was
1098 arg1
= value_literal_complex (arg1_real
, arg1_imag
, result_type
);
1100 struct value
*numerator
= scalar_binop (arg1
, conjugate
,
1102 arg1_real
= value_real_part (numerator
);
1103 arg1_imag
= value_imaginary_part (numerator
);
1105 struct value
*x1
= scalar_binop (arg2_real
, arg2_real
, BINOP_MUL
);
1106 struct value
*x2
= scalar_binop (arg2_imag
, arg2_imag
, BINOP_MUL
);
1107 arg2_real
= scalar_binop (x1
, x2
, BINOP_ADD
);
1110 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1111 result_imag
= scalar_binop (arg1_imag
, arg2_real
, op
);
1116 case BINOP_NOTEQUAL
:
1118 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1119 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1121 LONGEST v1
= value_as_long (x1
);
1122 LONGEST v2
= value_as_long (x2
);
1124 if (op
== BINOP_EQUAL
)
1129 return value_from_longest (value_type (x1
), v1
);
1134 error (_("Invalid binary operation on numbers."));
1137 return value_literal_complex (result_real
, result_imag
, result_type
);
1140 /* Perform a binary operation on two operands which have reasonable
1141 representations as integers or floats. This includes booleans,
1142 characters, integers, or floats.
1143 Does not support addition and subtraction on pointers;
1144 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1146 static struct value
*
1147 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1150 struct type
*type1
, *type2
, *result_type
;
1152 arg1
= coerce_ref (arg1
);
1153 arg2
= coerce_ref (arg2
);
1155 type1
= check_typedef (value_type (arg1
));
1156 type2
= check_typedef (value_type (arg2
));
1158 if (type1
->code () == TYPE_CODE_COMPLEX
1159 || type2
->code () == TYPE_CODE_COMPLEX
)
1160 return complex_binop (arg1
, arg2
, op
);
1162 if ((!is_floating_value (arg1
)
1163 && !is_integral_type (type1
)
1164 && !is_fixed_point_type (type1
))
1165 || (!is_floating_value (arg2
)
1166 && !is_integral_type (type2
)
1167 && !is_fixed_point_type (type2
)))
1168 error (_("Argument to arithmetic operation not a number or boolean."));
1170 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
1171 return fixed_point_binop (arg1
, arg2
, op
);
1173 if (is_floating_type (type1
) || is_floating_type (type2
))
1175 result_type
= promotion_type (type1
, type2
);
1176 val
= allocate_value (result_type
);
1178 struct type
*eff_type_v1
, *eff_type_v2
;
1179 gdb::byte_vector v1
, v2
;
1180 v1
.resize (TYPE_LENGTH (result_type
));
1181 v2
.resize (TYPE_LENGTH (result_type
));
1183 value_args_as_target_float (arg1
, arg2
,
1184 v1
.data (), &eff_type_v1
,
1185 v2
.data (), &eff_type_v2
);
1186 target_float_binop (op
, v1
.data (), eff_type_v1
,
1187 v2
.data (), eff_type_v2
,
1188 value_contents_raw (val
), result_type
);
1190 else if (type1
->code () == TYPE_CODE_BOOL
1191 || type2
->code () == TYPE_CODE_BOOL
)
1193 LONGEST v1
, v2
, v
= 0;
1195 v1
= value_as_long (arg1
);
1196 v2
= value_as_long (arg2
);
1200 case BINOP_BITWISE_AND
:
1204 case BINOP_BITWISE_IOR
:
1208 case BINOP_BITWISE_XOR
:
1216 case BINOP_NOTEQUAL
:
1221 error (_("Invalid operation on booleans."));
1224 result_type
= type1
;
1226 val
= allocate_value (result_type
);
1227 store_signed_integer (value_contents_raw (val
),
1228 TYPE_LENGTH (result_type
),
1229 type_byte_order (result_type
),
1233 /* Integral operations here. */
1235 /* Determine type length of the result, and if the operation should
1236 be done unsigned. For exponentiation and shift operators,
1237 use the length and type of the left operand. Otherwise,
1238 use the signedness of the operand with the greater length.
1239 If both operands are of equal length, use unsigned operation
1240 if one of the operands is unsigned. */
1241 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1242 result_type
= type1
;
1244 result_type
= promotion_type (type1
, type2
);
1246 if (result_type
->is_unsigned ())
1248 LONGEST v2_signed
= value_as_long (arg2
);
1249 ULONGEST v1
, v2
, v
= 0;
1251 v1
= (ULONGEST
) value_as_long (arg1
);
1252 v2
= (ULONGEST
) v2_signed
;
1273 error (_("Division by zero"));
1277 v
= uinteger_pow (v1
, v2_signed
);
1284 error (_("Division by zero"));
1288 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1289 v1 mod 0 has a defined value, v1. */
1297 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1310 case BINOP_BITWISE_AND
:
1314 case BINOP_BITWISE_IOR
:
1318 case BINOP_BITWISE_XOR
:
1322 case BINOP_LOGICAL_AND
:
1326 case BINOP_LOGICAL_OR
:
1331 v
= v1
< v2
? v1
: v2
;
1335 v
= v1
> v2
? v1
: v2
;
1342 case BINOP_NOTEQUAL
:
1363 error (_("Invalid binary operation on numbers."));
1366 val
= allocate_value (result_type
);
1367 store_unsigned_integer (value_contents_raw (val
),
1368 TYPE_LENGTH (value_type (val
)),
1369 type_byte_order (result_type
),
1374 LONGEST v1
, v2
, v
= 0;
1376 v1
= value_as_long (arg1
);
1377 v2
= value_as_long (arg2
);
1398 error (_("Division by zero"));
1402 v
= integer_pow (v1
, v2
);
1409 error (_("Division by zero"));
1413 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1414 X mod 0 has a defined value, X. */
1422 /* Compute floor. */
1423 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1439 case BINOP_BITWISE_AND
:
1443 case BINOP_BITWISE_IOR
:
1447 case BINOP_BITWISE_XOR
:
1451 case BINOP_LOGICAL_AND
:
1455 case BINOP_LOGICAL_OR
:
1460 v
= v1
< v2
? v1
: v2
;
1464 v
= v1
> v2
? v1
: v2
;
1471 case BINOP_NOTEQUAL
:
1492 error (_("Invalid binary operation on numbers."));
1495 val
= allocate_value (result_type
);
1496 store_signed_integer (value_contents_raw (val
),
1497 TYPE_LENGTH (value_type (val
)),
1498 type_byte_order (result_type
),
1506 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1507 replicating SCALAR_VALUE for each element of the vector. Only scalar
1508 types that can be cast to the type of one element of the vector are
1509 acceptable. The newly created vector value is returned upon success,
1510 otherwise an error is thrown. */
1513 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1515 /* Widen the scalar to a vector. */
1516 struct type
*eltype
, *scalar_type
;
1517 struct value
*val
, *elval
;
1518 LONGEST low_bound
, high_bound
;
1521 vector_type
= check_typedef (vector_type
);
1523 gdb_assert (vector_type
->code () == TYPE_CODE_ARRAY
1524 && vector_type
->is_vector ());
1526 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1527 error (_("Could not determine the vector bounds"));
1529 eltype
= check_typedef (TYPE_TARGET_TYPE (vector_type
));
1530 elval
= value_cast (eltype
, scalar_value
);
1532 scalar_type
= check_typedef (value_type (scalar_value
));
1534 /* If we reduced the length of the scalar then check we didn't loose any
1536 if (TYPE_LENGTH (eltype
) < TYPE_LENGTH (scalar_type
)
1537 && !value_equal (elval
, scalar_value
))
1538 error (_("conversion of scalar to vector involves truncation"));
1540 val
= allocate_value (vector_type
);
1541 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1542 /* Duplicate the contents of elval into the destination vector. */
1543 memcpy (value_contents_writeable (val
) + (i
* TYPE_LENGTH (eltype
)),
1544 value_contents_all (elval
), TYPE_LENGTH (eltype
));
1549 /* Performs a binary operation on two vector operands by calling scalar_binop
1550 for each pair of vector components. */
1552 static struct value
*
1553 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1555 struct value
*val
, *tmp
, *mark
;
1556 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1557 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1558 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1560 type1
= check_typedef (value_type (val1
));
1561 type2
= check_typedef (value_type (val2
));
1563 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1564 && type1
->is_vector ()) ? 1 : 0;
1565 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1566 && type2
->is_vector ()) ? 1 : 0;
1568 if (!t1_is_vec
|| !t2_is_vec
)
1569 error (_("Vector operations are only supported among vectors"));
1571 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1572 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1573 error (_("Could not determine the vector bounds"));
1575 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1576 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1577 elsize
= TYPE_LENGTH (eltype1
);
1579 if (eltype1
->code () != eltype2
->code ()
1580 || elsize
!= TYPE_LENGTH (eltype2
)
1581 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
1582 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1583 error (_("Cannot perform operation on vectors with different types"));
1585 val
= allocate_value (type1
);
1586 mark
= value_mark ();
1587 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1589 tmp
= value_binop (value_subscript (val1
, i
),
1590 value_subscript (val2
, i
), op
);
1591 memcpy (value_contents_writeable (val
) + i
* elsize
,
1592 value_contents_all (tmp
),
1595 value_free_to_mark (mark
);
1600 /* Perform a binary operation on two operands. */
1603 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1606 struct type
*type1
= check_typedef (value_type (arg1
));
1607 struct type
*type2
= check_typedef (value_type (arg2
));
1608 int t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1609 && type1
->is_vector ());
1610 int t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1611 && type2
->is_vector ());
1613 if (!t1_is_vec
&& !t2_is_vec
)
1614 val
= scalar_binop (arg1
, arg2
, op
);
1615 else if (t1_is_vec
&& t2_is_vec
)
1616 val
= vector_binop (arg1
, arg2
, op
);
1619 /* Widen the scalar operand to a vector. */
1620 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1621 struct type
*t
= t1_is_vec
? type2
: type1
;
1623 if (t
->code () != TYPE_CODE_FLT
1624 && t
->code () != TYPE_CODE_DECFLOAT
1625 && !is_integral_type (t
))
1626 error (_("Argument to operation not a number or boolean."));
1628 /* Replicate the scalar value to make a vector value. */
1629 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1631 val
= vector_binop (arg1
, arg2
, op
);
1637 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1640 value_logical_not (struct value
*arg1
)
1646 arg1
= coerce_array (arg1
);
1647 type1
= check_typedef (value_type (arg1
));
1649 if (is_floating_value (arg1
))
1650 return target_float_is_zero (value_contents (arg1
), type1
);
1652 len
= TYPE_LENGTH (type1
);
1653 p
= value_contents (arg1
);
1664 /* Perform a comparison on two string values (whose content are not
1665 necessarily null terminated) based on their length. */
1668 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1670 int len1
= TYPE_LENGTH (value_type (arg1
));
1671 int len2
= TYPE_LENGTH (value_type (arg2
));
1672 const gdb_byte
*s1
= value_contents (arg1
);
1673 const gdb_byte
*s2
= value_contents (arg2
);
1674 int i
, len
= len1
< len2
? len1
: len2
;
1676 for (i
= 0; i
< len
; i
++)
1680 else if (s1
[i
] > s2
[i
])
1688 else if (len1
> len2
)
1694 /* Simulate the C operator == by returning a 1
1695 iff ARG1 and ARG2 have equal contents. */
1698 value_equal (struct value
*arg1
, struct value
*arg2
)
1703 struct type
*type1
, *type2
;
1704 enum type_code code1
;
1705 enum type_code code2
;
1706 int is_int1
, is_int2
;
1708 arg1
= coerce_array (arg1
);
1709 arg2
= coerce_array (arg2
);
1711 type1
= check_typedef (value_type (arg1
));
1712 type2
= check_typedef (value_type (arg2
));
1713 code1
= type1
->code ();
1714 code2
= type2
->code ();
1715 is_int1
= is_integral_type (type1
);
1716 is_int2
= is_integral_type (type2
);
1718 if (is_int1
&& is_int2
)
1719 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1721 else if ((is_floating_value (arg1
) || is_int1
)
1722 && (is_floating_value (arg2
) || is_int2
))
1724 struct type
*eff_type_v1
, *eff_type_v2
;
1725 gdb::byte_vector v1
, v2
;
1726 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1727 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1729 value_args_as_target_float (arg1
, arg2
,
1730 v1
.data (), &eff_type_v1
,
1731 v2
.data (), &eff_type_v2
);
1733 return target_float_compare (v1
.data (), eff_type_v1
,
1734 v2
.data (), eff_type_v2
) == 0;
1737 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1739 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1740 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1741 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1742 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1744 else if (code1
== code2
1745 && ((len
= (int) TYPE_LENGTH (type1
))
1746 == (int) TYPE_LENGTH (type2
)))
1748 p1
= value_contents (arg1
);
1749 p2
= value_contents (arg2
);
1757 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1759 return value_strcmp (arg1
, arg2
) == 0;
1762 error (_("Invalid type combination in equality test."));
1765 /* Compare values based on their raw contents. Useful for arrays since
1766 value_equal coerces them to pointers, thus comparing just the address
1767 of the array instead of its contents. */
1770 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1772 struct type
*type1
, *type2
;
1774 type1
= check_typedef (value_type (arg1
));
1775 type2
= check_typedef (value_type (arg2
));
1777 return (type1
->code () == type2
->code ()
1778 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1779 && memcmp (value_contents (arg1
), value_contents (arg2
),
1780 TYPE_LENGTH (type1
)) == 0);
1783 /* Simulate the C operator < by returning 1
1784 iff ARG1's contents are less than ARG2's. */
1787 value_less (struct value
*arg1
, struct value
*arg2
)
1789 enum type_code code1
;
1790 enum type_code code2
;
1791 struct type
*type1
, *type2
;
1792 int is_int1
, is_int2
;
1794 arg1
= coerce_array (arg1
);
1795 arg2
= coerce_array (arg2
);
1797 type1
= check_typedef (value_type (arg1
));
1798 type2
= check_typedef (value_type (arg2
));
1799 code1
= type1
->code ();
1800 code2
= type2
->code ();
1801 is_int1
= is_integral_type (type1
);
1802 is_int2
= is_integral_type (type2
);
1804 if ((is_int1
&& is_int2
)
1805 || (is_fixed_point_type (type1
) && is_fixed_point_type (type2
)))
1806 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1808 else if ((is_floating_value (arg1
) || is_int1
)
1809 && (is_floating_value (arg2
) || is_int2
))
1811 struct type
*eff_type_v1
, *eff_type_v2
;
1812 gdb::byte_vector v1
, v2
;
1813 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1814 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1816 value_args_as_target_float (arg1
, arg2
,
1817 v1
.data (), &eff_type_v1
,
1818 v2
.data (), &eff_type_v2
);
1820 return target_float_compare (v1
.data (), eff_type_v1
,
1821 v2
.data (), eff_type_v2
) == -1;
1823 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1824 return value_as_address (arg1
) < value_as_address (arg2
);
1826 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1828 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1829 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1830 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1831 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1832 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1833 return value_strcmp (arg1
, arg2
) < 0;
1836 error (_("Invalid type combination in ordering comparison."));
1841 /* The unary operators +, - and ~. They free the argument ARG1. */
1844 value_pos (struct value
*arg1
)
1848 arg1
= coerce_ref (arg1
);
1849 type
= check_typedef (value_type (arg1
));
1851 if (is_integral_type (type
) || is_floating_value (arg1
)
1852 || (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1853 || type
->code () == TYPE_CODE_COMPLEX
)
1854 return value_from_contents (type
, value_contents (arg1
));
1856 error (_("Argument to positive operation not a number."));
1860 value_neg (struct value
*arg1
)
1864 arg1
= coerce_ref (arg1
);
1865 type
= check_typedef (value_type (arg1
));
1867 if (is_integral_type (type
) || is_floating_type (type
))
1868 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1869 else if (is_fixed_point_type (type
))
1870 return value_binop (value_zero (type
, not_lval
), arg1
, BINOP_SUB
);
1871 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1873 struct value
*tmp
, *val
= allocate_value (type
);
1874 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1876 LONGEST low_bound
, high_bound
;
1878 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1879 error (_("Could not determine the vector bounds"));
1881 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1883 tmp
= value_neg (value_subscript (arg1
, i
));
1884 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1885 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1889 else if (type
->code () == TYPE_CODE_COMPLEX
)
1891 struct value
*real
= value_real_part (arg1
);
1892 struct value
*imag
= value_imaginary_part (arg1
);
1894 real
= value_neg (real
);
1895 imag
= value_neg (imag
);
1896 return value_literal_complex (real
, imag
, type
);
1899 error (_("Argument to negate operation not a number."));
1903 value_complement (struct value
*arg1
)
1908 arg1
= coerce_ref (arg1
);
1909 type
= check_typedef (value_type (arg1
));
1911 if (is_integral_type (type
))
1912 val
= value_from_longest (type
, ~value_as_long (arg1
));
1913 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1916 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1918 LONGEST low_bound
, high_bound
;
1920 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1921 error (_("Could not determine the vector bounds"));
1923 val
= allocate_value (type
);
1924 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1926 tmp
= value_complement (value_subscript (arg1
, i
));
1927 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1928 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1931 else if (type
->code () == TYPE_CODE_COMPLEX
)
1933 /* GCC has an extension that treats ~complex as the complex
1935 struct value
*real
= value_real_part (arg1
);
1936 struct value
*imag
= value_imaginary_part (arg1
);
1938 imag
= value_neg (imag
);
1939 return value_literal_complex (real
, imag
, type
);
1942 error (_("Argument to complement operation not an integer, boolean."));
1947 /* The INDEX'th bit of SET value whose value_type is TYPE,
1948 and whose value_contents is valaddr.
1949 Return -1 if out of range, -2 other error. */
1952 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1954 struct gdbarch
*gdbarch
= get_type_arch (type
);
1955 LONGEST low_bound
, high_bound
;
1958 struct type
*range
= type
->index_type ();
1960 if (!get_discrete_bounds (range
, &low_bound
, &high_bound
))
1962 if (index
< low_bound
|| index
> high_bound
)
1964 rel_index
= index
- low_bound
;
1965 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1966 type_byte_order (type
));
1967 rel_index
%= TARGET_CHAR_BIT
;
1968 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
1969 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1970 return (word
>> rel_index
) & 1;
1974 value_in (struct value
*element
, struct value
*set
)
1977 struct type
*settype
= check_typedef (value_type (set
));
1978 struct type
*eltype
= check_typedef (value_type (element
));
1980 if (eltype
->code () == TYPE_CODE_RANGE
)
1981 eltype
= TYPE_TARGET_TYPE (eltype
);
1982 if (settype
->code () != TYPE_CODE_SET
)
1983 error (_("Second argument of 'IN' has wrong type"));
1984 if (eltype
->code () != TYPE_CODE_INT
1985 && eltype
->code () != TYPE_CODE_CHAR
1986 && eltype
->code () != TYPE_CODE_ENUM
1987 && eltype
->code () != TYPE_CODE_BOOL
)
1988 error (_("First argument of 'IN' has wrong type"));
1989 member
= value_bit_index (settype
, value_contents (set
),
1990 value_as_long (element
));
1992 error (_("First argument of 'IN' not in range"));