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"
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));
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;
130 struct type *range_type;
134 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
135 || TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_STRING)
137 range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
138 lowerbound = TYPE_FIELD_BITPOS (range_type, 0);
141 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
142 idx = value_sub (idx, bound);
144 if (VALUE_LVAL (array) != lval_memory)
146 return value_subscripted_rvalue (array, idx);
148 array = value_coerce_array (array);
150 return value_ind (value_add (array, idx));
153 /* Return the value of EXPR[IDX], expr an aggregate rvalue
154 (eg, a vector register). This routine used to promote floats
155 to doubles, but no longer does. */
158 value_subscripted_rvalue (array, idx)
159 value_ptr array, idx;
161 struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
162 int elt_size = TYPE_LENGTH (elt_type);
163 int elt_offs = elt_size * longest_to_int (value_as_long (idx));
166 if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array)))
167 error ("no such vector element");
169 v = allocate_value (elt_type);
170 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
172 if (VALUE_LVAL (array) == lval_internalvar)
173 VALUE_LVAL (v) = lval_internalvar_component;
175 VALUE_LVAL (v) = not_lval;
176 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
177 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
178 VALUE_BITSIZE (v) = elt_size * 8;
182 /* Check to see if either argument is a structure. This is called so
183 we know whether to go ahead with the normal binop or look for a
184 user defined function instead.
186 For now, we do not overload the `=' operator. */
189 binop_user_defined_p (op, arg1, arg2)
191 value_ptr arg1, arg2;
193 if (op == BINOP_ASSIGN)
195 return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
196 || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
197 || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
198 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
199 || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
200 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
203 /* Check to see if argument is a structure. This is called so
204 we know whether to go ahead with the normal unop or look for a
205 user defined function instead.
207 For now, we do not overload the `&' operator. */
209 int unop_user_defined_p (op, arg1)
215 return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
216 || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
217 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
220 /* We know either arg1 or arg2 is a structure, so try to find the right
221 user defined function. Create an argument vector that calls
222 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
223 binary operator which is legal for GNU C++).
225 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
226 is the opcode saying how to modify it. Otherwise, OTHEROP is
230 value_x_binop (arg1, arg2, op, otherop)
231 value_ptr arg1, arg2;
232 enum exp_opcode op, otherop;
244 /* now we know that what we have to do is construct our
245 arg vector and find the right function to call it with. */
247 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
248 error ("Can't do that binary op on that type"); /* FIXME be explicit */
250 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4);
251 argvec[1] = value_addr (arg1);
255 /* make the right function name up */
256 strcpy(tstr, "operator__");
260 case BINOP_ADD: strcpy(ptr,"+"); break;
261 case BINOP_SUB: strcpy(ptr,"-"); break;
262 case BINOP_MUL: strcpy(ptr,"*"); break;
263 case BINOP_DIV: strcpy(ptr,"/"); break;
264 case BINOP_REM: strcpy(ptr,"%"); break;
265 case BINOP_LSH: strcpy(ptr,"<<"); break;
266 case BINOP_RSH: strcpy(ptr,">>"); break;
267 case BINOP_BITWISE_AND: strcpy(ptr,"&"); break;
268 case BINOP_BITWISE_IOR: strcpy(ptr,"|"); break;
269 case BINOP_BITWISE_XOR: strcpy(ptr,"^"); break;
270 case BINOP_LOGICAL_AND: strcpy(ptr,"&&"); break;
271 case BINOP_LOGICAL_OR: strcpy(ptr,"||"); break;
272 case BINOP_MIN: strcpy(ptr,"<?"); break;
273 case BINOP_MAX: strcpy(ptr,">?"); break;
274 case BINOP_ASSIGN: strcpy(ptr,"="); break;
275 case BINOP_ASSIGN_MODIFY:
278 case BINOP_ADD: strcpy(ptr,"+="); break;
279 case BINOP_SUB: strcpy(ptr,"-="); break;
280 case BINOP_MUL: strcpy(ptr,"*="); break;
281 case BINOP_DIV: strcpy(ptr,"/="); break;
282 case BINOP_REM: strcpy(ptr,"%="); break;
283 case BINOP_BITWISE_AND: strcpy(ptr,"&="); break;
284 case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break;
285 case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break;
286 case BINOP_MOD: /* invalid */
288 error ("Invalid binary operation specified.");
291 case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
292 case BINOP_EQUAL: strcpy(ptr,"=="); break;
293 case BINOP_NOTEQUAL: strcpy(ptr,"!="); break;
294 case BINOP_LESS: strcpy(ptr,"<"); break;
295 case BINOP_GTR: strcpy(ptr,">"); break;
296 case BINOP_GEQ: strcpy(ptr,">="); break;
297 case BINOP_LEQ: strcpy(ptr,"<="); break;
298 case BINOP_MOD: /* invalid */
300 error ("Invalid binary operation specified.");
303 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
309 argvec[1] = argvec[0];
312 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
314 error ("member function %s not found", tstr);
316 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
320 /* We know that arg1 is a structure, so try to find a unary user
321 defined operator that matches the operator in question.
322 Create an argument vector that calls arg1.operator @ (arg1)
323 and return that value (where '@' is (almost) any unary operator which
324 is legal for GNU C++). */
327 value_x_unop (arg1, op)
332 char *ptr, *mangle_ptr;
333 char tstr[13], mangle_tstr[13];
338 /* now we know that what we have to do is construct our
339 arg vector and find the right function to call it with. */
341 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
342 error ("Can't do that unary op on that type"); /* FIXME be explicit */
344 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3);
345 argvec[1] = value_addr (arg1);
348 /* make the right function name up */
349 strcpy(tstr,"operator__");
351 strcpy(mangle_tstr, "__");
352 mangle_ptr = mangle_tstr+2;
355 case UNOP_PREINCREMENT: strcpy(ptr,"++"); break;
356 case UNOP_PREDECREMENT: strcpy(ptr,"++"); break;
357 case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break;
358 case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break;
359 case UNOP_LOGICAL_NOT: strcpy(ptr,"!"); break;
360 case UNOP_COMPLEMENT: strcpy(ptr,"~"); break;
361 case UNOP_NEG: strcpy(ptr,"-"); break;
363 error ("Invalid binary operation specified.");
366 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
372 argvec[1] = argvec[0];
375 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
377 error ("member function %s not found", tstr);
378 return 0; /* For lint -- never reached */
382 /* Concatenate two values with the following conditions:
384 (1) Both values must be either bitstring values or character string
385 values and the resulting value consists of the concatenation of
386 ARG1 followed by ARG2.
390 One value must be an integer value and the other value must be
391 either a bitstring value or character string value, which is
392 to be repeated by the number of times specified by the integer
396 (2) Boolean values are also allowed and are treated as bit string
399 (3) Character values are also allowed and are treated as character
400 string values of length 1.
404 value_concat (arg1, arg2)
405 value_ptr arg1, arg2;
407 register value_ptr inval1, inval2, outval;
408 int inval1len, inval2len;
413 /* First figure out if we are dealing with two values to be concatenated
414 or a repeat count and a value to be repeated. INVAL1 is set to the
415 first of two concatenated values, or the repeat count. INVAL2 is set
416 to the second of the two concatenated values or the value to be
419 if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
430 /* Now process the input values. */
432 if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_INT)
434 /* We have a repeat count. Validate the second value and then
435 construct a value repeated that many times. */
436 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_STRING
437 || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
439 count = longest_to_int (value_as_long (inval1));
440 inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
441 ptr = (char *) alloca (count * inval2len);
442 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
444 inchar = (char) unpack_long (VALUE_TYPE (inval2),
445 VALUE_CONTENTS (inval2));
446 for (idx = 0; idx < count; idx++)
448 *(ptr + idx) = inchar;
453 for (idx = 0; idx < count; idx++)
455 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
459 outval = value_string (ptr, count * inval2len);
461 else if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BITSTRING
462 || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BOOL)
464 error ("unimplemented support for bitstring/boolean repeats");
468 error ("can't repeat values of that type");
471 else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_STRING
472 || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
474 /* We have two character strings to concatenate. */
475 if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_STRING
476 && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_CHAR)
478 error ("Strings can only be concatenated with other strings.");
480 inval1len = TYPE_LENGTH (VALUE_TYPE (inval1));
481 inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
482 ptr = (char *) alloca (inval1len + inval2len);
483 if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
485 *ptr = (char) unpack_long (VALUE_TYPE (inval1), VALUE_CONTENTS (inval1));
489 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
491 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
494 (char) unpack_long (VALUE_TYPE (inval2), VALUE_CONTENTS (inval2));
498 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
500 outval = value_string (ptr, inval1len + inval2len);
502 else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BITSTRING
503 || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BOOL)
505 /* We have two bitstrings to concatenate. */
506 if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BITSTRING
507 && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BOOL)
509 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
511 error ("unimplemented support for bitstring/boolean concatenation.");
515 /* We don't know how to concatenate these operands. */
516 error ("illegal operands for concatenation.");
523 /* Perform a binary operation on two operands which have reasonable
524 representations as integers or floats. This includes booleans,
525 characters, integers, or floats.
526 Does not support addition and subtraction on pointers;
527 use value_add or value_sub if you want to handle those possibilities. */
530 value_binop (arg1, arg2, op)
531 value_ptr arg1, arg2;
534 register value_ptr val;
539 if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT
540 && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_CHAR
541 && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT
542 && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL
543 && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_RANGE)
545 (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT
546 && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_CHAR
547 && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT
548 && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL
549 && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_RANGE))
550 error ("Argument to arithmetic operation not a number or boolean.");
552 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT
554 TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT)
556 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
557 in target format. real.c in GCC probably has the necessary
560 v1 = value_as_double (arg1);
561 v2 = value_as_double (arg2);
581 error ("Integer-only operation on floating point number.");
584 val = allocate_value (builtin_type_double);
585 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
588 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL
590 TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL)
593 v1 = value_as_long (arg1);
594 v2 = value_as_long (arg2);
598 case BINOP_BITWISE_AND:
602 case BINOP_BITWISE_IOR:
606 case BINOP_BITWISE_XOR:
611 error ("Invalid operation on booleans.");
614 val = allocate_value (builtin_type_chill_bool);
615 store_signed_integer (VALUE_CONTENTS_RAW (val),
616 TYPE_LENGTH (VALUE_TYPE (val)),
620 /* Integral operations here. */
621 /* FIXME: Also mixed integral/booleans, with result an integer. */
622 /* FIXME: This implements ANSI C rules (also correct for C++).
623 What about FORTRAN and chill? */
625 struct type *type1 = VALUE_TYPE (arg1);
626 struct type *type2 = VALUE_TYPE (arg2);
627 int promoted_len1 = TYPE_LENGTH (type1);
628 int promoted_len2 = TYPE_LENGTH (type2);
629 int is_unsigned1 = TYPE_UNSIGNED (type1);
630 int is_unsigned2 = TYPE_UNSIGNED (type2);
632 int unsigned_operation;
634 /* Determine type length and signedness after promotion for
636 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
639 promoted_len1 = TYPE_LENGTH (builtin_type_int);
641 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
644 promoted_len2 = TYPE_LENGTH (builtin_type_int);
647 /* Determine type length of the result, and if the operation should
649 Use the signedness of the operand with the greater length.
650 If both operands are of equal length, use unsigned operation
651 if one of the operands is unsigned. */
652 if (promoted_len1 > promoted_len2)
654 unsigned_operation = is_unsigned1;
655 result_len = promoted_len1;
657 else if (promoted_len2 > promoted_len1)
659 unsigned_operation = is_unsigned2;
660 result_len = promoted_len2;
664 unsigned_operation = is_unsigned1 || is_unsigned2;
665 result_len = promoted_len1;
668 if (unsigned_operation)
670 unsigned LONGEST v1, v2, v;
671 v1 = (unsigned LONGEST) value_as_long (arg1);
672 v2 = (unsigned LONGEST) value_as_long (arg2);
674 /* Truncate values to the type length of the result. */
675 if (result_len < sizeof (unsigned LONGEST))
677 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
678 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
704 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
705 v1 mod 0 has a defined value, v1. */
706 /* Chill specifies that v2 must be > 0, so check for that. */
707 if (current_language -> la_language == language_chill
708 && value_as_long (arg2) <= 0)
710 error ("Second operand of MOD must be greater than zero.");
719 /* Note floor(v1/v2) == v1/v2 for unsigned. */
732 case BINOP_BITWISE_AND:
736 case BINOP_BITWISE_IOR:
740 case BINOP_BITWISE_XOR:
744 case BINOP_LOGICAL_AND:
748 case BINOP_LOGICAL_OR:
753 v = v1 < v2 ? v1 : v2;
757 v = v1 > v2 ? v1 : v2;
769 error ("Invalid binary operation on numbers.");
772 /* This is a kludge to get around the fact that we don't
773 know how to determine the result type from the types of
774 the operands. (I'm not really sure how much we feel the
775 need to duplicate the exact rules of the current
776 language. They can get really hairy. But not to do so
777 makes it hard to document just what we *do* do). */
779 /* Can't just call init_type because we wouldn't know what
780 name to give the type. */
782 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
783 ? builtin_type_unsigned_long_long
784 : builtin_type_unsigned_long);
785 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
786 TYPE_LENGTH (VALUE_TYPE (val)),
792 v1 = value_as_long (arg1);
793 v2 = value_as_long (arg2);
818 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
819 X mod 0 has a defined value, X. */
820 /* Chill specifies that v2 must be > 0, so check for that. */
821 if (current_language -> la_language == language_chill
824 error ("Second operand of MOD must be greater than zero.");
834 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
850 case BINOP_BITWISE_AND:
854 case BINOP_BITWISE_IOR:
858 case BINOP_BITWISE_XOR:
862 case BINOP_LOGICAL_AND:
866 case BINOP_LOGICAL_OR:
871 v = v1 < v2 ? v1 : v2;
875 v = v1 > v2 ? v1 : v2;
887 error ("Invalid binary operation on numbers.");
890 /* This is a kludge to get around the fact that we don't
891 know how to determine the result type from the types of
892 the operands. (I'm not really sure how much we feel the
893 need to duplicate the exact rules of the current
894 language. They can get really hairy. But not to do so
895 makes it hard to document just what we *do* do). */
897 /* Can't just call init_type because we wouldn't know what
898 name to give the type. */
900 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
901 ? builtin_type_long_long
902 : builtin_type_long);
903 store_signed_integer (VALUE_CONTENTS_RAW (val),
904 TYPE_LENGTH (VALUE_TYPE (val)),
912 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
915 value_logical_not (arg1)
923 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT)
924 return 0 == value_as_double (arg1);
926 len = TYPE_LENGTH (VALUE_TYPE (arg1));
927 p = VALUE_CONTENTS (arg1);
938 /* Simulate the C operator == by returning a 1
939 iff ARG1 and ARG2 have equal contents. */
942 value_equal (arg1, arg2)
943 register value_ptr arg1, arg2;
947 register char *p1, *p2;
948 enum type_code code1;
949 enum type_code code2;
954 code1 = TYPE_CODE (VALUE_TYPE (arg1));
955 code2 = TYPE_CODE (VALUE_TYPE (arg2));
957 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
958 return longest_to_int (value_as_long (value_binop (arg1, arg2,
960 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
961 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
962 return value_as_double (arg1) == value_as_double (arg2);
964 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
966 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
967 return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
968 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
969 return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
971 else if (code1 == code2
972 && ((len = TYPE_LENGTH (VALUE_TYPE (arg1)))
973 == TYPE_LENGTH (VALUE_TYPE (arg2))))
975 p1 = VALUE_CONTENTS (arg1);
976 p2 = VALUE_CONTENTS (arg2);
986 error ("Invalid type combination in equality test.");
987 return 0; /* For lint -- never reached */
991 /* Simulate the C operator < by returning 1
992 iff ARG1's contents are less than ARG2's. */
995 value_less (arg1, arg2)
996 register value_ptr arg1, arg2;
998 register enum type_code code1;
999 register enum type_code code2;
1001 COERCE_ARRAY (arg1);
1002 COERCE_ARRAY (arg2);
1004 code1 = TYPE_CODE (VALUE_TYPE (arg1));
1005 code2 = TYPE_CODE (VALUE_TYPE (arg2));
1007 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
1008 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1010 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
1011 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
1012 return value_as_double (arg1) < value_as_double (arg2);
1013 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1014 return value_as_pointer (arg1) < value_as_pointer (arg2);
1016 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1018 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
1019 return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
1020 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
1021 return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
1025 error ("Invalid type combination in ordering comparison.");
1030 /* The unary operators - and ~. Both free the argument ARG1. */
1034 register value_ptr arg1;
1036 register struct type *type;
1040 type = VALUE_TYPE (arg1);
1042 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1043 return value_from_double (type, - value_as_double (arg1));
1044 else if (TYPE_CODE (type) == TYPE_CODE_INT)
1045 return value_from_longest (type, - value_as_long (arg1));
1047 error ("Argument to negate operation not a number.");
1048 return 0; /* For lint -- never reached */
1053 value_complement (arg1)
1054 register value_ptr arg1;
1058 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
1059 error ("Argument to complement operation not an integer.");
1061 return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1));
1064 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1065 and whose VALUE_CONTENTS is valaddr.
1066 Return -1 if out of range, -2 other error. */
1069 value_bit_index (type, valaddr, index)
1075 int low_bound, high_bound;
1078 range = TYPE_FIELD_TYPE (type, 0);
1079 if (TYPE_CODE (range) != TYPE_CODE_RANGE)
1081 low_bound = TYPE_LOW_BOUND (range);
1082 high_bound = TYPE_HIGH_BOUND (range);
1083 if (index < low_bound || index > high_bound)
1085 rel_index = index - low_bound;
1086 word = unpack_long (builtin_type_unsigned_char,
1087 valaddr + (rel_index / TARGET_CHAR_BIT));
1088 rel_index %= TARGET_CHAR_BIT;
1089 if (BITS_BIG_ENDIAN)
1090 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1091 return (word >> rel_index) & 1;
1095 value_in (element, set)
1096 value_ptr element, set;
1099 if (TYPE_CODE (VALUE_TYPE (set)) != TYPE_CODE_SET)
1100 error ("Second argument of 'IN' has wrong type");
1101 if (TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_INT
1102 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_CHAR
1103 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_ENUM
1104 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_BOOL)
1105 error ("First argument of 'IN' has wrong type");
1106 member = value_bit_index (VALUE_TYPE (set), VALUE_CONTENTS (set),
1107 value_as_long (element));
1109 error ("First argument of 'IN' not in range");
1110 return value_from_longest (builtin_type_int, member);
1114 _initialize_valarith ()