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