1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2024 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/>. */
20 #include "extract-store-integer.h"
24 #include "expression.h"
27 #include "target-float.h"
29 #include "gdbsupport/byte-vector.h"
31 #include "rust-lang.h"
34 /* Forward declarations. */
35 static struct value
*value_subscripted_rvalue (struct value
*array
,
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 (ptr_type
->target_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 (arg1
->type ());
87 sz
= find_size_for_pointer_math (valptrtype
);
89 result
= value_from_pointer (valptrtype
,
90 value_as_address (arg1
) + sz
* arg2
);
91 if (arg1
->lval () != lval_internalvar
)
92 result
->set_component_location (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 (arg1
->type ());
108 type2
= check_typedef (arg2
->type ());
110 gdb_assert (type1
->code () == TYPE_CODE_PTR
);
111 gdb_assert (type2
->code () == TYPE_CODE_PTR
);
113 if (check_typedef (type1
->target_type ())->length ()
114 != check_typedef (type2
->target_type ())->length ())
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 (type1
->target_type ()));
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 (array
->type ());
149 if (tarray
->code () == TYPE_CODE_ARRAY
150 || tarray
->code () == TYPE_CODE_STRING
)
152 struct type
*range_type
= tarray
->index_type ();
153 std::optional
<LONGEST
> lowerbound
= get_discrete_low_bound (range_type
);
154 if (!lowerbound
.has_value ())
157 if (array
->lval () != lval_memory
)
158 return value_subscripted_rvalue (array
, index
, *lowerbound
);
160 std::optional
<LONGEST
> upperbound
161 = get_discrete_high_bound (range_type
);
163 if (!upperbound
.has_value ())
166 if (index
>= *lowerbound
&& index
<= *upperbound
)
167 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
;
181 /* Do not try to dereference a pointer to an unavailable value.
182 Instead mock up a new one and give it the original address. */
183 struct type
*elt_type
= check_typedef (tarray
->target_type ());
184 LONGEST elt_size
= type_length_units (elt_type
);
186 && !array
->bytes_available (elt_size
* index
, elt_size
))
188 struct value
*val
= value::allocate (elt_type
);
189 val
->mark_bytes_unavailable (0, elt_size
);
190 val
->set_lval (lval_memory
);
191 val
->set_address (array
->address () + elt_size
* index
);
195 array
= value_coerce_array (array
);
199 return value_ind (value_ptradd (array
, index
));
201 error (_("not an array or string"));
204 /* Return the value of EXPR[IDX], expr an aggregate rvalue
205 (eg, a vector register). This routine used to promote floats
206 to doubles, but no longer does. */
208 static struct value
*
209 value_subscripted_rvalue (struct value
*array
, LONGEST index
,
212 struct type
*array_type
= check_typedef (array
->type ());
213 struct type
*elt_type
= array_type
->target_type ();
214 LONGEST elt_size
= type_length_units (elt_type
);
216 /* Fetch the bit stride and convert it to a byte stride, assuming 8 bits
218 LONGEST stride
= array_type
->bit_stride ();
221 struct gdbarch
*arch
= elt_type
->arch ();
222 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
223 elt_size
= stride
/ (unit_size
* 8);
226 LONGEST elt_offs
= elt_size
* (index
- lowerbound
);
227 bool array_upper_bound_undefined
228 = array_type
->bounds ()->high
.kind () == PROP_UNDEFINED
;
230 if (index
< lowerbound
231 || (!array_upper_bound_undefined
232 && elt_offs
>= type_length_units (array_type
))
233 || (array
->lval () != lval_memory
&& array_upper_bound_undefined
))
235 if (type_not_associated (array_type
))
236 error (_("no such vector element (vector not associated)"));
237 else if (type_not_allocated (array_type
))
238 error (_("no such vector element (vector not allocated)"));
240 error (_("no such vector element"));
243 if (is_dynamic_type (elt_type
))
247 address
= array
->address () + elt_offs
;
248 elt_type
= resolve_dynamic_type (elt_type
, {}, address
);
251 return value_from_component (array
, elt_type
, elt_offs
);
257 value_to_array (struct value
*val
)
259 struct type
*type
= check_typedef (val
->type ());
260 if (type
->code () == TYPE_CODE_ARRAY
)
263 if (type
->is_array_like ())
265 const language_defn
*defn
= language_def (type
->language ());
266 return defn
->to_array (val
);
272 /* Check to see if either argument is a structure, or a reference to
273 one. This is called so we know whether to go ahead with the normal
274 binop or look for a user defined function instead.
276 For now, we do not overload the `=' operator. */
279 binop_types_user_defined_p (enum exp_opcode op
,
280 struct type
*type1
, struct type
*type2
)
282 if (op
== BINOP_ASSIGN
)
285 type1
= check_typedef (type1
);
286 if (TYPE_IS_REFERENCE (type1
))
287 type1
= check_typedef (type1
->target_type ());
289 type2
= check_typedef (type2
);
290 if (TYPE_IS_REFERENCE (type2
))
291 type2
= check_typedef (type2
->target_type ());
293 return (type1
->code () == TYPE_CODE_STRUCT
294 || type2
->code () == TYPE_CODE_STRUCT
);
297 /* Check to see if either argument is a structure, or a reference to
298 one. This is called so we know whether to go ahead with the normal
299 binop or look for a user defined function instead.
301 For now, we do not overload the `=' operator. */
304 binop_user_defined_p (enum exp_opcode op
,
305 struct value
*arg1
, struct value
*arg2
)
307 return binop_types_user_defined_p (op
, arg1
->type (), arg2
->type ());
310 /* Check to see if argument is a structure. This is called so
311 we know whether to go ahead with the normal unop or look for a
312 user defined function instead.
314 For now, we do not overload the `&' operator. */
317 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
323 type1
= check_typedef (arg1
->type ());
324 if (TYPE_IS_REFERENCE (type1
))
325 type1
= check_typedef (type1
->target_type ());
326 return type1
->code () == TYPE_CODE_STRUCT
;
329 /* Try to find an operator named OPERATOR which takes NARGS arguments
330 specified in ARGS. If the operator found is a static member operator
331 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
332 The search if performed through find_overload_match which will handle
333 member operators, non member operators, operators imported implicitly or
334 explicitly, and perform correct overload resolution in all of the above
335 situations or combinations thereof. */
337 static struct value
*
338 value_user_defined_cpp_op (gdb::array_view
<value
*> args
, char *oper
,
339 int *static_memfuncp
, enum noside noside
)
342 struct symbol
*symp
= NULL
;
343 struct value
*valp
= NULL
;
345 find_overload_match (args
, oper
, BOTH
/* could be method */,
347 NULL
/* pass NULL symbol since symbol is unknown */,
348 &valp
, &symp
, static_memfuncp
, 0, noside
);
355 /* This is a non member function and does not
356 expect a reference as its first argument
357 rather the explicit structure. */
358 args
[0] = value_ind (args
[0]);
359 return value_of_variable (symp
, 0);
362 error (_("Could not find %s."), oper
);
365 /* Lookup user defined operator NAME. Return a value representing the
366 function, otherwise return NULL. */
368 static struct value
*
369 value_user_defined_op (struct value
**argp
, gdb::array_view
<value
*> args
,
370 char *name
, int *static_memfuncp
, enum noside noside
)
372 struct value
*result
= NULL
;
374 if (current_language
->la_language
== language_cplus
)
376 result
= value_user_defined_cpp_op (args
, name
, static_memfuncp
,
380 result
= value_struct_elt (argp
, args
, name
, static_memfuncp
,
386 /* We know either arg1 or arg2 is a structure, so try to find the right
387 user defined function. Create an argument vector that calls
388 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
389 binary operator which is legal for GNU C++).
391 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
392 is the opcode saying how to modify it. Otherwise, OTHEROP is
396 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
397 enum exp_opcode otherop
, enum noside noside
)
403 arg1
= coerce_ref (arg1
);
404 arg2
= coerce_ref (arg2
);
406 /* now we know that what we have to do is construct our
407 arg vector and find the right function to call it with. */
409 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_STRUCT
)
410 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
412 value
*argvec_storage
[3];
413 gdb::array_view
<value
*> argvec
= argvec_storage
;
415 argvec
[1] = value_addr (arg1
);
418 /* Make the right function name up. */
419 strcpy (tstr
, "operator__");
444 case BINOP_BITWISE_AND
:
447 case BINOP_BITWISE_IOR
:
450 case BINOP_BITWISE_XOR
:
453 case BINOP_LOGICAL_AND
:
456 case BINOP_LOGICAL_OR
:
468 case BINOP_ASSIGN_MODIFY
:
486 case BINOP_BITWISE_AND
:
489 case BINOP_BITWISE_IOR
:
492 case BINOP_BITWISE_XOR
:
495 case BINOP_MOD
: /* invalid */
497 error (_("Invalid binary operation specified."));
500 case BINOP_SUBSCRIPT
:
521 case BINOP_MOD
: /* invalid */
523 error (_("Invalid binary operation specified."));
526 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1), tstr
,
527 &static_memfuncp
, noside
);
533 argvec
[1] = argvec
[0];
534 argvec
= argvec
.slice (1);
536 if (argvec
[0]->type ()->code () == TYPE_CODE_XMETHOD
)
538 /* Static xmethods are not supported yet. */
539 gdb_assert (static_memfuncp
== 0);
540 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
542 struct type
*return_type
543 = argvec
[0]->result_type_of_xmethod (argvec
.slice (1));
545 if (return_type
== NULL
)
546 error (_("Xmethod is missing return type."));
547 return value::zero (return_type
, arg1
->lval ());
549 return argvec
[0]->call_xmethod (argvec
.slice (1));
551 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
553 struct type
*return_type
;
555 return_type
= check_typedef (argvec
[0]->type ())->target_type ();
556 return value::zero (return_type
, arg1
->lval ());
558 return call_function_by_hand (argvec
[0], NULL
,
559 argvec
.slice (1, 2 - static_memfuncp
));
561 throw_error (NOT_FOUND_ERROR
,
562 _("member function %s not found"), tstr
);
565 /* We know that arg1 is a structure, so try to find a unary user
566 defined operator that matches the operator in question.
567 Create an argument vector that calls arg1.operator @ (arg1)
568 and return that value (where '@' is (almost) any unary operator which
569 is legal for GNU C++). */
572 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
574 struct gdbarch
*gdbarch
= arg1
->type ()->arch ();
576 char tstr
[13], mangle_tstr
[13];
577 int static_memfuncp
, nargs
;
579 arg1
= coerce_ref (arg1
);
581 /* now we know that what we have to do is construct our
582 arg vector and find the right function to call it with. */
584 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_STRUCT
)
585 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
587 value
*argvec_storage
[3];
588 gdb::array_view
<value
*> argvec
= argvec_storage
;
590 argvec
[1] = value_addr (arg1
);
595 /* Make the right function name up. */
596 strcpy (tstr
, "operator__");
598 strcpy (mangle_tstr
, "__");
601 case UNOP_PREINCREMENT
:
604 case UNOP_PREDECREMENT
:
607 case UNOP_POSTINCREMENT
:
609 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
612 case UNOP_POSTDECREMENT
:
614 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
617 case UNOP_LOGICAL_NOT
:
620 case UNOP_COMPLEMENT
:
636 error (_("Invalid unary operation specified."));
639 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1, nargs
), tstr
,
640 &static_memfuncp
, noside
);
646 argvec
[1] = argvec
[0];
647 argvec
= argvec
.slice (1);
649 if (argvec
[0]->type ()->code () == TYPE_CODE_XMETHOD
)
651 /* Static xmethods are not supported yet. */
652 gdb_assert (static_memfuncp
== 0);
653 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
655 struct type
*return_type
656 = argvec
[0]->result_type_of_xmethod (argvec
[1]);
658 if (return_type
== NULL
)
659 error (_("Xmethod is missing return type."));
660 return value::zero (return_type
, arg1
->lval ());
662 return argvec
[0]->call_xmethod (argvec
[1]);
664 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
666 struct type
*return_type
;
668 return_type
= check_typedef (argvec
[0]->type ())->target_type ();
669 return value::zero (return_type
, arg1
->lval ());
671 return call_function_by_hand (argvec
[0], NULL
,
672 argvec
.slice (1, nargs
));
674 throw_error (NOT_FOUND_ERROR
,
675 _("member function %s not found"), tstr
);
679 /* Concatenate two values. One value must be an array; and the other
680 value must either be an array with the same element type, or be of
681 the array's element type. */
684 value_concat (struct value
*arg1
, struct value
*arg2
)
686 struct type
*type1
= check_typedef (arg1
->type ());
687 struct type
*type2
= check_typedef (arg2
->type ());
689 if (type1
->code () != TYPE_CODE_ARRAY
&& type2
->code () != TYPE_CODE_ARRAY
)
690 error ("no array provided to concatenation");
693 struct type
*elttype1
= type1
;
694 if (elttype1
->code () == TYPE_CODE_ARRAY
)
696 elttype1
= elttype1
->target_type ();
697 if (!get_array_bounds (type1
, &low1
, &high1
))
698 error (_("could not determine array bounds on left-hand-side of "
699 "array concatenation"));
708 struct type
*elttype2
= type2
;
709 if (elttype2
->code () == TYPE_CODE_ARRAY
)
711 elttype2
= elttype2
->target_type ();
712 if (!get_array_bounds (type2
, &low2
, &high2
))
713 error (_("could not determine array bounds on right-hand-side of "
714 "array concatenation"));
722 if (!types_equal (elttype1
, elttype2
))
723 error (_("concatenation with different element types"));
725 LONGEST lowbound
= current_language
->c_style_arrays_p () ? 0 : 1;
726 LONGEST n_elts
= (high1
- low1
+ 1) + (high2
- low2
+ 1);
727 struct type
*atype
= lookup_array_range_type (elttype1
,
729 lowbound
+ n_elts
- 1);
731 struct value
*result
= value::allocate (atype
);
732 gdb::array_view
<gdb_byte
> contents
= result
->contents_raw ();
733 gdb::array_view
<const gdb_byte
> lhs_contents
= arg1
->contents ();
734 gdb::array_view
<const gdb_byte
> rhs_contents
= arg2
->contents ();
735 gdb::copy (lhs_contents
, contents
.slice (0, lhs_contents
.size ()));
736 gdb::copy (rhs_contents
, contents
.slice (lhs_contents
.size ()));
742 /* Obtain argument values for binary operation, converting from
743 other types if one of them is not floating point. */
745 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
746 gdb_byte
*x
, struct type
**eff_type_x
,
747 gdb_byte
*y
, struct type
**eff_type_y
)
749 struct type
*type1
, *type2
;
751 type1
= check_typedef (arg1
->type ());
752 type2
= check_typedef (arg2
->type ());
754 /* At least one of the arguments must be of floating-point type. */
755 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
757 if (is_floating_type (type1
) && is_floating_type (type2
)
758 && type1
->code () != type2
->code ())
759 /* The DFP extension to the C language does not allow mixing of
760 * decimal float types with other float types in expressions
761 * (see WDTR 24732, page 12). */
762 error (_("Mixing decimal floating types with "
763 "other floating types is not allowed."));
765 /* Obtain value of arg1, converting from other types if necessary. */
767 if (is_floating_type (type1
))
770 memcpy (x
, arg1
->contents ().data (), type1
->length ());
772 else if (is_integral_type (type1
))
775 if (type1
->is_unsigned ())
776 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
778 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
781 error (_("Don't know how to convert from %s to %s."), type1
->name (),
784 /* Obtain value of arg2, converting from other types if necessary. */
786 if (is_floating_type (type2
))
789 memcpy (y
, arg2
->contents ().data (), type2
->length ());
791 else if (is_integral_type (type2
))
794 if (type2
->is_unsigned ())
795 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
797 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
800 error (_("Don't know how to convert from %s to %s."), type1
->name (),
804 /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
805 perform the binary operation OP on these two operands, and return
806 the resulting value (also as a fixed point). */
808 static struct value
*
809 fixed_point_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
811 struct type
*type1
= check_typedef (arg1
->type ());
812 struct type
*type2
= check_typedef (arg2
->type ());
813 const struct language_defn
*language
= current_language
;
815 struct gdbarch
*gdbarch
= type1
->arch ();
820 gdb_assert (is_fixed_point_type (type1
) || is_fixed_point_type (type2
));
821 if (op
== BINOP_MUL
|| op
== BINOP_DIV
)
823 v1
= value_to_gdb_mpq (arg1
);
824 v2
= value_to_gdb_mpq (arg2
);
826 /* The code below uses TYPE1 for the result type, so make sure
827 it is set properly. */
828 if (!is_fixed_point_type (type1
))
833 if (!is_fixed_point_type (type1
))
835 arg1
= value_cast (type2
, arg1
);
838 if (!is_fixed_point_type (type2
))
840 arg2
= value_cast (type1
, arg2
);
844 v1
.read_fixed_point (arg1
->contents (),
845 type_byte_order (type1
), type1
->is_unsigned (),
846 type1
->fixed_point_scaling_factor ());
847 v2
.read_fixed_point (arg2
->contents (),
848 type_byte_order (type2
), type2
->is_unsigned (),
849 type2
->fixed_point_scaling_factor ());
852 auto fixed_point_to_value
= [type1
] (const gdb_mpq
&fp
)
854 value
*fp_val
= value::allocate (type1
);
857 (fp_val
->contents_raw (),
858 type_byte_order (type1
),
859 type1
->is_unsigned (),
860 type1
->fixed_point_scaling_factor ());
869 val
= fixed_point_to_value (res
);
874 val
= fixed_point_to_value (res
);
878 val
= fixed_point_to_value (std::min (v1
, v2
));
882 val
= fixed_point_to_value (std::max (v1
, v2
));
887 val
= fixed_point_to_value (res
);
892 error (_("Division by zero"));
894 val
= fixed_point_to_value (res
);
898 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
903 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
908 error (_("Integer-only operation on fixed point number."));
914 /* A helper function that finds the type to use for a binary operation
915 involving TYPE1 and TYPE2. */
918 promotion_type (struct type
*type1
, struct type
*type2
)
920 struct type
*result_type
;
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 (type2
->length () > type1
->length ())
938 if (type1
->length () > type2
->length ())
940 else if (type2
->length () > type1
->length ())
942 else if (type1
->is_unsigned ())
944 else if (type2
->is_unsigned ())
953 static struct value
*scalar_binop (struct value
*arg1
, struct value
*arg2
,
956 /* Perform a binary operation on complex operands. */
958 static struct value
*
959 complex_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
961 struct type
*arg1_type
= check_typedef (arg1
->type ());
962 struct type
*arg2_type
= check_typedef (arg2
->type ());
964 struct value
*arg1_real
, *arg1_imag
, *arg2_real
, *arg2_imag
;
965 if (arg1_type
->code () == TYPE_CODE_COMPLEX
)
967 arg1_real
= value_real_part (arg1
);
968 arg1_imag
= value_imaginary_part (arg1
);
973 arg1_imag
= value::zero (arg1_type
, not_lval
);
975 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
977 arg2_real
= value_real_part (arg2
);
978 arg2_imag
= value_imaginary_part (arg2
);
983 arg2_imag
= value::zero (arg2_type
, not_lval
);
986 struct type
*comp_type
= promotion_type (arg1_real
->type (),
988 if (!can_create_complex_type (comp_type
))
989 error (_("Argument to complex arithmetic operation not supported."));
991 arg1_real
= value_cast (comp_type
, arg1_real
);
992 arg1_imag
= value_cast (comp_type
, arg1_imag
);
993 arg2_real
= value_cast (comp_type
, arg2_real
);
994 arg2_imag
= value_cast (comp_type
, arg2_imag
);
996 struct type
*result_type
= init_complex_type (nullptr, comp_type
);
998 struct value
*result_real
, *result_imag
;
1003 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1004 result_imag
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1009 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1010 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1011 result_real
= scalar_binop (x1
, x2
, BINOP_SUB
);
1013 x1
= scalar_binop (arg1_real
, arg2_imag
, op
);
1014 x2
= scalar_binop (arg1_imag
, arg2_real
, op
);
1015 result_imag
= scalar_binop (x1
, x2
, BINOP_ADD
);
1021 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1023 struct value
*conjugate
= value_complement (arg2
);
1024 /* We have to reconstruct ARG1, in case the type was
1026 arg1
= value_literal_complex (arg1_real
, arg1_imag
, result_type
);
1028 struct value
*numerator
= scalar_binop (arg1
, conjugate
,
1030 arg1_real
= value_real_part (numerator
);
1031 arg1_imag
= value_imaginary_part (numerator
);
1033 struct value
*x1
= scalar_binop (arg2_real
, arg2_real
, BINOP_MUL
);
1034 struct value
*x2
= scalar_binop (arg2_imag
, arg2_imag
, BINOP_MUL
);
1035 arg2_real
= scalar_binop (x1
, x2
, BINOP_ADD
);
1038 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1039 result_imag
= scalar_binop (arg1_imag
, arg2_real
, op
);
1044 case BINOP_NOTEQUAL
:
1046 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1047 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1049 LONGEST v1
= value_as_long (x1
);
1050 LONGEST v2
= value_as_long (x2
);
1052 if (op
== BINOP_EQUAL
)
1057 return value_from_longest (x1
->type (), v1
);
1062 error (_("Invalid binary operation on numbers."));
1065 return value_literal_complex (result_real
, result_imag
, result_type
);
1068 /* Return the type's length in bits. */
1071 type_length_bits (type
*type
)
1073 int unit_size
= gdbarch_addressable_memory_unit_size (type
->arch ());
1074 return unit_size
* 8 * type
->length ();
1077 /* Check whether the RHS value of a shift is valid in C/C++ semantics.
1078 SHIFT_COUNT is the shift amount, SHIFT_COUNT_TYPE is the type of
1079 the shift count value, used to determine whether the type is
1080 signed, and RESULT_TYPE is the result type. This is used to avoid
1081 both negative and too-large shift amounts, which are undefined, and
1082 would crash a GDB built with UBSan. Depending on the current
1083 language, if the shift is not valid, this either warns and returns
1084 false, or errors out. Returns true and sets NBITS if valid. */
1087 check_valid_shift_count (enum exp_opcode op
, type
*result_type
,
1088 type
*shift_count_type
, const gdb_mpz
&shift_count
,
1091 if (!shift_count_type
->is_unsigned ())
1093 LONGEST count
= shift_count
.as_integer
<LONGEST
> ();
1096 auto error_or_warning
= [] (const char *msg
)
1098 /* Shifts by a negative amount are always an error in Go. Other
1099 languages are more permissive and their compilers just warn or
1100 have modes to disable the errors. */
1101 if (current_language
->la_language
== language_go
)
1102 error (("%s"), msg
);
1104 warning (("%s"), msg
);
1107 if (op
== BINOP_RSH
)
1108 error_or_warning (_("right shift count is negative"));
1110 error_or_warning (_("left shift count is negative"));
1115 nbits
= shift_count
.as_integer
<ULONGEST
> ();
1116 if (nbits
>= type_length_bits (result_type
))
1118 /* In Go, shifting by large amounts is defined. Be silent and
1119 still return false, as the caller's error path does the right
1121 if (current_language
->la_language
!= language_go
)
1123 if (op
== BINOP_RSH
)
1124 warning (_("right shift count >= width of type"));
1126 warning (_("left shift count >= width of type"));
1134 /* Perform a binary operation on two operands which have reasonable
1135 representations as integers or floats. This includes booleans,
1136 characters, integers, or floats.
1137 Does not support addition and subtraction on pointers;
1138 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1140 static struct value
*
1141 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1144 struct type
*type1
, *type2
, *result_type
;
1146 arg1
= coerce_ref (arg1
);
1147 arg2
= coerce_ref (arg2
);
1149 type1
= check_typedef (arg1
->type ());
1150 type2
= check_typedef (arg2
->type ());
1152 if (type1
->code () == TYPE_CODE_COMPLEX
1153 || type2
->code () == TYPE_CODE_COMPLEX
)
1154 return complex_binop (arg1
, arg2
, op
);
1156 if ((!is_floating_value (arg1
)
1157 && !is_integral_type (type1
)
1158 && !is_fixed_point_type (type1
))
1159 || (!is_floating_value (arg2
)
1160 && !is_integral_type (type2
)
1161 && !is_fixed_point_type (type2
)))
1162 error (_("Argument to arithmetic operation not a number or boolean."));
1164 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
1165 return fixed_point_binop (arg1
, arg2
, op
);
1167 if (is_floating_type (type1
) || is_floating_type (type2
))
1169 result_type
= promotion_type (type1
, type2
);
1170 val
= value::allocate (result_type
);
1172 struct type
*eff_type_v1
, *eff_type_v2
;
1173 gdb::byte_vector v1
, v2
;
1174 v1
.resize (result_type
->length ());
1175 v2
.resize (result_type
->length ());
1177 value_args_as_target_float (arg1
, arg2
,
1178 v1
.data (), &eff_type_v1
,
1179 v2
.data (), &eff_type_v2
);
1180 target_float_binop (op
, v1
.data (), eff_type_v1
,
1181 v2
.data (), eff_type_v2
,
1182 val
->contents_raw ().data (), result_type
);
1184 else if (type1
->code () == TYPE_CODE_BOOL
1185 || type2
->code () == TYPE_CODE_BOOL
)
1187 LONGEST v1
, v2
, v
= 0;
1189 v1
= value_as_long (arg1
);
1190 v2
= value_as_long (arg2
);
1194 case BINOP_BITWISE_AND
:
1198 case BINOP_BITWISE_IOR
:
1202 case BINOP_BITWISE_XOR
:
1210 case BINOP_NOTEQUAL
:
1215 error (_("Invalid operation on booleans."));
1218 result_type
= type1
;
1220 val
= value::allocate (result_type
);
1221 store_signed_integer (val
->contents_raw ().data (),
1222 result_type
->length (),
1223 type_byte_order (result_type
),
1227 /* Integral operations here. */
1229 /* Determine type length of the result, and if the operation should
1230 be done unsigned. For exponentiation and shift operators,
1231 use the length and type of the left operand. Otherwise,
1232 use the signedness of the operand with the greater length.
1233 If both operands are of equal length, use unsigned operation
1234 if one of the operands is unsigned. */
1235 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1236 result_type
= type1
;
1238 result_type
= promotion_type (type1
, type2
);
1240 gdb_mpz v1
= value_as_mpz (arg1
);
1241 gdb_mpz v2
= value_as_mpz (arg2
);
1263 error (_("Division by zero"));
1267 v
= v1
.pow (v2
.as_integer
<unsigned long> ());
1274 error (_("Division by zero"));
1278 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1279 v1 mod 0 has a defined value, v1. */
1287 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1295 if (!check_valid_shift_count (op
, result_type
, type2
, v2
, nbits
))
1305 if (!check_valid_shift_count (op
, result_type
, type2
, v2
, nbits
))
1307 /* Pretend the too-large shift was decomposed in a
1308 number of smaller shifts. An arithmetic signed
1309 right shift of a negative number always yields -1
1310 with such semantics. This is the right thing to
1311 do for Go, and we might as well do it for
1312 languages where it is undefined. Also, pretend a
1313 shift by a negative number was a shift by the
1314 negative number cast to unsigned, which is the
1315 same as shifting by a too-large number. */
1316 if (v1
< 0 && !result_type
->is_unsigned ())
1326 case BINOP_BITWISE_AND
:
1330 case BINOP_BITWISE_IOR
:
1334 case BINOP_BITWISE_XOR
:
1339 v
= v1
< v2
? v1
: v2
;
1343 v
= v1
> v2
? v1
: v2
;
1350 case BINOP_NOTEQUAL
:
1371 error (_("Invalid binary operation on numbers."));
1374 val
= value_from_mpz (result_type
, v
);
1380 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1381 replicating SCALAR_VALUE for each element of the vector. Only scalar
1382 types that can be cast to the type of one element of the vector are
1383 acceptable. The newly created vector value is returned upon success,
1384 otherwise an error is thrown. */
1387 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1389 /* Widen the scalar to a vector. */
1390 struct type
*eltype
, *scalar_type
;
1391 struct value
*elval
;
1392 LONGEST low_bound
, high_bound
;
1395 vector_type
= check_typedef (vector_type
);
1397 gdb_assert (vector_type
->code () == TYPE_CODE_ARRAY
1398 && vector_type
->is_vector ());
1400 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1401 error (_("Could not determine the vector bounds"));
1403 eltype
= check_typedef (vector_type
->target_type ());
1404 elval
= value_cast (eltype
, scalar_value
);
1406 scalar_type
= check_typedef (scalar_value
->type ());
1408 /* If we reduced the length of the scalar then check we didn't loose any
1410 if (eltype
->length () < scalar_type
->length ()
1411 && !value_equal (elval
, scalar_value
))
1412 error (_("conversion of scalar to vector involves truncation"));
1414 value
*val
= value::allocate (vector_type
);
1415 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1416 int elt_len
= eltype
->length ();
1418 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1419 /* Duplicate the contents of elval into the destination vector. */
1420 copy (elval
->contents_all (),
1421 val_contents
.slice (i
* elt_len
, elt_len
));
1426 /* Performs a binary operation on two vector operands by calling scalar_binop
1427 for each pair of vector components. */
1429 static struct value
*
1430 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1432 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1433 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1434 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1436 type1
= check_typedef (val1
->type ());
1437 type2
= check_typedef (val2
->type ());
1439 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1440 && type1
->is_vector ()) ? 1 : 0;
1441 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1442 && type2
->is_vector ()) ? 1 : 0;
1444 if (!t1_is_vec
|| !t2_is_vec
)
1445 error (_("Vector operations are only supported among vectors"));
1447 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1448 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1449 error (_("Could not determine the vector bounds"));
1451 eltype1
= check_typedef (type1
->target_type ());
1452 eltype2
= check_typedef (type2
->target_type ());
1453 elsize
= eltype1
->length ();
1455 if (eltype1
->code () != eltype2
->code ()
1456 || elsize
!= eltype2
->length ()
1457 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
1458 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1459 error (_("Cannot perform operation on vectors with different types"));
1461 value
*val
= value::allocate (type1
);
1462 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1463 scoped_value_mark mark
;
1464 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1466 value
*tmp
= value_binop (value_subscript (val1
, i
),
1467 value_subscript (val2
, i
), op
);
1468 copy (tmp
->contents_all (),
1469 val_contents
.slice (i
* elsize
, elsize
));
1475 /* Perform a binary operation on two operands. */
1478 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1481 struct type
*type1
= check_typedef (arg1
->type ());
1482 struct type
*type2
= check_typedef (arg2
->type ());
1483 int t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1484 && type1
->is_vector ());
1485 int t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1486 && type2
->is_vector ());
1488 if (!t1_is_vec
&& !t2_is_vec
)
1489 val
= scalar_binop (arg1
, arg2
, op
);
1490 else if (t1_is_vec
&& t2_is_vec
)
1491 val
= vector_binop (arg1
, arg2
, op
);
1494 /* Widen the scalar operand to a vector. */
1495 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1496 struct type
*t
= t1_is_vec
? type2
: type1
;
1498 if (t
->code () != TYPE_CODE_FLT
1499 && t
->code () != TYPE_CODE_DECFLOAT
1500 && !is_integral_type (t
))
1501 error (_("Argument to operation not a number or boolean."));
1503 /* Replicate the scalar value to make a vector value. */
1504 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1506 val
= vector_binop (arg1
, arg2
, op
);
1515 value_logical_not (struct value
*arg1
)
1521 arg1
= coerce_array (arg1
);
1522 type1
= check_typedef (arg1
->type ());
1524 if (is_floating_value (arg1
))
1525 return target_float_is_zero (arg1
->contents ().data (), type1
);
1527 len
= type1
->length ();
1528 p
= arg1
->contents ().data ();
1539 /* Perform a comparison on two string values (whose content are not
1540 necessarily null terminated) based on their length. */
1543 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1545 int len1
= arg1
->type ()->length ();
1546 int len2
= arg2
->type ()->length ();
1547 const gdb_byte
*s1
= arg1
->contents ().data ();
1548 const gdb_byte
*s2
= arg2
->contents ().data ();
1549 int i
, len
= len1
< len2
? len1
: len2
;
1551 for (i
= 0; i
< len
; i
++)
1555 else if (s1
[i
] > s2
[i
])
1563 else if (len1
> len2
)
1569 /* Simulate the C operator == by returning a 1
1570 iff ARG1 and ARG2 have equal contents. */
1573 value_equal (struct value
*arg1
, struct value
*arg2
)
1578 struct type
*type1
, *type2
;
1579 enum type_code code1
;
1580 enum type_code code2
;
1581 int is_int1
, is_int2
;
1583 arg1
= coerce_array (arg1
);
1584 arg2
= coerce_array (arg2
);
1586 type1
= check_typedef (arg1
->type ());
1587 type2
= check_typedef (arg2
->type ());
1588 code1
= type1
->code ();
1589 code2
= type2
->code ();
1590 is_int1
= is_integral_type (type1
);
1591 is_int2
= is_integral_type (type2
);
1593 if (is_int1
&& is_int2
)
1594 return value_true (value_binop (arg1
, arg2
, BINOP_EQUAL
));
1595 else if ((is_floating_value (arg1
) || is_int1
)
1596 && (is_floating_value (arg2
) || is_int2
))
1598 struct type
*eff_type_v1
, *eff_type_v2
;
1599 gdb::byte_vector v1
, v2
;
1600 v1
.resize (std::max (type1
->length (), type2
->length ()));
1601 v2
.resize (std::max (type1
->length (), type2
->length ()));
1603 value_args_as_target_float (arg1
, arg2
,
1604 v1
.data (), &eff_type_v1
,
1605 v2
.data (), &eff_type_v2
);
1607 return target_float_compare (v1
.data (), eff_type_v1
,
1608 v2
.data (), eff_type_v2
) == 0;
1611 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1613 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1614 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1615 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1616 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1618 else if (code1
== code2
1619 && ((len
= (int) type1
->length ())
1620 == (int) type2
->length ()))
1622 p1
= arg1
->contents ().data ();
1623 p2
= arg2
->contents ().data ();
1631 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1633 return value_strcmp (arg1
, arg2
) == 0;
1636 error (_("Invalid type combination in equality test."));
1639 /* Compare values based on their raw contents. Useful for arrays since
1640 value_equal coerces them to pointers, thus comparing just the address
1641 of the array instead of its contents. */
1644 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1646 struct type
*type1
, *type2
;
1648 type1
= check_typedef (arg1
->type ());
1649 type2
= check_typedef (arg2
->type ());
1651 return (type1
->code () == type2
->code ()
1652 && type1
->length () == type2
->length ()
1653 && memcmp (arg1
->contents ().data (),
1654 arg2
->contents ().data (),
1655 type1
->length ()) == 0);
1658 /* Simulate the C operator < by returning 1
1659 iff ARG1's contents are less than ARG2's. */
1662 value_less (struct value
*arg1
, struct value
*arg2
)
1664 enum type_code code1
;
1665 enum type_code code2
;
1666 struct type
*type1
, *type2
;
1667 int is_int1
, is_int2
;
1669 arg1
= coerce_array (arg1
);
1670 arg2
= coerce_array (arg2
);
1672 type1
= check_typedef (arg1
->type ());
1673 type2
= check_typedef (arg2
->type ());
1674 code1
= type1
->code ();
1675 code2
= type2
->code ();
1676 is_int1
= is_integral_type (type1
);
1677 is_int2
= is_integral_type (type2
);
1679 if ((is_int1
&& is_int2
)
1680 || (is_fixed_point_type (type1
) && is_fixed_point_type (type2
)))
1681 return value_true (value_binop (arg1
, arg2
, BINOP_LESS
));
1682 else if ((is_floating_value (arg1
) || is_int1
)
1683 && (is_floating_value (arg2
) || is_int2
))
1685 struct type
*eff_type_v1
, *eff_type_v2
;
1686 gdb::byte_vector v1
, v2
;
1687 v1
.resize (std::max (type1
->length (), type2
->length ()));
1688 v2
.resize (std::max (type1
->length (), type2
->length ()));
1690 value_args_as_target_float (arg1
, arg2
,
1691 v1
.data (), &eff_type_v1
,
1692 v2
.data (), &eff_type_v2
);
1694 return target_float_compare (v1
.data (), eff_type_v1
,
1695 v2
.data (), eff_type_v2
) == -1;
1697 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1698 return value_as_address (arg1
) < value_as_address (arg2
);
1700 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1702 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1703 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1704 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1705 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1706 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1707 return value_strcmp (arg1
, arg2
) < 0;
1710 error (_("Invalid type combination in ordering comparison."));
1718 value_pos (struct value
*arg1
)
1722 arg1
= coerce_ref (arg1
);
1723 type
= check_typedef (arg1
->type ());
1725 if (is_integral_type (type
) || is_floating_value (arg1
)
1726 || (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1727 || type
->code () == TYPE_CODE_COMPLEX
)
1728 return value_from_contents (type
, arg1
->contents ().data ());
1730 error (_("Argument to positive operation not a number."));
1736 value_neg (struct value
*arg1
)
1740 arg1
= coerce_ref (arg1
);
1741 type
= check_typedef (arg1
->type ());
1743 if (is_integral_type (type
) || is_floating_type (type
))
1744 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1745 else if (is_fixed_point_type (type
))
1746 return value_binop (value::zero (type
, not_lval
), arg1
, BINOP_SUB
);
1747 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1749 struct value
*val
= value::allocate (type
);
1750 struct type
*eltype
= check_typedef (type
->target_type ());
1752 LONGEST low_bound
, high_bound
;
1754 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1755 error (_("Could not determine the vector bounds"));
1757 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1758 int elt_len
= eltype
->length ();
1760 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1762 value
*tmp
= value_neg (value_subscript (arg1
, i
));
1763 copy (tmp
->contents_all (),
1764 val_contents
.slice (i
* elt_len
, elt_len
));
1768 else if (type
->code () == TYPE_CODE_COMPLEX
)
1770 struct value
*real
= value_real_part (arg1
);
1771 struct value
*imag
= value_imaginary_part (arg1
);
1773 real
= value_neg (real
);
1774 imag
= value_neg (imag
);
1775 return value_literal_complex (real
, imag
, type
);
1778 error (_("Argument to negate operation not a number."));
1784 value_complement (struct value
*arg1
)
1789 arg1
= coerce_ref (arg1
);
1790 type
= check_typedef (arg1
->type ());
1792 if (is_integral_type (type
))
1794 gdb_mpz num
= value_as_mpz (arg1
);
1796 val
= value_from_mpz (type
, num
);
1798 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1800 struct type
*eltype
= check_typedef (type
->target_type ());
1802 LONGEST low_bound
, high_bound
;
1804 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1805 error (_("Could not determine the vector bounds"));
1807 val
= value::allocate (type
);
1808 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1809 int elt_len
= eltype
->length ();
1811 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1813 value
*tmp
= value_complement (value_subscript (arg1
, i
));
1814 copy (tmp
->contents_all (),
1815 val_contents
.slice (i
* elt_len
, elt_len
));
1818 else if (type
->code () == TYPE_CODE_COMPLEX
)
1820 /* GCC has an extension that treats ~complex as the complex
1822 struct value
*real
= value_real_part (arg1
);
1823 struct value
*imag
= value_imaginary_part (arg1
);
1825 imag
= value_neg (imag
);
1826 return value_literal_complex (real
, imag
, type
);
1829 error (_("Argument to complement operation not an integer, boolean."));
1834 /* The INDEX'th bit of SET value whose value_type is TYPE,
1835 and whose value_contents is valaddr.
1836 Return -1 if out of range, -2 other error. */
1839 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1841 struct gdbarch
*gdbarch
= type
->arch ();
1842 LONGEST low_bound
, high_bound
;
1845 struct type
*range
= type
->index_type ();
1847 if (!get_discrete_bounds (range
, &low_bound
, &high_bound
))
1849 if (index
< low_bound
|| index
> high_bound
)
1851 rel_index
= index
- low_bound
;
1852 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1853 type_byte_order (type
));
1854 rel_index
%= TARGET_CHAR_BIT
;
1855 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
1856 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1857 return (word
>> rel_index
) & 1;