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