1 /* Perform arithmetic and other operations on values, for GDB.
3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "expression.h"
32 #include "exceptions.h"
34 /* Define whether or not the C operator '/' truncates towards zero for
35 differently signed operands (truncation direction is undefined in C). */
37 #ifndef TRUNCATION_TOWARDS_ZERO
38 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
41 void _initialize_valarith (void);
44 /* Given a pointer, return the size of its target.
45 If the pointer type is void *, then return 1.
46 If the target type is incomplete, then error out.
47 This isn't a general purpose function, but just a
48 helper for value_ptradd. */
51 find_size_for_pointer_math (struct type *ptr_type)
54 struct type *ptr_target;
56 gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
57 ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
59 sz = TYPE_LENGTH (ptr_target);
62 if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
68 name = TYPE_NAME (ptr_target);
70 name = TYPE_TAG_NAME (ptr_target);
72 error (_("Cannot perform pointer math on incomplete types, "
73 "try casting to a known type, or void *."));
75 error (_("Cannot perform pointer math on incomplete type \"%s\", "
76 "try casting to a known type, or void *."), name);
82 /* Given a pointer ARG1 and an integral value ARG2, return the
83 result of C-style pointer arithmetic ARG1 + ARG2. */
86 value_ptradd (struct value *arg1, LONGEST arg2)
88 struct type *valptrtype;
92 arg1 = coerce_array (arg1);
93 valptrtype = check_typedef (value_type (arg1));
94 sz = find_size_for_pointer_math (valptrtype);
96 result = value_from_pointer (valptrtype,
97 value_as_address (arg1) + sz * arg2);
98 if (VALUE_LVAL (result) != lval_internalvar)
99 set_value_component_location (result, arg1);
103 /* Given two compatible pointer values ARG1 and ARG2, return the
104 result of C-style pointer arithmetic ARG1 - ARG2. */
107 value_ptrdiff (struct value *arg1, struct value *arg2)
109 struct type *type1, *type2;
112 arg1 = coerce_array (arg1);
113 arg2 = coerce_array (arg2);
114 type1 = check_typedef (value_type (arg1));
115 type2 = check_typedef (value_type (arg2));
117 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
118 gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
120 if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
121 != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
122 error (_("First argument of `-' is a pointer and "
123 "second argument is neither\n"
124 "an integer nor a pointer of the same type."));
126 sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
129 warning (_("Type size unknown, assuming 1. "
130 "Try casting to a known type, or void *."));
134 return (value_as_long (arg1) - value_as_long (arg2)) / sz;
137 /* Return the value of ARRAY[IDX].
139 ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the
140 current language supports C-style arrays, it may also be TYPE_CODE_PTR.
142 See comments in value_coerce_array() for rationale for reason for
143 doing lower bounds adjustment here rather than there.
144 FIXME: Perhaps we should validate that the index is valid and if
145 verbosity is set, warn about invalid indices (but still use them). */
148 value_subscript (struct value *array, LONGEST index)
150 int c_style = current_language->c_style_arrays;
153 array = coerce_ref (array);
154 tarray = check_typedef (value_type (array));
156 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
157 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
159 struct type *range_type = TYPE_INDEX_TYPE (tarray);
160 LONGEST lowerbound, upperbound;
162 get_discrete_bounds (range_type, &lowerbound, &upperbound);
163 if (VALUE_LVAL (array) != lval_memory)
164 return value_subscripted_rvalue (array, index, lowerbound);
168 if (index >= lowerbound && index <= upperbound)
169 return value_subscripted_rvalue (array, index, lowerbound);
170 /* Emit warning unless we have an array of unknown size.
171 An array of unknown size has lowerbound 0 and upperbound -1. */
173 warning (_("array or string index out of range"));
174 /* fall doing C stuff */
179 array = value_coerce_array (array);
183 return value_ind (value_ptradd (array, index));
185 error (_("not an array or string"));
188 /* Return the value of EXPR[IDX], expr an aggregate rvalue
189 (eg, a vector register). This routine used to promote floats
190 to doubles, but no longer does. */
193 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
195 struct type *array_type = check_typedef (value_type (array));
196 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
197 unsigned int elt_size = TYPE_LENGTH (elt_type);
198 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
201 if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
202 && elt_offs >= TYPE_LENGTH (array_type)))
203 error (_("no such vector element"));
205 if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
206 v = allocate_value_lazy (elt_type);
209 v = allocate_value (elt_type);
210 value_contents_copy (v, value_embedded_offset (v),
211 array, value_embedded_offset (array) + elt_offs,
215 set_value_component_location (v, array);
216 VALUE_REGNUM (v) = VALUE_REGNUM (array);
217 VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
218 set_value_offset (v, value_offset (array) + elt_offs);
223 /* Check to see if either argument is a structure, or a reference to
224 one. This is called so we know whether to go ahead with the normal
225 binop or look for a user defined function instead.
227 For now, we do not overload the `=' operator. */
230 binop_types_user_defined_p (enum exp_opcode op,
231 struct type *type1, struct type *type2)
233 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
236 type1 = check_typedef (type1);
237 if (TYPE_CODE (type1) == TYPE_CODE_REF)
238 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
240 type2 = check_typedef (type2);
241 if (TYPE_CODE (type2) == TYPE_CODE_REF)
242 type2 = check_typedef (TYPE_TARGET_TYPE (type2));
244 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
245 || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
248 /* Check to see if either argument is a structure, or a reference to
249 one. This is called so we know whether to go ahead with the normal
250 binop or look for a user defined function instead.
252 For now, we do not overload the `=' operator. */
255 binop_user_defined_p (enum exp_opcode op,
256 struct value *arg1, struct value *arg2)
258 return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
261 /* Check to see if argument is a structure. This is called so
262 we know whether to go ahead with the normal unop or look for a
263 user defined function instead.
265 For now, we do not overload the `&' operator. */
268 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
274 type1 = check_typedef (value_type (arg1));
275 if (TYPE_CODE (type1) == TYPE_CODE_REF)
276 type1 = check_typedef (TYPE_TARGET_TYPE (type1));
277 return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
280 /* Try to find an operator named OPERATOR which takes NARGS arguments
281 specified in ARGS. If the operator found is a static member operator
282 *STATIC_MEMFUNP will be set to 1, and otherwise 0.
283 The search if performed through find_overload_match which will handle
284 member operators, non member operators, operators imported implicitly or
285 explicitly, and perform correct overload resolution in all of the above
286 situations or combinations thereof. */
288 static struct value *
289 value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
290 int *static_memfuncp)
293 struct symbol *symp = NULL;
294 struct value *valp = NULL;
296 find_overload_match (args, nargs, operator, BOTH /* could be method */,
298 NULL /* pass NULL symbol since symbol is unknown */,
299 &valp, &symp, static_memfuncp, 0);
306 /* This is a non member function and does not
307 expect a reference as its first argument
308 rather the explicit structure. */
309 args[0] = value_ind (args[0]);
310 return value_of_variable (symp, 0);
313 error (_("Could not find %s."), operator);
316 /* Lookup user defined operator NAME. Return a value representing the
317 function, otherwise return NULL. */
319 static struct value *
320 value_user_defined_op (struct value **argp, struct value **args, char *name,
321 int *static_memfuncp, int nargs)
323 struct value *result = NULL;
325 if (current_language->la_language == language_cplus)
326 result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp);
328 result = value_struct_elt (argp, args, name, static_memfuncp,
334 /* We know either arg1 or arg2 is a structure, so try to find the right
335 user defined function. Create an argument vector that calls
336 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
337 binary operator which is legal for GNU C++).
339 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
340 is the opcode saying how to modify it. Otherwise, OTHEROP is
344 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
345 enum exp_opcode otherop, enum noside noside)
347 struct value **argvec;
352 arg1 = coerce_ref (arg1);
353 arg2 = coerce_ref (arg2);
355 /* now we know that what we have to do is construct our
356 arg vector and find the right function to call it with. */
358 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
359 error (_("Can't do that binary op on that type")); /* FIXME be explicit */
361 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
362 argvec[1] = value_addr (arg1);
366 /* Make the right function name up. */
367 strcpy (tstr, "operator__");
392 case BINOP_BITWISE_AND:
395 case BINOP_BITWISE_IOR:
398 case BINOP_BITWISE_XOR:
401 case BINOP_LOGICAL_AND:
404 case BINOP_LOGICAL_OR:
416 case BINOP_ASSIGN_MODIFY:
434 case BINOP_BITWISE_AND:
437 case BINOP_BITWISE_IOR:
440 case BINOP_BITWISE_XOR:
443 case BINOP_MOD: /* invalid */
445 error (_("Invalid binary operation specified."));
448 case BINOP_SUBSCRIPT:
469 case BINOP_MOD: /* invalid */
471 error (_("Invalid binary operation specified."));
474 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
475 &static_memfuncp, 2);
481 argvec[1] = argvec[0];
484 if (noside == EVAL_AVOID_SIDE_EFFECTS)
486 struct type *return_type;
489 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
490 return value_zero (return_type, VALUE_LVAL (arg1));
493 if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
495 /* Static xmethods are not supported yet. */
496 gdb_assert (static_memfuncp == 0);
497 return call_xmethod (argvec[0], 2, argvec + 1);
500 return call_function_by_hand (argvec[0], 2 - static_memfuncp,
503 throw_error (NOT_FOUND_ERROR,
504 _("member function %s not found"), tstr);
506 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
510 /* We know that arg1 is a structure, so try to find a unary user
511 defined operator that matches the operator in question.
512 Create an argument vector that calls arg1.operator @ (arg1)
513 and return that value (where '@' is (almost) any unary operator which
514 is legal for GNU C++). */
517 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
519 struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
520 struct value **argvec;
522 char tstr[13], mangle_tstr[13];
523 int static_memfuncp, nargs;
525 arg1 = coerce_ref (arg1);
527 /* now we know that what we have to do is construct our
528 arg vector and find the right function to call it with. */
530 if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
531 error (_("Can't do that unary op on that type")); /* FIXME be explicit */
533 argvec = (struct value **) alloca (sizeof (struct value *) * 4);
534 argvec[1] = value_addr (arg1);
539 /* Make the right function name up. */
540 strcpy (tstr, "operator__");
542 strcpy (mangle_tstr, "__");
545 case UNOP_PREINCREMENT:
548 case UNOP_PREDECREMENT:
551 case UNOP_POSTINCREMENT:
553 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
557 case UNOP_POSTDECREMENT:
559 argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
563 case UNOP_LOGICAL_NOT:
566 case UNOP_COMPLEMENT:
582 error (_("Invalid unary operation specified."));
585 argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
586 &static_memfuncp, nargs);
592 argvec[1] = argvec[0];
596 if (noside == EVAL_AVOID_SIDE_EFFECTS)
598 struct type *return_type;
601 = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
602 return value_zero (return_type, VALUE_LVAL (arg1));
604 if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
606 /* Static xmethods are not supported yet. */
607 gdb_assert (static_memfuncp == 0);
608 return call_xmethod (argvec[0], 1, argvec + 1);
611 return call_function_by_hand (argvec[0], nargs, argvec + 1);
613 throw_error (NOT_FOUND_ERROR,
614 _("member function %s not found"), tstr);
616 return 0; /* For lint -- never reached */
620 /* Concatenate two values with the following conditions:
622 (1) Both values must be either bitstring values or character string
623 values and the resulting value consists of the concatenation of
624 ARG1 followed by ARG2.
628 One value must be an integer value and the other value must be
629 either a bitstring value or character string value, which is
630 to be repeated by the number of times specified by the integer
634 (2) Boolean values are also allowed and are treated as bit string
637 (3) Character values are also allowed and are treated as character
638 string values of length 1. */
641 value_concat (struct value *arg1, struct value *arg2)
643 struct value *inval1;
644 struct value *inval2;
645 struct value *outval = NULL;
646 int inval1len, inval2len;
650 struct type *type1 = check_typedef (value_type (arg1));
651 struct type *type2 = check_typedef (value_type (arg2));
652 struct type *char_type;
654 /* First figure out if we are dealing with two values to be concatenated
655 or a repeat count and a value to be repeated. INVAL1 is set to the
656 first of two concatenated values, or the repeat count. INVAL2 is set
657 to the second of the two concatenated values or the value to be
660 if (TYPE_CODE (type2) == TYPE_CODE_INT)
662 struct type *tmp = type1;
675 /* Now process the input values. */
677 if (TYPE_CODE (type1) == TYPE_CODE_INT)
679 /* We have a repeat count. Validate the second value and then
680 construct a value repeated that many times. */
681 if (TYPE_CODE (type2) == TYPE_CODE_STRING
682 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
684 struct cleanup *back_to;
686 count = longest_to_int (value_as_long (inval1));
687 inval2len = TYPE_LENGTH (type2);
688 ptr = (char *) xmalloc (count * inval2len);
689 back_to = make_cleanup (xfree, ptr);
690 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
694 inchar = (char) unpack_long (type2,
695 value_contents (inval2));
696 for (idx = 0; idx < count; idx++)
698 *(ptr + idx) = inchar;
703 char_type = TYPE_TARGET_TYPE (type2);
705 for (idx = 0; idx < count; idx++)
707 memcpy (ptr + (idx * inval2len), value_contents (inval2),
711 outval = value_string (ptr, count * inval2len, char_type);
712 do_cleanups (back_to);
714 else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
716 error (_("unimplemented support for boolean repeats"));
720 error (_("can't repeat values of that type"));
723 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
724 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
726 struct cleanup *back_to;
728 /* We have two character strings to concatenate. */
729 if (TYPE_CODE (type2) != TYPE_CODE_STRING
730 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
732 error (_("Strings can only be concatenated with other strings."));
734 inval1len = TYPE_LENGTH (type1);
735 inval2len = TYPE_LENGTH (type2);
736 ptr = (char *) xmalloc (inval1len + inval2len);
737 back_to = make_cleanup (xfree, ptr);
738 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
742 *ptr = (char) unpack_long (type1, value_contents (inval1));
746 char_type = TYPE_TARGET_TYPE (type1);
748 memcpy (ptr, value_contents (inval1), inval1len);
750 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
753 (char) unpack_long (type2, value_contents (inval2));
757 memcpy (ptr + inval1len, value_contents (inval2), inval2len);
759 outval = value_string (ptr, inval1len + inval2len, char_type);
760 do_cleanups (back_to);
762 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
764 /* We have two bitstrings to concatenate. */
765 if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
767 error (_("Booleans can only be concatenated "
768 "with other bitstrings or booleans."));
770 error (_("unimplemented support for boolean concatenation."));
774 /* We don't know how to concatenate these operands. */
775 error (_("illegal operands for concatenation."));
780 /* Integer exponentiation: V1**V2, where both arguments are
781 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
784 integer_pow (LONGEST v1, LONGEST v2)
789 error (_("Attempt to raise 0 to negative power."));
795 /* The Russian Peasant's Algorithm. */
811 /* Integer exponentiation: V1**V2, where both arguments are
812 integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */
815 uinteger_pow (ULONGEST v1, LONGEST v2)
820 error (_("Attempt to raise 0 to negative power."));
826 /* The Russian Peasant's Algorithm. */
842 /* Obtain decimal value of arguments for binary operation, converting from
843 other types if one of them is not decimal floating point. */
845 value_args_as_decimal (struct value *arg1, struct value *arg2,
846 gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
847 gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
849 struct type *type1, *type2;
851 type1 = check_typedef (value_type (arg1));
852 type2 = check_typedef (value_type (arg2));
854 /* At least one of the arguments must be of decimal float type. */
855 gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
856 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
858 if (TYPE_CODE (type1) == TYPE_CODE_FLT
859 || TYPE_CODE (type2) == TYPE_CODE_FLT)
860 /* The DFP extension to the C language does not allow mixing of
861 * decimal float types with other float types in expressions
862 * (see WDTR 24732, page 12). */
863 error (_("Mixing decimal floating types with "
864 "other floating types is not allowed."));
866 /* Obtain decimal value of arg1, converting from other types
869 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
871 *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
872 *len_x = TYPE_LENGTH (type1);
873 memcpy (x, value_contents (arg1), *len_x);
875 else if (is_integral_type (type1))
877 *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
878 *len_x = TYPE_LENGTH (type2);
879 decimal_from_integral (arg1, x, *len_x, *byte_order_x);
882 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
885 /* Obtain decimal value of arg2, converting from other types
888 if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
890 *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
891 *len_y = TYPE_LENGTH (type2);
892 memcpy (y, value_contents (arg2), *len_y);
894 else if (is_integral_type (type2))
896 *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
897 *len_y = TYPE_LENGTH (type1);
898 decimal_from_integral (arg2, y, *len_y, *byte_order_y);
901 error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
905 /* Perform a binary operation on two operands which have reasonable
906 representations as integers or floats. This includes booleans,
907 characters, integers, or floats.
908 Does not support addition and subtraction on pointers;
909 use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */
911 static struct value *
912 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
915 struct type *type1, *type2, *result_type;
917 arg1 = coerce_ref (arg1);
918 arg2 = coerce_ref (arg2);
920 type1 = check_typedef (value_type (arg1));
921 type2 = check_typedef (value_type (arg2));
923 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
924 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
925 && !is_integral_type (type1))
926 || (TYPE_CODE (type2) != TYPE_CODE_FLT
927 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
928 && !is_integral_type (type2)))
929 error (_("Argument to arithmetic operation not a number or boolean."));
931 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
932 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
934 int len_v1, len_v2, len_v;
935 enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
936 gdb_byte v1[16], v2[16];
939 /* If only one type is decimal float, use its type.
940 Otherwise use the bigger type. */
941 if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
943 else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
945 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
950 len_v = TYPE_LENGTH (result_type);
951 byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
953 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
954 v2, &len_v2, &byte_order_v2);
963 decimal_binop (op, v1, len_v1, byte_order_v1,
964 v2, len_v2, byte_order_v2,
965 v, len_v, byte_order_v);
969 error (_("Operation not valid for decimal floating point number."));
972 val = value_from_decfloat (result_type, v);
974 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
975 || TYPE_CODE (type2) == TYPE_CODE_FLT)
977 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
978 in target format. real.c in GCC probably has the necessary
980 DOUBLEST v1, v2, v = 0;
982 v1 = value_as_double (arg1);
983 v2 = value_as_double (arg2);
1007 error (_("Cannot perform exponentiation: %s"),
1008 safe_strerror (errno));
1012 v = v1 < v2 ? v1 : v2;
1016 v = v1 > v2 ? v1 : v2;
1020 error (_("Integer-only operation on floating point number."));
1023 /* If only one type is float, use its type.
1024 Otherwise use the bigger type. */
1025 if (TYPE_CODE (type1) != TYPE_CODE_FLT)
1026 result_type = type2;
1027 else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
1028 result_type = type1;
1029 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1030 result_type = type2;
1032 result_type = type1;
1034 val = allocate_value (result_type);
1035 store_typed_floating (value_contents_raw (val), value_type (val), v);
1037 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
1038 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
1040 LONGEST v1, v2, v = 0;
1042 v1 = value_as_long (arg1);
1043 v2 = value_as_long (arg2);
1047 case BINOP_BITWISE_AND:
1051 case BINOP_BITWISE_IOR:
1055 case BINOP_BITWISE_XOR:
1063 case BINOP_NOTEQUAL:
1068 error (_("Invalid operation on booleans."));
1071 result_type = type1;
1073 val = allocate_value (result_type);
1074 store_signed_integer (value_contents_raw (val),
1075 TYPE_LENGTH (result_type),
1076 gdbarch_byte_order (get_type_arch (result_type)),
1080 /* Integral operations here. */
1082 /* Determine type length of the result, and if the operation should
1083 be done unsigned. For exponentiation and shift operators,
1084 use the length and type of the left operand. Otherwise,
1085 use the signedness of the operand with the greater length.
1086 If both operands are of equal length, use unsigned operation
1087 if one of the operands is unsigned. */
1088 if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
1089 result_type = type1;
1090 else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
1091 result_type = type1;
1092 else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
1093 result_type = type2;
1094 else if (TYPE_UNSIGNED (type1))
1095 result_type = type1;
1096 else if (TYPE_UNSIGNED (type2))
1097 result_type = type2;
1099 result_type = type1;
1101 if (TYPE_UNSIGNED (result_type))
1103 LONGEST v2_signed = value_as_long (arg2);
1104 ULONGEST v1, v2, v = 0;
1106 v1 = (ULONGEST) value_as_long (arg1);
1107 v2 = (ULONGEST) v2_signed;
1128 error (_("Division by zero"));
1132 v = uinteger_pow (v1, v2_signed);
1139 error (_("Division by zero"));
1143 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1144 v1 mod 0 has a defined value, v1. */
1152 /* Note floor(v1/v2) == v1/v2 for unsigned. */
1165 case BINOP_BITWISE_AND:
1169 case BINOP_BITWISE_IOR:
1173 case BINOP_BITWISE_XOR:
1177 case BINOP_LOGICAL_AND:
1181 case BINOP_LOGICAL_OR:
1186 v = v1 < v2 ? v1 : v2;
1190 v = v1 > v2 ? v1 : v2;
1197 case BINOP_NOTEQUAL:
1218 error (_("Invalid binary operation on numbers."));
1221 val = allocate_value (result_type);
1222 store_unsigned_integer (value_contents_raw (val),
1223 TYPE_LENGTH (value_type (val)),
1225 (get_type_arch (result_type)),
1230 LONGEST v1, v2, v = 0;
1232 v1 = value_as_long (arg1);
1233 v2 = value_as_long (arg2);
1254 error (_("Division by zero"));
1258 v = integer_pow (v1, v2);
1265 error (_("Division by zero"));
1269 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1270 X mod 0 has a defined value, X. */
1278 /* Compute floor. */
1279 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1295 case BINOP_BITWISE_AND:
1299 case BINOP_BITWISE_IOR:
1303 case BINOP_BITWISE_XOR:
1307 case BINOP_LOGICAL_AND:
1311 case BINOP_LOGICAL_OR:
1316 v = v1 < v2 ? v1 : v2;
1320 v = v1 > v2 ? v1 : v2;
1327 case BINOP_NOTEQUAL:
1348 error (_("Invalid binary operation on numbers."));
1351 val = allocate_value (result_type);
1352 store_signed_integer (value_contents_raw (val),
1353 TYPE_LENGTH (value_type (val)),
1355 (get_type_arch (result_type)),
1363 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1364 replicating SCALAR_VALUE for each element of the vector. Only scalar
1365 types that can be cast to the type of one element of the vector are
1366 acceptable. The newly created vector value is returned upon success,
1367 otherwise an error is thrown. */
1370 value_vector_widen (struct value *scalar_value, struct type *vector_type)
1372 /* Widen the scalar to a vector. */
1373 struct type *eltype, *scalar_type;
1374 struct value *val, *elval;
1375 LONGEST low_bound, high_bound;
1378 CHECK_TYPEDEF (vector_type);
1380 gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
1381 && TYPE_VECTOR (vector_type));
1383 if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1384 error (_("Could not determine the vector bounds"));
1386 eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
1387 elval = value_cast (eltype, scalar_value);
1389 scalar_type = check_typedef (value_type (scalar_value));
1391 /* If we reduced the length of the scalar then check we didn't loose any
1393 if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
1394 && !value_equal (elval, scalar_value))
1395 error (_("conversion of scalar to vector involves truncation"));
1397 val = allocate_value (vector_type);
1398 for (i = 0; i < high_bound - low_bound + 1; i++)
1399 /* Duplicate the contents of elval into the destination vector. */
1400 memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
1401 value_contents_all (elval), TYPE_LENGTH (eltype));
1406 /* Performs a binary operation on two vector operands by calling scalar_binop
1407 for each pair of vector components. */
1409 static struct value *
1410 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1412 struct value *val, *tmp, *mark;
1413 struct type *type1, *type2, *eltype1, *eltype2;
1414 int t1_is_vec, t2_is_vec, elsize, i;
1415 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1417 type1 = check_typedef (value_type (val1));
1418 type2 = check_typedef (value_type (val2));
1420 t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1421 && TYPE_VECTOR (type1)) ? 1 : 0;
1422 t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1423 && TYPE_VECTOR (type2)) ? 1 : 0;
1425 if (!t1_is_vec || !t2_is_vec)
1426 error (_("Vector operations are only supported among vectors"));
1428 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1429 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1430 error (_("Could not determine the vector bounds"));
1432 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1433 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1434 elsize = TYPE_LENGTH (eltype1);
1436 if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1437 || elsize != TYPE_LENGTH (eltype2)
1438 || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1439 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1440 error (_("Cannot perform operation on vectors with different types"));
1442 val = allocate_value (type1);
1443 mark = value_mark ();
1444 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1446 tmp = value_binop (value_subscript (val1, i),
1447 value_subscript (val2, i), op);
1448 memcpy (value_contents_writeable (val) + i * elsize,
1449 value_contents_all (tmp),
1452 value_free_to_mark (mark);
1457 /* Perform a binary operation on two operands. */
1460 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1463 struct type *type1 = check_typedef (value_type (arg1));
1464 struct type *type2 = check_typedef (value_type (arg2));
1465 int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1466 && TYPE_VECTOR (type1));
1467 int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1468 && TYPE_VECTOR (type2));
1470 if (!t1_is_vec && !t2_is_vec)
1471 val = scalar_binop (arg1, arg2, op);
1472 else if (t1_is_vec && t2_is_vec)
1473 val = vector_binop (arg1, arg2, op);
1476 /* Widen the scalar operand to a vector. */
1477 struct value **v = t1_is_vec ? &arg2 : &arg1;
1478 struct type *t = t1_is_vec ? type2 : type1;
1480 if (TYPE_CODE (t) != TYPE_CODE_FLT
1481 && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1482 && !is_integral_type (t))
1483 error (_("Argument to operation not a number or boolean."));
1485 /* Replicate the scalar value to make a vector value. */
1486 *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1488 val = vector_binop (arg1, arg2, op);
1494 /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1497 value_logical_not (struct value *arg1)
1503 arg1 = coerce_array (arg1);
1504 type1 = check_typedef (value_type (arg1));
1506 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1507 return 0 == value_as_double (arg1);
1508 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
1509 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1510 gdbarch_byte_order (get_type_arch (type1)));
1512 len = TYPE_LENGTH (type1);
1513 p = value_contents (arg1);
1524 /* Perform a comparison on two string values (whose content are not
1525 necessarily null terminated) based on their length. */
1528 value_strcmp (struct value *arg1, struct value *arg2)
1530 int len1 = TYPE_LENGTH (value_type (arg1));
1531 int len2 = TYPE_LENGTH (value_type (arg2));
1532 const gdb_byte *s1 = value_contents (arg1);
1533 const gdb_byte *s2 = value_contents (arg2);
1534 int i, len = len1 < len2 ? len1 : len2;
1536 for (i = 0; i < len; i++)
1540 else if (s1[i] > s2[i])
1548 else if (len1 > len2)
1554 /* Simulate the C operator == by returning a 1
1555 iff ARG1 and ARG2 have equal contents. */
1558 value_equal (struct value *arg1, struct value *arg2)
1563 struct type *type1, *type2;
1564 enum type_code code1;
1565 enum type_code code2;
1566 int is_int1, is_int2;
1568 arg1 = coerce_array (arg1);
1569 arg2 = coerce_array (arg2);
1571 type1 = check_typedef (value_type (arg1));
1572 type2 = check_typedef (value_type (arg2));
1573 code1 = TYPE_CODE (type1);
1574 code2 = TYPE_CODE (type2);
1575 is_int1 = is_integral_type (type1);
1576 is_int2 = is_integral_type (type2);
1578 if (is_int1 && is_int2)
1579 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1581 else if ((code1 == TYPE_CODE_FLT || is_int1)
1582 && (code2 == TYPE_CODE_FLT || is_int2))
1584 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1585 `long double' values are returned in static storage (m68k). */
1586 DOUBLEST d = value_as_double (arg1);
1588 return d == value_as_double (arg2);
1590 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1591 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1593 gdb_byte v1[16], v2[16];
1595 enum bfd_endian byte_order_v1, byte_order_v2;
1597 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1598 v2, &len_v2, &byte_order_v2);
1600 return decimal_compare (v1, len_v1, byte_order_v1,
1601 v2, len_v2, byte_order_v2) == 0;
1604 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1606 else if (code1 == TYPE_CODE_PTR && is_int2)
1607 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
1608 else if (code2 == TYPE_CODE_PTR && is_int1)
1609 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
1611 else if (code1 == code2
1612 && ((len = (int) TYPE_LENGTH (type1))
1613 == (int) TYPE_LENGTH (type2)))
1615 p1 = value_contents (arg1);
1616 p2 = value_contents (arg2);
1624 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1626 return value_strcmp (arg1, arg2) == 0;
1630 error (_("Invalid type combination in equality test."));
1631 return 0; /* For lint -- never reached. */
1635 /* Compare values based on their raw contents. Useful for arrays since
1636 value_equal coerces them to pointers, thus comparing just the address
1637 of the array instead of its contents. */
1640 value_equal_contents (struct value *arg1, struct value *arg2)
1642 struct type *type1, *type2;
1644 type1 = check_typedef (value_type (arg1));
1645 type2 = check_typedef (value_type (arg2));
1647 return (TYPE_CODE (type1) == TYPE_CODE (type2)
1648 && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1649 && memcmp (value_contents (arg1), value_contents (arg2),
1650 TYPE_LENGTH (type1)) == 0);
1653 /* Simulate the C operator < by returning 1
1654 iff ARG1's contents are less than ARG2's. */
1657 value_less (struct value *arg1, struct value *arg2)
1659 enum type_code code1;
1660 enum type_code code2;
1661 struct type *type1, *type2;
1662 int is_int1, is_int2;
1664 arg1 = coerce_array (arg1);
1665 arg2 = coerce_array (arg2);
1667 type1 = check_typedef (value_type (arg1));
1668 type2 = check_typedef (value_type (arg2));
1669 code1 = TYPE_CODE (type1);
1670 code2 = TYPE_CODE (type2);
1671 is_int1 = is_integral_type (type1);
1672 is_int2 = is_integral_type (type2);
1674 if (is_int1 && is_int2)
1675 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1677 else if ((code1 == TYPE_CODE_FLT || is_int1)
1678 && (code2 == TYPE_CODE_FLT || is_int2))
1680 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1681 `long double' values are returned in static storage (m68k). */
1682 DOUBLEST d = value_as_double (arg1);
1684 return d < value_as_double (arg2);
1686 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1687 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1689 gdb_byte v1[16], v2[16];
1691 enum bfd_endian byte_order_v1, byte_order_v2;
1693 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1694 v2, &len_v2, &byte_order_v2);
1696 return decimal_compare (v1, len_v1, byte_order_v1,
1697 v2, len_v2, byte_order_v2) == -1;
1699 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1700 return value_as_address (arg1) < value_as_address (arg2);
1702 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1704 else if (code1 == TYPE_CODE_PTR && is_int2)
1705 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
1706 else if (code2 == TYPE_CODE_PTR && is_int1)
1707 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
1708 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1709 return value_strcmp (arg1, arg2) < 0;
1712 error (_("Invalid type combination in ordering comparison."));
1717 /* The unary operators +, - and ~. They free the argument ARG1. */
1720 value_pos (struct value *arg1)
1724 arg1 = coerce_ref (arg1);
1725 type = check_typedef (value_type (arg1));
1727 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1728 return value_from_double (type, value_as_double (arg1));
1729 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1730 return value_from_decfloat (type, value_contents (arg1));
1731 else if (is_integral_type (type))
1733 return value_from_longest (type, value_as_long (arg1));
1735 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1737 struct value *val = allocate_value (type);
1739 memcpy (value_contents_raw (val), value_contents (arg1),
1740 TYPE_LENGTH (type));
1745 error (_("Argument to positive operation not a number."));
1746 return 0; /* For lint -- never reached. */
1751 value_neg (struct value *arg1)
1755 arg1 = coerce_ref (arg1);
1756 type = check_typedef (value_type (arg1));
1758 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1760 struct value *val = allocate_value (type);
1761 int len = TYPE_LENGTH (type);
1762 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */
1764 memcpy (decbytes, value_contents (arg1), len);
1766 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
1767 decbytes[len-1] = decbytes[len - 1] | 0x80;
1769 decbytes[0] = decbytes[0] | 0x80;
1771 memcpy (value_contents_raw (val), decbytes, len);
1774 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1775 return value_from_double (type, -value_as_double (arg1));
1776 else if (is_integral_type (type))
1778 return value_from_longest (type, -value_as_long (arg1));
1780 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1782 struct value *tmp, *val = allocate_value (type);
1783 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1785 LONGEST low_bound, high_bound;
1787 if (!get_array_bounds (type, &low_bound, &high_bound))
1788 error (_("Could not determine the vector bounds"));
1790 for (i = 0; i < high_bound - low_bound + 1; i++)
1792 tmp = value_neg (value_subscript (arg1, i));
1793 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1794 value_contents_all (tmp), TYPE_LENGTH (eltype));
1800 error (_("Argument to negate operation not a number."));
1801 return 0; /* For lint -- never reached. */
1806 value_complement (struct value *arg1)
1811 arg1 = coerce_ref (arg1);
1812 type = check_typedef (value_type (arg1));
1814 if (is_integral_type (type))
1815 val = value_from_longest (type, ~value_as_long (arg1));
1816 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1819 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1821 LONGEST low_bound, high_bound;
1823 if (!get_array_bounds (type, &low_bound, &high_bound))
1824 error (_("Could not determine the vector bounds"));
1826 val = allocate_value (type);
1827 for (i = 0; i < high_bound - low_bound + 1; i++)
1829 tmp = value_complement (value_subscript (arg1, i));
1830 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1831 value_contents_all (tmp), TYPE_LENGTH (eltype));
1835 error (_("Argument to complement operation not an integer, boolean."));
1840 /* The INDEX'th bit of SET value whose value_type is TYPE,
1841 and whose value_contents is valaddr.
1842 Return -1 if out of range, -2 other error. */
1845 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
1847 struct gdbarch *gdbarch = get_type_arch (type);
1848 LONGEST low_bound, high_bound;
1851 struct type *range = TYPE_INDEX_TYPE (type);
1853 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1855 if (index < low_bound || index > high_bound)
1857 rel_index = index - low_bound;
1858 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1859 gdbarch_byte_order (gdbarch));
1860 rel_index %= TARGET_CHAR_BIT;
1861 if (gdbarch_bits_big_endian (gdbarch))
1862 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1863 return (word >> rel_index) & 1;
1867 value_in (struct value *element, struct value *set)
1870 struct type *settype = check_typedef (value_type (set));
1871 struct type *eltype = check_typedef (value_type (element));
1873 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1874 eltype = TYPE_TARGET_TYPE (eltype);
1875 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1876 error (_("Second argument of 'IN' has wrong type"));
1877 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1878 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1879 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1880 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1881 error (_("First argument of 'IN' has wrong type"));
1882 member = value_bit_index (settype, value_contents (set),
1883 value_as_long (element));
1885 error (_("First argument of 'IN' not in range"));
1890 _initialize_valarith (void)