1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "expression.h"
30 #include "gdb_string.h"
34 /* Define whether or not the C operator '/' truncates towards zero for
35 differently signed operands (truncation direction is undefined in C). */
37 #ifndef TRUNCATION_TOWARDS_ZERO
38 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
41 static struct value *value_subscripted_rvalue (struct value *, struct value *, int);
43 void _initialize_valarith (void);
46 /* Given a pointer, return the size of its target.
47 If the pointer type is void *, then return 1.
48 If the target type is incomplete, then error out.
49 This isn't a general purpose function, but just a
50 helper for value_sub & value_add.
54 find_size_for_pointer_math (struct type *ptr_type)
57 struct type *ptr_target;
59 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
61 sz = TYPE_LENGTH (ptr_target);
64 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
70 name = TYPE_NAME (ptr_target);
72 name = TYPE_TAG_NAME (ptr_target);
74 error ("Cannot perform pointer math on incomplete types, "
75 "try casting to a known type, or void *.");
77 error ("Cannot perform pointer math on incomplete type \"%s\", "
78 "try casting to a known type, or void *.", name);
85 value_add (struct value *arg1, struct value *arg2)
90 struct type *type1, *type2, *valptrtype;
94 type1 = check_typedef (VALUE_TYPE (arg1));
95 type2 = check_typedef (VALUE_TYPE (arg2));
97 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
98 || TYPE_CODE (type2) == TYPE_CODE_PTR)
100 (TYPE_CODE (type1) == TYPE_CODE_INT
101 || TYPE_CODE (type2) == TYPE_CODE_INT))
102 /* Exactly one argument is a pointer, and one is an integer. */
104 struct value *retval;
106 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
119 sz = find_size_for_pointer_math (valptrtype);
121 retval = value_from_pointer (valptrtype,
122 value_as_address (valptr)
123 + (sz * value_as_long (valint)));
124 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
128 return value_binop (arg1, arg2, BINOP_ADD);
132 value_sub (struct value *arg1, struct value *arg2)
134 struct type *type1, *type2;
135 COERCE_NUMBER (arg1);
136 COERCE_NUMBER (arg2);
137 type1 = check_typedef (VALUE_TYPE (arg1));
138 type2 = check_typedef (VALUE_TYPE (arg2));
140 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
142 if (TYPE_CODE (type2) == TYPE_CODE_INT)
144 /* pointer - integer. */
145 LONGEST sz = find_size_for_pointer_math (type1);
147 return value_from_pointer (type1,
148 (value_as_address (arg1)
149 - (sz * value_as_long (arg2))));
151 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
152 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
153 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
155 /* pointer to <type x> - pointer to <type x>. */
156 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
157 return value_from_longest
158 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
159 (value_as_long (arg1) - value_as_long (arg2)) / sz);
164 First argument of `-' is a pointer and second argument is neither\n\
165 an integer nor a pointer of the same type.");
169 return value_binop (arg1, arg2, BINOP_SUB);
172 /* Return the value of ARRAY[IDX].
173 See comments in value_coerce_array() for rationale for reason for
174 doing lower bounds adjustment here rather than there.
175 FIXME: Perhaps we should validate that the index is valid and if
176 verbosity is set, warn about invalid indices (but still use them). */
179 value_subscript (struct value *array, struct value *idx)
182 int c_style = current_language->c_style_arrays;
186 tarray = check_typedef (VALUE_TYPE (array));
187 COERCE_VARYING_ARRAY (array, tarray);
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 warning ("array or string index out of range");
205 /* fall doing C stuff */
211 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
212 idx = value_sub (idx, bound);
215 array = value_coerce_array (array);
218 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
220 struct type *range_type = TYPE_INDEX_TYPE (tarray);
221 LONGEST index = value_as_long (idx);
223 int offset, byte, bit_index;
224 LONGEST lowerbound, upperbound;
225 get_discrete_bounds (range_type, &lowerbound, &upperbound);
226 if (index < lowerbound || index > upperbound)
227 error ("bitstring index out of range");
229 offset = index / TARGET_CHAR_BIT;
230 byte = *((char *) VALUE_CONTENTS (array) + offset);
231 bit_index = index % TARGET_CHAR_BIT;
232 byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index);
233 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
234 VALUE_BITPOS (v) = bit_index;
235 VALUE_BITSIZE (v) = 1;
236 VALUE_LVAL (v) = VALUE_LVAL (array);
237 if (VALUE_LVAL (array) == lval_internalvar)
238 VALUE_LVAL (v) = lval_internalvar_component;
239 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
240 VALUE_OFFSET (v) = offset + VALUE_OFFSET (array);
245 return value_ind (value_add (array, idx));
247 error ("not an array or string");
250 /* Return the value of EXPR[IDX], expr an aggregate rvalue
251 (eg, a vector register). This routine used to promote floats
252 to doubles, but no longer does. */
254 static struct value *
255 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
257 struct type *array_type = check_typedef (VALUE_TYPE (array));
258 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
259 unsigned int elt_size = TYPE_LENGTH (elt_type);
260 LONGEST index = value_as_long (idx);
261 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
264 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
265 error ("no such vector element");
267 v = allocate_value (elt_type);
268 if (VALUE_LAZY (array))
271 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
273 if (VALUE_LVAL (array) == lval_internalvar)
274 VALUE_LVAL (v) = lval_internalvar_component;
276 VALUE_LVAL (v) = VALUE_LVAL (array);
277 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
278 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
282 /* Check to see if either argument is a structure. This is called so
283 we know whether to go ahead with the normal binop or look for a
284 user defined function instead.
286 For now, we do not overload the `=' operator. */
289 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
291 struct type *type1, *type2;
292 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
294 type1 = check_typedef (VALUE_TYPE (arg1));
295 type2 = check_typedef (VALUE_TYPE (arg2));
296 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
297 || TYPE_CODE (type2) == TYPE_CODE_STRUCT
298 || (TYPE_CODE (type1) == TYPE_CODE_REF
299 && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT)
300 || (TYPE_CODE (type2) == TYPE_CODE_REF
301 && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT));
304 /* Check to see if argument is a structure. This is called so
305 we know whether to go ahead with the normal unop or look for a
306 user defined function instead.
308 For now, we do not overload the `&' operator. */
311 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
316 type1 = check_typedef (VALUE_TYPE (arg1));
319 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
321 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
322 type1 = TYPE_TARGET_TYPE (type1);
328 /* We know either arg1 or arg2 is a structure, so try to find the right
329 user defined function. Create an argument vector that calls
330 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
331 binary operator which is legal for GNU C++).
333 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
334 is the opcode saying how to modify it. Otherwise, OTHEROP is
338 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
339 enum exp_opcode otherop, enum noside noside)
341 struct value **argvec;
351 /* now we know that what we have to do is construct our
352 arg vector and find the right function to call it with. */
354 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
355 error ("Can't do that binary op on that type"); /* FIXME be explicit */
357 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
358 argvec[1] = value_addr (arg1);
362 /* make the right function name up */
363 strcpy (tstr, "operator__");
388 case BINOP_BITWISE_AND:
391 case BINOP_BITWISE_IOR:
394 case BINOP_BITWISE_XOR:
397 case BINOP_LOGICAL_AND:
400 case BINOP_LOGICAL_OR:
412 case BINOP_ASSIGN_MODIFY:
430 case BINOP_BITWISE_AND:
433 case BINOP_BITWISE_IOR:
436 case BINOP_BITWISE_XOR:
439 case BINOP_MOD: /* invalid */
441 error ("Invalid binary operation specified.");
444 case BINOP_SUBSCRIPT:
465 case BINOP_MOD: /* invalid */
467 error ("Invalid binary operation specified.");
470 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
476 argvec[1] = argvec[0];
479 if (noside == EVAL_AVOID_SIDE_EFFECTS)
481 struct type *return_type;
483 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
484 return value_zero (return_type, VALUE_LVAL (arg1));
486 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
488 error ("member function %s not found", tstr);
490 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
494 /* We know that arg1 is a structure, so try to find a unary user
495 defined operator that matches the operator in question.
496 Create an argument vector that calls arg1.operator @ (arg1)
497 and return that value (where '@' is (almost) any unary operator which
498 is legal for GNU C++). */
501 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
503 struct value **argvec;
504 char *ptr, *mangle_ptr;
505 char tstr[13], mangle_tstr[13];
506 int static_memfuncp, nargs;
511 /* now we know that what we have to do is construct our
512 arg vector and find the right function to call it with. */
514 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
515 error ("Can't do that unary op on that type"); /* FIXME be explicit */
517 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
518 argvec[1] = value_addr (arg1);
523 /* make the right function name up */
524 strcpy (tstr, "operator__");
526 strcpy (mangle_tstr, "__");
527 mangle_ptr = mangle_tstr + 2;
530 case UNOP_PREINCREMENT:
533 case UNOP_PREDECREMENT:
536 case UNOP_POSTINCREMENT:
538 argvec[2] = value_from_longest (builtin_type_int, 0);
542 case UNOP_POSTDECREMENT:
544 argvec[2] = value_from_longest (builtin_type_int, 0);
548 case UNOP_LOGICAL_NOT:
551 case UNOP_COMPLEMENT:
561 error ("Invalid unary operation specified.");
564 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
570 argvec[1] = argvec[0];
574 if (noside == EVAL_AVOID_SIDE_EFFECTS)
576 struct type *return_type;
578 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
579 return value_zero (return_type, VALUE_LVAL (arg1));
581 return call_function_by_hand (argvec[0], nargs, argvec + 1);
583 error ("member function %s not found", tstr);
584 return 0; /* For lint -- never reached */
588 /* Concatenate two values with the following conditions:
590 (1) Both values must be either bitstring values or character string
591 values and the resulting value consists of the concatenation of
592 ARG1 followed by ARG2.
596 One value must be an integer value and the other value must be
597 either a bitstring value or character string value, which is
598 to be repeated by the number of times specified by the integer
602 (2) Boolean values are also allowed and are treated as bit string
605 (3) Character values are also allowed and are treated as character
606 string values of length 1.
610 value_concat (struct value *arg1, struct value *arg2)
612 struct value *inval1;
613 struct value *inval2;
614 struct value *outval = NULL;
615 int inval1len, inval2len;
619 struct type *type1 = check_typedef (VALUE_TYPE (arg1));
620 struct type *type2 = check_typedef (VALUE_TYPE (arg2));
622 COERCE_VARYING_ARRAY (arg1, type1);
623 COERCE_VARYING_ARRAY (arg2, type2);
625 /* First figure out if we are dealing with two values to be concatenated
626 or a repeat count and a value to be repeated. INVAL1 is set to the
627 first of two concatenated values, or the repeat count. INVAL2 is set
628 to the second of the two concatenated values or the value to be
631 if (TYPE_CODE (type2) == TYPE_CODE_INT)
633 struct type *tmp = type1;
645 /* Now process the input values. */
647 if (TYPE_CODE (type1) == TYPE_CODE_INT)
649 /* We have a repeat count. Validate the second value and then
650 construct a value repeated that many times. */
651 if (TYPE_CODE (type2) == TYPE_CODE_STRING
652 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
654 count = longest_to_int (value_as_long (inval1));
655 inval2len = TYPE_LENGTH (type2);
656 ptr = (char *) alloca (count * inval2len);
657 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
659 inchar = (char) unpack_long (type2,
660 VALUE_CONTENTS (inval2));
661 for (idx = 0; idx < count; idx++)
663 *(ptr + idx) = inchar;
668 for (idx = 0; idx < count; idx++)
670 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
674 outval = value_string (ptr, count * inval2len);
676 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
677 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
679 error ("unimplemented support for bitstring/boolean repeats");
683 error ("can't repeat values of that type");
686 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
687 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
689 /* We have two character strings to concatenate. */
690 if (TYPE_CODE (type2) != TYPE_CODE_STRING
691 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
693 error ("Strings can only be concatenated with other strings.");
695 inval1len = TYPE_LENGTH (type1);
696 inval2len = TYPE_LENGTH (type2);
697 ptr = (char *) alloca (inval1len + inval2len);
698 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
700 *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1));
704 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
706 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
709 (char) unpack_long (type2, VALUE_CONTENTS (inval2));
713 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
715 outval = value_string (ptr, inval1len + inval2len);
717 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
718 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
720 /* We have two bitstrings to concatenate. */
721 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
722 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
724 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
726 error ("unimplemented support for bitstring/boolean concatenation.");
730 /* We don't know how to concatenate these operands. */
731 error ("illegal operands for concatenation.");
738 /* Perform a binary operation on two operands which have reasonable
739 representations as integers or floats. This includes booleans,
740 characters, integers, or floats.
741 Does not support addition and subtraction on pointers;
742 use value_add or value_sub if you want to handle those possibilities. */
745 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
748 struct type *type1, *type2;
754 type1 = check_typedef (VALUE_TYPE (arg1));
755 type2 = check_typedef (VALUE_TYPE (arg2));
757 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
758 && TYPE_CODE (type1) != TYPE_CODE_CHAR
759 && TYPE_CODE (type1) != TYPE_CODE_INT
760 && TYPE_CODE (type1) != TYPE_CODE_BOOL
761 && TYPE_CODE (type1) != TYPE_CODE_RANGE)
763 (TYPE_CODE (type2) != TYPE_CODE_FLT
764 && TYPE_CODE (type2) != TYPE_CODE_CHAR
765 && TYPE_CODE (type2) != TYPE_CODE_INT
766 && TYPE_CODE (type2) != TYPE_CODE_BOOL
767 && TYPE_CODE (type2) != TYPE_CODE_RANGE))
768 error ("Argument to arithmetic operation not a number or boolean.");
770 if (TYPE_CODE (type1) == TYPE_CODE_FLT
772 TYPE_CODE (type2) == TYPE_CODE_FLT)
774 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
775 in target format. real.c in GCC probably has the necessary
777 DOUBLEST v1, v2, v = 0;
778 v1 = value_as_double (arg1);
779 v2 = value_as_double (arg2);
801 error ("Cannot perform exponentiation: %s", safe_strerror (errno));
805 error ("Integer-only operation on floating point number.");
808 /* If either arg was long double, make sure that value is also long
811 if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT
812 || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT)
813 val = allocate_value (builtin_type_long_double);
815 val = allocate_value (builtin_type_double);
817 store_typed_floating (VALUE_CONTENTS_RAW (val), VALUE_TYPE (val), v);
819 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
821 TYPE_CODE (type2) == TYPE_CODE_BOOL)
823 LONGEST v1, v2, v = 0;
824 v1 = value_as_long (arg1);
825 v2 = value_as_long (arg2);
829 case BINOP_BITWISE_AND:
833 case BINOP_BITWISE_IOR:
837 case BINOP_BITWISE_XOR:
850 error ("Invalid operation on booleans.");
853 val = allocate_value (type1);
854 store_signed_integer (VALUE_CONTENTS_RAW (val),
859 /* Integral operations here. */
860 /* FIXME: Also mixed integral/booleans, with result an integer. */
861 /* FIXME: This implements ANSI C rules (also correct for C++).
862 What about FORTRAN and chill? */
864 unsigned int promoted_len1 = TYPE_LENGTH (type1);
865 unsigned int promoted_len2 = TYPE_LENGTH (type2);
866 int is_unsigned1 = TYPE_UNSIGNED (type1);
867 int is_unsigned2 = TYPE_UNSIGNED (type2);
868 unsigned int result_len;
869 int unsigned_operation;
871 /* Determine type length and signedness after promotion for
873 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
876 promoted_len1 = TYPE_LENGTH (builtin_type_int);
878 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
881 promoted_len2 = TYPE_LENGTH (builtin_type_int);
884 /* Determine type length of the result, and if the operation should
886 Use the signedness of the operand with the greater length.
887 If both operands are of equal length, use unsigned operation
888 if one of the operands is unsigned. */
889 if (promoted_len1 > promoted_len2)
891 unsigned_operation = is_unsigned1;
892 result_len = promoted_len1;
894 else if (promoted_len2 > promoted_len1)
896 unsigned_operation = is_unsigned2;
897 result_len = promoted_len2;
901 unsigned_operation = is_unsigned1 || is_unsigned2;
902 result_len = promoted_len1;
905 if (unsigned_operation)
907 ULONGEST v1, v2, v = 0;
908 v1 = (ULONGEST) value_as_long (arg1);
909 v2 = (ULONGEST) value_as_long (arg2);
911 /* Truncate values to the type length of the result. */
912 if (result_len < sizeof (ULONGEST))
914 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
915 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
939 error ("Cannot perform exponentiation: %s", safe_strerror (errno));
947 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
948 v1 mod 0 has a defined value, v1. */
949 /* Chill specifies that v2 must be > 0, so check for that. */
950 if (current_language->la_language == language_chill
951 && value_as_long (arg2) <= 0)
953 error ("Second operand of MOD must be greater than zero.");
962 /* Note floor(v1/v2) == v1/v2 for unsigned. */
975 case BINOP_BITWISE_AND:
979 case BINOP_BITWISE_IOR:
983 case BINOP_BITWISE_XOR:
987 case BINOP_LOGICAL_AND:
991 case BINOP_LOGICAL_OR:
996 v = v1 < v2 ? v1 : v2;
1000 v = v1 > v2 ? v1 : v2;
1007 case BINOP_NOTEQUAL:
1016 error ("Invalid binary operation on numbers.");
1019 /* This is a kludge to get around the fact that we don't
1020 know how to determine the result type from the types of
1021 the operands. (I'm not really sure how much we feel the
1022 need to duplicate the exact rules of the current
1023 language. They can get really hairy. But not to do so
1024 makes it hard to document just what we *do* do). */
1026 /* Can't just call init_type because we wouldn't know what
1027 name to give the type. */
1028 val = allocate_value
1029 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1030 ? builtin_type_unsigned_long_long
1031 : builtin_type_unsigned_long);
1032 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
1033 TYPE_LENGTH (VALUE_TYPE (val)),
1038 LONGEST v1, v2, v = 0;
1039 v1 = value_as_long (arg1);
1040 v2 = value_as_long (arg2);
1063 error ("Cannot perform exponentiation: %s", safe_strerror (errno));
1071 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1072 X mod 0 has a defined value, X. */
1073 /* Chill specifies that v2 must be > 0, so check for that. */
1074 if (current_language->la_language == language_chill
1077 error ("Second operand of MOD must be greater than zero.");
1086 /* Compute floor. */
1087 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1103 case BINOP_BITWISE_AND:
1107 case BINOP_BITWISE_IOR:
1111 case BINOP_BITWISE_XOR:
1115 case BINOP_LOGICAL_AND:
1119 case BINOP_LOGICAL_OR:
1124 v = v1 < v2 ? v1 : v2;
1128 v = v1 > v2 ? v1 : v2;
1140 error ("Invalid binary operation on numbers.");
1143 /* This is a kludge to get around the fact that we don't
1144 know how to determine the result type from the types of
1145 the operands. (I'm not really sure how much we feel the
1146 need to duplicate the exact rules of the current
1147 language. They can get really hairy. But not to do so
1148 makes it hard to document just what we *do* do). */
1150 /* Can't just call init_type because we wouldn't know what
1151 name to give the type. */
1152 val = allocate_value
1153 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1154 ? builtin_type_long_long
1155 : builtin_type_long);
1156 store_signed_integer (VALUE_CONTENTS_RAW (val),
1157 TYPE_LENGTH (VALUE_TYPE (val)),
1165 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1168 value_logical_not (struct value *arg1)
1174 COERCE_NUMBER (arg1);
1175 type1 = check_typedef (VALUE_TYPE (arg1));
1177 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1178 return 0 == value_as_double (arg1);
1180 len = TYPE_LENGTH (type1);
1181 p = VALUE_CONTENTS (arg1);
1192 /* Perform a comparison on two string values (whose content are not
1193 necessarily null terminated) based on their length */
1196 value_strcmp (struct value *arg1, struct value *arg2)
1198 int len1 = TYPE_LENGTH (VALUE_TYPE (arg1));
1199 int len2 = TYPE_LENGTH (VALUE_TYPE (arg2));
1200 char *s1 = VALUE_CONTENTS (arg1);
1201 char *s2 = VALUE_CONTENTS (arg2);
1202 int i, len = len1 < len2 ? len1 : len2;
1204 for (i = 0; i < len; i++)
1208 else if (s1[i] > s2[i])
1216 else if (len1 > len2)
1222 /* Simulate the C operator == by returning a 1
1223 iff ARG1 and ARG2 have equal contents. */
1226 value_equal (struct value *arg1, struct value *arg2)
1229 register char *p1, *p2;
1230 struct type *type1, *type2;
1231 enum type_code code1;
1232 enum type_code code2;
1234 COERCE_NUMBER (arg1);
1235 COERCE_NUMBER (arg2);
1237 type1 = check_typedef (VALUE_TYPE (arg1));
1238 type2 = check_typedef (VALUE_TYPE (arg2));
1239 code1 = TYPE_CODE (type1);
1240 code2 = TYPE_CODE (type2);
1242 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1243 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1244 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1246 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1247 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1248 return value_as_double (arg1) == value_as_double (arg2);
1250 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1252 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1253 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1254 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1255 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1257 else if (code1 == code2
1258 && ((len = (int) TYPE_LENGTH (type1))
1259 == (int) TYPE_LENGTH (type2)))
1261 p1 = VALUE_CONTENTS (arg1);
1262 p2 = VALUE_CONTENTS (arg2);
1270 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1272 return value_strcmp (arg1, arg2) == 0;
1276 error ("Invalid type combination in equality test.");
1277 return 0; /* For lint -- never reached */
1281 /* Simulate the C operator < by returning 1
1282 iff ARG1's contents are less than ARG2's. */
1285 value_less (struct value *arg1, struct value *arg2)
1287 register enum type_code code1;
1288 register enum type_code code2;
1289 struct type *type1, *type2;
1291 COERCE_NUMBER (arg1);
1292 COERCE_NUMBER (arg2);
1294 type1 = check_typedef (VALUE_TYPE (arg1));
1295 type2 = check_typedef (VALUE_TYPE (arg2));
1296 code1 = TYPE_CODE (type1);
1297 code2 = TYPE_CODE (type2);
1299 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1300 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1301 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1303 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1304 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1305 return value_as_double (arg1) < value_as_double (arg2);
1306 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1307 return value_as_address (arg1) < value_as_address (arg2);
1309 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1311 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1312 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1313 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1314 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1315 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1316 return value_strcmp (arg1, arg2) < 0;
1319 error ("Invalid type combination in ordering comparison.");
1324 /* The unary operators - and ~. Both free the argument ARG1. */
1327 value_neg (struct value *arg1)
1329 register struct type *type;
1330 register struct type *result_type = VALUE_TYPE (arg1);
1335 type = check_typedef (VALUE_TYPE (arg1));
1337 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1338 return value_from_double (result_type, -value_as_double (arg1));
1339 else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_BOOL)
1341 /* Perform integral promotion for ANSI C/C++.
1342 FIXME: What about FORTRAN and chill ? */
1343 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1344 result_type = builtin_type_int;
1346 return value_from_longest (result_type, -value_as_long (arg1));
1350 error ("Argument to negate operation not a number.");
1351 return 0; /* For lint -- never reached */
1356 value_complement (struct value *arg1)
1358 register struct type *type;
1359 register struct type *result_type = VALUE_TYPE (arg1);
1365 type = check_typedef (VALUE_TYPE (arg1));
1367 typecode = TYPE_CODE (type);
1368 if ((typecode != TYPE_CODE_INT) && (typecode != TYPE_CODE_BOOL))
1369 error ("Argument to complement operation not an integer or boolean.");
1371 /* Perform integral promotion for ANSI C/C++.
1372 FIXME: What about FORTRAN ? */
1373 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1374 result_type = builtin_type_int;
1376 return value_from_longest (result_type, ~value_as_long (arg1));
1379 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1380 and whose VALUE_CONTENTS is valaddr.
1381 Return -1 if out of range, -2 other error. */
1384 value_bit_index (struct type *type, char *valaddr, int index)
1386 LONGEST low_bound, high_bound;
1389 struct type *range = TYPE_FIELD_TYPE (type, 0);
1390 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1392 if (index < low_bound || index > high_bound)
1394 rel_index = index - low_bound;
1395 word = unpack_long (builtin_type_unsigned_char,
1396 valaddr + (rel_index / TARGET_CHAR_BIT));
1397 rel_index %= TARGET_CHAR_BIT;
1398 if (BITS_BIG_ENDIAN)
1399 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1400 return (word >> rel_index) & 1;
1404 value_in (struct value *element, struct value *set)
1407 struct type *settype = check_typedef (VALUE_TYPE (set));
1408 struct type *eltype = check_typedef (VALUE_TYPE (element));
1409 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1410 eltype = TYPE_TARGET_TYPE (eltype);
1411 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1412 error ("Second argument of 'IN' has wrong type");
1413 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1414 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1415 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1416 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1417 error ("First argument of 'IN' has wrong type");
1418 member = value_bit_index (settype, VALUE_CONTENTS (set),
1419 value_as_long (element));
1421 error ("First argument of 'IN' not in range");
1422 return value_from_longest (LA_BOOL_TYPE, member);
1426 _initialize_valarith (void)