gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / valarith.c
blob7034fa6096b454d335981480122ef6805fd69eb0
1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "extract-store-integer.h"
21 #include "value.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "language.h"
27 #include "target-float.h"
28 #include "infcall.h"
29 #include "gdbsupport/byte-vector.h"
30 #include "gdbarch.h"
31 #include "rust-lang.h"
32 #include "ada-lang.h"
34 /* Forward declarations. */
35 static struct value *value_subscripted_rvalue (struct value *array,
36 LONGEST index,
37 LONGEST lowerbound);
39 /* Given a pointer, return the size of its target.
40 If the pointer type is void *, then return 1.
41 If the target type is incomplete, then error out.
42 This isn't a general purpose function, but just a
43 helper for value_ptradd. */
45 static LONGEST
46 find_size_for_pointer_math (struct type *ptr_type)
48 LONGEST sz = -1;
49 struct type *ptr_target;
51 gdb_assert (ptr_type->code () == TYPE_CODE_PTR);
52 ptr_target = check_typedef (ptr_type->target_type ());
54 sz = type_length_units (ptr_target);
55 if (sz == 0)
57 if (ptr_type->code () == TYPE_CODE_VOID)
58 sz = 1;
59 else
61 const char *name;
63 name = ptr_target->name ();
64 if (name == NULL)
65 error (_("Cannot perform pointer math on incomplete types, "
66 "try casting to a known type, or void *."));
67 else
68 error (_("Cannot perform pointer math on incomplete type \"%s\", "
69 "try casting to a known type, or void *."), name);
72 return sz;
75 /* Given a pointer ARG1 and an integral value ARG2, return the
76 result of C-style pointer arithmetic ARG1 + ARG2. */
78 struct value *
79 value_ptradd (struct value *arg1, LONGEST arg2)
81 struct type *valptrtype;
82 LONGEST sz;
83 struct value *result;
85 arg1 = coerce_array (arg1);
86 valptrtype = check_typedef (arg1->type ());
87 sz = find_size_for_pointer_math (valptrtype);
89 result = value_from_pointer (valptrtype,
90 value_as_address (arg1) + sz * arg2);
91 if (arg1->lval () != lval_internalvar)
92 result->set_component_location (arg1);
93 return result;
96 /* Given two compatible pointer values ARG1 and ARG2, return the
97 result of C-style pointer arithmetic ARG1 - ARG2. */
99 LONGEST
100 value_ptrdiff (struct value *arg1, struct value *arg2)
102 struct type *type1, *type2;
103 LONGEST sz;
105 arg1 = coerce_array (arg1);
106 arg2 = coerce_array (arg2);
107 type1 = check_typedef (arg1->type ());
108 type2 = check_typedef (arg2->type ());
110 gdb_assert (type1->code () == TYPE_CODE_PTR);
111 gdb_assert (type2->code () == TYPE_CODE_PTR);
113 if (check_typedef (type1->target_type ())->length ()
114 != check_typedef (type2->target_type ())->length ())
115 error (_("First argument of `-' is a pointer and "
116 "second argument is neither\n"
117 "an integer nor a pointer of the same type."));
119 sz = type_length_units (check_typedef (type1->target_type ()));
120 if (sz == 0)
122 warning (_("Type size unknown, assuming 1. "
123 "Try casting to a known type, or void *."));
124 sz = 1;
127 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
130 /* Return the value of ARRAY[IDX].
132 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
133 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
135 See comments in value_coerce_array() for rationale for reason for
136 doing lower bounds adjustment here rather than there.
137 FIXME: Perhaps we should validate that the index is valid and if
138 verbosity is set, warn about invalid indices (but still use them). */
140 struct value *
141 value_subscript (struct value *array, LONGEST index)
143 bool c_style = current_language->c_style_arrays_p ();
144 struct type *tarray;
146 array = coerce_ref (array);
147 tarray = check_typedef (array->type ());
149 if (tarray->code () == TYPE_CODE_ARRAY
150 || tarray->code () == TYPE_CODE_STRING)
152 struct type *range_type = tarray->index_type ();
153 std::optional<LONGEST> lowerbound = get_discrete_low_bound (range_type);
154 if (!lowerbound.has_value ())
155 lowerbound = 0;
157 if (array->lval () != lval_memory)
158 return value_subscripted_rvalue (array, index, *lowerbound);
160 std::optional<LONGEST> upperbound
161 = get_discrete_high_bound (range_type);
163 if (!upperbound.has_value ())
164 upperbound = -1;
166 if (index >= *lowerbound && index <= *upperbound)
167 return value_subscripted_rvalue (array, index, *lowerbound);
169 if (!c_style)
171 /* Emit warning unless we have an array of unknown size.
172 An array of unknown size has lowerbound 0 and upperbound -1. */
173 if (*upperbound > -1)
174 warning (_("array or string index out of range"));
175 /* fall doing C stuff */
176 c_style = true;
179 index -= *lowerbound;
181 /* Do not try to dereference a pointer to an unavailable value.
182 Instead mock up a new one and give it the original address. */
183 struct type *elt_type = check_typedef (tarray->target_type ());
184 LONGEST elt_size = type_length_units (elt_type);
185 if (!array->lazy ()
186 && !array->bytes_available (elt_size * index, elt_size))
188 struct value *val = value::allocate (elt_type);
189 val->mark_bytes_unavailable (0, elt_size);
190 val->set_lval (lval_memory);
191 val->set_address (array->address () + elt_size * index);
192 return val;
195 array = value_coerce_array (array);
198 if (c_style)
199 return value_ind (value_ptradd (array, index));
200 else
201 error (_("not an array or string"));
204 /* Return the value of EXPR[IDX], expr an aggregate rvalue
205 (eg, a vector register). This routine used to promote floats
206 to doubles, but no longer does. */
208 static struct value *
209 value_subscripted_rvalue (struct value *array, LONGEST index,
210 LONGEST lowerbound)
212 struct type *array_type = check_typedef (array->type ());
213 struct type *elt_type = array_type->target_type ();
214 LONGEST elt_size = type_length_units (elt_type);
216 /* Fetch the bit stride and convert it to a byte stride, assuming 8 bits
217 in a byte. */
218 LONGEST stride = array_type->bit_stride ();
219 if (stride != 0)
221 struct gdbarch *arch = elt_type->arch ();
222 int unit_size = gdbarch_addressable_memory_unit_size (arch);
223 elt_size = stride / (unit_size * 8);
226 LONGEST elt_offs = elt_size * (index - lowerbound);
227 bool array_upper_bound_undefined
228 = array_type->bounds ()->high.kind () == PROP_UNDEFINED;
230 if (index < lowerbound
231 || (!array_upper_bound_undefined
232 && elt_offs >= type_length_units (array_type))
233 || (array->lval () != lval_memory && array_upper_bound_undefined))
235 if (type_not_associated (array_type))
236 error (_("no such vector element (vector not associated)"));
237 else if (type_not_allocated (array_type))
238 error (_("no such vector element (vector not allocated)"));
239 else
240 error (_("no such vector element"));
243 if (is_dynamic_type (elt_type))
245 CORE_ADDR address;
247 address = array->address () + elt_offs;
248 elt_type = resolve_dynamic_type (elt_type, {}, address);
251 return value_from_component (array, elt_type, elt_offs);
254 /* See value.h. */
256 struct value *
257 value_to_array (struct value *val)
259 struct type *type = check_typedef (val->type ());
260 if (type->code () == TYPE_CODE_ARRAY)
261 return val;
263 if (type->is_array_like ())
265 const language_defn *defn = language_def (type->language ());
266 return defn->to_array (val);
268 return nullptr;
272 /* Check to see if either argument is a structure, or a reference to
273 one. This is called so we know whether to go ahead with the normal
274 binop or look for a user defined function instead.
276 For now, we do not overload the `=' operator. */
279 binop_types_user_defined_p (enum exp_opcode op,
280 struct type *type1, struct type *type2)
282 if (op == BINOP_ASSIGN)
283 return 0;
285 type1 = check_typedef (type1);
286 if (TYPE_IS_REFERENCE (type1))
287 type1 = check_typedef (type1->target_type ());
289 type2 = check_typedef (type2);
290 if (TYPE_IS_REFERENCE (type2))
291 type2 = check_typedef (type2->target_type ());
293 return (type1->code () == TYPE_CODE_STRUCT
294 || type2->code () == TYPE_CODE_STRUCT);
297 /* Check to see if either argument is a structure, or a reference to
298 one. This is called so we know whether to go ahead with the normal
299 binop or look for a user defined function instead.
301 For now, we do not overload the `=' operator. */
304 binop_user_defined_p (enum exp_opcode op,
305 struct value *arg1, struct value *arg2)
307 return binop_types_user_defined_p (op, arg1->type (), arg2->type ());
310 /* Check to see if argument is a structure. This is called so
311 we know whether to go ahead with the normal unop or look for a
312 user defined function instead.
314 For now, we do not overload the `&' operator. */
317 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
319 struct type *type1;
321 if (op == UNOP_ADDR)
322 return 0;
323 type1 = check_typedef (arg1->type ());
324 if (TYPE_IS_REFERENCE (type1))
325 type1 = check_typedef (type1->target_type ());
326 return type1->code () == TYPE_CODE_STRUCT;
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. */
337 static struct value *
338 value_user_defined_cpp_op (gdb::array_view<value *> args, char *oper,
339 int *static_memfuncp, enum noside noside)
342 struct symbol *symp = NULL;
343 struct value *valp = NULL;
345 find_overload_match (args, oper, BOTH /* could be method */,
346 &args[0] /* objp */,
347 NULL /* pass NULL symbol since symbol is unknown */,
348 &valp, &symp, static_memfuncp, 0, noside);
350 if (valp)
351 return valp;
353 if (symp)
355 /* This is a non member function and does not
356 expect a reference as its first argument
357 rather the explicit structure. */
358 args[0] = value_ind (args[0]);
359 return value_of_variable (symp, 0);
362 error (_("Could not find %s."), oper);
365 /* Lookup user defined operator NAME. Return a value representing the
366 function, otherwise return NULL. */
368 static struct value *
369 value_user_defined_op (struct value **argp, gdb::array_view<value *> args,
370 char *name, int *static_memfuncp, enum noside noside)
372 struct value *result = NULL;
374 if (current_language->la_language == language_cplus)
376 result = value_user_defined_cpp_op (args, name, static_memfuncp,
377 noside);
379 else
380 result = value_struct_elt (argp, args, name, static_memfuncp,
381 "structure");
383 return result;
386 /* We know either arg1 or arg2 is a structure, so try to find the right
387 user defined function. Create an argument vector that calls
388 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
389 binary operator which is legal for GNU C++).
391 OP is the operator, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
392 is the opcode saying how to modify it. Otherwise, OTHEROP is
393 unused. */
395 struct value *
396 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
397 enum exp_opcode otherop, enum noside noside)
399 char *ptr;
400 char tstr[13];
401 int static_memfuncp;
403 arg1 = coerce_ref (arg1);
404 arg2 = coerce_ref (arg2);
406 /* now we know that what we have to do is construct our
407 arg vector and find the right function to call it with. */
409 if (check_typedef (arg1->type ())->code () != TYPE_CODE_STRUCT)
410 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
412 value *argvec_storage[3];
413 gdb::array_view<value *> argvec = argvec_storage;
415 argvec[1] = value_addr (arg1);
416 argvec[2] = arg2;
418 /* Make the right function name up. */
419 strcpy (tstr, "operator__");
420 ptr = tstr + 8;
421 switch (op)
423 case BINOP_ADD:
424 strcpy (ptr, "+");
425 break;
426 case BINOP_SUB:
427 strcpy (ptr, "-");
428 break;
429 case BINOP_MUL:
430 strcpy (ptr, "*");
431 break;
432 case BINOP_DIV:
433 strcpy (ptr, "/");
434 break;
435 case BINOP_REM:
436 strcpy (ptr, "%");
437 break;
438 case BINOP_LSH:
439 strcpy (ptr, "<<");
440 break;
441 case BINOP_RSH:
442 strcpy (ptr, ">>");
443 break;
444 case BINOP_BITWISE_AND:
445 strcpy (ptr, "&");
446 break;
447 case BINOP_BITWISE_IOR:
448 strcpy (ptr, "|");
449 break;
450 case BINOP_BITWISE_XOR:
451 strcpy (ptr, "^");
452 break;
453 case BINOP_LOGICAL_AND:
454 strcpy (ptr, "&&");
455 break;
456 case BINOP_LOGICAL_OR:
457 strcpy (ptr, "||");
458 break;
459 case BINOP_MIN:
460 strcpy (ptr, "<?");
461 break;
462 case BINOP_MAX:
463 strcpy (ptr, ">?");
464 break;
465 case BINOP_ASSIGN:
466 strcpy (ptr, "=");
467 break;
468 case BINOP_ASSIGN_MODIFY:
469 switch (otherop)
471 case BINOP_ADD:
472 strcpy (ptr, "+=");
473 break;
474 case BINOP_SUB:
475 strcpy (ptr, "-=");
476 break;
477 case BINOP_MUL:
478 strcpy (ptr, "*=");
479 break;
480 case BINOP_DIV:
481 strcpy (ptr, "/=");
482 break;
483 case BINOP_REM:
484 strcpy (ptr, "%=");
485 break;
486 case BINOP_BITWISE_AND:
487 strcpy (ptr, "&=");
488 break;
489 case BINOP_BITWISE_IOR:
490 strcpy (ptr, "|=");
491 break;
492 case BINOP_BITWISE_XOR:
493 strcpy (ptr, "^=");
494 break;
495 case BINOP_MOD: /* invalid */
496 default:
497 error (_("Invalid binary operation specified."));
499 break;
500 case BINOP_SUBSCRIPT:
501 strcpy (ptr, "[]");
502 break;
503 case BINOP_EQUAL:
504 strcpy (ptr, "==");
505 break;
506 case BINOP_NOTEQUAL:
507 strcpy (ptr, "!=");
508 break;
509 case BINOP_LESS:
510 strcpy (ptr, "<");
511 break;
512 case BINOP_GTR:
513 strcpy (ptr, ">");
514 break;
515 case BINOP_GEQ:
516 strcpy (ptr, ">=");
517 break;
518 case BINOP_LEQ:
519 strcpy (ptr, "<=");
520 break;
521 case BINOP_MOD: /* invalid */
522 default:
523 error (_("Invalid binary operation specified."));
526 argvec[0] = value_user_defined_op (&arg1, argvec.slice (1), tstr,
527 &static_memfuncp, noside);
529 if (argvec[0])
531 if (static_memfuncp)
533 argvec[1] = argvec[0];
534 argvec = argvec.slice (1);
536 if (argvec[0]->type ()->code () == TYPE_CODE_XMETHOD)
538 /* Static xmethods are not supported yet. */
539 gdb_assert (static_memfuncp == 0);
540 if (noside == EVAL_AVOID_SIDE_EFFECTS)
542 struct type *return_type
543 = argvec[0]->result_type_of_xmethod (argvec.slice (1));
545 if (return_type == NULL)
546 error (_("Xmethod is missing return type."));
547 return value::zero (return_type, arg1->lval ());
549 return argvec[0]->call_xmethod (argvec.slice (1));
551 if (noside == EVAL_AVOID_SIDE_EFFECTS)
553 struct type *return_type;
555 return_type = check_typedef (argvec[0]->type ())->target_type ();
556 return value::zero (return_type, arg1->lval ());
558 return call_function_by_hand (argvec[0], NULL,
559 argvec.slice (1, 2 - static_memfuncp));
561 throw_error (NOT_FOUND_ERROR,
562 _("member function %s not found"), tstr);
565 /* We know that arg1 is a structure, so try to find a unary user
566 defined operator that matches the operator in question.
567 Create an argument vector that calls arg1.operator @ (arg1)
568 and return that value (where '@' is (almost) any unary operator which
569 is legal for GNU C++). */
571 struct value *
572 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
574 struct gdbarch *gdbarch = arg1->type ()->arch ();
575 char *ptr;
576 char tstr[13], mangle_tstr[13];
577 int static_memfuncp, nargs;
579 arg1 = coerce_ref (arg1);
581 /* now we know that what we have to do is construct our
582 arg vector and find the right function to call it with. */
584 if (check_typedef (arg1->type ())->code () != TYPE_CODE_STRUCT)
585 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
587 value *argvec_storage[3];
588 gdb::array_view<value *> argvec = argvec_storage;
590 argvec[1] = value_addr (arg1);
591 argvec[2] = 0;
593 nargs = 1;
595 /* Make the right function name up. */
596 strcpy (tstr, "operator__");
597 ptr = tstr + 8;
598 strcpy (mangle_tstr, "__");
599 switch (op)
601 case UNOP_PREINCREMENT:
602 strcpy (ptr, "++");
603 break;
604 case UNOP_PREDECREMENT:
605 strcpy (ptr, "--");
606 break;
607 case UNOP_POSTINCREMENT:
608 strcpy (ptr, "++");
609 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
610 nargs ++;
611 break;
612 case UNOP_POSTDECREMENT:
613 strcpy (ptr, "--");
614 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
615 nargs ++;
616 break;
617 case UNOP_LOGICAL_NOT:
618 strcpy (ptr, "!");
619 break;
620 case UNOP_COMPLEMENT:
621 strcpy (ptr, "~");
622 break;
623 case UNOP_NEG:
624 strcpy (ptr, "-");
625 break;
626 case UNOP_PLUS:
627 strcpy (ptr, "+");
628 break;
629 case UNOP_IND:
630 strcpy (ptr, "*");
631 break;
632 case STRUCTOP_PTR:
633 strcpy (ptr, "->");
634 break;
635 default:
636 error (_("Invalid unary operation specified."));
639 argvec[0] = value_user_defined_op (&arg1, argvec.slice (1, nargs), tstr,
640 &static_memfuncp, noside);
642 if (argvec[0])
644 if (static_memfuncp)
646 argvec[1] = argvec[0];
647 argvec = argvec.slice (1);
649 if (argvec[0]->type ()->code () == TYPE_CODE_XMETHOD)
651 /* Static xmethods are not supported yet. */
652 gdb_assert (static_memfuncp == 0);
653 if (noside == EVAL_AVOID_SIDE_EFFECTS)
655 struct type *return_type
656 = argvec[0]->result_type_of_xmethod (argvec[1]);
658 if (return_type == NULL)
659 error (_("Xmethod is missing return type."));
660 return value::zero (return_type, arg1->lval ());
662 return argvec[0]->call_xmethod (argvec[1]);
664 if (noside == EVAL_AVOID_SIDE_EFFECTS)
666 struct type *return_type;
668 return_type = check_typedef (argvec[0]->type ())->target_type ();
669 return value::zero (return_type, arg1->lval ());
671 return call_function_by_hand (argvec[0], NULL,
672 argvec.slice (1, nargs));
674 throw_error (NOT_FOUND_ERROR,
675 _("member function %s not found"), tstr);
679 /* Concatenate two values. One value must be an array; and the other
680 value must either be an array with the same element type, or be of
681 the array's element type. */
683 struct value *
684 value_concat (struct value *arg1, struct value *arg2)
686 struct type *type1 = check_typedef (arg1->type ());
687 struct type *type2 = check_typedef (arg2->type ());
689 if (type1->code () != TYPE_CODE_ARRAY && type2->code () != TYPE_CODE_ARRAY)
690 error ("no array provided to concatenation");
692 LONGEST low1, high1;
693 struct type *elttype1 = type1;
694 if (elttype1->code () == TYPE_CODE_ARRAY)
696 elttype1 = elttype1->target_type ();
697 if (!get_array_bounds (type1, &low1, &high1))
698 error (_("could not determine array bounds on left-hand-side of "
699 "array concatenation"));
701 else
703 low1 = 0;
704 high1 = 0;
707 LONGEST low2, high2;
708 struct type *elttype2 = type2;
709 if (elttype2->code () == TYPE_CODE_ARRAY)
711 elttype2 = elttype2->target_type ();
712 if (!get_array_bounds (type2, &low2, &high2))
713 error (_("could not determine array bounds on right-hand-side of "
714 "array concatenation"));
716 else
718 low2 = 0;
719 high2 = 0;
722 if (!types_equal (elttype1, elttype2))
723 error (_("concatenation with different element types"));
725 LONGEST lowbound = current_language->c_style_arrays_p () ? 0 : 1;
726 LONGEST n_elts = (high1 - low1 + 1) + (high2 - low2 + 1);
727 struct type *atype = lookup_array_range_type (elttype1,
728 lowbound,
729 lowbound + n_elts - 1);
731 struct value *result = value::allocate (atype);
732 gdb::array_view<gdb_byte> contents = result->contents_raw ();
733 gdb::array_view<const gdb_byte> lhs_contents = arg1->contents ();
734 gdb::array_view<const gdb_byte> rhs_contents = arg2->contents ();
735 gdb::copy (lhs_contents, contents.slice (0, lhs_contents.size ()));
736 gdb::copy (rhs_contents, contents.slice (lhs_contents.size ()));
738 return result;
742 /* Obtain argument values for binary operation, converting from
743 other types if one of them is not floating point. */
744 static void
745 value_args_as_target_float (struct value *arg1, struct value *arg2,
746 gdb_byte *x, struct type **eff_type_x,
747 gdb_byte *y, struct type **eff_type_y)
749 struct type *type1, *type2;
751 type1 = check_typedef (arg1->type ());
752 type2 = check_typedef (arg2->type ());
754 /* At least one of the arguments must be of floating-point type. */
755 gdb_assert (is_floating_type (type1) || is_floating_type (type2));
757 if (is_floating_type (type1) && is_floating_type (type2)
758 && type1->code () != type2->code ())
759 /* The DFP extension to the C language does not allow mixing of
760 * decimal float types with other float types in expressions
761 * (see WDTR 24732, page 12). */
762 error (_("Mixing decimal floating types with "
763 "other floating types is not allowed."));
765 /* Obtain value of arg1, converting from other types if necessary. */
767 if (is_floating_type (type1))
769 *eff_type_x = type1;
770 memcpy (x, arg1->contents ().data (), type1->length ());
772 else if (is_integral_type (type1))
774 *eff_type_x = type2;
775 if (type1->is_unsigned ())
776 target_float_from_ulongest (x, *eff_type_x, value_as_long (arg1));
777 else
778 target_float_from_longest (x, *eff_type_x, value_as_long (arg1));
780 else
781 error (_("Don't know how to convert from %s to %s."), type1->name (),
782 type2->name ());
784 /* Obtain value of arg2, converting from other types if necessary. */
786 if (is_floating_type (type2))
788 *eff_type_y = type2;
789 memcpy (y, arg2->contents ().data (), type2->length ());
791 else if (is_integral_type (type2))
793 *eff_type_y = type1;
794 if (type2->is_unsigned ())
795 target_float_from_ulongest (y, *eff_type_y, value_as_long (arg2));
796 else
797 target_float_from_longest (y, *eff_type_y, value_as_long (arg2));
799 else
800 error (_("Don't know how to convert from %s to %s."), type1->name (),
801 type2->name ());
804 /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
805 perform the binary operation OP on these two operands, and return
806 the resulting value (also as a fixed point). */
808 static struct value *
809 fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
811 struct type *type1 = check_typedef (arg1->type ());
812 struct type *type2 = check_typedef (arg2->type ());
813 const struct language_defn *language = current_language;
815 struct gdbarch *gdbarch = type1->arch ();
816 struct value *val;
818 gdb_mpq v1, v2, res;
820 gdb_assert (is_fixed_point_type (type1) || is_fixed_point_type (type2));
821 if (op == BINOP_MUL || op == BINOP_DIV)
823 v1 = value_to_gdb_mpq (arg1);
824 v2 = value_to_gdb_mpq (arg2);
826 /* The code below uses TYPE1 for the result type, so make sure
827 it is set properly. */
828 if (!is_fixed_point_type (type1))
829 type1 = type2;
831 else
833 if (!is_fixed_point_type (type1))
835 arg1 = value_cast (type2, arg1);
836 type1 = type2;
838 if (!is_fixed_point_type (type2))
840 arg2 = value_cast (type1, arg2);
841 type2 = type1;
844 v1.read_fixed_point (arg1->contents (),
845 type_byte_order (type1), type1->is_unsigned (),
846 type1->fixed_point_scaling_factor ());
847 v2.read_fixed_point (arg2->contents (),
848 type_byte_order (type2), type2->is_unsigned (),
849 type2->fixed_point_scaling_factor ());
852 auto fixed_point_to_value = [type1] (const gdb_mpq &fp)
854 value *fp_val = value::allocate (type1);
856 fp.write_fixed_point
857 (fp_val->contents_raw (),
858 type_byte_order (type1),
859 type1->is_unsigned (),
860 type1->fixed_point_scaling_factor ());
862 return fp_val;
865 switch (op)
867 case BINOP_ADD:
868 res = v1 + v2;
869 val = fixed_point_to_value (res);
870 break;
872 case BINOP_SUB:
873 res = v1 - v2;
874 val = fixed_point_to_value (res);
875 break;
877 case BINOP_MIN:
878 val = fixed_point_to_value (std::min (v1, v2));
879 break;
881 case BINOP_MAX:
882 val = fixed_point_to_value (std::max (v1, v2));
883 break;
885 case BINOP_MUL:
886 res = v1 * v2;
887 val = fixed_point_to_value (res);
888 break;
890 case BINOP_DIV:
891 if (v2.sgn () == 0)
892 error (_("Division by zero"));
893 res = v1 / v2;
894 val = fixed_point_to_value (res);
895 break;
897 case BINOP_EQUAL:
898 val = value_from_ulongest (language_bool_type (language, gdbarch),
899 v1 == v2 ? 1 : 0);
900 break;
902 case BINOP_LESS:
903 val = value_from_ulongest (language_bool_type (language, gdbarch),
904 v1 < v2 ? 1 : 0);
905 break;
907 default:
908 error (_("Integer-only operation on fixed point number."));
911 return val;
914 /* A helper function that finds the type to use for a binary operation
915 involving TYPE1 and TYPE2. */
917 static struct type *
918 promotion_type (struct type *type1, struct type *type2)
920 struct type *result_type;
922 if (is_floating_type (type1) || is_floating_type (type2))
924 /* If only one type is floating-point, use its type.
925 Otherwise use the bigger type. */
926 if (!is_floating_type (type1))
927 result_type = type2;
928 else if (!is_floating_type (type2))
929 result_type = type1;
930 else if (type2->length () > type1->length ())
931 result_type = type2;
932 else
933 result_type = type1;
935 else
937 /* Integer types. */
938 if (type1->length () > type2->length ())
939 result_type = type1;
940 else if (type2->length () > type1->length ())
941 result_type = type2;
942 else if (type1->is_unsigned ())
943 result_type = type1;
944 else if (type2->is_unsigned ())
945 result_type = type2;
946 else
947 result_type = type1;
950 return result_type;
953 static struct value *scalar_binop (struct value *arg1, struct value *arg2,
954 enum exp_opcode op);
956 /* Perform a binary operation on complex operands. */
958 static struct value *
959 complex_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
961 struct type *arg1_type = check_typedef (arg1->type ());
962 struct type *arg2_type = check_typedef (arg2->type ());
964 struct value *arg1_real, *arg1_imag, *arg2_real, *arg2_imag;
965 if (arg1_type->code () == TYPE_CODE_COMPLEX)
967 arg1_real = value_real_part (arg1);
968 arg1_imag = value_imaginary_part (arg1);
970 else
972 arg1_real = arg1;
973 arg1_imag = value::zero (arg1_type, not_lval);
975 if (arg2_type->code () == TYPE_CODE_COMPLEX)
977 arg2_real = value_real_part (arg2);
978 arg2_imag = value_imaginary_part (arg2);
980 else
982 arg2_real = arg2;
983 arg2_imag = value::zero (arg2_type, not_lval);
986 struct type *comp_type = promotion_type (arg1_real->type (),
987 arg2_real->type ());
988 if (!can_create_complex_type (comp_type))
989 error (_("Argument to complex arithmetic operation not supported."));
991 arg1_real = value_cast (comp_type, arg1_real);
992 arg1_imag = value_cast (comp_type, arg1_imag);
993 arg2_real = value_cast (comp_type, arg2_real);
994 arg2_imag = value_cast (comp_type, arg2_imag);
996 struct type *result_type = init_complex_type (nullptr, comp_type);
998 struct value *result_real, *result_imag;
999 switch (op)
1001 case BINOP_ADD:
1002 case BINOP_SUB:
1003 result_real = scalar_binop (arg1_real, arg2_real, op);
1004 result_imag = scalar_binop (arg1_imag, arg2_imag, op);
1005 break;
1007 case BINOP_MUL:
1009 struct value *x1 = scalar_binop (arg1_real, arg2_real, op);
1010 struct value *x2 = scalar_binop (arg1_imag, arg2_imag, op);
1011 result_real = scalar_binop (x1, x2, BINOP_SUB);
1013 x1 = scalar_binop (arg1_real, arg2_imag, op);
1014 x2 = scalar_binop (arg1_imag, arg2_real, op);
1015 result_imag = scalar_binop (x1, x2, BINOP_ADD);
1017 break;
1019 case BINOP_DIV:
1021 if (arg2_type->code () == TYPE_CODE_COMPLEX)
1023 struct value *conjugate = value_complement (arg2);
1024 /* We have to reconstruct ARG1, in case the type was
1025 promoted. */
1026 arg1 = value_literal_complex (arg1_real, arg1_imag, result_type);
1028 struct value *numerator = scalar_binop (arg1, conjugate,
1029 BINOP_MUL);
1030 arg1_real = value_real_part (numerator);
1031 arg1_imag = value_imaginary_part (numerator);
1033 struct value *x1 = scalar_binop (arg2_real, arg2_real, BINOP_MUL);
1034 struct value *x2 = scalar_binop (arg2_imag, arg2_imag, BINOP_MUL);
1035 arg2_real = scalar_binop (x1, x2, BINOP_ADD);
1038 result_real = scalar_binop (arg1_real, arg2_real, op);
1039 result_imag = scalar_binop (arg1_imag, arg2_real, op);
1041 break;
1043 case BINOP_EQUAL:
1044 case BINOP_NOTEQUAL:
1046 struct value *x1 = scalar_binop (arg1_real, arg2_real, op);
1047 struct value *x2 = scalar_binop (arg1_imag, arg2_imag, op);
1049 LONGEST v1 = value_as_long (x1);
1050 LONGEST v2 = value_as_long (x2);
1052 if (op == BINOP_EQUAL)
1053 v1 = v1 && v2;
1054 else
1055 v1 = v1 || v2;
1057 return value_from_longest (x1->type (), v1);
1059 break;
1061 default:
1062 error (_("Invalid binary operation on numbers."));
1065 return value_literal_complex (result_real, result_imag, result_type);
1068 /* Return the type's length in bits. */
1070 static int
1071 type_length_bits (type *type)
1073 int unit_size = gdbarch_addressable_memory_unit_size (type->arch ());
1074 return unit_size * 8 * type->length ();
1077 /* Check whether the RHS value of a shift is valid in C/C++ semantics.
1078 SHIFT_COUNT is the shift amount, SHIFT_COUNT_TYPE is the type of
1079 the shift count value, used to determine whether the type is
1080 signed, and RESULT_TYPE is the result type. This is used to avoid
1081 both negative and too-large shift amounts, which are undefined, and
1082 would crash a GDB built with UBSan. Depending on the current
1083 language, if the shift is not valid, this either warns and returns
1084 false, or errors out. Returns true and sets NBITS if valid. */
1086 static bool
1087 check_valid_shift_count (enum exp_opcode op, type *result_type,
1088 type *shift_count_type, const gdb_mpz &shift_count,
1089 unsigned long &nbits)
1091 if (!shift_count_type->is_unsigned ())
1093 LONGEST count = shift_count.as_integer<LONGEST> ();
1094 if (count < 0)
1096 auto error_or_warning = [] (const char *msg)
1098 /* Shifts by a negative amount are always an error in Go. Other
1099 languages are more permissive and their compilers just warn or
1100 have modes to disable the errors. */
1101 if (current_language->la_language == language_go)
1102 error (("%s"), msg);
1103 else
1104 warning (("%s"), msg);
1107 if (op == BINOP_RSH)
1108 error_or_warning (_("right shift count is negative"));
1109 else
1110 error_or_warning (_("left shift count is negative"));
1111 return false;
1115 nbits = shift_count.as_integer<unsigned long> ();
1116 if (nbits >= type_length_bits (result_type))
1118 /* In Go, shifting by large amounts is defined. Be silent and
1119 still return false, as the caller's error path does the right
1120 thing for Go. */
1121 if (current_language->la_language != language_go)
1123 if (op == BINOP_RSH)
1124 warning (_("right shift count >= width of type"));
1125 else
1126 warning (_("left shift count >= width of type"));
1128 return false;
1131 return true;
1134 /* Perform a binary operation on two operands which have reasonable
1135 representations as integers or floats. This includes booleans,
1136 characters, integers, or floats.
1137 Does not support addition and subtraction on pointers;
1138 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1140 static struct value *
1141 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1143 struct value *val;
1144 struct type *type1, *type2, *result_type;
1146 arg1 = coerce_ref (arg1);
1147 arg2 = coerce_ref (arg2);
1149 type1 = check_typedef (arg1->type ());
1150 type2 = check_typedef (arg2->type ());
1152 if (type1->code () == TYPE_CODE_COMPLEX
1153 || type2->code () == TYPE_CODE_COMPLEX)
1154 return complex_binop (arg1, arg2, op);
1156 if ((!is_floating_value (arg1)
1157 && !is_integral_type (type1)
1158 && !is_fixed_point_type (type1))
1159 || (!is_floating_value (arg2)
1160 && !is_integral_type (type2)
1161 && !is_fixed_point_type (type2)))
1162 error (_("Argument to arithmetic operation not a number or boolean."));
1164 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
1165 return fixed_point_binop (arg1, arg2, op);
1167 if (is_floating_type (type1) || is_floating_type (type2))
1169 result_type = promotion_type (type1, type2);
1170 val = value::allocate (result_type);
1172 struct type *eff_type_v1, *eff_type_v2;
1173 gdb::byte_vector v1, v2;
1174 v1.resize (result_type->length ());
1175 v2.resize (result_type->length ());
1177 value_args_as_target_float (arg1, arg2,
1178 v1.data (), &eff_type_v1,
1179 v2.data (), &eff_type_v2);
1180 target_float_binop (op, v1.data (), eff_type_v1,
1181 v2.data (), eff_type_v2,
1182 val->contents_raw ().data (), result_type);
1184 else if (type1->code () == TYPE_CODE_BOOL
1185 || type2->code () == TYPE_CODE_BOOL)
1187 LONGEST v1, v2, v = 0;
1189 v1 = value_as_long (arg1);
1190 v2 = value_as_long (arg2);
1192 switch (op)
1194 case BINOP_BITWISE_AND:
1195 v = v1 & v2;
1196 break;
1198 case BINOP_BITWISE_IOR:
1199 v = v1 | v2;
1200 break;
1202 case BINOP_BITWISE_XOR:
1203 v = v1 ^ v2;
1204 break;
1206 case BINOP_EQUAL:
1207 v = v1 == v2;
1208 break;
1210 case BINOP_NOTEQUAL:
1211 v = v1 != v2;
1212 break;
1214 default:
1215 error (_("Invalid operation on booleans."));
1218 result_type = type1;
1220 val = value::allocate (result_type);
1221 store_signed_integer (val->contents_raw ().data (),
1222 result_type->length (),
1223 type_byte_order (result_type),
1226 else
1227 /* Integral operations here. */
1229 /* Determine type length of the result, and if the operation should
1230 be done unsigned. For exponentiation and shift operators,
1231 use the length and type of the left operand. Otherwise,
1232 use the signedness of the operand with the greater length.
1233 If both operands are of equal length, use unsigned operation
1234 if one of the operands is unsigned. */
1235 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1236 result_type = type1;
1237 else
1238 result_type = promotion_type (type1, type2);
1240 gdb_mpz v1 = value_as_mpz (arg1);
1241 gdb_mpz v2 = value_as_mpz (arg2);
1242 gdb_mpz v;
1244 switch (op)
1246 case BINOP_ADD:
1247 v = v1 + v2;
1248 break;
1250 case BINOP_SUB:
1251 v = v1 - v2;
1252 break;
1254 case BINOP_MUL:
1255 v = v1 * v2;
1256 break;
1258 case BINOP_DIV:
1259 case BINOP_INTDIV:
1260 if (v2.sgn () != 0)
1261 v = v1 / v2;
1262 else
1263 error (_("Division by zero"));
1264 break;
1266 case BINOP_EXP:
1267 v = v1.pow (v2.as_integer<unsigned long> ());
1268 break;
1270 case BINOP_REM:
1271 if (v2.sgn () != 0)
1272 v = v1 % v2;
1273 else
1274 error (_("Division by zero"));
1275 break;
1277 case BINOP_MOD:
1278 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1279 v1 mod 0 has a defined value, v1. */
1280 if (v2.sgn () == 0)
1282 v = v1;
1284 else
1286 v = v1 / v2;
1287 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1288 v = v1 - (v2 * v);
1290 break;
1292 case BINOP_LSH:
1294 unsigned long nbits;
1295 if (!check_valid_shift_count (op, result_type, type2, v2, nbits))
1296 v = 0;
1297 else
1298 v = v1 << nbits;
1300 break;
1302 case BINOP_RSH:
1304 unsigned long nbits;
1305 if (!check_valid_shift_count (op, result_type, type2, v2, nbits))
1306 v = 0;
1307 else
1308 v = v1 >> nbits;
1310 break;
1312 case BINOP_BITWISE_AND:
1313 v = v1 & v2;
1314 break;
1316 case BINOP_BITWISE_IOR:
1317 v = v1 | v2;
1318 break;
1320 case BINOP_BITWISE_XOR:
1321 v = v1 ^ v2;
1322 break;
1324 case BINOP_MIN:
1325 v = v1 < v2 ? v1 : v2;
1326 break;
1328 case BINOP_MAX:
1329 v = v1 > v2 ? v1 : v2;
1330 break;
1332 case BINOP_EQUAL:
1333 v = v1 == v2;
1334 break;
1336 case BINOP_NOTEQUAL:
1337 v = v1 != v2;
1338 break;
1340 case BINOP_LESS:
1341 v = v1 < v2;
1342 break;
1344 case BINOP_GTR:
1345 v = v1 > v2;
1346 break;
1348 case BINOP_LEQ:
1349 v = v1 <= v2;
1350 break;
1352 case BINOP_GEQ:
1353 v = v1 >= v2;
1354 break;
1356 default:
1357 error (_("Invalid binary operation on numbers."));
1360 val = value_from_mpz (result_type, v);
1363 return val;
1366 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1367 replicating SCALAR_VALUE for each element of the vector. Only scalar
1368 types that can be cast to the type of one element of the vector are
1369 acceptable. The newly created vector value is returned upon success,
1370 otherwise an error is thrown. */
1372 struct value *
1373 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1375 /* Widen the scalar to a vector. */
1376 struct type *eltype, *scalar_type;
1377 struct value *elval;
1378 LONGEST low_bound, high_bound;
1379 int i;
1381 vector_type = check_typedef (vector_type);
1383 gdb_assert (vector_type->code () == TYPE_CODE_ARRAY
1384 && vector_type->is_vector ());
1386 if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1387 error (_("Could not determine the vector bounds"));
1389 eltype = check_typedef (vector_type->target_type ());
1390 elval = value_cast (eltype, scalar_value);
1392 scalar_type = check_typedef (scalar_value->type ());
1394 /* If we reduced the length of the scalar then check we didn't loose any
1395 important bits. */
1396 if (eltype->length () < scalar_type->length ()
1397 && !value_equal (elval, scalar_value))
1398 error (_("conversion of scalar to vector involves truncation"));
1400 value *val = value::allocate (vector_type);
1401 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1402 int elt_len = eltype->length ();
1404 for (i = 0; i < high_bound - low_bound + 1; i++)
1405 /* Duplicate the contents of elval into the destination vector. */
1406 copy (elval->contents_all (),
1407 val_contents.slice (i * elt_len, elt_len));
1409 return val;
1412 /* Performs a binary operation on two vector operands by calling scalar_binop
1413 for each pair of vector components. */
1415 static struct value *
1416 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1418 struct type *type1, *type2, *eltype1, *eltype2;
1419 int t1_is_vec, t2_is_vec, elsize, i;
1420 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1422 type1 = check_typedef (val1->type ());
1423 type2 = check_typedef (val2->type ());
1425 t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
1426 && type1->is_vector ()) ? 1 : 0;
1427 t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
1428 && type2->is_vector ()) ? 1 : 0;
1430 if (!t1_is_vec || !t2_is_vec)
1431 error (_("Vector operations are only supported among vectors"));
1433 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1434 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1435 error (_("Could not determine the vector bounds"));
1437 eltype1 = check_typedef (type1->target_type ());
1438 eltype2 = check_typedef (type2->target_type ());
1439 elsize = eltype1->length ();
1441 if (eltype1->code () != eltype2->code ()
1442 || elsize != eltype2->length ()
1443 || eltype1->is_unsigned () != eltype2->is_unsigned ()
1444 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1445 error (_("Cannot perform operation on vectors with different types"));
1447 value *val = value::allocate (type1);
1448 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1449 scoped_value_mark mark;
1450 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1452 value *tmp = value_binop (value_subscript (val1, i),
1453 value_subscript (val2, i), op);
1454 copy (tmp->contents_all (),
1455 val_contents.slice (i * elsize, elsize));
1458 return val;
1461 /* Perform a binary operation on two operands. */
1463 struct value *
1464 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1466 struct value *val;
1467 struct type *type1 = check_typedef (arg1->type ());
1468 struct type *type2 = check_typedef (arg2->type ());
1469 int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
1470 && type1->is_vector ());
1471 int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
1472 && type2->is_vector ());
1474 if (!t1_is_vec && !t2_is_vec)
1475 val = scalar_binop (arg1, arg2, op);
1476 else if (t1_is_vec && t2_is_vec)
1477 val = vector_binop (arg1, arg2, op);
1478 else
1480 /* Widen the scalar operand to a vector. */
1481 struct value **v = t1_is_vec ? &arg2 : &arg1;
1482 struct type *t = t1_is_vec ? type2 : type1;
1484 if (t->code () != TYPE_CODE_FLT
1485 && t->code () != TYPE_CODE_DECFLOAT
1486 && !is_integral_type (t))
1487 error (_("Argument to operation not a number or boolean."));
1489 /* Replicate the scalar value to make a vector value. */
1490 *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1492 val = vector_binop (arg1, arg2, op);
1495 return val;
1498 /* See value.h. */
1500 bool
1501 value_logical_not (struct value *arg1)
1503 int len;
1504 const gdb_byte *p;
1505 struct type *type1;
1507 arg1 = coerce_array (arg1);
1508 type1 = check_typedef (arg1->type ());
1510 if (is_floating_value (arg1))
1511 return target_float_is_zero (arg1->contents ().data (), type1);
1513 len = type1->length ();
1514 p = arg1->contents ().data ();
1516 while (--len >= 0)
1518 if (*p++)
1519 break;
1522 return len < 0;
1525 /* Perform a comparison on two string values (whose content are not
1526 necessarily null terminated) based on their length. */
1528 static int
1529 value_strcmp (struct value *arg1, struct value *arg2)
1531 int len1 = arg1->type ()->length ();
1532 int len2 = arg2->type ()->length ();
1533 const gdb_byte *s1 = arg1->contents ().data ();
1534 const gdb_byte *s2 = arg2->contents ().data ();
1535 int i, len = len1 < len2 ? len1 : len2;
1537 for (i = 0; i < len; i++)
1539 if (s1[i] < s2[i])
1540 return -1;
1541 else if (s1[i] > s2[i])
1542 return 1;
1543 else
1544 continue;
1547 if (len1 < len2)
1548 return -1;
1549 else if (len1 > len2)
1550 return 1;
1551 else
1552 return 0;
1555 /* Simulate the C operator == by returning a 1
1556 iff ARG1 and ARG2 have equal contents. */
1559 value_equal (struct value *arg1, struct value *arg2)
1561 int len;
1562 const gdb_byte *p1;
1563 const gdb_byte *p2;
1564 struct type *type1, *type2;
1565 enum type_code code1;
1566 enum type_code code2;
1567 int is_int1, is_int2;
1569 arg1 = coerce_array (arg1);
1570 arg2 = coerce_array (arg2);
1572 type1 = check_typedef (arg1->type ());
1573 type2 = check_typedef (arg2->type ());
1574 code1 = type1->code ();
1575 code2 = type2->code ();
1576 is_int1 = is_integral_type (type1);
1577 is_int2 = is_integral_type (type2);
1579 if (is_int1 && is_int2)
1580 return value_true (value_binop (arg1, arg2, BINOP_EQUAL));
1581 else if ((is_floating_value (arg1) || is_int1)
1582 && (is_floating_value (arg2) || is_int2))
1584 struct type *eff_type_v1, *eff_type_v2;
1585 gdb::byte_vector v1, v2;
1586 v1.resize (std::max (type1->length (), type2->length ()));
1587 v2.resize (std::max (type1->length (), type2->length ()));
1589 value_args_as_target_float (arg1, arg2,
1590 v1.data (), &eff_type_v1,
1591 v2.data (), &eff_type_v2);
1593 return target_float_compare (v1.data (), eff_type_v1,
1594 v2.data (), eff_type_v2) == 0;
1597 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1598 is bigger. */
1599 else if (code1 == TYPE_CODE_PTR && is_int2)
1600 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1601 else if (code2 == TYPE_CODE_PTR && is_int1)
1602 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1604 else if (code1 == code2
1605 && ((len = (int) type1->length ())
1606 == (int) type2->length ()))
1608 p1 = arg1->contents ().data ();
1609 p2 = arg2->contents ().data ();
1610 while (--len >= 0)
1612 if (*p1++ != *p2++)
1613 break;
1615 return len < 0;
1617 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1619 return value_strcmp (arg1, arg2) == 0;
1621 else
1622 error (_("Invalid type combination in equality test."));
1625 /* Compare values based on their raw contents. Useful for arrays since
1626 value_equal coerces them to pointers, thus comparing just the address
1627 of the array instead of its contents. */
1630 value_equal_contents (struct value *arg1, struct value *arg2)
1632 struct type *type1, *type2;
1634 type1 = check_typedef (arg1->type ());
1635 type2 = check_typedef (arg2->type ());
1637 return (type1->code () == type2->code ()
1638 && type1->length () == type2->length ()
1639 && memcmp (arg1->contents ().data (),
1640 arg2->contents ().data (),
1641 type1->length ()) == 0);
1644 /* Simulate the C operator < by returning 1
1645 iff ARG1's contents are less than ARG2's. */
1648 value_less (struct value *arg1, struct value *arg2)
1650 enum type_code code1;
1651 enum type_code code2;
1652 struct type *type1, *type2;
1653 int is_int1, is_int2;
1655 arg1 = coerce_array (arg1);
1656 arg2 = coerce_array (arg2);
1658 type1 = check_typedef (arg1->type ());
1659 type2 = check_typedef (arg2->type ());
1660 code1 = type1->code ();
1661 code2 = type2->code ();
1662 is_int1 = is_integral_type (type1);
1663 is_int2 = is_integral_type (type2);
1665 if ((is_int1 && is_int2)
1666 || (is_fixed_point_type (type1) && is_fixed_point_type (type2)))
1667 return value_true (value_binop (arg1, arg2, BINOP_LESS));
1668 else if ((is_floating_value (arg1) || is_int1)
1669 && (is_floating_value (arg2) || is_int2))
1671 struct type *eff_type_v1, *eff_type_v2;
1672 gdb::byte_vector v1, v2;
1673 v1.resize (std::max (type1->length (), type2->length ()));
1674 v2.resize (std::max (type1->length (), type2->length ()));
1676 value_args_as_target_float (arg1, arg2,
1677 v1.data (), &eff_type_v1,
1678 v2.data (), &eff_type_v2);
1680 return target_float_compare (v1.data (), eff_type_v1,
1681 v2.data (), eff_type_v2) == -1;
1683 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1684 return value_as_address (arg1) < value_as_address (arg2);
1686 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1687 is bigger. */
1688 else if (code1 == TYPE_CODE_PTR && is_int2)
1689 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1690 else if (code2 == TYPE_CODE_PTR && is_int1)
1691 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1692 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1693 return value_strcmp (arg1, arg2) < 0;
1694 else
1696 error (_("Invalid type combination in ordering comparison."));
1697 return 0;
1701 /* See value.h. */
1703 struct value *
1704 value_pos (struct value *arg1)
1706 struct type *type;
1708 arg1 = coerce_ref (arg1);
1709 type = check_typedef (arg1->type ());
1711 if (is_integral_type (type) || is_floating_value (arg1)
1712 || (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1713 || type->code () == TYPE_CODE_COMPLEX)
1714 return value_from_contents (type, arg1->contents ().data ());
1715 else
1716 error (_("Argument to positive operation not a number."));
1719 /* See value.h. */
1721 struct value *
1722 value_neg (struct value *arg1)
1724 struct type *type;
1726 arg1 = coerce_ref (arg1);
1727 type = check_typedef (arg1->type ());
1729 if (is_integral_type (type) || is_floating_type (type))
1730 return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
1731 else if (is_fixed_point_type (type))
1732 return value_binop (value::zero (type, not_lval), arg1, BINOP_SUB);
1733 else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1735 struct value *val = value::allocate (type);
1736 struct type *eltype = check_typedef (type->target_type ());
1737 int i;
1738 LONGEST low_bound, high_bound;
1740 if (!get_array_bounds (type, &low_bound, &high_bound))
1741 error (_("Could not determine the vector bounds"));
1743 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1744 int elt_len = eltype->length ();
1746 for (i = 0; i < high_bound - low_bound + 1; i++)
1748 value *tmp = value_neg (value_subscript (arg1, i));
1749 copy (tmp->contents_all (),
1750 val_contents.slice (i * elt_len, elt_len));
1752 return val;
1754 else if (type->code () == TYPE_CODE_COMPLEX)
1756 struct value *real = value_real_part (arg1);
1757 struct value *imag = value_imaginary_part (arg1);
1759 real = value_neg (real);
1760 imag = value_neg (imag);
1761 return value_literal_complex (real, imag, type);
1763 else
1764 error (_("Argument to negate operation not a number."));
1767 /* See value.h. */
1769 struct value *
1770 value_complement (struct value *arg1)
1772 struct type *type;
1773 struct value *val;
1775 arg1 = coerce_ref (arg1);
1776 type = check_typedef (arg1->type ());
1778 if (is_integral_type (type))
1780 gdb_mpz num = value_as_mpz (arg1);
1781 num.complement ();
1782 val = value_from_mpz (type, num);
1784 else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1786 struct type *eltype = check_typedef (type->target_type ());
1787 int i;
1788 LONGEST low_bound, high_bound;
1790 if (!get_array_bounds (type, &low_bound, &high_bound))
1791 error (_("Could not determine the vector bounds"));
1793 val = value::allocate (type);
1794 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1795 int elt_len = eltype->length ();
1797 for (i = 0; i < high_bound - low_bound + 1; i++)
1799 value *tmp = value_complement (value_subscript (arg1, i));
1800 copy (tmp->contents_all (),
1801 val_contents.slice (i * elt_len, elt_len));
1804 else if (type->code () == TYPE_CODE_COMPLEX)
1806 /* GCC has an extension that treats ~complex as the complex
1807 conjugate. */
1808 struct value *real = value_real_part (arg1);
1809 struct value *imag = value_imaginary_part (arg1);
1811 imag = value_neg (imag);
1812 return value_literal_complex (real, imag, type);
1814 else
1815 error (_("Argument to complement operation not an integer, boolean."));
1817 return val;
1820 /* The INDEX'th bit of SET value whose value_type is TYPE,
1821 and whose value_contents is valaddr.
1822 Return -1 if out of range, -2 other error. */
1825 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1827 struct gdbarch *gdbarch = type->arch ();
1828 LONGEST low_bound, high_bound;
1829 LONGEST word;
1830 unsigned rel_index;
1831 struct type *range = type->index_type ();
1833 if (!get_discrete_bounds (range, &low_bound, &high_bound))
1834 return -2;
1835 if (index < low_bound || index > high_bound)
1836 return -1;
1837 rel_index = index - low_bound;
1838 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1839 type_byte_order (type));
1840 rel_index %= TARGET_CHAR_BIT;
1841 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1842 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1843 return (word >> rel_index) & 1;