]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/valarith.c
909c1a08957f53a79978f6e6dc453591d17a5f46
[thirdparty/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 type *unop_result_type (enum exp_opcode op, struct type *type1);
43 static struct type *binop_result_type (enum exp_opcode op, struct type *type1,
44 struct type *type2);
45
46 void _initialize_valarith (void);
47 \f
48
49 /* Given a pointer, return the size of its target.
50 If the pointer type is void *, then return 1.
51 If the target type is incomplete, then error out.
52 This isn't a general purpose function, but just a
53 helper for value_sub & value_add.
54 */
55
56 static LONGEST
57 find_size_for_pointer_math (struct type *ptr_type)
58 {
59 LONGEST sz = -1;
60 struct type *ptr_target;
61
62 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
63
64 sz = TYPE_LENGTH (ptr_target);
65 if (sz == 0)
66 {
67 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
68 sz = 1;
69 else
70 {
71 char *name;
72
73 name = TYPE_NAME (ptr_target);
74 if (name == NULL)
75 name = TYPE_TAG_NAME (ptr_target);
76 if (name == NULL)
77 error (_("Cannot perform pointer math on incomplete types, "
78 "try casting to a known type, or void *."));
79 else
80 error (_("Cannot perform pointer math on incomplete type \"%s\", "
81 "try casting to a known type, or void *."), name);
82 }
83 }
84 return sz;
85 }
86
87 struct value *
88 value_add (struct value *arg1, struct value *arg2)
89 {
90 struct value *valint;
91 struct value *valptr;
92 LONGEST sz;
93 struct type *type1, *type2, *valptrtype;
94
95 arg1 = coerce_array (arg1);
96 arg2 = coerce_array (arg2);
97 type1 = check_typedef (value_type (arg1));
98 type2 = check_typedef (value_type (arg2));
99
100 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
101 || TYPE_CODE (type2) == TYPE_CODE_PTR)
102 &&
103 (is_integral_type (type1) || is_integral_type (type2)))
104 /* Exactly one argument is a pointer, and one is an integer. */
105 {
106 struct value *retval;
107
108 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
109 {
110 valptr = arg1;
111 valint = arg2;
112 valptrtype = type1;
113 }
114 else
115 {
116 valptr = arg2;
117 valint = arg1;
118 valptrtype = type2;
119 }
120
121 sz = find_size_for_pointer_math (valptrtype);
122
123 retval = value_from_pointer (valptrtype,
124 value_as_address (valptr)
125 + (sz * value_as_long (valint)));
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 arg1 = coerce_array (arg1);
137 arg2 = 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
175 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
176 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
177 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
178
179 See comments in value_coerce_array() for rationale for reason for
180 doing lower bounds adjustment here rather than there.
181 FIXME: Perhaps we should validate that the index is valid and if
182 verbosity is set, warn about invalid indices (but still use them). */
183
184 struct value *
185 value_subscript (struct value *array, struct value *idx)
186 {
187 struct value *bound;
188 int c_style = current_language->c_style_arrays;
189 struct type *tarray;
190
191 array = coerce_ref (array);
192 tarray = check_typedef (value_type (array));
193
194 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
195 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
196 {
197 struct type *range_type = TYPE_INDEX_TYPE (tarray);
198 LONGEST lowerbound, upperbound;
199 get_discrete_bounds (range_type, &lowerbound, &upperbound);
200
201 if (VALUE_LVAL (array) != lval_memory)
202 return value_subscripted_rvalue (array, idx, lowerbound);
203
204 if (c_style == 0)
205 {
206 LONGEST index = value_as_long (idx);
207 if (index >= lowerbound && index <= upperbound)
208 return value_subscripted_rvalue (array, idx, lowerbound);
209 /* Emit warning unless we have an array of unknown size.
210 An array of unknown size has lowerbound 0 and upperbound -1. */
211 if (upperbound > -1)
212 warning (_("array or string index out of range"));
213 /* fall doing C stuff */
214 c_style = 1;
215 }
216
217 if (lowerbound != 0)
218 {
219 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
220 idx = value_sub (idx, bound);
221 }
222
223 array = value_coerce_array (array);
224 }
225
226 if (c_style)
227 return value_ind (value_add (array, idx));
228 else
229 error (_("not an array or string"));
230 }
231
232 /* Return the value of EXPR[IDX], expr an aggregate rvalue
233 (eg, a vector register). This routine used to promote floats
234 to doubles, but no longer does. */
235
236 struct value *
237 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
238 {
239 struct type *array_type = check_typedef (value_type (array));
240 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
241 unsigned int elt_size = TYPE_LENGTH (elt_type);
242 LONGEST index = value_as_long (idx);
243 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
244 struct value *v;
245
246 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
247 error (_("no such vector element"));
248
249 v = allocate_value (elt_type);
250 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
251 set_value_lazy (v, 1);
252 else
253 memcpy (value_contents_writeable (v),
254 value_contents (array) + elt_offs, elt_size);
255
256 if (VALUE_LVAL (array) == lval_internalvar)
257 VALUE_LVAL (v) = lval_internalvar_component;
258 else
259 VALUE_LVAL (v) = VALUE_LVAL (array);
260 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
261 VALUE_REGNUM (v) = VALUE_REGNUM (array);
262 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
263 set_value_offset (v, value_offset (array) + elt_offs);
264 return v;
265 }
266
267 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
268
269 struct value *
270 value_bitstring_subscript (struct type *type,
271 struct value *bitstring, struct value *idx)
272 {
273
274 struct type *bitstring_type, *range_type;
275 LONGEST index = value_as_long (idx);
276 struct value *v;
277 int offset, byte, bit_index;
278 LONGEST lowerbound, upperbound;
279
280 bitstring_type = check_typedef (value_type (bitstring));
281 gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
282
283 range_type = TYPE_INDEX_TYPE (bitstring_type);
284 get_discrete_bounds (range_type, &lowerbound, &upperbound);
285 if (index < lowerbound || index > upperbound)
286 error (_("bitstring index out of range"));
287
288 index -= lowerbound;
289 offset = index / TARGET_CHAR_BIT;
290 byte = *((char *) value_contents (bitstring) + offset);
291
292 bit_index = index % TARGET_CHAR_BIT;
293 byte >>= (gdbarch_bits_big_endian (current_gdbarch) ?
294 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
295
296 v = value_from_longest (type, byte & 1);
297
298 set_value_bitpos (v, bit_index);
299 set_value_bitsize (v, 1);
300
301 VALUE_LVAL (v) = VALUE_LVAL (bitstring);
302 if (VALUE_LVAL (bitstring) == lval_internalvar)
303 VALUE_LVAL (v) = lval_internalvar_component;
304 VALUE_ADDRESS (v) = VALUE_ADDRESS (bitstring);
305 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
306
307 set_value_offset (v, offset + value_offset (bitstring));
308
309 return v;
310 }
311
312 \f
313 /* Check to see if either argument is a structure, or a reference to
314 one. This is called so we know whether to go ahead with the normal
315 binop or look for a user defined function instead.
316
317 For now, we do not overload the `=' operator. */
318
319 int
320 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
321 {
322 struct type *type1, *type2;
323 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
324 return 0;
325
326 type1 = check_typedef (value_type (arg1));
327 if (TYPE_CODE (type1) == TYPE_CODE_REF)
328 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
329
330 type2 = check_typedef (value_type (arg2));
331 if (TYPE_CODE (type2) == TYPE_CODE_REF)
332 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
333
334 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
335 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
336 }
337
338 /* Check to see if argument is a structure. This is called so
339 we know whether to go ahead with the normal unop or look for a
340 user defined function instead.
341
342 For now, we do not overload the `&' operator. */
343
344 int
345 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
346 {
347 struct type *type1;
348 if (op == UNOP_ADDR)
349 return 0;
350 type1 = check_typedef (value_type (arg1));
351 for (;;)
352 {
353 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
354 return 1;
355 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
356 type1 = TYPE_TARGET_TYPE (type1);
357 else
358 return 0;
359 }
360 }
361
362 /* We know either arg1 or arg2 is a structure, so try to find the right
363 user defined function. Create an argument vector that calls
364 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
365 binary operator which is legal for GNU C++).
366
367 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
368 is the opcode saying how to modify it. Otherwise, OTHEROP is
369 unused. */
370
371 struct value *
372 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
373 enum exp_opcode otherop, enum noside noside)
374 {
375 struct value **argvec;
376 char *ptr;
377 char tstr[13];
378 int static_memfuncp;
379
380 arg1 = coerce_ref (arg1);
381 arg2 = coerce_ref (arg2);
382 arg1 = coerce_enum (arg1);
383 arg2 = coerce_enum (arg2);
384
385 /* now we know that what we have to do is construct our
386 arg vector and find the right function to call it with. */
387
388 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
389 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
390
391 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
392 argvec[1] = value_addr (arg1);
393 argvec[2] = arg2;
394 argvec[3] = 0;
395
396 /* make the right function name up */
397 strcpy (tstr, "operator__");
398 ptr = tstr + 8;
399 switch (op)
400 {
401 case BINOP_ADD:
402 strcpy (ptr, "+");
403 break;
404 case BINOP_SUB:
405 strcpy (ptr, "-");
406 break;
407 case BINOP_MUL:
408 strcpy (ptr, "*");
409 break;
410 case BINOP_DIV:
411 strcpy (ptr, "/");
412 break;
413 case BINOP_REM:
414 strcpy (ptr, "%");
415 break;
416 case BINOP_LSH:
417 strcpy (ptr, "<<");
418 break;
419 case BINOP_RSH:
420 strcpy (ptr, ">>");
421 break;
422 case BINOP_BITWISE_AND:
423 strcpy (ptr, "&");
424 break;
425 case BINOP_BITWISE_IOR:
426 strcpy (ptr, "|");
427 break;
428 case BINOP_BITWISE_XOR:
429 strcpy (ptr, "^");
430 break;
431 case BINOP_LOGICAL_AND:
432 strcpy (ptr, "&&");
433 break;
434 case BINOP_LOGICAL_OR:
435 strcpy (ptr, "||");
436 break;
437 case BINOP_MIN:
438 strcpy (ptr, "<?");
439 break;
440 case BINOP_MAX:
441 strcpy (ptr, ">?");
442 break;
443 case BINOP_ASSIGN:
444 strcpy (ptr, "=");
445 break;
446 case BINOP_ASSIGN_MODIFY:
447 switch (otherop)
448 {
449 case BINOP_ADD:
450 strcpy (ptr, "+=");
451 break;
452 case BINOP_SUB:
453 strcpy (ptr, "-=");
454 break;
455 case BINOP_MUL:
456 strcpy (ptr, "*=");
457 break;
458 case BINOP_DIV:
459 strcpy (ptr, "/=");
460 break;
461 case BINOP_REM:
462 strcpy (ptr, "%=");
463 break;
464 case BINOP_BITWISE_AND:
465 strcpy (ptr, "&=");
466 break;
467 case BINOP_BITWISE_IOR:
468 strcpy (ptr, "|=");
469 break;
470 case BINOP_BITWISE_XOR:
471 strcpy (ptr, "^=");
472 break;
473 case BINOP_MOD: /* invalid */
474 default:
475 error (_("Invalid binary operation specified."));
476 }
477 break;
478 case BINOP_SUBSCRIPT:
479 strcpy (ptr, "[]");
480 break;
481 case BINOP_EQUAL:
482 strcpy (ptr, "==");
483 break;
484 case BINOP_NOTEQUAL:
485 strcpy (ptr, "!=");
486 break;
487 case BINOP_LESS:
488 strcpy (ptr, "<");
489 break;
490 case BINOP_GTR:
491 strcpy (ptr, ">");
492 break;
493 case BINOP_GEQ:
494 strcpy (ptr, ">=");
495 break;
496 case BINOP_LEQ:
497 strcpy (ptr, "<=");
498 break;
499 case BINOP_MOD: /* invalid */
500 default:
501 error (_("Invalid binary operation specified."));
502 }
503
504 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
505
506 if (argvec[0])
507 {
508 if (static_memfuncp)
509 {
510 argvec[1] = argvec[0];
511 argvec++;
512 }
513 if (noside == EVAL_AVOID_SIDE_EFFECTS)
514 {
515 struct type *return_type;
516 return_type
517 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
518 return value_zero (return_type, VALUE_LVAL (arg1));
519 }
520 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
521 }
522 error (_("member function %s not found"), tstr);
523 #ifdef lint
524 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
525 #endif
526 }
527
528 /* We know that arg1 is a structure, so try to find a unary user
529 defined operator that matches the operator in question.
530 Create an argument vector that calls arg1.operator @ (arg1)
531 and return that value (where '@' is (almost) any unary operator which
532 is legal for GNU C++). */
533
534 struct value *
535 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
536 {
537 struct value **argvec;
538 char *ptr, *mangle_ptr;
539 char tstr[13], mangle_tstr[13];
540 int static_memfuncp, nargs;
541
542 arg1 = coerce_ref (arg1);
543 arg1 = coerce_enum (arg1);
544
545 /* now we know that what we have to do is construct our
546 arg vector and find the right function to call it with. */
547
548 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
549 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
550
551 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
552 argvec[1] = value_addr (arg1);
553 argvec[2] = 0;
554
555 nargs = 1;
556
557 /* make the right function name up */
558 strcpy (tstr, "operator__");
559 ptr = tstr + 8;
560 strcpy (mangle_tstr, "__");
561 mangle_ptr = mangle_tstr + 2;
562 switch (op)
563 {
564 case UNOP_PREINCREMENT:
565 strcpy (ptr, "++");
566 break;
567 case UNOP_PREDECREMENT:
568 strcpy (ptr, "--");
569 break;
570 case UNOP_POSTINCREMENT:
571 strcpy (ptr, "++");
572 argvec[2] = value_from_longest (builtin_type_int, 0);
573 argvec[3] = 0;
574 nargs ++;
575 break;
576 case UNOP_POSTDECREMENT:
577 strcpy (ptr, "--");
578 argvec[2] = value_from_longest (builtin_type_int, 0);
579 argvec[3] = 0;
580 nargs ++;
581 break;
582 case UNOP_LOGICAL_NOT:
583 strcpy (ptr, "!");
584 break;
585 case UNOP_COMPLEMENT:
586 strcpy (ptr, "~");
587 break;
588 case UNOP_NEG:
589 strcpy (ptr, "-");
590 break;
591 case UNOP_PLUS:
592 strcpy (ptr, "+");
593 break;
594 case UNOP_IND:
595 strcpy (ptr, "*");
596 break;
597 default:
598 error (_("Invalid unary operation specified."));
599 }
600
601 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
602
603 if (argvec[0])
604 {
605 if (static_memfuncp)
606 {
607 argvec[1] = argvec[0];
608 nargs --;
609 argvec++;
610 }
611 if (noside == EVAL_AVOID_SIDE_EFFECTS)
612 {
613 struct type *return_type;
614 return_type
615 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
616 return value_zero (return_type, VALUE_LVAL (arg1));
617 }
618 return call_function_by_hand (argvec[0], nargs, argvec + 1);
619 }
620 error (_("member function %s not found"), tstr);
621 return 0; /* For lint -- never reached */
622 }
623 \f
624
625 /* Concatenate two values with the following conditions:
626
627 (1) Both values must be either bitstring values or character string
628 values and the resulting value consists of the concatenation of
629 ARG1 followed by ARG2.
630
631 or
632
633 One value must be an integer value and the other value must be
634 either a bitstring value or character string value, which is
635 to be repeated by the number of times specified by the integer
636 value.
637
638
639 (2) Boolean values are also allowed and are treated as bit string
640 values of length 1.
641
642 (3) Character values are also allowed and are treated as character
643 string values of length 1.
644 */
645
646 struct value *
647 value_concat (struct value *arg1, struct value *arg2)
648 {
649 struct value *inval1;
650 struct value *inval2;
651 struct value *outval = NULL;
652 int inval1len, inval2len;
653 int count, idx;
654 char *ptr;
655 char inchar;
656 struct type *type1 = check_typedef (value_type (arg1));
657 struct type *type2 = check_typedef (value_type (arg2));
658
659 /* First figure out if we are dealing with two values to be concatenated
660 or a repeat count and a value to be repeated. INVAL1 is set to the
661 first of two concatenated values, or the repeat count. INVAL2 is set
662 to the second of the two concatenated values or the value to be
663 repeated. */
664
665 if (TYPE_CODE (type2) == TYPE_CODE_INT)
666 {
667 struct type *tmp = type1;
668 type1 = tmp;
669 tmp = type2;
670 inval1 = arg2;
671 inval2 = arg1;
672 }
673 else
674 {
675 inval1 = arg1;
676 inval2 = arg2;
677 }
678
679 /* Now process the input values. */
680
681 if (TYPE_CODE (type1) == TYPE_CODE_INT)
682 {
683 /* We have a repeat count. Validate the second value and then
684 construct a value repeated that many times. */
685 if (TYPE_CODE (type2) == TYPE_CODE_STRING
686 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
687 {
688 count = longest_to_int (value_as_long (inval1));
689 inval2len = TYPE_LENGTH (type2);
690 ptr = (char *) alloca (count * inval2len);
691 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
692 {
693 inchar = (char) unpack_long (type2,
694 value_contents (inval2));
695 for (idx = 0; idx < count; idx++)
696 {
697 *(ptr + idx) = inchar;
698 }
699 }
700 else
701 {
702 for (idx = 0; idx < count; idx++)
703 {
704 memcpy (ptr + (idx * inval2len), value_contents (inval2),
705 inval2len);
706 }
707 }
708 outval = value_string (ptr, count * inval2len);
709 }
710 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
711 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
712 {
713 error (_("unimplemented support for bitstring/boolean repeats"));
714 }
715 else
716 {
717 error (_("can't repeat values of that type"));
718 }
719 }
720 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
721 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
722 {
723 /* We have two character strings to concatenate. */
724 if (TYPE_CODE (type2) != TYPE_CODE_STRING
725 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
726 {
727 error (_("Strings can only be concatenated with other strings."));
728 }
729 inval1len = TYPE_LENGTH (type1);
730 inval2len = TYPE_LENGTH (type2);
731 ptr = (char *) alloca (inval1len + inval2len);
732 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
733 {
734 *ptr = (char) unpack_long (type1, value_contents (inval1));
735 }
736 else
737 {
738 memcpy (ptr, value_contents (inval1), inval1len);
739 }
740 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
741 {
742 *(ptr + inval1len) =
743 (char) unpack_long (type2, value_contents (inval2));
744 }
745 else
746 {
747 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
748 }
749 outval = value_string (ptr, inval1len + inval2len);
750 }
751 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
752 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
753 {
754 /* We have two bitstrings to concatenate. */
755 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
756 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
757 {
758 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
759 }
760 error (_("unimplemented support for bitstring/boolean concatenation."));
761 }
762 else
763 {
764 /* We don't know how to concatenate these operands. */
765 error (_("illegal operands for concatenation."));
766 }
767 return (outval);
768 }
769 \f
770 /* Return result type of OP performed on TYPE1.
771 The result type follows ANSI C rules.
772 If the result is not appropropriate for any particular language then it
773 needs to patch this function to return the correct type. */
774
775 static struct type *
776 unop_result_type (enum exp_opcode op, struct type *type1)
777 {
778 struct type *result_type;
779
780 type1 = check_typedef (type1);
781 result_type = type1;
782
783 switch (op)
784 {
785 case UNOP_PLUS:
786 case UNOP_NEG:
787 break;
788 case UNOP_COMPLEMENT:
789 /* Reject floats and decimal floats. */
790 if (!is_integral_type (type1))
791 error (_("Argument to complement operation not an integer or boolean."));
792 break;
793 default:
794 error (_("Invalid unary operation on numbers."));
795 }
796
797 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
798 || TYPE_CODE (type1) == TYPE_CODE_FLT)
799 {
800 return result_type;
801 }
802 else if (is_integral_type (type1))
803 {
804 /* Perform integral promotion for ANSI C/C++.
805 If not appropropriate for any particular language it needs to
806 modify this function to return the correct result for it. */
807 if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_type_int))
808 result_type = builtin_type_int;
809
810 return result_type;
811 }
812 else
813 {
814 error (_("Argument to unary operation not a number."));
815 return 0; /* For lint -- never reached */
816 }
817 }
818
819 /* Return result type of OP performed on TYPE1, TYPE2.
820 If the result is not appropropriate for any particular language then it
821 needs to patch this function to return the correct type. */
822
823 static struct type *
824 binop_result_type (enum exp_opcode op, struct type *type1, struct type *type2)
825 {
826 type1 = check_typedef (type1);
827 type2 = check_typedef (type2);
828
829 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
830 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
831 && !is_integral_type (type1))
832 ||
833 (TYPE_CODE (type2) != TYPE_CODE_FLT
834 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
835 && !is_integral_type (type2)))
836 error (_("Argument to arithmetic operation not a number or boolean."));
837
838 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
839 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
840 {
841 switch (op)
842 {
843 case BINOP_ADD:
844 case BINOP_SUB:
845 case BINOP_MUL:
846 case BINOP_DIV:
847 case BINOP_EXP:
848 break;
849 default:
850 error (_("Operation not valid for decimal floating point number."));
851 }
852
853 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
854 /* If type1 is not a decimal float, the type of the result is the type
855 of the decimal float argument, type2. */
856 return type2;
857 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
858 /* Same logic, for the case where type2 is not a decimal float. */
859 return type1;
860 else
861 /* Both are decimal floats, the type of the result is the bigger
862 of the two. */
863 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) ? type1 : type2;
864 }
865 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
866 || TYPE_CODE (type2) == TYPE_CODE_FLT)
867 {
868 switch (op)
869 {
870 case BINOP_ADD:
871 case BINOP_SUB:
872 case BINOP_MUL:
873 case BINOP_DIV:
874 case BINOP_EXP:
875 case BINOP_MIN:
876 case BINOP_MAX:
877 break;
878 default:
879 error (_("Integer-only operation on floating point number."));
880 }
881
882 switch (current_language->la_language)
883 {
884 case language_c:
885 case language_cplus:
886 case language_asm:
887 case language_objc:
888 /* Perform ANSI/ISO-C promotions.
889 If only one type is float, use its type.
890 Otherwise use the bigger type. */
891 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
892 return type2;
893 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
894 return type1;
895 else
896 return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) ? type1 : type2;
897
898 default:
899 /* For other languages the result type is unchanged from gdb
900 version 6.7 for backward compatibility.
901 If either arg was long double, make sure that value is also long
902 double. Otherwise use double. */
903 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (current_gdbarch)
904 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (current_gdbarch))
905 return builtin_type_long_double;
906 else
907 return builtin_type_double;
908 }
909 }
910 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
911 && TYPE_CODE (type2) == TYPE_CODE_BOOL)
912 {
913 switch (op)
914 {
915 case BINOP_BITWISE_AND:
916 case BINOP_BITWISE_IOR:
917 case BINOP_BITWISE_XOR:
918 case BINOP_EQUAL:
919 case BINOP_NOTEQUAL:
920 break;
921 default:
922 error (_("Invalid operation on booleans."));
923 }
924
925 return type1;
926 }
927 else
928 /* Integral operations here. */
929 /* FIXME: Also mixed integral/booleans, with result an integer. */
930 {
931 unsigned int promoted_len1 = TYPE_LENGTH (type1);
932 unsigned int promoted_len2 = TYPE_LENGTH (type2);
933 int is_unsigned1 = TYPE_UNSIGNED (type1);
934 int is_unsigned2 = TYPE_UNSIGNED (type2);
935 unsigned int result_len;
936 int unsigned_operation;
937
938 /* Determine type length and signedness after promotion for
939 both operands. */
940 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
941 {
942 is_unsigned1 = 0;
943 promoted_len1 = TYPE_LENGTH (builtin_type_int);
944 }
945 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
946 {
947 is_unsigned2 = 0;
948 promoted_len2 = TYPE_LENGTH (builtin_type_int);
949 }
950
951 /* Determine type length of the result, and if the operation should
952 be done unsigned. For exponentiation and shift operators,
953 use the length and type of the left operand. Otherwise,
954 use the signedness of the operand with the greater length.
955 If both operands are of equal length, use unsigned operation
956 if one of the operands is unsigned. */
957 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
958 {
959 /* In case of the shift operators and exponentiation the type of
960 the result only depends on the type of the left operand. */
961 unsigned_operation = is_unsigned1;
962 result_len = promoted_len1;
963 }
964 else if (promoted_len1 > promoted_len2)
965 {
966 unsigned_operation = is_unsigned1;
967 result_len = promoted_len1;
968 }
969 else if (promoted_len2 > promoted_len1)
970 {
971 unsigned_operation = is_unsigned2;
972 result_len = promoted_len2;
973 }
974 else
975 {
976 unsigned_operation = is_unsigned1 || is_unsigned2;
977 result_len = promoted_len1;
978 }
979
980 switch (op)
981 {
982 case BINOP_ADD:
983 case BINOP_SUB:
984 case BINOP_MUL:
985 case BINOP_DIV:
986 case BINOP_INTDIV:
987 case BINOP_EXP:
988 case BINOP_REM:
989 case BINOP_MOD:
990 case BINOP_LSH:
991 case BINOP_RSH:
992 case BINOP_BITWISE_AND:
993 case BINOP_BITWISE_IOR:
994 case BINOP_BITWISE_XOR:
995 case BINOP_LOGICAL_AND:
996 case BINOP_LOGICAL_OR:
997 case BINOP_MIN:
998 case BINOP_MAX:
999 case BINOP_EQUAL:
1000 case BINOP_NOTEQUAL:
1001 case BINOP_LESS:
1002 break;
1003
1004 default:
1005 error (_("Invalid binary operation on numbers."));
1006 }
1007
1008 switch (current_language->la_language)
1009 {
1010 case language_c:
1011 case language_cplus:
1012 case language_asm:
1013 case language_objc:
1014 if (result_len <= TYPE_LENGTH (builtin_type_int))
1015 {
1016 return (unsigned_operation
1017 ? builtin_type_unsigned_int
1018 : builtin_type_int);
1019 }
1020 else if (result_len <= TYPE_LENGTH (builtin_type_long))
1021 {
1022 return (unsigned_operation
1023 ? builtin_type_unsigned_long
1024 : builtin_type_long);
1025 }
1026 else
1027 {
1028 return (unsigned_operation
1029 ? builtin_type_unsigned_long_long
1030 : builtin_type_long_long);
1031 }
1032
1033 default:
1034 /* For other languages the result type is unchanged from gdb
1035 version 6.7 for backward compatibility.
1036 If either arg was long long, make sure that value is also long
1037 long. Otherwise use long. */
1038 if (unsigned_operation)
1039 {
1040 if (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT)
1041 return builtin_type_unsigned_long_long;
1042 else
1043 return builtin_type_unsigned_long;
1044 }
1045 else
1046 {
1047 if (result_len > gdbarch_long_bit (current_gdbarch) / HOST_CHAR_BIT)
1048 return builtin_type_long_long;
1049 else
1050 return builtin_type_long;
1051 }
1052 }
1053 }
1054
1055 return NULL; /* avoid -Wall warning */
1056 }
1057
1058 /* Integer exponentiation: V1**V2, where both arguments are
1059 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1060 static LONGEST
1061 integer_pow (LONGEST v1, LONGEST v2)
1062 {
1063 if (v2 < 0)
1064 {
1065 if (v1 == 0)
1066 error (_("Attempt to raise 0 to negative power."));
1067 else
1068 return 0;
1069 }
1070 else
1071 {
1072 /* The Russian Peasant's Algorithm */
1073 LONGEST v;
1074
1075 v = 1;
1076 for (;;)
1077 {
1078 if (v2 & 1L)
1079 v *= v1;
1080 v2 >>= 1;
1081 if (v2 == 0)
1082 return v;
1083 v1 *= v1;
1084 }
1085 }
1086 }
1087
1088 /* Integer exponentiation: V1**V2, where both arguments are
1089 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
1090 static ULONGEST
1091 uinteger_pow (ULONGEST v1, LONGEST v2)
1092 {
1093 if (v2 < 0)
1094 {
1095 if (v1 == 0)
1096 error (_("Attempt to raise 0 to negative power."));
1097 else
1098 return 0;
1099 }
1100 else
1101 {
1102 /* The Russian Peasant's Algorithm */
1103 ULONGEST v;
1104
1105 v = 1;
1106 for (;;)
1107 {
1108 if (v2 & 1L)
1109 v *= v1;
1110 v2 >>= 1;
1111 if (v2 == 0)
1112 return v;
1113 v1 *= v1;
1114 }
1115 }
1116 }
1117
1118 /* Obtain decimal value of arguments for binary operation, converting from
1119 other types if one of them is not decimal floating point. */
1120 static void
1121 value_args_as_decimal (struct value *arg1, struct value *arg2,
1122 gdb_byte *x, int *len_x, gdb_byte *y, int *len_y)
1123 {
1124 struct type *type1, *type2;
1125
1126 type1 = check_typedef (value_type (arg1));
1127 type2 = check_typedef (value_type (arg2));
1128
1129 /* At least one of the arguments must be of decimal float type. */
1130 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
1131 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
1132
1133 if (TYPE_CODE (type1) == TYPE_CODE_FLT
1134 || TYPE_CODE (type2) == TYPE_CODE_FLT)
1135 /* The DFP extension to the C language does not allow mixing of
1136 * decimal float types with other float types in expressions
1137 * (see WDTR 24732, page 12). */
1138 error (_("Mixing decimal floating types with other floating types is not allowed."));
1139
1140 /* Obtain decimal value of arg1, converting from other types
1141 if necessary. */
1142
1143 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1144 {
1145 *len_x = TYPE_LENGTH (type1);
1146 memcpy (x, value_contents (arg1), *len_x);
1147 }
1148 else if (is_integral_type (type1))
1149 {
1150 *len_x = TYPE_LENGTH (type2);
1151 decimal_from_integral (arg1, x, *len_x);
1152 }
1153 else
1154 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
1155 TYPE_NAME (type2));
1156
1157 /* Obtain decimal value of arg2, converting from other types
1158 if necessary. */
1159
1160 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
1161 {
1162 *len_y = TYPE_LENGTH (type2);
1163 memcpy (y, value_contents (arg2), *len_y);
1164 }
1165 else if (is_integral_type (type2))
1166 {
1167 *len_y = TYPE_LENGTH (type1);
1168 decimal_from_integral (arg2, y, *len_y);
1169 }
1170 else
1171 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
1172 TYPE_NAME (type2));
1173 }
1174
1175 /* Perform a binary operation on two operands which have reasonable
1176 representations as integers or floats. This includes booleans,
1177 characters, integers, or floats.
1178 Does not support addition and subtraction on pointers;
1179 use value_add or value_sub if you want to handle those possibilities. */
1180
1181 struct value *
1182 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1183 {
1184 struct value *val;
1185 struct type *result_type;
1186
1187 arg1 = coerce_ref (arg1);
1188 arg2 = coerce_ref (arg2);
1189
1190 result_type = binop_result_type (op, value_type (arg1), value_type (arg2));
1191
1192 if (TYPE_CODE (result_type) == TYPE_CODE_DECFLOAT)
1193 {
1194 struct type *v_type;
1195 int len_v1, len_v2, len_v;
1196 gdb_byte v1[16], v2[16];
1197 gdb_byte v[16];
1198
1199 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1200
1201 switch (op)
1202 {
1203 case BINOP_ADD:
1204 case BINOP_SUB:
1205 case BINOP_MUL:
1206 case BINOP_DIV:
1207 case BINOP_EXP:
1208 decimal_binop (op, v1, len_v1, v2, len_v2, v, &len_v);
1209 break;
1210
1211 default:
1212 error (_("Operation not valid for decimal floating point number."));
1213 }
1214
1215 val = value_from_decfloat (result_type, v);
1216 }
1217 else if (TYPE_CODE (result_type) == TYPE_CODE_FLT)
1218 {
1219 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
1220 in target format. real.c in GCC probably has the necessary
1221 code. */
1222 DOUBLEST v1, v2, v = 0;
1223 v1 = value_as_double (arg1);
1224 v2 = value_as_double (arg2);
1225
1226 switch (op)
1227 {
1228 case BINOP_ADD:
1229 v = v1 + v2;
1230 break;
1231
1232 case BINOP_SUB:
1233 v = v1 - v2;
1234 break;
1235
1236 case BINOP_MUL:
1237 v = v1 * v2;
1238 break;
1239
1240 case BINOP_DIV:
1241 v = v1 / v2;
1242 break;
1243
1244 case BINOP_EXP:
1245 errno = 0;
1246 v = pow (v1, v2);
1247 if (errno)
1248 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
1249 break;
1250
1251 case BINOP_MIN:
1252 v = v1 < v2 ? v1 : v2;
1253 break;
1254
1255 case BINOP_MAX:
1256 v = v1 > v2 ? v1 : v2;
1257 break;
1258
1259 default:
1260 error (_("Integer-only operation on floating point number."));
1261 }
1262
1263 val = allocate_value (result_type);
1264 store_typed_floating (value_contents_raw (val), value_type (val), v);
1265 }
1266 else if (TYPE_CODE (result_type) == TYPE_CODE_BOOL)
1267 {
1268 LONGEST v1, v2, v = 0;
1269 v1 = value_as_long (arg1);
1270 v2 = value_as_long (arg2);
1271
1272 switch (op)
1273 {
1274 case BINOP_BITWISE_AND:
1275 v = v1 & v2;
1276 break;
1277
1278 case BINOP_BITWISE_IOR:
1279 v = v1 | v2;
1280 break;
1281
1282 case BINOP_BITWISE_XOR:
1283 v = v1 ^ v2;
1284 break;
1285
1286 case BINOP_EQUAL:
1287 v = v1 == v2;
1288 break;
1289
1290 case BINOP_NOTEQUAL:
1291 v = v1 != v2;
1292 break;
1293
1294 default:
1295 error (_("Invalid operation on booleans."));
1296 }
1297
1298 val = allocate_value (result_type);
1299 store_signed_integer (value_contents_raw (val),
1300 TYPE_LENGTH (result_type),
1301 v);
1302 }
1303 else
1304 /* Integral operations here. */
1305 {
1306 int unsigned_operation = TYPE_UNSIGNED (result_type);
1307
1308 if (unsigned_operation)
1309 {
1310 unsigned int len1, len2, result_len;
1311 LONGEST v2_signed = value_as_long (arg2);
1312 ULONGEST v1, v2, v = 0;
1313 v1 = (ULONGEST) value_as_long (arg1);
1314 v2 = (ULONGEST) v2_signed;
1315
1316 /* Truncate values to the type length of the result.
1317 Things are mildly tricky because binop_result_type may
1318 return a long which on amd64 is 8 bytes, and that's a problem if
1319 ARG1, ARG2 are both <= 4 bytes: we need to truncate the values
1320 at 4 bytes not 8. So fetch the lengths of the original types
1321 and truncate at the larger of the two. */
1322 len1 = TYPE_LENGTH (value_type (arg1));
1323 len2 = TYPE_LENGTH (value_type (arg1));
1324 result_len = len1 > len2 ? len1 : len2;
1325 if (result_len < sizeof (ULONGEST))
1326 {
1327 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1328 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
1329 }
1330
1331 switch (op)
1332 {
1333 case BINOP_ADD:
1334 v = v1 + v2;
1335 break;
1336
1337 case BINOP_SUB:
1338 v = v1 - v2;
1339 break;
1340
1341 case BINOP_MUL:
1342 v = v1 * v2;
1343 break;
1344
1345 case BINOP_DIV:
1346 case BINOP_INTDIV:
1347 if (v2 != 0)
1348 v = v1 / v2;
1349 else
1350 error (_("Division by zero"));
1351 break;
1352
1353 case BINOP_EXP:
1354 v = uinteger_pow (v1, v2_signed);
1355 break;
1356
1357 case BINOP_REM:
1358 if (v2 != 0)
1359 v = v1 % v2;
1360 else
1361 error (_("Division by zero"));
1362 break;
1363
1364 case BINOP_MOD:
1365 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1366 v1 mod 0 has a defined value, v1. */
1367 if (v2 == 0)
1368 {
1369 v = v1;
1370 }
1371 else
1372 {
1373 v = v1 / v2;
1374 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1375 v = v1 - (v2 * v);
1376 }
1377 break;
1378
1379 case BINOP_LSH:
1380 v = v1 << v2;
1381 break;
1382
1383 case BINOP_RSH:
1384 v = v1 >> v2;
1385 break;
1386
1387 case BINOP_BITWISE_AND:
1388 v = v1 & v2;
1389 break;
1390
1391 case BINOP_BITWISE_IOR:
1392 v = v1 | v2;
1393 break;
1394
1395 case BINOP_BITWISE_XOR:
1396 v = v1 ^ v2;
1397 break;
1398
1399 case BINOP_LOGICAL_AND:
1400 v = v1 && v2;
1401 break;
1402
1403 case BINOP_LOGICAL_OR:
1404 v = v1 || v2;
1405 break;
1406
1407 case BINOP_MIN:
1408 v = v1 < v2 ? v1 : v2;
1409 break;
1410
1411 case BINOP_MAX:
1412 v = v1 > v2 ? v1 : v2;
1413 break;
1414
1415 case BINOP_EQUAL:
1416 v = v1 == v2;
1417 break;
1418
1419 case BINOP_NOTEQUAL:
1420 v = v1 != v2;
1421 break;
1422
1423 case BINOP_LESS:
1424 v = v1 < v2;
1425 break;
1426
1427 default:
1428 error (_("Invalid binary operation on numbers."));
1429 }
1430
1431 val = allocate_value (result_type);
1432 store_unsigned_integer (value_contents_raw (val),
1433 TYPE_LENGTH (value_type (val)),
1434 v);
1435 }
1436 else
1437 {
1438 LONGEST v1, v2, v = 0;
1439 v1 = value_as_long (arg1);
1440 v2 = value_as_long (arg2);
1441
1442 switch (op)
1443 {
1444 case BINOP_ADD:
1445 v = v1 + v2;
1446 break;
1447
1448 case BINOP_SUB:
1449 v = v1 - v2;
1450 break;
1451
1452 case BINOP_MUL:
1453 v = v1 * v2;
1454 break;
1455
1456 case BINOP_DIV:
1457 case BINOP_INTDIV:
1458 if (v2 != 0)
1459 v = v1 / v2;
1460 else
1461 error (_("Division by zero"));
1462 break;
1463
1464 case BINOP_EXP:
1465 v = integer_pow (v1, v2);
1466 break;
1467
1468 case BINOP_REM:
1469 if (v2 != 0)
1470 v = v1 % v2;
1471 else
1472 error (_("Division by zero"));
1473 break;
1474
1475 case BINOP_MOD:
1476 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1477 X mod 0 has a defined value, X. */
1478 if (v2 == 0)
1479 {
1480 v = v1;
1481 }
1482 else
1483 {
1484 v = v1 / v2;
1485 /* Compute floor. */
1486 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1487 {
1488 v--;
1489 }
1490 v = v1 - (v2 * v);
1491 }
1492 break;
1493
1494 case BINOP_LSH:
1495 v = v1 << v2;
1496 break;
1497
1498 case BINOP_RSH:
1499 v = v1 >> v2;
1500 break;
1501
1502 case BINOP_BITWISE_AND:
1503 v = v1 & v2;
1504 break;
1505
1506 case BINOP_BITWISE_IOR:
1507 v = v1 | v2;
1508 break;
1509
1510 case BINOP_BITWISE_XOR:
1511 v = v1 ^ v2;
1512 break;
1513
1514 case BINOP_LOGICAL_AND:
1515 v = v1 && v2;
1516 break;
1517
1518 case BINOP_LOGICAL_OR:
1519 v = v1 || v2;
1520 break;
1521
1522 case BINOP_MIN:
1523 v = v1 < v2 ? v1 : v2;
1524 break;
1525
1526 case BINOP_MAX:
1527 v = v1 > v2 ? v1 : v2;
1528 break;
1529
1530 case BINOP_EQUAL:
1531 v = v1 == v2;
1532 break;
1533
1534 case BINOP_LESS:
1535 v = v1 < v2;
1536 break;
1537
1538 default:
1539 error (_("Invalid binary operation on numbers."));
1540 }
1541
1542 val = allocate_value (result_type);
1543 store_signed_integer (value_contents_raw (val),
1544 TYPE_LENGTH (value_type (val)),
1545 v);
1546 }
1547 }
1548
1549 return val;
1550 }
1551 \f
1552 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1553
1554 int
1555 value_logical_not (struct value *arg1)
1556 {
1557 int len;
1558 const gdb_byte *p;
1559 struct type *type1;
1560
1561 arg1 = coerce_number (arg1);
1562 type1 = check_typedef (value_type (arg1));
1563
1564 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1565 return 0 == value_as_double (arg1);
1566 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1567 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1));
1568
1569 len = TYPE_LENGTH (type1);
1570 p = value_contents (arg1);
1571
1572 while (--len >= 0)
1573 {
1574 if (*p++)
1575 break;
1576 }
1577
1578 return len < 0;
1579 }
1580
1581 /* Perform a comparison on two string values (whose content are not
1582 necessarily null terminated) based on their length */
1583
1584 static int
1585 value_strcmp (struct value *arg1, struct value *arg2)
1586 {
1587 int len1 = TYPE_LENGTH (value_type (arg1));
1588 int len2 = TYPE_LENGTH (value_type (arg2));
1589 const gdb_byte *s1 = value_contents (arg1);
1590 const gdb_byte *s2 = value_contents (arg2);
1591 int i, len = len1 < len2 ? len1 : len2;
1592
1593 for (i = 0; i < len; i++)
1594 {
1595 if (s1[i] < s2[i])
1596 return -1;
1597 else if (s1[i] > s2[i])
1598 return 1;
1599 else
1600 continue;
1601 }
1602
1603 if (len1 < len2)
1604 return -1;
1605 else if (len1 > len2)
1606 return 1;
1607 else
1608 return 0;
1609 }
1610
1611 /* Simulate the C operator == by returning a 1
1612 iff ARG1 and ARG2 have equal contents. */
1613
1614 int
1615 value_equal (struct value *arg1, struct value *arg2)
1616 {
1617 int len;
1618 const gdb_byte *p1;
1619 const gdb_byte *p2;
1620 struct type *type1, *type2;
1621 enum type_code code1;
1622 enum type_code code2;
1623 int is_int1, is_int2;
1624
1625 arg1 = coerce_array (arg1);
1626 arg2 = coerce_array (arg2);
1627
1628 type1 = check_typedef (value_type (arg1));
1629 type2 = check_typedef (value_type (arg2));
1630 code1 = TYPE_CODE (type1);
1631 code2 = TYPE_CODE (type2);
1632 is_int1 = is_integral_type (type1);
1633 is_int2 = is_integral_type (type2);
1634
1635 if (is_int1 && is_int2)
1636 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1637 BINOP_EQUAL)));
1638 else if ((code1 == TYPE_CODE_FLT || is_int1)
1639 && (code2 == TYPE_CODE_FLT || is_int2))
1640 {
1641 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1642 `long double' values are returned in static storage (m68k). */
1643 DOUBLEST d = value_as_double (arg1);
1644 return d == value_as_double (arg2);
1645 }
1646 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1647 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1648 {
1649 gdb_byte v1[16], v2[16];
1650 int len_v1, len_v2;
1651
1652 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1653
1654 return decimal_compare (v1, len_v1, v2, len_v2) == 0;
1655 }
1656
1657 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1658 is bigger. */
1659 else if (code1 == TYPE_CODE_PTR && is_int2)
1660 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1661 else if (code2 == TYPE_CODE_PTR && is_int1)
1662 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1663
1664 else if (code1 == code2
1665 && ((len = (int) TYPE_LENGTH (type1))
1666 == (int) TYPE_LENGTH (type2)))
1667 {
1668 p1 = value_contents (arg1);
1669 p2 = value_contents (arg2);
1670 while (--len >= 0)
1671 {
1672 if (*p1++ != *p2++)
1673 break;
1674 }
1675 return len < 0;
1676 }
1677 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1678 {
1679 return value_strcmp (arg1, arg2) == 0;
1680 }
1681 else
1682 {
1683 error (_("Invalid type combination in equality test."));
1684 return 0; /* For lint -- never reached */
1685 }
1686 }
1687
1688 /* Simulate the C operator < by returning 1
1689 iff ARG1's contents are less than ARG2's. */
1690
1691 int
1692 value_less (struct value *arg1, struct value *arg2)
1693 {
1694 enum type_code code1;
1695 enum type_code code2;
1696 struct type *type1, *type2;
1697 int is_int1, is_int2;
1698
1699 arg1 = coerce_array (arg1);
1700 arg2 = coerce_array (arg2);
1701
1702 type1 = check_typedef (value_type (arg1));
1703 type2 = check_typedef (value_type (arg2));
1704 code1 = TYPE_CODE (type1);
1705 code2 = TYPE_CODE (type2);
1706 is_int1 = is_integral_type (type1);
1707 is_int2 = is_integral_type (type2);
1708
1709 if (is_int1 && is_int2)
1710 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1711 BINOP_LESS)));
1712 else if ((code1 == TYPE_CODE_FLT || is_int1)
1713 && (code2 == TYPE_CODE_FLT || is_int2))
1714 {
1715 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1716 `long double' values are returned in static storage (m68k). */
1717 DOUBLEST d = value_as_double (arg1);
1718 return d < value_as_double (arg2);
1719 }
1720 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1721 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1722 {
1723 gdb_byte v1[16], v2[16];
1724 int len_v1, len_v2;
1725
1726 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1727
1728 return decimal_compare (v1, len_v1, v2, len_v2) == -1;
1729 }
1730 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1731 return value_as_address (arg1) < value_as_address (arg2);
1732
1733 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1734 is bigger. */
1735 else if (code1 == TYPE_CODE_PTR && is_int2)
1736 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1737 else if (code2 == TYPE_CODE_PTR && is_int1)
1738 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1739 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1740 return value_strcmp (arg1, arg2) < 0;
1741 else
1742 {
1743 error (_("Invalid type combination in ordering comparison."));
1744 return 0;
1745 }
1746 }
1747 \f
1748 /* The unary operators +, - and ~. They free the argument ARG1. */
1749
1750 struct value *
1751 value_pos (struct value *arg1)
1752 {
1753 struct type *type;
1754 struct type *result_type;
1755
1756 arg1 = coerce_ref (arg1);
1757 type = check_typedef (value_type (arg1));
1758 result_type = unop_result_type (UNOP_PLUS, value_type (arg1));
1759
1760 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1761 return value_from_double (result_type, value_as_double (arg1));
1762 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1763 return value_from_decfloat (result_type, value_contents (arg1));
1764 else if (is_integral_type (type))
1765 {
1766 return value_from_longest (result_type, value_as_long (arg1));
1767 }
1768 else
1769 {
1770 error ("Argument to positive operation not a number.");
1771 return 0; /* For lint -- never reached */
1772 }
1773 }
1774
1775 struct value *
1776 value_neg (struct value *arg1)
1777 {
1778 struct type *type;
1779 struct type *result_type;
1780
1781 arg1 = coerce_ref (arg1);
1782 type = check_typedef (value_type (arg1));
1783 result_type = unop_result_type (UNOP_NEG, value_type (arg1));
1784
1785 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1786 {
1787 struct value *val = allocate_value (result_type);
1788 int len = TYPE_LENGTH (type);
1789 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
1790
1791 memcpy (decbytes, value_contents (arg1), len);
1792
1793 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
1794 decbytes[len-1] = decbytes[len - 1] | 0x80;
1795 else
1796 decbytes[0] = decbytes[0] | 0x80;
1797
1798 memcpy (value_contents_raw (val), decbytes, len);
1799 return val;
1800 }
1801 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1802 return value_from_double (result_type, -value_as_double (arg1));
1803 else if (is_integral_type (type))
1804 {
1805 return value_from_longest (result_type, -value_as_long (arg1));
1806 }
1807 else
1808 {
1809 error (_("Argument to negate operation not a number."));
1810 return 0; /* For lint -- never reached */
1811 }
1812 }
1813
1814 struct value *
1815 value_complement (struct value *arg1)
1816 {
1817 struct type *type;
1818 struct type *result_type;
1819
1820 arg1 = coerce_ref (arg1);
1821 type = check_typedef (value_type (arg1));
1822 result_type = unop_result_type (UNOP_COMPLEMENT, value_type (arg1));
1823
1824 if (!is_integral_type (type))
1825 error (_("Argument to complement operation not an integer or boolean."));
1826
1827 return value_from_longest (result_type, ~value_as_long (arg1));
1828 }
1829 \f
1830 /* The INDEX'th bit of SET value whose value_type is TYPE,
1831 and whose value_contents is valaddr.
1832 Return -1 if out of range, -2 other error. */
1833
1834 int
1835 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1836 {
1837 LONGEST low_bound, high_bound;
1838 LONGEST word;
1839 unsigned rel_index;
1840 struct type *range = TYPE_FIELD_TYPE (type, 0);
1841 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1842 return -2;
1843 if (index < low_bound || index > high_bound)
1844 return -1;
1845 rel_index = index - low_bound;
1846 word = unpack_long (builtin_type_unsigned_char,
1847 valaddr + (rel_index / TARGET_CHAR_BIT));
1848 rel_index %= TARGET_CHAR_BIT;
1849 if (gdbarch_bits_big_endian (current_gdbarch))
1850 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1851 return (word >> rel_index) & 1;
1852 }
1853
1854 struct value *
1855 value_in (struct value *element, struct value *set)
1856 {
1857 int member;
1858 struct type *settype = check_typedef (value_type (set));
1859 struct type *eltype = check_typedef (value_type (element));
1860 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1861 eltype = TYPE_TARGET_TYPE (eltype);
1862 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1863 error (_("Second argument of 'IN' has wrong type"));
1864 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1865 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1866 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1867 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1868 error (_("First argument of 'IN' has wrong type"));
1869 member = value_bit_index (settype, value_contents (set),
1870 value_as_long (element));
1871 if (member < 0)
1872 error (_("First argument of 'IN' not in range"));
1873 return value_from_longest (LA_BOOL_TYPE, member);
1874 }
1875
1876 void
1877 _initialize_valarith (void)
1878 {
1879 }