1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986, 1988-2005, 2007-2012 Free Software Foundation,
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "expression.h"
28 #include "gdb_string.h"
33 #include "exceptions.h"
35 /* Define whether or not the C operator '/' truncates towards zero for
36 differently signed operands (truncation direction is undefined in C). */
38 #ifndef TRUNCATION_TOWARDS_ZERO
39 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
42 void _initialize_valarith (void);
45 /* Given a pointer, return the size of its target.
46 If the pointer type is void *, then return 1.
47 If the target type is incomplete, then error out.
48 This isn't a general purpose function, but just a
49 helper for value_ptradd. */
52 find_size_for_pointer_math (struct type
*ptr_type
)
55 struct type
*ptr_target
;
57 gdb_assert (TYPE_CODE (ptr_type
) == TYPE_CODE_PTR
);
58 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
60 sz
= TYPE_LENGTH (ptr_target
);
63 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
69 name
= TYPE_NAME (ptr_target
);
71 name
= TYPE_TAG_NAME (ptr_target
);
73 error (_("Cannot perform pointer math on incomplete types, "
74 "try casting to a known type, or void *."));
76 error (_("Cannot perform pointer math on incomplete type \"%s\", "
77 "try casting to a known type, or void *."), name
);
83 /* Given a pointer ARG1 and an integral value ARG2, return the
84 result of C-style pointer arithmetic ARG1 + ARG2. */
87 value_ptradd (struct value
*arg1
, LONGEST arg2
)
89 struct type
*valptrtype
;
93 arg1
= coerce_array (arg1
);
94 valptrtype
= check_typedef (value_type (arg1
));
95 sz
= find_size_for_pointer_math (valptrtype
);
97 result
= value_from_pointer (valptrtype
,
98 value_as_address (arg1
) + sz
* arg2
);
99 if (VALUE_LVAL (result
) != lval_internalvar
)
100 set_value_component_location (result
, arg1
);
104 /* Given two compatible pointer values ARG1 and ARG2, return the
105 result of C-style pointer arithmetic ARG1 - ARG2. */
108 value_ptrdiff (struct value
*arg1
, struct value
*arg2
)
110 struct type
*type1
, *type2
;
113 arg1
= coerce_array (arg1
);
114 arg2
= coerce_array (arg2
);
115 type1
= check_typedef (value_type (arg1
));
116 type2
= check_typedef (value_type (arg2
));
118 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_PTR
);
119 gdb_assert (TYPE_CODE (type2
) == TYPE_CODE_PTR
);
121 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
122 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
123 error (_("First argument of `-' is a pointer and "
124 "second argument is neither\n"
125 "an integer nor a pointer of the same type."));
127 sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
130 warning (_("Type size unknown, assuming 1. "
131 "Try casting to a known type, or void *."));
135 return (value_as_long (arg1
) - value_as_long (arg2
)) / sz
;
138 /* Return the value of ARRAY[IDX].
140 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
141 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
143 See comments in value_coerce_array() for rationale for reason for
144 doing lower bounds adjustment here rather than there.
145 FIXME: Perhaps we should validate that the index is valid and if
146 verbosity is set, warn about invalid indices (but still use them). */
149 value_subscript (struct value
*array
, LONGEST index
)
151 int c_style
= current_language
->c_style_arrays
;
154 array
= coerce_ref (array
);
155 tarray
= check_typedef (value_type (array
));
157 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
158 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
160 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
161 LONGEST lowerbound
, upperbound
;
163 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
164 if (VALUE_LVAL (array
) != lval_memory
)
165 return value_subscripted_rvalue (array
, index
, lowerbound
);
169 if (index
>= lowerbound
&& index
<= upperbound
)
170 return value_subscripted_rvalue (array
, index
, lowerbound
);
171 /* Emit warning unless we have an array of unknown size.
172 An array of unknown size has lowerbound 0 and upperbound -1. */
174 warning (_("array or string index out of range"));
175 /* fall doing C stuff */
180 array
= value_coerce_array (array
);
184 return value_ind (value_ptradd (array
, index
));
186 error (_("not an array or string"));
189 /* Return the value of EXPR[IDX], expr an aggregate rvalue
190 (eg, a vector register). This routine used to promote floats
191 to doubles, but no longer does. */
194 value_subscripted_rvalue (struct value
*array
, LONGEST index
, int lowerbound
)
196 struct type
*array_type
= check_typedef (value_type (array
));
197 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
198 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
199 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
202 if (index
< lowerbound
|| (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)
203 && elt_offs
>= TYPE_LENGTH (array_type
)))
204 error (_("no such vector element"));
206 if (VALUE_LVAL (array
) == lval_memory
&& value_lazy (array
))
207 v
= allocate_value_lazy (elt_type
);
210 v
= allocate_value (elt_type
);
211 value_contents_copy (v
, value_embedded_offset (v
),
212 array
, value_embedded_offset (array
) + elt_offs
,
216 set_value_component_location (v
, array
);
217 VALUE_REGNUM (v
) = VALUE_REGNUM (array
);
218 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
219 set_value_offset (v
, value_offset (array
) + elt_offs
);
224 /* Check to see if either argument is a structure, or a reference to
225 one. This is called so we know whether to go ahead with the normal
226 binop or look for a user defined function instead.
228 For now, we do not overload the `=' operator. */
231 binop_types_user_defined_p (enum exp_opcode op
,
232 struct type
*type1
, struct type
*type2
)
234 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
237 type1
= check_typedef (type1
);
238 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
239 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
241 type2
= check_typedef (type2
);
242 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
243 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
245 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
246 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
249 /* Check to see if either argument is a structure, or a reference to
250 one. This is called so we know whether to go ahead with the normal
251 binop or look for a user defined function instead.
253 For now, we do not overload the `=' operator. */
256 binop_user_defined_p (enum exp_opcode op
,
257 struct value
*arg1
, struct value
*arg2
)
259 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
262 /* Check to see if argument is a structure. This is called so
263 we know whether to go ahead with the normal unop or look for a
264 user defined function instead.
266 For now, we do not overload the `&' operator. */
269 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
275 type1
= check_typedef (value_type (arg1
));
276 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
277 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
278 return TYPE_CODE (type1
) == TYPE_CODE_STRUCT
;
281 /* Try to find an operator named OPERATOR which takes NARGS arguments
282 specified in ARGS. If the operator found is a static member operator
283 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
284 The search if performed through find_overload_match which will handle
285 member operators, non member operators, operators imported implicitly or
286 explicitly, and perform correct overload resolution in all of the above
287 situations or combinations thereof. */
289 static struct value
*
290 value_user_defined_cpp_op (struct value
**args
, int nargs
, char *operator,
291 int *static_memfuncp
)
294 struct symbol
*symp
= NULL
;
295 struct value
*valp
= NULL
;
297 find_overload_match (args
, nargs
, operator, BOTH
/* could be method */,
298 0 /* strict match */, &args
[0], /* objp */
299 NULL
/* pass NULL symbol since symbol is unknown */,
300 &valp
, &symp
, static_memfuncp
, 0);
307 /* This is a non member function and does not
308 expect a reference as its first argument
309 rather the explicit structure. */
310 args
[0] = value_ind (args
[0]);
311 return value_of_variable (symp
, 0);
314 error (_("Could not find %s."), operator);
317 /* Lookup user defined operator NAME. Return a value representing the
318 function, otherwise return NULL. */
320 static struct value
*
321 value_user_defined_op (struct value
**argp
, struct value
**args
, char *name
,
322 int *static_memfuncp
, int nargs
)
324 struct value
*result
= NULL
;
326 if (current_language
->la_language
== language_cplus
)
327 result
= value_user_defined_cpp_op (args
, nargs
, name
, static_memfuncp
);
329 result
= value_struct_elt (argp
, args
, name
, static_memfuncp
,
335 /* We know either arg1 or arg2 is a structure, so try to find the right
336 user defined function. Create an argument vector that calls
337 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
338 binary operator which is legal for GNU C++).
340 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
341 is the opcode saying how to modify it. Otherwise, OTHEROP is
345 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
346 enum exp_opcode otherop
, enum noside noside
)
348 struct value
**argvec
;
353 arg1
= coerce_ref (arg1
);
354 arg2
= coerce_ref (arg2
);
356 /* now we know that what we have to do is construct our
357 arg vector and find the right function to call it with. */
359 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
360 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
362 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
363 argvec
[1] = value_addr (arg1
);
367 /* Make the right function name up. */
368 strcpy (tstr
, "operator__");
393 case BINOP_BITWISE_AND
:
396 case BINOP_BITWISE_IOR
:
399 case BINOP_BITWISE_XOR
:
402 case BINOP_LOGICAL_AND
:
405 case BINOP_LOGICAL_OR
:
417 case BINOP_ASSIGN_MODIFY
:
435 case BINOP_BITWISE_AND
:
438 case BINOP_BITWISE_IOR
:
441 case BINOP_BITWISE_XOR
:
444 case BINOP_MOD
: /* invalid */
446 error (_("Invalid binary operation specified."));
449 case BINOP_SUBSCRIPT
:
470 case BINOP_MOD
: /* invalid */
472 error (_("Invalid binary operation specified."));
475 argvec
[0] = value_user_defined_op (&arg1
, argvec
+ 1, tstr
,
476 &static_memfuncp
, 2);
482 argvec
[1] = argvec
[0];
485 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
487 struct type
*return_type
;
490 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
491 return value_zero (return_type
, VALUE_LVAL (arg1
));
493 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
,
496 throw_error (NOT_FOUND_ERROR
,
497 _("member function %s not found"), tstr
);
499 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
503 /* We know that arg1 is a structure, so try to find a unary user
504 defined operator that matches the operator in question.
505 Create an argument vector that calls arg1.operator @ (arg1)
506 and return that value (where '@' is (almost) any unary operator which
507 is legal for GNU C++). */
510 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
512 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
513 struct value
**argvec
;
514 char *ptr
, *mangle_ptr
;
515 char tstr
[13], mangle_tstr
[13];
516 int static_memfuncp
, nargs
;
518 arg1
= coerce_ref (arg1
);
520 /* now we know that what we have to do is construct our
521 arg vector and find the right function to call it with. */
523 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
524 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
526 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
527 argvec
[1] = value_addr (arg1
);
532 /* Make the right function name up. */
533 strcpy (tstr
, "operator__");
535 strcpy (mangle_tstr
, "__");
536 mangle_ptr
= mangle_tstr
+ 2;
539 case UNOP_PREINCREMENT
:
542 case UNOP_PREDECREMENT
:
545 case UNOP_POSTINCREMENT
:
547 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
551 case UNOP_POSTDECREMENT
:
553 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
557 case UNOP_LOGICAL_NOT
:
560 case UNOP_COMPLEMENT
:
576 error (_("Invalid unary operation specified."));
579 argvec
[0] = value_user_defined_op (&arg1
, argvec
+ 1, tstr
,
580 &static_memfuncp
, nargs
);
586 argvec
[1] = argvec
[0];
590 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
592 struct type
*return_type
;
595 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
596 return value_zero (return_type
, VALUE_LVAL (arg1
));
598 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
600 throw_error (NOT_FOUND_ERROR
,
601 _("member function %s not found"), tstr
);
603 return 0; /* For lint -- never reached */
607 /* Concatenate two values with the following conditions:
609 (1) Both values must be either bitstring values or character string
610 values and the resulting value consists of the concatenation of
611 ARG1 followed by ARG2.
615 One value must be an integer value and the other value must be
616 either a bitstring value or character string value, which is
617 to be repeated by the number of times specified by the integer
621 (2) Boolean values are also allowed and are treated as bit string
624 (3) Character values are also allowed and are treated as character
625 string values of length 1. */
628 value_concat (struct value
*arg1
, struct value
*arg2
)
630 struct value
*inval1
;
631 struct value
*inval2
;
632 struct value
*outval
= NULL
;
633 int inval1len
, inval2len
;
637 struct type
*type1
= check_typedef (value_type (arg1
));
638 struct type
*type2
= check_typedef (value_type (arg2
));
639 struct type
*char_type
;
641 /* First figure out if we are dealing with two values to be concatenated
642 or a repeat count and a value to be repeated. INVAL1 is set to the
643 first of two concatenated values, or the repeat count. INVAL2 is set
644 to the second of the two concatenated values or the value to be
647 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
649 struct type
*tmp
= type1
;
662 /* Now process the input values. */
664 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
666 /* We have a repeat count. Validate the second value and then
667 construct a value repeated that many times. */
668 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
669 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
671 struct cleanup
*back_to
;
673 count
= longest_to_int (value_as_long (inval1
));
674 inval2len
= TYPE_LENGTH (type2
);
675 ptr
= (char *) xmalloc (count
* inval2len
);
676 back_to
= make_cleanup (xfree
, ptr
);
677 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
681 inchar
= (char) unpack_long (type2
,
682 value_contents (inval2
));
683 for (idx
= 0; idx
< count
; idx
++)
685 *(ptr
+ idx
) = inchar
;
690 char_type
= TYPE_TARGET_TYPE (type2
);
692 for (idx
= 0; idx
< count
; idx
++)
694 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
698 outval
= value_string (ptr
, count
* inval2len
, char_type
);
699 do_cleanups (back_to
);
701 else if (TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
703 error (_("unimplemented support for boolean repeats"));
707 error (_("can't repeat values of that type"));
710 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
711 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
713 struct cleanup
*back_to
;
715 /* We have two character strings to concatenate. */
716 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
717 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
719 error (_("Strings can only be concatenated with other strings."));
721 inval1len
= TYPE_LENGTH (type1
);
722 inval2len
= TYPE_LENGTH (type2
);
723 ptr
= (char *) xmalloc (inval1len
+ inval2len
);
724 back_to
= make_cleanup (xfree
, ptr
);
725 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
729 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
733 char_type
= TYPE_TARGET_TYPE (type1
);
735 memcpy (ptr
, value_contents (inval1
), inval1len
);
737 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
740 (char) unpack_long (type2
, value_contents (inval2
));
744 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
746 outval
= value_string (ptr
, inval1len
+ inval2len
, char_type
);
747 do_cleanups (back_to
);
749 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
751 /* We have two bitstrings to concatenate. */
752 if (TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
754 error (_("Booleans can only be concatenated "
755 "with other bitstrings or booleans."));
757 error (_("unimplemented support for boolean concatenation."));
761 /* We don't know how to concatenate these operands. */
762 error (_("illegal operands for concatenation."));
767 /* Integer exponentiation: V1**V2, where both arguments are
768 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
771 integer_pow (LONGEST v1
, LONGEST v2
)
776 error (_("Attempt to raise 0 to negative power."));
782 /* The Russian Peasant's Algorithm. */
798 /* Integer exponentiation: V1**V2, where both arguments are
799 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
802 uinteger_pow (ULONGEST v1
, LONGEST v2
)
807 error (_("Attempt to raise 0 to negative power."));
813 /* The Russian Peasant's Algorithm. */
829 /* Obtain decimal value of arguments for binary operation, converting from
830 other types if one of them is not decimal floating point. */
832 value_args_as_decimal (struct value
*arg1
, struct value
*arg2
,
833 gdb_byte
*x
, int *len_x
, enum bfd_endian
*byte_order_x
,
834 gdb_byte
*y
, int *len_y
, enum bfd_endian
*byte_order_y
)
836 struct type
*type1
, *type2
;
838 type1
= check_typedef (value_type (arg1
));
839 type2
= check_typedef (value_type (arg2
));
841 /* At least one of the arguments must be of decimal float type. */
842 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
843 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
);
845 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
846 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
847 /* The DFP extension to the C language does not allow mixing of
848 * decimal float types with other float types in expressions
849 * (see WDTR 24732, page 12). */
850 error (_("Mixing decimal floating types with "
851 "other floating types is not allowed."));
853 /* Obtain decimal value of arg1, converting from other types
856 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
858 *byte_order_x
= gdbarch_byte_order (get_type_arch (type1
));
859 *len_x
= TYPE_LENGTH (type1
);
860 memcpy (x
, value_contents (arg1
), *len_x
);
862 else if (is_integral_type (type1
))
864 *byte_order_x
= gdbarch_byte_order (get_type_arch (type2
));
865 *len_x
= TYPE_LENGTH (type2
);
866 decimal_from_integral (arg1
, x
, *len_x
, *byte_order_x
);
869 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
872 /* Obtain decimal value of arg2, converting from other types
875 if (TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
877 *byte_order_y
= gdbarch_byte_order (get_type_arch (type2
));
878 *len_y
= TYPE_LENGTH (type2
);
879 memcpy (y
, value_contents (arg2
), *len_y
);
881 else if (is_integral_type (type2
))
883 *byte_order_y
= gdbarch_byte_order (get_type_arch (type1
));
884 *len_y
= TYPE_LENGTH (type1
);
885 decimal_from_integral (arg2
, y
, *len_y
, *byte_order_y
);
888 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
892 /* Perform a binary operation on two operands which have reasonable
893 representations as integers or floats. This includes booleans,
894 characters, integers, or floats.
895 Does not support addition and subtraction on pointers;
896 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
898 static struct value
*
899 scalar_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
902 struct type
*type1
, *type2
, *result_type
;
904 arg1
= coerce_ref (arg1
);
905 arg2
= coerce_ref (arg2
);
907 type1
= check_typedef (value_type (arg1
));
908 type2
= check_typedef (value_type (arg2
));
910 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
911 && TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
912 && !is_integral_type (type1
))
913 || (TYPE_CODE (type2
) != TYPE_CODE_FLT
914 && TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
915 && !is_integral_type (type2
)))
916 error (_("Argument to arithmetic operation not a number or boolean."));
918 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
919 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
921 int len_v1
, len_v2
, len_v
;
922 enum bfd_endian byte_order_v1
, byte_order_v2
, byte_order_v
;
923 gdb_byte v1
[16], v2
[16];
926 /* If only one type is decimal float, use its type.
927 Otherwise use the bigger type. */
928 if (TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
)
930 else if (TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
)
932 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
937 len_v
= TYPE_LENGTH (result_type
);
938 byte_order_v
= gdbarch_byte_order (get_type_arch (result_type
));
940 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
941 v2
, &len_v2
, &byte_order_v2
);
950 decimal_binop (op
, v1
, len_v1
, byte_order_v1
,
951 v2
, len_v2
, byte_order_v2
,
952 v
, len_v
, byte_order_v
);
956 error (_("Operation not valid for decimal floating point number."));
959 val
= value_from_decfloat (result_type
, v
);
961 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
962 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
964 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
965 in target format. real.c in GCC probably has the necessary
967 DOUBLEST v1
, v2
, v
= 0;
969 v1
= value_as_double (arg1
);
970 v2
= value_as_double (arg2
);
994 error (_("Cannot perform exponentiation: %s"),
995 safe_strerror (errno
));
999 v
= v1
< v2
? v1
: v2
;
1003 v
= v1
> v2
? v1
: v2
;
1007 error (_("Integer-only operation on floating point number."));
1010 /* If only one type is float, use its type.
1011 Otherwise use the bigger type. */
1012 if (TYPE_CODE (type1
) != TYPE_CODE_FLT
)
1013 result_type
= type2
;
1014 else if (TYPE_CODE (type2
) != TYPE_CODE_FLT
)
1015 result_type
= type1
;
1016 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1017 result_type
= type2
;
1019 result_type
= type1
;
1021 val
= allocate_value (result_type
);
1022 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
1024 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
1025 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
1027 LONGEST v1
, v2
, v
= 0;
1029 v1
= value_as_long (arg1
);
1030 v2
= value_as_long (arg2
);
1034 case BINOP_BITWISE_AND
:
1038 case BINOP_BITWISE_IOR
:
1042 case BINOP_BITWISE_XOR
:
1050 case BINOP_NOTEQUAL
:
1055 error (_("Invalid operation on booleans."));
1058 result_type
= type1
;
1060 val
= allocate_value (result_type
);
1061 store_signed_integer (value_contents_raw (val
),
1062 TYPE_LENGTH (result_type
),
1063 gdbarch_byte_order (get_type_arch (result_type
)),
1067 /* Integral operations here. */
1069 /* Determine type length of the result, and if the operation should
1070 be done unsigned. For exponentiation and shift operators,
1071 use the length and type of the left operand. Otherwise,
1072 use the signedness of the operand with the greater length.
1073 If both operands are of equal length, use unsigned operation
1074 if one of the operands is unsigned. */
1075 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1076 result_type
= type1
;
1077 else if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1078 result_type
= type1
;
1079 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1080 result_type
= type2
;
1081 else if (TYPE_UNSIGNED (type1
))
1082 result_type
= type1
;
1083 else if (TYPE_UNSIGNED (type2
))
1084 result_type
= type2
;
1086 result_type
= type1
;
1088 if (TYPE_UNSIGNED (result_type
))
1090 LONGEST v2_signed
= value_as_long (arg2
);
1091 ULONGEST v1
, v2
, v
= 0;
1093 v1
= (ULONGEST
) value_as_long (arg1
);
1094 v2
= (ULONGEST
) v2_signed
;
1115 error (_("Division by zero"));
1119 v
= uinteger_pow (v1
, v2_signed
);
1126 error (_("Division by zero"));
1130 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1131 v1 mod 0 has a defined value, v1. */
1139 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1152 case BINOP_BITWISE_AND
:
1156 case BINOP_BITWISE_IOR
:
1160 case BINOP_BITWISE_XOR
:
1164 case BINOP_LOGICAL_AND
:
1168 case BINOP_LOGICAL_OR
:
1173 v
= v1
< v2
? v1
: v2
;
1177 v
= v1
> v2
? v1
: v2
;
1184 case BINOP_NOTEQUAL
:
1205 error (_("Invalid binary operation on numbers."));
1208 val
= allocate_value (result_type
);
1209 store_unsigned_integer (value_contents_raw (val
),
1210 TYPE_LENGTH (value_type (val
)),
1212 (get_type_arch (result_type
)),
1217 LONGEST v1
, v2
, v
= 0;
1219 v1
= value_as_long (arg1
);
1220 v2
= value_as_long (arg2
);
1241 error (_("Division by zero"));
1245 v
= integer_pow (v1
, v2
);
1252 error (_("Division by zero"));
1256 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1257 X mod 0 has a defined value, X. */
1265 /* Compute floor. */
1266 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1282 case BINOP_BITWISE_AND
:
1286 case BINOP_BITWISE_IOR
:
1290 case BINOP_BITWISE_XOR
:
1294 case BINOP_LOGICAL_AND
:
1298 case BINOP_LOGICAL_OR
:
1303 v
= v1
< v2
? v1
: v2
;
1307 v
= v1
> v2
? v1
: v2
;
1314 case BINOP_NOTEQUAL
:
1335 error (_("Invalid binary operation on numbers."));
1338 val
= allocate_value (result_type
);
1339 store_signed_integer (value_contents_raw (val
),
1340 TYPE_LENGTH (value_type (val
)),
1342 (get_type_arch (result_type
)),
1350 /* Performs a binary operation on two vector operands by calling scalar_binop
1351 for each pair of vector components. */
1353 static struct value
*
1354 vector_binop (struct value
*val1
, struct value
*val2
, enum exp_opcode op
)
1356 struct value
*val
, *tmp
, *mark
;
1357 struct type
*type1
, *type2
, *eltype1
, *eltype2
;
1358 int t1_is_vec
, t2_is_vec
, elsize
, i
;
1359 LONGEST low_bound1
, high_bound1
, low_bound2
, high_bound2
;
1361 type1
= check_typedef (value_type (val1
));
1362 type2
= check_typedef (value_type (val2
));
1364 t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1365 && TYPE_VECTOR (type1
)) ? 1 : 0;
1366 t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1367 && TYPE_VECTOR (type2
)) ? 1 : 0;
1369 if (!t1_is_vec
|| !t2_is_vec
)
1370 error (_("Vector operations are only supported among vectors"));
1372 if (!get_array_bounds (type1
, &low_bound1
, &high_bound1
)
1373 || !get_array_bounds (type2
, &low_bound2
, &high_bound2
))
1374 error (_("Could not determine the vector bounds"));
1376 eltype1
= check_typedef (TYPE_TARGET_TYPE (type1
));
1377 eltype2
= check_typedef (TYPE_TARGET_TYPE (type2
));
1378 elsize
= TYPE_LENGTH (eltype1
);
1380 if (TYPE_CODE (eltype1
) != TYPE_CODE (eltype2
)
1381 || elsize
!= TYPE_LENGTH (eltype2
)
1382 || TYPE_UNSIGNED (eltype1
) != TYPE_UNSIGNED (eltype2
)
1383 || low_bound1
!= low_bound2
|| high_bound1
!= high_bound2
)
1384 error (_("Cannot perform operation on vectors with different types"));
1386 val
= allocate_value (type1
);
1387 mark
= value_mark ();
1388 for (i
= 0; i
< high_bound1
- low_bound1
+ 1; i
++)
1390 tmp
= value_binop (value_subscript (val1
, i
),
1391 value_subscript (val2
, i
), op
);
1392 memcpy (value_contents_writeable (val
) + i
* elsize
,
1393 value_contents_all (tmp
),
1396 value_free_to_mark (mark
);
1401 /* Perform a binary operation on two operands. */
1404 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
1407 struct type
*type1
= check_typedef (value_type (arg1
));
1408 struct type
*type2
= check_typedef (value_type (arg2
));
1409 int t1_is_vec
= (TYPE_CODE (type1
) == TYPE_CODE_ARRAY
1410 && TYPE_VECTOR (type1
));
1411 int t2_is_vec
= (TYPE_CODE (type2
) == TYPE_CODE_ARRAY
1412 && TYPE_VECTOR (type2
));
1414 if (!t1_is_vec
&& !t2_is_vec
)
1415 val
= scalar_binop (arg1
, arg2
, op
);
1416 else if (t1_is_vec
&& t2_is_vec
)
1417 val
= vector_binop (arg1
, arg2
, op
);
1420 /* Widen the scalar operand to a vector. */
1421 struct value
**v
= t1_is_vec
? &arg2
: &arg1
;
1422 struct type
*t
= t1_is_vec
? type2
: type1
;
1424 if (TYPE_CODE (t
) != TYPE_CODE_FLT
1425 && TYPE_CODE (t
) != TYPE_CODE_DECFLOAT
1426 && !is_integral_type (t
))
1427 error (_("Argument to operation not a number or boolean."));
1429 *v
= value_cast (t1_is_vec
? type1
: type2
, *v
);
1430 val
= vector_binop (arg1
, arg2
, op
);
1436 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1439 value_logical_not (struct value
*arg1
)
1445 arg1
= coerce_array (arg1
);
1446 type1
= check_typedef (value_type (arg1
));
1448 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1449 return 0 == value_as_double (arg1
);
1450 else if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1451 return decimal_is_zero (value_contents (arg1
), TYPE_LENGTH (type1
),
1452 gdbarch_byte_order (get_type_arch (type1
)));
1454 len
= TYPE_LENGTH (type1
);
1455 p
= value_contents (arg1
);
1466 /* Perform a comparison on two string values (whose content are not
1467 necessarily null terminated) based on their length. */
1470 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1472 int len1
= TYPE_LENGTH (value_type (arg1
));
1473 int len2
= TYPE_LENGTH (value_type (arg2
));
1474 const gdb_byte
*s1
= value_contents (arg1
);
1475 const gdb_byte
*s2
= value_contents (arg2
);
1476 int i
, len
= len1
< len2
? len1
: len2
;
1478 for (i
= 0; i
< len
; i
++)
1482 else if (s1
[i
] > s2
[i
])
1490 else if (len1
> len2
)
1496 /* Simulate the C operator == by returning a 1
1497 iff ARG1 and ARG2 have equal contents. */
1500 value_equal (struct value
*arg1
, struct value
*arg2
)
1505 struct type
*type1
, *type2
;
1506 enum type_code code1
;
1507 enum type_code code2
;
1508 int is_int1
, is_int2
;
1510 arg1
= coerce_array (arg1
);
1511 arg2
= coerce_array (arg2
);
1513 type1
= check_typedef (value_type (arg1
));
1514 type2
= check_typedef (value_type (arg2
));
1515 code1
= TYPE_CODE (type1
);
1516 code2
= TYPE_CODE (type2
);
1517 is_int1
= is_integral_type (type1
);
1518 is_int2
= is_integral_type (type2
);
1520 if (is_int1
&& is_int2
)
1521 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1523 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1524 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1526 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1527 `long double' values are returned in static storage (m68k). */
1528 DOUBLEST d
= value_as_double (arg1
);
1530 return d
== value_as_double (arg2
);
1532 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1533 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1535 gdb_byte v1
[16], v2
[16];
1537 enum bfd_endian byte_order_v1
, byte_order_v2
;
1539 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
1540 v2
, &len_v2
, &byte_order_v2
);
1542 return decimal_compare (v1
, len_v1
, byte_order_v1
,
1543 v2
, len_v2
, byte_order_v2
) == 0;
1546 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1548 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1549 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1550 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1551 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1553 else if (code1
== code2
1554 && ((len
= (int) TYPE_LENGTH (type1
))
1555 == (int) TYPE_LENGTH (type2
)))
1557 p1
= value_contents (arg1
);
1558 p2
= value_contents (arg2
);
1566 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1568 return value_strcmp (arg1
, arg2
) == 0;
1572 error (_("Invalid type combination in equality test."));
1573 return 0; /* For lint -- never reached. */
1577 /* Compare values based on their raw contents. Useful for arrays since
1578 value_equal coerces them to pointers, thus comparing just the address
1579 of the array instead of its contents. */
1582 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1584 struct type
*type1
, *type2
;
1586 type1
= check_typedef (value_type (arg1
));
1587 type2
= check_typedef (value_type (arg2
));
1589 return (TYPE_CODE (type1
) == TYPE_CODE (type2
)
1590 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1591 && memcmp (value_contents (arg1
), value_contents (arg2
),
1592 TYPE_LENGTH (type1
)) == 0);
1595 /* Simulate the C operator < by returning 1
1596 iff ARG1's contents are less than ARG2's. */
1599 value_less (struct value
*arg1
, struct value
*arg2
)
1601 enum type_code code1
;
1602 enum type_code code2
;
1603 struct type
*type1
, *type2
;
1604 int is_int1
, is_int2
;
1606 arg1
= coerce_array (arg1
);
1607 arg2
= coerce_array (arg2
);
1609 type1
= check_typedef (value_type (arg1
));
1610 type2
= check_typedef (value_type (arg2
));
1611 code1
= TYPE_CODE (type1
);
1612 code2
= TYPE_CODE (type2
);
1613 is_int1
= is_integral_type (type1
);
1614 is_int2
= is_integral_type (type2
);
1616 if (is_int1
&& is_int2
)
1617 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1619 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1620 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1622 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1623 `long double' values are returned in static storage (m68k). */
1624 DOUBLEST d
= value_as_double (arg1
);
1626 return d
< value_as_double (arg2
);
1628 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1629 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1631 gdb_byte v1
[16], v2
[16];
1633 enum bfd_endian byte_order_v1
, byte_order_v2
;
1635 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
1636 v2
, &len_v2
, &byte_order_v2
);
1638 return decimal_compare (v1
, len_v1
, byte_order_v1
,
1639 v2
, len_v2
, byte_order_v2
) == -1;
1641 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1642 return value_as_address (arg1
) < value_as_address (arg2
);
1644 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1646 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1647 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1648 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1649 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1650 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1651 return value_strcmp (arg1
, arg2
) < 0;
1654 error (_("Invalid type combination in ordering comparison."));
1659 /* The unary operators +, - and ~. They free the argument ARG1. */
1662 value_pos (struct value
*arg1
)
1666 arg1
= coerce_ref (arg1
);
1667 type
= check_typedef (value_type (arg1
));
1669 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1670 return value_from_double (type
, value_as_double (arg1
));
1671 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1672 return value_from_decfloat (type
, value_contents (arg1
));
1673 else if (is_integral_type (type
))
1675 return value_from_longest (type
, value_as_long (arg1
));
1677 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1679 struct value
*val
= allocate_value (type
);
1681 memcpy (value_contents_raw (val
), value_contents (arg1
),
1682 TYPE_LENGTH (type
));
1687 error (_("Argument to positive operation not a number."));
1688 return 0; /* For lint -- never reached. */
1693 value_neg (struct value
*arg1
)
1697 arg1
= coerce_ref (arg1
);
1698 type
= check_typedef (value_type (arg1
));
1700 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1702 struct value
*val
= allocate_value (type
);
1703 int len
= TYPE_LENGTH (type
);
1704 gdb_byte decbytes
[16]; /* a decfloat is at most 128 bits long. */
1706 memcpy (decbytes
, value_contents (arg1
), len
);
1708 if (gdbarch_byte_order (get_type_arch (type
)) == BFD_ENDIAN_LITTLE
)
1709 decbytes
[len
-1] = decbytes
[len
- 1] | 0x80;
1711 decbytes
[0] = decbytes
[0] | 0x80;
1713 memcpy (value_contents_raw (val
), decbytes
, len
);
1716 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1717 return value_from_double (type
, -value_as_double (arg1
));
1718 else if (is_integral_type (type
))
1720 return value_from_longest (type
, -value_as_long (arg1
));
1722 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1724 struct value
*tmp
, *val
= allocate_value (type
);
1725 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1727 LONGEST low_bound
, high_bound
;
1729 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1730 error (_("Could not determine the vector bounds"));
1732 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1734 tmp
= value_neg (value_subscript (arg1
, i
));
1735 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1736 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1742 error (_("Argument to negate operation not a number."));
1743 return 0; /* For lint -- never reached. */
1748 value_complement (struct value
*arg1
)
1753 arg1
= coerce_ref (arg1
);
1754 type
= check_typedef (value_type (arg1
));
1756 if (is_integral_type (type
))
1757 val
= value_from_longest (type
, ~value_as_long (arg1
));
1758 else if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
&& TYPE_VECTOR (type
))
1761 struct type
*eltype
= check_typedef (TYPE_TARGET_TYPE (type
));
1763 LONGEST low_bound
, high_bound
;
1765 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
1766 error (_("Could not determine the vector bounds"));
1768 val
= allocate_value (type
);
1769 for (i
= 0; i
< high_bound
- low_bound
+ 1; i
++)
1771 tmp
= value_complement (value_subscript (arg1
, i
));
1772 memcpy (value_contents_writeable (val
) + i
* TYPE_LENGTH (eltype
),
1773 value_contents_all (tmp
), TYPE_LENGTH (eltype
));
1777 error (_("Argument to complement operation not an integer, boolean."));
1782 /* The INDEX'th bit of SET value whose value_type is TYPE,
1783 and whose value_contents is valaddr.
1784 Return -1 if out of range, -2 other error. */
1787 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1789 struct gdbarch
*gdbarch
= get_type_arch (type
);
1790 LONGEST low_bound
, high_bound
;
1793 struct type
*range
= TYPE_INDEX_TYPE (type
);
1795 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1797 if (index
< low_bound
|| index
> high_bound
)
1799 rel_index
= index
- low_bound
;
1800 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1801 gdbarch_byte_order (gdbarch
));
1802 rel_index
%= TARGET_CHAR_BIT
;
1803 if (gdbarch_bits_big_endian (gdbarch
))
1804 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1805 return (word
>> rel_index
) & 1;
1809 value_in (struct value
*element
, struct value
*set
)
1812 struct type
*settype
= check_typedef (value_type (set
));
1813 struct type
*eltype
= check_typedef (value_type (element
));
1815 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1816 eltype
= TYPE_TARGET_TYPE (eltype
);
1817 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1818 error (_("Second argument of 'IN' has wrong type"));
1819 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1820 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1821 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1822 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1823 error (_("First argument of 'IN' has wrong type"));
1824 member
= value_bit_index (settype
, value_contents (set
),
1825 value_as_long (element
));
1827 error (_("First argument of 'IN' not in range"));
1832 _initialize_valarith (void)
This page took 0.068877 seconds and 4 git commands to generate.