do_target_wait_1: Clear TARGET_WNOHANG if the target isn't async.
[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 || op == BINOP_CONCAT)
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 with the following conditions:
655
656 (1) Both values must be either bitstring values or character string
657 values and the resulting value consists of the concatenation of
658 ARG1 followed by ARG2.
659
660 or
661
662 One value must be an integer value and the other value must be
663 either a bitstring value or character string value, which is
664 to be repeated by the number of times specified by the integer
665 value.
666
667
668 (2) Boolean values are also allowed and are treated as bit string
669 values of length 1.
670
671 (3) Character values are also allowed and are treated as character
672 string values of length 1. */
673
674 struct value *
675 value_concat (struct value *arg1, struct value *arg2)
676 {
677 struct value *inval1;
678 struct value *inval2;
679 struct value *outval = NULL;
680 int inval1len, inval2len;
681 int count, idx;
682 char inchar;
683 struct type *type1 = check_typedef (value_type (arg1));
684 struct type *type2 = check_typedef (value_type (arg2));
685 struct type *char_type;
686
687 /* First figure out if we are dealing with two values to be concatenated
688 or a repeat count and a value to be repeated. INVAL1 is set to the
689 first of two concatenated values, or the repeat count. INVAL2 is set
690 to the second of the two concatenated values or the value to be
691 repeated. */
692
693 if (type2->code () == TYPE_CODE_INT)
694 {
695 struct type *tmp = type1;
696
697 type1 = tmp;
698 tmp = type2;
699 inval1 = arg2;
700 inval2 = arg1;
701 }
702 else
703 {
704 inval1 = arg1;
705 inval2 = arg2;
706 }
707
708 /* Now process the input values. */
709
710 if (type1->code () == TYPE_CODE_INT)
711 {
712 /* We have a repeat count. Validate the second value and then
713 construct a value repeated that many times. */
714 if (type2->code () == TYPE_CODE_STRING
715 || type2->code () == TYPE_CODE_CHAR)
716 {
717 count = longest_to_int (value_as_long (inval1));
718 inval2len = TYPE_LENGTH (type2);
719 std::vector<char> ptr (count * inval2len);
720 if (type2->code () == TYPE_CODE_CHAR)
721 {
722 char_type = type2;
723
724 inchar = (char) unpack_long (type2,
725 value_contents (inval2).data ());
726 for (idx = 0; idx < count; idx++)
727 {
728 ptr[idx] = inchar;
729 }
730 }
731 else
732 {
733 char_type = TYPE_TARGET_TYPE (type2);
734
735 for (idx = 0; idx < count; idx++)
736 memcpy (&ptr[idx * inval2len], value_contents (inval2).data (),
737 inval2len);
738 }
739 outval = value_string (ptr.data (), count * inval2len, char_type);
740 }
741 else if (type2->code () == TYPE_CODE_BOOL)
742 {
743 error (_("unimplemented support for boolean repeats"));
744 }
745 else
746 {
747 error (_("can't repeat values of that type"));
748 }
749 }
750 else if (type1->code () == TYPE_CODE_STRING
751 || type1->code () == TYPE_CODE_CHAR)
752 {
753 /* We have two character strings to concatenate. */
754 if (type2->code () != TYPE_CODE_STRING
755 && type2->code () != TYPE_CODE_CHAR)
756 {
757 error (_("Strings can only be concatenated with other strings."));
758 }
759 inval1len = TYPE_LENGTH (type1);
760 inval2len = TYPE_LENGTH (type2);
761 std::vector<char> ptr (inval1len + inval2len);
762 if (type1->code () == TYPE_CODE_CHAR)
763 {
764 char_type = type1;
765
766 ptr[0] = (char) unpack_long (type1, value_contents (inval1).data ());
767 }
768 else
769 {
770 char_type = TYPE_TARGET_TYPE (type1);
771
772 memcpy (ptr.data (), value_contents (inval1).data (), inval1len);
773 }
774 if (type2->code () == TYPE_CODE_CHAR)
775 {
776 ptr[inval1len] =
777 (char) unpack_long (type2, value_contents (inval2).data ());
778 }
779 else
780 {
781 memcpy (&ptr[inval1len], value_contents (inval2).data (), inval2len);
782 }
783 outval = value_string (ptr.data (), inval1len + inval2len, char_type);
784 }
785 else if (type1->code () == TYPE_CODE_BOOL)
786 {
787 /* We have two bitstrings to concatenate. */
788 if (type2->code () != TYPE_CODE_BOOL)
789 {
790 error (_("Booleans can only be concatenated "
791 "with other bitstrings or booleans."));
792 }
793 error (_("unimplemented support for boolean concatenation."));
794 }
795 else
796 {
797 /* We don't know how to concatenate these operands. */
798 error (_("illegal operands for concatenation."));
799 }
800 return (outval);
801 }
802 \f
803 /* Integer exponentiation: V1**V2, where both arguments are
804 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
805
806 static LONGEST
807 integer_pow (LONGEST v1, LONGEST v2)
808 {
809 if (v2 < 0)
810 {
811 if (v1 == 0)
812 error (_("Attempt to raise 0 to negative power."));
813 else
814 return 0;
815 }
816 else
817 {
818 /* The Russian Peasant's Algorithm. */
819 LONGEST v;
820
821 v = 1;
822 for (;;)
823 {
824 if (v2 & 1L)
825 v *= v1;
826 v2 >>= 1;
827 if (v2 == 0)
828 return v;
829 v1 *= v1;
830 }
831 }
832 }
833
834 /* Obtain argument values for binary operation, converting from
835 other types if one of them is not floating point. */
836 static void
837 value_args_as_target_float (struct value *arg1, struct value *arg2,
838 gdb_byte *x, struct type **eff_type_x,
839 gdb_byte *y, struct type **eff_type_y)
840 {
841 struct type *type1, *type2;
842
843 type1 = check_typedef (value_type (arg1));
844 type2 = check_typedef (value_type (arg2));
845
846 /* At least one of the arguments must be of floating-point type. */
847 gdb_assert (is_floating_type (type1) || is_floating_type (type2));
848
849 if (is_floating_type (type1) && is_floating_type (type2)
850 && type1->code () != type2->code ())
851 /* The DFP extension to the C language does not allow mixing of
852 * decimal float types with other float types in expressions
853 * (see WDTR 24732, page 12). */
854 error (_("Mixing decimal floating types with "
855 "other floating types is not allowed."));
856
857 /* Obtain value of arg1, converting from other types if necessary. */
858
859 if (is_floating_type (type1))
860 {
861 *eff_type_x = type1;
862 memcpy (x, value_contents (arg1).data (), TYPE_LENGTH (type1));
863 }
864 else if (is_integral_type (type1))
865 {
866 *eff_type_x = type2;
867 if (type1->is_unsigned ())
868 target_float_from_ulongest (x, *eff_type_x, value_as_long (arg1));
869 else
870 target_float_from_longest (x, *eff_type_x, value_as_long (arg1));
871 }
872 else
873 error (_("Don't know how to convert from %s to %s."), type1->name (),
874 type2->name ());
875
876 /* Obtain value of arg2, converting from other types if necessary. */
877
878 if (is_floating_type (type2))
879 {
880 *eff_type_y = type2;
881 memcpy (y, value_contents (arg2).data (), TYPE_LENGTH (type2));
882 }
883 else if (is_integral_type (type2))
884 {
885 *eff_type_y = type1;
886 if (type2->is_unsigned ())
887 target_float_from_ulongest (y, *eff_type_y, value_as_long (arg2));
888 else
889 target_float_from_longest (y, *eff_type_y, value_as_long (arg2));
890 }
891 else
892 error (_("Don't know how to convert from %s to %s."), type1->name (),
893 type2->name ());
894 }
895
896 /* Assuming at last one of ARG1 or ARG2 is a fixed point value,
897 perform the binary operation OP on these two operands, and return
898 the resulting value (also as a fixed point). */
899
900 static struct value *
901 fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
902 {
903 struct type *type1 = check_typedef (value_type (arg1));
904 struct type *type2 = check_typedef (value_type (arg2));
905 const struct language_defn *language = current_language;
906
907 struct gdbarch *gdbarch = type1->arch ();
908 struct value *val;
909
910 gdb_mpq v1, v2, res;
911
912 gdb_assert (is_fixed_point_type (type1) || is_fixed_point_type (type2));
913 if (op == BINOP_MUL || op == BINOP_DIV)
914 {
915 v1 = value_to_gdb_mpq (arg1);
916 v2 = value_to_gdb_mpq (arg2);
917
918 /* The code below uses TYPE1 for the result type, so make sure
919 it is set properly. */
920 if (!is_fixed_point_type (type1))
921 type1 = type2;
922 }
923 else
924 {
925 if (!is_fixed_point_type (type1))
926 {
927 arg1 = value_cast (type2, arg1);
928 type1 = type2;
929 }
930 if (!is_fixed_point_type (type2))
931 {
932 arg2 = value_cast (type1, arg2);
933 type2 = type1;
934 }
935
936 v1.read_fixed_point (value_contents (arg1),
937 type_byte_order (type1), type1->is_unsigned (),
938 type1->fixed_point_scaling_factor ());
939 v2.read_fixed_point (value_contents (arg2),
940 type_byte_order (type2), type2->is_unsigned (),
941 type2->fixed_point_scaling_factor ());
942 }
943
944 auto fixed_point_to_value = [type1] (const gdb_mpq &fp)
945 {
946 value *fp_val = allocate_value (type1);
947
948 fp.write_fixed_point
949 (value_contents_raw (fp_val),
950 type_byte_order (type1),
951 type1->is_unsigned (),
952 type1->fixed_point_scaling_factor ());
953
954 return fp_val;
955 };
956
957 switch (op)
958 {
959 case BINOP_ADD:
960 mpq_add (res.val, v1.val, v2.val);
961 val = fixed_point_to_value (res);
962 break;
963
964 case BINOP_SUB:
965 mpq_sub (res.val, v1.val, v2.val);
966 val = fixed_point_to_value (res);
967 break;
968
969 case BINOP_MIN:
970 val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) < 0 ? v1 : v2);
971 break;
972
973 case BINOP_MAX:
974 val = fixed_point_to_value (mpq_cmp (v1.val, v2.val) > 0 ? v1 : v2);
975 break;
976
977 case BINOP_MUL:
978 mpq_mul (res.val, v1.val, v2.val);
979 val = fixed_point_to_value (res);
980 break;
981
982 case BINOP_DIV:
983 if (mpq_sgn (v2.val) == 0)
984 error (_("Division by zero"));
985 mpq_div (res.val, v1.val, v2.val);
986 val = fixed_point_to_value (res);
987 break;
988
989 case BINOP_EQUAL:
990 val = value_from_ulongest (language_bool_type (language, gdbarch),
991 mpq_cmp (v1.val, v2.val) == 0 ? 1 : 0);
992 break;
993
994 case BINOP_LESS:
995 val = value_from_ulongest (language_bool_type (language, gdbarch),
996 mpq_cmp (v1.val, v2.val) < 0 ? 1 : 0);
997 break;
998
999 default:
1000 error (_("Integer-only operation on fixed point number."));
1001 }
1002
1003 return val;
1004 }
1005
1006 /* A helper function that finds the type to use for a binary operation
1007 involving TYPE1 and TYPE2. */
1008
1009 static struct type *
1010 promotion_type (struct type *type1, struct type *type2)
1011 {
1012 struct type *result_type;
1013
1014 if (is_floating_type (type1) || is_floating_type (type2))
1015 {
1016 /* If only one type is floating-point, use its type.
1017 Otherwise use the bigger type. */
1018 if (!is_floating_type (type1))
1019 result_type = type2;
1020 else if (!is_floating_type (type2))
1021 result_type = type1;
1022 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1023 result_type = type2;
1024 else
1025 result_type = type1;
1026 }
1027 else
1028 {
1029 /* Integer types. */
1030 if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1031 result_type = type1;
1032 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1033 result_type = type2;
1034 else if (type1->is_unsigned ())
1035 result_type = type1;
1036 else if (type2->is_unsigned ())
1037 result_type = type2;
1038 else
1039 result_type = type1;
1040 }
1041
1042 return result_type;
1043 }
1044
1045 static struct value *scalar_binop (struct value *arg1, struct value *arg2,
1046 enum exp_opcode op);
1047
1048 /* Perform a binary operation on complex operands. */
1049
1050 static struct value *
1051 complex_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1052 {
1053 struct type *arg1_type = check_typedef (value_type (arg1));
1054 struct type *arg2_type = check_typedef (value_type (arg2));
1055
1056 struct value *arg1_real, *arg1_imag, *arg2_real, *arg2_imag;
1057 if (arg1_type->code () == TYPE_CODE_COMPLEX)
1058 {
1059 arg1_real = value_real_part (arg1);
1060 arg1_imag = value_imaginary_part (arg1);
1061 }
1062 else
1063 {
1064 arg1_real = arg1;
1065 arg1_imag = value_zero (arg1_type, not_lval);
1066 }
1067 if (arg2_type->code () == TYPE_CODE_COMPLEX)
1068 {
1069 arg2_real = value_real_part (arg2);
1070 arg2_imag = value_imaginary_part (arg2);
1071 }
1072 else
1073 {
1074 arg2_real = arg2;
1075 arg2_imag = value_zero (arg2_type, not_lval);
1076 }
1077
1078 struct type *comp_type = promotion_type (value_type (arg1_real),
1079 value_type (arg2_real));
1080 if (!can_create_complex_type (comp_type))
1081 error (_("Argument to complex arithmetic operation not supported."));
1082
1083 arg1_real = value_cast (comp_type, arg1_real);
1084 arg1_imag = value_cast (comp_type, arg1_imag);
1085 arg2_real = value_cast (comp_type, arg2_real);
1086 arg2_imag = value_cast (comp_type, arg2_imag);
1087
1088 struct type *result_type = init_complex_type (nullptr, comp_type);
1089
1090 struct value *result_real, *result_imag;
1091 switch (op)
1092 {
1093 case BINOP_ADD:
1094 case BINOP_SUB:
1095 result_real = scalar_binop (arg1_real, arg2_real, op);
1096 result_imag = scalar_binop (arg1_imag, arg2_imag, op);
1097 break;
1098
1099 case BINOP_MUL:
1100 {
1101 struct value *x1 = scalar_binop (arg1_real, arg2_real, op);
1102 struct value *x2 = scalar_binop (arg1_imag, arg2_imag, op);
1103 result_real = scalar_binop (x1, x2, BINOP_SUB);
1104
1105 x1 = scalar_binop (arg1_real, arg2_imag, op);
1106 x2 = scalar_binop (arg1_imag, arg2_real, op);
1107 result_imag = scalar_binop (x1, x2, BINOP_ADD);
1108 }
1109 break;
1110
1111 case BINOP_DIV:
1112 {
1113 if (arg2_type->code () == TYPE_CODE_COMPLEX)
1114 {
1115 struct value *conjugate = value_complement (arg2);
1116 /* We have to reconstruct ARG1, in case the type was
1117 promoted. */
1118 arg1 = value_literal_complex (arg1_real, arg1_imag, result_type);
1119
1120 struct value *numerator = scalar_binop (arg1, conjugate,
1121 BINOP_MUL);
1122 arg1_real = value_real_part (numerator);
1123 arg1_imag = value_imaginary_part (numerator);
1124
1125 struct value *x1 = scalar_binop (arg2_real, arg2_real, BINOP_MUL);
1126 struct value *x2 = scalar_binop (arg2_imag, arg2_imag, BINOP_MUL);
1127 arg2_real = scalar_binop (x1, x2, BINOP_ADD);
1128 }
1129
1130 result_real = scalar_binop (arg1_real, arg2_real, op);
1131 result_imag = scalar_binop (arg1_imag, arg2_real, op);
1132 }
1133 break;
1134
1135 case BINOP_EQUAL:
1136 case BINOP_NOTEQUAL:
1137 {
1138 struct value *x1 = scalar_binop (arg1_real, arg2_real, op);
1139 struct value *x2 = scalar_binop (arg1_imag, arg2_imag, op);
1140
1141 LONGEST v1 = value_as_long (x1);
1142 LONGEST v2 = value_as_long (x2);
1143
1144 if (op == BINOP_EQUAL)
1145 v1 = v1 && v2;
1146 else
1147 v1 = v1 || v2;
1148
1149 return value_from_longest (value_type (x1), v1);
1150 }
1151 break;
1152
1153 default:
1154 error (_("Invalid binary operation on numbers."));
1155 }
1156
1157 return value_literal_complex (result_real, result_imag, result_type);
1158 }
1159
1160 /* Perform a binary operation on two operands which have reasonable
1161 representations as integers or floats. This includes booleans,
1162 characters, integers, or floats.
1163 Does not support addition and subtraction on pointers;
1164 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
1165
1166 static struct value *
1167 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1168 {
1169 struct value *val;
1170 struct type *type1, *type2, *result_type;
1171
1172 arg1 = coerce_ref (arg1);
1173 arg2 = coerce_ref (arg2);
1174
1175 type1 = check_typedef (value_type (arg1));
1176 type2 = check_typedef (value_type (arg2));
1177
1178 if (type1->code () == TYPE_CODE_COMPLEX
1179 || type2->code () == TYPE_CODE_COMPLEX)
1180 return complex_binop (arg1, arg2, op);
1181
1182 if ((!is_floating_value (arg1)
1183 && !is_integral_type (type1)
1184 && !is_fixed_point_type (type1))
1185 || (!is_floating_value (arg2)
1186 && !is_integral_type (type2)
1187 && !is_fixed_point_type (type2)))
1188 error (_("Argument to arithmetic operation not a number or boolean."));
1189
1190 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
1191 return fixed_point_binop (arg1, arg2, op);
1192
1193 if (is_floating_type (type1) || is_floating_type (type2))
1194 {
1195 result_type = promotion_type (type1, type2);
1196 val = allocate_value (result_type);
1197
1198 struct type *eff_type_v1, *eff_type_v2;
1199 gdb::byte_vector v1, v2;
1200 v1.resize (TYPE_LENGTH (result_type));
1201 v2.resize (TYPE_LENGTH (result_type));
1202
1203 value_args_as_target_float (arg1, arg2,
1204 v1.data (), &eff_type_v1,
1205 v2.data (), &eff_type_v2);
1206 target_float_binop (op, v1.data (), eff_type_v1,
1207 v2.data (), eff_type_v2,
1208 value_contents_raw (val).data (), result_type);
1209 }
1210 else if (type1->code () == TYPE_CODE_BOOL
1211 || type2->code () == TYPE_CODE_BOOL)
1212 {
1213 LONGEST v1, v2, v = 0;
1214
1215 v1 = value_as_long (arg1);
1216 v2 = value_as_long (arg2);
1217
1218 switch (op)
1219 {
1220 case BINOP_BITWISE_AND:
1221 v = v1 & v2;
1222 break;
1223
1224 case BINOP_BITWISE_IOR:
1225 v = v1 | v2;
1226 break;
1227
1228 case BINOP_BITWISE_XOR:
1229 v = v1 ^ v2;
1230 break;
1231
1232 case BINOP_EQUAL:
1233 v = v1 == v2;
1234 break;
1235
1236 case BINOP_NOTEQUAL:
1237 v = v1 != v2;
1238 break;
1239
1240 default:
1241 error (_("Invalid operation on booleans."));
1242 }
1243
1244 result_type = type1;
1245
1246 val = allocate_value (result_type);
1247 store_signed_integer (value_contents_raw (val).data (),
1248 TYPE_LENGTH (result_type),
1249 type_byte_order (result_type),
1250 v);
1251 }
1252 else
1253 /* Integral operations here. */
1254 {
1255 /* Determine type length of the result, and if the operation should
1256 be done unsigned. For exponentiation and shift operators,
1257 use the length and type of the left operand. Otherwise,
1258 use the signedness of the operand with the greater length.
1259 If both operands are of equal length, use unsigned operation
1260 if one of the operands is unsigned. */
1261 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1262 result_type = type1;
1263 else
1264 result_type = promotion_type (type1, type2);
1265
1266 if (result_type->is_unsigned ())
1267 {
1268 LONGEST v2_signed = value_as_long (arg2);
1269 ULONGEST v1, v2, v = 0;
1270
1271 v1 = (ULONGEST) value_as_long (arg1);
1272 v2 = (ULONGEST) v2_signed;
1273
1274 switch (op)
1275 {
1276 case BINOP_ADD:
1277 v = v1 + v2;
1278 break;
1279
1280 case BINOP_SUB:
1281 v = v1 - v2;
1282 break;
1283
1284 case BINOP_MUL:
1285 v = v1 * v2;
1286 break;
1287
1288 case BINOP_DIV:
1289 case BINOP_INTDIV:
1290 if (v2 != 0)
1291 v = v1 / v2;
1292 else
1293 error (_("Division by zero"));
1294 break;
1295
1296 case BINOP_EXP:
1297 v = uinteger_pow (v1, v2_signed);
1298 break;
1299
1300 case BINOP_REM:
1301 if (v2 != 0)
1302 v = v1 % v2;
1303 else
1304 error (_("Division by zero"));
1305 break;
1306
1307 case BINOP_MOD:
1308 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1309 v1 mod 0 has a defined value, v1. */
1310 if (v2 == 0)
1311 {
1312 v = v1;
1313 }
1314 else
1315 {
1316 v = v1 / v2;
1317 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1318 v = v1 - (v2 * v);
1319 }
1320 break;
1321
1322 case BINOP_LSH:
1323 v = v1 << v2;
1324 break;
1325
1326 case BINOP_RSH:
1327 v = v1 >> v2;
1328 break;
1329
1330 case BINOP_BITWISE_AND:
1331 v = v1 & v2;
1332 break;
1333
1334 case BINOP_BITWISE_IOR:
1335 v = v1 | v2;
1336 break;
1337
1338 case BINOP_BITWISE_XOR:
1339 v = v1 ^ v2;
1340 break;
1341
1342 case BINOP_LOGICAL_AND:
1343 v = v1 && v2;
1344 break;
1345
1346 case BINOP_LOGICAL_OR:
1347 v = v1 || v2;
1348 break;
1349
1350 case BINOP_MIN:
1351 v = v1 < v2 ? v1 : v2;
1352 break;
1353
1354 case BINOP_MAX:
1355 v = v1 > v2 ? v1 : v2;
1356 break;
1357
1358 case BINOP_EQUAL:
1359 v = v1 == v2;
1360 break;
1361
1362 case BINOP_NOTEQUAL:
1363 v = v1 != v2;
1364 break;
1365
1366 case BINOP_LESS:
1367 v = v1 < v2;
1368 break;
1369
1370 case BINOP_GTR:
1371 v = v1 > v2;
1372 break;
1373
1374 case BINOP_LEQ:
1375 v = v1 <= v2;
1376 break;
1377
1378 case BINOP_GEQ:
1379 v = v1 >= v2;
1380 break;
1381
1382 default:
1383 error (_("Invalid binary operation on numbers."));
1384 }
1385
1386 val = allocate_value (result_type);
1387 store_unsigned_integer (value_contents_raw (val).data (),
1388 TYPE_LENGTH (value_type (val)),
1389 type_byte_order (result_type),
1390 v);
1391 }
1392 else
1393 {
1394 LONGEST v1, v2, v = 0;
1395
1396 v1 = value_as_long (arg1);
1397 v2 = value_as_long (arg2);
1398
1399 switch (op)
1400 {
1401 case BINOP_ADD:
1402 v = v1 + v2;
1403 break;
1404
1405 case BINOP_SUB:
1406 v = v1 - v2;
1407 break;
1408
1409 case BINOP_MUL:
1410 v = v1 * v2;
1411 break;
1412
1413 case BINOP_DIV:
1414 case BINOP_INTDIV:
1415 if (v2 != 0)
1416 v = v1 / v2;
1417 else
1418 error (_("Division by zero"));
1419 break;
1420
1421 case BINOP_EXP:
1422 v = integer_pow (v1, v2);
1423 break;
1424
1425 case BINOP_REM:
1426 if (v2 != 0)
1427 v = v1 % v2;
1428 else
1429 error (_("Division by zero"));
1430 break;
1431
1432 case BINOP_MOD:
1433 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1434 X mod 0 has a defined value, X. */
1435 if (v2 == 0)
1436 {
1437 v = v1;
1438 }
1439 else
1440 {
1441 v = v1 / v2;
1442 /* Compute floor. */
1443 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1444 {
1445 v--;
1446 }
1447 v = v1 - (v2 * v);
1448 }
1449 break;
1450
1451 case BINOP_LSH:
1452 v = v1 << v2;
1453 break;
1454
1455 case BINOP_RSH:
1456 v = v1 >> v2;
1457 break;
1458
1459 case BINOP_BITWISE_AND:
1460 v = v1 & v2;
1461 break;
1462
1463 case BINOP_BITWISE_IOR:
1464 v = v1 | v2;
1465 break;
1466
1467 case BINOP_BITWISE_XOR:
1468 v = v1 ^ v2;
1469 break;
1470
1471 case BINOP_LOGICAL_AND:
1472 v = v1 && v2;
1473 break;
1474
1475 case BINOP_LOGICAL_OR:
1476 v = v1 || v2;
1477 break;
1478
1479 case BINOP_MIN:
1480 v = v1 < v2 ? v1 : v2;
1481 break;
1482
1483 case BINOP_MAX:
1484 v = v1 > v2 ? v1 : v2;
1485 break;
1486
1487 case BINOP_EQUAL:
1488 v = v1 == v2;
1489 break;
1490
1491 case BINOP_NOTEQUAL:
1492 v = v1 != v2;
1493 break;
1494
1495 case BINOP_LESS:
1496 v = v1 < v2;
1497 break;
1498
1499 case BINOP_GTR:
1500 v = v1 > v2;
1501 break;
1502
1503 case BINOP_LEQ:
1504 v = v1 <= v2;
1505 break;
1506
1507 case BINOP_GEQ:
1508 v = v1 >= v2;
1509 break;
1510
1511 default:
1512 error (_("Invalid binary operation on numbers."));
1513 }
1514
1515 val = allocate_value (result_type);
1516 store_signed_integer (value_contents_raw (val).data (),
1517 TYPE_LENGTH (value_type (val)),
1518 type_byte_order (result_type),
1519 v);
1520 }
1521 }
1522
1523 return val;
1524 }
1525
1526 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1527 replicating SCALAR_VALUE for each element of the vector. Only scalar
1528 types that can be cast to the type of one element of the vector are
1529 acceptable. The newly created vector value is returned upon success,
1530 otherwise an error is thrown. */
1531
1532 struct value *
1533 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1534 {
1535 /* Widen the scalar to a vector. */
1536 struct type *eltype, *scalar_type;
1537 struct value *elval;
1538 LONGEST low_bound, high_bound;
1539 int i;
1540
1541 vector_type = check_typedef (vector_type);
1542
1543 gdb_assert (vector_type->code () == TYPE_CODE_ARRAY
1544 && vector_type->is_vector ());
1545
1546 if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1547 error (_("Could not determine the vector bounds"));
1548
1549 eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
1550 elval = value_cast (eltype, scalar_value);
1551
1552 scalar_type = check_typedef (value_type (scalar_value));
1553
1554 /* If we reduced the length of the scalar then check we didn't loose any
1555 important bits. */
1556 if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
1557 && !value_equal (elval, scalar_value))
1558 error (_("conversion of scalar to vector involves truncation"));
1559
1560 value *val = allocate_value (vector_type);
1561 gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
1562 int elt_len = TYPE_LENGTH (eltype);
1563
1564 for (i = 0; i < high_bound - low_bound + 1; i++)
1565 /* Duplicate the contents of elval into the destination vector. */
1566 copy (value_contents_all (elval),
1567 val_contents.slice (i * elt_len, elt_len));
1568
1569 return val;
1570 }
1571
1572 /* Performs a binary operation on two vector operands by calling scalar_binop
1573 for each pair of vector components. */
1574
1575 static struct value *
1576 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1577 {
1578 struct type *type1, *type2, *eltype1, *eltype2;
1579 int t1_is_vec, t2_is_vec, elsize, i;
1580 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1581
1582 type1 = check_typedef (value_type (val1));
1583 type2 = check_typedef (value_type (val2));
1584
1585 t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
1586 && type1->is_vector ()) ? 1 : 0;
1587 t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
1588 && type2->is_vector ()) ? 1 : 0;
1589
1590 if (!t1_is_vec || !t2_is_vec)
1591 error (_("Vector operations are only supported among vectors"));
1592
1593 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1594 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1595 error (_("Could not determine the vector bounds"));
1596
1597 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1598 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1599 elsize = TYPE_LENGTH (eltype1);
1600
1601 if (eltype1->code () != eltype2->code ()
1602 || elsize != TYPE_LENGTH (eltype2)
1603 || eltype1->is_unsigned () != eltype2->is_unsigned ()
1604 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1605 error (_("Cannot perform operation on vectors with different types"));
1606
1607 value *val = allocate_value (type1);
1608 gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
1609 value *mark = value_mark ();
1610 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1611 {
1612 value *tmp = value_binop (value_subscript (val1, i),
1613 value_subscript (val2, i), op);
1614 copy (value_contents_all (tmp),
1615 val_contents.slice (i * elsize, elsize));
1616 }
1617 value_free_to_mark (mark);
1618
1619 return val;
1620 }
1621
1622 /* Perform a binary operation on two operands. */
1623
1624 struct value *
1625 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1626 {
1627 struct value *val;
1628 struct type *type1 = check_typedef (value_type (arg1));
1629 struct type *type2 = check_typedef (value_type (arg2));
1630 int t1_is_vec = (type1->code () == TYPE_CODE_ARRAY
1631 && type1->is_vector ());
1632 int t2_is_vec = (type2->code () == TYPE_CODE_ARRAY
1633 && type2->is_vector ());
1634
1635 if (!t1_is_vec && !t2_is_vec)
1636 val = scalar_binop (arg1, arg2, op);
1637 else if (t1_is_vec && t2_is_vec)
1638 val = vector_binop (arg1, arg2, op);
1639 else
1640 {
1641 /* Widen the scalar operand to a vector. */
1642 struct value **v = t1_is_vec ? &arg2 : &arg1;
1643 struct type *t = t1_is_vec ? type2 : type1;
1644
1645 if (t->code () != TYPE_CODE_FLT
1646 && t->code () != TYPE_CODE_DECFLOAT
1647 && !is_integral_type (t))
1648 error (_("Argument to operation not a number or boolean."));
1649
1650 /* Replicate the scalar value to make a vector value. */
1651 *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1652
1653 val = vector_binop (arg1, arg2, op);
1654 }
1655
1656 return val;
1657 }
1658 \f
1659 /* See value.h. */
1660
1661 bool
1662 value_logical_not (struct value *arg1)
1663 {
1664 int len;
1665 const gdb_byte *p;
1666 struct type *type1;
1667
1668 arg1 = coerce_array (arg1);
1669 type1 = check_typedef (value_type (arg1));
1670
1671 if (is_floating_value (arg1))
1672 return target_float_is_zero (value_contents (arg1).data (), type1);
1673
1674 len = TYPE_LENGTH (type1);
1675 p = value_contents (arg1).data ();
1676
1677 while (--len >= 0)
1678 {
1679 if (*p++)
1680 break;
1681 }
1682
1683 return len < 0;
1684 }
1685
1686 /* Perform a comparison on two string values (whose content are not
1687 necessarily null terminated) based on their length. */
1688
1689 static int
1690 value_strcmp (struct value *arg1, struct value *arg2)
1691 {
1692 int len1 = TYPE_LENGTH (value_type (arg1));
1693 int len2 = TYPE_LENGTH (value_type (arg2));
1694 const gdb_byte *s1 = value_contents (arg1).data ();
1695 const gdb_byte *s2 = value_contents (arg2).data ();
1696 int i, len = len1 < len2 ? len1 : len2;
1697
1698 for (i = 0; i < len; i++)
1699 {
1700 if (s1[i] < s2[i])
1701 return -1;
1702 else if (s1[i] > s2[i])
1703 return 1;
1704 else
1705 continue;
1706 }
1707
1708 if (len1 < len2)
1709 return -1;
1710 else if (len1 > len2)
1711 return 1;
1712 else
1713 return 0;
1714 }
1715
1716 /* Simulate the C operator == by returning a 1
1717 iff ARG1 and ARG2 have equal contents. */
1718
1719 int
1720 value_equal (struct value *arg1, struct value *arg2)
1721 {
1722 int len;
1723 const gdb_byte *p1;
1724 const gdb_byte *p2;
1725 struct type *type1, *type2;
1726 enum type_code code1;
1727 enum type_code code2;
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 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1742 BINOP_EQUAL)));
1743 else if ((is_floating_value (arg1) || is_int1)
1744 && (is_floating_value (arg2) || is_int2))
1745 {
1746 struct type *eff_type_v1, *eff_type_v2;
1747 gdb::byte_vector v1, v2;
1748 v1.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1749 v2.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1750
1751 value_args_as_target_float (arg1, arg2,
1752 v1.data (), &eff_type_v1,
1753 v2.data (), &eff_type_v2);
1754
1755 return target_float_compare (v1.data (), eff_type_v1,
1756 v2.data (), eff_type_v2) == 0;
1757 }
1758
1759 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1760 is bigger. */
1761 else if (code1 == TYPE_CODE_PTR && is_int2)
1762 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1763 else if (code2 == TYPE_CODE_PTR && is_int1)
1764 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1765
1766 else if (code1 == code2
1767 && ((len = (int) TYPE_LENGTH (type1))
1768 == (int) TYPE_LENGTH (type2)))
1769 {
1770 p1 = value_contents (arg1).data ();
1771 p2 = value_contents (arg2).data ();
1772 while (--len >= 0)
1773 {
1774 if (*p1++ != *p2++)
1775 break;
1776 }
1777 return len < 0;
1778 }
1779 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1780 {
1781 return value_strcmp (arg1, arg2) == 0;
1782 }
1783 else
1784 error (_("Invalid type combination in equality test."));
1785 }
1786
1787 /* Compare values based on their raw contents. Useful for arrays since
1788 value_equal coerces them to pointers, thus comparing just the address
1789 of the array instead of its contents. */
1790
1791 int
1792 value_equal_contents (struct value *arg1, struct value *arg2)
1793 {
1794 struct type *type1, *type2;
1795
1796 type1 = check_typedef (value_type (arg1));
1797 type2 = check_typedef (value_type (arg2));
1798
1799 return (type1->code () == type2->code ()
1800 && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1801 && memcmp (value_contents (arg1).data (),
1802 value_contents (arg2).data (),
1803 TYPE_LENGTH (type1)) == 0);
1804 }
1805
1806 /* Simulate the C operator < by returning 1
1807 iff ARG1's contents are less than ARG2's. */
1808
1809 int
1810 value_less (struct value *arg1, struct value *arg2)
1811 {
1812 enum type_code code1;
1813 enum type_code code2;
1814 struct type *type1, *type2;
1815 int is_int1, is_int2;
1816
1817 arg1 = coerce_array (arg1);
1818 arg2 = coerce_array (arg2);
1819
1820 type1 = check_typedef (value_type (arg1));
1821 type2 = check_typedef (value_type (arg2));
1822 code1 = type1->code ();
1823 code2 = type2->code ();
1824 is_int1 = is_integral_type (type1);
1825 is_int2 = is_integral_type (type2);
1826
1827 if ((is_int1 && is_int2)
1828 || (is_fixed_point_type (type1) && is_fixed_point_type (type2)))
1829 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1830 BINOP_LESS)));
1831 else if ((is_floating_value (arg1) || is_int1)
1832 && (is_floating_value (arg2) || is_int2))
1833 {
1834 struct type *eff_type_v1, *eff_type_v2;
1835 gdb::byte_vector v1, v2;
1836 v1.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1837 v2.resize (std::max (TYPE_LENGTH (type1), TYPE_LENGTH (type2)));
1838
1839 value_args_as_target_float (arg1, arg2,
1840 v1.data (), &eff_type_v1,
1841 v2.data (), &eff_type_v2);
1842
1843 return target_float_compare (v1.data (), eff_type_v1,
1844 v2.data (), eff_type_v2) == -1;
1845 }
1846 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1847 return value_as_address (arg1) < value_as_address (arg2);
1848
1849 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1850 is bigger. */
1851 else if (code1 == TYPE_CODE_PTR && is_int2)
1852 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1853 else if (code2 == TYPE_CODE_PTR && is_int1)
1854 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1855 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1856 return value_strcmp (arg1, arg2) < 0;
1857 else
1858 {
1859 error (_("Invalid type combination in ordering comparison."));
1860 return 0;
1861 }
1862 }
1863 \f
1864 /* The unary operators +, - and ~. They free the argument ARG1. */
1865
1866 struct value *
1867 value_pos (struct value *arg1)
1868 {
1869 struct type *type;
1870
1871 arg1 = coerce_ref (arg1);
1872 type = check_typedef (value_type (arg1));
1873
1874 if (is_integral_type (type) || is_floating_value (arg1)
1875 || (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1876 || type->code () == TYPE_CODE_COMPLEX)
1877 return value_from_contents (type, value_contents (arg1).data ());
1878 else
1879 error (_("Argument to positive operation not a number."));
1880 }
1881
1882 struct value *
1883 value_neg (struct value *arg1)
1884 {
1885 struct type *type;
1886
1887 arg1 = coerce_ref (arg1);
1888 type = check_typedef (value_type (arg1));
1889
1890 if (is_integral_type (type) || is_floating_type (type))
1891 return value_binop (value_from_longest (type, 0), arg1, BINOP_SUB);
1892 else if (is_fixed_point_type (type))
1893 return value_binop (value_zero (type, not_lval), arg1, BINOP_SUB);
1894 else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1895 {
1896 struct value *val = allocate_value (type);
1897 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1898 int i;
1899 LONGEST low_bound, high_bound;
1900
1901 if (!get_array_bounds (type, &low_bound, &high_bound))
1902 error (_("Could not determine the vector bounds"));
1903
1904 gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
1905 int elt_len = TYPE_LENGTH (eltype);
1906
1907 for (i = 0; i < high_bound - low_bound + 1; i++)
1908 {
1909 value *tmp = value_neg (value_subscript (arg1, i));
1910 copy (value_contents_all (tmp),
1911 val_contents.slice (i * elt_len, elt_len));
1912 }
1913 return val;
1914 }
1915 else if (type->code () == TYPE_CODE_COMPLEX)
1916 {
1917 struct value *real = value_real_part (arg1);
1918 struct value *imag = value_imaginary_part (arg1);
1919
1920 real = value_neg (real);
1921 imag = value_neg (imag);
1922 return value_literal_complex (real, imag, type);
1923 }
1924 else
1925 error (_("Argument to negate operation not a number."));
1926 }
1927
1928 struct value *
1929 value_complement (struct value *arg1)
1930 {
1931 struct type *type;
1932 struct value *val;
1933
1934 arg1 = coerce_ref (arg1);
1935 type = check_typedef (value_type (arg1));
1936
1937 if (is_integral_type (type))
1938 val = value_from_longest (type, ~value_as_long (arg1));
1939 else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
1940 {
1941 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1942 int i;
1943 LONGEST low_bound, high_bound;
1944
1945 if (!get_array_bounds (type, &low_bound, &high_bound))
1946 error (_("Could not determine the vector bounds"));
1947
1948 val = allocate_value (type);
1949 gdb::array_view<gdb_byte> val_contents = value_contents_writeable (val);
1950 int elt_len = TYPE_LENGTH (eltype);
1951
1952 for (i = 0; i < high_bound - low_bound + 1; i++)
1953 {
1954 value *tmp = value_complement (value_subscript (arg1, i));
1955 copy (value_contents_all (tmp),
1956 val_contents.slice (i * elt_len, elt_len));
1957 }
1958 }
1959 else if (type->code () == TYPE_CODE_COMPLEX)
1960 {
1961 /* GCC has an extension that treats ~complex as the complex
1962 conjugate. */
1963 struct value *real = value_real_part (arg1);
1964 struct value *imag = value_imaginary_part (arg1);
1965
1966 imag = value_neg (imag);
1967 return value_literal_complex (real, imag, type);
1968 }
1969 else
1970 error (_("Argument to complement operation not an integer, boolean."));
1971
1972 return val;
1973 }
1974 \f
1975 /* The INDEX'th bit of SET value whose value_type is TYPE,
1976 and whose value_contents is valaddr.
1977 Return -1 if out of range, -2 other error. */
1978
1979 int
1980 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1981 {
1982 struct gdbarch *gdbarch = type->arch ();
1983 LONGEST low_bound, high_bound;
1984 LONGEST word;
1985 unsigned rel_index;
1986 struct type *range = type->index_type ();
1987
1988 if (!get_discrete_bounds (range, &low_bound, &high_bound))
1989 return -2;
1990 if (index < low_bound || index > high_bound)
1991 return -1;
1992 rel_index = index - low_bound;
1993 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1994 type_byte_order (type));
1995 rel_index %= TARGET_CHAR_BIT;
1996 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1997 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1998 return (word >> rel_index) & 1;
1999 }
2000
2001 int
2002 value_in (struct value *element, struct value *set)
2003 {
2004 int member;
2005 struct type *settype = check_typedef (value_type (set));
2006 struct type *eltype = check_typedef (value_type (element));
2007
2008 if (eltype->code () == TYPE_CODE_RANGE)
2009 eltype = TYPE_TARGET_TYPE (eltype);
2010 if (settype->code () != TYPE_CODE_SET)
2011 error (_("Second argument of 'IN' has wrong type"));
2012 if (eltype->code () != TYPE_CODE_INT
2013 && eltype->code () != TYPE_CODE_CHAR
2014 && eltype->code () != TYPE_CODE_ENUM
2015 && eltype->code () != TYPE_CODE_BOOL)
2016 error (_("First argument of 'IN' has wrong type"));
2017 member = value_bit_index (settype, value_contents (set).data (),
2018 value_as_long (element));
2019 if (member < 0)
2020 error (_("First argument of 'IN' not in range"));
2021 return member;
2022 }