1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright 1986, 1989, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "expression.h"
29 #include "gdb_string.h"
31 /* Define whether or not the C operator '/' truncates towards zero for
32 differently signed operands (truncation direction is undefined in C). */
34 #ifndef TRUNCATION_TOWARDS_ZERO
35 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
38 static value_ptr value_subscripted_rvalue
PARAMS ((value_ptr
, value_ptr
, int));
42 value_add (arg1
, arg2
)
45 register value_ptr valint
, valptr
;
51 if ((TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
52 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_PTR
)
54 (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
55 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
))
56 /* Exactly one argument is a pointer, and one is an integer. */
58 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
68 len
= TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr
)));
69 if (len
== 0) len
= 1; /* For (void *) */
70 return value_from_longest (VALUE_TYPE (valptr
),
71 value_as_long (valptr
)
72 + (len
* value_as_long (valint
)));
75 return value_binop (arg1
, arg2
, BINOP_ADD
);
79 value_sub (arg1
, arg2
)
86 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
)
88 if (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
)
90 /* pointer - integer. */
91 return value_from_longest
94 - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)))
95 * value_as_long (arg2
)));
97 else if (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_PTR
98 && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)))
99 == TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))))
101 /* pointer to <type x> - pointer to <type x>. */
102 return value_from_longest
103 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
104 (value_as_long (arg1
) - value_as_long (arg2
))
105 / (LONGEST
) (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)))));
110 First argument of `-' is a pointer and second argument is neither\n\
111 an integer nor a pointer of the same type.");
115 return value_binop (arg1
, arg2
, BINOP_SUB
);
118 /* Return the value of ARRAY[IDX].
119 See comments in value_coerce_array() for rationale for reason for
120 doing lower bounds adjustment here rather than there.
121 FIXME: Perhaps we should validate that the index is valid and if
122 verbosity is set, warn about invalid indices (but still use them). */
125 value_subscript (array
, idx
)
126 value_ptr array
, idx
;
129 int c_style
= current_language
->c_style_arrays
;
132 COERCE_VARYING_ARRAY (array
);
134 if (TYPE_CODE (VALUE_TYPE (array
)) == TYPE_CODE_ARRAY
135 || TYPE_CODE (VALUE_TYPE (array
)) == TYPE_CODE_STRING
)
137 struct type
*range_type
= TYPE_FIELD_TYPE (VALUE_TYPE (array
), 0);
138 int lowerbound
= TYPE_LOW_BOUND (range_type
);
139 int upperbound
= TYPE_HIGH_BOUND (range_type
);
141 if (VALUE_LVAL (array
) != lval_memory
)
142 return value_subscripted_rvalue (array
, idx
, lowerbound
);
146 LONGEST index
= value_as_long (idx
);
147 if (index
>= lowerbound
&& index
<= upperbound
)
148 return value_subscripted_rvalue (array
, idx
, lowerbound
);
149 warning ("array or string index out of range");
150 /* fall doing C stuff */
156 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
157 idx
= value_sub (idx
, bound
);
160 array
= value_coerce_array (array
);
163 return value_ind (value_add (array
, idx
));
165 error ("not an array or string");
168 /* Return the value of EXPR[IDX], expr an aggregate rvalue
169 (eg, a vector register). This routine used to promote floats
170 to doubles, but no longer does. */
173 value_subscripted_rvalue (array
, idx
, lowerbound
)
174 value_ptr array
, idx
;
177 struct type
*elt_type
= TYPE_TARGET_TYPE (VALUE_TYPE (array
));
178 int elt_size
= TYPE_LENGTH (elt_type
);
179 LONGEST index
= value_as_long (idx
);
180 int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
183 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (VALUE_TYPE (array
)))
184 error ("no such vector element");
186 v
= allocate_value (elt_type
);
187 if (VALUE_LAZY (array
))
190 memcpy (VALUE_CONTENTS (v
), VALUE_CONTENTS (array
) + elt_offs
, elt_size
);
192 if (VALUE_LVAL (array
) == lval_internalvar
)
193 VALUE_LVAL (v
) = lval_internalvar_component
;
195 VALUE_LVAL (v
) = VALUE_LVAL (array
);
196 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
197 VALUE_OFFSET (v
) = VALUE_OFFSET (array
) + elt_offs
;
198 VALUE_BITSIZE (v
) = elt_size
* 8;
202 /* Check to see if either argument is a structure. This is called so
203 we know whether to go ahead with the normal binop or look for a
204 user defined function instead.
206 For now, we do not overload the `=' operator. */
209 binop_user_defined_p (op
, arg1
, arg2
)
211 value_ptr arg1
, arg2
;
213 if (op
== BINOP_ASSIGN
)
215 return (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRUCT
216 || TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_STRUCT
217 || (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
218 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) == TYPE_CODE_STRUCT
)
219 || (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_REF
220 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) == TYPE_CODE_STRUCT
));
223 /* Check to see if argument is a structure. This is called so
224 we know whether to go ahead with the normal unop or look for a
225 user defined function instead.
227 For now, we do not overload the `&' operator. */
229 int unop_user_defined_p (op
, arg1
)
235 return (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_STRUCT
236 || (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
237 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
))) == TYPE_CODE_STRUCT
));
240 /* We know either arg1 or arg2 is a structure, so try to find the right
241 user defined function. Create an argument vector that calls
242 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
243 binary operator which is legal for GNU C++).
245 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
246 is the opcode saying how to modify it. Otherwise, OTHEROP is
250 value_x_binop (arg1
, arg2
, op
, otherop
)
251 value_ptr arg1
, arg2
;
252 enum exp_opcode op
, otherop
;
264 /* now we know that what we have to do is construct our
265 arg vector and find the right function to call it with. */
267 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_STRUCT
)
268 error ("Can't do that binary op on that type"); /* FIXME be explicit */
270 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * 4);
271 argvec
[1] = value_addr (arg1
);
275 /* make the right function name up */
276 strcpy(tstr
, "operator__");
280 case BINOP_ADD
: strcpy(ptr
,"+"); break;
281 case BINOP_SUB
: strcpy(ptr
,"-"); break;
282 case BINOP_MUL
: strcpy(ptr
,"*"); break;
283 case BINOP_DIV
: strcpy(ptr
,"/"); break;
284 case BINOP_REM
: strcpy(ptr
,"%"); break;
285 case BINOP_LSH
: strcpy(ptr
,"<<"); break;
286 case BINOP_RSH
: strcpy(ptr
,">>"); break;
287 case BINOP_BITWISE_AND
: strcpy(ptr
,"&"); break;
288 case BINOP_BITWISE_IOR
: strcpy(ptr
,"|"); break;
289 case BINOP_BITWISE_XOR
: strcpy(ptr
,"^"); break;
290 case BINOP_LOGICAL_AND
: strcpy(ptr
,"&&"); break;
291 case BINOP_LOGICAL_OR
: strcpy(ptr
,"||"); break;
292 case BINOP_MIN
: strcpy(ptr
,"<?"); break;
293 case BINOP_MAX
: strcpy(ptr
,">?"); break;
294 case BINOP_ASSIGN
: strcpy(ptr
,"="); break;
295 case BINOP_ASSIGN_MODIFY
:
298 case BINOP_ADD
: strcpy(ptr
,"+="); break;
299 case BINOP_SUB
: strcpy(ptr
,"-="); break;
300 case BINOP_MUL
: strcpy(ptr
,"*="); break;
301 case BINOP_DIV
: strcpy(ptr
,"/="); break;
302 case BINOP_REM
: strcpy(ptr
,"%="); break;
303 case BINOP_BITWISE_AND
: strcpy(ptr
,"&="); break;
304 case BINOP_BITWISE_IOR
: strcpy(ptr
,"|="); break;
305 case BINOP_BITWISE_XOR
: strcpy(ptr
,"^="); break;
306 case BINOP_MOD
: /* invalid */
308 error ("Invalid binary operation specified.");
311 case BINOP_SUBSCRIPT
: strcpy(ptr
,"[]"); break;
312 case BINOP_EQUAL
: strcpy(ptr
,"=="); break;
313 case BINOP_NOTEQUAL
: strcpy(ptr
,"!="); break;
314 case BINOP_LESS
: strcpy(ptr
,"<"); break;
315 case BINOP_GTR
: strcpy(ptr
,">"); break;
316 case BINOP_GEQ
: strcpy(ptr
,">="); break;
317 case BINOP_LEQ
: strcpy(ptr
,"<="); break;
318 case BINOP_MOD
: /* invalid */
320 error ("Invalid binary operation specified.");
323 argvec
[0] = value_struct_elt (&arg1
, argvec
+1, tstr
, &static_memfuncp
, "structure");
329 argvec
[1] = argvec
[0];
332 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
334 error ("member function %s not found", tstr
);
336 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
340 /* We know that arg1 is a structure, so try to find a unary user
341 defined operator that matches the operator in question.
342 Create an argument vector that calls arg1.operator @ (arg1)
343 and return that value (where '@' is (almost) any unary operator which
344 is legal for GNU C++). */
347 value_x_unop (arg1
, op
)
352 char *ptr
, *mangle_ptr
;
353 char tstr
[13], mangle_tstr
[13];
358 /* now we know that what we have to do is construct our
359 arg vector and find the right function to call it with. */
361 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_STRUCT
)
362 error ("Can't do that unary op on that type"); /* FIXME be explicit */
364 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * 3);
365 argvec
[1] = value_addr (arg1
);
368 /* make the right function name up */
369 strcpy(tstr
,"operator__");
371 strcpy(mangle_tstr
, "__");
372 mangle_ptr
= mangle_tstr
+2;
375 case UNOP_PREINCREMENT
: strcpy(ptr
,"++"); break;
376 case UNOP_PREDECREMENT
: strcpy(ptr
,"++"); break;
377 case UNOP_POSTINCREMENT
: strcpy(ptr
,"++"); break;
378 case UNOP_POSTDECREMENT
: strcpy(ptr
,"++"); break;
379 case UNOP_LOGICAL_NOT
: strcpy(ptr
,"!"); break;
380 case UNOP_COMPLEMENT
: strcpy(ptr
,"~"); break;
381 case UNOP_NEG
: strcpy(ptr
,"-"); break;
383 error ("Invalid binary operation specified.");
386 argvec
[0] = value_struct_elt (&arg1
, argvec
+1, tstr
, &static_memfuncp
, "structure");
392 argvec
[1] = argvec
[0];
395 return call_function_by_hand (argvec
[0], 1 - static_memfuncp
, argvec
+ 1);
397 error ("member function %s not found", tstr
);
398 return 0; /* For lint -- never reached */
402 /* Concatenate two values with the following conditions:
404 (1) Both values must be either bitstring values or character string
405 values and the resulting value consists of the concatenation of
406 ARG1 followed by ARG2.
410 One value must be an integer value and the other value must be
411 either a bitstring value or character string value, which is
412 to be repeated by the number of times specified by the integer
416 (2) Boolean values are also allowed and are treated as bit string
419 (3) Character values are also allowed and are treated as character
420 string values of length 1.
424 value_concat (arg1
, arg2
)
425 value_ptr arg1
, arg2
;
427 register value_ptr inval1
, inval2
, outval
;
428 int inval1len
, inval2len
;
433 /* First figure out if we are dealing with two values to be concatenated
434 or a repeat count and a value to be repeated. INVAL1 is set to the
435 first of two concatenated values, or the repeat count. INVAL2 is set
436 to the second of the two concatenated values or the value to be
439 if (TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_INT
)
450 /* Now process the input values. */
452 if (TYPE_CODE (VALUE_TYPE (inval1
)) == TYPE_CODE_INT
)
454 /* We have a repeat count. Validate the second value and then
455 construct a value repeated that many times. */
456 if (TYPE_CODE (VALUE_TYPE (inval2
)) == TYPE_CODE_STRING
457 || TYPE_CODE (VALUE_TYPE (inval2
)) == TYPE_CODE_CHAR
)
459 count
= longest_to_int (value_as_long (inval1
));
460 inval2len
= TYPE_LENGTH (VALUE_TYPE (inval2
));
461 ptr
= (char *) alloca (count
* inval2len
);
462 if (TYPE_CODE (VALUE_TYPE (inval2
)) == TYPE_CODE_CHAR
)
464 inchar
= (char) unpack_long (VALUE_TYPE (inval2
),
465 VALUE_CONTENTS (inval2
));
466 for (idx
= 0; idx
< count
; idx
++)
468 *(ptr
+ idx
) = inchar
;
473 for (idx
= 0; idx
< count
; idx
++)
475 memcpy (ptr
+ (idx
* inval2len
), VALUE_CONTENTS (inval2
),
479 outval
= value_string (ptr
, count
* inval2len
);
481 else if (TYPE_CODE (VALUE_TYPE (inval2
)) == TYPE_CODE_BITSTRING
482 || TYPE_CODE (VALUE_TYPE (inval2
)) == TYPE_CODE_BOOL
)
484 error ("unimplemented support for bitstring/boolean repeats");
488 error ("can't repeat values of that type");
491 else if (TYPE_CODE (VALUE_TYPE (inval1
)) == TYPE_CODE_STRING
492 || TYPE_CODE (VALUE_TYPE (inval1
)) == TYPE_CODE_CHAR
)
494 /* We have two character strings to concatenate. */
495 if (TYPE_CODE (VALUE_TYPE (inval2
)) != TYPE_CODE_STRING
496 && TYPE_CODE (VALUE_TYPE (inval2
)) != TYPE_CODE_CHAR
)
498 error ("Strings can only be concatenated with other strings.");
500 inval1len
= TYPE_LENGTH (VALUE_TYPE (inval1
));
501 inval2len
= TYPE_LENGTH (VALUE_TYPE (inval2
));
502 ptr
= (char *) alloca (inval1len
+ inval2len
);
503 if (TYPE_CODE (VALUE_TYPE (inval1
)) == TYPE_CODE_CHAR
)
505 *ptr
= (char) unpack_long (VALUE_TYPE (inval1
), VALUE_CONTENTS (inval1
));
509 memcpy (ptr
, VALUE_CONTENTS (inval1
), inval1len
);
511 if (TYPE_CODE (VALUE_TYPE (inval2
)) == TYPE_CODE_CHAR
)
514 (char) unpack_long (VALUE_TYPE (inval2
), VALUE_CONTENTS (inval2
));
518 memcpy (ptr
+ inval1len
, VALUE_CONTENTS (inval2
), inval2len
);
520 outval
= value_string (ptr
, inval1len
+ inval2len
);
522 else if (TYPE_CODE (VALUE_TYPE (inval1
)) == TYPE_CODE_BITSTRING
523 || TYPE_CODE (VALUE_TYPE (inval1
)) == TYPE_CODE_BOOL
)
525 /* We have two bitstrings to concatenate. */
526 if (TYPE_CODE (VALUE_TYPE (inval2
)) != TYPE_CODE_BITSTRING
527 && TYPE_CODE (VALUE_TYPE (inval2
)) != TYPE_CODE_BOOL
)
529 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
531 error ("unimplemented support for bitstring/boolean concatenation.");
535 /* We don't know how to concatenate these operands. */
536 error ("illegal operands for concatenation.");
543 /* Perform a binary operation on two operands which have reasonable
544 representations as integers or floats. This includes booleans,
545 characters, integers, or floats.
546 Does not support addition and subtraction on pointers;
547 use value_add or value_sub if you want to handle those possibilities. */
550 value_binop (arg1
, arg2
, op
)
551 value_ptr arg1
, arg2
;
554 register value_ptr val
;
559 if ((TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_FLT
560 && TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_CHAR
561 && TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
562 && TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_BOOL
563 && TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_RANGE
)
565 (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_FLT
566 && TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_CHAR
567 && TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
568 && TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_BOOL
569 && TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_RANGE
))
570 error ("Argument to arithmetic operation not a number or boolean.");
572 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_FLT
574 TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_FLT
)
576 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
577 in target format. real.c in GCC probably has the necessary
580 v1
= value_as_double (arg1
);
581 v2
= value_as_double (arg2
);
601 error ("Integer-only operation on floating point number.");
604 val
= allocate_value (builtin_type_double
);
605 store_floating (VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (VALUE_TYPE (val
)),
608 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_BOOL
610 TYPE_CODE (VALUE_TYPE (arg2
)) == TYPE_CODE_BOOL
)
613 v1
= value_as_long (arg1
);
614 v2
= value_as_long (arg2
);
618 case BINOP_BITWISE_AND
:
622 case BINOP_BITWISE_IOR
:
626 case BINOP_BITWISE_XOR
:
631 error ("Invalid operation on booleans.");
634 val
= allocate_value (builtin_type_chill_bool
);
635 store_signed_integer (VALUE_CONTENTS_RAW (val
),
636 TYPE_LENGTH (VALUE_TYPE (val
)),
640 /* Integral operations here. */
641 /* FIXME: Also mixed integral/booleans, with result an integer. */
642 /* FIXME: This implements ANSI C rules (also correct for C++).
643 What about FORTRAN and chill? */
645 struct type
*type1
= VALUE_TYPE (arg1
);
646 struct type
*type2
= VALUE_TYPE (arg2
);
647 int promoted_len1
= TYPE_LENGTH (type1
);
648 int promoted_len2
= TYPE_LENGTH (type2
);
649 int is_unsigned1
= TYPE_UNSIGNED (type1
);
650 int is_unsigned2
= TYPE_UNSIGNED (type2
);
652 int unsigned_operation
;
654 /* Determine type length and signedness after promotion for
656 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
659 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
661 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
664 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
667 /* Determine type length of the result, and if the operation should
669 Use the signedness of the operand with the greater length.
670 If both operands are of equal length, use unsigned operation
671 if one of the operands is unsigned. */
672 if (promoted_len1
> promoted_len2
)
674 unsigned_operation
= is_unsigned1
;
675 result_len
= promoted_len1
;
677 else if (promoted_len2
> promoted_len1
)
679 unsigned_operation
= is_unsigned2
;
680 result_len
= promoted_len2
;
684 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
685 result_len
= promoted_len1
;
688 if (unsigned_operation
)
690 unsigned LONGEST v1
, v2
, v
;
691 v1
= (unsigned LONGEST
) value_as_long (arg1
);
692 v2
= (unsigned LONGEST
) value_as_long (arg2
);
694 /* Truncate values to the type length of the result. */
695 if (result_len
< sizeof (unsigned LONGEST
))
697 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
698 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
724 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
725 v1 mod 0 has a defined value, v1. */
726 /* Chill specifies that v2 must be > 0, so check for that. */
727 if (current_language
-> la_language
== language_chill
728 && value_as_long (arg2
) <= 0)
730 error ("Second operand of MOD must be greater than zero.");
739 /* Note floor(v1/v2) == v1/v2 for unsigned. */
752 case BINOP_BITWISE_AND
:
756 case BINOP_BITWISE_IOR
:
760 case BINOP_BITWISE_XOR
:
764 case BINOP_LOGICAL_AND
:
768 case BINOP_LOGICAL_OR
:
773 v
= v1
< v2
? v1
: v2
;
777 v
= v1
> v2
? v1
: v2
;
789 error ("Invalid binary operation on numbers.");
792 /* This is a kludge to get around the fact that we don't
793 know how to determine the result type from the types of
794 the operands. (I'm not really sure how much we feel the
795 need to duplicate the exact rules of the current
796 language. They can get really hairy. But not to do so
797 makes it hard to document just what we *do* do). */
799 /* Can't just call init_type because we wouldn't know what
800 name to give the type. */
802 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
803 ? builtin_type_unsigned_long_long
804 : builtin_type_unsigned_long
);
805 store_unsigned_integer (VALUE_CONTENTS_RAW (val
),
806 TYPE_LENGTH (VALUE_TYPE (val
)),
812 v1
= value_as_long (arg1
);
813 v2
= value_as_long (arg2
);
838 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
839 X mod 0 has a defined value, X. */
840 /* Chill specifies that v2 must be > 0, so check for that. */
841 if (current_language
-> la_language
== language_chill
844 error ("Second operand of MOD must be greater than zero.");
854 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
870 case BINOP_BITWISE_AND
:
874 case BINOP_BITWISE_IOR
:
878 case BINOP_BITWISE_XOR
:
882 case BINOP_LOGICAL_AND
:
886 case BINOP_LOGICAL_OR
:
891 v
= v1
< v2
? v1
: v2
;
895 v
= v1
> v2
? v1
: v2
;
907 error ("Invalid binary operation on numbers.");
910 /* This is a kludge to get around the fact that we don't
911 know how to determine the result type from the types of
912 the operands. (I'm not really sure how much we feel the
913 need to duplicate the exact rules of the current
914 language. They can get really hairy. But not to do so
915 makes it hard to document just what we *do* do). */
917 /* Can't just call init_type because we wouldn't know what
918 name to give the type. */
920 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
921 ? builtin_type_long_long
922 : builtin_type_long
);
923 store_signed_integer (VALUE_CONTENTS_RAW (val
),
924 TYPE_LENGTH (VALUE_TYPE (val
)),
932 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
935 value_logical_not (arg1
)
943 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_FLT
)
944 return 0 == value_as_double (arg1
);
946 len
= TYPE_LENGTH (VALUE_TYPE (arg1
));
947 p
= VALUE_CONTENTS (arg1
);
958 /* Simulate the C operator == by returning a 1
959 iff ARG1 and ARG2 have equal contents. */
962 value_equal (arg1
, arg2
)
963 register value_ptr arg1
, arg2
;
967 register char *p1
, *p2
;
968 enum type_code code1
;
969 enum type_code code2
;
974 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
975 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
977 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
978 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
980 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
981 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
982 return value_as_double (arg1
) == value_as_double (arg2
);
984 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
986 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_INT
)
987 return value_as_pointer (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
988 else if (code2
== TYPE_CODE_PTR
&& code1
== TYPE_CODE_INT
)
989 return (CORE_ADDR
) value_as_long (arg1
) == value_as_pointer (arg2
);
991 else if (code1
== code2
992 && ((len
= TYPE_LENGTH (VALUE_TYPE (arg1
)))
993 == TYPE_LENGTH (VALUE_TYPE (arg2
))))
995 p1
= VALUE_CONTENTS (arg1
);
996 p2
= VALUE_CONTENTS (arg2
);
1006 error ("Invalid type combination in equality test.");
1007 return 0; /* For lint -- never reached */
1011 /* Simulate the C operator < by returning 1
1012 iff ARG1's contents are less than ARG2's. */
1015 value_less (arg1
, arg2
)
1016 register value_ptr arg1
, arg2
;
1018 register enum type_code code1
;
1019 register enum type_code code2
;
1021 COERCE_ARRAY (arg1
);
1022 COERCE_ARRAY (arg2
);
1024 code1
= TYPE_CODE (VALUE_TYPE (arg1
));
1025 code2
= TYPE_CODE (VALUE_TYPE (arg2
));
1027 if (code1
== TYPE_CODE_INT
&& code2
== TYPE_CODE_INT
)
1028 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1030 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
)
1031 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
))
1032 return value_as_double (arg1
) < value_as_double (arg2
);
1033 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1034 return value_as_pointer (arg1
) < value_as_pointer (arg2
);
1036 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1038 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_INT
)
1039 return value_as_pointer (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1040 else if (code2
== TYPE_CODE_PTR
&& code1
== TYPE_CODE_INT
)
1041 return (CORE_ADDR
) value_as_long (arg1
) < value_as_pointer (arg2
);
1045 error ("Invalid type combination in ordering comparison.");
1050 /* The unary operators - and ~. Both free the argument ARG1. */
1054 register value_ptr arg1
;
1056 register struct type
*type
;
1060 type
= VALUE_TYPE (arg1
);
1062 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1063 return value_from_double (type
, - value_as_double (arg1
));
1064 else if (TYPE_CODE (type
) == TYPE_CODE_INT
)
1065 return value_from_longest (type
, - value_as_long (arg1
));
1067 error ("Argument to negate operation not a number.");
1068 return 0; /* For lint -- never reached */
1073 value_complement (arg1
)
1074 register value_ptr arg1
;
1078 if (TYPE_CODE (VALUE_TYPE (arg1
)) != TYPE_CODE_INT
)
1079 error ("Argument to complement operation not an integer.");
1081 return value_from_longest (VALUE_TYPE (arg1
), ~ value_as_long (arg1
));
1084 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1085 and whose VALUE_CONTENTS is valaddr.
1086 Return -1 if out of range, -2 other error. */
1089 value_bit_index (type
, valaddr
, index
)
1095 int low_bound
, high_bound
;
1098 range
= TYPE_FIELD_TYPE (type
, 0);
1099 if (TYPE_CODE (range
) != TYPE_CODE_RANGE
)
1101 low_bound
= TYPE_LOW_BOUND (range
);
1102 high_bound
= TYPE_HIGH_BOUND (range
);
1103 if (index
< low_bound
|| index
> high_bound
)
1105 rel_index
= index
- low_bound
;
1106 word
= unpack_long (builtin_type_unsigned_char
,
1107 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1108 rel_index
%= TARGET_CHAR_BIT
;
1109 if (BITS_BIG_ENDIAN
)
1110 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1111 return (word
>> rel_index
) & 1;
1115 value_in (element
, set
)
1116 value_ptr element
, set
;
1119 if (TYPE_CODE (VALUE_TYPE (set
)) != TYPE_CODE_SET
)
1120 error ("Second argument of 'IN' has wrong type");
1121 if (TYPE_CODE (VALUE_TYPE (element
)) != TYPE_CODE_INT
1122 && TYPE_CODE (VALUE_TYPE (element
)) != TYPE_CODE_CHAR
1123 && TYPE_CODE (VALUE_TYPE (element
)) != TYPE_CODE_ENUM
1124 && TYPE_CODE (VALUE_TYPE (element
)) != TYPE_CODE_BOOL
)
1125 error ("First argument of 'IN' has wrong type");
1126 member
= value_bit_index (VALUE_TYPE (set
), VALUE_CONTENTS (set
),
1127 value_as_long (element
));
1129 error ("First argument of 'IN' not in range");
1130 return value_from_longest (builtin_type_int
, member
);
1134 _initialize_valarith ()