ChangeLog:
[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 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
35 /* Define whether or not the C operator '/' truncates towards zero for
36 differently signed operands (truncation direction is undefined in C). */
37
38 #ifndef TRUNCATION_TOWARDS_ZERO
39 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
40 #endif
41
42 void _initialize_valarith (void);
43 \f
44
45 /* Given a pointer, return the size of its target.
46 If the pointer type is void *, then return 1.
47 If the target type is incomplete, then error out.
48 This isn't a general purpose function, but just a
49 helper for value_ptrsub & value_ptradd.
50 */
51
52 static LONGEST
53 find_size_for_pointer_math (struct type *ptr_type)
54 {
55 LONGEST sz = -1;
56 struct type *ptr_target;
57
58 gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
59 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
60
61 sz = TYPE_LENGTH (ptr_target);
62 if (sz == 0)
63 {
64 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
65 sz = 1;
66 else
67 {
68 char *name;
69
70 name = TYPE_NAME (ptr_target);
71 if (name == NULL)
72 name = TYPE_TAG_NAME (ptr_target);
73 if (name == NULL)
74 error (_("Cannot perform pointer math on incomplete types, "
75 "try casting to a known type, or void *."));
76 else
77 error (_("Cannot perform pointer math on incomplete type \"%s\", "
78 "try casting to a known type, or void *."), name);
79 }
80 }
81 return sz;
82 }
83
84 /* Given a pointer ARG1 and an integral value ARG2, return the
85 result of C-style pointer arithmetic ARG1 + ARG2. */
86
87 struct value *
88 value_ptradd (struct value *arg1, struct value *arg2)
89 {
90 struct type *valptrtype;
91 LONGEST sz;
92
93 arg1 = coerce_array (arg1);
94 valptrtype = check_typedef (value_type (arg1));
95 sz = find_size_for_pointer_math (valptrtype);
96
97 if (!is_integral_type (value_type (arg2)))
98 error (_("Argument to arithmetic operation not a number or boolean."));
99
100 return value_from_pointer (valptrtype,
101 value_as_address (arg1)
102 + (sz * value_as_long (arg2)));
103 }
104
105 /* Given a pointer ARG1 and an integral value ARG2, return the
106 result of C-style pointer arithmetic ARG1 - ARG2. */
107
108 struct value *
109 value_ptrsub (struct value *arg1, struct value *arg2)
110 {
111 struct type *valptrtype;
112 LONGEST sz;
113
114 arg1 = coerce_array (arg1);
115 valptrtype = check_typedef (value_type (arg1));
116 sz = find_size_for_pointer_math (valptrtype);
117
118 if (!is_integral_type (value_type (arg2)))
119 error (_("Argument to arithmetic operation not a number or boolean."));
120
121 return value_from_pointer (valptrtype,
122 value_as_address (arg1)
123 - (sz * value_as_long (arg2)));
124 }
125
126 /* Given two compatible pointer values ARG1 and ARG2, return the
127 result of C-style pointer arithmetic ARG1 - ARG2. */
128
129 LONGEST
130 value_ptrdiff (struct value *arg1, struct value *arg2)
131 {
132 struct type *type1, *type2;
133 LONGEST sz;
134
135 arg1 = coerce_array (arg1);
136 arg2 = coerce_array (arg2);
137 type1 = check_typedef (value_type (arg1));
138 type2 = check_typedef (value_type (arg2));
139
140 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
141 gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
142
143 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
144 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
145 error (_("\
146 First argument of `-' is a pointer and second argument is neither\n\
147 an integer nor a pointer of the same type."));
148
149 sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
150 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
151 }
152
153 /* Return the value of ARRAY[IDX].
154
155 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
156 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
157 To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
158
159 See comments in value_coerce_array() for rationale for reason for
160 doing lower bounds adjustment here rather than there.
161 FIXME: Perhaps we should validate that the index is valid and if
162 verbosity is set, warn about invalid indices (but still use them). */
163
164 struct value *
165 value_subscript (struct value *array, struct value *idx)
166 {
167 struct value *bound;
168 int c_style = current_language->c_style_arrays;
169 struct type *tarray;
170
171 array = coerce_ref (array);
172 tarray = check_typedef (value_type (array));
173
174 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
175 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
176 {
177 struct type *range_type = TYPE_INDEX_TYPE (tarray);
178 LONGEST lowerbound, upperbound;
179 get_discrete_bounds (range_type, &lowerbound, &upperbound);
180
181 if (VALUE_LVAL (array) != lval_memory)
182 return value_subscripted_rvalue (array, idx, lowerbound);
183
184 if (c_style == 0)
185 {
186 LONGEST index = value_as_long (idx);
187 if (index >= lowerbound && index <= upperbound)
188 return value_subscripted_rvalue (array, idx, lowerbound);
189 /* Emit warning unless we have an array of unknown size.
190 An array of unknown size has lowerbound 0 and upperbound -1. */
191 if (upperbound > -1)
192 warning (_("array or string index out of range"));
193 /* fall doing C stuff */
194 c_style = 1;
195 }
196
197 if (lowerbound != 0)
198 {
199 bound = value_from_longest (value_type (idx), (LONGEST) lowerbound);
200 idx = value_binop (idx, bound, BINOP_SUB);
201 }
202
203 array = value_coerce_array (array);
204 }
205
206 if (c_style)
207 return value_ind (value_ptradd (array, idx));
208 else
209 error (_("not an array or string"));
210 }
211
212 /* Return the value of EXPR[IDX], expr an aggregate rvalue
213 (eg, a vector register). This routine used to promote floats
214 to doubles, but no longer does. */
215
216 struct value *
217 value_subscripted_rvalue (struct value *array, struct value *idx, int lowerbound)
218 {
219 struct type *array_type = check_typedef (value_type (array));
220 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
221 unsigned int elt_size = TYPE_LENGTH (elt_type);
222 LONGEST index = value_as_long (idx);
223 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
224 struct value *v;
225
226 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
227 error (_("no such vector element"));
228
229 v = allocate_value (elt_type);
230 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
231 set_value_lazy (v, 1);
232 else
233 memcpy (value_contents_writeable (v),
234 value_contents (array) + elt_offs, elt_size);
235
236 set_value_component_location (v, array);
237 VALUE_REGNUM (v) = VALUE_REGNUM (array);
238 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
239 set_value_offset (v, value_offset (array) + elt_offs);
240 return v;
241 }
242
243 /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */
244
245 struct value *
246 value_bitstring_subscript (struct type *type,
247 struct value *bitstring, struct value *idx)
248 {
249
250 struct type *bitstring_type, *range_type;
251 LONGEST index = value_as_long (idx);
252 struct value *v;
253 int offset, byte, bit_index;
254 LONGEST lowerbound, upperbound;
255
256 bitstring_type = check_typedef (value_type (bitstring));
257 gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
258
259 range_type = TYPE_INDEX_TYPE (bitstring_type);
260 get_discrete_bounds (range_type, &lowerbound, &upperbound);
261 if (index < lowerbound || index > upperbound)
262 error (_("bitstring index out of range"));
263
264 index -= lowerbound;
265 offset = index / TARGET_CHAR_BIT;
266 byte = *((char *) value_contents (bitstring) + offset);
267
268 bit_index = index % TARGET_CHAR_BIT;
269 byte >>= (gdbarch_bits_big_endian (current_gdbarch) ?
270 TARGET_CHAR_BIT - 1 - bit_index : bit_index);
271
272 v = value_from_longest (type, byte & 1);
273
274 set_value_bitpos (v, bit_index);
275 set_value_bitsize (v, 1);
276 set_value_component_location (v, bitstring);
277 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
278
279 set_value_offset (v, offset + value_offset (bitstring));
280
281 return v;
282 }
283
284 \f
285 /* Check to see if either argument is a structure, or a reference to
286 one. This is called so we know whether to go ahead with the normal
287 binop or look for a user defined function instead.
288
289 For now, we do not overload the `=' operator. */
290
291 int
292 binop_user_defined_p (enum exp_opcode op, struct value *arg1, struct value *arg2)
293 {
294 struct type *type1, *type2;
295 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
296 return 0;
297
298 type1 = check_typedef (value_type (arg1));
299 if (TYPE_CODE (type1) == TYPE_CODE_REF)
300 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
301
302 type2 = check_typedef (value_type (arg2));
303 if (TYPE_CODE (type2) == TYPE_CODE_REF)
304 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
305
306 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
307 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
308 }
309
310 /* Check to see if argument is a structure. This is called so
311 we know whether to go ahead with the normal unop or look for a
312 user defined function instead.
313
314 For now, we do not overload the `&' operator. */
315
316 int
317 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
318 {
319 struct type *type1;
320 if (op == UNOP_ADDR)
321 return 0;
322 type1 = check_typedef (value_type (arg1));
323 for (;;)
324 {
325 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
326 return 1;
327 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
328 type1 = TYPE_TARGET_TYPE (type1);
329 else
330 return 0;
331 }
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_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
475
476 if (argvec[0])
477 {
478 if (static_memfuncp)
479 {
480 argvec[1] = argvec[0];
481 argvec++;
482 }
483 if (noside == EVAL_AVOID_SIDE_EFFECTS)
484 {
485 struct type *return_type;
486 return_type
487 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
488 return value_zero (return_type, VALUE_LVAL (arg1));
489 }
490 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
491 }
492 error (_("member function %s not found"), tstr);
493 #ifdef lint
494 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
495 #endif
496 }
497
498 /* We know that arg1 is a structure, so try to find a unary user
499 defined operator that matches the operator in question.
500 Create an argument vector that calls arg1.operator @ (arg1)
501 and return that value (where '@' is (almost) any unary operator which
502 is legal for GNU C++). */
503
504 struct value *
505 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
506 {
507 struct value **argvec;
508 char *ptr, *mangle_ptr;
509 char tstr[13], mangle_tstr[13];
510 int static_memfuncp, nargs;
511
512 arg1 = coerce_ref (arg1);
513
514 /* now we know that what we have to do is construct our
515 arg vector and find the right function to call it with. */
516
517 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
518 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
519
520 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
521 argvec[1] = value_addr (arg1);
522 argvec[2] = 0;
523
524 nargs = 1;
525
526 /* make the right function name up */
527 strcpy (tstr, "operator__");
528 ptr = tstr + 8;
529 strcpy (mangle_tstr, "__");
530 mangle_ptr = mangle_tstr + 2;
531 switch (op)
532 {
533 case UNOP_PREINCREMENT:
534 strcpy (ptr, "++");
535 break;
536 case UNOP_PREDECREMENT:
537 strcpy (ptr, "--");
538 break;
539 case UNOP_POSTINCREMENT:
540 strcpy (ptr, "++");
541 argvec[2] = value_from_longest (builtin_type_int8, 0);
542 argvec[3] = 0;
543 nargs ++;
544 break;
545 case UNOP_POSTDECREMENT:
546 strcpy (ptr, "--");
547 argvec[2] = value_from_longest (builtin_type_int8, 0);
548 argvec[3] = 0;
549 nargs ++;
550 break;
551 case UNOP_LOGICAL_NOT:
552 strcpy (ptr, "!");
553 break;
554 case UNOP_COMPLEMENT:
555 strcpy (ptr, "~");
556 break;
557 case UNOP_NEG:
558 strcpy (ptr, "-");
559 break;
560 case UNOP_PLUS:
561 strcpy (ptr, "+");
562 break;
563 case UNOP_IND:
564 strcpy (ptr, "*");
565 break;
566 default:
567 error (_("Invalid unary operation specified."));
568 }
569
570 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
571
572 if (argvec[0])
573 {
574 if (static_memfuncp)
575 {
576 argvec[1] = argvec[0];
577 nargs --;
578 argvec++;
579 }
580 if (noside == EVAL_AVOID_SIDE_EFFECTS)
581 {
582 struct type *return_type;
583 return_type
584 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
585 return value_zero (return_type, VALUE_LVAL (arg1));
586 }
587 return call_function_by_hand (argvec[0], nargs, argvec + 1);
588 }
589 error (_("member function %s not found"), tstr);
590 return 0; /* For lint -- never reached */
591 }
592 \f
593
594 /* Concatenate two values with the following conditions:
595
596 (1) Both values must be either bitstring values or character string
597 values and the resulting value consists of the concatenation of
598 ARG1 followed by ARG2.
599
600 or
601
602 One value must be an integer value and the other value must be
603 either a bitstring value or character string value, which is
604 to be repeated by the number of times specified by the integer
605 value.
606
607
608 (2) Boolean values are also allowed and are treated as bit string
609 values of length 1.
610
611 (3) Character values are also allowed and are treated as character
612 string values of length 1.
613 */
614
615 struct value *
616 value_concat (struct value *arg1, struct value *arg2)
617 {
618 struct value *inval1;
619 struct value *inval2;
620 struct value *outval = NULL;
621 int inval1len, inval2len;
622 int count, idx;
623 char *ptr;
624 char inchar;
625 struct type *type1 = check_typedef (value_type (arg1));
626 struct type *type2 = check_typedef (value_type (arg2));
627
628 /* First figure out if we are dealing with two values to be concatenated
629 or a repeat count and a value to be repeated. INVAL1 is set to the
630 first of two concatenated values, or the repeat count. INVAL2 is set
631 to the second of the two concatenated values or the value to be
632 repeated. */
633
634 if (TYPE_CODE (type2) == TYPE_CODE_INT)
635 {
636 struct type *tmp = type1;
637 type1 = tmp;
638 tmp = type2;
639 inval1 = arg2;
640 inval2 = arg1;
641 }
642 else
643 {
644 inval1 = arg1;
645 inval2 = arg2;
646 }
647
648 /* Now process the input values. */
649
650 if (TYPE_CODE (type1) == TYPE_CODE_INT)
651 {
652 /* We have a repeat count. Validate the second value and then
653 construct a value repeated that many times. */
654 if (TYPE_CODE (type2) == TYPE_CODE_STRING
655 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
656 {
657 count = longest_to_int (value_as_long (inval1));
658 inval2len = TYPE_LENGTH (type2);
659 ptr = (char *) alloca (count * inval2len);
660 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
661 {
662 inchar = (char) unpack_long (type2,
663 value_contents (inval2));
664 for (idx = 0; idx < count; idx++)
665 {
666 *(ptr + idx) = inchar;
667 }
668 }
669 else
670 {
671 for (idx = 0; idx < count; idx++)
672 {
673 memcpy (ptr + (idx * inval2len), value_contents (inval2),
674 inval2len);
675 }
676 }
677 outval = value_string (ptr, count * inval2len);
678 }
679 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
680 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
681 {
682 error (_("unimplemented support for bitstring/boolean repeats"));
683 }
684 else
685 {
686 error (_("can't repeat values of that type"));
687 }
688 }
689 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
690 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
691 {
692 /* We have two character strings to concatenate. */
693 if (TYPE_CODE (type2) != TYPE_CODE_STRING
694 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
695 {
696 error (_("Strings can only be concatenated with other strings."));
697 }
698 inval1len = TYPE_LENGTH (type1);
699 inval2len = TYPE_LENGTH (type2);
700 ptr = (char *) alloca (inval1len + inval2len);
701 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
702 {
703 *ptr = (char) unpack_long (type1, value_contents (inval1));
704 }
705 else
706 {
707 memcpy (ptr, value_contents (inval1), inval1len);
708 }
709 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
710 {
711 *(ptr + inval1len) =
712 (char) unpack_long (type2, value_contents (inval2));
713 }
714 else
715 {
716 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
717 }
718 outval = value_string (ptr, inval1len + inval2len);
719 }
720 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
721 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
722 {
723 /* We have two bitstrings to concatenate. */
724 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
725 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
726 {
727 error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."));
728 }
729 error (_("unimplemented support for bitstring/boolean concatenation."));
730 }
731 else
732 {
733 /* We don't know how to concatenate these operands. */
734 error (_("illegal operands for concatenation."));
735 }
736 return (outval);
737 }
738 \f
739 /* Integer exponentiation: V1**V2, where both arguments are
740 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
741 static LONGEST
742 integer_pow (LONGEST v1, LONGEST v2)
743 {
744 if (v2 < 0)
745 {
746 if (v1 == 0)
747 error (_("Attempt to raise 0 to negative power."));
748 else
749 return 0;
750 }
751 else
752 {
753 /* The Russian Peasant's Algorithm */
754 LONGEST v;
755
756 v = 1;
757 for (;;)
758 {
759 if (v2 & 1L)
760 v *= v1;
761 v2 >>= 1;
762 if (v2 == 0)
763 return v;
764 v1 *= v1;
765 }
766 }
767 }
768
769 /* Integer exponentiation: V1**V2, where both arguments are
770 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
771 static ULONGEST
772 uinteger_pow (ULONGEST v1, LONGEST v2)
773 {
774 if (v2 < 0)
775 {
776 if (v1 == 0)
777 error (_("Attempt to raise 0 to negative power."));
778 else
779 return 0;
780 }
781 else
782 {
783 /* The Russian Peasant's Algorithm */
784 ULONGEST v;
785
786 v = 1;
787 for (;;)
788 {
789 if (v2 & 1L)
790 v *= v1;
791 v2 >>= 1;
792 if (v2 == 0)
793 return v;
794 v1 *= v1;
795 }
796 }
797 }
798
799 /* Obtain decimal value of arguments for binary operation, converting from
800 other types if one of them is not decimal floating point. */
801 static void
802 value_args_as_decimal (struct value *arg1, struct value *arg2,
803 gdb_byte *x, int *len_x, gdb_byte *y, int *len_y)
804 {
805 struct type *type1, *type2;
806
807 type1 = check_typedef (value_type (arg1));
808 type2 = check_typedef (value_type (arg2));
809
810 /* At least one of the arguments must be of decimal float type. */
811 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
812 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
813
814 if (TYPE_CODE (type1) == TYPE_CODE_FLT
815 || TYPE_CODE (type2) == TYPE_CODE_FLT)
816 /* The DFP extension to the C language does not allow mixing of
817 * decimal float types with other float types in expressions
818 * (see WDTR 24732, page 12). */
819 error (_("Mixing decimal floating types with other floating types is not allowed."));
820
821 /* Obtain decimal value of arg1, converting from other types
822 if necessary. */
823
824 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
825 {
826 *len_x = TYPE_LENGTH (type1);
827 memcpy (x, value_contents (arg1), *len_x);
828 }
829 else if (is_integral_type (type1))
830 {
831 *len_x = TYPE_LENGTH (type2);
832 decimal_from_integral (arg1, x, *len_x);
833 }
834 else
835 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
836 TYPE_NAME (type2));
837
838 /* Obtain decimal value of arg2, converting from other types
839 if necessary. */
840
841 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
842 {
843 *len_y = TYPE_LENGTH (type2);
844 memcpy (y, value_contents (arg2), *len_y);
845 }
846 else if (is_integral_type (type2))
847 {
848 *len_y = TYPE_LENGTH (type1);
849 decimal_from_integral (arg2, y, *len_y);
850 }
851 else
852 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
853 TYPE_NAME (type2));
854 }
855
856 /* Perform a binary operation on two operands which have reasonable
857 representations as integers or floats. This includes booleans,
858 characters, integers, or floats.
859 Does not support addition and subtraction on pointers;
860 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
861
862 struct value *
863 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
864 {
865 struct value *val;
866 struct type *type1, *type2, *result_type;
867
868 arg1 = coerce_ref (arg1);
869 arg2 = coerce_ref (arg2);
870
871 type1 = check_typedef (value_type (arg1));
872 type2 = check_typedef (value_type (arg2));
873
874 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
875 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
876 && !is_integral_type (type1))
877 || (TYPE_CODE (type2) != TYPE_CODE_FLT
878 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
879 && !is_integral_type (type2)))
880 error (_("Argument to arithmetic operation not a number or boolean."));
881
882 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
883 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
884 {
885 struct type *v_type;
886 int len_v1, len_v2, len_v;
887 gdb_byte v1[16], v2[16];
888 gdb_byte v[16];
889
890 /* If only one type is decimal float, use its type.
891 Otherwise use the bigger type. */
892 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
893 result_type = type2;
894 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
895 result_type = type1;
896 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
897 result_type = type2;
898 else
899 result_type = type1;
900
901 len_v = TYPE_LENGTH (result_type);
902
903 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
904
905 switch (op)
906 {
907 case BINOP_ADD:
908 case BINOP_SUB:
909 case BINOP_MUL:
910 case BINOP_DIV:
911 case BINOP_EXP:
912 decimal_binop (op, v1, len_v1, v2, len_v2, v, len_v);
913 break;
914
915 default:
916 error (_("Operation not valid for decimal floating point number."));
917 }
918
919 val = value_from_decfloat (result_type, v);
920 }
921 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
922 || TYPE_CODE (type2) == TYPE_CODE_FLT)
923 {
924 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
925 in target format. real.c in GCC probably has the necessary
926 code. */
927 DOUBLEST v1, v2, v = 0;
928 v1 = value_as_double (arg1);
929 v2 = value_as_double (arg2);
930
931 switch (op)
932 {
933 case BINOP_ADD:
934 v = v1 + v2;
935 break;
936
937 case BINOP_SUB:
938 v = v1 - v2;
939 break;
940
941 case BINOP_MUL:
942 v = v1 * v2;
943 break;
944
945 case BINOP_DIV:
946 v = v1 / v2;
947 break;
948
949 case BINOP_EXP:
950 errno = 0;
951 v = pow (v1, v2);
952 if (errno)
953 error (_("Cannot perform exponentiation: %s"), safe_strerror (errno));
954 break;
955
956 case BINOP_MIN:
957 v = v1 < v2 ? v1 : v2;
958 break;
959
960 case BINOP_MAX:
961 v = v1 > v2 ? v1 : v2;
962 break;
963
964 default:
965 error (_("Integer-only operation on floating point number."));
966 }
967
968 /* If only one type is float, use its type.
969 Otherwise use the bigger type. */
970 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
971 result_type = type2;
972 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
973 result_type = type1;
974 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
975 result_type = type2;
976 else
977 result_type = type1;
978
979 val = allocate_value (result_type);
980 store_typed_floating (value_contents_raw (val), value_type (val), v);
981 }
982 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
983 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
984 {
985 LONGEST v1, v2, v = 0;
986 v1 = value_as_long (arg1);
987 v2 = value_as_long (arg2);
988
989 switch (op)
990 {
991 case BINOP_BITWISE_AND:
992 v = v1 & v2;
993 break;
994
995 case BINOP_BITWISE_IOR:
996 v = v1 | v2;
997 break;
998
999 case BINOP_BITWISE_XOR:
1000 v = v1 ^ v2;
1001 break;
1002
1003 case BINOP_EQUAL:
1004 v = v1 == v2;
1005 break;
1006
1007 case BINOP_NOTEQUAL:
1008 v = v1 != v2;
1009 break;
1010
1011 default:
1012 error (_("Invalid operation on booleans."));
1013 }
1014
1015 result_type = type1;
1016
1017 val = allocate_value (result_type);
1018 store_signed_integer (value_contents_raw (val),
1019 TYPE_LENGTH (result_type),
1020 v);
1021 }
1022 else
1023 /* Integral operations here. */
1024 {
1025 /* Determine type length of the result, and if the operation should
1026 be done unsigned. For exponentiation and shift operators,
1027 use the length and type of the left operand. Otherwise,
1028 use the signedness of the operand with the greater length.
1029 If both operands are of equal length, use unsigned operation
1030 if one of the operands is unsigned. */
1031 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1032 result_type = type1;
1033 else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1034 result_type = type1;
1035 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1036 result_type = type2;
1037 else if (TYPE_UNSIGNED (type1))
1038 result_type = type1;
1039 else if (TYPE_UNSIGNED (type2))
1040 result_type = type2;
1041 else
1042 result_type = type1;
1043
1044 if (TYPE_UNSIGNED (result_type))
1045 {
1046 LONGEST v2_signed = value_as_long (arg2);
1047 ULONGEST v1, v2, v = 0;
1048 v1 = (ULONGEST) value_as_long (arg1);
1049 v2 = (ULONGEST) v2_signed;
1050
1051 switch (op)
1052 {
1053 case BINOP_ADD:
1054 v = v1 + v2;
1055 break;
1056
1057 case BINOP_SUB:
1058 v = v1 - v2;
1059 break;
1060
1061 case BINOP_MUL:
1062 v = v1 * v2;
1063 break;
1064
1065 case BINOP_DIV:
1066 case BINOP_INTDIV:
1067 if (v2 != 0)
1068 v = v1 / v2;
1069 else
1070 error (_("Division by zero"));
1071 break;
1072
1073 case BINOP_EXP:
1074 v = uinteger_pow (v1, v2_signed);
1075 break;
1076
1077 case BINOP_REM:
1078 if (v2 != 0)
1079 v = v1 % v2;
1080 else
1081 error (_("Division by zero"));
1082 break;
1083
1084 case BINOP_MOD:
1085 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1086 v1 mod 0 has a defined value, v1. */
1087 if (v2 == 0)
1088 {
1089 v = v1;
1090 }
1091 else
1092 {
1093 v = v1 / v2;
1094 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1095 v = v1 - (v2 * v);
1096 }
1097 break;
1098
1099 case BINOP_LSH:
1100 v = v1 << v2;
1101 break;
1102
1103 case BINOP_RSH:
1104 v = v1 >> v2;
1105 break;
1106
1107 case BINOP_BITWISE_AND:
1108 v = v1 & v2;
1109 break;
1110
1111 case BINOP_BITWISE_IOR:
1112 v = v1 | v2;
1113 break;
1114
1115 case BINOP_BITWISE_XOR:
1116 v = v1 ^ v2;
1117 break;
1118
1119 case BINOP_LOGICAL_AND:
1120 v = v1 && v2;
1121 break;
1122
1123 case BINOP_LOGICAL_OR:
1124 v = v1 || v2;
1125 break;
1126
1127 case BINOP_MIN:
1128 v = v1 < v2 ? v1 : v2;
1129 break;
1130
1131 case BINOP_MAX:
1132 v = v1 > v2 ? v1 : v2;
1133 break;
1134
1135 case BINOP_EQUAL:
1136 v = v1 == v2;
1137 break;
1138
1139 case BINOP_NOTEQUAL:
1140 v = v1 != v2;
1141 break;
1142
1143 case BINOP_LESS:
1144 v = v1 < v2;
1145 break;
1146
1147 default:
1148 error (_("Invalid binary operation on numbers."));
1149 }
1150
1151 val = allocate_value (result_type);
1152 store_unsigned_integer (value_contents_raw (val),
1153 TYPE_LENGTH (value_type (val)),
1154 v);
1155 }
1156 else
1157 {
1158 LONGEST v1, v2, v = 0;
1159 v1 = value_as_long (arg1);
1160 v2 = value_as_long (arg2);
1161
1162 switch (op)
1163 {
1164 case BINOP_ADD:
1165 v = v1 + v2;
1166 break;
1167
1168 case BINOP_SUB:
1169 v = v1 - v2;
1170 break;
1171
1172 case BINOP_MUL:
1173 v = v1 * v2;
1174 break;
1175
1176 case BINOP_DIV:
1177 case BINOP_INTDIV:
1178 if (v2 != 0)
1179 v = v1 / v2;
1180 else
1181 error (_("Division by zero"));
1182 break;
1183
1184 case BINOP_EXP:
1185 v = integer_pow (v1, v2);
1186 break;
1187
1188 case BINOP_REM:
1189 if (v2 != 0)
1190 v = v1 % v2;
1191 else
1192 error (_("Division by zero"));
1193 break;
1194
1195 case BINOP_MOD:
1196 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1197 X mod 0 has a defined value, X. */
1198 if (v2 == 0)
1199 {
1200 v = v1;
1201 }
1202 else
1203 {
1204 v = v1 / v2;
1205 /* Compute floor. */
1206 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1207 {
1208 v--;
1209 }
1210 v = v1 - (v2 * v);
1211 }
1212 break;
1213
1214 case BINOP_LSH:
1215 v = v1 << v2;
1216 break;
1217
1218 case BINOP_RSH:
1219 v = v1 >> v2;
1220 break;
1221
1222 case BINOP_BITWISE_AND:
1223 v = v1 & v2;
1224 break;
1225
1226 case BINOP_BITWISE_IOR:
1227 v = v1 | v2;
1228 break;
1229
1230 case BINOP_BITWISE_XOR:
1231 v = v1 ^ v2;
1232 break;
1233
1234 case BINOP_LOGICAL_AND:
1235 v = v1 && v2;
1236 break;
1237
1238 case BINOP_LOGICAL_OR:
1239 v = v1 || v2;
1240 break;
1241
1242 case BINOP_MIN:
1243 v = v1 < v2 ? v1 : v2;
1244 break;
1245
1246 case BINOP_MAX:
1247 v = v1 > v2 ? v1 : v2;
1248 break;
1249
1250 case BINOP_EQUAL:
1251 v = v1 == v2;
1252 break;
1253
1254 case BINOP_LESS:
1255 v = v1 < v2;
1256 break;
1257
1258 default:
1259 error (_("Invalid binary operation on numbers."));
1260 }
1261
1262 val = allocate_value (result_type);
1263 store_signed_integer (value_contents_raw (val),
1264 TYPE_LENGTH (value_type (val)),
1265 v);
1266 }
1267 }
1268
1269 return val;
1270 }
1271 \f
1272 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1273
1274 int
1275 value_logical_not (struct value *arg1)
1276 {
1277 int len;
1278 const gdb_byte *p;
1279 struct type *type1;
1280
1281 arg1 = coerce_array (arg1);
1282 type1 = check_typedef (value_type (arg1));
1283
1284 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1285 return 0 == value_as_double (arg1);
1286 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1287 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1));
1288
1289 len = TYPE_LENGTH (type1);
1290 p = value_contents (arg1);
1291
1292 while (--len >= 0)
1293 {
1294 if (*p++)
1295 break;
1296 }
1297
1298 return len < 0;
1299 }
1300
1301 /* Perform a comparison on two string values (whose content are not
1302 necessarily null terminated) based on their length */
1303
1304 static int
1305 value_strcmp (struct value *arg1, struct value *arg2)
1306 {
1307 int len1 = TYPE_LENGTH (value_type (arg1));
1308 int len2 = TYPE_LENGTH (value_type (arg2));
1309 const gdb_byte *s1 = value_contents (arg1);
1310 const gdb_byte *s2 = value_contents (arg2);
1311 int i, len = len1 < len2 ? len1 : len2;
1312
1313 for (i = 0; i < len; i++)
1314 {
1315 if (s1[i] < s2[i])
1316 return -1;
1317 else if (s1[i] > s2[i])
1318 return 1;
1319 else
1320 continue;
1321 }
1322
1323 if (len1 < len2)
1324 return -1;
1325 else if (len1 > len2)
1326 return 1;
1327 else
1328 return 0;
1329 }
1330
1331 /* Simulate the C operator == by returning a 1
1332 iff ARG1 and ARG2 have equal contents. */
1333
1334 int
1335 value_equal (struct value *arg1, struct value *arg2)
1336 {
1337 int len;
1338 const gdb_byte *p1;
1339 const gdb_byte *p2;
1340 struct type *type1, *type2;
1341 enum type_code code1;
1342 enum type_code code2;
1343 int is_int1, is_int2;
1344
1345 arg1 = coerce_array (arg1);
1346 arg2 = coerce_array (arg2);
1347
1348 type1 = check_typedef (value_type (arg1));
1349 type2 = check_typedef (value_type (arg2));
1350 code1 = TYPE_CODE (type1);
1351 code2 = TYPE_CODE (type2);
1352 is_int1 = is_integral_type (type1);
1353 is_int2 = is_integral_type (type2);
1354
1355 if (is_int1 && is_int2)
1356 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1357 BINOP_EQUAL)));
1358 else if ((code1 == TYPE_CODE_FLT || is_int1)
1359 && (code2 == TYPE_CODE_FLT || is_int2))
1360 {
1361 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1362 `long double' values are returned in static storage (m68k). */
1363 DOUBLEST d = value_as_double (arg1);
1364 return d == value_as_double (arg2);
1365 }
1366 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1367 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1368 {
1369 gdb_byte v1[16], v2[16];
1370 int len_v1, len_v2;
1371
1372 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1373
1374 return decimal_compare (v1, len_v1, v2, len_v2) == 0;
1375 }
1376
1377 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1378 is bigger. */
1379 else if (code1 == TYPE_CODE_PTR && is_int2)
1380 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1381 else if (code2 == TYPE_CODE_PTR && is_int1)
1382 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1383
1384 else if (code1 == code2
1385 && ((len = (int) TYPE_LENGTH (type1))
1386 == (int) TYPE_LENGTH (type2)))
1387 {
1388 p1 = value_contents (arg1);
1389 p2 = value_contents (arg2);
1390 while (--len >= 0)
1391 {
1392 if (*p1++ != *p2++)
1393 break;
1394 }
1395 return len < 0;
1396 }
1397 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1398 {
1399 return value_strcmp (arg1, arg2) == 0;
1400 }
1401 else
1402 {
1403 error (_("Invalid type combination in equality test."));
1404 return 0; /* For lint -- never reached */
1405 }
1406 }
1407
1408 /* Simulate the C operator < by returning 1
1409 iff ARG1's contents are less than ARG2's. */
1410
1411 int
1412 value_less (struct value *arg1, struct value *arg2)
1413 {
1414 enum type_code code1;
1415 enum type_code code2;
1416 struct type *type1, *type2;
1417 int is_int1, is_int2;
1418
1419 arg1 = coerce_array (arg1);
1420 arg2 = coerce_array (arg2);
1421
1422 type1 = check_typedef (value_type (arg1));
1423 type2 = check_typedef (value_type (arg2));
1424 code1 = TYPE_CODE (type1);
1425 code2 = TYPE_CODE (type2);
1426 is_int1 = is_integral_type (type1);
1427 is_int2 = is_integral_type (type2);
1428
1429 if (is_int1 && is_int2)
1430 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1431 BINOP_LESS)));
1432 else if ((code1 == TYPE_CODE_FLT || is_int1)
1433 && (code2 == TYPE_CODE_FLT || is_int2))
1434 {
1435 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1436 `long double' values are returned in static storage (m68k). */
1437 DOUBLEST d = value_as_double (arg1);
1438 return d < value_as_double (arg2);
1439 }
1440 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1441 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1442 {
1443 gdb_byte v1[16], v2[16];
1444 int len_v1, len_v2;
1445
1446 value_args_as_decimal (arg1, arg2, v1, &len_v1, v2, &len_v2);
1447
1448 return decimal_compare (v1, len_v1, v2, len_v2) == -1;
1449 }
1450 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1451 return value_as_address (arg1) < value_as_address (arg2);
1452
1453 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1454 is bigger. */
1455 else if (code1 == TYPE_CODE_PTR && is_int2)
1456 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1457 else if (code2 == TYPE_CODE_PTR && is_int1)
1458 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1459 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1460 return value_strcmp (arg1, arg2) < 0;
1461 else
1462 {
1463 error (_("Invalid type combination in ordering comparison."));
1464 return 0;
1465 }
1466 }
1467 \f
1468 /* The unary operators +, - and ~. They free the argument ARG1. */
1469
1470 struct value *
1471 value_pos (struct value *arg1)
1472 {
1473 struct type *type;
1474
1475 arg1 = coerce_ref (arg1);
1476 type = check_typedef (value_type (arg1));
1477
1478 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1479 return value_from_double (type, value_as_double (arg1));
1480 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1481 return value_from_decfloat (type, value_contents (arg1));
1482 else if (is_integral_type (type))
1483 {
1484 return value_from_longest (type, value_as_long (arg1));
1485 }
1486 else
1487 {
1488 error ("Argument to positive operation not a number.");
1489 return 0; /* For lint -- never reached */
1490 }
1491 }
1492
1493 struct value *
1494 value_neg (struct value *arg1)
1495 {
1496 struct type *type;
1497
1498 arg1 = coerce_ref (arg1);
1499 type = check_typedef (value_type (arg1));
1500
1501 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1502 {
1503 struct value *val = allocate_value (type);
1504 int len = TYPE_LENGTH (type);
1505 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */
1506
1507 memcpy (decbytes, value_contents (arg1), len);
1508
1509 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
1510 decbytes[len-1] = decbytes[len - 1] | 0x80;
1511 else
1512 decbytes[0] = decbytes[0] | 0x80;
1513
1514 memcpy (value_contents_raw (val), decbytes, len);
1515 return val;
1516 }
1517 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1518 return value_from_double (type, -value_as_double (arg1));
1519 else if (is_integral_type (type))
1520 {
1521 return value_from_longest (type, -value_as_long (arg1));
1522 }
1523 else
1524 {
1525 error (_("Argument to negate operation not a number."));
1526 return 0; /* For lint -- never reached */
1527 }
1528 }
1529
1530 struct value *
1531 value_complement (struct value *arg1)
1532 {
1533 struct type *type;
1534
1535 arg1 = coerce_ref (arg1);
1536 type = check_typedef (value_type (arg1));
1537
1538 if (!is_integral_type (type))
1539 error (_("Argument to complement operation not an integer or boolean."));
1540
1541 return value_from_longest (type, ~value_as_long (arg1));
1542 }
1543 \f
1544 /* The INDEX'th bit of SET value whose value_type is TYPE,
1545 and whose value_contents is valaddr.
1546 Return -1 if out of range, -2 other error. */
1547
1548 int
1549 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1550 {
1551 LONGEST low_bound, high_bound;
1552 LONGEST word;
1553 unsigned rel_index;
1554 struct type *range = TYPE_INDEX_TYPE (type);
1555 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1556 return -2;
1557 if (index < low_bound || index > high_bound)
1558 return -1;
1559 rel_index = index - low_bound;
1560 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1);
1561 rel_index %= TARGET_CHAR_BIT;
1562 if (gdbarch_bits_big_endian (current_gdbarch))
1563 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1564 return (word >> rel_index) & 1;
1565 }
1566
1567 int
1568 value_in (struct value *element, struct value *set)
1569 {
1570 int member;
1571 struct type *settype = check_typedef (value_type (set));
1572 struct type *eltype = check_typedef (value_type (element));
1573 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1574 eltype = TYPE_TARGET_TYPE (eltype);
1575 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1576 error (_("Second argument of 'IN' has wrong type"));
1577 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1578 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1579 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1580 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1581 error (_("First argument of 'IN' has wrong type"));
1582 member = value_bit_index (settype, value_contents (set),
1583 value_as_long (element));
1584 if (member < 0)
1585 error (_("First argument of 'IN' not in range"));
1586 return member;
1587 }
1588
1589 void
1590 _initialize_valarith (void)
1591 {
1592 }
This page took 0.076781 seconds and 4 git commands to generate.