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
|| elt_offs
>= TYPE_LENGTH (array_type
))
202 error (_("no such vector element"));
204 v
= allocate_value (elt_type
);
205 if (VALUE_LVAL (array
) == lval_memory
&& value_lazy (array
))
206 set_value_lazy (v
, 1);
208 memcpy (value_contents_writeable (v
),
209 value_contents (array
) + elt_offs
, elt_size
);
211 set_value_component_location (v
, array
);
212 VALUE_REGNUM (v
) = VALUE_REGNUM (array
);
213 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (array
);
214 set_value_offset (v
, value_offset (array
) + elt_offs
);
218 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
221 value_bitstring_subscript (struct type
*type
,
222 struct value
*bitstring
, LONGEST index
)
225 struct type
*bitstring_type
, *range_type
;
227 int offset
, byte
, bit_index
;
228 LONGEST lowerbound
, upperbound
;
230 bitstring_type
= check_typedef (value_type (bitstring
));
231 gdb_assert (TYPE_CODE (bitstring_type
) == TYPE_CODE_BITSTRING
);
233 range_type
= TYPE_INDEX_TYPE (bitstring_type
);
234 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
235 if (index
< lowerbound
|| index
> upperbound
)
236 error (_("bitstring index out of range"));
239 offset
= index
/ TARGET_CHAR_BIT
;
240 byte
= *((char *) value_contents (bitstring
) + offset
);
242 bit_index
= index
% TARGET_CHAR_BIT
;
243 byte
>>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type
)) ?
244 TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
246 v
= value_from_longest (type
, byte
& 1);
248 set_value_bitpos (v
, bit_index
);
249 set_value_bitsize (v
, 1);
250 set_value_component_location (v
, bitstring
);
251 VALUE_FRAME_ID (v
) = VALUE_FRAME_ID (bitstring
);
253 set_value_offset (v
, offset
+ value_offset (bitstring
));
259 /* Check to see if either argument is a structure, or a reference to
260 one. This is called so we know whether to go ahead with the normal
261 binop or look for a user defined function instead.
263 For now, we do not overload the `=' operator. */
266 binop_types_user_defined_p (enum exp_opcode op
,
267 struct type
*type1
, struct type
*type2
)
269 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
272 type1
= check_typedef (type1
);
273 if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
274 type1
= check_typedef (TYPE_TARGET_TYPE (type1
));
276 type2
= check_typedef (type1
);
277 if (TYPE_CODE (type2
) == TYPE_CODE_REF
)
278 type2
= check_typedef (TYPE_TARGET_TYPE (type2
));
280 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
281 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
);
284 /* Check to see if either argument is a structure, or a reference to
285 one. This is called so we know whether to go ahead with the normal
286 binop or look for a user defined function instead.
288 For now, we do not overload the `=' operator. */
291 binop_user_defined_p (enum exp_opcode op
,
292 struct value
*arg1
, struct value
*arg2
)
294 return binop_types_user_defined_p (op
, value_type (arg1
), value_type (arg2
));
297 /* Check to see if argument is a structure. This is called so
298 we know whether to go ahead with the normal unop or look for a
299 user defined function instead.
301 For now, we do not overload the `&' operator. */
304 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
309 type1
= check_typedef (value_type (arg1
));
312 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
314 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
315 type1
= TYPE_TARGET_TYPE (type1
);
321 /* We know either arg1 or arg2 is a structure, so try to find the right
322 user defined function. Create an argument vector that calls
323 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
324 binary operator which is legal for GNU C++).
326 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
327 is the opcode saying how to modify it. Otherwise, OTHEROP is
331 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
332 enum exp_opcode otherop
, enum noside noside
)
334 struct value
**argvec
;
339 arg1
= coerce_ref (arg1
);
340 arg2
= coerce_ref (arg2
);
342 /* now we know that what we have to do is construct our
343 arg vector and find the right function to call it with. */
345 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
346 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
348 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
349 argvec
[1] = value_addr (arg1
);
353 /* make the right function name up */
354 strcpy (tstr
, "operator__");
379 case BINOP_BITWISE_AND
:
382 case BINOP_BITWISE_IOR
:
385 case BINOP_BITWISE_XOR
:
388 case BINOP_LOGICAL_AND
:
391 case BINOP_LOGICAL_OR
:
403 case BINOP_ASSIGN_MODIFY
:
421 case BINOP_BITWISE_AND
:
424 case BINOP_BITWISE_IOR
:
427 case BINOP_BITWISE_XOR
:
430 case BINOP_MOD
: /* invalid */
432 error (_("Invalid binary operation specified."));
435 case BINOP_SUBSCRIPT
:
456 case BINOP_MOD
: /* invalid */
458 error (_("Invalid binary operation specified."));
461 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
467 argvec
[1] = argvec
[0];
470 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
472 struct type
*return_type
;
474 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
475 return value_zero (return_type
, VALUE_LVAL (arg1
));
477 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
479 error (_("member function %s not found"), tstr
);
481 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
485 /* We know that arg1 is a structure, so try to find a unary user
486 defined operator that matches the operator in question.
487 Create an argument vector that calls arg1.operator @ (arg1)
488 and return that value (where '@' is (almost) any unary operator which
489 is legal for GNU C++). */
492 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
494 struct gdbarch
*gdbarch
= get_type_arch (value_type (arg1
));
495 struct value
**argvec
;
496 char *ptr
, *mangle_ptr
;
497 char tstr
[13], mangle_tstr
[13];
498 int static_memfuncp
, nargs
;
500 arg1
= coerce_ref (arg1
);
502 /* now we know that what we have to do is construct our
503 arg vector and find the right function to call it with. */
505 if (TYPE_CODE (check_typedef (value_type (arg1
))) != TYPE_CODE_STRUCT
)
506 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
508 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
509 argvec
[1] = value_addr (arg1
);
514 /* make the right function name up */
515 strcpy (tstr
, "operator__");
517 strcpy (mangle_tstr
, "__");
518 mangle_ptr
= mangle_tstr
+ 2;
521 case UNOP_PREINCREMENT
:
524 case UNOP_PREDECREMENT
:
527 case UNOP_POSTINCREMENT
:
529 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
533 case UNOP_POSTDECREMENT
:
535 argvec
[2] = value_from_longest (builtin_type (gdbarch
)->builtin_int
, 0);
539 case UNOP_LOGICAL_NOT
:
542 case UNOP_COMPLEMENT
:
555 error (_("Invalid unary operation specified."));
558 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
564 argvec
[1] = argvec
[0];
568 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
570 struct type
*return_type
;
572 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec
[0])));
573 return value_zero (return_type
, VALUE_LVAL (arg1
));
575 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
577 error (_("member function %s not found"), tstr
);
578 return 0; /* For lint -- never reached */
582 /* Concatenate two values with the following conditions:
584 (1) Both values must be either bitstring values or character string
585 values and the resulting value consists of the concatenation of
586 ARG1 followed by ARG2.
590 One value must be an integer value and the other value must be
591 either a bitstring value or character string value, which is
592 to be repeated by the number of times specified by the integer
596 (2) Boolean values are also allowed and are treated as bit string
599 (3) Character values are also allowed and are treated as character
600 string values of length 1.
604 value_concat (struct value
*arg1
, struct value
*arg2
)
606 struct value
*inval1
;
607 struct value
*inval2
;
608 struct value
*outval
= NULL
;
609 int inval1len
, inval2len
;
613 struct type
*type1
= check_typedef (value_type (arg1
));
614 struct type
*type2
= check_typedef (value_type (arg2
));
615 struct type
*char_type
;
617 /* First figure out if we are dealing with two values to be concatenated
618 or a repeat count and a value to be repeated. INVAL1 is set to the
619 first of two concatenated values, or the repeat count. INVAL2 is set
620 to the second of the two concatenated values or the value to be
623 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
625 struct type
*tmp
= type1
;
637 /* Now process the input values. */
639 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
641 /* We have a repeat count. Validate the second value and then
642 construct a value repeated that many times. */
643 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
644 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
646 count
= longest_to_int (value_as_long (inval1
));
647 inval2len
= TYPE_LENGTH (type2
);
648 ptr
= (char *) alloca (count
* inval2len
);
649 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
652 inchar
= (char) unpack_long (type2
,
653 value_contents (inval2
));
654 for (idx
= 0; idx
< count
; idx
++)
656 *(ptr
+ idx
) = inchar
;
661 char_type
= TYPE_TARGET_TYPE (type2
);
662 for (idx
= 0; idx
< count
; idx
++)
664 memcpy (ptr
+ (idx
* inval2len
), value_contents (inval2
),
668 outval
= value_string (ptr
, count
* inval2len
, char_type
);
670 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
671 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
673 error (_("unimplemented support for bitstring/boolean repeats"));
677 error (_("can't repeat values of that type"));
680 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
681 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
683 /* We have two character strings to concatenate. */
684 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
685 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
687 error (_("Strings can only be concatenated with other strings."));
689 inval1len
= TYPE_LENGTH (type1
);
690 inval2len
= TYPE_LENGTH (type2
);
691 ptr
= (char *) alloca (inval1len
+ inval2len
);
692 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
695 *ptr
= (char) unpack_long (type1
, value_contents (inval1
));
699 char_type
= TYPE_TARGET_TYPE (type1
);
700 memcpy (ptr
, value_contents (inval1
), inval1len
);
702 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
705 (char) unpack_long (type2
, value_contents (inval2
));
709 memcpy (ptr
+ inval1len
, value_contents (inval2
), inval2len
);
711 outval
= value_string (ptr
, inval1len
+ inval2len
, char_type
);
713 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
714 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
716 /* We have two bitstrings to concatenate. */
717 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
718 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
720 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
722 error (_("unimplemented support for bitstring/boolean concatenation."));
726 /* We don't know how to concatenate these operands. */
727 error (_("illegal operands for concatenation."));
732 /* Integer exponentiation: V1**V2, where both arguments are
733 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
735 integer_pow (LONGEST v1
, LONGEST v2
)
740 error (_("Attempt to raise 0 to negative power."));
746 /* The Russian Peasant's Algorithm */
762 /* Integer exponentiation: V1**V2, where both arguments are
763 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
765 uinteger_pow (ULONGEST v1
, LONGEST v2
)
770 error (_("Attempt to raise 0 to negative power."));
776 /* The Russian Peasant's Algorithm */
792 /* Obtain decimal value of arguments for binary operation, converting from
793 other types if one of them is not decimal floating point. */
795 value_args_as_decimal (struct value
*arg1
, struct value
*arg2
,
796 gdb_byte
*x
, int *len_x
, enum bfd_endian
*byte_order_x
,
797 gdb_byte
*y
, int *len_y
, enum bfd_endian
*byte_order_y
)
799 struct type
*type1
, *type2
;
801 type1
= check_typedef (value_type (arg1
));
802 type2
= check_typedef (value_type (arg2
));
804 /* At least one of the arguments must be of decimal float type. */
805 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
806 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
);
808 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
809 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
810 /* The DFP extension to the C language does not allow mixing of
811 * decimal float types with other float types in expressions
812 * (see WDTR 24732, page 12). */
813 error (_("Mixing decimal floating types with other floating types is not allowed."));
815 /* Obtain decimal value of arg1, converting from other types
818 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
820 *byte_order_x
= gdbarch_byte_order (get_type_arch (type1
));
821 *len_x
= TYPE_LENGTH (type1
);
822 memcpy (x
, value_contents (arg1
), *len_x
);
824 else if (is_integral_type (type1
))
826 *byte_order_x
= gdbarch_byte_order (get_type_arch (type2
));
827 *len_x
= TYPE_LENGTH (type2
);
828 decimal_from_integral (arg1
, x
, *len_x
, *byte_order_x
);
831 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
834 /* Obtain decimal value of arg2, converting from other types
837 if (TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
839 *byte_order_y
= gdbarch_byte_order (get_type_arch (type2
));
840 *len_y
= TYPE_LENGTH (type2
);
841 memcpy (y
, value_contents (arg2
), *len_y
);
843 else if (is_integral_type (type2
))
845 *byte_order_y
= gdbarch_byte_order (get_type_arch (type1
));
846 *len_y
= TYPE_LENGTH (type1
);
847 decimal_from_integral (arg2
, y
, *len_y
, *byte_order_y
);
850 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1
),
854 /* Perform a binary operation on two operands which have reasonable
855 representations as integers or floats. This includes booleans,
856 characters, integers, or floats.
857 Does not support addition and subtraction on pointers;
858 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
861 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
864 struct type
*type1
, *type2
, *result_type
;
866 arg1
= coerce_ref (arg1
);
867 arg2
= coerce_ref (arg2
);
869 type1
= check_typedef (value_type (arg1
));
870 type2
= check_typedef (value_type (arg2
));
872 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
873 && TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
874 && !is_integral_type (type1
))
875 || (TYPE_CODE (type2
) != TYPE_CODE_FLT
876 && TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
877 && !is_integral_type (type2
)))
878 error (_("Argument to arithmetic operation not a number or boolean."));
880 if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
881 || TYPE_CODE (type2
) == TYPE_CODE_DECFLOAT
)
884 int len_v1
, len_v2
, len_v
;
885 enum bfd_endian byte_order_v1
, byte_order_v2
, byte_order_v
;
886 gdb_byte v1
[16], v2
[16];
889 /* If only one type is decimal float, use its type.
890 Otherwise use the bigger type. */
891 if (TYPE_CODE (type1
) != TYPE_CODE_DECFLOAT
)
893 else if (TYPE_CODE (type2
) != TYPE_CODE_DECFLOAT
)
895 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
900 len_v
= TYPE_LENGTH (result_type
);
901 byte_order_v
= gdbarch_byte_order (get_type_arch (result_type
));
903 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
904 v2
, &len_v2
, &byte_order_v2
);
913 decimal_binop (op
, v1
, len_v1
, byte_order_v1
,
914 v2
, len_v2
, byte_order_v2
,
915 v
, len_v
, byte_order_v
);
919 error (_("Operation not valid for decimal floating point number."));
922 val
= value_from_decfloat (result_type
, v
);
924 else if (TYPE_CODE (type1
) == TYPE_CODE_FLT
925 || TYPE_CODE (type2
) == TYPE_CODE_FLT
)
927 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
928 in target format. real.c in GCC probably has the necessary
930 DOUBLEST v1
, v2
, v
= 0;
931 v1
= value_as_double (arg1
);
932 v2
= value_as_double (arg2
);
956 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno
));
960 v
= v1
< v2
? v1
: v2
;
964 v
= v1
> v2
? v1
: v2
;
968 error (_("Integer-only operation on floating point number."));
971 /* If only one type is float, use its type.
972 Otherwise use the bigger type. */
973 if (TYPE_CODE (type1
) != TYPE_CODE_FLT
)
975 else if (TYPE_CODE (type2
) != TYPE_CODE_FLT
)
977 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
982 val
= allocate_value (result_type
);
983 store_typed_floating (value_contents_raw (val
), value_type (val
), v
);
985 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
986 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
988 LONGEST v1
, v2
, v
= 0;
989 v1
= value_as_long (arg1
);
990 v2
= value_as_long (arg2
);
994 case BINOP_BITWISE_AND
:
998 case BINOP_BITWISE_IOR
:
1002 case BINOP_BITWISE_XOR
:
1010 case BINOP_NOTEQUAL
:
1015 error (_("Invalid operation on booleans."));
1018 result_type
= type1
;
1020 val
= allocate_value (result_type
);
1021 store_signed_integer (value_contents_raw (val
),
1022 TYPE_LENGTH (result_type
),
1023 gdbarch_byte_order (get_type_arch (result_type
)),
1027 /* Integral operations here. */
1029 /* Determine type length of the result, and if the operation should
1030 be done unsigned. For exponentiation and shift operators,
1031 use the length and type of the left operand. Otherwise,
1032 use the signedness of the operand with the greater length.
1033 If both operands are of equal length, use unsigned operation
1034 if one of the operands is unsigned. */
1035 if (op
== BINOP_RSH
|| op
== BINOP_LSH
|| op
== BINOP_EXP
)
1036 result_type
= type1
;
1037 else if (TYPE_LENGTH (type1
) > TYPE_LENGTH (type2
))
1038 result_type
= type1
;
1039 else if (TYPE_LENGTH (type2
) > TYPE_LENGTH (type1
))
1040 result_type
= type2
;
1041 else if (TYPE_UNSIGNED (type1
))
1042 result_type
= type1
;
1043 else if (TYPE_UNSIGNED (type2
))
1044 result_type
= type2
;
1046 result_type
= type1
;
1048 if (TYPE_UNSIGNED (result_type
))
1050 LONGEST v2_signed
= value_as_long (arg2
);
1051 ULONGEST v1
, v2
, v
= 0;
1052 v1
= (ULONGEST
) value_as_long (arg1
);
1053 v2
= (ULONGEST
) v2_signed
;
1074 error (_("Division by zero"));
1078 v
= uinteger_pow (v1
, v2_signed
);
1085 error (_("Division by zero"));
1089 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1090 v1 mod 0 has a defined value, v1. */
1098 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1111 case BINOP_BITWISE_AND
:
1115 case BINOP_BITWISE_IOR
:
1119 case BINOP_BITWISE_XOR
:
1123 case BINOP_LOGICAL_AND
:
1127 case BINOP_LOGICAL_OR
:
1132 v
= v1
< v2
? v1
: v2
;
1136 v
= v1
> v2
? v1
: v2
;
1143 case BINOP_NOTEQUAL
:
1164 error (_("Invalid binary operation on numbers."));
1167 val
= allocate_value (result_type
);
1168 store_unsigned_integer (value_contents_raw (val
),
1169 TYPE_LENGTH (value_type (val
)),
1171 (get_type_arch (result_type
)),
1176 LONGEST v1
, v2
, v
= 0;
1177 v1
= value_as_long (arg1
);
1178 v2
= value_as_long (arg2
);
1199 error (_("Division by zero"));
1203 v
= integer_pow (v1
, v2
);
1210 error (_("Division by zero"));
1214 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1215 X mod 0 has a defined value, X. */
1223 /* Compute floor. */
1224 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1240 case BINOP_BITWISE_AND
:
1244 case BINOP_BITWISE_IOR
:
1248 case BINOP_BITWISE_XOR
:
1252 case BINOP_LOGICAL_AND
:
1256 case BINOP_LOGICAL_OR
:
1261 v
= v1
< v2
? v1
: v2
;
1265 v
= v1
> v2
? v1
: v2
;
1272 case BINOP_NOTEQUAL
:
1293 error (_("Invalid binary operation on numbers."));
1296 val
= allocate_value (result_type
);
1297 store_signed_integer (value_contents_raw (val
),
1298 TYPE_LENGTH (value_type (val
)),
1300 (get_type_arch (result_type
)),
1308 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1311 value_logical_not (struct value
*arg1
)
1317 arg1
= coerce_array (arg1
);
1318 type1
= check_typedef (value_type (arg1
));
1320 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1321 return 0 == value_as_double (arg1
);
1322 else if (TYPE_CODE (type1
) == TYPE_CODE_DECFLOAT
)
1323 return decimal_is_zero (value_contents (arg1
), TYPE_LENGTH (type1
),
1324 gdbarch_byte_order (get_type_arch (type1
)));
1326 len
= TYPE_LENGTH (type1
);
1327 p
= value_contents (arg1
);
1338 /* Perform a comparison on two string values (whose content are not
1339 necessarily null terminated) based on their length */
1342 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1344 int len1
= TYPE_LENGTH (value_type (arg1
));
1345 int len2
= TYPE_LENGTH (value_type (arg2
));
1346 const gdb_byte
*s1
= value_contents (arg1
);
1347 const gdb_byte
*s2
= value_contents (arg2
);
1348 int i
, len
= len1
< len2
? len1
: len2
;
1350 for (i
= 0; i
< len
; i
++)
1354 else if (s1
[i
] > s2
[i
])
1362 else if (len1
> len2
)
1368 /* Simulate the C operator == by returning a 1
1369 iff ARG1 and ARG2 have equal contents. */
1372 value_equal (struct value
*arg1
, struct value
*arg2
)
1377 struct type
*type1
, *type2
;
1378 enum type_code code1
;
1379 enum type_code code2
;
1380 int is_int1
, is_int2
;
1382 arg1
= coerce_array (arg1
);
1383 arg2
= coerce_array (arg2
);
1385 type1
= check_typedef (value_type (arg1
));
1386 type2
= check_typedef (value_type (arg2
));
1387 code1
= TYPE_CODE (type1
);
1388 code2
= TYPE_CODE (type2
);
1389 is_int1
= is_integral_type (type1
);
1390 is_int2
= is_integral_type (type2
);
1392 if (is_int1
&& is_int2
)
1393 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1395 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1396 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1398 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1399 `long double' values are returned in static storage (m68k). */
1400 DOUBLEST d
= value_as_double (arg1
);
1401 return d
== value_as_double (arg2
);
1403 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1404 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1406 gdb_byte v1
[16], v2
[16];
1408 enum bfd_endian byte_order_v1
, byte_order_v2
;
1410 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
1411 v2
, &len_v2
, &byte_order_v2
);
1413 return decimal_compare (v1
, len_v1
, byte_order_v1
,
1414 v2
, len_v2
, byte_order_v2
) == 0;
1417 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1419 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1420 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1421 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1422 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1424 else if (code1
== code2
1425 && ((len
= (int) TYPE_LENGTH (type1
))
1426 == (int) TYPE_LENGTH (type2
)))
1428 p1
= value_contents (arg1
);
1429 p2
= value_contents (arg2
);
1437 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1439 return value_strcmp (arg1
, arg2
) == 0;
1443 error (_("Invalid type combination in equality test."));
1444 return 0; /* For lint -- never reached */
1448 /* Compare values based on their raw contents. Useful for arrays since
1449 value_equal coerces them to pointers, thus comparing just the address
1450 of the array instead of its contents. */
1453 value_equal_contents (struct value
*arg1
, struct value
*arg2
)
1455 struct type
*type1
, *type2
;
1457 type1
= check_typedef (value_type (arg1
));
1458 type2
= check_typedef (value_type (arg2
));
1460 return (TYPE_CODE (type1
) == TYPE_CODE (type2
)
1461 && TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1462 && memcmp (value_contents (arg1
), value_contents (arg2
),
1463 TYPE_LENGTH (type1
)) == 0);
1466 /* Simulate the C operator < by returning 1
1467 iff ARG1's contents are less than ARG2's. */
1470 value_less (struct value
*arg1
, struct value
*arg2
)
1472 enum type_code code1
;
1473 enum type_code code2
;
1474 struct type
*type1
, *type2
;
1475 int is_int1
, is_int2
;
1477 arg1
= coerce_array (arg1
);
1478 arg2
= coerce_array (arg2
);
1480 type1
= check_typedef (value_type (arg1
));
1481 type2
= check_typedef (value_type (arg2
));
1482 code1
= TYPE_CODE (type1
);
1483 code2
= TYPE_CODE (type2
);
1484 is_int1
= is_integral_type (type1
);
1485 is_int2
= is_integral_type (type2
);
1487 if (is_int1
&& is_int2
)
1488 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1490 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1491 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1493 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1494 `long double' values are returned in static storage (m68k). */
1495 DOUBLEST d
= value_as_double (arg1
);
1496 return d
< value_as_double (arg2
);
1498 else if ((code1
== TYPE_CODE_DECFLOAT
|| is_int1
)
1499 && (code2
== TYPE_CODE_DECFLOAT
|| is_int2
))
1501 gdb_byte v1
[16], v2
[16];
1503 enum bfd_endian byte_order_v1
, byte_order_v2
;
1505 value_args_as_decimal (arg1
, arg2
, v1
, &len_v1
, &byte_order_v1
,
1506 v2
, &len_v2
, &byte_order_v2
);
1508 return decimal_compare (v1
, len_v1
, byte_order_v1
,
1509 v2
, len_v2
, byte_order_v2
) == -1;
1511 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1512 return value_as_address (arg1
) < value_as_address (arg2
);
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
);
1520 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1521 return value_strcmp (arg1
, arg2
) < 0;
1524 error (_("Invalid type combination in ordering comparison."));
1529 /* The unary operators +, - and ~. They free the argument ARG1. */
1532 value_pos (struct value
*arg1
)
1536 arg1
= coerce_ref (arg1
);
1537 type
= check_typedef (value_type (arg1
));
1539 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1540 return value_from_double (type
, value_as_double (arg1
));
1541 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1542 return value_from_decfloat (type
, value_contents (arg1
));
1543 else if (is_integral_type (type
))
1545 return value_from_longest (type
, value_as_long (arg1
));
1549 error ("Argument to positive operation not a number.");
1550 return 0; /* For lint -- never reached */
1555 value_neg (struct value
*arg1
)
1559 arg1
= coerce_ref (arg1
);
1560 type
= check_typedef (value_type (arg1
));
1562 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1564 struct value
*val
= allocate_value (type
);
1565 int len
= TYPE_LENGTH (type
);
1566 gdb_byte decbytes
[16]; /* a decfloat is at most 128 bits long */
1568 memcpy (decbytes
, value_contents (arg1
), len
);
1570 if (gdbarch_byte_order (get_type_arch (type
)) == BFD_ENDIAN_LITTLE
)
1571 decbytes
[len
-1] = decbytes
[len
- 1] | 0x80;
1573 decbytes
[0] = decbytes
[0] | 0x80;
1575 memcpy (value_contents_raw (val
), decbytes
, len
);
1578 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1579 return value_from_double (type
, -value_as_double (arg1
));
1580 else if (is_integral_type (type
))
1582 return value_from_longest (type
, -value_as_long (arg1
));
1586 error (_("Argument to negate operation not a number."));
1587 return 0; /* For lint -- never reached */
1592 value_complement (struct value
*arg1
)
1596 arg1
= coerce_ref (arg1
);
1597 type
= check_typedef (value_type (arg1
));
1599 if (!is_integral_type (type
))
1600 error (_("Argument to complement operation not an integer or boolean."));
1602 return value_from_longest (type
, ~value_as_long (arg1
));
1605 /* The INDEX'th bit of SET value whose value_type is TYPE,
1606 and whose value_contents is valaddr.
1607 Return -1 if out of range, -2 other error. */
1610 value_bit_index (struct type
*type
, const gdb_byte
*valaddr
, int index
)
1612 struct gdbarch
*gdbarch
= get_type_arch (type
);
1613 LONGEST low_bound
, high_bound
;
1616 struct type
*range
= TYPE_INDEX_TYPE (type
);
1617 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1619 if (index
< low_bound
|| index
> high_bound
)
1621 rel_index
= index
- low_bound
;
1622 word
= extract_unsigned_integer (valaddr
+ (rel_index
/ TARGET_CHAR_BIT
), 1,
1623 gdbarch_byte_order (gdbarch
));
1624 rel_index
%= TARGET_CHAR_BIT
;
1625 if (gdbarch_bits_big_endian (gdbarch
))
1626 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1627 return (word
>> rel_index
) & 1;
1631 value_in (struct value
*element
, struct value
*set
)
1634 struct type
*settype
= check_typedef (value_type (set
));
1635 struct type
*eltype
= check_typedef (value_type (element
));
1636 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1637 eltype
= TYPE_TARGET_TYPE (eltype
);
1638 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1639 error (_("Second argument of 'IN' has wrong type"));
1640 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1641 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1642 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1643 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1644 error (_("First argument of 'IN' has wrong type"));
1645 member
= value_bit_index (settype
, value_contents (set
),
1646 value_as_long (element
));
1648 error (_("First argument of 'IN' not in range"));
1653 _initialize_valarith (void)
This page took 0.067438 seconds and 4 git commands to generate.