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 PARAMS ((value_ptr, value_ptr, int));
42 void _initialize_valarith PARAMS ((void));
46 value_add (arg1, arg2)
49 register value_ptr valint, valptr;
51 struct type *type1, *type2, *valptrtype;
55 type1 = check_typedef (VALUE_TYPE (arg1));
56 type2 = check_typedef (VALUE_TYPE (arg2));
58 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
59 || TYPE_CODE (type2) == TYPE_CODE_PTR)
61 (TYPE_CODE (type1) == TYPE_CODE_INT
62 || TYPE_CODE (type2) == TYPE_CODE_INT))
63 /* Exactly one argument is a pointer, and one is an integer. */
67 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
79 len = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype)));
81 len = 1; /* For (void *) */
82 retval = value_from_longest (valptrtype,
83 value_as_long (valptr)
84 + (len * value_as_long (valint)));
85 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
89 return value_binop (arg1, arg2, BINOP_ADD);
93 value_sub (arg1, arg2)
96 struct type *type1, *type2;
99 type1 = check_typedef (VALUE_TYPE (arg1));
100 type2 = check_typedef (VALUE_TYPE (arg2));
102 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
104 if (TYPE_CODE (type2) == TYPE_CODE_INT)
106 /* pointer - integer. */
107 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
108 return value_from_longest
110 value_as_long (arg1) - (sz * value_as_long (arg2)));
112 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
113 && TYPE_LENGTH (TYPE_TARGET_TYPE (type1))
114 == TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
116 /* pointer to <type x> - pointer to <type x>. */
117 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
118 return value_from_longest
119 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
120 (value_as_long (arg1) - value_as_long (arg2)) / sz);
125 First argument of `-' is a pointer and second argument is neither\n\
126 an integer nor a pointer of the same type.");
130 return value_binop (arg1, arg2, BINOP_SUB);
133 /* Return the value of ARRAY[IDX].
134 See comments in value_coerce_array() for rationale for reason for
135 doing lower bounds adjustment here rather than there.
136 FIXME: Perhaps we should validate that the index is valid and if
137 verbosity is set, warn about invalid indices (but still use them). */
140 value_subscript (array, idx)
141 value_ptr array, idx;
144 int c_style = current_language->c_style_arrays;
148 tarray = check_typedef (VALUE_TYPE (array));
149 COERCE_VARYING_ARRAY (array, tarray);
151 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
152 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
154 struct type *range_type = TYPE_INDEX_TYPE (tarray);
155 LONGEST lowerbound, upperbound;
156 get_discrete_bounds (range_type, &lowerbound, &upperbound);
158 if (VALUE_LVAL (array) != lval_memory)
159 return value_subscripted_rvalue (array, idx, lowerbound);
163 LONGEST index = value_as_long (idx);
164 if (index >= lowerbound && index <= upperbound)
165 return value_subscripted_rvalue (array, idx, lowerbound);
166 warning ("array or string index out of range");
167 /* fall doing C stuff */
173 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
174 idx = value_sub (idx, bound);
177 array = value_coerce_array (array);
180 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
182 struct type *range_type = TYPE_INDEX_TYPE (tarray);
183 LONGEST index = value_as_long (idx);
185 int offset, byte, bit_index;
186 LONGEST lowerbound, upperbound;
187 get_discrete_bounds (range_type, &lowerbound, &upperbound);
188 if (index < lowerbound || index > upperbound)
189 error ("bitstring index out of range");
191 offset = index / TARGET_CHAR_BIT;
192 byte = *((char *) VALUE_CONTENTS (array) + offset);
193 bit_index = index % TARGET_CHAR_BIT;
194 byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index);
195 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
196 VALUE_BITPOS (v) = bit_index;
197 VALUE_BITSIZE (v) = 1;
198 VALUE_LVAL (v) = VALUE_LVAL (array);
199 if (VALUE_LVAL (array) == lval_internalvar)
200 VALUE_LVAL (v) = lval_internalvar_component;
201 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
202 VALUE_OFFSET (v) = offset + VALUE_OFFSET (array);
207 return value_ind (value_add (array, idx));
209 error ("not an array or string");
212 /* Return the value of EXPR[IDX], expr an aggregate rvalue
213 (eg, a vector register). This routine used to promote floats
214 to doubles, but no longer does. */
217 value_subscripted_rvalue (array, idx, lowerbound)
218 value_ptr array, idx;
221 struct type *array_type = check_typedef (VALUE_TYPE (array));
222 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
223 unsigned int elt_size = TYPE_LENGTH (elt_type);
224 LONGEST index = value_as_long (idx);
225 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
228 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
229 error ("no such vector element");
231 v = allocate_value (elt_type);
232 if (VALUE_LAZY (array))
235 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
237 if (VALUE_LVAL (array) == lval_internalvar)
238 VALUE_LVAL (v) = lval_internalvar_component;
240 VALUE_LVAL (v) = VALUE_LVAL (array);
241 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
242 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
246 /* Check to see if either argument is a structure. This is called so
247 we know whether to go ahead with the normal binop or look for a
248 user defined function instead.
250 For now, we do not overload the `=' operator. */
253 binop_user_defined_p (op, arg1, arg2)
255 value_ptr arg1, arg2;
257 struct type *type1, *type2;
258 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
260 type1 = check_typedef (VALUE_TYPE (arg1));
261 type2 = check_typedef (VALUE_TYPE (arg2));
262 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
263 || TYPE_CODE (type2) == TYPE_CODE_STRUCT
264 || (TYPE_CODE (type1) == TYPE_CODE_REF
265 && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT)
266 || (TYPE_CODE (type2) == TYPE_CODE_REF
267 && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT));
270 /* Check to see if argument is a structure. This is called so
271 we know whether to go ahead with the normal unop or look for a
272 user defined function instead.
274 For now, we do not overload the `&' operator. */
277 unop_user_defined_p (op, arg1)
284 type1 = check_typedef (VALUE_TYPE (arg1));
287 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
289 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
290 type1 = TYPE_TARGET_TYPE (type1);
296 /* We know either arg1 or arg2 is a structure, so try to find the right
297 user defined function. Create an argument vector that calls
298 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
299 binary operator which is legal for GNU C++).
301 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
302 is the opcode saying how to modify it. Otherwise, OTHEROP is
306 value_x_binop (arg1, arg2, op, otherop, noside)
307 value_ptr arg1, arg2;
308 enum exp_opcode op, otherop;
321 /* now we know that what we have to do is construct our
322 arg vector and find the right function to call it with. */
324 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
325 error ("Can't do that binary op on that type"); /* FIXME be explicit */
327 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4);
328 argvec[1] = value_addr (arg1);
332 /* make the right function name up */
333 strcpy (tstr, "operator__");
358 case BINOP_BITWISE_AND:
361 case BINOP_BITWISE_IOR:
364 case BINOP_BITWISE_XOR:
367 case BINOP_LOGICAL_AND:
370 case BINOP_LOGICAL_OR:
382 case BINOP_ASSIGN_MODIFY:
400 case BINOP_BITWISE_AND:
403 case BINOP_BITWISE_IOR:
406 case BINOP_BITWISE_XOR:
409 case BINOP_MOD: /* invalid */
411 error ("Invalid binary operation specified.");
414 case BINOP_SUBSCRIPT:
435 case BINOP_MOD: /* invalid */
437 error ("Invalid binary operation specified.");
440 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
446 argvec[1] = argvec[0];
449 if (noside == EVAL_AVOID_SIDE_EFFECTS)
451 struct type *return_type;
453 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
454 return value_zero (return_type, VALUE_LVAL (arg1));
456 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
458 error ("member function %s not found", tstr);
460 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
464 /* We know that arg1 is a structure, so try to find a unary user
465 defined operator that matches the operator in question.
466 Create an argument vector that calls arg1.operator @ (arg1)
467 and return that value (where '@' is (almost) any unary operator which
468 is legal for GNU C++). */
471 value_x_unop (arg1, op, noside)
477 char *ptr, *mangle_ptr;
478 char tstr[13], mangle_tstr[13];
484 /* now we know that what we have to do is construct our
485 arg vector and find the right function to call it with. */
487 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
488 error ("Can't do that unary op on that type"); /* FIXME be explicit */
490 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3);
491 argvec[1] = value_addr (arg1);
494 /* make the right function name up */
495 strcpy (tstr, "operator__");
497 strcpy (mangle_tstr, "__");
498 mangle_ptr = mangle_tstr + 2;
501 case UNOP_PREINCREMENT:
504 case UNOP_PREDECREMENT:
507 case UNOP_POSTINCREMENT:
510 case UNOP_POSTDECREMENT:
513 case UNOP_LOGICAL_NOT:
516 case UNOP_COMPLEMENT:
526 error ("Invalid unary operation specified.");
529 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
535 argvec[1] = argvec[0];
538 if (noside == EVAL_AVOID_SIDE_EFFECTS)
540 struct type *return_type;
542 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
543 return value_zero (return_type, VALUE_LVAL (arg1));
545 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
547 error ("member function %s not found", tstr);
548 return 0; /* For lint -- never reached */
552 /* Concatenate two values with the following conditions:
554 (1) Both values must be either bitstring values or character string
555 values and the resulting value consists of the concatenation of
556 ARG1 followed by ARG2.
560 One value must be an integer value and the other value must be
561 either a bitstring value or character string value, which is
562 to be repeated by the number of times specified by the integer
566 (2) Boolean values are also allowed and are treated as bit string
569 (3) Character values are also allowed and are treated as character
570 string values of length 1.
574 value_concat (arg1, arg2)
575 value_ptr arg1, arg2;
577 register value_ptr inval1, inval2, outval = NULL;
578 int inval1len, inval2len;
582 struct type *type1 = check_typedef (VALUE_TYPE (arg1));
583 struct type *type2 = check_typedef (VALUE_TYPE (arg2));
585 COERCE_VARYING_ARRAY (arg1, type1);
586 COERCE_VARYING_ARRAY (arg2, type2);
588 /* First figure out if we are dealing with two values to be concatenated
589 or a repeat count and a value to be repeated. INVAL1 is set to the
590 first of two concatenated values, or the repeat count. INVAL2 is set
591 to the second of the two concatenated values or the value to be
594 if (TYPE_CODE (type2) == TYPE_CODE_INT)
596 struct type *tmp = type1;
608 /* Now process the input values. */
610 if (TYPE_CODE (type1) == TYPE_CODE_INT)
612 /* We have a repeat count. Validate the second value and then
613 construct a value repeated that many times. */
614 if (TYPE_CODE (type2) == TYPE_CODE_STRING
615 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
617 count = longest_to_int (value_as_long (inval1));
618 inval2len = TYPE_LENGTH (type2);
619 ptr = (char *) alloca (count * inval2len);
620 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
622 inchar = (char) unpack_long (type2,
623 VALUE_CONTENTS (inval2));
624 for (idx = 0; idx < count; idx++)
626 *(ptr + idx) = inchar;
631 for (idx = 0; idx < count; idx++)
633 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
637 outval = value_string (ptr, count * inval2len);
639 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
640 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
642 error ("unimplemented support for bitstring/boolean repeats");
646 error ("can't repeat values of that type");
649 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
650 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
652 /* We have two character strings to concatenate. */
653 if (TYPE_CODE (type2) != TYPE_CODE_STRING
654 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
656 error ("Strings can only be concatenated with other strings.");
658 inval1len = TYPE_LENGTH (type1);
659 inval2len = TYPE_LENGTH (type2);
660 ptr = (char *) alloca (inval1len + inval2len);
661 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
663 *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1));
667 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
669 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
672 (char) unpack_long (type2, VALUE_CONTENTS (inval2));
676 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
678 outval = value_string (ptr, inval1len + inval2len);
680 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
681 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
683 /* We have two bitstrings to concatenate. */
684 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
685 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
687 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
689 error ("unimplemented support for bitstring/boolean concatenation.");
693 /* We don't know how to concatenate these operands. */
694 error ("illegal operands for concatenation.");
701 /* Perform a binary operation on two operands which have reasonable
702 representations as integers or floats. This includes booleans,
703 characters, integers, or floats.
704 Does not support addition and subtraction on pointers;
705 use value_add or value_sub if you want to handle those possibilities. */
708 value_binop (arg1, arg2, op)
709 value_ptr arg1, arg2;
712 register value_ptr val;
713 struct type *type1, *type2;
719 type1 = check_typedef (VALUE_TYPE (arg1));
720 type2 = check_typedef (VALUE_TYPE (arg2));
722 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
723 && TYPE_CODE (type1) != TYPE_CODE_CHAR
724 && TYPE_CODE (type1) != TYPE_CODE_INT
725 && TYPE_CODE (type1) != TYPE_CODE_BOOL
726 && TYPE_CODE (type1) != TYPE_CODE_RANGE)
728 (TYPE_CODE (type2) != TYPE_CODE_FLT
729 && TYPE_CODE (type2) != TYPE_CODE_CHAR
730 && TYPE_CODE (type2) != TYPE_CODE_INT
731 && TYPE_CODE (type2) != TYPE_CODE_BOOL
732 && TYPE_CODE (type2) != TYPE_CODE_RANGE))
733 error ("Argument to arithmetic operation not a number or boolean.");
735 if (TYPE_CODE (type1) == TYPE_CODE_FLT
737 TYPE_CODE (type2) == TYPE_CODE_FLT)
739 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
740 in target format. real.c in GCC probably has the necessary
742 DOUBLEST v1, v2, v = 0;
743 v1 = value_as_double (arg1);
744 v2 = value_as_double (arg2);
766 error ("Cannot perform exponentiation: %s", strerror (errno));
770 error ("Integer-only operation on floating point number.");
773 /* If either arg was long double, make sure that value is also long
776 if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT
777 || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT)
778 val = allocate_value (builtin_type_long_double);
780 val = allocate_value (builtin_type_double);
782 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
785 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
787 TYPE_CODE (type2) == TYPE_CODE_BOOL)
789 LONGEST v1, v2, v = 0;
790 v1 = value_as_long (arg1);
791 v2 = value_as_long (arg2);
795 case BINOP_BITWISE_AND:
799 case BINOP_BITWISE_IOR:
803 case BINOP_BITWISE_XOR:
816 error ("Invalid operation on booleans.");
819 val = allocate_value (type1);
820 store_signed_integer (VALUE_CONTENTS_RAW (val),
825 /* Integral operations here. */
826 /* FIXME: Also mixed integral/booleans, with result an integer. */
827 /* FIXME: This implements ANSI C rules (also correct for C++).
828 What about FORTRAN and chill? */
830 unsigned int promoted_len1 = TYPE_LENGTH (type1);
831 unsigned int promoted_len2 = TYPE_LENGTH (type2);
832 int is_unsigned1 = TYPE_UNSIGNED (type1);
833 int is_unsigned2 = TYPE_UNSIGNED (type2);
834 unsigned int result_len;
835 int unsigned_operation;
837 /* Determine type length and signedness after promotion for
839 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
842 promoted_len1 = TYPE_LENGTH (builtin_type_int);
844 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
847 promoted_len2 = TYPE_LENGTH (builtin_type_int);
850 /* Determine type length of the result, and if the operation should
852 Use the signedness of the operand with the greater length.
853 If both operands are of equal length, use unsigned operation
854 if one of the operands is unsigned. */
855 if (promoted_len1 > promoted_len2)
857 unsigned_operation = is_unsigned1;
858 result_len = promoted_len1;
860 else if (promoted_len2 > promoted_len1)
862 unsigned_operation = is_unsigned2;
863 result_len = promoted_len2;
867 unsigned_operation = is_unsigned1 || is_unsigned2;
868 result_len = promoted_len1;
871 if (unsigned_operation)
873 ULONGEST v1, v2, v = 0;
874 v1 = (ULONGEST) value_as_long (arg1);
875 v2 = (ULONGEST) value_as_long (arg2);
877 /* Truncate values to the type length of the result. */
878 if (result_len < sizeof (ULONGEST))
880 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
881 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
905 error ("Cannot perform exponentiation: %s", strerror (errno));
913 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
914 v1 mod 0 has a defined value, v1. */
915 /* Chill specifies that v2 must be > 0, so check for that. */
916 if (current_language->la_language == language_chill
917 && value_as_long (arg2) <= 0)
919 error ("Second operand of MOD must be greater than zero.");
928 /* Note floor(v1/v2) == v1/v2 for unsigned. */
941 case BINOP_BITWISE_AND:
945 case BINOP_BITWISE_IOR:
949 case BINOP_BITWISE_XOR:
953 case BINOP_LOGICAL_AND:
957 case BINOP_LOGICAL_OR:
962 v = v1 < v2 ? v1 : v2;
966 v = v1 > v2 ? v1 : v2;
982 error ("Invalid binary operation on numbers.");
985 /* This is a kludge to get around the fact that we don't
986 know how to determine the result type from the types of
987 the operands. (I'm not really sure how much we feel the
988 need to duplicate the exact rules of the current
989 language. They can get really hairy. But not to do so
990 makes it hard to document just what we *do* do). */
992 /* Can't just call init_type because we wouldn't know what
993 name to give the type. */
995 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
996 ? builtin_type_unsigned_long_long
997 : builtin_type_unsigned_long);
998 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
999 TYPE_LENGTH (VALUE_TYPE (val)),
1004 LONGEST v1, v2, v = 0;
1005 v1 = value_as_long (arg1);
1006 v2 = value_as_long (arg2);
1029 error ("Cannot perform exponentiation: %s", strerror (errno));
1037 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1038 X mod 0 has a defined value, X. */
1039 /* Chill specifies that v2 must be > 0, so check for that. */
1040 if (current_language->la_language == language_chill
1043 error ("Second operand of MOD must be greater than zero.");
1052 /* Compute floor. */
1053 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1069 case BINOP_BITWISE_AND:
1073 case BINOP_BITWISE_IOR:
1077 case BINOP_BITWISE_XOR:
1081 case BINOP_LOGICAL_AND:
1085 case BINOP_LOGICAL_OR:
1090 v = v1 < v2 ? v1 : v2;
1094 v = v1 > v2 ? v1 : v2;
1106 error ("Invalid binary operation on numbers.");
1109 /* This is a kludge to get around the fact that we don't
1110 know how to determine the result type from the types of
1111 the operands. (I'm not really sure how much we feel the
1112 need to duplicate the exact rules of the current
1113 language. They can get really hairy. But not to do so
1114 makes it hard to document just what we *do* do). */
1116 /* Can't just call init_type because we wouldn't know what
1117 name to give the type. */
1118 val = allocate_value
1119 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1120 ? builtin_type_long_long
1121 : builtin_type_long);
1122 store_signed_integer (VALUE_CONTENTS_RAW (val),
1123 TYPE_LENGTH (VALUE_TYPE (val)),
1131 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1134 value_logical_not (arg1)
1141 COERCE_NUMBER (arg1);
1142 type1 = check_typedef (VALUE_TYPE (arg1));
1144 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1145 return 0 == value_as_double (arg1);
1147 len = TYPE_LENGTH (type1);
1148 p = VALUE_CONTENTS (arg1);
1159 /* Perform a comparison on two string values (whose content are not
1160 necessarily null terminated) based on their length */
1163 value_strcmp (arg1, arg2)
1164 register value_ptr arg1, arg2;
1166 int len1 = TYPE_LENGTH (VALUE_TYPE (arg1));
1167 int len2 = TYPE_LENGTH (VALUE_TYPE (arg2));
1168 char *s1 = VALUE_CONTENTS (arg1);
1169 char *s2 = VALUE_CONTENTS (arg2);
1170 int i, len = len1 < len2 ? len1 : len2;
1172 for (i = 0; i < len; i++)
1176 else if (s1[i] > s2[i])
1184 else if (len1 > len2)
1190 /* Simulate the C operator == by returning a 1
1191 iff ARG1 and ARG2 have equal contents. */
1194 value_equal (arg1, arg2)
1195 register value_ptr arg1, arg2;
1199 register char *p1, *p2;
1200 struct type *type1, *type2;
1201 enum type_code code1;
1202 enum type_code code2;
1204 COERCE_NUMBER (arg1);
1205 COERCE_NUMBER (arg2);
1207 type1 = check_typedef (VALUE_TYPE (arg1));
1208 type2 = check_typedef (VALUE_TYPE (arg2));
1209 code1 = TYPE_CODE (type1);
1210 code2 = TYPE_CODE (type2);
1212 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1213 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1214 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1216 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1217 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1218 return value_as_double (arg1) == value_as_double (arg2);
1220 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1222 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1223 return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
1224 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1225 return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
1227 else if (code1 == code2
1228 && ((len = (int) TYPE_LENGTH (type1))
1229 == (int) TYPE_LENGTH (type2)))
1231 p1 = VALUE_CONTENTS (arg1);
1232 p2 = VALUE_CONTENTS (arg2);
1240 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1242 return value_strcmp (arg1, arg2) == 0;
1246 error ("Invalid type combination in equality test.");
1247 return 0; /* For lint -- never reached */
1251 /* Simulate the C operator < by returning 1
1252 iff ARG1's contents are less than ARG2's. */
1255 value_less (arg1, arg2)
1256 register value_ptr arg1, arg2;
1258 register enum type_code code1;
1259 register enum type_code code2;
1260 struct type *type1, *type2;
1262 COERCE_NUMBER (arg1);
1263 COERCE_NUMBER (arg2);
1265 type1 = check_typedef (VALUE_TYPE (arg1));
1266 type2 = check_typedef (VALUE_TYPE (arg2));
1267 code1 = TYPE_CODE (type1);
1268 code2 = TYPE_CODE (type2);
1270 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1271 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1272 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1274 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1275 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1276 return value_as_double (arg1) < value_as_double (arg2);
1277 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1278 return value_as_pointer (arg1) < value_as_pointer (arg2);
1280 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1282 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1283 return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
1284 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1285 return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
1286 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1287 return value_strcmp (arg1, arg2) < 0;
1290 error ("Invalid type combination in ordering comparison.");
1295 /* The unary operators - and ~. Both free the argument ARG1. */
1299 register value_ptr arg1;
1301 register struct type *type;
1302 register struct type *result_type = VALUE_TYPE (arg1);
1307 type = check_typedef (VALUE_TYPE (arg1));
1309 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1310 return value_from_double (result_type, -value_as_double (arg1));
1311 else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_BOOL)
1313 /* Perform integral promotion for ANSI C/C++.
1314 FIXME: What about FORTRAN and chill ? */
1315 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1316 result_type = builtin_type_int;
1318 return value_from_longest (result_type, -value_as_long (arg1));
1322 error ("Argument to negate operation not a number.");
1323 return 0; /* For lint -- never reached */
1328 value_complement (arg1)
1329 register value_ptr arg1;
1331 register struct type *type;
1332 register struct type *result_type = VALUE_TYPE (arg1);
1338 type = check_typedef (VALUE_TYPE (arg1));
1340 typecode = TYPE_CODE (type);
1341 if ((typecode != TYPE_CODE_INT) && (typecode != TYPE_CODE_BOOL))
1342 error ("Argument to complement operation not an integer or boolean.");
1344 /* Perform integral promotion for ANSI C/C++.
1345 FIXME: What about FORTRAN ? */
1346 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1347 result_type = builtin_type_int;
1349 return value_from_longest (result_type, ~value_as_long (arg1));
1352 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1353 and whose VALUE_CONTENTS is valaddr.
1354 Return -1 if out of range, -2 other error. */
1357 value_bit_index (type, valaddr, index)
1362 LONGEST low_bound, high_bound;
1365 struct type *range = TYPE_FIELD_TYPE (type, 0);
1366 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1368 if (index < low_bound || index > high_bound)
1370 rel_index = index - low_bound;
1371 word = unpack_long (builtin_type_unsigned_char,
1372 valaddr + (rel_index / TARGET_CHAR_BIT));
1373 rel_index %= TARGET_CHAR_BIT;
1374 if (BITS_BIG_ENDIAN)
1375 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1376 return (word >> rel_index) & 1;
1380 value_in (element, set)
1381 value_ptr element, set;
1384 struct type *settype = check_typedef (VALUE_TYPE (set));
1385 struct type *eltype = check_typedef (VALUE_TYPE (element));
1386 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1387 eltype = TYPE_TARGET_TYPE (eltype);
1388 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1389 error ("Second argument of 'IN' has wrong type");
1390 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1391 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1392 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1393 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1394 error ("First argument of 'IN' has wrong type");
1395 member = value_bit_index (settype, VALUE_CONTENTS (set),
1396 value_as_long (element));
1398 error ("First argument of 'IN' not in range");
1399 return value_from_longest (LA_BOOL_TYPE, member);
1403 _initialize_valarith ()