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 LONGEST lowerbound
, upperbound
;
155 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
156 if (VALUE_LVAL (array
) != lval_memory
)
157 return value_subscripted_rvalue (array
, index
, lowerbound
);
161 if (index
>= lowerbound
&& index
<= upperbound
)
162 return value_subscripted_rvalue (array
, index
, lowerbound
);
163 /* Emit warning unless we have an array of unknown size.
164 An array of unknown size has lowerbound 0 and upperbound -1. */
166 warning (_("array or string index out of range"));
167 /* fall doing C stuff */
172 array
= value_coerce_array (array
);
176 return value_ind (value_ptradd (array
, index
));
178 error (_("not an array or string"));
181 /* Return the value of EXPR[IDX], expr an aggregate rvalue
182 (eg, a vector register). This routine used to promote floats
183 to doubles, but no longer does. */
186 value_subscripted_rvalue (struct value
*array
, LONGEST index
, LONGEST lowerbound
)
188 struct type
*array_type
= check_typedef (value_type (array
));
189 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
190 LONGEST elt_size
= type_length_units (elt_type
);
192 /* Fetch the bit stride and convert it to a byte stride, assuming 8 bits
194 LONGEST stride
= array_type
->bit_stride ();
197 struct gdbarch
*arch
= get_type_arch (elt_type
);
198 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
199 elt_size
= stride
/ (unit_size
* 8);
202 LONGEST elt_offs
= elt_size
* (index
- lowerbound
);
203 bool array_upper_bound_undefined
204 = array_type
->bounds ()->high
.kind () == PROP_UNDEFINED
;
206 if (index
< lowerbound
207 || (!array_upper_bound_undefined
208 && elt_offs
>= type_length_units (array_type
))
209 || (VALUE_LVAL (array
) != lval_memory
&& array_upper_bound_undefined
))
211 if (type_not_associated (array_type
))
212 error (_("no such vector element (vector not associated)"));
213 else if (type_not_allocated (array_type
))
214 error (_("no such vector element (vector not allocated)"));
216 error (_("no such vector element"));
219 if (is_dynamic_type (elt_type
))
223 address
= value_address (array
) + elt_offs
;
224 elt_type
= resolve_dynamic_type (elt_type
, {}, address
);
227 return value_from_component (array
, elt_type
, elt_offs
);
231 /* Check to see if either argument is a structure, or a reference to
232 one. This is called so we know whether to go ahead with the normal
233 binop or look for a user defined function instead.
235 For now, we do not overload the `=' operator. */
238 binop_types_user_defined_p (enum exp_opcode op
,
239 struct type
*type1
, struct type
*type2
)
241 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
244 type1
= check_typedef (type1
);
245 if (TYPE_IS_REFERENCE (type1
))
246 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
248 type2
= check_typedef (type2
);
249 if (TYPE_IS_REFERENCE (type2
))
250 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
252 return (type1
->code () == TYPE_CODE_STRUCT
253 || type2
->code () == TYPE_CODE_STRUCT
);
256 /* Check to see if either argument is a structure, or a reference to
257 one. This is called so we know whether to go ahead with the normal
258 binop or look for a user defined function instead.
260 For now, we do not overload the `=' operator. */
263 binop_user_defined_p (enum exp_opcode op
,
264 struct value
*arg1
, struct value
*arg2
)
266 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
269 /* Check to see if argument is a structure. This is called so
270 we know whether to go ahead with the normal unop or look for a
271 user defined function instead.
273 For now, we do not overload the `&' operator. */
276 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
282 type1
= check_typedef (value_type (arg1
));
283 if (TYPE_IS_REFERENCE (type1
))
284 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
285 return type1
->code () == TYPE_CODE_STRUCT
;
288 /* Try to find an operator named OPERATOR which takes NARGS arguments
289 specified in ARGS. If the operator found is a static member operator
290 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
291 The search if performed through find_overload_match which will handle
292 member operators, non member operators, operators imported implicitly or
293 explicitly, and perform correct overload resolution in all of the above
294 situations or combinations thereof. */
296 static struct value
*
297 value_user_defined_cpp_op (gdb::array_view
<value
*> args
, char *oper
,
298 int *static_memfuncp
, enum noside noside
)
301 struct symbol
*symp
= NULL
;
302 struct value
*valp
= NULL
;
304 find_overload_match (args
, oper
, BOTH
/* could be method */,
306 NULL
/* pass NULL symbol since symbol is unknown */,
307 &valp
, &symp
, static_memfuncp
, 0, noside
);
314 /* This is a non member function and does not
315 expect a reference as its first argument
316 rather the explicit structure. */
317 args
[0] = value_ind (args
[0]);
318 return value_of_variable (symp
, 0);
321 error (_("Could not find %s."), oper
);
324 /* Lookup user defined operator NAME. Return a value representing the
325 function, otherwise return NULL. */
327 static struct value
*
328 value_user_defined_op (struct value
**argp
, gdb::array_view
<value
*> args
,
329 char *name
, int *static_memfuncp
, enum noside noside
)
331 struct value
*result
= NULL
;
333 if (current_language
->la_language
== language_cplus
)
335 result
= value_user_defined_cpp_op (args
, name
, static_memfuncp
,
339 result
= value_struct_elt (argp
, args
.data (), name
, static_memfuncp
,
345 /* We know either arg1 or arg2 is a structure, so try to find the right
346 user defined function. Create an argument vector that calls
347 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
348 binary operator which is legal for GNU C++).
350 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
351 is the opcode saying how to modify it. Otherwise, OTHEROP is
355 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
356 enum exp_opcode otherop
, enum noside noside
)
362 arg1
= coerce_ref (arg1
);
363 arg2
= coerce_ref (arg2
);
365 /* now we know that what we have to do is construct our
366 arg vector and find the right function to call it with. */
368 if (check_typedef (value_type (arg1
))->code () != TYPE_CODE_STRUCT
)
369 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
371 value
*argvec_storage
[3];
372 gdb::array_view
<value
*> argvec
= argvec_storage
;
374 argvec
[1] = value_addr (arg1
);
377 /* Make the right function name up. */
378 strcpy (tstr
, "operator__");
403 case BINOP_BITWISE_AND
:
406 case BINOP_BITWISE_IOR
:
409 case BINOP_BITWISE_XOR
:
412 case BINOP_LOGICAL_AND
:
415 case BINOP_LOGICAL_OR
:
427 case BINOP_ASSIGN_MODIFY
:
445 case BINOP_BITWISE_AND
:
448 case BINOP_BITWISE_IOR
:
451 case BINOP_BITWISE_XOR
:
454 case BINOP_MOD
: /* invalid */
456 error (_("Invalid binary operation specified."));
459 case BINOP_SUBSCRIPT
:
480 case BINOP_MOD
: /* invalid */
482 error (_("Invalid binary operation specified."));
485 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1), tstr
,
486 &static_memfuncp
, noside
);
492 argvec
[1] = argvec
[0];
493 argvec
= argvec
.slice (1);
495 if (value_type (argvec
[0])->code () == TYPE_CODE_XMETHOD
)
497 /* Static xmethods are not supported yet. */
498 gdb_assert (static_memfuncp
== 0);
499 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
501 struct type
*return_type
502 = result_type_of_xmethod (argvec
[0], argvec
.slice (1));
504 if (return_type
== NULL
)
505 error (_("Xmethod is missing return type."));
506 return value_zero (return_type
, VALUE_LVAL (arg1
));
508 return call_xmethod (argvec
[0], argvec
.slice (1));
510 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
512 struct type
*return_type
;
515 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
516 return value_zero (return_type
, VALUE_LVAL (arg1
));
518 return call_function_by_hand (argvec
[0], NULL
,
519 argvec
.slice (1, 2 - static_memfuncp
));
521 throw_error (NOT_FOUND_ERROR
,
522 _("member function %s not found"), tstr
);
525 /* We know that arg1 is a structure, so try to find a unary user
526 defined operator that matches the operator in question.
527 Create an argument vector that calls arg1.operator @ (arg1)
528 and return that value (where '@' is (almost) any unary operator which
529 is legal for GNU C++). */
532 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
534 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
536 char tstr
[13], mangle_tstr
[13];
537 int static_memfuncp
, nargs
;
539 arg1
= coerce_ref (arg1
);
541 /* now we know that what we have to do is construct our
542 arg vector and find the right function to call it with. */
544 if (check_typedef (value_type (arg1
))->code () != TYPE_CODE_STRUCT
)
545 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
547 value
*argvec_storage
[3];
548 gdb::array_view
<value
*> argvec
= argvec_storage
;
550 argvec
[1] = value_addr (arg1
);
555 /* Make the right function name up. */
556 strcpy (tstr
, "operator__");
558 strcpy (mangle_tstr
, "__");
561 case UNOP_PREINCREMENT
:
564 case UNOP_PREDECREMENT
:
567 case UNOP_POSTINCREMENT
:
569 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
572 case UNOP_POSTDECREMENT
:
574 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
577 case UNOP_LOGICAL_NOT
:
580 case UNOP_COMPLEMENT
:
596 error (_("Invalid unary operation specified."));
599 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1, nargs
), tstr
,
600 &static_memfuncp
, noside
);
606 argvec
[1] = argvec
[0];
607 argvec
= argvec
.slice (1);
609 if (value_type (argvec
[0])->code () == TYPE_CODE_XMETHOD
)
611 /* Static xmethods are not supported yet. */
612 gdb_assert (static_memfuncp
== 0);
613 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
615 struct type
*return_type
616 = result_type_of_xmethod (argvec
[0], argvec
[1]);
618 if (return_type
== NULL
)
619 error (_("Xmethod is missing return type."));
620 return value_zero (return_type
, VALUE_LVAL (arg1
));
622 return call_xmethod (argvec
[0], argvec
[1]);
624 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
626 struct type
*return_type
;
629 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
630 return value_zero (return_type
, VALUE_LVAL (arg1
));
632 return call_function_by_hand (argvec
[0], NULL
,
633 argvec
.slice (1, nargs
));
635 throw_error (NOT_FOUND_ERROR
,
636 _("member function %s not found"), tstr
);
640 /* Concatenate two values with the following conditions:
642 (1) Both values must be either bitstring values or character string
643 values and the resulting value consists of the concatenation of
644 ARG1 followed by ARG2.
648 One value must be an integer value and the other value must be
649 either a bitstring value or character string value, which is
650 to be repeated by the number of times specified by the integer
654 (2) Boolean values are also allowed and are treated as bit string
657 (3) Character values are also allowed and are treated as character
658 string values of length 1. */
661 value_concat (struct value
*arg1
, struct value
*arg2
)
663 struct value
*inval1
;
664 struct value
*inval2
;
665 struct value
*outval
= NULL
;
666 int inval1len
, inval2len
;
669 struct type
*type1
= check_typedef (value_type (arg1
));
670 struct type
*type2
= check_typedef (value_type (arg2
));
671 struct type
*char_type
;
673 /* First figure out if we are dealing with two values to be concatenated
674 or a repeat count and a value to be repeated. INVAL1 is set to the
675 first of two concatenated values, or the repeat count. INVAL2 is set
676 to the second of the two concatenated values or the value to be
679 if (type2
->code () == TYPE_CODE_INT
)
681 struct type
*tmp
= type1
;
694 /* Now process the input values. */
696 if (type1
->code () == TYPE_CODE_INT
)
698 /* We have a repeat count. Validate the second value and then
699 construct a value repeated that many times. */
700 if (type2
->code () == TYPE_CODE_STRING
701 || type2
->code () == TYPE_CODE_CHAR
)
703 count
= longest_to_int (value_as_long (inval1
));
704 inval2len
= TYPE_LENGTH (type2
);
705 std::vector
<char> ptr (count
* inval2len
);
706 if (type2
->code () == TYPE_CODE_CHAR
)
710 inchar
= (char) unpack_long (type2
,
711 value_contents (inval2
));
712 for (idx
= 0; idx
< count
; idx
++)
719 char_type
= TYPE_TARGET_TYPE (type2
);
721 for (idx
= 0; idx
< count
; idx
++)
723 memcpy (&ptr
[idx
* inval2len
], value_contents (inval2
),
727 outval
= value_string (ptr
.data (), count
* inval2len
, char_type
);
729 else if (type2
->code () == TYPE_CODE_BOOL
)
731 error (_("unimplemented support for boolean repeats"));
735 error (_("can't repeat values of that type"));
738 else if (type1
->code () == TYPE_CODE_STRING
739 || type1
->code () == TYPE_CODE_CHAR
)
741 /* We have two character strings to concatenate. */
742 if (type2
->code () != TYPE_CODE_STRING
743 && type2
->code () != TYPE_CODE_CHAR
)
745 error (_("Strings can only be concatenated with other strings."));
747 inval1len
= TYPE_LENGTH (type1
);
748 inval2len
= TYPE_LENGTH (type2
);
749 std::vector
<char> ptr (inval1len
+ inval2len
);
750 if (type1
->code () == TYPE_CODE_CHAR
)
754 ptr
[0] = (char) unpack_long (type1
, value_contents (inval1
));
758 char_type
= TYPE_TARGET_TYPE (type1
);
760 memcpy (ptr
.data (), value_contents (inval1
), inval1len
);
762 if (type2
->code () == TYPE_CODE_CHAR
)
765 (char) unpack_long (type2
, value_contents (inval2
));
769 memcpy (&ptr
[inval1len
], value_contents (inval2
), inval2len
);
771 outval
= value_string (ptr
.data (), inval1len
+ inval2len
, char_type
);
773 else if (type1
->code () == TYPE_CODE_BOOL
)
775 /* We have two bitstrings to concatenate. */
776 if (type2
->code () != TYPE_CODE_BOOL
)
778 error (_("Booleans can only be concatenated "
779 "with other bitstrings or booleans."));
781 error (_("unimplemented support for boolean concatenation."));
785 /* We don't know how to concatenate these operands. */
786 error (_("illegal operands for concatenation."));
791 /* Integer exponentiation: V1**V2, where both arguments are
792 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
795 integer_pow (LONGEST v1
, LONGEST v2
)
800 error (_("Attempt to raise 0 to negative power."));
806 /* The Russian Peasant's Algorithm. */
822 /* Obtain argument values for binary operation, converting from
823 other types if one of them is not floating point. */
825 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
826 gdb_byte
*x
, struct type
**eff_type_x
,
827 gdb_byte
*y
, struct type
**eff_type_y
)
829 struct type
*type1
, *type2
;
831 type1
= check_typedef (value_type (arg1
));
832 type2
= check_typedef (value_type (arg2
));
834 /* At least one of the arguments must be of floating-point type. */
835 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
837 if (is_floating_type (type1
) && is_floating_type (type2
)
838 && type1
->code () != type2
->code ())
839 /* The DFP extension to the C language does not allow mixing of
840 * decimal float types with other float types in expressions
841 * (see WDTR 24732, page 12). */
842 error (_("Mixing decimal floating types with "
843 "other floating types is not allowed."));
845 /* Obtain value of arg1, converting from other types if necessary. */
847 if (is_floating_type (type1
))
850 memcpy (x
, value_contents (arg1
), TYPE_LENGTH (type1
));
852 else if (is_integral_type (type1
))
855 if (type1
->is_unsigned ())
856 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
858 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
861 error (_("Don't know how to convert from %s to %s."), type1
->name (),
864 /* Obtain value of arg2, converting from other types if necessary. */
866 if (is_floating_type (type2
))
869 memcpy (y
, value_contents (arg2
), TYPE_LENGTH (type2
));
871 else if (is_integral_type (type2
))
874 if (type2
->is_unsigned ())
875 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
877 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
880 error (_("Don't know how to convert from %s to %s."), type1
->name (),
884 /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
885 perform the binary operation OP on these two operands, and return
886 the resulting value (also as a fixed point). */
888 static struct value
*
889 fixed_point_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
891 struct type
*type1
= check_typedef (value_type (arg1
));
892 struct type
*type2
= check_typedef (value_type (arg2
));
893 const struct language_defn
*language
= current_language
;
895 struct gdbarch
*gdbarch
= get_type_arch (type1
);
898 gdb_assert (is_fixed_point_type (type1
) || is_fixed_point_type (type2
));
899 if (!is_fixed_point_type (type1
))
901 arg1
= value_cast (type2
, arg1
);
904 if (!is_fixed_point_type (type2
))
906 arg2
= value_cast (type1
, arg2
);
911 v1
.read_fixed_point (gdb::make_array_view (value_contents (arg1
),
912 TYPE_LENGTH (type1
)),
913 type_byte_order (type1
), type1
->is_unsigned (),
914 type1
->fixed_point_scaling_factor ());
915 v2
.read_fixed_point (gdb::make_array_view (value_contents (arg2
),
916 TYPE_LENGTH (type2
)),
917 type_byte_order (type2
), type2
->is_unsigned (),
918 type2
->fixed_point_scaling_factor ());
920 auto fixed_point_to_value
= [type1
] (const gdb_mpq
&fp
)
922 value
*fp_val
= allocate_value (type1
);
925 (gdb::make_array_view (value_contents_raw (fp_val
),
926 TYPE_LENGTH (type1
)),
927 type_byte_order (type1
),
928 type1
->is_unsigned (),
929 type1
->fixed_point_scaling_factor ());
937 mpq_add (res
.val
, v1
.val
, v2
.val
);
938 val
= fixed_point_to_value (res
);
942 mpq_sub (res
.val
, v1
.val
, v2
.val
);
943 val
= fixed_point_to_value (res
);
947 val
= fixed_point_to_value (mpq_cmp (v1
.val
, v2
.val
) < 0 ? v1
: v2
);
951 val
= fixed_point_to_value (mpq_cmp (v1
.val
, v2
.val
) > 0 ? v1
: v2
);
955 mpq_mul (res
.val
, v1
.val
, v2
.val
);
956 val
= fixed_point_to_value (res
);
960 mpq_div (res
.val
, v1
.val
, v2
.val
);
961 val
= fixed_point_to_value (res
);
965 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
966 mpq_cmp (v1
.val
, v2
.val
) == 0 ? 1 : 0);
970 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
971 mpq_cmp (v1
.val
, v2
.val
) < 0 ? 1 : 0);
975 error (_("Integer-only operation on fixed point number."));
981 /* A helper function that finds the type to use for a binary operation
982 involving TYPE1 and TYPE2. */
985 promotion_type (struct type
*type1
, struct type
*type2
)
987 struct type
*result_type
;
989 if (is_floating_type (type1
) || is_floating_type (type2
))
991 /* If only one type is floating-point, use its type.
992 Otherwise use the bigger type. */
993 if (!is_floating_type (type1
))
995 else if (!is_floating_type (type2
))
997 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1000 result_type
= type1
;
1004 /* Integer types. */
1005 if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1006 result_type
= type1
;
1007 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1008 result_type
= type2
;
1009 else if (type1
->is_unsigned ())
1010 result_type
= type1
;
1011 else if (type2
->is_unsigned ())
1012 result_type
= type2
;
1014 result_type
= type1
;
1020 static struct value
*scalar_binop (struct value
*arg1
, struct value
*arg2
,
1021 enum exp_opcode op
);
1023 /* Perform a binary operation on complex operands. */
1025 static struct value
*
1026 complex_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1028 struct type
*arg1_type
= check_typedef (value_type (arg1
));
1029 struct type
*arg2_type
= check_typedef (value_type (arg2
));
1031 struct value
*arg1_real
, *arg1_imag
, *arg2_real
, *arg2_imag
;
1032 if (arg1_type
->code () == TYPE_CODE_COMPLEX
)
1034 arg1_real
= value_real_part (arg1
);
1035 arg1_imag
= value_imaginary_part (arg1
);
1040 arg1_imag
= value_zero (arg1_type
, not_lval
);
1042 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1044 arg2_real
= value_real_part (arg2
);
1045 arg2_imag
= value_imaginary_part (arg2
);
1050 arg2_imag
= value_zero (arg2_type
, not_lval
);
1053 struct type
*comp_type
= promotion_type (value_type (arg1_real
),
1054 value_type (arg2_real
));
1055 arg1_real
= value_cast (comp_type
, arg1_real
);
1056 arg1_imag
= value_cast (comp_type
, arg1_imag
);
1057 arg2_real
= value_cast (comp_type
, arg2_real
);
1058 arg2_imag
= value_cast (comp_type
, arg2_imag
);
1060 struct type
*result_type
= init_complex_type (nullptr, comp_type
);
1062 struct value
*result_real
, *result_imag
;
1067 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1068 result_imag
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1073 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1074 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1075 result_real
= scalar_binop (x1
, x2
, BINOP_SUB
);
1077 x1
= scalar_binop (arg1_real
, arg2_imag
, op
);
1078 x2
= scalar_binop (arg1_imag
, arg2_real
, op
);
1079 result_imag
= scalar_binop (x1
, x2
, BINOP_ADD
);
1085 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1087 struct value
*conjugate
= value_complement (arg2
);
1088 /* We have to reconstruct ARG1, in case the type was
1090 arg1
= value_literal_complex (arg1_real
, arg1_imag
, result_type
);
1092 struct value
*numerator
= scalar_binop (arg1
, conjugate
,
1094 arg1_real
= value_real_part (numerator
);
1095 arg1_imag
= value_imaginary_part (numerator
);
1097 struct value
*x1
= scalar_binop (arg2_real
, arg2_real
, BINOP_MUL
);
1098 struct value
*x2
= scalar_binop (arg2_imag
, arg2_imag
, BINOP_MUL
);
1099 arg2_real
= scalar_binop (x1
, x2
, BINOP_ADD
);
1102 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1103 result_imag
= scalar_binop (arg1_imag
, arg2_real
, op
);
1108 case BINOP_NOTEQUAL
:
1110 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1111 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1113 LONGEST v1
= value_as_long (x1
);
1114 LONGEST v2
= value_as_long (x2
);
1116 if (op
== BINOP_EQUAL
)
1121 return value_from_longest (value_type (x1
), v1
);
1126 error (_("Invalid binary operation on numbers."));
1129 return value_literal_complex (result_real
, result_imag
, result_type
);
1132 /* Perform a binary operation on two operands which have reasonable
1133 representations as integers or floats. This includes booleans,
1134 characters, integers, or floats.
1135 Does not support addition and subtraction on pointers;
1136 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1138 static struct value
*
1139 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1142 struct type
*type1
, *type2
, *result_type
;
1144 arg1
= coerce_ref (arg1
);
1145 arg2
= coerce_ref (arg2
);
1147 type1
= check_typedef (value_type (arg1
));
1148 type2
= check_typedef (value_type (arg2
));
1150 if (type1
->code () == TYPE_CODE_COMPLEX
1151 || type2
->code () == TYPE_CODE_COMPLEX
)
1152 return complex_binop (arg1
, arg2
, op
);
1154 if ((!is_floating_value (arg1
)
1155 && !is_integral_type (type1
)
1156 && !is_fixed_point_type (type1
))
1157 || (!is_floating_value (arg2
)
1158 && !is_integral_type (type2
)
1159 && !is_fixed_point_type (type2
)))
1160 error (_("Argument to arithmetic operation not a number or boolean."));
1162 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
1163 return fixed_point_binop (arg1
, arg2
, op
);
1165 if (is_floating_type (type1
) || is_floating_type (type2
))
1167 result_type
= promotion_type (type1
, type2
);
1168 val
= allocate_value (result_type
);
1170 struct type
*eff_type_v1
, *eff_type_v2
;
1171 gdb::byte_vector v1
, v2
;
1172 v1
.resize (TYPE_LENGTH (result_type
));
1173 v2
.resize (TYPE_LENGTH (result_type
));
1175 value_args_as_target_float (arg1
, arg2
,
1176 v1
.data (), &eff_type_v1
,
1177 v2
.data (), &eff_type_v2
);
1178 target_float_binop (op
, v1
.data (), eff_type_v1
,
1179 v2
.data (), eff_type_v2
,
1180 value_contents_raw (val
), result_type
);
1182 else if (type1
->code () == TYPE_CODE_BOOL
1183 || type2
->code () == TYPE_CODE_BOOL
)
1185 LONGEST v1
, v2
, v
= 0;
1187 v1
= value_as_long (arg1
);
1188 v2
= value_as_long (arg2
);
1192 case BINOP_BITWISE_AND
:
1196 case BINOP_BITWISE_IOR
:
1200 case BINOP_BITWISE_XOR
:
1208 case BINOP_NOTEQUAL
:
1213 error (_("Invalid operation on booleans."));
1216 result_type
= type1
;
1218 val
= allocate_value (result_type
);
1219 store_signed_integer (value_contents_raw (val
),
1220 TYPE_LENGTH (result_type
),
1221 type_byte_order (result_type
),
1225 /* Integral operations here. */
1227 /* Determine type length of the result, and if the operation should
1228 be done unsigned. For exponentiation and shift operators,
1229 use the length and type of the left operand. Otherwise,
1230 use the signedness of the operand with the greater length.
1231 If both operands are of equal length, use unsigned operation
1232 if one of the operands is unsigned. */
1233 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1234 result_type
= type1
;
1236 result_type
= promotion_type (type1
, type2
);
1238 if (result_type
->is_unsigned ())
1240 LONGEST v2_signed
= value_as_long (arg2
);
1241 ULONGEST v1
, v2
, v
= 0;
1243 v1
= (ULONGEST
) value_as_long (arg1
);
1244 v2
= (ULONGEST
) v2_signed
;
1265 error (_("Division by zero"));
1269 v
= uinteger_pow (v1
, v2_signed
);
1276 error (_("Division by zero"));
1280 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1281 v1 mod 0 has a defined value, v1. */
1289 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1302 case BINOP_BITWISE_AND
:
1306 case BINOP_BITWISE_IOR
:
1310 case BINOP_BITWISE_XOR
:
1314 case BINOP_LOGICAL_AND
:
1318 case BINOP_LOGICAL_OR
:
1323 v
= v1
< v2
? v1
: v2
;
1327 v
= v1
> v2
? v1
: v2
;
1334 case BINOP_NOTEQUAL
:
1355 error (_("Invalid binary operation on numbers."));
1358 val
= allocate_value (result_type
);
1359 store_unsigned_integer (value_contents_raw (val
),
1360 TYPE_LENGTH (value_type (val
)),
1361 type_byte_order (result_type
),
1366 LONGEST v1
, v2
, v
= 0;
1368 v1
= value_as_long (arg1
);
1369 v2
= value_as_long (arg2
);
1390 error (_("Division by zero"));
1394 v
= integer_pow (v1
, v2
);
1401 error (_("Division by zero"));
1405 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1406 X mod 0 has a defined value, X. */
1414 /* Compute floor. */
1415 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1431 case BINOP_BITWISE_AND
:
1435 case BINOP_BITWISE_IOR
:
1439 case BINOP_BITWISE_XOR
:
1443 case BINOP_LOGICAL_AND
:
1447 case BINOP_LOGICAL_OR
:
1452 v
= v1
< v2
? v1
: v2
;
1456 v
= v1
> v2
? v1
: v2
;
1463 case BINOP_NOTEQUAL
:
1484 error (_("Invalid binary operation on numbers."));
1487 val
= allocate_value (result_type
);
1488 store_signed_integer (value_contents_raw (val
),
1489 TYPE_LENGTH (value_type (val
)),
1490 type_byte_order (result_type
),
1498 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1499 replicating SCALAR_VALUE for each element of the vector. Only scalar
1500 types that can be cast to the type of one element of the vector are
1501 acceptable. The newly created vector value is returned upon success,
1502 otherwise an error is thrown. */
1505 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1507 /* Widen the scalar to a vector. */
1508 struct type
*eltype
, *scalar_type
;
1509 struct value
*val
, *elval
;
1510 LONGEST low_bound
, high_bound
;
1513 vector_type
= check_typedef (vector_type
);
1515 gdb_assert (vector_type
->code () == TYPE_CODE_ARRAY
1516 && vector_type
->is_vector ());
1518 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1519 error (_("Could not determine the vector bounds"));
1521 eltype
= check_typedef (TYPE_TARGET_TYPE (vector_type
));
1522 elval
= value_cast (eltype
, scalar_value
);
1524 scalar_type
= check_typedef (value_type (scalar_value
));
1526 /* If we reduced the length of the scalar then check we didn't loose any
1528 if (TYPE_LENGTH (eltype
) < TYPE_LENGTH (scalar_type
)
1529 && !value_equal (elval
, scalar_value
))
1530 error (_("conversion of scalar to vector involves truncation"));
1532 val
= allocate_value (vector_type
);
1533 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1534 /* Duplicate the contents of elval into the destination vector. */
1535 memcpy (value_contents_writeable (val
) + (i
* TYPE_LENGTH (eltype
)),
1536 value_contents_all (elval
), TYPE_LENGTH (eltype
));
1541 /* Performs a binary operation on two vector operands by calling scalar_binop
1542 for each pair of vector components. */
1544 static struct value
*
1545 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1547 struct value
*val
, *tmp
, *mark
;
1548 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1549 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1550 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1552 type1
= check_typedef (value_type (val1
));
1553 type2
= check_typedef (value_type (val2
));
1555 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1556 && type1
->is_vector ()) ? 1 : 0;
1557 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1558 && type2
->is_vector ()) ? 1 : 0;
1560 if (!t1_is_vec
|| !t2_is_vec
)
1561 error (_("Vector operations are only supported among vectors"));
1563 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1564 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1565 error (_("Could not determine the vector bounds"));
1567 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1568 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1569 elsize
= TYPE_LENGTH (eltype1
);
1571 if (eltype1
->code () != eltype2
->code ()
1572 || elsize
!= TYPE_LENGTH (eltype2
)
1573 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
1574 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1575 error (_("Cannot perform operation on vectors with different types"));
1577 val
= allocate_value (type1
);
1578 mark
= value_mark ();
1579 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1581 tmp
= value_binop (value_subscript (val1
, i
),
1582 value_subscript (val2
, i
), op
);
1583 memcpy (value_contents_writeable (val
) + i
* elsize
,
1584 value_contents_all (tmp
),
1587 value_free_to_mark (mark
);
1592 /* Perform a binary operation on two operands. */
1595 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1598 struct type
*type1
= check_typedef (value_type (arg1
));
1599 struct type
*type2
= check_typedef (value_type (arg2
));
1600 int t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1601 && type1
->is_vector ());
1602 int t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1603 && type2
->is_vector ());
1605 if (!t1_is_vec
&& !t2_is_vec
)
1606 val
= scalar_binop (arg1
, arg2
, op
);
1607 else if (t1_is_vec
&& t2_is_vec
)
1608 val
= vector_binop (arg1
, arg2
, op
);
1611 /* Widen the scalar operand to a vector. */
1612 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1613 struct type
*t
= t1_is_vec
? type2
: type1
;
1615 if (t
->code () != TYPE_CODE_FLT
1616 && t
->code () != TYPE_CODE_DECFLOAT
1617 && !is_integral_type (t
))
1618 error (_("Argument to operation not a number or boolean."));
1620 /* Replicate the scalar value to make a vector value. */
1621 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1623 val
= vector_binop (arg1
, arg2
, op
);
1629 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1632 value_logical_not (struct value
*arg1
)
1638 arg1
= coerce_array (arg1
);
1639 type1
= check_typedef (value_type (arg1
));
1641 if (is_floating_value (arg1
))
1642 return target_float_is_zero (value_contents (arg1
), type1
);
1644 len
= TYPE_LENGTH (type1
);
1645 p
= value_contents (arg1
);
1656 /* Perform a comparison on two string values (whose content are not
1657 necessarily null terminated) based on their length. */
1660 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1662 int len1
= TYPE_LENGTH (value_type (arg1
));
1663 int len2
= TYPE_LENGTH (value_type (arg2
));
1664 const gdb_byte
*s1
= value_contents (arg1
);
1665 const gdb_byte
*s2
= value_contents (arg2
);
1666 int i
, len
= len1
< len2
? len1
: len2
;
1668 for (i
= 0; i
< len
; i
++)
1672 else if (s1
[i
] > s2
[i
])
1680 else if (len1
> len2
)
1686 /* Simulate the C operator == by returning a 1
1687 iff ARG1 and ARG2 have equal contents. */
1690 value_equal (struct value
*arg1
, struct value
*arg2
)
1695 struct type
*type1
, *type2
;
1696 enum type_code code1
;
1697 enum type_code code2
;
1698 int is_int1
, is_int2
;
1700 arg1
= coerce_array (arg1
);
1701 arg2
= coerce_array (arg2
);
1703 type1
= check_typedef (value_type (arg1
));
1704 type2
= check_typedef (value_type (arg2
));
1705 code1
= type1
->code ();
1706 code2
= type2
->code ();
1707 is_int1
= is_integral_type (type1
);
1708 is_int2
= is_integral_type (type2
);
1710 if (is_int1
&& is_int2
)
1711 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1713 else if ((is_floating_value (arg1
) || is_int1
)
1714 && (is_floating_value (arg2
) || is_int2
))
1716 struct type
*eff_type_v1
, *eff_type_v2
;
1717 gdb::byte_vector v1
, v2
;
1718 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1719 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1721 value_args_as_target_float (arg1
, arg2
,
1722 v1
.data (), &eff_type_v1
,
1723 v2
.data (), &eff_type_v2
);
1725 return target_float_compare (v1
.data (), eff_type_v1
,
1726 v2
.data (), eff_type_v2
) == 0;
1729 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1731 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1732 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1733 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1734 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1736 else if (code1
== code2
1737 && ((len
= (int) TYPE_LENGTH (type1
))
1738 == (int) TYPE_LENGTH (type2
)))
1740 p1
= value_contents (arg1
);
1741 p2
= value_contents (arg2
);
1749 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1751 return value_strcmp (arg1
, arg2
) == 0;
1754 error (_("Invalid type combination in equality test."));
1757 /* Compare values based on their raw contents. Useful for arrays since
1758 value_equal coerces them to pointers, thus comparing just the address
1759 of the array instead of its contents. */
1762 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1764 struct type
*type1
, *type2
;
1766 type1
= check_typedef (value_type (arg1
));
1767 type2
= check_typedef (value_type (arg2
));
1769 return (type1
->code () == type2
->code ()
1770 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1771 && memcmp (value_contents (arg1
), value_contents (arg2
),
1772 TYPE_LENGTH (type1
)) == 0);
1775 /* Simulate the C operator < by returning 1
1776 iff ARG1's contents are less than ARG2's. */
1779 value_less (struct value
*arg1
, struct value
*arg2
)
1781 enum type_code code1
;
1782 enum type_code code2
;
1783 struct type
*type1
, *type2
;
1784 int is_int1
, is_int2
;
1786 arg1
= coerce_array (arg1
);
1787 arg2
= coerce_array (arg2
);
1789 type1
= check_typedef (value_type (arg1
));
1790 type2
= check_typedef (value_type (arg2
));
1791 code1
= type1
->code ();
1792 code2
= type2
->code ();
1793 is_int1
= is_integral_type (type1
);
1794 is_int2
= is_integral_type (type2
);
1796 if ((is_int1
&& is_int2
)
1797 || (is_fixed_point_type (type1
) && is_fixed_point_type (type2
)))
1798 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1800 else if ((is_floating_value (arg1
) || is_int1
)
1801 && (is_floating_value (arg2
) || is_int2
))
1803 struct type
*eff_type_v1
, *eff_type_v2
;
1804 gdb::byte_vector v1
, v2
;
1805 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1806 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1808 value_args_as_target_float (arg1
, arg2
,
1809 v1
.data (), &eff_type_v1
,
1810 v2
.data (), &eff_type_v2
);
1812 return target_float_compare (v1
.data (), eff_type_v1
,
1813 v2
.data (), eff_type_v2
) == -1;
1815 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1816 return value_as_address (arg1
) < value_as_address (arg2
);
1818 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1820 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1821 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1822 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1823 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1824 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1825 return value_strcmp (arg1
, arg2
) < 0;
1828 error (_("Invalid type combination in ordering comparison."));
1833 /* The unary operators +, - and ~. They free the argument ARG1. */
1836 value_pos (struct value
*arg1
)
1840 arg1
= coerce_ref (arg1
);
1841 type
= check_typedef (value_type (arg1
));
1843 if (is_integral_type (type
) || is_floating_value (arg1
)
1844 || (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1845 || type
->code () == TYPE_CODE_COMPLEX
)
1846 return value_from_contents (type
, value_contents (arg1
));
1848 error (_("Argument to positive operation not a number."));
1852 value_neg (struct value
*arg1
)
1856 arg1
= coerce_ref (arg1
);
1857 type
= check_typedef (value_type (arg1
));
1859 if (is_integral_type (type
) || is_floating_type (type
))
1860 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1861 else if (is_fixed_point_type (type
))
1862 return value_binop (value_zero (type
, not_lval
), arg1
, BINOP_SUB
);
1863 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1865 struct value
*tmp
, *val
= allocate_value (type
);
1866 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1868 LONGEST low_bound
, high_bound
;
1870 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1871 error (_("Could not determine the vector bounds"));
1873 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1875 tmp
= value_neg (value_subscript (arg1
, i
));
1876 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1877 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1881 else if (type
->code () == TYPE_CODE_COMPLEX
)
1883 struct value
*real
= value_real_part (arg1
);
1884 struct value
*imag
= value_imaginary_part (arg1
);
1886 real
= value_neg (real
);
1887 imag
= value_neg (imag
);
1888 return value_literal_complex (real
, imag
, type
);
1891 error (_("Argument to negate operation not a number."));
1895 value_complement (struct value
*arg1
)
1900 arg1
= coerce_ref (arg1
);
1901 type
= check_typedef (value_type (arg1
));
1903 if (is_integral_type (type
))
1904 val
= value_from_longest (type
, ~value_as_long (arg1
));
1905 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1908 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1910 LONGEST low_bound
, high_bound
;
1912 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1913 error (_("Could not determine the vector bounds"));
1915 val
= allocate_value (type
);
1916 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1918 tmp
= value_complement (value_subscript (arg1
, i
));
1919 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1920 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1923 else if (type
->code () == TYPE_CODE_COMPLEX
)
1925 /* GCC has an extension that treats ~complex as the complex
1927 struct value
*real
= value_real_part (arg1
);
1928 struct value
*imag
= value_imaginary_part (arg1
);
1930 imag
= value_neg (imag
);
1931 return value_literal_complex (real
, imag
, type
);
1934 error (_("Argument to complement operation not an integer, boolean."));
1939 /* The INDEX'th bit of SET value whose value_type is TYPE,
1940 and whose value_contents is valaddr.
1941 Return -1 if out of range, -2 other error. */
1944 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1946 struct gdbarch
*gdbarch
= get_type_arch (type
);
1947 LONGEST low_bound
, high_bound
;
1950 struct type
*range
= type
->index_type ();
1952 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1954 if (index
< low_bound
|| index
> high_bound
)
1956 rel_index
= index
- low_bound
;
1957 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1958 type_byte_order (type
));
1959 rel_index
%= TARGET_CHAR_BIT
;
1960 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
1961 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1962 return (word
>> rel_index
) & 1;
1966 value_in (struct value
*element
, struct value
*set
)
1969 struct type
*settype
= check_typedef (value_type (set
));
1970 struct type
*eltype
= check_typedef (value_type (element
));
1972 if (eltype
->code () == TYPE_CODE_RANGE
)
1973 eltype
= TYPE_TARGET_TYPE (eltype
);
1974 if (settype
->code () != TYPE_CODE_SET
)
1975 error (_("Second argument of 'IN' has wrong type"));
1976 if (eltype
->code () != TYPE_CODE_INT
1977 && eltype
->code () != TYPE_CODE_CHAR
1978 && eltype
->code () != TYPE_CODE_ENUM
1979 && eltype
->code () != TYPE_CODE_BOOL
)
1980 error (_("First argument of 'IN' has wrong type"));
1981 member
= value_bit_index (settype
, value_contents (set
),
1982 value_as_long (element
));
1984 error (_("First argument of 'IN' not in range"));