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