1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
5 2010 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "expression.h"
29 #include "gdb_string.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.
53 find_size_for_pointer_math (struct type
*ptr_type
)
56 struct type
*ptr_target
;
58 gdb_assert (TYPE_CODE (ptr_type
) == TYPE_CODE_PTR
);
59 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
61 sz
= TYPE_LENGTH (ptr_target
);
64 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
70 name
= TYPE_NAME (ptr_target
);
72 name
= TYPE_TAG_NAME (ptr_target
);
74 error (_("Cannot perform pointer math on incomplete types, "
75 "try casting to a known type, or void *."));
77 error (_("Cannot perform pointer math on incomplete type \"%s\", "
78 "try casting to a known type, or void *."), name
);
84 /* Given a pointer ARG1 and an integral value ARG2, return the
85 result of C-style pointer arithmetic ARG1 + ARG2. */
88 value_ptradd (struct value
*arg1
, LONGEST arg2
)
90 struct type
*valptrtype
;
93 arg1
= coerce_array (arg1
);
94 valptrtype
= check_typedef (value_type (arg1
));
95 sz
= find_size_for_pointer_math (valptrtype
);
97 return value_from_pointer (valptrtype
,
98 value_as_address (arg1
) + sz
* arg2
);
101 /* Given two compatible pointer values ARG1 and ARG2, return the
102 result of C-style pointer arithmetic ARG1 - ARG2. */
105 value_ptrdiff (struct value
*arg1
, struct value
*arg2
)
107 struct type
*type1
, *type2
;
110 arg1
= coerce_array (arg1
);
111 arg2
= coerce_array (arg2
);
112 type1
= check_typedef (value_type (arg1
));
113 type2
= check_typedef (value_type (arg2
));
115 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_PTR
);
116 gdb_assert (TYPE_CODE (type2
) == TYPE_CODE_PTR
);
118 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
119 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
121 First argument of `-' is a pointer and second argument is neither\n\
122 an integer nor a pointer of the same type."));
124 sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
127 warning (_("Type size unknown, assuming 1. "
128 "Try casting to a known type, or void *."));
132 return (value_as_long (arg1
) - value_as_long (arg2
)) / sz
;
135 /* Return the value of ARRAY[IDX].
137 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
138 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
139 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
141 See comments in value_coerce_array() for rationale for reason for
142 doing lower bounds adjustment here rather than there.
143 FIXME: Perhaps we should validate that the index is valid and if
144 verbosity is set, warn about invalid indices (but still use them). */
147 value_subscript (struct value
*array
, LONGEST index
)
150 int c_style
= current_language
->c_style_arrays
;
153 array
= coerce_ref (array
);
154 tarray
= check_typedef (value_type (array
));
156 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
157 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
159 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
160 LONGEST lowerbound
, upperbound
;
161 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
163 if (VALUE_LVAL (array
) != lval_memory
)
164 return value_subscripted_rvalue (array
, index
, lowerbound
);
168 if (index
>= lowerbound
&& index
<= upperbound
)
169 return value_subscripted_rvalue (array
, index
, lowerbound
);
170 /* Emit warning unless we have an array of unknown size.
171 An array of unknown size has lowerbound 0 and upperbound -1. */
173 warning (_("array or string index out of range"));
174 /* fall doing C stuff */
179 array
= value_coerce_array (array
);
183 return value_ind (value_ptradd (array
, index
));
185 error (_("not an array or string"));
188 /* Return the value of EXPR[IDX], expr an aggregate rvalue
189 (eg, a vector register). This routine used to promote floats
190 to doubles, but no longer does. */
193 value_subscripted_rvalue (struct value
*array
, LONGEST index
, int lowerbound
)
195 struct type
*array_type
= check_typedef (value_type (array
));
196 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
197 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
198 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
201 if (index
< lowerbound
|| (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type
)
202 && elt_offs
>= TYPE_LENGTH (array_type
)))
203 error (_("no such vector element"));
205 v
= allocate_value (elt_type
);
206 if (VALUE_LVAL (array
) == lval_memory
&& value_lazy (array
))
207 set_value_lazy (v
, 1);
209 memcpy (value_contents_writeable (v
),
210 value_contents (array
) + elt_offs
, elt_size
);
212 set_value_component_location (v
, array
);
213 VALUE_REGNUM (v
) = VALUE_REGNUM (array
);
214 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
215 set_value_offset (v
, value_offset (array
) + elt_offs
);
219 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
222 value_bitstring_subscript (struct type
*type
,
223 struct value
*bitstring
, LONGEST index
)
226 struct type
*bitstring_type
, *range_type
;
228 int offset
, byte
, bit_index
;
229 LONGEST lowerbound
, upperbound
;
231 bitstring_type
= check_typedef (value_type (bitstring
));
232 gdb_assert (TYPE_CODE (bitstring_type
) == TYPE_CODE_BITSTRING
);
234 range_type
= TYPE_INDEX_TYPE (bitstring_type
);
235 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
236 if (index
< lowerbound
|| index
> upperbound
)
237 error (_("bitstring index out of range"));
240 offset
= index
/ TARGET_CHAR_BIT
;
241 byte
= *((char *) value_contents (bitstring
) + offset
);
243 bit_index
= index
% TARGET_CHAR_BIT
;
244 byte
>>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type
)) ?
245 TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
247 v
= value_from_longest (type
, byte
& 1);
249 set_value_bitpos (v
, bit_index
);
250 set_value_bitsize (v
, 1);
251 set_value_component_location (v
, bitstring
);
252 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (bitstring
);
254 set_value_offset (v
, offset
+ value_offset (bitstring
));
260 /* Check to see if either argument is a structure, or a reference to
261 one. This is called so we know whether to go ahead with the normal
262 binop or look for a user defined function instead.
264 For now, we do not overload the `=' operator. */
267 binop_types_user_defined_p (enum exp_opcode op
,
268 struct type
*type1
, struct type
*type2
)
270 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
273 type1
= check_typedef (type1
);
274 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
275 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
277 type2
= check_typedef (type1
);
278 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
279 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
281 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
282 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
285 /* Check to see if either argument is a structure, or a reference to
286 one. This is called so we know whether to go ahead with the normal
287 binop or look for a user defined function instead.
289 For now, we do not overload the `=' operator. */
292 binop_user_defined_p (enum exp_opcode op
,
293 struct value
*arg1
, struct value
*arg2
)
295 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
298 /* Check to see if argument is a structure. This is called so
299 we know whether to go ahead with the normal unop or look for a
300 user defined function instead.
302 For now, we do not overload the `&' operator. */
305 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
310 type1
= check_typedef (value_type (arg1
));
313 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
315 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
316 type1
= TYPE_TARGET_TYPE (type1
);
322 /* We know either arg1 or arg2 is a structure, so try to find the right
323 user defined function. Create an argument vector that calls
324 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
325 binary operator which is legal for GNU C++).
327 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
328 is the opcode saying how to modify it. Otherwise, OTHEROP is
332 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
333 enum exp_opcode otherop
, enum noside noside
)
335 struct value
**argvec
;
340 arg1
= coerce_ref (arg1
);
341 arg2
= coerce_ref (arg2
);
343 /* now we know that what we have to do is construct our
344 arg vector and find the right function to call it with. */
346 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
347 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
349 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
350 argvec
[1] = value_addr (arg1
);
354 /* make the right function name up */
355 strcpy (tstr
, "operator__");
380 case BINOP_BITWISE_AND
:
383 case BINOP_BITWISE_IOR
:
386 case BINOP_BITWISE_XOR
:
389 case BINOP_LOGICAL_AND
:
392 case BINOP_LOGICAL_OR
:
404 case BINOP_ASSIGN_MODIFY
:
422 case BINOP_BITWISE_AND
:
425 case BINOP_BITWISE_IOR
:
428 case BINOP_BITWISE_XOR
:
431 case BINOP_MOD
: /* invalid */
433 error (_("Invalid binary operation specified."));
436 case BINOP_SUBSCRIPT
:
457 case BINOP_MOD
: /* invalid */
459 error (_("Invalid binary operation specified."));
462 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
468 argvec
[1] = argvec
[0];
471 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
473 struct type
*return_type
;
475 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
476 return value_zero (return_type
, VALUE_LVAL (arg1
));
478 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
480 error (_("member function %s not found"), tstr
);
482 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
486 /* We know that arg1 is a structure, so try to find a unary user
487 defined operator that matches the operator in question.
488 Create an argument vector that calls arg1.operator @ (arg1)
489 and return that value (where '@' is (almost) any unary operator which
490 is legal for GNU C++). */
493 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
495 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
496 struct value
**argvec
;
497 char *ptr
, *mangle_ptr
;
498 char tstr
[13], mangle_tstr
[13];
499 int static_memfuncp
, nargs
;
501 arg1
= coerce_ref (arg1
);
503 /* now we know that what we have to do is construct our
504 arg vector and find the right function to call it with. */
506 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
507 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
509 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
510 argvec
[1] = value_addr (arg1
);
515 /* make the right function name up */
516 strcpy (tstr
, "operator__");
518 strcpy (mangle_tstr
, "__");
519 mangle_ptr
= mangle_tstr
+ 2;
522 case UNOP_PREINCREMENT
:
525 case UNOP_PREDECREMENT
:
528 case UNOP_POSTINCREMENT
:
530 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
534 case UNOP_POSTDECREMENT
:
536 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
540 case UNOP_LOGICAL_NOT
:
543 case UNOP_COMPLEMENT
:
556 error (_("Invalid unary operation specified."));
559 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
565 argvec
[1] = argvec
[0];
569 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
571 struct type
*return_type
;
573 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
574 return value_zero (return_type
, VALUE_LVAL (arg1
));
576 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
578 error (_("member function %s not found"), tstr
);
579 return 0; /* For lint -- never reached */
583 /* Concatenate two values with the following conditions:
585 (1) Both values must be either bitstring values or character string
586 values and the resulting value consists of the concatenation of
587 ARG1 followed by ARG2.
591 One value must be an integer value and the other value must be
592 either a bitstring value or character string value, which is
593 to be repeated by the number of times specified by the integer
597 (2) Boolean values are also allowed and are treated as bit string
600 (3) Character values are also allowed and are treated as character
601 string values of length 1.
605 value_concat (struct value
*arg1
, struct value
*arg2
)
607 struct value
*inval1
;
608 struct value
*inval2
;
609 struct value
*outval
= NULL
;
610 int inval1len
, inval2len
;
614 struct type
*type1
= check_typedef (value_type (arg1
));
615 struct type
*type2
= check_typedef (value_type (arg2
));
616 struct type
*char_type
;
618 /* First figure out if we are dealing with two values to be concatenated
619 or a repeat count and a value to be repeated. INVAL1 is set to the
620 first of two concatenated values, or the repeat count. INVAL2 is set
621 to the second of the two concatenated values or the value to be
624 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
626 struct type
*tmp
= type1
;
638 /* Now process the input values. */
640 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
642 /* We have a repeat count. Validate the second value and then
643 construct a value repeated that many times. */
644 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
645 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
647 count
= longest_to_int (value_as_long (inval1
));
648 inval2len
= TYPE_LENGTH (type2
);
649 ptr
= (char *) alloca (count
* inval2len
);
650 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
653 inchar
= (char) unpack_long (type2
,
654 value_contents (inval2
));
655 for (idx
= 0; idx
< count
; idx
++)
657 *(ptr
+ idx
) = inchar
;
662 char_type
= TYPE_TARGET_TYPE (type2
);
663 for (idx
= 0; idx
< count
; idx
++)
665 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
669 outval
= value_string (ptr
, count
* inval2len
, char_type
);
671 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
672 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
674 error (_("unimplemented support for bitstring/boolean repeats"));
678 error (_("can't repeat values of that type"));
681 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
682 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
684 /* We have two character strings to concatenate. */
685 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
686 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
688 error (_("Strings can only be concatenated with other strings."));
690 inval1len
= TYPE_LENGTH (type1
);
691 inval2len
= TYPE_LENGTH (type2
);
692 ptr
= (char *) alloca (inval1len
+ inval2len
);
693 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
696 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
700 char_type
= TYPE_TARGET_TYPE (type1
);
701 memcpy (ptr
, value_contents (inval1
), inval1len
);
703 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
706 (char) unpack_long (type2
, value_contents (inval2
));
710 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
712 outval
= value_string (ptr
, inval1len
+ inval2len
, char_type
);
714 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
715 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
717 /* We have two bitstrings to concatenate. */
718 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
719 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
721 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
723 error (_("unimplemented support for bitstring/boolean concatenation."));
727 /* We don't know how to concatenate these operands. */
728 error (_("illegal operands for concatenation."));
733 /* Integer exponentiation: V1**V2, where both arguments are
734 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
736 integer_pow (LONGEST v1
, LONGEST v2
)
741 error (_("Attempt to raise 0 to negative power."));
747 /* The Russian Peasant's Algorithm */
763 /* Integer exponentiation: V1**V2, where both arguments are
764 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
766 uinteger_pow (ULONGEST v1
, LONGEST v2
)
771 error (_("Attempt to raise 0 to negative power."));
777 /* The Russian Peasant's Algorithm */
793 /* Obtain decimal value of arguments for binary operation, converting from
794 other types if one of them is not decimal floating point. */
796 value_args_as_decimal (struct value
*arg1
, struct value
*arg2
,
797 gdb_byte
*x
, int *len_x
, enum bfd_endian
*byte_order_x
,
798 gdb_byte
*y
, int *len_y
, enum bfd_endian
*byte_order_y
)
800 struct type
*type1
, *type2
;
802 type1
= check_typedef (value_type (arg1
));
803 type2
= check_typedef (value_type (arg2
));
805 /* At least one of the arguments must be of decimal float type. */
806 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
807 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
);
809 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
810 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
811 /* The DFP extension to the C language does not allow mixing of
812 * decimal float types with other float types in expressions
813 * (see WDTR 24732, page 12). */
814 error (_("Mixing decimal floating types with other floating types is not allowed."));
816 /* Obtain decimal value of arg1, converting from other types
819 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
821 *byte_order_x
= gdbarch_byte_order (get_type_arch (type1
));
822 *len_x
= TYPE_LENGTH (type1
);
823 memcpy (x
, value_contents (arg1
), *len_x
);
825 else if (is_integral_type (type1
))
827 *byte_order_x
= gdbarch_byte_order (get_type_arch (type2
));
828 *len_x
= TYPE_LENGTH (type2
);
829 decimal_from_integral (arg1
, x
, *len_x
, *byte_order_x
);
832 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
835 /* Obtain decimal value of arg2, converting from other types
838 if (TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
840 *byte_order_y
= gdbarch_byte_order (get_type_arch (type2
));
841 *len_y
= TYPE_LENGTH (type2
);
842 memcpy (y
, value_contents (arg2
), *len_y
);
844 else if (is_integral_type (type2
))
846 *byte_order_y
= gdbarch_byte_order (get_type_arch (type1
));
847 *len_y
= TYPE_LENGTH (type1
);
848 decimal_from_integral (arg2
, y
, *len_y
, *byte_order_y
);
851 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
855 /* Perform a binary operation on two operands which have reasonable
856 representations as integers or floats. This includes booleans,
857 characters, integers, or floats.
858 Does not support addition and subtraction on pointers;
859 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
862 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
865 struct type
*type1
, *type2
, *result_type
;
867 arg1
= coerce_ref (arg1
);
868 arg2
= coerce_ref (arg2
);
870 type1
= check_typedef (value_type (arg1
));
871 type2
= check_typedef (value_type (arg2
));
873 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
874 && TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
875 && !is_integral_type (type1
))
876 || (TYPE_CODE (type2
) != TYPE_CODE_FLT
877 && TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
878 && !is_integral_type (type2
)))
879 error (_("Argument to arithmetic operation not a number or boolean."));
881 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
882 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
885 int len_v1
, len_v2
, len_v
;
886 enum bfd_endian byte_order_v1
, byte_order_v2
, byte_order_v
;
887 gdb_byte v1
[16], v2
[16];
890 /* If only one type is decimal float, use its type.
891 Otherwise use the bigger type. */
892 if (TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
)
894 else if (TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
)
896 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
901 len_v
= TYPE_LENGTH (result_type
);
902 byte_order_v
= gdbarch_byte_order (get_type_arch (result_type
));
904 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
905 v2
, &len_v2
, &byte_order_v2
);
914 decimal_binop (op
, v1
, len_v1
, byte_order_v1
,
915 v2
, len_v2
, byte_order_v2
,
916 v
, len_v
, byte_order_v
);
920 error (_("Operation not valid for decimal floating point number."));
923 val
= value_from_decfloat (result_type
, v
);
925 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
926 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
928 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
929 in target format. real.c in GCC probably has the necessary
931 DOUBLEST v1
, v2
, v
= 0;
932 v1
= value_as_double (arg1
);
933 v2
= value_as_double (arg2
);
957 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
961 v
= v1
< v2
? v1
: v2
;
965 v
= v1
> v2
? v1
: v2
;
969 error (_("Integer-only operation on floating point number."));
972 /* If only one type is float, use its type.
973 Otherwise use the bigger type. */
974 if (TYPE_CODE (type1
) != TYPE_CODE_FLT
)
976 else if (TYPE_CODE (type2
) != TYPE_CODE_FLT
)
978 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
983 val
= allocate_value (result_type
);
984 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
986 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
987 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
989 LONGEST v1
, v2
, v
= 0;
990 v1
= value_as_long (arg1
);
991 v2
= value_as_long (arg2
);
995 case BINOP_BITWISE_AND
:
999 case BINOP_BITWISE_IOR
:
1003 case BINOP_BITWISE_XOR
:
1011 case BINOP_NOTEQUAL
:
1016 error (_("Invalid operation on booleans."));
1019 result_type
= type1
;
1021 val
= allocate_value (result_type
);
1022 store_signed_integer (value_contents_raw (val
),
1023 TYPE_LENGTH (result_type
),
1024 gdbarch_byte_order (get_type_arch (result_type
)),
1028 /* Integral operations here. */
1030 /* Determine type length of the result, and if the operation should
1031 be done unsigned. For exponentiation and shift operators,
1032 use the length and type of the left operand. Otherwise,
1033 use the signedness of the operand with the greater length.
1034 If both operands are of equal length, use unsigned operation
1035 if one of the operands is unsigned. */
1036 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1037 result_type
= type1
;
1038 else if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1039 result_type
= type1
;
1040 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1041 result_type
= type2
;
1042 else if (TYPE_UNSIGNED (type1
))
1043 result_type
= type1
;
1044 else if (TYPE_UNSIGNED (type2
))
1045 result_type
= type2
;
1047 result_type
= type1
;
1049 if (TYPE_UNSIGNED (result_type
))
1051 LONGEST v2_signed
= value_as_long (arg2
);
1052 ULONGEST v1
, v2
, v
= 0;
1053 v1
= (ULONGEST
) value_as_long (arg1
);
1054 v2
= (ULONGEST
) v2_signed
;
1075 error (_("Division by zero"));
1079 v
= uinteger_pow (v1
, v2_signed
);
1086 error (_("Division by zero"));
1090 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1091 v1 mod 0 has a defined value, v1. */
1099 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1112 case BINOP_BITWISE_AND
:
1116 case BINOP_BITWISE_IOR
:
1120 case BINOP_BITWISE_XOR
:
1124 case BINOP_LOGICAL_AND
:
1128 case BINOP_LOGICAL_OR
:
1133 v
= v1
< v2
? v1
: v2
;
1137 v
= v1
> v2
? v1
: v2
;
1144 case BINOP_NOTEQUAL
:
1165 error (_("Invalid binary operation on numbers."));
1168 val
= allocate_value (result_type
);
1169 store_unsigned_integer (value_contents_raw (val
),
1170 TYPE_LENGTH (value_type (val
)),
1172 (get_type_arch (result_type
)),
1177 LONGEST v1
, v2
, v
= 0;
1178 v1
= value_as_long (arg1
);
1179 v2
= value_as_long (arg2
);
1200 error (_("Division by zero"));
1204 v
= integer_pow (v1
, v2
);
1211 error (_("Division by zero"));
1215 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1216 X mod 0 has a defined value, X. */
1224 /* Compute floor. */
1225 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1241 case BINOP_BITWISE_AND
:
1245 case BINOP_BITWISE_IOR
:
1249 case BINOP_BITWISE_XOR
:
1253 case BINOP_LOGICAL_AND
:
1257 case BINOP_LOGICAL_OR
:
1262 v
= v1
< v2
? v1
: v2
;
1266 v
= v1
> v2
? v1
: v2
;
1273 case BINOP_NOTEQUAL
:
1294 error (_("Invalid binary operation on numbers."));
1297 val
= allocate_value (result_type
);
1298 store_signed_integer (value_contents_raw (val
),
1299 TYPE_LENGTH (value_type (val
)),
1301 (get_type_arch (result_type
)),
1309 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1312 value_logical_not (struct value
*arg1
)
1318 arg1
= coerce_array (arg1
);
1319 type1
= check_typedef (value_type (arg1
));
1321 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1322 return 0 == value_as_double (arg1
);
1323 else if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1324 return decimal_is_zero (value_contents (arg1
), TYPE_LENGTH (type1
),
1325 gdbarch_byte_order (get_type_arch (type1
)));
1327 len
= TYPE_LENGTH (type1
);
1328 p
= value_contents (arg1
);
1339 /* Perform a comparison on two string values (whose content are not
1340 necessarily null terminated) based on their length */
1343 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1345 int len1
= TYPE_LENGTH (value_type (arg1
));
1346 int len2
= TYPE_LENGTH (value_type (arg2
));
1347 const gdb_byte
*s1
= value_contents (arg1
);
1348 const gdb_byte
*s2
= value_contents (arg2
);
1349 int i
, len
= len1
< len2
? len1
: len2
;
1351 for (i
= 0; i
< len
; i
++)
1355 else if (s1
[i
] > s2
[i
])
1363 else if (len1
> len2
)
1369 /* Simulate the C operator == by returning a 1
1370 iff ARG1 and ARG2 have equal contents. */
1373 value_equal (struct value
*arg1
, struct value
*arg2
)
1378 struct type
*type1
, *type2
;
1379 enum type_code code1
;
1380 enum type_code code2
;
1381 int is_int1
, is_int2
;
1383 arg1
= coerce_array (arg1
);
1384 arg2
= coerce_array (arg2
);
1386 type1
= check_typedef (value_type (arg1
));
1387 type2
= check_typedef (value_type (arg2
));
1388 code1
= TYPE_CODE (type1
);
1389 code2
= TYPE_CODE (type2
);
1390 is_int1
= is_integral_type (type1
);
1391 is_int2
= is_integral_type (type2
);
1393 if (is_int1
&& is_int2
)
1394 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1396 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1397 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1399 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1400 `long double' values are returned in static storage (m68k). */
1401 DOUBLEST d
= value_as_double (arg1
);
1402 return d
== value_as_double (arg2
);
1404 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1405 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1407 gdb_byte v1
[16], v2
[16];
1409 enum bfd_endian byte_order_v1
, byte_order_v2
;
1411 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
1412 v2
, &len_v2
, &byte_order_v2
);
1414 return decimal_compare (v1
, len_v1
, byte_order_v1
,
1415 v2
, len_v2
, byte_order_v2
) == 0;
1418 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1420 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1421 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1422 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1423 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1425 else if (code1
== code2
1426 && ((len
= (int) TYPE_LENGTH (type1
))
1427 == (int) TYPE_LENGTH (type2
)))
1429 p1
= value_contents (arg1
);
1430 p2
= value_contents (arg2
);
1438 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1440 return value_strcmp (arg1
, arg2
) == 0;
1444 error (_("Invalid type combination in equality test."));
1445 return 0; /* For lint -- never reached */
1449 /* Compare values based on their raw contents. Useful for arrays since
1450 value_equal coerces them to pointers, thus comparing just the address
1451 of the array instead of its contents. */
1454 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1456 struct type
*type1
, *type2
;
1458 type1
= check_typedef (value_type (arg1
));
1459 type2
= check_typedef (value_type (arg2
));
1461 return (TYPE_CODE (type1
) == TYPE_CODE (type2
)
1462 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1463 && memcmp (value_contents (arg1
), value_contents (arg2
),
1464 TYPE_LENGTH (type1
)) == 0);
1467 /* Simulate the C operator < by returning 1
1468 iff ARG1's contents are less than ARG2's. */
1471 value_less (struct value
*arg1
, struct value
*arg2
)
1473 enum type_code code1
;
1474 enum type_code code2
;
1475 struct type
*type1
, *type2
;
1476 int is_int1
, is_int2
;
1478 arg1
= coerce_array (arg1
);
1479 arg2
= coerce_array (arg2
);
1481 type1
= check_typedef (value_type (arg1
));
1482 type2
= check_typedef (value_type (arg2
));
1483 code1
= TYPE_CODE (type1
);
1484 code2
= TYPE_CODE (type2
);
1485 is_int1
= is_integral_type (type1
);
1486 is_int2
= is_integral_type (type2
);
1488 if (is_int1
&& is_int2
)
1489 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1491 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1492 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1494 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1495 `long double' values are returned in static storage (m68k). */
1496 DOUBLEST d
= value_as_double (arg1
);
1497 return d
< value_as_double (arg2
);
1499 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1500 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1502 gdb_byte v1
[16], v2
[16];
1504 enum bfd_endian byte_order_v1
, byte_order_v2
;
1506 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
1507 v2
, &len_v2
, &byte_order_v2
);
1509 return decimal_compare (v1
, len_v1
, byte_order_v1
,
1510 v2
, len_v2
, byte_order_v2
) == -1;
1512 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1513 return value_as_address (arg1
) < value_as_address (arg2
);
1515 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1517 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1518 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1519 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1520 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1521 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1522 return value_strcmp (arg1
, arg2
) < 0;
1525 error (_("Invalid type combination in ordering comparison."));
1530 /* The unary operators +, - and ~. They free the argument ARG1. */
1533 value_pos (struct value
*arg1
)
1537 arg1
= coerce_ref (arg1
);
1538 type
= check_typedef (value_type (arg1
));
1540 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1541 return value_from_double (type
, value_as_double (arg1
));
1542 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1543 return value_from_decfloat (type
, value_contents (arg1
));
1544 else if (is_integral_type (type
))
1546 return value_from_longest (type
, value_as_long (arg1
));
1550 error ("Argument to positive operation not a number.");
1551 return 0; /* For lint -- never reached */
1556 value_neg (struct value
*arg1
)
1560 arg1
= coerce_ref (arg1
);
1561 type
= check_typedef (value_type (arg1
));
1563 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1565 struct value
*val
= allocate_value (type
);
1566 int len
= TYPE_LENGTH (type
);
1567 gdb_byte decbytes
[16]; /* a decfloat is at most 128 bits long */
1569 memcpy (decbytes
, value_contents (arg1
), len
);
1571 if (gdbarch_byte_order (get_type_arch (type
)) == BFD_ENDIAN_LITTLE
)
1572 decbytes
[len
-1] = decbytes
[len
- 1] | 0x80;
1574 decbytes
[0] = decbytes
[0] | 0x80;
1576 memcpy (value_contents_raw (val
), decbytes
, len
);
1579 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1580 return value_from_double (type
, -value_as_double (arg1
));
1581 else if (is_integral_type (type
))
1583 return value_from_longest (type
, -value_as_long (arg1
));
1587 error (_("Argument to negate operation not a number."));
1588 return 0; /* For lint -- never reached */
1593 value_complement (struct value
*arg1
)
1597 arg1
= coerce_ref (arg1
);
1598 type
= check_typedef (value_type (arg1
));
1600 if (!is_integral_type (type
))
1601 error (_("Argument to complement operation not an integer or boolean."));
1603 return value_from_longest (type
, ~value_as_long (arg1
));
1606 /* The INDEX'th bit of SET value whose value_type is TYPE,
1607 and whose value_contents is valaddr.
1608 Return -1 if out of range, -2 other error. */
1611 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1613 struct gdbarch
*gdbarch
= get_type_arch (type
);
1614 LONGEST low_bound
, high_bound
;
1617 struct type
*range
= TYPE_INDEX_TYPE (type
);
1618 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1620 if (index
< low_bound
|| index
> high_bound
)
1622 rel_index
= index
- low_bound
;
1623 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1624 gdbarch_byte_order (gdbarch
));
1625 rel_index
%= TARGET_CHAR_BIT
;
1626 if (gdbarch_bits_big_endian (gdbarch
))
1627 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1628 return (word
>> rel_index
) & 1;
1632 value_in (struct value
*element
, struct value
*set
)
1635 struct type
*settype
= check_typedef (value_type (set
));
1636 struct type
*eltype
= check_typedef (value_type (element
));
1637 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1638 eltype
= TYPE_TARGET_TYPE (eltype
);
1639 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1640 error (_("Second argument of 'IN' has wrong type"));
1641 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1642 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1643 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1644 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1645 error (_("First argument of 'IN' has wrong type"));
1646 member
= value_bit_index (settype
, value_contents (set
),
1647 value_as_long (element
));
1649 error (_("First argument of 'IN' not in range"));
1654 _initialize_valarith (void)
This page took 0.067674 seconds and 4 git commands to generate.