1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
32 /* Define whether or not the C operator '/' truncates towards zero for
33 differently signed operands (truncation direction is undefined in C). */
35 #ifndef TRUNCATION_TOWARDS_ZERO
36 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
39 void _initialize_valarith (void);
42 /* Given a pointer, return the size of its target.
43 If the pointer type is void *, then return 1.
44 If the target type is incomplete, then error out.
45 This isn't a general purpose function, but just a
46 helper for value_ptradd. */
49 find_size_for_pointer_math (struct type *ptr_type)
52 struct type *ptr_target;
54 gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
55 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
57 sz = type_length_units (ptr_target);
60 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
66 name = TYPE_NAME (ptr_target);
68 name = TYPE_TAG_NAME (ptr_target);
70 error (_("Cannot perform pointer math on incomplete types, "
71 "try casting to a known type, or void *."));
73 error (_("Cannot perform pointer math on incomplete type \"%s\", "
74 "try casting to a known type, or void *."), name);
80 /* Given a pointer ARG1 and an integral value ARG2, return the
81 result of C-style pointer arithmetic ARG1 + ARG2. */
84 value_ptradd (struct value *arg1, LONGEST arg2)
86 struct type *valptrtype;
90 arg1 = coerce_array (arg1);
91 valptrtype = check_typedef (value_type (arg1));
92 sz = find_size_for_pointer_math (valptrtype);
94 result = value_from_pointer (valptrtype,
95 value_as_address (arg1) + sz * arg2);
96 if (VALUE_LVAL (result) != lval_internalvar)
97 set_value_component_location (result, arg1);
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))))
120 error (_("First argument of `-' is a pointer and "
121 "second argument is neither\n"
122 "an integer nor a pointer of the same type."));
124 sz = type_length_units (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.
140 See comments in value_coerce_array() for rationale for reason for
141 doing lower bounds adjustment here rather than there.
142 FIXME: Perhaps we should validate that the index is valid and if
143 verbosity is set, warn about invalid indices (but still use them). */
146 value_subscript (struct value *array, LONGEST index)
148 int c_style = current_language->c_style_arrays;
151 array = coerce_ref (array);
152 tarray = check_typedef (value_type (array));
154 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
155 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
157 struct type *range_type = TYPE_INDEX_TYPE (tarray);
158 LONGEST lowerbound, upperbound;
160 get_discrete_bounds (range_type, &lowerbound, &upperbound);
161 if (VALUE_LVAL (array) != lval_memory)
162 return value_subscripted_rvalue (array, index, lowerbound);
166 if (index >= lowerbound && index <= upperbound)
167 return value_subscripted_rvalue (array, index, lowerbound);
168 /* Emit warning unless we have an array of unknown size.
169 An array of unknown size has lowerbound 0 and upperbound -1. */
171 warning (_("array or string index out of range"));
172 /* fall doing C stuff */
177 array = value_coerce_array (array);
181 return value_ind (value_ptradd (array, index));
183 error (_("not an array or string"));
186 /* Return the value of EXPR[IDX], expr an aggregate rvalue
187 (eg, a vector register). This routine used to promote floats
188 to doubles, but no longer does. */
191 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
193 struct type *array_type = check_typedef (value_type (array));
194 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
195 unsigned int elt_size = type_length_units (elt_type);
196 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
199 if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
200 && elt_offs >= type_length_units (array_type)))
202 if (type_not_associated (array_type))
203 error (_("no such vector element (vector not associated)"));
204 else if (type_not_allocated (array_type))
205 error (_("no such vector element (vector not allocated)"));
207 error (_("no such vector element"));
210 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
211 v = allocate_value_lazy (elt_type);
214 v = allocate_value (elt_type);
215 value_contents_copy (v, value_embedded_offset (v),
216 array, value_embedded_offset (array) + elt_offs,
220 set_value_component_location (v, array);
221 VALUE_REGNUM (v) = VALUE_REGNUM (array);
222 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
223 set_value_offset (v, value_offset (array) + elt_offs);
228 /* Check to see if either argument is a structure, or a reference to
229 one. This is called so we know whether to go ahead with the normal
230 binop or look for a user defined function instead.
232 For now, we do not overload the `=' operator. */
235 binop_types_user_defined_p (enum exp_opcode op,
236 struct type *type1, struct type *type2)
238 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
241 type1 = check_typedef (type1);
242 if (TYPE_CODE (type1) == TYPE_CODE_REF)
243 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
245 type2 = check_typedef (type2);
246 if (TYPE_CODE (type2) == TYPE_CODE_REF)
247 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
249 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
250 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
253 /* Check to see if either argument is a structure, or a reference to
254 one. This is called so we know whether to go ahead with the normal
255 binop or look for a user defined function instead.
257 For now, we do not overload the `=' operator. */
260 binop_user_defined_p (enum exp_opcode op,
261 struct value *arg1, struct value *arg2)
263 return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
266 /* Check to see if argument is a structure. This is called so
267 we know whether to go ahead with the normal unop or look for a
268 user defined function instead.
270 For now, we do not overload the `&' operator. */
273 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
279 type1 = check_typedef (value_type (arg1));
280 if (TYPE_CODE (type1) == TYPE_CODE_REF)
281 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
282 return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
285 /* Try to find an operator named OPERATOR which takes NARGS arguments
286 specified in ARGS. If the operator found is a static member operator
287 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
288 The search if performed through find_overload_match which will handle
289 member operators, non member operators, operators imported implicitly or
290 explicitly, and perform correct overload resolution in all of the above
291 situations or combinations thereof. */
293 static struct value *
294 value_user_defined_cpp_op (struct value **args, int nargs, char *oper,
295 int *static_memfuncp, enum noside noside)
298 struct symbol *symp = NULL;
299 struct value *valp = NULL;
301 find_overload_match (args, nargs, oper, BOTH /* could be method */,
303 NULL /* pass NULL symbol since symbol is unknown */,
304 &valp, &symp, static_memfuncp, 0, noside);
311 /* This is a non member function and does not
312 expect a reference as its first argument
313 rather the explicit structure. */
314 args[0] = value_ind (args[0]);
315 return value_of_variable (symp, 0);
318 error (_("Could not find %s."), oper);
321 /* Lookup user defined operator NAME. Return a value representing the
322 function, otherwise return NULL. */
324 static struct value *
325 value_user_defined_op (struct value **argp, struct value **args, char *name,
326 int *static_memfuncp, int nargs, enum noside noside)
328 struct value *result = NULL;
330 if (current_language->la_language == language_cplus)
332 result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp,
336 result = value_struct_elt (argp, args, name, static_memfuncp,
342 /* We know either arg1 or arg2 is a structure, so try to find the right
343 user defined function. Create an argument vector that calls
344 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
345 binary operator which is legal for GNU C++).
347 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
348 is the opcode saying how to modify it. Otherwise, OTHEROP is
352 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
353 enum exp_opcode otherop, enum noside noside)
355 struct value **argvec;
360 arg1 = coerce_ref (arg1);
361 arg2 = coerce_ref (arg2);
363 /* now we know that what we have to do is construct our
364 arg vector and find the right function to call it with. */
366 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
367 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
369 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
370 argvec[1] = value_addr (arg1);
374 /* Make the right function name up. */
375 strcpy (tstr, "operator__");
400 case BINOP_BITWISE_AND:
403 case BINOP_BITWISE_IOR:
406 case BINOP_BITWISE_XOR:
409 case BINOP_LOGICAL_AND:
412 case BINOP_LOGICAL_OR:
424 case BINOP_ASSIGN_MODIFY:
442 case BINOP_BITWISE_AND:
445 case BINOP_BITWISE_IOR:
448 case BINOP_BITWISE_XOR:
451 case BINOP_MOD: /* invalid */
453 error (_("Invalid binary operation specified."));
456 case BINOP_SUBSCRIPT:
477 case BINOP_MOD: /* invalid */
479 error (_("Invalid binary operation specified."));
482 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
483 &static_memfuncp, 2, noside);
489 argvec[1] = argvec[0];
492 if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
494 /* Static xmethods are not supported yet. */
495 gdb_assert (static_memfuncp == 0);
496 if (noside == EVAL_AVOID_SIDE_EFFECTS)
498 struct type *return_type
499 = result_type_of_xmethod (argvec[0], 2, argvec + 1);
501 if (return_type == NULL)
502 error (_("Xmethod is missing return type."));
503 return value_zero (return_type, VALUE_LVAL (arg1));
505 return call_xmethod (argvec[0], 2, argvec + 1);
507 if (noside == EVAL_AVOID_SIDE_EFFECTS)
509 struct type *return_type;
512 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
513 return value_zero (return_type, VALUE_LVAL (arg1));
515 return call_function_by_hand (argvec[0], 2 - static_memfuncp,
518 throw_error (NOT_FOUND_ERROR,
519 _("member function %s not found"), tstr);
521 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
525 /* We know that arg1 is a structure, so try to find a unary user
526 defined operator that matches the operator in question.
527 Create an argument vector that calls arg1.operator @ (arg1)
528 and return that value (where '@' is (almost) any unary operator which
529 is legal for GNU C++). */
532 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
534 struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
535 struct value **argvec;
537 char tstr[13], mangle_tstr[13];
538 int static_memfuncp, nargs;
540 arg1 = coerce_ref (arg1);
542 /* now we know that what we have to do is construct our
543 arg vector and find the right function to call it with. */
545 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
546 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
548 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
549 argvec[1] = value_addr (arg1);
554 /* Make the right function name up. */
555 strcpy (tstr, "operator__");
557 strcpy (mangle_tstr, "__");
560 case UNOP_PREINCREMENT:
563 case UNOP_PREDECREMENT:
566 case UNOP_POSTINCREMENT:
568 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
572 case UNOP_POSTDECREMENT:
574 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
578 case UNOP_LOGICAL_NOT:
581 case UNOP_COMPLEMENT:
597 error (_("Invalid unary operation specified."));
600 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
601 &static_memfuncp, nargs, noside);
607 argvec[1] = argvec[0];
611 if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
613 /* Static xmethods are not supported yet. */
614 gdb_assert (static_memfuncp == 0);
615 if (noside == EVAL_AVOID_SIDE_EFFECTS)
617 struct type *return_type
618 = result_type_of_xmethod (argvec[0], 1, argvec + 1);
620 if (return_type == NULL)
621 error (_("Xmethod is missing return type."));
622 return value_zero (return_type, VALUE_LVAL (arg1));
624 return call_xmethod (argvec[0], 1, argvec + 1);
626 if (noside == EVAL_AVOID_SIDE_EFFECTS)
628 struct type *return_type;
631 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
632 return value_zero (return_type, VALUE_LVAL (arg1));
634 return call_function_by_hand (argvec[0], nargs, argvec + 1);
636 throw_error (NOT_FOUND_ERROR,
637 _("member function %s not found"), tstr);
639 return 0; /* For lint -- never reached */
643 /* Concatenate two values with the following conditions:
645 (1) Both values must be either bitstring values or character string
646 values and the resulting value consists of the concatenation of
647 ARG1 followed by ARG2.
651 One value must be an integer value and the other value must be
652 either a bitstring value or character string value, which is
653 to be repeated by the number of times specified by the integer
657 (2) Boolean values are also allowed and are treated as bit string
660 (3) Character values are also allowed and are treated as character
661 string values of length 1. */
664 value_concat (struct value *arg1, struct value *arg2)
666 struct value *inval1;
667 struct value *inval2;
668 struct value *outval = NULL;
669 int inval1len, inval2len;
673 struct type *type1 = check_typedef (value_type (arg1));
674 struct type *type2 = check_typedef (value_type (arg2));
675 struct type *char_type;
677 /* First figure out if we are dealing with two values to be concatenated
678 or a repeat count and a value to be repeated. INVAL1 is set to the
679 first of two concatenated values, or the repeat count. INVAL2 is set
680 to the second of the two concatenated values or the value to be
683 if (TYPE_CODE (type2) == TYPE_CODE_INT)
685 struct type *tmp = type1;
698 /* Now process the input values. */
700 if (TYPE_CODE (type1) == TYPE_CODE_INT)
702 /* We have a repeat count. Validate the second value and then
703 construct a value repeated that many times. */
704 if (TYPE_CODE (type2) == TYPE_CODE_STRING
705 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
707 struct cleanup *back_to;
709 count = longest_to_int (value_as_long (inval1));
710 inval2len = TYPE_LENGTH (type2);
711 ptr = (char *) xmalloc (count * inval2len);
712 back_to = make_cleanup (xfree, ptr);
713 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
717 inchar = (char) unpack_long (type2,
718 value_contents (inval2));
719 for (idx = 0; idx < count; idx++)
721 *(ptr + idx) = inchar;
726 char_type = TYPE_TARGET_TYPE (type2);
728 for (idx = 0; idx < count; idx++)
730 memcpy (ptr + (idx * inval2len), value_contents (inval2),
734 outval = value_string (ptr, count * inval2len, char_type);
735 do_cleanups (back_to);
737 else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
739 error (_("unimplemented support for boolean repeats"));
743 error (_("can't repeat values of that type"));
746 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
747 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
749 struct cleanup *back_to;
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 *) xmalloc (inval1len + inval2len);
760 back_to = make_cleanup (xfree, ptr);
761 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
765 *ptr = (char) unpack_long (type1, value_contents (inval1));
769 char_type = TYPE_TARGET_TYPE (type1);
771 memcpy (ptr, value_contents (inval1), inval1len);
773 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
776 (char) unpack_long (type2, value_contents (inval2));
780 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
782 outval = value_string (ptr, inval1len + inval2len, char_type);
783 do_cleanups (back_to);
785 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
787 /* We have two bitstrings to concatenate. */
788 if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
790 error (_("Booleans can only be concatenated "
791 "with other bitstrings or booleans."));
793 error (_("unimplemented support for 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 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1387 replicating SCALAR_VALUE for each element of the vector. Only scalar
1388 types that can be cast to the type of one element of the vector are
1389 acceptable. The newly created vector value is returned upon success,
1390 otherwise an error is thrown. */
1393 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1395 /* Widen the scalar to a vector. */
1396 struct type *eltype, *scalar_type;
1397 struct value *val, *elval;
1398 LONGEST low_bound, high_bound;
1401 vector_type = check_typedef (vector_type);
1403 gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
1404 && TYPE_VECTOR (vector_type));
1406 if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1407 error (_("Could not determine the vector bounds"));
1409 eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
1410 elval = value_cast (eltype, scalar_value);
1412 scalar_type = check_typedef (value_type (scalar_value));
1414 /* If we reduced the length of the scalar then check we didn't loose any
1416 if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
1417 && !value_equal (elval, scalar_value))
1418 error (_("conversion of scalar to vector involves truncation"));
1420 val = allocate_value (vector_type);
1421 for (i = 0; i < high_bound - low_bound + 1; i++)
1422 /* Duplicate the contents of elval into the destination vector. */
1423 memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
1424 value_contents_all (elval), TYPE_LENGTH (eltype));
1429 /* Performs a binary operation on two vector operands by calling scalar_binop
1430 for each pair of vector components. */
1432 static struct value *
1433 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1435 struct value *val, *tmp, *mark;
1436 struct type *type1, *type2, *eltype1, *eltype2;
1437 int t1_is_vec, t2_is_vec, elsize, i;
1438 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1440 type1 = check_typedef (value_type (val1));
1441 type2 = check_typedef (value_type (val2));
1443 t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1444 && TYPE_VECTOR (type1)) ? 1 : 0;
1445 t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1446 && TYPE_VECTOR (type2)) ? 1 : 0;
1448 if (!t1_is_vec || !t2_is_vec)
1449 error (_("Vector operations are only supported among vectors"));
1451 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1452 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1453 error (_("Could not determine the vector bounds"));
1455 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1456 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1457 elsize = TYPE_LENGTH (eltype1);
1459 if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1460 || elsize != TYPE_LENGTH (eltype2)
1461 || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1462 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1463 error (_("Cannot perform operation on vectors with different types"));
1465 val = allocate_value (type1);
1466 mark = value_mark ();
1467 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1469 tmp = value_binop (value_subscript (val1, i),
1470 value_subscript (val2, i), op);
1471 memcpy (value_contents_writeable (val) + i * elsize,
1472 value_contents_all (tmp),
1475 value_free_to_mark (mark);
1480 /* Perform a binary operation on two operands. */
1483 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1486 struct type *type1 = check_typedef (value_type (arg1));
1487 struct type *type2 = check_typedef (value_type (arg2));
1488 int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1489 && TYPE_VECTOR (type1));
1490 int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1491 && TYPE_VECTOR (type2));
1493 if (!t1_is_vec && !t2_is_vec)
1494 val = scalar_binop (arg1, arg2, op);
1495 else if (t1_is_vec && t2_is_vec)
1496 val = vector_binop (arg1, arg2, op);
1499 /* Widen the scalar operand to a vector. */
1500 struct value **v = t1_is_vec ? &arg2 : &arg1;
1501 struct type *t = t1_is_vec ? type2 : type1;
1503 if (TYPE_CODE (t) != TYPE_CODE_FLT
1504 && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1505 && !is_integral_type (t))
1506 error (_("Argument to operation not a number or boolean."));
1508 /* Replicate the scalar value to make a vector value. */
1509 *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1511 val = vector_binop (arg1, arg2, op);
1517 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1520 value_logical_not (struct value *arg1)
1526 arg1 = coerce_array (arg1);
1527 type1 = check_typedef (value_type (arg1));
1529 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1530 return 0 == value_as_double (arg1);
1531 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1532 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1533 gdbarch_byte_order (get_type_arch (type1)));
1535 len = TYPE_LENGTH (type1);
1536 p = value_contents (arg1);
1547 /* Perform a comparison on two string values (whose content are not
1548 necessarily null terminated) based on their length. */
1551 value_strcmp (struct value *arg1, struct value *arg2)
1553 int len1 = TYPE_LENGTH (value_type (arg1));
1554 int len2 = TYPE_LENGTH (value_type (arg2));
1555 const gdb_byte *s1 = value_contents (arg1);
1556 const gdb_byte *s2 = value_contents (arg2);
1557 int i, len = len1 < len2 ? len1 : len2;
1559 for (i = 0; i < len; i++)
1563 else if (s1[i] > s2[i])
1571 else if (len1 > len2)
1577 /* Simulate the C operator == by returning a 1
1578 iff ARG1 and ARG2 have equal contents. */
1581 value_equal (struct value *arg1, struct value *arg2)
1586 struct type *type1, *type2;
1587 enum type_code code1;
1588 enum type_code code2;
1589 int is_int1, is_int2;
1591 arg1 = coerce_array (arg1);
1592 arg2 = coerce_array (arg2);
1594 type1 = check_typedef (value_type (arg1));
1595 type2 = check_typedef (value_type (arg2));
1596 code1 = TYPE_CODE (type1);
1597 code2 = TYPE_CODE (type2);
1598 is_int1 = is_integral_type (type1);
1599 is_int2 = is_integral_type (type2);
1601 if (is_int1 && is_int2)
1602 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1604 else if ((code1 == TYPE_CODE_FLT || is_int1)
1605 && (code2 == TYPE_CODE_FLT || is_int2))
1607 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1608 `long double' values are returned in static storage (m68k). */
1609 DOUBLEST d = value_as_double (arg1);
1611 return d == value_as_double (arg2);
1613 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1614 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1616 gdb_byte v1[16], v2[16];
1618 enum bfd_endian byte_order_v1, byte_order_v2;
1620 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1621 v2, &len_v2, &byte_order_v2);
1623 return decimal_compare (v1, len_v1, byte_order_v1,
1624 v2, len_v2, byte_order_v2) == 0;
1627 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1629 else if (code1 == TYPE_CODE_PTR && is_int2)
1630 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1631 else if (code2 == TYPE_CODE_PTR && is_int1)
1632 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1634 else if (code1 == code2
1635 && ((len = (int) TYPE_LENGTH (type1))
1636 == (int) TYPE_LENGTH (type2)))
1638 p1 = value_contents (arg1);
1639 p2 = value_contents (arg2);
1647 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1649 return value_strcmp (arg1, arg2) == 0;
1653 error (_("Invalid type combination in equality test."));
1654 return 0; /* For lint -- never reached. */
1658 /* Compare values based on their raw contents. Useful for arrays since
1659 value_equal coerces them to pointers, thus comparing just the address
1660 of the array instead of its contents. */
1663 value_equal_contents (struct value *arg1, struct value *arg2)
1665 struct type *type1, *type2;
1667 type1 = check_typedef (value_type (arg1));
1668 type2 = check_typedef (value_type (arg2));
1670 return (TYPE_CODE (type1) == TYPE_CODE (type2)
1671 && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1672 && memcmp (value_contents (arg1), value_contents (arg2),
1673 TYPE_LENGTH (type1)) == 0);
1676 /* Simulate the C operator < by returning 1
1677 iff ARG1's contents are less than ARG2's. */
1680 value_less (struct value *arg1, struct value *arg2)
1682 enum type_code code1;
1683 enum type_code code2;
1684 struct type *type1, *type2;
1685 int is_int1, is_int2;
1687 arg1 = coerce_array (arg1);
1688 arg2 = coerce_array (arg2);
1690 type1 = check_typedef (value_type (arg1));
1691 type2 = check_typedef (value_type (arg2));
1692 code1 = TYPE_CODE (type1);
1693 code2 = TYPE_CODE (type2);
1694 is_int1 = is_integral_type (type1);
1695 is_int2 = is_integral_type (type2);
1697 if (is_int1 && is_int2)
1698 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1700 else if ((code1 == TYPE_CODE_FLT || is_int1)
1701 && (code2 == TYPE_CODE_FLT || is_int2))
1703 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1704 `long double' values are returned in static storage (m68k). */
1705 DOUBLEST d = value_as_double (arg1);
1707 return d < value_as_double (arg2);
1709 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1710 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1712 gdb_byte v1[16], v2[16];
1714 enum bfd_endian byte_order_v1, byte_order_v2;
1716 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1717 v2, &len_v2, &byte_order_v2);
1719 return decimal_compare (v1, len_v1, byte_order_v1,
1720 v2, len_v2, byte_order_v2) == -1;
1722 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1723 return value_as_address (arg1) < value_as_address (arg2);
1725 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1727 else if (code1 == TYPE_CODE_PTR && is_int2)
1728 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1729 else if (code2 == TYPE_CODE_PTR && is_int1)
1730 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1731 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1732 return value_strcmp (arg1, arg2) < 0;
1735 error (_("Invalid type combination in ordering comparison."));
1740 /* The unary operators +, - and ~. They free the argument ARG1. */
1743 value_pos (struct value *arg1)
1747 arg1 = coerce_ref (arg1);
1748 type = check_typedef (value_type (arg1));
1750 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1751 return value_from_double (type, value_as_double (arg1));
1752 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1753 return value_from_decfloat (type, value_contents (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 *val = allocate_value (type);
1762 memcpy (value_contents_raw (val), value_contents (arg1),
1763 TYPE_LENGTH (type));
1768 error (_("Argument to positive operation not a number."));
1769 return 0; /* For lint -- never reached. */
1774 value_neg (struct value *arg1)
1778 arg1 = coerce_ref (arg1);
1779 type = check_typedef (value_type (arg1));
1781 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1783 struct value *val = allocate_value (type);
1784 int len = TYPE_LENGTH (type);
1785 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */
1787 memcpy (decbytes, value_contents (arg1), len);
1789 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
1790 decbytes[len-1] = decbytes[len - 1] | 0x80;
1792 decbytes[0] = decbytes[0] | 0x80;
1794 memcpy (value_contents_raw (val), decbytes, len);
1797 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1798 return value_from_double (type, -value_as_double (arg1));
1799 else if (is_integral_type (type))
1801 return value_from_longest (type, -value_as_long (arg1));
1803 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1805 struct value *tmp, *val = allocate_value (type);
1806 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1808 LONGEST low_bound, high_bound;
1810 if (!get_array_bounds (type, &low_bound, &high_bound))
1811 error (_("Could not determine the vector bounds"));
1813 for (i = 0; i < high_bound - low_bound + 1; i++)
1815 tmp = value_neg (value_subscript (arg1, i));
1816 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1817 value_contents_all (tmp), TYPE_LENGTH (eltype));
1823 error (_("Argument to negate operation not a number."));
1824 return 0; /* For lint -- never reached. */
1829 value_complement (struct value *arg1)
1834 arg1 = coerce_ref (arg1);
1835 type = check_typedef (value_type (arg1));
1837 if (is_integral_type (type))
1838 val = value_from_longest (type, ~value_as_long (arg1));
1839 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1842 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1844 LONGEST low_bound, high_bound;
1846 if (!get_array_bounds (type, &low_bound, &high_bound))
1847 error (_("Could not determine the vector bounds"));
1849 val = allocate_value (type);
1850 for (i = 0; i < high_bound - low_bound + 1; i++)
1852 tmp = value_complement (value_subscript (arg1, i));
1853 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1854 value_contents_all (tmp), TYPE_LENGTH (eltype));
1858 error (_("Argument to complement operation not an integer, boolean."));
1863 /* The INDEX'th bit of SET value whose value_type is TYPE,
1864 and whose value_contents is valaddr.
1865 Return -1 if out of range, -2 other error. */
1868 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1870 struct gdbarch *gdbarch = get_type_arch (type);
1871 LONGEST low_bound, high_bound;
1874 struct type *range = TYPE_INDEX_TYPE (type);
1876 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1878 if (index < low_bound || index > high_bound)
1880 rel_index = index - low_bound;
1881 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1882 gdbarch_byte_order (gdbarch));
1883 rel_index %= TARGET_CHAR_BIT;
1884 if (gdbarch_bits_big_endian (gdbarch))
1885 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1886 return (word >> rel_index) & 1;
1890 value_in (struct value *element, struct value *set)
1893 struct type *settype = check_typedef (value_type (set));
1894 struct type *eltype = check_typedef (value_type (element));
1896 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1897 eltype = TYPE_TARGET_TYPE (eltype);
1898 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1899 error (_("Second argument of 'IN' has wrong type"));
1900 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1901 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1902 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1903 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1904 error (_("First argument of 'IN' has wrong type"));
1905 member = value_bit_index (settype, value_contents (set),
1906 value_as_long (element));
1908 error (_("First argument of 'IN' not in range"));
1913 _initialize_valarith (void)