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 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "expression.h"
29 #include "gdb_string.h"
35 /* Define whether or not the C operator '/' truncates towards zero for
36 differently signed operands (truncation direction is undefined in C). */
38 #ifndef TRUNCATION_TOWARDS_ZERO
39 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
42 void _initialize_valarith (void);
45 /* Given a pointer, return the size of its target.
46 If the pointer type is void *, then return 1.
47 If the target type is incomplete, then error out.
48 This isn't a general purpose function, but just a
49 helper for value_ptrsub & 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, struct value *arg2)
90 struct type *valptrtype;
93 arg1 = coerce_array (arg1);
94 valptrtype = check_typedef (value_type (arg1));
95 sz = find_size_for_pointer_math (valptrtype);
97 if (!is_integral_type (value_type (arg2)))
98 error (_("Argument to arithmetic operation not a number or boolean."));
100 return value_from_pointer (valptrtype,
101 value_as_address (arg1)
102 + (sz * value_as_long (arg2)));
105 /* Given a pointer ARG1 and an integral value ARG2, return the
106 result of C-style pointer arithmetic ARG1 - ARG2. */
109 value_ptrsub (struct value *arg1, struct value *arg2)
111 struct type *valptrtype;
114 arg1 = coerce_array (arg1);
115 valptrtype = check_typedef (value_type (arg1));
116 sz = find_size_for_pointer_math (valptrtype);
118 if (!is_integral_type (value_type (arg2)))
119 error (_("Argument to arithmetic operation not a number or boolean."));
121 return value_from_pointer (valptrtype,
122 value_as_address (arg1)
123 - (sz * value_as_long (arg2)));
126 /* Given two compatible pointer values ARG1 and ARG2, return the
127 result of C-style pointer arithmetic ARG1 - ARG2. */
130 value_ptrdiff (struct value *arg1, struct value *arg2)
132 struct type *type1, *type2;
135 arg1 = coerce_array (arg1);
136 arg2 = coerce_array (arg2);
137 type1 = check_typedef (value_type (arg1));
138 type2 = check_typedef (value_type (arg2));
140 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
141 gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
143 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
144 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
146 First argument of `-' is a pointer and second argument is neither\n\
147 an integer nor a pointer of the same type."));
149 sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
150 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
153 /* Return the value of ARRAY[IDX].
155 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
156 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
157 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
159 See comments in value_coerce_array() for rationale for reason for
160 doing lower bounds adjustment here rather than there.
161 FIXME: Perhaps we should validate that the index is valid and if
162 verbosity is set, warn about invalid indices (but still use them). */
165 value_subscript (struct value *array, struct value *idx)
168 int c_style = current_language->c_style_arrays;
171 array = coerce_ref (array);
172 tarray = check_typedef (value_type (array));
174 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
175 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
177 struct type *range_type = TYPE_INDEX_TYPE (tarray);
178 LONGEST lowerbound, upperbound;
179 get_discrete_bounds (range_type, &lowerbound, &upperbound);
181 if (VALUE_LVAL (array) != lval_memory)
182 return value_subscripted_rvalue (array, idx, lowerbound);
186 LONGEST index = value_as_long (idx);
187 if (index >= lowerbound && index <= upperbound)
188 return value_subscripted_rvalue (array, idx, lowerbound);
189 /* Emit warning unless we have an array of unknown size.
190 An array of unknown size has lowerbound 0 and upperbound -1. */
192 warning (_("array or string index out of range"));
193 /* fall doing C stuff */
199 bound = value_from_longest (value_type (idx), (LONGEST) lowerbound);
200 idx = value_binop (idx, bound, BINOP_SUB);
203 array = value_coerce_array (array);
207 return value_ind (value_ptradd (array, idx));
209 error (_("not an array or string"));
212 /* Return the value of EXPR[IDX], expr an aggregate rvalue
213 (eg, a vector register). This routine used to promote floats
214 to doubles, but no longer does. */
217 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
219 struct type *array_type = check_typedef (value_type (array));
220 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
221 unsigned int elt_size = TYPE_LENGTH (elt_type);
222 LONGEST index = value_as_long (idx);
223 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
226 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
227 error (_("no such vector element"));
229 v = allocate_value (elt_type);
230 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
231 set_value_lazy (v, 1);
233 memcpy (value_contents_writeable (v),
234 value_contents (array) + elt_offs, elt_size);
236 set_value_component_location (v, array);
237 VALUE_REGNUM (v) = VALUE_REGNUM (array);
238 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
239 set_value_offset (v, value_offset (array) + elt_offs);
243 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
246 value_bitstring_subscript (struct type *type,
247 struct value *bitstring, struct value *idx)
250 struct type *bitstring_type, *range_type;
251 LONGEST index = value_as_long (idx);
253 int offset, byte, bit_index;
254 LONGEST lowerbound, upperbound;
256 bitstring_type = check_typedef (value_type (bitstring));
257 gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
259 range_type = TYPE_INDEX_TYPE (bitstring_type);
260 get_discrete_bounds (range_type, &lowerbound, &upperbound);
261 if (index < lowerbound || index > upperbound)
262 error (_("bitstring index out of range"));
265 offset = index / TARGET_CHAR_BIT;
266 byte = *((char *) value_contents (bitstring) + offset);
268 bit_index = index % TARGET_CHAR_BIT;
269 byte >>= (gdbarch_bits_big_endian (current_gdbarch) ?
270 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
272 v = value_from_longest (type, byte & 1);
274 set_value_bitpos (v, bit_index);
275 set_value_bitsize (v, 1);
276 set_value_component_location (v, bitstring);
277 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
279 set_value_offset (v, offset + value_offset (bitstring));
285 /* Check to see if either argument is a structure, or a reference to
286 one. This is called so we know whether to go ahead with the normal
287 binop or look for a user defined function instead.
289 For now, we do not overload the `=' operator. */
292 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
294 struct type *type1, *type2;
295 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
298 type1 = check_typedef (value_type (arg1));
299 if (TYPE_CODE (type1) == TYPE_CODE_REF)
300 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
302 type2 = check_typedef (value_type (arg2));
303 if (TYPE_CODE (type2) == TYPE_CODE_REF)
304 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
306 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
307 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
310 /* Check to see if argument is a structure. This is called so
311 we know whether to go ahead with the normal unop or look for a
312 user defined function instead.
314 For now, we do not overload the `&' operator. */
317 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
322 type1 = check_typedef (value_type (arg1));
325 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
327 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
328 type1 = TYPE_TARGET_TYPE (type1);
334 /* We know either arg1 or arg2 is a structure, so try to find the right
335 user defined function. Create an argument vector that calls
336 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
337 binary operator which is legal for GNU C++).
339 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
340 is the opcode saying how to modify it. Otherwise, OTHEROP is
344 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
345 enum exp_opcode otherop, enum noside noside)
347 struct value **argvec;
352 arg1 = coerce_ref (arg1);
353 arg2 = coerce_ref (arg2);
355 /* now we know that what we have to do is construct our
356 arg vector and find the right function to call it with. */
358 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
359 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
361 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
362 argvec[1] = value_addr (arg1);
366 /* make the right function name up */
367 strcpy (tstr, "operator__");
392 case BINOP_BITWISE_AND:
395 case BINOP_BITWISE_IOR:
398 case BINOP_BITWISE_XOR:
401 case BINOP_LOGICAL_AND:
404 case BINOP_LOGICAL_OR:
416 case BINOP_ASSIGN_MODIFY:
434 case BINOP_BITWISE_AND:
437 case BINOP_BITWISE_IOR:
440 case BINOP_BITWISE_XOR:
443 case BINOP_MOD: /* invalid */
445 error (_("Invalid binary operation specified."));
448 case BINOP_SUBSCRIPT:
469 case BINOP_MOD: /* invalid */
471 error (_("Invalid binary operation specified."));
474 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
480 argvec[1] = argvec[0];
483 if (noside == EVAL_AVOID_SIDE_EFFECTS)
485 struct type *return_type;
487 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
488 return value_zero (return_type, VALUE_LVAL (arg1));
490 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
492 error (_("member function %s not found"), tstr);
494 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
498 /* We know that arg1 is a structure, so try to find a unary user
499 defined operator that matches the operator in question.
500 Create an argument vector that calls arg1.operator @ (arg1)
501 and return that value (where '@' is (almost) any unary operator which
502 is legal for GNU C++). */
505 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
507 struct value **argvec;
508 char *ptr, *mangle_ptr;
509 char tstr[13], mangle_tstr[13];
510 int static_memfuncp, nargs;
512 arg1 = coerce_ref (arg1);
514 /* now we know that what we have to do is construct our
515 arg vector and find the right function to call it with. */
517 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
518 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
520 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
521 argvec[1] = value_addr (arg1);
526 /* make the right function name up */
527 strcpy (tstr, "operator__");
529 strcpy (mangle_tstr, "__");
530 mangle_ptr = mangle_tstr + 2;
533 case UNOP_PREINCREMENT:
536 case UNOP_PREDECREMENT:
539 case UNOP_POSTINCREMENT:
541 argvec[2] = value_from_longest (builtin_type_int8, 0);
545 case UNOP_POSTDECREMENT:
547 argvec[2] = value_from_longest (builtin_type_int8, 0);
551 case UNOP_LOGICAL_NOT:
554 case UNOP_COMPLEMENT:
567 error (_("Invalid unary operation specified."));
570 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
576 argvec[1] = argvec[0];
580 if (noside == EVAL_AVOID_SIDE_EFFECTS)
582 struct type *return_type;
584 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
585 return value_zero (return_type, VALUE_LVAL (arg1));
587 return call_function_by_hand (argvec[0], nargs, argvec + 1);
589 error (_("member function %s not found"), tstr);
590 return 0; /* For lint -- never reached */
594 /* Concatenate two values with the following conditions:
596 (1) Both values must be either bitstring values or character string
597 values and the resulting value consists of the concatenation of
598 ARG1 followed by ARG2.
602 One value must be an integer value and the other value must be
603 either a bitstring value or character string value, which is
604 to be repeated by the number of times specified by the integer
608 (2) Boolean values are also allowed and are treated as bit string
611 (3) Character values are also allowed and are treated as character
612 string values of length 1.
616 value_concat (struct value *arg1, struct value *arg2)
618 struct value *inval1;
619 struct value *inval2;
620 struct value *outval = NULL;
621 int inval1len, inval2len;
625 struct type *type1 = check_typedef (value_type (arg1));
626 struct type *type2 = check_typedef (value_type (arg2));
627 struct type *char_type;
629 /* First figure out if we are dealing with two values to be concatenated
630 or a repeat count and a value to be repeated. INVAL1 is set to the
631 first of two concatenated values, or the repeat count. INVAL2 is set
632 to the second of the two concatenated values or the value to be
635 if (TYPE_CODE (type2) == TYPE_CODE_INT)
637 struct type *tmp = type1;
649 /* Now process the input values. */
651 if (TYPE_CODE (type1) == TYPE_CODE_INT)
653 /* We have a repeat count. Validate the second value and then
654 construct a value repeated that many times. */
655 if (TYPE_CODE (type2) == TYPE_CODE_STRING
656 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
658 count = longest_to_int (value_as_long (inval1));
659 inval2len = TYPE_LENGTH (type2);
660 ptr = (char *) alloca (count * inval2len);
661 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
664 inchar = (char) unpack_long (type2,
665 value_contents (inval2));
666 for (idx = 0; idx < count; idx++)
668 *(ptr + idx) = inchar;
673 char_type = TYPE_TARGET_TYPE (type2);
674 for (idx = 0; idx < count; idx++)
676 memcpy (ptr + (idx * inval2len), value_contents (inval2),
680 outval = value_string (ptr, count * inval2len, char_type);
682 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
683 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
685 error (_("unimplemented support for bitstring/boolean repeats"));
689 error (_("can't repeat values of that type"));
692 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
693 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
695 /* We have two character strings to concatenate. */
696 if (TYPE_CODE (type2) != TYPE_CODE_STRING
697 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
699 error (_("Strings can only be concatenated with other strings."));
701 inval1len = TYPE_LENGTH (type1);
702 inval2len = TYPE_LENGTH (type2);
703 ptr = (char *) alloca (inval1len + inval2len);
704 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
707 *ptr = (char) unpack_long (type1, value_contents (inval1));
711 char_type = TYPE_TARGET_TYPE (type1);
712 memcpy (ptr, value_contents (inval1), inval1len);
714 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
717 (char) unpack_long (type2, value_contents (inval2));
721 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
723 outval = value_string (ptr, inval1len + inval2len, char_type);
725 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
726 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
728 /* We have two bitstrings to concatenate. */
729 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
730 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
732 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
734 error (_("unimplemented support for bitstring/boolean concatenation."));
738 /* We don't know how to concatenate these operands. */
739 error (_("illegal operands for concatenation."));
744 /* Integer exponentiation: V1**V2, where both arguments are
745 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
747 integer_pow (LONGEST v1, LONGEST v2)
752 error (_("Attempt to raise 0 to negative power."));
758 /* The Russian Peasant's Algorithm */
774 /* Integer exponentiation: V1**V2, where both arguments are
775 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
777 uinteger_pow (ULONGEST v1, LONGEST v2)
782 error (_("Attempt to raise 0 to negative power."));
788 /* The Russian Peasant's Algorithm */
804 /* Obtain decimal value of arguments for binary operation, converting from
805 other types if one of them is not decimal floating point. */
807 value_args_as_decimal (struct value *arg1, struct value *arg2,
808 gdb_byte *x, int *len_x, gdb_byte *y, int *len_y)
810 struct type *type1, *type2;
812 type1 = check_typedef (value_type (arg1));
813 type2 = check_typedef (value_type (arg2));
815 /* At least one of the arguments must be of decimal float type. */
816 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
817 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
819 if (TYPE_CODE (type1) == TYPE_CODE_FLT
820 || TYPE_CODE (type2) == TYPE_CODE_FLT)
821 /* The DFP extension to the C language does not allow mixing of
822 * decimal float types with other float types in expressions
823 * (see WDTR 24732, page 12). */
824 error (_("Mixing decimal floating types with other floating types is not allowed."));
826 /* Obtain decimal value of arg1, converting from other types
829 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
831 *len_x = TYPE_LENGTH (type1);
832 memcpy (x, value_contents (arg1), *len_x);
834 else if (is_integral_type (type1))
836 *len_x = TYPE_LENGTH (type2);
837 decimal_from_integral (arg1, x, *len_x);
840 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
843 /* Obtain decimal value of arg2, converting from other types
846 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
848 *len_y = TYPE_LENGTH (type2);
849 memcpy (y, value_contents (arg2), *len_y);
851 else if (is_integral_type (type2))
853 *len_y = TYPE_LENGTH (type1);
854 decimal_from_integral (arg2, y, *len_y);
857 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
861 /* Perform a binary operation on two operands which have reasonable
862 representations as integers or floats. This includes booleans,
863 characters, integers, or floats.
864 Does not support addition and subtraction on pointers;
865 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
868 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
871 struct type *type1, *type2, *result_type;
873 arg1 = coerce_ref (arg1);
874 arg2 = coerce_ref (arg2);
876 type1 = check_typedef (value_type (arg1));
877 type2 = check_typedef (value_type (arg2));
879 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
880 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
881 && !is_integral_type (type1))
882 || (TYPE_CODE (type2) != TYPE_CODE_FLT
883 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
884 && !is_integral_type (type2)))
885 error (_("Argument to arithmetic operation not a number or boolean."));
887 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
888 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
891 int len_v1, len_v2, len_v;
892 gdb_byte v1[16], v2[16];
895 /* If only one type is decimal float, use its type.
896 Otherwise use the bigger type. */
897 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
899 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
901 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
906 len_v = TYPE_LENGTH (result_type);
908 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
917 decimal_binop (op, v1, len_v1, v2, len_v2, v, len_v);
921 error (_("Operation not valid for decimal floating point number."));
924 val = value_from_decfloat (result_type, v);
926 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
927 || TYPE_CODE (type2) == TYPE_CODE_FLT)
929 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
930 in target format. real.c in GCC probably has the necessary
932 DOUBLEST v1, v2, v = 0;
933 v1 = value_as_double (arg1);
934 v2 = value_as_double (arg2);
958 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
962 v = v1 < v2 ? v1 : v2;
966 v = v1 > v2 ? v1 : v2;
970 error (_("Integer-only operation on floating point number."));
973 /* If only one type is float, use its type.
974 Otherwise use the bigger type. */
975 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
977 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
979 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
984 val = allocate_value (result_type);
985 store_typed_floating (value_contents_raw (val), value_type (val), v);
987 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
988 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
990 LONGEST v1, v2, v = 0;
991 v1 = value_as_long (arg1);
992 v2 = value_as_long (arg2);
996 case BINOP_BITWISE_AND:
1000 case BINOP_BITWISE_IOR:
1004 case BINOP_BITWISE_XOR:
1012 case BINOP_NOTEQUAL:
1017 error (_("Invalid operation on booleans."));
1020 result_type = type1;
1022 val = allocate_value (result_type);
1023 store_signed_integer (value_contents_raw (val),
1024 TYPE_LENGTH (result_type),
1028 /* Integral operations here. */
1030 /* Determine type length of the result, and if the operation should
1031 be done unsigned. For exponentiation and shift operators,
1032 use the length and type of the left operand. Otherwise,
1033 use the signedness of the operand with the greater length.
1034 If both operands are of equal length, use unsigned operation
1035 if one of the operands is unsigned. */
1036 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1037 result_type = type1;
1038 else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1039 result_type = type1;
1040 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1041 result_type = type2;
1042 else if (TYPE_UNSIGNED (type1))
1043 result_type = type1;
1044 else if (TYPE_UNSIGNED (type2))
1045 result_type = type2;
1047 result_type = type1;
1049 if (TYPE_UNSIGNED (result_type))
1051 LONGEST v2_signed = value_as_long (arg2);
1052 ULONGEST v1, v2, v = 0;
1053 v1 = (ULONGEST) value_as_long (arg1);
1054 v2 = (ULONGEST) v2_signed;
1075 error (_("Division by zero"));
1079 v = uinteger_pow (v1, v2_signed);
1086 error (_("Division by zero"));
1090 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1091 v1 mod 0 has a defined value, v1. */
1099 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1112 case BINOP_BITWISE_AND:
1116 case BINOP_BITWISE_IOR:
1120 case BINOP_BITWISE_XOR:
1124 case BINOP_LOGICAL_AND:
1128 case BINOP_LOGICAL_OR:
1133 v = v1 < v2 ? v1 : v2;
1137 v = v1 > v2 ? v1 : v2;
1144 case BINOP_NOTEQUAL:
1153 error (_("Invalid binary operation on numbers."));
1156 val = allocate_value (result_type);
1157 store_unsigned_integer (value_contents_raw (val),
1158 TYPE_LENGTH (value_type (val)),
1163 LONGEST v1, v2, v = 0;
1164 v1 = value_as_long (arg1);
1165 v2 = value_as_long (arg2);
1186 error (_("Division by zero"));
1190 v = integer_pow (v1, v2);
1197 error (_("Division by zero"));
1201 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1202 X mod 0 has a defined value, X. */
1210 /* Compute floor. */
1211 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1227 case BINOP_BITWISE_AND:
1231 case BINOP_BITWISE_IOR:
1235 case BINOP_BITWISE_XOR:
1239 case BINOP_LOGICAL_AND:
1243 case BINOP_LOGICAL_OR:
1248 v = v1 < v2 ? v1 : v2;
1252 v = v1 > v2 ? v1 : v2;
1264 error (_("Invalid binary operation on numbers."));
1267 val = allocate_value (result_type);
1268 store_signed_integer (value_contents_raw (val),
1269 TYPE_LENGTH (value_type (val)),
1277 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1280 value_logical_not (struct value *arg1)
1286 arg1 = coerce_array (arg1);
1287 type1 = check_typedef (value_type (arg1));
1289 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1290 return 0 == value_as_double (arg1);
1291 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1292 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1));
1294 len = TYPE_LENGTH (type1);
1295 p = value_contents (arg1);
1306 /* Perform a comparison on two string values (whose content are not
1307 necessarily null terminated) based on their length */
1310 value_strcmp (struct value *arg1, struct value *arg2)
1312 int len1 = TYPE_LENGTH (value_type (arg1));
1313 int len2 = TYPE_LENGTH (value_type (arg2));
1314 const gdb_byte *s1 = value_contents (arg1);
1315 const gdb_byte *s2 = value_contents (arg2);
1316 int i, len = len1 < len2 ? len1 : len2;
1318 for (i = 0; i < len; i++)
1322 else if (s1[i] > s2[i])
1330 else if (len1 > len2)
1336 /* Simulate the C operator == by returning a 1
1337 iff ARG1 and ARG2 have equal contents. */
1340 value_equal (struct value *arg1, struct value *arg2)
1345 struct type *type1, *type2;
1346 enum type_code code1;
1347 enum type_code code2;
1348 int is_int1, is_int2;
1350 arg1 = coerce_array (arg1);
1351 arg2 = coerce_array (arg2);
1353 type1 = check_typedef (value_type (arg1));
1354 type2 = check_typedef (value_type (arg2));
1355 code1 = TYPE_CODE (type1);
1356 code2 = TYPE_CODE (type2);
1357 is_int1 = is_integral_type (type1);
1358 is_int2 = is_integral_type (type2);
1360 if (is_int1 && is_int2)
1361 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1363 else if ((code1 == TYPE_CODE_FLT || is_int1)
1364 && (code2 == TYPE_CODE_FLT || is_int2))
1366 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1367 `long double' values are returned in static storage (m68k). */
1368 DOUBLEST d = value_as_double (arg1);
1369 return d == value_as_double (arg2);
1371 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1372 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1374 gdb_byte v1[16], v2[16];
1377 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1379 return decimal_compare (v1, len_v1, v2, len_v2) == 0;
1382 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1384 else if (code1 == TYPE_CODE_PTR && is_int2)
1385 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1386 else if (code2 == TYPE_CODE_PTR && is_int1)
1387 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1389 else if (code1 == code2
1390 && ((len = (int) TYPE_LENGTH (type1))
1391 == (int) TYPE_LENGTH (type2)))
1393 p1 = value_contents (arg1);
1394 p2 = value_contents (arg2);
1402 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1404 return value_strcmp (arg1, arg2) == 0;
1408 error (_("Invalid type combination in equality test."));
1409 return 0; /* For lint -- never reached */
1413 /* Simulate the C operator < by returning 1
1414 iff ARG1's contents are less than ARG2's. */
1417 value_less (struct value *arg1, struct value *arg2)
1419 enum type_code code1;
1420 enum type_code code2;
1421 struct type *type1, *type2;
1422 int is_int1, is_int2;
1424 arg1 = coerce_array (arg1);
1425 arg2 = coerce_array (arg2);
1427 type1 = check_typedef (value_type (arg1));
1428 type2 = check_typedef (value_type (arg2));
1429 code1 = TYPE_CODE (type1);
1430 code2 = TYPE_CODE (type2);
1431 is_int1 = is_integral_type (type1);
1432 is_int2 = is_integral_type (type2);
1434 if (is_int1 && is_int2)
1435 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1437 else if ((code1 == TYPE_CODE_FLT || is_int1)
1438 && (code2 == TYPE_CODE_FLT || is_int2))
1440 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1441 `long double' values are returned in static storage (m68k). */
1442 DOUBLEST d = value_as_double (arg1);
1443 return d < value_as_double (arg2);
1445 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1446 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1448 gdb_byte v1[16], v2[16];
1451 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1453 return decimal_compare (v1, len_v1, v2, len_v2) == -1;
1455 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1456 return value_as_address (arg1) < value_as_address (arg2);
1458 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1460 else if (code1 == TYPE_CODE_PTR && is_int2)
1461 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1462 else if (code2 == TYPE_CODE_PTR && is_int1)
1463 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1464 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1465 return value_strcmp (arg1, arg2) < 0;
1468 error (_("Invalid type combination in ordering comparison."));
1473 /* The unary operators +, - and ~. They free the argument ARG1. */
1476 value_pos (struct value *arg1)
1480 arg1 = coerce_ref (arg1);
1481 type = check_typedef (value_type (arg1));
1483 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1484 return value_from_double (type, value_as_double (arg1));
1485 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1486 return value_from_decfloat (type, value_contents (arg1));
1487 else if (is_integral_type (type))
1489 return value_from_longest (type, value_as_long (arg1));
1493 error ("Argument to positive operation not a number.");
1494 return 0; /* For lint -- never reached */
1499 value_neg (struct value *arg1)
1503 arg1 = coerce_ref (arg1);
1504 type = check_typedef (value_type (arg1));
1506 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1508 struct value *val = allocate_value (type);
1509 int len = TYPE_LENGTH (type);
1510 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
1512 memcpy (decbytes, value_contents (arg1), len);
1514 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
1515 decbytes[len-1] = decbytes[len - 1] | 0x80;
1517 decbytes[0] = decbytes[0] | 0x80;
1519 memcpy (value_contents_raw (val), decbytes, len);
1522 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1523 return value_from_double (type, -value_as_double (arg1));
1524 else if (is_integral_type (type))
1526 return value_from_longest (type, -value_as_long (arg1));
1530 error (_("Argument to negate operation not a number."));
1531 return 0; /* For lint -- never reached */
1536 value_complement (struct value *arg1)
1540 arg1 = coerce_ref (arg1);
1541 type = check_typedef (value_type (arg1));
1543 if (!is_integral_type (type))
1544 error (_("Argument to complement operation not an integer or boolean."));
1546 return value_from_longest (type, ~value_as_long (arg1));
1549 /* The INDEX'th bit of SET value whose value_type is TYPE,
1550 and whose value_contents is valaddr.
1551 Return -1 if out of range, -2 other error. */
1554 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1556 LONGEST low_bound, high_bound;
1559 struct type *range = TYPE_INDEX_TYPE (type);
1560 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1562 if (index < low_bound || index > high_bound)
1564 rel_index = index - low_bound;
1565 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1);
1566 rel_index %= TARGET_CHAR_BIT;
1567 if (gdbarch_bits_big_endian (current_gdbarch))
1568 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1569 return (word >> rel_index) & 1;
1573 value_in (struct value *element, struct value *set)
1576 struct type *settype = check_typedef (value_type (set));
1577 struct type *eltype = check_typedef (value_type (element));
1578 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1579 eltype = TYPE_TARGET_TYPE (eltype);
1580 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1581 error (_("Second argument of 'IN' has wrong type"));
1582 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1583 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1584 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1585 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1586 error (_("First argument of 'IN' has wrong type"));
1587 member = value_bit_index (settype, value_contents (set),
1588 value_as_long (element));
1590 error (_("First argument of 'IN' not in range"));
1595 _initialize_valarith (void)