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_ptrsub & value_ptradd.
57 find_size_for_pointer_math (struct type *ptr_type)
60 struct type *ptr_target;
62 gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
63 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
65 sz = TYPE_LENGTH (ptr_target);
68 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
74 name = TYPE_NAME (ptr_target);
76 name = TYPE_TAG_NAME (ptr_target);
78 error (_("Cannot perform pointer math on incomplete types, "
79 "try casting to a known type, or void *."));
81 error (_("Cannot perform pointer math on incomplete type \"%s\", "
82 "try casting to a known type, or void *."), name);
88 /* Given a pointer ARG1 and an integral value ARG2, return the
89 result of C-style pointer arithmetic ARG1 + ARG2. */
92 value_ptradd (struct value *arg1, struct value *arg2)
94 struct type *valptrtype;
97 arg1 = coerce_array (arg1);
98 valptrtype = check_typedef (value_type (arg1));
99 sz = find_size_for_pointer_math (valptrtype);
101 if (!is_integral_type (value_type (arg2)))
102 error (_("Argument to arithmetic operation not a number or boolean."));
104 return value_from_pointer (valptrtype,
105 value_as_address (arg1)
106 + (sz * value_as_long (arg2)));
109 /* Given a pointer ARG1 and an integral value ARG2, return the
110 result of C-style pointer arithmetic ARG1 - ARG2. */
113 value_ptrsub (struct value *arg1, struct value *arg2)
115 struct type *valptrtype;
118 arg1 = coerce_array (arg1);
119 valptrtype = check_typedef (value_type (arg1));
120 sz = find_size_for_pointer_math (valptrtype);
122 if (!is_integral_type (value_type (arg2)))
123 error (_("Argument to arithmetic operation not a number or boolean."));
125 return value_from_pointer (valptrtype,
126 value_as_address (arg1)
127 - (sz * value_as_long (arg2)));
130 /* Given two compatible pointer values ARG1 and ARG2, return the
131 result of C-style pointer arithmetic ARG1 - ARG2. */
134 value_ptrdiff (struct value *arg1, struct value *arg2)
136 struct type *type1, *type2;
139 arg1 = coerce_array (arg1);
140 arg2 = coerce_array (arg2);
141 type1 = check_typedef (value_type (arg1));
142 type2 = check_typedef (value_type (arg2));
144 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
145 gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
147 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
148 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
150 First argument of `-' is a pointer and second argument is neither\n\
151 an integer nor a pointer of the same type."));
153 sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
154 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
157 /* Return the value of ARRAY[IDX].
159 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
160 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
161 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
163 See comments in value_coerce_array() for rationale for reason for
164 doing lower bounds adjustment here rather than there.
165 FIXME: Perhaps we should validate that the index is valid and if
166 verbosity is set, warn about invalid indices (but still use them). */
169 value_subscript (struct value *array, struct value *idx)
172 int c_style = current_language->c_style_arrays;
175 array = coerce_ref (array);
176 tarray = check_typedef (value_type (array));
178 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
179 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
181 struct type *range_type = TYPE_INDEX_TYPE (tarray);
182 LONGEST lowerbound, upperbound;
183 get_discrete_bounds (range_type, &lowerbound, &upperbound);
185 if (VALUE_LVAL (array) != lval_memory)
186 return value_subscripted_rvalue (array, idx, lowerbound);
190 LONGEST index = value_as_long (idx);
191 if (index >= lowerbound && index <= upperbound)
192 return value_subscripted_rvalue (array, idx, lowerbound);
193 /* Emit warning unless we have an array of unknown size.
194 An array of unknown size has lowerbound 0 and upperbound -1. */
196 warning (_("array or string index out of range"));
197 /* fall doing C stuff */
203 bound = value_from_longest (value_type (idx), (LONGEST) lowerbound);
204 idx = value_binop (idx, bound, BINOP_SUB);
207 array = value_coerce_array (array);
211 return value_ind (value_ptradd (array, idx));
213 error (_("not an array or string"));
216 /* Return the value of EXPR[IDX], expr an aggregate rvalue
217 (eg, a vector register). This routine used to promote floats
218 to doubles, but no longer does. */
221 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
223 struct type *array_type = check_typedef (value_type (array));
224 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
225 unsigned int elt_size = TYPE_LENGTH (elt_type);
226 LONGEST index = value_as_long (idx);
227 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
230 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
231 error (_("no such vector element"));
233 v = allocate_value (elt_type);
234 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
235 set_value_lazy (v, 1);
237 memcpy (value_contents_writeable (v),
238 value_contents (array) + elt_offs, elt_size);
240 if (VALUE_LVAL (array) == lval_internalvar)
241 VALUE_LVAL (v) = lval_internalvar_component;
243 VALUE_LVAL (v) = VALUE_LVAL (array);
244 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
245 VALUE_REGNUM (v) = VALUE_REGNUM (array);
246 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
247 set_value_offset (v, value_offset (array) + elt_offs);
251 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
254 value_bitstring_subscript (struct type *type,
255 struct value *bitstring, struct value *idx)
258 struct type *bitstring_type, *range_type;
259 LONGEST index = value_as_long (idx);
261 int offset, byte, bit_index;
262 LONGEST lowerbound, upperbound;
264 bitstring_type = check_typedef (value_type (bitstring));
265 gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
267 range_type = TYPE_INDEX_TYPE (bitstring_type);
268 get_discrete_bounds (range_type, &lowerbound, &upperbound);
269 if (index < lowerbound || index > upperbound)
270 error (_("bitstring index out of range"));
273 offset = index / TARGET_CHAR_BIT;
274 byte = *((char *) value_contents (bitstring) + offset);
276 bit_index = index % TARGET_CHAR_BIT;
277 byte >>= (gdbarch_bits_big_endian (current_gdbarch) ?
278 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
280 v = value_from_longest (type, byte & 1);
282 set_value_bitpos (v, bit_index);
283 set_value_bitsize (v, 1);
285 VALUE_LVAL (v) = VALUE_LVAL (bitstring);
286 if (VALUE_LVAL (bitstring) == lval_internalvar)
287 VALUE_LVAL (v) = lval_internalvar_component;
288 VALUE_ADDRESS (v) = VALUE_ADDRESS (bitstring);
289 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
291 set_value_offset (v, offset + value_offset (bitstring));
297 /* Check to see if either argument is a structure, or a reference to
298 one. This is called so we know whether to go ahead with the normal
299 binop or look for a user defined function instead.
301 For now, we do not overload the `=' operator. */
304 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
306 struct type *type1, *type2;
307 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
310 type1 = check_typedef (value_type (arg1));
311 if (TYPE_CODE (type1) == TYPE_CODE_REF)
312 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
314 type2 = check_typedef (value_type (arg2));
315 if (TYPE_CODE (type2) == TYPE_CODE_REF)
316 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
318 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
319 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
322 /* Check to see if argument is a structure. This is called so
323 we know whether to go ahead with the normal unop or look for a
324 user defined function instead.
326 For now, we do not overload the `&' operator. */
329 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
334 type1 = check_typedef (value_type (arg1));
337 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
339 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
340 type1 = TYPE_TARGET_TYPE (type1);
346 /* We know either arg1 or arg2 is a structure, so try to find the right
347 user defined function. Create an argument vector that calls
348 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
349 binary operator which is legal for GNU C++).
351 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
352 is the opcode saying how to modify it. Otherwise, OTHEROP is
356 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
357 enum exp_opcode otherop, enum noside noside)
359 struct value **argvec;
364 arg1 = coerce_ref (arg1);
365 arg2 = coerce_ref (arg2);
366 arg1 = coerce_enum (arg1);
367 arg2 = coerce_enum (arg2);
369 /* now we know that what we have to do is construct our
370 arg vector and find the right function to call it with. */
372 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
373 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
375 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
376 argvec[1] = value_addr (arg1);
380 /* make the right function name up */
381 strcpy (tstr, "operator__");
406 case BINOP_BITWISE_AND:
409 case BINOP_BITWISE_IOR:
412 case BINOP_BITWISE_XOR:
415 case BINOP_LOGICAL_AND:
418 case BINOP_LOGICAL_OR:
430 case BINOP_ASSIGN_MODIFY:
448 case BINOP_BITWISE_AND:
451 case BINOP_BITWISE_IOR:
454 case BINOP_BITWISE_XOR:
457 case BINOP_MOD: /* invalid */
459 error (_("Invalid binary operation specified."));
462 case BINOP_SUBSCRIPT:
483 case BINOP_MOD: /* invalid */
485 error (_("Invalid binary operation specified."));
488 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
494 argvec[1] = argvec[0];
497 if (noside == EVAL_AVOID_SIDE_EFFECTS)
499 struct type *return_type;
501 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
502 return value_zero (return_type, VALUE_LVAL (arg1));
504 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
506 error (_("member function %s not found"), tstr);
508 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
512 /* We know that arg1 is a structure, so try to find a unary user
513 defined operator that matches the operator in question.
514 Create an argument vector that calls arg1.operator @ (arg1)
515 and return that value (where '@' is (almost) any unary operator which
516 is legal for GNU C++). */
519 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
521 struct value **argvec;
522 char *ptr, *mangle_ptr;
523 char tstr[13], mangle_tstr[13];
524 int static_memfuncp, nargs;
526 arg1 = coerce_ref (arg1);
527 arg1 = coerce_enum (arg1);
529 /* now we know that what we have to do is construct our
530 arg vector and find the right function to call it with. */
532 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
533 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
535 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
536 argvec[1] = value_addr (arg1);
541 /* make the right function name up */
542 strcpy (tstr, "operator__");
544 strcpy (mangle_tstr, "__");
545 mangle_ptr = mangle_tstr + 2;
548 case UNOP_PREINCREMENT:
551 case UNOP_PREDECREMENT:
554 case UNOP_POSTINCREMENT:
556 argvec[2] = value_from_longest (builtin_type_int, 0);
560 case UNOP_POSTDECREMENT:
562 argvec[2] = value_from_longest (builtin_type_int, 0);
566 case UNOP_LOGICAL_NOT:
569 case UNOP_COMPLEMENT:
582 error (_("Invalid unary operation specified."));
585 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
591 argvec[1] = argvec[0];
595 if (noside == EVAL_AVOID_SIDE_EFFECTS)
597 struct type *return_type;
599 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
600 return value_zero (return_type, VALUE_LVAL (arg1));
602 return call_function_by_hand (argvec[0], nargs, argvec + 1);
604 error (_("member function %s not found"), tstr);
605 return 0; /* For lint -- never reached */
609 /* Concatenate two values with the following conditions:
611 (1) Both values must be either bitstring values or character string
612 values and the resulting value consists of the concatenation of
613 ARG1 followed by ARG2.
617 One value must be an integer value and the other value must be
618 either a bitstring value or character string value, which is
619 to be repeated by the number of times specified by the integer
623 (2) Boolean values are also allowed and are treated as bit string
626 (3) Character values are also allowed and are treated as character
627 string values of length 1.
631 value_concat (struct value *arg1, struct value *arg2)
633 struct value *inval1;
634 struct value *inval2;
635 struct value *outval = NULL;
636 int inval1len, inval2len;
640 struct type *type1 = check_typedef (value_type (arg1));
641 struct type *type2 = check_typedef (value_type (arg2));
643 /* First figure out if we are dealing with two values to be concatenated
644 or a repeat count and a value to be repeated. INVAL1 is set to the
645 first of two concatenated values, or the repeat count. INVAL2 is set
646 to the second of the two concatenated values or the value to be
649 if (TYPE_CODE (type2) == TYPE_CODE_INT)
651 struct type *tmp = type1;
663 /* Now process the input values. */
665 if (TYPE_CODE (type1) == TYPE_CODE_INT)
667 /* We have a repeat count. Validate the second value and then
668 construct a value repeated that many times. */
669 if (TYPE_CODE (type2) == TYPE_CODE_STRING
670 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
672 count = longest_to_int (value_as_long (inval1));
673 inval2len = TYPE_LENGTH (type2);
674 ptr = (char *) alloca (count * inval2len);
675 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
677 inchar = (char) unpack_long (type2,
678 value_contents (inval2));
679 for (idx = 0; idx < count; idx++)
681 *(ptr + idx) = inchar;
686 for (idx = 0; idx < count; idx++)
688 memcpy (ptr + (idx * inval2len), value_contents (inval2),
692 outval = value_string (ptr, count * inval2len);
694 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
695 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
697 error (_("unimplemented support for bitstring/boolean repeats"));
701 error (_("can't repeat values of that type"));
704 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
705 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
707 /* We have two character strings to concatenate. */
708 if (TYPE_CODE (type2) != TYPE_CODE_STRING
709 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
711 error (_("Strings can only be concatenated with other strings."));
713 inval1len = TYPE_LENGTH (type1);
714 inval2len = TYPE_LENGTH (type2);
715 ptr = (char *) alloca (inval1len + inval2len);
716 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
718 *ptr = (char) unpack_long (type1, value_contents (inval1));
722 memcpy (ptr, value_contents (inval1), inval1len);
724 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
727 (char) unpack_long (type2, value_contents (inval2));
731 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
733 outval = value_string (ptr, inval1len + inval2len);
735 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
736 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
738 /* We have two bitstrings to concatenate. */
739 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
740 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
742 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
744 error (_("unimplemented support for bitstring/boolean concatenation."));
748 /* We don't know how to concatenate these operands. */
749 error (_("illegal operands for concatenation."));
754 /* Return result type of OP performed on TYPE1.
755 The result type follows ANSI C rules.
756 If the result is not appropropriate for any particular language then it
757 needs to patch this function to return the correct type. */
760 unop_result_type (enum exp_opcode op, struct type *type1)
762 struct type *result_type;
764 type1 = check_typedef (type1);
772 case UNOP_COMPLEMENT:
773 /* Reject floats and decimal floats. */
774 if (!is_integral_type (type1))
775 error (_("Argument to complement operation not an integer or boolean."));
778 error (_("Invalid unary operation on numbers."));
781 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
782 || TYPE_CODE (type1) == TYPE_CODE_FLT)
786 else if (is_integral_type (type1))
788 /* Perform integral promotion for ANSI C/C++.
789 If not appropropriate for any particular language it needs to
790 modify this function to return the correct result for it. */
791 if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_type_int))
792 result_type = builtin_type_int;
798 error (_("Argument to unary operation not a number."));
799 return 0; /* For lint -- never reached */
803 /* Return result type of OP performed on TYPE1, TYPE2.
804 If the result is not appropropriate for any particular language then it
805 needs to patch this function to return the correct type. */
808 binop_result_type (enum exp_opcode op, struct type *type1, struct type *type2)
810 type1 = check_typedef (type1);
811 type2 = check_typedef (type2);
813 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
814 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
815 && !is_integral_type (type1))
817 (TYPE_CODE (type2) != TYPE_CODE_FLT
818 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
819 && !is_integral_type (type2)))
820 error (_("Argument to arithmetic operation not a number or boolean."));
822 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
823 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
834 error (_("Operation not valid for decimal floating point number."));
837 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
838 /* If type1 is not a decimal float, the type of the result is the type
839 of the decimal float argument, type2. */
841 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
842 /* Same logic, for the case where type2 is not a decimal float. */
845 /* Both are decimal floats, the type of the result is the bigger
847 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) ? type1 : type2;
849 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
850 || TYPE_CODE (type2) == TYPE_CODE_FLT)
863 error (_("Integer-only operation on floating point number."));
866 switch (current_language->la_language)
872 /* Perform ANSI/ISO-C promotions.
873 If only one type is float, use its type.
874 Otherwise use the bigger type. */
875 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
877 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
880 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) ? type1 : type2;
883 /* For other languages the result type is unchanged from gdb
884 version 6.7 for backward compatibility.
885 If either arg was long double, make sure that value is also long
886 double. Otherwise use double. */
887 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (current_gdbarch)
888 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (current_gdbarch))
889 return builtin_type_long_double;
891 return builtin_type_double;
894 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
895 && TYPE_CODE (type2) == TYPE_CODE_BOOL)
899 case BINOP_BITWISE_AND:
900 case BINOP_BITWISE_IOR:
901 case BINOP_BITWISE_XOR:
906 error (_("Invalid operation on booleans."));
912 /* Integral operations here. */
913 /* FIXME: Also mixed integral/booleans, with result an integer. */
915 unsigned int promoted_len1 = TYPE_LENGTH (type1);
916 unsigned int promoted_len2 = TYPE_LENGTH (type2);
917 int is_unsigned1 = TYPE_UNSIGNED (type1);
918 int is_unsigned2 = TYPE_UNSIGNED (type2);
919 unsigned int result_len;
920 int unsigned_operation;
922 /* Determine type length and signedness after promotion for
924 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
927 promoted_len1 = TYPE_LENGTH (builtin_type_int);
929 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
932 promoted_len2 = TYPE_LENGTH (builtin_type_int);
935 /* Determine type length of the result, and if the operation should
936 be done unsigned. For exponentiation and shift operators,
937 use the length and type of the left operand. Otherwise,
938 use the signedness of the operand with the greater length.
939 If both operands are of equal length, use unsigned operation
940 if one of the operands is unsigned. */
941 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
943 /* In case of the shift operators and exponentiation the type of
944 the result only depends on the type of the left operand. */
945 unsigned_operation = is_unsigned1;
946 result_len = promoted_len1;
948 else if (promoted_len1 > promoted_len2)
950 unsigned_operation = is_unsigned1;
951 result_len = promoted_len1;
953 else if (promoted_len2 > promoted_len1)
955 unsigned_operation = is_unsigned2;
956 result_len = promoted_len2;
960 unsigned_operation = is_unsigned1 || is_unsigned2;
961 result_len = promoted_len1;
976 case BINOP_BITWISE_AND:
977 case BINOP_BITWISE_IOR:
978 case BINOP_BITWISE_XOR:
979 case BINOP_LOGICAL_AND:
980 case BINOP_LOGICAL_OR:
989 error (_("Invalid binary operation on numbers."));
992 switch (current_language->la_language)
998 if (result_len <= TYPE_LENGTH (builtin_type_int))
1000 return (unsigned_operation
1001 ? builtin_type_unsigned_int
1002 : builtin_type_int);
1004 else if (result_len <= TYPE_LENGTH (builtin_type_long))
1006 return (unsigned_operation
1007 ? builtin_type_unsigned_long
1008 : builtin_type_long);
1012 return (unsigned_operation
1013 ? builtin_type_unsigned_long_long
1014 : builtin_type_long_long);
1018 /* For other languages the result type is unchanged from gdb
1019 version 6.7 for backward compatibility.
1020 If either arg was long long, make sure that value is also long
1021 long. Otherwise use long. */
1022 if (unsigned_operation)
1024 if (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT)
1025 return builtin_type_unsigned_long_long;
1027 return builtin_type_unsigned_long;
1031 if (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT)
1032 return builtin_type_long_long;
1034 return builtin_type_long;
1039 return NULL; /* avoid -Wall warning */
1042 /* Integer exponentiation: V1**V2, where both arguments are
1043 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1045 integer_pow (LONGEST v1, LONGEST v2)
1050 error (_("Attempt to raise 0 to negative power."));
1056 /* The Russian Peasant's Algorithm */
1072 /* Integer exponentiation: V1**V2, where both arguments are
1073 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1075 uinteger_pow (ULONGEST v1, LONGEST v2)
1080 error (_("Attempt to raise 0 to negative power."));
1086 /* The Russian Peasant's Algorithm */
1102 /* Obtain decimal value of arguments for binary operation, converting from
1103 other types if one of them is not decimal floating point. */
1105 value_args_as_decimal (struct value *arg1, struct value *arg2,
1106 gdb_byte *x, int *len_x, gdb_byte *y, int *len_y)
1108 struct type *type1, *type2;
1110 type1 = check_typedef (value_type (arg1));
1111 type2 = check_typedef (value_type (arg2));
1113 /* At least one of the arguments must be of decimal float type. */
1114 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
1115 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
1117 if (TYPE_CODE (type1) == TYPE_CODE_FLT
1118 || TYPE_CODE (type2) == TYPE_CODE_FLT)
1119 /* The DFP extension to the C language does not allow mixing of
1120 * decimal float types with other float types in expressions
1121 * (see WDTR 24732, page 12). */
1122 error (_("Mixing decimal floating types with other floating types is not allowed."));
1124 /* Obtain decimal value of arg1, converting from other types
1127 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1129 *len_x = TYPE_LENGTH (type1);
1130 memcpy (x, value_contents (arg1), *len_x);
1132 else if (is_integral_type (type1))
1134 *len_x = TYPE_LENGTH (type2);
1135 decimal_from_integral (arg1, x, *len_x);
1138 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
1141 /* Obtain decimal value of arg2, converting from other types
1144 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
1146 *len_y = TYPE_LENGTH (type2);
1147 memcpy (y, value_contents (arg2), *len_y);
1149 else if (is_integral_type (type2))
1151 *len_y = TYPE_LENGTH (type1);
1152 decimal_from_integral (arg2, y, *len_y);
1155 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
1159 /* Perform a binary operation on two operands which have reasonable
1160 representations as integers or floats. This includes booleans,
1161 characters, integers, or floats.
1162 Does not support addition and subtraction on pointers;
1163 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1166 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1169 struct type *result_type;
1171 arg1 = coerce_ref (arg1);
1172 arg2 = coerce_ref (arg2);
1174 result_type = binop_result_type (op, value_type (arg1), value_type (arg2));
1176 if (TYPE_CODE (result_type) == TYPE_CODE_DECFLOAT)
1178 struct type *v_type;
1179 int len_v1, len_v2, len_v;
1180 gdb_byte v1[16], v2[16];
1183 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1192 decimal_binop (op, v1, len_v1, v2, len_v2, v, &len_v);
1196 error (_("Operation not valid for decimal floating point number."));
1199 val = value_from_decfloat (result_type, v);
1201 else if (TYPE_CODE (result_type) == TYPE_CODE_FLT)
1203 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1204 in target format. real.c in GCC probably has the necessary
1206 DOUBLEST v1, v2, v = 0;
1207 v1 = value_as_double (arg1);
1208 v2 = value_as_double (arg2);
1232 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
1236 v = v1 < v2 ? v1 : v2;
1240 v = v1 > v2 ? v1 : v2;
1244 error (_("Integer-only operation on floating point number."));
1247 val = allocate_value (result_type);
1248 store_typed_floating (value_contents_raw (val), value_type (val), v);
1250 else if (TYPE_CODE (result_type) == TYPE_CODE_BOOL)
1252 LONGEST v1, v2, v = 0;
1253 v1 = value_as_long (arg1);
1254 v2 = value_as_long (arg2);
1258 case BINOP_BITWISE_AND:
1262 case BINOP_BITWISE_IOR:
1266 case BINOP_BITWISE_XOR:
1274 case BINOP_NOTEQUAL:
1279 error (_("Invalid operation on booleans."));
1282 val = allocate_value (result_type);
1283 store_signed_integer (value_contents_raw (val),
1284 TYPE_LENGTH (result_type),
1288 /* Integral operations here. */
1290 int unsigned_operation = TYPE_UNSIGNED (result_type);
1292 if (unsigned_operation)
1294 unsigned int len1, len2, result_len;
1295 LONGEST v2_signed = value_as_long (arg2);
1296 ULONGEST v1, v2, v = 0;
1297 v1 = (ULONGEST) value_as_long (arg1);
1298 v2 = (ULONGEST) v2_signed;
1300 /* Truncate values to the type length of the result.
1301 Things are mildly tricky because binop_result_type may
1302 return a long which on amd64 is 8 bytes, and that's a problem if
1303 ARG1, ARG2 are both <= 4 bytes: we need to truncate the values
1304 at 4 bytes not 8. So fetch the lengths of the original types
1305 and truncate at the larger of the two. */
1306 len1 = TYPE_LENGTH (value_type (arg1));
1307 len2 = TYPE_LENGTH (value_type (arg1));
1308 result_len = len1 > len2 ? len1 : len2;
1309 if (result_len < sizeof (ULONGEST))
1311 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1312 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1334 error (_("Division by zero"));
1338 v = uinteger_pow (v1, v2_signed);
1345 error (_("Division by zero"));
1349 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1350 v1 mod 0 has a defined value, v1. */
1358 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1371 case BINOP_BITWISE_AND:
1375 case BINOP_BITWISE_IOR:
1379 case BINOP_BITWISE_XOR:
1383 case BINOP_LOGICAL_AND:
1387 case BINOP_LOGICAL_OR:
1392 v = v1 < v2 ? v1 : v2;
1396 v = v1 > v2 ? v1 : v2;
1403 case BINOP_NOTEQUAL:
1412 error (_("Invalid binary operation on numbers."));
1415 val = allocate_value (result_type);
1416 store_unsigned_integer (value_contents_raw (val),
1417 TYPE_LENGTH (value_type (val)),
1422 LONGEST v1, v2, v = 0;
1423 v1 = value_as_long (arg1);
1424 v2 = value_as_long (arg2);
1445 error (_("Division by zero"));
1449 v = integer_pow (v1, v2);
1456 error (_("Division by zero"));
1460 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1461 X mod 0 has a defined value, X. */
1469 /* Compute floor. */
1470 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1486 case BINOP_BITWISE_AND:
1490 case BINOP_BITWISE_IOR:
1494 case BINOP_BITWISE_XOR:
1498 case BINOP_LOGICAL_AND:
1502 case BINOP_LOGICAL_OR:
1507 v = v1 < v2 ? v1 : v2;
1511 v = v1 > v2 ? v1 : v2;
1523 error (_("Invalid binary operation on numbers."));
1526 val = allocate_value (result_type);
1527 store_signed_integer (value_contents_raw (val),
1528 TYPE_LENGTH (value_type (val)),
1536 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1539 value_logical_not (struct value *arg1)
1545 arg1 = coerce_number (arg1);
1546 type1 = check_typedef (value_type (arg1));
1548 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1549 return 0 == value_as_double (arg1);
1550 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1551 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1));
1553 len = TYPE_LENGTH (type1);
1554 p = value_contents (arg1);
1565 /* Perform a comparison on two string values (whose content are not
1566 necessarily null terminated) based on their length */
1569 value_strcmp (struct value *arg1, struct value *arg2)
1571 int len1 = TYPE_LENGTH (value_type (arg1));
1572 int len2 = TYPE_LENGTH (value_type (arg2));
1573 const gdb_byte *s1 = value_contents (arg1);
1574 const gdb_byte *s2 = value_contents (arg2);
1575 int i, len = len1 < len2 ? len1 : len2;
1577 for (i = 0; i < len; i++)
1581 else if (s1[i] > s2[i])
1589 else if (len1 > len2)
1595 /* Simulate the C operator == by returning a 1
1596 iff ARG1 and ARG2 have equal contents. */
1599 value_equal (struct value *arg1, struct value *arg2)
1604 struct type *type1, *type2;
1605 enum type_code code1;
1606 enum type_code code2;
1607 int is_int1, is_int2;
1609 arg1 = coerce_array (arg1);
1610 arg2 = coerce_array (arg2);
1612 type1 = check_typedef (value_type (arg1));
1613 type2 = check_typedef (value_type (arg2));
1614 code1 = TYPE_CODE (type1);
1615 code2 = TYPE_CODE (type2);
1616 is_int1 = is_integral_type (type1);
1617 is_int2 = is_integral_type (type2);
1619 if (is_int1 && is_int2)
1620 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1622 else if ((code1 == TYPE_CODE_FLT || is_int1)
1623 && (code2 == TYPE_CODE_FLT || is_int2))
1625 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1626 `long double' values are returned in static storage (m68k). */
1627 DOUBLEST d = value_as_double (arg1);
1628 return d == value_as_double (arg2);
1630 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1631 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1633 gdb_byte v1[16], v2[16];
1636 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1638 return decimal_compare (v1, len_v1, v2, len_v2) == 0;
1641 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1643 else if (code1 == TYPE_CODE_PTR && is_int2)
1644 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1645 else if (code2 == TYPE_CODE_PTR && is_int1)
1646 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1648 else if (code1 == code2
1649 && ((len = (int) TYPE_LENGTH (type1))
1650 == (int) TYPE_LENGTH (type2)))
1652 p1 = value_contents (arg1);
1653 p2 = value_contents (arg2);
1661 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1663 return value_strcmp (arg1, arg2) == 0;
1667 error (_("Invalid type combination in equality test."));
1668 return 0; /* For lint -- never reached */
1672 /* Simulate the C operator < by returning 1
1673 iff ARG1's contents are less than ARG2's. */
1676 value_less (struct value *arg1, struct value *arg2)
1678 enum type_code code1;
1679 enum type_code code2;
1680 struct type *type1, *type2;
1681 int is_int1, is_int2;
1683 arg1 = coerce_array (arg1);
1684 arg2 = coerce_array (arg2);
1686 type1 = check_typedef (value_type (arg1));
1687 type2 = check_typedef (value_type (arg2));
1688 code1 = TYPE_CODE (type1);
1689 code2 = TYPE_CODE (type2);
1690 is_int1 = is_integral_type (type1);
1691 is_int2 = is_integral_type (type2);
1693 if (is_int1 && is_int2)
1694 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1696 else if ((code1 == TYPE_CODE_FLT || is_int1)
1697 && (code2 == TYPE_CODE_FLT || is_int2))
1699 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1700 `long double' values are returned in static storage (m68k). */
1701 DOUBLEST d = value_as_double (arg1);
1702 return d < value_as_double (arg2);
1704 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1705 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1707 gdb_byte v1[16], v2[16];
1710 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1712 return decimal_compare (v1, len_v1, v2, len_v2) == -1;
1714 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1715 return value_as_address (arg1) < value_as_address (arg2);
1717 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1719 else if (code1 == TYPE_CODE_PTR && is_int2)
1720 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1721 else if (code2 == TYPE_CODE_PTR && is_int1)
1722 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1723 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1724 return value_strcmp (arg1, arg2) < 0;
1727 error (_("Invalid type combination in ordering comparison."));
1732 /* The unary operators +, - and ~. They free the argument ARG1. */
1735 value_pos (struct value *arg1)
1738 struct type *result_type;
1740 arg1 = coerce_ref (arg1);
1741 type = check_typedef (value_type (arg1));
1742 result_type = unop_result_type (UNOP_PLUS, value_type (arg1));
1744 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1745 return value_from_double (result_type, value_as_double (arg1));
1746 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1747 return value_from_decfloat (result_type, value_contents (arg1));
1748 else if (is_integral_type (type))
1750 return value_from_longest (result_type, value_as_long (arg1));
1754 error ("Argument to positive operation not a number.");
1755 return 0; /* For lint -- never reached */
1760 value_neg (struct value *arg1)
1763 struct type *result_type;
1765 arg1 = coerce_ref (arg1);
1766 type = check_typedef (value_type (arg1));
1767 result_type = unop_result_type (UNOP_NEG, value_type (arg1));
1769 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1771 struct value *val = allocate_value (result_type);
1772 int len = TYPE_LENGTH (type);
1773 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
1775 memcpy (decbytes, value_contents (arg1), len);
1777 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
1778 decbytes[len-1] = decbytes[len - 1] | 0x80;
1780 decbytes[0] = decbytes[0] | 0x80;
1782 memcpy (value_contents_raw (val), decbytes, len);
1785 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1786 return value_from_double (result_type, -value_as_double (arg1));
1787 else if (is_integral_type (type))
1789 return value_from_longest (result_type, -value_as_long (arg1));
1793 error (_("Argument to negate operation not a number."));
1794 return 0; /* For lint -- never reached */
1799 value_complement (struct value *arg1)
1802 struct type *result_type;
1804 arg1 = coerce_ref (arg1);
1805 type = check_typedef (value_type (arg1));
1806 result_type = unop_result_type (UNOP_COMPLEMENT, value_type (arg1));
1808 if (!is_integral_type (type))
1809 error (_("Argument to complement operation not an integer or boolean."));
1811 return value_from_longest (result_type, ~value_as_long (arg1));
1814 /* The INDEX'th bit of SET value whose value_type is TYPE,
1815 and whose value_contents is valaddr.
1816 Return -1 if out of range, -2 other error. */
1819 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1821 LONGEST low_bound, high_bound;
1824 struct type *range = TYPE_FIELD_TYPE (type, 0);
1825 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1827 if (index < low_bound || index > high_bound)
1829 rel_index = index - low_bound;
1830 word = unpack_long (builtin_type_unsigned_char,
1831 valaddr + (rel_index / TARGET_CHAR_BIT));
1832 rel_index %= TARGET_CHAR_BIT;
1833 if (gdbarch_bits_big_endian (current_gdbarch))
1834 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1835 return (word >> rel_index) & 1;
1839 value_in (struct value *element, struct value *set)
1842 struct type *settype = check_typedef (value_type (set));
1843 struct type *eltype = check_typedef (value_type (element));
1844 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1845 eltype = TYPE_TARGET_TYPE (eltype);
1846 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1847 error (_("Second argument of 'IN' has wrong type"));
1848 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1849 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1850 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1851 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1852 error (_("First argument of 'IN' has wrong type"));
1853 member = value_bit_index (settype, value_contents (set),
1854 value_as_long (element));
1856 error (_("First argument of 'IN' not in range"));
1861 _initialize_valarith (void)