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 (is_dynamic_type (elt_type))
214 address = value_address (array) + elt_offs;
215 elt_type = resolve_dynamic_type (elt_type, NULL, address);
218 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
219 v = allocate_value_lazy (elt_type);
222 v = allocate_value (elt_type);
223 value_contents_copy (v, value_embedded_offset (v),
224 array, value_embedded_offset (array) + elt_offs,
228 set_value_component_location (v, array);
229 VALUE_REGNUM (v) = VALUE_REGNUM (array);
230 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
231 set_value_offset (v, value_offset (array) + elt_offs);
236 /* Check to see if either argument is a structure, or a reference to
237 one. This is called so we know whether to go ahead with the normal
238 binop or look for a user defined function instead.
240 For now, we do not overload the `=' operator. */
243 binop_types_user_defined_p (enum exp_opcode op,
244 struct type *type1, struct type *type2)
246 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
249 type1 = check_typedef (type1);
250 if (TYPE_CODE (type1) == TYPE_CODE_REF)
251 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
253 type2 = check_typedef (type2);
254 if (TYPE_CODE (type2) == TYPE_CODE_REF)
255 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
257 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
258 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
261 /* Check to see if either argument is a structure, or a reference to
262 one. This is called so we know whether to go ahead with the normal
263 binop or look for a user defined function instead.
265 For now, we do not overload the `=' operator. */
268 binop_user_defined_p (enum exp_opcode op,
269 struct value *arg1, struct value *arg2)
271 return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
274 /* Check to see if argument is a structure. This is called so
275 we know whether to go ahead with the normal unop or look for a
276 user defined function instead.
278 For now, we do not overload the `&' operator. */
281 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
287 type1 = check_typedef (value_type (arg1));
288 if (TYPE_CODE (type1) == TYPE_CODE_REF)
289 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
290 return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
293 /* Try to find an operator named OPERATOR which takes NARGS arguments
294 specified in ARGS. If the operator found is a static member operator
295 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
296 The search if performed through find_overload_match which will handle
297 member operators, non member operators, operators imported implicitly or
298 explicitly, and perform correct overload resolution in all of the above
299 situations or combinations thereof. */
301 static struct value *
302 value_user_defined_cpp_op (struct value **args, int nargs, char *oper,
303 int *static_memfuncp, enum noside noside)
306 struct symbol *symp = NULL;
307 struct value *valp = NULL;
309 find_overload_match (args, nargs, oper, BOTH /* could be method */,
311 NULL /* pass NULL symbol since symbol is unknown */,
312 &valp, &symp, static_memfuncp, 0, noside);
319 /* This is a non member function and does not
320 expect a reference as its first argument
321 rather the explicit structure. */
322 args[0] = value_ind (args[0]);
323 return value_of_variable (symp, 0);
326 error (_("Could not find %s."), oper);
329 /* Lookup user defined operator NAME. Return a value representing the
330 function, otherwise return NULL. */
332 static struct value *
333 value_user_defined_op (struct value **argp, struct value **args, char *name,
334 int *static_memfuncp, int nargs, enum noside noside)
336 struct value *result = NULL;
338 if (current_language->la_language == language_cplus)
340 result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp,
344 result = value_struct_elt (argp, args, name, static_memfuncp,
350 /* We know either arg1 or arg2 is a structure, so try to find the right
351 user defined function. Create an argument vector that calls
352 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
353 binary operator which is legal for GNU C++).
355 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
356 is the opcode saying how to modify it. Otherwise, OTHEROP is
360 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
361 enum exp_opcode otherop, enum noside noside)
363 struct value **argvec;
368 arg1 = coerce_ref (arg1);
369 arg2 = coerce_ref (arg2);
371 /* now we know that what we have to do is construct our
372 arg vector and find the right function to call it with. */
374 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
375 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
377 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
378 argvec[1] = value_addr (arg1);
382 /* Make the right function name up. */
383 strcpy (tstr, "operator__");
408 case BINOP_BITWISE_AND:
411 case BINOP_BITWISE_IOR:
414 case BINOP_BITWISE_XOR:
417 case BINOP_LOGICAL_AND:
420 case BINOP_LOGICAL_OR:
432 case BINOP_ASSIGN_MODIFY:
450 case BINOP_BITWISE_AND:
453 case BINOP_BITWISE_IOR:
456 case BINOP_BITWISE_XOR:
459 case BINOP_MOD: /* invalid */
461 error (_("Invalid binary operation specified."));
464 case BINOP_SUBSCRIPT:
485 case BINOP_MOD: /* invalid */
487 error (_("Invalid binary operation specified."));
490 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
491 &static_memfuncp, 2, noside);
497 argvec[1] = argvec[0];
500 if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
502 /* Static xmethods are not supported yet. */
503 gdb_assert (static_memfuncp == 0);
504 if (noside == EVAL_AVOID_SIDE_EFFECTS)
506 struct type *return_type
507 = result_type_of_xmethod (argvec[0], 2, argvec + 1);
509 if (return_type == NULL)
510 error (_("Xmethod is missing return type."));
511 return value_zero (return_type, VALUE_LVAL (arg1));
513 return call_xmethod (argvec[0], 2, argvec + 1);
515 if (noside == EVAL_AVOID_SIDE_EFFECTS)
517 struct type *return_type;
520 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
521 return value_zero (return_type, VALUE_LVAL (arg1));
523 return call_function_by_hand (argvec[0], 2 - static_memfuncp,
526 throw_error (NOT_FOUND_ERROR,
527 _("member function %s not found"), tstr);
529 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
533 /* We know that arg1 is a structure, so try to find a unary user
534 defined operator that matches the operator in question.
535 Create an argument vector that calls arg1.operator @ (arg1)
536 and return that value (where '@' is (almost) any unary operator which
537 is legal for GNU C++). */
540 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
542 struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
543 struct value **argvec;
545 char tstr[13], mangle_tstr[13];
546 int static_memfuncp, nargs;
548 arg1 = coerce_ref (arg1);
550 /* now we know that what we have to do is construct our
551 arg vector and find the right function to call it with. */
553 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
554 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
556 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
557 argvec[1] = value_addr (arg1);
562 /* Make the right function name up. */
563 strcpy (tstr, "operator__");
565 strcpy (mangle_tstr, "__");
568 case UNOP_PREINCREMENT:
571 case UNOP_PREDECREMENT:
574 case UNOP_POSTINCREMENT:
576 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
580 case UNOP_POSTDECREMENT:
582 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
586 case UNOP_LOGICAL_NOT:
589 case UNOP_COMPLEMENT:
605 error (_("Invalid unary operation specified."));
608 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
609 &static_memfuncp, nargs, noside);
615 argvec[1] = argvec[0];
619 if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
621 /* Static xmethods are not supported yet. */
622 gdb_assert (static_memfuncp == 0);
623 if (noside == EVAL_AVOID_SIDE_EFFECTS)
625 struct type *return_type
626 = result_type_of_xmethod (argvec[0], 1, argvec + 1);
628 if (return_type == NULL)
629 error (_("Xmethod is missing return type."));
630 return value_zero (return_type, VALUE_LVAL (arg1));
632 return call_xmethod (argvec[0], 1, argvec + 1);
634 if (noside == EVAL_AVOID_SIDE_EFFECTS)
636 struct type *return_type;
639 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
640 return value_zero (return_type, VALUE_LVAL (arg1));
642 return call_function_by_hand (argvec[0], nargs, argvec + 1);
644 throw_error (NOT_FOUND_ERROR,
645 _("member function %s not found"), tstr);
647 return 0; /* For lint -- never reached */
651 /* Concatenate two values with the following conditions:
653 (1) Both values must be either bitstring values or character string
654 values and the resulting value consists of the concatenation of
655 ARG1 followed by ARG2.
659 One value must be an integer value and the other value must be
660 either a bitstring value or character string value, which is
661 to be repeated by the number of times specified by the integer
665 (2) Boolean values are also allowed and are treated as bit string
668 (3) Character values are also allowed and are treated as character
669 string values of length 1. */
672 value_concat (struct value *arg1, struct value *arg2)
674 struct value *inval1;
675 struct value *inval2;
676 struct value *outval = NULL;
677 int inval1len, inval2len;
681 struct type *type1 = check_typedef (value_type (arg1));
682 struct type *type2 = check_typedef (value_type (arg2));
683 struct type *char_type;
685 /* First figure out if we are dealing with two values to be concatenated
686 or a repeat count and a value to be repeated. INVAL1 is set to the
687 first of two concatenated values, or the repeat count. INVAL2 is set
688 to the second of the two concatenated values or the value to be
691 if (TYPE_CODE (type2) == TYPE_CODE_INT)
693 struct type *tmp = type1;
706 /* Now process the input values. */
708 if (TYPE_CODE (type1) == TYPE_CODE_INT)
710 /* We have a repeat count. Validate the second value and then
711 construct a value repeated that many times. */
712 if (TYPE_CODE (type2) == TYPE_CODE_STRING
713 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
715 struct cleanup *back_to;
717 count = longest_to_int (value_as_long (inval1));
718 inval2len = TYPE_LENGTH (type2);
719 ptr = (char *) xmalloc (count * inval2len);
720 back_to = make_cleanup (xfree, ptr);
721 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
725 inchar = (char) unpack_long (type2,
726 value_contents (inval2));
727 for (idx = 0; idx < count; idx++)
729 *(ptr + idx) = inchar;
734 char_type = TYPE_TARGET_TYPE (type2);
736 for (idx = 0; idx < count; idx++)
738 memcpy (ptr + (idx * inval2len), value_contents (inval2),
742 outval = value_string (ptr, count * inval2len, char_type);
743 do_cleanups (back_to);
745 else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
747 error (_("unimplemented support for boolean repeats"));
751 error (_("can't repeat values of that type"));
754 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
755 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
757 struct cleanup *back_to;
759 /* We have two character strings to concatenate. */
760 if (TYPE_CODE (type2) != TYPE_CODE_STRING
761 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
763 error (_("Strings can only be concatenated with other strings."));
765 inval1len = TYPE_LENGTH (type1);
766 inval2len = TYPE_LENGTH (type2);
767 ptr = (char *) xmalloc (inval1len + inval2len);
768 back_to = make_cleanup (xfree, ptr);
769 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
773 *ptr = (char) unpack_long (type1, value_contents (inval1));
777 char_type = TYPE_TARGET_TYPE (type1);
779 memcpy (ptr, value_contents (inval1), inval1len);
781 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
784 (char) unpack_long (type2, value_contents (inval2));
788 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
790 outval = value_string (ptr, inval1len + inval2len, char_type);
791 do_cleanups (back_to);
793 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
795 /* We have two bitstrings to concatenate. */
796 if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
798 error (_("Booleans can only be concatenated "
799 "with other bitstrings or booleans."));
801 error (_("unimplemented support for boolean concatenation."));
805 /* We don't know how to concatenate these operands. */
806 error (_("illegal operands for concatenation."));
811 /* Integer exponentiation: V1**V2, where both arguments are
812 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
815 integer_pow (LONGEST v1, LONGEST v2)
820 error (_("Attempt to raise 0 to negative power."));
826 /* The Russian Peasant's Algorithm. */
842 /* Integer exponentiation: V1**V2, where both arguments are
843 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
846 uinteger_pow (ULONGEST v1, LONGEST v2)
851 error (_("Attempt to raise 0 to negative power."));
857 /* The Russian Peasant's Algorithm. */
873 /* Obtain decimal value of arguments for binary operation, converting from
874 other types if one of them is not decimal floating point. */
876 value_args_as_decimal (struct value *arg1, struct value *arg2,
877 gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
878 gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
880 struct type *type1, *type2;
882 type1 = check_typedef (value_type (arg1));
883 type2 = check_typedef (value_type (arg2));
885 /* At least one of the arguments must be of decimal float type. */
886 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
887 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
889 if (TYPE_CODE (type1) == TYPE_CODE_FLT
890 || TYPE_CODE (type2) == TYPE_CODE_FLT)
891 /* The DFP extension to the C language does not allow mixing of
892 * decimal float types with other float types in expressions
893 * (see WDTR 24732, page 12). */
894 error (_("Mixing decimal floating types with "
895 "other floating types is not allowed."));
897 /* Obtain decimal value of arg1, converting from other types
900 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
902 *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
903 *len_x = TYPE_LENGTH (type1);
904 memcpy (x, value_contents (arg1), *len_x);
906 else if (is_integral_type (type1))
908 *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
909 *len_x = TYPE_LENGTH (type2);
910 decimal_from_integral (arg1, x, *len_x, *byte_order_x);
913 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
916 /* Obtain decimal value of arg2, converting from other types
919 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
921 *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
922 *len_y = TYPE_LENGTH (type2);
923 memcpy (y, value_contents (arg2), *len_y);
925 else if (is_integral_type (type2))
927 *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
928 *len_y = TYPE_LENGTH (type1);
929 decimal_from_integral (arg2, y, *len_y, *byte_order_y);
932 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
936 /* Perform a binary operation on two operands which have reasonable
937 representations as integers or floats. This includes booleans,
938 characters, integers, or floats.
939 Does not support addition and subtraction on pointers;
940 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
942 static struct value *
943 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
946 struct type *type1, *type2, *result_type;
948 arg1 = coerce_ref (arg1);
949 arg2 = coerce_ref (arg2);
951 type1 = check_typedef (value_type (arg1));
952 type2 = check_typedef (value_type (arg2));
954 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
955 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
956 && !is_integral_type (type1))
957 || (TYPE_CODE (type2) != TYPE_CODE_FLT
958 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
959 && !is_integral_type (type2)))
960 error (_("Argument to arithmetic operation not a number or boolean."));
962 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
963 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
965 int len_v1, len_v2, len_v;
966 enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
967 gdb_byte v1[16], v2[16];
970 /* If only one type is decimal float, use its type.
971 Otherwise use the bigger type. */
972 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
974 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
976 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
981 len_v = TYPE_LENGTH (result_type);
982 byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
984 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
985 v2, &len_v2, &byte_order_v2);
994 decimal_binop (op, v1, len_v1, byte_order_v1,
995 v2, len_v2, byte_order_v2,
996 v, len_v, byte_order_v);
1000 error (_("Operation not valid for decimal floating point number."));
1003 val = value_from_decfloat (result_type, v);
1005 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
1006 || TYPE_CODE (type2) == TYPE_CODE_FLT)
1008 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1009 in target format. real.c in GCC probably has the necessary
1011 DOUBLEST v1, v2, v = 0;
1013 v1 = value_as_double (arg1);
1014 v2 = value_as_double (arg2);
1038 error (_("Cannot perform exponentiation: %s"),
1039 safe_strerror (errno));
1043 v = v1 < v2 ? v1 : v2;
1047 v = v1 > v2 ? v1 : v2;
1051 error (_("Integer-only operation on floating point number."));
1054 /* If only one type is float, use its type.
1055 Otherwise use the bigger type. */
1056 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
1057 result_type = type2;
1058 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
1059 result_type = type1;
1060 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1061 result_type = type2;
1063 result_type = type1;
1065 val = allocate_value (result_type);
1066 store_typed_floating (value_contents_raw (val), value_type (val), v);
1068 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
1069 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
1071 LONGEST v1, v2, v = 0;
1073 v1 = value_as_long (arg1);
1074 v2 = value_as_long (arg2);
1078 case BINOP_BITWISE_AND:
1082 case BINOP_BITWISE_IOR:
1086 case BINOP_BITWISE_XOR:
1094 case BINOP_NOTEQUAL:
1099 error (_("Invalid operation on booleans."));
1102 result_type = type1;
1104 val = allocate_value (result_type);
1105 store_signed_integer (value_contents_raw (val),
1106 TYPE_LENGTH (result_type),
1107 gdbarch_byte_order (get_type_arch (result_type)),
1111 /* Integral operations here. */
1113 /* Determine type length of the result, and if the operation should
1114 be done unsigned. For exponentiation and shift operators,
1115 use the length and type of the left operand. Otherwise,
1116 use the signedness of the operand with the greater length.
1117 If both operands are of equal length, use unsigned operation
1118 if one of the operands is unsigned. */
1119 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1120 result_type = type1;
1121 else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1122 result_type = type1;
1123 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1124 result_type = type2;
1125 else if (TYPE_UNSIGNED (type1))
1126 result_type = type1;
1127 else if (TYPE_UNSIGNED (type2))
1128 result_type = type2;
1130 result_type = type1;
1132 if (TYPE_UNSIGNED (result_type))
1134 LONGEST v2_signed = value_as_long (arg2);
1135 ULONGEST v1, v2, v = 0;
1137 v1 = (ULONGEST) value_as_long (arg1);
1138 v2 = (ULONGEST) v2_signed;
1159 error (_("Division by zero"));
1163 v = uinteger_pow (v1, v2_signed);
1170 error (_("Division by zero"));
1174 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1175 v1 mod 0 has a defined value, v1. */
1183 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1196 case BINOP_BITWISE_AND:
1200 case BINOP_BITWISE_IOR:
1204 case BINOP_BITWISE_XOR:
1208 case BINOP_LOGICAL_AND:
1212 case BINOP_LOGICAL_OR:
1217 v = v1 < v2 ? v1 : v2;
1221 v = v1 > v2 ? v1 : v2;
1228 case BINOP_NOTEQUAL:
1249 error (_("Invalid binary operation on numbers."));
1252 val = allocate_value (result_type);
1253 store_unsigned_integer (value_contents_raw (val),
1254 TYPE_LENGTH (value_type (val)),
1256 (get_type_arch (result_type)),
1261 LONGEST v1, v2, v = 0;
1263 v1 = value_as_long (arg1);
1264 v2 = value_as_long (arg2);
1285 error (_("Division by zero"));
1289 v = integer_pow (v1, v2);
1296 error (_("Division by zero"));
1300 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1301 X mod 0 has a defined value, X. */
1309 /* Compute floor. */
1310 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1326 case BINOP_BITWISE_AND:
1330 case BINOP_BITWISE_IOR:
1334 case BINOP_BITWISE_XOR:
1338 case BINOP_LOGICAL_AND:
1342 case BINOP_LOGICAL_OR:
1347 v = v1 < v2 ? v1 : v2;
1351 v = v1 > v2 ? v1 : v2;
1358 case BINOP_NOTEQUAL:
1379 error (_("Invalid binary operation on numbers."));
1382 val = allocate_value (result_type);
1383 store_signed_integer (value_contents_raw (val),
1384 TYPE_LENGTH (value_type (val)),
1386 (get_type_arch (result_type)),
1394 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1395 replicating SCALAR_VALUE for each element of the vector. Only scalar
1396 types that can be cast to the type of one element of the vector are
1397 acceptable. The newly created vector value is returned upon success,
1398 otherwise an error is thrown. */
1401 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1403 /* Widen the scalar to a vector. */
1404 struct type *eltype, *scalar_type;
1405 struct value *val, *elval;
1406 LONGEST low_bound, high_bound;
1409 vector_type = check_typedef (vector_type);
1411 gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
1412 && TYPE_VECTOR (vector_type));
1414 if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1415 error (_("Could not determine the vector bounds"));
1417 eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
1418 elval = value_cast (eltype, scalar_value);
1420 scalar_type = check_typedef (value_type (scalar_value));
1422 /* If we reduced the length of the scalar then check we didn't loose any
1424 if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
1425 && !value_equal (elval, scalar_value))
1426 error (_("conversion of scalar to vector involves truncation"));
1428 val = allocate_value (vector_type);
1429 for (i = 0; i < high_bound - low_bound + 1; i++)
1430 /* Duplicate the contents of elval into the destination vector. */
1431 memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
1432 value_contents_all (elval), TYPE_LENGTH (eltype));
1437 /* Performs a binary operation on two vector operands by calling scalar_binop
1438 for each pair of vector components. */
1440 static struct value *
1441 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1443 struct value *val, *tmp, *mark;
1444 struct type *type1, *type2, *eltype1, *eltype2;
1445 int t1_is_vec, t2_is_vec, elsize, i;
1446 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1448 type1 = check_typedef (value_type (val1));
1449 type2 = check_typedef (value_type (val2));
1451 t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1452 && TYPE_VECTOR (type1)) ? 1 : 0;
1453 t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1454 && TYPE_VECTOR (type2)) ? 1 : 0;
1456 if (!t1_is_vec || !t2_is_vec)
1457 error (_("Vector operations are only supported among vectors"));
1459 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1460 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1461 error (_("Could not determine the vector bounds"));
1463 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1464 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1465 elsize = TYPE_LENGTH (eltype1);
1467 if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1468 || elsize != TYPE_LENGTH (eltype2)
1469 || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1470 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1471 error (_("Cannot perform operation on vectors with different types"));
1473 val = allocate_value (type1);
1474 mark = value_mark ();
1475 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1477 tmp = value_binop (value_subscript (val1, i),
1478 value_subscript (val2, i), op);
1479 memcpy (value_contents_writeable (val) + i * elsize,
1480 value_contents_all (tmp),
1483 value_free_to_mark (mark);
1488 /* Perform a binary operation on two operands. */
1491 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1494 struct type *type1 = check_typedef (value_type (arg1));
1495 struct type *type2 = check_typedef (value_type (arg2));
1496 int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1497 && TYPE_VECTOR (type1));
1498 int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1499 && TYPE_VECTOR (type2));
1501 if (!t1_is_vec && !t2_is_vec)
1502 val = scalar_binop (arg1, arg2, op);
1503 else if (t1_is_vec && t2_is_vec)
1504 val = vector_binop (arg1, arg2, op);
1507 /* Widen the scalar operand to a vector. */
1508 struct value **v = t1_is_vec ? &arg2 : &arg1;
1509 struct type *t = t1_is_vec ? type2 : type1;
1511 if (TYPE_CODE (t) != TYPE_CODE_FLT
1512 && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1513 && !is_integral_type (t))
1514 error (_("Argument to operation not a number or boolean."));
1516 /* Replicate the scalar value to make a vector value. */
1517 *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1519 val = vector_binop (arg1, arg2, op);
1525 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1528 value_logical_not (struct value *arg1)
1534 arg1 = coerce_array (arg1);
1535 type1 = check_typedef (value_type (arg1));
1537 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1538 return 0 == value_as_double (arg1);
1539 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1540 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1541 gdbarch_byte_order (get_type_arch (type1)));
1543 len = TYPE_LENGTH (type1);
1544 p = value_contents (arg1);
1555 /* Perform a comparison on two string values (whose content are not
1556 necessarily null terminated) based on their length. */
1559 value_strcmp (struct value *arg1, struct value *arg2)
1561 int len1 = TYPE_LENGTH (value_type (arg1));
1562 int len2 = TYPE_LENGTH (value_type (arg2));
1563 const gdb_byte *s1 = value_contents (arg1);
1564 const gdb_byte *s2 = value_contents (arg2);
1565 int i, len = len1 < len2 ? len1 : len2;
1567 for (i = 0; i < len; i++)
1571 else if (s1[i] > s2[i])
1579 else if (len1 > len2)
1585 /* Simulate the C operator == by returning a 1
1586 iff ARG1 and ARG2 have equal contents. */
1589 value_equal (struct value *arg1, struct value *arg2)
1594 struct type *type1, *type2;
1595 enum type_code code1;
1596 enum type_code code2;
1597 int is_int1, is_int2;
1599 arg1 = coerce_array (arg1);
1600 arg2 = coerce_array (arg2);
1602 type1 = check_typedef (value_type (arg1));
1603 type2 = check_typedef (value_type (arg2));
1604 code1 = TYPE_CODE (type1);
1605 code2 = TYPE_CODE (type2);
1606 is_int1 = is_integral_type (type1);
1607 is_int2 = is_integral_type (type2);
1609 if (is_int1 && is_int2)
1610 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1612 else if ((code1 == TYPE_CODE_FLT || is_int1)
1613 && (code2 == TYPE_CODE_FLT || is_int2))
1615 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1616 `long double' values are returned in static storage (m68k). */
1617 DOUBLEST d = value_as_double (arg1);
1619 return d == value_as_double (arg2);
1621 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1622 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1624 gdb_byte v1[16], v2[16];
1626 enum bfd_endian byte_order_v1, byte_order_v2;
1628 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1629 v2, &len_v2, &byte_order_v2);
1631 return decimal_compare (v1, len_v1, byte_order_v1,
1632 v2, len_v2, byte_order_v2) == 0;
1635 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1637 else if (code1 == TYPE_CODE_PTR && is_int2)
1638 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1639 else if (code2 == TYPE_CODE_PTR && is_int1)
1640 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1642 else if (code1 == code2
1643 && ((len = (int) TYPE_LENGTH (type1))
1644 == (int) TYPE_LENGTH (type2)))
1646 p1 = value_contents (arg1);
1647 p2 = value_contents (arg2);
1655 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1657 return value_strcmp (arg1, arg2) == 0;
1661 error (_("Invalid type combination in equality test."));
1662 return 0; /* For lint -- never reached. */
1666 /* Compare values based on their raw contents. Useful for arrays since
1667 value_equal coerces them to pointers, thus comparing just the address
1668 of the array instead of its contents. */
1671 value_equal_contents (struct value *arg1, struct value *arg2)
1673 struct type *type1, *type2;
1675 type1 = check_typedef (value_type (arg1));
1676 type2 = check_typedef (value_type (arg2));
1678 return (TYPE_CODE (type1) == TYPE_CODE (type2)
1679 && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1680 && memcmp (value_contents (arg1), value_contents (arg2),
1681 TYPE_LENGTH (type1)) == 0);
1684 /* Simulate the C operator < by returning 1
1685 iff ARG1's contents are less than ARG2's. */
1688 value_less (struct value *arg1, struct value *arg2)
1690 enum type_code code1;
1691 enum type_code code2;
1692 struct type *type1, *type2;
1693 int is_int1, is_int2;
1695 arg1 = coerce_array (arg1);
1696 arg2 = coerce_array (arg2);
1698 type1 = check_typedef (value_type (arg1));
1699 type2 = check_typedef (value_type (arg2));
1700 code1 = TYPE_CODE (type1);
1701 code2 = TYPE_CODE (type2);
1702 is_int1 = is_integral_type (type1);
1703 is_int2 = is_integral_type (type2);
1705 if (is_int1 && is_int2)
1706 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1708 else if ((code1 == TYPE_CODE_FLT || is_int1)
1709 && (code2 == TYPE_CODE_FLT || is_int2))
1711 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1712 `long double' values are returned in static storage (m68k). */
1713 DOUBLEST d = value_as_double (arg1);
1715 return d < value_as_double (arg2);
1717 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1718 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1720 gdb_byte v1[16], v2[16];
1722 enum bfd_endian byte_order_v1, byte_order_v2;
1724 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1725 v2, &len_v2, &byte_order_v2);
1727 return decimal_compare (v1, len_v1, byte_order_v1,
1728 v2, len_v2, byte_order_v2) == -1;
1730 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1731 return value_as_address (arg1) < value_as_address (arg2);
1733 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1735 else if (code1 == TYPE_CODE_PTR && is_int2)
1736 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1737 else if (code2 == TYPE_CODE_PTR && is_int1)
1738 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1739 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1740 return value_strcmp (arg1, arg2) < 0;
1743 error (_("Invalid type combination in ordering comparison."));
1748 /* The unary operators +, - and ~. They free the argument ARG1. */
1751 value_pos (struct value *arg1)
1755 arg1 = coerce_ref (arg1);
1756 type = check_typedef (value_type (arg1));
1758 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1759 return value_from_double (type, value_as_double (arg1));
1760 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1761 return value_from_decfloat (type, value_contents (arg1));
1762 else if (is_integral_type (type))
1764 return value_from_longest (type, value_as_long (arg1));
1766 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1768 struct value *val = allocate_value (type);
1770 memcpy (value_contents_raw (val), value_contents (arg1),
1771 TYPE_LENGTH (type));
1776 error (_("Argument to positive operation not a number."));
1777 return 0; /* For lint -- never reached. */
1782 value_neg (struct value *arg1)
1786 arg1 = coerce_ref (arg1);
1787 type = check_typedef (value_type (arg1));
1789 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1791 struct value *val = allocate_value (type);
1792 int len = TYPE_LENGTH (type);
1793 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */
1795 memcpy (decbytes, value_contents (arg1), len);
1797 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
1798 decbytes[len-1] = decbytes[len - 1] | 0x80;
1800 decbytes[0] = decbytes[0] | 0x80;
1802 memcpy (value_contents_raw (val), decbytes, len);
1805 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1806 return value_from_double (type, -value_as_double (arg1));
1807 else if (is_integral_type (type))
1809 return value_from_longest (type, -value_as_long (arg1));
1811 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1813 struct value *tmp, *val = allocate_value (type);
1814 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1816 LONGEST low_bound, high_bound;
1818 if (!get_array_bounds (type, &low_bound, &high_bound))
1819 error (_("Could not determine the vector bounds"));
1821 for (i = 0; i < high_bound - low_bound + 1; i++)
1823 tmp = value_neg (value_subscript (arg1, i));
1824 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1825 value_contents_all (tmp), TYPE_LENGTH (eltype));
1831 error (_("Argument to negate operation not a number."));
1832 return 0; /* For lint -- never reached. */
1837 value_complement (struct value *arg1)
1842 arg1 = coerce_ref (arg1);
1843 type = check_typedef (value_type (arg1));
1845 if (is_integral_type (type))
1846 val = value_from_longest (type, ~value_as_long (arg1));
1847 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1850 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1852 LONGEST low_bound, high_bound;
1854 if (!get_array_bounds (type, &low_bound, &high_bound))
1855 error (_("Could not determine the vector bounds"));
1857 val = allocate_value (type);
1858 for (i = 0; i < high_bound - low_bound + 1; i++)
1860 tmp = value_complement (value_subscript (arg1, i));
1861 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1862 value_contents_all (tmp), TYPE_LENGTH (eltype));
1866 error (_("Argument to complement operation not an integer, boolean."));
1871 /* The INDEX'th bit of SET value whose value_type is TYPE,
1872 and whose value_contents is valaddr.
1873 Return -1 if out of range, -2 other error. */
1876 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1878 struct gdbarch *gdbarch = get_type_arch (type);
1879 LONGEST low_bound, high_bound;
1882 struct type *range = TYPE_INDEX_TYPE (type);
1884 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1886 if (index < low_bound || index > high_bound)
1888 rel_index = index - low_bound;
1889 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1890 gdbarch_byte_order (gdbarch));
1891 rel_index %= TARGET_CHAR_BIT;
1892 if (gdbarch_bits_big_endian (gdbarch))
1893 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1894 return (word >> rel_index) & 1;
1898 value_in (struct value *element, struct value *set)
1901 struct type *settype = check_typedef (value_type (set));
1902 struct type *eltype = check_typedef (value_type (element));
1904 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1905 eltype = TYPE_TARGET_TYPE (eltype);
1906 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1907 error (_("Second argument of 'IN' has wrong type"));
1908 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1909 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1910 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1911 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1912 error (_("First argument of 'IN' has wrong type"));
1913 member = value_bit_index (settype, value_contents (set),
1914 value_as_long (element));
1916 error (_("First argument of 'IN' not in range"));
1921 _initialize_valarith (void)