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