]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/valarith.c
582fe6cc9450ddac1846177b6f0bb55debfedf80
[thirdparty/binutils-gdb.git] / gdb / valarith.c
1 /* Perform arithmetic and other operations on values, for GDB.
2
3 Copyright (C) 1986-2025 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 "extract-store-integer.h"
21 #include "value.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "target.h"
26 #include "language.h"
27 #include "target-float.h"
28 #include "infcall.h"
29 #include "gdbsupport/byte-vector.h"
30 #include "gdbarch.h"
31 #include "rust-lang.h"
32 #include "ada-lang.h"
33
34 /* Forward declarations. */
35 static 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
45 static LONGEST
46 find_size_for_pointer_math (struct type *ptr_type)
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
78 struct value *
79 value_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
99 LONGEST
100 value_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
119 sz = type_length_units (check_typedef (type1->target_type ()));
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
140 struct value *
141 value_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 std::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 std::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
208 static struct value *
209 value_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
256 struct value *
257 value_to_array (struct value *val)
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 \f
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
278 int
279 binop_types_user_defined_p (enum exp_opcode op,
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
303 int
304 binop_user_defined_p (enum exp_opcode op,
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
316 int
317 unop_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
337 static struct value *
338 value_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
368 static struct value *
369 value_user_defined_op (struct value **argp, gdb::array_view<value *> args,
370 char *name, int *static_memfuncp, enum noside noside)
371 {
372 struct value *result = NULL;
373
374 if (current_language->la_language == language_cplus)
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
395 struct value *
396 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
397 enum exp_opcode otherop, enum noside noside)
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);
540 if (noside == EVAL_AVOID_SIDE_EFFECTS)
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 }
551 if (noside == EVAL_AVOID_SIDE_EFFECTS)
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
571 struct value *
572 value_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);
653 if (noside == EVAL_AVOID_SIDE_EFFECTS)
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 }
664 if (noside == EVAL_AVOID_SIDE_EFFECTS)
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 \f
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
683 struct value *
684 value_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 \f
741
742 /* Obtain argument values for binary operation, converting from
743 other types if one of them is not floating point. */
744 static void
745 value_args_as_target_float (struct value *arg1, struct value *arg2,
746 gdb_byte *x, struct type **eff_type_x,
747 gdb_byte *y, struct type **eff_type_y)
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
808 static struct value *
809 fixed_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 ());
813 const struct language_defn *language = current_language;
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 (),
846 type1->fixed_point_scaling_factor ());
847 v2.read_fixed_point (arg2->contents (),
848 type_byte_order (type2), type2->is_unsigned (),
849 type2->fixed_point_scaling_factor ());
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 (),
860 type1->fixed_point_scaling_factor ());
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:
898 val = value_from_ulongest (language_bool_type (language, gdbarch),
899 v1 == v2 ? 1 : 0);
900 break;
901
902 case BINOP_LESS:
903 val = value_from_ulongest (language_bool_type (language, gdbarch),
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
917 static struct type *
918 promotion_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
953 static 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
958 static struct value *
959 complex_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
1070 static int
1071 type_length_bits (type *type)
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
1086 static bool
1087 check_valid_shift_count (enum exp_opcode op, type *result_type,
1088 type *shift_count_type, const gdb_mpz &shift_count,
1089 ULONGEST &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. */
1101 if (current_language->la_language == language_go)
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<ULONGEST> ();
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. */
1121 if (current_language->la_language != language_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
1140 static struct value *
1141 scalar_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 ULONGEST 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 ULONGEST nbits;
1305 if (!check_valid_shift_count (op, result_type, type2, v2, nbits))
1306 {
1307 /* Pretend the too-large shift was decomposed in a
1308 number of smaller shifts. An arithmetic signed
1309 right shift of a negative number always yields -1
1310 with such semantics. This is the right thing to
1311 do for Go, and we might as well do it for
1312 languages where it is undefined. Also, pretend a
1313 shift by a negative number was a shift by the
1314 negative number cast to unsigned, which is the
1315 same as shifting by a too-large number. */
1316 if (v1 < 0 && !result_type->is_unsigned ())
1317 v = -1;
1318 else
1319 v = 0;
1320 }
1321 else
1322 v = v1 >> nbits;
1323 }
1324 break;
1325
1326 case BINOP_BITWISE_AND:
1327 v = v1 & v2;
1328 break;
1329
1330 case BINOP_BITWISE_IOR:
1331 v = v1 | v2;
1332 break;
1333
1334 case BINOP_BITWISE_XOR:
1335 v = v1 ^ v2;
1336 break;
1337
1338 case BINOP_MIN:
1339 v = v1 < v2 ? v1 : v2;
1340 break;
1341
1342 case BINOP_MAX:
1343 v = v1 > v2 ? v1 : v2;
1344 break;
1345
1346 case BINOP_EQUAL:
1347 v = v1 == v2;
1348 break;
1349
1350 case BINOP_NOTEQUAL:
1351 v = v1 != v2;
1352 break;
1353
1354 case BINOP_LESS:
1355 v = v1 < v2;
1356 break;
1357
1358 case BINOP_GTR:
1359 v = v1 > v2;
1360 break;
1361
1362 case BINOP_LEQ:
1363 v = v1 <= v2;
1364 break;
1365
1366 case BINOP_GEQ:
1367 v = v1 >= v2;
1368 break;
1369
1370 default:
1371 error (_("Invalid binary operation on numbers."));
1372 }
1373
1374 val = value_from_mpz (result_type, v);
1375 }
1376
1377 return val;
1378 }
1379
1380 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1381 replicating SCALAR_VALUE for each element of the vector. Only scalar
1382 types that can be cast to the type of one element of the vector are
1383 acceptable. The newly created vector value is returned upon success,
1384 otherwise an error is thrown. */
1385
1386 struct value *
1387 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1388 {
1389 /* Widen the scalar to a vector. */
1390 struct type *eltype, *scalar_type;
1391 struct value *elval;
1392 LONGEST low_bound, high_bound;
1393 int i;
1394
1395 vector_type = check_typedef (vector_type);
1396
1397 gdb_assert (vector_type->code () == TYPE_CODE_ARRAY
1398 && vector_type->is_vector ());
1399
1400 if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1401 error (_("Could not determine the vector bounds"));
1402
1403 eltype = check_typedef (vector_type->target_type ());
1404 elval = value_cast (eltype, scalar_value);
1405
1406 scalar_type = check_typedef (scalar_value->type ());
1407
1408 /* If we reduced the length of the scalar then check we didn't loose any
1409 important bits. */
1410 if (eltype->length () < scalar_type->length ()
1411 && !value_equal (elval, scalar_value))
1412 error (_("conversion of scalar to vector involves truncation"));
1413
1414 value *val = value::allocate (vector_type);
1415 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1416 int elt_len = eltype->length ();
1417
1418 for (i = 0; i < high_bound - low_bound + 1; i++)
1419 /* Duplicate the contents of elval into the destination vector. */
1420 copy (elval->contents_all (),
1421 val_contents.slice (i * elt_len, elt_len));
1422
1423 return val;
1424 }
1425
1426 /* Performs a binary operation on two vector operands by calling scalar_binop
1427 for each pair of vector components. */
1428
1429 static struct value *
1430 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1431 {
1432 struct type *type1, *type2, *eltype1, *eltype2;
1433 int t1_is_vec, t2_is_vec, elsize, i;
1434 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1435
1436 type1 = check_typedef (val1->type ());
1437 type2 = check_typedef (val2->type ());
1438
1439 t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
1440 && type1->is_vector ()) ? 1 : 0;
1441 t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
1442 && type2->is_vector ()) ? 1 : 0;
1443
1444 if (!t1_is_vec || !t2_is_vec)
1445 error (_("Vector operations are only supported among vectors"));
1446
1447 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1448 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1449 error (_("Could not determine the vector bounds"));
1450
1451 eltype1 = check_typedef (type1->target_type ());
1452 eltype2 = check_typedef (type2->target_type ());
1453 elsize = eltype1->length ();
1454
1455 if (eltype1->code () != eltype2->code ()
1456 || elsize != eltype2->length ()
1457 || eltype1->is_unsigned () != eltype2->is_unsigned ()
1458 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1459 error (_("Cannot perform operation on vectors with different types"));
1460
1461 value *val = value::allocate (type1);
1462 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1463 scoped_value_mark mark;
1464 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1465 {
1466 value *tmp = value_binop (value_subscript (val1, i),
1467 value_subscript (val2, i), op);
1468 copy (tmp->contents_all (),
1469 val_contents.slice (i * elsize, elsize));
1470 }
1471
1472 return val;
1473 }
1474
1475 /* Perform a binary operation on two operands. */
1476
1477 struct value *
1478 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1479 {
1480 struct value *val;
1481 struct type *type1 = check_typedef (arg1->type ());
1482 struct type *type2 = check_typedef (arg2->type ());
1483 int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
1484 && type1->is_vector ());
1485 int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
1486 && type2->is_vector ());
1487
1488 if (!t1_is_vec && !t2_is_vec)
1489 val = scalar_binop (arg1, arg2, op);
1490 else if (t1_is_vec && t2_is_vec)
1491 val = vector_binop (arg1, arg2, op);
1492 else
1493 {
1494 /* Widen the scalar operand to a vector. */
1495 struct value **v = t1_is_vec ? &arg2 : &arg1;
1496 struct type *t = t1_is_vec ? type2 : type1;
1497
1498 if (t->code () != TYPE_CODE_FLT
1499 && t->code () != TYPE_CODE_DECFLOAT
1500 && !is_integral_type (t))
1501 error (_("Argument to operation not a number or boolean."));
1502
1503 /* Replicate the scalar value to make a vector value. */
1504 *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1505
1506 val = vector_binop (arg1, arg2, op);
1507 }
1508
1509 return val;
1510 }
1511 \f
1512 /* See value.h. */
1513
1514 bool
1515 value_logical_not (struct value *arg1)
1516 {
1517 int len;
1518 const gdb_byte *p;
1519 struct type *type1;
1520
1521 arg1 = coerce_array (arg1);
1522 type1 = check_typedef (arg1->type ());
1523
1524 if (is_floating_value (arg1))
1525 return target_float_is_zero (arg1->contents ().data (), type1);
1526
1527 len = type1->length ();
1528 p = arg1->contents ().data ();
1529
1530 while (--len >= 0)
1531 {
1532 if (*p++)
1533 break;
1534 }
1535
1536 return len < 0;
1537 }
1538
1539 /* Perform a comparison on two string values (whose content are not
1540 necessarily null terminated) based on their length. */
1541
1542 static int
1543 value_strcmp (struct value *arg1, struct value *arg2)
1544 {
1545 int len1 = arg1->type ()->length ();
1546 int len2 = arg2->type ()->length ();
1547 const gdb_byte *s1 = arg1->contents ().data ();
1548 const gdb_byte *s2 = arg2->contents ().data ();
1549 int i, len = len1 < len2 ? len1 : len2;
1550
1551 for (i = 0; i < len; i++)
1552 {
1553 if (s1[i] < s2[i])
1554 return -1;
1555 else if (s1[i] > s2[i])
1556 return 1;
1557 else
1558 continue;
1559 }
1560
1561 if (len1 < len2)
1562 return -1;
1563 else if (len1 > len2)
1564 return 1;
1565 else
1566 return 0;
1567 }
1568
1569 /* Simulate the C operator == by returning a 1
1570 iff ARG1 and ARG2 have equal contents. */
1571
1572 int
1573 value_equal (struct value *arg1, struct value *arg2)
1574 {
1575 int len;
1576 const gdb_byte *p1;
1577 const gdb_byte *p2;
1578 struct type *type1, *type2;
1579 enum type_code code1;
1580 enum type_code code2;
1581 int is_int1, is_int2;
1582
1583 arg1 = coerce_array (arg1);
1584 arg2 = coerce_array (arg2);
1585
1586 type1 = check_typedef (arg1->type ());
1587 type2 = check_typedef (arg2->type ());
1588 code1 = type1->code ();
1589 code2 = type2->code ();
1590 is_int1 = is_integral_type (type1);
1591 is_int2 = is_integral_type (type2);
1592
1593 if (is_int1 && is_int2)
1594 return value_true (value_binop (arg1, arg2, BINOP_EQUAL));
1595 else if ((is_floating_value (arg1) || is_int1)
1596 && (is_floating_value (arg2) || is_int2))
1597 {
1598 struct type *eff_type_v1, *eff_type_v2;
1599 gdb::byte_vector v1, v2;
1600 v1.resize (std::max (type1->length (), type2->length ()));
1601 v2.resize (std::max (type1->length (), type2->length ()));
1602
1603 value_args_as_target_float (arg1, arg2,
1604 v1.data (), &eff_type_v1,
1605 v2.data (), &eff_type_v2);
1606
1607 return target_float_compare (v1.data (), eff_type_v1,
1608 v2.data (), eff_type_v2) == 0;
1609 }
1610
1611 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1612 is bigger. */
1613 else if (code1 == TYPE_CODE_PTR && is_int2)
1614 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1615 else if (code2 == TYPE_CODE_PTR && is_int1)
1616 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1617
1618 else if (code1 == code2
1619 && ((len = (int) type1->length ())
1620 == (int) type2->length ()))
1621 {
1622 p1 = arg1->contents ().data ();
1623 p2 = arg2->contents ().data ();
1624 while (--len >= 0)
1625 {
1626 if (*p1++ != *p2++)
1627 break;
1628 }
1629 return len < 0;
1630 }
1631 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1632 {
1633 return value_strcmp (arg1, arg2) == 0;
1634 }
1635 else
1636 error (_("Invalid type combination in equality test."));
1637 }
1638
1639 /* Compare values based on their raw contents. Useful for arrays since
1640 value_equal coerces them to pointers, thus comparing just the address
1641 of the array instead of its contents. */
1642
1643 int
1644 value_equal_contents (struct value *arg1, struct value *arg2)
1645 {
1646 struct type *type1, *type2;
1647
1648 type1 = check_typedef (arg1->type ());
1649 type2 = check_typedef (arg2->type ());
1650
1651 return (type1->code () == type2->code ()
1652 && type1->length () == type2->length ()
1653 && memcmp (arg1->contents ().data (),
1654 arg2->contents ().data (),
1655 type1->length ()) == 0);
1656 }
1657
1658 /* Simulate the C operator < by returning 1
1659 iff ARG1's contents are less than ARG2's. */
1660
1661 int
1662 value_less (struct value *arg1, struct value *arg2)
1663 {
1664 enum type_code code1;
1665 enum type_code code2;
1666 struct type *type1, *type2;
1667 int is_int1, is_int2;
1668
1669 arg1 = coerce_array (arg1);
1670 arg2 = coerce_array (arg2);
1671
1672 type1 = check_typedef (arg1->type ());
1673 type2 = check_typedef (arg2->type ());
1674 code1 = type1->code ();
1675 code2 = type2->code ();
1676 is_int1 = is_integral_type (type1);
1677 is_int2 = is_integral_type (type2);
1678
1679 if ((is_int1 && is_int2)
1680 || (is_fixed_point_type (type1) && is_fixed_point_type (type2)))
1681 return value_true (value_binop (arg1, arg2, BINOP_LESS));
1682 else if ((is_floating_value (arg1) || is_int1)
1683 && (is_floating_value (arg2) || is_int2))
1684 {
1685 struct type *eff_type_v1, *eff_type_v2;
1686 gdb::byte_vector v1, v2;
1687 v1.resize (std::max (type1->length (), type2->length ()));
1688 v2.resize (std::max (type1->length (), type2->length ()));
1689
1690 value_args_as_target_float (arg1, arg2,
1691 v1.data (), &eff_type_v1,
1692 v2.data (), &eff_type_v2);
1693
1694 return target_float_compare (v1.data (), eff_type_v1,
1695 v2.data (), eff_type_v2) == -1;
1696 }
1697 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1698 return value_as_address (arg1) < value_as_address (arg2);
1699
1700 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1701 is bigger. */
1702 else if (code1 == TYPE_CODE_PTR && is_int2)
1703 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1704 else if (code2 == TYPE_CODE_PTR && is_int1)
1705 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1706 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1707 return value_strcmp (arg1, arg2) < 0;
1708 else
1709 {
1710 error (_("Invalid type combination in ordering comparison."));
1711 return 0;
1712 }
1713 }
1714 \f
1715 /* See value.h. */
1716
1717 struct value *
1718 value_pos (struct value *arg1)
1719 {
1720 struct type *type;
1721
1722 arg1 = coerce_ref (arg1);
1723 type = check_typedef (arg1->type ());
1724
1725 if (is_integral_type (type) || is_floating_value (arg1)
1726 || (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1727 || type->code () == TYPE_CODE_COMPLEX)
1728 return value_from_contents (type, arg1->contents ().data ());
1729 else
1730 error (_("Argument to positive operation not a number."));
1731 }
1732
1733 /* See value.h. */
1734
1735 struct value *
1736 value_neg (struct value *arg1)
1737 {
1738 struct type *type;
1739
1740 arg1 = coerce_ref (arg1);
1741 type = check_typedef (arg1->type ());
1742
1743 if (is_integral_type (type) || is_floating_type (type))
1744 return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
1745 else if (is_fixed_point_type (type))
1746 return value_binop (value::zero (type, not_lval), arg1, BINOP_SUB);
1747 else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1748 {
1749 struct value *val = value::allocate (type);
1750 struct type *eltype = check_typedef (type->target_type ());
1751 int i;
1752 LONGEST low_bound, high_bound;
1753
1754 if (!get_array_bounds (type, &low_bound, &high_bound))
1755 error (_("Could not determine the vector bounds"));
1756
1757 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1758 int elt_len = eltype->length ();
1759
1760 for (i = 0; i < high_bound - low_bound + 1; i++)
1761 {
1762 value *tmp = value_neg (value_subscript (arg1, i));
1763 copy (tmp->contents_all (),
1764 val_contents.slice (i * elt_len, elt_len));
1765 }
1766 return val;
1767 }
1768 else if (type->code () == TYPE_CODE_COMPLEX)
1769 {
1770 struct value *real = value_real_part (arg1);
1771 struct value *imag = value_imaginary_part (arg1);
1772
1773 real = value_neg (real);
1774 imag = value_neg (imag);
1775 return value_literal_complex (real, imag, type);
1776 }
1777 else
1778 error (_("Argument to negate operation not a number."));
1779 }
1780
1781 /* See value.h. */
1782
1783 struct value *
1784 value_complement (struct value *arg1)
1785 {
1786 struct type *type;
1787 struct value *val;
1788
1789 arg1 = coerce_ref (arg1);
1790 type = check_typedef (arg1->type ());
1791
1792 if (is_integral_type (type))
1793 {
1794 gdb_mpz num = value_as_mpz (arg1);
1795 num.complement ();
1796 val = value_from_mpz (type, num);
1797 }
1798 else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1799 {
1800 struct type *eltype = check_typedef (type->target_type ());
1801 int i;
1802 LONGEST low_bound, high_bound;
1803
1804 if (!get_array_bounds (type, &low_bound, &high_bound))
1805 error (_("Could not determine the vector bounds"));
1806
1807 val = value::allocate (type);
1808 gdb::array_view<gdb_byte> val_contents = val->contents_writeable ();
1809 int elt_len = eltype->length ();
1810
1811 for (i = 0; i < high_bound - low_bound + 1; i++)
1812 {
1813 value *tmp = value_complement (value_subscript (arg1, i));
1814 copy (tmp->contents_all (),
1815 val_contents.slice (i * elt_len, elt_len));
1816 }
1817 }
1818 else if (type->code () == TYPE_CODE_COMPLEX)
1819 {
1820 /* GCC has an extension that treats ~complex as the complex
1821 conjugate. */
1822 struct value *real = value_real_part (arg1);
1823 struct value *imag = value_imaginary_part (arg1);
1824
1825 imag = value_neg (imag);
1826 return value_literal_complex (real, imag, type);
1827 }
1828 else
1829 error (_("Argument to complement operation not an integer, boolean."));
1830
1831 return val;
1832 }
1833 \f
1834 /* The INDEX'th bit of SET value whose value_type is TYPE,
1835 and whose value_contents is valaddr.
1836 Return -1 if out of range, -2 other error. */
1837
1838 int
1839 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1840 {
1841 struct gdbarch *gdbarch = type->arch ();
1842 LONGEST low_bound, high_bound;
1843 LONGEST word;
1844 unsigned rel_index;
1845 struct type *range = type->index_type ();
1846
1847 if (!get_discrete_bounds (range, &low_bound, &high_bound))
1848 return -2;
1849 if (index < low_bound || index > high_bound)
1850 return -1;
1851 rel_index = index - low_bound;
1852 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1853 type_byte_order (type));
1854 rel_index %= TARGET_CHAR_BIT;
1855 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1856 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1857 return (word >> rel_index) & 1;
1858 }