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