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
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 static struct type *unop_result_type (enum exp_opcode op, struct type *type1);
43 static struct type *binop_result_type (enum exp_opcode op, struct type *type1,
46 void _initialize_valarith (void);
49 /* Given a pointer, return the size of its target.
50 If the pointer type is void *, then return 1.
51 If the target type is incomplete, then error out.
52 This isn't a general purpose function, but just a
53 helper for value_sub & value_add.
57 find_size_for_pointer_math (struct type *ptr_type)
60 struct type *ptr_target;
62 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
64 sz = TYPE_LENGTH (ptr_target);
67 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
73 name = TYPE_NAME (ptr_target);
75 name = TYPE_TAG_NAME (ptr_target);
77 error (_("Cannot perform pointer math on incomplete types, "
78 "try casting to a known type, or void *."));
80 error (_("Cannot perform pointer math on incomplete type \"%s\", "
81 "try casting to a known type, or void *."), name);
88 value_add (struct value *arg1, struct value *arg2)
93 struct type *type1, *type2, *valptrtype;
95 arg1 = coerce_array (arg1);
96 arg2 = coerce_array (arg2);
97 type1 = check_typedef (value_type (arg1));
98 type2 = check_typedef (value_type (arg2));
100 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
101 || TYPE_CODE (type2) == TYPE_CODE_PTR)
103 (is_integral_type (type1) || is_integral_type (type2)))
104 /* Exactly one argument is a pointer, and one is an integer. */
106 struct value *retval;
108 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
121 sz = find_size_for_pointer_math (valptrtype);
123 retval = value_from_pointer (valptrtype,
124 value_as_address (valptr)
125 + (sz * value_as_long (valint)));
129 return value_binop (arg1, arg2, BINOP_ADD);
133 value_sub (struct value *arg1, struct value *arg2)
135 struct type *type1, *type2;
136 arg1 = coerce_array (arg1);
137 arg2 = coerce_array (arg2);
138 type1 = check_typedef (value_type (arg1));
139 type2 = check_typedef (value_type (arg2));
141 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
143 if (is_integral_type (type2))
145 /* pointer - integer. */
146 LONGEST sz = find_size_for_pointer_math (type1);
148 return value_from_pointer (type1,
149 (value_as_address (arg1)
150 - (sz * value_as_long (arg2))));
152 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
153 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
154 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
156 /* pointer to <type x> - pointer to <type x>. */
157 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
158 return value_from_longest
159 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
160 (value_as_long (arg1) - value_as_long (arg2)) / sz);
165 First argument of `-' is a pointer and second argument is neither\n\
166 an integer nor a pointer of the same type."));
170 return value_binop (arg1, arg2, BINOP_SUB);
173 /* Return the value of ARRAY[IDX].
174 See comments in value_coerce_array() for rationale for reason for
175 doing lower bounds adjustment here rather than there.
176 FIXME: Perhaps we should validate that the index is valid and if
177 verbosity is set, warn about invalid indices (but still use them). */
180 value_subscript (struct value *array, struct value *idx)
183 int c_style = current_language->c_style_arrays;
186 array = coerce_ref (array);
187 tarray = check_typedef (value_type (array));
189 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
190 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
192 struct type *range_type = TYPE_INDEX_TYPE (tarray);
193 LONGEST lowerbound, upperbound;
194 get_discrete_bounds (range_type, &lowerbound, &upperbound);
196 if (VALUE_LVAL (array) != lval_memory)
197 return value_subscripted_rvalue (array, idx, lowerbound);
201 LONGEST index = value_as_long (idx);
202 if (index >= lowerbound && index <= upperbound)
203 return value_subscripted_rvalue (array, idx, lowerbound);
204 /* Emit warning unless we have an array of unknown size.
205 An array of unknown size has lowerbound 0 and upperbound -1. */
207 warning (_("array or string index out of range"));
208 /* fall doing C stuff */
214 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
215 idx = value_sub (idx, bound);
218 array = value_coerce_array (array);
221 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
223 struct type *range_type = TYPE_INDEX_TYPE (tarray);
224 LONGEST index = value_as_long (idx);
226 int offset, byte, bit_index;
227 LONGEST lowerbound, upperbound;
228 get_discrete_bounds (range_type, &lowerbound, &upperbound);
229 if (index < lowerbound || index > upperbound)
230 error (_("bitstring index out of range"));
232 offset = index / TARGET_CHAR_BIT;
233 byte = *((char *) value_contents (array) + offset);
234 bit_index = index % TARGET_CHAR_BIT;
235 byte >>= (gdbarch_bits_big_endian (current_gdbarch) ?
236 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
237 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
238 set_value_bitpos (v, bit_index);
239 set_value_bitsize (v, 1);
240 VALUE_LVAL (v) = VALUE_LVAL (array);
241 if (VALUE_LVAL (array) == lval_internalvar)
242 VALUE_LVAL (v) = lval_internalvar_component;
243 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
244 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
245 set_value_offset (v, offset + value_offset (array));
250 return value_ind (value_add (array, idx));
252 error (_("not an array or string"));
255 /* Return the value of EXPR[IDX], expr an aggregate rvalue
256 (eg, a vector register). This routine used to promote floats
257 to doubles, but no longer does. */
260 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
262 struct type *array_type = check_typedef (value_type (array));
263 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
264 unsigned int elt_size = TYPE_LENGTH (elt_type);
265 LONGEST index = value_as_long (idx);
266 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
269 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
270 error (_("no such vector element"));
272 v = allocate_value (elt_type);
273 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
274 set_value_lazy (v, 1);
276 memcpy (value_contents_writeable (v),
277 value_contents (array) + elt_offs, elt_size);
279 if (VALUE_LVAL (array) == lval_internalvar)
280 VALUE_LVAL (v) = lval_internalvar_component;
282 VALUE_LVAL (v) = VALUE_LVAL (array);
283 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
284 VALUE_REGNUM (v) = VALUE_REGNUM (array);
285 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
286 set_value_offset (v, value_offset (array) + elt_offs);
290 /* Check to see if either argument is a structure, or a reference to
291 one. This is called so we know whether to go ahead with the normal
292 binop or look for a user defined function instead.
294 For now, we do not overload the `=' operator. */
297 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
299 struct type *type1, *type2;
300 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
303 type1 = check_typedef (value_type (arg1));
304 if (TYPE_CODE (type1) == TYPE_CODE_REF)
305 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
307 type2 = check_typedef (value_type (arg2));
308 if (TYPE_CODE (type2) == TYPE_CODE_REF)
309 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
311 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
312 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
315 /* Check to see if argument is a structure. This is called so
316 we know whether to go ahead with the normal unop or look for a
317 user defined function instead.
319 For now, we do not overload the `&' operator. */
322 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
327 type1 = check_typedef (value_type (arg1));
330 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
332 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
333 type1 = TYPE_TARGET_TYPE (type1);
339 /* We know either arg1 or arg2 is a structure, so try to find the right
340 user defined function. Create an argument vector that calls
341 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
342 binary operator which is legal for GNU C++).
344 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
345 is the opcode saying how to modify it. Otherwise, OTHEROP is
349 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
350 enum exp_opcode otherop, enum noside noside)
352 struct value **argvec;
357 arg1 = coerce_ref (arg1);
358 arg2 = coerce_ref (arg2);
359 arg1 = coerce_enum (arg1);
360 arg2 = coerce_enum (arg2);
362 /* now we know that what we have to do is construct our
363 arg vector and find the right function to call it with. */
365 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
366 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
368 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
369 argvec[1] = value_addr (arg1);
373 /* make the right function name up */
374 strcpy (tstr, "operator__");
399 case BINOP_BITWISE_AND:
402 case BINOP_BITWISE_IOR:
405 case BINOP_BITWISE_XOR:
408 case BINOP_LOGICAL_AND:
411 case BINOP_LOGICAL_OR:
423 case BINOP_ASSIGN_MODIFY:
441 case BINOP_BITWISE_AND:
444 case BINOP_BITWISE_IOR:
447 case BINOP_BITWISE_XOR:
450 case BINOP_MOD: /* invalid */
452 error (_("Invalid binary operation specified."));
455 case BINOP_SUBSCRIPT:
476 case BINOP_MOD: /* invalid */
478 error (_("Invalid binary operation specified."));
481 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
487 argvec[1] = argvec[0];
490 if (noside == EVAL_AVOID_SIDE_EFFECTS)
492 struct type *return_type;
494 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
495 return value_zero (return_type, VALUE_LVAL (arg1));
497 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
499 error (_("member function %s not found"), tstr);
501 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
505 /* We know that arg1 is a structure, so try to find a unary user
506 defined operator that matches the operator in question.
507 Create an argument vector that calls arg1.operator @ (arg1)
508 and return that value (where '@' is (almost) any unary operator which
509 is legal for GNU C++). */
512 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
514 struct value **argvec;
515 char *ptr, *mangle_ptr;
516 char tstr[13], mangle_tstr[13];
517 int static_memfuncp, nargs;
519 arg1 = coerce_ref (arg1);
520 arg1 = coerce_enum (arg1);
522 /* now we know that what we have to do is construct our
523 arg vector and find the right function to call it with. */
525 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
526 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
528 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
529 argvec[1] = value_addr (arg1);
534 /* make the right function name up */
535 strcpy (tstr, "operator__");
537 strcpy (mangle_tstr, "__");
538 mangle_ptr = mangle_tstr + 2;
541 case UNOP_PREINCREMENT:
544 case UNOP_PREDECREMENT:
547 case UNOP_POSTINCREMENT:
549 argvec[2] = value_from_longest (builtin_type_int, 0);
553 case UNOP_POSTDECREMENT:
555 argvec[2] = value_from_longest (builtin_type_int, 0);
559 case UNOP_LOGICAL_NOT:
562 case UNOP_COMPLEMENT:
575 error (_("Invalid unary operation specified."));
578 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
584 argvec[1] = argvec[0];
588 if (noside == EVAL_AVOID_SIDE_EFFECTS)
590 struct type *return_type;
592 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
593 return value_zero (return_type, VALUE_LVAL (arg1));
595 return call_function_by_hand (argvec[0], nargs, argvec + 1);
597 error (_("member function %s not found"), tstr);
598 return 0; /* For lint -- never reached */
602 /* Concatenate two values with the following conditions:
604 (1) Both values must be either bitstring values or character string
605 values and the resulting value consists of the concatenation of
606 ARG1 followed by ARG2.
610 One value must be an integer value and the other value must be
611 either a bitstring value or character string value, which is
612 to be repeated by the number of times specified by the integer
616 (2) Boolean values are also allowed and are treated as bit string
619 (3) Character values are also allowed and are treated as character
620 string values of length 1.
624 value_concat (struct value *arg1, struct value *arg2)
626 struct value *inval1;
627 struct value *inval2;
628 struct value *outval = NULL;
629 int inval1len, inval2len;
633 struct type *type1 = check_typedef (value_type (arg1));
634 struct type *type2 = check_typedef (value_type (arg2));
636 /* First figure out if we are dealing with two values to be concatenated
637 or a repeat count and a value to be repeated. INVAL1 is set to the
638 first of two concatenated values, or the repeat count. INVAL2 is set
639 to the second of the two concatenated values or the value to be
642 if (TYPE_CODE (type2) == TYPE_CODE_INT)
644 struct type *tmp = type1;
656 /* Now process the input values. */
658 if (TYPE_CODE (type1) == TYPE_CODE_INT)
660 /* We have a repeat count. Validate the second value and then
661 construct a value repeated that many times. */
662 if (TYPE_CODE (type2) == TYPE_CODE_STRING
663 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
665 count = longest_to_int (value_as_long (inval1));
666 inval2len = TYPE_LENGTH (type2);
667 ptr = (char *) alloca (count * inval2len);
668 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
670 inchar = (char) unpack_long (type2,
671 value_contents (inval2));
672 for (idx = 0; idx < count; idx++)
674 *(ptr + idx) = inchar;
679 for (idx = 0; idx < count; idx++)
681 memcpy (ptr + (idx * inval2len), value_contents (inval2),
685 outval = value_string (ptr, count * inval2len);
687 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
688 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
690 error (_("unimplemented support for bitstring/boolean repeats"));
694 error (_("can't repeat values of that type"));
697 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
698 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
700 /* We have two character strings to concatenate. */
701 if (TYPE_CODE (type2) != TYPE_CODE_STRING
702 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
704 error (_("Strings can only be concatenated with other strings."));
706 inval1len = TYPE_LENGTH (type1);
707 inval2len = TYPE_LENGTH (type2);
708 ptr = (char *) alloca (inval1len + inval2len);
709 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
711 *ptr = (char) unpack_long (type1, value_contents (inval1));
715 memcpy (ptr, value_contents (inval1), inval1len);
717 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
720 (char) unpack_long (type2, value_contents (inval2));
724 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
726 outval = value_string (ptr, inval1len + inval2len);
728 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
729 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
731 /* We have two bitstrings to concatenate. */
732 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
733 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
735 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
737 error (_("unimplemented support for bitstring/boolean concatenation."));
741 /* We don't know how to concatenate these operands. */
742 error (_("illegal operands for concatenation."));
747 /* Return result type of OP performed on TYPE1.
748 The result type follows ANSI C rules.
749 If the result is not appropropriate for any particular language then it
750 needs to patch this function to return the correct type. */
753 unop_result_type (enum exp_opcode op, struct type *type1)
755 struct type *result_type;
757 type1 = check_typedef (type1);
765 case UNOP_COMPLEMENT:
766 /* Reject floats and decimal floats. */
767 if (!is_integral_type (type1))
768 error (_("Argument to complement operation not an integer or boolean."));
771 error (_("Invalid unary operation on numbers."));
774 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
775 || TYPE_CODE (type1) == TYPE_CODE_FLT)
779 else if (is_integral_type (type1))
781 /* Perform integral promotion for ANSI C/C++.
782 If not appropropriate for any particular language it needs to
783 modify this function to return the correct result for it. */
784 if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_type_int))
785 result_type = builtin_type_int;
791 error (_("Argument to unary operation not a number."));
792 return 0; /* For lint -- never reached */
796 /* Return result type of OP performed on TYPE1, TYPE2.
797 If the result is not appropropriate for any particular language then it
798 needs to patch this function to return the correct type. */
801 binop_result_type (enum exp_opcode op, struct type *type1, struct type *type2)
803 type1 = check_typedef (type1);
804 type2 = check_typedef (type2);
806 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
807 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
808 && !is_integral_type (type1))
810 (TYPE_CODE (type2) != TYPE_CODE_FLT
811 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
812 && !is_integral_type (type2)))
813 error (_("Argument to arithmetic operation not a number or boolean."));
815 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
816 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
827 error (_("Operation not valid for decimal floating point number."));
830 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
831 /* If type1 is not a decimal float, the type of the result is the type
832 of the decimal float argument, type2. */
834 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
835 /* Same logic, for the case where type2 is not a decimal float. */
838 /* Both are decimal floats, the type of the result is the bigger
840 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) ? type1 : type2;
842 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
843 || TYPE_CODE (type2) == TYPE_CODE_FLT)
856 error (_("Integer-only operation on floating point number."));
859 switch (current_language->la_language)
865 /* Perform ANSI/ISO-C promotions.
866 If only one type is float, use its type.
867 Otherwise use the bigger type. */
868 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
870 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
873 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) ? type1 : type2;
876 /* For other languages the result type is unchanged from gdb
877 version 6.7 for backward compatibility.
878 If either arg was long double, make sure that value is also long
879 double. Otherwise use double. */
880 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (current_gdbarch)
881 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (current_gdbarch))
882 return builtin_type_long_double;
884 return builtin_type_double;
887 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
888 && TYPE_CODE (type2) == TYPE_CODE_BOOL)
892 case BINOP_BITWISE_AND:
893 case BINOP_BITWISE_IOR:
894 case BINOP_BITWISE_XOR:
899 error (_("Invalid operation on booleans."));
905 /* Integral operations here. */
906 /* FIXME: Also mixed integral/booleans, with result an integer. */
908 unsigned int promoted_len1 = TYPE_LENGTH (type1);
909 unsigned int promoted_len2 = TYPE_LENGTH (type2);
910 int is_unsigned1 = TYPE_UNSIGNED (type1);
911 int is_unsigned2 = TYPE_UNSIGNED (type2);
912 unsigned int result_len;
913 int unsigned_operation;
915 /* Determine type length and signedness after promotion for
917 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
920 promoted_len1 = TYPE_LENGTH (builtin_type_int);
922 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
925 promoted_len2 = TYPE_LENGTH (builtin_type_int);
928 /* Determine type length of the result, and if the operation should
929 be done unsigned. For exponentiation and shift operators,
930 use the length and type of the left operand. Otherwise,
931 use the signedness of the operand with the greater length.
932 If both operands are of equal length, use unsigned operation
933 if one of the operands is unsigned. */
934 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
936 /* In case of the shift operators and exponentiation the type of
937 the result only depends on the type of the left operand. */
938 unsigned_operation = is_unsigned1;
939 result_len = promoted_len1;
941 else if (promoted_len1 > promoted_len2)
943 unsigned_operation = is_unsigned1;
944 result_len = promoted_len1;
946 else if (promoted_len2 > promoted_len1)
948 unsigned_operation = is_unsigned2;
949 result_len = promoted_len2;
953 unsigned_operation = is_unsigned1 || is_unsigned2;
954 result_len = promoted_len1;
969 case BINOP_BITWISE_AND:
970 case BINOP_BITWISE_IOR:
971 case BINOP_BITWISE_XOR:
972 case BINOP_LOGICAL_AND:
973 case BINOP_LOGICAL_OR:
982 error (_("Invalid binary operation on numbers."));
985 switch (current_language->la_language)
991 if (result_len <= TYPE_LENGTH (builtin_type_int))
993 return (unsigned_operation
994 ? builtin_type_unsigned_int
997 else if (result_len <= TYPE_LENGTH (builtin_type_long))
999 return (unsigned_operation
1000 ? builtin_type_unsigned_long
1001 : builtin_type_long);
1005 return (unsigned_operation
1006 ? builtin_type_unsigned_long_long
1007 : builtin_type_long_long);
1011 /* For other languages the result type is unchanged from gdb
1012 version 6.7 for backward compatibility.
1013 If either arg was long long, make sure that value is also long
1014 long. Otherwise use long. */
1015 if (unsigned_operation)
1017 if (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT)
1018 return builtin_type_unsigned_long_long;
1020 return builtin_type_unsigned_long;
1024 if (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT)
1025 return builtin_type_long_long;
1027 return builtin_type_long;
1032 return NULL; /* avoid -Wall warning */
1035 /* Integer exponentiation: V1**V2, where both arguments are
1036 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1038 integer_pow (LONGEST v1, LONGEST v2)
1043 error (_("Attempt to raise 0 to negative power."));
1049 /* The Russian Peasant's Algorithm */
1065 /* Integer exponentiation: V1**V2, where both arguments are
1066 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1068 uinteger_pow (ULONGEST v1, LONGEST v2)
1073 error (_("Attempt to raise 0 to negative power."));
1079 /* The Russian Peasant's Algorithm */
1095 /* Obtain decimal value of arguments for binary operation, converting from
1096 other types if one of them is not decimal floating point. */
1098 value_args_as_decimal (struct value *arg1, struct value *arg2,
1099 gdb_byte *x, int *len_x, gdb_byte *y, int *len_y)
1101 struct type *type1, *type2;
1103 type1 = check_typedef (value_type (arg1));
1104 type2 = check_typedef (value_type (arg2));
1106 /* At least one of the arguments must be of decimal float type. */
1107 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
1108 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
1110 if (TYPE_CODE (type1) == TYPE_CODE_FLT
1111 || TYPE_CODE (type2) == TYPE_CODE_FLT)
1112 /* The DFP extension to the C language does not allow mixing of
1113 * decimal float types with other float types in expressions
1114 * (see WDTR 24732, page 12). */
1115 error (_("Mixing decimal floating types with other floating types is not allowed."));
1117 /* Obtain decimal value of arg1, converting from other types
1120 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1122 *len_x = TYPE_LENGTH (type1);
1123 memcpy (x, value_contents (arg1), *len_x);
1125 else if (is_integral_type (type1))
1127 *len_x = TYPE_LENGTH (type2);
1128 decimal_from_integral (arg1, x, *len_x);
1131 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
1134 /* Obtain decimal value of arg2, converting from other types
1137 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
1139 *len_y = TYPE_LENGTH (type2);
1140 memcpy (y, value_contents (arg2), *len_y);
1142 else if (is_integral_type (type2))
1144 *len_y = TYPE_LENGTH (type1);
1145 decimal_from_integral (arg2, y, *len_y);
1148 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
1152 /* Perform a binary operation on two operands which have reasonable
1153 representations as integers or floats. This includes booleans,
1154 characters, integers, or floats.
1155 Does not support addition and subtraction on pointers;
1156 use value_add or value_sub if you want to handle those possibilities. */
1159 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1162 struct type *result_type;
1164 arg1 = coerce_ref (arg1);
1165 arg2 = coerce_ref (arg2);
1167 result_type = binop_result_type (op, value_type (arg1), value_type (arg2));
1169 if (TYPE_CODE (result_type) == TYPE_CODE_DECFLOAT)
1171 struct type *v_type;
1172 int len_v1, len_v2, len_v;
1173 gdb_byte v1[16], v2[16];
1176 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1185 decimal_binop (op, v1, len_v1, v2, len_v2, v, &len_v);
1189 error (_("Operation not valid for decimal floating point number."));
1192 val = value_from_decfloat (result_type, v);
1194 else if (TYPE_CODE (result_type) == TYPE_CODE_FLT)
1196 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1197 in target format. real.c in GCC probably has the necessary
1199 DOUBLEST v1, v2, v = 0;
1200 v1 = value_as_double (arg1);
1201 v2 = value_as_double (arg2);
1225 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
1229 v = v1 < v2 ? v1 : v2;
1233 v = v1 > v2 ? v1 : v2;
1237 error (_("Integer-only operation on floating point number."));
1240 val = allocate_value (result_type);
1241 store_typed_floating (value_contents_raw (val), value_type (val), v);
1243 else if (TYPE_CODE (result_type) == TYPE_CODE_BOOL)
1245 LONGEST v1, v2, v = 0;
1246 v1 = value_as_long (arg1);
1247 v2 = value_as_long (arg2);
1251 case BINOP_BITWISE_AND:
1255 case BINOP_BITWISE_IOR:
1259 case BINOP_BITWISE_XOR:
1267 case BINOP_NOTEQUAL:
1272 error (_("Invalid operation on booleans."));
1275 val = allocate_value (result_type);
1276 store_signed_integer (value_contents_raw (val),
1277 TYPE_LENGTH (result_type),
1281 /* Integral operations here. */
1283 int unsigned_operation = TYPE_UNSIGNED (result_type);
1285 if (unsigned_operation)
1287 unsigned int len1, len2, result_len;
1288 LONGEST v2_signed = value_as_long (arg2);
1289 ULONGEST v1, v2, v = 0;
1290 v1 = (ULONGEST) value_as_long (arg1);
1291 v2 = (ULONGEST) v2_signed;
1293 /* Truncate values to the type length of the result.
1294 Things are mildly tricky because binop_result_type may
1295 return a long which on amd64 is 8 bytes, and that's a problem if
1296 ARG1, ARG2 are both <= 4 bytes: we need to truncate the values
1297 at 4 bytes not 8. So fetch the lengths of the original types
1298 and truncate at the larger of the two. */
1299 len1 = TYPE_LENGTH (value_type (arg1));
1300 len2 = TYPE_LENGTH (value_type (arg1));
1301 result_len = len1 > len2 ? len1 : len2;
1302 if (result_len < sizeof (ULONGEST))
1304 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1305 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1327 error (_("Division by zero"));
1331 v = uinteger_pow (v1, v2_signed);
1338 error (_("Division by zero"));
1342 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1343 v1 mod 0 has a defined value, v1. */
1351 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1364 case BINOP_BITWISE_AND:
1368 case BINOP_BITWISE_IOR:
1372 case BINOP_BITWISE_XOR:
1376 case BINOP_LOGICAL_AND:
1380 case BINOP_LOGICAL_OR:
1385 v = v1 < v2 ? v1 : v2;
1389 v = v1 > v2 ? v1 : v2;
1396 case BINOP_NOTEQUAL:
1405 error (_("Invalid binary operation on numbers."));
1408 val = allocate_value (result_type);
1409 store_unsigned_integer (value_contents_raw (val),
1410 TYPE_LENGTH (value_type (val)),
1415 LONGEST v1, v2, v = 0;
1416 v1 = value_as_long (arg1);
1417 v2 = value_as_long (arg2);
1438 error (_("Division by zero"));
1442 v = integer_pow (v1, v2);
1449 error (_("Division by zero"));
1453 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1454 X mod 0 has a defined value, X. */
1462 /* Compute floor. */
1463 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1479 case BINOP_BITWISE_AND:
1483 case BINOP_BITWISE_IOR:
1487 case BINOP_BITWISE_XOR:
1491 case BINOP_LOGICAL_AND:
1495 case BINOP_LOGICAL_OR:
1500 v = v1 < v2 ? v1 : v2;
1504 v = v1 > v2 ? v1 : v2;
1516 error (_("Invalid binary operation on numbers."));
1519 val = allocate_value (result_type);
1520 store_signed_integer (value_contents_raw (val),
1521 TYPE_LENGTH (value_type (val)),
1529 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1532 value_logical_not (struct value *arg1)
1538 arg1 = coerce_number (arg1);
1539 type1 = check_typedef (value_type (arg1));
1541 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1542 return 0 == value_as_double (arg1);
1543 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1544 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1));
1546 len = TYPE_LENGTH (type1);
1547 p = value_contents (arg1);
1558 /* Perform a comparison on two string values (whose content are not
1559 necessarily null terminated) based on their length */
1562 value_strcmp (struct value *arg1, struct value *arg2)
1564 int len1 = TYPE_LENGTH (value_type (arg1));
1565 int len2 = TYPE_LENGTH (value_type (arg2));
1566 const gdb_byte *s1 = value_contents (arg1);
1567 const gdb_byte *s2 = value_contents (arg2);
1568 int i, len = len1 < len2 ? len1 : len2;
1570 for (i = 0; i < len; i++)
1574 else if (s1[i] > s2[i])
1582 else if (len1 > len2)
1588 /* Simulate the C operator == by returning a 1
1589 iff ARG1 and ARG2 have equal contents. */
1592 value_equal (struct value *arg1, struct value *arg2)
1597 struct type *type1, *type2;
1598 enum type_code code1;
1599 enum type_code code2;
1600 int is_int1, is_int2;
1602 arg1 = coerce_array (arg1);
1603 arg2 = coerce_array (arg2);
1605 type1 = check_typedef (value_type (arg1));
1606 type2 = check_typedef (value_type (arg2));
1607 code1 = TYPE_CODE (type1);
1608 code2 = TYPE_CODE (type2);
1609 is_int1 = is_integral_type (type1);
1610 is_int2 = is_integral_type (type2);
1612 if (is_int1 && is_int2)
1613 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1615 else if ((code1 == TYPE_CODE_FLT || is_int1)
1616 && (code2 == TYPE_CODE_FLT || is_int2))
1618 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1619 `long double' values are returned in static storage (m68k). */
1620 DOUBLEST d = value_as_double (arg1);
1621 return d == value_as_double (arg2);
1623 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1624 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1626 gdb_byte v1[16], v2[16];
1629 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1631 return decimal_compare (v1, len_v1, v2, len_v2) == 0;
1634 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1636 else if (code1 == TYPE_CODE_PTR && is_int2)
1637 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1638 else if (code2 == TYPE_CODE_PTR && is_int1)
1639 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1641 else if (code1 == code2
1642 && ((len = (int) TYPE_LENGTH (type1))
1643 == (int) TYPE_LENGTH (type2)))
1645 p1 = value_contents (arg1);
1646 p2 = value_contents (arg2);
1654 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1656 return value_strcmp (arg1, arg2) == 0;
1660 error (_("Invalid type combination in equality test."));
1661 return 0; /* For lint -- never reached */
1665 /* Simulate the C operator < by returning 1
1666 iff ARG1's contents are less than ARG2's. */
1669 value_less (struct value *arg1, struct value *arg2)
1671 enum type_code code1;
1672 enum type_code code2;
1673 struct type *type1, *type2;
1674 int is_int1, is_int2;
1676 arg1 = coerce_array (arg1);
1677 arg2 = coerce_array (arg2);
1679 type1 = check_typedef (value_type (arg1));
1680 type2 = check_typedef (value_type (arg2));
1681 code1 = TYPE_CODE (type1);
1682 code2 = TYPE_CODE (type2);
1683 is_int1 = is_integral_type (type1);
1684 is_int2 = is_integral_type (type2);
1686 if (is_int1 && is_int2)
1687 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1689 else if ((code1 == TYPE_CODE_FLT || is_int1)
1690 && (code2 == TYPE_CODE_FLT || is_int2))
1692 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1693 `long double' values are returned in static storage (m68k). */
1694 DOUBLEST d = value_as_double (arg1);
1695 return d < value_as_double (arg2);
1697 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1698 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1700 gdb_byte v1[16], v2[16];
1703 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1705 return decimal_compare (v1, len_v1, v2, len_v2) == -1;
1707 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1708 return value_as_address (arg1) < value_as_address (arg2);
1710 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1712 else if (code1 == TYPE_CODE_PTR && is_int2)
1713 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1714 else if (code2 == TYPE_CODE_PTR && is_int1)
1715 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1716 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1717 return value_strcmp (arg1, arg2) < 0;
1720 error (_("Invalid type combination in ordering comparison."));
1725 /* The unary operators +, - and ~. They free the argument ARG1. */
1728 value_pos (struct value *arg1)
1731 struct type *result_type;
1733 arg1 = coerce_ref (arg1);
1734 type = check_typedef (value_type (arg1));
1735 result_type = unop_result_type (UNOP_PLUS, value_type (arg1));
1737 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1738 return value_from_double (result_type, value_as_double (arg1));
1739 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1740 return value_from_decfloat (result_type, value_contents (arg1));
1741 else if (is_integral_type (type))
1743 return value_from_longest (result_type, value_as_long (arg1));
1747 error ("Argument to positive operation not a number.");
1748 return 0; /* For lint -- never reached */
1753 value_neg (struct value *arg1)
1756 struct type *result_type;
1758 arg1 = coerce_ref (arg1);
1759 type = check_typedef (value_type (arg1));
1760 result_type = unop_result_type (UNOP_NEG, value_type (arg1));
1762 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1764 struct value *val = allocate_value (result_type);
1765 int len = TYPE_LENGTH (type);
1766 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
1768 memcpy (decbytes, value_contents (arg1), len);
1770 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
1771 decbytes[len-1] = decbytes[len - 1] | 0x80;
1773 decbytes[0] = decbytes[0] | 0x80;
1775 memcpy (value_contents_raw (val), decbytes, len);
1778 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1779 return value_from_double (result_type, -value_as_double (arg1));
1780 else if (is_integral_type (type))
1782 return value_from_longest (result_type, -value_as_long (arg1));
1786 error (_("Argument to negate operation not a number."));
1787 return 0; /* For lint -- never reached */
1792 value_complement (struct value *arg1)
1795 struct type *result_type;
1797 arg1 = coerce_ref (arg1);
1798 type = check_typedef (value_type (arg1));
1799 result_type = unop_result_type (UNOP_COMPLEMENT, value_type (arg1));
1801 if (!is_integral_type (type))
1802 error (_("Argument to complement operation not an integer or boolean."));
1804 return value_from_longest (result_type, ~value_as_long (arg1));
1807 /* The INDEX'th bit of SET value whose value_type is TYPE,
1808 and whose value_contents is valaddr.
1809 Return -1 if out of range, -2 other error. */
1812 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1814 LONGEST low_bound, high_bound;
1817 struct type *range = TYPE_FIELD_TYPE (type, 0);
1818 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1820 if (index < low_bound || index > high_bound)
1822 rel_index = index - low_bound;
1823 word = unpack_long (builtin_type_unsigned_char,
1824 valaddr + (rel_index / TARGET_CHAR_BIT));
1825 rel_index %= TARGET_CHAR_BIT;
1826 if (gdbarch_bits_big_endian (current_gdbarch))
1827 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1828 return (word >> rel_index) & 1;
1832 value_in (struct value *element, struct value *set)
1835 struct type *settype = check_typedef (value_type (set));
1836 struct type *eltype = check_typedef (value_type (element));
1837 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1838 eltype = TYPE_TARGET_TYPE (eltype);
1839 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1840 error (_("Second argument of 'IN' has wrong type"));
1841 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1842 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1843 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1844 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1845 error (_("First argument of 'IN' has wrong type"));
1846 member = value_bit_index (settype, value_contents (set),
1847 value_as_long (element));
1849 error (_("First argument of 'IN' not in range"));
1850 return value_from_longest (LA_BOOL_TYPE, member);
1854 _initialize_valarith (void)