1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009,
5 2010, 2011 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "expression.h"
29 #include "gdb_string.h"
34 #include "exceptions.h"
36 /* Define whether or not the C operator '/' truncates towards zero for
37 differently signed operands (truncation direction is undefined in C). */
39 #ifndef TRUNCATION_TOWARDS_ZERO
40 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
43 void _initialize_valarith (void);
46 /* Given a pointer, return the size of its target.
47 If the pointer type is void *, then return 1.
48 If the target type is incomplete, then error out.
49 This isn't a general purpose function, but just a
50 helper for value_ptradd. */
53 find_size_for_pointer_math (struct type *ptr_type)
56 struct type *ptr_target;
58 gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
59 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
61 sz = TYPE_LENGTH (ptr_target);
64 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
70 name = TYPE_NAME (ptr_target);
72 name = TYPE_TAG_NAME (ptr_target);
74 error (_("Cannot perform pointer math on incomplete types, "
75 "try casting to a known type, or void *."));
77 error (_("Cannot perform pointer math on incomplete type \"%s\", "
78 "try casting to a known type, or void *."), name);
84 /* Given a pointer ARG1 and an integral value ARG2, return the
85 result of C-style pointer arithmetic ARG1 + ARG2. */
88 value_ptradd (struct value *arg1, LONGEST arg2)
90 struct type *valptrtype;
94 arg1 = coerce_array (arg1);
95 valptrtype = check_typedef (value_type (arg1));
96 sz = find_size_for_pointer_math (valptrtype);
98 result = value_from_pointer (valptrtype,
99 value_as_address (arg1) + sz * arg2);
100 if (VALUE_LVAL (result) != lval_internalvar)
101 set_value_component_location (result, arg1);
105 /* Given two compatible pointer values ARG1 and ARG2, return the
106 result of C-style pointer arithmetic ARG1 - ARG2. */
109 value_ptrdiff (struct value *arg1, struct value *arg2)
111 struct type *type1, *type2;
114 arg1 = coerce_array (arg1);
115 arg2 = coerce_array (arg2);
116 type1 = check_typedef (value_type (arg1));
117 type2 = check_typedef (value_type (arg2));
119 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
120 gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
122 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
123 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
124 error (_("First argument of `-' is a pointer and "
125 "second argument is neither\n"
126 "an integer nor a pointer of the same type."));
128 sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
131 warning (_("Type size unknown, assuming 1. "
132 "Try casting to a known type, or void *."));
136 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
139 /* Return the value of ARRAY[IDX].
141 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
142 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
143 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
145 See comments in value_coerce_array() for rationale for reason for
146 doing lower bounds adjustment here rather than there.
147 FIXME: Perhaps we should validate that the index is valid and if
148 verbosity is set, warn about invalid indices (but still use them). */
151 value_subscript (struct value *array, LONGEST index)
153 int c_style = current_language->c_style_arrays;
156 array = coerce_ref (array);
157 tarray = check_typedef (value_type (array));
159 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
160 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
162 struct type *range_type = TYPE_INDEX_TYPE (tarray);
163 LONGEST lowerbound, upperbound;
165 get_discrete_bounds (range_type, &lowerbound, &upperbound);
166 if (VALUE_LVAL (array) != lval_memory)
167 return value_subscripted_rvalue (array, index, lowerbound);
171 if (index >= lowerbound && index <= upperbound)
172 return value_subscripted_rvalue (array, index, lowerbound);
173 /* Emit warning unless we have an array of unknown size.
174 An array of unknown size has lowerbound 0 and upperbound -1. */
176 warning (_("array or string index out of range"));
177 /* fall doing C stuff */
182 array = value_coerce_array (array);
186 return value_ind (value_ptradd (array, index));
188 error (_("not an array or string"));
191 /* Return the value of EXPR[IDX], expr an aggregate rvalue
192 (eg, a vector register). This routine used to promote floats
193 to doubles, but no longer does. */
196 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
198 struct type *array_type = check_typedef (value_type (array));
199 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
200 unsigned int elt_size = TYPE_LENGTH (elt_type);
201 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
204 if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
205 && elt_offs >= TYPE_LENGTH (array_type)))
206 error (_("no such vector element"));
208 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
209 v = allocate_value_lazy (elt_type);
212 v = allocate_value (elt_type);
213 value_contents_copy (v, value_embedded_offset (v),
214 array, value_embedded_offset (array) + elt_offs,
218 set_value_component_location (v, array);
219 VALUE_REGNUM (v) = VALUE_REGNUM (array);
220 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
221 set_value_offset (v, value_offset (array) + elt_offs);
225 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
228 value_bitstring_subscript (struct type *type,
229 struct value *bitstring, LONGEST index)
232 struct type *bitstring_type, *range_type;
234 int offset, byte, bit_index;
235 LONGEST lowerbound, upperbound;
237 bitstring_type = check_typedef (value_type (bitstring));
238 gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
240 range_type = TYPE_INDEX_TYPE (bitstring_type);
241 get_discrete_bounds (range_type, &lowerbound, &upperbound);
242 if (index < lowerbound || index > upperbound)
243 error (_("bitstring index out of range"));
246 offset = index / TARGET_CHAR_BIT;
247 byte = *((char *) value_contents (bitstring) + offset);
249 bit_index = index % TARGET_CHAR_BIT;
250 byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ?
251 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
253 v = value_from_longest (type, byte & 1);
255 set_value_bitpos (v, bit_index);
256 set_value_bitsize (v, 1);
257 set_value_component_location (v, bitstring);
258 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
260 set_value_offset (v, offset + value_offset (bitstring));
266 /* Check to see if either argument is a structure, or a reference to
267 one. This is called so we know whether to go ahead with the normal
268 binop or look for a user defined function instead.
270 For now, we do not overload the `=' operator. */
273 binop_types_user_defined_p (enum exp_opcode op,
274 struct type *type1, struct type *type2)
276 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
279 type1 = check_typedef (type1);
280 if (TYPE_CODE (type1) == TYPE_CODE_REF)
281 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
283 type2 = check_typedef (type1);
284 if (TYPE_CODE (type2) == TYPE_CODE_REF)
285 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
287 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
288 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
291 /* Check to see if either argument is a structure, or a reference to
292 one. This is called so we know whether to go ahead with the normal
293 binop or look for a user defined function instead.
295 For now, we do not overload the `=' operator. */
298 binop_user_defined_p (enum exp_opcode op,
299 struct value *arg1, struct value *arg2)
301 return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
304 /* Check to see if argument is a structure. This is called so
305 we know whether to go ahead with the normal unop or look for a
306 user defined function instead.
308 For now, we do not overload the `&' operator. */
311 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
317 type1 = check_typedef (value_type (arg1));
318 if (TYPE_CODE (type1) == TYPE_CODE_REF)
319 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
320 return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
323 /* Try to find an operator named OPERATOR which takes NARGS arguments
324 specified in ARGS. If the operator found is a static member operator
325 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
326 The search if performed through find_overload_match which will handle
327 member operators, non member operators, operators imported implicitly or
328 explicitly, and perform correct overload resolution in all of the above
329 situations or combinations thereof. */
331 static struct value *
332 value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
333 int *static_memfuncp)
336 struct symbol *symp = NULL;
337 struct value *valp = NULL;
338 struct type **arg_types;
341 arg_types = (struct type **) alloca (nargs * (sizeof (struct type *)));
342 /* Prepare list of argument types for overload resolution. */
343 for (i = 0; i < nargs; i++)
344 arg_types[i] = value_type (args[i]);
346 find_overload_match (arg_types, nargs, operator, BOTH /* could be method */,
347 0 /* strict match */, &args[0], /* objp */
348 NULL /* pass NULL symbol since symbol is unknown */,
349 &valp, &symp, static_memfuncp, 0);
356 /* This is a non member function and does not
357 expect a reference as its first argument
358 rather the explicit structure. */
359 args[0] = value_ind (args[0]);
360 return value_of_variable (symp, 0);
363 error (_("Could not find %s."), operator);
366 /* Lookup user defined operator NAME. Return a value representing the
367 function, otherwise return NULL. */
369 static struct value *
370 value_user_defined_op (struct value **argp, struct value **args, char *name,
371 int *static_memfuncp, int nargs)
373 struct value *result = NULL;
375 if (current_language->la_language == language_cplus)
376 result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp);
378 result = value_struct_elt (argp, args, name, static_memfuncp,
384 /* We know either arg1 or arg2 is a structure, so try to find the right
385 user defined function. Create an argument vector that calls
386 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
387 binary operator which is legal for GNU C++).
389 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
390 is the opcode saying how to modify it. Otherwise, OTHEROP is
394 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
395 enum exp_opcode otherop, enum noside noside)
397 struct value **argvec;
402 arg1 = coerce_ref (arg1);
403 arg2 = coerce_ref (arg2);
405 /* now we know that what we have to do is construct our
406 arg vector and find the right function to call it with. */
408 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
409 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
411 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
412 argvec[1] = value_addr (arg1);
416 /* Make the right function name up. */
417 strcpy (tstr, "operator__");
442 case BINOP_BITWISE_AND:
445 case BINOP_BITWISE_IOR:
448 case BINOP_BITWISE_XOR:
451 case BINOP_LOGICAL_AND:
454 case BINOP_LOGICAL_OR:
466 case BINOP_ASSIGN_MODIFY:
484 case BINOP_BITWISE_AND:
487 case BINOP_BITWISE_IOR:
490 case BINOP_BITWISE_XOR:
493 case BINOP_MOD: /* invalid */
495 error (_("Invalid binary operation specified."));
498 case BINOP_SUBSCRIPT:
519 case BINOP_MOD: /* invalid */
521 error (_("Invalid binary operation specified."));
524 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
525 &static_memfuncp, 2);
531 argvec[1] = argvec[0];
534 if (noside == EVAL_AVOID_SIDE_EFFECTS)
536 struct type *return_type;
539 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
540 return value_zero (return_type, VALUE_LVAL (arg1));
542 return call_function_by_hand (argvec[0], 2 - static_memfuncp,
545 throw_error (NOT_FOUND_ERROR,
546 _("member function %s not found"), tstr);
548 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
552 /* We know that arg1 is a structure, so try to find a unary user
553 defined operator that matches the operator in question.
554 Create an argument vector that calls arg1.operator @ (arg1)
555 and return that value (where '@' is (almost) any unary operator which
556 is legal for GNU C++). */
559 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
561 struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
562 struct value **argvec;
563 char *ptr, *mangle_ptr;
564 char tstr[13], mangle_tstr[13];
565 int static_memfuncp, nargs;
567 arg1 = coerce_ref (arg1);
569 /* now we know that what we have to do is construct our
570 arg vector and find the right function to call it with. */
572 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
573 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
575 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
576 argvec[1] = value_addr (arg1);
581 /* Make the right function name up. */
582 strcpy (tstr, "operator__");
584 strcpy (mangle_tstr, "__");
585 mangle_ptr = mangle_tstr + 2;
588 case UNOP_PREINCREMENT:
591 case UNOP_PREDECREMENT:
594 case UNOP_POSTINCREMENT:
596 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
600 case UNOP_POSTDECREMENT:
602 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
606 case UNOP_LOGICAL_NOT:
609 case UNOP_COMPLEMENT:
625 error (_("Invalid unary operation specified."));
628 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
629 &static_memfuncp, nargs);
635 argvec[1] = argvec[0];
639 if (noside == EVAL_AVOID_SIDE_EFFECTS)
641 struct type *return_type;
644 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
645 return value_zero (return_type, VALUE_LVAL (arg1));
647 return call_function_by_hand (argvec[0], nargs, argvec + 1);
649 throw_error (NOT_FOUND_ERROR,
650 _("member function %s not found"), tstr);
652 return 0; /* For lint -- never reached */
656 /* Concatenate two values with the following conditions:
658 (1) Both values must be either bitstring values or character string
659 values and the resulting value consists of the concatenation of
660 ARG1 followed by ARG2.
664 One value must be an integer value and the other value must be
665 either a bitstring value or character string value, which is
666 to be repeated by the number of times specified by the integer
670 (2) Boolean values are also allowed and are treated as bit string
673 (3) Character values are also allowed and are treated as character
674 string values of length 1. */
677 value_concat (struct value *arg1, struct value *arg2)
679 struct value *inval1;
680 struct value *inval2;
681 struct value *outval = NULL;
682 int inval1len, inval2len;
686 struct type *type1 = check_typedef (value_type (arg1));
687 struct type *type2 = check_typedef (value_type (arg2));
688 struct type *char_type;
690 /* First figure out if we are dealing with two values to be concatenated
691 or a repeat count and a value to be repeated. INVAL1 is set to the
692 first of two concatenated values, or the repeat count. INVAL2 is set
693 to the second of the two concatenated values or the value to be
696 if (TYPE_CODE (type2) == TYPE_CODE_INT)
698 struct type *tmp = type1;
711 /* Now process the input values. */
713 if (TYPE_CODE (type1) == TYPE_CODE_INT)
715 /* We have a repeat count. Validate the second value and then
716 construct a value repeated that many times. */
717 if (TYPE_CODE (type2) == TYPE_CODE_STRING
718 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
720 count = longest_to_int (value_as_long (inval1));
721 inval2len = TYPE_LENGTH (type2);
722 ptr = (char *) alloca (count * inval2len);
723 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
727 inchar = (char) unpack_long (type2,
728 value_contents (inval2));
729 for (idx = 0; idx < count; idx++)
731 *(ptr + idx) = inchar;
736 char_type = TYPE_TARGET_TYPE (type2);
738 for (idx = 0; idx < count; idx++)
740 memcpy (ptr + (idx * inval2len), value_contents (inval2),
744 outval = value_string (ptr, count * inval2len, char_type);
746 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
747 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
749 error (_("unimplemented support for bitstring/boolean repeats"));
753 error (_("can't repeat values of that type"));
756 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
757 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
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 *) alloca (inval1len + inval2len);
768 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
772 *ptr = (char) unpack_long (type1, value_contents (inval1));
776 char_type = TYPE_TARGET_TYPE (type1);
778 memcpy (ptr, value_contents (inval1), inval1len);
780 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
783 (char) unpack_long (type2, value_contents (inval2));
787 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
789 outval = value_string (ptr, inval1len + inval2len, char_type);
791 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
792 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
794 /* We have two bitstrings to concatenate. */
795 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
796 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
798 error (_("Bitstrings or booleans can only be concatenated "
799 "with other bitstrings or booleans."));
801 error (_("unimplemented support for bitstring/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 /* Performs a binary operation on two vector operands by calling scalar_binop
1395 for each pair of vector components. */
1397 static struct value *
1398 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1400 struct value *val, *tmp, *mark;
1401 struct type *type1, *type2, *eltype1, *eltype2, *result_type;
1402 int t1_is_vec, t2_is_vec, elsize, i;
1403 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1405 type1 = check_typedef (value_type (val1));
1406 type2 = check_typedef (value_type (val2));
1408 t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1409 && TYPE_VECTOR (type1)) ? 1 : 0;
1410 t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1411 && TYPE_VECTOR (type2)) ? 1 : 0;
1413 if (!t1_is_vec || !t2_is_vec)
1414 error (_("Vector operations are only supported among vectors"));
1416 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1417 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1418 error (_("Could not determine the vector bounds"));
1420 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1421 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1422 elsize = TYPE_LENGTH (eltype1);
1424 if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1425 || elsize != TYPE_LENGTH (eltype2)
1426 || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1427 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1428 error (_("Cannot perform operation on vectors with different types"));
1430 val = allocate_value (type1);
1431 mark = value_mark ();
1432 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1434 tmp = value_binop (value_subscript (val1, i),
1435 value_subscript (val2, i), op);
1436 memcpy (value_contents_writeable (val) + i * elsize,
1437 value_contents_all (tmp),
1440 value_free_to_mark (mark);
1445 /* Perform a binary operation on two operands. */
1448 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1451 struct type *type1 = check_typedef (value_type (arg1));
1452 struct type *type2 = check_typedef (value_type (arg2));
1453 int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1454 && TYPE_VECTOR (type1));
1455 int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1456 && TYPE_VECTOR (type2));
1458 if (!t1_is_vec && !t2_is_vec)
1459 val = scalar_binop (arg1, arg2, op);
1460 else if (t1_is_vec && t2_is_vec)
1461 val = vector_binop (arg1, arg2, op);
1464 /* Widen the scalar operand to a vector. */
1465 struct value **v = t1_is_vec ? &arg2 : &arg1;
1466 struct type *t = t1_is_vec ? type2 : type1;
1468 if (TYPE_CODE (t) != TYPE_CODE_FLT
1469 && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1470 && !is_integral_type (t))
1471 error (_("Argument to operation not a number or boolean."));
1473 *v = value_cast (t1_is_vec ? type1 : type2, *v);
1474 val = vector_binop (arg1, arg2, op);
1480 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1483 value_logical_not (struct value *arg1)
1489 arg1 = coerce_array (arg1);
1490 type1 = check_typedef (value_type (arg1));
1492 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1493 return 0 == value_as_double (arg1);
1494 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1495 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1496 gdbarch_byte_order (get_type_arch (type1)));
1498 len = TYPE_LENGTH (type1);
1499 p = value_contents (arg1);
1510 /* Perform a comparison on two string values (whose content are not
1511 necessarily null terminated) based on their length. */
1514 value_strcmp (struct value *arg1, struct value *arg2)
1516 int len1 = TYPE_LENGTH (value_type (arg1));
1517 int len2 = TYPE_LENGTH (value_type (arg2));
1518 const gdb_byte *s1 = value_contents (arg1);
1519 const gdb_byte *s2 = value_contents (arg2);
1520 int i, len = len1 < len2 ? len1 : len2;
1522 for (i = 0; i < len; i++)
1526 else if (s1[i] > s2[i])
1534 else if (len1 > len2)
1540 /* Simulate the C operator == by returning a 1
1541 iff ARG1 and ARG2 have equal contents. */
1544 value_equal (struct value *arg1, struct value *arg2)
1549 struct type *type1, *type2;
1550 enum type_code code1;
1551 enum type_code code2;
1552 int is_int1, is_int2;
1554 arg1 = coerce_array (arg1);
1555 arg2 = coerce_array (arg2);
1557 type1 = check_typedef (value_type (arg1));
1558 type2 = check_typedef (value_type (arg2));
1559 code1 = TYPE_CODE (type1);
1560 code2 = TYPE_CODE (type2);
1561 is_int1 = is_integral_type (type1);
1562 is_int2 = is_integral_type (type2);
1564 if (is_int1 && is_int2)
1565 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1567 else if ((code1 == TYPE_CODE_FLT || is_int1)
1568 && (code2 == TYPE_CODE_FLT || is_int2))
1570 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1571 `long double' values are returned in static storage (m68k). */
1572 DOUBLEST d = value_as_double (arg1);
1574 return d == value_as_double (arg2);
1576 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1577 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1579 gdb_byte v1[16], v2[16];
1581 enum bfd_endian byte_order_v1, byte_order_v2;
1583 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1584 v2, &len_v2, &byte_order_v2);
1586 return decimal_compare (v1, len_v1, byte_order_v1,
1587 v2, len_v2, byte_order_v2) == 0;
1590 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1592 else if (code1 == TYPE_CODE_PTR && is_int2)
1593 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1594 else if (code2 == TYPE_CODE_PTR && is_int1)
1595 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1597 else if (code1 == code2
1598 && ((len = (int) TYPE_LENGTH (type1))
1599 == (int) TYPE_LENGTH (type2)))
1601 p1 = value_contents (arg1);
1602 p2 = value_contents (arg2);
1610 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1612 return value_strcmp (arg1, arg2) == 0;
1616 error (_("Invalid type combination in equality test."));
1617 return 0; /* For lint -- never reached. */
1621 /* Compare values based on their raw contents. Useful for arrays since
1622 value_equal coerces them to pointers, thus comparing just the address
1623 of the array instead of its contents. */
1626 value_equal_contents (struct value *arg1, struct value *arg2)
1628 struct type *type1, *type2;
1630 type1 = check_typedef (value_type (arg1));
1631 type2 = check_typedef (value_type (arg2));
1633 return (TYPE_CODE (type1) == TYPE_CODE (type2)
1634 && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1635 && memcmp (value_contents (arg1), value_contents (arg2),
1636 TYPE_LENGTH (type1)) == 0);
1639 /* Simulate the C operator < by returning 1
1640 iff ARG1's contents are less than ARG2's. */
1643 value_less (struct value *arg1, struct value *arg2)
1645 enum type_code code1;
1646 enum type_code code2;
1647 struct type *type1, *type2;
1648 int is_int1, is_int2;
1650 arg1 = coerce_array (arg1);
1651 arg2 = coerce_array (arg2);
1653 type1 = check_typedef (value_type (arg1));
1654 type2 = check_typedef (value_type (arg2));
1655 code1 = TYPE_CODE (type1);
1656 code2 = TYPE_CODE (type2);
1657 is_int1 = is_integral_type (type1);
1658 is_int2 = is_integral_type (type2);
1660 if (is_int1 && is_int2)
1661 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1663 else if ((code1 == TYPE_CODE_FLT || is_int1)
1664 && (code2 == TYPE_CODE_FLT || is_int2))
1666 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1667 `long double' values are returned in static storage (m68k). */
1668 DOUBLEST d = value_as_double (arg1);
1670 return d < value_as_double (arg2);
1672 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1673 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1675 gdb_byte v1[16], v2[16];
1677 enum bfd_endian byte_order_v1, byte_order_v2;
1679 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1680 v2, &len_v2, &byte_order_v2);
1682 return decimal_compare (v1, len_v1, byte_order_v1,
1683 v2, len_v2, byte_order_v2) == -1;
1685 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1686 return value_as_address (arg1) < value_as_address (arg2);
1688 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1690 else if (code1 == TYPE_CODE_PTR && is_int2)
1691 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1692 else if (code2 == TYPE_CODE_PTR && is_int1)
1693 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1694 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1695 return value_strcmp (arg1, arg2) < 0;
1698 error (_("Invalid type combination in ordering comparison."));
1703 /* The unary operators +, - and ~. They free the argument ARG1. */
1706 value_pos (struct value *arg1)
1710 arg1 = coerce_ref (arg1);
1711 type = check_typedef (value_type (arg1));
1713 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1714 return value_from_double (type, value_as_double (arg1));
1715 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1716 return value_from_decfloat (type, value_contents (arg1));
1717 else if (is_integral_type (type))
1719 return value_from_longest (type, value_as_long (arg1));
1721 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1723 struct value *val = allocate_value (type);
1725 memcpy (value_contents_raw (val), value_contents (arg1),
1726 TYPE_LENGTH (type));
1731 error (_("Argument to positive operation not a number."));
1732 return 0; /* For lint -- never reached. */
1737 value_neg (struct value *arg1)
1741 arg1 = coerce_ref (arg1);
1742 type = check_typedef (value_type (arg1));
1744 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1746 struct value *val = allocate_value (type);
1747 int len = TYPE_LENGTH (type);
1748 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */
1750 memcpy (decbytes, value_contents (arg1), len);
1752 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
1753 decbytes[len-1] = decbytes[len - 1] | 0x80;
1755 decbytes[0] = decbytes[0] | 0x80;
1757 memcpy (value_contents_raw (val), decbytes, len);
1760 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1761 return value_from_double (type, -value_as_double (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 *tmp, *val = allocate_value (type);
1769 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1771 LONGEST low_bound, high_bound;
1773 if (!get_array_bounds (type, &low_bound, &high_bound))
1774 error (_("Could not determine the vector bounds"));
1776 for (i = 0; i < high_bound - low_bound + 1; i++)
1778 tmp = value_neg (value_subscript (arg1, i));
1779 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1780 value_contents_all (tmp), TYPE_LENGTH (eltype));
1786 error (_("Argument to negate operation not a number."));
1787 return 0; /* For lint -- never reached. */
1792 value_complement (struct value *arg1)
1797 arg1 = coerce_ref (arg1);
1798 type = check_typedef (value_type (arg1));
1800 if (is_integral_type (type))
1801 val = value_from_longest (type, ~value_as_long (arg1));
1802 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1805 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1807 LONGEST low_bound, high_bound;
1809 if (!get_array_bounds (type, &low_bound, &high_bound))
1810 error (_("Could not determine the vector bounds"));
1812 val = allocate_value (type);
1813 for (i = 0; i < high_bound - low_bound + 1; i++)
1815 tmp = value_complement (value_subscript (arg1, i));
1816 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1817 value_contents_all (tmp), TYPE_LENGTH (eltype));
1821 error (_("Argument to complement operation not an integer, boolean."));
1826 /* The INDEX'th bit of SET value whose value_type is TYPE,
1827 and whose value_contents is valaddr.
1828 Return -1 if out of range, -2 other error. */
1831 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1833 struct gdbarch *gdbarch = get_type_arch (type);
1834 LONGEST low_bound, high_bound;
1837 struct type *range = TYPE_INDEX_TYPE (type);
1839 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1841 if (index < low_bound || index > high_bound)
1843 rel_index = index - low_bound;
1844 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1845 gdbarch_byte_order (gdbarch));
1846 rel_index %= TARGET_CHAR_BIT;
1847 if (gdbarch_bits_big_endian (gdbarch))
1848 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1849 return (word >> rel_index) & 1;
1853 value_in (struct value *element, struct value *set)
1856 struct type *settype = check_typedef (value_type (set));
1857 struct type *eltype = check_typedef (value_type (element));
1859 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1860 eltype = TYPE_TARGET_TYPE (eltype);
1861 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1862 error (_("Second argument of 'IN' has wrong type"));
1863 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1864 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1865 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1866 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1867 error (_("First argument of 'IN' has wrong type"));
1868 member = value_bit_index (settype, value_contents (set),
1869 value_as_long (element));
1871 error (_("First argument of 'IN' not in range"));
1876 _initialize_valarith (void)