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