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