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