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.
142 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
144 See comments in value_coerce_array() for rationale for reason for
145 doing lower bounds adjustment here rather than there.
146 FIXME: Perhaps we should validate that the index is valid and if
147 verbosity is set, warn about invalid indices (but still use them). */
150 value_subscript (struct value *array, LONGEST index)
152 int c_style = current_language->c_style_arrays;
155 array = coerce_ref (array);
156 tarray = check_typedef (value_type (array));
158 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
159 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
161 struct type *range_type = TYPE_INDEX_TYPE (tarray);
162 LONGEST lowerbound, upperbound;
164 get_discrete_bounds (range_type, &lowerbound, &upperbound);
165 if (VALUE_LVAL (array) != lval_memory)
166 return value_subscripted_rvalue (array, index, lowerbound);
170 if (index >= lowerbound && index <= upperbound)
171 return value_subscripted_rvalue (array, index, lowerbound);
172 /* Emit warning unless we have an array of unknown size.
173 An array of unknown size has lowerbound 0 and upperbound -1. */
175 warning (_("array or string index out of range"));
176 /* fall doing C stuff */
181 array = value_coerce_array (array);
185 return value_ind (value_ptradd (array, index));
187 error (_("not an array or string"));
190 /* Return the value of EXPR[IDX], expr an aggregate rvalue
191 (eg, a vector register). This routine used to promote floats
192 to doubles, but no longer does. */
195 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
197 struct type *array_type = check_typedef (value_type (array));
198 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
199 unsigned int elt_size = TYPE_LENGTH (elt_type);
200 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
203 if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
204 && elt_offs >= TYPE_LENGTH (array_type)))
205 error (_("no such vector element"));
207 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
208 v = allocate_value_lazy (elt_type);
211 v = allocate_value (elt_type);
212 value_contents_copy (v, value_embedded_offset (v),
213 array, value_embedded_offset (array) + elt_offs,
217 set_value_component_location (v, array);
218 VALUE_REGNUM (v) = VALUE_REGNUM (array);
219 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
220 set_value_offset (v, value_offset (array) + elt_offs);
224 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
227 value_bitstring_subscript (struct type *type,
228 struct value *bitstring, LONGEST index)
231 struct type *bitstring_type, *range_type;
233 int offset, byte, bit_index;
234 LONGEST lowerbound, upperbound;
236 bitstring_type = check_typedef (value_type (bitstring));
237 gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
239 range_type = TYPE_INDEX_TYPE (bitstring_type);
240 get_discrete_bounds (range_type, &lowerbound, &upperbound);
241 if (index < lowerbound || index > upperbound)
242 error (_("bitstring index out of range"));
245 offset = index / TARGET_CHAR_BIT;
246 byte = *((char *) value_contents (bitstring) + offset);
248 bit_index = index % TARGET_CHAR_BIT;
249 byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ?
250 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
252 v = value_from_longest (type, byte & 1);
254 set_value_bitpos (v, bit_index);
255 set_value_bitsize (v, 1);
256 set_value_component_location (v, bitstring);
257 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
259 set_value_offset (v, offset + value_offset (bitstring));
265 /* Check to see if either argument is a structure, or a reference to
266 one. This is called so we know whether to go ahead with the normal
267 binop or look for a user defined function instead.
269 For now, we do not overload the `=' operator. */
272 binop_types_user_defined_p (enum exp_opcode op,
273 struct type *type1, struct type *type2)
275 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
278 type1 = check_typedef (type1);
279 if (TYPE_CODE (type1) == TYPE_CODE_REF)
280 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
282 type2 = check_typedef (type2);
283 if (TYPE_CODE (type2) == TYPE_CODE_REF)
284 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
286 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
287 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
290 /* Check to see if either argument is a structure, or a reference to
291 one. This is called so we know whether to go ahead with the normal
292 binop or look for a user defined function instead.
294 For now, we do not overload the `=' operator. */
297 binop_user_defined_p (enum exp_opcode op,
298 struct value *arg1, struct value *arg2)
300 return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
303 /* Check to see if argument is a structure. This is called so
304 we know whether to go ahead with the normal unop or look for a
305 user defined function instead.
307 For now, we do not overload the `&' operator. */
310 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
316 type1 = check_typedef (value_type (arg1));
317 if (TYPE_CODE (type1) == TYPE_CODE_REF)
318 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
319 return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
322 /* Try to find an operator named OPERATOR which takes NARGS arguments
323 specified in ARGS. If the operator found is a static member operator
324 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
325 The search if performed through find_overload_match which will handle
326 member operators, non member operators, operators imported implicitly or
327 explicitly, and perform correct overload resolution in all of the above
328 situations or combinations thereof. */
330 static struct value *
331 value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
332 int *static_memfuncp)
335 struct symbol *symp = NULL;
336 struct value *valp = NULL;
338 find_overload_match (args, nargs, operator, BOTH /* could be method */,
339 0 /* strict match */, &args[0], /* objp */
340 NULL /* pass NULL symbol since symbol is unknown */,
341 &valp, &symp, static_memfuncp, 0);
348 /* This is a non member function and does not
349 expect a reference as its first argument
350 rather the explicit structure. */
351 args[0] = value_ind (args[0]);
352 return value_of_variable (symp, 0);
355 error (_("Could not find %s."), operator);
358 /* Lookup user defined operator NAME. Return a value representing the
359 function, otherwise return NULL. */
361 static struct value *
362 value_user_defined_op (struct value **argp, struct value **args, char *name,
363 int *static_memfuncp, int nargs)
365 struct value *result = NULL;
367 if (current_language->la_language == language_cplus)
368 result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp);
370 result = value_struct_elt (argp, args, name, static_memfuncp,
376 /* We know either arg1 or arg2 is a structure, so try to find the right
377 user defined function. Create an argument vector that calls
378 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
379 binary operator which is legal for GNU C++).
381 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
382 is the opcode saying how to modify it. Otherwise, OTHEROP is
386 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
387 enum exp_opcode otherop, enum noside noside)
389 struct value **argvec;
394 arg1 = coerce_ref (arg1);
395 arg2 = coerce_ref (arg2);
397 /* now we know that what we have to do is construct our
398 arg vector and find the right function to call it with. */
400 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
401 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
403 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
404 argvec[1] = value_addr (arg1);
408 /* Make the right function name up. */
409 strcpy (tstr, "operator__");
434 case BINOP_BITWISE_AND:
437 case BINOP_BITWISE_IOR:
440 case BINOP_BITWISE_XOR:
443 case BINOP_LOGICAL_AND:
446 case BINOP_LOGICAL_OR:
458 case BINOP_ASSIGN_MODIFY:
476 case BINOP_BITWISE_AND:
479 case BINOP_BITWISE_IOR:
482 case BINOP_BITWISE_XOR:
485 case BINOP_MOD: /* invalid */
487 error (_("Invalid binary operation specified."));
490 case BINOP_SUBSCRIPT:
511 case BINOP_MOD: /* invalid */
513 error (_("Invalid binary operation specified."));
516 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
517 &static_memfuncp, 2);
523 argvec[1] = argvec[0];
526 if (noside == EVAL_AVOID_SIDE_EFFECTS)
528 struct type *return_type;
531 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
532 return value_zero (return_type, VALUE_LVAL (arg1));
534 return call_function_by_hand (argvec[0], 2 - static_memfuncp,
537 throw_error (NOT_FOUND_ERROR,
538 _("member function %s not found"), tstr);
540 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
544 /* We know that arg1 is a structure, so try to find a unary user
545 defined operator that matches the operator in question.
546 Create an argument vector that calls arg1.operator @ (arg1)
547 and return that value (where '@' is (almost) any unary operator which
548 is legal for GNU C++). */
551 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
553 struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
554 struct value **argvec;
555 char *ptr, *mangle_ptr;
556 char tstr[13], mangle_tstr[13];
557 int static_memfuncp, nargs;
559 arg1 = coerce_ref (arg1);
561 /* now we know that what we have to do is construct our
562 arg vector and find the right function to call it with. */
564 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
565 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
567 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
568 argvec[1] = value_addr (arg1);
573 /* Make the right function name up. */
574 strcpy (tstr, "operator__");
576 strcpy (mangle_tstr, "__");
577 mangle_ptr = mangle_tstr + 2;
580 case UNOP_PREINCREMENT:
583 case UNOP_PREDECREMENT:
586 case UNOP_POSTINCREMENT:
588 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
592 case UNOP_POSTDECREMENT:
594 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
598 case UNOP_LOGICAL_NOT:
601 case UNOP_COMPLEMENT:
617 error (_("Invalid unary operation specified."));
620 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
621 &static_memfuncp, nargs);
627 argvec[1] = argvec[0];
631 if (noside == EVAL_AVOID_SIDE_EFFECTS)
633 struct type *return_type;
636 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
637 return value_zero (return_type, VALUE_LVAL (arg1));
639 return call_function_by_hand (argvec[0], nargs, argvec + 1);
641 throw_error (NOT_FOUND_ERROR,
642 _("member function %s not found"), tstr);
644 return 0; /* For lint -- never reached */
648 /* Concatenate two values with the following conditions:
650 (1) Both values must be either bitstring values or character string
651 values and the resulting value consists of the concatenation of
652 ARG1 followed by ARG2.
656 One value must be an integer value and the other value must be
657 either a bitstring value or character string value, which is
658 to be repeated by the number of times specified by the integer
662 (2) Boolean values are also allowed and are treated as bit string
665 (3) Character values are also allowed and are treated as character
666 string values of length 1. */
669 value_concat (struct value *arg1, struct value *arg2)
671 struct value *inval1;
672 struct value *inval2;
673 struct value *outval = NULL;
674 int inval1len, inval2len;
678 struct type *type1 = check_typedef (value_type (arg1));
679 struct type *type2 = check_typedef (value_type (arg2));
680 struct type *char_type;
682 /* First figure out if we are dealing with two values to be concatenated
683 or a repeat count and a value to be repeated. INVAL1 is set to the
684 first of two concatenated values, or the repeat count. INVAL2 is set
685 to the second of the two concatenated values or the value to be
688 if (TYPE_CODE (type2) == TYPE_CODE_INT)
690 struct type *tmp = type1;
703 /* Now process the input values. */
705 if (TYPE_CODE (type1) == TYPE_CODE_INT)
707 /* We have a repeat count. Validate the second value and then
708 construct a value repeated that many times. */
709 if (TYPE_CODE (type2) == TYPE_CODE_STRING
710 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
712 count = longest_to_int (value_as_long (inval1));
713 inval2len = TYPE_LENGTH (type2);
714 ptr = (char *) alloca (count * inval2len);
715 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
719 inchar = (char) unpack_long (type2,
720 value_contents (inval2));
721 for (idx = 0; idx < count; idx++)
723 *(ptr + idx) = inchar;
728 char_type = TYPE_TARGET_TYPE (type2);
730 for (idx = 0; idx < count; idx++)
732 memcpy (ptr + (idx * inval2len), value_contents (inval2),
736 outval = value_string (ptr, count * inval2len, char_type);
738 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
739 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
741 error (_("unimplemented support for bitstring/boolean repeats"));
745 error (_("can't repeat values of that type"));
748 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
749 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
751 /* We have two character strings to concatenate. */
752 if (TYPE_CODE (type2) != TYPE_CODE_STRING
753 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
755 error (_("Strings can only be concatenated with other strings."));
757 inval1len = TYPE_LENGTH (type1);
758 inval2len = TYPE_LENGTH (type2);
759 ptr = (char *) alloca (inval1len + inval2len);
760 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
764 *ptr = (char) unpack_long (type1, value_contents (inval1));
768 char_type = TYPE_TARGET_TYPE (type1);
770 memcpy (ptr, value_contents (inval1), inval1len);
772 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
775 (char) unpack_long (type2, value_contents (inval2));
779 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
781 outval = value_string (ptr, inval1len + inval2len, char_type);
783 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
784 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
786 /* We have two bitstrings to concatenate. */
787 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
788 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
790 error (_("Bitstrings or booleans can only be concatenated "
791 "with other bitstrings or booleans."));
793 error (_("unimplemented support for bitstring/boolean concatenation."));
797 /* We don't know how to concatenate these operands. */
798 error (_("illegal operands for concatenation."));
803 /* Integer exponentiation: V1**V2, where both arguments are
804 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
807 integer_pow (LONGEST v1, LONGEST v2)
812 error (_("Attempt to raise 0 to negative power."));
818 /* The Russian Peasant's Algorithm. */
834 /* Integer exponentiation: V1**V2, where both arguments are
835 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
838 uinteger_pow (ULONGEST v1, LONGEST v2)
843 error (_("Attempt to raise 0 to negative power."));
849 /* The Russian Peasant's Algorithm. */
865 /* Obtain decimal value of arguments for binary operation, converting from
866 other types if one of them is not decimal floating point. */
868 value_args_as_decimal (struct value *arg1, struct value *arg2,
869 gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
870 gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
872 struct type *type1, *type2;
874 type1 = check_typedef (value_type (arg1));
875 type2 = check_typedef (value_type (arg2));
877 /* At least one of the arguments must be of decimal float type. */
878 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
879 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
881 if (TYPE_CODE (type1) == TYPE_CODE_FLT
882 || TYPE_CODE (type2) == TYPE_CODE_FLT)
883 /* The DFP extension to the C language does not allow mixing of
884 * decimal float types with other float types in expressions
885 * (see WDTR 24732, page 12). */
886 error (_("Mixing decimal floating types with "
887 "other floating types is not allowed."));
889 /* Obtain decimal value of arg1, converting from other types
892 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
894 *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
895 *len_x = TYPE_LENGTH (type1);
896 memcpy (x, value_contents (arg1), *len_x);
898 else if (is_integral_type (type1))
900 *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
901 *len_x = TYPE_LENGTH (type2);
902 decimal_from_integral (arg1, x, *len_x, *byte_order_x);
905 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
908 /* Obtain decimal value of arg2, converting from other types
911 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
913 *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
914 *len_y = TYPE_LENGTH (type2);
915 memcpy (y, value_contents (arg2), *len_y);
917 else if (is_integral_type (type2))
919 *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
920 *len_y = TYPE_LENGTH (type1);
921 decimal_from_integral (arg2, y, *len_y, *byte_order_y);
924 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
928 /* Perform a binary operation on two operands which have reasonable
929 representations as integers or floats. This includes booleans,
930 characters, integers, or floats.
931 Does not support addition and subtraction on pointers;
932 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
934 static struct value *
935 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
938 struct type *type1, *type2, *result_type;
940 arg1 = coerce_ref (arg1);
941 arg2 = coerce_ref (arg2);
943 type1 = check_typedef (value_type (arg1));
944 type2 = check_typedef (value_type (arg2));
946 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
947 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
948 && !is_integral_type (type1))
949 || (TYPE_CODE (type2) != TYPE_CODE_FLT
950 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
951 && !is_integral_type (type2)))
952 error (_("Argument to arithmetic operation not a number or boolean."));
954 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
955 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
957 int len_v1, len_v2, len_v;
958 enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
959 gdb_byte v1[16], v2[16];
962 /* If only one type is decimal float, use its type.
963 Otherwise use the bigger type. */
964 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
966 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
968 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
973 len_v = TYPE_LENGTH (result_type);
974 byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
976 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
977 v2, &len_v2, &byte_order_v2);
986 decimal_binop (op, v1, len_v1, byte_order_v1,
987 v2, len_v2, byte_order_v2,
988 v, len_v, byte_order_v);
992 error (_("Operation not valid for decimal floating point number."));
995 val = value_from_decfloat (result_type, v);
997 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
998 || TYPE_CODE (type2) == TYPE_CODE_FLT)
1000 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1001 in target format. real.c in GCC probably has the necessary
1003 DOUBLEST v1, v2, v = 0;
1005 v1 = value_as_double (arg1);
1006 v2 = value_as_double (arg2);
1030 error (_("Cannot perform exponentiation: %s"),
1031 safe_strerror (errno));
1035 v = v1 < v2 ? v1 : v2;
1039 v = v1 > v2 ? v1 : v2;
1043 error (_("Integer-only operation on floating point number."));
1046 /* If only one type is float, use its type.
1047 Otherwise use the bigger type. */
1048 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
1049 result_type = type2;
1050 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
1051 result_type = type1;
1052 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1053 result_type = type2;
1055 result_type = type1;
1057 val = allocate_value (result_type);
1058 store_typed_floating (value_contents_raw (val), value_type (val), v);
1060 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
1061 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
1063 LONGEST v1, v2, v = 0;
1065 v1 = value_as_long (arg1);
1066 v2 = value_as_long (arg2);
1070 case BINOP_BITWISE_AND:
1074 case BINOP_BITWISE_IOR:
1078 case BINOP_BITWISE_XOR:
1086 case BINOP_NOTEQUAL:
1091 error (_("Invalid operation on booleans."));
1094 result_type = type1;
1096 val = allocate_value (result_type);
1097 store_signed_integer (value_contents_raw (val),
1098 TYPE_LENGTH (result_type),
1099 gdbarch_byte_order (get_type_arch (result_type)),
1103 /* Integral operations here. */
1105 /* Determine type length of the result, and if the operation should
1106 be done unsigned. For exponentiation and shift operators,
1107 use the length and type of the left operand. Otherwise,
1108 use the signedness of the operand with the greater length.
1109 If both operands are of equal length, use unsigned operation
1110 if one of the operands is unsigned. */
1111 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1112 result_type = type1;
1113 else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1114 result_type = type1;
1115 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1116 result_type = type2;
1117 else if (TYPE_UNSIGNED (type1))
1118 result_type = type1;
1119 else if (TYPE_UNSIGNED (type2))
1120 result_type = type2;
1122 result_type = type1;
1124 if (TYPE_UNSIGNED (result_type))
1126 LONGEST v2_signed = value_as_long (arg2);
1127 ULONGEST v1, v2, v = 0;
1129 v1 = (ULONGEST) value_as_long (arg1);
1130 v2 = (ULONGEST) v2_signed;
1151 error (_("Division by zero"));
1155 v = uinteger_pow (v1, v2_signed);
1162 error (_("Division by zero"));
1166 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1167 v1 mod 0 has a defined value, v1. */
1175 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1188 case BINOP_BITWISE_AND:
1192 case BINOP_BITWISE_IOR:
1196 case BINOP_BITWISE_XOR:
1200 case BINOP_LOGICAL_AND:
1204 case BINOP_LOGICAL_OR:
1209 v = v1 < v2 ? v1 : v2;
1213 v = v1 > v2 ? v1 : v2;
1220 case BINOP_NOTEQUAL:
1241 error (_("Invalid binary operation on numbers."));
1244 val = allocate_value (result_type);
1245 store_unsigned_integer (value_contents_raw (val),
1246 TYPE_LENGTH (value_type (val)),
1248 (get_type_arch (result_type)),
1253 LONGEST v1, v2, v = 0;
1255 v1 = value_as_long (arg1);
1256 v2 = value_as_long (arg2);
1277 error (_("Division by zero"));
1281 v = integer_pow (v1, v2);
1288 error (_("Division by zero"));
1292 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1293 X mod 0 has a defined value, X. */
1301 /* Compute floor. */
1302 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1318 case BINOP_BITWISE_AND:
1322 case BINOP_BITWISE_IOR:
1326 case BINOP_BITWISE_XOR:
1330 case BINOP_LOGICAL_AND:
1334 case BINOP_LOGICAL_OR:
1339 v = v1 < v2 ? v1 : v2;
1343 v = v1 > v2 ? v1 : v2;
1350 case BINOP_NOTEQUAL:
1371 error (_("Invalid binary operation on numbers."));
1374 val = allocate_value (result_type);
1375 store_signed_integer (value_contents_raw (val),
1376 TYPE_LENGTH (value_type (val)),
1378 (get_type_arch (result_type)),
1386 /* Performs a binary operation on two vector operands by calling scalar_binop
1387 for each pair of vector components. */
1389 static struct value *
1390 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1392 struct value *val, *tmp, *mark;
1393 struct type *type1, *type2, *eltype1, *eltype2;
1394 int t1_is_vec, t2_is_vec, elsize, i;
1395 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1397 type1 = check_typedef (value_type (val1));
1398 type2 = check_typedef (value_type (val2));
1400 t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1401 && TYPE_VECTOR (type1)) ? 1 : 0;
1402 t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1403 && TYPE_VECTOR (type2)) ? 1 : 0;
1405 if (!t1_is_vec || !t2_is_vec)
1406 error (_("Vector operations are only supported among vectors"));
1408 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1409 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1410 error (_("Could not determine the vector bounds"));
1412 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1413 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1414 elsize = TYPE_LENGTH (eltype1);
1416 if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1417 || elsize != TYPE_LENGTH (eltype2)
1418 || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1419 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1420 error (_("Cannot perform operation on vectors with different types"));
1422 val = allocate_value (type1);
1423 mark = value_mark ();
1424 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1426 tmp = value_binop (value_subscript (val1, i),
1427 value_subscript (val2, i), op);
1428 memcpy (value_contents_writeable (val) + i * elsize,
1429 value_contents_all (tmp),
1432 value_free_to_mark (mark);
1437 /* Perform a binary operation on two operands. */
1440 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1443 struct type *type1 = check_typedef (value_type (arg1));
1444 struct type *type2 = check_typedef (value_type (arg2));
1445 int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1446 && TYPE_VECTOR (type1));
1447 int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1448 && TYPE_VECTOR (type2));
1450 if (!t1_is_vec && !t2_is_vec)
1451 val = scalar_binop (arg1, arg2, op);
1452 else if (t1_is_vec && t2_is_vec)
1453 val = vector_binop (arg1, arg2, op);
1456 /* Widen the scalar operand to a vector. */
1457 struct value **v = t1_is_vec ? &arg2 : &arg1;
1458 struct type *t = t1_is_vec ? type2 : type1;
1460 if (TYPE_CODE (t) != TYPE_CODE_FLT
1461 && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1462 && !is_integral_type (t))
1463 error (_("Argument to operation not a number or boolean."));
1465 *v = value_cast (t1_is_vec ? type1 : type2, *v);
1466 val = vector_binop (arg1, arg2, op);
1472 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1475 value_logical_not (struct value *arg1)
1481 arg1 = coerce_array (arg1);
1482 type1 = check_typedef (value_type (arg1));
1484 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1485 return 0 == value_as_double (arg1);
1486 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1487 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1488 gdbarch_byte_order (get_type_arch (type1)));
1490 len = TYPE_LENGTH (type1);
1491 p = value_contents (arg1);
1502 /* Perform a comparison on two string values (whose content are not
1503 necessarily null terminated) based on their length. */
1506 value_strcmp (struct value *arg1, struct value *arg2)
1508 int len1 = TYPE_LENGTH (value_type (arg1));
1509 int len2 = TYPE_LENGTH (value_type (arg2));
1510 const gdb_byte *s1 = value_contents (arg1);
1511 const gdb_byte *s2 = value_contents (arg2);
1512 int i, len = len1 < len2 ? len1 : len2;
1514 for (i = 0; i < len; i++)
1518 else if (s1[i] > s2[i])
1526 else if (len1 > len2)
1532 /* Simulate the C operator == by returning a 1
1533 iff ARG1 and ARG2 have equal contents. */
1536 value_equal (struct value *arg1, struct value *arg2)
1541 struct type *type1, *type2;
1542 enum type_code code1;
1543 enum type_code code2;
1544 int is_int1, is_int2;
1546 arg1 = coerce_array (arg1);
1547 arg2 = coerce_array (arg2);
1549 type1 = check_typedef (value_type (arg1));
1550 type2 = check_typedef (value_type (arg2));
1551 code1 = TYPE_CODE (type1);
1552 code2 = TYPE_CODE (type2);
1553 is_int1 = is_integral_type (type1);
1554 is_int2 = is_integral_type (type2);
1556 if (is_int1 && is_int2)
1557 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1559 else if ((code1 == TYPE_CODE_FLT || is_int1)
1560 && (code2 == TYPE_CODE_FLT || is_int2))
1562 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1563 `long double' values are returned in static storage (m68k). */
1564 DOUBLEST d = value_as_double (arg1);
1566 return d == value_as_double (arg2);
1568 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1569 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1571 gdb_byte v1[16], v2[16];
1573 enum bfd_endian byte_order_v1, byte_order_v2;
1575 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1576 v2, &len_v2, &byte_order_v2);
1578 return decimal_compare (v1, len_v1, byte_order_v1,
1579 v2, len_v2, byte_order_v2) == 0;
1582 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1584 else if (code1 == TYPE_CODE_PTR && is_int2)
1585 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1586 else if (code2 == TYPE_CODE_PTR && is_int1)
1587 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1589 else if (code1 == code2
1590 && ((len = (int) TYPE_LENGTH (type1))
1591 == (int) TYPE_LENGTH (type2)))
1593 p1 = value_contents (arg1);
1594 p2 = value_contents (arg2);
1602 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1604 return value_strcmp (arg1, arg2) == 0;
1608 error (_("Invalid type combination in equality test."));
1609 return 0; /* For lint -- never reached. */
1613 /* Compare values based on their raw contents. Useful for arrays since
1614 value_equal coerces them to pointers, thus comparing just the address
1615 of the array instead of its contents. */
1618 value_equal_contents (struct value *arg1, struct value *arg2)
1620 struct type *type1, *type2;
1622 type1 = check_typedef (value_type (arg1));
1623 type2 = check_typedef (value_type (arg2));
1625 return (TYPE_CODE (type1) == TYPE_CODE (type2)
1626 && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1627 && memcmp (value_contents (arg1), value_contents (arg2),
1628 TYPE_LENGTH (type1)) == 0);
1631 /* Simulate the C operator < by returning 1
1632 iff ARG1's contents are less than ARG2's. */
1635 value_less (struct value *arg1, struct value *arg2)
1637 enum type_code code1;
1638 enum type_code code2;
1639 struct type *type1, *type2;
1640 int is_int1, is_int2;
1642 arg1 = coerce_array (arg1);
1643 arg2 = coerce_array (arg2);
1645 type1 = check_typedef (value_type (arg1));
1646 type2 = check_typedef (value_type (arg2));
1647 code1 = TYPE_CODE (type1);
1648 code2 = TYPE_CODE (type2);
1649 is_int1 = is_integral_type (type1);
1650 is_int2 = is_integral_type (type2);
1652 if (is_int1 && is_int2)
1653 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1655 else if ((code1 == TYPE_CODE_FLT || is_int1)
1656 && (code2 == TYPE_CODE_FLT || is_int2))
1658 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1659 `long double' values are returned in static storage (m68k). */
1660 DOUBLEST d = value_as_double (arg1);
1662 return d < value_as_double (arg2);
1664 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1665 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1667 gdb_byte v1[16], v2[16];
1669 enum bfd_endian byte_order_v1, byte_order_v2;
1671 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1672 v2, &len_v2, &byte_order_v2);
1674 return decimal_compare (v1, len_v1, byte_order_v1,
1675 v2, len_v2, byte_order_v2) == -1;
1677 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1678 return value_as_address (arg1) < value_as_address (arg2);
1680 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1682 else if (code1 == TYPE_CODE_PTR && is_int2)
1683 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1684 else if (code2 == TYPE_CODE_PTR && is_int1)
1685 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1686 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1687 return value_strcmp (arg1, arg2) < 0;
1690 error (_("Invalid type combination in ordering comparison."));
1695 /* The unary operators +, - and ~. They free the argument ARG1. */
1698 value_pos (struct value *arg1)
1702 arg1 = coerce_ref (arg1);
1703 type = check_typedef (value_type (arg1));
1705 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1706 return value_from_double (type, value_as_double (arg1));
1707 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1708 return value_from_decfloat (type, value_contents (arg1));
1709 else if (is_integral_type (type))
1711 return value_from_longest (type, value_as_long (arg1));
1713 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1715 struct value *val = allocate_value (type);
1717 memcpy (value_contents_raw (val), value_contents (arg1),
1718 TYPE_LENGTH (type));
1723 error (_("Argument to positive operation not a number."));
1724 return 0; /* For lint -- never reached. */
1729 value_neg (struct value *arg1)
1733 arg1 = coerce_ref (arg1);
1734 type = check_typedef (value_type (arg1));
1736 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1738 struct value *val = allocate_value (type);
1739 int len = TYPE_LENGTH (type);
1740 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */
1742 memcpy (decbytes, value_contents (arg1), len);
1744 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
1745 decbytes[len-1] = decbytes[len - 1] | 0x80;
1747 decbytes[0] = decbytes[0] | 0x80;
1749 memcpy (value_contents_raw (val), decbytes, len);
1752 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1753 return value_from_double (type, -value_as_double (arg1));
1754 else if (is_integral_type (type))
1756 return value_from_longest (type, -value_as_long (arg1));
1758 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1760 struct value *tmp, *val = allocate_value (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 for (i = 0; i < high_bound - low_bound + 1; i++)
1770 tmp = value_neg (value_subscript (arg1, i));
1771 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1772 value_contents_all (tmp), TYPE_LENGTH (eltype));
1778 error (_("Argument to negate operation not a number."));
1779 return 0; /* For lint -- never reached. */
1784 value_complement (struct value *arg1)
1789 arg1 = coerce_ref (arg1);
1790 type = check_typedef (value_type (arg1));
1792 if (is_integral_type (type))
1793 val = value_from_longest (type, ~value_as_long (arg1));
1794 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1797 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1799 LONGEST low_bound, high_bound;
1801 if (!get_array_bounds (type, &low_bound, &high_bound))
1802 error (_("Could not determine the vector bounds"));
1804 val = allocate_value (type);
1805 for (i = 0; i < high_bound - low_bound + 1; i++)
1807 tmp = value_complement (value_subscript (arg1, i));
1808 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1809 value_contents_all (tmp), TYPE_LENGTH (eltype));
1813 error (_("Argument to complement operation not an integer, boolean."));
1818 /* The INDEX'th bit of SET value whose value_type is TYPE,
1819 and whose value_contents is valaddr.
1820 Return -1 if out of range, -2 other error. */
1823 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1825 struct gdbarch *gdbarch = get_type_arch (type);
1826 LONGEST low_bound, high_bound;
1829 struct type *range = TYPE_INDEX_TYPE (type);
1831 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1833 if (index < low_bound || index > high_bound)
1835 rel_index = index - low_bound;
1836 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1837 gdbarch_byte_order (gdbarch));
1838 rel_index %= TARGET_CHAR_BIT;
1839 if (gdbarch_bits_big_endian (gdbarch))
1840 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1841 return (word >> rel_index) & 1;
1845 value_in (struct value *element, struct value *set)
1848 struct type *settype = check_typedef (value_type (set));
1849 struct type *eltype = check_typedef (value_type (element));
1851 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1852 eltype = TYPE_TARGET_TYPE (eltype);
1853 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1854 error (_("Second argument of 'IN' has wrong type"));
1855 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1856 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1857 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1858 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1859 error (_("First argument of 'IN' has wrong type"));
1860 member = value_bit_index (settype, value_contents (set),
1861 value_as_long (element));
1863 error (_("First argument of 'IN' not in range"));
1868 _initialize_valarith (void)