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