1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2017 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 "common/byte-vector.h"
31 /* Define whether or not the C operator '/' truncates towards zero for
32 differently signed operands (truncation direction is undefined in C). */
34 #ifndef TRUNCATION_TOWARDS_ZERO
35 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
38 /* Given a pointer, return the size of its target.
39 If the pointer type is void *, then return 1.
40 If the target type is incomplete, then error out.
41 This isn't a general purpose function, but just a
42 helper for value_ptradd. */
45 find_size_for_pointer_math (struct type
*ptr_type
)
48 struct type
*ptr_target
;
50 gdb_assert (TYPE_CODE (ptr_type
) == TYPE_CODE_PTR
);
51 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
53 sz
= type_length_units (ptr_target
);
56 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
62 name
= TYPE_NAME (ptr_target
);
64 name
= TYPE_TAG_NAME (ptr_target
);
66 error (_("Cannot perform pointer math on incomplete types, "
67 "try casting to a known type, or void *."));
69 error (_("Cannot perform pointer math on incomplete type \"%s\", "
70 "try casting to a known type, or void *."), name
);
76 /* Given a pointer ARG1 and an integral value ARG2, return the
77 result of C-style pointer arithmetic ARG1 + ARG2. */
80 value_ptradd (struct value
*arg1
, LONGEST arg2
)
82 struct type
*valptrtype
;
86 arg1
= coerce_array (arg1
);
87 valptrtype
= check_typedef (value_type (arg1
));
88 sz
= find_size_for_pointer_math (valptrtype
);
90 result
= value_from_pointer (valptrtype
,
91 value_as_address (arg1
) + sz
* arg2
);
92 if (VALUE_LVAL (result
) != lval_internalvar
)
93 set_value_component_location (result
, arg1
);
97 /* Given two compatible pointer values ARG1 and ARG2, return the
98 result of C-style pointer arithmetic ARG1 - ARG2. */
101 value_ptrdiff (struct value
*arg1
, struct value
*arg2
)
103 struct type
*type1
, *type2
;
106 arg1
= coerce_array (arg1
);
107 arg2
= coerce_array (arg2
);
108 type1
= check_typedef (value_type (arg1
));
109 type2
= check_typedef (value_type (arg2
));
111 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_PTR
);
112 gdb_assert (TYPE_CODE (type2
) == TYPE_CODE_PTR
);
114 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
115 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
116 error (_("First argument of `-' is a pointer and "
117 "second argument is neither\n"
118 "an integer nor a pointer of the same type."));
120 sz
= type_length_units (check_typedef (TYPE_TARGET_TYPE (type1
)));
123 warning (_("Type size unknown, assuming 1. "
124 "Try casting to a known type, or void *."));
128 return (value_as_long (arg1
) - value_as_long (arg2
)) / sz
;
131 /* Return the value of ARRAY[IDX].
133 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
134 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
136 See comments in value_coerce_array() for rationale for reason for
137 doing lower bounds adjustment here rather than there.
138 FIXME: Perhaps we should validate that the index is valid and if
139 verbosity is set, warn about invalid indices (but still use them). */
142 value_subscript (struct value
*array
, LONGEST index
)
144 int c_style
= current_language
->c_style_arrays
;
147 array
= coerce_ref (array
);
148 tarray
= check_typedef (value_type (array
));
150 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
151 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
153 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
154 LONGEST lowerbound
, upperbound
;
156 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
157 if (VALUE_LVAL (array
) != lval_memory
)
158 return value_subscripted_rvalue (array
, index
, lowerbound
);
162 if (index
>= lowerbound
&& index
<= upperbound
)
163 return value_subscripted_rvalue (array
, index
, lowerbound
);
164 /* Emit warning unless we have an array of unknown size.
165 An array of unknown size has lowerbound 0 and upperbound -1. */
167 warning (_("array or string index out of range"));
168 /* fall doing C stuff */
173 array
= value_coerce_array (array
);
177 return value_ind (value_ptradd (array
, index
));
179 error (_("not an array or string"));
182 /* Return the value of EXPR[IDX], expr an aggregate rvalue
183 (eg, a vector register). This routine used to promote floats
184 to doubles, but no longer does. */
187 value_subscripted_rvalue (struct value
*array
, LONGEST index
, int lowerbound
)
189 struct type
*array_type
= check_typedef (value_type (array
));
190 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
191 ULONGEST elt_size
= type_length_units (elt_type
);
192 ULONGEST elt_offs
= elt_size
* (index
- lowerbound
);
194 if (index
< lowerbound
|| (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)
195 && elt_offs
>= type_length_units (array_type
)))
197 if (type_not_associated (array_type
))
198 error (_("no such vector element (vector not associated)"));
199 else if (type_not_allocated (array_type
))
200 error (_("no such vector element (vector not allocated)"));
202 error (_("no such vector element"));
205 if (is_dynamic_type (elt_type
))
209 address
= value_address (array
) + elt_offs
;
210 elt_type
= resolve_dynamic_type (elt_type
, NULL
, address
);
213 return value_from_component (array
, elt_type
, elt_offs
);
217 /* Check to see if either argument is a structure, or a reference to
218 one. This is called so we know whether to go ahead with the normal
219 binop or look for a user defined function instead.
221 For now, we do not overload the `=' operator. */
224 binop_types_user_defined_p (enum exp_opcode op
,
225 struct type
*type1
, struct type
*type2
)
227 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
230 type1
= check_typedef (type1
);
231 if (TYPE_IS_REFERENCE (type1
))
232 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
234 type2
= check_typedef (type2
);
235 if (TYPE_IS_REFERENCE (type2
))
236 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
238 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
239 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
242 /* Check to see if either argument is a structure, or a reference to
243 one. This is called so we know whether to go ahead with the normal
244 binop or look for a user defined function instead.
246 For now, we do not overload the `=' operator. */
249 binop_user_defined_p (enum exp_opcode op
,
250 struct value
*arg1
, struct value
*arg2
)
252 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
255 /* Check to see if argument is a structure. This is called so
256 we know whether to go ahead with the normal unop or look for a
257 user defined function instead.
259 For now, we do not overload the `&' operator. */
262 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
268 type1
= check_typedef (value_type (arg1
));
269 if (TYPE_IS_REFERENCE (type1
))
270 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
271 return TYPE_CODE (type1
) == TYPE_CODE_STRUCT
;
274 /* Try to find an operator named OPERATOR which takes NARGS arguments
275 specified in ARGS. If the operator found is a static member operator
276 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
277 The search if performed through find_overload_match which will handle
278 member operators, non member operators, operators imported implicitly or
279 explicitly, and perform correct overload resolution in all of the above
280 situations or combinations thereof. */
282 static struct value
*
283 value_user_defined_cpp_op (struct value
**args
, int nargs
, char *oper
,
284 int *static_memfuncp
, enum noside noside
)
287 struct symbol
*symp
= NULL
;
288 struct value
*valp
= NULL
;
290 find_overload_match (args
, nargs
, oper
, BOTH
/* could be method */,
292 NULL
/* pass NULL symbol since symbol is unknown */,
293 &valp
, &symp
, static_memfuncp
, 0, noside
);
300 /* This is a non member function and does not
301 expect a reference as its first argument
302 rather the explicit structure. */
303 args
[0] = value_ind (args
[0]);
304 return value_of_variable (symp
, 0);
307 error (_("Could not find %s."), oper
);
310 /* Lookup user defined operator NAME. Return a value representing the
311 function, otherwise return NULL. */
313 static struct value
*
314 value_user_defined_op (struct value
**argp
, struct value
**args
, char *name
,
315 int *static_memfuncp
, int nargs
, enum noside noside
)
317 struct value
*result
= NULL
;
319 if (current_language
->la_language
== language_cplus
)
321 result
= value_user_defined_cpp_op (args
, nargs
, name
, static_memfuncp
,
325 result
= value_struct_elt (argp
, args
, name
, static_memfuncp
,
331 /* We know either arg1 or arg2 is a structure, so try to find the right
332 user defined function. Create an argument vector that calls
333 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
334 binary operator which is legal for GNU C++).
336 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
337 is the opcode saying how to modify it. Otherwise, OTHEROP is
341 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
342 enum exp_opcode otherop
, enum noside noside
)
344 struct value
**argvec
;
349 arg1
= coerce_ref (arg1
);
350 arg2
= coerce_ref (arg2
);
352 /* now we know that what we have to do is construct our
353 arg vector and find the right function to call it with. */
355 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
356 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
358 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
359 argvec
[1] = value_addr (arg1
);
363 /* Make the right function name up. */
364 strcpy (tstr
, "operator__");
389 case BINOP_BITWISE_AND
:
392 case BINOP_BITWISE_IOR
:
395 case BINOP_BITWISE_XOR
:
398 case BINOP_LOGICAL_AND
:
401 case BINOP_LOGICAL_OR
:
413 case BINOP_ASSIGN_MODIFY
:
431 case BINOP_BITWISE_AND
:
434 case BINOP_BITWISE_IOR
:
437 case BINOP_BITWISE_XOR
:
440 case BINOP_MOD
: /* invalid */
442 error (_("Invalid binary operation specified."));
445 case BINOP_SUBSCRIPT
:
466 case BINOP_MOD
: /* invalid */
468 error (_("Invalid binary operation specified."));
471 argvec
[0] = value_user_defined_op (&arg1
, argvec
+ 1, tstr
,
472 &static_memfuncp
, 2, noside
);
478 argvec
[1] = argvec
[0];
481 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
483 /* Static xmethods are not supported yet. */
484 gdb_assert (static_memfuncp
== 0);
485 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
487 struct type
*return_type
488 = result_type_of_xmethod (argvec
[0], 2, argvec
+ 1);
490 if (return_type
== NULL
)
491 error (_("Xmethod is missing return type."));
492 return value_zero (return_type
, VALUE_LVAL (arg1
));
494 return call_xmethod (argvec
[0], 2, argvec
+ 1);
496 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
498 struct type
*return_type
;
501 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
502 return value_zero (return_type
, VALUE_LVAL (arg1
));
504 return call_function_by_hand (argvec
[0], NULL
, 2 - static_memfuncp
,
507 throw_error (NOT_FOUND_ERROR
,
508 _("member function %s not found"), tstr
);
510 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
514 /* We know that arg1 is a structure, so try to find a unary user
515 defined operator that matches the operator in question.
516 Create an argument vector that calls arg1.operator @ (arg1)
517 and return that value (where '@' is (almost) any unary operator which
518 is legal for GNU C++). */
521 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
523 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
524 struct value
**argvec
;
526 char tstr
[13], mangle_tstr
[13];
527 int static_memfuncp
, nargs
;
529 arg1
= coerce_ref (arg1
);
531 /* now we know that what we have to do is construct our
532 arg vector and find the right function to call it with. */
534 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
535 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
537 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
538 argvec
[1] = value_addr (arg1
);
543 /* Make the right function name up. */
544 strcpy (tstr
, "operator__");
546 strcpy (mangle_tstr
, "__");
549 case UNOP_PREINCREMENT
:
552 case UNOP_PREDECREMENT
:
555 case UNOP_POSTINCREMENT
:
557 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
561 case UNOP_POSTDECREMENT
:
563 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
567 case UNOP_LOGICAL_NOT
:
570 case UNOP_COMPLEMENT
:
586 error (_("Invalid unary operation specified."));
589 argvec
[0] = value_user_defined_op (&arg1
, argvec
+ 1, tstr
,
590 &static_memfuncp
, nargs
, noside
);
596 argvec
[1] = argvec
[0];
600 if (TYPE_CODE (value_type (argvec
[0])) == TYPE_CODE_XMETHOD
)
602 /* Static xmethods are not supported yet. */
603 gdb_assert (static_memfuncp
== 0);
604 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
606 struct type
*return_type
607 = result_type_of_xmethod (argvec
[0], 1, argvec
+ 1);
609 if (return_type
== NULL
)
610 error (_("Xmethod is missing return type."));
611 return value_zero (return_type
, VALUE_LVAL (arg1
));
613 return call_xmethod (argvec
[0], 1, argvec
+ 1);
615 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
617 struct type
*return_type
;
620 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
621 return value_zero (return_type
, VALUE_LVAL (arg1
));
623 return call_function_by_hand (argvec
[0], NULL
, nargs
, argvec
+ 1);
625 throw_error (NOT_FOUND_ERROR
,
626 _("member function %s not found"), tstr
);
628 return 0; /* For lint -- never reached */
632 /* Concatenate two values with the following conditions:
634 (1) Both values must be either bitstring values or character string
635 values and the resulting value consists of the concatenation of
636 ARG1 followed by ARG2.
640 One value must be an integer value and the other value must be
641 either a bitstring value or character string value, which is
642 to be repeated by the number of times specified by the integer
646 (2) Boolean values are also allowed and are treated as bit string
649 (3) Character values are also allowed and are treated as character
650 string values of length 1. */
653 value_concat (struct value
*arg1
, struct value
*arg2
)
655 struct value
*inval1
;
656 struct value
*inval2
;
657 struct value
*outval
= NULL
;
658 int inval1len
, inval2len
;
662 struct type
*type1
= check_typedef (value_type (arg1
));
663 struct type
*type2
= check_typedef (value_type (arg2
));
664 struct type
*char_type
;
666 /* First figure out if we are dealing with two values to be concatenated
667 or a repeat count and a value to be repeated. INVAL1 is set to the
668 first of two concatenated values, or the repeat count. INVAL2 is set
669 to the second of the two concatenated values or the value to be
672 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
674 struct type
*tmp
= type1
;
687 /* Now process the input values. */
689 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
691 /* We have a repeat count. Validate the second value and then
692 construct a value repeated that many times. */
693 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
694 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
696 count
= longest_to_int (value_as_long (inval1
));
697 inval2len
= TYPE_LENGTH (type2
);
698 std::vector
<char> ptr (count
* inval2len
);
699 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
703 inchar
= (char) unpack_long (type2
,
704 value_contents (inval2
));
705 for (idx
= 0; idx
< count
; idx
++)
712 char_type
= TYPE_TARGET_TYPE (type2
);
714 for (idx
= 0; idx
< count
; idx
++)
716 memcpy (&ptr
[idx
* inval2len
], value_contents (inval2
),
720 outval
= value_string (ptr
.data (), count
* inval2len
, char_type
);
722 else if (TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
724 error (_("unimplemented support for boolean repeats"));
728 error (_("can't repeat values of that type"));
731 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
732 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
734 /* We have two character strings to concatenate. */
735 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
736 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
738 error (_("Strings can only be concatenated with other strings."));
740 inval1len
= TYPE_LENGTH (type1
);
741 inval2len
= TYPE_LENGTH (type2
);
742 std::vector
<char> ptr (inval1len
+ inval2len
);
743 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
747 ptr
[0] = (char) unpack_long (type1
, value_contents (inval1
));
751 char_type
= TYPE_TARGET_TYPE (type1
);
753 memcpy (ptr
.data (), value_contents (inval1
), inval1len
);
755 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
758 (char) unpack_long (type2
, value_contents (inval2
));
762 memcpy (&ptr
[inval1len
], value_contents (inval2
), inval2len
);
764 outval
= value_string (ptr
.data (), inval1len
+ inval2len
, char_type
);
766 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
768 /* We have two bitstrings to concatenate. */
769 if (TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
771 error (_("Booleans can only be concatenated "
772 "with other bitstrings or booleans."));
774 error (_("unimplemented support for boolean concatenation."));
778 /* We don't know how to concatenate these operands. */
779 error (_("illegal operands for concatenation."));
784 /* Integer exponentiation: V1**V2, where both arguments are
785 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
788 integer_pow (LONGEST v1
, LONGEST v2
)
793 error (_("Attempt to raise 0 to negative power."));
799 /* The Russian Peasant's Algorithm. */
815 /* Integer exponentiation: V1**V2, where both arguments are
816 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
819 uinteger_pow (ULONGEST v1
, LONGEST v2
)
824 error (_("Attempt to raise 0 to negative power."));
830 /* The Russian Peasant's Algorithm. */
846 /* Obtain argument values for binary operation, converting from
847 other types if one of them is not floating point. */
849 value_args_as_target_float (struct value
*arg1
, struct value
*arg2
,
850 gdb_byte
*x
, struct type
**eff_type_x
,
851 gdb_byte
*y
, struct type
**eff_type_y
)
853 struct type
*type1
, *type2
;
855 type1
= check_typedef (value_type (arg1
));
856 type2
= check_typedef (value_type (arg2
));
858 /* At least one of the arguments must be of floating-point type. */
859 gdb_assert (is_floating_type (type1
) || is_floating_type (type2
));
861 if (is_floating_type (type1
) && is_floating_type (type2
)
862 && TYPE_CODE (type1
) != TYPE_CODE (type2
))
863 /* The DFP extension to the C language does not allow mixing of
864 * decimal float types with other float types in expressions
865 * (see WDTR 24732, page 12). */
866 error (_("Mixing decimal floating types with "
867 "other floating types is not allowed."));
869 /* Obtain value of arg1, converting from other types if necessary. */
871 if (is_floating_type (type1
))
874 memcpy (x
, value_contents (arg1
), TYPE_LENGTH (type1
));
876 else if (is_integral_type (type1
))
879 if (TYPE_UNSIGNED (type1
))
880 target_float_from_ulongest (x
, *eff_type_x
, value_as_long (arg1
));
882 target_float_from_longest (x
, *eff_type_x
, value_as_long (arg1
));
885 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
888 /* Obtain value of arg2, converting from other types if necessary. */
890 if (is_floating_type (type2
))
893 memcpy (y
, value_contents (arg2
), TYPE_LENGTH (type2
));
895 else if (is_integral_type (type2
))
898 if (TYPE_UNSIGNED (type2
))
899 target_float_from_ulongest (y
, *eff_type_y
, value_as_long (arg2
));
901 target_float_from_longest (y
, *eff_type_y
, value_as_long (arg2
));
904 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
908 /* Perform a binary operation on two operands which have reasonable
909 representations as integers or floats. This includes booleans,
910 characters, integers, or floats.
911 Does not support addition and subtraction on pointers;
912 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
914 static struct value
*
915 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
918 struct type
*type1
, *type2
, *result_type
;
920 arg1
= coerce_ref (arg1
);
921 arg2
= coerce_ref (arg2
);
923 type1
= check_typedef (value_type (arg1
));
924 type2
= check_typedef (value_type (arg2
));
926 if ((!is_floating_value (arg1
) && !is_integral_type (type1
))
927 || (!is_floating_value (arg2
) && !is_integral_type (type2
)))
928 error (_("Argument to arithmetic operation not a number or boolean."));
930 if (is_floating_type (type1
) || is_floating_type (type2
))
932 /* If only one type is floating-point, use its type.
933 Otherwise use the bigger type. */
934 if (!is_floating_type (type1
))
936 else if (!is_floating_type (type2
))
938 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
943 val
= allocate_value (result_type
);
945 struct type
*eff_type_v1
, *eff_type_v2
;
946 gdb::byte_vector v1
, v2
;
947 v1
.resize (TYPE_LENGTH (result_type
));
948 v2
.resize (TYPE_LENGTH (result_type
));
950 value_args_as_target_float (arg1
, arg2
,
951 v1
.data (), &eff_type_v1
,
952 v2
.data (), &eff_type_v2
);
953 target_float_binop (op
, v1
.data (), eff_type_v1
,
954 v2
.data (), eff_type_v2
,
955 value_contents_raw (val
), result_type
);
957 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
958 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
960 LONGEST v1
, v2
, v
= 0;
962 v1
= value_as_long (arg1
);
963 v2
= value_as_long (arg2
);
967 case BINOP_BITWISE_AND
:
971 case BINOP_BITWISE_IOR
:
975 case BINOP_BITWISE_XOR
:
988 error (_("Invalid operation on booleans."));
993 val
= allocate_value (result_type
);
994 store_signed_integer (value_contents_raw (val
),
995 TYPE_LENGTH (result_type
),
996 gdbarch_byte_order (get_type_arch (result_type
)),
1000 /* Integral operations here. */
1002 /* Determine type length of the result, and if the operation should
1003 be done unsigned. For exponentiation and shift operators,
1004 use the length and type of the left operand. Otherwise,
1005 use the signedness of the operand with the greater length.
1006 If both operands are of equal length, use unsigned operation
1007 if one of the operands is unsigned. */
1008 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1009 result_type
= type1
;
1010 else if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1011 result_type
= type1
;
1012 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1013 result_type
= type2
;
1014 else if (TYPE_UNSIGNED (type1
))
1015 result_type
= type1
;
1016 else if (TYPE_UNSIGNED (type2
))
1017 result_type
= type2
;
1019 result_type
= type1
;
1021 if (TYPE_UNSIGNED (result_type
))
1023 LONGEST v2_signed
= value_as_long (arg2
);
1024 ULONGEST v1
, v2
, v
= 0;
1026 v1
= (ULONGEST
) value_as_long (arg1
);
1027 v2
= (ULONGEST
) v2_signed
;
1048 error (_("Division by zero"));
1052 v
= uinteger_pow (v1
, v2_signed
);
1059 error (_("Division by zero"));
1063 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1064 v1 mod 0 has a defined value, v1. */
1072 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1085 case BINOP_BITWISE_AND
:
1089 case BINOP_BITWISE_IOR
:
1093 case BINOP_BITWISE_XOR
:
1097 case BINOP_LOGICAL_AND
:
1101 case BINOP_LOGICAL_OR
:
1106 v
= v1
< v2
? v1
: v2
;
1110 v
= v1
> v2
? v1
: v2
;
1117 case BINOP_NOTEQUAL
:
1138 error (_("Invalid binary operation on numbers."));
1141 val
= allocate_value (result_type
);
1142 store_unsigned_integer (value_contents_raw (val
),
1143 TYPE_LENGTH (value_type (val
)),
1145 (get_type_arch (result_type
)),
1150 LONGEST v1
, v2
, v
= 0;
1152 v1
= value_as_long (arg1
);
1153 v2
= value_as_long (arg2
);
1174 error (_("Division by zero"));
1178 v
= integer_pow (v1
, v2
);
1185 error (_("Division by zero"));
1189 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1190 X mod 0 has a defined value, X. */
1198 /* Compute floor. */
1199 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1215 case BINOP_BITWISE_AND
:
1219 case BINOP_BITWISE_IOR
:
1223 case BINOP_BITWISE_XOR
:
1227 case BINOP_LOGICAL_AND
:
1231 case BINOP_LOGICAL_OR
:
1236 v
= v1
< v2
? v1
: v2
;
1240 v
= v1
> v2
? v1
: v2
;
1247 case BINOP_NOTEQUAL
:
1268 error (_("Invalid binary operation on numbers."));
1271 val
= allocate_value (result_type
);
1272 store_signed_integer (value_contents_raw (val
),
1273 TYPE_LENGTH (value_type (val
)),
1275 (get_type_arch (result_type
)),
1283 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1284 replicating SCALAR_VALUE for each element of the vector. Only scalar
1285 types that can be cast to the type of one element of the vector are
1286 acceptable. The newly created vector value is returned upon success,
1287 otherwise an error is thrown. */
1290 value_vector_widen (struct value
*scalar_value
, struct type
*vector_type
)
1292 /* Widen the scalar to a vector. */
1293 struct type
*eltype
, *scalar_type
;
1294 struct value
*val
, *elval
;
1295 LONGEST low_bound
, high_bound
;
1298 vector_type
= check_typedef (vector_type
);
1300 gdb_assert (TYPE_CODE (vector_type
) == TYPE_CODE_ARRAY
1301 && TYPE_VECTOR (vector_type
));
1303 if (!get_array_bounds (vector_type
, &low_bound
, &high_bound
))
1304 error (_("Could not determine the vector bounds"));
1306 eltype
= check_typedef (TYPE_TARGET_TYPE (vector_type
));
1307 elval
= value_cast (eltype
, scalar_value
);
1309 scalar_type
= check_typedef (value_type (scalar_value
));
1311 /* If we reduced the length of the scalar then check we didn't loose any
1313 if (TYPE_LENGTH (eltype
) < TYPE_LENGTH (scalar_type
)
1314 && !value_equal (elval
, scalar_value
))
1315 error (_("conversion of scalar to vector involves truncation"));
1317 val
= allocate_value (vector_type
);
1318 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1319 /* Duplicate the contents of elval into the destination vector. */
1320 memcpy (value_contents_writeable (val
) + (i
* TYPE_LENGTH (eltype
)),
1321 value_contents_all (elval
), TYPE_LENGTH (eltype
));
1326 /* Performs a binary operation on two vector operands by calling scalar_binop
1327 for each pair of vector components. */
1329 static struct value
*
1330 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1332 struct value
*val
, *tmp
, *mark
;
1333 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1334 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1335 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1337 type1
= check_typedef (value_type (val1
));
1338 type2
= check_typedef (value_type (val2
));
1340 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1341 && TYPE_VECTOR (type1
)) ? 1 : 0;
1342 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1343 && TYPE_VECTOR (type2
)) ? 1 : 0;
1345 if (!t1_is_vec
|| !t2_is_vec
)
1346 error (_("Vector operations are only supported among vectors"));
1348 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1349 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1350 error (_("Could not determine the vector bounds"));
1352 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1353 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1354 elsize
= TYPE_LENGTH (eltype1
);
1356 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
1357 || elsize
!= TYPE_LENGTH (eltype2
)
1358 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
1359 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1360 error (_("Cannot perform operation on vectors with different types"));
1362 val
= allocate_value (type1
);
1363 mark
= value_mark ();
1364 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1366 tmp
= value_binop (value_subscript (val1
, i
),
1367 value_subscript (val2
, i
), op
);
1368 memcpy (value_contents_writeable (val
) + i
* elsize
,
1369 value_contents_all (tmp
),
1372 value_free_to_mark (mark
);
1377 /* Perform a binary operation on two operands. */
1380 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1383 struct type
*type1
= check_typedef (value_type (arg1
));
1384 struct type
*type2
= check_typedef (value_type (arg2
));
1385 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1386 && TYPE_VECTOR (type1
));
1387 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1388 && TYPE_VECTOR (type2
));
1390 if (!t1_is_vec
&& !t2_is_vec
)
1391 val
= scalar_binop (arg1
, arg2
, op
);
1392 else if (t1_is_vec
&& t2_is_vec
)
1393 val
= vector_binop (arg1
, arg2
, op
);
1396 /* Widen the scalar operand to a vector. */
1397 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1398 struct type
*t
= t1_is_vec
? type2
: type1
;
1400 if (TYPE_CODE (t
) != TYPE_CODE_FLT
1401 && TYPE_CODE (t
) != TYPE_CODE_DECFLOAT
1402 && !is_integral_type (t
))
1403 error (_("Argument to operation not a number or boolean."));
1405 /* Replicate the scalar value to make a vector value. */
1406 *v
= value_vector_widen (*v
, t1_is_vec
? type1
: type2
);
1408 val
= vector_binop (arg1
, arg2
, op
);
1414 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1417 value_logical_not (struct value
*arg1
)
1423 arg1
= coerce_array (arg1
);
1424 type1
= check_typedef (value_type (arg1
));
1426 if (is_floating_value (arg1
))
1427 return target_float_is_zero (value_contents (arg1
), type1
);
1429 len
= TYPE_LENGTH (type1
);
1430 p
= value_contents (arg1
);
1441 /* Perform a comparison on two string values (whose content are not
1442 necessarily null terminated) based on their length. */
1445 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1447 int len1
= TYPE_LENGTH (value_type (arg1
));
1448 int len2
= TYPE_LENGTH (value_type (arg2
));
1449 const gdb_byte
*s1
= value_contents (arg1
);
1450 const gdb_byte
*s2
= value_contents (arg2
);
1451 int i
, len
= len1
< len2
? len1
: len2
;
1453 for (i
= 0; i
< len
; i
++)
1457 else if (s1
[i
] > s2
[i
])
1465 else if (len1
> len2
)
1471 /* Simulate the C operator == by returning a 1
1472 iff ARG1 and ARG2 have equal contents. */
1475 value_equal (struct value
*arg1
, struct value
*arg2
)
1480 struct type
*type1
, *type2
;
1481 enum type_code code1
;
1482 enum type_code code2
;
1483 int is_int1
, is_int2
;
1485 arg1
= coerce_array (arg1
);
1486 arg2
= coerce_array (arg2
);
1488 type1
= check_typedef (value_type (arg1
));
1489 type2
= check_typedef (value_type (arg2
));
1490 code1
= TYPE_CODE (type1
);
1491 code2
= TYPE_CODE (type2
);
1492 is_int1
= is_integral_type (type1
);
1493 is_int2
= is_integral_type (type2
);
1495 if (is_int1
&& is_int2
)
1496 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1498 else if ((is_floating_value (arg1
) || is_int1
)
1499 && (is_floating_value (arg2
) || is_int2
))
1501 struct type
*eff_type_v1
, *eff_type_v2
;
1502 gdb::byte_vector v1
, v2
;
1503 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1504 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1506 value_args_as_target_float (arg1
, arg2
,
1507 v1
.data (), &eff_type_v1
,
1508 v2
.data (), &eff_type_v2
);
1510 return target_float_compare (v1
.data (), eff_type_v1
,
1511 v2
.data (), eff_type_v2
) == 0;
1514 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1516 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1517 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1518 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1519 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1521 else if (code1
== code2
1522 && ((len
= (int) TYPE_LENGTH (type1
))
1523 == (int) TYPE_LENGTH (type2
)))
1525 p1
= value_contents (arg1
);
1526 p2
= value_contents (arg2
);
1534 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1536 return value_strcmp (arg1
, arg2
) == 0;
1540 error (_("Invalid type combination in equality test."));
1541 return 0; /* For lint -- never reached. */
1545 /* Compare values based on their raw contents. Useful for arrays since
1546 value_equal coerces them to pointers, thus comparing just the address
1547 of the array instead of its contents. */
1550 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1552 struct type
*type1
, *type2
;
1554 type1
= check_typedef (value_type (arg1
));
1555 type2
= check_typedef (value_type (arg2
));
1557 return (TYPE_CODE (type1
) == TYPE_CODE (type2
)
1558 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1559 && memcmp (value_contents (arg1
), value_contents (arg2
),
1560 TYPE_LENGTH (type1
)) == 0);
1563 /* Simulate the C operator < by returning 1
1564 iff ARG1's contents are less than ARG2's. */
1567 value_less (struct value
*arg1
, struct value
*arg2
)
1569 enum type_code code1
;
1570 enum type_code code2
;
1571 struct type
*type1
, *type2
;
1572 int is_int1
, is_int2
;
1574 arg1
= coerce_array (arg1
);
1575 arg2
= coerce_array (arg2
);
1577 type1
= check_typedef (value_type (arg1
));
1578 type2
= check_typedef (value_type (arg2
));
1579 code1
= TYPE_CODE (type1
);
1580 code2
= TYPE_CODE (type2
);
1581 is_int1
= is_integral_type (type1
);
1582 is_int2
= is_integral_type (type2
);
1584 if (is_int1
&& is_int2
)
1585 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1587 else if ((is_floating_value (arg1
) || is_int1
)
1588 && (is_floating_value (arg2
) || is_int2
))
1590 struct type
*eff_type_v1
, *eff_type_v2
;
1591 gdb::byte_vector v1
, v2
;
1592 v1
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1593 v2
.resize (std::max (TYPE_LENGTH (type1
), TYPE_LENGTH (type2
)));
1595 value_args_as_target_float (arg1
, arg2
,
1596 v1
.data (), &eff_type_v1
,
1597 v2
.data (), &eff_type_v2
);
1599 return target_float_compare (v1
.data (), eff_type_v1
,
1600 v2
.data (), eff_type_v2
) == -1;
1602 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1603 return value_as_address (arg1
) < value_as_address (arg2
);
1605 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1607 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1608 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1609 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1610 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1611 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1612 return value_strcmp (arg1
, arg2
) < 0;
1615 error (_("Invalid type combination in ordering comparison."));
1620 /* The unary operators +, - and ~. They free the argument ARG1. */
1623 value_pos (struct value
*arg1
)
1627 arg1
= coerce_ref (arg1
);
1628 type
= check_typedef (value_type (arg1
));
1630 if (is_integral_type (type
) || is_floating_value (arg1
)
1631 || (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
)))
1632 return value_from_contents (type
, value_contents (arg1
));
1635 error (_("Argument to positive operation not a number."));
1636 return 0; /* For lint -- never reached. */
1641 value_neg (struct value
*arg1
)
1645 arg1
= coerce_ref (arg1
);
1646 type
= check_typedef (value_type (arg1
));
1648 if (is_integral_type (type
) || is_floating_type (type
))
1649 return value_binop (value_from_longest (type
, 0), arg1
, BINOP_SUB
);
1650 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1652 struct value
*tmp
, *val
= allocate_value (type
);
1653 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1655 LONGEST low_bound
, high_bound
;
1657 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1658 error (_("Could not determine the vector bounds"));
1660 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1662 tmp
= value_neg (value_subscript (arg1
, i
));
1663 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1664 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1670 error (_("Argument to negate operation not a number."));
1671 return 0; /* For lint -- never reached. */
1676 value_complement (struct value
*arg1
)
1681 arg1
= coerce_ref (arg1
);
1682 type
= check_typedef (value_type (arg1
));
1684 if (is_integral_type (type
))
1685 val
= value_from_longest (type
, ~value_as_long (arg1
));
1686 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1689 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1691 LONGEST low_bound
, high_bound
;
1693 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1694 error (_("Could not determine the vector bounds"));
1696 val
= allocate_value (type
);
1697 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1699 tmp
= value_complement (value_subscript (arg1
, i
));
1700 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1701 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1705 error (_("Argument to complement operation not an integer, boolean."));
1710 /* The INDEX'th bit of SET value whose value_type is TYPE,
1711 and whose value_contents is valaddr.
1712 Return -1 if out of range, -2 other error. */
1715 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1717 struct gdbarch
*gdbarch
= get_type_arch (type
);
1718 LONGEST low_bound
, high_bound
;
1721 struct type
*range
= TYPE_INDEX_TYPE (type
);
1723 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1725 if (index
< low_bound
|| index
> high_bound
)
1727 rel_index
= index
- low_bound
;
1728 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1729 gdbarch_byte_order (gdbarch
));
1730 rel_index
%= TARGET_CHAR_BIT
;
1731 if (gdbarch_bits_big_endian (gdbarch
))
1732 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1733 return (word
>> rel_index
) & 1;
1737 value_in (struct value
*element
, struct value
*set
)
1740 struct type
*settype
= check_typedef (value_type (set
));
1741 struct type
*eltype
= check_typedef (value_type (element
));
1743 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1744 eltype
= TYPE_TARGET_TYPE (eltype
);
1745 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1746 error (_("Second argument of 'IN' has wrong type"));
1747 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1748 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1749 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1750 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1751 error (_("First argument of 'IN' has wrong type"));
1752 member
= value_bit_index (settype
, value_contents (set
),
1753 value_as_long (element
));
1755 error (_("First argument of 'IN' not in range"));
1760 _initialize_valarith (void)
This page took 0.065684 seconds and 4 git commands to generate.