1 /* Perform arithmetic and other operations on values, for GDB.
2 Copyright 1986, 1989, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "expression.h"
30 /* Define whether or not the C operator '/' truncates towards zero for
31 differently signed operands (truncation direction is undefined in C). */
33 #ifndef TRUNCATION_TOWARDS_ZERO
34 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
38 value_subscripted_rvalue PARAMS ((value, value));
42 value_add (arg1, arg2)
45 register value 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 (VALUE_TYPE (arg1) == VALUE_TYPE (arg2))
99 /* pointer to <type x> - pointer to <type x>. */
100 return value_from_longest
101 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
102 (value_as_long (arg1) - value_as_long (arg2))
103 / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))));
108 First argument of `-' is a pointer and second argument is neither\n\
109 an integer nor a pointer of the same type.");
113 return value_binop (arg1, arg2, BINOP_SUB);
116 /* Return the value of ARRAY[IDX].
117 See comments in value_coerce_array() for rationale for reason for
118 doing lower bounds adjustment here rather than there.
119 FIXME: Perhaps we should validate that the index is valid and if
120 verbosity is set, warn about invalid indices (but still use them). */
123 value_subscript (array, idx)
128 struct type *range_type;
132 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
133 || TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_STRING)
135 range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
136 lowerbound = TYPE_FIELD_BITPOS (range_type, 0);
139 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
140 idx = value_sub (idx, bound);
142 if (VALUE_LVAL (array) != lval_memory)
144 return value_subscripted_rvalue (array, idx);
146 array = value_coerce_array (array);
148 return value_ind (value_add (array, idx));
151 /* Return the value of EXPR[IDX], expr an aggregate rvalue
152 (eg, a vector register). This routine used to promote floats
153 to doubles, but no longer does. */
156 value_subscripted_rvalue (array, idx)
159 struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
160 int elt_size = TYPE_LENGTH (elt_type);
161 int elt_offs = elt_size * longest_to_int (value_as_long (idx));
164 if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array)))
165 error ("no such vector element");
167 v = allocate_value (elt_type);
168 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
170 if (VALUE_LVAL (array) == lval_internalvar)
171 VALUE_LVAL (v) = lval_internalvar_component;
173 VALUE_LVAL (v) = not_lval;
174 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
175 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
176 VALUE_BITSIZE (v) = elt_size * 8;
180 /* Check to see if either argument is a structure. This is called so
181 we know whether to go ahead with the normal binop or look for a
182 user defined function instead.
184 For now, we do not overload the `=' operator. */
187 binop_user_defined_p (op, arg1, arg2)
191 if (op == BINOP_ASSIGN)
193 return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
194 || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
195 || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
196 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
197 || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
198 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
201 /* Check to see if argument is a structure. This is called so
202 we know whether to go ahead with the normal unop or look for a
203 user defined function instead.
205 For now, we do not overload the `&' operator. */
207 int unop_user_defined_p (op, arg1)
213 return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
214 || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
215 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
218 /* We know either arg1 or arg2 is a structure, so try to find the right
219 user defined function. Create an argument vector that calls
220 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
221 binary operator which is legal for GNU C++).
223 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
224 is the opcode saying how to modify it. Otherwise, OTHEROP is
228 value_x_binop (arg1, arg2, op, otherop)
230 enum exp_opcode op, otherop;
233 char *ptr, *mangle_ptr;
234 char tstr[13], mangle_tstr[13];
242 /* now we know that what we have to do is construct our
243 arg vector and find the right function to call it with. */
245 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
246 error ("Can't do that binary op on that type"); /* FIXME be explicit */
248 argvec = (value *) alloca (sizeof (value) * 4);
249 argvec[1] = value_addr (arg1);
253 /* make the right function name up */
254 strcpy(tstr, "operator__");
258 case BINOP_ADD: strcpy(ptr,"+"); break;
259 case BINOP_SUB: strcpy(ptr,"-"); break;
260 case BINOP_MUL: strcpy(ptr,"*"); break;
261 case BINOP_DIV: strcpy(ptr,"/"); break;
262 case BINOP_REM: strcpy(ptr,"%"); break;
263 case BINOP_LSH: strcpy(ptr,"<<"); break;
264 case BINOP_RSH: strcpy(ptr,">>"); break;
265 case BINOP_BITWISE_AND: strcpy(ptr,"&"); break;
266 case BINOP_BITWISE_IOR: strcpy(ptr,"|"); break;
267 case BINOP_BITWISE_XOR: strcpy(ptr,"^"); break;
268 case BINOP_LOGICAL_AND: strcpy(ptr,"&&"); break;
269 case BINOP_LOGICAL_OR: strcpy(ptr,"||"); break;
270 case BINOP_MIN: strcpy(ptr,"<?"); break;
271 case BINOP_MAX: strcpy(ptr,">?"); break;
272 case BINOP_ASSIGN: strcpy(ptr,"="); break;
273 case BINOP_ASSIGN_MODIFY:
276 case BINOP_ADD: strcpy(ptr,"+="); break;
277 case BINOP_SUB: strcpy(ptr,"-="); break;
278 case BINOP_MUL: strcpy(ptr,"*="); break;
279 case BINOP_DIV: strcpy(ptr,"/="); break;
280 case BINOP_REM: strcpy(ptr,"%="); break;
281 case BINOP_BITWISE_AND: strcpy(ptr,"&="); break;
282 case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break;
283 case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break;
284 case BINOP_MOD: /* invalid */
286 error ("Invalid binary operation specified.");
289 case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
290 case BINOP_EQUAL: strcpy(ptr,"=="); break;
291 case BINOP_NOTEQUAL: strcpy(ptr,"!="); break;
292 case BINOP_LESS: strcpy(ptr,"<"); break;
293 case BINOP_GTR: strcpy(ptr,">"); break;
294 case BINOP_GEQ: strcpy(ptr,">="); break;
295 case BINOP_LEQ: strcpy(ptr,"<="); break;
296 case BINOP_MOD: /* invalid */
298 error ("Invalid binary operation specified.");
301 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
307 argvec[1] = argvec[0];
310 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
312 error ("member function %s not found", tstr);
314 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
318 /* We know that arg1 is a structure, so try to find a unary user
319 defined operator that matches the operator in question.
320 Create an argument vector that calls arg1.operator @ (arg1)
321 and return that value (where '@' is (almost) any unary operator which
322 is legal for GNU C++). */
325 value_x_unop (arg1, op)
330 char *ptr, *mangle_ptr;
331 char tstr[13], mangle_tstr[13];
336 /* now we know that what we have to do is construct our
337 arg vector and find the right function to call it with. */
339 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
340 error ("Can't do that unary op on that type"); /* FIXME be explicit */
342 argvec = (value *) alloca (sizeof (value) * 3);
343 argvec[1] = value_addr (arg1);
346 /* make the right function name up */
347 strcpy(tstr,"operator__");
349 strcpy(mangle_tstr, "__");
350 mangle_ptr = mangle_tstr+2;
353 case UNOP_PREINCREMENT: strcpy(ptr,"++"); break;
354 case UNOP_PREDECREMENT: strcpy(ptr,"++"); break;
355 case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break;
356 case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break;
357 case UNOP_LOGICAL_NOT: strcpy(ptr,"!"); break;
358 case UNOP_COMPLEMENT: strcpy(ptr,"~"); break;
359 case UNOP_NEG: strcpy(ptr,"-"); break;
361 error ("Invalid binary operation specified.");
364 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
370 argvec[1] = argvec[0];
373 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
375 error ("member function %s not found", tstr);
376 return 0; /* For lint -- never reached */
380 /* Concatenate two values with the following conditions:
382 (1) Both values must be either bitstring values or character string
383 values and the resulting value consists of the concatenation of
384 ARG1 followed by ARG2.
388 One value must be an integer value and the other value must be
389 either a bitstring value or character string value, which is
390 to be repeated by the number of times specified by the integer
394 (2) Boolean values are also allowed and are treated as bit string
397 (3) Character values are also allowed and are treated as character
398 string values of length 1.
402 value_concat (arg1, arg2)
405 register value inval1, inval2, outval;
406 int inval1len, inval2len;
411 /* First figure out if we are dealing with two values to be concatenated
412 or a repeat count and a value to be repeated. INVAL1 is set to the
413 first of two concatenated values, or the repeat count. INVAL2 is set
414 to the second of the two concatenated values or the value to be
417 if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
428 /* Now process the input values. */
430 if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_INT)
432 /* We have a repeat count. Validate the second value and then
433 construct a value repeated that many times. */
434 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_STRING
435 || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
437 count = longest_to_int (value_as_long (inval1));
438 inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
439 ptr = (char *) alloca (count * inval2len);
440 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
442 inchar = (char) unpack_long (VALUE_TYPE (inval2),
443 VALUE_CONTENTS (inval2));
444 for (idx = 0; idx < count; idx++)
446 *(ptr + idx) = inchar;
451 for (idx = 0; idx < count; idx++)
453 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
457 outval = value_string (ptr, count * inval2len);
459 else if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BITSTRING
460 || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BOOL)
462 error ("unimplemented support for bitstring/boolean repeats");
466 error ("can't repeat values of that type");
469 else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_STRING
470 || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
472 /* We have two character strings to concatenate. */
473 if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_STRING
474 && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_CHAR)
476 error ("Strings can only be concatenated with other strings.");
478 inval1len = TYPE_LENGTH (VALUE_TYPE (inval1));
479 inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
480 ptr = (char *) alloca (inval1len + inval2len);
481 if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
483 *ptr = (char) unpack_long (VALUE_TYPE (inval1), VALUE_CONTENTS (inval1));
487 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
489 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
492 (char) unpack_long (VALUE_TYPE (inval2), VALUE_CONTENTS (inval2));
496 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
498 outval = value_string (ptr, inval1len + inval2len);
500 else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BITSTRING
501 || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BOOL)
503 /* We have two bitstrings to concatenate. */
504 if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BITSTRING
505 && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BOOL)
507 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
509 error ("unimplemented support for bitstring/boolean concatenation.");
513 /* We don't know how to concatenate these operands. */
514 error ("illegal operands for concatenation.");
520 /* The type we give to value_binop results. This is a kludge to get around
521 the fact that we don't know how to determine the result type from
522 the types of the operands. (I'm not really sure how much we feel
523 the need to duplicate the exact rules of the current language.
524 They can get really hairy. But not to do so makes it hard to document
525 just what we *do* do). */
526 static struct type *signed_operation_result;
527 static struct type *unsigned_operation_result;
529 /* Perform a binary operation on two operands which have reasonable
530 representations as integers or floats. This includes booleans,
531 characters, integers, or floats.
532 Does not support addition and subtraction on pointers;
533 use value_add or value_sub if you want to handle those possibilities. */
536 value_binop (arg1, arg2, op)
545 if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT
547 TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_CHAR
549 TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT
551 TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL)
553 (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT
555 TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_CHAR
557 TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT
559 TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL))
560 error ("Argument to arithmetic operation not a number or boolean.");
562 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT
564 TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT)
566 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
567 in target format. real.c in GCC probably has the necessary
570 v1 = value_as_double (arg1);
571 v2 = value_as_double (arg2);
591 error ("Integer-only operation on floating point number.");
594 val = allocate_value (builtin_type_double);
595 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
598 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL
600 TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL)
603 v1 = value_as_long (arg1);
604 v2 = value_as_long (arg2);
608 case BINOP_BITWISE_AND:
612 case BINOP_BITWISE_IOR:
616 case BINOP_BITWISE_XOR:
621 error ("Invalid operation on booleans.");
624 val = allocate_value (builtin_type_chill_bool);
625 store_signed_integer (VALUE_CONTENTS_RAW (val),
626 TYPE_LENGTH (VALUE_TYPE (val)),
630 /* Integral operations here. */
631 /* FIXME: Also mixed integral/booleans, with result an integer. */
633 /* Should we promote to unsigned longest? */
634 if ((TYPE_UNSIGNED (VALUE_TYPE (arg1))
635 || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
636 && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST)
637 || TYPE_LENGTH (VALUE_TYPE (arg2)) >= sizeof (unsigned LONGEST)))
639 unsigned LONGEST v1, v2, v;
640 v1 = (unsigned LONGEST) value_as_long (arg1);
641 v2 = (unsigned LONGEST) value_as_long (arg2);
666 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
667 v1 mod 0 has a defined value, v1. */
668 /* Chill specifies that v2 must be > 0, so check for that. */
669 if (current_language -> la_language == language_chill
670 && value_as_long (arg2) <= 0)
672 error ("Second operand of MOD must be greater than zero.");
681 /* Note floor(v1/v2) == v1/v2 for unsigned. */
694 case BINOP_BITWISE_AND:
698 case BINOP_BITWISE_IOR:
702 case BINOP_BITWISE_XOR:
706 case BINOP_LOGICAL_AND:
710 case BINOP_LOGICAL_OR:
715 v = v1 < v2 ? v1 : v2;
719 v = v1 > v2 ? v1 : v2;
723 error ("Invalid binary operation on numbers.");
726 val = allocate_value (unsigned_operation_result);
727 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
728 TYPE_LENGTH (VALUE_TYPE (val)),
734 v1 = value_as_long (arg1);
735 v2 = value_as_long (arg2);
760 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
761 X mod 0 has a defined value, X. */
762 /* Chill specifies that v2 must be > 0, so check for that. */
763 if (current_language -> la_language == language_chill
766 error ("Second operand of MOD must be greater than zero.");
776 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
792 case BINOP_BITWISE_AND:
796 case BINOP_BITWISE_IOR:
800 case BINOP_BITWISE_XOR:
804 case BINOP_LOGICAL_AND:
808 case BINOP_LOGICAL_OR:
813 v = v1 < v2 ? v1 : v2;
817 v = v1 > v2 ? v1 : v2;
821 error ("Invalid binary operation on numbers.");
824 val = allocate_value (signed_operation_result);
825 store_signed_integer (VALUE_CONTENTS_RAW (val),
826 TYPE_LENGTH (VALUE_TYPE (val)),
834 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
837 value_logical_not (arg1)
845 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT)
846 return 0 == value_as_double (arg1);
848 len = TYPE_LENGTH (VALUE_TYPE (arg1));
849 p = VALUE_CONTENTS (arg1);
860 /* Simulate the C operator == by returning a 1
861 iff ARG1 and ARG2 have equal contents. */
864 value_equal (arg1, arg2)
865 register value arg1, arg2;
869 register char *p1, *p2;
870 enum type_code code1;
871 enum type_code code2;
876 code1 = TYPE_CODE (VALUE_TYPE (arg1));
877 code2 = TYPE_CODE (VALUE_TYPE (arg2));
879 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
880 return value_as_long (arg1) == value_as_long (arg2);
881 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
882 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
883 return value_as_double (arg1) == value_as_double (arg2);
885 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
887 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
888 return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
889 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
890 return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
892 else if (code1 == code2
893 && ((len = TYPE_LENGTH (VALUE_TYPE (arg1)))
894 == TYPE_LENGTH (VALUE_TYPE (arg2))))
896 p1 = VALUE_CONTENTS (arg1);
897 p2 = VALUE_CONTENTS (arg2);
907 error ("Invalid type combination in equality test.");
908 return 0; /* For lint -- never reached */
912 /* Simulate the C operator < by returning 1
913 iff ARG1's contents are less than ARG2's. */
916 value_less (arg1, arg2)
917 register value arg1, arg2;
919 register enum type_code code1;
920 register enum type_code code2;
925 code1 = TYPE_CODE (VALUE_TYPE (arg1));
926 code2 = TYPE_CODE (VALUE_TYPE (arg2));
928 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
930 if (TYPE_UNSIGNED (VALUE_TYPE (arg1))
931 || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
932 return ((unsigned LONGEST) value_as_long (arg1)
933 < (unsigned LONGEST) value_as_long (arg2));
935 return value_as_long (arg1) < value_as_long (arg2);
937 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
938 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
939 return value_as_double (arg1) < value_as_double (arg2);
940 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
941 return value_as_pointer (arg1) < value_as_pointer (arg2);
943 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
945 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
946 return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
947 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
948 return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
952 error ("Invalid type combination in ordering comparison.");
957 /* The unary operators - and ~. Both free the argument ARG1. */
963 register struct type *type;
967 type = VALUE_TYPE (arg1);
969 if (TYPE_CODE (type) == TYPE_CODE_FLT)
970 return value_from_double (type, - value_as_double (arg1));
971 else if (TYPE_CODE (type) == TYPE_CODE_INT)
972 return value_from_longest (type, - value_as_long (arg1));
974 error ("Argument to negate operation not a number.");
975 return 0; /* For lint -- never reached */
980 value_complement (arg1)
985 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
986 error ("Argument to complement operation not an integer.");
988 return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1));
991 /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
992 and whose VALUE_CONTENTS is valaddr.
993 Return -1 if out of range, -2 other error. */
996 value_bit_index (type, valaddr, index)
1002 int low_bound, high_bound, bit_length;
1004 range = TYPE_FIELD_TYPE (type, 0);
1005 if (TYPE_CODE (range) != TYPE_CODE_RANGE)
1007 low_bound = TYPE_LOW_BOUND (range);
1008 high_bound = TYPE_HIGH_BOUND (range);
1009 if (index < low_bound || index > high_bound)
1011 bit_length = high_bound - low_bound + 1;
1013 if (bit_length <= TARGET_CHAR_BIT)
1014 word = unpack_long (builtin_type_unsigned_char, valaddr);
1015 else if (bit_length <= TARGET_SHORT_BIT)
1016 word = unpack_long (builtin_type_unsigned_short, valaddr);
1019 int word_start_index = (index / TARGET_INT_BIT) * TARGET_INT_BIT;
1020 index -= word_start_index;
1021 word = unpack_long (builtin_type_unsigned_int,
1022 valaddr + (word_start_index / HOST_CHAR_BIT));
1025 if (bit_length <= TARGET_CHAR_BIT)
1026 index = TARGET_CHAR_BIT - 1 - index;
1027 else if (bit_length <= TARGET_SHORT_BIT)
1028 index = TARGET_SHORT_BIT - 1 - index;
1030 index = TARGET_INT_BIT - 1 - index;
1032 return (word >> index) & 1;
1036 value_in (element, set)
1040 if (TYPE_CODE (VALUE_TYPE (set)) != TYPE_CODE_SET)
1041 error ("Second argument of 'IN' has wrong type");
1042 if (TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_INT
1043 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_CHAR
1044 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_ENUM
1045 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_BOOL)
1046 error ("First argument of 'IN' has wrong type");
1047 member = value_bit_index (VALUE_TYPE (set), VALUE_CONTENTS (set),
1048 value_as_long (element));
1050 error ("First argument of 'IN' not in range");
1051 return value_from_longest (builtin_type_int, member);
1055 _initialize_valarith ()
1057 /* Can't just call init_type because we wouldn't know what names to give
1059 if (sizeof (LONGEST) > TARGET_LONG_BIT / HOST_CHAR_BIT)
1061 unsigned_operation_result = builtin_type_unsigned_long_long;
1062 signed_operation_result = builtin_type_long_long;
1066 unsigned_operation_result = builtin_type_unsigned_long;
1067 signed_operation_result = builtin_type_long;