2008-01-30 Paul N. Hilfinger <hilfinger@adacore.com>
[deliverable/binutils-gdb.git] / gdb / valarith.c
1 /* Perform arithmetic and other operations on values, for GDB.
2
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
5 Free 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 3 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, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "value.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "target.h"
28 #include "language.h"
29 #include "gdb_string.h"
30 #include "doublest.h"
31 #include "dfp.h"
32 #include <math.h>
33 #include "infcall.h"
34
35 /* Define whether or not the C operator '/' truncates towards zero for
36 differently signed operands (truncation direction is undefined in C). */
37
38 #ifndef TRUNCATION_TOWARDS_ZERO
39 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
40 #endif
41
42 static struct value *value_subscripted_rvalue (struct value *, struct value *, int);
43
44 void _initialize_valarith (void);
45 \f
46
47 /* Given a pointer, return the size of its target.
48 If the pointer type is void *, then return 1.
49 If the target type is incomplete, then error out.
50 This isn't a general purpose function, but just a
51 helper for value_sub & value_add.
52 */
53
54 static LONGEST
55 find_size_for_pointer_math (struct type *ptr_type)
56 {
57 LONGEST sz = -1;
58 struct type *ptr_target;
59
60 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
61
62 sz = TYPE_LENGTH (ptr_target);
63 if (sz == 0)
64 {
65 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
66 sz = 1;
67 else
68 {
69 char *name;
70
71 name = TYPE_NAME (ptr_target);
72 if (name == NULL)
73 name = TYPE_TAG_NAME (ptr_target);
74 if (name == NULL)
75 error (_("Cannot perform pointer math on incomplete types, "
76 "try casting to a known type, or void *."));
77 else
78 error (_("Cannot perform pointer math on incomplete type \"%s\", "
79 "try casting to a known type, or void *."), name);
80 }
81 }
82 return sz;
83 }
84
85 struct value *
86 value_add (struct value *arg1, struct value *arg2)
87 {
88 struct value *valint;
89 struct value *valptr;
90 LONGEST sz;
91 struct type *type1, *type2, *valptrtype;
92
93 arg1 = coerce_array (arg1);
94 arg2 = coerce_array (arg2);
95 type1 = check_typedef (value_type (arg1));
96 type2 = check_typedef (value_type (arg2));
97
98 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
99 || TYPE_CODE (type2) == TYPE_CODE_PTR)
100 &&
101 (is_integral_type (type1) || is_integral_type (type2)))
102 /* Exactly one argument is a pointer, and one is an integer. */
103 {
104 struct value *retval;
105
106 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
107 {
108 valptr = arg1;
109 valint = arg2;
110 valptrtype = type1;
111 }
112 else
113 {
114 valptr = arg2;
115 valint = arg1;
116 valptrtype = type2;
117 }
118
119 sz = find_size_for_pointer_math (valptrtype);
120
121 retval = value_from_pointer (valptrtype,
122 value_as_address (valptr)
123 + (sz * value_as_long (valint)));
124 return retval;
125 }
126
127 return value_binop (arg1, arg2, BINOP_ADD);
128 }
129
130 struct value *
131 value_sub (struct value *arg1, struct value *arg2)
132 {
133 struct type *type1, *type2;
134 arg1 = coerce_array (arg1);
135 arg2 = coerce_array (arg2);
136 type1 = check_typedef (value_type (arg1));
137 type2 = check_typedef (value_type (arg2));
138
139 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
140 {
141 if (is_integral_type (type2))
142 {
143 /* pointer - integer. */
144 LONGEST sz = find_size_for_pointer_math (type1);
145
146 return value_from_pointer (type1,
147 (value_as_address (arg1)
148 - (sz * value_as_long (arg2))));
149 }
150 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
151 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
152 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
153 {
154 /* pointer to <type x> - pointer to <type x>. */
155 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
156 return value_from_longest
157 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
158 (value_as_long (arg1) - value_as_long (arg2)) / sz);
159 }
160 else
161 {
162 error (_("\
163 First argument of `-' is a pointer and second argument is neither\n\
164 an integer nor a pointer of the same type."));
165 }
166 }
167
168 return value_binop (arg1, arg2, BINOP_SUB);
169 }
170
171 /* Return the value of ARRAY[IDX].
172 See comments in value_coerce_array() for rationale for reason for
173 doing lower bounds adjustment here rather than there.
174 FIXME: Perhaps we should validate that the index is valid and if
175 verbosity is set, warn about invalid indices (but still use them). */
176
177 struct value *
178 value_subscript (struct value *array, struct value *idx)
179 {
180 struct value *bound;
181 int c_style = current_language->c_style_arrays;
182 struct type *tarray;
183
184 array = coerce_ref (array);
185 tarray = check_typedef (value_type (array));
186
187 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
188 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
189 {
190 struct type *range_type = TYPE_INDEX_TYPE (tarray);
191 LONGEST lowerbound, upperbound;
192 get_discrete_bounds (range_type, &lowerbound, &upperbound);
193
194 if (VALUE_LVAL (array) != lval_memory)
195 return value_subscripted_rvalue (array, idx, lowerbound);
196
197 if (c_style == 0)
198 {
199 LONGEST index = value_as_long (idx);
200 if (index >= lowerbound && index <= upperbound)
201 return value_subscripted_rvalue (array, idx, lowerbound);
202 /* Emit warning unless we have an array of unknown size.
203 An array of unknown size has lowerbound 0 and upperbound -1. */
204 if (upperbound > -1)
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 >>= (gdbarch_bits_big_endian (current_gdbarch) ?
234 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, or a reference to
289 one. This is called so we know whether to go ahead with the normal
290 binop or look for a 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
301 type1 = check_typedef (value_type (arg1));
302 if (TYPE_CODE (type1) == TYPE_CODE_REF)
303 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
304
305 type2 = check_typedef (value_type (arg2));
306 if (TYPE_CODE (type2) == TYPE_CODE_REF)
307 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
308
309 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
310 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
311 }
312
313 /* Check to see if argument is a structure. This is called so
314 we know whether to go ahead with the normal unop or look for a
315 user defined function instead.
316
317 For now, we do not overload the `&' operator. */
318
319 int
320 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
321 {
322 struct type *type1;
323 if (op == UNOP_ADDR)
324 return 0;
325 type1 = check_typedef (value_type (arg1));
326 for (;;)
327 {
328 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
329 return 1;
330 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
331 type1 = TYPE_TARGET_TYPE (type1);
332 else
333 return 0;
334 }
335 }
336
337 /* We know either arg1 or arg2 is a structure, so try to find the right
338 user defined function. Create an argument vector that calls
339 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
340 binary operator which is legal for GNU C++).
341
342 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
343 is the opcode saying how to modify it. Otherwise, OTHEROP is
344 unused. */
345
346 struct value *
347 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
348 enum exp_opcode otherop, enum noside noside)
349 {
350 struct value **argvec;
351 char *ptr;
352 char tstr[13];
353 int static_memfuncp;
354
355 arg1 = coerce_ref (arg1);
356 arg2 = coerce_ref (arg2);
357 arg1 = coerce_enum (arg1);
358 arg2 = coerce_enum (arg2);
359
360 /* now we know that what we have to do is construct our
361 arg vector and find the right function to call it with. */
362
363 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
364 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
365
366 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
367 argvec[1] = value_addr (arg1);
368 argvec[2] = arg2;
369 argvec[3] = 0;
370
371 /* make the right function name up */
372 strcpy (tstr, "operator__");
373 ptr = tstr + 8;
374 switch (op)
375 {
376 case BINOP_ADD:
377 strcpy (ptr, "+");
378 break;
379 case BINOP_SUB:
380 strcpy (ptr, "-");
381 break;
382 case BINOP_MUL:
383 strcpy (ptr, "*");
384 break;
385 case BINOP_DIV:
386 strcpy (ptr, "/");
387 break;
388 case BINOP_REM:
389 strcpy (ptr, "%");
390 break;
391 case BINOP_LSH:
392 strcpy (ptr, "<<");
393 break;
394 case BINOP_RSH:
395 strcpy (ptr, ">>");
396 break;
397 case BINOP_BITWISE_AND:
398 strcpy (ptr, "&");
399 break;
400 case BINOP_BITWISE_IOR:
401 strcpy (ptr, "|");
402 break;
403 case BINOP_BITWISE_XOR:
404 strcpy (ptr, "^");
405 break;
406 case BINOP_LOGICAL_AND:
407 strcpy (ptr, "&&");
408 break;
409 case BINOP_LOGICAL_OR:
410 strcpy (ptr, "||");
411 break;
412 case BINOP_MIN:
413 strcpy (ptr, "<?");
414 break;
415 case BINOP_MAX:
416 strcpy (ptr, ">?");
417 break;
418 case BINOP_ASSIGN:
419 strcpy (ptr, "=");
420 break;
421 case BINOP_ASSIGN_MODIFY:
422 switch (otherop)
423 {
424 case BINOP_ADD:
425 strcpy (ptr, "+=");
426 break;
427 case BINOP_SUB:
428 strcpy (ptr, "-=");
429 break;
430 case BINOP_MUL:
431 strcpy (ptr, "*=");
432 break;
433 case BINOP_DIV:
434 strcpy (ptr, "/=");
435 break;
436 case BINOP_REM:
437 strcpy (ptr, "%=");
438 break;
439 case BINOP_BITWISE_AND:
440 strcpy (ptr, "&=");
441 break;
442 case BINOP_BITWISE_IOR:
443 strcpy (ptr, "|=");
444 break;
445 case BINOP_BITWISE_XOR:
446 strcpy (ptr, "^=");
447 break;
448 case BINOP_MOD: /* invalid */
449 default:
450 error (_("Invalid binary operation specified."));
451 }
452 break;
453 case BINOP_SUBSCRIPT:
454 strcpy (ptr, "[]");
455 break;
456 case BINOP_EQUAL:
457 strcpy (ptr, "==");
458 break;
459 case BINOP_NOTEQUAL:
460 strcpy (ptr, "!=");
461 break;
462 case BINOP_LESS:
463 strcpy (ptr, "<");
464 break;
465 case BINOP_GTR:
466 strcpy (ptr, ">");
467 break;
468 case BINOP_GEQ:
469 strcpy (ptr, ">=");
470 break;
471 case BINOP_LEQ:
472 strcpy (ptr, "<=");
473 break;
474 case BINOP_MOD: /* invalid */
475 default:
476 error (_("Invalid binary operation specified."));
477 }
478
479 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
480
481 if (argvec[0])
482 {
483 if (static_memfuncp)
484 {
485 argvec[1] = argvec[0];
486 argvec++;
487 }
488 if (noside == EVAL_AVOID_SIDE_EFFECTS)
489 {
490 struct type *return_type;
491 return_type
492 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
493 return value_zero (return_type, VALUE_LVAL (arg1));
494 }
495 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
496 }
497 error (_("member function %s not found"), tstr);
498 #ifdef lint
499 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
500 #endif
501 }
502
503 /* We know that arg1 is a structure, so try to find a unary user
504 defined operator that matches the operator in question.
505 Create an argument vector that calls arg1.operator @ (arg1)
506 and return that value (where '@' is (almost) any unary operator which
507 is legal for GNU C++). */
508
509 struct value *
510 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
511 {
512 struct value **argvec;
513 char *ptr, *mangle_ptr;
514 char tstr[13], mangle_tstr[13];
515 int static_memfuncp, nargs;
516
517 arg1 = coerce_ref (arg1);
518 arg1 = coerce_enum (arg1);
519
520 /* now we know that what we have to do is construct our
521 arg vector and find the right function to call it with. */
522
523 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
524 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
525
526 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
527 argvec[1] = value_addr (arg1);
528 argvec[2] = 0;
529
530 nargs = 1;
531
532 /* make the right function name up */
533 strcpy (tstr, "operator__");
534 ptr = tstr + 8;
535 strcpy (mangle_tstr, "__");
536 mangle_ptr = mangle_tstr + 2;
537 switch (op)
538 {
539 case UNOP_PREINCREMENT:
540 strcpy (ptr, "++");
541 break;
542 case UNOP_PREDECREMENT:
543 strcpy (ptr, "--");
544 break;
545 case UNOP_POSTINCREMENT:
546 strcpy (ptr, "++");
547 argvec[2] = value_from_longest (builtin_type_int, 0);
548 argvec[3] = 0;
549 nargs ++;
550 break;
551 case UNOP_POSTDECREMENT:
552 strcpy (ptr, "--");
553 argvec[2] = value_from_longest (builtin_type_int, 0);
554 argvec[3] = 0;
555 nargs ++;
556 break;
557 case UNOP_LOGICAL_NOT:
558 strcpy (ptr, "!");
559 break;
560 case UNOP_COMPLEMENT:
561 strcpy (ptr, "~");
562 break;
563 case UNOP_NEG:
564 strcpy (ptr, "-");
565 break;
566 case UNOP_PLUS:
567 strcpy (ptr, "+");
568 break;
569 case UNOP_IND:
570 strcpy (ptr, "*");
571 break;
572 default:
573 error (_("Invalid unary operation specified."));
574 }
575
576 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
577
578 if (argvec[0])
579 {
580 if (static_memfuncp)
581 {
582 argvec[1] = argvec[0];
583 nargs --;
584 argvec++;
585 }
586 if (noside == EVAL_AVOID_SIDE_EFFECTS)
587 {
588 struct type *return_type;
589 return_type
590 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
591 return value_zero (return_type, VALUE_LVAL (arg1));
592 }
593 return call_function_by_hand (argvec[0], nargs, argvec + 1);
594 }
595 error (_("member function %s not found"), tstr);
596 return 0; /* For lint -- never reached */
597 }
598 \f
599
600 /* Concatenate two values with the following conditions:
601
602 (1) Both values must be either bitstring values or character string
603 values and the resulting value consists of the concatenation of
604 ARG1 followed by ARG2.
605
606 or
607
608 One value must be an integer value and the other value must be
609 either a bitstring value or character string value, which is
610 to be repeated by the number of times specified by the integer
611 value.
612
613
614 (2) Boolean values are also allowed and are treated as bit string
615 values of length 1.
616
617 (3) Character values are also allowed and are treated as character
618 string values of length 1.
619 */
620
621 struct value *
622 value_concat (struct value *arg1, struct value *arg2)
623 {
624 struct value *inval1;
625 struct value *inval2;
626 struct value *outval = NULL;
627 int inval1len, inval2len;
628 int count, idx;
629 char *ptr;
630 char inchar;
631 struct type *type1 = check_typedef (value_type (arg1));
632 struct type *type2 = check_typedef (value_type (arg2));
633
634 /* First figure out if we are dealing with two values to be concatenated
635 or a repeat count and a value to be repeated. INVAL1 is set to the
636 first of two concatenated values, or the repeat count. INVAL2 is set
637 to the second of the two concatenated values or the value to be
638 repeated. */
639
640 if (TYPE_CODE (type2) == TYPE_CODE_INT)
641 {
642 struct type *tmp = type1;
643 type1 = tmp;
644 tmp = type2;
645 inval1 = arg2;
646 inval2 = arg1;
647 }
648 else
649 {
650 inval1 = arg1;
651 inval2 = arg2;
652 }
653
654 /* Now process the input values. */
655
656 if (TYPE_CODE (type1) == TYPE_CODE_INT)
657 {
658 /* We have a repeat count. Validate the second value and then
659 construct a value repeated that many times. */
660 if (TYPE_CODE (type2) == TYPE_CODE_STRING
661 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
662 {
663 count = longest_to_int (value_as_long (inval1));
664 inval2len = TYPE_LENGTH (type2);
665 ptr = (char *) alloca (count * inval2len);
666 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
667 {
668 inchar = (char) unpack_long (type2,
669 value_contents (inval2));
670 for (idx = 0; idx < count; idx++)
671 {
672 *(ptr + idx) = inchar;
673 }
674 }
675 else
676 {
677 for (idx = 0; idx < count; idx++)
678 {
679 memcpy (ptr + (idx * inval2len), value_contents (inval2),
680 inval2len);
681 }
682 }
683 outval = value_string (ptr, count * inval2len);
684 }
685 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
686 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
687 {
688 error (_("unimplemented support for bitstring/boolean repeats"));
689 }
690 else
691 {
692 error (_("can't repeat values of that type"));
693 }
694 }
695 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
696 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
697 {
698 /* We have two character strings to concatenate. */
699 if (TYPE_CODE (type2) != TYPE_CODE_STRING
700 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
701 {
702 error (_("Strings can only be concatenated with other strings."));
703 }
704 inval1len = TYPE_LENGTH (type1);
705 inval2len = TYPE_LENGTH (type2);
706 ptr = (char *) alloca (inval1len + inval2len);
707 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
708 {
709 *ptr = (char) unpack_long (type1, value_contents (inval1));
710 }
711 else
712 {
713 memcpy (ptr, value_contents (inval1), inval1len);
714 }
715 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
716 {
717 *(ptr + inval1len) =
718 (char) unpack_long (type2, value_contents (inval2));
719 }
720 else
721 {
722 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
723 }
724 outval = value_string (ptr, inval1len + inval2len);
725 }
726 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
727 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
728 {
729 /* We have two bitstrings to concatenate. */
730 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
731 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
732 {
733 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
734 }
735 error (_("unimplemented support for bitstring/boolean concatenation."));
736 }
737 else
738 {
739 /* We don't know how to concatenate these operands. */
740 error (_("illegal operands for concatenation."));
741 }
742 return (outval);
743 }
744 \f
745
746 /* Integer exponentiation: V1**V2, where both arguments are
747 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
748 static LONGEST
749 integer_pow (LONGEST v1, LONGEST v2)
750 {
751 if (v2 < 0)
752 {
753 if (v1 == 0)
754 error (_("Attempt to raise 0 to negative power."));
755 else
756 return 0;
757 }
758 else
759 {
760 /* The Russian Peasant's Algorithm */
761 LONGEST v;
762
763 v = 1;
764 for (;;)
765 {
766 if (v2 & 1L)
767 v *= v1;
768 v2 >>= 1;
769 if (v2 == 0)
770 return v;
771 v1 *= v1;
772 }
773 }
774 }
775
776 /* Integer exponentiation: V1**V2, where both arguments are
777 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
778 static ULONGEST
779 uinteger_pow (ULONGEST v1, LONGEST v2)
780 {
781 if (v2 < 0)
782 {
783 if (v1 == 0)
784 error (_("Attempt to raise 0 to negative power."));
785 else
786 return 0;
787 }
788 else
789 {
790 /* The Russian Peasant's Algorithm */
791 ULONGEST v;
792
793 v = 1;
794 for (;;)
795 {
796 if (v2 & 1L)
797 v *= v1;
798 v2 >>= 1;
799 if (v2 == 0)
800 return v;
801 v1 *= v1;
802 }
803 }
804 }
805
806 /* Obtain decimal value of arguments for binary operation, converting from
807 other types if one of them is not decimal floating point. */
808 static void
809 value_args_as_decimal (struct value *arg1, struct value *arg2,
810 gdb_byte *x, int *len_x, gdb_byte *y, int *len_y)
811 {
812 struct type *type1, *type2;
813
814 type1 = check_typedef (value_type (arg1));
815 type2 = check_typedef (value_type (arg2));
816
817 /* At least one of the arguments must be of decimal float type. */
818 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
819 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
820
821 if (TYPE_CODE (type1) == TYPE_CODE_FLT
822 || TYPE_CODE (type2) == TYPE_CODE_FLT)
823 /* The DFP extension to the C language does not allow mixing of
824 * decimal float types with other float types in expressions
825 * (see WDTR 24732, page 12). */
826 error (_("Mixing decimal floating types with other floating types is not allowed."));
827
828 /* Obtain decimal value of arg1, converting from other types
829 if necessary. */
830
831 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
832 {
833 *len_x = TYPE_LENGTH (type1);
834 memcpy (x, value_contents (arg1), *len_x);
835 }
836 else if (is_integral_type (type1))
837 {
838 *len_x = TYPE_LENGTH (type2);
839 decimal_from_integral (arg1, x, *len_x);
840 }
841 else
842 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
843 TYPE_NAME (type2));
844
845 /* Obtain decimal value of arg2, converting from other types
846 if necessary. */
847
848 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
849 {
850 *len_y = TYPE_LENGTH (type2);
851 memcpy (y, value_contents (arg2), *len_y);
852 }
853 else if (is_integral_type (type2))
854 {
855 *len_y = TYPE_LENGTH (type1);
856 decimal_from_integral (arg2, y, *len_y);
857 }
858 else
859 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
860 TYPE_NAME (type2));
861 }
862
863 /* Perform a binary operation on two operands which have reasonable
864 representations as integers or floats. This includes booleans,
865 characters, integers, or floats.
866 Does not support addition and subtraction on pointers;
867 use value_add or value_sub if you want to handle those possibilities. */
868
869 struct value *
870 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
871 {
872 struct value *val;
873 struct type *type1, *type2;
874
875 arg1 = coerce_ref (arg1);
876 arg2 = coerce_ref (arg2);
877 type1 = check_typedef (value_type (arg1));
878 type2 = check_typedef (value_type (arg2));
879
880 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
881 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT && !is_integral_type (type1))
882 ||
883 (TYPE_CODE (type2) != TYPE_CODE_FLT
884 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT && !is_integral_type (type2)))
885 error (_("Argument to arithmetic operation not a number or boolean."));
886
887 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
888 ||
889 TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
890 {
891 struct type *v_type;
892 int len_v1, len_v2, len_v;
893 gdb_byte v1[16], v2[16];
894 gdb_byte v[16];
895
896 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
897
898 switch (op)
899 {
900 case BINOP_ADD:
901 case BINOP_SUB:
902 case BINOP_MUL:
903 case BINOP_DIV:
904 case BINOP_EXP:
905 decimal_binop (op, v1, len_v1, v2, len_v2, v, &len_v);
906 break;
907
908 default:
909 error (_("Operation not valid for decimal floating point number."));
910 }
911
912 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
913 /* If arg1 is not a decimal float, the type of the result is the type
914 of the decimal float argument, arg2. */
915 v_type = type2;
916 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
917 /* Same logic, for the case where arg2 is not a decimal float. */
918 v_type = type1;
919 else
920 /* len_v is equal either to len_v1 or to len_v2. the type of the
921 result is the type of the argument with the same length as v. */
922 v_type = (len_v == len_v1)? type1 : type2;
923
924 val = value_from_decfloat (v_type, v);
925 }
926 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
927 ||
928 TYPE_CODE (type2) == TYPE_CODE_FLT)
929 {
930 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
931 in target format. real.c in GCC probably has the necessary
932 code. */
933 DOUBLEST v1, v2, v = 0;
934 v1 = value_as_double (arg1);
935 v2 = value_as_double (arg2);
936 switch (op)
937 {
938 case BINOP_ADD:
939 v = v1 + v2;
940 break;
941
942 case BINOP_SUB:
943 v = v1 - v2;
944 break;
945
946 case BINOP_MUL:
947 v = v1 * v2;
948 break;
949
950 case BINOP_DIV:
951 v = v1 / v2;
952 break;
953
954 case BINOP_EXP:
955 errno = 0;
956 v = pow (v1, v2);
957 if (errno)
958 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
959 break;
960
961 case BINOP_MIN:
962 v = v1 < v2 ? v1 : v2;
963 break;
964
965 case BINOP_MAX:
966 v = v1 > v2 ? v1 : v2;
967 break;
968
969 default:
970 error (_("Integer-only operation on floating point number."));
971 }
972
973 /* If either arg was long double, make sure that value is also long
974 double. */
975
976 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (current_gdbarch)
977 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (current_gdbarch))
978 val = allocate_value (builtin_type_long_double);
979 else
980 val = allocate_value (builtin_type_double);
981
982 store_typed_floating (value_contents_raw (val), value_type (val), v);
983 }
984 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
985 &&
986 TYPE_CODE (type2) == TYPE_CODE_BOOL)
987 {
988 LONGEST v1, v2, v = 0;
989 v1 = value_as_long (arg1);
990 v2 = value_as_long (arg2);
991
992 switch (op)
993 {
994 case BINOP_BITWISE_AND:
995 v = v1 & v2;
996 break;
997
998 case BINOP_BITWISE_IOR:
999 v = v1 | v2;
1000 break;
1001
1002 case BINOP_BITWISE_XOR:
1003 v = v1 ^ v2;
1004 break;
1005
1006 case BINOP_EQUAL:
1007 v = v1 == v2;
1008 break;
1009
1010 case BINOP_NOTEQUAL:
1011 v = v1 != v2;
1012 break;
1013
1014 default:
1015 error (_("Invalid operation on booleans."));
1016 }
1017
1018 val = allocate_value (type1);
1019 store_signed_integer (value_contents_raw (val),
1020 TYPE_LENGTH (type1),
1021 v);
1022 }
1023 else
1024 /* Integral operations here. */
1025 /* FIXME: Also mixed integral/booleans, with result an integer. */
1026 /* FIXME: This implements ANSI C rules (also correct for C++).
1027 What about FORTRAN and (the deleted) chill ? */
1028 {
1029 unsigned int promoted_len1 = TYPE_LENGTH (type1);
1030 unsigned int promoted_len2 = TYPE_LENGTH (type2);
1031 int is_unsigned1 = TYPE_UNSIGNED (type1);
1032 int is_unsigned2 = TYPE_UNSIGNED (type2);
1033 unsigned int result_len;
1034 int unsigned_operation;
1035
1036 /* Determine type length and signedness after promotion for
1037 both operands. */
1038 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
1039 {
1040 is_unsigned1 = 0;
1041 promoted_len1 = TYPE_LENGTH (builtin_type_int);
1042 }
1043 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
1044 {
1045 is_unsigned2 = 0;
1046 promoted_len2 = TYPE_LENGTH (builtin_type_int);
1047 }
1048
1049 /* Determine type length of the result, and if the operation should
1050 be done unsigned. For exponentiation and shift operators,
1051 use the length and type of the left operand. Otherwise,
1052 use the signedness of the operand with the greater length.
1053 If both operands are of equal length, use unsigned operation
1054 if one of the operands is unsigned. */
1055 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1056 {
1057 /* In case of the shift operators and exponentiation the type of
1058 the result only depends on the type of the left operand. */
1059 unsigned_operation = is_unsigned1;
1060 result_len = promoted_len1;
1061 }
1062 else if (promoted_len1 > promoted_len2)
1063 {
1064 unsigned_operation = is_unsigned1;
1065 result_len = promoted_len1;
1066 }
1067 else if (promoted_len2 > promoted_len1)
1068 {
1069 unsigned_operation = is_unsigned2;
1070 result_len = promoted_len2;
1071 }
1072 else
1073 {
1074 unsigned_operation = is_unsigned1 || is_unsigned2;
1075 result_len = promoted_len1;
1076 }
1077
1078 if (unsigned_operation)
1079 {
1080 LONGEST v2_signed = value_as_long (arg2);
1081 ULONGEST v1, v2, v = 0;
1082 v1 = (ULONGEST) value_as_long (arg1);
1083 v2 = (ULONGEST) v2_signed;
1084
1085 /* Truncate values to the type length of the result. */
1086 if (result_len < sizeof (ULONGEST))
1087 {
1088 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1089 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1090 }
1091
1092 switch (op)
1093 {
1094 case BINOP_ADD:
1095 v = v1 + v2;
1096 break;
1097
1098 case BINOP_SUB:
1099 v = v1 - v2;
1100 break;
1101
1102 case BINOP_MUL:
1103 v = v1 * v2;
1104 break;
1105
1106 case BINOP_DIV:
1107 case BINOP_INTDIV:
1108 if (v2 != 0)
1109 v = v1 / v2;
1110 else
1111 error (_("Division by zero"));
1112 break;
1113
1114 case BINOP_EXP:
1115 v = uinteger_pow (v1, v2_signed);
1116 break;
1117
1118 case BINOP_REM:
1119 v = v1 % v2;
1120 break;
1121
1122 case BINOP_MOD:
1123 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1124 v1 mod 0 has a defined value, v1. */
1125 if (v2 == 0)
1126 {
1127 v = v1;
1128 }
1129 else
1130 {
1131 v = v1 / v2;
1132 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1133 v = v1 - (v2 * v);
1134 }
1135 break;
1136
1137 case BINOP_LSH:
1138 v = v1 << v2;
1139 break;
1140
1141 case BINOP_RSH:
1142 v = v1 >> v2;
1143 break;
1144
1145 case BINOP_BITWISE_AND:
1146 v = v1 & v2;
1147 break;
1148
1149 case BINOP_BITWISE_IOR:
1150 v = v1 | v2;
1151 break;
1152
1153 case BINOP_BITWISE_XOR:
1154 v = v1 ^ v2;
1155 break;
1156
1157 case BINOP_LOGICAL_AND:
1158 v = v1 && v2;
1159 break;
1160
1161 case BINOP_LOGICAL_OR:
1162 v = v1 || v2;
1163 break;
1164
1165 case BINOP_MIN:
1166 v = v1 < v2 ? v1 : v2;
1167 break;
1168
1169 case BINOP_MAX:
1170 v = v1 > v2 ? v1 : v2;
1171 break;
1172
1173 case BINOP_EQUAL:
1174 v = v1 == v2;
1175 break;
1176
1177 case BINOP_NOTEQUAL:
1178 v = v1 != v2;
1179 break;
1180
1181 case BINOP_LESS:
1182 v = v1 < v2;
1183 break;
1184
1185 default:
1186 error (_("Invalid binary operation on numbers."));
1187 }
1188
1189 /* This is a kludge to get around the fact that we don't
1190 know how to determine the result type from the types of
1191 the operands. (I'm not really sure how much we feel the
1192 need to duplicate the exact rules of the current
1193 language. They can get really hairy. But not to do so
1194 makes it hard to document just what we *do* do). */
1195
1196 /* Can't just call init_type because we wouldn't know what
1197 name to give the type. */
1198 val = allocate_value
1199 (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT
1200 ? builtin_type_unsigned_long_long
1201 : builtin_type_unsigned_long);
1202 store_unsigned_integer (value_contents_raw (val),
1203 TYPE_LENGTH (value_type (val)),
1204 v);
1205 }
1206 else
1207 {
1208 LONGEST v1, v2, v = 0;
1209 v1 = value_as_long (arg1);
1210 v2 = value_as_long (arg2);
1211
1212 switch (op)
1213 {
1214 case BINOP_ADD:
1215 v = v1 + v2;
1216 break;
1217
1218 case BINOP_SUB:
1219 v = v1 - v2;
1220 break;
1221
1222 case BINOP_MUL:
1223 v = v1 * v2;
1224 break;
1225
1226 case BINOP_DIV:
1227 case BINOP_INTDIV:
1228 if (v2 != 0)
1229 v = v1 / v2;
1230 else
1231 error (_("Division by zero"));
1232 break;
1233
1234 case BINOP_EXP:
1235 v = integer_pow (v1, v2);
1236 break;
1237
1238 case BINOP_REM:
1239 if (v2 != 0)
1240 v = v1 % v2;
1241 else
1242 error (_("Division by zero"));
1243 break;
1244
1245 case BINOP_MOD:
1246 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1247 X mod 0 has a defined value, X. */
1248 if (v2 == 0)
1249 {
1250 v = v1;
1251 }
1252 else
1253 {
1254 v = v1 / v2;
1255 /* Compute floor. */
1256 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1257 {
1258 v--;
1259 }
1260 v = v1 - (v2 * v);
1261 }
1262 break;
1263
1264 case BINOP_LSH:
1265 v = v1 << v2;
1266 break;
1267
1268 case BINOP_RSH:
1269 v = v1 >> v2;
1270 break;
1271
1272 case BINOP_BITWISE_AND:
1273 v = v1 & v2;
1274 break;
1275
1276 case BINOP_BITWISE_IOR:
1277 v = v1 | v2;
1278 break;
1279
1280 case BINOP_BITWISE_XOR:
1281 v = v1 ^ v2;
1282 break;
1283
1284 case BINOP_LOGICAL_AND:
1285 v = v1 && v2;
1286 break;
1287
1288 case BINOP_LOGICAL_OR:
1289 v = v1 || v2;
1290 break;
1291
1292 case BINOP_MIN:
1293 v = v1 < v2 ? v1 : v2;
1294 break;
1295
1296 case BINOP_MAX:
1297 v = v1 > v2 ? v1 : v2;
1298 break;
1299
1300 case BINOP_EQUAL:
1301 v = v1 == v2;
1302 break;
1303
1304 case BINOP_LESS:
1305 v = v1 < v2;
1306 break;
1307
1308 default:
1309 error (_("Invalid binary operation on numbers."));
1310 }
1311
1312 /* This is a kludge to get around the fact that we don't
1313 know how to determine the result type from the types of
1314 the operands. (I'm not really sure how much we feel the
1315 need to duplicate the exact rules of the current
1316 language. They can get really hairy. But not to do so
1317 makes it hard to document just what we *do* do). */
1318
1319 /* Can't just call init_type because we wouldn't know what
1320 name to give the type. */
1321 val = allocate_value
1322 (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT
1323 ? builtin_type_long_long
1324 : builtin_type_long);
1325 store_signed_integer (value_contents_raw (val),
1326 TYPE_LENGTH (value_type (val)),
1327 v);
1328 }
1329 }
1330
1331 return val;
1332 }
1333 \f
1334 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1335
1336 int
1337 value_logical_not (struct value *arg1)
1338 {
1339 int len;
1340 const gdb_byte *p;
1341 struct type *type1;
1342
1343 arg1 = coerce_number (arg1);
1344 type1 = check_typedef (value_type (arg1));
1345
1346 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1347 return 0 == value_as_double (arg1);
1348 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1349 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1));
1350
1351 len = TYPE_LENGTH (type1);
1352 p = value_contents (arg1);
1353
1354 while (--len >= 0)
1355 {
1356 if (*p++)
1357 break;
1358 }
1359
1360 return len < 0;
1361 }
1362
1363 /* Perform a comparison on two string values (whose content are not
1364 necessarily null terminated) based on their length */
1365
1366 static int
1367 value_strcmp (struct value *arg1, struct value *arg2)
1368 {
1369 int len1 = TYPE_LENGTH (value_type (arg1));
1370 int len2 = TYPE_LENGTH (value_type (arg2));
1371 const gdb_byte *s1 = value_contents (arg1);
1372 const gdb_byte *s2 = value_contents (arg2);
1373 int i, len = len1 < len2 ? len1 : len2;
1374
1375 for (i = 0; i < len; i++)
1376 {
1377 if (s1[i] < s2[i])
1378 return -1;
1379 else if (s1[i] > s2[i])
1380 return 1;
1381 else
1382 continue;
1383 }
1384
1385 if (len1 < len2)
1386 return -1;
1387 else if (len1 > len2)
1388 return 1;
1389 else
1390 return 0;
1391 }
1392
1393 /* Simulate the C operator == by returning a 1
1394 iff ARG1 and ARG2 have equal contents. */
1395
1396 int
1397 value_equal (struct value *arg1, struct value *arg2)
1398 {
1399 int len;
1400 const gdb_byte *p1;
1401 const gdb_byte *p2;
1402 struct type *type1, *type2;
1403 enum type_code code1;
1404 enum type_code code2;
1405 int is_int1, is_int2;
1406
1407 arg1 = coerce_array (arg1);
1408 arg2 = coerce_array (arg2);
1409
1410 type1 = check_typedef (value_type (arg1));
1411 type2 = check_typedef (value_type (arg2));
1412 code1 = TYPE_CODE (type1);
1413 code2 = TYPE_CODE (type2);
1414 is_int1 = is_integral_type (type1);
1415 is_int2 = is_integral_type (type2);
1416
1417 if (is_int1 && is_int2)
1418 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1419 BINOP_EQUAL)));
1420 else if ((code1 == TYPE_CODE_FLT || is_int1)
1421 && (code2 == TYPE_CODE_FLT || is_int2))
1422 {
1423 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1424 `long double' values are returned in static storage (m68k). */
1425 DOUBLEST d = value_as_double (arg1);
1426 return d == value_as_double (arg2);
1427 }
1428 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1429 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1430 {
1431 gdb_byte v1[16], v2[16];
1432 int len_v1, len_v2;
1433
1434 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1435
1436 return decimal_compare (v1, len_v1, v2, len_v2) == 0;
1437 }
1438
1439 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1440 is bigger. */
1441 else if (code1 == TYPE_CODE_PTR && is_int2)
1442 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1443 else if (code2 == TYPE_CODE_PTR && is_int1)
1444 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1445
1446 else if (code1 == code2
1447 && ((len = (int) TYPE_LENGTH (type1))
1448 == (int) TYPE_LENGTH (type2)))
1449 {
1450 p1 = value_contents (arg1);
1451 p2 = value_contents (arg2);
1452 while (--len >= 0)
1453 {
1454 if (*p1++ != *p2++)
1455 break;
1456 }
1457 return len < 0;
1458 }
1459 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1460 {
1461 return value_strcmp (arg1, arg2) == 0;
1462 }
1463 else
1464 {
1465 error (_("Invalid type combination in equality test."));
1466 return 0; /* For lint -- never reached */
1467 }
1468 }
1469
1470 /* Simulate the C operator < by returning 1
1471 iff ARG1's contents are less than ARG2's. */
1472
1473 int
1474 value_less (struct value *arg1, struct value *arg2)
1475 {
1476 enum type_code code1;
1477 enum type_code code2;
1478 struct type *type1, *type2;
1479 int is_int1, is_int2;
1480
1481 arg1 = coerce_array (arg1);
1482 arg2 = coerce_array (arg2);
1483
1484 type1 = check_typedef (value_type (arg1));
1485 type2 = check_typedef (value_type (arg2));
1486 code1 = TYPE_CODE (type1);
1487 code2 = TYPE_CODE (type2);
1488 is_int1 = is_integral_type (type1);
1489 is_int2 = is_integral_type (type2);
1490
1491 if (is_int1 && is_int2)
1492 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1493 BINOP_LESS)));
1494 else if ((code1 == TYPE_CODE_FLT || is_int1)
1495 && (code2 == TYPE_CODE_FLT || is_int2))
1496 {
1497 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1498 `long double' values are returned in static storage (m68k). */
1499 DOUBLEST d = value_as_double (arg1);
1500 return d < value_as_double (arg2);
1501 }
1502 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1503 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1504 {
1505 gdb_byte v1[16], v2[16];
1506 int len_v1, len_v2;
1507
1508 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1509
1510 return decimal_compare (v1, len_v1, v2, len_v2) == -1;
1511 }
1512 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1513 return value_as_address (arg1) < value_as_address (arg2);
1514
1515 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1516 is bigger. */
1517 else if (code1 == TYPE_CODE_PTR && is_int2)
1518 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1519 else if (code2 == TYPE_CODE_PTR && is_int1)
1520 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1521 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1522 return value_strcmp (arg1, arg2) < 0;
1523 else
1524 {
1525 error (_("Invalid type combination in ordering comparison."));
1526 return 0;
1527 }
1528 }
1529 \f
1530 /* The unary operators +, - and ~. They free the argument ARG1. */
1531
1532 struct value *
1533 value_pos (struct value *arg1)
1534 {
1535 struct type *type;
1536
1537 arg1 = coerce_ref (arg1);
1538
1539 type = check_typedef (value_type (arg1));
1540
1541 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1542 return value_from_double (type, value_as_double (arg1));
1543 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1544 return value_from_decfloat (type, value_contents (arg1));
1545 else if (is_integral_type (type))
1546 {
1547 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1548 FORTRAN and (the deleted) chill ? */
1549 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1550 type = builtin_type_int;
1551
1552 return value_from_longest (type, value_as_long (arg1));
1553 }
1554 else
1555 {
1556 error ("Argument to positive operation not a number.");
1557 return 0; /* For lint -- never reached */
1558 }
1559 }
1560
1561 struct value *
1562 value_neg (struct value *arg1)
1563 {
1564 struct type *type;
1565 struct type *result_type = value_type (arg1);
1566
1567 arg1 = coerce_ref (arg1);
1568
1569 type = check_typedef (value_type (arg1));
1570
1571 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1572 {
1573 struct value *val = allocate_value (result_type);
1574 int len = TYPE_LENGTH (type);
1575 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
1576
1577 memcpy (decbytes, value_contents (arg1), len);
1578
1579 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
1580 decbytes[len-1] = decbytes[len - 1] | 0x80;
1581 else
1582 decbytes[0] = decbytes[0] | 0x80;
1583
1584 memcpy (value_contents_raw (val), decbytes, len);
1585 return val;
1586 }
1587
1588 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1589 return value_from_double (result_type, -value_as_double (arg1));
1590 else if (is_integral_type (type))
1591 {
1592 /* Perform integral promotion for ANSI C/C++. FIXME: What about
1593 FORTRAN and (the deleted) chill ? */
1594 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1595 result_type = builtin_type_int;
1596
1597 return value_from_longest (result_type, -value_as_long (arg1));
1598 }
1599 else
1600 {
1601 error (_("Argument to negate operation not a number."));
1602 return 0; /* For lint -- never reached */
1603 }
1604 }
1605
1606 struct value *
1607 value_complement (struct value *arg1)
1608 {
1609 struct type *type;
1610 struct type *result_type = value_type (arg1);
1611
1612 arg1 = coerce_ref (arg1);
1613
1614 type = check_typedef (value_type (arg1));
1615
1616 if (!is_integral_type (type))
1617 error (_("Argument to complement operation not an integer or boolean."));
1618
1619 /* Perform integral promotion for ANSI C/C++.
1620 FIXME: What about FORTRAN ? */
1621 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1622 result_type = builtin_type_int;
1623
1624 return value_from_longest (result_type, ~value_as_long (arg1));
1625 }
1626 \f
1627 /* The INDEX'th bit of SET value whose value_type is TYPE,
1628 and whose value_contents is valaddr.
1629 Return -1 if out of range, -2 other error. */
1630
1631 int
1632 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1633 {
1634 LONGEST low_bound, high_bound;
1635 LONGEST word;
1636 unsigned rel_index;
1637 struct type *range = TYPE_FIELD_TYPE (type, 0);
1638 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1639 return -2;
1640 if (index < low_bound || index > high_bound)
1641 return -1;
1642 rel_index = index - low_bound;
1643 word = unpack_long (builtin_type_unsigned_char,
1644 valaddr + (rel_index / TARGET_CHAR_BIT));
1645 rel_index %= TARGET_CHAR_BIT;
1646 if (gdbarch_bits_big_endian (current_gdbarch))
1647 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1648 return (word >> rel_index) & 1;
1649 }
1650
1651 struct value *
1652 value_in (struct value *element, struct value *set)
1653 {
1654 int member;
1655 struct type *settype = check_typedef (value_type (set));
1656 struct type *eltype = check_typedef (value_type (element));
1657 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1658 eltype = TYPE_TARGET_TYPE (eltype);
1659 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1660 error (_("Second argument of 'IN' has wrong type"));
1661 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1662 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1663 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1664 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1665 error (_("First argument of 'IN' has wrong type"));
1666 member = value_bit_index (settype, value_contents (set),
1667 value_as_long (element));
1668 if (member < 0)
1669 error (_("First argument of 'IN' not in range"));
1670 return value_from_longest (LA_BOOL_TYPE, member);
1671 }
1672
1673 void
1674 _initialize_valarith (void)
1675 {
1676 }
This page took 0.063952 seconds and 5 git commands to generate.