1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2023 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 /* Forward declarations. */
33 static struct value
*value_subscripted_rvalue (struct value
*array
,
37 /* Given a pointer, return the size of its target.
38 If the pointer type is void *, then return 1.
39 If the target type is incomplete, then error out.
40 This isn't a general purpose function, but just a
41 helper for value_ptradd. */
44 find_size_for_pointer_math (struct type
*ptr_type
)
47 struct type
*ptr_target
;
49 gdb_assert (ptr_type
->code () == TYPE_CODE_PTR
);
50 ptr_target
= check_typedef (ptr_type
->target_type ());
52 sz
= type_length_units (ptr_target
);
55 if (ptr_type
->code () == TYPE_CODE_VOID
)
61 name
= ptr_target
->name ();
63 error (_("Cannot perform pointer math on incomplete types, "
64 "try casting to a known type, or void *."));
66 error (_("Cannot perform pointer math on incomplete type \"%s\", "
67 "try casting to a known type, or void *."), name
);
73 /* Given a pointer ARG1 and an integral value ARG2, return the
74 result of C-style pointer arithmetic ARG1 + ARG2. */
77 value_ptradd (struct value
*arg1
, LONGEST arg2
)
79 struct type
*valptrtype
;
83 arg1
= coerce_array (arg1
);
84 valptrtype
= check_typedef (arg1
->type ());
85 sz
= find_size_for_pointer_math (valptrtype
);
87 result
= value_from_pointer (valptrtype
,
88 value_as_address (arg1
) + sz
* arg2
);
89 if (arg1
->lval () != lval_internalvar
)
90 result
->set_component_location (arg1
);
94 /* Given two compatible pointer values ARG1 and ARG2, return the
95 result of C-style pointer arithmetic ARG1 - ARG2. */
98 value_ptrdiff (struct value
*arg1
, struct value
*arg2
)
100 struct type
*type1
, *type2
;
103 arg1
= coerce_array (arg1
);
104 arg2
= coerce_array (arg2
);
105 type1
= check_typedef (arg1
->type ());
106 type2
= check_typedef (arg2
->type ());
108 gdb_assert (type1
->code () == TYPE_CODE_PTR
);
109 gdb_assert (type2
->code () == TYPE_CODE_PTR
);
111 if (check_typedef (type1
->target_type ())->length ()
112 != check_typedef (type2
->target_type ())->length ())
113 error (_("First argument of `-' is a pointer and "
114 "second argument is neither\n"
115 "an integer nor a pointer of the same type."));
117 sz
= type_length_units (check_typedef (type1
->target_type ()));
120 warning (_("Type size unknown, assuming 1. "
121 "Try casting to a known type, or void *."));
125 return (value_as_long (arg1
) - value_as_long (arg2
)) / sz
;
128 /* Return the value of ARRAY[IDX].
130 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
131 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
133 See comments in value_coerce_array() for rationale for reason for
134 doing lower bounds adjustment here rather than there.
135 FIXME: Perhaps we should validate that the index is valid and if
136 verbosity is set, warn about invalid indices (but still use them). */
139 value_subscript (struct value
*array
, LONGEST index
)
141 bool c_style
= current_language
->c_style_arrays_p ();
144 array
= coerce_ref (array
);
145 tarray
= check_typedef (array
->type ());
147 if (tarray
->code () == TYPE_CODE_ARRAY
148 || tarray
->code () == TYPE_CODE_STRING
)
150 struct type
*range_type
= tarray
->index_type ();
151 gdb::optional
<LONGEST
> lowerbound
= get_discrete_low_bound (range_type
);
152 if (!lowerbound
.has_value ())
155 if (array
->lval () != lval_memory
)
156 return value_subscripted_rvalue (array
, index
, *lowerbound
);
158 gdb::optional
<LONGEST
> upperbound
159 = get_discrete_high_bound (range_type
);
161 if (!upperbound
.has_value ())
164 if (index
>= *lowerbound
&& index
<= *upperbound
)
165 return value_subscripted_rvalue (array
, index
, *lowerbound
);
169 /* Emit warning unless we have an array of unknown size.
170 An array of unknown size has lowerbound 0 and upperbound -1. */
171 if (*upperbound
> -1)
172 warning (_("array or string index out of range"));
173 /* fall doing C stuff */
177 index
-= *lowerbound
;
179 /* Do not try to dereference a pointer to an unavailable value.
180 Instead mock up a new one and give it the original address. */
181 struct type
*elt_type
= check_typedef (tarray
->target_type ());
182 LONGEST elt_size
= type_length_units (elt_type
);
184 && !array
->bytes_available (elt_size
* index
, elt_size
))
186 struct value
*val
= value::allocate (elt_type
);
187 val
->mark_bytes_unavailable (0, elt_size
);
188 val
->set_lval (lval_memory
);
189 val
->set_address (array
->address () + elt_size
* index
);
193 array
= value_coerce_array (array
);
197 return value_ind (value_ptradd (array
, index
));
199 error (_("not an array or string"));
202 /* Return the value of EXPR[IDX], expr an aggregate rvalue
203 (eg, a vector register). This routine used to promote floats
204 to doubles, but no longer does. */
206 static struct value
*
207 value_subscripted_rvalue (struct value
*array
, LONGEST index
,
210 struct type
*array_type
= check_typedef (array
->type ());
211 struct type
*elt_type
= array_type
->target_type ();
212 LONGEST elt_size
= type_length_units (elt_type
);
214 /* Fetch the bit stride and convert it to a byte stride, assuming 8 bits
216 LONGEST stride
= array_type
->bit_stride ();
219 struct gdbarch
*arch
= elt_type
->arch ();
220 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
221 elt_size
= stride
/ (unit_size
* 8);
224 LONGEST elt_offs
= elt_size
* (index
- lowerbound
);
225 bool array_upper_bound_undefined
226 = array_type
->bounds ()->high
.kind () == PROP_UNDEFINED
;
228 if (index
< lowerbound
229 || (!array_upper_bound_undefined
230 && elt_offs
>= type_length_units (array_type
))
231 || (array
->lval () != lval_memory
&& array_upper_bound_undefined
))
233 if (type_not_associated (array_type
))
234 error (_("no such vector element (vector not associated)"));
235 else if (type_not_allocated (array_type
))
236 error (_("no such vector element (vector not allocated)"));
238 error (_("no such vector element"));
241 if (is_dynamic_type (elt_type
))
245 address
= array
->address () + elt_offs
;
246 elt_type
= resolve_dynamic_type (elt_type
, {}, address
);
249 return value_from_component (array
, elt_type
, elt_offs
);
253 /* Check to see if either argument is a structure, or a reference to
254 one. This is called so we know whether to go ahead with the normal
255 binop or look for a user defined function instead.
257 For now, we do not overload the `=' operator. */
260 binop_types_user_defined_p (enum exp_opcode op
,
261 struct type
*type1
, struct type
*type2
)
263 if (op
== BINOP_ASSIGN
)
266 type1
= check_typedef (type1
);
267 if (TYPE_IS_REFERENCE (type1
))
268 type1
= check_typedef (type1
->target_type ());
270 type2
= check_typedef (type2
);
271 if (TYPE_IS_REFERENCE (type2
))
272 type2
= check_typedef (type2
->target_type ());
274 return (type1
->code () == TYPE_CODE_STRUCT
275 || type2
->code () == TYPE_CODE_STRUCT
);
278 /* Check to see if either argument is a structure, or a reference to
279 one. This is called so we know whether to go ahead with the normal
280 binop or look for a user defined function instead.
282 For now, we do not overload the `=' operator. */
285 binop_user_defined_p (enum exp_opcode op
,
286 struct value
*arg1
, struct value
*arg2
)
288 return binop_types_user_defined_p (op
, arg1
->type (), arg2
->type ());
291 /* Check to see if argument is a structure. This is called so
292 we know whether to go ahead with the normal unop or look for a
293 user defined function instead.
295 For now, we do not overload the `&' operator. */
298 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
304 type1
= check_typedef (arg1
->type ());
305 if (TYPE_IS_REFERENCE (type1
))
306 type1
= check_typedef (type1
->target_type ());
307 return type1
->code () == TYPE_CODE_STRUCT
;
310 /* Try to find an operator named OPERATOR which takes NARGS arguments
311 specified in ARGS. If the operator found is a static member operator
312 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
313 The search if performed through find_overload_match which will handle
314 member operators, non member operators, operators imported implicitly or
315 explicitly, and perform correct overload resolution in all of the above
316 situations or combinations thereof. */
318 static struct value
*
319 value_user_defined_cpp_op (gdb::array_view
<value
*> args
, char *oper
,
320 int *static_memfuncp
, enum noside noside
)
323 struct symbol
*symp
= NULL
;
324 struct value
*valp
= NULL
;
326 find_overload_match (args
, oper
, BOTH
/* could be method */,
328 NULL
/* pass NULL symbol since symbol is unknown */,
329 &valp
, &symp
, static_memfuncp
, 0, noside
);
336 /* This is a non member function and does not
337 expect a reference as its first argument
338 rather the explicit structure. */
339 args
[0] = value_ind (args
[0]);
340 return value_of_variable (symp
, 0);
343 error (_("Could not find %s."), oper
);
346 /* Lookup user defined operator NAME. Return a value representing the
347 function, otherwise return NULL. */
349 static struct value
*
350 value_user_defined_op (struct value
**argp
, gdb::array_view
<value
*> args
,
351 char *name
, int *static_memfuncp
, enum noside noside
)
353 struct value
*result
= NULL
;
355 if (current_language
->la_language
== language_cplus
)
357 result
= value_user_defined_cpp_op (args
, name
, static_memfuncp
,
361 result
= value_struct_elt (argp
, args
, name
, static_memfuncp
,
367 /* We know either arg1 or arg2 is a structure, so try to find the right
368 user defined function. Create an argument vector that calls
369 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
370 binary operator which is legal for GNU C++).
372 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
373 is the opcode saying how to modify it. Otherwise, OTHEROP is
377 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
378 enum exp_opcode otherop
, enum noside noside
)
384 arg1
= coerce_ref (arg1
);
385 arg2
= coerce_ref (arg2
);
387 /* now we know that what we have to do is construct our
388 arg vector and find the right function to call it with. */
390 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_STRUCT
)
391 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
393 value
*argvec_storage
[3];
394 gdb::array_view
<value
*> argvec
= argvec_storage
;
396 argvec
[1] = value_addr (arg1
);
399 /* Make the right function name up. */
400 strcpy (tstr
, "operator__");
425 case BINOP_BITWISE_AND
:
428 case BINOP_BITWISE_IOR
:
431 case BINOP_BITWISE_XOR
:
434 case BINOP_LOGICAL_AND
:
437 case BINOP_LOGICAL_OR
:
449 case BINOP_ASSIGN_MODIFY
:
467 case BINOP_BITWISE_AND
:
470 case BINOP_BITWISE_IOR
:
473 case BINOP_BITWISE_XOR
:
476 case BINOP_MOD
: /* invalid */
478 error (_("Invalid binary operation specified."));
481 case BINOP_SUBSCRIPT
:
502 case BINOP_MOD
: /* invalid */
504 error (_("Invalid binary operation specified."));
507 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1), tstr
,
508 &static_memfuncp
, noside
);
514 argvec
[1] = argvec
[0];
515 argvec
= argvec
.slice (1);
517 if (argvec
[0]->type ()->code () == TYPE_CODE_XMETHOD
)
519 /* Static xmethods are not supported yet. */
520 gdb_assert (static_memfuncp
== 0);
521 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
523 struct type
*return_type
524 = argvec
[0]->result_type_of_xmethod (argvec
.slice (1));
526 if (return_type
== NULL
)
527 error (_("Xmethod is missing return type."));
528 return value::zero (return_type
, arg1
->lval ());
530 return argvec
[0]->call_xmethod (argvec
.slice (1));
532 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
534 struct type
*return_type
;
536 return_type
= check_typedef (argvec
[0]->type ())->target_type ();
537 return value::zero (return_type
, arg1
->lval ());
539 return call_function_by_hand (argvec
[0], NULL
,
540 argvec
.slice (1, 2 - static_memfuncp
));
542 throw_error (NOT_FOUND_ERROR
,
543 _("member function %s not found"), tstr
);
546 /* We know that arg1 is a structure, so try to find a unary user
547 defined operator that matches the operator in question.
548 Create an argument vector that calls arg1.operator @ (arg1)
549 and return that value (where '@' is (almost) any unary operator which
550 is legal for GNU C++). */
553 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
555 struct gdbarch
*gdbarch
= arg1
->type ()->arch ();
557 char tstr
[13], mangle_tstr
[13];
558 int static_memfuncp
, nargs
;
560 arg1
= coerce_ref (arg1
);
562 /* now we know that what we have to do is construct our
563 arg vector and find the right function to call it with. */
565 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_STRUCT
)
566 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
568 value
*argvec_storage
[3];
569 gdb::array_view
<value
*> argvec
= argvec_storage
;
571 argvec
[1] = value_addr (arg1
);
576 /* Make the right function name up. */
577 strcpy (tstr
, "operator__");
579 strcpy (mangle_tstr
, "__");
582 case UNOP_PREINCREMENT
:
585 case UNOP_PREDECREMENT
:
588 case UNOP_POSTINCREMENT
:
590 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
593 case UNOP_POSTDECREMENT
:
595 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
598 case UNOP_LOGICAL_NOT
:
601 case UNOP_COMPLEMENT
:
617 error (_("Invalid unary operation specified."));
620 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1, nargs
), tstr
,
621 &static_memfuncp
, noside
);
627 argvec
[1] = argvec
[0];
628 argvec
= argvec
.slice (1);
630 if (argvec
[0]->type ()->code () == TYPE_CODE_XMETHOD
)
632 /* Static xmethods are not supported yet. */
633 gdb_assert (static_memfuncp
== 0);
634 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
636 struct type
*return_type
637 = argvec
[0]->result_type_of_xmethod (argvec
[1]);
639 if (return_type
== NULL
)
640 error (_("Xmethod is missing return type."));
641 return value::zero (return_type
, arg1
->lval ());
643 return argvec
[0]->call_xmethod (argvec
[1]);
645 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
647 struct type
*return_type
;
649 return_type
= check_typedef (argvec
[0]->type ())->target_type ();
650 return value::zero (return_type
, arg1
->lval ());
652 return call_function_by_hand (argvec
[0], NULL
,
653 argvec
.slice (1, nargs
));
655 throw_error (NOT_FOUND_ERROR
,
656 _("member function %s not found"), tstr
);
660 /* Concatenate two values. One value must be an array; and the other
661 value must either be an array with the same element type, or be of
662 the array's element type. */
665 value_concat (struct value
*arg1
, struct value
*arg2
)
667 struct type
*type1
= check_typedef (arg1
->type ());
668 struct type
*type2
= check_typedef (arg2
->type ());
670 if (type1
->code () != TYPE_CODE_ARRAY
&& type2
->code () != TYPE_CODE_ARRAY
)
671 error ("no array provided to concatenation");
674 struct type
*elttype1
= type1
;
675 if (elttype1
->code () == TYPE_CODE_ARRAY
)
677 elttype1
= elttype1
->target_type ();
678 if (!get_array_bounds (type1
, &low1
, &high1
))
679 error (_("could not determine array bounds on left-hand-side of "
680 "array concatenation"));
689 struct type
*elttype2
= type2
;
690 if (elttype2
->code () == TYPE_CODE_ARRAY
)
692 elttype2
= elttype2
->target_type ();
693 if (!get_array_bounds (type2
, &low2
, &high2
))
694 error (_("could not determine array bounds on right-hand-side of "
695 "array concatenation"));
703 if (!types_equal (elttype1
, elttype2
))
704 error (_("concatenation with different element types"));
706 LONGEST lowbound
= current_language
->c_style_arrays_p () ? 0 : 1;
707 LONGEST n_elts
= (high1
- low1
+ 1) + (high2
- low2
+ 1);
708 struct type
*atype
= lookup_array_range_type (elttype1
,
710 lowbound
+ n_elts
- 1);
712 struct value
*result
= value::allocate (atype
);
713 gdb::array_view
<gdb_byte
> contents
= result
->contents_raw ();
714 gdb::array_view
<const gdb_byte
> lhs_contents
= arg1
->contents ();
715 gdb::array_view
<const gdb_byte
> rhs_contents
= arg2
->contents ();
716 gdb::copy (lhs_contents
, contents
.slice (0, lhs_contents
.size ()));
717 gdb::copy (rhs_contents
, contents
.slice (lhs_contents
.size ()));
723 /* Obtain argument values for binary operation, converting from
724 other types if one of them is not floating point. */
726 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
727 gdb_byte
*x
, struct type
**eff_type_x
,
728 gdb_byte
*y
, struct type
**eff_type_y
)
730 struct type
*type1
, *type2
;
732 type1
= check_typedef (arg1
->type ());
733 type2
= check_typedef (arg2
->type ());
735 /* At least one of the arguments must be of floating-point type. */
736 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
738 if (is_floating_type (type1
) && is_floating_type (type2
)
739 && type1
->code () != type2
->code ())
740 /* The DFP extension to the C language does not allow mixing of
741 * decimal float types with other float types in expressions
742 * (see WDTR 24732, page 12). */
743 error (_("Mixing decimal floating types with "
744 "other floating types is not allowed."));
746 /* Obtain value of arg1, converting from other types if necessary. */
748 if (is_floating_type (type1
))
751 memcpy (x
, arg1
->contents ().data (), type1
->length ());
753 else if (is_integral_type (type1
))
756 if (type1
->is_unsigned ())
757 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
759 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
762 error (_("Don't know how to convert from %s to %s."), type1
->name (),
765 /* Obtain value of arg2, converting from other types if necessary. */
767 if (is_floating_type (type2
))
770 memcpy (y
, arg2
->contents ().data (), type2
->length ());
772 else if (is_integral_type (type2
))
775 if (type2
->is_unsigned ())
776 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
778 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
781 error (_("Don't know how to convert from %s to %s."), type1
->name (),
785 /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
786 perform the binary operation OP on these two operands, and return
787 the resulting value (also as a fixed point). */
789 static struct value
*
790 fixed_point_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
792 struct type
*type1
= check_typedef (arg1
->type ());
793 struct type
*type2
= check_typedef (arg2
->type ());
794 const struct language_defn
*language
= current_language
;
796 struct gdbarch
*gdbarch
= type1
->arch ();
801 gdb_assert (is_fixed_point_type (type1
) || is_fixed_point_type (type2
));
802 if (op
== BINOP_MUL
|| op
== BINOP_DIV
)
804 v1
= value_to_gdb_mpq (arg1
);
805 v2
= value_to_gdb_mpq (arg2
);
807 /* The code below uses TYPE1 for the result type, so make sure
808 it is set properly. */
809 if (!is_fixed_point_type (type1
))
814 if (!is_fixed_point_type (type1
))
816 arg1
= value_cast (type2
, arg1
);
819 if (!is_fixed_point_type (type2
))
821 arg2
= value_cast (type1
, arg2
);
825 v1
.read_fixed_point (arg1
->contents (),
826 type_byte_order (type1
), type1
->is_unsigned (),
827 type1
->fixed_point_scaling_factor ());
828 v2
.read_fixed_point (arg2
->contents (),
829 type_byte_order (type2
), type2
->is_unsigned (),
830 type2
->fixed_point_scaling_factor ());
833 auto fixed_point_to_value
= [type1
] (const gdb_mpq
&fp
)
835 value
*fp_val
= value::allocate (type1
);
838 (fp_val
->contents_raw (),
839 type_byte_order (type1
),
840 type1
->is_unsigned (),
841 type1
->fixed_point_scaling_factor ());
850 val
= fixed_point_to_value (res
);
855 val
= fixed_point_to_value (res
);
859 val
= fixed_point_to_value (std::min (v1
, v2
));
863 val
= fixed_point_to_value (std::max (v1
, v2
));
868 val
= fixed_point_to_value (res
);
873 error (_("Division by zero"));
875 val
= fixed_point_to_value (res
);
879 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
884 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
889 error (_("Integer-only operation on fixed point number."));
895 /* A helper function that finds the type to use for a binary operation
896 involving TYPE1 and TYPE2. */
899 promotion_type (struct type
*type1
, struct type
*type2
)
901 struct type
*result_type
;
903 if (is_floating_type (type1
) || is_floating_type (type2
))
905 /* If only one type is floating-point, use its type.
906 Otherwise use the bigger type. */
907 if (!is_floating_type (type1
))
909 else if (!is_floating_type (type2
))
911 else if (type2
->length () > type1
->length ())
919 if (type1
->length () > type2
->length ())
921 else if (type2
->length () > type1
->length ())
923 else if (type1
->is_unsigned ())
925 else if (type2
->is_unsigned ())
934 static struct value
*scalar_binop (struct value
*arg1
, struct value
*arg2
,
937 /* Perform a binary operation on complex operands. */
939 static struct value
*
940 complex_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
942 struct type
*arg1_type
= check_typedef (arg1
->type ());
943 struct type
*arg2_type
= check_typedef (arg2
->type ());
945 struct value
*arg1_real
, *arg1_imag
, *arg2_real
, *arg2_imag
;
946 if (arg1_type
->code () == TYPE_CODE_COMPLEX
)
948 arg1_real
= value_real_part (arg1
);
949 arg1_imag
= value_imaginary_part (arg1
);
954 arg1_imag
= value::zero (arg1_type
, not_lval
);
956 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
958 arg2_real
= value_real_part (arg2
);
959 arg2_imag
= value_imaginary_part (arg2
);
964 arg2_imag
= value::zero (arg2_type
, not_lval
);
967 struct type
*comp_type
= promotion_type (arg1_real
->type (),
969 if (!can_create_complex_type (comp_type
))
970 error (_("Argument to complex arithmetic operation not supported."));
972 arg1_real
= value_cast (comp_type
, arg1_real
);
973 arg1_imag
= value_cast (comp_type
, arg1_imag
);
974 arg2_real
= value_cast (comp_type
, arg2_real
);
975 arg2_imag
= value_cast (comp_type
, arg2_imag
);
977 struct type
*result_type
= init_complex_type (nullptr, comp_type
);
979 struct value
*result_real
, *result_imag
;
984 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
985 result_imag
= scalar_binop (arg1_imag
, arg2_imag
, op
);
990 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
991 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
992 result_real
= scalar_binop (x1
, x2
, BINOP_SUB
);
994 x1
= scalar_binop (arg1_real
, arg2_imag
, op
);
995 x2
= scalar_binop (arg1_imag
, arg2_real
, op
);
996 result_imag
= scalar_binop (x1
, x2
, BINOP_ADD
);
1002 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1004 struct value
*conjugate
= value_complement (arg2
);
1005 /* We have to reconstruct ARG1, in case the type was
1007 arg1
= value_literal_complex (arg1_real
, arg1_imag
, result_type
);
1009 struct value
*numerator
= scalar_binop (arg1
, conjugate
,
1011 arg1_real
= value_real_part (numerator
);
1012 arg1_imag
= value_imaginary_part (numerator
);
1014 struct value
*x1
= scalar_binop (arg2_real
, arg2_real
, BINOP_MUL
);
1015 struct value
*x2
= scalar_binop (arg2_imag
, arg2_imag
, BINOP_MUL
);
1016 arg2_real
= scalar_binop (x1
, x2
, BINOP_ADD
);
1019 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1020 result_imag
= scalar_binop (arg1_imag
, arg2_real
, op
);
1025 case BINOP_NOTEQUAL
:
1027 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1028 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1030 LONGEST v1
= value_as_long (x1
);
1031 LONGEST v2
= value_as_long (x2
);
1033 if (op
== BINOP_EQUAL
)
1038 return value_from_longest (x1
->type (), v1
);
1043 error (_("Invalid binary operation on numbers."));
1046 return value_literal_complex (result_real
, result_imag
, result_type
);
1049 /* Return the type's length in bits. */
1052 type_length_bits (type
*type
)
1054 int unit_size
= gdbarch_addressable_memory_unit_size (type
->arch ());
1055 return unit_size
* 8 * type
->length ();
1058 /* Check whether the RHS value of a shift is valid in C/C++ semantics.
1059 SHIFT_COUNT is the shift amount, SHIFT_COUNT_TYPE is the type of
1060 the shift count value, used to determine whether the type is
1061 signed, and RESULT_TYPE is the result type. This is used to avoid
1062 both negative and too-large shift amounts, which are undefined, and
1063 would crash a GDB built with UBSan. Depending on the current
1064 language, if the shift is not valid, this either warns and returns
1065 false, or errors out. Returns true and sets NBITS if valid. */
1068 check_valid_shift_count (enum exp_opcode op
, type
*result_type
,
1069 type
*shift_count_type
, const gdb_mpz
&shift_count
,
1070 unsigned long &nbits
)
1072 if (!shift_count_type
->is_unsigned ())
1074 LONGEST count
= shift_count
.as_integer
<LONGEST
> ();
1077 auto error_or_warning
= [] (const char *msg
)
1079 /* Shifts by a negative amount are always an error in Go. Other
1080 languages are more permissive and their compilers just warn or
1081 have modes to disable the errors. */
1082 if (current_language
->la_language
== language_go
)
1083 error (("%s"), msg
);
1085 warning (("%s"), msg
);
1088 if (op
== BINOP_RSH
)
1089 error_or_warning (_("right shift count is negative"));
1091 error_or_warning (_("left shift count is negative"));
1096 nbits
= shift_count
.as_integer
<unsigned long> ();
1097 if (nbits
>= type_length_bits (result_type
))
1099 /* In Go, shifting by large amounts is defined. Be silent and
1100 still return false, as the caller's error path does the right
1102 if (current_language
->la_language
!= language_go
)
1104 if (op
== BINOP_RSH
)
1105 warning (_("right shift count >= width of type"));
1107 warning (_("left shift count >= width of type"));
1115 /* Perform a binary operation on two operands which have reasonable
1116 representations as integers or floats. This includes booleans,
1117 characters, integers, or floats.
1118 Does not support addition and subtraction on pointers;
1119 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1121 static struct value
*
1122 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1125 struct type
*type1
, *type2
, *result_type
;
1127 arg1
= coerce_ref (arg1
);
1128 arg2
= coerce_ref (arg2
);
1130 type1
= check_typedef (arg1
->type ());
1131 type2
= check_typedef (arg2
->type ());
1133 if (type1
->code () == TYPE_CODE_COMPLEX
1134 || type2
->code () == TYPE_CODE_COMPLEX
)
1135 return complex_binop (arg1
, arg2
, op
);
1137 if ((!is_floating_value (arg1
)
1138 && !is_integral_type (type1
)
1139 && !is_fixed_point_type (type1
))
1140 || (!is_floating_value (arg2
)
1141 && !is_integral_type (type2
)
1142 && !is_fixed_point_type (type2
)))
1143 error (_("Argument to arithmetic operation not a number or boolean."));
1145 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
1146 return fixed_point_binop (arg1
, arg2
, op
);
1148 if (is_floating_type (type1
) || is_floating_type (type2
))
1150 result_type
= promotion_type (type1
, type2
);
1151 val
= value::allocate (result_type
);
1153 struct type
*eff_type_v1
, *eff_type_v2
;
1154 gdb::byte_vector v1
, v2
;
1155 v1
.resize (result_type
->length ());
1156 v2
.resize (result_type
->length ());
1158 value_args_as_target_float (arg1
, arg2
,
1159 v1
.data (), &eff_type_v1
,
1160 v2
.data (), &eff_type_v2
);
1161 target_float_binop (op
, v1
.data (), eff_type_v1
,
1162 v2
.data (), eff_type_v2
,
1163 val
->contents_raw ().data (), result_type
);
1165 else if (type1
->code () == TYPE_CODE_BOOL
1166 || type2
->code () == TYPE_CODE_BOOL
)
1168 LONGEST v1
, v2
, v
= 0;
1170 v1
= value_as_long (arg1
);
1171 v2
= value_as_long (arg2
);
1175 case BINOP_BITWISE_AND
:
1179 case BINOP_BITWISE_IOR
:
1183 case BINOP_BITWISE_XOR
:
1191 case BINOP_NOTEQUAL
:
1196 error (_("Invalid operation on booleans."));
1199 result_type
= type1
;
1201 val
= value::allocate (result_type
);
1202 store_signed_integer (val
->contents_raw ().data (),
1203 result_type
->length (),
1204 type_byte_order (result_type
),
1208 /* Integral operations here. */
1210 /* Determine type length of the result, and if the operation should
1211 be done unsigned. For exponentiation and shift operators,
1212 use the length and type of the left operand. Otherwise,
1213 use the signedness of the operand with the greater length.
1214 If both operands are of equal length, use unsigned operation
1215 if one of the operands is unsigned. */
1216 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1217 result_type
= type1
;
1219 result_type
= promotion_type (type1
, type2
);
1221 gdb_mpz v1
= value_as_mpz (arg1
);
1222 gdb_mpz v2
= value_as_mpz (arg2
);
1244 error (_("Division by zero"));
1248 v
= v1
.pow (v2
.as_integer
<unsigned long> ());
1255 error (_("Division by zero"));
1259 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1260 v1 mod 0 has a defined value, v1. */
1268 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1275 unsigned long nbits
;
1276 if (!check_valid_shift_count (op
, result_type
, type2
, v2
, nbits
))
1285 unsigned long nbits
;
1286 if (!check_valid_shift_count (op
, result_type
, type2
, v2
, nbits
))
1293 case BINOP_BITWISE_AND
:
1297 case BINOP_BITWISE_IOR
:
1301 case BINOP_BITWISE_XOR
:
1306 v
= v1
< v2
? v1
: v2
;
1310 v
= v1
> v2
? v1
: v2
;
1317 case BINOP_NOTEQUAL
:
1338 error (_("Invalid binary operation on numbers."));
1341 val
= value_from_mpz (result_type
, v
);
1347 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1348 replicating SCALAR_VALUE for each element of the vector. Only scalar
1349 types that can be cast to the type of one element of the vector are
1350 acceptable. The newly created vector value is returned upon success,
1351 otherwise an error is thrown. */
1354 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1356 /* Widen the scalar to a vector. */
1357 struct type
*eltype
, *scalar_type
;
1358 struct value
*elval
;
1359 LONGEST low_bound
, high_bound
;
1362 vector_type
= check_typedef (vector_type
);
1364 gdb_assert (vector_type
->code () == TYPE_CODE_ARRAY
1365 && vector_type
->is_vector ());
1367 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1368 error (_("Could not determine the vector bounds"));
1370 eltype
= check_typedef (vector_type
->target_type ());
1371 elval
= value_cast (eltype
, scalar_value
);
1373 scalar_type
= check_typedef (scalar_value
->type ());
1375 /* If we reduced the length of the scalar then check we didn't loose any
1377 if (eltype
->length () < scalar_type
->length ()
1378 && !value_equal (elval
, scalar_value
))
1379 error (_("conversion of scalar to vector involves truncation"));
1381 value
*val
= value::allocate (vector_type
);
1382 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1383 int elt_len
= eltype
->length ();
1385 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1386 /* Duplicate the contents of elval into the destination vector. */
1387 copy (elval
->contents_all (),
1388 val_contents
.slice (i
* elt_len
, elt_len
));
1393 /* Performs a binary operation on two vector operands by calling scalar_binop
1394 for each pair of vector components. */
1396 static struct value
*
1397 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1399 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1400 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1401 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1403 type1
= check_typedef (val1
->type ());
1404 type2
= check_typedef (val2
->type ());
1406 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1407 && type1
->is_vector ()) ? 1 : 0;
1408 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1409 && type2
->is_vector ()) ? 1 : 0;
1411 if (!t1_is_vec
|| !t2_is_vec
)
1412 error (_("Vector operations are only supported among vectors"));
1414 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1415 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1416 error (_("Could not determine the vector bounds"));
1418 eltype1
= check_typedef (type1
->target_type ());
1419 eltype2
= check_typedef (type2
->target_type ());
1420 elsize
= eltype1
->length ();
1422 if (eltype1
->code () != eltype2
->code ()
1423 || elsize
!= eltype2
->length ()
1424 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
1425 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1426 error (_("Cannot perform operation on vectors with different types"));
1428 value
*val
= value::allocate (type1
);
1429 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1430 scoped_value_mark mark
;
1431 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1433 value
*tmp
= value_binop (value_subscript (val1
, i
),
1434 value_subscript (val2
, i
), op
);
1435 copy (tmp
->contents_all (),
1436 val_contents
.slice (i
* elsize
, elsize
));
1442 /* Perform a binary operation on two operands. */
1445 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1448 struct type
*type1
= check_typedef (arg1
->type ());
1449 struct type
*type2
= check_typedef (arg2
->type ());
1450 int t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1451 && type1
->is_vector ());
1452 int t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1453 && type2
->is_vector ());
1455 if (!t1_is_vec
&& !t2_is_vec
)
1456 val
= scalar_binop (arg1
, arg2
, op
);
1457 else if (t1_is_vec
&& t2_is_vec
)
1458 val
= vector_binop (arg1
, arg2
, op
);
1461 /* Widen the scalar operand to a vector. */
1462 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1463 struct type
*t
= t1_is_vec
? type2
: type1
;
1465 if (t
->code () != TYPE_CODE_FLT
1466 && t
->code () != TYPE_CODE_DECFLOAT
1467 && !is_integral_type (t
))
1468 error (_("Argument to operation not a number or boolean."));
1470 /* Replicate the scalar value to make a vector value. */
1471 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1473 val
= vector_binop (arg1
, arg2
, op
);
1482 value_logical_not (struct value
*arg1
)
1488 arg1
= coerce_array (arg1
);
1489 type1
= check_typedef (arg1
->type ());
1491 if (is_floating_value (arg1
))
1492 return target_float_is_zero (arg1
->contents ().data (), type1
);
1494 len
= type1
->length ();
1495 p
= arg1
->contents ().data ();
1506 /* Perform a comparison on two string values (whose content are not
1507 necessarily null terminated) based on their length. */
1510 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1512 int len1
= arg1
->type ()->length ();
1513 int len2
= arg2
->type ()->length ();
1514 const gdb_byte
*s1
= arg1
->contents ().data ();
1515 const gdb_byte
*s2
= arg2
->contents ().data ();
1516 int i
, len
= len1
< len2
? len1
: len2
;
1518 for (i
= 0; i
< len
; i
++)
1522 else if (s1
[i
] > s2
[i
])
1530 else if (len1
> len2
)
1536 /* Simulate the C operator == by returning a 1
1537 iff ARG1 and ARG2 have equal contents. */
1540 value_equal (struct value
*arg1
, struct value
*arg2
)
1545 struct type
*type1
, *type2
;
1546 enum type_code code1
;
1547 enum type_code code2
;
1548 int is_int1
, is_int2
;
1550 arg1
= coerce_array (arg1
);
1551 arg2
= coerce_array (arg2
);
1553 type1
= check_typedef (arg1
->type ());
1554 type2
= check_typedef (arg2
->type ());
1555 code1
= type1
->code ();
1556 code2
= type2
->code ();
1557 is_int1
= is_integral_type (type1
);
1558 is_int2
= is_integral_type (type2
);
1560 if (is_int1
&& is_int2
)
1561 return value_true (value_binop (arg1
, arg2
, BINOP_EQUAL
));
1562 else if ((is_floating_value (arg1
) || is_int1
)
1563 && (is_floating_value (arg2
) || is_int2
))
1565 struct type
*eff_type_v1
, *eff_type_v2
;
1566 gdb::byte_vector v1
, v2
;
1567 v1
.resize (std::max (type1
->length (), type2
->length ()));
1568 v2
.resize (std::max (type1
->length (), type2
->length ()));
1570 value_args_as_target_float (arg1
, arg2
,
1571 v1
.data (), &eff_type_v1
,
1572 v2
.data (), &eff_type_v2
);
1574 return target_float_compare (v1
.data (), eff_type_v1
,
1575 v2
.data (), eff_type_v2
) == 0;
1578 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1580 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1581 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1582 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1583 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1585 else if (code1
== code2
1586 && ((len
= (int) type1
->length ())
1587 == (int) type2
->length ()))
1589 p1
= arg1
->contents ().data ();
1590 p2
= arg2
->contents ().data ();
1598 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1600 return value_strcmp (arg1
, arg2
) == 0;
1603 error (_("Invalid type combination in equality test."));
1606 /* Compare values based on their raw contents. Useful for arrays since
1607 value_equal coerces them to pointers, thus comparing just the address
1608 of the array instead of its contents. */
1611 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1613 struct type
*type1
, *type2
;
1615 type1
= check_typedef (arg1
->type ());
1616 type2
= check_typedef (arg2
->type ());
1618 return (type1
->code () == type2
->code ()
1619 && type1
->length () == type2
->length ()
1620 && memcmp (arg1
->contents ().data (),
1621 arg2
->contents ().data (),
1622 type1
->length ()) == 0);
1625 /* Simulate the C operator < by returning 1
1626 iff ARG1's contents are less than ARG2's. */
1629 value_less (struct value
*arg1
, struct value
*arg2
)
1631 enum type_code code1
;
1632 enum type_code code2
;
1633 struct type
*type1
, *type2
;
1634 int is_int1
, is_int2
;
1636 arg1
= coerce_array (arg1
);
1637 arg2
= coerce_array (arg2
);
1639 type1
= check_typedef (arg1
->type ());
1640 type2
= check_typedef (arg2
->type ());
1641 code1
= type1
->code ();
1642 code2
= type2
->code ();
1643 is_int1
= is_integral_type (type1
);
1644 is_int2
= is_integral_type (type2
);
1646 if ((is_int1
&& is_int2
)
1647 || (is_fixed_point_type (type1
) && is_fixed_point_type (type2
)))
1648 return value_true (value_binop (arg1
, arg2
, BINOP_LESS
));
1649 else if ((is_floating_value (arg1
) || is_int1
)
1650 && (is_floating_value (arg2
) || is_int2
))
1652 struct type
*eff_type_v1
, *eff_type_v2
;
1653 gdb::byte_vector v1
, v2
;
1654 v1
.resize (std::max (type1
->length (), type2
->length ()));
1655 v2
.resize (std::max (type1
->length (), type2
->length ()));
1657 value_args_as_target_float (arg1
, arg2
,
1658 v1
.data (), &eff_type_v1
,
1659 v2
.data (), &eff_type_v2
);
1661 return target_float_compare (v1
.data (), eff_type_v1
,
1662 v2
.data (), eff_type_v2
) == -1;
1664 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1665 return value_as_address (arg1
) < value_as_address (arg2
);
1667 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1669 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1670 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1671 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1672 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1673 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1674 return value_strcmp (arg1
, arg2
) < 0;
1677 error (_("Invalid type combination in ordering comparison."));
1685 value_pos (struct value
*arg1
)
1689 arg1
= coerce_ref (arg1
);
1690 type
= check_typedef (arg1
->type ());
1692 if (is_integral_type (type
) || is_floating_value (arg1
)
1693 || (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1694 || type
->code () == TYPE_CODE_COMPLEX
)
1695 return value_from_contents (type
, arg1
->contents ().data ());
1697 error (_("Argument to positive operation not a number."));
1703 value_neg (struct value
*arg1
)
1707 arg1
= coerce_ref (arg1
);
1708 type
= check_typedef (arg1
->type ());
1710 if (is_integral_type (type
) || is_floating_type (type
))
1711 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1712 else if (is_fixed_point_type (type
))
1713 return value_binop (value::zero (type
, not_lval
), arg1
, BINOP_SUB
);
1714 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1716 struct value
*val
= value::allocate (type
);
1717 struct type
*eltype
= check_typedef (type
->target_type ());
1719 LONGEST low_bound
, high_bound
;
1721 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1722 error (_("Could not determine the vector bounds"));
1724 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1725 int elt_len
= eltype
->length ();
1727 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1729 value
*tmp
= value_neg (value_subscript (arg1
, i
));
1730 copy (tmp
->contents_all (),
1731 val_contents
.slice (i
* elt_len
, elt_len
));
1735 else if (type
->code () == TYPE_CODE_COMPLEX
)
1737 struct value
*real
= value_real_part (arg1
);
1738 struct value
*imag
= value_imaginary_part (arg1
);
1740 real
= value_neg (real
);
1741 imag
= value_neg (imag
);
1742 return value_literal_complex (real
, imag
, type
);
1745 error (_("Argument to negate operation not a number."));
1751 value_complement (struct value
*arg1
)
1756 arg1
= coerce_ref (arg1
);
1757 type
= check_typedef (arg1
->type ());
1759 if (is_integral_type (type
))
1761 gdb_mpz num
= value_as_mpz (arg1
);
1763 val
= value_from_mpz (type
, num
);
1765 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1767 struct type
*eltype
= check_typedef (type
->target_type ());
1769 LONGEST low_bound
, high_bound
;
1771 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1772 error (_("Could not determine the vector bounds"));
1774 val
= value::allocate (type
);
1775 gdb::array_view
<gdb_byte
> val_contents
= val
->contents_writeable ();
1776 int elt_len
= eltype
->length ();
1778 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1780 value
*tmp
= value_complement (value_subscript (arg1
, i
));
1781 copy (tmp
->contents_all (),
1782 val_contents
.slice (i
* elt_len
, elt_len
));
1785 else if (type
->code () == TYPE_CODE_COMPLEX
)
1787 /* GCC has an extension that treats ~complex as the complex
1789 struct value
*real
= value_real_part (arg1
);
1790 struct value
*imag
= value_imaginary_part (arg1
);
1792 imag
= value_neg (imag
);
1793 return value_literal_complex (real
, imag
, type
);
1796 error (_("Argument to complement operation not an integer, boolean."));
1801 /* The INDEX'th bit of SET value whose value_type is TYPE,
1802 and whose value_contents is valaddr.
1803 Return -1 if out of range, -2 other error. */
1806 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1808 struct gdbarch
*gdbarch
= type
->arch ();
1809 LONGEST low_bound
, high_bound
;
1812 struct type
*range
= type
->index_type ();
1814 if (!get_discrete_bounds (range
, &low_bound
, &high_bound
))
1816 if (index
< low_bound
|| index
> high_bound
)
1818 rel_index
= index
- low_bound
;
1819 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1820 type_byte_order (type
));
1821 rel_index
%= TARGET_CHAR_BIT
;
1822 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
1823 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1824 return (word
>> rel_index
) & 1;