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];
467 int static_memfuncp
, nargs
;
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
*) * 4);
479 argvec
[1] = value_addr (arg1
);
484 /* make the right function name up */
485 strcpy (tstr
, "operator__");
487 strcpy (mangle_tstr
, "__");
488 mangle_ptr
= mangle_tstr
+ 2;
491 case UNOP_PREINCREMENT
:
494 case UNOP_PREDECREMENT
:
497 case UNOP_POSTINCREMENT
:
499 argvec
[2] = value_from_longest (builtin_type_int
, 0);
503 case UNOP_POSTDECREMENT
:
505 argvec
[2] = value_from_longest (builtin_type_int
, 0);
509 case UNOP_LOGICAL_NOT
:
512 case UNOP_COMPLEMENT
:
522 error ("Invalid unary operation specified.");
525 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
531 argvec
[1] = argvec
[0];
535 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
537 struct type
*return_type
;
539 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
540 return value_zero (return_type
, VALUE_LVAL (arg1
));
542 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
544 error ("member function %s not found", tstr
);
545 return 0; /* For lint -- never reached */
549 /* Concatenate two values with the following conditions:
551 (1) Both values must be either bitstring values or character string
552 values and the resulting value consists of the concatenation of
553 ARG1 followed by ARG2.
557 One value must be an integer value and the other value must be
558 either a bitstring value or character string value, which is
559 to be repeated by the number of times specified by the integer
563 (2) Boolean values are also allowed and are treated as bit string
566 (3) Character values are also allowed and are treated as character
567 string values of length 1.
571 value_concat (struct value
*arg1
, struct value
*arg2
)
573 struct value
*inval1
;
574 struct value
*inval2
;
575 struct value
*outval
= NULL
;
576 int inval1len
, inval2len
;
580 struct type
*type1
= check_typedef (VALUE_TYPE (arg1
));
581 struct type
*type2
= check_typedef (VALUE_TYPE (arg2
));
583 COERCE_VARYING_ARRAY (arg1
, type1
);
584 COERCE_VARYING_ARRAY (arg2
, type2
);
586 /* First figure out if we are dealing with two values to be concatenated
587 or a repeat count and a value to be repeated. INVAL1 is set to the
588 first of two concatenated values, or the repeat count. INVAL2 is set
589 to the second of the two concatenated values or the value to be
592 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
594 struct type
*tmp
= type1
;
606 /* Now process the input values. */
608 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
610 /* We have a repeat count. Validate the second value and then
611 construct a value repeated that many times. */
612 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
613 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
615 count
= longest_to_int (value_as_long (inval1
));
616 inval2len
= TYPE_LENGTH (type2
);
617 ptr
= (char *) alloca (count
* inval2len
);
618 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
620 inchar
= (char) unpack_long (type2
,
621 VALUE_CONTENTS (inval2
));
622 for (idx
= 0; idx
< count
; idx
++)
624 *(ptr
+ idx
) = inchar
;
629 for (idx
= 0; idx
< count
; idx
++)
631 memcpy (ptr
+ (idx
* inval2len
), VALUE_CONTENTS (inval2
),
635 outval
= value_string (ptr
, count
* inval2len
);
637 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
638 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
640 error ("unimplemented support for bitstring/boolean repeats");
644 error ("can't repeat values of that type");
647 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
648 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
650 /* We have two character strings to concatenate. */
651 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
652 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
654 error ("Strings can only be concatenated with other strings.");
656 inval1len
= TYPE_LENGTH (type1
);
657 inval2len
= TYPE_LENGTH (type2
);
658 ptr
= (char *) alloca (inval1len
+ inval2len
);
659 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
661 *ptr
= (char) unpack_long (type1
, VALUE_CONTENTS (inval1
));
665 memcpy (ptr
, VALUE_CONTENTS (inval1
), inval1len
);
667 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
670 (char) unpack_long (type2
, VALUE_CONTENTS (inval2
));
674 memcpy (ptr
+ inval1len
, VALUE_CONTENTS (inval2
), inval2len
);
676 outval
= value_string (ptr
, inval1len
+ inval2len
);
678 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
679 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
681 /* We have two bitstrings to concatenate. */
682 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
683 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
685 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
687 error ("unimplemented support for bitstring/boolean concatenation.");
691 /* We don't know how to concatenate these operands. */
692 error ("illegal operands for concatenation.");
699 /* Perform a binary operation on two operands which have reasonable
700 representations as integers or floats. This includes booleans,
701 characters, integers, or floats.
702 Does not support addition and subtraction on pointers;
703 use value_add or value_sub if you want to handle those possibilities. */
706 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
709 struct type
*type1
, *type2
;
715 type1
= check_typedef (VALUE_TYPE (arg1
));
716 type2
= check_typedef (VALUE_TYPE (arg2
));
718 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
719 && TYPE_CODE (type1
) != TYPE_CODE_CHAR
720 && TYPE_CODE (type1
) != TYPE_CODE_INT
721 && TYPE_CODE (type1
) != TYPE_CODE_BOOL
722 && TYPE_CODE (type1
) != TYPE_CODE_RANGE
)
724 (TYPE_CODE (type2
) != TYPE_CODE_FLT
725 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
726 && TYPE_CODE (type2
) != TYPE_CODE_INT
727 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
728 && TYPE_CODE (type2
) != TYPE_CODE_RANGE
))
729 error ("Argument to arithmetic operation not a number or boolean.");
731 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
733 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
735 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
736 in target format. real.c in GCC probably has the necessary
738 DOUBLEST v1
, v2
, v
= 0;
739 v1
= value_as_double (arg1
);
740 v2
= value_as_double (arg2
);
762 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
766 error ("Integer-only operation on floating point number.");
769 /* If either arg was long double, make sure that value is also long
772 if (TYPE_LENGTH (type1
) * 8 > TARGET_DOUBLE_BIT
773 || TYPE_LENGTH (type2
) * 8 > TARGET_DOUBLE_BIT
)
774 val
= allocate_value (builtin_type_long_double
);
776 val
= allocate_value (builtin_type_double
);
778 store_typed_floating (VALUE_CONTENTS_RAW (val
), VALUE_TYPE (val
), v
);
780 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
782 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
784 LONGEST v1
, v2
, v
= 0;
785 v1
= value_as_long (arg1
);
786 v2
= value_as_long (arg2
);
790 case BINOP_BITWISE_AND
:
794 case BINOP_BITWISE_IOR
:
798 case BINOP_BITWISE_XOR
:
811 error ("Invalid operation on booleans.");
814 val
= allocate_value (type1
);
815 store_signed_integer (VALUE_CONTENTS_RAW (val
),
820 /* Integral operations here. */
821 /* FIXME: Also mixed integral/booleans, with result an integer. */
822 /* FIXME: This implements ANSI C rules (also correct for C++).
823 What about FORTRAN and chill? */
825 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
826 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
827 int is_unsigned1
= TYPE_UNSIGNED (type1
);
828 int is_unsigned2
= TYPE_UNSIGNED (type2
);
829 unsigned int result_len
;
830 int unsigned_operation
;
832 /* Determine type length and signedness after promotion for
834 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
837 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
839 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
842 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
845 /* Determine type length of the result, and if the operation should
847 Use the signedness of the operand with the greater length.
848 If both operands are of equal length, use unsigned operation
849 if one of the operands is unsigned. */
850 if (promoted_len1
> promoted_len2
)
852 unsigned_operation
= is_unsigned1
;
853 result_len
= promoted_len1
;
855 else if (promoted_len2
> promoted_len1
)
857 unsigned_operation
= is_unsigned2
;
858 result_len
= promoted_len2
;
862 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
863 result_len
= promoted_len1
;
866 if (unsigned_operation
)
868 ULONGEST v1
, v2
, v
= 0;
869 v1
= (ULONGEST
) value_as_long (arg1
);
870 v2
= (ULONGEST
) value_as_long (arg2
);
872 /* Truncate values to the type length of the result. */
873 if (result_len
< sizeof (ULONGEST
))
875 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
876 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
900 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
908 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
909 v1 mod 0 has a defined value, v1. */
910 /* Chill specifies that v2 must be > 0, so check for that. */
911 if (current_language
->la_language
== language_chill
912 && value_as_long (arg2
) <= 0)
914 error ("Second operand of MOD must be greater than zero.");
923 /* Note floor(v1/v2) == v1/v2 for unsigned. */
936 case BINOP_BITWISE_AND
:
940 case BINOP_BITWISE_IOR
:
944 case BINOP_BITWISE_XOR
:
948 case BINOP_LOGICAL_AND
:
952 case BINOP_LOGICAL_OR
:
957 v
= v1
< v2
? v1
: v2
;
961 v
= v1
> v2
? v1
: v2
;
977 error ("Invalid binary operation on numbers.");
980 /* This is a kludge to get around the fact that we don't
981 know how to determine the result type from the types of
982 the operands. (I'm not really sure how much we feel the
983 need to duplicate the exact rules of the current
984 language. They can get really hairy. But not to do so
985 makes it hard to document just what we *do* do). */
987 /* Can't just call init_type because we wouldn't know what
988 name to give the type. */
990 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
991 ? builtin_type_unsigned_long_long
992 : builtin_type_unsigned_long
);
993 store_unsigned_integer (VALUE_CONTENTS_RAW (val
),
994 TYPE_LENGTH (VALUE_TYPE (val
)),
999 LONGEST v1
, v2
, v
= 0;
1000 v1
= value_as_long (arg1
);
1001 v2
= value_as_long (arg2
);
1024 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
1032 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1033 X mod 0 has a defined value, X. */
1034 /* Chill specifies that v2 must be > 0, so check for that. */
1035 if (current_language
->la_language
== language_chill
1038 error ("Second operand of MOD must be greater than zero.");
1047 /* Compute floor. */
1048 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1064 case BINOP_BITWISE_AND
:
1068 case BINOP_BITWISE_IOR
:
1072 case BINOP_BITWISE_XOR
:
1076 case BINOP_LOGICAL_AND
:
1080 case BINOP_LOGICAL_OR
:
1085 v
= v1
< v2
? v1
: v2
;
1089 v
= v1
> v2
? v1
: v2
;
1101 error ("Invalid binary operation on numbers.");
1104 /* This is a kludge to get around the fact that we don't
1105 know how to determine the result type from the types of
1106 the operands. (I'm not really sure how much we feel the
1107 need to duplicate the exact rules of the current
1108 language. They can get really hairy. But not to do so
1109 makes it hard to document just what we *do* do). */
1111 /* Can't just call init_type because we wouldn't know what
1112 name to give the type. */
1113 val
= allocate_value
1114 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1115 ? builtin_type_long_long
1116 : builtin_type_long
);
1117 store_signed_integer (VALUE_CONTENTS_RAW (val
),
1118 TYPE_LENGTH (VALUE_TYPE (val
)),
1126 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1129 value_logical_not (struct value
*arg1
)
1135 COERCE_NUMBER (arg1
);
1136 type1
= check_typedef (VALUE_TYPE (arg1
));
1138 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1139 return 0 == value_as_double (arg1
);
1141 len
= TYPE_LENGTH (type1
);
1142 p
= VALUE_CONTENTS (arg1
);
1153 /* Perform a comparison on two string values (whose content are not
1154 necessarily null terminated) based on their length */
1157 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1159 int len1
= TYPE_LENGTH (VALUE_TYPE (arg1
));
1160 int len2
= TYPE_LENGTH (VALUE_TYPE (arg2
));
1161 char *s1
= VALUE_CONTENTS (arg1
);
1162 char *s2
= VALUE_CONTENTS (arg2
);
1163 int i
, len
= len1
< len2
? len1
: len2
;
1165 for (i
= 0; i
< len
; i
++)
1169 else if (s1
[i
] > s2
[i
])
1177 else if (len1
> len2
)
1183 /* Simulate the C operator == by returning a 1
1184 iff ARG1 and ARG2 have equal contents. */
1187 value_equal (struct value
*arg1
, struct value
*arg2
)
1190 register char *p1
, *p2
;
1191 struct type
*type1
, *type2
;
1192 enum type_code code1
;
1193 enum type_code code2
;
1195 COERCE_NUMBER (arg1
);
1196 COERCE_NUMBER (arg2
);
1198 type1
= check_typedef (VALUE_TYPE (arg1
));
1199 type2
= check_typedef (VALUE_TYPE (arg2
));
1200 code1
= TYPE_CODE (type1
);
1201 code2
= TYPE_CODE (type2
);
1203 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1204 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1205 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1207 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1208 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1209 return value_as_double (arg1
) == value_as_double (arg2
);
1211 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1213 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1214 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1215 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1216 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1218 else if (code1
== code2
1219 && ((len
= (int) TYPE_LENGTH (type1
))
1220 == (int) TYPE_LENGTH (type2
)))
1222 p1
= VALUE_CONTENTS (arg1
);
1223 p2
= VALUE_CONTENTS (arg2
);
1231 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1233 return value_strcmp (arg1
, arg2
) == 0;
1237 error ("Invalid type combination in equality test.");
1238 return 0; /* For lint -- never reached */
1242 /* Simulate the C operator < by returning 1
1243 iff ARG1's contents are less than ARG2's. */
1246 value_less (struct value
*arg1
, struct value
*arg2
)
1248 register enum type_code code1
;
1249 register enum type_code code2
;
1250 struct type
*type1
, *type2
;
1252 COERCE_NUMBER (arg1
);
1253 COERCE_NUMBER (arg2
);
1255 type1
= check_typedef (VALUE_TYPE (arg1
));
1256 type2
= check_typedef (VALUE_TYPE (arg2
));
1257 code1
= TYPE_CODE (type1
);
1258 code2
= TYPE_CODE (type2
);
1260 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1261 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1262 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1264 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1265 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1266 return value_as_double (arg1
) < value_as_double (arg2
);
1267 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1268 return value_as_address (arg1
) < value_as_address (arg2
);
1270 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1272 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1273 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1274 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1275 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1276 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1277 return value_strcmp (arg1
, arg2
) < 0;
1280 error ("Invalid type combination in ordering comparison.");
1285 /* The unary operators - and ~. Both free the argument ARG1. */
1288 value_neg (struct value
*arg1
)
1290 register struct type
*type
;
1291 register struct type
*result_type
= VALUE_TYPE (arg1
);
1296 type
= check_typedef (VALUE_TYPE (arg1
));
1298 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1299 return value_from_double (result_type
, -value_as_double (arg1
));
1300 else if (TYPE_CODE (type
) == TYPE_CODE_INT
|| TYPE_CODE (type
) == TYPE_CODE_BOOL
)
1302 /* Perform integral promotion for ANSI C/C++.
1303 FIXME: What about FORTRAN and chill ? */
1304 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1305 result_type
= builtin_type_int
;
1307 return value_from_longest (result_type
, -value_as_long (arg1
));
1311 error ("Argument to negate operation not a number.");
1312 return 0; /* For lint -- never reached */
1317 value_complement (struct value
*arg1
)
1319 register struct type
*type
;
1320 register struct type
*result_type
= VALUE_TYPE (arg1
);
1326 type
= check_typedef (VALUE_TYPE (arg1
));
1328 typecode
= TYPE_CODE (type
);
1329 if ((typecode
!= TYPE_CODE_INT
) && (typecode
!= TYPE_CODE_BOOL
))
1330 error ("Argument to complement operation not an integer or boolean.");
1332 /* Perform integral promotion for ANSI C/C++.
1333 FIXME: What about FORTRAN ? */
1334 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1335 result_type
= builtin_type_int
;
1337 return value_from_longest (result_type
, ~value_as_long (arg1
));
1340 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1341 and whose VALUE_CONTENTS is valaddr.
1342 Return -1 if out of range, -2 other error. */
1345 value_bit_index (struct type
*type
, char *valaddr
, int index
)
1347 LONGEST low_bound
, high_bound
;
1350 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1351 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1353 if (index
< low_bound
|| index
> high_bound
)
1355 rel_index
= index
- low_bound
;
1356 word
= unpack_long (builtin_type_unsigned_char
,
1357 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1358 rel_index
%= TARGET_CHAR_BIT
;
1359 if (BITS_BIG_ENDIAN
)
1360 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1361 return (word
>> rel_index
) & 1;
1365 value_in (struct value
*element
, struct value
*set
)
1368 struct type
*settype
= check_typedef (VALUE_TYPE (set
));
1369 struct type
*eltype
= check_typedef (VALUE_TYPE (element
));
1370 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1371 eltype
= TYPE_TARGET_TYPE (eltype
);
1372 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1373 error ("Second argument of 'IN' has wrong type");
1374 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1375 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1376 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1377 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1378 error ("First argument of 'IN' has wrong type");
1379 member
= value_bit_index (settype
, VALUE_CONTENTS (set
),
1380 value_as_long (element
));
1382 error ("First argument of 'IN' not in range");
1383 return value_from_longest (LA_BOOL_TYPE
, member
);
1387 _initialize_valarith (void)