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