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