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