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"
32 /* Define whether or not the C operator '/' truncates towards zero for
33 differently signed operands (truncation direction is undefined in C). */
35 #ifndef TRUNCATION_TOWARDS_ZERO
36 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
39 static value_ptr value_subscripted_rvalue
PARAMS ((value_ptr
, value_ptr
, int));
41 void _initialize_valarith
PARAMS ((void));
45 value_add (arg1
, 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_longest (valptrtype
,
82 value_as_long (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 (arg1
, arg2
)
95 struct type
*type1
, *type2
;
98 type1
= check_typedef (VALUE_TYPE (arg1
));
99 type2
= check_typedef (VALUE_TYPE (arg2
));
101 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
103 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
105 /* pointer - integer. */
106 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
107 return value_from_longest
109 value_as_long (arg1
) - (sz
* value_as_long (arg2
)));
111 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
112 && TYPE_LENGTH (TYPE_TARGET_TYPE (type1
))
113 == TYPE_LENGTH (TYPE_TARGET_TYPE (type2
)))
115 /* pointer to <type x> - pointer to <type x>. */
116 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
117 return value_from_longest
118 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
119 (value_as_long (arg1
) - value_as_long (arg2
)) / sz
);
124 First argument of `-' is a pointer and second argument is neither\n\
125 an integer nor a pointer of the same type.");
129 return value_binop (arg1
, arg2
, BINOP_SUB
);
132 /* Return the value of ARRAY[IDX].
133 See comments in value_coerce_array() for rationale for reason for
134 doing lower bounds adjustment here rather than there.
135 FIXME: Perhaps we should validate that the index is valid and if
136 verbosity is set, warn about invalid indices (but still use them). */
139 value_subscript (array
, idx
)
140 value_ptr array
, idx
;
143 int c_style
= current_language
->c_style_arrays
;
147 tarray
= check_typedef (VALUE_TYPE (array
));
148 COERCE_VARYING_ARRAY (array
, tarray
);
150 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
151 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
153 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
154 LONGEST lowerbound
, upperbound
;
155 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
157 if (VALUE_LVAL (array
) != lval_memory
)
158 return value_subscripted_rvalue (array
, idx
, lowerbound
);
162 LONGEST index
= value_as_long (idx
);
163 if (index
>= lowerbound
&& index
<= upperbound
)
164 return value_subscripted_rvalue (array
, idx
, lowerbound
);
165 warning ("array or string index out of range");
166 /* fall doing C stuff */
172 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
173 idx
= value_sub (idx
, bound
);
176 array
= value_coerce_array (array
);
179 if (TYPE_CODE (tarray
) == TYPE_CODE_BITSTRING
)
181 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
182 LONGEST index
= value_as_long (idx
);
184 int offset
, byte
, bit_index
;
185 LONGEST lowerbound
, upperbound
;
186 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
187 if (index
< lowerbound
|| index
> upperbound
)
188 error ("bitstring index out of range");
190 offset
= index
/ TARGET_CHAR_BIT
;
191 byte
= *((char *) VALUE_CONTENTS (array
) + offset
);
192 bit_index
= index
% TARGET_CHAR_BIT
;
193 byte
>>= (BITS_BIG_ENDIAN
? TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
194 v
= value_from_longest (LA_BOOL_TYPE
, byte
& 1);
195 VALUE_BITPOS (v
) = bit_index
;
196 VALUE_BITSIZE (v
) = 1;
197 VALUE_LVAL (v
) = VALUE_LVAL (array
);
198 if (VALUE_LVAL (array
) == lval_internalvar
)
199 VALUE_LVAL (v
) = lval_internalvar_component
;
200 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
201 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (array
);
206 return value_ind (value_add (array
, idx
));
208 error ("not an array or string");
211 /* Return the value of EXPR[IDX], expr an aggregate rvalue
212 (eg, a vector register). This routine used to promote floats
213 to doubles, but no longer does. */
216 value_subscripted_rvalue (array
, idx
, lowerbound
)
217 value_ptr array
, idx
;
220 struct type
*array_type
= check_typedef (VALUE_TYPE (array
));
221 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
222 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
223 LONGEST index
= value_as_long (idx
);
224 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
227 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
228 error ("no such vector element");
230 v
= allocate_value (elt_type
);
231 if (VALUE_LAZY (array
))
234 memcpy (VALUE_CONTENTS (v
), VALUE_CONTENTS (array
) + elt_offs
, elt_size
);
236 if (VALUE_LVAL (array
) == lval_internalvar
)
237 VALUE_LVAL (v
) = lval_internalvar_component
;
239 VALUE_LVAL (v
) = VALUE_LVAL (array
);
240 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
241 VALUE_OFFSET (v
) = VALUE_OFFSET (array
) + elt_offs
;
245 /* Check to see if either argument is a structure. This is called so
246 we know whether to go ahead with the normal binop or look for a
247 user defined function instead.
249 For now, we do not overload the `=' operator. */
252 binop_user_defined_p (op
, arg1
, arg2
)
254 value_ptr arg1
, arg2
;
256 struct type
*type1
, *type2
;
257 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
259 type1
= check_typedef (VALUE_TYPE (arg1
));
260 type2
= check_typedef (VALUE_TYPE (arg2
));
261 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
262 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
263 || (TYPE_CODE (type1
) == TYPE_CODE_REF
264 && TYPE_CODE (TYPE_TARGET_TYPE (type1
)) == TYPE_CODE_STRUCT
)
265 || (TYPE_CODE (type2
) == TYPE_CODE_REF
266 && TYPE_CODE (TYPE_TARGET_TYPE (type2
)) == TYPE_CODE_STRUCT
));
269 /* Check to see if argument is a structure. This is called so
270 we know whether to go ahead with the normal unop or look for a
271 user defined function instead.
273 For now, we do not overload the `&' operator. */
276 unop_user_defined_p (op
, arg1
)
283 type1
= check_typedef (VALUE_TYPE (arg1
));
286 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
288 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
289 type1
= TYPE_TARGET_TYPE (type1
);
295 /* We know either arg1 or arg2 is a structure, so try to find the right
296 user defined function. Create an argument vector that calls
297 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
298 binary operator which is legal for GNU C++).
300 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
301 is the opcode saying how to modify it. Otherwise, OTHEROP is
305 value_x_binop (arg1
, arg2
, op
, otherop
, noside
)
306 value_ptr arg1
, arg2
;
307 enum exp_opcode op
, otherop
;
320 /* now we know that what we have to do is construct our
321 arg vector and find the right function to call it with. */
323 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
324 error ("Can't do that binary op on that type"); /* FIXME be explicit */
326 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * 4);
327 argvec
[1] = value_addr (arg1
);
331 /* make the right function name up */
332 strcpy (tstr
, "operator__");
357 case BINOP_BITWISE_AND
:
360 case BINOP_BITWISE_IOR
:
363 case BINOP_BITWISE_XOR
:
366 case BINOP_LOGICAL_AND
:
369 case BINOP_LOGICAL_OR
:
381 case BINOP_ASSIGN_MODIFY
:
399 case BINOP_BITWISE_AND
:
402 case BINOP_BITWISE_IOR
:
405 case BINOP_BITWISE_XOR
:
408 case BINOP_MOD
: /* invalid */
410 error ("Invalid binary operation specified.");
413 case BINOP_SUBSCRIPT
:
434 case BINOP_MOD
: /* invalid */
436 error ("Invalid binary operation specified.");
439 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
445 argvec
[1] = argvec
[0];
448 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
450 struct type
*return_type
;
452 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
453 return value_zero (return_type
, VALUE_LVAL (arg1
));
455 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
457 error ("member function %s not found", tstr
);
459 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
463 /* We know that arg1 is a structure, so try to find a unary user
464 defined operator that matches the operator in question.
465 Create an argument vector that calls arg1.operator @ (arg1)
466 and return that value (where '@' is (almost) any unary operator which
467 is legal for GNU C++). */
470 value_x_unop (arg1
, op
, noside
)
476 char *ptr
, *mangle_ptr
;
477 char tstr
[13], mangle_tstr
[13];
483 /* now we know that what we have to do is construct our
484 arg vector and find the right function to call it with. */
486 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
487 error ("Can't do that unary op on that type"); /* FIXME be explicit */
489 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * 3);
490 argvec
[1] = value_addr (arg1
);
493 /* make the right function name up */
494 strcpy (tstr
, "operator__");
496 strcpy (mangle_tstr
, "__");
497 mangle_ptr
= mangle_tstr
+ 2;
500 case UNOP_PREINCREMENT
:
503 case UNOP_PREDECREMENT
:
506 case UNOP_POSTINCREMENT
:
509 case UNOP_POSTDECREMENT
:
512 case UNOP_LOGICAL_NOT
:
515 case UNOP_COMPLEMENT
:
525 error ("Invalid unary operation specified.");
528 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
534 argvec
[1] = argvec
[0];
537 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
539 struct type
*return_type
;
541 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
542 return value_zero (return_type
, VALUE_LVAL (arg1
));
544 return call_function_by_hand (argvec
[0], 1 - static_memfuncp
, argvec
+ 1);
546 error ("member function %s not found", tstr
);
547 return 0; /* For lint -- never reached */
551 /* Concatenate two values with the following conditions:
553 (1) Both values must be either bitstring values or character string
554 values and the resulting value consists of the concatenation of
555 ARG1 followed by ARG2.
559 One value must be an integer value and the other value must be
560 either a bitstring value or character string value, which is
561 to be repeated by the number of times specified by the integer
565 (2) Boolean values are also allowed and are treated as bit string
568 (3) Character values are also allowed and are treated as character
569 string values of length 1.
573 value_concat (arg1
, arg2
)
574 value_ptr arg1
, arg2
;
576 register value_ptr inval1
, inval2
, outval
;
577 int inval1len
, inval2len
;
581 struct type
*type1
= check_typedef (VALUE_TYPE (arg1
));
582 struct type
*type2
= check_typedef (VALUE_TYPE (arg2
));
584 COERCE_VARYING_ARRAY (arg1
, type1
);
585 COERCE_VARYING_ARRAY (arg2
, type2
);
587 /* First figure out if we are dealing with two values to be concatenated
588 or a repeat count and a value to be repeated. INVAL1 is set to the
589 first of two concatenated values, or the repeat count. INVAL2 is set
590 to the second of the two concatenated values or the value to be
593 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
595 struct type
*tmp
= type1
;
607 /* Now process the input values. */
609 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
611 /* We have a repeat count. Validate the second value and then
612 construct a value repeated that many times. */
613 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
614 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
616 count
= longest_to_int (value_as_long (inval1
));
617 inval2len
= TYPE_LENGTH (type2
);
618 ptr
= (char *) alloca (count
* inval2len
);
619 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
621 inchar
= (char) unpack_long (type2
,
622 VALUE_CONTENTS (inval2
));
623 for (idx
= 0; idx
< count
; idx
++)
625 *(ptr
+ idx
) = inchar
;
630 for (idx
= 0; idx
< count
; idx
++)
632 memcpy (ptr
+ (idx
* inval2len
), VALUE_CONTENTS (inval2
),
636 outval
= value_string (ptr
, count
* inval2len
);
638 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
639 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
641 error ("unimplemented support for bitstring/boolean repeats");
645 error ("can't repeat values of that type");
648 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
649 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
651 /* We have two character strings to concatenate. */
652 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
653 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
655 error ("Strings can only be concatenated with other strings.");
657 inval1len
= TYPE_LENGTH (type1
);
658 inval2len
= TYPE_LENGTH (type2
);
659 ptr
= (char *) alloca (inval1len
+ inval2len
);
660 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
662 *ptr
= (char) unpack_long (type1
, VALUE_CONTENTS (inval1
));
666 memcpy (ptr
, VALUE_CONTENTS (inval1
), inval1len
);
668 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
671 (char) unpack_long (type2
, VALUE_CONTENTS (inval2
));
675 memcpy (ptr
+ inval1len
, VALUE_CONTENTS (inval2
), inval2len
);
677 outval
= value_string (ptr
, inval1len
+ inval2len
);
679 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
680 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
682 /* We have two bitstrings to concatenate. */
683 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
684 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
686 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
688 error ("unimplemented support for bitstring/boolean concatenation.");
692 /* We don't know how to concatenate these operands. */
693 error ("illegal operands for concatenation.");
700 /* Perform a binary operation on two operands which have reasonable
701 representations as integers or floats. This includes booleans,
702 characters, integers, or floats.
703 Does not support addition and subtraction on pointers;
704 use value_add or value_sub if you want to handle those possibilities. */
707 value_binop (arg1
, arg2
, op
)
708 value_ptr arg1
, arg2
;
711 register value_ptr val
;
712 struct type
*type1
, *type2
;
718 type1
= check_typedef (VALUE_TYPE (arg1
));
719 type2
= check_typedef (VALUE_TYPE (arg2
));
721 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
722 && TYPE_CODE (type1
) != TYPE_CODE_CHAR
723 && TYPE_CODE (type1
) != TYPE_CODE_INT
724 && TYPE_CODE (type1
) != TYPE_CODE_BOOL
725 && TYPE_CODE (type1
) != TYPE_CODE_RANGE
)
727 (TYPE_CODE (type2
) != TYPE_CODE_FLT
728 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
729 && TYPE_CODE (type2
) != TYPE_CODE_INT
730 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
731 && TYPE_CODE (type2
) != TYPE_CODE_RANGE
))
732 error ("Argument to arithmetic operation not a number or boolean.");
734 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
736 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
738 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
739 in target format. real.c in GCC probably has the necessary
742 v1
= value_as_double (arg1
);
743 v2
= value_as_double (arg2
);
763 error ("Integer-only operation on floating point number.");
766 /* If either arg was long double, make sure that value is also long
769 if (TYPE_LENGTH (type1
) * 8 > TARGET_DOUBLE_BIT
770 || TYPE_LENGTH (type2
) * 8 > TARGET_DOUBLE_BIT
)
771 val
= allocate_value (builtin_type_long_double
);
773 val
= allocate_value (builtin_type_double
);
775 store_floating (VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (VALUE_TYPE (val
)),
778 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
780 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
783 v1
= value_as_long (arg1
);
784 v2
= value_as_long (arg2
);
788 case BINOP_BITWISE_AND
:
792 case BINOP_BITWISE_IOR
:
796 case BINOP_BITWISE_XOR
:
801 error ("Invalid operation on booleans.");
804 val
= allocate_value (type1
);
805 store_signed_integer (VALUE_CONTENTS_RAW (val
),
810 /* Integral operations here. */
811 /* FIXME: Also mixed integral/booleans, with result an integer. */
812 /* FIXME: This implements ANSI C rules (also correct for C++).
813 What about FORTRAN and chill? */
815 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
816 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
817 int is_unsigned1
= TYPE_UNSIGNED (type1
);
818 int is_unsigned2
= TYPE_UNSIGNED (type2
);
819 unsigned int result_len
;
820 int unsigned_operation
;
822 /* Determine type length and signedness after promotion for
824 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
827 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
829 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
832 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
835 /* Determine type length of the result, and if the operation should
837 Use the signedness of the operand with the greater length.
838 If both operands are of equal length, use unsigned operation
839 if one of the operands is unsigned. */
840 if (promoted_len1
> promoted_len2
)
842 unsigned_operation
= is_unsigned1
;
843 result_len
= promoted_len1
;
845 else if (promoted_len2
> promoted_len1
)
847 unsigned_operation
= is_unsigned2
;
848 result_len
= promoted_len2
;
852 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
853 result_len
= promoted_len1
;
856 if (unsigned_operation
)
859 v1
= (ULONGEST
) value_as_long (arg1
);
860 v2
= (ULONGEST
) value_as_long (arg2
);
862 /* Truncate values to the type length of the result. */
863 if (result_len
< sizeof (ULONGEST
))
865 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
866 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
892 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
893 v1 mod 0 has a defined value, v1. */
894 /* Chill specifies that v2 must be > 0, so check for that. */
895 if (current_language
->la_language
== language_chill
896 && value_as_long (arg2
) <= 0)
898 error ("Second operand of MOD must be greater than zero.");
907 /* Note floor(v1/v2) == v1/v2 for unsigned. */
920 case BINOP_BITWISE_AND
:
924 case BINOP_BITWISE_IOR
:
928 case BINOP_BITWISE_XOR
:
932 case BINOP_LOGICAL_AND
:
936 case BINOP_LOGICAL_OR
:
941 v
= v1
< v2
? v1
: v2
;
945 v
= v1
> v2
? v1
: v2
;
957 error ("Invalid binary operation on numbers.");
960 /* This is a kludge to get around the fact that we don't
961 know how to determine the result type from the types of
962 the operands. (I'm not really sure how much we feel the
963 need to duplicate the exact rules of the current
964 language. They can get really hairy. But not to do so
965 makes it hard to document just what we *do* do). */
967 /* Can't just call init_type because we wouldn't know what
968 name to give the type. */
970 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
971 ? builtin_type_unsigned_long_long
972 : builtin_type_unsigned_long
);
973 store_unsigned_integer (VALUE_CONTENTS_RAW (val
),
974 TYPE_LENGTH (VALUE_TYPE (val
)),
980 v1
= value_as_long (arg1
);
981 v2
= value_as_long (arg2
);
1006 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1007 X mod 0 has a defined value, X. */
1008 /* Chill specifies that v2 must be > 0, so check for that. */
1009 if (current_language
->la_language
== language_chill
1012 error ("Second operand of MOD must be greater than zero.");
1021 /* Compute floor. */
1022 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1038 case BINOP_BITWISE_AND
:
1042 case BINOP_BITWISE_IOR
:
1046 case BINOP_BITWISE_XOR
:
1050 case BINOP_LOGICAL_AND
:
1054 case BINOP_LOGICAL_OR
:
1059 v
= v1
< v2
? v1
: v2
;
1063 v
= v1
> v2
? v1
: v2
;
1075 error ("Invalid binary operation on numbers.");
1078 /* This is a kludge to get around the fact that we don't
1079 know how to determine the result type from the types of
1080 the operands. (I'm not really sure how much we feel the
1081 need to duplicate the exact rules of the current
1082 language. They can get really hairy. But not to do so
1083 makes it hard to document just what we *do* do). */
1085 /* Can't just call init_type because we wouldn't know what
1086 name to give the type. */
1087 val
= allocate_value
1088 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1089 ? builtin_type_long_long
1090 : builtin_type_long
);
1091 store_signed_integer (VALUE_CONTENTS_RAW (val
),
1092 TYPE_LENGTH (VALUE_TYPE (val
)),
1100 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1103 value_logical_not (arg1
)
1110 COERCE_NUMBER (arg1
);
1111 type1
= check_typedef (VALUE_TYPE (arg1
));
1113 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1114 return 0 == value_as_double (arg1
);
1116 len
= TYPE_LENGTH (type1
);
1117 p
= VALUE_CONTENTS (arg1
);
1128 /* Simulate the C operator == by returning a 1
1129 iff ARG1 and ARG2 have equal contents. */
1132 value_equal (arg1
, arg2
)
1133 register value_ptr arg1
, arg2
;
1137 register char *p1
, *p2
;
1138 struct type
*type1
, *type2
;
1139 enum type_code code1
;
1140 enum type_code code2
;
1142 COERCE_NUMBER (arg1
);
1143 COERCE_NUMBER (arg2
);
1145 type1
= check_typedef (VALUE_TYPE (arg1
));
1146 type2
= check_typedef (VALUE_TYPE (arg2
));
1147 code1
= TYPE_CODE (type1
);
1148 code2
= TYPE_CODE (type2
);
1150 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1151 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1152 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1154 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1155 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1156 return value_as_double (arg1
) == value_as_double (arg2
);
1158 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1160 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1161 return value_as_pointer (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1162 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1163 return (CORE_ADDR
) value_as_long (arg1
) == value_as_pointer (arg2
);
1165 else if (code1
== code2
1166 && ((len
= (int) TYPE_LENGTH (type1
))
1167 == (int) TYPE_LENGTH (type2
)))
1169 p1
= VALUE_CONTENTS (arg1
);
1170 p2
= VALUE_CONTENTS (arg2
);
1180 error ("Invalid type combination in equality test.");
1181 return 0; /* For lint -- never reached */
1185 /* Simulate the C operator < by returning 1
1186 iff ARG1's contents are less than ARG2's. */
1189 value_less (arg1
, arg2
)
1190 register value_ptr arg1
, arg2
;
1192 register enum type_code code1
;
1193 register enum type_code code2
;
1194 struct type
*type1
, *type2
;
1196 COERCE_NUMBER (arg1
);
1197 COERCE_NUMBER (arg2
);
1199 type1
= check_typedef (VALUE_TYPE (arg1
));
1200 type2
= check_typedef (VALUE_TYPE (arg2
));
1201 code1
= TYPE_CODE (type1
);
1202 code2
= TYPE_CODE (type2
);
1204 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1205 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1206 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1208 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1209 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1210 return value_as_double (arg1
) < value_as_double (arg2
);
1211 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1212 return value_as_pointer (arg1
) < value_as_pointer (arg2
);
1214 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1216 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1217 return value_as_pointer (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1218 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1219 return (CORE_ADDR
) value_as_long (arg1
) < value_as_pointer (arg2
);
1223 error ("Invalid type combination in ordering comparison.");
1228 /* The unary operators - and ~. Both free the argument ARG1. */
1232 register value_ptr arg1
;
1234 register struct type
*type
;
1235 register struct type
*result_type
= VALUE_TYPE (arg1
);
1240 type
= check_typedef (VALUE_TYPE (arg1
));
1242 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1243 return value_from_double (result_type
, -value_as_double (arg1
));
1244 else if (TYPE_CODE (type
) == TYPE_CODE_INT
|| TYPE_CODE (type
) == TYPE_CODE_BOOL
)
1246 /* Perform integral promotion for ANSI C/C++.
1247 FIXME: What about FORTRAN and chill ? */
1248 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1249 result_type
= builtin_type_int
;
1251 return value_from_longest (result_type
, -value_as_long (arg1
));
1255 error ("Argument to negate operation not a number.");
1256 return 0; /* For lint -- never reached */
1261 value_complement (arg1
)
1262 register value_ptr arg1
;
1264 register struct type
*type
;
1265 register struct type
*result_type
= VALUE_TYPE (arg1
);
1271 type
= check_typedef (VALUE_TYPE (arg1
));
1273 typecode
= TYPE_CODE (type
);
1274 if ((typecode
!= TYPE_CODE_INT
) && (typecode
!= TYPE_CODE_BOOL
))
1275 error ("Argument to complement operation not an integer or boolean.");
1277 /* Perform integral promotion for ANSI C/C++.
1278 FIXME: What about FORTRAN ? */
1279 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1280 result_type
= builtin_type_int
;
1282 return value_from_longest (result_type
, ~value_as_long (arg1
));
1285 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1286 and whose VALUE_CONTENTS is valaddr.
1287 Return -1 if out of range, -2 other error. */
1290 value_bit_index (type
, valaddr
, index
)
1295 LONGEST low_bound
, high_bound
;
1298 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1299 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1301 if (index
< low_bound
|| index
> high_bound
)
1303 rel_index
= index
- low_bound
;
1304 word
= unpack_long (builtin_type_unsigned_char
,
1305 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1306 rel_index
%= TARGET_CHAR_BIT
;
1307 if (BITS_BIG_ENDIAN
)
1308 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1309 return (word
>> rel_index
) & 1;
1313 value_in (element
, set
)
1314 value_ptr element
, set
;
1317 struct type
*settype
= check_typedef (VALUE_TYPE (set
));
1318 struct type
*eltype
= check_typedef (VALUE_TYPE (element
));
1319 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1320 eltype
= TYPE_TARGET_TYPE (eltype
);
1321 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1322 error ("Second argument of 'IN' has wrong type");
1323 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1324 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1325 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1326 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1327 error ("First argument of 'IN' has wrong type");
1328 member
= value_bit_index (settype
, VALUE_CONTENTS (set
),
1329 value_as_long (element
));
1331 error ("First argument of 'IN' not in range");
1332 return value_from_longest (LA_BOOL_TYPE
, member
);
1336 _initialize_valarith ()