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, 2004 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"
36 /* Define whether or not the C operator '/' truncates towards zero for
37 differently signed operands (truncation direction is undefined in C). */
39 #ifndef TRUNCATION_TOWARDS_ZERO
40 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
43 static struct value
*value_subscripted_rvalue (struct value
*, struct value
*, int);
45 void _initialize_valarith (void);
48 /* Given a pointer, return the size of its target.
49 If the pointer type is void *, then return 1.
50 If the target type is incomplete, then error out.
51 This isn't a general purpose function, but just a
52 helper for value_sub & value_add.
56 find_size_for_pointer_math (struct type
*ptr_type
)
59 struct type
*ptr_target
;
61 ptr_target
= check_typedef (TYPE_TARGET_TYPE (ptr_type
));
63 sz
= TYPE_LENGTH (ptr_target
);
66 if (TYPE_CODE (ptr_type
) == TYPE_CODE_VOID
)
72 name
= TYPE_NAME (ptr_target
);
74 name
= TYPE_TAG_NAME (ptr_target
);
76 error ("Cannot perform pointer math on incomplete types, "
77 "try casting to a known type, or void *.");
79 error ("Cannot perform pointer math on incomplete type \"%s\", "
80 "try casting to a known type, or void *.", name
);
87 value_add (struct value
*arg1
, struct value
*arg2
)
92 struct type
*type1
, *type2
, *valptrtype
;
96 type1
= check_typedef (VALUE_TYPE (arg1
));
97 type2
= check_typedef (VALUE_TYPE (arg2
));
99 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
100 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
102 (is_integral_type (type1
) || is_integral_type (type2
)))
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
;
138 type1
= check_typedef (VALUE_TYPE (arg1
));
139 type2
= check_typedef (VALUE_TYPE (arg2
));
141 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
143 if (is_integral_type (type2
))
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
));
189 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
190 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
192 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
193 LONGEST lowerbound
, upperbound
;
194 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
196 if (VALUE_LVAL (array
) != lval_memory
)
197 return value_subscripted_rvalue (array
, idx
, lowerbound
);
201 LONGEST index
= value_as_long (idx
);
202 if (index
>= lowerbound
&& index
<= upperbound
)
203 return value_subscripted_rvalue (array
, idx
, lowerbound
);
204 /* Emit warning unless we have an array of unknown size.
205 An array of unknown size has lowerbound 0 and upperbound -1. */
207 warning ("array or string index out of range");
208 /* fall doing C stuff */
214 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
215 idx
= value_sub (idx
, bound
);
218 array
= value_coerce_array (array
);
221 if (TYPE_CODE (tarray
) == TYPE_CODE_BITSTRING
)
223 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
224 LONGEST index
= value_as_long (idx
);
226 int offset
, byte
, bit_index
;
227 LONGEST lowerbound
, upperbound
;
228 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
229 if (index
< lowerbound
|| index
> upperbound
)
230 error ("bitstring index out of range");
232 offset
= index
/ TARGET_CHAR_BIT
;
233 byte
= *((char *) VALUE_CONTENTS (array
) + offset
);
234 bit_index
= index
% TARGET_CHAR_BIT
;
235 byte
>>= (BITS_BIG_ENDIAN
? TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
236 v
= value_from_longest (LA_BOOL_TYPE
, byte
& 1);
237 VALUE_BITPOS (v
) = bit_index
;
238 VALUE_BITSIZE (v
) = 1;
239 VALUE_LVAL (v
) = VALUE_LVAL (array
);
240 if (VALUE_LVAL (array
) == lval_internalvar
)
241 VALUE_LVAL (v
) = lval_internalvar_component
;
242 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
243 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (array
);
248 return value_ind (value_add (array
, idx
));
250 error ("not an array or string");
253 /* Return the value of EXPR[IDX], expr an aggregate rvalue
254 (eg, a vector register). This routine used to promote floats
255 to doubles, but no longer does. */
257 static struct value
*
258 value_subscripted_rvalue (struct value
*array
, struct value
*idx
, int lowerbound
)
260 struct type
*array_type
= check_typedef (VALUE_TYPE (array
));
261 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
262 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
263 LONGEST index
= value_as_long (idx
);
264 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
267 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
268 error ("no such vector element");
270 v
= allocate_value (elt_type
);
271 if (VALUE_LAZY (array
))
274 memcpy (VALUE_CONTENTS (v
), VALUE_CONTENTS (array
) + elt_offs
, elt_size
);
276 if (VALUE_LVAL (array
) == lval_internalvar
)
277 VALUE_LVAL (v
) = lval_internalvar_component
;
279 VALUE_LVAL (v
) = VALUE_LVAL (array
);
280 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
281 VALUE_REGNO (v
) = VALUE_REGNO (array
);
282 VALUE_OFFSET (v
) = VALUE_OFFSET (array
) + elt_offs
;
286 /* Check to see if either argument is a structure. This is called so
287 we know whether to go ahead with the normal binop or look for a
288 user defined function instead.
290 For now, we do not overload the `=' operator. */
293 binop_user_defined_p (enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
295 struct type
*type1
, *type2
;
296 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
298 type1
= check_typedef (VALUE_TYPE (arg1
));
299 type2
= check_typedef (VALUE_TYPE (arg2
));
300 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
301 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
302 || (TYPE_CODE (type1
) == TYPE_CODE_REF
303 && TYPE_CODE (TYPE_TARGET_TYPE (type1
)) == TYPE_CODE_STRUCT
)
304 || (TYPE_CODE (type2
) == TYPE_CODE_REF
305 && TYPE_CODE (TYPE_TARGET_TYPE (type2
)) == TYPE_CODE_STRUCT
));
308 /* Check to see if argument is a structure. This is called so
309 we know whether to go ahead with the normal unop or look for a
310 user defined function instead.
312 For now, we do not overload the `&' operator. */
315 unop_user_defined_p (enum exp_opcode op
, struct value
*arg1
)
320 type1
= check_typedef (VALUE_TYPE (arg1
));
323 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
325 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
326 type1
= TYPE_TARGET_TYPE (type1
);
332 /* We know either arg1 or arg2 is a structure, so try to find the right
333 user defined function. Create an argument vector that calls
334 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
335 binary operator which is legal for GNU C++).
337 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
338 is the opcode saying how to modify it. Otherwise, OTHEROP is
342 value_x_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
,
343 enum exp_opcode otherop
, enum noside noside
)
345 struct value
**argvec
;
355 /* now we know that what we have to do is construct our
356 arg vector and find the right function to call it with. */
358 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
359 error ("Can't do that binary op on that type"); /* FIXME be explicit */
361 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
362 argvec
[1] = value_addr (arg1
);
366 /* make the right function name up */
367 strcpy (tstr
, "operator__");
392 case BINOP_BITWISE_AND
:
395 case BINOP_BITWISE_IOR
:
398 case BINOP_BITWISE_XOR
:
401 case BINOP_LOGICAL_AND
:
404 case BINOP_LOGICAL_OR
:
416 case BINOP_ASSIGN_MODIFY
:
434 case BINOP_BITWISE_AND
:
437 case BINOP_BITWISE_IOR
:
440 case BINOP_BITWISE_XOR
:
443 case BINOP_MOD
: /* invalid */
445 error ("Invalid binary operation specified.");
448 case BINOP_SUBSCRIPT
:
469 case BINOP_MOD
: /* invalid */
471 error ("Invalid binary operation specified.");
474 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
480 argvec
[1] = argvec
[0];
483 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
485 struct type
*return_type
;
487 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
488 return value_zero (return_type
, VALUE_LVAL (arg1
));
490 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
492 error ("member function %s not found", tstr
);
494 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
498 /* We know that arg1 is a structure, so try to find a unary user
499 defined operator that matches the operator in question.
500 Create an argument vector that calls arg1.operator @ (arg1)
501 and return that value (where '@' is (almost) any unary operator which
502 is legal for GNU C++). */
505 value_x_unop (struct value
*arg1
, enum exp_opcode op
, enum noside noside
)
507 struct value
**argvec
;
508 char *ptr
, *mangle_ptr
;
509 char tstr
[13], mangle_tstr
[13];
510 int static_memfuncp
, nargs
;
515 /* now we know that what we have to do is construct our
516 arg vector and find the right function to call it with. */
518 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
519 error ("Can't do that unary op on that type"); /* FIXME be explicit */
521 argvec
= (struct value
**) alloca (sizeof (struct value
*) * 4);
522 argvec
[1] = value_addr (arg1
);
527 /* make the right function name up */
528 strcpy (tstr
, "operator__");
530 strcpy (mangle_tstr
, "__");
531 mangle_ptr
= mangle_tstr
+ 2;
534 case UNOP_PREINCREMENT
:
537 case UNOP_PREDECREMENT
:
540 case UNOP_POSTINCREMENT
:
542 argvec
[2] = value_from_longest (builtin_type_int
, 0);
546 case UNOP_POSTDECREMENT
:
548 argvec
[2] = value_from_longest (builtin_type_int
, 0);
552 case UNOP_LOGICAL_NOT
:
555 case UNOP_COMPLEMENT
:
565 error ("Invalid unary operation specified.");
568 argvec
[0] = value_struct_elt (&arg1
, argvec
+ 1, tstr
, &static_memfuncp
, "structure");
574 argvec
[1] = argvec
[0];
578 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
580 struct type
*return_type
;
582 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
583 return value_zero (return_type
, VALUE_LVAL (arg1
));
585 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
587 error ("member function %s not found", tstr
);
588 return 0; /* For lint -- never reached */
592 /* Concatenate two values with the following conditions:
594 (1) Both values must be either bitstring values or character string
595 values and the resulting value consists of the concatenation of
596 ARG1 followed by ARG2.
600 One value must be an integer value and the other value must be
601 either a bitstring value or character string value, which is
602 to be repeated by the number of times specified by the integer
606 (2) Boolean values are also allowed and are treated as bit string
609 (3) Character values are also allowed and are treated as character
610 string values of length 1.
614 value_concat (struct value
*arg1
, struct value
*arg2
)
616 struct value
*inval1
;
617 struct value
*inval2
;
618 struct value
*outval
= NULL
;
619 int inval1len
, inval2len
;
623 struct type
*type1
= check_typedef (VALUE_TYPE (arg1
));
624 struct type
*type2
= check_typedef (VALUE_TYPE (arg2
));
626 /* First figure out if we are dealing with two values to be concatenated
627 or a repeat count and a value to be repeated. INVAL1 is set to the
628 first of two concatenated values, or the repeat count. INVAL2 is set
629 to the second of the two concatenated values or the value to be
632 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
634 struct type
*tmp
= type1
;
646 /* Now process the input values. */
648 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
650 /* We have a repeat count. Validate the second value and then
651 construct a value repeated that many times. */
652 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
653 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
655 count
= longest_to_int (value_as_long (inval1
));
656 inval2len
= TYPE_LENGTH (type2
);
657 ptr
= (char *) alloca (count
* inval2len
);
658 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
660 inchar
= (char) unpack_long (type2
,
661 VALUE_CONTENTS (inval2
));
662 for (idx
= 0; idx
< count
; idx
++)
664 *(ptr
+ idx
) = inchar
;
669 for (idx
= 0; idx
< count
; idx
++)
671 memcpy (ptr
+ (idx
* inval2len
), VALUE_CONTENTS (inval2
),
675 outval
= value_string (ptr
, count
* inval2len
);
677 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
678 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
680 error ("unimplemented support for bitstring/boolean repeats");
684 error ("can't repeat values of that type");
687 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
688 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
690 /* We have two character strings to concatenate. */
691 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
692 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
694 error ("Strings can only be concatenated with other strings.");
696 inval1len
= TYPE_LENGTH (type1
);
697 inval2len
= TYPE_LENGTH (type2
);
698 ptr
= (char *) alloca (inval1len
+ inval2len
);
699 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
701 *ptr
= (char) unpack_long (type1
, VALUE_CONTENTS (inval1
));
705 memcpy (ptr
, VALUE_CONTENTS (inval1
), inval1len
);
707 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
710 (char) unpack_long (type2
, VALUE_CONTENTS (inval2
));
714 memcpy (ptr
+ inval1len
, VALUE_CONTENTS (inval2
), inval2len
);
716 outval
= value_string (ptr
, inval1len
+ inval2len
);
718 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
719 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
721 /* We have two bitstrings to concatenate. */
722 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
723 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
725 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
727 error ("unimplemented support for bitstring/boolean concatenation.");
731 /* We don't know how to concatenate these operands. */
732 error ("illegal operands for concatenation.");
739 /* Perform a binary operation on two operands which have reasonable
740 representations as integers or floats. This includes booleans,
741 characters, integers, or floats.
742 Does not support addition and subtraction on pointers;
743 use value_add or value_sub if you want to handle those possibilities. */
746 value_binop (struct value
*arg1
, struct value
*arg2
, enum exp_opcode op
)
749 struct type
*type1
, *type2
;
753 type1
= check_typedef (VALUE_TYPE (arg1
));
754 type2
= check_typedef (VALUE_TYPE (arg2
));
756 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
&& !is_integral_type (type1
))
758 (TYPE_CODE (type2
) != TYPE_CODE_FLT
&& !is_integral_type (type2
)))
759 error ("Argument to arithmetic operation not a number or boolean.");
761 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
763 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
765 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
766 in target format. real.c in GCC probably has the necessary
768 DOUBLEST v1
, v2
, v
= 0;
769 v1
= value_as_double (arg1
);
770 v2
= value_as_double (arg2
);
792 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
796 error ("Integer-only operation on floating point number.");
799 /* If either arg was long double, make sure that value is also long
802 if (TYPE_LENGTH (type1
) * 8 > TARGET_DOUBLE_BIT
803 || TYPE_LENGTH (type2
) * 8 > TARGET_DOUBLE_BIT
)
804 val
= allocate_value (builtin_type_long_double
);
806 val
= allocate_value (builtin_type_double
);
808 store_typed_floating (VALUE_CONTENTS_RAW (val
), VALUE_TYPE (val
), v
);
810 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
812 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
814 LONGEST v1
, v2
, v
= 0;
815 v1
= value_as_long (arg1
);
816 v2
= value_as_long (arg2
);
820 case BINOP_BITWISE_AND
:
824 case BINOP_BITWISE_IOR
:
828 case BINOP_BITWISE_XOR
:
841 error ("Invalid operation on booleans.");
844 val
= allocate_value (type1
);
845 store_signed_integer (VALUE_CONTENTS_RAW (val
),
850 /* Integral operations here. */
851 /* FIXME: Also mixed integral/booleans, with result an integer. */
852 /* FIXME: This implements ANSI C rules (also correct for C++).
853 What about FORTRAN and (the deleted) chill ? */
855 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
856 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
857 int is_unsigned1
= TYPE_UNSIGNED (type1
);
858 int is_unsigned2
= TYPE_UNSIGNED (type2
);
859 unsigned int result_len
;
860 int unsigned_operation
;
862 /* Determine type length and signedness after promotion for
864 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
867 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
869 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
872 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
875 /* Determine type length of the result, and if the operation should
877 Use the signedness of the operand with the greater length.
878 If both operands are of equal length, use unsigned operation
879 if one of the operands is unsigned. */
880 if (promoted_len1
> promoted_len2
)
882 unsigned_operation
= is_unsigned1
;
883 result_len
= promoted_len1
;
885 else if (promoted_len2
> promoted_len1
)
887 unsigned_operation
= is_unsigned2
;
888 result_len
= promoted_len2
;
892 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
893 result_len
= promoted_len1
;
896 if (unsigned_operation
)
898 ULONGEST v1
, v2
, v
= 0;
899 v1
= (ULONGEST
) value_as_long (arg1
);
900 v2
= (ULONGEST
) value_as_long (arg2
);
902 /* Truncate values to the type length of the result. */
903 if (result_len
< sizeof (ULONGEST
))
905 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
906 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
930 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
938 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
939 v1 mod 0 has a defined value, v1. */
947 /* Note floor(v1/v2) == v1/v2 for unsigned. */
960 case BINOP_BITWISE_AND
:
964 case BINOP_BITWISE_IOR
:
968 case BINOP_BITWISE_XOR
:
972 case BINOP_LOGICAL_AND
:
976 case BINOP_LOGICAL_OR
:
981 v
= v1
< v2
? v1
: v2
;
985 v
= v1
> v2
? v1
: v2
;
1001 error ("Invalid binary operation on numbers.");
1004 /* This is a kludge to get around the fact that we don't
1005 know how to determine the result type from the types of
1006 the operands. (I'm not really sure how much we feel the
1007 need to duplicate the exact rules of the current
1008 language. They can get really hairy. But not to do so
1009 makes it hard to document just what we *do* do). */
1011 /* Can't just call init_type because we wouldn't know what
1012 name to give the type. */
1013 val
= allocate_value
1014 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1015 ? builtin_type_unsigned_long_long
1016 : builtin_type_unsigned_long
);
1017 store_unsigned_integer (VALUE_CONTENTS_RAW (val
),
1018 TYPE_LENGTH (VALUE_TYPE (val
)),
1023 LONGEST v1
, v2
, v
= 0;
1024 v1
= value_as_long (arg1
);
1025 v2
= value_as_long (arg2
);
1045 error ("Division by zero");
1051 error ("Cannot perform exponentiation: %s", safe_strerror (errno
));
1058 error ("Division by zero");
1062 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1063 X mod 0 has a defined value, X. */
1071 /* Compute floor. */
1072 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
1088 case BINOP_BITWISE_AND
:
1092 case BINOP_BITWISE_IOR
:
1096 case BINOP_BITWISE_XOR
:
1100 case BINOP_LOGICAL_AND
:
1104 case BINOP_LOGICAL_OR
:
1109 v
= v1
< v2
? v1
: v2
;
1113 v
= v1
> v2
? v1
: v2
;
1125 error ("Invalid binary operation on numbers.");
1128 /* This is a kludge to get around the fact that we don't
1129 know how to determine the result type from the types of
1130 the operands. (I'm not really sure how much we feel the
1131 need to duplicate the exact rules of the current
1132 language. They can get really hairy. But not to do so
1133 makes it hard to document just what we *do* do). */
1135 /* Can't just call init_type because we wouldn't know what
1136 name to give the type. */
1137 val
= allocate_value
1138 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1139 ? builtin_type_long_long
1140 : builtin_type_long
);
1141 store_signed_integer (VALUE_CONTENTS_RAW (val
),
1142 TYPE_LENGTH (VALUE_TYPE (val
)),
1150 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1153 value_logical_not (struct value
*arg1
)
1159 COERCE_NUMBER (arg1
);
1160 type1
= check_typedef (VALUE_TYPE (arg1
));
1162 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1163 return 0 == value_as_double (arg1
);
1165 len
= TYPE_LENGTH (type1
);
1166 p
= VALUE_CONTENTS (arg1
);
1177 /* Perform a comparison on two string values (whose content are not
1178 necessarily null terminated) based on their length */
1181 value_strcmp (struct value
*arg1
, struct value
*arg2
)
1183 int len1
= TYPE_LENGTH (VALUE_TYPE (arg1
));
1184 int len2
= TYPE_LENGTH (VALUE_TYPE (arg2
));
1185 char *s1
= VALUE_CONTENTS (arg1
);
1186 char *s2
= VALUE_CONTENTS (arg2
);
1187 int i
, len
= len1
< len2
? len1
: len2
;
1189 for (i
= 0; i
< len
; i
++)
1193 else if (s1
[i
] > s2
[i
])
1201 else if (len1
> len2
)
1207 /* Simulate the C operator == by returning a 1
1208 iff ARG1 and ARG2 have equal contents. */
1211 value_equal (struct value
*arg1
, struct value
*arg2
)
1215 struct type
*type1
, *type2
;
1216 enum type_code code1
;
1217 enum type_code code2
;
1218 int is_int1
, is_int2
;
1220 COERCE_ARRAY (arg1
);
1221 COERCE_ARRAY (arg2
);
1223 type1
= check_typedef (VALUE_TYPE (arg1
));
1224 type2
= check_typedef (VALUE_TYPE (arg2
));
1225 code1
= TYPE_CODE (type1
);
1226 code2
= TYPE_CODE (type2
);
1227 is_int1
= is_integral_type (type1
);
1228 is_int2
= is_integral_type (type2
);
1230 if (is_int1
&& is_int2
)
1231 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1233 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1234 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1235 return value_as_double (arg1
) == value_as_double (arg2
);
1237 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1239 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1240 return value_as_address (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1241 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1242 return (CORE_ADDR
) value_as_long (arg1
) == value_as_address (arg2
);
1244 else if (code1
== code2
1245 && ((len
= (int) TYPE_LENGTH (type1
))
1246 == (int) TYPE_LENGTH (type2
)))
1248 p1
= VALUE_CONTENTS (arg1
);
1249 p2
= VALUE_CONTENTS (arg2
);
1257 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1259 return value_strcmp (arg1
, arg2
) == 0;
1263 error ("Invalid type combination in equality test.");
1264 return 0; /* For lint -- never reached */
1268 /* Simulate the C operator < by returning 1
1269 iff ARG1's contents are less than ARG2's. */
1272 value_less (struct value
*arg1
, struct value
*arg2
)
1274 enum type_code code1
;
1275 enum type_code code2
;
1276 struct type
*type1
, *type2
;
1277 int is_int1
, is_int2
;
1279 COERCE_ARRAY (arg1
);
1280 COERCE_ARRAY (arg2
);
1282 type1
= check_typedef (VALUE_TYPE (arg1
));
1283 type2
= check_typedef (VALUE_TYPE (arg2
));
1284 code1
= TYPE_CODE (type1
);
1285 code2
= TYPE_CODE (type2
);
1286 is_int1
= is_integral_type (type1
);
1287 is_int2
= is_integral_type (type2
);
1289 if (is_int1
&& is_int2
)
1290 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1292 else if ((code1
== TYPE_CODE_FLT
|| is_int1
)
1293 && (code2
== TYPE_CODE_FLT
|| is_int2
))
1294 return value_as_double (arg1
) < value_as_double (arg2
);
1295 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1296 return value_as_address (arg1
) < value_as_address (arg2
);
1298 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1300 else if (code1
== TYPE_CODE_PTR
&& is_int2
)
1301 return value_as_address (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1302 else if (code2
== TYPE_CODE_PTR
&& is_int1
)
1303 return (CORE_ADDR
) value_as_long (arg1
) < value_as_address (arg2
);
1304 else if (code1
== TYPE_CODE_STRING
&& code2
== TYPE_CODE_STRING
)
1305 return value_strcmp (arg1
, arg2
) < 0;
1308 error ("Invalid type combination in ordering comparison.");
1313 /* The unary operators - and ~. Both free the argument ARG1. */
1316 value_neg (struct value
*arg1
)
1319 struct type
*result_type
= VALUE_TYPE (arg1
);
1323 type
= check_typedef (VALUE_TYPE (arg1
));
1325 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1326 return value_from_double (result_type
, -value_as_double (arg1
));
1327 else if (is_integral_type (type
))
1329 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1330 FORTRAN and (the deleted) chill ? */
1331 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1332 result_type
= builtin_type_int
;
1334 return value_from_longest (result_type
, -value_as_long (arg1
));
1338 error ("Argument to negate operation not a number.");
1339 return 0; /* For lint -- never reached */
1344 value_complement (struct value
*arg1
)
1347 struct type
*result_type
= VALUE_TYPE (arg1
);
1351 type
= check_typedef (VALUE_TYPE (arg1
));
1353 if (!is_integral_type (type
))
1354 error ("Argument to complement operation not an integer or boolean.");
1356 /* Perform integral promotion for ANSI C/C++.
1357 FIXME: What about FORTRAN ? */
1358 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1359 result_type
= builtin_type_int
;
1361 return value_from_longest (result_type
, ~value_as_long (arg1
));
1364 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1365 and whose VALUE_CONTENTS is valaddr.
1366 Return -1 if out of range, -2 other error. */
1369 value_bit_index (struct type
*type
, char *valaddr
, int index
)
1371 LONGEST low_bound
, high_bound
;
1374 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1375 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1377 if (index
< low_bound
|| index
> high_bound
)
1379 rel_index
= index
- low_bound
;
1380 word
= unpack_long (builtin_type_unsigned_char
,
1381 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1382 rel_index
%= TARGET_CHAR_BIT
;
1383 if (BITS_BIG_ENDIAN
)
1384 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1385 return (word
>> rel_index
) & 1;
1389 value_in (struct value
*element
, struct value
*set
)
1392 struct type
*settype
= check_typedef (VALUE_TYPE (set
));
1393 struct type
*eltype
= check_typedef (VALUE_TYPE (element
));
1394 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1395 eltype
= TYPE_TARGET_TYPE (eltype
);
1396 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1397 error ("Second argument of 'IN' has wrong type");
1398 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1399 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1400 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1401 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1402 error ("First argument of 'IN' has wrong type");
1403 member
= value_bit_index (settype
, VALUE_CONTENTS (set
),
1404 value_as_long (element
));
1406 error ("First argument of 'IN' not in range");
1407 return value_from_longest (LA_BOOL_TYPE
, member
);
1411 _initialize_valarith (void)