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