1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright 1986, 89, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "expression.h"
30 #include "gdb_string.h"
33 /* Define whether or not the C operator '/' truncates towards zero for
34 differently signed operands (truncation direction is undefined in C). */
36 #ifndef TRUNCATION_TOWARDS_ZERO
37 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
40 static value_ptr value_subscripted_rvalue (value_ptr, value_ptr, int);
42 void _initialize_valarith (void);
46 value_add (value_ptr arg1, value_ptr arg2)
48 register value_ptr valint, valptr;
50 struct type *type1, *type2, *valptrtype;
54 type1 = check_typedef (VALUE_TYPE (arg1));
55 type2 = check_typedef (VALUE_TYPE (arg2));
57 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
58 || TYPE_CODE (type2) == TYPE_CODE_PTR)
60 (TYPE_CODE (type1) == TYPE_CODE_INT
61 || TYPE_CODE (type2) == TYPE_CODE_INT))
62 /* Exactly one argument is a pointer, and one is an integer. */
66 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
78 len = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype)));
80 len = 1; /* For (void *) */
81 retval = value_from_pointer (valptrtype,
82 value_as_pointer (valptr)
83 + (len * value_as_long (valint)));
84 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
88 return value_binop (arg1, arg2, BINOP_ADD);
92 value_sub (value_ptr arg1, value_ptr arg2)
94 struct type *type1, *type2;
97 type1 = check_typedef (VALUE_TYPE (arg1));
98 type2 = check_typedef (VALUE_TYPE (arg2));
100 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
102 if (TYPE_CODE (type2) == TYPE_CODE_INT)
104 /* pointer - integer. */
105 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
106 return value_from_pointer (VALUE_TYPE (arg1),
107 (value_as_pointer (arg1)
108 - (sz * value_as_long (arg2))));
110 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
111 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
112 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
114 /* pointer to <type x> - pointer to <type x>. */
115 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
116 return value_from_longest
117 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
118 (value_as_long (arg1) - value_as_long (arg2)) / sz);
123 First argument of `-' is a pointer and second argument is neither\n\
124 an integer nor a pointer of the same type.");
128 return value_binop (arg1, arg2, BINOP_SUB);
131 /* Return the value of ARRAY[IDX].
132 See comments in value_coerce_array() for rationale for reason for
133 doing lower bounds adjustment here rather than there.
134 FIXME: Perhaps we should validate that the index is valid and if
135 verbosity is set, warn about invalid indices (but still use them). */
138 value_subscript (value_ptr array, value_ptr idx)
141 int c_style = current_language->c_style_arrays;
145 tarray = check_typedef (VALUE_TYPE (array));
146 COERCE_VARYING_ARRAY (array, tarray);
148 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
149 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
151 struct type *range_type = TYPE_INDEX_TYPE (tarray);
152 LONGEST lowerbound, upperbound;
153 get_discrete_bounds (range_type, &lowerbound, &upperbound);
155 if (VALUE_LVAL (array) != lval_memory)
156 return value_subscripted_rvalue (array, idx, lowerbound);
160 LONGEST index = value_as_long (idx);
161 if (index >= lowerbound && index <= upperbound)
162 return value_subscripted_rvalue (array, idx, lowerbound);
163 warning ("array or string index out of range");
164 /* fall doing C stuff */
170 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
171 idx = value_sub (idx, bound);
174 array = value_coerce_array (array);
177 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
179 struct type *range_type = TYPE_INDEX_TYPE (tarray);
180 LONGEST index = value_as_long (idx);
182 int offset, byte, bit_index;
183 LONGEST lowerbound, upperbound;
184 get_discrete_bounds (range_type, &lowerbound, &upperbound);
185 if (index < lowerbound || index > upperbound)
186 error ("bitstring index out of range");
188 offset = index / TARGET_CHAR_BIT;
189 byte = *((char *) VALUE_CONTENTS (array) + offset);
190 bit_index = index % TARGET_CHAR_BIT;
191 byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index);
192 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
193 VALUE_BITPOS (v) = bit_index;
194 VALUE_BITSIZE (v) = 1;
195 VALUE_LVAL (v) = VALUE_LVAL (array);
196 if (VALUE_LVAL (array) == lval_internalvar)
197 VALUE_LVAL (v) = lval_internalvar_component;
198 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
199 VALUE_OFFSET (v) = offset + VALUE_OFFSET (array);
204 return value_ind (value_add (array, idx));
206 error ("not an array or string");
209 /* Return the value of EXPR[IDX], expr an aggregate rvalue
210 (eg, a vector register). This routine used to promote floats
211 to doubles, but no longer does. */
214 value_subscripted_rvalue (value_ptr array, value_ptr idx, int lowerbound)
216 struct type *array_type = check_typedef (VALUE_TYPE (array));
217 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
218 unsigned int elt_size = TYPE_LENGTH (elt_type);
219 LONGEST index = value_as_long (idx);
220 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
223 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
224 error ("no such vector element");
226 v = allocate_value (elt_type);
227 if (VALUE_LAZY (array))
230 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
232 if (VALUE_LVAL (array) == lval_internalvar)
233 VALUE_LVAL (v) = lval_internalvar_component;
235 VALUE_LVAL (v) = VALUE_LVAL (array);
236 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
237 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
241 /* Check to see if either argument is a structure. This is called so
242 we know whether to go ahead with the normal binop or look for a
243 user defined function instead.
245 For now, we do not overload the `=' operator. */
248 binop_user_defined_p (enum exp_opcode op, value_ptr arg1, value_ptr arg2)
250 struct type *type1, *type2;
251 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
253 type1 = check_typedef (VALUE_TYPE (arg1));
254 type2 = check_typedef (VALUE_TYPE (arg2));
255 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
256 || TYPE_CODE (type2) == TYPE_CODE_STRUCT
257 || (TYPE_CODE (type1) == TYPE_CODE_REF
258 && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT)
259 || (TYPE_CODE (type2) == TYPE_CODE_REF
260 && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT));
263 /* Check to see if argument is a structure. This is called so
264 we know whether to go ahead with the normal unop or look for a
265 user defined function instead.
267 For now, we do not overload the `&' operator. */
270 unop_user_defined_p (enum exp_opcode op, value_ptr arg1)
275 type1 = check_typedef (VALUE_TYPE (arg1));
278 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
280 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
281 type1 = TYPE_TARGET_TYPE (type1);
287 /* We know either arg1 or arg2 is a structure, so try to find the right
288 user defined function. Create an argument vector that calls
289 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
290 binary operator which is legal for GNU C++).
292 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
293 is the opcode saying how to modify it. Otherwise, OTHEROP is
297 value_x_binop (value_ptr arg1, value_ptr arg2, enum exp_opcode op,
298 enum exp_opcode otherop, enum noside noside)
310 /* now we know that what we have to do is construct our
311 arg vector and find the right function to call it with. */
313 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
314 error ("Can't do that binary op on that type"); /* FIXME be explicit */
316 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4);
317 argvec[1] = value_addr (arg1);
321 /* make the right function name up */
322 strcpy (tstr, "operator__");
347 case BINOP_BITWISE_AND:
350 case BINOP_BITWISE_IOR:
353 case BINOP_BITWISE_XOR:
356 case BINOP_LOGICAL_AND:
359 case BINOP_LOGICAL_OR:
371 case BINOP_ASSIGN_MODIFY:
389 case BINOP_BITWISE_AND:
392 case BINOP_BITWISE_IOR:
395 case BINOP_BITWISE_XOR:
398 case BINOP_MOD: /* invalid */
400 error ("Invalid binary operation specified.");
403 case BINOP_SUBSCRIPT:
424 case BINOP_MOD: /* invalid */
426 error ("Invalid binary operation specified.");
429 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
435 argvec[1] = argvec[0];
438 if (noside == EVAL_AVOID_SIDE_EFFECTS)
440 struct type *return_type;
442 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
443 return value_zero (return_type, VALUE_LVAL (arg1));
445 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
447 error ("member function %s not found", tstr);
449 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
453 /* We know that arg1 is a structure, so try to find a unary user
454 defined operator that matches the operator in question.
455 Create an argument vector that calls arg1.operator @ (arg1)
456 and return that value (where '@' is (almost) any unary operator which
457 is legal for GNU C++). */
460 value_x_unop (value_ptr arg1, enum exp_opcode op, enum noside noside)
463 char *ptr, *mangle_ptr;
464 char tstr[13], mangle_tstr[13];
470 /* now we know that what we have to do is construct our
471 arg vector and find the right function to call it with. */
473 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
474 error ("Can't do that unary op on that type"); /* FIXME be explicit */
476 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3);
477 argvec[1] = value_addr (arg1);
480 /* make the right function name up */
481 strcpy (tstr, "operator__");
483 strcpy (mangle_tstr, "__");
484 mangle_ptr = mangle_tstr + 2;
487 case UNOP_PREINCREMENT:
490 case UNOP_PREDECREMENT:
493 case UNOP_POSTINCREMENT:
496 case UNOP_POSTDECREMENT:
499 case UNOP_LOGICAL_NOT:
502 case UNOP_COMPLEMENT:
512 error ("Invalid unary operation specified.");
515 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
521 argvec[1] = argvec[0];
524 if (noside == EVAL_AVOID_SIDE_EFFECTS)
526 struct type *return_type;
528 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
529 return value_zero (return_type, VALUE_LVAL (arg1));
531 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
533 error ("member function %s not found", tstr);
534 return 0; /* For lint -- never reached */
538 /* Concatenate two values with the following conditions:
540 (1) Both values must be either bitstring values or character string
541 values and the resulting value consists of the concatenation of
542 ARG1 followed by ARG2.
546 One value must be an integer value and the other value must be
547 either a bitstring value or character string value, which is
548 to be repeated by the number of times specified by the integer
552 (2) Boolean values are also allowed and are treated as bit string
555 (3) Character values are also allowed and are treated as character
556 string values of length 1.
560 value_concat (value_ptr arg1, value_ptr arg2)
562 register value_ptr inval1, inval2, outval = NULL;
563 int inval1len, inval2len;
567 struct type *type1 = check_typedef (VALUE_TYPE (arg1));
568 struct type *type2 = check_typedef (VALUE_TYPE (arg2));
570 COERCE_VARYING_ARRAY (arg1, type1);
571 COERCE_VARYING_ARRAY (arg2, type2);
573 /* First figure out if we are dealing with two values to be concatenated
574 or a repeat count and a value to be repeated. INVAL1 is set to the
575 first of two concatenated values, or the repeat count. INVAL2 is set
576 to the second of the two concatenated values or the value to be
579 if (TYPE_CODE (type2) == TYPE_CODE_INT)
581 struct type *tmp = type1;
593 /* Now process the input values. */
595 if (TYPE_CODE (type1) == TYPE_CODE_INT)
597 /* We have a repeat count. Validate the second value and then
598 construct a value repeated that many times. */
599 if (TYPE_CODE (type2) == TYPE_CODE_STRING
600 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
602 count = longest_to_int (value_as_long (inval1));
603 inval2len = TYPE_LENGTH (type2);
604 ptr = (char *) alloca (count * inval2len);
605 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
607 inchar = (char) unpack_long (type2,
608 VALUE_CONTENTS (inval2));
609 for (idx = 0; idx < count; idx++)
611 *(ptr + idx) = inchar;
616 for (idx = 0; idx < count; idx++)
618 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
622 outval = value_string (ptr, count * inval2len);
624 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
625 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
627 error ("unimplemented support for bitstring/boolean repeats");
631 error ("can't repeat values of that type");
634 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
635 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
637 /* We have two character strings to concatenate. */
638 if (TYPE_CODE (type2) != TYPE_CODE_STRING
639 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
641 error ("Strings can only be concatenated with other strings.");
643 inval1len = TYPE_LENGTH (type1);
644 inval2len = TYPE_LENGTH (type2);
645 ptr = (char *) alloca (inval1len + inval2len);
646 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
648 *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1));
652 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
654 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
657 (char) unpack_long (type2, VALUE_CONTENTS (inval2));
661 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
663 outval = value_string (ptr, inval1len + inval2len);
665 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
666 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
668 /* We have two bitstrings to concatenate. */
669 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
670 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
672 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
674 error ("unimplemented support for bitstring/boolean concatenation.");
678 /* We don't know how to concatenate these operands. */
679 error ("illegal operands for concatenation.");
686 /* Perform a binary operation on two operands which have reasonable
687 representations as integers or floats. This includes booleans,
688 characters, integers, or floats.
689 Does not support addition and subtraction on pointers;
690 use value_add or value_sub if you want to handle those possibilities. */
693 value_binop (value_ptr arg1, value_ptr arg2, enum exp_opcode op)
695 register value_ptr val;
696 struct type *type1, *type2;
702 type1 = check_typedef (VALUE_TYPE (arg1));
703 type2 = check_typedef (VALUE_TYPE (arg2));
705 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
706 && TYPE_CODE (type1) != TYPE_CODE_CHAR
707 && TYPE_CODE (type1) != TYPE_CODE_INT
708 && TYPE_CODE (type1) != TYPE_CODE_BOOL
709 && TYPE_CODE (type1) != TYPE_CODE_RANGE)
711 (TYPE_CODE (type2) != TYPE_CODE_FLT
712 && TYPE_CODE (type2) != TYPE_CODE_CHAR
713 && TYPE_CODE (type2) != TYPE_CODE_INT
714 && TYPE_CODE (type2) != TYPE_CODE_BOOL
715 && TYPE_CODE (type2) != TYPE_CODE_RANGE))
716 error ("Argument to arithmetic operation not a number or boolean.");
718 if (TYPE_CODE (type1) == TYPE_CODE_FLT
720 TYPE_CODE (type2) == TYPE_CODE_FLT)
722 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
723 in target format. real.c in GCC probably has the necessary
725 DOUBLEST v1, v2, v = 0;
726 v1 = value_as_double (arg1);
727 v2 = value_as_double (arg2);
749 error ("Cannot perform exponentiation: %s", strerror (errno));
753 error ("Integer-only operation on floating point number.");
756 /* If either arg was long double, make sure that value is also long
759 if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT
760 || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT)
761 val = allocate_value (builtin_type_long_double);
763 val = allocate_value (builtin_type_double);
765 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
768 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
770 TYPE_CODE (type2) == TYPE_CODE_BOOL)
772 LONGEST v1, v2, v = 0;
773 v1 = value_as_long (arg1);
774 v2 = value_as_long (arg2);
778 case BINOP_BITWISE_AND:
782 case BINOP_BITWISE_IOR:
786 case BINOP_BITWISE_XOR:
799 error ("Invalid operation on booleans.");
802 val = allocate_value (type1);
803 store_signed_integer (VALUE_CONTENTS_RAW (val),
808 /* Integral operations here. */
809 /* FIXME: Also mixed integral/booleans, with result an integer. */
810 /* FIXME: This implements ANSI C rules (also correct for C++).
811 What about FORTRAN and chill? */
813 unsigned int promoted_len1 = TYPE_LENGTH (type1);
814 unsigned int promoted_len2 = TYPE_LENGTH (type2);
815 int is_unsigned1 = TYPE_UNSIGNED (type1);
816 int is_unsigned2 = TYPE_UNSIGNED (type2);
817 unsigned int result_len;
818 int unsigned_operation;
820 /* Determine type length and signedness after promotion for
822 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
825 promoted_len1 = TYPE_LENGTH (builtin_type_int);
827 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
830 promoted_len2 = TYPE_LENGTH (builtin_type_int);
833 /* Determine type length of the result, and if the operation should
835 Use the signedness of the operand with the greater length.
836 If both operands are of equal length, use unsigned operation
837 if one of the operands is unsigned. */
838 if (promoted_len1 > promoted_len2)
840 unsigned_operation = is_unsigned1;
841 result_len = promoted_len1;
843 else if (promoted_len2 > promoted_len1)
845 unsigned_operation = is_unsigned2;
846 result_len = promoted_len2;
850 unsigned_operation = is_unsigned1 || is_unsigned2;
851 result_len = promoted_len1;
854 if (unsigned_operation)
856 ULONGEST v1, v2, v = 0;
857 v1 = (ULONGEST) value_as_long (arg1);
858 v2 = (ULONGEST) value_as_long (arg2);
860 /* Truncate values to the type length of the result. */
861 if (result_len < sizeof (ULONGEST))
863 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
864 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
888 error ("Cannot perform exponentiation: %s", strerror (errno));
896 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
897 v1 mod 0 has a defined value, v1. */
898 /* Chill specifies that v2 must be > 0, so check for that. */
899 if (current_language->la_language == language_chill
900 && value_as_long (arg2) <= 0)
902 error ("Second operand of MOD must be greater than zero.");
911 /* Note floor(v1/v2) == v1/v2 for unsigned. */
924 case BINOP_BITWISE_AND:
928 case BINOP_BITWISE_IOR:
932 case BINOP_BITWISE_XOR:
936 case BINOP_LOGICAL_AND:
940 case BINOP_LOGICAL_OR:
945 v = v1 < v2 ? v1 : v2;
949 v = v1 > v2 ? v1 : v2;
965 error ("Invalid binary operation on numbers.");
968 /* This is a kludge to get around the fact that we don't
969 know how to determine the result type from the types of
970 the operands. (I'm not really sure how much we feel the
971 need to duplicate the exact rules of the current
972 language. They can get really hairy. But not to do so
973 makes it hard to document just what we *do* do). */
975 /* Can't just call init_type because we wouldn't know what
976 name to give the type. */
978 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
979 ? builtin_type_unsigned_long_long
980 : builtin_type_unsigned_long);
981 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
982 TYPE_LENGTH (VALUE_TYPE (val)),
987 LONGEST v1, v2, v = 0;
988 v1 = value_as_long (arg1);
989 v2 = value_as_long (arg2);
1012 error ("Cannot perform exponentiation: %s", strerror (errno));
1020 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1021 X mod 0 has a defined value, X. */
1022 /* Chill specifies that v2 must be > 0, so check for that. */
1023 if (current_language->la_language == language_chill
1026 error ("Second operand of MOD must be greater than zero.");
1035 /* Compute floor. */
1036 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1052 case BINOP_BITWISE_AND:
1056 case BINOP_BITWISE_IOR:
1060 case BINOP_BITWISE_XOR:
1064 case BINOP_LOGICAL_AND:
1068 case BINOP_LOGICAL_OR:
1073 v = v1 < v2 ? v1 : v2;
1077 v = v1 > v2 ? v1 : v2;
1089 error ("Invalid binary operation on numbers.");
1092 /* This is a kludge to get around the fact that we don't
1093 know how to determine the result type from the types of
1094 the operands. (I'm not really sure how much we feel the
1095 need to duplicate the exact rules of the current
1096 language. They can get really hairy. But not to do so
1097 makes it hard to document just what we *do* do). */
1099 /* Can't just call init_type because we wouldn't know what
1100 name to give the type. */
1101 val = allocate_value
1102 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1103 ? builtin_type_long_long
1104 : builtin_type_long);
1105 store_signed_integer (VALUE_CONTENTS_RAW (val),
1106 TYPE_LENGTH (VALUE_TYPE (val)),
1114 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1117 value_logical_not (value_ptr arg1)
1123 COERCE_NUMBER (arg1);
1124 type1 = check_typedef (VALUE_TYPE (arg1));
1126 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1127 return 0 == value_as_double (arg1);
1129 len = TYPE_LENGTH (type1);
1130 p = VALUE_CONTENTS (arg1);
1141 /* Perform a comparison on two string values (whose content are not
1142 necessarily null terminated) based on their length */
1145 value_strcmp (register value_ptr arg1, register value_ptr arg2)
1147 int len1 = TYPE_LENGTH (VALUE_TYPE (arg1));
1148 int len2 = TYPE_LENGTH (VALUE_TYPE (arg2));
1149 char *s1 = VALUE_CONTENTS (arg1);
1150 char *s2 = VALUE_CONTENTS (arg2);
1151 int i, len = len1 < len2 ? len1 : len2;
1153 for (i = 0; i < len; i++)
1157 else if (s1[i] > s2[i])
1165 else if (len1 > len2)
1171 /* Simulate the C operator == by returning a 1
1172 iff ARG1 and ARG2 have equal contents. */
1175 value_equal (register value_ptr arg1, register value_ptr arg2)
1178 register char *p1, *p2;
1179 struct type *type1, *type2;
1180 enum type_code code1;
1181 enum type_code code2;
1183 COERCE_NUMBER (arg1);
1184 COERCE_NUMBER (arg2);
1186 type1 = check_typedef (VALUE_TYPE (arg1));
1187 type2 = check_typedef (VALUE_TYPE (arg2));
1188 code1 = TYPE_CODE (type1);
1189 code2 = TYPE_CODE (type2);
1191 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1192 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1193 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1195 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1196 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1197 return value_as_double (arg1) == value_as_double (arg2);
1199 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1201 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1202 return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
1203 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1204 return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
1206 else if (code1 == code2
1207 && ((len = (int) TYPE_LENGTH (type1))
1208 == (int) TYPE_LENGTH (type2)))
1210 p1 = VALUE_CONTENTS (arg1);
1211 p2 = VALUE_CONTENTS (arg2);
1219 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1221 return value_strcmp (arg1, arg2) == 0;
1225 error ("Invalid type combination in equality test.");
1226 return 0; /* For lint -- never reached */
1230 /* Simulate the C operator < by returning 1
1231 iff ARG1's contents are less than ARG2's. */
1234 value_less (register value_ptr arg1, register value_ptr arg2)
1236 register enum type_code code1;
1237 register enum type_code code2;
1238 struct type *type1, *type2;
1240 COERCE_NUMBER (arg1);
1241 COERCE_NUMBER (arg2);
1243 type1 = check_typedef (VALUE_TYPE (arg1));
1244 type2 = check_typedef (VALUE_TYPE (arg2));
1245 code1 = TYPE_CODE (type1);
1246 code2 = TYPE_CODE (type2);
1248 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1249 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1250 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1252 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1253 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1254 return value_as_double (arg1) < value_as_double (arg2);
1255 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1256 return value_as_pointer (arg1) < value_as_pointer (arg2);
1258 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1260 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1261 return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
1262 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1263 return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
1264 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1265 return value_strcmp (arg1, arg2) < 0;
1268 error ("Invalid type combination in ordering comparison.");
1273 /* The unary operators - and ~. Both free the argument ARG1. */
1276 value_neg (register value_ptr arg1)
1278 register struct type *type;
1279 register struct type *result_type = VALUE_TYPE (arg1);
1284 type = check_typedef (VALUE_TYPE (arg1));
1286 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1287 return value_from_double (result_type, -value_as_double (arg1));
1288 else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_BOOL)
1290 /* Perform integral promotion for ANSI C/C++.
1291 FIXME: What about FORTRAN and chill ? */
1292 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1293 result_type = builtin_type_int;
1295 return value_from_longest (result_type, -value_as_long (arg1));
1299 error ("Argument to negate operation not a number.");
1300 return 0; /* For lint -- never reached */
1305 value_complement (register value_ptr arg1)
1307 register struct type *type;
1308 register struct type *result_type = VALUE_TYPE (arg1);
1314 type = check_typedef (VALUE_TYPE (arg1));
1316 typecode = TYPE_CODE (type);
1317 if ((typecode != TYPE_CODE_INT) && (typecode != TYPE_CODE_BOOL))
1318 error ("Argument to complement operation not an integer or boolean.");
1320 /* Perform integral promotion for ANSI C/C++.
1321 FIXME: What about FORTRAN ? */
1322 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1323 result_type = builtin_type_int;
1325 return value_from_longest (result_type, ~value_as_long (arg1));
1328 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1329 and whose VALUE_CONTENTS is valaddr.
1330 Return -1 if out of range, -2 other error. */
1333 value_bit_index (struct type *type, char *valaddr, int index)
1335 LONGEST low_bound, high_bound;
1338 struct type *range = TYPE_FIELD_TYPE (type, 0);
1339 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1341 if (index < low_bound || index > high_bound)
1343 rel_index = index - low_bound;
1344 word = unpack_long (builtin_type_unsigned_char,
1345 valaddr + (rel_index / TARGET_CHAR_BIT));
1346 rel_index %= TARGET_CHAR_BIT;
1347 if (BITS_BIG_ENDIAN)
1348 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1349 return (word >> rel_index) & 1;
1353 value_in (value_ptr element, value_ptr set)
1356 struct type *settype = check_typedef (VALUE_TYPE (set));
1357 struct type *eltype = check_typedef (VALUE_TYPE (element));
1358 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1359 eltype = TYPE_TARGET_TYPE (eltype);
1360 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1361 error ("Second argument of 'IN' has wrong type");
1362 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1363 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1364 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1365 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1366 error ("First argument of 'IN' has wrong type");
1367 member = value_bit_index (settype, VALUE_CONTENTS (set),
1368 value_as_long (element));
1370 error ("First argument of 'IN' not in range");
1371 return value_from_longest (LA_BOOL_TYPE, member);
1375 _initialize_valarith (void)