GDB (xrefs)
Loading...
Searching...
No Matches
valarith.c
Go to the documentation of this file.
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#include "rust-lang.h"
32#include "ada-lang.h"
33
34/* Forward declarations. */
35static struct value *value_subscripted_rvalue (struct value *array,
36 LONGEST index,
37 LONGEST lowerbound);
38
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. */
44
45static LONGEST
47{
48 LONGEST sz = -1;
49 struct type *ptr_target;
50
51 gdb_assert (ptr_type->code () == TYPE_CODE_PTR);
52 ptr_target = check_typedef (ptr_type->target_type ());
53
54 sz = type_length_units (ptr_target);
55 if (sz == 0)
56 {
57 if (ptr_type->code () == TYPE_CODE_VOID)
58 sz = 1;
59 else
60 {
61 const char *name;
62
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);
70 }
71 }
72 return sz;
73}
74
75/* Given a pointer ARG1 and an integral value ARG2, return the
76 result of C-style pointer arithmetic ARG1 + ARG2. */
77
78struct value *
79value_ptradd (struct value *arg1, LONGEST arg2)
80{
81 struct type *valptrtype;
82 LONGEST sz;
83 struct value *result;
84
85 arg1 = coerce_array (arg1);
86 valptrtype = check_typedef (arg1->type ());
87 sz = find_size_for_pointer_math (valptrtype);
88
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;
94}
95
96/* Given two compatible pointer values ARG1 and ARG2, return the
97 result of C-style pointer arithmetic ARG1 - ARG2. */
98
99LONGEST
100value_ptrdiff (struct value *arg1, struct value *arg2)
101{
102 struct type *type1, *type2;
103 LONGEST sz;
104
105 arg1 = coerce_array (arg1);
106 arg2 = coerce_array (arg2);
107 type1 = check_typedef (arg1->type ());
108 type2 = check_typedef (arg2->type ());
109
110 gdb_assert (type1->code () == TYPE_CODE_PTR);
111 gdb_assert (type2->code () == TYPE_CODE_PTR);
112
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."));
118
120 if (sz == 0)
121 {
122 warning (_("Type size unknown, assuming 1. "
123 "Try casting to a known type, or void *."));
124 sz = 1;
125 }
126
127 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
128}
129
130/* Return the value of ARRAY[IDX].
131
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.
134
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). */
139
140struct value *
141value_subscript (struct value *array, LONGEST index)
142{
143 bool c_style = current_language->c_style_arrays_p ();
144 struct type *tarray;
145
146 array = coerce_ref (array);
147 tarray = check_typedef (array->type ());
148
149 if (tarray->code () == TYPE_CODE_ARRAY
150 || tarray->code () == TYPE_CODE_STRING)
151 {
152 struct type *range_type = tarray->index_type ();
153 gdb::optional<LONGEST> lowerbound = get_discrete_low_bound (range_type);
154 if (!lowerbound.has_value ())
155 lowerbound = 0;
156
157 if (array->lval () != lval_memory)
158 return value_subscripted_rvalue (array, index, *lowerbound);
159
160 gdb::optional<LONGEST> upperbound
161 = get_discrete_high_bound (range_type);
162
163 if (!upperbound.has_value ())
164 upperbound = -1;
165
166 if (index >= *lowerbound && index <= *upperbound)
167 return value_subscripted_rvalue (array, index, *lowerbound);
168
169 if (!c_style)
170 {
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;
177 }
178
179 index -= *lowerbound;
180
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))
187 {
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;
193 }
194
195 array = value_coerce_array (array);
196 }
197
198 if (c_style)
199 return value_ind (value_ptradd (array, index));
200 else
201 error (_("not an array or string"));
202}
203
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. */
207
208static struct value *
209value_subscripted_rvalue (struct value *array, LONGEST index,
210 LONGEST lowerbound)
211{
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);
215
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)
220 {
221 struct gdbarch *arch = elt_type->arch ();
222 int unit_size = gdbarch_addressable_memory_unit_size (arch);
223 elt_size = stride / (unit_size * 8);
224 }
225
226 LONGEST elt_offs = elt_size * (index - lowerbound);
227 bool array_upper_bound_undefined
228 = array_type->bounds ()->high.kind () == PROP_UNDEFINED;
229
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))
234 {
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"));
241 }
242
243 if (is_dynamic_type (elt_type))
244 {
245 CORE_ADDR address;
246
247 address = array->address () + elt_offs;
248 elt_type = resolve_dynamic_type (elt_type, {}, address);
249 }
250
251 return value_from_component (array, elt_type, elt_offs);
252}
253
254/* See value.h. */
255
256struct value *
258{
259 struct type *type = check_typedef (val->type ());
260 if (type->code () == TYPE_CODE_ARRAY)
261 return val;
262
263 if (type->is_array_like ())
264 {
265 const language_defn *defn = language_def (type->language ());
266 return defn->to_array (val);
267 }
268 return nullptr;
269}
270
271
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.
275
276 For now, we do not overload the `=' operator. */
277
278int
280 struct type *type1, struct type *type2)
281{
282 if (op == BINOP_ASSIGN)
283 return 0;
284
285 type1 = check_typedef (type1);
286 if (TYPE_IS_REFERENCE (type1))
287 type1 = check_typedef (type1->target_type ());
288
289 type2 = check_typedef (type2);
290 if (TYPE_IS_REFERENCE (type2))
291 type2 = check_typedef (type2->target_type ());
292
293 return (type1->code () == TYPE_CODE_STRUCT
294 || type2->code () == TYPE_CODE_STRUCT);
295}
296
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.
300
301 For now, we do not overload the `=' operator. */
302
303int
305 struct value *arg1, struct value *arg2)
306{
307 return binop_types_user_defined_p (op, arg1->type (), arg2->type ());
308}
309
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.
313
314 For now, we do not overload the `&' operator. */
315
316int
317unop_user_defined_p (enum exp_opcode op, struct value *arg1)
318{
319 struct type *type1;
320
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;
327}
328
329/* Try to find an operator named OPERATOR which takes NARGS arguments
330 specified in ARGS. If the operator found is a static member operator
331 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
332 The search if performed through find_overload_match which will handle
333 member operators, non member operators, operators imported implicitly or
334 explicitly, and perform correct overload resolution in all of the above
335 situations or combinations thereof. */
336
337static struct value *
338value_user_defined_cpp_op (gdb::array_view<value *> args, char *oper,
339 int *static_memfuncp, enum noside noside)
340{
341
342 struct symbol *symp = NULL;
343 struct value *valp = NULL;
344
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);
349
350 if (valp)
351 return valp;
352
353 if (symp)
354 {
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);
360 }
361
362 error (_("Could not find %s."), oper);
363}
364
365/* Lookup user defined operator NAME. Return a value representing the
366 function, otherwise return NULL. */
367
368static struct value *
369value_user_defined_op (struct value **argp, gdb::array_view<value *> args,
370 char *name, int *static_memfuncp, enum noside noside)
371{
372 struct value *result = NULL;
373
375 {
376 result = value_user_defined_cpp_op (args, name, static_memfuncp,
377 noside);
378 }
379 else
380 result = value_struct_elt (argp, args, name, static_memfuncp,
381 "structure");
382
383 return result;
384}
385
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++).
390
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. */
394
395struct value *
396value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
397 enum exp_opcode otherop, enum noside noside)
398{
399 char *ptr;
400 char tstr[13];
401 int static_memfuncp;
402
403 arg1 = coerce_ref (arg1);
404 arg2 = coerce_ref (arg2);
405
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. */
408
409 if (check_typedef (arg1->type ())->code () != TYPE_CODE_STRUCT)
410 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
411
412 value *argvec_storage[3];
413 gdb::array_view<value *> argvec = argvec_storage;
414
415 argvec[1] = value_addr (arg1);
416 argvec[2] = arg2;
417
418 /* Make the right function name up. */
419 strcpy (tstr, "operator__");
420 ptr = tstr + 8;
421 switch (op)
422 {
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)
470 {
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."));
498 }
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."));
524 }
525
526 argvec[0] = value_user_defined_op (&arg1, argvec.slice (1), tstr,
527 &static_memfuncp, noside);
528
529 if (argvec[0])
530 {
531 if (static_memfuncp)
532 {
533 argvec[1] = argvec[0];
534 argvec = argvec.slice (1);
535 }
536 if (argvec[0]->type ()->code () == TYPE_CODE_XMETHOD)
537 {
538 /* Static xmethods are not supported yet. */
539 gdb_assert (static_memfuncp == 0);
541 {
542 struct type *return_type
543 = argvec[0]->result_type_of_xmethod (argvec.slice (1));
544
545 if (return_type == NULL)
546 error (_("Xmethod is missing return type."));
547 return value::zero (return_type, arg1->lval ());
548 }
549 return argvec[0]->call_xmethod (argvec.slice (1));
550 }
552 {
553 struct type *return_type;
554
555 return_type = check_typedef (argvec[0]->type ())->target_type ();
556 return value::zero (return_type, arg1->lval ());
557 }
558 return call_function_by_hand (argvec[0], NULL,
559 argvec.slice (1, 2 - static_memfuncp));
560 }
561 throw_error (NOT_FOUND_ERROR,
562 _("member function %s not found"), tstr);
563}
564
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++). */
570
571struct value *
572value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
573{
574 struct gdbarch *gdbarch = arg1->type ()->arch ();
575 char *ptr;
576 char tstr[13], mangle_tstr[13];
577 int static_memfuncp, nargs;
578
579 arg1 = coerce_ref (arg1);
580
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. */
583
584 if (check_typedef (arg1->type ())->code () != TYPE_CODE_STRUCT)
585 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
586
587 value *argvec_storage[3];
588 gdb::array_view<value *> argvec = argvec_storage;
589
590 argvec[1] = value_addr (arg1);
591 argvec[2] = 0;
592
593 nargs = 1;
594
595 /* Make the right function name up. */
596 strcpy (tstr, "operator__");
597 ptr = tstr + 8;
598 strcpy (mangle_tstr, "__");
599 switch (op)
600 {
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."));
637 }
638
639 argvec[0] = value_user_defined_op (&arg1, argvec.slice (1, nargs), tstr,
640 &static_memfuncp, noside);
641
642 if (argvec[0])
643 {
644 if (static_memfuncp)
645 {
646 argvec[1] = argvec[0];
647 argvec = argvec.slice (1);
648 }
649 if (argvec[0]->type ()->code () == TYPE_CODE_XMETHOD)
650 {
651 /* Static xmethods are not supported yet. */
652 gdb_assert (static_memfuncp == 0);
654 {
655 struct type *return_type
656 = argvec[0]->result_type_of_xmethod (argvec[1]);
657
658 if (return_type == NULL)
659 error (_("Xmethod is missing return type."));
660 return value::zero (return_type, arg1->lval ());
661 }
662 return argvec[0]->call_xmethod (argvec[1]);
663 }
665 {
666 struct type *return_type;
667
668 return_type = check_typedef (argvec[0]->type ())->target_type ();
669 return value::zero (return_type, arg1->lval ());
670 }
671 return call_function_by_hand (argvec[0], NULL,
672 argvec.slice (1, nargs));
673 }
674 throw_error (NOT_FOUND_ERROR,
675 _("member function %s not found"), tstr);
676}
677
678
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. */
682
683struct value *
684value_concat (struct value *arg1, struct value *arg2)
685{
686 struct type *type1 = check_typedef (arg1->type ());
687 struct type *type2 = check_typedef (arg2->type ());
688
689 if (type1->code () != TYPE_CODE_ARRAY && type2->code () != TYPE_CODE_ARRAY)
690 error ("no array provided to concatenation");
691
692 LONGEST low1, high1;
693 struct type *elttype1 = type1;
694 if (elttype1->code () == TYPE_CODE_ARRAY)
695 {
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"));
700 }
701 else
702 {
703 low1 = 0;
704 high1 = 0;
705 }
706
707 LONGEST low2, high2;
708 struct type *elttype2 = type2;
709 if (elttype2->code () == TYPE_CODE_ARRAY)
710 {
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"));
715 }
716 else
717 {
718 low2 = 0;
719 high2 = 0;
720 }
721
722 if (!types_equal (elttype1, elttype2))
723 error (_("concatenation with different element types"));
724
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);
730
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 ()));
737
738 return result;
739}
740
741
742/* Obtain argument values for binary operation, converting from
743 other types if one of them is not floating point. */
744static void
745value_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)
748{
749 struct type *type1, *type2;
750
751 type1 = check_typedef (arg1->type ());
752 type2 = check_typedef (arg2->type ());
753
754 /* At least one of the arguments must be of floating-point type. */
755 gdb_assert (is_floating_type (type1) || is_floating_type (type2));
756
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."));
764
765 /* Obtain value of arg1, converting from other types if necessary. */
766
767 if (is_floating_type (type1))
768 {
769 *eff_type_x = type1;
770 memcpy (x, arg1->contents ().data (), type1->length ());
771 }
772 else if (is_integral_type (type1))
773 {
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));
779 }
780 else
781 error (_("Don't know how to convert from %s to %s."), type1->name (),
782 type2->name ());
783
784 /* Obtain value of arg2, converting from other types if necessary. */
785
786 if (is_floating_type (type2))
787 {
788 *eff_type_y = type2;
789 memcpy (y, arg2->contents ().data (), type2->length ());
790 }
791 else if (is_integral_type (type2))
792 {
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));
798 }
799 else
800 error (_("Don't know how to convert from %s to %s."), type1->name (),
801 type2->name ());
802}
803
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). */
807
808static struct value *
809fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
810{
811 struct type *type1 = check_typedef (arg1->type ());
812 struct type *type2 = check_typedef (arg2->type ());
814
815 struct gdbarch *gdbarch = type1->arch ();
816 struct value *val;
817
818 gdb_mpq v1, v2, res;
819
820 gdb_assert (is_fixed_point_type (type1) || is_fixed_point_type (type2));
821 if (op == BINOP_MUL || op == BINOP_DIV)
822 {
823 v1 = value_to_gdb_mpq (arg1);
824 v2 = value_to_gdb_mpq (arg2);
825
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;
830 }
831 else
832 {
833 if (!is_fixed_point_type (type1))
834 {
835 arg1 = value_cast (type2, arg1);
836 type1 = type2;
837 }
838 if (!is_fixed_point_type (type2))
839 {
840 arg2 = value_cast (type1, arg2);
841 type2 = type1;
842 }
843
844 v1.read_fixed_point (arg1->contents (),
845 type_byte_order (type1), type1->is_unsigned (),
847 v2.read_fixed_point (arg2->contents (),
848 type_byte_order (type2), type2->is_unsigned (),
850 }
851
852 auto fixed_point_to_value = [type1] (const gdb_mpq &fp)
853 {
854 value *fp_val = value::allocate (type1);
855
856 fp.write_fixed_point
857 (fp_val->contents_raw (),
858 type_byte_order (type1),
859 type1->is_unsigned (),
861
862 return fp_val;
863 };
864
865 switch (op)
866 {
867 case BINOP_ADD:
868 res = v1 + v2;
869 val = fixed_point_to_value (res);
870 break;
871
872 case BINOP_SUB:
873 res = v1 - v2;
874 val = fixed_point_to_value (res);
875 break;
876
877 case BINOP_MIN:
878 val = fixed_point_to_value (std::min (v1, v2));
879 break;
880
881 case BINOP_MAX:
882 val = fixed_point_to_value (std::max (v1, v2));
883 break;
884
885 case BINOP_MUL:
886 res = v1 * v2;
887 val = fixed_point_to_value (res);
888 break;
889
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;
896
897 case BINOP_EQUAL:
899 v1 == v2 ? 1 : 0);
900 break;
901
902 case BINOP_LESS:
904 v1 < v2 ? 1 : 0);
905 break;
906
907 default:
908 error (_("Integer-only operation on fixed point number."));
909 }
910
911 return val;
912}
913
914/* A helper function that finds the type to use for a binary operation
915 involving TYPE1 and TYPE2. */
916
917static struct type *
918promotion_type (struct type *type1, struct type *type2)
919{
920 struct type *result_type;
921
922 if (is_floating_type (type1) || is_floating_type (type2))
923 {
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;
934 }
935 else
936 {
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;
948 }
949
950 return result_type;
951}
952
953static struct value *scalar_binop (struct value *arg1, struct value *arg2,
954 enum exp_opcode op);
955
956/* Perform a binary operation on complex operands. */
957
958static struct value *
959complex_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
960{
961 struct type *arg1_type = check_typedef (arg1->type ());
962 struct type *arg2_type = check_typedef (arg2->type ());
963
964 struct value *arg1_real, *arg1_imag, *arg2_real, *arg2_imag;
965 if (arg1_type->code () == TYPE_CODE_COMPLEX)
966 {
967 arg1_real = value_real_part (arg1);
968 arg1_imag = value_imaginary_part (arg1);
969 }
970 else
971 {
972 arg1_real = arg1;
973 arg1_imag = value::zero (arg1_type, not_lval);
974 }
975 if (arg2_type->code () == TYPE_CODE_COMPLEX)
976 {
977 arg2_real = value_real_part (arg2);
978 arg2_imag = value_imaginary_part (arg2);
979 }
980 else
981 {
982 arg2_real = arg2;
983 arg2_imag = value::zero (arg2_type, not_lval);
984 }
985
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."));
990
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);
995
996 struct type *result_type = init_complex_type (nullptr, comp_type);
997
998 struct value *result_real, *result_imag;
999 switch (op)
1000 {
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;
1006
1007 case BINOP_MUL:
1008 {
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);
1012
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);
1016 }
1017 break;
1018
1019 case BINOP_DIV:
1020 {
1021 if (arg2_type->code () == TYPE_CODE_COMPLEX)
1022 {
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);
1027
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);
1032
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);
1036 }
1037
1038 result_real = scalar_binop (arg1_real, arg2_real, op);
1039 result_imag = scalar_binop (arg1_imag, arg2_real, op);
1040 }
1041 break;
1042
1043 case BINOP_EQUAL:
1044 case BINOP_NOTEQUAL:
1045 {
1046 struct value *x1 = scalar_binop (arg1_real, arg2_real, op);
1047 struct value *x2 = scalar_binop (arg1_imag, arg2_imag, op);
1048
1049 LONGEST v1 = value_as_long (x1);
1050 LONGEST v2 = value_as_long (x2);
1051
1052 if (op == BINOP_EQUAL)
1053 v1 = v1 && v2;
1054 else
1055 v1 = v1 || v2;
1056
1057 return value_from_longest (x1->type (), v1);
1058 }
1059 break;
1060
1061 default:
1062 error (_("Invalid binary operation on numbers."));
1063 }
1064
1065 return value_literal_complex (result_real, result_imag, result_type);
1066}
1067
1068/* Return the type's length in bits. */
1069
1070static int
1072{
1073 int unit_size = gdbarch_addressable_memory_unit_size (type->arch ());
1074 return unit_size * 8 * type->length ();
1075}
1076
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. */
1085
1086static bool
1088 type *shift_count_type, const gdb_mpz &shift_count,
1089 unsigned long &nbits)
1090{
1091 if (!shift_count_type->is_unsigned ())
1092 {
1093 LONGEST count = shift_count.as_integer<LONGEST> ();
1094 if (count < 0)
1095 {
1096 auto error_or_warning = [] (const char *msg)
1097 {
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. */
1102 error (("%s"), msg);
1103 else
1104 warning (("%s"), msg);
1105 };
1106
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;
1112 }
1113 }
1114
1115 nbits = shift_count.as_integer<unsigned long> ();
1116 if (nbits >= type_length_bits (result_type))
1117 {
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. */
1122 {
1123 if (op == BINOP_RSH)
1124 warning (_("right shift count >= width of type"));
1125 else
1126 warning (_("left shift count >= width of type"));
1127 }
1128 return false;
1129 }
1130
1131 return true;
1132}
1133
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. */
1139
1140static struct value *
1141scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1142{
1143 struct value *val;
1144 struct type *type1, *type2, *result_type;
1145
1146 arg1 = coerce_ref (arg1);
1147 arg2 = coerce_ref (arg2);
1148
1149 type1 = check_typedef (arg1->type ());
1150 type2 = check_typedef (arg2->type ());
1151
1152 if (type1->code () == TYPE_CODE_COMPLEX
1153 || type2->code () == TYPE_CODE_COMPLEX)
1154 return complex_binop (arg1, arg2, op);
1155
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."));
1163
1164 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
1165 return fixed_point_binop (arg1, arg2, op);
1166
1167 if (is_floating_type (type1) || is_floating_type (type2))
1168 {
1169 result_type = promotion_type (type1, type2);
1170 val = value::allocate (result_type);
1171
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 ());
1176
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);
1183 }
1184 else if (type1->code () == TYPE_CODE_BOOL
1185 || type2->code () == TYPE_CODE_BOOL)
1186 {
1187 LONGEST v1, v2, v = 0;
1188
1189 v1 = value_as_long (arg1);
1190 v2 = value_as_long (arg2);
1191
1192 switch (op)
1193 {
1194 case BINOP_BITWISE_AND:
1195 v = v1 & v2;
1196 break;
1197
1198 case BINOP_BITWISE_IOR:
1199 v = v1 | v2;
1200 break;
1201
1202 case BINOP_BITWISE_XOR:
1203 v = v1 ^ v2;
1204 break;
1205
1206 case BINOP_EQUAL:
1207 v = v1 == v2;
1208 break;
1209
1210 case BINOP_NOTEQUAL:
1211 v = v1 != v2;
1212 break;
1213
1214 default:
1215 error (_("Invalid operation on booleans."));
1216 }
1217
1218 result_type = type1;
1219
1220 val = value::allocate (result_type);
1221 store_signed_integer (val->contents_raw ().data (),
1222 result_type->length (),
1223 type_byte_order (result_type),
1224 v);
1225 }
1226 else
1227 /* Integral operations here. */
1228 {
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);
1239
1240 gdb_mpz v1 = value_as_mpz (arg1);
1241 gdb_mpz v2 = value_as_mpz (arg2);
1242 gdb_mpz v;
1243
1244 switch (op)
1245 {
1246 case BINOP_ADD:
1247 v = v1 + v2;
1248 break;
1249
1250 case BINOP_SUB:
1251 v = v1 - v2;
1252 break;
1253
1254 case BINOP_MUL:
1255 v = v1 * v2;
1256 break;
1257
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;
1265
1266 case BINOP_EXP:
1267 v = v1.pow (v2.as_integer<unsigned long> ());
1268 break;
1269
1270 case BINOP_REM:
1271 if (v2.sgn () != 0)
1272 v = v1 % v2;
1273 else
1274 error (_("Division by zero"));
1275 break;
1276
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)
1281 {
1282 v = v1;
1283 }
1284 else
1285 {
1286 v = v1 / v2;
1287 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1288 v = v1 - (v2 * v);
1289 }
1290 break;
1291
1292 case BINOP_LSH:
1293 {
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;
1299 }
1300 break;
1301
1302 case BINOP_RSH:
1303 {
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;
1309 }
1310 break;
1311
1312 case BINOP_BITWISE_AND:
1313 v = v1 & v2;
1314 break;
1315
1316 case BINOP_BITWISE_IOR:
1317 v = v1 | v2;
1318 break;
1319
1320 case BINOP_BITWISE_XOR:
1321 v = v1 ^ v2;
1322 break;
1323
1324 case BINOP_MIN:
1325 v = v1 < v2 ? v1 : v2;
1326 break;
1327
1328 case BINOP_MAX:
1329 v = v1 > v2 ? v1 : v2;
1330 break;
1331
1332 case BINOP_EQUAL:
1333 v = v1 == v2;
1334 break;
1335
1336 case BINOP_NOTEQUAL:
1337 v = v1 != v2;
1338 break;
1339
1340 case BINOP_LESS:
1341 v = v1 < v2;
1342 break;
1343
1344 case BINOP_GTR:
1345 v = v1 > v2;
1346 break;
1347
1348 case BINOP_LEQ:
1349 v = v1 <= v2;
1350 break;
1351
1352 case BINOP_GEQ:
1353 v = v1 >= v2;
1354 break;
1355
1356 default:
1357 error (_("Invalid binary operation on numbers."));
1358 }
1359
1360 val = value_from_mpz (result_type, v);
1361 }
1362
1363 return val;
1364}
1365
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. */
1371
1372struct value *
1373value_vector_widen (struct value *scalar_value, struct type *vector_type)
1374{
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;
1380
1381 vector_type = check_typedef (vector_type);
1382
1383 gdb_assert (vector_type->code () == TYPE_CODE_ARRAY
1384 && vector_type->is_vector ());
1385
1386 if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1387 error (_("Could not determine the vector bounds"));
1388
1389 eltype = check_typedef (vector_type->target_type ());
1390 elval = value_cast (eltype, scalar_value);
1391
1392 scalar_type = check_typedef (scalar_value->type ());
1393
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"));
1399
1400 value *val = value::allocate (vector_type);
1401 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1402 int elt_len = eltype->length ();
1403
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));
1408
1409 return val;
1410}
1411
1412/* Performs a binary operation on two vector operands by calling scalar_binop
1413 for each pair of vector components. */
1414
1415static struct value *
1416vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1417{
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;
1421
1422 type1 = check_typedef (val1->type ());
1423 type2 = check_typedef (val2->type ());
1424
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;
1429
1430 if (!t1_is_vec || !t2_is_vec)
1431 error (_("Vector operations are only supported among vectors"));
1432
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"));
1436
1437 eltype1 = check_typedef (type1->target_type ());
1438 eltype2 = check_typedef (type2->target_type ());
1439 elsize = eltype1->length ();
1440
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"));
1446
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++)
1451 {
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));
1456 }
1457
1458 return val;
1459}
1460
1461/* Perform a binary operation on two operands. */
1462
1463struct value *
1464value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1465{
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 ());
1473
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
1479 {
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;
1483
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."));
1488
1489 /* Replicate the scalar value to make a vector value. */
1490 *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1491
1492 val = vector_binop (arg1, arg2, op);
1493 }
1494
1495 return val;
1496}
1497
1498/* See value.h. */
1499
1500bool
1502{
1503 int len;
1504 const gdb_byte *p;
1505 struct type *type1;
1506
1507 arg1 = coerce_array (arg1);
1508 type1 = check_typedef (arg1->type ());
1509
1510 if (is_floating_value (arg1))
1511 return target_float_is_zero (arg1->contents ().data (), type1);
1512
1513 len = type1->length ();
1514 p = arg1->contents ().data ();
1515
1516 while (--len >= 0)
1517 {
1518 if (*p++)
1519 break;
1520 }
1521
1522 return len < 0;
1523}
1524
1525/* Perform a comparison on two string values (whose content are not
1526 necessarily null terminated) based on their length. */
1527
1528static int
1529value_strcmp (struct value *arg1, struct value *arg2)
1530{
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;
1536
1537 for (i = 0; i < len; i++)
1538 {
1539 if (s1[i] < s2[i])
1540 return -1;
1541 else if (s1[i] > s2[i])
1542 return 1;
1543 else
1544 continue;
1545 }
1546
1547 if (len1 < len2)
1548 return -1;
1549 else if (len1 > len2)
1550 return 1;
1551 else
1552 return 0;
1553}
1554
1555/* Simulate the C operator == by returning a 1
1556 iff ARG1 and ARG2 have equal contents. */
1557
1558int
1559value_equal (struct value *arg1, struct value *arg2)
1560{
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;
1568
1569 arg1 = coerce_array (arg1);
1570 arg2 = coerce_array (arg2);
1571
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);
1578
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))
1583 {
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 ()));
1588
1589 value_args_as_target_float (arg1, arg2,
1590 v1.data (), &eff_type_v1,
1591 v2.data (), &eff_type_v2);
1592
1593 return target_float_compare (v1.data (), eff_type_v1,
1594 v2.data (), eff_type_v2) == 0;
1595 }
1596
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);
1603
1604 else if (code1 == code2
1605 && ((len = (int) type1->length ())
1606 == (int) type2->length ()))
1607 {
1608 p1 = arg1->contents ().data ();
1609 p2 = arg2->contents ().data ();
1610 while (--len >= 0)
1611 {
1612 if (*p1++ != *p2++)
1613 break;
1614 }
1615 return len < 0;
1616 }
1617 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1618 {
1619 return value_strcmp (arg1, arg2) == 0;
1620 }
1621 else
1622 error (_("Invalid type combination in equality test."));
1623}
1624
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. */
1628
1629int
1630value_equal_contents (struct value *arg1, struct value *arg2)
1631{
1632 struct type *type1, *type2;
1633
1634 type1 = check_typedef (arg1->type ());
1635 type2 = check_typedef (arg2->type ());
1636
1637 return (type1->code () == type2->code ()
1638 && type1->length () == type2->length ()
1639 && memcmp (arg1->contents ().data (),
1640 arg2->contents ().data (),
1641 type1->length ()) == 0);
1642}
1643
1644/* Simulate the C operator < by returning 1
1645 iff ARG1's contents are less than ARG2's. */
1646
1647int
1648value_less (struct value *arg1, struct value *arg2)
1649{
1650 enum type_code code1;
1651 enum type_code code2;
1652 struct type *type1, *type2;
1653 int is_int1, is_int2;
1654
1655 arg1 = coerce_array (arg1);
1656 arg2 = coerce_array (arg2);
1657
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);
1664
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))
1670 {
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 ()));
1675
1676 value_args_as_target_float (arg1, arg2,
1677 v1.data (), &eff_type_v1,
1678 v2.data (), &eff_type_v2);
1679
1680 return target_float_compare (v1.data (), eff_type_v1,
1681 v2.data (), eff_type_v2) == -1;
1682 }
1683 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1684 return value_as_address (arg1) < value_as_address (arg2);
1685
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
1695 {
1696 error (_("Invalid type combination in ordering comparison."));
1697 return 0;
1698 }
1699}
1700
1701/* See value.h. */
1702
1703struct value *
1704value_pos (struct value *arg1)
1705{
1706 struct type *type;
1707
1708 arg1 = coerce_ref (arg1);
1709 type = check_typedef (arg1->type ());
1710
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."));
1717}
1718
1719/* See value.h. */
1720
1721struct value *
1722value_neg (struct value *arg1)
1723{
1724 struct type *type;
1725
1726 arg1 = coerce_ref (arg1);
1727 type = check_typedef (arg1->type ());
1728
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 ())
1734 {
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;
1739
1740 if (!get_array_bounds (type, &low_bound, &high_bound))
1741 error (_("Could not determine the vector bounds"));
1742
1743 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1744 int elt_len = eltype->length ();
1745
1746 for (i = 0; i < high_bound - low_bound + 1; i++)
1747 {
1748 value *tmp = value_neg (value_subscript (arg1, i));
1749 copy (tmp->contents_all (),
1750 val_contents.slice (i * elt_len, elt_len));
1751 }
1752 return val;
1753 }
1754 else if (type->code () == TYPE_CODE_COMPLEX)
1755 {
1756 struct value *real = value_real_part (arg1);
1757 struct value *imag = value_imaginary_part (arg1);
1758
1759 real = value_neg (real);
1760 imag = value_neg (imag);
1761 return value_literal_complex (real, imag, type);
1762 }
1763 else
1764 error (_("Argument to negate operation not a number."));
1765}
1766
1767/* See value.h. */
1768
1769struct value *
1771{
1772 struct type *type;
1773 struct value *val;
1774
1775 arg1 = coerce_ref (arg1);
1776 type = check_typedef (arg1->type ());
1777
1778 if (is_integral_type (type))
1779 {
1780 gdb_mpz num = value_as_mpz (arg1);
1781 num.complement ();
1782 val = value_from_mpz (type, num);
1783 }
1784 else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1785 {
1786 struct type *eltype = check_typedef (type->target_type ());
1787 int i;
1788 LONGEST low_bound, high_bound;
1789
1790 if (!get_array_bounds (type, &low_bound, &high_bound))
1791 error (_("Could not determine the vector bounds"));
1792
1793 val = value::allocate (type);
1794 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1795 int elt_len = eltype->length ();
1796
1797 for (i = 0; i < high_bound - low_bound + 1; i++)
1798 {
1799 value *tmp = value_complement (value_subscript (arg1, i));
1800 copy (tmp->contents_all (),
1801 val_contents.slice (i * elt_len, elt_len));
1802 }
1803 }
1804 else if (type->code () == TYPE_CODE_COMPLEX)
1805 {
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);
1810
1811 imag = value_neg (imag);
1812 return value_literal_complex (real, imag, type);
1813 }
1814 else
1815 error (_("Argument to complement operation not an integer, boolean."));
1816
1817 return val;
1818}
1819
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. */
1823
1824int
1825value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1826{
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 ();
1832
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,
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;
1844}
const char *const name
int code
Definition ada-lex.l:670
constexpr std::string_view s1
Definition 2.cc:26
constexpr std::string_view s2
Definition 2.cc:27
static void store_signed_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, LONGEST val)
Definition defs.h:508
language
Definition defs.h:211
@ language_cplus
Definition defs.h:216
@ language_go
Definition defs.h:218
@ lval_memory
Definition defs.h:363
@ not_lval
Definition defs.h:361
@ lval_internalvar
Definition defs.h:367
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition defs.h:480
exp_opcode
Definition expression.h:45
noside
Definition expression.h:56
@ EVAL_AVOID_SIDE_EFFECTS
Definition expression.h:58
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition gdbarch.c:1396
int gdbarch_addressable_memory_unit_size(struct gdbarch *gdbarch)
Definition gdbarch.c:5300
enum bfd_endian type_byte_order(const struct type *type)
Definition gdbtypes.c:3900
int is_integral_type(struct type *t)
Definition gdbtypes.c:3654
int is_floating_type(struct type *t)
Definition gdbtypes.c:3669
int is_dynamic_type(struct type *type)
Definition gdbtypes.c:2140
struct type * lookup_array_range_type(struct type *element_type, LONGEST low_bound, LONGEST high_bound)
Definition gdbtypes.c:1397
bool get_discrete_bounds(struct type *type, LONGEST *lowp, LONGEST *highp)
Definition gdbtypes.c:1192
gdb::optional< LONGEST > get_discrete_high_bound(struct type *type)
Definition gdbtypes.c:1116
gdb::optional< LONGEST > get_discrete_low_bound(struct type *type)
Definition gdbtypes.c:1049
struct type * resolve_dynamic_type(struct type *type, gdb::array_view< const gdb_byte > valaddr, CORE_ADDR addr, const frame_info_ptr *in_frame)
Definition gdbtypes.c:2857
bool get_array_bounds(struct type *type, LONGEST *low_bound, LONGEST *high_bound)
Definition gdbtypes.c:1211
int type_not_associated(const struct type *type)
Definition gdbtypes.c:4372
unsigned int type_length_units(struct type *type)
Definition gdbtypes.c:308
bool types_equal(struct type *a, struct type *b)
Definition gdbtypes.c:4114
struct type * init_complex_type(const char *name, struct type *target_type)
Definition gdbtypes.c:3449
bool is_fixed_point_type(struct type *type)
Definition gdbtypes.c:5861
int type_not_allocated(const struct type *type)
Definition gdbtypes.c:4361
struct type * check_typedef(struct type *type)
Definition gdbtypes.c:2966
bool can_create_complex_type(struct type *target_type)
Definition gdbtypes.c:3439
@ PROP_UNDEFINED
Definition gdbtypes.h:275
#define TYPE_IS_REFERENCE(t)
Definition gdbtypes.h:139
type_code
Definition gdbtypes.h:82
struct value * call_function_by_hand(struct value *function, type *default_return_type, gdb::array_view< value * > args)
Definition infcall.c:824
const struct language_defn * language_def(enum language lang)
Definition language.c:439
const struct language_defn * current_language
Definition language.c:82
struct type * language_bool_type(const struct language_defn *la, struct gdbarch *gdbarch)
Definition language.c:888
enum var_types type
Definition scm-param.c:142
dynamic_prop_kind kind() const
Definition gdbtypes.h:320
void read_fixed_point(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order, bool unsigned_p, const gdb_mpq &scaling_factor)
Definition gmp-utils.c:210
static gdb_mpz pow(unsigned long base, unsigned long exp)
Definition gmp-utils.h:107
T as_integer() const
Definition gmp-utils.h:625
void complement()
Definition gmp-utils.h:174
enum language la_language
Definition language.h:275
virtual struct value * to_array(struct value *val) const
Definition language.h:579
virtual bool c_style_arrays_p() const
Definition language.h:605
struct dynamic_prop high
Definition gdbtypes.h:721
Definition value.h:90
struct type * target_type() const
Definition gdbtypes.h:1037
type_code code() const
Definition gdbtypes.h:956
ULONGEST length() const
Definition gdbtypes.h:983
bool is_unsigned() const
Definition gdbtypes.h:1100
bool is_vector() const
Definition gdbtypes.h:1186
enum language language() const
Definition gdbtypes.h:1446
gdbarch * arch() const
Definition gdbtypes.c:273
ULONGEST bit_stride() const
Definition gdbtypes.h:1090
const gdb_mpq & fixed_point_scaling_factor()
Definition gdbtypes.c:5888
range_bounds * bounds() const
Definition gdbtypes.h:1065
const char * name() const
Definition gdbtypes.h:968
type * index_type() const
Definition gdbtypes.h:1032
bool is_array_like()
Definition gdbtypes.c:5957
Definition value.h:130
static struct value * zero(struct type *type, enum lval_type lv)
Definition value.c:3426
gdb::array_view< const gdb_byte > contents_all()
Definition value.c:1119
bool lazy() const
Definition value.h:265
gdb::array_view< gdb_byte > contents_writeable()
Definition value.c:1271
static struct value * allocate(struct type *type)
Definition value.c:957
bool bytes_available(LONGEST offset, ULONGEST length) const
Definition value.c:187
void set_lval(lval_type val)
Definition value.h:336
void mark_bytes_unavailable(LONGEST offset, ULONGEST length)
Definition value.c:419
gdb::array_view< const gdb_byte > contents()
Definition value.c:1262
void set_address(CORE_ADDR)
Definition value.c:1389
void set_component_location(const struct value *whole)
Definition value.c:1599
gdb::array_view< gdb_byte > contents_raw()
Definition value.c:1009
struct type * type() const
Definition value.h:180
enum lval_type lval() const
Definition value.h:332
CORE_ADDR address
Definition value.h:658
int target_float_compare(const gdb_byte *x, const struct type *type_x, const gdb_byte *y, const struct type *type_y)
bool target_float_is_zero(const gdb_byte *addr, const struct type *type)
void target_float_binop(enum exp_opcode opcode, const gdb_byte *x, const struct type *type_x, const gdb_byte *y, const struct type *type_y, gdb_byte *res, const struct type *type_res)
void target_float_from_longest(gdb_byte *addr, const struct type *type, LONGEST val)
void target_float_from_ulongest(gdb_byte *addr, const struct type *type, ULONGEST val)
static LONGEST find_size_for_pointer_math(struct type *ptr_type)
Definition valarith.c:46
int value_equal_contents(struct value *arg1, struct value *arg2)
Definition valarith.c:1630
bool value_logical_not(struct value *arg1)
Definition valarith.c:1501
struct value * value_subscript(struct value *array, LONGEST index)
Definition valarith.c:141
struct value * value_x_binop(struct value *arg1, struct value *arg2, enum exp_opcode op, enum exp_opcode otherop, enum noside noside)
Definition valarith.c:396
struct value * value_neg(struct value *arg1)
Definition valarith.c:1722
static struct value * complex_binop(struct value *arg1, struct value *arg2, enum exp_opcode op)
Definition valarith.c:959
static int type_length_bits(type *type)
Definition valarith.c:1071
static struct type * promotion_type(struct type *type1, struct type *type2)
Definition valarith.c:918
static struct value * scalar_binop(struct value *arg1, struct value *arg2, enum exp_opcode op)
Definition valarith.c:1141
struct value * value_x_unop(struct value *arg1, enum exp_opcode op, enum noside noside)
Definition valarith.c:572
static struct value * value_user_defined_op(struct value **argp, gdb::array_view< value * > args, char *name, int *static_memfuncp, enum noside noside)
Definition valarith.c:369
static struct value * value_subscripted_rvalue(struct value *array, LONGEST index, LONGEST lowerbound)
Definition valarith.c:209
struct value * value_complement(struct value *arg1)
Definition valarith.c:1770
struct value * value_to_array(struct value *val)
Definition valarith.c:257
struct value * value_pos(struct value *arg1)
Definition valarith.c:1704
static struct value * value_user_defined_cpp_op(gdb::array_view< value * > args, char *oper, int *static_memfuncp, enum noside noside)
Definition valarith.c:338
static int value_strcmp(struct value *arg1, struct value *arg2)
Definition valarith.c:1529
struct value * value_vector_widen(struct value *scalar_value, struct type *vector_type)
Definition valarith.c:1373
int binop_user_defined_p(enum exp_opcode op, struct value *arg1, struct value *arg2)
Definition valarith.c:304
static struct value * fixed_point_binop(struct value *arg1, struct value *arg2, enum exp_opcode op)
Definition valarith.c:809
static struct value * vector_binop(struct value *val1, struct value *val2, enum exp_opcode op)
Definition valarith.c:1416
static bool check_valid_shift_count(enum exp_opcode op, type *result_type, type *shift_count_type, const gdb_mpz &shift_count, unsigned long &nbits)
Definition valarith.c:1087
int value_equal(struct value *arg1, struct value *arg2)
Definition valarith.c:1559
int binop_types_user_defined_p(enum exp_opcode op, struct type *type1, struct type *type2)
Definition valarith.c:279
int value_less(struct value *arg1, struct value *arg2)
Definition valarith.c:1648
struct value * value_concat(struct value *arg1, struct value *arg2)
Definition valarith.c:684
struct value * value_ptradd(struct value *arg1, LONGEST arg2)
Definition valarith.c:79
LONGEST value_ptrdiff(struct value *arg1, struct value *arg2)
Definition valarith.c:100
int value_bit_index(struct type *type, const gdb_byte *valaddr, int index)
Definition valarith.c:1825
static void value_args_as_target_float(struct value *arg1, struct value *arg2, gdb_byte *x, struct type **eff_type_x, gdb_byte *y, struct type **eff_type_y)
Definition valarith.c:745
struct value * value_binop(struct value *arg1, struct value *arg2, enum exp_opcode op)
Definition valarith.c:1464
int unop_user_defined_p(enum exp_opcode op, struct value *arg1)
Definition valarith.c:317
struct value * value_coerce_array(struct value *arg1)
Definition valops.c:1515
struct value * value_struct_elt(struct value **argp, gdb::optional< gdb::array_view< value * > > args, const char *name, int *static_memfuncp, const char *err)
Definition valops.c:2334
struct value * value_of_variable(struct symbol *var, const struct block *b)
Definition valops.c:1386
struct value * value_addr(struct value *arg1)
Definition valops.c:1551
struct value * value_cast(struct type *type, struct value *arg2)
Definition valops.c:403
struct value * value_real_part(struct value *value)
Definition valops.c:4116
struct value * value_ind(struct value *arg1)
Definition valops.c:1630
struct value * value_literal_complex(struct value *arg1, struct value *arg2, struct type *type)
Definition valops.c:4092
gdb_mpq value_to_gdb_mpq(struct value *value)
Definition valops.c:338
struct value * value_imaginary_part(struct value *value)
Definition valops.c:4128
int find_overload_match(gdb::array_view< value * > args, const char *name, enum oload_search_type method, struct value **objp, struct symbol *fsym, struct value **valp, struct symbol **symp, int *staticp, const int no_adl, const enum noside noside)
Definition valops.c:2710
struct value * value_from_component(struct value *whole, struct type *type, LONGEST offset)
Definition value.c:3657
bool is_floating_value(struct value *val)
Definition value.c:2851
CORE_ADDR value_as_address(struct value *val)
Definition value.c:2636
struct value * value_from_ulongest(struct type *type, ULONGEST num)
Definition value.c:3450
struct value * value_from_longest(struct type *type, LONGEST num)
Definition value.c:3438
struct value * coerce_ref(struct value *arg)
Definition value.c:3752
struct value * value_from_contents(struct type *type, const gdb_byte *contents)
Definition value.c:3581
struct value * coerce_array(struct value *arg)
Definition value.c:3776
LONGEST value_as_long(struct value *val)
Definition value.c:2554
gdb_mpz value_as_mpz(struct value *val)
Definition value.c:2566
struct value * value_from_mpz(struct type *type, const gdb_mpz &v)
Definition value.c:3462
struct value * value_from_pointer(struct type *type, CORE_ADDR addr)
Definition value.c:3500
static bool value_true(struct value *val)
Definition value.h:1461
@ BOTH
Definition value.h:1289