1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
28 #include "expression.h"
31 #include "gdb_string.h"
36 /* Define whether or not the C operator '/' truncates towards zero for
37 differently signed operands (truncation direction is undefined in C). */
39 #ifndef TRUNCATION_TOWARDS_ZERO
40 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
43 static struct value *value_subscripted_rvalue (struct value *, struct value *, int);
45 void _initialize_valarith (void);
48 /* Given a pointer, return the size of its target.
49 If the pointer type is void *, then return 1.
50 If the target type is incomplete, then error out.
51 This isn't a general purpose function, but just a
52 helper for value_sub & value_add.
56 find_size_for_pointer_math (struct type *ptr_type)
59 struct type *ptr_target;
61 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
63 sz = TYPE_LENGTH (ptr_target);
66 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
72 name = TYPE_NAME (ptr_target);
74 name = TYPE_TAG_NAME (ptr_target);
76 error ("Cannot perform pointer math on incomplete types, "
77 "try casting to a known type, or void *.");
79 error ("Cannot perform pointer math on incomplete type \"%s\", "
80 "try casting to a known type, or void *.", name);
87 value_add (struct value *arg1, struct value *arg2)
92 struct type *type1, *type2, *valptrtype;
96 type1 = check_typedef (VALUE_TYPE (arg1));
97 type2 = check_typedef (VALUE_TYPE (arg2));
99 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
100 || TYPE_CODE (type2) == TYPE_CODE_PTR)
102 (TYPE_CODE (type1) == TYPE_CODE_INT
103 || TYPE_CODE (type2) == TYPE_CODE_INT))
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)));
126 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
130 return value_binop (arg1, arg2, BINOP_ADD);
134 value_sub (struct value *arg1, struct value *arg2)
136 struct type *type1, *type2;
137 COERCE_NUMBER (arg1);
138 COERCE_NUMBER (arg2);
139 type1 = check_typedef (VALUE_TYPE (arg1));
140 type2 = check_typedef (VALUE_TYPE (arg2));
142 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
144 if (TYPE_CODE (type2) == TYPE_CODE_INT)
146 /* pointer - integer. */
147 LONGEST sz = find_size_for_pointer_math (type1);
149 return value_from_pointer (type1,
150 (value_as_address (arg1)
151 - (sz * value_as_long (arg2))));
153 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
154 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
155 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
157 /* pointer to <type x> - pointer to <type x>. */
158 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
159 return value_from_longest
160 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
161 (value_as_long (arg1) - value_as_long (arg2)) / sz);
166 First argument of `-' is a pointer and second argument is neither\n\
167 an integer nor a pointer of the same type.");
171 return value_binop (arg1, arg2, BINOP_SUB);
174 /* Return the value of ARRAY[IDX].
175 See comments in value_coerce_array() for rationale for reason for
176 doing lower bounds adjustment here rather than there.
177 FIXME: Perhaps we should validate that the index is valid and if
178 verbosity is set, warn about invalid indices (but still use them). */
181 value_subscript (struct value *array, struct value *idx)
184 int c_style = current_language->c_style_arrays;
188 tarray = check_typedef (VALUE_TYPE (array));
189 COERCE_VARYING_ARRAY (array, tarray);
191 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
192 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
194 struct type *range_type = TYPE_INDEX_TYPE (tarray);
195 LONGEST lowerbound, upperbound;
196 get_discrete_bounds (range_type, &lowerbound, &upperbound);
198 if (VALUE_LVAL (array) != lval_memory)
199 return value_subscripted_rvalue (array, idx, lowerbound);
203 LONGEST index = value_as_long (idx);
204 if (index >= lowerbound && index <= upperbound)
205 return value_subscripted_rvalue (array, idx, lowerbound);
206 warning ("array or string index out of range");
207 /* fall doing C stuff */
213 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
214 idx = value_sub (idx, bound);
217 array = value_coerce_array (array);
220 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
222 struct type *range_type = TYPE_INDEX_TYPE (tarray);
223 LONGEST index = value_as_long (idx);
225 int offset, byte, bit_index;
226 LONGEST lowerbound, upperbound;
227 get_discrete_bounds (range_type, &lowerbound, &upperbound);
228 if (index < lowerbound || index > upperbound)
229 error ("bitstring index out of range");
231 offset = index / TARGET_CHAR_BIT;
232 byte = *((char *) VALUE_CONTENTS (array) + offset);
233 bit_index = index % TARGET_CHAR_BIT;
234 byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index);
235 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
236 VALUE_BITPOS (v) = bit_index;
237 VALUE_BITSIZE (v) = 1;
238 VALUE_LVAL (v) = VALUE_LVAL (array);
239 if (VALUE_LVAL (array) == lval_internalvar)
240 VALUE_LVAL (v) = lval_internalvar_component;
241 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
242 VALUE_OFFSET (v) = offset + VALUE_OFFSET (array);
247 return value_ind (value_add (array, idx));
249 error ("not an array or string");
252 /* Return the value of EXPR[IDX], expr an aggregate rvalue
253 (eg, a vector register). This routine used to promote floats
254 to doubles, but no longer does. */
256 static struct value *
257 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
259 struct type *array_type = check_typedef (VALUE_TYPE (array));
260 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
261 unsigned int elt_size = TYPE_LENGTH (elt_type);
262 LONGEST index = value_as_long (idx);
263 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
266 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
267 error ("no such vector element");
269 v = allocate_value (elt_type);
270 if (VALUE_LAZY (array))
273 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
275 if (VALUE_LVAL (array) == lval_internalvar)
276 VALUE_LVAL (v) = lval_internalvar_component;
278 VALUE_LVAL (v) = VALUE_LVAL (array);
279 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
280 VALUE_REGNO (v) = VALUE_REGNO (array);
281 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
285 /* Check to see if either argument is a structure. This is called so
286 we know whether to go ahead with the normal binop or look for a
287 user defined function instead.
289 For now, we do not overload the `=' operator. */
292 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
294 struct type *type1, *type2;
295 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
297 type1 = check_typedef (VALUE_TYPE (arg1));
298 type2 = check_typedef (VALUE_TYPE (arg2));
299 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
300 || TYPE_CODE (type2) == TYPE_CODE_STRUCT
301 || (TYPE_CODE (type1) == TYPE_CODE_REF
302 && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT)
303 || (TYPE_CODE (type2) == TYPE_CODE_REF
304 && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT));
307 /* Check to see if argument is a structure. This is called so
308 we know whether to go ahead with the normal unop or look for a
309 user defined function instead.
311 For now, we do not overload the `&' operator. */
314 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
319 type1 = check_typedef (VALUE_TYPE (arg1));
322 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
324 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
325 type1 = TYPE_TARGET_TYPE (type1);
331 /* We know either arg1 or arg2 is a structure, so try to find the right
332 user defined function. Create an argument vector that calls
333 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
334 binary operator which is legal for GNU C++).
336 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
337 is the opcode saying how to modify it. Otherwise, OTHEROP is
341 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
342 enum exp_opcode otherop, enum noside noside)
344 struct value **argvec;
354 /* now we know that what we have to do is construct our
355 arg vector and find the right function to call it with. */
357 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
358 error ("Can't do that binary op on that type"); /* FIXME be explicit */
360 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
361 argvec[1] = value_addr (arg1);
365 /* make the right function name up */
366 strcpy (tstr, "operator__");
391 case BINOP_BITWISE_AND:
394 case BINOP_BITWISE_IOR:
397 case BINOP_BITWISE_XOR:
400 case BINOP_LOGICAL_AND:
403 case BINOP_LOGICAL_OR:
415 case BINOP_ASSIGN_MODIFY:
433 case BINOP_BITWISE_AND:
436 case BINOP_BITWISE_IOR:
439 case BINOP_BITWISE_XOR:
442 case BINOP_MOD: /* invalid */
444 error ("Invalid binary operation specified.");
447 case BINOP_SUBSCRIPT:
468 case BINOP_MOD: /* invalid */
470 error ("Invalid binary operation specified.");
473 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
479 argvec[1] = argvec[0];
482 if (noside == EVAL_AVOID_SIDE_EFFECTS)
484 struct type *return_type;
486 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
487 return value_zero (return_type, VALUE_LVAL (arg1));
489 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
491 error ("member function %s not found", tstr);
493 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
497 /* We know that arg1 is a structure, so try to find a unary user
498 defined operator that matches the operator in question.
499 Create an argument vector that calls arg1.operator @ (arg1)
500 and return that value (where '@' is (almost) any unary operator which
501 is legal for GNU C++). */
504 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
506 struct value **argvec;
507 char *ptr, *mangle_ptr;
508 char tstr[13], mangle_tstr[13];
509 int static_memfuncp, nargs;
514 /* now we know that what we have to do is construct our
515 arg vector and find the right function to call it with. */
517 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
518 error ("Can't do that unary op on that type"); /* FIXME be explicit */
520 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
521 argvec[1] = value_addr (arg1);
526 /* make the right function name up */
527 strcpy (tstr, "operator__");
529 strcpy (mangle_tstr, "__");
530 mangle_ptr = mangle_tstr + 2;
533 case UNOP_PREINCREMENT:
536 case UNOP_PREDECREMENT:
539 case UNOP_POSTINCREMENT:
541 argvec[2] = value_from_longest (builtin_type_int, 0);
545 case UNOP_POSTDECREMENT:
547 argvec[2] = value_from_longest (builtin_type_int, 0);
551 case UNOP_LOGICAL_NOT:
554 case UNOP_COMPLEMENT:
564 error ("Invalid unary operation specified.");
567 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
573 argvec[1] = argvec[0];
577 if (noside == EVAL_AVOID_SIDE_EFFECTS)
579 struct type *return_type;
581 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
582 return value_zero (return_type, VALUE_LVAL (arg1));
584 return call_function_by_hand (argvec[0], nargs, argvec + 1);
586 error ("member function %s not found", tstr);
587 return 0; /* For lint -- never reached */
591 /* Concatenate two values with the following conditions:
593 (1) Both values must be either bitstring values or character string
594 values and the resulting value consists of the concatenation of
595 ARG1 followed by ARG2.
599 One value must be an integer value and the other value must be
600 either a bitstring value or character string value, which is
601 to be repeated by the number of times specified by the integer
605 (2) Boolean values are also allowed and are treated as bit string
608 (3) Character values are also allowed and are treated as character
609 string values of length 1.
613 value_concat (struct value *arg1, struct value *arg2)
615 struct value *inval1;
616 struct value *inval2;
617 struct value *outval = NULL;
618 int inval1len, inval2len;
622 struct type *type1 = check_typedef (VALUE_TYPE (arg1));
623 struct type *type2 = check_typedef (VALUE_TYPE (arg2));
625 COERCE_VARYING_ARRAY (arg1, type1);
626 COERCE_VARYING_ARRAY (arg2, type2);
628 /* First figure out if we are dealing with two values to be concatenated
629 or a repeat count and a value to be repeated. INVAL1 is set to the
630 first of two concatenated values, or the repeat count. INVAL2 is set
631 to the second of the two concatenated values or the value to be
634 if (TYPE_CODE (type2) == TYPE_CODE_INT)
636 struct type *tmp = type1;
648 /* Now process the input values. */
650 if (TYPE_CODE (type1) == TYPE_CODE_INT)
652 /* We have a repeat count. Validate the second value and then
653 construct a value repeated that many times. */
654 if (TYPE_CODE (type2) == TYPE_CODE_STRING
655 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
657 count = longest_to_int (value_as_long (inval1));
658 inval2len = TYPE_LENGTH (type2);
659 ptr = (char *) alloca (count * inval2len);
660 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
662 inchar = (char) unpack_long (type2,
663 VALUE_CONTENTS (inval2));
664 for (idx = 0; idx < count; idx++)
666 *(ptr + idx) = inchar;
671 for (idx = 0; idx < count; idx++)
673 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
677 outval = value_string (ptr, count * inval2len);
679 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
680 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
682 error ("unimplemented support for bitstring/boolean repeats");
686 error ("can't repeat values of that type");
689 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
690 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
692 /* We have two character strings to concatenate. */
693 if (TYPE_CODE (type2) != TYPE_CODE_STRING
694 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
696 error ("Strings can only be concatenated with other strings.");
698 inval1len = TYPE_LENGTH (type1);
699 inval2len = TYPE_LENGTH (type2);
700 ptr = (char *) alloca (inval1len + inval2len);
701 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
703 *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1));
707 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
709 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
712 (char) unpack_long (type2, VALUE_CONTENTS (inval2));
716 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
718 outval = value_string (ptr, inval1len + inval2len);
720 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
721 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
723 /* We have two bitstrings to concatenate. */
724 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
725 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
727 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
729 error ("unimplemented support for bitstring/boolean concatenation.");
733 /* We don't know how to concatenate these operands. */
734 error ("illegal operands for concatenation.");
741 /* Perform a binary operation on two operands which have reasonable
742 representations as integers or floats. This includes booleans,
743 characters, integers, or floats.
744 Does not support addition and subtraction on pointers;
745 use value_add or value_sub if you want to handle those possibilities. */
748 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
751 struct type *type1, *type2;
757 type1 = check_typedef (VALUE_TYPE (arg1));
758 type2 = check_typedef (VALUE_TYPE (arg2));
760 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
761 && TYPE_CODE (type1) != TYPE_CODE_CHAR
762 && TYPE_CODE (type1) != TYPE_CODE_INT
763 && TYPE_CODE (type1) != TYPE_CODE_BOOL
764 && TYPE_CODE (type1) != TYPE_CODE_RANGE)
766 (TYPE_CODE (type2) != TYPE_CODE_FLT
767 && TYPE_CODE (type2) != TYPE_CODE_CHAR
768 && TYPE_CODE (type2) != TYPE_CODE_INT
769 && TYPE_CODE (type2) != TYPE_CODE_BOOL
770 && TYPE_CODE (type2) != TYPE_CODE_RANGE))
771 error ("Argument to arithmetic operation not a number or boolean.");
773 if (TYPE_CODE (type1) == TYPE_CODE_FLT
775 TYPE_CODE (type2) == TYPE_CODE_FLT)
777 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
778 in target format. real.c in GCC probably has the necessary
780 DOUBLEST v1, v2, v = 0;
781 v1 = value_as_double (arg1);
782 v2 = value_as_double (arg2);
804 error ("Cannot perform exponentiation: %s", safe_strerror (errno));
808 error ("Integer-only operation on floating point number.");
811 /* If either arg was long double, make sure that value is also long
814 if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT
815 || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT)
816 val = allocate_value (builtin_type_long_double);
818 val = allocate_value (builtin_type_double);
820 store_typed_floating (VALUE_CONTENTS_RAW (val), VALUE_TYPE (val), v);
822 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
824 TYPE_CODE (type2) == TYPE_CODE_BOOL)
826 LONGEST v1, v2, v = 0;
827 v1 = value_as_long (arg1);
828 v2 = value_as_long (arg2);
832 case BINOP_BITWISE_AND:
836 case BINOP_BITWISE_IOR:
840 case BINOP_BITWISE_XOR:
853 error ("Invalid operation on booleans.");
856 val = allocate_value (type1);
857 store_signed_integer (VALUE_CONTENTS_RAW (val),
862 /* Integral operations here. */
863 /* FIXME: Also mixed integral/booleans, with result an integer. */
864 /* FIXME: This implements ANSI C rules (also correct for C++).
865 What about FORTRAN and (the deleted) chill ? */
867 unsigned int promoted_len1 = TYPE_LENGTH (type1);
868 unsigned int promoted_len2 = TYPE_LENGTH (type2);
869 int is_unsigned1 = TYPE_UNSIGNED (type1);
870 int is_unsigned2 = TYPE_UNSIGNED (type2);
871 unsigned int result_len;
872 int unsigned_operation;
874 /* Determine type length and signedness after promotion for
876 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
879 promoted_len1 = TYPE_LENGTH (builtin_type_int);
881 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
884 promoted_len2 = TYPE_LENGTH (builtin_type_int);
887 /* Determine type length of the result, and if the operation should
889 Use the signedness of the operand with the greater length.
890 If both operands are of equal length, use unsigned operation
891 if one of the operands is unsigned. */
892 if (promoted_len1 > promoted_len2)
894 unsigned_operation = is_unsigned1;
895 result_len = promoted_len1;
897 else if (promoted_len2 > promoted_len1)
899 unsigned_operation = is_unsigned2;
900 result_len = promoted_len2;
904 unsigned_operation = is_unsigned1 || is_unsigned2;
905 result_len = promoted_len1;
908 if (unsigned_operation)
910 ULONGEST v1, v2, v = 0;
911 v1 = (ULONGEST) value_as_long (arg1);
912 v2 = (ULONGEST) value_as_long (arg2);
914 /* Truncate values to the type length of the result. */
915 if (result_len < sizeof (ULONGEST))
917 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
918 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
942 error ("Cannot perform exponentiation: %s", safe_strerror (errno));
950 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
951 v1 mod 0 has a defined value, v1. */
959 /* Note floor(v1/v2) == v1/v2 for unsigned. */
972 case BINOP_BITWISE_AND:
976 case BINOP_BITWISE_IOR:
980 case BINOP_BITWISE_XOR:
984 case BINOP_LOGICAL_AND:
988 case BINOP_LOGICAL_OR:
993 v = v1 < v2 ? v1 : v2;
997 v = v1 > v2 ? v1 : v2;
1004 case BINOP_NOTEQUAL:
1013 error ("Invalid binary operation on numbers.");
1016 /* This is a kludge to get around the fact that we don't
1017 know how to determine the result type from the types of
1018 the operands. (I'm not really sure how much we feel the
1019 need to duplicate the exact rules of the current
1020 language. They can get really hairy. But not to do so
1021 makes it hard to document just what we *do* do). */
1023 /* Can't just call init_type because we wouldn't know what
1024 name to give the type. */
1025 val = allocate_value
1026 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1027 ? builtin_type_unsigned_long_long
1028 : builtin_type_unsigned_long);
1029 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
1030 TYPE_LENGTH (VALUE_TYPE (val)),
1035 LONGEST v1, v2, v = 0;
1036 v1 = value_as_long (arg1);
1037 v2 = value_as_long (arg2);
1060 error ("Cannot perform exponentiation: %s", safe_strerror (errno));
1068 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1069 X mod 0 has a defined value, X. */
1077 /* Compute floor. */
1078 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1094 case BINOP_BITWISE_AND:
1098 case BINOP_BITWISE_IOR:
1102 case BINOP_BITWISE_XOR:
1106 case BINOP_LOGICAL_AND:
1110 case BINOP_LOGICAL_OR:
1115 v = v1 < v2 ? v1 : v2;
1119 v = v1 > v2 ? v1 : v2;
1131 error ("Invalid binary operation on numbers.");
1134 /* This is a kludge to get around the fact that we don't
1135 know how to determine the result type from the types of
1136 the operands. (I'm not really sure how much we feel the
1137 need to duplicate the exact rules of the current
1138 language. They can get really hairy. But not to do so
1139 makes it hard to document just what we *do* do). */
1141 /* Can't just call init_type because we wouldn't know what
1142 name to give the type. */
1143 val = allocate_value
1144 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1145 ? builtin_type_long_long
1146 : builtin_type_long);
1147 store_signed_integer (VALUE_CONTENTS_RAW (val),
1148 TYPE_LENGTH (VALUE_TYPE (val)),
1156 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1159 value_logical_not (struct value *arg1)
1165 COERCE_NUMBER (arg1);
1166 type1 = check_typedef (VALUE_TYPE (arg1));
1168 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1169 return 0 == value_as_double (arg1);
1171 len = TYPE_LENGTH (type1);
1172 p = VALUE_CONTENTS (arg1);
1183 /* Perform a comparison on two string values (whose content are not
1184 necessarily null terminated) based on their length */
1187 value_strcmp (struct value *arg1, struct value *arg2)
1189 int len1 = TYPE_LENGTH (VALUE_TYPE (arg1));
1190 int len2 = TYPE_LENGTH (VALUE_TYPE (arg2));
1191 char *s1 = VALUE_CONTENTS (arg1);
1192 char *s2 = VALUE_CONTENTS (arg2);
1193 int i, len = len1 < len2 ? len1 : len2;
1195 for (i = 0; i < len; i++)
1199 else if (s1[i] > s2[i])
1207 else if (len1 > len2)
1213 /* Simulate the C operator == by returning a 1
1214 iff ARG1 and ARG2 have equal contents. */
1217 value_equal (struct value *arg1, struct value *arg2)
1221 struct type *type1, *type2;
1222 enum type_code code1;
1223 enum type_code code2;
1225 COERCE_NUMBER (arg1);
1226 COERCE_NUMBER (arg2);
1228 type1 = check_typedef (VALUE_TYPE (arg1));
1229 type2 = check_typedef (VALUE_TYPE (arg2));
1230 code1 = TYPE_CODE (type1);
1231 code2 = TYPE_CODE (type2);
1233 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1234 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1235 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1237 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1238 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1239 return value_as_double (arg1) == value_as_double (arg2);
1241 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1243 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1244 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1245 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1246 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1248 else if (code1 == code2
1249 && ((len = (int) TYPE_LENGTH (type1))
1250 == (int) TYPE_LENGTH (type2)))
1252 p1 = VALUE_CONTENTS (arg1);
1253 p2 = VALUE_CONTENTS (arg2);
1261 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1263 return value_strcmp (arg1, arg2) == 0;
1267 error ("Invalid type combination in equality test.");
1268 return 0; /* For lint -- never reached */
1272 /* Simulate the C operator < by returning 1
1273 iff ARG1's contents are less than ARG2's. */
1276 value_less (struct value *arg1, struct value *arg2)
1278 enum type_code code1;
1279 enum type_code code2;
1280 struct type *type1, *type2;
1282 COERCE_NUMBER (arg1);
1283 COERCE_NUMBER (arg2);
1285 type1 = check_typedef (VALUE_TYPE (arg1));
1286 type2 = check_typedef (VALUE_TYPE (arg2));
1287 code1 = TYPE_CODE (type1);
1288 code2 = TYPE_CODE (type2);
1290 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1291 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1292 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1294 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1295 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1296 return value_as_double (arg1) < value_as_double (arg2);
1297 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1298 return value_as_address (arg1) < value_as_address (arg2);
1300 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1302 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1303 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1304 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1305 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1306 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1307 return value_strcmp (arg1, arg2) < 0;
1310 error ("Invalid type combination in ordering comparison.");
1315 /* The unary operators - and ~. Both free the argument ARG1. */
1318 value_neg (struct value *arg1)
1321 struct type *result_type = VALUE_TYPE (arg1);
1326 type = check_typedef (VALUE_TYPE (arg1));
1328 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1329 return value_from_double (result_type, -value_as_double (arg1));
1330 else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_BOOL)
1332 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1333 FORTRAN and (the deleted) chill ? */
1334 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1335 result_type = builtin_type_int;
1337 return value_from_longest (result_type, -value_as_long (arg1));
1341 error ("Argument to negate operation not a number.");
1342 return 0; /* For lint -- never reached */
1347 value_complement (struct value *arg1)
1350 struct type *result_type = VALUE_TYPE (arg1);
1356 type = check_typedef (VALUE_TYPE (arg1));
1358 typecode = TYPE_CODE (type);
1359 if ((typecode != TYPE_CODE_INT) && (typecode != TYPE_CODE_BOOL))
1360 error ("Argument to complement operation not an integer or boolean.");
1362 /* Perform integral promotion for ANSI C/C++.
1363 FIXME: What about FORTRAN ? */
1364 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1365 result_type = builtin_type_int;
1367 return value_from_longest (result_type, ~value_as_long (arg1));
1370 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1371 and whose VALUE_CONTENTS is valaddr.
1372 Return -1 if out of range, -2 other error. */
1375 value_bit_index (struct type *type, char *valaddr, int index)
1377 LONGEST low_bound, high_bound;
1380 struct type *range = TYPE_FIELD_TYPE (type, 0);
1381 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1383 if (index < low_bound || index > high_bound)
1385 rel_index = index - low_bound;
1386 word = unpack_long (builtin_type_unsigned_char,
1387 valaddr + (rel_index / TARGET_CHAR_BIT));
1388 rel_index %= TARGET_CHAR_BIT;
1389 if (BITS_BIG_ENDIAN)
1390 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1391 return (word >> rel_index) & 1;
1395 value_in (struct value *element, struct value *set)
1398 struct type *settype = check_typedef (VALUE_TYPE (set));
1399 struct type *eltype = check_typedef (VALUE_TYPE (element));
1400 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1401 eltype = TYPE_TARGET_TYPE (eltype);
1402 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1403 error ("Second argument of 'IN' has wrong type");
1404 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1405 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1406 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1407 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1408 error ("First argument of 'IN' has wrong type");
1409 member = value_bit_index (settype, VALUE_CONTENTS (set),
1410 value_as_long (element));
1412 error ("First argument of 'IN' not in range");
1413 return value_from_longest (LA_BOOL_TYPE, member);
1417 _initialize_valarith (void)