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);
47 value_add (struct value
*arg1
, struct value
*arg2
)
52 struct type
*type1
, *type2
, *valptrtype
;
56 type1
= check_typedef (VALUE_TYPE (arg1
));
57 type2
= check_typedef (VALUE_TYPE (arg2
));
59 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
60 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
62 (TYPE_CODE (type1
) == TYPE_CODE_INT
63 || TYPE_CODE (type2
) == TYPE_CODE_INT
))
64 /* Exactly one argument is a pointer, and one is an integer. */
68 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
80 len
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype
)));
82 len
= 1; /* For (void *) */
83 retval
= value_from_pointer (valptrtype
,
84 value_as_address (valptr
)
85 + (len
* value_as_long (valint
)));
86 VALUE_BFD_SECTION (retval
) = VALUE_BFD_SECTION (valptr
);
90 return value_binop (arg1
, arg2
, BINOP_ADD
);
94 value_sub (struct value
*arg1
, struct value
*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_pointer (type1
,
109 (value_as_address (arg1
)
110 - (sz
* value_as_long (arg2
))));
112 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
113 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
114 == TYPE_LENGTH (check_typedef (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 (struct value
*array
, struct value
*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. */
215 static struct value
*
216 value_subscripted_rvalue (struct value
*array
, struct value
*idx
, int lowerbound
)
218 struct type
*array_type
= check_typedef (VALUE_TYPE (array
));
219 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
220 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
221 LONGEST index
= value_as_long (idx
);
222 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
225 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
226 error ("no such vector element");
228 v
= allocate_value (elt_type
);
229 if (VALUE_LAZY (array
))
232 memcpy (VALUE_CONTENTS (v
), VALUE_CONTENTS (array
) + elt_offs
, elt_size
);
234 if (VALUE_LVAL (array
) == lval_internalvar
)
235 VALUE_LVAL (v
) = lval_internalvar_component
;
237 VALUE_LVAL (v
) = VALUE_LVAL (array
);
238 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
239 VALUE_OFFSET (v
) = VALUE_OFFSET (array
) + elt_offs
;
243 /* Check to see if either argument is a structure. This is called so
244 we know whether to go ahead with the normal binop or look for a
245 user defined function instead.
247 For now, we do not overload the `=' operator. */
250 binop_user_defined_p (enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
252 struct type
*type1
, *type2
;
253 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
255 type1
= check_typedef (VALUE_TYPE (arg1
));
256 type2
= check_typedef (VALUE_TYPE (arg2
));
257 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
258 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
259 || (TYPE_CODE (type1
) == TYPE_CODE_REF
260 && TYPE_CODE (TYPE_TARGET_TYPE (type1
)) == TYPE_CODE_STRUCT
)
261 || (TYPE_CODE (type2
) == TYPE_CODE_REF
262 && TYPE_CODE (TYPE_TARGET_TYPE (type2
)) == TYPE_CODE_STRUCT
));
265 /* Check to see if argument is a structure. This is called so
266 we know whether to go ahead with the normal unop or look for a
267 user defined function instead.
269 For now, we do not overload the `&' operator. */
272 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
277 type1
= check_typedef (VALUE_TYPE (arg1
));
280 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
282 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
283 type1
= TYPE_TARGET_TYPE (type1
);
289 /* We know either arg1 or arg2 is a structure, so try to find the right
290 user defined function. Create an argument vector that calls
291 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
292 binary operator which is legal for GNU C++).
294 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
295 is the opcode saying how to modify it. Otherwise, OTHEROP is
299 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
300 enum exp_opcode otherop
, enum noside noside
)
302 struct value
**argvec
;
312 /* now we know that what we have to do is construct our
313 arg vector and find the right function to call it with. */
315 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
316 error ("Can't do that binary op on that type"); /* FIXME be explicit */
318 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
319 argvec
[1] = value_addr (arg1
);
323 /* make the right function name up */
324 strcpy (tstr
, "operator__");
349 case BINOP_BITWISE_AND
:
352 case BINOP_BITWISE_IOR
:
355 case BINOP_BITWISE_XOR
:
358 case BINOP_LOGICAL_AND
:
361 case BINOP_LOGICAL_OR
:
373 case BINOP_ASSIGN_MODIFY
:
391 case BINOP_BITWISE_AND
:
394 case BINOP_BITWISE_IOR
:
397 case BINOP_BITWISE_XOR
:
400 case BINOP_MOD
: /* invalid */
402 error ("Invalid binary operation specified.");
405 case BINOP_SUBSCRIPT
:
426 case BINOP_MOD
: /* invalid */
428 error ("Invalid binary operation specified.");
431 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
437 argvec
[1] = argvec
[0];
440 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
442 struct type
*return_type
;
444 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
445 return value_zero (return_type
, VALUE_LVAL (arg1
));
447 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
449 error ("member function %s not found", tstr
);
451 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
455 /* We know that arg1 is a structure, so try to find a unary user
456 defined operator that matches the operator in question.
457 Create an argument vector that calls arg1.operator @ (arg1)
458 and return that value (where '@' is (almost) any unary operator which
459 is legal for GNU C++). */
462 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
464 struct value
**argvec
;
465 char *ptr
, *mangle_ptr
;
466 char tstr
[13], mangle_tstr
[13];
472 /* now we know that what we have to do is construct our
473 arg vector and find the right function to call it with. */
475 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
476 error ("Can't do that unary op on that type"); /* FIXME be explicit */
478 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 3);
479 argvec
[1] = value_addr (arg1
);
482 /* make the right function name up */
483 strcpy (tstr
, "operator__");
485 strcpy (mangle_tstr
, "__");
486 mangle_ptr
= mangle_tstr
+ 2;
489 case UNOP_PREINCREMENT
:
492 case UNOP_PREDECREMENT
:
495 case UNOP_POSTINCREMENT
:
498 case UNOP_POSTDECREMENT
:
501 case UNOP_LOGICAL_NOT
:
504 case UNOP_COMPLEMENT
:
514 error ("Invalid unary operation specified.");
517 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
523 argvec
[1] = argvec
[0];
526 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
528 struct type
*return_type
;
530 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
531 return value_zero (return_type
, VALUE_LVAL (arg1
));
533 return call_function_by_hand (argvec
[0], 1 - static_memfuncp
, argvec
+ 1);
535 error ("member function %s not found", tstr
);
536 return 0; /* For lint -- never reached */
540 /* Concatenate two values with the following conditions:
542 (1) Both values must be either bitstring values or character string
543 values and the resulting value consists of the concatenation of
544 ARG1 followed by ARG2.
548 One value must be an integer value and the other value must be
549 either a bitstring value or character string value, which is
550 to be repeated by the number of times specified by the integer
554 (2) Boolean values are also allowed and are treated as bit string
557 (3) Character values are also allowed and are treated as character
558 string values of length 1.
562 value_concat (struct value
*arg1
, struct value
*arg2
)
564 struct value
*inval1
;
565 struct value
*inval2
;
566 struct value
*outval
= NULL
;
567 int inval1len
, inval2len
;
571 struct type
*type1
= check_typedef (VALUE_TYPE (arg1
));
572 struct type
*type2
= check_typedef (VALUE_TYPE (arg2
));
574 COERCE_VARYING_ARRAY (arg1
, type1
);
575 COERCE_VARYING_ARRAY (arg2
, type2
);
577 /* First figure out if we are dealing with two values to be concatenated
578 or a repeat count and a value to be repeated. INVAL1 is set to the
579 first of two concatenated values, or the repeat count. INVAL2 is set
580 to the second of the two concatenated values or the value to be
583 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
585 struct type
*tmp
= type1
;
597 /* Now process the input values. */
599 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
601 /* We have a repeat count. Validate the second value and then
602 construct a value repeated that many times. */
603 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
604 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
606 count
= longest_to_int (value_as_long (inval1
));
607 inval2len
= TYPE_LENGTH (type2
);
608 ptr
= (char *) alloca (count
* inval2len
);
609 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
611 inchar
= (char) unpack_long (type2
,
612 VALUE_CONTENTS (inval2
));
613 for (idx
= 0; idx
< count
; idx
++)
615 *(ptr
+ idx
) = inchar
;
620 for (idx
= 0; idx
< count
; idx
++)
622 memcpy (ptr
+ (idx
* inval2len
), VALUE_CONTENTS (inval2
),
626 outval
= value_string (ptr
, count
* inval2len
);
628 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
629 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
631 error ("unimplemented support for bitstring/boolean repeats");
635 error ("can't repeat values of that type");
638 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
639 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
641 /* We have two character strings to concatenate. */
642 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
643 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
645 error ("Strings can only be concatenated with other strings.");
647 inval1len
= TYPE_LENGTH (type1
);
648 inval2len
= TYPE_LENGTH (type2
);
649 ptr
= (char *) alloca (inval1len
+ inval2len
);
650 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
652 *ptr
= (char) unpack_long (type1
, VALUE_CONTENTS (inval1
));
656 memcpy (ptr
, VALUE_CONTENTS (inval1
), inval1len
);
658 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
661 (char) unpack_long (type2
, VALUE_CONTENTS (inval2
));
665 memcpy (ptr
+ inval1len
, VALUE_CONTENTS (inval2
), inval2len
);
667 outval
= value_string (ptr
, inval1len
+ inval2len
);
669 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
670 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
672 /* We have two bitstrings to concatenate. */
673 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
674 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
676 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
678 error ("unimplemented support for bitstring/boolean concatenation.");
682 /* We don't know how to concatenate these operands. */
683 error ("illegal operands for concatenation.");
690 /* Perform a binary operation on two operands which have reasonable
691 representations as integers or floats. This includes booleans,
692 characters, integers, or floats.
693 Does not support addition and subtraction on pointers;
694 use value_add or value_sub if you want to handle those possibilities. */
697 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
700 struct type
*type1
, *type2
;
706 type1
= check_typedef (VALUE_TYPE (arg1
));
707 type2
= check_typedef (VALUE_TYPE (arg2
));
709 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
710 && TYPE_CODE (type1
) != TYPE_CODE_CHAR
711 && TYPE_CODE (type1
) != TYPE_CODE_INT
712 && TYPE_CODE (type1
) != TYPE_CODE_BOOL
713 && TYPE_CODE (type1
) != TYPE_CODE_RANGE
)
715 (TYPE_CODE (type2
) != TYPE_CODE_FLT
716 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
717 && TYPE_CODE (type2
) != TYPE_CODE_INT
718 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
719 && TYPE_CODE (type2
) != TYPE_CODE_RANGE
))
720 error ("Argument to arithmetic operation not a number or boolean.");
722 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
724 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
726 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
727 in target format. real.c in GCC probably has the necessary
729 DOUBLEST v1
, v2
, v
= 0;
730 v1
= value_as_double (arg1
);
731 v2
= value_as_double (arg2
);
753 error ("Cannot perform exponentiation: %s", strerror (errno
));
757 error ("Integer-only operation on floating point number.");
760 /* If either arg was long double, make sure that value is also long
763 if (TYPE_LENGTH (type1
) * 8 > TARGET_DOUBLE_BIT
764 || TYPE_LENGTH (type2
) * 8 > TARGET_DOUBLE_BIT
)
765 val
= allocate_value (builtin_type_long_double
);
767 val
= allocate_value (builtin_type_double
);
769 store_typed_floating (VALUE_CONTENTS_RAW (val
), VALUE_TYPE (val
), v
);
771 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
773 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
775 LONGEST v1
, v2
, v
= 0;
776 v1
= value_as_long (arg1
);
777 v2
= value_as_long (arg2
);
781 case BINOP_BITWISE_AND
:
785 case BINOP_BITWISE_IOR
:
789 case BINOP_BITWISE_XOR
:
802 error ("Invalid operation on booleans.");
805 val
= allocate_value (type1
);
806 store_signed_integer (VALUE_CONTENTS_RAW (val
),
811 /* Integral operations here. */
812 /* FIXME: Also mixed integral/booleans, with result an integer. */
813 /* FIXME: This implements ANSI C rules (also correct for C++).
814 What about FORTRAN and chill? */
816 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
817 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
818 int is_unsigned1
= TYPE_UNSIGNED (type1
);
819 int is_unsigned2
= TYPE_UNSIGNED (type2
);
820 unsigned int result_len
;
821 int unsigned_operation
;
823 /* Determine type length and signedness after promotion for
825 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
828 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
830 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
833 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
836 /* Determine type length of the result, and if the operation should
838 Use the signedness of the operand with the greater length.
839 If both operands are of equal length, use unsigned operation
840 if one of the operands is unsigned. */
841 if (promoted_len1
> promoted_len2
)
843 unsigned_operation
= is_unsigned1
;
844 result_len
= promoted_len1
;
846 else if (promoted_len2
> promoted_len1
)
848 unsigned_operation
= is_unsigned2
;
849 result_len
= promoted_len2
;
853 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
854 result_len
= promoted_len1
;
857 if (unsigned_operation
)
859 ULONGEST v1
, v2
, v
= 0;
860 v1
= (ULONGEST
) value_as_long (arg1
);
861 v2
= (ULONGEST
) value_as_long (arg2
);
863 /* Truncate values to the type length of the result. */
864 if (result_len
< sizeof (ULONGEST
))
866 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
867 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
891 error ("Cannot perform exponentiation: %s", strerror (errno
));
899 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
900 v1 mod 0 has a defined value, v1. */
901 /* Chill specifies that v2 must be > 0, so check for that. */
902 if (current_language
->la_language
== language_chill
903 && value_as_long (arg2
) <= 0)
905 error ("Second operand of MOD must be greater than zero.");
914 /* Note floor(v1/v2) == v1/v2 for unsigned. */
927 case BINOP_BITWISE_AND
:
931 case BINOP_BITWISE_IOR
:
935 case BINOP_BITWISE_XOR
:
939 case BINOP_LOGICAL_AND
:
943 case BINOP_LOGICAL_OR
:
948 v
= v1
< v2
? v1
: v2
;
952 v
= v1
> v2
? v1
: v2
;
968 error ("Invalid binary operation on numbers.");
971 /* This is a kludge to get around the fact that we don't
972 know how to determine the result type from the types of
973 the operands. (I'm not really sure how much we feel the
974 need to duplicate the exact rules of the current
975 language. They can get really hairy. But not to do so
976 makes it hard to document just what we *do* do). */
978 /* Can't just call init_type because we wouldn't know what
979 name to give the type. */
981 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
982 ? builtin_type_unsigned_long_long
983 : builtin_type_unsigned_long
);
984 store_unsigned_integer (VALUE_CONTENTS_RAW (val
),
985 TYPE_LENGTH (VALUE_TYPE (val
)),
990 LONGEST v1
, v2
, v
= 0;
991 v1
= value_as_long (arg1
);
992 v2
= value_as_long (arg2
);
1015 error ("Cannot perform exponentiation: %s", strerror (errno
));
1023 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1024 X mod 0 has a defined value, X. */
1025 /* Chill specifies that v2 must be > 0, so check for that. */
1026 if (current_language
->la_language
== language_chill
1029 error ("Second operand of MOD must be greater than zero.");
1038 /* Compute floor. */
1039 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1055 case BINOP_BITWISE_AND
:
1059 case BINOP_BITWISE_IOR
:
1063 case BINOP_BITWISE_XOR
:
1067 case BINOP_LOGICAL_AND
:
1071 case BINOP_LOGICAL_OR
:
1076 v
= v1
< v2
? v1
: v2
;
1080 v
= v1
> v2
? v1
: v2
;
1092 error ("Invalid binary operation on numbers.");
1095 /* This is a kludge to get around the fact that we don't
1096 know how to determine the result type from the types of
1097 the operands. (I'm not really sure how much we feel the
1098 need to duplicate the exact rules of the current
1099 language. They can get really hairy. But not to do so
1100 makes it hard to document just what we *do* do). */
1102 /* Can't just call init_type because we wouldn't know what
1103 name to give the type. */
1104 val
= allocate_value
1105 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1106 ? builtin_type_long_long
1107 : builtin_type_long
);
1108 store_signed_integer (VALUE_CONTENTS_RAW (val
),
1109 TYPE_LENGTH (VALUE_TYPE (val
)),
1117 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1120 value_logical_not (struct value
*arg1
)
1126 COERCE_NUMBER (arg1
);
1127 type1
= check_typedef (VALUE_TYPE (arg1
));
1129 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1130 return 0 == value_as_double (arg1
);
1132 len
= TYPE_LENGTH (type1
);
1133 p
= VALUE_CONTENTS (arg1
);
1144 /* Perform a comparison on two string values (whose content are not
1145 necessarily null terminated) based on their length */
1148 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1150 int len1
= TYPE_LENGTH (VALUE_TYPE (arg1
));
1151 int len2
= TYPE_LENGTH (VALUE_TYPE (arg2
));
1152 char *s1
= VALUE_CONTENTS (arg1
);
1153 char *s2
= VALUE_CONTENTS (arg2
);
1154 int i
, len
= len1
< len2
? len1
: len2
;
1156 for (i
= 0; i
< len
; i
++)
1160 else if (s1
[i
] > s2
[i
])
1168 else if (len1
> len2
)
1174 /* Simulate the C operator == by returning a 1
1175 iff ARG1 and ARG2 have equal contents. */
1178 value_equal (struct value
*arg1
, struct value
*arg2
)
1181 register char *p1
, *p2
;
1182 struct type
*type1
, *type2
;
1183 enum type_code code1
;
1184 enum type_code code2
;
1186 COERCE_NUMBER (arg1
);
1187 COERCE_NUMBER (arg2
);
1189 type1
= check_typedef (VALUE_TYPE (arg1
));
1190 type2
= check_typedef (VALUE_TYPE (arg2
));
1191 code1
= TYPE_CODE (type1
);
1192 code2
= TYPE_CODE (type2
);
1194 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1195 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1196 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1198 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1199 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1200 return value_as_double (arg1
) == value_as_double (arg2
);
1202 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1204 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1205 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1206 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1207 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1209 else if (code1
== code2
1210 && ((len
= (int) TYPE_LENGTH (type1
))
1211 == (int) TYPE_LENGTH (type2
)))
1213 p1
= VALUE_CONTENTS (arg1
);
1214 p2
= VALUE_CONTENTS (arg2
);
1222 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1224 return value_strcmp (arg1
, arg2
) == 0;
1228 error ("Invalid type combination in equality test.");
1229 return 0; /* For lint -- never reached */
1233 /* Simulate the C operator < by returning 1
1234 iff ARG1's contents are less than ARG2's. */
1237 value_less (struct value
*arg1
, struct value
*arg2
)
1239 register enum type_code code1
;
1240 register enum type_code code2
;
1241 struct type
*type1
, *type2
;
1243 COERCE_NUMBER (arg1
);
1244 COERCE_NUMBER (arg2
);
1246 type1
= check_typedef (VALUE_TYPE (arg1
));
1247 type2
= check_typedef (VALUE_TYPE (arg2
));
1248 code1
= TYPE_CODE (type1
);
1249 code2
= TYPE_CODE (type2
);
1251 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1252 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1253 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1255 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1256 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1257 return value_as_double (arg1
) < value_as_double (arg2
);
1258 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1259 return value_as_address (arg1
) < value_as_address (arg2
);
1261 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1263 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1264 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1265 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1266 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1267 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1268 return value_strcmp (arg1
, arg2
) < 0;
1271 error ("Invalid type combination in ordering comparison.");
1276 /* The unary operators - and ~. Both free the argument ARG1. */
1279 value_neg (struct value
*arg1
)
1281 register struct type
*type
;
1282 register struct type
*result_type
= VALUE_TYPE (arg1
);
1287 type
= check_typedef (VALUE_TYPE (arg1
));
1289 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1290 return value_from_double (result_type
, -value_as_double (arg1
));
1291 else if (TYPE_CODE (type
) == TYPE_CODE_INT
|| TYPE_CODE (type
) == TYPE_CODE_BOOL
)
1293 /* Perform integral promotion for ANSI C/C++.
1294 FIXME: What about FORTRAN and chill ? */
1295 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1296 result_type
= builtin_type_int
;
1298 return value_from_longest (result_type
, -value_as_long (arg1
));
1302 error ("Argument to negate operation not a number.");
1303 return 0; /* For lint -- never reached */
1308 value_complement (struct value
*arg1
)
1310 register struct type
*type
;
1311 register struct type
*result_type
= VALUE_TYPE (arg1
);
1317 type
= check_typedef (VALUE_TYPE (arg1
));
1319 typecode
= TYPE_CODE (type
);
1320 if ((typecode
!= TYPE_CODE_INT
) && (typecode
!= TYPE_CODE_BOOL
))
1321 error ("Argument to complement operation not an integer or boolean.");
1323 /* Perform integral promotion for ANSI C/C++.
1324 FIXME: What about FORTRAN ? */
1325 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1326 result_type
= builtin_type_int
;
1328 return value_from_longest (result_type
, ~value_as_long (arg1
));
1331 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1332 and whose VALUE_CONTENTS is valaddr.
1333 Return -1 if out of range, -2 other error. */
1336 value_bit_index (struct type
*type
, char *valaddr
, int index
)
1338 LONGEST low_bound
, high_bound
;
1341 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1342 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1344 if (index
< low_bound
|| index
> high_bound
)
1346 rel_index
= index
- low_bound
;
1347 word
= unpack_long (builtin_type_unsigned_char
,
1348 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1349 rel_index
%= TARGET_CHAR_BIT
;
1350 if (BITS_BIG_ENDIAN
)
1351 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1352 return (word
>> rel_index
) & 1;
1356 value_in (struct value
*element
, struct value
*set
)
1359 struct type
*settype
= check_typedef (VALUE_TYPE (set
));
1360 struct type
*eltype
= check_typedef (VALUE_TYPE (element
));
1361 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1362 eltype
= TYPE_TARGET_TYPE (eltype
);
1363 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1364 error ("Second argument of 'IN' has wrong type");
1365 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1366 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1367 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1368 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1369 error ("First argument of 'IN' has wrong type");
1370 member
= value_bit_index (settype
, VALUE_CONTENTS (set
),
1371 value_as_long (element
));
1373 error ("First argument of 'IN' not in range");
1374 return value_from_longest (LA_BOOL_TYPE
, member
);
1378 _initialize_valarith (void)