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_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;
93 arg1 = coerce_array (arg1);
94 valptrtype = check_typedef (value_type (arg1));
95 sz = find_size_for_pointer_math (valptrtype);
97 return value_from_pointer (valptrtype,
98 value_as_address (arg1) + sz * arg2);
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))))
121 First argument of `-' is a pointer and second argument is neither\n\
122 an integer nor a pointer of the same type."));
124 sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
125 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
128 /* Return the value of ARRAY[IDX].
130 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
131 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
132 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
134 See comments in value_coerce_array() for rationale for reason for
135 doing lower bounds adjustment here rather than there.
136 FIXME: Perhaps we should validate that the index is valid and if
137 verbosity is set, warn about invalid indices (but still use them). */
140 value_subscript (struct value *array, LONGEST index)
143 int c_style = current_language->c_style_arrays;
146 array = coerce_ref (array);
147 tarray = check_typedef (value_type (array));
149 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
150 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
152 struct type *range_type = TYPE_INDEX_TYPE (tarray);
153 LONGEST lowerbound, upperbound;
154 get_discrete_bounds (range_type, &lowerbound, &upperbound);
156 if (VALUE_LVAL (array) != lval_memory)
157 return value_subscripted_rvalue (array, index, lowerbound);
161 if (index >= lowerbound && index <= upperbound)
162 return value_subscripted_rvalue (array, index, lowerbound);
163 /* Emit warning unless we have an array of unknown size.
164 An array of unknown size has lowerbound 0 and upperbound -1. */
166 warning (_("array or string index out of range"));
167 /* fall doing C stuff */
172 array = value_coerce_array (array);
176 return value_ind (value_ptradd (array, index));
178 error (_("not an array or string"));
181 /* Return the value of EXPR[IDX], expr an aggregate rvalue
182 (eg, a vector register). This routine used to promote floats
183 to doubles, but no longer does. */
186 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
188 struct type *array_type = check_typedef (value_type (array));
189 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
190 unsigned int elt_size = TYPE_LENGTH (elt_type);
191 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
194 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
195 error (_("no such vector element"));
197 v = allocate_value (elt_type);
198 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
199 set_value_lazy (v, 1);
201 memcpy (value_contents_writeable (v),
202 value_contents (array) + elt_offs, elt_size);
204 set_value_component_location (v, array);
205 VALUE_REGNUM (v) = VALUE_REGNUM (array);
206 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
207 set_value_offset (v, value_offset (array) + elt_offs);
211 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
214 value_bitstring_subscript (struct type *type,
215 struct value *bitstring, LONGEST index)
218 struct type *bitstring_type, *range_type;
220 int offset, byte, bit_index;
221 LONGEST lowerbound, upperbound;
223 bitstring_type = check_typedef (value_type (bitstring));
224 gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
226 range_type = TYPE_INDEX_TYPE (bitstring_type);
227 get_discrete_bounds (range_type, &lowerbound, &upperbound);
228 if (index < lowerbound || index > upperbound)
229 error (_("bitstring index out of range"));
232 offset = index / TARGET_CHAR_BIT;
233 byte = *((char *) value_contents (bitstring) + offset);
235 bit_index = index % TARGET_CHAR_BIT;
236 byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ?
237 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
239 v = value_from_longest (type, byte & 1);
241 set_value_bitpos (v, bit_index);
242 set_value_bitsize (v, 1);
243 set_value_component_location (v, bitstring);
244 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
246 set_value_offset (v, offset + value_offset (bitstring));
252 /* Check to see if either argument is a structure, or a reference to
253 one. This is called so we know whether to go ahead with the normal
254 binop or look for a user defined function instead.
256 For now, we do not overload the `=' operator. */
259 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
261 struct type *type1, *type2;
262 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
265 type1 = check_typedef (value_type (arg1));
266 if (TYPE_CODE (type1) == TYPE_CODE_REF)
267 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
269 type2 = check_typedef (value_type (arg2));
270 if (TYPE_CODE (type2) == TYPE_CODE_REF)
271 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
273 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
274 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
277 /* Check to see if argument is a structure. This is called so
278 we know whether to go ahead with the normal unop or look for a
279 user defined function instead.
281 For now, we do not overload the `&' operator. */
284 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
289 type1 = check_typedef (value_type (arg1));
292 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
294 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
295 type1 = TYPE_TARGET_TYPE (type1);
301 /* We know either arg1 or arg2 is a structure, so try to find the right
302 user defined function. Create an argument vector that calls
303 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
304 binary operator which is legal for GNU C++).
306 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
307 is the opcode saying how to modify it. Otherwise, OTHEROP is
311 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
312 enum exp_opcode otherop, enum noside noside)
314 struct value **argvec;
319 arg1 = coerce_ref (arg1);
320 arg2 = coerce_ref (arg2);
322 /* now we know that what we have to do is construct our
323 arg vector and find the right function to call it with. */
325 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
326 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
328 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
329 argvec[1] = value_addr (arg1);
333 /* make the right function name up */
334 strcpy (tstr, "operator__");
359 case BINOP_BITWISE_AND:
362 case BINOP_BITWISE_IOR:
365 case BINOP_BITWISE_XOR:
368 case BINOP_LOGICAL_AND:
371 case BINOP_LOGICAL_OR:
383 case BINOP_ASSIGN_MODIFY:
401 case BINOP_BITWISE_AND:
404 case BINOP_BITWISE_IOR:
407 case BINOP_BITWISE_XOR:
410 case BINOP_MOD: /* invalid */
412 error (_("Invalid binary operation specified."));
415 case BINOP_SUBSCRIPT:
436 case BINOP_MOD: /* invalid */
438 error (_("Invalid binary operation specified."));
441 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
447 argvec[1] = argvec[0];
450 if (noside == EVAL_AVOID_SIDE_EFFECTS)
452 struct type *return_type;
454 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
455 return value_zero (return_type, VALUE_LVAL (arg1));
457 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
459 error (_("member function %s not found"), tstr);
461 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
465 /* We know that arg1 is a structure, so try to find a unary user
466 defined operator that matches the operator in question.
467 Create an argument vector that calls arg1.operator @ (arg1)
468 and return that value (where '@' is (almost) any unary operator which
469 is legal for GNU C++). */
472 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
474 struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
475 struct value **argvec;
476 char *ptr, *mangle_ptr;
477 char tstr[13], mangle_tstr[13];
478 int static_memfuncp, nargs;
480 arg1 = coerce_ref (arg1);
482 /* now we know that what we have to do is construct our
483 arg vector and find the right function to call it with. */
485 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
486 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
488 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
489 argvec[1] = value_addr (arg1);
494 /* make the right function name up */
495 strcpy (tstr, "operator__");
497 strcpy (mangle_tstr, "__");
498 mangle_ptr = mangle_tstr + 2;
501 case UNOP_PREINCREMENT:
504 case UNOP_PREDECREMENT:
507 case UNOP_POSTINCREMENT:
509 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
513 case UNOP_POSTDECREMENT:
515 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
519 case UNOP_LOGICAL_NOT:
522 case UNOP_COMPLEMENT:
535 error (_("Invalid unary operation specified."));
538 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
544 argvec[1] = argvec[0];
548 if (noside == EVAL_AVOID_SIDE_EFFECTS)
550 struct type *return_type;
552 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
553 return value_zero (return_type, VALUE_LVAL (arg1));
555 return call_function_by_hand (argvec[0], nargs, argvec + 1);
557 error (_("member function %s not found"), tstr);
558 return 0; /* For lint -- never reached */
562 /* Concatenate two values with the following conditions:
564 (1) Both values must be either bitstring values or character string
565 values and the resulting value consists of the concatenation of
566 ARG1 followed by ARG2.
570 One value must be an integer value and the other value must be
571 either a bitstring value or character string value, which is
572 to be repeated by the number of times specified by the integer
576 (2) Boolean values are also allowed and are treated as bit string
579 (3) Character values are also allowed and are treated as character
580 string values of length 1.
584 value_concat (struct value *arg1, struct value *arg2)
586 struct value *inval1;
587 struct value *inval2;
588 struct value *outval = NULL;
589 int inval1len, inval2len;
593 struct type *type1 = check_typedef (value_type (arg1));
594 struct type *type2 = check_typedef (value_type (arg2));
595 struct type *char_type;
597 /* First figure out if we are dealing with two values to be concatenated
598 or a repeat count and a value to be repeated. INVAL1 is set to the
599 first of two concatenated values, or the repeat count. INVAL2 is set
600 to the second of the two concatenated values or the value to be
603 if (TYPE_CODE (type2) == TYPE_CODE_INT)
605 struct type *tmp = type1;
617 /* Now process the input values. */
619 if (TYPE_CODE (type1) == TYPE_CODE_INT)
621 /* We have a repeat count. Validate the second value and then
622 construct a value repeated that many times. */
623 if (TYPE_CODE (type2) == TYPE_CODE_STRING
624 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
626 count = longest_to_int (value_as_long (inval1));
627 inval2len = TYPE_LENGTH (type2);
628 ptr = (char *) alloca (count * inval2len);
629 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
632 inchar = (char) unpack_long (type2,
633 value_contents (inval2));
634 for (idx = 0; idx < count; idx++)
636 *(ptr + idx) = inchar;
641 char_type = TYPE_TARGET_TYPE (type2);
642 for (idx = 0; idx < count; idx++)
644 memcpy (ptr + (idx * inval2len), value_contents (inval2),
648 outval = value_string (ptr, count * inval2len, char_type);
650 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
651 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
653 error (_("unimplemented support for bitstring/boolean repeats"));
657 error (_("can't repeat values of that type"));
660 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
661 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
663 /* We have two character strings to concatenate. */
664 if (TYPE_CODE (type2) != TYPE_CODE_STRING
665 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
667 error (_("Strings can only be concatenated with other strings."));
669 inval1len = TYPE_LENGTH (type1);
670 inval2len = TYPE_LENGTH (type2);
671 ptr = (char *) alloca (inval1len + inval2len);
672 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
675 *ptr = (char) unpack_long (type1, value_contents (inval1));
679 char_type = TYPE_TARGET_TYPE (type1);
680 memcpy (ptr, value_contents (inval1), inval1len);
682 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
685 (char) unpack_long (type2, value_contents (inval2));
689 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
691 outval = value_string (ptr, inval1len + inval2len, char_type);
693 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
694 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
696 /* We have two bitstrings to concatenate. */
697 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
698 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
700 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
702 error (_("unimplemented support for bitstring/boolean concatenation."));
706 /* We don't know how to concatenate these operands. */
707 error (_("illegal operands for concatenation."));
712 /* Integer exponentiation: V1**V2, where both arguments are
713 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
715 integer_pow (LONGEST v1, LONGEST v2)
720 error (_("Attempt to raise 0 to negative power."));
726 /* The Russian Peasant's Algorithm */
742 /* Integer exponentiation: V1**V2, where both arguments are
743 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
745 uinteger_pow (ULONGEST v1, LONGEST v2)
750 error (_("Attempt to raise 0 to negative power."));
756 /* The Russian Peasant's Algorithm */
772 /* Obtain decimal value of arguments for binary operation, converting from
773 other types if one of them is not decimal floating point. */
775 value_args_as_decimal (struct value *arg1, struct value *arg2,
776 gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
777 gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
779 struct type *type1, *type2;
781 type1 = check_typedef (value_type (arg1));
782 type2 = check_typedef (value_type (arg2));
784 /* At least one of the arguments must be of decimal float type. */
785 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
786 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
788 if (TYPE_CODE (type1) == TYPE_CODE_FLT
789 || TYPE_CODE (type2) == TYPE_CODE_FLT)
790 /* The DFP extension to the C language does not allow mixing of
791 * decimal float types with other float types in expressions
792 * (see WDTR 24732, page 12). */
793 error (_("Mixing decimal floating types with other floating types is not allowed."));
795 /* Obtain decimal value of arg1, converting from other types
798 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
800 *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
801 *len_x = TYPE_LENGTH (type1);
802 memcpy (x, value_contents (arg1), *len_x);
804 else if (is_integral_type (type1))
806 *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
807 *len_x = TYPE_LENGTH (type2);
808 decimal_from_integral (arg1, x, *len_x, *byte_order_x);
811 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
814 /* Obtain decimal value of arg2, converting from other types
817 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
819 *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
820 *len_y = TYPE_LENGTH (type2);
821 memcpy (y, value_contents (arg2), *len_y);
823 else if (is_integral_type (type2))
825 *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
826 *len_y = TYPE_LENGTH (type1);
827 decimal_from_integral (arg2, y, *len_y, *byte_order_y);
830 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
834 /* Perform a binary operation on two operands which have reasonable
835 representations as integers or floats. This includes booleans,
836 characters, integers, or floats.
837 Does not support addition and subtraction on pointers;
838 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
841 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
844 struct type *type1, *type2, *result_type;
846 arg1 = coerce_ref (arg1);
847 arg2 = coerce_ref (arg2);
849 type1 = check_typedef (value_type (arg1));
850 type2 = check_typedef (value_type (arg2));
852 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
853 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
854 && !is_integral_type (type1))
855 || (TYPE_CODE (type2) != TYPE_CODE_FLT
856 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
857 && !is_integral_type (type2)))
858 error (_("Argument to arithmetic operation not a number or boolean."));
860 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
861 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
864 int len_v1, len_v2, len_v;
865 enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
866 gdb_byte v1[16], v2[16];
869 /* If only one type is decimal float, use its type.
870 Otherwise use the bigger type. */
871 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
873 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
875 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
880 len_v = TYPE_LENGTH (result_type);
881 byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
883 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
884 v2, &len_v2, &byte_order_v2);
893 decimal_binop (op, v1, len_v1, byte_order_v1,
894 v2, len_v2, byte_order_v2,
895 v, len_v, byte_order_v);
899 error (_("Operation not valid for decimal floating point number."));
902 val = value_from_decfloat (result_type, v);
904 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
905 || TYPE_CODE (type2) == TYPE_CODE_FLT)
907 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
908 in target format. real.c in GCC probably has the necessary
910 DOUBLEST v1, v2, v = 0;
911 v1 = value_as_double (arg1);
912 v2 = value_as_double (arg2);
936 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
940 v = v1 < v2 ? v1 : v2;
944 v = v1 > v2 ? v1 : v2;
948 error (_("Integer-only operation on floating point number."));
951 /* If only one type is float, use its type.
952 Otherwise use the bigger type. */
953 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
955 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
957 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
962 val = allocate_value (result_type);
963 store_typed_floating (value_contents_raw (val), value_type (val), v);
965 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
966 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
968 LONGEST v1, v2, v = 0;
969 v1 = value_as_long (arg1);
970 v2 = value_as_long (arg2);
974 case BINOP_BITWISE_AND:
978 case BINOP_BITWISE_IOR:
982 case BINOP_BITWISE_XOR:
995 error (_("Invalid operation on booleans."));
1000 val = allocate_value (result_type);
1001 store_signed_integer (value_contents_raw (val),
1002 TYPE_LENGTH (result_type),
1003 gdbarch_byte_order (get_type_arch (result_type)),
1007 /* Integral operations here. */
1009 /* Determine type length of the result, and if the operation should
1010 be done unsigned. For exponentiation and shift operators,
1011 use the length and type of the left operand. Otherwise,
1012 use the signedness of the operand with the greater length.
1013 If both operands are of equal length, use unsigned operation
1014 if one of the operands is unsigned. */
1015 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1016 result_type = type1;
1017 else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1018 result_type = type1;
1019 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1020 result_type = type2;
1021 else if (TYPE_UNSIGNED (type1))
1022 result_type = type1;
1023 else if (TYPE_UNSIGNED (type2))
1024 result_type = type2;
1026 result_type = type1;
1028 if (TYPE_UNSIGNED (result_type))
1030 LONGEST v2_signed = value_as_long (arg2);
1031 ULONGEST v1, v2, v = 0;
1032 v1 = (ULONGEST) value_as_long (arg1);
1033 v2 = (ULONGEST) v2_signed;
1054 error (_("Division by zero"));
1058 v = uinteger_pow (v1, v2_signed);
1065 error (_("Division by zero"));
1069 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1070 v1 mod 0 has a defined value, v1. */
1078 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1091 case BINOP_BITWISE_AND:
1095 case BINOP_BITWISE_IOR:
1099 case BINOP_BITWISE_XOR:
1103 case BINOP_LOGICAL_AND:
1107 case BINOP_LOGICAL_OR:
1112 v = v1 < v2 ? v1 : v2;
1116 v = v1 > v2 ? v1 : v2;
1123 case BINOP_NOTEQUAL:
1132 error (_("Invalid binary operation on numbers."));
1135 val = allocate_value (result_type);
1136 store_unsigned_integer (value_contents_raw (val),
1137 TYPE_LENGTH (value_type (val)),
1139 (get_type_arch (result_type)),
1144 LONGEST v1, v2, v = 0;
1145 v1 = value_as_long (arg1);
1146 v2 = value_as_long (arg2);
1167 error (_("Division by zero"));
1171 v = integer_pow (v1, v2);
1178 error (_("Division by zero"));
1182 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1183 X mod 0 has a defined value, X. */
1191 /* Compute floor. */
1192 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1208 case BINOP_BITWISE_AND:
1212 case BINOP_BITWISE_IOR:
1216 case BINOP_BITWISE_XOR:
1220 case BINOP_LOGICAL_AND:
1224 case BINOP_LOGICAL_OR:
1229 v = v1 < v2 ? v1 : v2;
1233 v = v1 > v2 ? v1 : v2;
1245 error (_("Invalid binary operation on numbers."));
1248 val = allocate_value (result_type);
1249 store_signed_integer (value_contents_raw (val),
1250 TYPE_LENGTH (value_type (val)),
1252 (get_type_arch (result_type)),
1260 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1263 value_logical_not (struct value *arg1)
1269 arg1 = coerce_array (arg1);
1270 type1 = check_typedef (value_type (arg1));
1272 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1273 return 0 == value_as_double (arg1);
1274 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1275 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1276 gdbarch_byte_order (get_type_arch (type1)));
1278 len = TYPE_LENGTH (type1);
1279 p = value_contents (arg1);
1290 /* Perform a comparison on two string values (whose content are not
1291 necessarily null terminated) based on their length */
1294 value_strcmp (struct value *arg1, struct value *arg2)
1296 int len1 = TYPE_LENGTH (value_type (arg1));
1297 int len2 = TYPE_LENGTH (value_type (arg2));
1298 const gdb_byte *s1 = value_contents (arg1);
1299 const gdb_byte *s2 = value_contents (arg2);
1300 int i, len = len1 < len2 ? len1 : len2;
1302 for (i = 0; i < len; i++)
1306 else if (s1[i] > s2[i])
1314 else if (len1 > len2)
1320 /* Simulate the C operator == by returning a 1
1321 iff ARG1 and ARG2 have equal contents. */
1324 value_equal (struct value *arg1, struct value *arg2)
1329 struct type *type1, *type2;
1330 enum type_code code1;
1331 enum type_code code2;
1332 int is_int1, is_int2;
1334 arg1 = coerce_array (arg1);
1335 arg2 = coerce_array (arg2);
1337 type1 = check_typedef (value_type (arg1));
1338 type2 = check_typedef (value_type (arg2));
1339 code1 = TYPE_CODE (type1);
1340 code2 = TYPE_CODE (type2);
1341 is_int1 = is_integral_type (type1);
1342 is_int2 = is_integral_type (type2);
1344 if (is_int1 && is_int2)
1345 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1347 else if ((code1 == TYPE_CODE_FLT || is_int1)
1348 && (code2 == TYPE_CODE_FLT || is_int2))
1350 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1351 `long double' values are returned in static storage (m68k). */
1352 DOUBLEST d = value_as_double (arg1);
1353 return d == value_as_double (arg2);
1355 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1356 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1358 gdb_byte v1[16], v2[16];
1360 enum bfd_endian byte_order_v1, byte_order_v2;
1362 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1363 v2, &len_v2, &byte_order_v2);
1365 return decimal_compare (v1, len_v1, byte_order_v1,
1366 v2, len_v2, byte_order_v2) == 0;
1369 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1371 else if (code1 == TYPE_CODE_PTR && is_int2)
1372 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1373 else if (code2 == TYPE_CODE_PTR && is_int1)
1374 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1376 else if (code1 == code2
1377 && ((len = (int) TYPE_LENGTH (type1))
1378 == (int) TYPE_LENGTH (type2)))
1380 p1 = value_contents (arg1);
1381 p2 = value_contents (arg2);
1389 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1391 return value_strcmp (arg1, arg2) == 0;
1395 error (_("Invalid type combination in equality test."));
1396 return 0; /* For lint -- never reached */
1400 /* Simulate the C operator < by returning 1
1401 iff ARG1's contents are less than ARG2's. */
1404 value_less (struct value *arg1, struct value *arg2)
1406 enum type_code code1;
1407 enum type_code code2;
1408 struct type *type1, *type2;
1409 int is_int1, is_int2;
1411 arg1 = coerce_array (arg1);
1412 arg2 = coerce_array (arg2);
1414 type1 = check_typedef (value_type (arg1));
1415 type2 = check_typedef (value_type (arg2));
1416 code1 = TYPE_CODE (type1);
1417 code2 = TYPE_CODE (type2);
1418 is_int1 = is_integral_type (type1);
1419 is_int2 = is_integral_type (type2);
1421 if (is_int1 && is_int2)
1422 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1424 else if ((code1 == TYPE_CODE_FLT || is_int1)
1425 && (code2 == TYPE_CODE_FLT || is_int2))
1427 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1428 `long double' values are returned in static storage (m68k). */
1429 DOUBLEST d = value_as_double (arg1);
1430 return d < value_as_double (arg2);
1432 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1433 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1435 gdb_byte v1[16], v2[16];
1437 enum bfd_endian byte_order_v1, byte_order_v2;
1439 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1440 v2, &len_v2, &byte_order_v2);
1442 return decimal_compare (v1, len_v1, byte_order_v1,
1443 v2, len_v2, byte_order_v2) == -1;
1445 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1446 return value_as_address (arg1) < value_as_address (arg2);
1448 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1450 else if (code1 == TYPE_CODE_PTR && is_int2)
1451 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1452 else if (code2 == TYPE_CODE_PTR && is_int1)
1453 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1454 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1455 return value_strcmp (arg1, arg2) < 0;
1458 error (_("Invalid type combination in ordering comparison."));
1463 /* The unary operators +, - and ~. They free the argument ARG1. */
1466 value_pos (struct value *arg1)
1470 arg1 = coerce_ref (arg1);
1471 type = check_typedef (value_type (arg1));
1473 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1474 return value_from_double (type, value_as_double (arg1));
1475 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1476 return value_from_decfloat (type, value_contents (arg1));
1477 else if (is_integral_type (type))
1479 return value_from_longest (type, value_as_long (arg1));
1483 error ("Argument to positive operation not a number.");
1484 return 0; /* For lint -- never reached */
1489 value_neg (struct value *arg1)
1493 arg1 = coerce_ref (arg1);
1494 type = check_typedef (value_type (arg1));
1496 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1498 struct value *val = allocate_value (type);
1499 int len = TYPE_LENGTH (type);
1500 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
1502 memcpy (decbytes, value_contents (arg1), len);
1504 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
1505 decbytes[len-1] = decbytes[len - 1] | 0x80;
1507 decbytes[0] = decbytes[0] | 0x80;
1509 memcpy (value_contents_raw (val), decbytes, len);
1512 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1513 return value_from_double (type, -value_as_double (arg1));
1514 else if (is_integral_type (type))
1516 return value_from_longest (type, -value_as_long (arg1));
1520 error (_("Argument to negate operation not a number."));
1521 return 0; /* For lint -- never reached */
1526 value_complement (struct value *arg1)
1530 arg1 = coerce_ref (arg1);
1531 type = check_typedef (value_type (arg1));
1533 if (!is_integral_type (type))
1534 error (_("Argument to complement operation not an integer or boolean."));
1536 return value_from_longest (type, ~value_as_long (arg1));
1539 /* The INDEX'th bit of SET value whose value_type is TYPE,
1540 and whose value_contents is valaddr.
1541 Return -1 if out of range, -2 other error. */
1544 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1546 struct gdbarch *gdbarch = get_type_arch (type);
1547 LONGEST low_bound, high_bound;
1550 struct type *range = TYPE_INDEX_TYPE (type);
1551 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1553 if (index < low_bound || index > high_bound)
1555 rel_index = index - low_bound;
1556 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1557 gdbarch_byte_order (gdbarch));
1558 rel_index %= TARGET_CHAR_BIT;
1559 if (gdbarch_bits_big_endian (gdbarch))
1560 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1561 return (word >> rel_index) & 1;
1565 value_in (struct value *element, struct value *set)
1568 struct type *settype = check_typedef (value_type (set));
1569 struct type *eltype = check_typedef (value_type (element));
1570 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1571 eltype = TYPE_TARGET_TYPE (eltype);
1572 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1573 error (_("Second argument of 'IN' has wrong type"));
1574 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1575 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1576 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1577 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1578 error (_("First argument of 'IN' has wrong type"));
1579 member = value_bit_index (settype, value_contents (set),
1580 value_as_long (element));
1582 error (_("First argument of 'IN' not in range"));
1587 _initialize_valarith (void)