]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/valarith.c
Initial creation of sourceware repository
[thirdparty/binutils-gdb.git] / gdb / valarith.c
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.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
20
21 #include "defs.h"
22 #include "value.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "target.h"
27 #include "language.h"
28 #include "demangle.h"
29 #include "gdb_string.h"
30
31 /* Define whether or not the C operator '/' truncates towards zero for
32 differently signed operands (truncation direction is undefined in C). */
33
34 #ifndef TRUNCATION_TOWARDS_ZERO
35 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
36 #endif
37
38 static value_ptr value_subscripted_rvalue PARAMS ((value_ptr, value_ptr, int));
39
40 void _initialize_valarith PARAMS ((void));
41
42 \f
43 value_ptr
44 value_add (arg1, arg2)
45 value_ptr arg1, arg2;
46 {
47 register value_ptr valint, valptr;
48 register int len;
49 struct type *type1, *type2, *valptrtype;
50
51 COERCE_NUMBER (arg1);
52 COERCE_NUMBER (arg2);
53 type1 = check_typedef (VALUE_TYPE (arg1));
54 type2 = check_typedef (VALUE_TYPE (arg2));
55
56 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
57 || TYPE_CODE (type2) == TYPE_CODE_PTR)
58 &&
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. */
62 {
63 value_ptr retval;
64
65 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
66 {
67 valptr = arg1;
68 valint = arg2;
69 valptrtype = type1;
70 }
71 else
72 {
73 valptr = arg2;
74 valint = arg1;
75 valptrtype = type2;
76 }
77 len = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype)));
78 if (len == 0)
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);
84 return retval;
85 }
86
87 return value_binop (arg1, arg2, BINOP_ADD);
88 }
89
90 value_ptr
91 value_sub (arg1, arg2)
92 value_ptr arg1, arg2;
93 {
94 struct type *type1, *type2;
95 COERCE_NUMBER (arg1);
96 COERCE_NUMBER (arg2);
97 type1 = check_typedef (VALUE_TYPE (arg1));
98 type2 = check_typedef (VALUE_TYPE (arg2));
99
100 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
101 {
102 if (TYPE_CODE (type2) == TYPE_CODE_INT)
103 {
104 /* pointer - integer. */
105 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
106 return value_from_longest
107 (VALUE_TYPE (arg1),
108 value_as_long (arg1) - (sz * value_as_long (arg2)));
109 }
110 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
111 && TYPE_LENGTH (TYPE_TARGET_TYPE (type1))
112 == TYPE_LENGTH (TYPE_TARGET_TYPE (type2)))
113 {
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);
119 }
120 else
121 {
122 error ("\
123 First argument of `-' is a pointer and second argument is neither\n\
124 an integer nor a pointer of the same type.");
125 }
126 }
127
128 return value_binop (arg1, arg2, BINOP_SUB);
129 }
130
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). */
136
137 value_ptr
138 value_subscript (array, idx)
139 value_ptr array, idx;
140 {
141 value_ptr bound;
142 int c_style = current_language->c_style_arrays;
143 struct type *tarray;
144
145 COERCE_REF (array);
146 tarray = check_typedef (VALUE_TYPE (array));
147 COERCE_VARYING_ARRAY (array, tarray);
148
149 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
150 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
151 {
152 struct type *range_type = TYPE_INDEX_TYPE (tarray);
153 LONGEST lowerbound, upperbound;
154 get_discrete_bounds (range_type, &lowerbound, &upperbound);
155
156 if (VALUE_LVAL (array) != lval_memory)
157 return value_subscripted_rvalue (array, idx, lowerbound);
158
159 if (c_style == 0)
160 {
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 */
166 c_style = 1;
167 }
168
169 if (lowerbound != 0)
170 {
171 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
172 idx = value_sub (idx, bound);
173 }
174
175 array = value_coerce_array (array);
176 }
177
178 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
179 {
180 struct type *range_type = TYPE_INDEX_TYPE (tarray);
181 LONGEST index = value_as_long (idx);
182 value_ptr v;
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");
188 index -= lowerbound;
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);
201 return v;
202 }
203
204 if (c_style)
205 return value_ind (value_add (array, idx));
206 else
207 error ("not an array or string");
208 }
209
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. */
213
214 static value_ptr
215 value_subscripted_rvalue (array, idx, lowerbound)
216 value_ptr array, idx;
217 int lowerbound;
218 {
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);
224 value_ptr v;
225
226 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
227 error ("no such vector element");
228
229 v = allocate_value (elt_type);
230 if (VALUE_LAZY (array))
231 VALUE_LAZY (v) = 1;
232 else
233 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
234
235 if (VALUE_LVAL (array) == lval_internalvar)
236 VALUE_LVAL (v) = lval_internalvar_component;
237 else
238 VALUE_LVAL (v) = VALUE_LVAL (array);
239 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
240 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
241 return v;
242 }
243 \f
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.
247
248 For now, we do not overload the `=' operator. */
249
250 int
251 binop_user_defined_p (op, arg1, arg2)
252 enum exp_opcode op;
253 value_ptr arg1, arg2;
254 {
255 struct type *type1, *type2;
256 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
257 return 0;
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));
266 }
267
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.
271
272 For now, we do not overload the `&' operator. */
273
274 int unop_user_defined_p (op, arg1)
275 enum exp_opcode op;
276 value_ptr arg1;
277 {
278 struct type *type1;
279 if (op == UNOP_ADDR)
280 return 0;
281 type1 = check_typedef (VALUE_TYPE (arg1));
282 for (;;)
283 {
284 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
285 return 1;
286 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
287 type1 = TYPE_TARGET_TYPE (type1);
288 else
289 return 0;
290 }
291 }
292
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++).
297
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
300 unused. */
301
302 value_ptr
303 value_x_binop (arg1, arg2, op, otherop, noside)
304 value_ptr arg1, arg2;
305 enum exp_opcode op, otherop;
306 enum noside noside;
307 {
308 value_ptr * argvec;
309 char *ptr;
310 char tstr[13];
311 int static_memfuncp;
312
313 COERCE_REF (arg1);
314 COERCE_REF (arg2);
315 COERCE_ENUM (arg1);
316 COERCE_ENUM (arg2);
317
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. */
320
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 */
323
324 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4);
325 argvec[1] = value_addr (arg1);
326 argvec[2] = arg2;
327 argvec[3] = 0;
328
329 /* make the right function name up */
330 strcpy(tstr, "operator__");
331 ptr = tstr+8;
332 switch (op)
333 {
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:
350 switch (otherop)
351 {
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 */
361 default:
362 error ("Invalid binary operation specified.");
363 }
364 break;
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 */
373 default:
374 error ("Invalid binary operation specified.");
375 }
376
377 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
378
379 if (argvec[0])
380 {
381 if (static_memfuncp)
382 {
383 argvec[1] = argvec[0];
384 argvec++;
385 }
386 if (noside == EVAL_AVOID_SIDE_EFFECTS)
387 {
388 struct type *return_type;
389 return_type
390 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
391 return value_zero (return_type, VALUE_LVAL (arg1));
392 }
393 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
394 }
395 error ("member function %s not found", tstr);
396 #ifdef lint
397 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
398 #endif
399 }
400
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++). */
406
407 value_ptr
408 value_x_unop (arg1, op, noside)
409 value_ptr arg1;
410 enum exp_opcode op;
411 enum noside noside;
412 {
413 value_ptr * argvec;
414 char *ptr, *mangle_ptr;
415 char tstr[13], mangle_tstr[13];
416 int static_memfuncp;
417
418 COERCE_REF (arg1);
419 COERCE_ENUM (arg1);
420
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. */
423
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 */
426
427 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3);
428 argvec[1] = value_addr (arg1);
429 argvec[2] = 0;
430
431 /* make the right function name up */
432 strcpy(tstr,"operator__");
433 ptr = tstr+8;
434 strcpy(mangle_tstr, "__");
435 mangle_ptr = mangle_tstr+2;
436 switch (op)
437 {
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;
446 default:
447 error ("Invalid unary operation specified.");
448 }
449
450 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
451
452 if (argvec[0])
453 {
454 if (static_memfuncp)
455 {
456 argvec[1] = argvec[0];
457 argvec++;
458 }
459 if (noside == EVAL_AVOID_SIDE_EFFECTS)
460 {
461 struct type *return_type;
462 return_type
463 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
464 return value_zero (return_type, VALUE_LVAL (arg1));
465 }
466 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
467 }
468 error ("member function %s not found", tstr);
469 return 0; /* For lint -- never reached */
470 }
471
472 \f
473 /* Concatenate two values with the following conditions:
474
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.
478
479 or
480
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
484 value.
485
486
487 (2) Boolean values are also allowed and are treated as bit string
488 values of length 1.
489
490 (3) Character values are also allowed and are treated as character
491 string values of length 1.
492 */
493
494 value_ptr
495 value_concat (arg1, arg2)
496 value_ptr arg1, arg2;
497 {
498 register value_ptr inval1, inval2, outval;
499 int inval1len, inval2len;
500 int count, idx;
501 char *ptr;
502 char inchar;
503 struct type *type1 = check_typedef (VALUE_TYPE (arg1));
504 struct type *type2 = check_typedef (VALUE_TYPE (arg2));
505
506 COERCE_VARYING_ARRAY (arg1, type1);
507 COERCE_VARYING_ARRAY (arg2, type2);
508
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
513 repeated. */
514
515 if (TYPE_CODE (type2) == TYPE_CODE_INT)
516 {
517 struct type *tmp = type1;
518 type1 = tmp;
519 tmp = type2;
520 inval1 = arg2;
521 inval2 = arg1;
522 }
523 else
524 {
525 inval1 = arg1;
526 inval2 = arg2;
527 }
528
529 /* Now process the input values. */
530
531 if (TYPE_CODE (type1) == TYPE_CODE_INT)
532 {
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)
537 {
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)
542 {
543 inchar = (char) unpack_long (type2,
544 VALUE_CONTENTS (inval2));
545 for (idx = 0; idx < count; idx++)
546 {
547 *(ptr + idx) = inchar;
548 }
549 }
550 else
551 {
552 for (idx = 0; idx < count; idx++)
553 {
554 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
555 inval2len);
556 }
557 }
558 outval = value_string (ptr, count * inval2len);
559 }
560 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
561 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
562 {
563 error ("unimplemented support for bitstring/boolean repeats");
564 }
565 else
566 {
567 error ("can't repeat values of that type");
568 }
569 }
570 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
571 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
572 {
573 /* We have two character strings to concatenate. */
574 if (TYPE_CODE (type2) != TYPE_CODE_STRING
575 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
576 {
577 error ("Strings can only be concatenated with other strings.");
578 }
579 inval1len = TYPE_LENGTH (type1);
580 inval2len = TYPE_LENGTH (type2);
581 ptr = (char *) alloca (inval1len + inval2len);
582 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
583 {
584 *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1));
585 }
586 else
587 {
588 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
589 }
590 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
591 {
592 *(ptr + inval1len) =
593 (char) unpack_long (type2, VALUE_CONTENTS (inval2));
594 }
595 else
596 {
597 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
598 }
599 outval = value_string (ptr, inval1len + inval2len);
600 }
601 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
602 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
603 {
604 /* We have two bitstrings to concatenate. */
605 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
606 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
607 {
608 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
609 }
610 error ("unimplemented support for bitstring/boolean concatenation.");
611 }
612 else
613 {
614 /* We don't know how to concatenate these operands. */
615 error ("illegal operands for concatenation.");
616 }
617 return (outval);
618 }
619
620 \f
621
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. */
627
628 value_ptr
629 value_binop (arg1, arg2, op)
630 value_ptr arg1, arg2;
631 enum exp_opcode op;
632 {
633 register value_ptr val;
634 struct type *type1, *type2;
635
636 COERCE_REF (arg1);
637 COERCE_REF (arg2);
638 COERCE_ENUM (arg1);
639 COERCE_ENUM (arg2);
640 type1 = check_typedef (VALUE_TYPE (arg1));
641 type2 = check_typedef (VALUE_TYPE (arg2));
642
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)
648 ||
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.");
655
656 if (TYPE_CODE (type1) == TYPE_CODE_FLT
657 ||
658 TYPE_CODE (type2) == TYPE_CODE_FLT)
659 {
660 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
661 in target format. real.c in GCC probably has the necessary
662 code. */
663 DOUBLEST v1, v2, v;
664 v1 = value_as_double (arg1);
665 v2 = value_as_double (arg2);
666 switch (op)
667 {
668 case BINOP_ADD:
669 v = v1 + v2;
670 break;
671
672 case BINOP_SUB:
673 v = v1 - v2;
674 break;
675
676 case BINOP_MUL:
677 v = v1 * v2;
678 break;
679
680 case BINOP_DIV:
681 v = v1 / v2;
682 break;
683
684 default:
685 error ("Integer-only operation on floating point number.");
686 }
687
688 /* If either arg was long double, make sure that value is also long
689 double. */
690
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);
694 else
695 val = allocate_value (builtin_type_double);
696
697 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
698 v);
699 }
700 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
701 &&
702 TYPE_CODE (type2) == TYPE_CODE_BOOL)
703 {
704 LONGEST v1, v2, v;
705 v1 = value_as_long (arg1);
706 v2 = value_as_long (arg2);
707
708 switch (op)
709 {
710 case BINOP_BITWISE_AND:
711 v = v1 & v2;
712 break;
713
714 case BINOP_BITWISE_IOR:
715 v = v1 | v2;
716 break;
717
718 case BINOP_BITWISE_XOR:
719 v = v1 ^ v2;
720 break;
721
722 default:
723 error ("Invalid operation on booleans.");
724 }
725
726 val = allocate_value (type1);
727 store_signed_integer (VALUE_CONTENTS_RAW (val),
728 TYPE_LENGTH (type1),
729 v);
730 }
731 else
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? */
736 {
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;
743
744 /* Determine type length and signedness after promotion for
745 both operands. */
746 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
747 {
748 is_unsigned1 = 0;
749 promoted_len1 = TYPE_LENGTH (builtin_type_int);
750 }
751 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
752 {
753 is_unsigned2 = 0;
754 promoted_len2 = TYPE_LENGTH (builtin_type_int);
755 }
756
757 /* Determine type length of the result, and if the operation should
758 be done unsigned.
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)
763 {
764 unsigned_operation = is_unsigned1;
765 result_len = promoted_len1;
766 }
767 else if (promoted_len2 > promoted_len1)
768 {
769 unsigned_operation = is_unsigned2;
770 result_len = promoted_len2;
771 }
772 else
773 {
774 unsigned_operation = is_unsigned1 || is_unsigned2;
775 result_len = promoted_len1;
776 }
777
778 if (unsigned_operation)
779 {
780 ULONGEST v1, v2, v;
781 v1 = (ULONGEST) value_as_long (arg1);
782 v2 = (ULONGEST) value_as_long (arg2);
783
784 /* Truncate values to the type length of the result. */
785 if (result_len < sizeof (ULONGEST))
786 {
787 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
788 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
789 }
790
791 switch (op)
792 {
793 case BINOP_ADD:
794 v = v1 + v2;
795 break;
796
797 case BINOP_SUB:
798 v = v1 - v2;
799 break;
800
801 case BINOP_MUL:
802 v = v1 * v2;
803 break;
804
805 case BINOP_DIV:
806 v = v1 / v2;
807 break;
808
809 case BINOP_REM:
810 v = v1 % v2;
811 break;
812
813 case BINOP_MOD:
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)
819 {
820 error ("Second operand of MOD must be greater than zero.");
821 }
822 if (v2 == 0)
823 {
824 v = v1;
825 }
826 else
827 {
828 v = v1/v2;
829 /* Note floor(v1/v2) == v1/v2 for unsigned. */
830 v = v1 - (v2 * v);
831 }
832 break;
833
834 case BINOP_LSH:
835 v = v1 << v2;
836 break;
837
838 case BINOP_RSH:
839 v = v1 >> v2;
840 break;
841
842 case BINOP_BITWISE_AND:
843 v = v1 & v2;
844 break;
845
846 case BINOP_BITWISE_IOR:
847 v = v1 | v2;
848 break;
849
850 case BINOP_BITWISE_XOR:
851 v = v1 ^ v2;
852 break;
853
854 case BINOP_LOGICAL_AND:
855 v = v1 && v2;
856 break;
857
858 case BINOP_LOGICAL_OR:
859 v = v1 || v2;
860 break;
861
862 case BINOP_MIN:
863 v = v1 < v2 ? v1 : v2;
864 break;
865
866 case BINOP_MAX:
867 v = v1 > v2 ? v1 : v2;
868 break;
869
870 case BINOP_EQUAL:
871 v = v1 == v2;
872 break;
873
874 case BINOP_LESS:
875 v = v1 < v2;
876 break;
877
878 default:
879 error ("Invalid binary operation on numbers.");
880 }
881
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). */
888
889 /* Can't just call init_type because we wouldn't know what
890 name to give the type. */
891 val = allocate_value
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)),
897 v);
898 }
899 else
900 {
901 LONGEST v1, v2, v;
902 v1 = value_as_long (arg1);
903 v2 = value_as_long (arg2);
904
905 switch (op)
906 {
907 case BINOP_ADD:
908 v = v1 + v2;
909 break;
910
911 case BINOP_SUB:
912 v = v1 - v2;
913 break;
914
915 case BINOP_MUL:
916 v = v1 * v2;
917 break;
918
919 case BINOP_DIV:
920 v = v1 / v2;
921 break;
922
923 case BINOP_REM:
924 v = v1 % v2;
925 break;
926
927 case BINOP_MOD:
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
932 && v2 <= 0)
933 {
934 error ("Second operand of MOD must be greater than zero.");
935 }
936 if (v2 == 0)
937 {
938 v = v1;
939 }
940 else
941 {
942 v = v1/v2;
943 /* Compute floor. */
944 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
945 {
946 v--;
947 }
948 v = v1 - (v2 * v);
949 }
950 break;
951
952 case BINOP_LSH:
953 v = v1 << v2;
954 break;
955
956 case BINOP_RSH:
957 v = v1 >> v2;
958 break;
959
960 case BINOP_BITWISE_AND:
961 v = v1 & v2;
962 break;
963
964 case BINOP_BITWISE_IOR:
965 v = v1 | v2;
966 break;
967
968 case BINOP_BITWISE_XOR:
969 v = v1 ^ v2;
970 break;
971
972 case BINOP_LOGICAL_AND:
973 v = v1 && v2;
974 break;
975
976 case BINOP_LOGICAL_OR:
977 v = v1 || v2;
978 break;
979
980 case BINOP_MIN:
981 v = v1 < v2 ? v1 : v2;
982 break;
983
984 case BINOP_MAX:
985 v = v1 > v2 ? v1 : v2;
986 break;
987
988 case BINOP_EQUAL:
989 v = v1 == v2;
990 break;
991
992 case BINOP_LESS:
993 v = v1 < v2;
994 break;
995
996 default:
997 error ("Invalid binary operation on numbers.");
998 }
999
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). */
1006
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)),
1015 v);
1016 }
1017 }
1018
1019 return val;
1020 }
1021 \f
1022 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1023
1024 int
1025 value_logical_not (arg1)
1026 value_ptr arg1;
1027 {
1028 register int len;
1029 register char *p;
1030 struct type *type1;
1031
1032 COERCE_NUMBER (arg1);
1033 type1 = check_typedef (VALUE_TYPE (arg1));
1034
1035 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1036 return 0 == value_as_double (arg1);
1037
1038 len = TYPE_LENGTH (type1);
1039 p = VALUE_CONTENTS (arg1);
1040
1041 while (--len >= 0)
1042 {
1043 if (*p++)
1044 break;
1045 }
1046
1047 return len < 0;
1048 }
1049
1050 /* Simulate the C operator == by returning a 1
1051 iff ARG1 and ARG2 have equal contents. */
1052
1053 int
1054 value_equal (arg1, arg2)
1055 register value_ptr arg1, arg2;
1056
1057 {
1058 register int len;
1059 register char *p1, *p2;
1060 struct type *type1, *type2;
1061 enum type_code code1;
1062 enum type_code code2;
1063
1064 COERCE_NUMBER (arg1);
1065 COERCE_NUMBER (arg2);
1066
1067 type1 = check_typedef (VALUE_TYPE (arg1));
1068 type2 = check_typedef (VALUE_TYPE (arg2));
1069 code1 = TYPE_CODE (type1);
1070 code2 = TYPE_CODE (type2);
1071
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,
1075 BINOP_EQUAL)));
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);
1079
1080 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1081 is bigger. */
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);
1086
1087 else if (code1 == code2
1088 && ((len = (int) TYPE_LENGTH (type1))
1089 == (int) TYPE_LENGTH (type2)))
1090 {
1091 p1 = VALUE_CONTENTS (arg1);
1092 p2 = VALUE_CONTENTS (arg2);
1093 while (--len >= 0)
1094 {
1095 if (*p1++ != *p2++)
1096 break;
1097 }
1098 return len < 0;
1099 }
1100 else
1101 {
1102 error ("Invalid type combination in equality test.");
1103 return 0; /* For lint -- never reached */
1104 }
1105 }
1106
1107 /* Simulate the C operator < by returning 1
1108 iff ARG1's contents are less than ARG2's. */
1109
1110 int
1111 value_less (arg1, arg2)
1112 register value_ptr arg1, arg2;
1113 {
1114 register enum type_code code1;
1115 register enum type_code code2;
1116 struct type *type1, *type2;
1117
1118 COERCE_NUMBER (arg1);
1119 COERCE_NUMBER (arg2);
1120
1121 type1 = check_typedef (VALUE_TYPE (arg1));
1122 type2 = check_typedef (VALUE_TYPE (arg2));
1123 code1 = TYPE_CODE (type1);
1124 code2 = TYPE_CODE (type2);
1125
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,
1129 BINOP_LESS)));
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);
1135
1136 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1137 is bigger. */
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);
1142
1143 else
1144 {
1145 error ("Invalid type combination in ordering comparison.");
1146 return 0;
1147 }
1148 }
1149 \f
1150 /* The unary operators - and ~. Both free the argument ARG1. */
1151
1152 value_ptr
1153 value_neg (arg1)
1154 register value_ptr arg1;
1155 {
1156 register struct type *type;
1157 register struct type *result_type = VALUE_TYPE (arg1);
1158
1159 COERCE_REF (arg1);
1160 COERCE_ENUM (arg1);
1161
1162 type = check_typedef (VALUE_TYPE (arg1));
1163
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)
1167 {
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;
1172
1173 return value_from_longest (result_type, - value_as_long (arg1));
1174 }
1175 else {
1176 error ("Argument to negate operation not a number.");
1177 return 0; /* For lint -- never reached */
1178 }
1179 }
1180
1181 value_ptr
1182 value_complement (arg1)
1183 register value_ptr arg1;
1184 {
1185 register struct type *type;
1186 register struct type *result_type = VALUE_TYPE (arg1);
1187 int typecode;
1188
1189 COERCE_REF (arg1);
1190 COERCE_ENUM (arg1);
1191
1192 type = check_typedef (VALUE_TYPE (arg1));
1193
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.");
1197
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;
1202
1203 return value_from_longest (result_type, ~ value_as_long (arg1));
1204 }
1205 \f
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. */
1209
1210 int
1211 value_bit_index (type, valaddr, index)
1212 struct type *type;
1213 char *valaddr;
1214 int index;
1215 {
1216 LONGEST low_bound, high_bound;
1217 LONGEST word;
1218 unsigned rel_index;
1219 struct type *range = TYPE_FIELD_TYPE (type, 0);
1220 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1221 return -2;
1222 if (index < low_bound || index > high_bound)
1223 return -1;
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;
1231 }
1232
1233 value_ptr
1234 value_in (element, set)
1235 value_ptr element, set;
1236 {
1237 int member;
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));
1251 if (member < 0)
1252 error ("First argument of 'IN' not in range");
1253 return value_from_longest (LA_BOOL_TYPE, member);
1254 }
1255
1256 void
1257 _initialize_valarith ()
1258 {
1259 }