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