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 (TYPE_CODE (ptr_type
) == TYPE_CODE_PTR
);
52 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
54 sz
= type_length_units (ptr_target
);
57 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
63 name
= TYPE_NAME (ptr_target
);
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 (TYPE_CODE (type1
) == TYPE_CODE_PTR
);
111 gdb_assert (TYPE_CODE (type2
) == 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 int c_style
= current_language
->c_style_arrays
;
146 array
= coerce_ref (array
);
147 tarray
= check_typedef (value_type (array
));
149 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
150 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
152 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
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
= TYPE_ARRAY_BIT_STRIDE (array_type
);
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
);
204 if (index
< lowerbound
205 || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)
206 && elt_offs
>= type_length_units (array_type
))
207 || (VALUE_LVAL (array
) != lval_memory
208 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)))
210 if (type_not_associated (array_type
))
211 error (_("no such vector element (vector not associated)"));
212 else if (type_not_allocated (array_type
))
213 error (_("no such vector element (vector not allocated)"));
215 error (_("no such vector element"));
218 if (is_dynamic_type (elt_type
))
222 address
= value_address (array
) + elt_offs
;
223 elt_type
= resolve_dynamic_type (elt_type
, {}, address
);
226 return value_from_component (array
, elt_type
, elt_offs
);
230 /* Check to see if either argument is a structure, or a reference to
231 one. This is called so we know whether to go ahead with the normal
232 binop or look for a user defined function instead.
234 For now, we do not overload the `=' operator. */
237 binop_types_user_defined_p (enum exp_opcode op
,
238 struct type
*type1
, struct type
*type2
)
240 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
243 type1
= check_typedef (type1
);
244 if (TYPE_IS_REFERENCE (type1
))
245 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
247 type2
= check_typedef (type2
);
248 if (TYPE_IS_REFERENCE (type2
))
249 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
251 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
252 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
255 /* Check to see if either argument is a structure, or a reference to
256 one. This is called so we know whether to go ahead with the normal
257 binop or look for a user defined function instead.
259 For now, we do not overload the `=' operator. */
262 binop_user_defined_p (enum exp_opcode op
,
263 struct value
*arg1
, struct value
*arg2
)
265 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
268 /* Check to see if argument is a structure. This is called so
269 we know whether to go ahead with the normal unop or look for a
270 user defined function instead.
272 For now, we do not overload the `&' operator. */
275 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
281 type1
= check_typedef (value_type (arg1
));
282 if (TYPE_IS_REFERENCE (type1
))
283 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
284 return TYPE_CODE (type1
) == TYPE_CODE_STRUCT
;
287 /* Try to find an operator named OPERATOR which takes NARGS arguments
288 specified in ARGS. If the operator found is a static member operator
289 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
290 The search if performed through find_overload_match which will handle
291 member operators, non member operators, operators imported implicitly or
292 explicitly, and perform correct overload resolution in all of the above
293 situations or combinations thereof. */
295 static struct value
*
296 value_user_defined_cpp_op (gdb::array_view
<value
*> args
, char *oper
,
297 int *static_memfuncp
, enum noside noside
)
300 struct symbol
*symp
= NULL
;
301 struct value
*valp
= NULL
;
303 find_overload_match (args
, oper
, BOTH
/* could be method */,
305 NULL
/* pass NULL symbol since symbol is unknown */,
306 &valp
, &symp
, static_memfuncp
, 0, noside
);
313 /* This is a non member function and does not
314 expect a reference as its first argument
315 rather the explicit structure. */
316 args
[0] = value_ind (args
[0]);
317 return value_of_variable (symp
, 0);
320 error (_("Could not find %s."), oper
);
323 /* Lookup user defined operator NAME. Return a value representing the
324 function, otherwise return NULL. */
326 static struct value
*
327 value_user_defined_op (struct value
**argp
, gdb::array_view
<value
*> args
,
328 char *name
, int *static_memfuncp
, enum noside noside
)
330 struct value
*result
= NULL
;
332 if (current_language
->la_language
== language_cplus
)
334 result
= value_user_defined_cpp_op (args
, name
, static_memfuncp
,
338 result
= value_struct_elt (argp
, args
.data (), name
, static_memfuncp
,
344 /* We know either arg1 or arg2 is a structure, so try to find the right
345 user defined function. Create an argument vector that calls
346 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
347 binary operator which is legal for GNU C++).
349 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
350 is the opcode saying how to modify it. Otherwise, OTHEROP is
354 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
355 enum exp_opcode otherop
, enum noside noside
)
361 arg1
= coerce_ref (arg1
);
362 arg2
= coerce_ref (arg2
);
364 /* now we know that what we have to do is construct our
365 arg vector and find the right function to call it with. */
367 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
368 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
370 value
*argvec_storage
[3];
371 gdb::array_view
<value
*> argvec
= argvec_storage
;
373 argvec
[1] = value_addr (arg1
);
376 /* Make the right function name up. */
377 strcpy (tstr
, "operator__");
402 case BINOP_BITWISE_AND
:
405 case BINOP_BITWISE_IOR
:
408 case BINOP_BITWISE_XOR
:
411 case BINOP_LOGICAL_AND
:
414 case BINOP_LOGICAL_OR
:
426 case BINOP_ASSIGN_MODIFY
:
444 case BINOP_BITWISE_AND
:
447 case BINOP_BITWISE_IOR
:
450 case BINOP_BITWISE_XOR
:
453 case BINOP_MOD
: /* invalid */
455 error (_("Invalid binary operation specified."));
458 case BINOP_SUBSCRIPT
:
479 case BINOP_MOD
: /* invalid */
481 error (_("Invalid binary operation specified."));
484 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1), tstr
,
485 &static_memfuncp
, noside
);
491 argvec
[1] = argvec
[0];
492 argvec
= argvec
.slice (1);
494 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
496 /* Static xmethods are not supported yet. */
497 gdb_assert (static_memfuncp
== 0);
498 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
500 struct type
*return_type
501 = result_type_of_xmethod (argvec
[0], argvec
.slice (1));
503 if (return_type
== NULL
)
504 error (_("Xmethod is missing return type."));
505 return value_zero (return_type
, VALUE_LVAL (arg1
));
507 return call_xmethod (argvec
[0], argvec
.slice (1));
509 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
511 struct type
*return_type
;
514 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
515 return value_zero (return_type
, VALUE_LVAL (arg1
));
517 return call_function_by_hand (argvec
[0], NULL
,
518 argvec
.slice (1, 2 - static_memfuncp
));
520 throw_error (NOT_FOUND_ERROR
,
521 _("member function %s not found"), tstr
);
524 /* We know that arg1 is a structure, so try to find a unary user
525 defined operator that matches the operator in question.
526 Create an argument vector that calls arg1.operator @ (arg1)
527 and return that value (where '@' is (almost) any unary operator which
528 is legal for GNU C++). */
531 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
533 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
535 char tstr
[13], mangle_tstr
[13];
536 int static_memfuncp
, nargs
;
538 arg1
= coerce_ref (arg1
);
540 /* now we know that what we have to do is construct our
541 arg vector and find the right function to call it with. */
543 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
544 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
546 value
*argvec_storage
[3];
547 gdb::array_view
<value
*> argvec
= argvec_storage
;
549 argvec
[1] = value_addr (arg1
);
554 /* Make the right function name up. */
555 strcpy (tstr
, "operator__");
557 strcpy (mangle_tstr
, "__");
560 case UNOP_PREINCREMENT
:
563 case UNOP_PREDECREMENT
:
566 case UNOP_POSTINCREMENT
:
568 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
571 case UNOP_POSTDECREMENT
:
573 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
576 case UNOP_LOGICAL_NOT
:
579 case UNOP_COMPLEMENT
:
595 error (_("Invalid unary operation specified."));
598 argvec
[0] = value_user_defined_op (&arg1
, argvec
.slice (1, nargs
), tstr
,
599 &static_memfuncp
, noside
);
605 argvec
[1] = argvec
[0];
606 argvec
= argvec
.slice (1);
608 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
610 /* Static xmethods are not supported yet. */
611 gdb_assert (static_memfuncp
== 0);
612 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
614 struct type
*return_type
615 = result_type_of_xmethod (argvec
[0], argvec
[1]);
617 if (return_type
== NULL
)
618 error (_("Xmethod is missing return type."));
619 return value_zero (return_type
, VALUE_LVAL (arg1
));
621 return call_xmethod (argvec
[0], argvec
[1]);
623 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
625 struct type
*return_type
;
628 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
629 return value_zero (return_type
, VALUE_LVAL (arg1
));
631 return call_function_by_hand (argvec
[0], NULL
,
632 argvec
.slice (1, nargs
));
634 throw_error (NOT_FOUND_ERROR
,
635 _("member function %s not found"), tstr
);
639 /* Concatenate two values with the following conditions:
641 (1) Both values must be either bitstring values or character string
642 values and the resulting value consists of the concatenation of
643 ARG1 followed by ARG2.
647 One value must be an integer value and the other value must be
648 either a bitstring value or character string value, which is
649 to be repeated by the number of times specified by the integer
653 (2) Boolean values are also allowed and are treated as bit string
656 (3) Character values are also allowed and are treated as character
657 string values of length 1. */
660 value_concat (struct value
*arg1
, struct value
*arg2
)
662 struct value
*inval1
;
663 struct value
*inval2
;
664 struct value
*outval
= NULL
;
665 int inval1len
, inval2len
;
668 struct type
*type1
= check_typedef (value_type (arg1
));
669 struct type
*type2
= check_typedef (value_type (arg2
));
670 struct type
*char_type
;
672 /* First figure out if we are dealing with two values to be concatenated
673 or a repeat count and a value to be repeated. INVAL1 is set to the
674 first of two concatenated values, or the repeat count. INVAL2 is set
675 to the second of the two concatenated values or the value to be
678 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
680 struct type
*tmp
= type1
;
693 /* Now process the input values. */
695 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
697 /* We have a repeat count. Validate the second value and then
698 construct a value repeated that many times. */
699 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
700 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
702 count
= longest_to_int (value_as_long (inval1
));
703 inval2len
= TYPE_LENGTH (type2
);
704 std::vector
<char> ptr (count
* inval2len
);
705 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
709 inchar
= (char) unpack_long (type2
,
710 value_contents (inval2
));
711 for (idx
= 0; idx
< count
; idx
++)
718 char_type
= TYPE_TARGET_TYPE (type2
);
720 for (idx
= 0; idx
< count
; idx
++)
722 memcpy (&ptr
[idx
* inval2len
], value_contents (inval2
),
726 outval
= value_string (ptr
.data (), count
* inval2len
, char_type
);
728 else if (TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
730 error (_("unimplemented support for boolean repeats"));
734 error (_("can't repeat values of that type"));
737 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
738 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
740 /* We have two character strings to concatenate. */
741 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
742 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
744 error (_("Strings can only be concatenated with other strings."));
746 inval1len
= TYPE_LENGTH (type1
);
747 inval2len
= TYPE_LENGTH (type2
);
748 std::vector
<char> ptr (inval1len
+ inval2len
);
749 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
753 ptr
[0] = (char) unpack_long (type1
, value_contents (inval1
));
757 char_type
= TYPE_TARGET_TYPE (type1
);
759 memcpy (ptr
.data (), value_contents (inval1
), inval1len
);
761 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
764 (char) unpack_long (type2
, value_contents (inval2
));
768 memcpy (&ptr
[inval1len
], value_contents (inval2
), inval2len
);
770 outval
= value_string (ptr
.data (), inval1len
+ inval2len
, char_type
);
772 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
774 /* We have two bitstrings to concatenate. */
775 if (TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
777 error (_("Booleans can only be concatenated "
778 "with other bitstrings or booleans."));
780 error (_("unimplemented support for boolean concatenation."));
784 /* We don't know how to concatenate these operands. */
785 error (_("illegal operands for concatenation."));
790 /* Integer exponentiation: V1**V2, where both arguments are
791 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
794 integer_pow (LONGEST v1
, LONGEST v2
)
799 error (_("Attempt to raise 0 to negative power."));
805 /* The Russian Peasant's Algorithm. */
821 /* Integer exponentiation: V1**V2, where both arguments are
822 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
825 uinteger_pow (ULONGEST v1
, LONGEST v2
)
830 error (_("Attempt to raise 0 to negative power."));
836 /* The Russian Peasant's Algorithm. */
852 /* Obtain argument values for binary operation, converting from
853 other types if one of them is not floating point. */
855 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
856 gdb_byte
*x
, struct type
**eff_type_x
,
857 gdb_byte
*y
, struct type
**eff_type_y
)
859 struct type
*type1
, *type2
;
861 type1
= check_typedef (value_type (arg1
));
862 type2
= check_typedef (value_type (arg2
));
864 /* At least one of the arguments must be of floating-point type. */
865 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
867 if (is_floating_type (type1
) && is_floating_type (type2
)
868 && TYPE_CODE (type1
) != TYPE_CODE (type2
))
869 /* The DFP extension to the C language does not allow mixing of
870 * decimal float types with other float types in expressions
871 * (see WDTR 24732, page 12). */
872 error (_("Mixing decimal floating types with "
873 "other floating types is not allowed."));
875 /* Obtain value of arg1, converting from other types if necessary. */
877 if (is_floating_type (type1
))
880 memcpy (x
, value_contents (arg1
), TYPE_LENGTH (type1
));
882 else if (is_integral_type (type1
))
885 if (TYPE_UNSIGNED (type1
))
886 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
888 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
891 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
894 /* Obtain value of arg2, converting from other types if necessary. */
896 if (is_floating_type (type2
))
899 memcpy (y
, value_contents (arg2
), TYPE_LENGTH (type2
));
901 else if (is_integral_type (type2
))
904 if (TYPE_UNSIGNED (type2
))
905 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
907 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
910 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
914 /* A helper function that finds the type to use for a binary operation
915 involving TYPE1 and TYPE2. */
918 promotion_type (struct type
*type1
, struct type
*type2
)
920 struct type
*result_type
;
922 if (is_floating_type (type1
) || is_floating_type (type2
))
924 /* If only one type is floating-point, use its type.
925 Otherwise use the bigger type. */
926 if (!is_floating_type (type1
))
928 else if (!is_floating_type (type2
))
930 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
938 if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
940 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
942 else if (TYPE_UNSIGNED (type1
))
944 else if (TYPE_UNSIGNED (type2
))
953 static struct value
*scalar_binop (struct value
*arg1
, struct value
*arg2
,
956 /* Perform a binary operation on complex operands. */
958 static struct value
*
959 complex_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
961 struct type
*arg1_type
= check_typedef (value_type (arg1
));
962 struct type
*arg2_type
= check_typedef (value_type (arg2
));
964 struct value
*arg1_real
, *arg1_imag
, *arg2_real
, *arg2_imag
;
965 if (TYPE_CODE (arg1_type
) == TYPE_CODE_COMPLEX
)
967 arg1_real
= value_real_part (arg1
);
968 arg1_imag
= value_imaginary_part (arg1
);
973 arg1_imag
= value_zero (arg1_type
, not_lval
);
975 if (TYPE_CODE (arg2_type
) == TYPE_CODE_COMPLEX
)
977 arg2_real
= value_real_part (arg2
);
978 arg2_imag
= value_imaginary_part (arg2
);
983 arg2_imag
= value_zero (arg2_type
, not_lval
);
986 struct type
*comp_type
= promotion_type (value_type (arg1_real
),
987 value_type (arg2_real
));
988 arg1_real
= value_cast (comp_type
, arg1_real
);
989 arg1_imag
= value_cast (comp_type
, arg1_imag
);
990 arg2_real
= value_cast (comp_type
, arg2_real
);
991 arg2_imag
= value_cast (comp_type
, arg2_imag
);
993 struct type
*result_type
= init_complex_type (nullptr, comp_type
);
995 struct value
*result_real
, *result_imag
;
1000 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1001 result_imag
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1006 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1007 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1008 result_real
= scalar_binop (x1
, x2
, BINOP_SUB
);
1010 x1
= scalar_binop (arg1_real
, arg2_imag
, op
);
1011 x2
= scalar_binop (arg1_imag
, arg2_real
, op
);
1012 result_imag
= scalar_binop (x1
, x2
, BINOP_ADD
);
1018 if (TYPE_CODE (arg2_type
) == TYPE_CODE_COMPLEX
)
1020 struct value
*conjugate
= value_complement (arg2
);
1021 /* We have to reconstruct ARG1, in case the type was
1023 arg1
= value_literal_complex (arg1_real
, arg1_imag
, result_type
);
1025 struct value
*numerator
= scalar_binop (arg1
, conjugate
,
1027 arg1_real
= value_real_part (numerator
);
1028 arg1_imag
= value_imaginary_part (numerator
);
1030 struct value
*x1
= scalar_binop (arg2_real
, arg2_real
, BINOP_MUL
);
1031 struct value
*x2
= scalar_binop (arg2_imag
, arg2_imag
, BINOP_MUL
);
1032 arg2_real
= scalar_binop (x1
, x2
, BINOP_ADD
);
1035 result_real
= scalar_binop (arg1_real
, arg2_real
, op
);
1036 result_imag
= scalar_binop (arg1_imag
, arg2_real
, op
);
1041 case BINOP_NOTEQUAL
:
1043 struct value
*x1
= scalar_binop (arg1_real
, arg2_real
, op
);
1044 struct value
*x2
= scalar_binop (arg1_imag
, arg2_imag
, op
);
1046 LONGEST v1
= value_as_long (x1
);
1047 LONGEST v2
= value_as_long (x2
);
1049 if (op
== BINOP_EQUAL
)
1054 return value_from_longest (value_type (x1
), v1
);
1059 error (_("Invalid binary operation on numbers."));
1062 return value_literal_complex (result_real
, result_imag
, result_type
);
1065 /* Perform a binary operation on two operands which have reasonable
1066 representations as integers or floats. This includes booleans,
1067 characters, integers, or floats.
1068 Does not support addition and subtraction on pointers;
1069 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1071 static struct value
*
1072 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1075 struct type
*type1
, *type2
, *result_type
;
1077 arg1
= coerce_ref (arg1
);
1078 arg2
= coerce_ref (arg2
);
1080 type1
= check_typedef (value_type (arg1
));
1081 type2
= check_typedef (value_type (arg2
));
1083 if (TYPE_CODE (type1
) == TYPE_CODE_COMPLEX
1084 || TYPE_CODE (type2
) == TYPE_CODE_COMPLEX
)
1085 return complex_binop (arg1
, arg2
, op
);
1087 if ((!is_floating_value (arg1
) && !is_integral_type (type1
))
1088 || (!is_floating_value (arg2
) && !is_integral_type (type2
)))
1089 error (_("Argument to arithmetic operation not a number or boolean."));
1091 if (is_floating_type (type1
) || is_floating_type (type2
))
1093 result_type
= promotion_type (type1
, type2
);
1094 val
= allocate_value (result_type
);
1096 struct type
*eff_type_v1
, *eff_type_v2
;
1097 gdb::byte_vector v1
, v2
;
1098 v1
.resize (TYPE_LENGTH (result_type
));
1099 v2
.resize (TYPE_LENGTH (result_type
));
1101 value_args_as_target_float (arg1
, arg2
,
1102 v1
.data (), &eff_type_v1
,
1103 v2
.data (), &eff_type_v2
);
1104 target_float_binop (op
, v1
.data (), eff_type_v1
,
1105 v2
.data (), eff_type_v2
,
1106 value_contents_raw (val
), result_type
);
1108 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
1109 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
1111 LONGEST v1
, v2
, v
= 0;
1113 v1
= value_as_long (arg1
);
1114 v2
= value_as_long (arg2
);
1118 case BINOP_BITWISE_AND
:
1122 case BINOP_BITWISE_IOR
:
1126 case BINOP_BITWISE_XOR
:
1134 case BINOP_NOTEQUAL
:
1139 error (_("Invalid operation on booleans."));
1142 result_type
= type1
;
1144 val
= allocate_value (result_type
);
1145 store_signed_integer (value_contents_raw (val
),
1146 TYPE_LENGTH (result_type
),
1147 type_byte_order (result_type
),
1151 /* Integral operations here. */
1153 /* Determine type length of the result, and if the operation should
1154 be done unsigned. For exponentiation and shift operators,
1155 use the length and type of the left operand. Otherwise,
1156 use the signedness of the operand with the greater length.
1157 If both operands are of equal length, use unsigned operation
1158 if one of the operands is unsigned. */
1159 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1160 result_type
= type1
;
1162 result_type
= promotion_type (type1
, type2
);
1164 if (TYPE_UNSIGNED (result_type
))
1166 LONGEST v2_signed
= value_as_long (arg2
);
1167 ULONGEST v1
, v2
, v
= 0;
1169 v1
= (ULONGEST
) value_as_long (arg1
);
1170 v2
= (ULONGEST
) v2_signed
;
1191 error (_("Division by zero"));
1195 v
= uinteger_pow (v1
, v2_signed
);
1202 error (_("Division by zero"));
1206 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1207 v1 mod 0 has a defined value, v1. */
1215 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1228 case BINOP_BITWISE_AND
:
1232 case BINOP_BITWISE_IOR
:
1236 case BINOP_BITWISE_XOR
:
1240 case BINOP_LOGICAL_AND
:
1244 case BINOP_LOGICAL_OR
:
1249 v
= v1
< v2
? v1
: v2
;
1253 v
= v1
> v2
? v1
: v2
;
1260 case BINOP_NOTEQUAL
:
1281 error (_("Invalid binary operation on numbers."));
1284 val
= allocate_value (result_type
);
1285 store_unsigned_integer (value_contents_raw (val
),
1286 TYPE_LENGTH (value_type (val
)),
1287 type_byte_order (result_type
),
1292 LONGEST v1
, v2
, v
= 0;
1294 v1
= value_as_long (arg1
);
1295 v2
= value_as_long (arg2
);
1316 error (_("Division by zero"));
1320 v
= integer_pow (v1
, v2
);
1327 error (_("Division by zero"));
1331 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1332 X mod 0 has a defined value, X. */
1340 /* Compute floor. */
1341 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1357 case BINOP_BITWISE_AND
:
1361 case BINOP_BITWISE_IOR
:
1365 case BINOP_BITWISE_XOR
:
1369 case BINOP_LOGICAL_AND
:
1373 case BINOP_LOGICAL_OR
:
1378 v
= v1
< v2
? v1
: v2
;
1382 v
= v1
> v2
? v1
: v2
;
1389 case BINOP_NOTEQUAL
:
1410 error (_("Invalid binary operation on numbers."));
1413 val
= allocate_value (result_type
);
1414 store_signed_integer (value_contents_raw (val
),
1415 TYPE_LENGTH (value_type (val
)),
1416 type_byte_order (result_type
),
1424 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1425 replicating SCALAR_VALUE for each element of the vector. Only scalar
1426 types that can be cast to the type of one element of the vector are
1427 acceptable. The newly created vector value is returned upon success,
1428 otherwise an error is thrown. */
1431 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1433 /* Widen the scalar to a vector. */
1434 struct type
*eltype
, *scalar_type
;
1435 struct value
*val
, *elval
;
1436 LONGEST low_bound
, high_bound
;
1439 vector_type
= check_typedef (vector_type
);
1441 gdb_assert (TYPE_CODE (vector_type
) == TYPE_CODE_ARRAY
1442 && TYPE_VECTOR (vector_type
));
1444 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1445 error (_("Could not determine the vector bounds"));
1447 eltype
= check_typedef (TYPE_TARGET_TYPE (vector_type
));
1448 elval
= value_cast (eltype
, scalar_value
);
1450 scalar_type
= check_typedef (value_type (scalar_value
));
1452 /* If we reduced the length of the scalar then check we didn't loose any
1454 if (TYPE_LENGTH (eltype
) < TYPE_LENGTH (scalar_type
)
1455 && !value_equal (elval
, scalar_value
))
1456 error (_("conversion of scalar to vector involves truncation"));
1458 val
= allocate_value (vector_type
);
1459 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1460 /* Duplicate the contents of elval into the destination vector. */
1461 memcpy (value_contents_writeable (val
) + (i
* TYPE_LENGTH (eltype
)),
1462 value_contents_all (elval
), TYPE_LENGTH (eltype
));
1467 /* Performs a binary operation on two vector operands by calling scalar_binop
1468 for each pair of vector components. */
1470 static struct value
*
1471 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1473 struct value
*val
, *tmp
, *mark
;
1474 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1475 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1476 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1478 type1
= check_typedef (value_type (val1
));
1479 type2
= check_typedef (value_type (val2
));
1481 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1482 && TYPE_VECTOR (type1
)) ? 1 : 0;
1483 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1484 && TYPE_VECTOR (type2
)) ? 1 : 0;
1486 if (!t1_is_vec
|| !t2_is_vec
)
1487 error (_("Vector operations are only supported among vectors"));
1489 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1490 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1491 error (_("Could not determine the vector bounds"));
1493 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1494 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1495 elsize
= TYPE_LENGTH (eltype1
);
1497 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
1498 || elsize
!= TYPE_LENGTH (eltype2
)
1499 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
1500 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1501 error (_("Cannot perform operation on vectors with different types"));
1503 val
= allocate_value (type1
);
1504 mark
= value_mark ();
1505 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1507 tmp
= value_binop (value_subscript (val1
, i
),
1508 value_subscript (val2
, i
), op
);
1509 memcpy (value_contents_writeable (val
) + i
* elsize
,
1510 value_contents_all (tmp
),
1513 value_free_to_mark (mark
);
1518 /* Perform a binary operation on two operands. */
1521 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1524 struct type
*type1
= check_typedef (value_type (arg1
));
1525 struct type
*type2
= check_typedef (value_type (arg2
));
1526 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1527 && TYPE_VECTOR (type1
));
1528 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1529 && TYPE_VECTOR (type2
));
1531 if (!t1_is_vec
&& !t2_is_vec
)
1532 val
= scalar_binop (arg1
, arg2
, op
);
1533 else if (t1_is_vec
&& t2_is_vec
)
1534 val
= vector_binop (arg1
, arg2
, op
);
1537 /* Widen the scalar operand to a vector. */
1538 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1539 struct type
*t
= t1_is_vec
? type2
: type1
;
1541 if (TYPE_CODE (t
) != TYPE_CODE_FLT
1542 && TYPE_CODE (t
) != TYPE_CODE_DECFLOAT
1543 && !is_integral_type (t
))
1544 error (_("Argument to operation not a number or boolean."));
1546 /* Replicate the scalar value to make a vector value. */
1547 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1549 val
= vector_binop (arg1
, arg2
, op
);
1555 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1558 value_logical_not (struct value
*arg1
)
1564 arg1
= coerce_array (arg1
);
1565 type1
= check_typedef (value_type (arg1
));
1567 if (is_floating_value (arg1
))
1568 return target_float_is_zero (value_contents (arg1
), type1
);
1570 len
= TYPE_LENGTH (type1
);
1571 p
= value_contents (arg1
);
1582 /* Perform a comparison on two string values (whose content are not
1583 necessarily null terminated) based on their length. */
1586 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1588 int len1
= TYPE_LENGTH (value_type (arg1
));
1589 int len2
= TYPE_LENGTH (value_type (arg2
));
1590 const gdb_byte
*s1
= value_contents (arg1
);
1591 const gdb_byte
*s2
= value_contents (arg2
);
1592 int i
, len
= len1
< len2
? len1
: len2
;
1594 for (i
= 0; i
< len
; i
++)
1598 else if (s1
[i
] > s2
[i
])
1606 else if (len1
> len2
)
1612 /* Simulate the C operator == by returning a 1
1613 iff ARG1 and ARG2 have equal contents. */
1616 value_equal (struct value
*arg1
, struct value
*arg2
)
1621 struct type
*type1
, *type2
;
1622 enum type_code code1
;
1623 enum type_code code2
;
1624 int is_int1
, is_int2
;
1626 arg1
= coerce_array (arg1
);
1627 arg2
= coerce_array (arg2
);
1629 type1
= check_typedef (value_type (arg1
));
1630 type2
= check_typedef (value_type (arg2
));
1631 code1
= TYPE_CODE (type1
);
1632 code2
= TYPE_CODE (type2
);
1633 is_int1
= is_integral_type (type1
);
1634 is_int2
= is_integral_type (type2
);
1636 if (is_int1
&& is_int2
)
1637 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1639 else if ((is_floating_value (arg1
) || is_int1
)
1640 && (is_floating_value (arg2
) || is_int2
))
1642 struct type
*eff_type_v1
, *eff_type_v2
;
1643 gdb::byte_vector v1
, v2
;
1644 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1645 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1647 value_args_as_target_float (arg1
, arg2
,
1648 v1
.data (), &eff_type_v1
,
1649 v2
.data (), &eff_type_v2
);
1651 return target_float_compare (v1
.data (), eff_type_v1
,
1652 v2
.data (), eff_type_v2
) == 0;
1655 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1657 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1658 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1659 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1660 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1662 else if (code1
== code2
1663 && ((len
= (int) TYPE_LENGTH (type1
))
1664 == (int) TYPE_LENGTH (type2
)))
1666 p1
= value_contents (arg1
);
1667 p2
= value_contents (arg2
);
1675 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1677 return value_strcmp (arg1
, arg2
) == 0;
1680 error (_("Invalid type combination in equality test."));
1683 /* Compare values based on their raw contents. Useful for arrays since
1684 value_equal coerces them to pointers, thus comparing just the address
1685 of the array instead of its contents. */
1688 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1690 struct type
*type1
, *type2
;
1692 type1
= check_typedef (value_type (arg1
));
1693 type2
= check_typedef (value_type (arg2
));
1695 return (TYPE_CODE (type1
) == TYPE_CODE (type2
)
1696 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1697 && memcmp (value_contents (arg1
), value_contents (arg2
),
1698 TYPE_LENGTH (type1
)) == 0);
1701 /* Simulate the C operator < by returning 1
1702 iff ARG1's contents are less than ARG2's. */
1705 value_less (struct value
*arg1
, struct value
*arg2
)
1707 enum type_code code1
;
1708 enum type_code code2
;
1709 struct type
*type1
, *type2
;
1710 int is_int1
, is_int2
;
1712 arg1
= coerce_array (arg1
);
1713 arg2
= coerce_array (arg2
);
1715 type1
= check_typedef (value_type (arg1
));
1716 type2
= check_typedef (value_type (arg2
));
1717 code1
= TYPE_CODE (type1
);
1718 code2
= TYPE_CODE (type2
);
1719 is_int1
= is_integral_type (type1
);
1720 is_int2
= is_integral_type (type2
);
1722 if (is_int1
&& is_int2
)
1723 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1725 else if ((is_floating_value (arg1
) || is_int1
)
1726 && (is_floating_value (arg2
) || is_int2
))
1728 struct type
*eff_type_v1
, *eff_type_v2
;
1729 gdb::byte_vector v1
, v2
;
1730 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1731 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1733 value_args_as_target_float (arg1
, arg2
,
1734 v1
.data (), &eff_type_v1
,
1735 v2
.data (), &eff_type_v2
);
1737 return target_float_compare (v1
.data (), eff_type_v1
,
1738 v2
.data (), eff_type_v2
) == -1;
1740 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1741 return value_as_address (arg1
) < value_as_address (arg2
);
1743 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1745 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1746 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1747 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1748 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1749 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1750 return value_strcmp (arg1
, arg2
) < 0;
1753 error (_("Invalid type combination in ordering comparison."));
1758 /* The unary operators +, - and ~. They free the argument ARG1. */
1761 value_pos (struct value
*arg1
)
1765 arg1
= coerce_ref (arg1
);
1766 type
= check_typedef (value_type (arg1
));
1768 if (is_integral_type (type
) || is_floating_value (arg1
)
1769 || (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1770 || TYPE_CODE (type
) == TYPE_CODE_COMPLEX
)
1771 return value_from_contents (type
, value_contents (arg1
));
1773 error (_("Argument to positive operation not a number."));
1777 value_neg (struct value
*arg1
)
1781 arg1
= coerce_ref (arg1
);
1782 type
= check_typedef (value_type (arg1
));
1784 if (is_integral_type (type
) || is_floating_type (type
))
1785 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1786 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1788 struct value
*tmp
, *val
= allocate_value (type
);
1789 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1791 LONGEST low_bound
, high_bound
;
1793 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1794 error (_("Could not determine the vector bounds"));
1796 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1798 tmp
= value_neg (value_subscript (arg1
, i
));
1799 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1800 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1804 else if (TYPE_CODE (type
) == TYPE_CODE_COMPLEX
)
1806 struct value
*real
= value_real_part (arg1
);
1807 struct value
*imag
= value_imaginary_part (arg1
);
1809 real
= value_neg (real
);
1810 imag
= value_neg (imag
);
1811 return value_literal_complex (real
, imag
, type
);
1814 error (_("Argument to negate operation not a number."));
1818 value_complement (struct value
*arg1
)
1823 arg1
= coerce_ref (arg1
);
1824 type
= check_typedef (value_type (arg1
));
1826 if (is_integral_type (type
))
1827 val
= value_from_longest (type
, ~value_as_long (arg1
));
1828 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1831 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1833 LONGEST low_bound
, high_bound
;
1835 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1836 error (_("Could not determine the vector bounds"));
1838 val
= allocate_value (type
);
1839 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1841 tmp
= value_complement (value_subscript (arg1
, i
));
1842 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1843 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1846 else if (TYPE_CODE (type
) == TYPE_CODE_COMPLEX
)
1848 /* GCC has an extension that treats ~complex as the complex
1850 struct value
*real
= value_real_part (arg1
);
1851 struct value
*imag
= value_imaginary_part (arg1
);
1853 imag
= value_neg (imag
);
1854 return value_literal_complex (real
, imag
, type
);
1857 error (_("Argument to complement operation not an integer, boolean."));
1862 /* The INDEX'th bit of SET value whose value_type is TYPE,
1863 and whose value_contents is valaddr.
1864 Return -1 if out of range, -2 other error. */
1867 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1869 struct gdbarch
*gdbarch
= get_type_arch (type
);
1870 LONGEST low_bound
, high_bound
;
1873 struct type
*range
= TYPE_INDEX_TYPE (type
);
1875 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1877 if (index
< low_bound
|| index
> high_bound
)
1879 rel_index
= index
- low_bound
;
1880 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1881 type_byte_order (type
));
1882 rel_index
%= TARGET_CHAR_BIT
;
1883 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
1884 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1885 return (word
>> rel_index
) & 1;
1889 value_in (struct value
*element
, struct value
*set
)
1892 struct type
*settype
= check_typedef (value_type (set
));
1893 struct type
*eltype
= check_typedef (value_type (element
));
1895 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1896 eltype
= TYPE_TARGET_TYPE (eltype
);
1897 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1898 error (_("Second argument of 'IN' has wrong type"));
1899 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1900 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1901 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1902 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1903 error (_("First argument of 'IN' has wrong type"));
1904 member
= value_bit_index (settype
, value_contents (set
),
1905 value_as_long (element
));
1907 error (_("First argument of 'IN' not in range"));
This page took 0.14398 seconds and 4 git commands to generate.