* ax-gdb.c (gen_expr): Add UNOP_PLUS case.
[deliverable/binutils-gdb.git] / gdb / valarith.c
1 /* Perform arithmetic and other operations on values, for GDB.
2
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free
5 Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "value.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "target.h"
30 #include "language.h"
31 #include "gdb_string.h"
32 #include "doublest.h"
33 #include <math.h>
34 #include "infcall.h"
35
36 /* Define whether or not the C operator '/' truncates towards zero for
37 differently signed operands (truncation direction is undefined in C). */
38
39 #ifndef TRUNCATION_TOWARDS_ZERO
40 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
41 #endif
42
43 static struct value *value_subscripted_rvalue (struct value *, struct value *, int);
44
45 void _initialize_valarith (void);
46 \f
47
48 /* Given a pointer, return the size of its target.
49 If the pointer type is void *, then return 1.
50 If the target type is incomplete, then error out.
51 This isn't a general purpose function, but just a
52 helper for value_sub & value_add.
53 */
54
55 static LONGEST
56 find_size_for_pointer_math (struct type *ptr_type)
57 {
58 LONGEST sz = -1;
59 struct type *ptr_target;
60
61 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
62
63 sz = TYPE_LENGTH (ptr_target);
64 if (sz == 0)
65 {
66 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
67 sz = 1;
68 else
69 {
70 char *name;
71
72 name = TYPE_NAME (ptr_target);
73 if (name == NULL)
74 name = TYPE_TAG_NAME (ptr_target);
75 if (name == NULL)
76 error (_("Cannot perform pointer math on incomplete types, "
77 "try casting to a known type, or void *."));
78 else
79 error (_("Cannot perform pointer math on incomplete type \"%s\", "
80 "try casting to a known type, or void *."), name);
81 }
82 }
83 return sz;
84 }
85
86 struct value *
87 value_add (struct value *arg1, struct value *arg2)
88 {
89 struct value *valint;
90 struct value *valptr;
91 LONGEST sz;
92 struct type *type1, *type2, *valptrtype;
93
94 arg1 = coerce_array (arg1);
95 arg2 = coerce_array (arg2);
96 type1 = check_typedef (value_type (arg1));
97 type2 = check_typedef (value_type (arg2));
98
99 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
100 || TYPE_CODE (type2) == TYPE_CODE_PTR)
101 &&
102 (is_integral_type (type1) || is_integral_type (type2)))
103 /* Exactly one argument is a pointer, and one is an integer. */
104 {
105 struct value *retval;
106
107 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
108 {
109 valptr = arg1;
110 valint = arg2;
111 valptrtype = type1;
112 }
113 else
114 {
115 valptr = arg2;
116 valint = arg1;
117 valptrtype = type2;
118 }
119
120 sz = find_size_for_pointer_math (valptrtype);
121
122 retval = value_from_pointer (valptrtype,
123 value_as_address (valptr)
124 + (sz * value_as_long (valint)));
125 return retval;
126 }
127
128 return value_binop (arg1, arg2, BINOP_ADD);
129 }
130
131 struct value *
132 value_sub (struct value *arg1, struct value *arg2)
133 {
134 struct type *type1, *type2;
135 arg1 = coerce_array (arg1);
136 arg2 = coerce_array (arg2);
137 type1 = check_typedef (value_type (arg1));
138 type2 = check_typedef (value_type (arg2));
139
140 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
141 {
142 if (is_integral_type (type2))
143 {
144 /* pointer - integer. */
145 LONGEST sz = find_size_for_pointer_math (type1);
146
147 return value_from_pointer (type1,
148 (value_as_address (arg1)
149 - (sz * value_as_long (arg2))));
150 }
151 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
152 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
153 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
154 {
155 /* pointer to <type x> - pointer to <type x>. */
156 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
157 return value_from_longest
158 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
159 (value_as_long (arg1) - value_as_long (arg2)) / sz);
160 }
161 else
162 {
163 error (_("\
164 First argument of `-' is a pointer and second argument is neither\n\
165 an integer nor a pointer of the same type."));
166 }
167 }
168
169 return value_binop (arg1, arg2, BINOP_SUB);
170 }
171
172 /* Return the value of ARRAY[IDX].
173 See comments in value_coerce_array() for rationale for reason for
174 doing lower bounds adjustment here rather than there.
175 FIXME: Perhaps we should validate that the index is valid and if
176 verbosity is set, warn about invalid indices (but still use them). */
177
178 struct value *
179 value_subscript (struct value *array, struct value *idx)
180 {
181 struct value *bound;
182 int c_style = current_language->c_style_arrays;
183 struct type *tarray;
184
185 array = coerce_ref (array);
186 tarray = check_typedef (value_type (array));
187
188 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
189 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
190 {
191 struct type *range_type = TYPE_INDEX_TYPE (tarray);
192 LONGEST lowerbound, upperbound;
193 get_discrete_bounds (range_type, &lowerbound, &upperbound);
194
195 if (VALUE_LVAL (array) != lval_memory)
196 return value_subscripted_rvalue (array, idx, lowerbound);
197
198 if (c_style == 0)
199 {
200 LONGEST index = value_as_long (idx);
201 if (index >= lowerbound && index <= upperbound)
202 return value_subscripted_rvalue (array, idx, lowerbound);
203 /* Emit warning unless we have an array of unknown size.
204 An array of unknown size has lowerbound 0 and upperbound -1. */
205 if (upperbound > -1)
206 warning (_("array or string index out of range"));
207 /* fall doing C stuff */
208 c_style = 1;
209 }
210
211 if (lowerbound != 0)
212 {
213 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
214 idx = value_sub (idx, bound);
215 }
216
217 array = value_coerce_array (array);
218 }
219
220 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
221 {
222 struct type *range_type = TYPE_INDEX_TYPE (tarray);
223 LONGEST index = value_as_long (idx);
224 struct value *v;
225 int offset, byte, bit_index;
226 LONGEST lowerbound, upperbound;
227 get_discrete_bounds (range_type, &lowerbound, &upperbound);
228 if (index < lowerbound || index > upperbound)
229 error (_("bitstring index out of range"));
230 index -= lowerbound;
231 offset = index / TARGET_CHAR_BIT;
232 byte = *((char *) value_contents (array) + offset);
233 bit_index = index % TARGET_CHAR_BIT;
234 byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index);
235 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
236 set_value_bitpos (v, bit_index);
237 set_value_bitsize (v, 1);
238 VALUE_LVAL (v) = VALUE_LVAL (array);
239 if (VALUE_LVAL (array) == lval_internalvar)
240 VALUE_LVAL (v) = lval_internalvar_component;
241 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
242 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
243 set_value_offset (v, offset + value_offset (array));
244 return v;
245 }
246
247 if (c_style)
248 return value_ind (value_add (array, idx));
249 else
250 error (_("not an array or string"));
251 }
252
253 /* Return the value of EXPR[IDX], expr an aggregate rvalue
254 (eg, a vector register). This routine used to promote floats
255 to doubles, but no longer does. */
256
257 static struct value *
258 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
259 {
260 struct type *array_type = check_typedef (value_type (array));
261 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
262 unsigned int elt_size = TYPE_LENGTH (elt_type);
263 LONGEST index = value_as_long (idx);
264 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
265 struct value *v;
266
267 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
268 error (_("no such vector element"));
269
270 v = allocate_value (elt_type);
271 if (value_lazy (array))
272 set_value_lazy (v, 1);
273 else
274 memcpy (value_contents_writeable (v),
275 value_contents (array) + elt_offs, elt_size);
276
277 if (VALUE_LVAL (array) == lval_internalvar)
278 VALUE_LVAL (v) = lval_internalvar_component;
279 else
280 VALUE_LVAL (v) = VALUE_LVAL (array);
281 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
282 VALUE_REGNUM (v) = VALUE_REGNUM (array);
283 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
284 set_value_offset (v, value_offset (array) + elt_offs);
285 return v;
286 }
287 \f
288 /* Check to see if either argument is a structure. This is called so
289 we know whether to go ahead with the normal binop or look for a
290 user defined function instead.
291
292 For now, we do not overload the `=' operator. */
293
294 int
295 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
296 {
297 struct type *type1, *type2;
298 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
299 return 0;
300 type1 = check_typedef (value_type (arg1));
301 type2 = check_typedef (value_type (arg2));
302 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
303 || TYPE_CODE (type2) == TYPE_CODE_STRUCT
304 || (TYPE_CODE (type1) == TYPE_CODE_REF
305 && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT)
306 || (TYPE_CODE (type2) == TYPE_CODE_REF
307 && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT));
308 }
309
310 /* Check to see if argument is a structure. This is called so
311 we know whether to go ahead with the normal unop or look for a
312 user defined function instead.
313
314 For now, we do not overload the `&' operator. */
315
316 int
317 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
318 {
319 struct type *type1;
320 if (op == UNOP_ADDR)
321 return 0;
322 type1 = check_typedef (value_type (arg1));
323 for (;;)
324 {
325 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
326 return 1;
327 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
328 type1 = TYPE_TARGET_TYPE (type1);
329 else
330 return 0;
331 }
332 }
333
334 /* We know either arg1 or arg2 is a structure, so try to find the right
335 user defined function. Create an argument vector that calls
336 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
337 binary operator which is legal for GNU C++).
338
339 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
340 is the opcode saying how to modify it. Otherwise, OTHEROP is
341 unused. */
342
343 struct value *
344 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
345 enum exp_opcode otherop, enum noside noside)
346 {
347 struct value **argvec;
348 char *ptr;
349 char tstr[13];
350 int static_memfuncp;
351
352 arg1 = coerce_ref (arg1);
353 arg2 = coerce_ref (arg2);
354 arg1 = coerce_enum (arg1);
355 arg2 = coerce_enum (arg2);
356
357 /* now we know that what we have to do is construct our
358 arg vector and find the right function to call it with. */
359
360 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
361 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
362
363 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
364 argvec[1] = value_addr (arg1);
365 argvec[2] = arg2;
366 argvec[3] = 0;
367
368 /* make the right function name up */
369 strcpy (tstr, "operator__");
370 ptr = tstr + 8;
371 switch (op)
372 {
373 case BINOP_ADD:
374 strcpy (ptr, "+");
375 break;
376 case BINOP_SUB:
377 strcpy (ptr, "-");
378 break;
379 case BINOP_MUL:
380 strcpy (ptr, "*");
381 break;
382 case BINOP_DIV:
383 strcpy (ptr, "/");
384 break;
385 case BINOP_REM:
386 strcpy (ptr, "%");
387 break;
388 case BINOP_LSH:
389 strcpy (ptr, "<<");
390 break;
391 case BINOP_RSH:
392 strcpy (ptr, ">>");
393 break;
394 case BINOP_BITWISE_AND:
395 strcpy (ptr, "&");
396 break;
397 case BINOP_BITWISE_IOR:
398 strcpy (ptr, "|");
399 break;
400 case BINOP_BITWISE_XOR:
401 strcpy (ptr, "^");
402 break;
403 case BINOP_LOGICAL_AND:
404 strcpy (ptr, "&&");
405 break;
406 case BINOP_LOGICAL_OR:
407 strcpy (ptr, "||");
408 break;
409 case BINOP_MIN:
410 strcpy (ptr, "<?");
411 break;
412 case BINOP_MAX:
413 strcpy (ptr, ">?");
414 break;
415 case BINOP_ASSIGN:
416 strcpy (ptr, "=");
417 break;
418 case BINOP_ASSIGN_MODIFY:
419 switch (otherop)
420 {
421 case BINOP_ADD:
422 strcpy (ptr, "+=");
423 break;
424 case BINOP_SUB:
425 strcpy (ptr, "-=");
426 break;
427 case BINOP_MUL:
428 strcpy (ptr, "*=");
429 break;
430 case BINOP_DIV:
431 strcpy (ptr, "/=");
432 break;
433 case BINOP_REM:
434 strcpy (ptr, "%=");
435 break;
436 case BINOP_BITWISE_AND:
437 strcpy (ptr, "&=");
438 break;
439 case BINOP_BITWISE_IOR:
440 strcpy (ptr, "|=");
441 break;
442 case BINOP_BITWISE_XOR:
443 strcpy (ptr, "^=");
444 break;
445 case BINOP_MOD: /* invalid */
446 default:
447 error (_("Invalid binary operation specified."));
448 }
449 break;
450 case BINOP_SUBSCRIPT:
451 strcpy (ptr, "[]");
452 break;
453 case BINOP_EQUAL:
454 strcpy (ptr, "==");
455 break;
456 case BINOP_NOTEQUAL:
457 strcpy (ptr, "!=");
458 break;
459 case BINOP_LESS:
460 strcpy (ptr, "<");
461 break;
462 case BINOP_GTR:
463 strcpy (ptr, ">");
464 break;
465 case BINOP_GEQ:
466 strcpy (ptr, ">=");
467 break;
468 case BINOP_LEQ:
469 strcpy (ptr, "<=");
470 break;
471 case BINOP_MOD: /* invalid */
472 default:
473 error (_("Invalid binary operation specified."));
474 }
475
476 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
477
478 if (argvec[0])
479 {
480 if (static_memfuncp)
481 {
482 argvec[1] = argvec[0];
483 argvec++;
484 }
485 if (noside == EVAL_AVOID_SIDE_EFFECTS)
486 {
487 struct type *return_type;
488 return_type
489 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
490 return value_zero (return_type, VALUE_LVAL (arg1));
491 }
492 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
493 }
494 error (_("member function %s not found"), tstr);
495 #ifdef lint
496 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
497 #endif
498 }
499
500 /* We know that arg1 is a structure, so try to find a unary user
501 defined operator that matches the operator in question.
502 Create an argument vector that calls arg1.operator @ (arg1)
503 and return that value (where '@' is (almost) any unary operator which
504 is legal for GNU C++). */
505
506 struct value *
507 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
508 {
509 struct value **argvec;
510 char *ptr, *mangle_ptr;
511 char tstr[13], mangle_tstr[13];
512 int static_memfuncp, nargs;
513
514 arg1 = coerce_ref (arg1);
515 arg1 = coerce_enum (arg1);
516
517 /* now we know that what we have to do is construct our
518 arg vector and find the right function to call it with. */
519
520 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
521 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
522
523 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
524 argvec[1] = value_addr (arg1);
525 argvec[2] = 0;
526
527 nargs = 1;
528
529 /* make the right function name up */
530 strcpy (tstr, "operator__");
531 ptr = tstr + 8;
532 strcpy (mangle_tstr, "__");
533 mangle_ptr = mangle_tstr + 2;
534 switch (op)
535 {
536 case UNOP_PREINCREMENT:
537 strcpy (ptr, "++");
538 break;
539 case UNOP_PREDECREMENT:
540 strcpy (ptr, "--");
541 break;
542 case UNOP_POSTINCREMENT:
543 strcpy (ptr, "++");
544 argvec[2] = value_from_longest (builtin_type_int, 0);
545 argvec[3] = 0;
546 nargs ++;
547 break;
548 case UNOP_POSTDECREMENT:
549 strcpy (ptr, "--");
550 argvec[2] = value_from_longest (builtin_type_int, 0);
551 argvec[3] = 0;
552 nargs ++;
553 break;
554 case UNOP_LOGICAL_NOT:
555 strcpy (ptr, "!");
556 break;
557 case UNOP_COMPLEMENT:
558 strcpy (ptr, "~");
559 break;
560 case UNOP_NEG:
561 strcpy (ptr, "-");
562 break;
563 case UNOP_PLUS:
564 strcpy (ptr, "+");
565 break;
566 case UNOP_IND:
567 strcpy (ptr, "*");
568 break;
569 default:
570 error (_("Invalid unary operation specified."));
571 }
572
573 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
574
575 if (argvec[0])
576 {
577 if (static_memfuncp)
578 {
579 argvec[1] = argvec[0];
580 nargs --;
581 argvec++;
582 }
583 if (noside == EVAL_AVOID_SIDE_EFFECTS)
584 {
585 struct type *return_type;
586 return_type
587 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
588 return value_zero (return_type, VALUE_LVAL (arg1));
589 }
590 return call_function_by_hand (argvec[0], nargs, argvec + 1);
591 }
592 error (_("member function %s not found"), tstr);
593 return 0; /* For lint -- never reached */
594 }
595 \f
596
597 /* Concatenate two values with the following conditions:
598
599 (1) Both values must be either bitstring values or character string
600 values and the resulting value consists of the concatenation of
601 ARG1 followed by ARG2.
602
603 or
604
605 One value must be an integer value and the other value must be
606 either a bitstring value or character string value, which is
607 to be repeated by the number of times specified by the integer
608 value.
609
610
611 (2) Boolean values are also allowed and are treated as bit string
612 values of length 1.
613
614 (3) Character values are also allowed and are treated as character
615 string values of length 1.
616 */
617
618 struct value *
619 value_concat (struct value *arg1, struct value *arg2)
620 {
621 struct value *inval1;
622 struct value *inval2;
623 struct value *outval = NULL;
624 int inval1len, inval2len;
625 int count, idx;
626 char *ptr;
627 char inchar;
628 struct type *type1 = check_typedef (value_type (arg1));
629 struct type *type2 = check_typedef (value_type (arg2));
630
631 /* First figure out if we are dealing with two values to be concatenated
632 or a repeat count and a value to be repeated. INVAL1 is set to the
633 first of two concatenated values, or the repeat count. INVAL2 is set
634 to the second of the two concatenated values or the value to be
635 repeated. */
636
637 if (TYPE_CODE (type2) == TYPE_CODE_INT)
638 {
639 struct type *tmp = type1;
640 type1 = tmp;
641 tmp = type2;
642 inval1 = arg2;
643 inval2 = arg1;
644 }
645 else
646 {
647 inval1 = arg1;
648 inval2 = arg2;
649 }
650
651 /* Now process the input values. */
652
653 if (TYPE_CODE (type1) == TYPE_CODE_INT)
654 {
655 /* We have a repeat count. Validate the second value and then
656 construct a value repeated that many times. */
657 if (TYPE_CODE (type2) == TYPE_CODE_STRING
658 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
659 {
660 count = longest_to_int (value_as_long (inval1));
661 inval2len = TYPE_LENGTH (type2);
662 ptr = (char *) alloca (count * inval2len);
663 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
664 {
665 inchar = (char) unpack_long (type2,
666 value_contents (inval2));
667 for (idx = 0; idx < count; idx++)
668 {
669 *(ptr + idx) = inchar;
670 }
671 }
672 else
673 {
674 for (idx = 0; idx < count; idx++)
675 {
676 memcpy (ptr + (idx * inval2len), value_contents (inval2),
677 inval2len);
678 }
679 }
680 outval = value_string (ptr, count * inval2len);
681 }
682 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
683 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
684 {
685 error (_("unimplemented support for bitstring/boolean repeats"));
686 }
687 else
688 {
689 error (_("can't repeat values of that type"));
690 }
691 }
692 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
693 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
694 {
695 /* We have two character strings to concatenate. */
696 if (TYPE_CODE (type2) != TYPE_CODE_STRING
697 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
698 {
699 error (_("Strings can only be concatenated with other strings."));
700 }
701 inval1len = TYPE_LENGTH (type1);
702 inval2len = TYPE_LENGTH (type2);
703 ptr = (char *) alloca (inval1len + inval2len);
704 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
705 {
706 *ptr = (char) unpack_long (type1, value_contents (inval1));
707 }
708 else
709 {
710 memcpy (ptr, value_contents (inval1), inval1len);
711 }
712 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
713 {
714 *(ptr + inval1len) =
715 (char) unpack_long (type2, value_contents (inval2));
716 }
717 else
718 {
719 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
720 }
721 outval = value_string (ptr, inval1len + inval2len);
722 }
723 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
724 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
725 {
726 /* We have two bitstrings to concatenate. */
727 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
728 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
729 {
730 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
731 }
732 error (_("unimplemented support for bitstring/boolean concatenation."));
733 }
734 else
735 {
736 /* We don't know how to concatenate these operands. */
737 error (_("illegal operands for concatenation."));
738 }
739 return (outval);
740 }
741 \f
742
743
744 /* Perform a binary operation on two operands which have reasonable
745 representations as integers or floats. This includes booleans,
746 characters, integers, or floats.
747 Does not support addition and subtraction on pointers;
748 use value_add or value_sub if you want to handle those possibilities. */
749
750 struct value *
751 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
752 {
753 struct value *val;
754 struct type *type1, *type2;
755
756 arg1 = coerce_ref (arg1);
757 arg2 = coerce_ref (arg2);
758 type1 = check_typedef (value_type (arg1));
759 type2 = check_typedef (value_type (arg2));
760
761 if ((TYPE_CODE (type1) != TYPE_CODE_FLT && !is_integral_type (type1))
762 ||
763 (TYPE_CODE (type2) != TYPE_CODE_FLT && !is_integral_type (type2)))
764 error (_("Argument to arithmetic operation not a number or boolean."));
765
766 if (TYPE_CODE (type1) == TYPE_CODE_FLT
767 ||
768 TYPE_CODE (type2) == TYPE_CODE_FLT)
769 {
770 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
771 in target format. real.c in GCC probably has the necessary
772 code. */
773 DOUBLEST v1, v2, v = 0;
774 v1 = value_as_double (arg1);
775 v2 = value_as_double (arg2);
776 switch (op)
777 {
778 case BINOP_ADD:
779 v = v1 + v2;
780 break;
781
782 case BINOP_SUB:
783 v = v1 - v2;
784 break;
785
786 case BINOP_MUL:
787 v = v1 * v2;
788 break;
789
790 case BINOP_DIV:
791 v = v1 / v2;
792 break;
793
794 case BINOP_EXP:
795 v = pow (v1, v2);
796 if (errno)
797 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
798 break;
799
800 default:
801 error (_("Integer-only operation on floating point number."));
802 }
803
804 /* If either arg was long double, make sure that value is also long
805 double. */
806
807 if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT
808 || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT)
809 val = allocate_value (builtin_type_long_double);
810 else
811 val = allocate_value (builtin_type_double);
812
813 store_typed_floating (value_contents_raw (val), value_type (val), v);
814 }
815 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
816 &&
817 TYPE_CODE (type2) == TYPE_CODE_BOOL)
818 {
819 LONGEST v1, v2, v = 0;
820 v1 = value_as_long (arg1);
821 v2 = value_as_long (arg2);
822
823 switch (op)
824 {
825 case BINOP_BITWISE_AND:
826 v = v1 & v2;
827 break;
828
829 case BINOP_BITWISE_IOR:
830 v = v1 | v2;
831 break;
832
833 case BINOP_BITWISE_XOR:
834 v = v1 ^ v2;
835 break;
836
837 case BINOP_EQUAL:
838 v = v1 == v2;
839 break;
840
841 case BINOP_NOTEQUAL:
842 v = v1 != v2;
843 break;
844
845 default:
846 error (_("Invalid operation on booleans."));
847 }
848
849 val = allocate_value (type1);
850 store_signed_integer (value_contents_raw (val),
851 TYPE_LENGTH (type1),
852 v);
853 }
854 else
855 /* Integral operations here. */
856 /* FIXME: Also mixed integral/booleans, with result an integer. */
857 /* FIXME: This implements ANSI C rules (also correct for C++).
858 What about FORTRAN and (the deleted) chill ? */
859 {
860 unsigned int promoted_len1 = TYPE_LENGTH (type1);
861 unsigned int promoted_len2 = TYPE_LENGTH (type2);
862 int is_unsigned1 = TYPE_UNSIGNED (type1);
863 int is_unsigned2 = TYPE_UNSIGNED (type2);
864 unsigned int result_len;
865 int unsigned_operation;
866
867 /* Determine type length and signedness after promotion for
868 both operands. */
869 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
870 {
871 is_unsigned1 = 0;
872 promoted_len1 = TYPE_LENGTH (builtin_type_int);
873 }
874 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
875 {
876 is_unsigned2 = 0;
877 promoted_len2 = TYPE_LENGTH (builtin_type_int);
878 }
879
880 /* Determine type length of the result, and if the operation should
881 be done unsigned.
882 Use the signedness of the operand with the greater length.
883 If both operands are of equal length, use unsigned operation
884 if one of the operands is unsigned. */
885 if (promoted_len1 > promoted_len2)
886 {
887 unsigned_operation = is_unsigned1;
888 result_len = promoted_len1;
889 }
890 else if (promoted_len2 > promoted_len1)
891 {
892 unsigned_operation = is_unsigned2;
893 result_len = promoted_len2;
894 }
895 else
896 {
897 unsigned_operation = is_unsigned1 || is_unsigned2;
898 result_len = promoted_len1;
899 }
900
901 if (unsigned_operation)
902 {
903 ULONGEST v1, v2, v = 0;
904 v1 = (ULONGEST) value_as_long (arg1);
905 v2 = (ULONGEST) value_as_long (arg2);
906
907 /* Truncate values to the type length of the result. */
908 if (result_len < sizeof (ULONGEST))
909 {
910 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
911 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
912 }
913
914 switch (op)
915 {
916 case BINOP_ADD:
917 v = v1 + v2;
918 break;
919
920 case BINOP_SUB:
921 v = v1 - v2;
922 break;
923
924 case BINOP_MUL:
925 v = v1 * v2;
926 break;
927
928 case BINOP_DIV:
929 v = v1 / v2;
930 break;
931
932 case BINOP_EXP:
933 v = pow (v1, v2);
934 if (errno)
935 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
936 break;
937
938 case BINOP_REM:
939 v = v1 % v2;
940 break;
941
942 case BINOP_MOD:
943 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
944 v1 mod 0 has a defined value, v1. */
945 if (v2 == 0)
946 {
947 v = v1;
948 }
949 else
950 {
951 v = v1 / v2;
952 /* Note floor(v1/v2) == v1/v2 for unsigned. */
953 v = v1 - (v2 * v);
954 }
955 break;
956
957 case BINOP_LSH:
958 v = v1 << v2;
959 break;
960
961 case BINOP_RSH:
962 v = v1 >> v2;
963 break;
964
965 case BINOP_BITWISE_AND:
966 v = v1 & v2;
967 break;
968
969 case BINOP_BITWISE_IOR:
970 v = v1 | v2;
971 break;
972
973 case BINOP_BITWISE_XOR:
974 v = v1 ^ v2;
975 break;
976
977 case BINOP_LOGICAL_AND:
978 v = v1 && v2;
979 break;
980
981 case BINOP_LOGICAL_OR:
982 v = v1 || v2;
983 break;
984
985 case BINOP_MIN:
986 v = v1 < v2 ? v1 : v2;
987 break;
988
989 case BINOP_MAX:
990 v = v1 > v2 ? v1 : v2;
991 break;
992
993 case BINOP_EQUAL:
994 v = v1 == v2;
995 break;
996
997 case BINOP_NOTEQUAL:
998 v = v1 != v2;
999 break;
1000
1001 case BINOP_LESS:
1002 v = v1 < v2;
1003 break;
1004
1005 default:
1006 error (_("Invalid binary operation on numbers."));
1007 }
1008
1009 /* This is a kludge to get around the fact that we don't
1010 know how to determine the result type from the types of
1011 the operands. (I'm not really sure how much we feel the
1012 need to duplicate the exact rules of the current
1013 language. They can get really hairy. But not to do so
1014 makes it hard to document just what we *do* do). */
1015
1016 /* Can't just call init_type because we wouldn't know what
1017 name to give the type. */
1018 val = allocate_value
1019 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1020 ? builtin_type_unsigned_long_long
1021 : builtin_type_unsigned_long);
1022 store_unsigned_integer (value_contents_raw (val),
1023 TYPE_LENGTH (value_type (val)),
1024 v);
1025 }
1026 else
1027 {
1028 LONGEST v1, v2, v = 0;
1029 v1 = value_as_long (arg1);
1030 v2 = value_as_long (arg2);
1031
1032 switch (op)
1033 {
1034 case BINOP_ADD:
1035 v = v1 + v2;
1036 break;
1037
1038 case BINOP_SUB:
1039 v = v1 - v2;
1040 break;
1041
1042 case BINOP_MUL:
1043 v = v1 * v2;
1044 break;
1045
1046 case BINOP_DIV:
1047 if (v2 != 0)
1048 v = v1 / v2;
1049 else
1050 error (_("Division by zero"));
1051 break;
1052
1053 case BINOP_EXP:
1054 v = pow (v1, v2);
1055 if (errno)
1056 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
1057 break;
1058
1059 case BINOP_REM:
1060 if (v2 != 0)
1061 v = v1 % v2;
1062 else
1063 error (_("Division by zero"));
1064 break;
1065
1066 case BINOP_MOD:
1067 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1068 X mod 0 has a defined value, X. */
1069 if (v2 == 0)
1070 {
1071 v = v1;
1072 }
1073 else
1074 {
1075 v = v1 / v2;
1076 /* Compute floor. */
1077 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1078 {
1079 v--;
1080 }
1081 v = v1 - (v2 * v);
1082 }
1083 break;
1084
1085 case BINOP_LSH:
1086 v = v1 << v2;
1087 break;
1088
1089 case BINOP_RSH:
1090 v = v1 >> v2;
1091 break;
1092
1093 case BINOP_BITWISE_AND:
1094 v = v1 & v2;
1095 break;
1096
1097 case BINOP_BITWISE_IOR:
1098 v = v1 | v2;
1099 break;
1100
1101 case BINOP_BITWISE_XOR:
1102 v = v1 ^ v2;
1103 break;
1104
1105 case BINOP_LOGICAL_AND:
1106 v = v1 && v2;
1107 break;
1108
1109 case BINOP_LOGICAL_OR:
1110 v = v1 || v2;
1111 break;
1112
1113 case BINOP_MIN:
1114 v = v1 < v2 ? v1 : v2;
1115 break;
1116
1117 case BINOP_MAX:
1118 v = v1 > v2 ? v1 : v2;
1119 break;
1120
1121 case BINOP_EQUAL:
1122 v = v1 == v2;
1123 break;
1124
1125 case BINOP_LESS:
1126 v = v1 < v2;
1127 break;
1128
1129 default:
1130 error (_("Invalid binary operation on numbers."));
1131 }
1132
1133 /* This is a kludge to get around the fact that we don't
1134 know how to determine the result type from the types of
1135 the operands. (I'm not really sure how much we feel the
1136 need to duplicate the exact rules of the current
1137 language. They can get really hairy. But not to do so
1138 makes it hard to document just what we *do* do). */
1139
1140 /* Can't just call init_type because we wouldn't know what
1141 name to give the type. */
1142 val = allocate_value
1143 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1144 ? builtin_type_long_long
1145 : builtin_type_long);
1146 store_signed_integer (value_contents_raw (val),
1147 TYPE_LENGTH (value_type (val)),
1148 v);
1149 }
1150 }
1151
1152 return val;
1153 }
1154 \f
1155 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1156
1157 int
1158 value_logical_not (struct value *arg1)
1159 {
1160 int len;
1161 const bfd_byte *p;
1162 struct type *type1;
1163
1164 arg1 = coerce_number (arg1);
1165 type1 = check_typedef (value_type (arg1));
1166
1167 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1168 return 0 == value_as_double (arg1);
1169
1170 len = TYPE_LENGTH (type1);
1171 p = value_contents (arg1);
1172
1173 while (--len >= 0)
1174 {
1175 if (*p++)
1176 break;
1177 }
1178
1179 return len < 0;
1180 }
1181
1182 /* Perform a comparison on two string values (whose content are not
1183 necessarily null terminated) based on their length */
1184
1185 static int
1186 value_strcmp (struct value *arg1, struct value *arg2)
1187 {
1188 int len1 = TYPE_LENGTH (value_type (arg1));
1189 int len2 = TYPE_LENGTH (value_type (arg2));
1190 const bfd_byte *s1 = value_contents (arg1);
1191 const bfd_byte *s2 = value_contents (arg2);
1192 int i, len = len1 < len2 ? len1 : len2;
1193
1194 for (i = 0; i < len; i++)
1195 {
1196 if (s1[i] < s2[i])
1197 return -1;
1198 else if (s1[i] > s2[i])
1199 return 1;
1200 else
1201 continue;
1202 }
1203
1204 if (len1 < len2)
1205 return -1;
1206 else if (len1 > len2)
1207 return 1;
1208 else
1209 return 0;
1210 }
1211
1212 /* Simulate the C operator == by returning a 1
1213 iff ARG1 and ARG2 have equal contents. */
1214
1215 int
1216 value_equal (struct value *arg1, struct value *arg2)
1217 {
1218 int len;
1219 const bfd_byte *p1;
1220 const bfd_byte *p2;
1221 struct type *type1, *type2;
1222 enum type_code code1;
1223 enum type_code code2;
1224 int is_int1, is_int2;
1225
1226 arg1 = coerce_array (arg1);
1227 arg2 = coerce_array (arg2);
1228
1229 type1 = check_typedef (value_type (arg1));
1230 type2 = check_typedef (value_type (arg2));
1231 code1 = TYPE_CODE (type1);
1232 code2 = TYPE_CODE (type2);
1233 is_int1 = is_integral_type (type1);
1234 is_int2 = is_integral_type (type2);
1235
1236 if (is_int1 && is_int2)
1237 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1238 BINOP_EQUAL)));
1239 else if ((code1 == TYPE_CODE_FLT || is_int1)
1240 && (code2 == TYPE_CODE_FLT || is_int2))
1241 return value_as_double (arg1) == value_as_double (arg2);
1242
1243 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1244 is bigger. */
1245 else if (code1 == TYPE_CODE_PTR && is_int2)
1246 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1247 else if (code2 == TYPE_CODE_PTR && is_int1)
1248 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1249
1250 else if (code1 == code2
1251 && ((len = (int) TYPE_LENGTH (type1))
1252 == (int) TYPE_LENGTH (type2)))
1253 {
1254 p1 = value_contents (arg1);
1255 p2 = value_contents (arg2);
1256 while (--len >= 0)
1257 {
1258 if (*p1++ != *p2++)
1259 break;
1260 }
1261 return len < 0;
1262 }
1263 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1264 {
1265 return value_strcmp (arg1, arg2) == 0;
1266 }
1267 else
1268 {
1269 error (_("Invalid type combination in equality test."));
1270 return 0; /* For lint -- never reached */
1271 }
1272 }
1273
1274 /* Simulate the C operator < by returning 1
1275 iff ARG1's contents are less than ARG2's. */
1276
1277 int
1278 value_less (struct value *arg1, struct value *arg2)
1279 {
1280 enum type_code code1;
1281 enum type_code code2;
1282 struct type *type1, *type2;
1283 int is_int1, is_int2;
1284
1285 arg1 = coerce_array (arg1);
1286 arg2 = coerce_array (arg2);
1287
1288 type1 = check_typedef (value_type (arg1));
1289 type2 = check_typedef (value_type (arg2));
1290 code1 = TYPE_CODE (type1);
1291 code2 = TYPE_CODE (type2);
1292 is_int1 = is_integral_type (type1);
1293 is_int2 = is_integral_type (type2);
1294
1295 if (is_int1 && is_int2)
1296 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1297 BINOP_LESS)));
1298 else if ((code1 == TYPE_CODE_FLT || is_int1)
1299 && (code2 == TYPE_CODE_FLT || is_int2))
1300 return value_as_double (arg1) < value_as_double (arg2);
1301 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1302 return value_as_address (arg1) < value_as_address (arg2);
1303
1304 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1305 is bigger. */
1306 else if (code1 == TYPE_CODE_PTR && is_int2)
1307 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1308 else if (code2 == TYPE_CODE_PTR && is_int1)
1309 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1310 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1311 return value_strcmp (arg1, arg2) < 0;
1312 else
1313 {
1314 error (_("Invalid type combination in ordering comparison."));
1315 return 0;
1316 }
1317 }
1318 \f
1319 /* The unary operators +, - and ~. They free the argument ARG1. */
1320
1321 struct value *
1322 value_pos (struct value *arg1)
1323 {
1324 struct type *type;
1325
1326 arg1 = coerce_ref (arg1);
1327
1328 type = check_typedef (value_type (arg1));
1329
1330 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1331 return value_from_double (type, value_as_double (arg1));
1332 else if (is_integral_type (type))
1333 {
1334 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1335 FORTRAN and (the deleted) chill ? */
1336 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1337 type = builtin_type_int;
1338
1339 return value_from_longest (type, value_as_long (arg1));
1340 }
1341 else
1342 {
1343 error ("Argument to positive operation not a number.");
1344 return 0; /* For lint -- never reached */
1345 }
1346 }
1347
1348 struct value *
1349 value_neg (struct value *arg1)
1350 {
1351 struct type *type;
1352 struct type *result_type = value_type (arg1);
1353
1354 arg1 = coerce_ref (arg1);
1355
1356 type = check_typedef (value_type (arg1));
1357
1358 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1359 return value_from_double (result_type, -value_as_double (arg1));
1360 else if (is_integral_type (type))
1361 {
1362 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1363 FORTRAN and (the deleted) chill ? */
1364 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1365 result_type = builtin_type_int;
1366
1367 return value_from_longest (result_type, -value_as_long (arg1));
1368 }
1369 else
1370 {
1371 error (_("Argument to negate operation not a number."));
1372 return 0; /* For lint -- never reached */
1373 }
1374 }
1375
1376 struct value *
1377 value_complement (struct value *arg1)
1378 {
1379 struct type *type;
1380 struct type *result_type = value_type (arg1);
1381
1382 arg1 = coerce_ref (arg1);
1383
1384 type = check_typedef (value_type (arg1));
1385
1386 if (!is_integral_type (type))
1387 error (_("Argument to complement operation not an integer or boolean."));
1388
1389 /* Perform integral promotion for ANSI C/C++.
1390 FIXME: What about FORTRAN ? */
1391 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1392 result_type = builtin_type_int;
1393
1394 return value_from_longest (result_type, ~value_as_long (arg1));
1395 }
1396 \f
1397 /* The INDEX'th bit of SET value whose value_type is TYPE,
1398 and whose value_contents is valaddr.
1399 Return -1 if out of range, -2 other error. */
1400
1401 int
1402 value_bit_index (struct type *type, const bfd_byte *valaddr, int index)
1403 {
1404 LONGEST low_bound, high_bound;
1405 LONGEST word;
1406 unsigned rel_index;
1407 struct type *range = TYPE_FIELD_TYPE (type, 0);
1408 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1409 return -2;
1410 if (index < low_bound || index > high_bound)
1411 return -1;
1412 rel_index = index - low_bound;
1413 word = unpack_long (builtin_type_unsigned_char,
1414 valaddr + (rel_index / TARGET_CHAR_BIT));
1415 rel_index %= TARGET_CHAR_BIT;
1416 if (BITS_BIG_ENDIAN)
1417 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1418 return (word >> rel_index) & 1;
1419 }
1420
1421 struct value *
1422 value_in (struct value *element, struct value *set)
1423 {
1424 int member;
1425 struct type *settype = check_typedef (value_type (set));
1426 struct type *eltype = check_typedef (value_type (element));
1427 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1428 eltype = TYPE_TARGET_TYPE (eltype);
1429 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1430 error (_("Second argument of 'IN' has wrong type"));
1431 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1432 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1433 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1434 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1435 error (_("First argument of 'IN' has wrong type"));
1436 member = value_bit_index (settype, value_contents (set),
1437 value_as_long (element));
1438 if (member < 0)
1439 error (_("First argument of 'IN' not in range"));
1440 return value_from_longest (LA_BOOL_TYPE, member);
1441 }
1442
1443 void
1444 _initialize_valarith (void)
1445 {
1446 }
This page took 0.058543 seconds and 4 git commands to generate.