1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2021 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 /* Define whether or not the C operator '/' truncates towards zero for
38 differently signed operands (truncation direction is undefined in C). */
40 #ifndef TRUNCATION_TOWARDS_ZERO
41 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
44 /* Given a pointer, return the size of its target.
45 If the pointer type is void *, then return 1.
46 If the target type is incomplete, then error out.
47 This isn't a general purpose function, but just a
48 helper for value_ptradd. */
51 find_size_for_pointer_math (struct type
*ptr_type
)
54 struct type
*ptr_target
;
56 gdb_assert (ptr_type
->code () == TYPE_CODE_PTR
);
57 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
59 sz
= type_length_units (ptr_target
);
62 if (ptr_type
->code () == TYPE_CODE_VOID
)
68 name
= ptr_target
->name ();
70 error (_("Cannot perform pointer math on incomplete types, "
71 "try casting to a known type, or void *."));
73 error (_("Cannot perform pointer math on incomplete type \"%s\", "
74 "try casting to a known type, or void *."), name
);
80 /* Given a pointer ARG1 and an integral value ARG2, return the
81 result of C-style pointer arithmetic ARG1 + ARG2. */
84 value_ptradd (struct value
*arg1
, LONGEST arg2
)
86 struct type
*valptrtype
;
90 arg1
= coerce_array (arg1
);
91 valptrtype
= check_typedef (value_type (arg1
));
92 sz
= find_size_for_pointer_math (valptrtype
);
94 result
= value_from_pointer (valptrtype
,
95 value_as_address (arg1
) + sz
* arg2
);
96 if (VALUE_LVAL (result
) != lval_internalvar
)
97 set_value_component_location (result
, arg1
);
101 /* Given two compatible pointer values ARG1 and ARG2, return the
102 result of C-style pointer arithmetic ARG1 - ARG2. */
105 value_ptrdiff (struct value
*arg1
, struct value
*arg2
)
107 struct type
*type1
, *type2
;
110 arg1
= coerce_array (arg1
);
111 arg2
= coerce_array (arg2
);
112 type1
= check_typedef (value_type (arg1
));
113 type2
= check_typedef (value_type (arg2
));
115 gdb_assert (type1
->code () == TYPE_CODE_PTR
);
116 gdb_assert (type2
->code () == TYPE_CODE_PTR
);
118 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
119 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
120 error (_("First argument of `-' is a pointer and "
121 "second argument is neither\n"
122 "an integer nor a pointer of the same type."));
124 sz
= type_length_units (check_typedef (TYPE_TARGET_TYPE (type1
)));
127 warning (_("Type size unknown, assuming 1. "
128 "Try casting to a known type, or void *."));
132 return (value_as_long (arg1
) - value_as_long (arg2
)) / sz
;
135 /* Return the value of ARRAY[IDX].
137 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
138 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
140 See comments in value_coerce_array() for rationale for reason for
141 doing lower bounds adjustment here rather than there.
142 FIXME: Perhaps we should validate that the index is valid and if
143 verbosity is set, warn about invalid indices (but still use them). */
146 value_subscript (struct value
*array
, LONGEST index
)
148 bool c_style
= current_language
->c_style_arrays_p ();
151 array
= coerce_ref (array
);
152 tarray
= check_typedef (value_type (array
));
154 if (tarray
->code () == TYPE_CODE_ARRAY
155 || tarray
->code () == TYPE_CODE_STRING
)
157 struct type
*range_type
= tarray
->index_type ();
158 gdb::optional
<LONGEST
> lowerbound
= get_discrete_low_bound (range_type
);
159 if (!lowerbound
.has_value ())
162 if (VALUE_LVAL (array
) != lval_memory
)
163 return value_subscripted_rvalue (array
, index
, *lowerbound
);
167 gdb::optional
<LONGEST
> upperbound
168 = get_discrete_high_bound (range_type
);
170 if (!upperbound
.has_value ())
173 if (index
>= *lowerbound
&& index
<= *upperbound
)
174 return value_subscripted_rvalue (array
, index
, *lowerbound
);
176 /* Emit warning unless we have an array of unknown size.
177 An array of unknown size has lowerbound 0 and upperbound -1. */
178 if (*upperbound
> -1)
179 warning (_("array or string index out of range"));
180 /* fall doing C stuff */
184 index
-= *lowerbound
;
185 array
= value_coerce_array (array
);
189 return value_ind (value_ptradd (array
, index
));
191 error (_("not an array or string"));
194 /* Return the value of EXPR[IDX], expr an aggregate rvalue
195 (eg, a vector register). This routine used to promote floats
196 to doubles, but no longer does. */
198 static struct value
*
199 value_subscripted_rvalue (struct value
*array
, LONGEST index
,
202 struct type
*array_type
= check_typedef (value_type (array
));
203 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
204 LONGEST elt_size
= type_length_units (elt_type
);
206 /* Fetch the bit stride and convert it to a byte stride, assuming 8 bits
208 LONGEST stride
= array_type
->bit_stride ();
211 struct gdbarch
*arch
= elt_type
->arch ();
212 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
213 elt_size
= stride
/ (unit_size
* 8);
216 LONGEST elt_offs
= elt_size
* (index
- lowerbound
);
217 bool array_upper_bound_undefined
218 = array_type
->bounds ()->high
.kind () == PROP_UNDEFINED
;
220 if (index
< lowerbound
221 || (!array_upper_bound_undefined
222 && elt_offs
>= type_length_units (array_type
))
223 || (VALUE_LVAL (array
) != lval_memory
&& array_upper_bound_undefined
))
225 if (type_not_associated (array_type
))
226 error (_("no such vector element (vector not associated)"));
227 else if (type_not_allocated (array_type
))
228 error (_("no such vector element (vector not allocated)"));
230 error (_("no such vector element"));
233 if (is_dynamic_type (elt_type
))
237 address
= value_address (array
) + elt_offs
;
238 elt_type
= resolve_dynamic_type (elt_type
, {}, address
);
241 return value_from_component (array
, elt_type
, elt_offs
);
245 /* Check to see if either argument is a structure, or a reference to
246 one. This is called so we know whether to go ahead with the normal
247 binop or look for a user defined function instead.
249 For now, we do not overload the `=' operator. */
252 binop_types_user_defined_p (enum exp_opcode op
,
253 struct type
*type1
, struct type
*type2
)
255 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
258 type1
= check_typedef (type1
);
259 if (TYPE_IS_REFERENCE (type1
))
260 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
262 type2
= check_typedef (type2
);
263 if (TYPE_IS_REFERENCE (type2
))
264 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
266 return (type1
->code () == TYPE_CODE_STRUCT
267 || type2
->code () == TYPE_CODE_STRUCT
);
270 /* Check to see if either argument is a structure, or a reference to
271 one. This is called so we know whether to go ahead with the normal
272 binop or look for a user defined function instead.
274 For now, we do not overload the `=' operator. */
277 binop_user_defined_p (enum exp_opcode op
,
278 struct value
*arg1
, struct value
*arg2
)
280 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
283 /* Check to see if argument is a structure. This is called so
284 we know whether to go ahead with the normal unop or look for a
285 user defined function instead.
287 For now, we do not overload the `&' operator. */
290 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
296 type1
= check_typedef (value_type (arg1
));
297 if (TYPE_IS_REFERENCE (type1
))
298 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
299 return type1
->code () == TYPE_CODE_STRUCT
;
302 /* Try to find an operator named OPERATOR which takes NARGS arguments
303 specified in ARGS. If the operator found is a static member operator
304 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
305 The search if performed through find_overload_match which will handle
306 member operators, non member operators, operators imported implicitly or
307 explicitly, and perform correct overload resolution in all of the above
308 situations or combinations thereof. */
310 static struct value
*
311 value_user_defined_cpp_op (gdb::array_view
<value
*> args
, char *oper
,
312 int *static_memfuncp
, enum noside noside
)
315 struct symbol
*symp
= NULL
;
316 struct value
*valp
= NULL
;
318 find_overload_match (args
, oper
, BOTH
/* could be method */,
320 NULL
/* pass NULL symbol since symbol is unknown */,
321 &valp
, &symp
, static_memfuncp
, 0, noside
);
328 /* This is a non member function and does not
329 expect a reference as its first argument
330 rather the explicit structure. */
331 args
[0] = value_ind (args
[0]);
332 return value_of_variable (symp
, 0);
335 error (_("Could not find %s."), oper
);
338 /* Lookup user defined operator NAME. Return a value representing the
339 function, otherwise return NULL. */
341 static struct value
*
342 value_user_defined_op (struct value
**argp
, gdb::array_view
<value
*> args
,
343 char *name
, int *static_memfuncp
, enum noside noside
)
345 struct value
*result
= NULL
;
347 if (current_language
->la_language
== language_cplus
)
349 result
= value_user_defined_cpp_op (args
, name
, static_memfuncp
,
353 result
= value_struct_elt (argp
, args
, name
, static_memfuncp
,
359 /* We know either arg1 or arg2 is a structure, so try to find the right
360 user defined function. Create an argument vector that calls
361 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
362 binary operator which is legal for GNU C++).
364 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
365 is the opcode saying how to modify it. Otherwise, OTHEROP is
369 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
370 enum exp_opcode otherop
, enum noside noside
)
376 arg1
= coerce_ref (arg1
);
377 arg2
= coerce_ref (arg2
);
379 /* now we know that what we have to do is construct our
380 arg vector and find the right function to call it with. */
382 if (check_typedef (value_type (arg1
))->code () != TYPE_CODE_STRUCT
)
383 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
385 value
*argvec_storage
[3];
386 gdb::array_view
<value
*> argvec
= argvec_storage
;
388 argvec
[1] = value_addr (arg1
);
391 /* Make the right function name up. */
392 strcpy (tstr
, "operator__");
417 case BINOP_BITWISE_AND
:
420 case BINOP_BITWISE_IOR
:
423 case BINOP_BITWISE_XOR
:
426 case BINOP_LOGICAL_AND
:
429 case BINOP_LOGICAL_OR
:
441 case BINOP_ASSIGN_MODIFY
:
459 case BINOP_BITWISE_AND
:
462 case BINOP_BITWISE_IOR
:
465 case BINOP_BITWISE_XOR
:
468 case BINOP_MOD
: /* invalid */
470 error (_("Invalid binary operation specified."));
473 case BINOP_SUBSCRIPT
:
494 case BINOP_MOD
: /* invalid */
496 error (_("Invalid binary operation specified."));
499 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1), tstr
,
500 &static_memfuncp
, noside
);
506 argvec
[1] = argvec
[0];
507 argvec
= argvec
.slice (1);
509 if (value_type (argvec
[0])->code () == TYPE_CODE_XMETHOD
)
511 /* Static xmethods are not supported yet. */
512 gdb_assert (static_memfuncp
== 0);
513 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
515 struct type
*return_type
516 = result_type_of_xmethod (argvec
[0], argvec
.slice (1));
518 if (return_type
== NULL
)
519 error (_("Xmethod is missing return type."));
520 return value_zero (return_type
, VALUE_LVAL (arg1
));
522 return call_xmethod (argvec
[0], argvec
.slice (1));
524 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
526 struct type
*return_type
;
529 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
530 return value_zero (return_type
, VALUE_LVAL (arg1
));
532 return call_function_by_hand (argvec
[0], NULL
,
533 argvec
.slice (1, 2 - static_memfuncp
));
535 throw_error (NOT_FOUND_ERROR
,
536 _("member function %s not found"), tstr
);
539 /* We know that arg1 is a structure, so try to find a unary user
540 defined operator that matches the operator in question.
541 Create an argument vector that calls arg1.operator @ (arg1)
542 and return that value (where '@' is (almost) any unary operator which
543 is legal for GNU C++). */
546 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
548 struct gdbarch
*gdbarch
= value_type (arg1
)->arch ();
550 char tstr
[13], mangle_tstr
[13];
551 int static_memfuncp
, nargs
;
553 arg1
= coerce_ref (arg1
);
555 /* now we know that what we have to do is construct our
556 arg vector and find the right function to call it with. */
558 if (check_typedef (value_type (arg1
))->code () != TYPE_CODE_STRUCT
)
559 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
561 value
*argvec_storage
[3];
562 gdb::array_view
<value
*> argvec
= argvec_storage
;
564 argvec
[1] = value_addr (arg1
);
569 /* Make the right function name up. */
570 strcpy (tstr
, "operator__");
572 strcpy (mangle_tstr
, "__");
575 case UNOP_PREINCREMENT
:
578 case UNOP_PREDECREMENT
:
581 case UNOP_POSTINCREMENT
:
583 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
586 case UNOP_POSTDECREMENT
:
588 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
591 case UNOP_LOGICAL_NOT
:
594 case UNOP_COMPLEMENT
:
610 error (_("Invalid unary operation specified."));
613 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1, nargs
), tstr
,
614 &static_memfuncp
, noside
);
620 argvec
[1] = argvec
[0];
621 argvec
= argvec
.slice (1);
623 if (value_type (argvec
[0])->code () == TYPE_CODE_XMETHOD
)
625 /* Static xmethods are not supported yet. */
626 gdb_assert (static_memfuncp
== 0);
627 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
629 struct type
*return_type
630 = result_type_of_xmethod (argvec
[0], argvec
[1]);
632 if (return_type
== NULL
)
633 error (_("Xmethod is missing return type."));
634 return value_zero (return_type
, VALUE_LVAL (arg1
));
636 return call_xmethod (argvec
[0], argvec
[1]);
638 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
640 struct type
*return_type
;
643 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
644 return value_zero (return_type
, VALUE_LVAL (arg1
));
646 return call_function_by_hand (argvec
[0], NULL
,
647 argvec
.slice (1, nargs
));
649 throw_error (NOT_FOUND_ERROR
,
650 _("member function %s not found"), tstr
);
654 /* Concatenate two values with the following conditions:
656 (1) Both values must be either bitstring values or character string
657 values and the resulting value consists of the concatenation of
658 ARG1 followed by ARG2.
662 One value must be an integer value and the other value must be
663 either a bitstring value or character string value, which is
664 to be repeated by the number of times specified by the integer
668 (2) Boolean values are also allowed and are treated as bit string
671 (3) Character values are also allowed and are treated as character
672 string values of length 1. */
675 value_concat (struct value
*arg1
, struct value
*arg2
)
677 struct value
*inval1
;
678 struct value
*inval2
;
679 struct value
*outval
= NULL
;
680 int inval1len
, inval2len
;
683 struct type
*type1
= check_typedef (value_type (arg1
));
684 struct type
*type2
= check_typedef (value_type (arg2
));
685 struct type
*char_type
;
687 /* First figure out if we are dealing with two values to be concatenated
688 or a repeat count and a value to be repeated. INVAL1 is set to the
689 first of two concatenated values, or the repeat count. INVAL2 is set
690 to the second of the two concatenated values or the value to be
693 if (type2
->code () == TYPE_CODE_INT
)
695 struct type
*tmp
= type1
;
708 /* Now process the input values. */
710 if (type1
->code () == TYPE_CODE_INT
)
712 /* We have a repeat count. Validate the second value and then
713 construct a value repeated that many times. */
714 if (type2
->code () == TYPE_CODE_STRING
715 || type2
->code () == TYPE_CODE_CHAR
)
717 count
= longest_to_int (value_as_long (inval1
));
718 inval2len
= TYPE_LENGTH (type2
);
719 std::vector
<char> ptr (count
* inval2len
);
720 if (type2
->code () == TYPE_CODE_CHAR
)
724 inchar
= (char) unpack_long (type2
,
725 value_contents (inval2
).data ());
726 for (idx
= 0; idx
< count
; idx
++)
733 char_type
= TYPE_TARGET_TYPE (type2
);
735 for (idx
= 0; idx
< count
; idx
++)
736 memcpy (&ptr
[idx
* inval2len
], value_contents (inval2
).data (),
739 outval
= value_string (ptr
.data (), count
* inval2len
, char_type
);
741 else if (type2
->code () == TYPE_CODE_BOOL
)
743 error (_("unimplemented support for boolean repeats"));
747 error (_("can't repeat values of that type"));
750 else if (type1
->code () == TYPE_CODE_STRING
751 || type1
->code () == TYPE_CODE_CHAR
)
753 /* We have two character strings to concatenate. */
754 if (type2
->code () != TYPE_CODE_STRING
755 && type2
->code () != TYPE_CODE_CHAR
)
757 error (_("Strings can only be concatenated with other strings."));
759 inval1len
= TYPE_LENGTH (type1
);
760 inval2len
= TYPE_LENGTH (type2
);
761 std::vector
<char> ptr (inval1len
+ inval2len
);
762 if (type1
->code () == TYPE_CODE_CHAR
)
766 ptr
[0] = (char) unpack_long (type1
, value_contents (inval1
).data ());
770 char_type
= TYPE_TARGET_TYPE (type1
);
772 memcpy (ptr
.data (), value_contents (inval1
).data (), inval1len
);
774 if (type2
->code () == TYPE_CODE_CHAR
)
777 (char) unpack_long (type2
, value_contents (inval2
).data ());
781 memcpy (&ptr
[inval1len
], value_contents (inval2
).data (), inval2len
);
783 outval
= value_string (ptr
.data (), inval1len
+ inval2len
, char_type
);
785 else if (type1
->code () == TYPE_CODE_BOOL
)
787 /* We have two bitstrings to concatenate. */
788 if (type2
->code () != TYPE_CODE_BOOL
)
790 error (_("Booleans can only be concatenated "
791 "with other bitstrings or booleans."));
793 error (_("unimplemented support for boolean concatenation."));
797 /* We don't know how to concatenate these operands. */
798 error (_("illegal operands for concatenation."));
803 /* Integer exponentiation: V1**V2, where both arguments are
804 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
807 integer_pow (LONGEST v1
, LONGEST v2
)
812 error (_("Attempt to raise 0 to negative power."));
818 /* The Russian Peasant's Algorithm. */
834 /* Obtain argument values for binary operation, converting from
835 other types if one of them is not floating point. */
837 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
838 gdb_byte
*x
, struct type
**eff_type_x
,
839 gdb_byte
*y
, struct type
**eff_type_y
)
841 struct type
*type1
, *type2
;
843 type1
= check_typedef (value_type (arg1
));
844 type2
= check_typedef (value_type (arg2
));
846 /* At least one of the arguments must be of floating-point type. */
847 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
849 if (is_floating_type (type1
) && is_floating_type (type2
)
850 && type1
->code () != type2
->code ())
851 /* The DFP extension to the C language does not allow mixing of
852 * decimal float types with other float types in expressions
853 * (see WDTR 24732, page 12). */
854 error (_("Mixing decimal floating types with "
855 "other floating types is not allowed."));
857 /* Obtain value of arg1, converting from other types if necessary. */
859 if (is_floating_type (type1
))
862 memcpy (x
, value_contents (arg1
).data (), TYPE_LENGTH (type1
));
864 else if (is_integral_type (type1
))
867 if (type1
->is_unsigned ())
868 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
870 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
873 error (_("Don't know how to convert from %s to %s."), type1
->name (),
876 /* Obtain value of arg2, converting from other types if necessary. */
878 if (is_floating_type (type2
))
881 memcpy (y
, value_contents (arg2
).data (), TYPE_LENGTH (type2
));
883 else if (is_integral_type (type2
))
886 if (type2
->is_unsigned ())
887 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
889 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
892 error (_("Don't know how to convert from %s to %s."), type1
->name (),
896 /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
897 perform the binary operation OP on these two operands, and return
898 the resulting value (also as a fixed point). */
900 static struct value
*
901 fixed_point_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
903 struct type
*type1
= check_typedef (value_type (arg1
));
904 struct type
*type2
= check_typedef (value_type (arg2
));
905 const struct language_defn
*language
= current_language
;
907 struct gdbarch
*gdbarch
= type1
->arch ();
912 gdb_assert (is_fixed_point_type (type1
) || is_fixed_point_type (type2
));
913 if (op
== BINOP_MUL
|| op
== BINOP_DIV
)
915 v1
= value_to_gdb_mpq (arg1
);
916 v2
= value_to_gdb_mpq (arg2
);
918 /* The code below uses TYPE1 for the result type, so make sure
919 it is set properly. */
920 if (!is_fixed_point_type (type1
))
925 if (!is_fixed_point_type (type1
))
927 arg1
= value_cast (type2
, arg1
);
930 if (!is_fixed_point_type (type2
))
932 arg2
= value_cast (type1
, arg2
);
936 v1
.read_fixed_point (gdb::make_array_view (value_contents (arg1
).data (),
937 TYPE_LENGTH (type1
)),
938 type_byte_order (type1
), type1
->is_unsigned (),
939 type1
->fixed_point_scaling_factor ());
940 v2
.read_fixed_point (gdb::make_array_view (value_contents (arg2
).data (),
941 TYPE_LENGTH (type2
)),
942 type_byte_order (type2
), type2
->is_unsigned (),
943 type2
->fixed_point_scaling_factor ());
946 auto fixed_point_to_value
= [type1
] (const gdb_mpq
&fp
)
948 value
*fp_val
= allocate_value (type1
);
951 (gdb::make_array_view (value_contents_raw (fp_val
).data (),
952 TYPE_LENGTH (type1
)),
953 type_byte_order (type1
),
954 type1
->is_unsigned (),
955 type1
->fixed_point_scaling_factor ());
963 mpq_add (res
.val
, v1
.val
, v2
.val
);
964 val
= fixed_point_to_value (res
);
968 mpq_sub (res
.val
, v1
.val
, v2
.val
);
969 val
= fixed_point_to_value (res
);
973 val
= fixed_point_to_value (mpq_cmp (v1
.val
, v2
.val
) < 0 ? v1
: v2
);
977 val
= fixed_point_to_value (mpq_cmp (v1
.val
, v2
.val
) > 0 ? v1
: v2
);
981 mpq_mul (res
.val
, v1
.val
, v2
.val
);
982 val
= fixed_point_to_value (res
);
986 if (mpq_sgn (v2
.val
) == 0)
987 error (_("Division by zero"));
988 mpq_div (res
.val
, v1
.val
, v2
.val
);
989 val
= fixed_point_to_value (res
);
993 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
994 mpq_cmp (v1
.val
, v2
.val
) == 0 ? 1 : 0);
998 val
= value_from_ulongest (language_bool_type (language
, gdbarch
),
999 mpq_cmp (v1
.val
, v2
.val
) < 0 ? 1 : 0);
1003 error (_("Integer-only operation on fixed point number."));
1009 /* A helper function that finds the type to use for a binary operation
1010 involving TYPE1 and TYPE2. */
1012 static struct type
*
1013 promotion_type (struct type
*type1
, struct type
*type2
)
1015 struct type
*result_type
;
1017 if (is_floating_type (type1
) || is_floating_type (type2
))
1019 /* If only one type is floating-point, use its type.
1020 Otherwise use the bigger type. */
1021 if (!is_floating_type (type1
))
1022 result_type
= type2
;
1023 else if (!is_floating_type (type2
))
1024 result_type
= type1
;
1025 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1026 result_type
= type2
;
1028 result_type
= type1
;
1032 /* Integer types. */
1033 if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1034 result_type
= type1
;
1035 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1036 result_type
= type2
;
1037 else if (type1
->is_unsigned ())
1038 result_type
= type1
;
1039 else if (type2
->is_unsigned ())
1040 result_type
= type2
;
1042 result_type
= type1
;
1048 static struct value
*scalar_binop (struct value
*arg1
, struct value
*arg2
,
1049 enum exp_opcode op
);
1051 /* Perform a binary operation on complex operands. */
1053 static struct value
*
1054 complex_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1056 struct type
*arg1_type
= check_typedef (value_type (arg1
));
1057 struct type
*arg2_type
= check_typedef (value_type (arg2
));
1059 struct value
*arg1_real
, *arg1_imag
, *arg2_real
, *arg2_imag
;
1060 if (arg1_type
->code () == TYPE_CODE_COMPLEX
)
1062 arg1_real
= value_real_part (arg1
);
1063 arg1_imag
= value_imaginary_part (arg1
);
1068 arg1_imag
= value_zero (arg1_type
, not_lval
);
1070 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1072 arg2_real
= value_real_part (arg2
);
1073 arg2_imag
= value_imaginary_part (arg2
);
1078 arg2_imag
= value_zero (arg2_type
, not_lval
);
1081 struct type
*comp_type
= promotion_type (value_type (arg1_real
),
1082 value_type (arg2_real
));
1083 if (!can_create_complex_type (comp_type
))
1084 error (_("Argument to complex arithmetic operation not supported."));
1086 arg1_real
= value_cast (comp_type
, arg1_real
);
1087 arg1_imag
= value_cast (comp_type
, arg1_imag
);
1088 arg2_real
= value_cast (comp_type
, arg2_real
);
1089 arg2_imag
= value_cast (comp_type
, arg2_imag
);
1091 struct type
*result_type
= init_complex_type (nullptr, comp_type
);
1093 struct value
*result_real
, *result_imag
;
1098 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1099 result_imag
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1104 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1105 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1106 result_real
= scalar_binop (x1
, x2
, BINOP_SUB
);
1108 x1
= scalar_binop (arg1_real
, arg2_imag
, op
);
1109 x2
= scalar_binop (arg1_imag
, arg2_real
, op
);
1110 result_imag
= scalar_binop (x1
, x2
, BINOP_ADD
);
1116 if (arg2_type
->code () == TYPE_CODE_COMPLEX
)
1118 struct value
*conjugate
= value_complement (arg2
);
1119 /* We have to reconstruct ARG1, in case the type was
1121 arg1
= value_literal_complex (arg1_real
, arg1_imag
, result_type
);
1123 struct value
*numerator
= scalar_binop (arg1
, conjugate
,
1125 arg1_real
= value_real_part (numerator
);
1126 arg1_imag
= value_imaginary_part (numerator
);
1128 struct value
*x1
= scalar_binop (arg2_real
, arg2_real
, BINOP_MUL
);
1129 struct value
*x2
= scalar_binop (arg2_imag
, arg2_imag
, BINOP_MUL
);
1130 arg2_real
= scalar_binop (x1
, x2
, BINOP_ADD
);
1133 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1134 result_imag
= scalar_binop (arg1_imag
, arg2_real
, op
);
1139 case BINOP_NOTEQUAL
:
1141 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1142 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1144 LONGEST v1
= value_as_long (x1
);
1145 LONGEST v2
= value_as_long (x2
);
1147 if (op
== BINOP_EQUAL
)
1152 return value_from_longest (value_type (x1
), v1
);
1157 error (_("Invalid binary operation on numbers."));
1160 return value_literal_complex (result_real
, result_imag
, result_type
);
1163 /* Perform a binary operation on two operands which have reasonable
1164 representations as integers or floats. This includes booleans,
1165 characters, integers, or floats.
1166 Does not support addition and subtraction on pointers;
1167 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1169 static struct value
*
1170 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1173 struct type
*type1
, *type2
, *result_type
;
1175 arg1
= coerce_ref (arg1
);
1176 arg2
= coerce_ref (arg2
);
1178 type1
= check_typedef (value_type (arg1
));
1179 type2
= check_typedef (value_type (arg2
));
1181 if (type1
->code () == TYPE_CODE_COMPLEX
1182 || type2
->code () == TYPE_CODE_COMPLEX
)
1183 return complex_binop (arg1
, arg2
, op
);
1185 if ((!is_floating_value (arg1
)
1186 && !is_integral_type (type1
)
1187 && !is_fixed_point_type (type1
))
1188 || (!is_floating_value (arg2
)
1189 && !is_integral_type (type2
)
1190 && !is_fixed_point_type (type2
)))
1191 error (_("Argument to arithmetic operation not a number or boolean."));
1193 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
1194 return fixed_point_binop (arg1
, arg2
, op
);
1196 if (is_floating_type (type1
) || is_floating_type (type2
))
1198 result_type
= promotion_type (type1
, type2
);
1199 val
= allocate_value (result_type
);
1201 struct type
*eff_type_v1
, *eff_type_v2
;
1202 gdb::byte_vector v1
, v2
;
1203 v1
.resize (TYPE_LENGTH (result_type
));
1204 v2
.resize (TYPE_LENGTH (result_type
));
1206 value_args_as_target_float (arg1
, arg2
,
1207 v1
.data (), &eff_type_v1
,
1208 v2
.data (), &eff_type_v2
);
1209 target_float_binop (op
, v1
.data (), eff_type_v1
,
1210 v2
.data (), eff_type_v2
,
1211 value_contents_raw (val
).data (), result_type
);
1213 else if (type1
->code () == TYPE_CODE_BOOL
1214 || type2
->code () == TYPE_CODE_BOOL
)
1216 LONGEST v1
, v2
, v
= 0;
1218 v1
= value_as_long (arg1
);
1219 v2
= value_as_long (arg2
);
1223 case BINOP_BITWISE_AND
:
1227 case BINOP_BITWISE_IOR
:
1231 case BINOP_BITWISE_XOR
:
1239 case BINOP_NOTEQUAL
:
1244 error (_("Invalid operation on booleans."));
1247 result_type
= type1
;
1249 val
= allocate_value (result_type
);
1250 store_signed_integer (value_contents_raw (val
).data (),
1251 TYPE_LENGTH (result_type
),
1252 type_byte_order (result_type
),
1256 /* Integral operations here. */
1258 /* Determine type length of the result, and if the operation should
1259 be done unsigned. For exponentiation and shift operators,
1260 use the length and type of the left operand. Otherwise,
1261 use the signedness of the operand with the greater length.
1262 If both operands are of equal length, use unsigned operation
1263 if one of the operands is unsigned. */
1264 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1265 result_type
= type1
;
1267 result_type
= promotion_type (type1
, type2
);
1269 if (result_type
->is_unsigned ())
1271 LONGEST v2_signed
= value_as_long (arg2
);
1272 ULONGEST v1
, v2
, v
= 0;
1274 v1
= (ULONGEST
) value_as_long (arg1
);
1275 v2
= (ULONGEST
) v2_signed
;
1296 error (_("Division by zero"));
1300 v
= uinteger_pow (v1
, v2_signed
);
1307 error (_("Division by zero"));
1311 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1312 v1 mod 0 has a defined value, v1. */
1320 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1333 case BINOP_BITWISE_AND
:
1337 case BINOP_BITWISE_IOR
:
1341 case BINOP_BITWISE_XOR
:
1345 case BINOP_LOGICAL_AND
:
1349 case BINOP_LOGICAL_OR
:
1354 v
= v1
< v2
? v1
: v2
;
1358 v
= v1
> v2
? v1
: v2
;
1365 case BINOP_NOTEQUAL
:
1386 error (_("Invalid binary operation on numbers."));
1389 val
= allocate_value (result_type
);
1390 store_unsigned_integer (value_contents_raw (val
).data (),
1391 TYPE_LENGTH (value_type (val
)),
1392 type_byte_order (result_type
),
1397 LONGEST v1
, v2
, v
= 0;
1399 v1
= value_as_long (arg1
);
1400 v2
= value_as_long (arg2
);
1421 error (_("Division by zero"));
1425 v
= integer_pow (v1
, v2
);
1432 error (_("Division by zero"));
1436 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1437 X mod 0 has a defined value, X. */
1445 /* Compute floor. */
1446 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1462 case BINOP_BITWISE_AND
:
1466 case BINOP_BITWISE_IOR
:
1470 case BINOP_BITWISE_XOR
:
1474 case BINOP_LOGICAL_AND
:
1478 case BINOP_LOGICAL_OR
:
1483 v
= v1
< v2
? v1
: v2
;
1487 v
= v1
> v2
? v1
: v2
;
1494 case BINOP_NOTEQUAL
:
1515 error (_("Invalid binary operation on numbers."));
1518 val
= allocate_value (result_type
);
1519 store_signed_integer (value_contents_raw (val
).data (),
1520 TYPE_LENGTH (value_type (val
)),
1521 type_byte_order (result_type
),
1529 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1530 replicating SCALAR_VALUE for each element of the vector. Only scalar
1531 types that can be cast to the type of one element of the vector are
1532 acceptable. The newly created vector value is returned upon success,
1533 otherwise an error is thrown. */
1536 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1538 /* Widen the scalar to a vector. */
1539 struct type
*eltype
, *scalar_type
;
1540 struct value
*val
, *elval
;
1541 LONGEST low_bound
, high_bound
;
1544 vector_type
= check_typedef (vector_type
);
1546 gdb_assert (vector_type
->code () == TYPE_CODE_ARRAY
1547 && vector_type
->is_vector ());
1549 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1550 error (_("Could not determine the vector bounds"));
1552 eltype
= check_typedef (TYPE_TARGET_TYPE (vector_type
));
1553 elval
= value_cast (eltype
, scalar_value
);
1555 scalar_type
= check_typedef (value_type (scalar_value
));
1557 /* If we reduced the length of the scalar then check we didn't loose any
1559 if (TYPE_LENGTH (eltype
) < TYPE_LENGTH (scalar_type
)
1560 && !value_equal (elval
, scalar_value
))
1561 error (_("conversion of scalar to vector involves truncation"));
1563 val
= allocate_value (vector_type
);
1564 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1565 /* Duplicate the contents of elval into the destination vector. */
1566 memcpy (value_contents_writeable (val
).data () + (i
* TYPE_LENGTH (eltype
)),
1567 value_contents_all (elval
).data (), TYPE_LENGTH (eltype
));
1572 /* Performs a binary operation on two vector operands by calling scalar_binop
1573 for each pair of vector components. */
1575 static struct value
*
1576 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1578 struct value
*val
, *tmp
, *mark
;
1579 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1580 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1581 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1583 type1
= check_typedef (value_type (val1
));
1584 type2
= check_typedef (value_type (val2
));
1586 t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1587 && type1
->is_vector ()) ? 1 : 0;
1588 t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1589 && type2
->is_vector ()) ? 1 : 0;
1591 if (!t1_is_vec
|| !t2_is_vec
)
1592 error (_("Vector operations are only supported among vectors"));
1594 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1595 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1596 error (_("Could not determine the vector bounds"));
1598 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1599 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1600 elsize
= TYPE_LENGTH (eltype1
);
1602 if (eltype1
->code () != eltype2
->code ()
1603 || elsize
!= TYPE_LENGTH (eltype2
)
1604 || eltype1
->is_unsigned () != eltype2
->is_unsigned ()
1605 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1606 error (_("Cannot perform operation on vectors with different types"));
1608 val
= allocate_value (type1
);
1609 mark
= value_mark ();
1610 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1612 tmp
= value_binop (value_subscript (val1
, i
),
1613 value_subscript (val2
, i
), op
);
1614 memcpy (value_contents_writeable (val
).data () + i
* elsize
,
1615 value_contents_all (tmp
).data (),
1618 value_free_to_mark (mark
);
1623 /* Perform a binary operation on two operands. */
1626 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1629 struct type
*type1
= check_typedef (value_type (arg1
));
1630 struct type
*type2
= check_typedef (value_type (arg2
));
1631 int t1_is_vec
= (type1
->code () == TYPE_CODE_ARRAY
1632 && type1
->is_vector ());
1633 int t2_is_vec
= (type2
->code () == TYPE_CODE_ARRAY
1634 && type2
->is_vector ());
1636 if (!t1_is_vec
&& !t2_is_vec
)
1637 val
= scalar_binop (arg1
, arg2
, op
);
1638 else if (t1_is_vec
&& t2_is_vec
)
1639 val
= vector_binop (arg1
, arg2
, op
);
1642 /* Widen the scalar operand to a vector. */
1643 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1644 struct type
*t
= t1_is_vec
? type2
: type1
;
1646 if (t
->code () != TYPE_CODE_FLT
1647 && t
->code () != TYPE_CODE_DECFLOAT
1648 && !is_integral_type (t
))
1649 error (_("Argument to operation not a number or boolean."));
1651 /* Replicate the scalar value to make a vector value. */
1652 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1654 val
= vector_binop (arg1
, arg2
, op
);
1663 value_logical_not (struct value
*arg1
)
1669 arg1
= coerce_array (arg1
);
1670 type1
= check_typedef (value_type (arg1
));
1672 if (is_floating_value (arg1
))
1673 return target_float_is_zero (value_contents (arg1
).data (), type1
);
1675 len
= TYPE_LENGTH (type1
);
1676 p
= value_contents (arg1
).data ();
1687 /* Perform a comparison on two string values (whose content are not
1688 necessarily null terminated) based on their length. */
1691 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1693 int len1
= TYPE_LENGTH (value_type (arg1
));
1694 int len2
= TYPE_LENGTH (value_type (arg2
));
1695 const gdb_byte
*s1
= value_contents (arg1
).data ();
1696 const gdb_byte
*s2
= value_contents (arg2
).data ();
1697 int i
, len
= len1
< len2
? len1
: len2
;
1699 for (i
= 0; i
< len
; i
++)
1703 else if (s1
[i
] > s2
[i
])
1711 else if (len1
> len2
)
1717 /* Simulate the C operator == by returning a 1
1718 iff ARG1 and ARG2 have equal contents. */
1721 value_equal (struct value
*arg1
, struct value
*arg2
)
1726 struct type
*type1
, *type2
;
1727 enum type_code code1
;
1728 enum type_code code2
;
1729 int is_int1
, is_int2
;
1731 arg1
= coerce_array (arg1
);
1732 arg2
= coerce_array (arg2
);
1734 type1
= check_typedef (value_type (arg1
));
1735 type2
= check_typedef (value_type (arg2
));
1736 code1
= type1
->code ();
1737 code2
= type2
->code ();
1738 is_int1
= is_integral_type (type1
);
1739 is_int2
= is_integral_type (type2
);
1741 if (is_int1
&& is_int2
)
1742 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1744 else if ((is_floating_value (arg1
) || is_int1
)
1745 && (is_floating_value (arg2
) || is_int2
))
1747 struct type
*eff_type_v1
, *eff_type_v2
;
1748 gdb::byte_vector v1
, v2
;
1749 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1750 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1752 value_args_as_target_float (arg1
, arg2
,
1753 v1
.data (), &eff_type_v1
,
1754 v2
.data (), &eff_type_v2
);
1756 return target_float_compare (v1
.data (), eff_type_v1
,
1757 v2
.data (), eff_type_v2
) == 0;
1760 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1762 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1763 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1764 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1765 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1767 else if (code1
== code2
1768 && ((len
= (int) TYPE_LENGTH (type1
))
1769 == (int) TYPE_LENGTH (type2
)))
1771 p1
= value_contents (arg1
).data ();
1772 p2
= value_contents (arg2
).data ();
1780 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1782 return value_strcmp (arg1
, arg2
) == 0;
1785 error (_("Invalid type combination in equality test."));
1788 /* Compare values based on their raw contents. Useful for arrays since
1789 value_equal coerces them to pointers, thus comparing just the address
1790 of the array instead of its contents. */
1793 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1795 struct type
*type1
, *type2
;
1797 type1
= check_typedef (value_type (arg1
));
1798 type2
= check_typedef (value_type (arg2
));
1800 return (type1
->code () == type2
->code ()
1801 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1802 && memcmp (value_contents (arg1
).data (),
1803 value_contents (arg2
).data (),
1804 TYPE_LENGTH (type1
)) == 0);
1807 /* Simulate the C operator < by returning 1
1808 iff ARG1's contents are less than ARG2's. */
1811 value_less (struct value
*arg1
, struct value
*arg2
)
1813 enum type_code code1
;
1814 enum type_code code2
;
1815 struct type
*type1
, *type2
;
1816 int is_int1
, is_int2
;
1818 arg1
= coerce_array (arg1
);
1819 arg2
= coerce_array (arg2
);
1821 type1
= check_typedef (value_type (arg1
));
1822 type2
= check_typedef (value_type (arg2
));
1823 code1
= type1
->code ();
1824 code2
= type2
->code ();
1825 is_int1
= is_integral_type (type1
);
1826 is_int2
= is_integral_type (type2
);
1828 if ((is_int1
&& is_int2
)
1829 || (is_fixed_point_type (type1
) && is_fixed_point_type (type2
)))
1830 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1832 else if ((is_floating_value (arg1
) || is_int1
)
1833 && (is_floating_value (arg2
) || is_int2
))
1835 struct type
*eff_type_v1
, *eff_type_v2
;
1836 gdb::byte_vector v1
, v2
;
1837 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1838 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1840 value_args_as_target_float (arg1
, arg2
,
1841 v1
.data (), &eff_type_v1
,
1842 v2
.data (), &eff_type_v2
);
1844 return target_float_compare (v1
.data (), eff_type_v1
,
1845 v2
.data (), eff_type_v2
) == -1;
1847 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1848 return value_as_address (arg1
) < value_as_address (arg2
);
1850 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1852 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1853 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1854 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1855 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1856 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1857 return value_strcmp (arg1
, arg2
) < 0;
1860 error (_("Invalid type combination in ordering comparison."));
1865 /* The unary operators +, - and ~. They free the argument ARG1. */
1868 value_pos (struct value
*arg1
)
1872 arg1
= coerce_ref (arg1
);
1873 type
= check_typedef (value_type (arg1
));
1875 if (is_integral_type (type
) || is_floating_value (arg1
)
1876 || (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1877 || type
->code () == TYPE_CODE_COMPLEX
)
1878 return value_from_contents (type
, value_contents (arg1
).data ());
1880 error (_("Argument to positive operation not a number."));
1884 value_neg (struct value
*arg1
)
1888 arg1
= coerce_ref (arg1
);
1889 type
= check_typedef (value_type (arg1
));
1891 if (is_integral_type (type
) || is_floating_type (type
))
1892 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1893 else if (is_fixed_point_type (type
))
1894 return value_binop (value_zero (type
, not_lval
), arg1
, BINOP_SUB
);
1895 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1897 struct value
*tmp
, *val
= allocate_value (type
);
1898 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1900 LONGEST low_bound
, high_bound
;
1902 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1903 error (_("Could not determine the vector bounds"));
1905 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1907 tmp
= value_neg (value_subscript (arg1
, i
));
1908 memcpy ((value_contents_writeable (val
).data ()
1909 + i
* TYPE_LENGTH (eltype
)),
1910 value_contents_all (tmp
).data (), TYPE_LENGTH (eltype
));
1914 else if (type
->code () == TYPE_CODE_COMPLEX
)
1916 struct value
*real
= value_real_part (arg1
);
1917 struct value
*imag
= value_imaginary_part (arg1
);
1919 real
= value_neg (real
);
1920 imag
= value_neg (imag
);
1921 return value_literal_complex (real
, imag
, type
);
1924 error (_("Argument to negate operation not a number."));
1928 value_complement (struct value
*arg1
)
1933 arg1
= coerce_ref (arg1
);
1934 type
= check_typedef (value_type (arg1
));
1936 if (is_integral_type (type
))
1937 val
= value_from_longest (type
, ~value_as_long (arg1
));
1938 else if (type
->code () == TYPE_CODE_ARRAY
&& type
->is_vector ())
1941 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1943 LONGEST low_bound
, high_bound
;
1945 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1946 error (_("Could not determine the vector bounds"));
1948 val
= allocate_value (type
);
1949 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1951 tmp
= value_complement (value_subscript (arg1
, i
));
1952 memcpy ((value_contents_writeable (val
).data ()
1953 + i
* TYPE_LENGTH (eltype
)),
1954 value_contents_all (tmp
).data (), TYPE_LENGTH (eltype
));
1957 else if (type
->code () == TYPE_CODE_COMPLEX
)
1959 /* GCC has an extension that treats ~complex as the complex
1961 struct value
*real
= value_real_part (arg1
);
1962 struct value
*imag
= value_imaginary_part (arg1
);
1964 imag
= value_neg (imag
);
1965 return value_literal_complex (real
, imag
, type
);
1968 error (_("Argument to complement operation not an integer, boolean."));
1973 /* The INDEX'th bit of SET value whose value_type is TYPE,
1974 and whose value_contents is valaddr.
1975 Return -1 if out of range, -2 other error. */
1978 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1980 struct gdbarch
*gdbarch
= type
->arch ();
1981 LONGEST low_bound
, high_bound
;
1984 struct type
*range
= type
->index_type ();
1986 if (!get_discrete_bounds (range
, &low_bound
, &high_bound
))
1988 if (index
< low_bound
|| index
> high_bound
)
1990 rel_index
= index
- low_bound
;
1991 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1992 type_byte_order (type
));
1993 rel_index
%= TARGET_CHAR_BIT
;
1994 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
1995 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1996 return (word
>> rel_index
) & 1;
2000 value_in (struct value
*element
, struct value
*set
)
2003 struct type
*settype
= check_typedef (value_type (set
));
2004 struct type
*eltype
= check_typedef (value_type (element
));
2006 if (eltype
->code () == TYPE_CODE_RANGE
)
2007 eltype
= TYPE_TARGET_TYPE (eltype
);
2008 if (settype
->code () != TYPE_CODE_SET
)
2009 error (_("Second argument of 'IN' has wrong type"));
2010 if (eltype
->code () != TYPE_CODE_INT
2011 && eltype
->code () != TYPE_CODE_CHAR
2012 && eltype
->code () != TYPE_CODE_ENUM
2013 && eltype
->code () != TYPE_CODE_BOOL
)
2014 error (_("First argument of 'IN' has wrong type"));
2015 member
= value_bit_index (settype
, value_contents (set
).data (),
2016 value_as_long (element
));
2018 error (_("First argument of 'IN' not in range"));