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., 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));
42 value_add (arg1, arg2)
45 register value_ptr valint, valptr;
47 struct type *type1, *type2, *valptrtype;
51 type1 = check_typedef (VALUE_TYPE (arg1));
52 type2 = check_typedef (VALUE_TYPE (arg2));
54 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
55 || TYPE_CODE (type2) == TYPE_CODE_PTR)
57 (TYPE_CODE (type1) == TYPE_CODE_INT
58 || TYPE_CODE (type2) == TYPE_CODE_INT))
59 /* Exactly one argument is a pointer, and one is an integer. */
61 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
73 len = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype)));
74 if (len == 0) len = 1; /* For (void *) */
75 return value_from_longest (valptrtype,
76 value_as_long (valptr)
77 + (len * value_as_long (valint)));
80 return value_binop (arg1, arg2, BINOP_ADD);
84 value_sub (arg1, arg2)
87 struct type *type1, *type2;
90 type1 = check_typedef (VALUE_TYPE (arg1));
91 type2 = check_typedef (VALUE_TYPE (arg2));
93 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
95 if (TYPE_CODE (type2) == TYPE_CODE_INT)
97 /* pointer - integer. */
98 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
99 return value_from_longest
101 value_as_long (arg1) - (sz * value_as_long (arg2)));
103 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
104 && TYPE_LENGTH (TYPE_TARGET_TYPE (type1))
105 == TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
107 /* pointer to <type x> - pointer to <type x>. */
108 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
109 return value_from_longest
110 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
111 (value_as_long (arg1) - value_as_long (arg2)) / sz);
116 First argument of `-' is a pointer and second argument is neither\n\
117 an integer nor a pointer of the same type.");
121 return value_binop (arg1, arg2, BINOP_SUB);
124 /* Return the value of ARRAY[IDX].
125 See comments in value_coerce_array() for rationale for reason for
126 doing lower bounds adjustment here rather than there.
127 FIXME: Perhaps we should validate that the index is valid and if
128 verbosity is set, warn about invalid indices (but still use them). */
131 value_subscript (array, idx)
132 value_ptr array, idx;
135 int c_style = current_language->c_style_arrays;
139 tarray = check_typedef (VALUE_TYPE (array));
140 COERCE_VARYING_ARRAY (array, tarray);
142 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
143 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
145 struct type *range_type = TYPE_INDEX_TYPE (tarray);
146 LONGEST lowerbound, upperbound;
147 get_discrete_bounds (range_type, &lowerbound, &upperbound);
149 if (VALUE_LVAL (array) != lval_memory)
150 return value_subscripted_rvalue (array, idx, lowerbound);
154 LONGEST index = value_as_long (idx);
155 if (index >= lowerbound && index <= upperbound)
156 return value_subscripted_rvalue (array, idx, lowerbound);
157 warning ("array or string index out of range");
158 /* fall doing C stuff */
164 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
165 idx = value_sub (idx, bound);
168 array = value_coerce_array (array);
171 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
173 struct type *range_type = TYPE_INDEX_TYPE (tarray);
174 LONGEST index = value_as_long (idx);
176 int offset, byte, bit_index;
177 LONGEST lowerbound, upperbound;
178 get_discrete_bounds (range_type, &lowerbound, &upperbound);
179 if (index < lowerbound || index > upperbound)
180 error ("bitstring index out of range");
182 offset = index / TARGET_CHAR_BIT;
183 byte = *((char*)VALUE_CONTENTS (array) + offset);
184 bit_index = index % TARGET_CHAR_BIT;
185 byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index);
186 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
187 VALUE_BITPOS (v) = bit_index;
188 VALUE_BITSIZE (v) = 1;
189 VALUE_LVAL (v) = VALUE_LVAL (array);
190 if (VALUE_LVAL (array) == lval_internalvar)
191 VALUE_LVAL (v) = lval_internalvar_component;
192 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
193 VALUE_OFFSET (v) = offset + VALUE_OFFSET (array);
198 return value_ind (value_add (array, idx));
200 error ("not an array or string");
203 /* Return the value of EXPR[IDX], expr an aggregate rvalue
204 (eg, a vector register). This routine used to promote floats
205 to doubles, but no longer does. */
208 value_subscripted_rvalue (array, idx, lowerbound)
209 value_ptr array, idx;
212 struct type *array_type = check_typedef (VALUE_TYPE (array));
213 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
214 unsigned int elt_size = TYPE_LENGTH (elt_type);
215 LONGEST index = value_as_long (idx);
216 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
219 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
220 error ("no such vector element");
222 v = allocate_value (elt_type);
223 if (VALUE_LAZY (array))
226 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
228 if (VALUE_LVAL (array) == lval_internalvar)
229 VALUE_LVAL (v) = lval_internalvar_component;
231 VALUE_LVAL (v) = VALUE_LVAL (array);
232 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
233 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
237 /* Check to see if either argument is a structure. This is called so
238 we know whether to go ahead with the normal binop or look for a
239 user defined function instead.
241 For now, we do not overload the `=' operator. */
244 binop_user_defined_p (op, arg1, arg2)
246 value_ptr arg1, arg2;
248 struct type *type1, *type2;
249 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
251 type1 = check_typedef (VALUE_TYPE (arg1));
252 type2 = check_typedef (VALUE_TYPE (arg2));
253 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
254 || TYPE_CODE (type2) == TYPE_CODE_STRUCT
255 || (TYPE_CODE (type1) == TYPE_CODE_REF
256 && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT)
257 || (TYPE_CODE (type2) == TYPE_CODE_REF
258 && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT));
261 /* Check to see if argument is a structure. This is called so
262 we know whether to go ahead with the normal unop or look for a
263 user defined function instead.
265 For now, we do not overload the `&' operator. */
267 int unop_user_defined_p (op, arg1)
274 type1 = check_typedef (VALUE_TYPE (arg1));
277 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
279 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
280 type1 = TYPE_TARGET_TYPE (type1);
286 /* We know either arg1 or arg2 is a structure, so try to find the right
287 user defined function. Create an argument vector that calls
288 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
289 binary operator which is legal for GNU C++).
291 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
292 is the opcode saying how to modify it. Otherwise, OTHEROP is
296 value_x_binop (arg1, arg2, op, otherop)
297 value_ptr arg1, arg2;
298 enum exp_opcode op, otherop;
310 /* now we know that what we have to do is construct our
311 arg vector and find the right function to call it with. */
313 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
314 error ("Can't do that binary op on that type"); /* FIXME be explicit */
316 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4);
317 argvec[1] = value_addr (arg1);
321 /* make the right function name up */
322 strcpy(tstr, "operator__");
326 case BINOP_ADD: strcpy(ptr,"+"); break;
327 case BINOP_SUB: strcpy(ptr,"-"); break;
328 case BINOP_MUL: strcpy(ptr,"*"); break;
329 case BINOP_DIV: strcpy(ptr,"/"); break;
330 case BINOP_REM: strcpy(ptr,"%"); break;
331 case BINOP_LSH: strcpy(ptr,"<<"); break;
332 case BINOP_RSH: strcpy(ptr,">>"); break;
333 case BINOP_BITWISE_AND: strcpy(ptr,"&"); break;
334 case BINOP_BITWISE_IOR: strcpy(ptr,"|"); break;
335 case BINOP_BITWISE_XOR: strcpy(ptr,"^"); break;
336 case BINOP_LOGICAL_AND: strcpy(ptr,"&&"); break;
337 case BINOP_LOGICAL_OR: strcpy(ptr,"||"); break;
338 case BINOP_MIN: strcpy(ptr,"<?"); break;
339 case BINOP_MAX: strcpy(ptr,">?"); break;
340 case BINOP_ASSIGN: strcpy(ptr,"="); break;
341 case BINOP_ASSIGN_MODIFY:
344 case BINOP_ADD: strcpy(ptr,"+="); break;
345 case BINOP_SUB: strcpy(ptr,"-="); break;
346 case BINOP_MUL: strcpy(ptr,"*="); break;
347 case BINOP_DIV: strcpy(ptr,"/="); break;
348 case BINOP_REM: strcpy(ptr,"%="); break;
349 case BINOP_BITWISE_AND: strcpy(ptr,"&="); break;
350 case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break;
351 case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break;
352 case BINOP_MOD: /* invalid */
354 error ("Invalid binary operation specified.");
357 case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
358 case BINOP_EQUAL: strcpy(ptr,"=="); break;
359 case BINOP_NOTEQUAL: strcpy(ptr,"!="); break;
360 case BINOP_LESS: strcpy(ptr,"<"); break;
361 case BINOP_GTR: strcpy(ptr,">"); break;
362 case BINOP_GEQ: strcpy(ptr,">="); break;
363 case BINOP_LEQ: strcpy(ptr,"<="); break;
364 case BINOP_MOD: /* invalid */
366 error ("Invalid binary operation specified.");
369 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
375 argvec[1] = argvec[0];
378 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
380 error ("member function %s not found", tstr);
382 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
386 /* We know that arg1 is a structure, so try to find a unary user
387 defined operator that matches the operator in question.
388 Create an argument vector that calls arg1.operator @ (arg1)
389 and return that value (where '@' is (almost) any unary operator which
390 is legal for GNU C++). */
393 value_x_unop (arg1, op)
398 char *ptr, *mangle_ptr;
399 char tstr[13], mangle_tstr[13];
405 /* now we know that what we have to do is construct our
406 arg vector and find the right function to call it with. */
408 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
409 error ("Can't do that unary op on that type"); /* FIXME be explicit */
411 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3);
412 argvec[1] = value_addr (arg1);
415 /* make the right function name up */
416 strcpy(tstr,"operator__");
418 strcpy(mangle_tstr, "__");
419 mangle_ptr = mangle_tstr+2;
422 case UNOP_PREINCREMENT: strcpy(ptr,"++"); break;
423 case UNOP_PREDECREMENT: strcpy(ptr,"++"); break;
424 case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break;
425 case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break;
426 case UNOP_LOGICAL_NOT: strcpy(ptr,"!"); break;
427 case UNOP_COMPLEMENT: strcpy(ptr,"~"); break;
428 case UNOP_NEG: strcpy(ptr,"-"); break;
430 error ("Invalid binary operation specified.");
433 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
439 argvec[1] = argvec[0];
442 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
444 error ("member function %s not found", tstr);
445 return 0; /* For lint -- never reached */
449 /* Concatenate two values with the following conditions:
451 (1) Both values must be either bitstring values or character string
452 values and the resulting value consists of the concatenation of
453 ARG1 followed by ARG2.
457 One value must be an integer value and the other value must be
458 either a bitstring value or character string value, which is
459 to be repeated by the number of times specified by the integer
463 (2) Boolean values are also allowed and are treated as bit string
466 (3) Character values are also allowed and are treated as character
467 string values of length 1.
471 value_concat (arg1, arg2)
472 value_ptr arg1, arg2;
474 register value_ptr inval1, inval2, outval;
475 int inval1len, inval2len;
479 struct type *type1 = check_typedef (VALUE_TYPE (arg1));
480 struct type *type2 = check_typedef (VALUE_TYPE (arg2));
482 COERCE_VARYING_ARRAY (arg1, type1);
483 COERCE_VARYING_ARRAY (arg2, type2);
485 /* First figure out if we are dealing with two values to be concatenated
486 or a repeat count and a value to be repeated. INVAL1 is set to the
487 first of two concatenated values, or the repeat count. INVAL2 is set
488 to the second of the two concatenated values or the value to be
491 if (TYPE_CODE (type2) == TYPE_CODE_INT)
493 struct type *tmp = type1;
505 /* Now process the input values. */
507 if (TYPE_CODE (type1) == TYPE_CODE_INT)
509 /* We have a repeat count. Validate the second value and then
510 construct a value repeated that many times. */
511 if (TYPE_CODE (type2) == TYPE_CODE_STRING
512 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
514 count = longest_to_int (value_as_long (inval1));
515 inval2len = TYPE_LENGTH (type2);
516 ptr = (char *) alloca (count * inval2len);
517 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
519 inchar = (char) unpack_long (type2,
520 VALUE_CONTENTS (inval2));
521 for (idx = 0; idx < count; idx++)
523 *(ptr + idx) = inchar;
528 for (idx = 0; idx < count; idx++)
530 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
534 outval = value_string (ptr, count * inval2len);
536 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
537 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
539 error ("unimplemented support for bitstring/boolean repeats");
543 error ("can't repeat values of that type");
546 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
547 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
549 /* We have two character strings to concatenate. */
550 if (TYPE_CODE (type2) != TYPE_CODE_STRING
551 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
553 error ("Strings can only be concatenated with other strings.");
555 inval1len = TYPE_LENGTH (type1);
556 inval2len = TYPE_LENGTH (type2);
557 ptr = (char *) alloca (inval1len + inval2len);
558 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
560 *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1));
564 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
566 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
569 (char) unpack_long (type2, VALUE_CONTENTS (inval2));
573 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
575 outval = value_string (ptr, inval1len + inval2len);
577 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
578 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
580 /* We have two bitstrings to concatenate. */
581 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
582 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
584 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
586 error ("unimplemented support for bitstring/boolean concatenation.");
590 /* We don't know how to concatenate these operands. */
591 error ("illegal operands for concatenation.");
598 /* Perform a binary operation on two operands which have reasonable
599 representations as integers or floats. This includes booleans,
600 characters, integers, or floats.
601 Does not support addition and subtraction on pointers;
602 use value_add or value_sub if you want to handle those possibilities. */
605 value_binop (arg1, arg2, op)
606 value_ptr arg1, arg2;
609 register value_ptr val;
610 struct type *type1, *type2;
616 type1 = check_typedef (VALUE_TYPE (arg1));
617 type2 = check_typedef (VALUE_TYPE (arg2));
619 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
620 && TYPE_CODE (type1) != TYPE_CODE_CHAR
621 && TYPE_CODE (type1) != TYPE_CODE_INT
622 && TYPE_CODE (type1) != TYPE_CODE_BOOL
623 && TYPE_CODE (type1) != TYPE_CODE_RANGE)
625 (TYPE_CODE (type2) != TYPE_CODE_FLT
626 && TYPE_CODE (type2) != TYPE_CODE_CHAR
627 && TYPE_CODE (type2) != TYPE_CODE_INT
628 && TYPE_CODE (type2) != TYPE_CODE_BOOL
629 && TYPE_CODE (type2) != TYPE_CODE_RANGE))
630 error ("Argument to arithmetic operation not a number or boolean.");
632 if (TYPE_CODE (type1) == TYPE_CODE_FLT
634 TYPE_CODE (type2) == TYPE_CODE_FLT)
636 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
637 in target format. real.c in GCC probably has the necessary
640 v1 = value_as_double (arg1);
641 v2 = value_as_double (arg2);
661 error ("Integer-only operation on floating point number.");
664 /* If either arg was long double, make sure that value is also long
667 if (TYPE_LENGTH(type1) * 8 > TARGET_DOUBLE_BIT
668 || TYPE_LENGTH(type2) * 8 > TARGET_DOUBLE_BIT)
669 val = allocate_value (builtin_type_long_double);
671 val = allocate_value (builtin_type_double);
673 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
676 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
678 TYPE_CODE (type2) == TYPE_CODE_BOOL)
681 v1 = value_as_long (arg1);
682 v2 = value_as_long (arg2);
686 case BINOP_BITWISE_AND:
690 case BINOP_BITWISE_IOR:
694 case BINOP_BITWISE_XOR:
699 error ("Invalid operation on booleans.");
702 val = allocate_value (type1);
703 store_signed_integer (VALUE_CONTENTS_RAW (val),
708 /* Integral operations here. */
709 /* FIXME: Also mixed integral/booleans, with result an integer. */
710 /* FIXME: This implements ANSI C rules (also correct for C++).
711 What about FORTRAN and chill? */
713 unsigned int promoted_len1 = TYPE_LENGTH (type1);
714 unsigned int promoted_len2 = TYPE_LENGTH (type2);
715 int is_unsigned1 = TYPE_UNSIGNED (type1);
716 int is_unsigned2 = TYPE_UNSIGNED (type2);
717 unsigned int result_len;
718 int unsigned_operation;
720 /* Determine type length and signedness after promotion for
722 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
725 promoted_len1 = TYPE_LENGTH (builtin_type_int);
727 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
730 promoted_len2 = TYPE_LENGTH (builtin_type_int);
733 /* Determine type length of the result, and if the operation should
735 Use the signedness of the operand with the greater length.
736 If both operands are of equal length, use unsigned operation
737 if one of the operands is unsigned. */
738 if (promoted_len1 > promoted_len2)
740 unsigned_operation = is_unsigned1;
741 result_len = promoted_len1;
743 else if (promoted_len2 > promoted_len1)
745 unsigned_operation = is_unsigned2;
746 result_len = promoted_len2;
750 unsigned_operation = is_unsigned1 || is_unsigned2;
751 result_len = promoted_len1;
754 if (unsigned_operation)
756 unsigned LONGEST v1, v2, v;
757 v1 = (unsigned LONGEST) value_as_long (arg1);
758 v2 = (unsigned LONGEST) value_as_long (arg2);
760 /* Truncate values to the type length of the result. */
761 if (result_len < sizeof (unsigned LONGEST))
763 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
764 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
790 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
791 v1 mod 0 has a defined value, v1. */
792 /* Chill specifies that v2 must be > 0, so check for that. */
793 if (current_language -> la_language == language_chill
794 && value_as_long (arg2) <= 0)
796 error ("Second operand of MOD must be greater than zero.");
805 /* Note floor(v1/v2) == v1/v2 for unsigned. */
818 case BINOP_BITWISE_AND:
822 case BINOP_BITWISE_IOR:
826 case BINOP_BITWISE_XOR:
830 case BINOP_LOGICAL_AND:
834 case BINOP_LOGICAL_OR:
839 v = v1 < v2 ? v1 : v2;
843 v = v1 > v2 ? v1 : v2;
855 error ("Invalid binary operation on numbers.");
858 /* This is a kludge to get around the fact that we don't
859 know how to determine the result type from the types of
860 the operands. (I'm not really sure how much we feel the
861 need to duplicate the exact rules of the current
862 language. They can get really hairy. But not to do so
863 makes it hard to document just what we *do* do). */
865 /* Can't just call init_type because we wouldn't know what
866 name to give the type. */
868 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
869 ? builtin_type_unsigned_long_long
870 : builtin_type_unsigned_long);
871 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
872 TYPE_LENGTH (VALUE_TYPE (val)),
878 v1 = value_as_long (arg1);
879 v2 = value_as_long (arg2);
904 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
905 X mod 0 has a defined value, X. */
906 /* Chill specifies that v2 must be > 0, so check for that. */
907 if (current_language -> la_language == language_chill
910 error ("Second operand of MOD must be greater than zero.");
920 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
936 case BINOP_BITWISE_AND:
940 case BINOP_BITWISE_IOR:
944 case BINOP_BITWISE_XOR:
948 case BINOP_LOGICAL_AND:
952 case BINOP_LOGICAL_OR:
957 v = v1 < v2 ? v1 : v2;
961 v = v1 > v2 ? v1 : v2;
973 error ("Invalid binary operation on numbers.");
976 /* This is a kludge to get around the fact that we don't
977 know how to determine the result type from the types of
978 the operands. (I'm not really sure how much we feel the
979 need to duplicate the exact rules of the current
980 language. They can get really hairy. But not to do so
981 makes it hard to document just what we *do* do). */
983 /* Can't just call init_type because we wouldn't know what
984 name to give the type. */
986 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
987 ? builtin_type_long_long
988 : builtin_type_long);
989 store_signed_integer (VALUE_CONTENTS_RAW (val),
990 TYPE_LENGTH (VALUE_TYPE (val)),
998 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1001 value_logical_not (arg1)
1008 COERCE_NUMBER (arg1);
1009 type1 = check_typedef (VALUE_TYPE (arg1));
1011 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1012 return 0 == value_as_double (arg1);
1014 len = TYPE_LENGTH (type1);
1015 p = VALUE_CONTENTS (arg1);
1026 /* Simulate the C operator == by returning a 1
1027 iff ARG1 and ARG2 have equal contents. */
1030 value_equal (arg1, arg2)
1031 register value_ptr arg1, arg2;
1035 register char *p1, *p2;
1036 struct type *type1, *type2;
1037 enum type_code code1;
1038 enum type_code code2;
1040 COERCE_NUMBER (arg1);
1041 COERCE_NUMBER (arg2);
1043 type1 = check_typedef (VALUE_TYPE (arg1));
1044 type2 = check_typedef (VALUE_TYPE (arg2));
1045 code1 = TYPE_CODE (type1);
1046 code2 = TYPE_CODE (type2);
1048 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
1049 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1051 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
1052 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
1053 return value_as_double (arg1) == value_as_double (arg2);
1055 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1057 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
1058 return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
1059 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
1060 return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
1062 else if (code1 == code2
1063 && ((len = (int) TYPE_LENGTH (type1))
1064 == (int) TYPE_LENGTH (type2)))
1066 p1 = VALUE_CONTENTS (arg1);
1067 p2 = VALUE_CONTENTS (arg2);
1077 error ("Invalid type combination in equality test.");
1078 return 0; /* For lint -- never reached */
1082 /* Simulate the C operator < by returning 1
1083 iff ARG1's contents are less than ARG2's. */
1086 value_less (arg1, arg2)
1087 register value_ptr arg1, arg2;
1089 register enum type_code code1;
1090 register enum type_code code2;
1091 struct type *type1, *type2;
1093 COERCE_NUMBER (arg1);
1094 COERCE_NUMBER (arg2);
1096 type1 = check_typedef (VALUE_TYPE (arg1));
1097 type2 = check_typedef (VALUE_TYPE (arg2));
1098 code1 = TYPE_CODE (type1);
1099 code2 = TYPE_CODE (type2);
1101 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
1102 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1104 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
1105 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
1106 return value_as_double (arg1) < value_as_double (arg2);
1107 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1108 return value_as_pointer (arg1) < value_as_pointer (arg2);
1110 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1112 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
1113 return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
1114 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
1115 return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
1119 error ("Invalid type combination in ordering comparison.");
1124 /* The unary operators - and ~. Both free the argument ARG1. */
1128 register value_ptr arg1;
1130 register struct type *type;
1135 type = check_typedef (VALUE_TYPE (arg1));
1137 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1138 return value_from_double (type, - value_as_double (arg1));
1139 else if (TYPE_CODE (type) == TYPE_CODE_INT)
1140 return value_from_longest (type, - value_as_long (arg1));
1142 error ("Argument to negate operation not a number.");
1143 return 0; /* For lint -- never reached */
1148 value_complement (arg1)
1149 register value_ptr arg1;
1154 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_INT)
1155 error ("Argument to complement operation not an integer.");
1157 return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1));
1160 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1161 and whose VALUE_CONTENTS is valaddr.
1162 Return -1 if out of range, -2 other error. */
1165 value_bit_index (type, valaddr, index)
1170 LONGEST low_bound, high_bound;
1173 struct type *range = TYPE_FIELD_TYPE (type, 0);
1174 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1176 if (index < low_bound || index > high_bound)
1178 rel_index = index - low_bound;
1179 word = unpack_long (builtin_type_unsigned_char,
1180 valaddr + (rel_index / TARGET_CHAR_BIT));
1181 rel_index %= TARGET_CHAR_BIT;
1182 if (BITS_BIG_ENDIAN)
1183 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1184 return (word >> rel_index) & 1;
1188 value_in (element, set)
1189 value_ptr element, set;
1192 struct type *settype = check_typedef (VALUE_TYPE (set));
1193 struct type *eltype = check_typedef (VALUE_TYPE (element));
1194 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1195 eltype = TYPE_TARGET_TYPE (eltype);
1196 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1197 error ("Second argument of 'IN' has wrong type");
1198 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1199 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1200 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1201 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1202 error ("First argument of 'IN' has wrong type");
1203 member = value_bit_index (settype, VALUE_CONTENTS (set),
1204 value_as_long (element));
1206 error ("First argument of 'IN' not in range");
1207 return value_from_longest (LA_BOOL_TYPE, member);
1211 _initialize_valarith ()