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