1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright 1986, 89, 91, 92, 93, 94, 95, 96, 97, 1998
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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));
40 void _initialize_valarith
PARAMS ((void));
44 value_add (arg1
, arg2
)
47 register value_ptr valint
, valptr
;
49 struct type
*type1
, *type2
, *valptrtype
;
53 type1
= check_typedef (VALUE_TYPE (arg1
));
54 type2
= check_typedef (VALUE_TYPE (arg2
));
56 if ((TYPE_CODE (type1
) == TYPE_CODE_PTR
57 || TYPE_CODE (type2
) == TYPE_CODE_PTR
)
59 (TYPE_CODE (type1
) == TYPE_CODE_INT
60 || TYPE_CODE (type2
) == TYPE_CODE_INT
))
61 /* Exactly one argument is a pointer, and one is an integer. */
65 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
77 len
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype
)));
79 len
= 1; /* For (void *) */
80 retval
= value_from_longest (valptrtype
,
81 value_as_long (valptr
)
82 + (len
* value_as_long (valint
)));
83 VALUE_BFD_SECTION (retval
) = VALUE_BFD_SECTION (valptr
);
87 return value_binop (arg1
, arg2
, BINOP_ADD
);
91 value_sub (arg1
, arg2
)
94 struct type
*type1
, *type2
;
97 type1
= check_typedef (VALUE_TYPE (arg1
));
98 type2
= check_typedef (VALUE_TYPE (arg2
));
100 if (TYPE_CODE (type1
) == TYPE_CODE_PTR
)
102 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
104 /* pointer - integer. */
105 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
106 return value_from_longest
108 value_as_long (arg1
) - (sz
* value_as_long (arg2
)));
110 else if (TYPE_CODE (type2
) == TYPE_CODE_PTR
111 && TYPE_LENGTH (TYPE_TARGET_TYPE (type1
))
112 == TYPE_LENGTH (TYPE_TARGET_TYPE (type2
)))
114 /* pointer to <type x> - pointer to <type x>. */
115 LONGEST sz
= TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1
)));
116 return value_from_longest
117 (builtin_type_long
, /* FIXME -- should be ptrdiff_t */
118 (value_as_long (arg1
) - value_as_long (arg2
)) / sz
);
123 First argument of `-' is a pointer and second argument is neither\n\
124 an integer nor a pointer of the same type.");
128 return value_binop (arg1
, arg2
, BINOP_SUB
);
131 /* Return the value of ARRAY[IDX].
132 See comments in value_coerce_array() for rationale for reason for
133 doing lower bounds adjustment here rather than there.
134 FIXME: Perhaps we should validate that the index is valid and if
135 verbosity is set, warn about invalid indices (but still use them). */
138 value_subscript (array
, idx
)
139 value_ptr array
, idx
;
142 int c_style
= current_language
->c_style_arrays
;
146 tarray
= check_typedef (VALUE_TYPE (array
));
147 COERCE_VARYING_ARRAY (array
, tarray
);
149 if (TYPE_CODE (tarray
) == TYPE_CODE_ARRAY
150 || TYPE_CODE (tarray
) == TYPE_CODE_STRING
)
152 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
153 LONGEST lowerbound
, upperbound
;
154 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
156 if (VALUE_LVAL (array
) != lval_memory
)
157 return value_subscripted_rvalue (array
, idx
, lowerbound
);
161 LONGEST index
= value_as_long (idx
);
162 if (index
>= lowerbound
&& index
<= upperbound
)
163 return value_subscripted_rvalue (array
, idx
, lowerbound
);
164 warning ("array or string index out of range");
165 /* fall doing C stuff */
171 bound
= value_from_longest (builtin_type_int
, (LONGEST
) lowerbound
);
172 idx
= value_sub (idx
, bound
);
175 array
= value_coerce_array (array
);
178 if (TYPE_CODE (tarray
) == TYPE_CODE_BITSTRING
)
180 struct type
*range_type
= TYPE_INDEX_TYPE (tarray
);
181 LONGEST index
= value_as_long (idx
);
183 int offset
, byte
, bit_index
;
184 LONGEST lowerbound
, upperbound
;
185 get_discrete_bounds (range_type
, &lowerbound
, &upperbound
);
186 if (index
< lowerbound
|| index
> upperbound
)
187 error ("bitstring index out of range");
189 offset
= index
/ TARGET_CHAR_BIT
;
190 byte
= *((char*)VALUE_CONTENTS (array
) + offset
);
191 bit_index
= index
% TARGET_CHAR_BIT
;
192 byte
>>= (BITS_BIG_ENDIAN
? TARGET_CHAR_BIT
- 1 - bit_index
: bit_index
);
193 v
= value_from_longest (LA_BOOL_TYPE
, byte
& 1);
194 VALUE_BITPOS (v
) = bit_index
;
195 VALUE_BITSIZE (v
) = 1;
196 VALUE_LVAL (v
) = VALUE_LVAL (array
);
197 if (VALUE_LVAL (array
) == lval_internalvar
)
198 VALUE_LVAL (v
) = lval_internalvar_component
;
199 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
200 VALUE_OFFSET (v
) = offset
+ VALUE_OFFSET (array
);
205 return value_ind (value_add (array
, idx
));
207 error ("not an array or string");
210 /* Return the value of EXPR[IDX], expr an aggregate rvalue
211 (eg, a vector register). This routine used to promote floats
212 to doubles, but no longer does. */
215 value_subscripted_rvalue (array
, idx
, lowerbound
)
216 value_ptr array
, idx
;
219 struct type
*array_type
= check_typedef (VALUE_TYPE (array
));
220 struct type
*elt_type
= check_typedef (TYPE_TARGET_TYPE (array_type
));
221 unsigned int elt_size
= TYPE_LENGTH (elt_type
);
222 LONGEST index
= value_as_long (idx
);
223 unsigned int elt_offs
= elt_size
* longest_to_int (index
- lowerbound
);
226 if (index
< lowerbound
|| elt_offs
>= TYPE_LENGTH (array_type
))
227 error ("no such vector element");
229 v
= allocate_value (elt_type
);
230 if (VALUE_LAZY (array
))
233 memcpy (VALUE_CONTENTS (v
), VALUE_CONTENTS (array
) + elt_offs
, elt_size
);
235 if (VALUE_LVAL (array
) == lval_internalvar
)
236 VALUE_LVAL (v
) = lval_internalvar_component
;
238 VALUE_LVAL (v
) = VALUE_LVAL (array
);
239 VALUE_ADDRESS (v
) = VALUE_ADDRESS (array
);
240 VALUE_OFFSET (v
) = VALUE_OFFSET (array
) + elt_offs
;
244 /* Check to see if either argument is a structure. This is called so
245 we know whether to go ahead with the normal binop or look for a
246 user defined function instead.
248 For now, we do not overload the `=' operator. */
251 binop_user_defined_p (op
, arg1
, arg2
)
253 value_ptr arg1
, arg2
;
255 struct type
*type1
, *type2
;
256 if (op
== BINOP_ASSIGN
|| op
== BINOP_CONCAT
)
258 type1
= check_typedef (VALUE_TYPE (arg1
));
259 type2
= check_typedef (VALUE_TYPE (arg2
));
260 return (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
261 || TYPE_CODE (type2
) == TYPE_CODE_STRUCT
262 || (TYPE_CODE (type1
) == TYPE_CODE_REF
263 && TYPE_CODE (TYPE_TARGET_TYPE (type1
)) == TYPE_CODE_STRUCT
)
264 || (TYPE_CODE (type2
) == TYPE_CODE_REF
265 && TYPE_CODE (TYPE_TARGET_TYPE (type2
)) == TYPE_CODE_STRUCT
));
268 /* Check to see if argument is a structure. This is called so
269 we know whether to go ahead with the normal unop or look for a
270 user defined function instead.
272 For now, we do not overload the `&' operator. */
274 int unop_user_defined_p (op
, arg1
)
281 type1
= check_typedef (VALUE_TYPE (arg1
));
284 if (TYPE_CODE (type1
) == TYPE_CODE_STRUCT
)
286 else if (TYPE_CODE (type1
) == TYPE_CODE_REF
)
287 type1
= TYPE_TARGET_TYPE (type1
);
293 /* We know either arg1 or arg2 is a structure, so try to find the right
294 user defined function. Create an argument vector that calls
295 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
296 binary operator which is legal for GNU C++).
298 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
299 is the opcode saying how to modify it. Otherwise, OTHEROP is
303 value_x_binop (arg1
, arg2
, op
, otherop
, noside
)
304 value_ptr arg1
, arg2
;
305 enum exp_opcode op
, otherop
;
318 /* now we know that what we have to do is construct our
319 arg vector and find the right function to call it with. */
321 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
322 error ("Can't do that binary op on that type"); /* FIXME be explicit */
324 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * 4);
325 argvec
[1] = value_addr (arg1
);
329 /* make the right function name up */
330 strcpy(tstr
, "operator__");
334 case BINOP_ADD
: strcpy(ptr
,"+"); break;
335 case BINOP_SUB
: strcpy(ptr
,"-"); break;
336 case BINOP_MUL
: strcpy(ptr
,"*"); break;
337 case BINOP_DIV
: strcpy(ptr
,"/"); break;
338 case BINOP_REM
: strcpy(ptr
,"%"); break;
339 case BINOP_LSH
: strcpy(ptr
,"<<"); break;
340 case BINOP_RSH
: strcpy(ptr
,">>"); break;
341 case BINOP_BITWISE_AND
: strcpy(ptr
,"&"); break;
342 case BINOP_BITWISE_IOR
: strcpy(ptr
,"|"); break;
343 case BINOP_BITWISE_XOR
: strcpy(ptr
,"^"); break;
344 case BINOP_LOGICAL_AND
: strcpy(ptr
,"&&"); break;
345 case BINOP_LOGICAL_OR
: strcpy(ptr
,"||"); break;
346 case BINOP_MIN
: strcpy(ptr
,"<?"); break;
347 case BINOP_MAX
: strcpy(ptr
,">?"); break;
348 case BINOP_ASSIGN
: strcpy(ptr
,"="); break;
349 case BINOP_ASSIGN_MODIFY
:
352 case BINOP_ADD
: strcpy(ptr
,"+="); break;
353 case BINOP_SUB
: strcpy(ptr
,"-="); break;
354 case BINOP_MUL
: strcpy(ptr
,"*="); break;
355 case BINOP_DIV
: strcpy(ptr
,"/="); break;
356 case BINOP_REM
: strcpy(ptr
,"%="); break;
357 case BINOP_BITWISE_AND
: strcpy(ptr
,"&="); break;
358 case BINOP_BITWISE_IOR
: strcpy(ptr
,"|="); break;
359 case BINOP_BITWISE_XOR
: strcpy(ptr
,"^="); break;
360 case BINOP_MOD
: /* invalid */
362 error ("Invalid binary operation specified.");
365 case BINOP_SUBSCRIPT
: strcpy(ptr
,"[]"); break;
366 case BINOP_EQUAL
: strcpy(ptr
,"=="); break;
367 case BINOP_NOTEQUAL
: strcpy(ptr
,"!="); break;
368 case BINOP_LESS
: strcpy(ptr
,"<"); break;
369 case BINOP_GTR
: strcpy(ptr
,">"); break;
370 case BINOP_GEQ
: strcpy(ptr
,">="); break;
371 case BINOP_LEQ
: strcpy(ptr
,"<="); break;
372 case BINOP_MOD
: /* invalid */
374 error ("Invalid binary operation specified.");
377 argvec
[0] = value_struct_elt (&arg1
, argvec
+1, tstr
, &static_memfuncp
, "structure");
383 argvec
[1] = argvec
[0];
386 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
388 struct type
*return_type
;
390 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
391 return value_zero (return_type
, VALUE_LVAL (arg1
));
393 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
395 error ("member function %s not found", tstr
);
397 return call_function_by_hand (argvec
[0], 2 - static_memfuncp
, argvec
+ 1);
401 /* We know that arg1 is a structure, so try to find a unary user
402 defined operator that matches the operator in question.
403 Create an argument vector that calls arg1.operator @ (arg1)
404 and return that value (where '@' is (almost) any unary operator which
405 is legal for GNU C++). */
408 value_x_unop (arg1
, op
, noside
)
414 char *ptr
, *mangle_ptr
;
415 char tstr
[13], mangle_tstr
[13];
421 /* now we know that what we have to do is construct our
422 arg vector and find the right function to call it with. */
424 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1
))) != TYPE_CODE_STRUCT
)
425 error ("Can't do that unary op on that type"); /* FIXME be explicit */
427 argvec
= (value_ptr
*) alloca (sizeof (value_ptr
) * 3);
428 argvec
[1] = value_addr (arg1
);
431 /* make the right function name up */
432 strcpy(tstr
,"operator__");
434 strcpy(mangle_tstr
, "__");
435 mangle_ptr
= mangle_tstr
+2;
438 case UNOP_PREINCREMENT
: strcpy(ptr
,"++"); break;
439 case UNOP_PREDECREMENT
: strcpy(ptr
,"++"); break;
440 case UNOP_POSTINCREMENT
: strcpy(ptr
,"++"); break;
441 case UNOP_POSTDECREMENT
: strcpy(ptr
,"++"); break;
442 case UNOP_LOGICAL_NOT
: strcpy(ptr
,"!"); break;
443 case UNOP_COMPLEMENT
: strcpy(ptr
,"~"); break;
444 case UNOP_NEG
: strcpy(ptr
,"-"); break;
445 case UNOP_IND
: strcpy(ptr
,"*"); break;
447 error ("Invalid unary operation specified.");
450 argvec
[0] = value_struct_elt (&arg1
, argvec
+1, tstr
, &static_memfuncp
, "structure");
456 argvec
[1] = argvec
[0];
459 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
461 struct type
*return_type
;
463 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec
[0])));
464 return value_zero (return_type
, VALUE_LVAL (arg1
));
466 return call_function_by_hand (argvec
[0], 1 - static_memfuncp
, argvec
+ 1);
468 error ("member function %s not found", tstr
);
469 return 0; /* For lint -- never reached */
473 /* Concatenate two values with the following conditions:
475 (1) Both values must be either bitstring values or character string
476 values and the resulting value consists of the concatenation of
477 ARG1 followed by ARG2.
481 One value must be an integer value and the other value must be
482 either a bitstring value or character string value, which is
483 to be repeated by the number of times specified by the integer
487 (2) Boolean values are also allowed and are treated as bit string
490 (3) Character values are also allowed and are treated as character
491 string values of length 1.
495 value_concat (arg1
, arg2
)
496 value_ptr arg1
, arg2
;
498 register value_ptr inval1
, inval2
, outval
;
499 int inval1len
, inval2len
;
503 struct type
*type1
= check_typedef (VALUE_TYPE (arg1
));
504 struct type
*type2
= check_typedef (VALUE_TYPE (arg2
));
506 COERCE_VARYING_ARRAY (arg1
, type1
);
507 COERCE_VARYING_ARRAY (arg2
, type2
);
509 /* First figure out if we are dealing with two values to be concatenated
510 or a repeat count and a value to be repeated. INVAL1 is set to the
511 first of two concatenated values, or the repeat count. INVAL2 is set
512 to the second of the two concatenated values or the value to be
515 if (TYPE_CODE (type2
) == TYPE_CODE_INT
)
517 struct type
*tmp
= type1
;
529 /* Now process the input values. */
531 if (TYPE_CODE (type1
) == TYPE_CODE_INT
)
533 /* We have a repeat count. Validate the second value and then
534 construct a value repeated that many times. */
535 if (TYPE_CODE (type2
) == TYPE_CODE_STRING
536 || TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
538 count
= longest_to_int (value_as_long (inval1
));
539 inval2len
= TYPE_LENGTH (type2
);
540 ptr
= (char *) alloca (count
* inval2len
);
541 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
543 inchar
= (char) unpack_long (type2
,
544 VALUE_CONTENTS (inval2
));
545 for (idx
= 0; idx
< count
; idx
++)
547 *(ptr
+ idx
) = inchar
;
552 for (idx
= 0; idx
< count
; idx
++)
554 memcpy (ptr
+ (idx
* inval2len
), VALUE_CONTENTS (inval2
),
558 outval
= value_string (ptr
, count
* inval2len
);
560 else if (TYPE_CODE (type2
) == TYPE_CODE_BITSTRING
561 || TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
563 error ("unimplemented support for bitstring/boolean repeats");
567 error ("can't repeat values of that type");
570 else if (TYPE_CODE (type1
) == TYPE_CODE_STRING
571 || TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
573 /* We have two character strings to concatenate. */
574 if (TYPE_CODE (type2
) != TYPE_CODE_STRING
575 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
)
577 error ("Strings can only be concatenated with other strings.");
579 inval1len
= TYPE_LENGTH (type1
);
580 inval2len
= TYPE_LENGTH (type2
);
581 ptr
= (char *) alloca (inval1len
+ inval2len
);
582 if (TYPE_CODE (type1
) == TYPE_CODE_CHAR
)
584 *ptr
= (char) unpack_long (type1
, VALUE_CONTENTS (inval1
));
588 memcpy (ptr
, VALUE_CONTENTS (inval1
), inval1len
);
590 if (TYPE_CODE (type2
) == TYPE_CODE_CHAR
)
593 (char) unpack_long (type2
, VALUE_CONTENTS (inval2
));
597 memcpy (ptr
+ inval1len
, VALUE_CONTENTS (inval2
), inval2len
);
599 outval
= value_string (ptr
, inval1len
+ inval2len
);
601 else if (TYPE_CODE (type1
) == TYPE_CODE_BITSTRING
602 || TYPE_CODE (type1
) == TYPE_CODE_BOOL
)
604 /* We have two bitstrings to concatenate. */
605 if (TYPE_CODE (type2
) != TYPE_CODE_BITSTRING
606 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
)
608 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
610 error ("unimplemented support for bitstring/boolean concatenation.");
614 /* We don't know how to concatenate these operands. */
615 error ("illegal operands for concatenation.");
622 /* Perform a binary operation on two operands which have reasonable
623 representations as integers or floats. This includes booleans,
624 characters, integers, or floats.
625 Does not support addition and subtraction on pointers;
626 use value_add or value_sub if you want to handle those possibilities. */
629 value_binop (arg1
, arg2
, op
)
630 value_ptr arg1
, arg2
;
633 register value_ptr val
;
634 struct type
*type1
, *type2
;
640 type1
= check_typedef (VALUE_TYPE (arg1
));
641 type2
= check_typedef (VALUE_TYPE (arg2
));
643 if ((TYPE_CODE (type1
) != TYPE_CODE_FLT
644 && TYPE_CODE (type1
) != TYPE_CODE_CHAR
645 && TYPE_CODE (type1
) != TYPE_CODE_INT
646 && TYPE_CODE (type1
) != TYPE_CODE_BOOL
647 && TYPE_CODE (type1
) != TYPE_CODE_RANGE
)
649 (TYPE_CODE (type2
) != TYPE_CODE_FLT
650 && TYPE_CODE (type2
) != TYPE_CODE_CHAR
651 && TYPE_CODE (type2
) != TYPE_CODE_INT
652 && TYPE_CODE (type2
) != TYPE_CODE_BOOL
653 && TYPE_CODE (type2
) != TYPE_CODE_RANGE
))
654 error ("Argument to arithmetic operation not a number or boolean.");
656 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
658 TYPE_CODE (type2
) == TYPE_CODE_FLT
)
660 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
661 in target format. real.c in GCC probably has the necessary
664 v1
= value_as_double (arg1
);
665 v2
= value_as_double (arg2
);
685 error ("Integer-only operation on floating point number.");
688 /* If either arg was long double, make sure that value is also long
691 if (TYPE_LENGTH(type1
) * 8 > TARGET_DOUBLE_BIT
692 || TYPE_LENGTH(type2
) * 8 > TARGET_DOUBLE_BIT
)
693 val
= allocate_value (builtin_type_long_double
);
695 val
= allocate_value (builtin_type_double
);
697 store_floating (VALUE_CONTENTS_RAW (val
), TYPE_LENGTH (VALUE_TYPE (val
)),
700 else if (TYPE_CODE (type1
) == TYPE_CODE_BOOL
702 TYPE_CODE (type2
) == TYPE_CODE_BOOL
)
705 v1
= value_as_long (arg1
);
706 v2
= value_as_long (arg2
);
710 case BINOP_BITWISE_AND
:
714 case BINOP_BITWISE_IOR
:
718 case BINOP_BITWISE_XOR
:
723 error ("Invalid operation on booleans.");
726 val
= allocate_value (type1
);
727 store_signed_integer (VALUE_CONTENTS_RAW (val
),
732 /* Integral operations here. */
733 /* FIXME: Also mixed integral/booleans, with result an integer. */
734 /* FIXME: This implements ANSI C rules (also correct for C++).
735 What about FORTRAN and chill? */
737 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
738 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
739 int is_unsigned1
= TYPE_UNSIGNED (type1
);
740 int is_unsigned2
= TYPE_UNSIGNED (type2
);
741 unsigned int result_len
;
742 int unsigned_operation
;
744 /* Determine type length and signedness after promotion for
746 if (promoted_len1
< TYPE_LENGTH (builtin_type_int
))
749 promoted_len1
= TYPE_LENGTH (builtin_type_int
);
751 if (promoted_len2
< TYPE_LENGTH (builtin_type_int
))
754 promoted_len2
= TYPE_LENGTH (builtin_type_int
);
757 /* Determine type length of the result, and if the operation should
759 Use the signedness of the operand with the greater length.
760 If both operands are of equal length, use unsigned operation
761 if one of the operands is unsigned. */
762 if (promoted_len1
> promoted_len2
)
764 unsigned_operation
= is_unsigned1
;
765 result_len
= promoted_len1
;
767 else if (promoted_len2
> promoted_len1
)
769 unsigned_operation
= is_unsigned2
;
770 result_len
= promoted_len2
;
774 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
775 result_len
= promoted_len1
;
778 if (unsigned_operation
)
781 v1
= (ULONGEST
) value_as_long (arg1
);
782 v2
= (ULONGEST
) value_as_long (arg2
);
784 /* Truncate values to the type length of the result. */
785 if (result_len
< sizeof (ULONGEST
))
787 v1
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
788 v2
&= ((LONGEST
) 1 << HOST_CHAR_BIT
* result_len
) - 1;
814 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
815 v1 mod 0 has a defined value, v1. */
816 /* Chill specifies that v2 must be > 0, so check for that. */
817 if (current_language
-> la_language
== language_chill
818 && value_as_long (arg2
) <= 0)
820 error ("Second operand of MOD must be greater than zero.");
829 /* Note floor(v1/v2) == v1/v2 for unsigned. */
842 case BINOP_BITWISE_AND
:
846 case BINOP_BITWISE_IOR
:
850 case BINOP_BITWISE_XOR
:
854 case BINOP_LOGICAL_AND
:
858 case BINOP_LOGICAL_OR
:
863 v
= v1
< v2
? v1
: v2
;
867 v
= v1
> v2
? v1
: v2
;
879 error ("Invalid binary operation on numbers.");
882 /* This is a kludge to get around the fact that we don't
883 know how to determine the result type from the types of
884 the operands. (I'm not really sure how much we feel the
885 need to duplicate the exact rules of the current
886 language. They can get really hairy. But not to do so
887 makes it hard to document just what we *do* do). */
889 /* Can't just call init_type because we wouldn't know what
890 name to give the type. */
892 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
893 ? builtin_type_unsigned_long_long
894 : builtin_type_unsigned_long
);
895 store_unsigned_integer (VALUE_CONTENTS_RAW (val
),
896 TYPE_LENGTH (VALUE_TYPE (val
)),
902 v1
= value_as_long (arg1
);
903 v2
= value_as_long (arg2
);
928 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
929 X mod 0 has a defined value, X. */
930 /* Chill specifies that v2 must be > 0, so check for that. */
931 if (current_language
-> la_language
== language_chill
934 error ("Second operand of MOD must be greater than zero.");
944 if (TRUNCATION_TOWARDS_ZERO
&& (v
< 0) && ((v1
% v2
) != 0))
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
;
997 error ("Invalid binary operation on numbers.");
1000 /* This is a kludge to get around the fact that we don't
1001 know how to determine the result type from the types of
1002 the operands. (I'm not really sure how much we feel the
1003 need to duplicate the exact rules of the current
1004 language. They can get really hairy. But not to do so
1005 makes it hard to document just what we *do* do). */
1007 /* Can't just call init_type because we wouldn't know what
1008 name to give the type. */
1009 val
= allocate_value
1010 (result_len
> TARGET_LONG_BIT
/ HOST_CHAR_BIT
1011 ? builtin_type_long_long
1012 : builtin_type_long
);
1013 store_signed_integer (VALUE_CONTENTS_RAW (val
),
1014 TYPE_LENGTH (VALUE_TYPE (val
)),
1022 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1025 value_logical_not (arg1
)
1032 COERCE_NUMBER (arg1
);
1033 type1
= check_typedef (VALUE_TYPE (arg1
));
1035 if (TYPE_CODE (type1
) == TYPE_CODE_FLT
)
1036 return 0 == value_as_double (arg1
);
1038 len
= TYPE_LENGTH (type1
);
1039 p
= VALUE_CONTENTS (arg1
);
1050 /* Simulate the C operator == by returning a 1
1051 iff ARG1 and ARG2 have equal contents. */
1054 value_equal (arg1
, arg2
)
1055 register value_ptr arg1
, arg2
;
1059 register char *p1
, *p2
;
1060 struct type
*type1
, *type2
;
1061 enum type_code code1
;
1062 enum type_code code2
;
1064 COERCE_NUMBER (arg1
);
1065 COERCE_NUMBER (arg2
);
1067 type1
= check_typedef (VALUE_TYPE (arg1
));
1068 type2
= check_typedef (VALUE_TYPE (arg2
));
1069 code1
= TYPE_CODE (type1
);
1070 code2
= TYPE_CODE (type2
);
1072 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1073 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1074 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1076 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1077 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1078 return value_as_double (arg1
) == value_as_double (arg2
);
1080 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1082 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1083 return value_as_pointer (arg1
) == (CORE_ADDR
) value_as_long (arg2
);
1084 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1085 return (CORE_ADDR
) value_as_long (arg1
) == value_as_pointer (arg2
);
1087 else if (code1
== code2
1088 && ((len
= (int) TYPE_LENGTH (type1
))
1089 == (int) TYPE_LENGTH (type2
)))
1091 p1
= VALUE_CONTENTS (arg1
);
1092 p2
= VALUE_CONTENTS (arg2
);
1102 error ("Invalid type combination in equality test.");
1103 return 0; /* For lint -- never reached */
1107 /* Simulate the C operator < by returning 1
1108 iff ARG1's contents are less than ARG2's. */
1111 value_less (arg1
, arg2
)
1112 register value_ptr arg1
, arg2
;
1114 register enum type_code code1
;
1115 register enum type_code code2
;
1116 struct type
*type1
, *type2
;
1118 COERCE_NUMBER (arg1
);
1119 COERCE_NUMBER (arg2
);
1121 type1
= check_typedef (VALUE_TYPE (arg1
));
1122 type2
= check_typedef (VALUE_TYPE (arg2
));
1123 code1
= TYPE_CODE (type1
);
1124 code2
= TYPE_CODE (type2
);
1126 if ((code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
) &&
1127 (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1128 return longest_to_int (value_as_long (value_binop (arg1
, arg2
,
1130 else if ((code1
== TYPE_CODE_FLT
|| code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
)
1131 && (code2
== TYPE_CODE_FLT
|| code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1132 return value_as_double (arg1
) < value_as_double (arg2
);
1133 else if (code1
== TYPE_CODE_PTR
&& code2
== TYPE_CODE_PTR
)
1134 return value_as_pointer (arg1
) < value_as_pointer (arg2
);
1136 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1138 else if (code1
== TYPE_CODE_PTR
&& (code2
== TYPE_CODE_INT
|| code2
== TYPE_CODE_BOOL
))
1139 return value_as_pointer (arg1
) < (CORE_ADDR
) value_as_long (arg2
);
1140 else if (code2
== TYPE_CODE_PTR
&& (code1
== TYPE_CODE_INT
|| code1
== TYPE_CODE_BOOL
))
1141 return (CORE_ADDR
) value_as_long (arg1
) < value_as_pointer (arg2
);
1145 error ("Invalid type combination in ordering comparison.");
1150 /* The unary operators - and ~. Both free the argument ARG1. */
1154 register value_ptr arg1
;
1156 register struct type
*type
;
1157 register struct type
*result_type
= VALUE_TYPE (arg1
);
1162 type
= check_typedef (VALUE_TYPE (arg1
));
1164 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1165 return value_from_double (result_type
, - value_as_double (arg1
));
1166 else if (TYPE_CODE (type
) == TYPE_CODE_INT
|| TYPE_CODE (type
) == TYPE_CODE_BOOL
)
1168 /* Perform integral promotion for ANSI C/C++.
1169 FIXME: What about FORTRAN and chill ? */
1170 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1171 result_type
= builtin_type_int
;
1173 return value_from_longest (result_type
, - value_as_long (arg1
));
1176 error ("Argument to negate operation not a number.");
1177 return 0; /* For lint -- never reached */
1182 value_complement (arg1
)
1183 register value_ptr arg1
;
1185 register struct type
*type
;
1186 register struct type
*result_type
= VALUE_TYPE (arg1
);
1192 type
= check_typedef (VALUE_TYPE (arg1
));
1194 typecode
= TYPE_CODE (type
);
1195 if ((typecode
!= TYPE_CODE_INT
) && (typecode
!= TYPE_CODE_BOOL
))
1196 error ("Argument to complement operation not an integer or boolean.");
1198 /* Perform integral promotion for ANSI C/C++.
1199 FIXME: What about FORTRAN ? */
1200 if (TYPE_LENGTH (type
) < TYPE_LENGTH (builtin_type_int
))
1201 result_type
= builtin_type_int
;
1203 return value_from_longest (result_type
, ~ value_as_long (arg1
));
1206 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1207 and whose VALUE_CONTENTS is valaddr.
1208 Return -1 if out of range, -2 other error. */
1211 value_bit_index (type
, valaddr
, index
)
1216 LONGEST low_bound
, high_bound
;
1219 struct type
*range
= TYPE_FIELD_TYPE (type
, 0);
1220 if (get_discrete_bounds (range
, &low_bound
, &high_bound
) < 0)
1222 if (index
< low_bound
|| index
> high_bound
)
1224 rel_index
= index
- low_bound
;
1225 word
= unpack_long (builtin_type_unsigned_char
,
1226 valaddr
+ (rel_index
/ TARGET_CHAR_BIT
));
1227 rel_index
%= TARGET_CHAR_BIT
;
1228 if (BITS_BIG_ENDIAN
)
1229 rel_index
= TARGET_CHAR_BIT
- 1 - rel_index
;
1230 return (word
>> rel_index
) & 1;
1234 value_in (element
, set
)
1235 value_ptr element
, set
;
1238 struct type
*settype
= check_typedef (VALUE_TYPE (set
));
1239 struct type
*eltype
= check_typedef (VALUE_TYPE (element
));
1240 if (TYPE_CODE (eltype
) == TYPE_CODE_RANGE
)
1241 eltype
= TYPE_TARGET_TYPE (eltype
);
1242 if (TYPE_CODE (settype
) != TYPE_CODE_SET
)
1243 error ("Second argument of 'IN' has wrong type");
1244 if (TYPE_CODE (eltype
) != TYPE_CODE_INT
1245 && TYPE_CODE (eltype
) != TYPE_CODE_CHAR
1246 && TYPE_CODE (eltype
) != TYPE_CODE_ENUM
1247 && TYPE_CODE (eltype
) != TYPE_CODE_BOOL
)
1248 error ("First argument of 'IN' has wrong type");
1249 member
= value_bit_index (settype
, VALUE_CONTENTS (set
),
1250 value_as_long (element
));
1252 error ("First argument of 'IN' not in range");
1253 return value_from_longest (LA_BOOL_TYPE
, member
);
1257 _initialize_valarith ()