15796c8dcSSimon Schubert /* Perform arithmetic and other operations on values, for GDB. 25796c8dcSSimon Schubert 35796c8dcSSimon Schubert Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 4*cf7f2e2dSJohn Marino 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 5*cf7f2e2dSJohn Marino 2010 Free Software Foundation, Inc. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This file is part of GDB. 85796c8dcSSimon Schubert 95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 125796c8dcSSimon Schubert (at your option) any later version. 135796c8dcSSimon Schubert 145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 175796c8dcSSimon Schubert GNU General Public License for more details. 185796c8dcSSimon Schubert 195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "defs.h" 235796c8dcSSimon Schubert #include "value.h" 245796c8dcSSimon Schubert #include "symtab.h" 255796c8dcSSimon Schubert #include "gdbtypes.h" 265796c8dcSSimon Schubert #include "expression.h" 275796c8dcSSimon Schubert #include "target.h" 285796c8dcSSimon Schubert #include "language.h" 295796c8dcSSimon Schubert #include "gdb_string.h" 305796c8dcSSimon Schubert #include "doublest.h" 315796c8dcSSimon Schubert #include "dfp.h" 325796c8dcSSimon Schubert #include <math.h> 335796c8dcSSimon Schubert #include "infcall.h" 34*cf7f2e2dSJohn Marino #include "exceptions.h" 355796c8dcSSimon Schubert 365796c8dcSSimon Schubert /* Define whether or not the C operator '/' truncates towards zero for 375796c8dcSSimon Schubert differently signed operands (truncation direction is undefined in C). */ 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert #ifndef TRUNCATION_TOWARDS_ZERO 405796c8dcSSimon Schubert #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2) 415796c8dcSSimon Schubert #endif 425796c8dcSSimon Schubert 435796c8dcSSimon Schubert void _initialize_valarith (void); 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert 465796c8dcSSimon Schubert /* Given a pointer, return the size of its target. 475796c8dcSSimon Schubert If the pointer type is void *, then return 1. 485796c8dcSSimon Schubert If the target type is incomplete, then error out. 495796c8dcSSimon Schubert This isn't a general purpose function, but just a 505796c8dcSSimon Schubert helper for value_ptradd. 515796c8dcSSimon Schubert */ 525796c8dcSSimon Schubert 535796c8dcSSimon Schubert static LONGEST 545796c8dcSSimon Schubert find_size_for_pointer_math (struct type *ptr_type) 555796c8dcSSimon Schubert { 565796c8dcSSimon Schubert LONGEST sz = -1; 575796c8dcSSimon Schubert struct type *ptr_target; 585796c8dcSSimon Schubert 595796c8dcSSimon Schubert gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR); 605796c8dcSSimon Schubert ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type)); 615796c8dcSSimon Schubert 625796c8dcSSimon Schubert sz = TYPE_LENGTH (ptr_target); 635796c8dcSSimon Schubert if (sz == 0) 645796c8dcSSimon Schubert { 655796c8dcSSimon Schubert if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID) 665796c8dcSSimon Schubert sz = 1; 675796c8dcSSimon Schubert else 685796c8dcSSimon Schubert { 695796c8dcSSimon Schubert char *name; 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert name = TYPE_NAME (ptr_target); 725796c8dcSSimon Schubert if (name == NULL) 735796c8dcSSimon Schubert name = TYPE_TAG_NAME (ptr_target); 745796c8dcSSimon Schubert if (name == NULL) 755796c8dcSSimon Schubert error (_("Cannot perform pointer math on incomplete types, " 765796c8dcSSimon Schubert "try casting to a known type, or void *.")); 775796c8dcSSimon Schubert else 785796c8dcSSimon Schubert error (_("Cannot perform pointer math on incomplete type \"%s\", " 795796c8dcSSimon Schubert "try casting to a known type, or void *."), name); 805796c8dcSSimon Schubert } 815796c8dcSSimon Schubert } 825796c8dcSSimon Schubert return sz; 835796c8dcSSimon Schubert } 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert /* Given a pointer ARG1 and an integral value ARG2, return the 865796c8dcSSimon Schubert result of C-style pointer arithmetic ARG1 + ARG2. */ 875796c8dcSSimon Schubert 885796c8dcSSimon Schubert struct value * 895796c8dcSSimon Schubert value_ptradd (struct value *arg1, LONGEST arg2) 905796c8dcSSimon Schubert { 915796c8dcSSimon Schubert struct type *valptrtype; 925796c8dcSSimon Schubert LONGEST sz; 935796c8dcSSimon Schubert 945796c8dcSSimon Schubert arg1 = coerce_array (arg1); 955796c8dcSSimon Schubert valptrtype = check_typedef (value_type (arg1)); 965796c8dcSSimon Schubert sz = find_size_for_pointer_math (valptrtype); 975796c8dcSSimon Schubert 985796c8dcSSimon Schubert return value_from_pointer (valptrtype, 995796c8dcSSimon Schubert value_as_address (arg1) + sz * arg2); 1005796c8dcSSimon Schubert } 1015796c8dcSSimon Schubert 1025796c8dcSSimon Schubert /* Given two compatible pointer values ARG1 and ARG2, return the 1035796c8dcSSimon Schubert result of C-style pointer arithmetic ARG1 - ARG2. */ 1045796c8dcSSimon Schubert 1055796c8dcSSimon Schubert LONGEST 1065796c8dcSSimon Schubert value_ptrdiff (struct value *arg1, struct value *arg2) 1075796c8dcSSimon Schubert { 1085796c8dcSSimon Schubert struct type *type1, *type2; 1095796c8dcSSimon Schubert LONGEST sz; 1105796c8dcSSimon Schubert 1115796c8dcSSimon Schubert arg1 = coerce_array (arg1); 1125796c8dcSSimon Schubert arg2 = coerce_array (arg2); 1135796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 1145796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 1155796c8dcSSimon Schubert 1165796c8dcSSimon Schubert gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR); 1175796c8dcSSimon Schubert gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR); 1185796c8dcSSimon Schubert 1195796c8dcSSimon Schubert if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))) 1205796c8dcSSimon Schubert != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2)))) 1215796c8dcSSimon Schubert error (_("\ 1225796c8dcSSimon Schubert First argument of `-' is a pointer and second argument is neither\n\ 1235796c8dcSSimon Schubert an integer nor a pointer of the same type.")); 1245796c8dcSSimon Schubert 1255796c8dcSSimon Schubert sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))); 126*cf7f2e2dSJohn Marino if (sz == 0) 127*cf7f2e2dSJohn Marino { 128*cf7f2e2dSJohn Marino warning (_("Type size unknown, assuming 1. " 129*cf7f2e2dSJohn Marino "Try casting to a known type, or void *.")); 130*cf7f2e2dSJohn Marino sz = 1; 131*cf7f2e2dSJohn Marino } 132*cf7f2e2dSJohn Marino 1335796c8dcSSimon Schubert return (value_as_long (arg1) - value_as_long (arg2)) / sz; 1345796c8dcSSimon Schubert } 1355796c8dcSSimon Schubert 1365796c8dcSSimon Schubert /* Return the value of ARRAY[IDX]. 1375796c8dcSSimon Schubert 1385796c8dcSSimon Schubert ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the 1395796c8dcSSimon Schubert current language supports C-style arrays, it may also be TYPE_CODE_PTR. 1405796c8dcSSimon Schubert To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript. 1415796c8dcSSimon Schubert 1425796c8dcSSimon Schubert See comments in value_coerce_array() for rationale for reason for 1435796c8dcSSimon Schubert doing lower bounds adjustment here rather than there. 1445796c8dcSSimon Schubert FIXME: Perhaps we should validate that the index is valid and if 1455796c8dcSSimon Schubert verbosity is set, warn about invalid indices (but still use them). */ 1465796c8dcSSimon Schubert 1475796c8dcSSimon Schubert struct value * 1485796c8dcSSimon Schubert value_subscript (struct value *array, LONGEST index) 1495796c8dcSSimon Schubert { 1505796c8dcSSimon Schubert int c_style = current_language->c_style_arrays; 1515796c8dcSSimon Schubert struct type *tarray; 1525796c8dcSSimon Schubert 1535796c8dcSSimon Schubert array = coerce_ref (array); 1545796c8dcSSimon Schubert tarray = check_typedef (value_type (array)); 1555796c8dcSSimon Schubert 1565796c8dcSSimon Schubert if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY 1575796c8dcSSimon Schubert || TYPE_CODE (tarray) == TYPE_CODE_STRING) 1585796c8dcSSimon Schubert { 1595796c8dcSSimon Schubert struct type *range_type = TYPE_INDEX_TYPE (tarray); 1605796c8dcSSimon Schubert LONGEST lowerbound, upperbound; 1615796c8dcSSimon Schubert 162*cf7f2e2dSJohn Marino get_discrete_bounds (range_type, &lowerbound, &upperbound); 1635796c8dcSSimon Schubert if (VALUE_LVAL (array) != lval_memory) 1645796c8dcSSimon Schubert return value_subscripted_rvalue (array, index, lowerbound); 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert if (c_style == 0) 1675796c8dcSSimon Schubert { 1685796c8dcSSimon Schubert if (index >= lowerbound && index <= upperbound) 1695796c8dcSSimon Schubert return value_subscripted_rvalue (array, index, lowerbound); 1705796c8dcSSimon Schubert /* Emit warning unless we have an array of unknown size. 1715796c8dcSSimon Schubert An array of unknown size has lowerbound 0 and upperbound -1. */ 1725796c8dcSSimon Schubert if (upperbound > -1) 1735796c8dcSSimon Schubert warning (_("array or string index out of range")); 1745796c8dcSSimon Schubert /* fall doing C stuff */ 1755796c8dcSSimon Schubert c_style = 1; 1765796c8dcSSimon Schubert } 1775796c8dcSSimon Schubert 1785796c8dcSSimon Schubert index -= lowerbound; 1795796c8dcSSimon Schubert array = value_coerce_array (array); 1805796c8dcSSimon Schubert } 1815796c8dcSSimon Schubert 1825796c8dcSSimon Schubert if (c_style) 1835796c8dcSSimon Schubert return value_ind (value_ptradd (array, index)); 1845796c8dcSSimon Schubert else 1855796c8dcSSimon Schubert error (_("not an array or string")); 1865796c8dcSSimon Schubert } 1875796c8dcSSimon Schubert 1885796c8dcSSimon Schubert /* Return the value of EXPR[IDX], expr an aggregate rvalue 1895796c8dcSSimon Schubert (eg, a vector register). This routine used to promote floats 1905796c8dcSSimon Schubert to doubles, but no longer does. */ 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert struct value * 1935796c8dcSSimon Schubert value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound) 1945796c8dcSSimon Schubert { 1955796c8dcSSimon Schubert struct type *array_type = check_typedef (value_type (array)); 1965796c8dcSSimon Schubert struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type)); 1975796c8dcSSimon Schubert unsigned int elt_size = TYPE_LENGTH (elt_type); 1985796c8dcSSimon Schubert unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound); 1995796c8dcSSimon Schubert struct value *v; 2005796c8dcSSimon Schubert 201*cf7f2e2dSJohn Marino if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type) 202*cf7f2e2dSJohn Marino && elt_offs >= TYPE_LENGTH (array_type))) 2035796c8dcSSimon Schubert error (_("no such vector element")); 2045796c8dcSSimon Schubert 2055796c8dcSSimon Schubert v = allocate_value (elt_type); 2065796c8dcSSimon Schubert if (VALUE_LVAL (array) == lval_memory && value_lazy (array)) 2075796c8dcSSimon Schubert set_value_lazy (v, 1); 2085796c8dcSSimon Schubert else 2095796c8dcSSimon Schubert memcpy (value_contents_writeable (v), 2105796c8dcSSimon Schubert value_contents (array) + elt_offs, elt_size); 2115796c8dcSSimon Schubert 2125796c8dcSSimon Schubert set_value_component_location (v, array); 2135796c8dcSSimon Schubert VALUE_REGNUM (v) = VALUE_REGNUM (array); 2145796c8dcSSimon Schubert VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array); 2155796c8dcSSimon Schubert set_value_offset (v, value_offset (array) + elt_offs); 2165796c8dcSSimon Schubert return v; 2175796c8dcSSimon Schubert } 2185796c8dcSSimon Schubert 2195796c8dcSSimon Schubert /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */ 2205796c8dcSSimon Schubert 2215796c8dcSSimon Schubert struct value * 2225796c8dcSSimon Schubert value_bitstring_subscript (struct type *type, 2235796c8dcSSimon Schubert struct value *bitstring, LONGEST index) 2245796c8dcSSimon Schubert { 2255796c8dcSSimon Schubert 2265796c8dcSSimon Schubert struct type *bitstring_type, *range_type; 2275796c8dcSSimon Schubert struct value *v; 2285796c8dcSSimon Schubert int offset, byte, bit_index; 2295796c8dcSSimon Schubert LONGEST lowerbound, upperbound; 2305796c8dcSSimon Schubert 2315796c8dcSSimon Schubert bitstring_type = check_typedef (value_type (bitstring)); 2325796c8dcSSimon Schubert gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING); 2335796c8dcSSimon Schubert 2345796c8dcSSimon Schubert range_type = TYPE_INDEX_TYPE (bitstring_type); 2355796c8dcSSimon Schubert get_discrete_bounds (range_type, &lowerbound, &upperbound); 2365796c8dcSSimon Schubert if (index < lowerbound || index > upperbound) 2375796c8dcSSimon Schubert error (_("bitstring index out of range")); 2385796c8dcSSimon Schubert 2395796c8dcSSimon Schubert index -= lowerbound; 2405796c8dcSSimon Schubert offset = index / TARGET_CHAR_BIT; 2415796c8dcSSimon Schubert byte = *((char *) value_contents (bitstring) + offset); 2425796c8dcSSimon Schubert 2435796c8dcSSimon Schubert bit_index = index % TARGET_CHAR_BIT; 2445796c8dcSSimon Schubert byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ? 2455796c8dcSSimon Schubert TARGET_CHAR_BIT - 1 - bit_index : bit_index); 2465796c8dcSSimon Schubert 2475796c8dcSSimon Schubert v = value_from_longest (type, byte & 1); 2485796c8dcSSimon Schubert 2495796c8dcSSimon Schubert set_value_bitpos (v, bit_index); 2505796c8dcSSimon Schubert set_value_bitsize (v, 1); 2515796c8dcSSimon Schubert set_value_component_location (v, bitstring); 2525796c8dcSSimon Schubert VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring); 2535796c8dcSSimon Schubert 2545796c8dcSSimon Schubert set_value_offset (v, offset + value_offset (bitstring)); 2555796c8dcSSimon Schubert 2565796c8dcSSimon Schubert return v; 2575796c8dcSSimon Schubert } 2585796c8dcSSimon Schubert 2595796c8dcSSimon Schubert 2605796c8dcSSimon Schubert /* Check to see if either argument is a structure, or a reference to 2615796c8dcSSimon Schubert one. This is called so we know whether to go ahead with the normal 2625796c8dcSSimon Schubert binop or look for a user defined function instead. 2635796c8dcSSimon Schubert 2645796c8dcSSimon Schubert For now, we do not overload the `=' operator. */ 2655796c8dcSSimon Schubert 2665796c8dcSSimon Schubert int 267*cf7f2e2dSJohn Marino binop_types_user_defined_p (enum exp_opcode op, 268*cf7f2e2dSJohn Marino struct type *type1, struct type *type2) 2695796c8dcSSimon Schubert { 2705796c8dcSSimon Schubert if (op == BINOP_ASSIGN || op == BINOP_CONCAT) 2715796c8dcSSimon Schubert return 0; 2725796c8dcSSimon Schubert 273*cf7f2e2dSJohn Marino type1 = check_typedef (type1); 2745796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_REF) 2755796c8dcSSimon Schubert type1 = check_typedef (TYPE_TARGET_TYPE (type1)); 2765796c8dcSSimon Schubert 277*cf7f2e2dSJohn Marino type2 = check_typedef (type1); 2785796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_REF) 2795796c8dcSSimon Schubert type2 = check_typedef (TYPE_TARGET_TYPE (type2)); 2805796c8dcSSimon Schubert 2815796c8dcSSimon Schubert return (TYPE_CODE (type1) == TYPE_CODE_STRUCT 2825796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_STRUCT); 2835796c8dcSSimon Schubert } 2845796c8dcSSimon Schubert 285*cf7f2e2dSJohn Marino /* Check to see if either argument is a structure, or a reference to 286*cf7f2e2dSJohn Marino one. This is called so we know whether to go ahead with the normal 287*cf7f2e2dSJohn Marino binop or look for a user defined function instead. 288*cf7f2e2dSJohn Marino 289*cf7f2e2dSJohn Marino For now, we do not overload the `=' operator. */ 290*cf7f2e2dSJohn Marino 291*cf7f2e2dSJohn Marino int 292*cf7f2e2dSJohn Marino binop_user_defined_p (enum exp_opcode op, 293*cf7f2e2dSJohn Marino struct value *arg1, struct value *arg2) 294*cf7f2e2dSJohn Marino { 295*cf7f2e2dSJohn Marino return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2)); 296*cf7f2e2dSJohn Marino } 297*cf7f2e2dSJohn Marino 2985796c8dcSSimon Schubert /* Check to see if argument is a structure. This is called so 2995796c8dcSSimon Schubert we know whether to go ahead with the normal unop or look for a 3005796c8dcSSimon Schubert user defined function instead. 3015796c8dcSSimon Schubert 3025796c8dcSSimon Schubert For now, we do not overload the `&' operator. */ 3035796c8dcSSimon Schubert 3045796c8dcSSimon Schubert int 3055796c8dcSSimon Schubert unop_user_defined_p (enum exp_opcode op, struct value *arg1) 3065796c8dcSSimon Schubert { 3075796c8dcSSimon Schubert struct type *type1; 308*cf7f2e2dSJohn Marino 3095796c8dcSSimon Schubert if (op == UNOP_ADDR) 3105796c8dcSSimon Schubert return 0; 3115796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 3125796c8dcSSimon Schubert for (;;) 3135796c8dcSSimon Schubert { 3145796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_STRUCT) 3155796c8dcSSimon Schubert return 1; 3165796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_REF) 3175796c8dcSSimon Schubert type1 = TYPE_TARGET_TYPE (type1); 3185796c8dcSSimon Schubert else 3195796c8dcSSimon Schubert return 0; 3205796c8dcSSimon Schubert } 3215796c8dcSSimon Schubert } 3225796c8dcSSimon Schubert 323*cf7f2e2dSJohn Marino /* Try to find an operator named OPERATOR which takes NARGS arguments 324*cf7f2e2dSJohn Marino specified in ARGS. If the operator found is a static member operator 325*cf7f2e2dSJohn Marino *STATIC_MEMFUNP will be set to 1, and otherwise 0. 326*cf7f2e2dSJohn Marino The search if performed through find_overload_match which will handle 327*cf7f2e2dSJohn Marino member operators, non member operators, operators imported implicitly or 328*cf7f2e2dSJohn Marino explicitly, and perform correct overload resolution in all of the above 329*cf7f2e2dSJohn Marino situations or combinations thereof. */ 330*cf7f2e2dSJohn Marino 331*cf7f2e2dSJohn Marino static struct value * 332*cf7f2e2dSJohn Marino value_user_defined_cpp_op (struct value **args, int nargs, char *operator, 333*cf7f2e2dSJohn Marino int *static_memfuncp) 334*cf7f2e2dSJohn Marino { 335*cf7f2e2dSJohn Marino 336*cf7f2e2dSJohn Marino struct symbol *symp = NULL; 337*cf7f2e2dSJohn Marino struct value *valp = NULL; 338*cf7f2e2dSJohn Marino struct type **arg_types; 339*cf7f2e2dSJohn Marino int i; 340*cf7f2e2dSJohn Marino 341*cf7f2e2dSJohn Marino arg_types = (struct type **) alloca (nargs * (sizeof (struct type *))); 342*cf7f2e2dSJohn Marino /* Prepare list of argument types for overload resolution */ 343*cf7f2e2dSJohn Marino for (i = 0; i < nargs; i++) 344*cf7f2e2dSJohn Marino arg_types[i] = value_type (args[i]); 345*cf7f2e2dSJohn Marino 346*cf7f2e2dSJohn Marino find_overload_match (arg_types, nargs, operator, BOTH /* could be method */, 347*cf7f2e2dSJohn Marino 0 /* strict match */, &args[0], /* objp */ 348*cf7f2e2dSJohn Marino NULL /* pass NULL symbol since symbol is unknown */, 349*cf7f2e2dSJohn Marino &valp, &symp, static_memfuncp, 0); 350*cf7f2e2dSJohn Marino 351*cf7f2e2dSJohn Marino if (valp) 352*cf7f2e2dSJohn Marino return valp; 353*cf7f2e2dSJohn Marino 354*cf7f2e2dSJohn Marino if (symp) 355*cf7f2e2dSJohn Marino { 356*cf7f2e2dSJohn Marino /* This is a non member function and does not 357*cf7f2e2dSJohn Marino expect a reference as its first argument 358*cf7f2e2dSJohn Marino rather the explicit structure. */ 359*cf7f2e2dSJohn Marino args[0] = value_ind (args[0]); 360*cf7f2e2dSJohn Marino return value_of_variable (symp, 0); 361*cf7f2e2dSJohn Marino } 362*cf7f2e2dSJohn Marino 363*cf7f2e2dSJohn Marino error (_("Could not find %s."), operator); 364*cf7f2e2dSJohn Marino } 365*cf7f2e2dSJohn Marino 366*cf7f2e2dSJohn Marino /* Lookup user defined operator NAME. Return a value representing the 367*cf7f2e2dSJohn Marino function, otherwise return NULL. */ 368*cf7f2e2dSJohn Marino 369*cf7f2e2dSJohn Marino static struct value * 370*cf7f2e2dSJohn Marino value_user_defined_op (struct value **argp, struct value **args, char *name, 371*cf7f2e2dSJohn Marino int *static_memfuncp, int nargs) 372*cf7f2e2dSJohn Marino { 373*cf7f2e2dSJohn Marino struct value *result = NULL; 374*cf7f2e2dSJohn Marino 375*cf7f2e2dSJohn Marino if (current_language->la_language == language_cplus) 376*cf7f2e2dSJohn Marino result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp); 377*cf7f2e2dSJohn Marino else 378*cf7f2e2dSJohn Marino result = value_struct_elt (argp, args, name, static_memfuncp, 379*cf7f2e2dSJohn Marino "structure"); 380*cf7f2e2dSJohn Marino 381*cf7f2e2dSJohn Marino return result; 382*cf7f2e2dSJohn Marino } 383*cf7f2e2dSJohn Marino 3845796c8dcSSimon Schubert /* We know either arg1 or arg2 is a structure, so try to find the right 3855796c8dcSSimon Schubert user defined function. Create an argument vector that calls 3865796c8dcSSimon Schubert arg1.operator @ (arg1,arg2) and return that value (where '@' is any 3875796c8dcSSimon Schubert binary operator which is legal for GNU C++). 3885796c8dcSSimon Schubert 3895796c8dcSSimon Schubert OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP 3905796c8dcSSimon Schubert is the opcode saying how to modify it. Otherwise, OTHEROP is 3915796c8dcSSimon Schubert unused. */ 3925796c8dcSSimon Schubert 3935796c8dcSSimon Schubert struct value * 3945796c8dcSSimon Schubert value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op, 3955796c8dcSSimon Schubert enum exp_opcode otherop, enum noside noside) 3965796c8dcSSimon Schubert { 3975796c8dcSSimon Schubert struct value **argvec; 3985796c8dcSSimon Schubert char *ptr; 3995796c8dcSSimon Schubert char tstr[13]; 4005796c8dcSSimon Schubert int static_memfuncp; 4015796c8dcSSimon Schubert 4025796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 4035796c8dcSSimon Schubert arg2 = coerce_ref (arg2); 4045796c8dcSSimon Schubert 4055796c8dcSSimon Schubert /* now we know that what we have to do is construct our 4065796c8dcSSimon Schubert arg vector and find the right function to call it with. */ 4075796c8dcSSimon Schubert 4085796c8dcSSimon Schubert if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) 4095796c8dcSSimon Schubert error (_("Can't do that binary op on that type")); /* FIXME be explicit */ 4105796c8dcSSimon Schubert 4115796c8dcSSimon Schubert argvec = (struct value **) alloca (sizeof (struct value *) * 4); 4125796c8dcSSimon Schubert argvec[1] = value_addr (arg1); 4135796c8dcSSimon Schubert argvec[2] = arg2; 4145796c8dcSSimon Schubert argvec[3] = 0; 4155796c8dcSSimon Schubert 4165796c8dcSSimon Schubert /* make the right function name up */ 4175796c8dcSSimon Schubert strcpy (tstr, "operator__"); 4185796c8dcSSimon Schubert ptr = tstr + 8; 4195796c8dcSSimon Schubert switch (op) 4205796c8dcSSimon Schubert { 4215796c8dcSSimon Schubert case BINOP_ADD: 4225796c8dcSSimon Schubert strcpy (ptr, "+"); 4235796c8dcSSimon Schubert break; 4245796c8dcSSimon Schubert case BINOP_SUB: 4255796c8dcSSimon Schubert strcpy (ptr, "-"); 4265796c8dcSSimon Schubert break; 4275796c8dcSSimon Schubert case BINOP_MUL: 4285796c8dcSSimon Schubert strcpy (ptr, "*"); 4295796c8dcSSimon Schubert break; 4305796c8dcSSimon Schubert case BINOP_DIV: 4315796c8dcSSimon Schubert strcpy (ptr, "/"); 4325796c8dcSSimon Schubert break; 4335796c8dcSSimon Schubert case BINOP_REM: 4345796c8dcSSimon Schubert strcpy (ptr, "%"); 4355796c8dcSSimon Schubert break; 4365796c8dcSSimon Schubert case BINOP_LSH: 4375796c8dcSSimon Schubert strcpy (ptr, "<<"); 4385796c8dcSSimon Schubert break; 4395796c8dcSSimon Schubert case BINOP_RSH: 4405796c8dcSSimon Schubert strcpy (ptr, ">>"); 4415796c8dcSSimon Schubert break; 4425796c8dcSSimon Schubert case BINOP_BITWISE_AND: 4435796c8dcSSimon Schubert strcpy (ptr, "&"); 4445796c8dcSSimon Schubert break; 4455796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 4465796c8dcSSimon Schubert strcpy (ptr, "|"); 4475796c8dcSSimon Schubert break; 4485796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 4495796c8dcSSimon Schubert strcpy (ptr, "^"); 4505796c8dcSSimon Schubert break; 4515796c8dcSSimon Schubert case BINOP_LOGICAL_AND: 4525796c8dcSSimon Schubert strcpy (ptr, "&&"); 4535796c8dcSSimon Schubert break; 4545796c8dcSSimon Schubert case BINOP_LOGICAL_OR: 4555796c8dcSSimon Schubert strcpy (ptr, "||"); 4565796c8dcSSimon Schubert break; 4575796c8dcSSimon Schubert case BINOP_MIN: 4585796c8dcSSimon Schubert strcpy (ptr, "<?"); 4595796c8dcSSimon Schubert break; 4605796c8dcSSimon Schubert case BINOP_MAX: 4615796c8dcSSimon Schubert strcpy (ptr, ">?"); 4625796c8dcSSimon Schubert break; 4635796c8dcSSimon Schubert case BINOP_ASSIGN: 4645796c8dcSSimon Schubert strcpy (ptr, "="); 4655796c8dcSSimon Schubert break; 4665796c8dcSSimon Schubert case BINOP_ASSIGN_MODIFY: 4675796c8dcSSimon Schubert switch (otherop) 4685796c8dcSSimon Schubert { 4695796c8dcSSimon Schubert case BINOP_ADD: 4705796c8dcSSimon Schubert strcpy (ptr, "+="); 4715796c8dcSSimon Schubert break; 4725796c8dcSSimon Schubert case BINOP_SUB: 4735796c8dcSSimon Schubert strcpy (ptr, "-="); 4745796c8dcSSimon Schubert break; 4755796c8dcSSimon Schubert case BINOP_MUL: 4765796c8dcSSimon Schubert strcpy (ptr, "*="); 4775796c8dcSSimon Schubert break; 4785796c8dcSSimon Schubert case BINOP_DIV: 4795796c8dcSSimon Schubert strcpy (ptr, "/="); 4805796c8dcSSimon Schubert break; 4815796c8dcSSimon Schubert case BINOP_REM: 4825796c8dcSSimon Schubert strcpy (ptr, "%="); 4835796c8dcSSimon Schubert break; 4845796c8dcSSimon Schubert case BINOP_BITWISE_AND: 4855796c8dcSSimon Schubert strcpy (ptr, "&="); 4865796c8dcSSimon Schubert break; 4875796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 4885796c8dcSSimon Schubert strcpy (ptr, "|="); 4895796c8dcSSimon Schubert break; 4905796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 4915796c8dcSSimon Schubert strcpy (ptr, "^="); 4925796c8dcSSimon Schubert break; 4935796c8dcSSimon Schubert case BINOP_MOD: /* invalid */ 4945796c8dcSSimon Schubert default: 4955796c8dcSSimon Schubert error (_("Invalid binary operation specified.")); 4965796c8dcSSimon Schubert } 4975796c8dcSSimon Schubert break; 4985796c8dcSSimon Schubert case BINOP_SUBSCRIPT: 4995796c8dcSSimon Schubert strcpy (ptr, "[]"); 5005796c8dcSSimon Schubert break; 5015796c8dcSSimon Schubert case BINOP_EQUAL: 5025796c8dcSSimon Schubert strcpy (ptr, "=="); 5035796c8dcSSimon Schubert break; 5045796c8dcSSimon Schubert case BINOP_NOTEQUAL: 5055796c8dcSSimon Schubert strcpy (ptr, "!="); 5065796c8dcSSimon Schubert break; 5075796c8dcSSimon Schubert case BINOP_LESS: 5085796c8dcSSimon Schubert strcpy (ptr, "<"); 5095796c8dcSSimon Schubert break; 5105796c8dcSSimon Schubert case BINOP_GTR: 5115796c8dcSSimon Schubert strcpy (ptr, ">"); 5125796c8dcSSimon Schubert break; 5135796c8dcSSimon Schubert case BINOP_GEQ: 5145796c8dcSSimon Schubert strcpy (ptr, ">="); 5155796c8dcSSimon Schubert break; 5165796c8dcSSimon Schubert case BINOP_LEQ: 5175796c8dcSSimon Schubert strcpy (ptr, "<="); 5185796c8dcSSimon Schubert break; 5195796c8dcSSimon Schubert case BINOP_MOD: /* invalid */ 5205796c8dcSSimon Schubert default: 5215796c8dcSSimon Schubert error (_("Invalid binary operation specified.")); 5225796c8dcSSimon Schubert } 5235796c8dcSSimon Schubert 524*cf7f2e2dSJohn Marino argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr, 525*cf7f2e2dSJohn Marino &static_memfuncp, 2); 5265796c8dcSSimon Schubert 5275796c8dcSSimon Schubert if (argvec[0]) 5285796c8dcSSimon Schubert { 5295796c8dcSSimon Schubert if (static_memfuncp) 5305796c8dcSSimon Schubert { 5315796c8dcSSimon Schubert argvec[1] = argvec[0]; 5325796c8dcSSimon Schubert argvec++; 5335796c8dcSSimon Schubert } 5345796c8dcSSimon Schubert if (noside == EVAL_AVOID_SIDE_EFFECTS) 5355796c8dcSSimon Schubert { 5365796c8dcSSimon Schubert struct type *return_type; 537*cf7f2e2dSJohn Marino 5385796c8dcSSimon Schubert return_type 5395796c8dcSSimon Schubert = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); 5405796c8dcSSimon Schubert return value_zero (return_type, VALUE_LVAL (arg1)); 5415796c8dcSSimon Schubert } 5425796c8dcSSimon Schubert return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); 5435796c8dcSSimon Schubert } 5445796c8dcSSimon Schubert error (_("member function %s not found"), tstr); 5455796c8dcSSimon Schubert #ifdef lint 5465796c8dcSSimon Schubert return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); 5475796c8dcSSimon Schubert #endif 5485796c8dcSSimon Schubert } 5495796c8dcSSimon Schubert 5505796c8dcSSimon Schubert /* We know that arg1 is a structure, so try to find a unary user 5515796c8dcSSimon Schubert defined operator that matches the operator in question. 5525796c8dcSSimon Schubert Create an argument vector that calls arg1.operator @ (arg1) 5535796c8dcSSimon Schubert and return that value (where '@' is (almost) any unary operator which 5545796c8dcSSimon Schubert is legal for GNU C++). */ 5555796c8dcSSimon Schubert 5565796c8dcSSimon Schubert struct value * 5575796c8dcSSimon Schubert value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside) 5585796c8dcSSimon Schubert { 5595796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (value_type (arg1)); 5605796c8dcSSimon Schubert struct value **argvec; 5615796c8dcSSimon Schubert char *ptr, *mangle_ptr; 5625796c8dcSSimon Schubert char tstr[13], mangle_tstr[13]; 5635796c8dcSSimon Schubert int static_memfuncp, nargs; 5645796c8dcSSimon Schubert 5655796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 5665796c8dcSSimon Schubert 5675796c8dcSSimon Schubert /* now we know that what we have to do is construct our 5685796c8dcSSimon Schubert arg vector and find the right function to call it with. */ 5695796c8dcSSimon Schubert 5705796c8dcSSimon Schubert if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) 5715796c8dcSSimon Schubert error (_("Can't do that unary op on that type")); /* FIXME be explicit */ 5725796c8dcSSimon Schubert 5735796c8dcSSimon Schubert argvec = (struct value **) alloca (sizeof (struct value *) * 4); 5745796c8dcSSimon Schubert argvec[1] = value_addr (arg1); 5755796c8dcSSimon Schubert argvec[2] = 0; 5765796c8dcSSimon Schubert 5775796c8dcSSimon Schubert nargs = 1; 5785796c8dcSSimon Schubert 5795796c8dcSSimon Schubert /* make the right function name up */ 5805796c8dcSSimon Schubert strcpy (tstr, "operator__"); 5815796c8dcSSimon Schubert ptr = tstr + 8; 5825796c8dcSSimon Schubert strcpy (mangle_tstr, "__"); 5835796c8dcSSimon Schubert mangle_ptr = mangle_tstr + 2; 5845796c8dcSSimon Schubert switch (op) 5855796c8dcSSimon Schubert { 5865796c8dcSSimon Schubert case UNOP_PREINCREMENT: 5875796c8dcSSimon Schubert strcpy (ptr, "++"); 5885796c8dcSSimon Schubert break; 5895796c8dcSSimon Schubert case UNOP_PREDECREMENT: 5905796c8dcSSimon Schubert strcpy (ptr, "--"); 5915796c8dcSSimon Schubert break; 5925796c8dcSSimon Schubert case UNOP_POSTINCREMENT: 5935796c8dcSSimon Schubert strcpy (ptr, "++"); 5945796c8dcSSimon Schubert argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); 5955796c8dcSSimon Schubert argvec[3] = 0; 5965796c8dcSSimon Schubert nargs ++; 5975796c8dcSSimon Schubert break; 5985796c8dcSSimon Schubert case UNOP_POSTDECREMENT: 5995796c8dcSSimon Schubert strcpy (ptr, "--"); 6005796c8dcSSimon Schubert argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); 6015796c8dcSSimon Schubert argvec[3] = 0; 6025796c8dcSSimon Schubert nargs ++; 6035796c8dcSSimon Schubert break; 6045796c8dcSSimon Schubert case UNOP_LOGICAL_NOT: 6055796c8dcSSimon Schubert strcpy (ptr, "!"); 6065796c8dcSSimon Schubert break; 6075796c8dcSSimon Schubert case UNOP_COMPLEMENT: 6085796c8dcSSimon Schubert strcpy (ptr, "~"); 6095796c8dcSSimon Schubert break; 6105796c8dcSSimon Schubert case UNOP_NEG: 6115796c8dcSSimon Schubert strcpy (ptr, "-"); 6125796c8dcSSimon Schubert break; 6135796c8dcSSimon Schubert case UNOP_PLUS: 6145796c8dcSSimon Schubert strcpy (ptr, "+"); 6155796c8dcSSimon Schubert break; 6165796c8dcSSimon Schubert case UNOP_IND: 6175796c8dcSSimon Schubert strcpy (ptr, "*"); 6185796c8dcSSimon Schubert break; 6195796c8dcSSimon Schubert default: 6205796c8dcSSimon Schubert error (_("Invalid unary operation specified.")); 6215796c8dcSSimon Schubert } 6225796c8dcSSimon Schubert 623*cf7f2e2dSJohn Marino argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr, 624*cf7f2e2dSJohn Marino &static_memfuncp, nargs); 6255796c8dcSSimon Schubert 6265796c8dcSSimon Schubert if (argvec[0]) 6275796c8dcSSimon Schubert { 6285796c8dcSSimon Schubert if (static_memfuncp) 6295796c8dcSSimon Schubert { 6305796c8dcSSimon Schubert argvec[1] = argvec[0]; 6315796c8dcSSimon Schubert nargs --; 6325796c8dcSSimon Schubert argvec++; 6335796c8dcSSimon Schubert } 6345796c8dcSSimon Schubert if (noside == EVAL_AVOID_SIDE_EFFECTS) 6355796c8dcSSimon Schubert { 6365796c8dcSSimon Schubert struct type *return_type; 637*cf7f2e2dSJohn Marino 6385796c8dcSSimon Schubert return_type 6395796c8dcSSimon Schubert = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); 6405796c8dcSSimon Schubert return value_zero (return_type, VALUE_LVAL (arg1)); 6415796c8dcSSimon Schubert } 6425796c8dcSSimon Schubert return call_function_by_hand (argvec[0], nargs, argvec + 1); 6435796c8dcSSimon Schubert } 6445796c8dcSSimon Schubert error (_("member function %s not found"), tstr); 6455796c8dcSSimon Schubert return 0; /* For lint -- never reached */ 6465796c8dcSSimon Schubert } 6475796c8dcSSimon Schubert 6485796c8dcSSimon Schubert 6495796c8dcSSimon Schubert /* Concatenate two values with the following conditions: 6505796c8dcSSimon Schubert 6515796c8dcSSimon Schubert (1) Both values must be either bitstring values or character string 6525796c8dcSSimon Schubert values and the resulting value consists of the concatenation of 6535796c8dcSSimon Schubert ARG1 followed by ARG2. 6545796c8dcSSimon Schubert 6555796c8dcSSimon Schubert or 6565796c8dcSSimon Schubert 6575796c8dcSSimon Schubert One value must be an integer value and the other value must be 6585796c8dcSSimon Schubert either a bitstring value or character string value, which is 6595796c8dcSSimon Schubert to be repeated by the number of times specified by the integer 6605796c8dcSSimon Schubert value. 6615796c8dcSSimon Schubert 6625796c8dcSSimon Schubert 6635796c8dcSSimon Schubert (2) Boolean values are also allowed and are treated as bit string 6645796c8dcSSimon Schubert values of length 1. 6655796c8dcSSimon Schubert 6665796c8dcSSimon Schubert (3) Character values are also allowed and are treated as character 6675796c8dcSSimon Schubert string values of length 1. 6685796c8dcSSimon Schubert */ 6695796c8dcSSimon Schubert 6705796c8dcSSimon Schubert struct value * 6715796c8dcSSimon Schubert value_concat (struct value *arg1, struct value *arg2) 6725796c8dcSSimon Schubert { 6735796c8dcSSimon Schubert struct value *inval1; 6745796c8dcSSimon Schubert struct value *inval2; 6755796c8dcSSimon Schubert struct value *outval = NULL; 6765796c8dcSSimon Schubert int inval1len, inval2len; 6775796c8dcSSimon Schubert int count, idx; 6785796c8dcSSimon Schubert char *ptr; 6795796c8dcSSimon Schubert char inchar; 6805796c8dcSSimon Schubert struct type *type1 = check_typedef (value_type (arg1)); 6815796c8dcSSimon Schubert struct type *type2 = check_typedef (value_type (arg2)); 6825796c8dcSSimon Schubert struct type *char_type; 6835796c8dcSSimon Schubert 6845796c8dcSSimon Schubert /* First figure out if we are dealing with two values to be concatenated 6855796c8dcSSimon Schubert or a repeat count and a value to be repeated. INVAL1 is set to the 6865796c8dcSSimon Schubert first of two concatenated values, or the repeat count. INVAL2 is set 6875796c8dcSSimon Schubert to the second of the two concatenated values or the value to be 6885796c8dcSSimon Schubert repeated. */ 6895796c8dcSSimon Schubert 6905796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_INT) 6915796c8dcSSimon Schubert { 6925796c8dcSSimon Schubert struct type *tmp = type1; 693*cf7f2e2dSJohn Marino 6945796c8dcSSimon Schubert type1 = tmp; 6955796c8dcSSimon Schubert tmp = type2; 6965796c8dcSSimon Schubert inval1 = arg2; 6975796c8dcSSimon Schubert inval2 = arg1; 6985796c8dcSSimon Schubert } 6995796c8dcSSimon Schubert else 7005796c8dcSSimon Schubert { 7015796c8dcSSimon Schubert inval1 = arg1; 7025796c8dcSSimon Schubert inval2 = arg2; 7035796c8dcSSimon Schubert } 7045796c8dcSSimon Schubert 7055796c8dcSSimon Schubert /* Now process the input values. */ 7065796c8dcSSimon Schubert 7075796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_INT) 7085796c8dcSSimon Schubert { 7095796c8dcSSimon Schubert /* We have a repeat count. Validate the second value and then 7105796c8dcSSimon Schubert construct a value repeated that many times. */ 7115796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_STRING 7125796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_CHAR) 7135796c8dcSSimon Schubert { 7145796c8dcSSimon Schubert count = longest_to_int (value_as_long (inval1)); 7155796c8dcSSimon Schubert inval2len = TYPE_LENGTH (type2); 7165796c8dcSSimon Schubert ptr = (char *) alloca (count * inval2len); 7175796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_CHAR) 7185796c8dcSSimon Schubert { 7195796c8dcSSimon Schubert char_type = type2; 720*cf7f2e2dSJohn Marino 7215796c8dcSSimon Schubert inchar = (char) unpack_long (type2, 7225796c8dcSSimon Schubert value_contents (inval2)); 7235796c8dcSSimon Schubert for (idx = 0; idx < count; idx++) 7245796c8dcSSimon Schubert { 7255796c8dcSSimon Schubert *(ptr + idx) = inchar; 7265796c8dcSSimon Schubert } 7275796c8dcSSimon Schubert } 7285796c8dcSSimon Schubert else 7295796c8dcSSimon Schubert { 7305796c8dcSSimon Schubert char_type = TYPE_TARGET_TYPE (type2); 731*cf7f2e2dSJohn Marino 7325796c8dcSSimon Schubert for (idx = 0; idx < count; idx++) 7335796c8dcSSimon Schubert { 7345796c8dcSSimon Schubert memcpy (ptr + (idx * inval2len), value_contents (inval2), 7355796c8dcSSimon Schubert inval2len); 7365796c8dcSSimon Schubert } 7375796c8dcSSimon Schubert } 7385796c8dcSSimon Schubert outval = value_string (ptr, count * inval2len, char_type); 7395796c8dcSSimon Schubert } 7405796c8dcSSimon Schubert else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING 7415796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_BOOL) 7425796c8dcSSimon Schubert { 7435796c8dcSSimon Schubert error (_("unimplemented support for bitstring/boolean repeats")); 7445796c8dcSSimon Schubert } 7455796c8dcSSimon Schubert else 7465796c8dcSSimon Schubert { 7475796c8dcSSimon Schubert error (_("can't repeat values of that type")); 7485796c8dcSSimon Schubert } 7495796c8dcSSimon Schubert } 7505796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_STRING 7515796c8dcSSimon Schubert || TYPE_CODE (type1) == TYPE_CODE_CHAR) 7525796c8dcSSimon Schubert { 7535796c8dcSSimon Schubert /* We have two character strings to concatenate. */ 7545796c8dcSSimon Schubert if (TYPE_CODE (type2) != TYPE_CODE_STRING 7555796c8dcSSimon Schubert && TYPE_CODE (type2) != TYPE_CODE_CHAR) 7565796c8dcSSimon Schubert { 7575796c8dcSSimon Schubert error (_("Strings can only be concatenated with other strings.")); 7585796c8dcSSimon Schubert } 7595796c8dcSSimon Schubert inval1len = TYPE_LENGTH (type1); 7605796c8dcSSimon Schubert inval2len = TYPE_LENGTH (type2); 7615796c8dcSSimon Schubert ptr = (char *) alloca (inval1len + inval2len); 7625796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_CHAR) 7635796c8dcSSimon Schubert { 7645796c8dcSSimon Schubert char_type = type1; 765*cf7f2e2dSJohn Marino 7665796c8dcSSimon Schubert *ptr = (char) unpack_long (type1, value_contents (inval1)); 7675796c8dcSSimon Schubert } 7685796c8dcSSimon Schubert else 7695796c8dcSSimon Schubert { 7705796c8dcSSimon Schubert char_type = TYPE_TARGET_TYPE (type1); 771*cf7f2e2dSJohn Marino 7725796c8dcSSimon Schubert memcpy (ptr, value_contents (inval1), inval1len); 7735796c8dcSSimon Schubert } 7745796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_CHAR) 7755796c8dcSSimon Schubert { 7765796c8dcSSimon Schubert *(ptr + inval1len) = 7775796c8dcSSimon Schubert (char) unpack_long (type2, value_contents (inval2)); 7785796c8dcSSimon Schubert } 7795796c8dcSSimon Schubert else 7805796c8dcSSimon Schubert { 7815796c8dcSSimon Schubert memcpy (ptr + inval1len, value_contents (inval2), inval2len); 7825796c8dcSSimon Schubert } 7835796c8dcSSimon Schubert outval = value_string (ptr, inval1len + inval2len, char_type); 7845796c8dcSSimon Schubert } 7855796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING 7865796c8dcSSimon Schubert || TYPE_CODE (type1) == TYPE_CODE_BOOL) 7875796c8dcSSimon Schubert { 7885796c8dcSSimon Schubert /* We have two bitstrings to concatenate. */ 7895796c8dcSSimon Schubert if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING 7905796c8dcSSimon Schubert && TYPE_CODE (type2) != TYPE_CODE_BOOL) 7915796c8dcSSimon Schubert { 7925796c8dcSSimon Schubert error (_("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.")); 7935796c8dcSSimon Schubert } 7945796c8dcSSimon Schubert error (_("unimplemented support for bitstring/boolean concatenation.")); 7955796c8dcSSimon Schubert } 7965796c8dcSSimon Schubert else 7975796c8dcSSimon Schubert { 7985796c8dcSSimon Schubert /* We don't know how to concatenate these operands. */ 7995796c8dcSSimon Schubert error (_("illegal operands for concatenation.")); 8005796c8dcSSimon Schubert } 8015796c8dcSSimon Schubert return (outval); 8025796c8dcSSimon Schubert } 8035796c8dcSSimon Schubert 8045796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are 8055796c8dcSSimon Schubert integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ 8065796c8dcSSimon Schubert static LONGEST 8075796c8dcSSimon Schubert integer_pow (LONGEST v1, LONGEST v2) 8085796c8dcSSimon Schubert { 8095796c8dcSSimon Schubert if (v2 < 0) 8105796c8dcSSimon Schubert { 8115796c8dcSSimon Schubert if (v1 == 0) 8125796c8dcSSimon Schubert error (_("Attempt to raise 0 to negative power.")); 8135796c8dcSSimon Schubert else 8145796c8dcSSimon Schubert return 0; 8155796c8dcSSimon Schubert } 8165796c8dcSSimon Schubert else 8175796c8dcSSimon Schubert { 8185796c8dcSSimon Schubert /* The Russian Peasant's Algorithm */ 8195796c8dcSSimon Schubert LONGEST v; 8205796c8dcSSimon Schubert 8215796c8dcSSimon Schubert v = 1; 8225796c8dcSSimon Schubert for (;;) 8235796c8dcSSimon Schubert { 8245796c8dcSSimon Schubert if (v2 & 1L) 8255796c8dcSSimon Schubert v *= v1; 8265796c8dcSSimon Schubert v2 >>= 1; 8275796c8dcSSimon Schubert if (v2 == 0) 8285796c8dcSSimon Schubert return v; 8295796c8dcSSimon Schubert v1 *= v1; 8305796c8dcSSimon Schubert } 8315796c8dcSSimon Schubert } 8325796c8dcSSimon Schubert } 8335796c8dcSSimon Schubert 8345796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are 8355796c8dcSSimon Schubert integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ 8365796c8dcSSimon Schubert static ULONGEST 8375796c8dcSSimon Schubert uinteger_pow (ULONGEST v1, LONGEST v2) 8385796c8dcSSimon Schubert { 8395796c8dcSSimon Schubert if (v2 < 0) 8405796c8dcSSimon Schubert { 8415796c8dcSSimon Schubert if (v1 == 0) 8425796c8dcSSimon Schubert error (_("Attempt to raise 0 to negative power.")); 8435796c8dcSSimon Schubert else 8445796c8dcSSimon Schubert return 0; 8455796c8dcSSimon Schubert } 8465796c8dcSSimon Schubert else 8475796c8dcSSimon Schubert { 8485796c8dcSSimon Schubert /* The Russian Peasant's Algorithm */ 8495796c8dcSSimon Schubert ULONGEST v; 8505796c8dcSSimon Schubert 8515796c8dcSSimon Schubert v = 1; 8525796c8dcSSimon Schubert for (;;) 8535796c8dcSSimon Schubert { 8545796c8dcSSimon Schubert if (v2 & 1L) 8555796c8dcSSimon Schubert v *= v1; 8565796c8dcSSimon Schubert v2 >>= 1; 8575796c8dcSSimon Schubert if (v2 == 0) 8585796c8dcSSimon Schubert return v; 8595796c8dcSSimon Schubert v1 *= v1; 8605796c8dcSSimon Schubert } 8615796c8dcSSimon Schubert } 8625796c8dcSSimon Schubert } 8635796c8dcSSimon Schubert 8645796c8dcSSimon Schubert /* Obtain decimal value of arguments for binary operation, converting from 8655796c8dcSSimon Schubert other types if one of them is not decimal floating point. */ 8665796c8dcSSimon Schubert static void 8675796c8dcSSimon Schubert value_args_as_decimal (struct value *arg1, struct value *arg2, 8685796c8dcSSimon Schubert gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x, 8695796c8dcSSimon Schubert gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y) 8705796c8dcSSimon Schubert { 8715796c8dcSSimon Schubert struct type *type1, *type2; 8725796c8dcSSimon Schubert 8735796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 8745796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 8755796c8dcSSimon Schubert 8765796c8dcSSimon Schubert /* At least one of the arguments must be of decimal float type. */ 8775796c8dcSSimon Schubert gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT 8785796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT); 8795796c8dcSSimon Schubert 8805796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_FLT 8815796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_FLT) 8825796c8dcSSimon Schubert /* The DFP extension to the C language does not allow mixing of 8835796c8dcSSimon Schubert * decimal float types with other float types in expressions 8845796c8dcSSimon Schubert * (see WDTR 24732, page 12). */ 8855796c8dcSSimon Schubert error (_("Mixing decimal floating types with other floating types is not allowed.")); 8865796c8dcSSimon Schubert 8875796c8dcSSimon Schubert /* Obtain decimal value of arg1, converting from other types 8885796c8dcSSimon Schubert if necessary. */ 8895796c8dcSSimon Schubert 8905796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) 8915796c8dcSSimon Schubert { 8925796c8dcSSimon Schubert *byte_order_x = gdbarch_byte_order (get_type_arch (type1)); 8935796c8dcSSimon Schubert *len_x = TYPE_LENGTH (type1); 8945796c8dcSSimon Schubert memcpy (x, value_contents (arg1), *len_x); 8955796c8dcSSimon Schubert } 8965796c8dcSSimon Schubert else if (is_integral_type (type1)) 8975796c8dcSSimon Schubert { 8985796c8dcSSimon Schubert *byte_order_x = gdbarch_byte_order (get_type_arch (type2)); 8995796c8dcSSimon Schubert *len_x = TYPE_LENGTH (type2); 9005796c8dcSSimon Schubert decimal_from_integral (arg1, x, *len_x, *byte_order_x); 9015796c8dcSSimon Schubert } 9025796c8dcSSimon Schubert else 9035796c8dcSSimon Schubert error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), 9045796c8dcSSimon Schubert TYPE_NAME (type2)); 9055796c8dcSSimon Schubert 9065796c8dcSSimon Schubert /* Obtain decimal value of arg2, converting from other types 9075796c8dcSSimon Schubert if necessary. */ 9085796c8dcSSimon Schubert 9095796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) 9105796c8dcSSimon Schubert { 9115796c8dcSSimon Schubert *byte_order_y = gdbarch_byte_order (get_type_arch (type2)); 9125796c8dcSSimon Schubert *len_y = TYPE_LENGTH (type2); 9135796c8dcSSimon Schubert memcpy (y, value_contents (arg2), *len_y); 9145796c8dcSSimon Schubert } 9155796c8dcSSimon Schubert else if (is_integral_type (type2)) 9165796c8dcSSimon Schubert { 9175796c8dcSSimon Schubert *byte_order_y = gdbarch_byte_order (get_type_arch (type1)); 9185796c8dcSSimon Schubert *len_y = TYPE_LENGTH (type1); 9195796c8dcSSimon Schubert decimal_from_integral (arg2, y, *len_y, *byte_order_y); 9205796c8dcSSimon Schubert } 9215796c8dcSSimon Schubert else 9225796c8dcSSimon Schubert error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), 9235796c8dcSSimon Schubert TYPE_NAME (type2)); 9245796c8dcSSimon Schubert } 9255796c8dcSSimon Schubert 9265796c8dcSSimon Schubert /* Perform a binary operation on two operands which have reasonable 9275796c8dcSSimon Schubert representations as integers or floats. This includes booleans, 9285796c8dcSSimon Schubert characters, integers, or floats. 9295796c8dcSSimon Schubert Does not support addition and subtraction on pointers; 9305796c8dcSSimon Schubert use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */ 9315796c8dcSSimon Schubert 9325796c8dcSSimon Schubert struct value * 9335796c8dcSSimon Schubert value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) 9345796c8dcSSimon Schubert { 9355796c8dcSSimon Schubert struct value *val; 9365796c8dcSSimon Schubert struct type *type1, *type2, *result_type; 9375796c8dcSSimon Schubert 9385796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 9395796c8dcSSimon Schubert arg2 = coerce_ref (arg2); 9405796c8dcSSimon Schubert 9415796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 9425796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 9435796c8dcSSimon Schubert 9445796c8dcSSimon Schubert if ((TYPE_CODE (type1) != TYPE_CODE_FLT 9455796c8dcSSimon Schubert && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT 9465796c8dcSSimon Schubert && !is_integral_type (type1)) 9475796c8dcSSimon Schubert || (TYPE_CODE (type2) != TYPE_CODE_FLT 9485796c8dcSSimon Schubert && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT 9495796c8dcSSimon Schubert && !is_integral_type (type2))) 9505796c8dcSSimon Schubert error (_("Argument to arithmetic operation not a number or boolean.")); 9515796c8dcSSimon Schubert 9525796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT 9535796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) 9545796c8dcSSimon Schubert { 9555796c8dcSSimon Schubert int len_v1, len_v2, len_v; 9565796c8dcSSimon Schubert enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v; 9575796c8dcSSimon Schubert gdb_byte v1[16], v2[16]; 9585796c8dcSSimon Schubert gdb_byte v[16]; 9595796c8dcSSimon Schubert 9605796c8dcSSimon Schubert /* If only one type is decimal float, use its type. 9615796c8dcSSimon Schubert Otherwise use the bigger type. */ 9625796c8dcSSimon Schubert if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT) 9635796c8dcSSimon Schubert result_type = type2; 9645796c8dcSSimon Schubert else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT) 9655796c8dcSSimon Schubert result_type = type1; 9665796c8dcSSimon Schubert else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) 9675796c8dcSSimon Schubert result_type = type2; 9685796c8dcSSimon Schubert else 9695796c8dcSSimon Schubert result_type = type1; 9705796c8dcSSimon Schubert 9715796c8dcSSimon Schubert len_v = TYPE_LENGTH (result_type); 9725796c8dcSSimon Schubert byte_order_v = gdbarch_byte_order (get_type_arch (result_type)); 9735796c8dcSSimon Schubert 9745796c8dcSSimon Schubert value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, 9755796c8dcSSimon Schubert v2, &len_v2, &byte_order_v2); 9765796c8dcSSimon Schubert 9775796c8dcSSimon Schubert switch (op) 9785796c8dcSSimon Schubert { 9795796c8dcSSimon Schubert case BINOP_ADD: 9805796c8dcSSimon Schubert case BINOP_SUB: 9815796c8dcSSimon Schubert case BINOP_MUL: 9825796c8dcSSimon Schubert case BINOP_DIV: 9835796c8dcSSimon Schubert case BINOP_EXP: 9845796c8dcSSimon Schubert decimal_binop (op, v1, len_v1, byte_order_v1, 9855796c8dcSSimon Schubert v2, len_v2, byte_order_v2, 9865796c8dcSSimon Schubert v, len_v, byte_order_v); 9875796c8dcSSimon Schubert break; 9885796c8dcSSimon Schubert 9895796c8dcSSimon Schubert default: 9905796c8dcSSimon Schubert error (_("Operation not valid for decimal floating point number.")); 9915796c8dcSSimon Schubert } 9925796c8dcSSimon Schubert 9935796c8dcSSimon Schubert val = value_from_decfloat (result_type, v); 9945796c8dcSSimon Schubert } 9955796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_FLT 9965796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_FLT) 9975796c8dcSSimon Schubert { 9985796c8dcSSimon Schubert /* FIXME-if-picky-about-floating-accuracy: Should be doing this 9995796c8dcSSimon Schubert in target format. real.c in GCC probably has the necessary 10005796c8dcSSimon Schubert code. */ 10015796c8dcSSimon Schubert DOUBLEST v1, v2, v = 0; 1002*cf7f2e2dSJohn Marino 10035796c8dcSSimon Schubert v1 = value_as_double (arg1); 10045796c8dcSSimon Schubert v2 = value_as_double (arg2); 10055796c8dcSSimon Schubert 10065796c8dcSSimon Schubert switch (op) 10075796c8dcSSimon Schubert { 10085796c8dcSSimon Schubert case BINOP_ADD: 10095796c8dcSSimon Schubert v = v1 + v2; 10105796c8dcSSimon Schubert break; 10115796c8dcSSimon Schubert 10125796c8dcSSimon Schubert case BINOP_SUB: 10135796c8dcSSimon Schubert v = v1 - v2; 10145796c8dcSSimon Schubert break; 10155796c8dcSSimon Schubert 10165796c8dcSSimon Schubert case BINOP_MUL: 10175796c8dcSSimon Schubert v = v1 * v2; 10185796c8dcSSimon Schubert break; 10195796c8dcSSimon Schubert 10205796c8dcSSimon Schubert case BINOP_DIV: 10215796c8dcSSimon Schubert v = v1 / v2; 10225796c8dcSSimon Schubert break; 10235796c8dcSSimon Schubert 10245796c8dcSSimon Schubert case BINOP_EXP: 10255796c8dcSSimon Schubert errno = 0; 10265796c8dcSSimon Schubert v = pow (v1, v2); 10275796c8dcSSimon Schubert if (errno) 10285796c8dcSSimon Schubert error (_("Cannot perform exponentiation: %s"), safe_strerror (errno)); 10295796c8dcSSimon Schubert break; 10305796c8dcSSimon Schubert 10315796c8dcSSimon Schubert case BINOP_MIN: 10325796c8dcSSimon Schubert v = v1 < v2 ? v1 : v2; 10335796c8dcSSimon Schubert break; 10345796c8dcSSimon Schubert 10355796c8dcSSimon Schubert case BINOP_MAX: 10365796c8dcSSimon Schubert v = v1 > v2 ? v1 : v2; 10375796c8dcSSimon Schubert break; 10385796c8dcSSimon Schubert 10395796c8dcSSimon Schubert default: 10405796c8dcSSimon Schubert error (_("Integer-only operation on floating point number.")); 10415796c8dcSSimon Schubert } 10425796c8dcSSimon Schubert 10435796c8dcSSimon Schubert /* If only one type is float, use its type. 10445796c8dcSSimon Schubert Otherwise use the bigger type. */ 10455796c8dcSSimon Schubert if (TYPE_CODE (type1) != TYPE_CODE_FLT) 10465796c8dcSSimon Schubert result_type = type2; 10475796c8dcSSimon Schubert else if (TYPE_CODE (type2) != TYPE_CODE_FLT) 10485796c8dcSSimon Schubert result_type = type1; 10495796c8dcSSimon Schubert else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) 10505796c8dcSSimon Schubert result_type = type2; 10515796c8dcSSimon Schubert else 10525796c8dcSSimon Schubert result_type = type1; 10535796c8dcSSimon Schubert 10545796c8dcSSimon Schubert val = allocate_value (result_type); 10555796c8dcSSimon Schubert store_typed_floating (value_contents_raw (val), value_type (val), v); 10565796c8dcSSimon Schubert } 10575796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_BOOL 10585796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_BOOL) 10595796c8dcSSimon Schubert { 10605796c8dcSSimon Schubert LONGEST v1, v2, v = 0; 1061*cf7f2e2dSJohn Marino 10625796c8dcSSimon Schubert v1 = value_as_long (arg1); 10635796c8dcSSimon Schubert v2 = value_as_long (arg2); 10645796c8dcSSimon Schubert 10655796c8dcSSimon Schubert switch (op) 10665796c8dcSSimon Schubert { 10675796c8dcSSimon Schubert case BINOP_BITWISE_AND: 10685796c8dcSSimon Schubert v = v1 & v2; 10695796c8dcSSimon Schubert break; 10705796c8dcSSimon Schubert 10715796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 10725796c8dcSSimon Schubert v = v1 | v2; 10735796c8dcSSimon Schubert break; 10745796c8dcSSimon Schubert 10755796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 10765796c8dcSSimon Schubert v = v1 ^ v2; 10775796c8dcSSimon Schubert break; 10785796c8dcSSimon Schubert 10795796c8dcSSimon Schubert case BINOP_EQUAL: 10805796c8dcSSimon Schubert v = v1 == v2; 10815796c8dcSSimon Schubert break; 10825796c8dcSSimon Schubert 10835796c8dcSSimon Schubert case BINOP_NOTEQUAL: 10845796c8dcSSimon Schubert v = v1 != v2; 10855796c8dcSSimon Schubert break; 10865796c8dcSSimon Schubert 10875796c8dcSSimon Schubert default: 10885796c8dcSSimon Schubert error (_("Invalid operation on booleans.")); 10895796c8dcSSimon Schubert } 10905796c8dcSSimon Schubert 10915796c8dcSSimon Schubert result_type = type1; 10925796c8dcSSimon Schubert 10935796c8dcSSimon Schubert val = allocate_value (result_type); 10945796c8dcSSimon Schubert store_signed_integer (value_contents_raw (val), 10955796c8dcSSimon Schubert TYPE_LENGTH (result_type), 10965796c8dcSSimon Schubert gdbarch_byte_order (get_type_arch (result_type)), 10975796c8dcSSimon Schubert v); 10985796c8dcSSimon Schubert } 10995796c8dcSSimon Schubert else 11005796c8dcSSimon Schubert /* Integral operations here. */ 11015796c8dcSSimon Schubert { 11025796c8dcSSimon Schubert /* Determine type length of the result, and if the operation should 11035796c8dcSSimon Schubert be done unsigned. For exponentiation and shift operators, 11045796c8dcSSimon Schubert use the length and type of the left operand. Otherwise, 11055796c8dcSSimon Schubert use the signedness of the operand with the greater length. 11065796c8dcSSimon Schubert If both operands are of equal length, use unsigned operation 11075796c8dcSSimon Schubert if one of the operands is unsigned. */ 11085796c8dcSSimon Schubert if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP) 11095796c8dcSSimon Schubert result_type = type1; 11105796c8dcSSimon Schubert else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) 11115796c8dcSSimon Schubert result_type = type1; 11125796c8dcSSimon Schubert else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) 11135796c8dcSSimon Schubert result_type = type2; 11145796c8dcSSimon Schubert else if (TYPE_UNSIGNED (type1)) 11155796c8dcSSimon Schubert result_type = type1; 11165796c8dcSSimon Schubert else if (TYPE_UNSIGNED (type2)) 11175796c8dcSSimon Schubert result_type = type2; 11185796c8dcSSimon Schubert else 11195796c8dcSSimon Schubert result_type = type1; 11205796c8dcSSimon Schubert 11215796c8dcSSimon Schubert if (TYPE_UNSIGNED (result_type)) 11225796c8dcSSimon Schubert { 11235796c8dcSSimon Schubert LONGEST v2_signed = value_as_long (arg2); 11245796c8dcSSimon Schubert ULONGEST v1, v2, v = 0; 1125*cf7f2e2dSJohn Marino 11265796c8dcSSimon Schubert v1 = (ULONGEST) value_as_long (arg1); 11275796c8dcSSimon Schubert v2 = (ULONGEST) v2_signed; 11285796c8dcSSimon Schubert 11295796c8dcSSimon Schubert switch (op) 11305796c8dcSSimon Schubert { 11315796c8dcSSimon Schubert case BINOP_ADD: 11325796c8dcSSimon Schubert v = v1 + v2; 11335796c8dcSSimon Schubert break; 11345796c8dcSSimon Schubert 11355796c8dcSSimon Schubert case BINOP_SUB: 11365796c8dcSSimon Schubert v = v1 - v2; 11375796c8dcSSimon Schubert break; 11385796c8dcSSimon Schubert 11395796c8dcSSimon Schubert case BINOP_MUL: 11405796c8dcSSimon Schubert v = v1 * v2; 11415796c8dcSSimon Schubert break; 11425796c8dcSSimon Schubert 11435796c8dcSSimon Schubert case BINOP_DIV: 11445796c8dcSSimon Schubert case BINOP_INTDIV: 11455796c8dcSSimon Schubert if (v2 != 0) 11465796c8dcSSimon Schubert v = v1 / v2; 11475796c8dcSSimon Schubert else 11485796c8dcSSimon Schubert error (_("Division by zero")); 11495796c8dcSSimon Schubert break; 11505796c8dcSSimon Schubert 11515796c8dcSSimon Schubert case BINOP_EXP: 11525796c8dcSSimon Schubert v = uinteger_pow (v1, v2_signed); 11535796c8dcSSimon Schubert break; 11545796c8dcSSimon Schubert 11555796c8dcSSimon Schubert case BINOP_REM: 11565796c8dcSSimon Schubert if (v2 != 0) 11575796c8dcSSimon Schubert v = v1 % v2; 11585796c8dcSSimon Schubert else 11595796c8dcSSimon Schubert error (_("Division by zero")); 11605796c8dcSSimon Schubert break; 11615796c8dcSSimon Schubert 11625796c8dcSSimon Schubert case BINOP_MOD: 11635796c8dcSSimon Schubert /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, 11645796c8dcSSimon Schubert v1 mod 0 has a defined value, v1. */ 11655796c8dcSSimon Schubert if (v2 == 0) 11665796c8dcSSimon Schubert { 11675796c8dcSSimon Schubert v = v1; 11685796c8dcSSimon Schubert } 11695796c8dcSSimon Schubert else 11705796c8dcSSimon Schubert { 11715796c8dcSSimon Schubert v = v1 / v2; 11725796c8dcSSimon Schubert /* Note floor(v1/v2) == v1/v2 for unsigned. */ 11735796c8dcSSimon Schubert v = v1 - (v2 * v); 11745796c8dcSSimon Schubert } 11755796c8dcSSimon Schubert break; 11765796c8dcSSimon Schubert 11775796c8dcSSimon Schubert case BINOP_LSH: 11785796c8dcSSimon Schubert v = v1 << v2; 11795796c8dcSSimon Schubert break; 11805796c8dcSSimon Schubert 11815796c8dcSSimon Schubert case BINOP_RSH: 11825796c8dcSSimon Schubert v = v1 >> v2; 11835796c8dcSSimon Schubert break; 11845796c8dcSSimon Schubert 11855796c8dcSSimon Schubert case BINOP_BITWISE_AND: 11865796c8dcSSimon Schubert v = v1 & v2; 11875796c8dcSSimon Schubert break; 11885796c8dcSSimon Schubert 11895796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 11905796c8dcSSimon Schubert v = v1 | v2; 11915796c8dcSSimon Schubert break; 11925796c8dcSSimon Schubert 11935796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 11945796c8dcSSimon Schubert v = v1 ^ v2; 11955796c8dcSSimon Schubert break; 11965796c8dcSSimon Schubert 11975796c8dcSSimon Schubert case BINOP_LOGICAL_AND: 11985796c8dcSSimon Schubert v = v1 && v2; 11995796c8dcSSimon Schubert break; 12005796c8dcSSimon Schubert 12015796c8dcSSimon Schubert case BINOP_LOGICAL_OR: 12025796c8dcSSimon Schubert v = v1 || v2; 12035796c8dcSSimon Schubert break; 12045796c8dcSSimon Schubert 12055796c8dcSSimon Schubert case BINOP_MIN: 12065796c8dcSSimon Schubert v = v1 < v2 ? v1 : v2; 12075796c8dcSSimon Schubert break; 12085796c8dcSSimon Schubert 12095796c8dcSSimon Schubert case BINOP_MAX: 12105796c8dcSSimon Schubert v = v1 > v2 ? v1 : v2; 12115796c8dcSSimon Schubert break; 12125796c8dcSSimon Schubert 12135796c8dcSSimon Schubert case BINOP_EQUAL: 12145796c8dcSSimon Schubert v = v1 == v2; 12155796c8dcSSimon Schubert break; 12165796c8dcSSimon Schubert 12175796c8dcSSimon Schubert case BINOP_NOTEQUAL: 12185796c8dcSSimon Schubert v = v1 != v2; 12195796c8dcSSimon Schubert break; 12205796c8dcSSimon Schubert 12215796c8dcSSimon Schubert case BINOP_LESS: 12225796c8dcSSimon Schubert v = v1 < v2; 12235796c8dcSSimon Schubert break; 12245796c8dcSSimon Schubert 1225*cf7f2e2dSJohn Marino case BINOP_GTR: 1226*cf7f2e2dSJohn Marino v = v1 > v2; 1227*cf7f2e2dSJohn Marino break; 1228*cf7f2e2dSJohn Marino 1229*cf7f2e2dSJohn Marino case BINOP_LEQ: 1230*cf7f2e2dSJohn Marino v = v1 <= v2; 1231*cf7f2e2dSJohn Marino break; 1232*cf7f2e2dSJohn Marino 1233*cf7f2e2dSJohn Marino case BINOP_GEQ: 1234*cf7f2e2dSJohn Marino v = v1 >= v2; 1235*cf7f2e2dSJohn Marino break; 1236*cf7f2e2dSJohn Marino 12375796c8dcSSimon Schubert default: 12385796c8dcSSimon Schubert error (_("Invalid binary operation on numbers.")); 12395796c8dcSSimon Schubert } 12405796c8dcSSimon Schubert 12415796c8dcSSimon Schubert val = allocate_value (result_type); 12425796c8dcSSimon Schubert store_unsigned_integer (value_contents_raw (val), 12435796c8dcSSimon Schubert TYPE_LENGTH (value_type (val)), 12445796c8dcSSimon Schubert gdbarch_byte_order 12455796c8dcSSimon Schubert (get_type_arch (result_type)), 12465796c8dcSSimon Schubert v); 12475796c8dcSSimon Schubert } 12485796c8dcSSimon Schubert else 12495796c8dcSSimon Schubert { 12505796c8dcSSimon Schubert LONGEST v1, v2, v = 0; 1251*cf7f2e2dSJohn Marino 12525796c8dcSSimon Schubert v1 = value_as_long (arg1); 12535796c8dcSSimon Schubert v2 = value_as_long (arg2); 12545796c8dcSSimon Schubert 12555796c8dcSSimon Schubert switch (op) 12565796c8dcSSimon Schubert { 12575796c8dcSSimon Schubert case BINOP_ADD: 12585796c8dcSSimon Schubert v = v1 + v2; 12595796c8dcSSimon Schubert break; 12605796c8dcSSimon Schubert 12615796c8dcSSimon Schubert case BINOP_SUB: 12625796c8dcSSimon Schubert v = v1 - v2; 12635796c8dcSSimon Schubert break; 12645796c8dcSSimon Schubert 12655796c8dcSSimon Schubert case BINOP_MUL: 12665796c8dcSSimon Schubert v = v1 * v2; 12675796c8dcSSimon Schubert break; 12685796c8dcSSimon Schubert 12695796c8dcSSimon Schubert case BINOP_DIV: 12705796c8dcSSimon Schubert case BINOP_INTDIV: 12715796c8dcSSimon Schubert if (v2 != 0) 12725796c8dcSSimon Schubert v = v1 / v2; 12735796c8dcSSimon Schubert else 12745796c8dcSSimon Schubert error (_("Division by zero")); 12755796c8dcSSimon Schubert break; 12765796c8dcSSimon Schubert 12775796c8dcSSimon Schubert case BINOP_EXP: 12785796c8dcSSimon Schubert v = integer_pow (v1, v2); 12795796c8dcSSimon Schubert break; 12805796c8dcSSimon Schubert 12815796c8dcSSimon Schubert case BINOP_REM: 12825796c8dcSSimon Schubert if (v2 != 0) 12835796c8dcSSimon Schubert v = v1 % v2; 12845796c8dcSSimon Schubert else 12855796c8dcSSimon Schubert error (_("Division by zero")); 12865796c8dcSSimon Schubert break; 12875796c8dcSSimon Schubert 12885796c8dcSSimon Schubert case BINOP_MOD: 12895796c8dcSSimon Schubert /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, 12905796c8dcSSimon Schubert X mod 0 has a defined value, X. */ 12915796c8dcSSimon Schubert if (v2 == 0) 12925796c8dcSSimon Schubert { 12935796c8dcSSimon Schubert v = v1; 12945796c8dcSSimon Schubert } 12955796c8dcSSimon Schubert else 12965796c8dcSSimon Schubert { 12975796c8dcSSimon Schubert v = v1 / v2; 12985796c8dcSSimon Schubert /* Compute floor. */ 12995796c8dcSSimon Schubert if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) 13005796c8dcSSimon Schubert { 13015796c8dcSSimon Schubert v--; 13025796c8dcSSimon Schubert } 13035796c8dcSSimon Schubert v = v1 - (v2 * v); 13045796c8dcSSimon Schubert } 13055796c8dcSSimon Schubert break; 13065796c8dcSSimon Schubert 13075796c8dcSSimon Schubert case BINOP_LSH: 13085796c8dcSSimon Schubert v = v1 << v2; 13095796c8dcSSimon Schubert break; 13105796c8dcSSimon Schubert 13115796c8dcSSimon Schubert case BINOP_RSH: 13125796c8dcSSimon Schubert v = v1 >> v2; 13135796c8dcSSimon Schubert break; 13145796c8dcSSimon Schubert 13155796c8dcSSimon Schubert case BINOP_BITWISE_AND: 13165796c8dcSSimon Schubert v = v1 & v2; 13175796c8dcSSimon Schubert break; 13185796c8dcSSimon Schubert 13195796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 13205796c8dcSSimon Schubert v = v1 | v2; 13215796c8dcSSimon Schubert break; 13225796c8dcSSimon Schubert 13235796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 13245796c8dcSSimon Schubert v = v1 ^ v2; 13255796c8dcSSimon Schubert break; 13265796c8dcSSimon Schubert 13275796c8dcSSimon Schubert case BINOP_LOGICAL_AND: 13285796c8dcSSimon Schubert v = v1 && v2; 13295796c8dcSSimon Schubert break; 13305796c8dcSSimon Schubert 13315796c8dcSSimon Schubert case BINOP_LOGICAL_OR: 13325796c8dcSSimon Schubert v = v1 || v2; 13335796c8dcSSimon Schubert break; 13345796c8dcSSimon Schubert 13355796c8dcSSimon Schubert case BINOP_MIN: 13365796c8dcSSimon Schubert v = v1 < v2 ? v1 : v2; 13375796c8dcSSimon Schubert break; 13385796c8dcSSimon Schubert 13395796c8dcSSimon Schubert case BINOP_MAX: 13405796c8dcSSimon Schubert v = v1 > v2 ? v1 : v2; 13415796c8dcSSimon Schubert break; 13425796c8dcSSimon Schubert 13435796c8dcSSimon Schubert case BINOP_EQUAL: 13445796c8dcSSimon Schubert v = v1 == v2; 13455796c8dcSSimon Schubert break; 13465796c8dcSSimon Schubert 1347*cf7f2e2dSJohn Marino case BINOP_NOTEQUAL: 1348*cf7f2e2dSJohn Marino v = v1 != v2; 1349*cf7f2e2dSJohn Marino break; 1350*cf7f2e2dSJohn Marino 13515796c8dcSSimon Schubert case BINOP_LESS: 13525796c8dcSSimon Schubert v = v1 < v2; 13535796c8dcSSimon Schubert break; 13545796c8dcSSimon Schubert 1355*cf7f2e2dSJohn Marino case BINOP_GTR: 1356*cf7f2e2dSJohn Marino v = v1 > v2; 1357*cf7f2e2dSJohn Marino break; 1358*cf7f2e2dSJohn Marino 1359*cf7f2e2dSJohn Marino case BINOP_LEQ: 1360*cf7f2e2dSJohn Marino v = v1 <= v2; 1361*cf7f2e2dSJohn Marino break; 1362*cf7f2e2dSJohn Marino 1363*cf7f2e2dSJohn Marino case BINOP_GEQ: 1364*cf7f2e2dSJohn Marino v = v1 >= v2; 1365*cf7f2e2dSJohn Marino break; 1366*cf7f2e2dSJohn Marino 13675796c8dcSSimon Schubert default: 13685796c8dcSSimon Schubert error (_("Invalid binary operation on numbers.")); 13695796c8dcSSimon Schubert } 13705796c8dcSSimon Schubert 13715796c8dcSSimon Schubert val = allocate_value (result_type); 13725796c8dcSSimon Schubert store_signed_integer (value_contents_raw (val), 13735796c8dcSSimon Schubert TYPE_LENGTH (value_type (val)), 13745796c8dcSSimon Schubert gdbarch_byte_order 13755796c8dcSSimon Schubert (get_type_arch (result_type)), 13765796c8dcSSimon Schubert v); 13775796c8dcSSimon Schubert } 13785796c8dcSSimon Schubert } 13795796c8dcSSimon Schubert 13805796c8dcSSimon Schubert return val; 13815796c8dcSSimon Schubert } 13825796c8dcSSimon Schubert 13835796c8dcSSimon Schubert /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ 13845796c8dcSSimon Schubert 13855796c8dcSSimon Schubert int 13865796c8dcSSimon Schubert value_logical_not (struct value *arg1) 13875796c8dcSSimon Schubert { 13885796c8dcSSimon Schubert int len; 13895796c8dcSSimon Schubert const gdb_byte *p; 13905796c8dcSSimon Schubert struct type *type1; 13915796c8dcSSimon Schubert 13925796c8dcSSimon Schubert arg1 = coerce_array (arg1); 13935796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 13945796c8dcSSimon Schubert 13955796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_FLT) 13965796c8dcSSimon Schubert return 0 == value_as_double (arg1); 13975796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) 13985796c8dcSSimon Schubert return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1), 13995796c8dcSSimon Schubert gdbarch_byte_order (get_type_arch (type1))); 14005796c8dcSSimon Schubert 14015796c8dcSSimon Schubert len = TYPE_LENGTH (type1); 14025796c8dcSSimon Schubert p = value_contents (arg1); 14035796c8dcSSimon Schubert 14045796c8dcSSimon Schubert while (--len >= 0) 14055796c8dcSSimon Schubert { 14065796c8dcSSimon Schubert if (*p++) 14075796c8dcSSimon Schubert break; 14085796c8dcSSimon Schubert } 14095796c8dcSSimon Schubert 14105796c8dcSSimon Schubert return len < 0; 14115796c8dcSSimon Schubert } 14125796c8dcSSimon Schubert 14135796c8dcSSimon Schubert /* Perform a comparison on two string values (whose content are not 14145796c8dcSSimon Schubert necessarily null terminated) based on their length */ 14155796c8dcSSimon Schubert 14165796c8dcSSimon Schubert static int 14175796c8dcSSimon Schubert value_strcmp (struct value *arg1, struct value *arg2) 14185796c8dcSSimon Schubert { 14195796c8dcSSimon Schubert int len1 = TYPE_LENGTH (value_type (arg1)); 14205796c8dcSSimon Schubert int len2 = TYPE_LENGTH (value_type (arg2)); 14215796c8dcSSimon Schubert const gdb_byte *s1 = value_contents (arg1); 14225796c8dcSSimon Schubert const gdb_byte *s2 = value_contents (arg2); 14235796c8dcSSimon Schubert int i, len = len1 < len2 ? len1 : len2; 14245796c8dcSSimon Schubert 14255796c8dcSSimon Schubert for (i = 0; i < len; i++) 14265796c8dcSSimon Schubert { 14275796c8dcSSimon Schubert if (s1[i] < s2[i]) 14285796c8dcSSimon Schubert return -1; 14295796c8dcSSimon Schubert else if (s1[i] > s2[i]) 14305796c8dcSSimon Schubert return 1; 14315796c8dcSSimon Schubert else 14325796c8dcSSimon Schubert continue; 14335796c8dcSSimon Schubert } 14345796c8dcSSimon Schubert 14355796c8dcSSimon Schubert if (len1 < len2) 14365796c8dcSSimon Schubert return -1; 14375796c8dcSSimon Schubert else if (len1 > len2) 14385796c8dcSSimon Schubert return 1; 14395796c8dcSSimon Schubert else 14405796c8dcSSimon Schubert return 0; 14415796c8dcSSimon Schubert } 14425796c8dcSSimon Schubert 14435796c8dcSSimon Schubert /* Simulate the C operator == by returning a 1 14445796c8dcSSimon Schubert iff ARG1 and ARG2 have equal contents. */ 14455796c8dcSSimon Schubert 14465796c8dcSSimon Schubert int 14475796c8dcSSimon Schubert value_equal (struct value *arg1, struct value *arg2) 14485796c8dcSSimon Schubert { 14495796c8dcSSimon Schubert int len; 14505796c8dcSSimon Schubert const gdb_byte *p1; 14515796c8dcSSimon Schubert const gdb_byte *p2; 14525796c8dcSSimon Schubert struct type *type1, *type2; 14535796c8dcSSimon Schubert enum type_code code1; 14545796c8dcSSimon Schubert enum type_code code2; 14555796c8dcSSimon Schubert int is_int1, is_int2; 14565796c8dcSSimon Schubert 14575796c8dcSSimon Schubert arg1 = coerce_array (arg1); 14585796c8dcSSimon Schubert arg2 = coerce_array (arg2); 14595796c8dcSSimon Schubert 14605796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 14615796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 14625796c8dcSSimon Schubert code1 = TYPE_CODE (type1); 14635796c8dcSSimon Schubert code2 = TYPE_CODE (type2); 14645796c8dcSSimon Schubert is_int1 = is_integral_type (type1); 14655796c8dcSSimon Schubert is_int2 = is_integral_type (type2); 14665796c8dcSSimon Schubert 14675796c8dcSSimon Schubert if (is_int1 && is_int2) 14685796c8dcSSimon Schubert return longest_to_int (value_as_long (value_binop (arg1, arg2, 14695796c8dcSSimon Schubert BINOP_EQUAL))); 14705796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_FLT || is_int1) 14715796c8dcSSimon Schubert && (code2 == TYPE_CODE_FLT || is_int2)) 14725796c8dcSSimon Schubert { 14735796c8dcSSimon Schubert /* NOTE: kettenis/20050816: Avoid compiler bug on systems where 14745796c8dcSSimon Schubert `long double' values are returned in static storage (m68k). */ 14755796c8dcSSimon Schubert DOUBLEST d = value_as_double (arg1); 1476*cf7f2e2dSJohn Marino 14775796c8dcSSimon Schubert return d == value_as_double (arg2); 14785796c8dcSSimon Schubert } 14795796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) 14805796c8dcSSimon Schubert && (code2 == TYPE_CODE_DECFLOAT || is_int2)) 14815796c8dcSSimon Schubert { 14825796c8dcSSimon Schubert gdb_byte v1[16], v2[16]; 14835796c8dcSSimon Schubert int len_v1, len_v2; 14845796c8dcSSimon Schubert enum bfd_endian byte_order_v1, byte_order_v2; 14855796c8dcSSimon Schubert 14865796c8dcSSimon Schubert value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, 14875796c8dcSSimon Schubert v2, &len_v2, &byte_order_v2); 14885796c8dcSSimon Schubert 14895796c8dcSSimon Schubert return decimal_compare (v1, len_v1, byte_order_v1, 14905796c8dcSSimon Schubert v2, len_v2, byte_order_v2) == 0; 14915796c8dcSSimon Schubert } 14925796c8dcSSimon Schubert 14935796c8dcSSimon Schubert /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever 14945796c8dcSSimon Schubert is bigger. */ 14955796c8dcSSimon Schubert else if (code1 == TYPE_CODE_PTR && is_int2) 14965796c8dcSSimon Schubert return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2); 14975796c8dcSSimon Schubert else if (code2 == TYPE_CODE_PTR && is_int1) 14985796c8dcSSimon Schubert return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2); 14995796c8dcSSimon Schubert 15005796c8dcSSimon Schubert else if (code1 == code2 15015796c8dcSSimon Schubert && ((len = (int) TYPE_LENGTH (type1)) 15025796c8dcSSimon Schubert == (int) TYPE_LENGTH (type2))) 15035796c8dcSSimon Schubert { 15045796c8dcSSimon Schubert p1 = value_contents (arg1); 15055796c8dcSSimon Schubert p2 = value_contents (arg2); 15065796c8dcSSimon Schubert while (--len >= 0) 15075796c8dcSSimon Schubert { 15085796c8dcSSimon Schubert if (*p1++ != *p2++) 15095796c8dcSSimon Schubert break; 15105796c8dcSSimon Schubert } 15115796c8dcSSimon Schubert return len < 0; 15125796c8dcSSimon Schubert } 15135796c8dcSSimon Schubert else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) 15145796c8dcSSimon Schubert { 15155796c8dcSSimon Schubert return value_strcmp (arg1, arg2) == 0; 15165796c8dcSSimon Schubert } 15175796c8dcSSimon Schubert else 15185796c8dcSSimon Schubert { 15195796c8dcSSimon Schubert error (_("Invalid type combination in equality test.")); 15205796c8dcSSimon Schubert return 0; /* For lint -- never reached */ 15215796c8dcSSimon Schubert } 15225796c8dcSSimon Schubert } 15235796c8dcSSimon Schubert 1524*cf7f2e2dSJohn Marino /* Compare values based on their raw contents. Useful for arrays since 1525*cf7f2e2dSJohn Marino value_equal coerces them to pointers, thus comparing just the address 1526*cf7f2e2dSJohn Marino of the array instead of its contents. */ 1527*cf7f2e2dSJohn Marino 1528*cf7f2e2dSJohn Marino int 1529*cf7f2e2dSJohn Marino value_equal_contents (struct value *arg1, struct value *arg2) 1530*cf7f2e2dSJohn Marino { 1531*cf7f2e2dSJohn Marino struct type *type1, *type2; 1532*cf7f2e2dSJohn Marino 1533*cf7f2e2dSJohn Marino type1 = check_typedef (value_type (arg1)); 1534*cf7f2e2dSJohn Marino type2 = check_typedef (value_type (arg2)); 1535*cf7f2e2dSJohn Marino 1536*cf7f2e2dSJohn Marino return (TYPE_CODE (type1) == TYPE_CODE (type2) 1537*cf7f2e2dSJohn Marino && TYPE_LENGTH (type1) == TYPE_LENGTH (type2) 1538*cf7f2e2dSJohn Marino && memcmp (value_contents (arg1), value_contents (arg2), 1539*cf7f2e2dSJohn Marino TYPE_LENGTH (type1)) == 0); 1540*cf7f2e2dSJohn Marino } 1541*cf7f2e2dSJohn Marino 15425796c8dcSSimon Schubert /* Simulate the C operator < by returning 1 15435796c8dcSSimon Schubert iff ARG1's contents are less than ARG2's. */ 15445796c8dcSSimon Schubert 15455796c8dcSSimon Schubert int 15465796c8dcSSimon Schubert value_less (struct value *arg1, struct value *arg2) 15475796c8dcSSimon Schubert { 15485796c8dcSSimon Schubert enum type_code code1; 15495796c8dcSSimon Schubert enum type_code code2; 15505796c8dcSSimon Schubert struct type *type1, *type2; 15515796c8dcSSimon Schubert int is_int1, is_int2; 15525796c8dcSSimon Schubert 15535796c8dcSSimon Schubert arg1 = coerce_array (arg1); 15545796c8dcSSimon Schubert arg2 = coerce_array (arg2); 15555796c8dcSSimon Schubert 15565796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 15575796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 15585796c8dcSSimon Schubert code1 = TYPE_CODE (type1); 15595796c8dcSSimon Schubert code2 = TYPE_CODE (type2); 15605796c8dcSSimon Schubert is_int1 = is_integral_type (type1); 15615796c8dcSSimon Schubert is_int2 = is_integral_type (type2); 15625796c8dcSSimon Schubert 15635796c8dcSSimon Schubert if (is_int1 && is_int2) 15645796c8dcSSimon Schubert return longest_to_int (value_as_long (value_binop (arg1, arg2, 15655796c8dcSSimon Schubert BINOP_LESS))); 15665796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_FLT || is_int1) 15675796c8dcSSimon Schubert && (code2 == TYPE_CODE_FLT || is_int2)) 15685796c8dcSSimon Schubert { 15695796c8dcSSimon Schubert /* NOTE: kettenis/20050816: Avoid compiler bug on systems where 15705796c8dcSSimon Schubert `long double' values are returned in static storage (m68k). */ 15715796c8dcSSimon Schubert DOUBLEST d = value_as_double (arg1); 1572*cf7f2e2dSJohn Marino 15735796c8dcSSimon Schubert return d < value_as_double (arg2); 15745796c8dcSSimon Schubert } 15755796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) 15765796c8dcSSimon Schubert && (code2 == TYPE_CODE_DECFLOAT || is_int2)) 15775796c8dcSSimon Schubert { 15785796c8dcSSimon Schubert gdb_byte v1[16], v2[16]; 15795796c8dcSSimon Schubert int len_v1, len_v2; 15805796c8dcSSimon Schubert enum bfd_endian byte_order_v1, byte_order_v2; 15815796c8dcSSimon Schubert 15825796c8dcSSimon Schubert value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, 15835796c8dcSSimon Schubert v2, &len_v2, &byte_order_v2); 15845796c8dcSSimon Schubert 15855796c8dcSSimon Schubert return decimal_compare (v1, len_v1, byte_order_v1, 15865796c8dcSSimon Schubert v2, len_v2, byte_order_v2) == -1; 15875796c8dcSSimon Schubert } 15885796c8dcSSimon Schubert else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) 15895796c8dcSSimon Schubert return value_as_address (arg1) < value_as_address (arg2); 15905796c8dcSSimon Schubert 15915796c8dcSSimon Schubert /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever 15925796c8dcSSimon Schubert is bigger. */ 15935796c8dcSSimon Schubert else if (code1 == TYPE_CODE_PTR && is_int2) 15945796c8dcSSimon Schubert return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2); 15955796c8dcSSimon Schubert else if (code2 == TYPE_CODE_PTR && is_int1) 15965796c8dcSSimon Schubert return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2); 15975796c8dcSSimon Schubert else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) 15985796c8dcSSimon Schubert return value_strcmp (arg1, arg2) < 0; 15995796c8dcSSimon Schubert else 16005796c8dcSSimon Schubert { 16015796c8dcSSimon Schubert error (_("Invalid type combination in ordering comparison.")); 16025796c8dcSSimon Schubert return 0; 16035796c8dcSSimon Schubert } 16045796c8dcSSimon Schubert } 16055796c8dcSSimon Schubert 16065796c8dcSSimon Schubert /* The unary operators +, - and ~. They free the argument ARG1. */ 16075796c8dcSSimon Schubert 16085796c8dcSSimon Schubert struct value * 16095796c8dcSSimon Schubert value_pos (struct value *arg1) 16105796c8dcSSimon Schubert { 16115796c8dcSSimon Schubert struct type *type; 16125796c8dcSSimon Schubert 16135796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 16145796c8dcSSimon Schubert type = check_typedef (value_type (arg1)); 16155796c8dcSSimon Schubert 16165796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_FLT) 16175796c8dcSSimon Schubert return value_from_double (type, value_as_double (arg1)); 16185796c8dcSSimon Schubert else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) 16195796c8dcSSimon Schubert return value_from_decfloat (type, value_contents (arg1)); 16205796c8dcSSimon Schubert else if (is_integral_type (type)) 16215796c8dcSSimon Schubert { 16225796c8dcSSimon Schubert return value_from_longest (type, value_as_long (arg1)); 16235796c8dcSSimon Schubert } 16245796c8dcSSimon Schubert else 16255796c8dcSSimon Schubert { 16265796c8dcSSimon Schubert error ("Argument to positive operation not a number."); 16275796c8dcSSimon Schubert return 0; /* For lint -- never reached */ 16285796c8dcSSimon Schubert } 16295796c8dcSSimon Schubert } 16305796c8dcSSimon Schubert 16315796c8dcSSimon Schubert struct value * 16325796c8dcSSimon Schubert value_neg (struct value *arg1) 16335796c8dcSSimon Schubert { 16345796c8dcSSimon Schubert struct type *type; 16355796c8dcSSimon Schubert 16365796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 16375796c8dcSSimon Schubert type = check_typedef (value_type (arg1)); 16385796c8dcSSimon Schubert 16395796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) 16405796c8dcSSimon Schubert { 16415796c8dcSSimon Schubert struct value *val = allocate_value (type); 16425796c8dcSSimon Schubert int len = TYPE_LENGTH (type); 16435796c8dcSSimon Schubert gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long */ 16445796c8dcSSimon Schubert 16455796c8dcSSimon Schubert memcpy (decbytes, value_contents (arg1), len); 16465796c8dcSSimon Schubert 16475796c8dcSSimon Schubert if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE) 16485796c8dcSSimon Schubert decbytes[len-1] = decbytes[len - 1] | 0x80; 16495796c8dcSSimon Schubert else 16505796c8dcSSimon Schubert decbytes[0] = decbytes[0] | 0x80; 16515796c8dcSSimon Schubert 16525796c8dcSSimon Schubert memcpy (value_contents_raw (val), decbytes, len); 16535796c8dcSSimon Schubert return val; 16545796c8dcSSimon Schubert } 16555796c8dcSSimon Schubert else if (TYPE_CODE (type) == TYPE_CODE_FLT) 16565796c8dcSSimon Schubert return value_from_double (type, -value_as_double (arg1)); 16575796c8dcSSimon Schubert else if (is_integral_type (type)) 16585796c8dcSSimon Schubert { 16595796c8dcSSimon Schubert return value_from_longest (type, -value_as_long (arg1)); 16605796c8dcSSimon Schubert } 16615796c8dcSSimon Schubert else 16625796c8dcSSimon Schubert { 16635796c8dcSSimon Schubert error (_("Argument to negate operation not a number.")); 16645796c8dcSSimon Schubert return 0; /* For lint -- never reached */ 16655796c8dcSSimon Schubert } 16665796c8dcSSimon Schubert } 16675796c8dcSSimon Schubert 16685796c8dcSSimon Schubert struct value * 16695796c8dcSSimon Schubert value_complement (struct value *arg1) 16705796c8dcSSimon Schubert { 16715796c8dcSSimon Schubert struct type *type; 16725796c8dcSSimon Schubert 16735796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 16745796c8dcSSimon Schubert type = check_typedef (value_type (arg1)); 16755796c8dcSSimon Schubert 16765796c8dcSSimon Schubert if (!is_integral_type (type)) 16775796c8dcSSimon Schubert error (_("Argument to complement operation not an integer or boolean.")); 16785796c8dcSSimon Schubert 16795796c8dcSSimon Schubert return value_from_longest (type, ~value_as_long (arg1)); 16805796c8dcSSimon Schubert } 16815796c8dcSSimon Schubert 16825796c8dcSSimon Schubert /* The INDEX'th bit of SET value whose value_type is TYPE, 16835796c8dcSSimon Schubert and whose value_contents is valaddr. 16845796c8dcSSimon Schubert Return -1 if out of range, -2 other error. */ 16855796c8dcSSimon Schubert 16865796c8dcSSimon Schubert int 16875796c8dcSSimon Schubert value_bit_index (struct type *type, const gdb_byte *valaddr, int index) 16885796c8dcSSimon Schubert { 16895796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (type); 16905796c8dcSSimon Schubert LONGEST low_bound, high_bound; 16915796c8dcSSimon Schubert LONGEST word; 16925796c8dcSSimon Schubert unsigned rel_index; 16935796c8dcSSimon Schubert struct type *range = TYPE_INDEX_TYPE (type); 1694*cf7f2e2dSJohn Marino 16955796c8dcSSimon Schubert if (get_discrete_bounds (range, &low_bound, &high_bound) < 0) 16965796c8dcSSimon Schubert return -2; 16975796c8dcSSimon Schubert if (index < low_bound || index > high_bound) 16985796c8dcSSimon Schubert return -1; 16995796c8dcSSimon Schubert rel_index = index - low_bound; 17005796c8dcSSimon Schubert word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1, 17015796c8dcSSimon Schubert gdbarch_byte_order (gdbarch)); 17025796c8dcSSimon Schubert rel_index %= TARGET_CHAR_BIT; 17035796c8dcSSimon Schubert if (gdbarch_bits_big_endian (gdbarch)) 17045796c8dcSSimon Schubert rel_index = TARGET_CHAR_BIT - 1 - rel_index; 17055796c8dcSSimon Schubert return (word >> rel_index) & 1; 17065796c8dcSSimon Schubert } 17075796c8dcSSimon Schubert 17085796c8dcSSimon Schubert int 17095796c8dcSSimon Schubert value_in (struct value *element, struct value *set) 17105796c8dcSSimon Schubert { 17115796c8dcSSimon Schubert int member; 17125796c8dcSSimon Schubert struct type *settype = check_typedef (value_type (set)); 17135796c8dcSSimon Schubert struct type *eltype = check_typedef (value_type (element)); 1714*cf7f2e2dSJohn Marino 17155796c8dcSSimon Schubert if (TYPE_CODE (eltype) == TYPE_CODE_RANGE) 17165796c8dcSSimon Schubert eltype = TYPE_TARGET_TYPE (eltype); 17175796c8dcSSimon Schubert if (TYPE_CODE (settype) != TYPE_CODE_SET) 17185796c8dcSSimon Schubert error (_("Second argument of 'IN' has wrong type")); 17195796c8dcSSimon Schubert if (TYPE_CODE (eltype) != TYPE_CODE_INT 17205796c8dcSSimon Schubert && TYPE_CODE (eltype) != TYPE_CODE_CHAR 17215796c8dcSSimon Schubert && TYPE_CODE (eltype) != TYPE_CODE_ENUM 17225796c8dcSSimon Schubert && TYPE_CODE (eltype) != TYPE_CODE_BOOL) 17235796c8dcSSimon Schubert error (_("First argument of 'IN' has wrong type")); 17245796c8dcSSimon Schubert member = value_bit_index (settype, value_contents (set), 17255796c8dcSSimon Schubert value_as_long (element)); 17265796c8dcSSimon Schubert if (member < 0) 17275796c8dcSSimon Schubert error (_("First argument of 'IN' not in range")); 17285796c8dcSSimon Schubert return member; 17295796c8dcSSimon Schubert } 17305796c8dcSSimon Schubert 17315796c8dcSSimon Schubert void 17325796c8dcSSimon Schubert _initialize_valarith (void) 17335796c8dcSSimon Schubert { 17345796c8dcSSimon Schubert } 1735