1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
28 #include "expression.h"
31 #include "gdb_string.h"
35 /* Define whether or not the C operator '/' truncates towards zero for
36 differently signed operands (truncation direction is undefined in C). */
38 #ifndef TRUNCATION_TOWARDS_ZERO
39 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
42 static struct value
*value_subscripted_rvalue (struct value
*, struct value
*, int);
44 void _initialize_valarith (void);
47 /* Given a pointer, return the size of its target.
48 If the pointer type is void *, then return 1.
49 If the target type is incomplete, then error out.
50 This isn't a general purpose function, but just a
51 helper for value_sub & value_add.
55 find_size_for_pointer_math (struct type
*ptr_type
)
58 struct type
*ptr_target
;
60 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
62 sz
= TYPE_LENGTH (ptr_target
);
65 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
71 name
= TYPE_NAME (ptr_target
);
73 name
= TYPE_TAG_NAME (ptr_target
);
75 error ("Cannot perform pointer math on incomplete types, "
76 "try casting to a known type, or void *.");
78 error ("Cannot perform pointer math on incomplete type \"%s\", "
79 "try casting to a known type, or void *.", name
);
86 value_add (struct value
*arg1
, struct value
*arg2
)
91 struct type
*type1
, *type2
, *valptrtype
;
95 type1
= check_typedef (VALUE_TYPE (arg1
));
96 type2
= check_typedef (VALUE_TYPE (arg2
));
98 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
99 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
101 (TYPE_CODE (type1
) == TYPE_CODE_INT
102 || TYPE_CODE (type2
) == TYPE_CODE_INT
))
103 /* Exactly one argument is a pointer, and one is an integer. */
105 struct value
*retval
;
107 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
120 sz
= find_size_for_pointer_math (valptrtype
);
122 retval
= value_from_pointer (valptrtype
,
123 value_as_address (valptr
)
124 + (sz
* value_as_long (valint
)));
125 VALUE_BFD_SECTION (retval
) = VALUE_BFD_SECTION (valptr
);
129 return value_binop (arg1
, arg2
, BINOP_ADD
);
133 value_sub (struct value
*arg1
, struct value
*arg2
)
135 struct type
*type1
, *type2
;
136 COERCE_NUMBER (arg1
);
137 COERCE_NUMBER (arg2
);
138 type1
= check_typedef (VALUE_TYPE (arg1
));
139 type2
= check_typedef (VALUE_TYPE (arg2
));
141 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
143 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
145 /* pointer - integer. */
146 LONGEST sz
= find_size_for_pointer_math (type1
);
148 return value_from_pointer (type1
,
149 (value_as_address (arg1
)
150 - (sz
* value_as_long (arg2
))));
152 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
153 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)))
154 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2
))))
156 /* pointer to <type x> - pointer to <type x>. */
157 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
158 return value_from_longest
159 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
160 (value_as_long (arg1
) - value_as_long (arg2
)) / sz
);
165 First argument of `-' is a pointer and second argument is neither\n\
166 an integer nor a pointer of the same type.");
170 return value_binop (arg1
, arg2
, BINOP_SUB
);
173 /* Return the value of ARRAY[IDX].
174 See comments in value_coerce_array() for rationale for reason for
175 doing lower bounds adjustment here rather than there.
176 FIXME: Perhaps we should validate that the index is valid and if
177 verbosity is set, warn about invalid indices (but still use them). */
180 value_subscript (struct value
*array
, struct value
*idx
)
183 int c_style
= current_language
->c_style_arrays
;
187 tarray
= check_typedef (VALUE_TYPE (array
));
188 COERCE_VARYING_ARRAY (array
, tarray
);
190 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
191 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
193 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
194 LONGEST lowerbound
, upperbound
;
195 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
197 if (VALUE_LVAL (array
) != lval_memory
)
198 return value_subscripted_rvalue (array
, idx
, lowerbound
);
202 LONGEST index
= value_as_long (idx
);
203 if (index
>= lowerbound
&& index
<= upperbound
)
204 return value_subscripted_rvalue (array
, idx
, lowerbound
);
205 warning ("array or string index out of range");
206 /* fall doing C stuff */
212 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
213 idx
= value_sub (idx
, bound
);
216 array
= value_coerce_array (array
);
219 if (TYPE_CODE (tarray
) == TYPE_CODE_BITSTRING
)
221 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
222 LONGEST index
= value_as_long (idx
);
224 int offset
, byte
, bit_index
;
225 LONGEST lowerbound
, upperbound
;
226 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
227 if (index
< lowerbound
|| index
> upperbound
)
228 error ("bitstring index out of range");
230 offset
= index
/ TARGET_CHAR_BIT
;
231 byte
= *((char *) VALUE_CONTENTS (array
) + offset
);
232 bit_index
= index
% TARGET_CHAR_BIT
;
233 byte
>>= (BITS_BIG_ENDIAN
? TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
234 v
= value_from_longest (LA_BOOL_TYPE
, byte
& 1);
235 VALUE_BITPOS (v
) = bit_index
;
236 VALUE_BITSIZE (v
) = 1;
237 VALUE_LVAL (v
) = VALUE_LVAL (array
);
238 if (VALUE_LVAL (array
) == lval_internalvar
)
239 VALUE_LVAL (v
) = lval_internalvar_component
;
240 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
241 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (array
);
246 return value_ind (value_add (array
, idx
));
248 error ("not an array or string");
251 /* Return the value of EXPR[IDX], expr an aggregate rvalue
252 (eg, a vector register). This routine used to promote floats
253 to doubles, but no longer does. */
255 static struct value
*
256 value_subscripted_rvalue (struct value
*array
, struct value
*idx
, int lowerbound
)
258 struct type
*array_type
= check_typedef (VALUE_TYPE (array
));
259 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
260 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
261 LONGEST index
= value_as_long (idx
);
262 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
265 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
266 error ("no such vector element");
268 v
= allocate_value (elt_type
);
269 if (VALUE_LAZY (array
))
272 memcpy (VALUE_CONTENTS (v
), VALUE_CONTENTS (array
) + elt_offs
, elt_size
);
274 if (VALUE_LVAL (array
) == lval_internalvar
)
275 VALUE_LVAL (v
) = lval_internalvar_component
;
277 VALUE_LVAL (v
) = VALUE_LVAL (array
);
278 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
279 VALUE_REGNO (v
) = VALUE_REGNO (array
);
280 VALUE_OFFSET (v
) = VALUE_OFFSET (array
) + elt_offs
;
284 /* Check to see if either argument is a structure. This is called so
285 we know whether to go ahead with the normal binop or look for a
286 user defined function instead.
288 For now, we do not overload the `=' operator. */
291 binop_user_defined_p (enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
293 struct type
*type1
, *type2
;
294 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
296 type1
= check_typedef (VALUE_TYPE (arg1
));
297 type2
= check_typedef (VALUE_TYPE (arg2
));
298 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
299 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
300 || (TYPE_CODE (type1
) == TYPE_CODE_REF
301 && TYPE_CODE (TYPE_TARGET_TYPE (type1
)) == TYPE_CODE_STRUCT
)
302 || (TYPE_CODE (type2
) == TYPE_CODE_REF
303 && TYPE_CODE (TYPE_TARGET_TYPE (type2
)) == TYPE_CODE_STRUCT
));
306 /* Check to see if argument is a structure. This is called so
307 we know whether to go ahead with the normal unop or look for a
308 user defined function instead.
310 For now, we do not overload the `&' operator. */
313 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
318 type1
= check_typedef (VALUE_TYPE (arg1
));
321 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
323 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
324 type1
= TYPE_TARGET_TYPE (type1
);
330 /* We know either arg1 or arg2 is a structure, so try to find the right
331 user defined function. Create an argument vector that calls
332 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
333 binary operator which is legal for GNU C++).
335 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
336 is the opcode saying how to modify it. Otherwise, OTHEROP is
340 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
341 enum exp_opcode otherop
, enum noside noside
)
343 struct value
**argvec
;
353 /* now we know that what we have to do is construct our
354 arg vector and find the right function to call it with. */
356 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
357 error ("Can't do that binary op on that type"); /* FIXME be explicit */
359 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
360 argvec
[1] = value_addr (arg1
);
364 /* make the right function name up */
365 strcpy (tstr
, "operator__");
390 case BINOP_BITWISE_AND
:
393 case BINOP_BITWISE_IOR
:
396 case BINOP_BITWISE_XOR
:
399 case BINOP_LOGICAL_AND
:
402 case BINOP_LOGICAL_OR
:
414 case BINOP_ASSIGN_MODIFY
:
432 case BINOP_BITWISE_AND
:
435 case BINOP_BITWISE_IOR
:
438 case BINOP_BITWISE_XOR
:
441 case BINOP_MOD
: /* invalid */
443 error ("Invalid binary operation specified.");
446 case BINOP_SUBSCRIPT
:
467 case BINOP_MOD
: /* invalid */
469 error ("Invalid binary operation specified.");
472 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
478 argvec
[1] = argvec
[0];
481 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
483 struct type
*return_type
;
485 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
486 return value_zero (return_type
, VALUE_LVAL (arg1
));
488 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
490 error ("member function %s not found", tstr
);
492 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
496 /* We know that arg1 is a structure, so try to find a unary user
497 defined operator that matches the operator in question.
498 Create an argument vector that calls arg1.operator @ (arg1)
499 and return that value (where '@' is (almost) any unary operator which
500 is legal for GNU C++). */
503 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
505 struct value
**argvec
;
506 char *ptr
, *mangle_ptr
;
507 char tstr
[13], mangle_tstr
[13];
508 int static_memfuncp
, nargs
;
513 /* now we know that what we have to do is construct our
514 arg vector and find the right function to call it with. */
516 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
517 error ("Can't do that unary op on that type"); /* FIXME be explicit */
519 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
520 argvec
[1] = value_addr (arg1
);
525 /* make the right function name up */
526 strcpy (tstr
, "operator__");
528 strcpy (mangle_tstr
, "__");
529 mangle_ptr
= mangle_tstr
+ 2;
532 case UNOP_PREINCREMENT
:
535 case UNOP_PREDECREMENT
:
538 case UNOP_POSTINCREMENT
:
540 argvec
[2] = value_from_longest (builtin_type_int
, 0);
544 case UNOP_POSTDECREMENT
:
546 argvec
[2] = value_from_longest (builtin_type_int
, 0);
550 case UNOP_LOGICAL_NOT
:
553 case UNOP_COMPLEMENT
:
563 error ("Invalid unary operation specified.");
566 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
572 argvec
[1] = argvec
[0];
576 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
578 struct type
*return_type
;
580 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
581 return value_zero (return_type
, VALUE_LVAL (arg1
));
583 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
585 error ("member function %s not found", tstr
);
586 return 0; /* For lint -- never reached */
590 /* Concatenate two values with the following conditions:
592 (1) Both values must be either bitstring values or character string
593 values and the resulting value consists of the concatenation of
594 ARG1 followed by ARG2.
598 One value must be an integer value and the other value must be
599 either a bitstring value or character string value, which is
600 to be repeated by the number of times specified by the integer
604 (2) Boolean values are also allowed and are treated as bit string
607 (3) Character values are also allowed and are treated as character
608 string values of length 1.
612 value_concat (struct value
*arg1
, struct value
*arg2
)
614 struct value
*inval1
;
615 struct value
*inval2
;
616 struct value
*outval
= NULL
;
617 int inval1len
, inval2len
;
621 struct type
*type1
= check_typedef (VALUE_TYPE (arg1
));
622 struct type
*type2
= check_typedef (VALUE_TYPE (arg2
));
624 COERCE_VARYING_ARRAY (arg1
, type1
);
625 COERCE_VARYING_ARRAY (arg2
, type2
);
627 /* First figure out if we are dealing with two values to be concatenated
628 or a repeat count and a value to be repeated. INVAL1 is set to the
629 first of two concatenated values, or the repeat count. INVAL2 is set
630 to the second of the two concatenated values or the value to be
633 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
635 struct type
*tmp
= type1
;
647 /* Now process the input values. */
649 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
651 /* We have a repeat count. Validate the second value and then
652 construct a value repeated that many times. */
653 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
654 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
656 count
= longest_to_int (value_as_long (inval1
));
657 inval2len
= TYPE_LENGTH (type2
);
658 ptr
= (char *) alloca (count
* inval2len
);
659 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
661 inchar
= (char) unpack_long (type2
,
662 VALUE_CONTENTS (inval2
));
663 for (idx
= 0; idx
< count
; idx
++)
665 *(ptr
+ idx
) = inchar
;
670 for (idx
= 0; idx
< count
; idx
++)
672 memcpy (ptr
+ (idx
* inval2len
), VALUE_CONTENTS (inval2
),
676 outval
= value_string (ptr
, count
* inval2len
);
678 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
679 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
681 error ("unimplemented support for bitstring/boolean repeats");
685 error ("can't repeat values of that type");
688 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
689 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
691 /* We have two character strings to concatenate. */
692 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
693 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
695 error ("Strings can only be concatenated with other strings.");
697 inval1len
= TYPE_LENGTH (type1
);
698 inval2len
= TYPE_LENGTH (type2
);
699 ptr
= (char *) alloca (inval1len
+ inval2len
);
700 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
702 *ptr
= (char) unpack_long (type1
, VALUE_CONTENTS (inval1
));
706 memcpy (ptr
, VALUE_CONTENTS (inval1
), inval1len
);
708 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
711 (char) unpack_long (type2
, VALUE_CONTENTS (inval2
));
715 memcpy (ptr
+ inval1len
, VALUE_CONTENTS (inval2
), inval2len
);
717 outval
= value_string (ptr
, inval1len
+ inval2len
);
719 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
720 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
722 /* We have two bitstrings to concatenate. */
723 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
724 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
726 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
728 error ("unimplemented support for bitstring/boolean concatenation.");
732 /* We don't know how to concatenate these operands. */
733 error ("illegal operands for concatenation.");
740 /* Perform a binary operation on two operands which have reasonable
741 representations as integers or floats. This includes booleans,
742 characters, integers, or floats.
743 Does not support addition and subtraction on pointers;
744 use value_add or value_sub if you want to handle those possibilities. */
747 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
750 struct type
*type1
, *type2
;
756 type1
= check_typedef (VALUE_TYPE (arg1
));
757 type2
= check_typedef (VALUE_TYPE (arg2
));
759 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
760 && TYPE_CODE (type1
) != TYPE_CODE_CHAR
761 && TYPE_CODE (type1
) != TYPE_CODE_INT
762 && TYPE_CODE (type1
) != TYPE_CODE_BOOL
763 && TYPE_CODE (type1
) != TYPE_CODE_RANGE
)
765 (TYPE_CODE (type2
) != TYPE_CODE_FLT
766 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
767 && TYPE_CODE (type2
) != TYPE_CODE_INT
768 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
769 && TYPE_CODE (type2
) != TYPE_CODE_RANGE
))
770 error ("Argument to arithmetic operation not a number or boolean.");
772 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
774 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
776 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
777 in target format. real.c in GCC probably has the necessary
779 DOUBLEST v1
, v2
, v
= 0;
780 v1
= value_as_double (arg1
);
781 v2
= value_as_double (arg2
);
803 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
807 error ("Integer-only operation on floating point number.");
810 /* If either arg was long double, make sure that value is also long
813 if (TYPE_LENGTH (type1
) * 8 > TARGET_DOUBLE_BIT
814 || TYPE_LENGTH (type2
) * 8 > TARGET_DOUBLE_BIT
)
815 val
= allocate_value (builtin_type_long_double
);
817 val
= allocate_value (builtin_type_double
);
819 store_typed_floating (VALUE_CONTENTS_RAW (val
), VALUE_TYPE (val
), v
);
821 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
823 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
825 LONGEST v1
, v2
, v
= 0;
826 v1
= value_as_long (arg1
);
827 v2
= value_as_long (arg2
);
831 case BINOP_BITWISE_AND
:
835 case BINOP_BITWISE_IOR
:
839 case BINOP_BITWISE_XOR
:
852 error ("Invalid operation on booleans.");
855 val
= allocate_value (type1
);
856 store_signed_integer (VALUE_CONTENTS_RAW (val
),
861 /* Integral operations here. */
862 /* FIXME: Also mixed integral/booleans, with result an integer. */
863 /* FIXME: This implements ANSI C rules (also correct for C++).
864 What about FORTRAN and (the deleted) chill ? */
866 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
867 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
868 int is_unsigned1
= TYPE_UNSIGNED (type1
);
869 int is_unsigned2
= TYPE_UNSIGNED (type2
);
870 unsigned int result_len
;
871 int unsigned_operation
;
873 /* Determine type length and signedness after promotion for
875 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
878 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
880 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
883 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
886 /* Determine type length of the result, and if the operation should
888 Use the signedness of the operand with the greater length.
889 If both operands are of equal length, use unsigned operation
890 if one of the operands is unsigned. */
891 if (promoted_len1
> promoted_len2
)
893 unsigned_operation
= is_unsigned1
;
894 result_len
= promoted_len1
;
896 else if (promoted_len2
> promoted_len1
)
898 unsigned_operation
= is_unsigned2
;
899 result_len
= promoted_len2
;
903 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
904 result_len
= promoted_len1
;
907 if (unsigned_operation
)
909 ULONGEST v1
, v2
, v
= 0;
910 v1
= (ULONGEST
) value_as_long (arg1
);
911 v2
= (ULONGEST
) value_as_long (arg2
);
913 /* Truncate values to the type length of the result. */
914 if (result_len
< sizeof (ULONGEST
))
916 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
917 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
941 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
949 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
950 v1 mod 0 has a defined value, v1. */
958 /* Note floor(v1/v2) == v1/v2 for unsigned. */
971 case BINOP_BITWISE_AND
:
975 case BINOP_BITWISE_IOR
:
979 case BINOP_BITWISE_XOR
:
983 case BINOP_LOGICAL_AND
:
987 case BINOP_LOGICAL_OR
:
992 v
= v1
< v2
? v1
: v2
;
996 v
= v1
> v2
? v1
: v2
;
1003 case BINOP_NOTEQUAL
:
1012 error ("Invalid binary operation on numbers.");
1015 /* This is a kludge to get around the fact that we don't
1016 know how to determine the result type from the types of
1017 the operands. (I'm not really sure how much we feel the
1018 need to duplicate the exact rules of the current
1019 language. They can get really hairy. But not to do so
1020 makes it hard to document just what we *do* do). */
1022 /* Can't just call init_type because we wouldn't know what
1023 name to give the type. */
1024 val
= allocate_value
1025 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1026 ? builtin_type_unsigned_long_long
1027 : builtin_type_unsigned_long
);
1028 store_unsigned_integer (VALUE_CONTENTS_RAW (val
),
1029 TYPE_LENGTH (VALUE_TYPE (val
)),
1034 LONGEST v1
, v2
, v
= 0;
1035 v1
= value_as_long (arg1
);
1036 v2
= value_as_long (arg2
);
1059 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
1067 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1068 X mod 0 has a defined value, X. */
1076 /* Compute floor. */
1077 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1093 case BINOP_BITWISE_AND
:
1097 case BINOP_BITWISE_IOR
:
1101 case BINOP_BITWISE_XOR
:
1105 case BINOP_LOGICAL_AND
:
1109 case BINOP_LOGICAL_OR
:
1114 v
= v1
< v2
? v1
: v2
;
1118 v
= v1
> v2
? v1
: v2
;
1130 error ("Invalid binary operation on numbers.");
1133 /* This is a kludge to get around the fact that we don't
1134 know how to determine the result type from the types of
1135 the operands. (I'm not really sure how much we feel the
1136 need to duplicate the exact rules of the current
1137 language. They can get really hairy. But not to do so
1138 makes it hard to document just what we *do* do). */
1140 /* Can't just call init_type because we wouldn't know what
1141 name to give the type. */
1142 val
= allocate_value
1143 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1144 ? builtin_type_long_long
1145 : builtin_type_long
);
1146 store_signed_integer (VALUE_CONTENTS_RAW (val
),
1147 TYPE_LENGTH (VALUE_TYPE (val
)),
1155 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1158 value_logical_not (struct value
*arg1
)
1164 COERCE_NUMBER (arg1
);
1165 type1
= check_typedef (VALUE_TYPE (arg1
));
1167 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1168 return 0 == value_as_double (arg1
);
1170 len
= TYPE_LENGTH (type1
);
1171 p
= VALUE_CONTENTS (arg1
);
1182 /* Perform a comparison on two string values (whose content are not
1183 necessarily null terminated) based on their length */
1186 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1188 int len1
= TYPE_LENGTH (VALUE_TYPE (arg1
));
1189 int len2
= TYPE_LENGTH (VALUE_TYPE (arg2
));
1190 char *s1
= VALUE_CONTENTS (arg1
);
1191 char *s2
= VALUE_CONTENTS (arg2
);
1192 int i
, len
= len1
< len2
? len1
: len2
;
1194 for (i
= 0; i
< len
; i
++)
1198 else if (s1
[i
] > s2
[i
])
1206 else if (len1
> len2
)
1212 /* Simulate the C operator == by returning a 1
1213 iff ARG1 and ARG2 have equal contents. */
1216 value_equal (struct value
*arg1
, struct value
*arg2
)
1219 register char *p1
, *p2
;
1220 struct type
*type1
, *type2
;
1221 enum type_code code1
;
1222 enum type_code code2
;
1224 COERCE_NUMBER (arg1
);
1225 COERCE_NUMBER (arg2
);
1227 type1
= check_typedef (VALUE_TYPE (arg1
));
1228 type2
= check_typedef (VALUE_TYPE (arg2
));
1229 code1
= TYPE_CODE (type1
);
1230 code2
= TYPE_CODE (type2
);
1232 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1233 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1234 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1236 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1237 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1238 return value_as_double (arg1
) == value_as_double (arg2
);
1240 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1242 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1243 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1244 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1245 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1247 else if (code1
== code2
1248 && ((len
= (int) TYPE_LENGTH (type1
))
1249 == (int) TYPE_LENGTH (type2
)))
1251 p1
= VALUE_CONTENTS (arg1
);
1252 p2
= VALUE_CONTENTS (arg2
);
1260 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1262 return value_strcmp (arg1
, arg2
) == 0;
1266 error ("Invalid type combination in equality test.");
1267 return 0; /* For lint -- never reached */
1271 /* Simulate the C operator < by returning 1
1272 iff ARG1's contents are less than ARG2's. */
1275 value_less (struct value
*arg1
, struct value
*arg2
)
1277 register enum type_code code1
;
1278 register enum type_code code2
;
1279 struct type
*type1
, *type2
;
1281 COERCE_NUMBER (arg1
);
1282 COERCE_NUMBER (arg2
);
1284 type1
= check_typedef (VALUE_TYPE (arg1
));
1285 type2
= check_typedef (VALUE_TYPE (arg2
));
1286 code1
= TYPE_CODE (type1
);
1287 code2
= TYPE_CODE (type2
);
1289 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1290 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1291 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1293 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1294 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1295 return value_as_double (arg1
) < value_as_double (arg2
);
1296 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1297 return value_as_address (arg1
) < value_as_address (arg2
);
1299 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1301 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1302 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1303 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1304 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1305 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1306 return value_strcmp (arg1
, arg2
) < 0;
1309 error ("Invalid type combination in ordering comparison.");
1314 /* The unary operators - and ~. Both free the argument ARG1. */
1317 value_neg (struct value
*arg1
)
1319 register struct type
*type
;
1320 register struct type
*result_type
= VALUE_TYPE (arg1
);
1325 type
= check_typedef (VALUE_TYPE (arg1
));
1327 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1328 return value_from_double (result_type
, -value_as_double (arg1
));
1329 else if (TYPE_CODE (type
) == TYPE_CODE_INT
|| TYPE_CODE (type
) == TYPE_CODE_BOOL
)
1331 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1332 FORTRAN and (the deleted) chill ? */
1333 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1334 result_type
= builtin_type_int
;
1336 return value_from_longest (result_type
, -value_as_long (arg1
));
1340 error ("Argument to negate operation not a number.");
1341 return 0; /* For lint -- never reached */
1346 value_complement (struct value
*arg1
)
1348 register struct type
*type
;
1349 register struct type
*result_type
= VALUE_TYPE (arg1
);
1355 type
= check_typedef (VALUE_TYPE (arg1
));
1357 typecode
= TYPE_CODE (type
);
1358 if ((typecode
!= TYPE_CODE_INT
) && (typecode
!= TYPE_CODE_BOOL
))
1359 error ("Argument to complement operation not an integer or boolean.");
1361 /* Perform integral promotion for ANSI C/C++.
1362 FIXME: What about FORTRAN ? */
1363 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1364 result_type
= builtin_type_int
;
1366 return value_from_longest (result_type
, ~value_as_long (arg1
));
1369 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1370 and whose VALUE_CONTENTS is valaddr.
1371 Return -1 if out of range, -2 other error. */
1374 value_bit_index (struct type
*type
, char *valaddr
, int index
)
1376 LONGEST low_bound
, high_bound
;
1379 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1380 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1382 if (index
< low_bound
|| index
> high_bound
)
1384 rel_index
= index
- low_bound
;
1385 word
= unpack_long (builtin_type_unsigned_char
,
1386 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1387 rel_index
%= TARGET_CHAR_BIT
;
1388 if (BITS_BIG_ENDIAN
)
1389 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1390 return (word
>> rel_index
) & 1;
1394 value_in (struct value
*element
, struct value
*set
)
1397 struct type
*settype
= check_typedef (VALUE_TYPE (set
));
1398 struct type
*eltype
= check_typedef (VALUE_TYPE (element
));
1399 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1400 eltype
= TYPE_TARGET_TYPE (eltype
);
1401 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1402 error ("Second argument of 'IN' has wrong type");
1403 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1404 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1405 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1406 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1407 error ("First argument of 'IN' has wrong type");
1408 member
= value_bit_index (settype
, VALUE_CONTENTS (set
),
1409 value_as_long (element
));
1411 error ("First argument of 'IN' not in range");
1412 return value_from_longest (LA_BOOL_TYPE
, member
);
1416 _initialize_valarith (void)