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, 4cf7f2e2dSJohn Marino 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 5*c50c785cSJohn Marino 2010, 2011 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" 34cf7f2e2dSJohn 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 50*c50c785cSJohn Marino helper for value_ptradd. */ 515796c8dcSSimon Schubert 525796c8dcSSimon Schubert static LONGEST 535796c8dcSSimon Schubert find_size_for_pointer_math (struct type *ptr_type) 545796c8dcSSimon Schubert { 555796c8dcSSimon Schubert LONGEST sz = -1; 565796c8dcSSimon Schubert struct type *ptr_target; 575796c8dcSSimon Schubert 585796c8dcSSimon Schubert gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR); 595796c8dcSSimon Schubert ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type)); 605796c8dcSSimon Schubert 615796c8dcSSimon Schubert sz = TYPE_LENGTH (ptr_target); 625796c8dcSSimon Schubert if (sz == 0) 635796c8dcSSimon Schubert { 645796c8dcSSimon Schubert if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID) 655796c8dcSSimon Schubert sz = 1; 665796c8dcSSimon Schubert else 675796c8dcSSimon Schubert { 685796c8dcSSimon Schubert char *name; 695796c8dcSSimon Schubert 705796c8dcSSimon Schubert name = TYPE_NAME (ptr_target); 715796c8dcSSimon Schubert if (name == NULL) 725796c8dcSSimon Schubert name = TYPE_TAG_NAME (ptr_target); 735796c8dcSSimon Schubert if (name == NULL) 745796c8dcSSimon Schubert error (_("Cannot perform pointer math on incomplete types, " 755796c8dcSSimon Schubert "try casting to a known type, or void *.")); 765796c8dcSSimon Schubert else 775796c8dcSSimon Schubert error (_("Cannot perform pointer math on incomplete type \"%s\", " 785796c8dcSSimon Schubert "try casting to a known type, or void *."), name); 795796c8dcSSimon Schubert } 805796c8dcSSimon Schubert } 815796c8dcSSimon Schubert return sz; 825796c8dcSSimon Schubert } 835796c8dcSSimon Schubert 845796c8dcSSimon Schubert /* Given a pointer ARG1 and an integral value ARG2, return the 855796c8dcSSimon Schubert result of C-style pointer arithmetic ARG1 + ARG2. */ 865796c8dcSSimon Schubert 875796c8dcSSimon Schubert struct value * 885796c8dcSSimon Schubert value_ptradd (struct value *arg1, LONGEST arg2) 895796c8dcSSimon Schubert { 905796c8dcSSimon Schubert struct type *valptrtype; 915796c8dcSSimon Schubert LONGEST sz; 92*c50c785cSJohn Marino struct value *result; 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 98*c50c785cSJohn Marino result = value_from_pointer (valptrtype, 995796c8dcSSimon Schubert value_as_address (arg1) + sz * arg2); 100*c50c785cSJohn Marino if (VALUE_LVAL (result) != lval_internalvar) 101*c50c785cSJohn Marino set_value_component_location (result, arg1); 102*c50c785cSJohn Marino return result; 1035796c8dcSSimon Schubert } 1045796c8dcSSimon Schubert 1055796c8dcSSimon Schubert /* Given two compatible pointer values ARG1 and ARG2, return the 1065796c8dcSSimon Schubert result of C-style pointer arithmetic ARG1 - ARG2. */ 1075796c8dcSSimon Schubert 1085796c8dcSSimon Schubert LONGEST 1095796c8dcSSimon Schubert value_ptrdiff (struct value *arg1, struct value *arg2) 1105796c8dcSSimon Schubert { 1115796c8dcSSimon Schubert struct type *type1, *type2; 1125796c8dcSSimon Schubert LONGEST sz; 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert arg1 = coerce_array (arg1); 1155796c8dcSSimon Schubert arg2 = coerce_array (arg2); 1165796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 1175796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 1185796c8dcSSimon Schubert 1195796c8dcSSimon Schubert gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR); 1205796c8dcSSimon Schubert gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR); 1215796c8dcSSimon Schubert 1225796c8dcSSimon Schubert if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))) 1235796c8dcSSimon Schubert != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2)))) 124*c50c785cSJohn Marino error (_("First argument of `-' is a pointer and " 125*c50c785cSJohn Marino "second argument is neither\n" 126*c50c785cSJohn Marino "an integer nor a pointer of the same type.")); 1275796c8dcSSimon Schubert 1285796c8dcSSimon Schubert sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1))); 129cf7f2e2dSJohn Marino if (sz == 0) 130cf7f2e2dSJohn Marino { 131cf7f2e2dSJohn Marino warning (_("Type size unknown, assuming 1. " 132cf7f2e2dSJohn Marino "Try casting to a known type, or void *.")); 133cf7f2e2dSJohn Marino sz = 1; 134cf7f2e2dSJohn Marino } 135cf7f2e2dSJohn Marino 1365796c8dcSSimon Schubert return (value_as_long (arg1) - value_as_long (arg2)) / sz; 1375796c8dcSSimon Schubert } 1385796c8dcSSimon Schubert 1395796c8dcSSimon Schubert /* Return the value of ARRAY[IDX]. 1405796c8dcSSimon Schubert 1415796c8dcSSimon Schubert ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING. If the 1425796c8dcSSimon Schubert current language supports C-style arrays, it may also be TYPE_CODE_PTR. 1435796c8dcSSimon Schubert To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript. 1445796c8dcSSimon Schubert 1455796c8dcSSimon Schubert See comments in value_coerce_array() for rationale for reason for 1465796c8dcSSimon Schubert doing lower bounds adjustment here rather than there. 1475796c8dcSSimon Schubert FIXME: Perhaps we should validate that the index is valid and if 1485796c8dcSSimon Schubert verbosity is set, warn about invalid indices (but still use them). */ 1495796c8dcSSimon Schubert 1505796c8dcSSimon Schubert struct value * 1515796c8dcSSimon Schubert value_subscript (struct value *array, LONGEST index) 1525796c8dcSSimon Schubert { 1535796c8dcSSimon Schubert int c_style = current_language->c_style_arrays; 1545796c8dcSSimon Schubert struct type *tarray; 1555796c8dcSSimon Schubert 1565796c8dcSSimon Schubert array = coerce_ref (array); 1575796c8dcSSimon Schubert tarray = check_typedef (value_type (array)); 1585796c8dcSSimon Schubert 1595796c8dcSSimon Schubert if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY 1605796c8dcSSimon Schubert || TYPE_CODE (tarray) == TYPE_CODE_STRING) 1615796c8dcSSimon Schubert { 1625796c8dcSSimon Schubert struct type *range_type = TYPE_INDEX_TYPE (tarray); 1635796c8dcSSimon Schubert LONGEST lowerbound, upperbound; 1645796c8dcSSimon Schubert 165cf7f2e2dSJohn Marino get_discrete_bounds (range_type, &lowerbound, &upperbound); 1665796c8dcSSimon Schubert if (VALUE_LVAL (array) != lval_memory) 1675796c8dcSSimon Schubert return value_subscripted_rvalue (array, index, lowerbound); 1685796c8dcSSimon Schubert 1695796c8dcSSimon Schubert if (c_style == 0) 1705796c8dcSSimon Schubert { 1715796c8dcSSimon Schubert if (index >= lowerbound && index <= upperbound) 1725796c8dcSSimon Schubert return value_subscripted_rvalue (array, index, lowerbound); 1735796c8dcSSimon Schubert /* Emit warning unless we have an array of unknown size. 1745796c8dcSSimon Schubert An array of unknown size has lowerbound 0 and upperbound -1. */ 1755796c8dcSSimon Schubert if (upperbound > -1) 1765796c8dcSSimon Schubert warning (_("array or string index out of range")); 1775796c8dcSSimon Schubert /* fall doing C stuff */ 1785796c8dcSSimon Schubert c_style = 1; 1795796c8dcSSimon Schubert } 1805796c8dcSSimon Schubert 1815796c8dcSSimon Schubert index -= lowerbound; 1825796c8dcSSimon Schubert array = value_coerce_array (array); 1835796c8dcSSimon Schubert } 1845796c8dcSSimon Schubert 1855796c8dcSSimon Schubert if (c_style) 1865796c8dcSSimon Schubert return value_ind (value_ptradd (array, index)); 1875796c8dcSSimon Schubert else 1885796c8dcSSimon Schubert error (_("not an array or string")); 1895796c8dcSSimon Schubert } 1905796c8dcSSimon Schubert 1915796c8dcSSimon Schubert /* Return the value of EXPR[IDX], expr an aggregate rvalue 1925796c8dcSSimon Schubert (eg, a vector register). This routine used to promote floats 1935796c8dcSSimon Schubert to doubles, but no longer does. */ 1945796c8dcSSimon Schubert 1955796c8dcSSimon Schubert struct value * 1965796c8dcSSimon Schubert value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound) 1975796c8dcSSimon Schubert { 1985796c8dcSSimon Schubert struct type *array_type = check_typedef (value_type (array)); 1995796c8dcSSimon Schubert struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type)); 2005796c8dcSSimon Schubert unsigned int elt_size = TYPE_LENGTH (elt_type); 2015796c8dcSSimon Schubert unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound); 2025796c8dcSSimon Schubert struct value *v; 2035796c8dcSSimon Schubert 204cf7f2e2dSJohn Marino if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type) 205cf7f2e2dSJohn Marino && elt_offs >= TYPE_LENGTH (array_type))) 2065796c8dcSSimon Schubert error (_("no such vector element")); 2075796c8dcSSimon Schubert 2085796c8dcSSimon Schubert if (VALUE_LVAL (array) == lval_memory && value_lazy (array)) 209*c50c785cSJohn Marino v = allocate_value_lazy (elt_type); 2105796c8dcSSimon Schubert else 211*c50c785cSJohn Marino { 212*c50c785cSJohn Marino v = allocate_value (elt_type); 213*c50c785cSJohn Marino value_contents_copy (v, value_embedded_offset (v), 214*c50c785cSJohn Marino array, value_embedded_offset (array) + elt_offs, 215*c50c785cSJohn Marino elt_size); 216*c50c785cSJohn Marino } 2175796c8dcSSimon Schubert 2185796c8dcSSimon Schubert set_value_component_location (v, array); 2195796c8dcSSimon Schubert VALUE_REGNUM (v) = VALUE_REGNUM (array); 2205796c8dcSSimon Schubert VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array); 2215796c8dcSSimon Schubert set_value_offset (v, value_offset (array) + elt_offs); 2225796c8dcSSimon Schubert return v; 2235796c8dcSSimon Schubert } 2245796c8dcSSimon Schubert 2255796c8dcSSimon Schubert /* Return the value of BITSTRING[IDX] as (boolean) type TYPE. */ 2265796c8dcSSimon Schubert 2275796c8dcSSimon Schubert struct value * 2285796c8dcSSimon Schubert value_bitstring_subscript (struct type *type, 2295796c8dcSSimon Schubert struct value *bitstring, LONGEST index) 2305796c8dcSSimon Schubert { 2315796c8dcSSimon Schubert 2325796c8dcSSimon Schubert struct type *bitstring_type, *range_type; 2335796c8dcSSimon Schubert struct value *v; 2345796c8dcSSimon Schubert int offset, byte, bit_index; 2355796c8dcSSimon Schubert LONGEST lowerbound, upperbound; 2365796c8dcSSimon Schubert 2375796c8dcSSimon Schubert bitstring_type = check_typedef (value_type (bitstring)); 2385796c8dcSSimon Schubert gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING); 2395796c8dcSSimon Schubert 2405796c8dcSSimon Schubert range_type = TYPE_INDEX_TYPE (bitstring_type); 2415796c8dcSSimon Schubert get_discrete_bounds (range_type, &lowerbound, &upperbound); 2425796c8dcSSimon Schubert if (index < lowerbound || index > upperbound) 2435796c8dcSSimon Schubert error (_("bitstring index out of range")); 2445796c8dcSSimon Schubert 2455796c8dcSSimon Schubert index -= lowerbound; 2465796c8dcSSimon Schubert offset = index / TARGET_CHAR_BIT; 2475796c8dcSSimon Schubert byte = *((char *) value_contents (bitstring) + offset); 2485796c8dcSSimon Schubert 2495796c8dcSSimon Schubert bit_index = index % TARGET_CHAR_BIT; 2505796c8dcSSimon Schubert byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ? 2515796c8dcSSimon Schubert TARGET_CHAR_BIT - 1 - bit_index : bit_index); 2525796c8dcSSimon Schubert 2535796c8dcSSimon Schubert v = value_from_longest (type, byte & 1); 2545796c8dcSSimon Schubert 2555796c8dcSSimon Schubert set_value_bitpos (v, bit_index); 2565796c8dcSSimon Schubert set_value_bitsize (v, 1); 2575796c8dcSSimon Schubert set_value_component_location (v, bitstring); 2585796c8dcSSimon Schubert VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring); 2595796c8dcSSimon Schubert 2605796c8dcSSimon Schubert set_value_offset (v, offset + value_offset (bitstring)); 2615796c8dcSSimon Schubert 2625796c8dcSSimon Schubert return v; 2635796c8dcSSimon Schubert } 2645796c8dcSSimon Schubert 2655796c8dcSSimon Schubert 2665796c8dcSSimon Schubert /* Check to see if either argument is a structure, or a reference to 2675796c8dcSSimon Schubert one. This is called so we know whether to go ahead with the normal 2685796c8dcSSimon Schubert binop or look for a user defined function instead. 2695796c8dcSSimon Schubert 2705796c8dcSSimon Schubert For now, we do not overload the `=' operator. */ 2715796c8dcSSimon Schubert 2725796c8dcSSimon Schubert int 273cf7f2e2dSJohn Marino binop_types_user_defined_p (enum exp_opcode op, 274cf7f2e2dSJohn Marino struct type *type1, struct type *type2) 2755796c8dcSSimon Schubert { 2765796c8dcSSimon Schubert if (op == BINOP_ASSIGN || op == BINOP_CONCAT) 2775796c8dcSSimon Schubert return 0; 2785796c8dcSSimon Schubert 279cf7f2e2dSJohn Marino type1 = check_typedef (type1); 2805796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_REF) 2815796c8dcSSimon Schubert type1 = check_typedef (TYPE_TARGET_TYPE (type1)); 2825796c8dcSSimon Schubert 283cf7f2e2dSJohn Marino type2 = check_typedef (type1); 2845796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_REF) 2855796c8dcSSimon Schubert type2 = check_typedef (TYPE_TARGET_TYPE (type2)); 2865796c8dcSSimon Schubert 2875796c8dcSSimon Schubert return (TYPE_CODE (type1) == TYPE_CODE_STRUCT 2885796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_STRUCT); 2895796c8dcSSimon Schubert } 2905796c8dcSSimon Schubert 291cf7f2e2dSJohn Marino /* Check to see if either argument is a structure, or a reference to 292cf7f2e2dSJohn Marino one. This is called so we know whether to go ahead with the normal 293cf7f2e2dSJohn Marino binop or look for a user defined function instead. 294cf7f2e2dSJohn Marino 295cf7f2e2dSJohn Marino For now, we do not overload the `=' operator. */ 296cf7f2e2dSJohn Marino 297cf7f2e2dSJohn Marino int 298cf7f2e2dSJohn Marino binop_user_defined_p (enum exp_opcode op, 299cf7f2e2dSJohn Marino struct value *arg1, struct value *arg2) 300cf7f2e2dSJohn Marino { 301cf7f2e2dSJohn Marino return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2)); 302cf7f2e2dSJohn Marino } 303cf7f2e2dSJohn Marino 3045796c8dcSSimon Schubert /* Check to see if argument is a structure. This is called so 3055796c8dcSSimon Schubert we know whether to go ahead with the normal unop or look for a 3065796c8dcSSimon Schubert user defined function instead. 3075796c8dcSSimon Schubert 3085796c8dcSSimon Schubert For now, we do not overload the `&' operator. */ 3095796c8dcSSimon Schubert 3105796c8dcSSimon Schubert int 3115796c8dcSSimon Schubert unop_user_defined_p (enum exp_opcode op, struct value *arg1) 3125796c8dcSSimon Schubert { 3135796c8dcSSimon Schubert struct type *type1; 314cf7f2e2dSJohn Marino 3155796c8dcSSimon Schubert if (op == UNOP_ADDR) 3165796c8dcSSimon Schubert return 0; 3175796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 318*c50c785cSJohn Marino if (TYPE_CODE (type1) == TYPE_CODE_REF) 319*c50c785cSJohn Marino type1 = check_typedef (TYPE_TARGET_TYPE (type1)); 320*c50c785cSJohn Marino return TYPE_CODE (type1) == TYPE_CODE_STRUCT; 3215796c8dcSSimon Schubert } 3225796c8dcSSimon Schubert 323cf7f2e2dSJohn Marino /* Try to find an operator named OPERATOR which takes NARGS arguments 324cf7f2e2dSJohn Marino specified in ARGS. If the operator found is a static member operator 325cf7f2e2dSJohn Marino *STATIC_MEMFUNP will be set to 1, and otherwise 0. 326cf7f2e2dSJohn Marino The search if performed through find_overload_match which will handle 327cf7f2e2dSJohn Marino member operators, non member operators, operators imported implicitly or 328cf7f2e2dSJohn Marino explicitly, and perform correct overload resolution in all of the above 329cf7f2e2dSJohn Marino situations or combinations thereof. */ 330cf7f2e2dSJohn Marino 331cf7f2e2dSJohn Marino static struct value * 332cf7f2e2dSJohn Marino value_user_defined_cpp_op (struct value **args, int nargs, char *operator, 333cf7f2e2dSJohn Marino int *static_memfuncp) 334cf7f2e2dSJohn Marino { 335cf7f2e2dSJohn Marino 336cf7f2e2dSJohn Marino struct symbol *symp = NULL; 337cf7f2e2dSJohn Marino struct value *valp = NULL; 338cf7f2e2dSJohn Marino struct type **arg_types; 339cf7f2e2dSJohn Marino int i; 340cf7f2e2dSJohn Marino 341cf7f2e2dSJohn Marino arg_types = (struct type **) alloca (nargs * (sizeof (struct type *))); 342*c50c785cSJohn Marino /* Prepare list of argument types for overload resolution. */ 343cf7f2e2dSJohn Marino for (i = 0; i < nargs; i++) 344cf7f2e2dSJohn Marino arg_types[i] = value_type (args[i]); 345cf7f2e2dSJohn Marino 346cf7f2e2dSJohn Marino find_overload_match (arg_types, nargs, operator, BOTH /* could be method */, 347cf7f2e2dSJohn Marino 0 /* strict match */, &args[0], /* objp */ 348cf7f2e2dSJohn Marino NULL /* pass NULL symbol since symbol is unknown */, 349cf7f2e2dSJohn Marino &valp, &symp, static_memfuncp, 0); 350cf7f2e2dSJohn Marino 351cf7f2e2dSJohn Marino if (valp) 352cf7f2e2dSJohn Marino return valp; 353cf7f2e2dSJohn Marino 354cf7f2e2dSJohn Marino if (symp) 355cf7f2e2dSJohn Marino { 356cf7f2e2dSJohn Marino /* This is a non member function and does not 357cf7f2e2dSJohn Marino expect a reference as its first argument 358cf7f2e2dSJohn Marino rather the explicit structure. */ 359cf7f2e2dSJohn Marino args[0] = value_ind (args[0]); 360cf7f2e2dSJohn Marino return value_of_variable (symp, 0); 361cf7f2e2dSJohn Marino } 362cf7f2e2dSJohn Marino 363cf7f2e2dSJohn Marino error (_("Could not find %s."), operator); 364cf7f2e2dSJohn Marino } 365cf7f2e2dSJohn Marino 366cf7f2e2dSJohn Marino /* Lookup user defined operator NAME. Return a value representing the 367cf7f2e2dSJohn Marino function, otherwise return NULL. */ 368cf7f2e2dSJohn Marino 369cf7f2e2dSJohn Marino static struct value * 370cf7f2e2dSJohn Marino value_user_defined_op (struct value **argp, struct value **args, char *name, 371cf7f2e2dSJohn Marino int *static_memfuncp, int nargs) 372cf7f2e2dSJohn Marino { 373cf7f2e2dSJohn Marino struct value *result = NULL; 374cf7f2e2dSJohn Marino 375cf7f2e2dSJohn Marino if (current_language->la_language == language_cplus) 376cf7f2e2dSJohn Marino result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp); 377cf7f2e2dSJohn Marino else 378cf7f2e2dSJohn Marino result = value_struct_elt (argp, args, name, static_memfuncp, 379cf7f2e2dSJohn Marino "structure"); 380cf7f2e2dSJohn Marino 381cf7f2e2dSJohn Marino return result; 382cf7f2e2dSJohn Marino } 383cf7f2e2dSJohn 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 416*c50c785cSJohn Marino /* 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 524cf7f2e2dSJohn Marino argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr, 525cf7f2e2dSJohn 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; 537cf7f2e2dSJohn 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 } 542*c50c785cSJohn Marino return call_function_by_hand (argvec[0], 2 - static_memfuncp, 543*c50c785cSJohn Marino argvec + 1); 5445796c8dcSSimon Schubert } 545*c50c785cSJohn Marino throw_error (NOT_FOUND_ERROR, 546*c50c785cSJohn Marino _("member function %s not found"), tstr); 5475796c8dcSSimon Schubert #ifdef lint 5485796c8dcSSimon Schubert return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); 5495796c8dcSSimon Schubert #endif 5505796c8dcSSimon Schubert } 5515796c8dcSSimon Schubert 5525796c8dcSSimon Schubert /* We know that arg1 is a structure, so try to find a unary user 5535796c8dcSSimon Schubert defined operator that matches the operator in question. 5545796c8dcSSimon Schubert Create an argument vector that calls arg1.operator @ (arg1) 5555796c8dcSSimon Schubert and return that value (where '@' is (almost) any unary operator which 5565796c8dcSSimon Schubert is legal for GNU C++). */ 5575796c8dcSSimon Schubert 5585796c8dcSSimon Schubert struct value * 5595796c8dcSSimon Schubert value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside) 5605796c8dcSSimon Schubert { 5615796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (value_type (arg1)); 5625796c8dcSSimon Schubert struct value **argvec; 5635796c8dcSSimon Schubert char *ptr, *mangle_ptr; 5645796c8dcSSimon Schubert char tstr[13], mangle_tstr[13]; 5655796c8dcSSimon Schubert int static_memfuncp, nargs; 5665796c8dcSSimon Schubert 5675796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 5685796c8dcSSimon Schubert 5695796c8dcSSimon Schubert /* now we know that what we have to do is construct our 5705796c8dcSSimon Schubert arg vector and find the right function to call it with. */ 5715796c8dcSSimon Schubert 5725796c8dcSSimon Schubert if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT) 5735796c8dcSSimon Schubert error (_("Can't do that unary op on that type")); /* FIXME be explicit */ 5745796c8dcSSimon Schubert 5755796c8dcSSimon Schubert argvec = (struct value **) alloca (sizeof (struct value *) * 4); 5765796c8dcSSimon Schubert argvec[1] = value_addr (arg1); 5775796c8dcSSimon Schubert argvec[2] = 0; 5785796c8dcSSimon Schubert 5795796c8dcSSimon Schubert nargs = 1; 5805796c8dcSSimon Schubert 581*c50c785cSJohn Marino /* Make the right function name up. */ 5825796c8dcSSimon Schubert strcpy (tstr, "operator__"); 5835796c8dcSSimon Schubert ptr = tstr + 8; 5845796c8dcSSimon Schubert strcpy (mangle_tstr, "__"); 5855796c8dcSSimon Schubert mangle_ptr = mangle_tstr + 2; 5865796c8dcSSimon Schubert switch (op) 5875796c8dcSSimon Schubert { 5885796c8dcSSimon Schubert case UNOP_PREINCREMENT: 5895796c8dcSSimon Schubert strcpy (ptr, "++"); 5905796c8dcSSimon Schubert break; 5915796c8dcSSimon Schubert case UNOP_PREDECREMENT: 5925796c8dcSSimon Schubert strcpy (ptr, "--"); 5935796c8dcSSimon Schubert break; 5945796c8dcSSimon Schubert case UNOP_POSTINCREMENT: 5955796c8dcSSimon Schubert strcpy (ptr, "++"); 5965796c8dcSSimon Schubert argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); 5975796c8dcSSimon Schubert argvec[3] = 0; 5985796c8dcSSimon Schubert nargs ++; 5995796c8dcSSimon Schubert break; 6005796c8dcSSimon Schubert case UNOP_POSTDECREMENT: 6015796c8dcSSimon Schubert strcpy (ptr, "--"); 6025796c8dcSSimon Schubert argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0); 6035796c8dcSSimon Schubert argvec[3] = 0; 6045796c8dcSSimon Schubert nargs ++; 6055796c8dcSSimon Schubert break; 6065796c8dcSSimon Schubert case UNOP_LOGICAL_NOT: 6075796c8dcSSimon Schubert strcpy (ptr, "!"); 6085796c8dcSSimon Schubert break; 6095796c8dcSSimon Schubert case UNOP_COMPLEMENT: 6105796c8dcSSimon Schubert strcpy (ptr, "~"); 6115796c8dcSSimon Schubert break; 6125796c8dcSSimon Schubert case UNOP_NEG: 6135796c8dcSSimon Schubert strcpy (ptr, "-"); 6145796c8dcSSimon Schubert break; 6155796c8dcSSimon Schubert case UNOP_PLUS: 6165796c8dcSSimon Schubert strcpy (ptr, "+"); 6175796c8dcSSimon Schubert break; 6185796c8dcSSimon Schubert case UNOP_IND: 6195796c8dcSSimon Schubert strcpy (ptr, "*"); 6205796c8dcSSimon Schubert break; 621*c50c785cSJohn Marino case STRUCTOP_PTR: 622*c50c785cSJohn Marino strcpy (ptr, "->"); 623*c50c785cSJohn Marino break; 6245796c8dcSSimon Schubert default: 6255796c8dcSSimon Schubert error (_("Invalid unary operation specified.")); 6265796c8dcSSimon Schubert } 6275796c8dcSSimon Schubert 628cf7f2e2dSJohn Marino argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr, 629cf7f2e2dSJohn Marino &static_memfuncp, nargs); 6305796c8dcSSimon Schubert 6315796c8dcSSimon Schubert if (argvec[0]) 6325796c8dcSSimon Schubert { 6335796c8dcSSimon Schubert if (static_memfuncp) 6345796c8dcSSimon Schubert { 6355796c8dcSSimon Schubert argvec[1] = argvec[0]; 6365796c8dcSSimon Schubert nargs --; 6375796c8dcSSimon Schubert argvec++; 6385796c8dcSSimon Schubert } 6395796c8dcSSimon Schubert if (noside == EVAL_AVOID_SIDE_EFFECTS) 6405796c8dcSSimon Schubert { 6415796c8dcSSimon Schubert struct type *return_type; 642cf7f2e2dSJohn Marino 6435796c8dcSSimon Schubert return_type 6445796c8dcSSimon Schubert = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0]))); 6455796c8dcSSimon Schubert return value_zero (return_type, VALUE_LVAL (arg1)); 6465796c8dcSSimon Schubert } 6475796c8dcSSimon Schubert return call_function_by_hand (argvec[0], nargs, argvec + 1); 6485796c8dcSSimon Schubert } 649*c50c785cSJohn Marino throw_error (NOT_FOUND_ERROR, 650*c50c785cSJohn Marino _("member function %s not found"), tstr); 651*c50c785cSJohn Marino 6525796c8dcSSimon Schubert return 0; /* For lint -- never reached */ 6535796c8dcSSimon Schubert } 6545796c8dcSSimon Schubert 6555796c8dcSSimon Schubert 6565796c8dcSSimon Schubert /* Concatenate two values with the following conditions: 6575796c8dcSSimon Schubert 6585796c8dcSSimon Schubert (1) Both values must be either bitstring values or character string 6595796c8dcSSimon Schubert values and the resulting value consists of the concatenation of 6605796c8dcSSimon Schubert ARG1 followed by ARG2. 6615796c8dcSSimon Schubert 6625796c8dcSSimon Schubert or 6635796c8dcSSimon Schubert 6645796c8dcSSimon Schubert One value must be an integer value and the other value must be 6655796c8dcSSimon Schubert either a bitstring value or character string value, which is 6665796c8dcSSimon Schubert to be repeated by the number of times specified by the integer 6675796c8dcSSimon Schubert value. 6685796c8dcSSimon Schubert 6695796c8dcSSimon Schubert 6705796c8dcSSimon Schubert (2) Boolean values are also allowed and are treated as bit string 6715796c8dcSSimon Schubert values of length 1. 6725796c8dcSSimon Schubert 6735796c8dcSSimon Schubert (3) Character values are also allowed and are treated as character 674*c50c785cSJohn Marino string values of length 1. */ 6755796c8dcSSimon Schubert 6765796c8dcSSimon Schubert struct value * 6775796c8dcSSimon Schubert value_concat (struct value *arg1, struct value *arg2) 6785796c8dcSSimon Schubert { 6795796c8dcSSimon Schubert struct value *inval1; 6805796c8dcSSimon Schubert struct value *inval2; 6815796c8dcSSimon Schubert struct value *outval = NULL; 6825796c8dcSSimon Schubert int inval1len, inval2len; 6835796c8dcSSimon Schubert int count, idx; 6845796c8dcSSimon Schubert char *ptr; 6855796c8dcSSimon Schubert char inchar; 6865796c8dcSSimon Schubert struct type *type1 = check_typedef (value_type (arg1)); 6875796c8dcSSimon Schubert struct type *type2 = check_typedef (value_type (arg2)); 6885796c8dcSSimon Schubert struct type *char_type; 6895796c8dcSSimon Schubert 6905796c8dcSSimon Schubert /* First figure out if we are dealing with two values to be concatenated 6915796c8dcSSimon Schubert or a repeat count and a value to be repeated. INVAL1 is set to the 6925796c8dcSSimon Schubert first of two concatenated values, or the repeat count. INVAL2 is set 6935796c8dcSSimon Schubert to the second of the two concatenated values or the value to be 6945796c8dcSSimon Schubert repeated. */ 6955796c8dcSSimon Schubert 6965796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_INT) 6975796c8dcSSimon Schubert { 6985796c8dcSSimon Schubert struct type *tmp = type1; 699cf7f2e2dSJohn Marino 7005796c8dcSSimon Schubert type1 = tmp; 7015796c8dcSSimon Schubert tmp = type2; 7025796c8dcSSimon Schubert inval1 = arg2; 7035796c8dcSSimon Schubert inval2 = arg1; 7045796c8dcSSimon Schubert } 7055796c8dcSSimon Schubert else 7065796c8dcSSimon Schubert { 7075796c8dcSSimon Schubert inval1 = arg1; 7085796c8dcSSimon Schubert inval2 = arg2; 7095796c8dcSSimon Schubert } 7105796c8dcSSimon Schubert 7115796c8dcSSimon Schubert /* Now process the input values. */ 7125796c8dcSSimon Schubert 7135796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_INT) 7145796c8dcSSimon Schubert { 7155796c8dcSSimon Schubert /* We have a repeat count. Validate the second value and then 7165796c8dcSSimon Schubert construct a value repeated that many times. */ 7175796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_STRING 7185796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_CHAR) 7195796c8dcSSimon Schubert { 7205796c8dcSSimon Schubert count = longest_to_int (value_as_long (inval1)); 7215796c8dcSSimon Schubert inval2len = TYPE_LENGTH (type2); 7225796c8dcSSimon Schubert ptr = (char *) alloca (count * inval2len); 7235796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_CHAR) 7245796c8dcSSimon Schubert { 7255796c8dcSSimon Schubert char_type = type2; 726cf7f2e2dSJohn Marino 7275796c8dcSSimon Schubert inchar = (char) unpack_long (type2, 7285796c8dcSSimon Schubert value_contents (inval2)); 7295796c8dcSSimon Schubert for (idx = 0; idx < count; idx++) 7305796c8dcSSimon Schubert { 7315796c8dcSSimon Schubert *(ptr + idx) = inchar; 7325796c8dcSSimon Schubert } 7335796c8dcSSimon Schubert } 7345796c8dcSSimon Schubert else 7355796c8dcSSimon Schubert { 7365796c8dcSSimon Schubert char_type = TYPE_TARGET_TYPE (type2); 737cf7f2e2dSJohn Marino 7385796c8dcSSimon Schubert for (idx = 0; idx < count; idx++) 7395796c8dcSSimon Schubert { 7405796c8dcSSimon Schubert memcpy (ptr + (idx * inval2len), value_contents (inval2), 7415796c8dcSSimon Schubert inval2len); 7425796c8dcSSimon Schubert } 7435796c8dcSSimon Schubert } 7445796c8dcSSimon Schubert outval = value_string (ptr, count * inval2len, char_type); 7455796c8dcSSimon Schubert } 7465796c8dcSSimon Schubert else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING 7475796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_BOOL) 7485796c8dcSSimon Schubert { 7495796c8dcSSimon Schubert error (_("unimplemented support for bitstring/boolean repeats")); 7505796c8dcSSimon Schubert } 7515796c8dcSSimon Schubert else 7525796c8dcSSimon Schubert { 7535796c8dcSSimon Schubert error (_("can't repeat values of that type")); 7545796c8dcSSimon Schubert } 7555796c8dcSSimon Schubert } 7565796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_STRING 7575796c8dcSSimon Schubert || TYPE_CODE (type1) == TYPE_CODE_CHAR) 7585796c8dcSSimon Schubert { 7595796c8dcSSimon Schubert /* We have two character strings to concatenate. */ 7605796c8dcSSimon Schubert if (TYPE_CODE (type2) != TYPE_CODE_STRING 7615796c8dcSSimon Schubert && TYPE_CODE (type2) != TYPE_CODE_CHAR) 7625796c8dcSSimon Schubert { 7635796c8dcSSimon Schubert error (_("Strings can only be concatenated with other strings.")); 7645796c8dcSSimon Schubert } 7655796c8dcSSimon Schubert inval1len = TYPE_LENGTH (type1); 7665796c8dcSSimon Schubert inval2len = TYPE_LENGTH (type2); 7675796c8dcSSimon Schubert ptr = (char *) alloca (inval1len + inval2len); 7685796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_CHAR) 7695796c8dcSSimon Schubert { 7705796c8dcSSimon Schubert char_type = type1; 771cf7f2e2dSJohn Marino 7725796c8dcSSimon Schubert *ptr = (char) unpack_long (type1, value_contents (inval1)); 7735796c8dcSSimon Schubert } 7745796c8dcSSimon Schubert else 7755796c8dcSSimon Schubert { 7765796c8dcSSimon Schubert char_type = TYPE_TARGET_TYPE (type1); 777cf7f2e2dSJohn Marino 7785796c8dcSSimon Schubert memcpy (ptr, value_contents (inval1), inval1len); 7795796c8dcSSimon Schubert } 7805796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_CHAR) 7815796c8dcSSimon Schubert { 7825796c8dcSSimon Schubert *(ptr + inval1len) = 7835796c8dcSSimon Schubert (char) unpack_long (type2, value_contents (inval2)); 7845796c8dcSSimon Schubert } 7855796c8dcSSimon Schubert else 7865796c8dcSSimon Schubert { 7875796c8dcSSimon Schubert memcpy (ptr + inval1len, value_contents (inval2), inval2len); 7885796c8dcSSimon Schubert } 7895796c8dcSSimon Schubert outval = value_string (ptr, inval1len + inval2len, char_type); 7905796c8dcSSimon Schubert } 7915796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING 7925796c8dcSSimon Schubert || TYPE_CODE (type1) == TYPE_CODE_BOOL) 7935796c8dcSSimon Schubert { 7945796c8dcSSimon Schubert /* We have two bitstrings to concatenate. */ 7955796c8dcSSimon Schubert if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING 7965796c8dcSSimon Schubert && TYPE_CODE (type2) != TYPE_CODE_BOOL) 7975796c8dcSSimon Schubert { 798*c50c785cSJohn Marino error (_("Bitstrings or booleans can only be concatenated " 799*c50c785cSJohn Marino "with other bitstrings or booleans.")); 8005796c8dcSSimon Schubert } 8015796c8dcSSimon Schubert error (_("unimplemented support for bitstring/boolean concatenation.")); 8025796c8dcSSimon Schubert } 8035796c8dcSSimon Schubert else 8045796c8dcSSimon Schubert { 8055796c8dcSSimon Schubert /* We don't know how to concatenate these operands. */ 8065796c8dcSSimon Schubert error (_("illegal operands for concatenation.")); 8075796c8dcSSimon Schubert } 8085796c8dcSSimon Schubert return (outval); 8095796c8dcSSimon Schubert } 8105796c8dcSSimon Schubert 8115796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are 8125796c8dcSSimon Schubert integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ 813*c50c785cSJohn Marino 8145796c8dcSSimon Schubert static LONGEST 8155796c8dcSSimon Schubert integer_pow (LONGEST v1, LONGEST v2) 8165796c8dcSSimon Schubert { 8175796c8dcSSimon Schubert if (v2 < 0) 8185796c8dcSSimon Schubert { 8195796c8dcSSimon Schubert if (v1 == 0) 8205796c8dcSSimon Schubert error (_("Attempt to raise 0 to negative power.")); 8215796c8dcSSimon Schubert else 8225796c8dcSSimon Schubert return 0; 8235796c8dcSSimon Schubert } 8245796c8dcSSimon Schubert else 8255796c8dcSSimon Schubert { 826*c50c785cSJohn Marino /* The Russian Peasant's Algorithm. */ 8275796c8dcSSimon Schubert LONGEST v; 8285796c8dcSSimon Schubert 8295796c8dcSSimon Schubert v = 1; 8305796c8dcSSimon Schubert for (;;) 8315796c8dcSSimon Schubert { 8325796c8dcSSimon Schubert if (v2 & 1L) 8335796c8dcSSimon Schubert v *= v1; 8345796c8dcSSimon Schubert v2 >>= 1; 8355796c8dcSSimon Schubert if (v2 == 0) 8365796c8dcSSimon Schubert return v; 8375796c8dcSSimon Schubert v1 *= v1; 8385796c8dcSSimon Schubert } 8395796c8dcSSimon Schubert } 8405796c8dcSSimon Schubert } 8415796c8dcSSimon Schubert 8425796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are 8435796c8dcSSimon Schubert integers. Requires V1 != 0 if V2 < 0. Returns 1 for 0 ** 0. */ 844*c50c785cSJohn Marino 8455796c8dcSSimon Schubert static ULONGEST 8465796c8dcSSimon Schubert uinteger_pow (ULONGEST v1, LONGEST v2) 8475796c8dcSSimon Schubert { 8485796c8dcSSimon Schubert if (v2 < 0) 8495796c8dcSSimon Schubert { 8505796c8dcSSimon Schubert if (v1 == 0) 8515796c8dcSSimon Schubert error (_("Attempt to raise 0 to negative power.")); 8525796c8dcSSimon Schubert else 8535796c8dcSSimon Schubert return 0; 8545796c8dcSSimon Schubert } 8555796c8dcSSimon Schubert else 8565796c8dcSSimon Schubert { 857*c50c785cSJohn Marino /* The Russian Peasant's Algorithm. */ 8585796c8dcSSimon Schubert ULONGEST v; 8595796c8dcSSimon Schubert 8605796c8dcSSimon Schubert v = 1; 8615796c8dcSSimon Schubert for (;;) 8625796c8dcSSimon Schubert { 8635796c8dcSSimon Schubert if (v2 & 1L) 8645796c8dcSSimon Schubert v *= v1; 8655796c8dcSSimon Schubert v2 >>= 1; 8665796c8dcSSimon Schubert if (v2 == 0) 8675796c8dcSSimon Schubert return v; 8685796c8dcSSimon Schubert v1 *= v1; 8695796c8dcSSimon Schubert } 8705796c8dcSSimon Schubert } 8715796c8dcSSimon Schubert } 8725796c8dcSSimon Schubert 8735796c8dcSSimon Schubert /* Obtain decimal value of arguments for binary operation, converting from 8745796c8dcSSimon Schubert other types if one of them is not decimal floating point. */ 8755796c8dcSSimon Schubert static void 8765796c8dcSSimon Schubert value_args_as_decimal (struct value *arg1, struct value *arg2, 8775796c8dcSSimon Schubert gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x, 8785796c8dcSSimon Schubert gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y) 8795796c8dcSSimon Schubert { 8805796c8dcSSimon Schubert struct type *type1, *type2; 8815796c8dcSSimon Schubert 8825796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 8835796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 8845796c8dcSSimon Schubert 8855796c8dcSSimon Schubert /* At least one of the arguments must be of decimal float type. */ 8865796c8dcSSimon Schubert gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT 8875796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT); 8885796c8dcSSimon Schubert 8895796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_FLT 8905796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_FLT) 8915796c8dcSSimon Schubert /* The DFP extension to the C language does not allow mixing of 8925796c8dcSSimon Schubert * decimal float types with other float types in expressions 8935796c8dcSSimon Schubert * (see WDTR 24732, page 12). */ 894*c50c785cSJohn Marino error (_("Mixing decimal floating types with " 895*c50c785cSJohn Marino "other floating types is not allowed.")); 8965796c8dcSSimon Schubert 8975796c8dcSSimon Schubert /* Obtain decimal value of arg1, converting from other types 8985796c8dcSSimon Schubert if necessary. */ 8995796c8dcSSimon Schubert 9005796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) 9015796c8dcSSimon Schubert { 9025796c8dcSSimon Schubert *byte_order_x = gdbarch_byte_order (get_type_arch (type1)); 9035796c8dcSSimon Schubert *len_x = TYPE_LENGTH (type1); 9045796c8dcSSimon Schubert memcpy (x, value_contents (arg1), *len_x); 9055796c8dcSSimon Schubert } 9065796c8dcSSimon Schubert else if (is_integral_type (type1)) 9075796c8dcSSimon Schubert { 9085796c8dcSSimon Schubert *byte_order_x = gdbarch_byte_order (get_type_arch (type2)); 9095796c8dcSSimon Schubert *len_x = TYPE_LENGTH (type2); 9105796c8dcSSimon Schubert decimal_from_integral (arg1, x, *len_x, *byte_order_x); 9115796c8dcSSimon Schubert } 9125796c8dcSSimon Schubert else 9135796c8dcSSimon Schubert error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), 9145796c8dcSSimon Schubert TYPE_NAME (type2)); 9155796c8dcSSimon Schubert 9165796c8dcSSimon Schubert /* Obtain decimal value of arg2, converting from other types 9175796c8dcSSimon Schubert if necessary. */ 9185796c8dcSSimon Schubert 9195796c8dcSSimon Schubert if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) 9205796c8dcSSimon Schubert { 9215796c8dcSSimon Schubert *byte_order_y = gdbarch_byte_order (get_type_arch (type2)); 9225796c8dcSSimon Schubert *len_y = TYPE_LENGTH (type2); 9235796c8dcSSimon Schubert memcpy (y, value_contents (arg2), *len_y); 9245796c8dcSSimon Schubert } 9255796c8dcSSimon Schubert else if (is_integral_type (type2)) 9265796c8dcSSimon Schubert { 9275796c8dcSSimon Schubert *byte_order_y = gdbarch_byte_order (get_type_arch (type1)); 9285796c8dcSSimon Schubert *len_y = TYPE_LENGTH (type1); 9295796c8dcSSimon Schubert decimal_from_integral (arg2, y, *len_y, *byte_order_y); 9305796c8dcSSimon Schubert } 9315796c8dcSSimon Schubert else 9325796c8dcSSimon Schubert error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1), 9335796c8dcSSimon Schubert TYPE_NAME (type2)); 9345796c8dcSSimon Schubert } 9355796c8dcSSimon Schubert 9365796c8dcSSimon Schubert /* Perform a binary operation on two operands which have reasonable 9375796c8dcSSimon Schubert representations as integers or floats. This includes booleans, 9385796c8dcSSimon Schubert characters, integers, or floats. 9395796c8dcSSimon Schubert Does not support addition and subtraction on pointers; 9405796c8dcSSimon Schubert use value_ptradd, value_ptrsub or value_ptrdiff for those operations. */ 9415796c8dcSSimon Schubert 942*c50c785cSJohn Marino static struct value * 943*c50c785cSJohn Marino scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) 9445796c8dcSSimon Schubert { 9455796c8dcSSimon Schubert struct value *val; 9465796c8dcSSimon Schubert struct type *type1, *type2, *result_type; 9475796c8dcSSimon Schubert 9485796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 9495796c8dcSSimon Schubert arg2 = coerce_ref (arg2); 9505796c8dcSSimon Schubert 9515796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 9525796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 9535796c8dcSSimon Schubert 9545796c8dcSSimon Schubert if ((TYPE_CODE (type1) != TYPE_CODE_FLT 9555796c8dcSSimon Schubert && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT 9565796c8dcSSimon Schubert && !is_integral_type (type1)) 9575796c8dcSSimon Schubert || (TYPE_CODE (type2) != TYPE_CODE_FLT 9585796c8dcSSimon Schubert && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT 9595796c8dcSSimon Schubert && !is_integral_type (type2))) 9605796c8dcSSimon Schubert error (_("Argument to arithmetic operation not a number or boolean.")); 9615796c8dcSSimon Schubert 9625796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT 9635796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT) 9645796c8dcSSimon Schubert { 9655796c8dcSSimon Schubert int len_v1, len_v2, len_v; 9665796c8dcSSimon Schubert enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v; 9675796c8dcSSimon Schubert gdb_byte v1[16], v2[16]; 9685796c8dcSSimon Schubert gdb_byte v[16]; 9695796c8dcSSimon Schubert 9705796c8dcSSimon Schubert /* If only one type is decimal float, use its type. 9715796c8dcSSimon Schubert Otherwise use the bigger type. */ 9725796c8dcSSimon Schubert if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT) 9735796c8dcSSimon Schubert result_type = type2; 9745796c8dcSSimon Schubert else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT) 9755796c8dcSSimon Schubert result_type = type1; 9765796c8dcSSimon Schubert else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) 9775796c8dcSSimon Schubert result_type = type2; 9785796c8dcSSimon Schubert else 9795796c8dcSSimon Schubert result_type = type1; 9805796c8dcSSimon Schubert 9815796c8dcSSimon Schubert len_v = TYPE_LENGTH (result_type); 9825796c8dcSSimon Schubert byte_order_v = gdbarch_byte_order (get_type_arch (result_type)); 9835796c8dcSSimon Schubert 9845796c8dcSSimon Schubert value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, 9855796c8dcSSimon Schubert v2, &len_v2, &byte_order_v2); 9865796c8dcSSimon Schubert 9875796c8dcSSimon Schubert switch (op) 9885796c8dcSSimon Schubert { 9895796c8dcSSimon Schubert case BINOP_ADD: 9905796c8dcSSimon Schubert case BINOP_SUB: 9915796c8dcSSimon Schubert case BINOP_MUL: 9925796c8dcSSimon Schubert case BINOP_DIV: 9935796c8dcSSimon Schubert case BINOP_EXP: 9945796c8dcSSimon Schubert decimal_binop (op, v1, len_v1, byte_order_v1, 9955796c8dcSSimon Schubert v2, len_v2, byte_order_v2, 9965796c8dcSSimon Schubert v, len_v, byte_order_v); 9975796c8dcSSimon Schubert break; 9985796c8dcSSimon Schubert 9995796c8dcSSimon Schubert default: 10005796c8dcSSimon Schubert error (_("Operation not valid for decimal floating point number.")); 10015796c8dcSSimon Schubert } 10025796c8dcSSimon Schubert 10035796c8dcSSimon Schubert val = value_from_decfloat (result_type, v); 10045796c8dcSSimon Schubert } 10055796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_FLT 10065796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_FLT) 10075796c8dcSSimon Schubert { 10085796c8dcSSimon Schubert /* FIXME-if-picky-about-floating-accuracy: Should be doing this 10095796c8dcSSimon Schubert in target format. real.c in GCC probably has the necessary 10105796c8dcSSimon Schubert code. */ 10115796c8dcSSimon Schubert DOUBLEST v1, v2, v = 0; 1012cf7f2e2dSJohn Marino 10135796c8dcSSimon Schubert v1 = value_as_double (arg1); 10145796c8dcSSimon Schubert v2 = value_as_double (arg2); 10155796c8dcSSimon Schubert 10165796c8dcSSimon Schubert switch (op) 10175796c8dcSSimon Schubert { 10185796c8dcSSimon Schubert case BINOP_ADD: 10195796c8dcSSimon Schubert v = v1 + v2; 10205796c8dcSSimon Schubert break; 10215796c8dcSSimon Schubert 10225796c8dcSSimon Schubert case BINOP_SUB: 10235796c8dcSSimon Schubert v = v1 - v2; 10245796c8dcSSimon Schubert break; 10255796c8dcSSimon Schubert 10265796c8dcSSimon Schubert case BINOP_MUL: 10275796c8dcSSimon Schubert v = v1 * v2; 10285796c8dcSSimon Schubert break; 10295796c8dcSSimon Schubert 10305796c8dcSSimon Schubert case BINOP_DIV: 10315796c8dcSSimon Schubert v = v1 / v2; 10325796c8dcSSimon Schubert break; 10335796c8dcSSimon Schubert 10345796c8dcSSimon Schubert case BINOP_EXP: 10355796c8dcSSimon Schubert errno = 0; 10365796c8dcSSimon Schubert v = pow (v1, v2); 10375796c8dcSSimon Schubert if (errno) 1038*c50c785cSJohn Marino error (_("Cannot perform exponentiation: %s"), 1039*c50c785cSJohn Marino safe_strerror (errno)); 10405796c8dcSSimon Schubert break; 10415796c8dcSSimon Schubert 10425796c8dcSSimon Schubert case BINOP_MIN: 10435796c8dcSSimon Schubert v = v1 < v2 ? v1 : v2; 10445796c8dcSSimon Schubert break; 10455796c8dcSSimon Schubert 10465796c8dcSSimon Schubert case BINOP_MAX: 10475796c8dcSSimon Schubert v = v1 > v2 ? v1 : v2; 10485796c8dcSSimon Schubert break; 10495796c8dcSSimon Schubert 10505796c8dcSSimon Schubert default: 10515796c8dcSSimon Schubert error (_("Integer-only operation on floating point number.")); 10525796c8dcSSimon Schubert } 10535796c8dcSSimon Schubert 10545796c8dcSSimon Schubert /* If only one type is float, use its type. 10555796c8dcSSimon Schubert Otherwise use the bigger type. */ 10565796c8dcSSimon Schubert if (TYPE_CODE (type1) != TYPE_CODE_FLT) 10575796c8dcSSimon Schubert result_type = type2; 10585796c8dcSSimon Schubert else if (TYPE_CODE (type2) != TYPE_CODE_FLT) 10595796c8dcSSimon Schubert result_type = type1; 10605796c8dcSSimon Schubert else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) 10615796c8dcSSimon Schubert result_type = type2; 10625796c8dcSSimon Schubert else 10635796c8dcSSimon Schubert result_type = type1; 10645796c8dcSSimon Schubert 10655796c8dcSSimon Schubert val = allocate_value (result_type); 10665796c8dcSSimon Schubert store_typed_floating (value_contents_raw (val), value_type (val), v); 10675796c8dcSSimon Schubert } 10685796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_BOOL 10695796c8dcSSimon Schubert || TYPE_CODE (type2) == TYPE_CODE_BOOL) 10705796c8dcSSimon Schubert { 10715796c8dcSSimon Schubert LONGEST v1, v2, v = 0; 1072cf7f2e2dSJohn Marino 10735796c8dcSSimon Schubert v1 = value_as_long (arg1); 10745796c8dcSSimon Schubert v2 = value_as_long (arg2); 10755796c8dcSSimon Schubert 10765796c8dcSSimon Schubert switch (op) 10775796c8dcSSimon Schubert { 10785796c8dcSSimon Schubert case BINOP_BITWISE_AND: 10795796c8dcSSimon Schubert v = v1 & v2; 10805796c8dcSSimon Schubert break; 10815796c8dcSSimon Schubert 10825796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 10835796c8dcSSimon Schubert v = v1 | v2; 10845796c8dcSSimon Schubert break; 10855796c8dcSSimon Schubert 10865796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 10875796c8dcSSimon Schubert v = v1 ^ v2; 10885796c8dcSSimon Schubert break; 10895796c8dcSSimon Schubert 10905796c8dcSSimon Schubert case BINOP_EQUAL: 10915796c8dcSSimon Schubert v = v1 == v2; 10925796c8dcSSimon Schubert break; 10935796c8dcSSimon Schubert 10945796c8dcSSimon Schubert case BINOP_NOTEQUAL: 10955796c8dcSSimon Schubert v = v1 != v2; 10965796c8dcSSimon Schubert break; 10975796c8dcSSimon Schubert 10985796c8dcSSimon Schubert default: 10995796c8dcSSimon Schubert error (_("Invalid operation on booleans.")); 11005796c8dcSSimon Schubert } 11015796c8dcSSimon Schubert 11025796c8dcSSimon Schubert result_type = type1; 11035796c8dcSSimon Schubert 11045796c8dcSSimon Schubert val = allocate_value (result_type); 11055796c8dcSSimon Schubert store_signed_integer (value_contents_raw (val), 11065796c8dcSSimon Schubert TYPE_LENGTH (result_type), 11075796c8dcSSimon Schubert gdbarch_byte_order (get_type_arch (result_type)), 11085796c8dcSSimon Schubert v); 11095796c8dcSSimon Schubert } 11105796c8dcSSimon Schubert else 11115796c8dcSSimon Schubert /* Integral operations here. */ 11125796c8dcSSimon Schubert { 11135796c8dcSSimon Schubert /* Determine type length of the result, and if the operation should 11145796c8dcSSimon Schubert be done unsigned. For exponentiation and shift operators, 11155796c8dcSSimon Schubert use the length and type of the left operand. Otherwise, 11165796c8dcSSimon Schubert use the signedness of the operand with the greater length. 11175796c8dcSSimon Schubert If both operands are of equal length, use unsigned operation 11185796c8dcSSimon Schubert if one of the operands is unsigned. */ 11195796c8dcSSimon Schubert if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP) 11205796c8dcSSimon Schubert result_type = type1; 11215796c8dcSSimon Schubert else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)) 11225796c8dcSSimon Schubert result_type = type1; 11235796c8dcSSimon Schubert else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1)) 11245796c8dcSSimon Schubert result_type = type2; 11255796c8dcSSimon Schubert else if (TYPE_UNSIGNED (type1)) 11265796c8dcSSimon Schubert result_type = type1; 11275796c8dcSSimon Schubert else if (TYPE_UNSIGNED (type2)) 11285796c8dcSSimon Schubert result_type = type2; 11295796c8dcSSimon Schubert else 11305796c8dcSSimon Schubert result_type = type1; 11315796c8dcSSimon Schubert 11325796c8dcSSimon Schubert if (TYPE_UNSIGNED (result_type)) 11335796c8dcSSimon Schubert { 11345796c8dcSSimon Schubert LONGEST v2_signed = value_as_long (arg2); 11355796c8dcSSimon Schubert ULONGEST v1, v2, v = 0; 1136cf7f2e2dSJohn Marino 11375796c8dcSSimon Schubert v1 = (ULONGEST) value_as_long (arg1); 11385796c8dcSSimon Schubert v2 = (ULONGEST) v2_signed; 11395796c8dcSSimon Schubert 11405796c8dcSSimon Schubert switch (op) 11415796c8dcSSimon Schubert { 11425796c8dcSSimon Schubert case BINOP_ADD: 11435796c8dcSSimon Schubert v = v1 + v2; 11445796c8dcSSimon Schubert break; 11455796c8dcSSimon Schubert 11465796c8dcSSimon Schubert case BINOP_SUB: 11475796c8dcSSimon Schubert v = v1 - v2; 11485796c8dcSSimon Schubert break; 11495796c8dcSSimon Schubert 11505796c8dcSSimon Schubert case BINOP_MUL: 11515796c8dcSSimon Schubert v = v1 * v2; 11525796c8dcSSimon Schubert break; 11535796c8dcSSimon Schubert 11545796c8dcSSimon Schubert case BINOP_DIV: 11555796c8dcSSimon Schubert case BINOP_INTDIV: 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_EXP: 11635796c8dcSSimon Schubert v = uinteger_pow (v1, v2_signed); 11645796c8dcSSimon Schubert break; 11655796c8dcSSimon Schubert 11665796c8dcSSimon Schubert case BINOP_REM: 11675796c8dcSSimon Schubert if (v2 != 0) 11685796c8dcSSimon Schubert v = v1 % v2; 11695796c8dcSSimon Schubert else 11705796c8dcSSimon Schubert error (_("Division by zero")); 11715796c8dcSSimon Schubert break; 11725796c8dcSSimon Schubert 11735796c8dcSSimon Schubert case BINOP_MOD: 11745796c8dcSSimon Schubert /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, 11755796c8dcSSimon Schubert v1 mod 0 has a defined value, v1. */ 11765796c8dcSSimon Schubert if (v2 == 0) 11775796c8dcSSimon Schubert { 11785796c8dcSSimon Schubert v = v1; 11795796c8dcSSimon Schubert } 11805796c8dcSSimon Schubert else 11815796c8dcSSimon Schubert { 11825796c8dcSSimon Schubert v = v1 / v2; 11835796c8dcSSimon Schubert /* Note floor(v1/v2) == v1/v2 for unsigned. */ 11845796c8dcSSimon Schubert v = v1 - (v2 * v); 11855796c8dcSSimon Schubert } 11865796c8dcSSimon Schubert break; 11875796c8dcSSimon Schubert 11885796c8dcSSimon Schubert case BINOP_LSH: 11895796c8dcSSimon Schubert v = v1 << v2; 11905796c8dcSSimon Schubert break; 11915796c8dcSSimon Schubert 11925796c8dcSSimon Schubert case BINOP_RSH: 11935796c8dcSSimon Schubert v = v1 >> v2; 11945796c8dcSSimon Schubert break; 11955796c8dcSSimon Schubert 11965796c8dcSSimon Schubert case BINOP_BITWISE_AND: 11975796c8dcSSimon Schubert v = v1 & v2; 11985796c8dcSSimon Schubert break; 11995796c8dcSSimon Schubert 12005796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 12015796c8dcSSimon Schubert v = v1 | v2; 12025796c8dcSSimon Schubert break; 12035796c8dcSSimon Schubert 12045796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 12055796c8dcSSimon Schubert v = v1 ^ v2; 12065796c8dcSSimon Schubert break; 12075796c8dcSSimon Schubert 12085796c8dcSSimon Schubert case BINOP_LOGICAL_AND: 12095796c8dcSSimon Schubert v = v1 && v2; 12105796c8dcSSimon Schubert break; 12115796c8dcSSimon Schubert 12125796c8dcSSimon Schubert case BINOP_LOGICAL_OR: 12135796c8dcSSimon Schubert v = v1 || v2; 12145796c8dcSSimon Schubert break; 12155796c8dcSSimon Schubert 12165796c8dcSSimon Schubert case BINOP_MIN: 12175796c8dcSSimon Schubert v = v1 < v2 ? v1 : v2; 12185796c8dcSSimon Schubert break; 12195796c8dcSSimon Schubert 12205796c8dcSSimon Schubert case BINOP_MAX: 12215796c8dcSSimon Schubert v = v1 > v2 ? v1 : v2; 12225796c8dcSSimon Schubert break; 12235796c8dcSSimon Schubert 12245796c8dcSSimon Schubert case BINOP_EQUAL: 12255796c8dcSSimon Schubert v = v1 == v2; 12265796c8dcSSimon Schubert break; 12275796c8dcSSimon Schubert 12285796c8dcSSimon Schubert case BINOP_NOTEQUAL: 12295796c8dcSSimon Schubert v = v1 != v2; 12305796c8dcSSimon Schubert break; 12315796c8dcSSimon Schubert 12325796c8dcSSimon Schubert case BINOP_LESS: 12335796c8dcSSimon Schubert v = v1 < v2; 12345796c8dcSSimon Schubert break; 12355796c8dcSSimon Schubert 1236cf7f2e2dSJohn Marino case BINOP_GTR: 1237cf7f2e2dSJohn Marino v = v1 > v2; 1238cf7f2e2dSJohn Marino break; 1239cf7f2e2dSJohn Marino 1240cf7f2e2dSJohn Marino case BINOP_LEQ: 1241cf7f2e2dSJohn Marino v = v1 <= v2; 1242cf7f2e2dSJohn Marino break; 1243cf7f2e2dSJohn Marino 1244cf7f2e2dSJohn Marino case BINOP_GEQ: 1245cf7f2e2dSJohn Marino v = v1 >= v2; 1246cf7f2e2dSJohn Marino break; 1247cf7f2e2dSJohn Marino 12485796c8dcSSimon Schubert default: 12495796c8dcSSimon Schubert error (_("Invalid binary operation on numbers.")); 12505796c8dcSSimon Schubert } 12515796c8dcSSimon Schubert 12525796c8dcSSimon Schubert val = allocate_value (result_type); 12535796c8dcSSimon Schubert store_unsigned_integer (value_contents_raw (val), 12545796c8dcSSimon Schubert TYPE_LENGTH (value_type (val)), 12555796c8dcSSimon Schubert gdbarch_byte_order 12565796c8dcSSimon Schubert (get_type_arch (result_type)), 12575796c8dcSSimon Schubert v); 12585796c8dcSSimon Schubert } 12595796c8dcSSimon Schubert else 12605796c8dcSSimon Schubert { 12615796c8dcSSimon Schubert LONGEST v1, v2, v = 0; 1262cf7f2e2dSJohn Marino 12635796c8dcSSimon Schubert v1 = value_as_long (arg1); 12645796c8dcSSimon Schubert v2 = value_as_long (arg2); 12655796c8dcSSimon Schubert 12665796c8dcSSimon Schubert switch (op) 12675796c8dcSSimon Schubert { 12685796c8dcSSimon Schubert case BINOP_ADD: 12695796c8dcSSimon Schubert v = v1 + v2; 12705796c8dcSSimon Schubert break; 12715796c8dcSSimon Schubert 12725796c8dcSSimon Schubert case BINOP_SUB: 12735796c8dcSSimon Schubert v = v1 - v2; 12745796c8dcSSimon Schubert break; 12755796c8dcSSimon Schubert 12765796c8dcSSimon Schubert case BINOP_MUL: 12775796c8dcSSimon Schubert v = v1 * v2; 12785796c8dcSSimon Schubert break; 12795796c8dcSSimon Schubert 12805796c8dcSSimon Schubert case BINOP_DIV: 12815796c8dcSSimon Schubert case BINOP_INTDIV: 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_EXP: 12895796c8dcSSimon Schubert v = integer_pow (v1, v2); 12905796c8dcSSimon Schubert break; 12915796c8dcSSimon Schubert 12925796c8dcSSimon Schubert case BINOP_REM: 12935796c8dcSSimon Schubert if (v2 != 0) 12945796c8dcSSimon Schubert v = v1 % v2; 12955796c8dcSSimon Schubert else 12965796c8dcSSimon Schubert error (_("Division by zero")); 12975796c8dcSSimon Schubert break; 12985796c8dcSSimon Schubert 12995796c8dcSSimon Schubert case BINOP_MOD: 13005796c8dcSSimon Schubert /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, 13015796c8dcSSimon Schubert X mod 0 has a defined value, X. */ 13025796c8dcSSimon Schubert if (v2 == 0) 13035796c8dcSSimon Schubert { 13045796c8dcSSimon Schubert v = v1; 13055796c8dcSSimon Schubert } 13065796c8dcSSimon Schubert else 13075796c8dcSSimon Schubert { 13085796c8dcSSimon Schubert v = v1 / v2; 13095796c8dcSSimon Schubert /* Compute floor. */ 13105796c8dcSSimon Schubert if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) 13115796c8dcSSimon Schubert { 13125796c8dcSSimon Schubert v--; 13135796c8dcSSimon Schubert } 13145796c8dcSSimon Schubert v = v1 - (v2 * v); 13155796c8dcSSimon Schubert } 13165796c8dcSSimon Schubert break; 13175796c8dcSSimon Schubert 13185796c8dcSSimon Schubert case BINOP_LSH: 13195796c8dcSSimon Schubert v = v1 << v2; 13205796c8dcSSimon Schubert break; 13215796c8dcSSimon Schubert 13225796c8dcSSimon Schubert case BINOP_RSH: 13235796c8dcSSimon Schubert v = v1 >> v2; 13245796c8dcSSimon Schubert break; 13255796c8dcSSimon Schubert 13265796c8dcSSimon Schubert case BINOP_BITWISE_AND: 13275796c8dcSSimon Schubert v = v1 & v2; 13285796c8dcSSimon Schubert break; 13295796c8dcSSimon Schubert 13305796c8dcSSimon Schubert case BINOP_BITWISE_IOR: 13315796c8dcSSimon Schubert v = v1 | v2; 13325796c8dcSSimon Schubert break; 13335796c8dcSSimon Schubert 13345796c8dcSSimon Schubert case BINOP_BITWISE_XOR: 13355796c8dcSSimon Schubert v = v1 ^ v2; 13365796c8dcSSimon Schubert break; 13375796c8dcSSimon Schubert 13385796c8dcSSimon Schubert case BINOP_LOGICAL_AND: 13395796c8dcSSimon Schubert v = v1 && v2; 13405796c8dcSSimon Schubert break; 13415796c8dcSSimon Schubert 13425796c8dcSSimon Schubert case BINOP_LOGICAL_OR: 13435796c8dcSSimon Schubert v = v1 || v2; 13445796c8dcSSimon Schubert break; 13455796c8dcSSimon Schubert 13465796c8dcSSimon Schubert case BINOP_MIN: 13475796c8dcSSimon Schubert v = v1 < v2 ? v1 : v2; 13485796c8dcSSimon Schubert break; 13495796c8dcSSimon Schubert 13505796c8dcSSimon Schubert case BINOP_MAX: 13515796c8dcSSimon Schubert v = v1 > v2 ? v1 : v2; 13525796c8dcSSimon Schubert break; 13535796c8dcSSimon Schubert 13545796c8dcSSimon Schubert case BINOP_EQUAL: 13555796c8dcSSimon Schubert v = v1 == v2; 13565796c8dcSSimon Schubert break; 13575796c8dcSSimon Schubert 1358cf7f2e2dSJohn Marino case BINOP_NOTEQUAL: 1359cf7f2e2dSJohn Marino v = v1 != v2; 1360cf7f2e2dSJohn Marino break; 1361cf7f2e2dSJohn Marino 13625796c8dcSSimon Schubert case BINOP_LESS: 13635796c8dcSSimon Schubert v = v1 < v2; 13645796c8dcSSimon Schubert break; 13655796c8dcSSimon Schubert 1366cf7f2e2dSJohn Marino case BINOP_GTR: 1367cf7f2e2dSJohn Marino v = v1 > v2; 1368cf7f2e2dSJohn Marino break; 1369cf7f2e2dSJohn Marino 1370cf7f2e2dSJohn Marino case BINOP_LEQ: 1371cf7f2e2dSJohn Marino v = v1 <= v2; 1372cf7f2e2dSJohn Marino break; 1373cf7f2e2dSJohn Marino 1374cf7f2e2dSJohn Marino case BINOP_GEQ: 1375cf7f2e2dSJohn Marino v = v1 >= v2; 1376cf7f2e2dSJohn Marino break; 1377cf7f2e2dSJohn Marino 13785796c8dcSSimon Schubert default: 13795796c8dcSSimon Schubert error (_("Invalid binary operation on numbers.")); 13805796c8dcSSimon Schubert } 13815796c8dcSSimon Schubert 13825796c8dcSSimon Schubert val = allocate_value (result_type); 13835796c8dcSSimon Schubert store_signed_integer (value_contents_raw (val), 13845796c8dcSSimon Schubert TYPE_LENGTH (value_type (val)), 13855796c8dcSSimon Schubert gdbarch_byte_order 13865796c8dcSSimon Schubert (get_type_arch (result_type)), 13875796c8dcSSimon Schubert v); 13885796c8dcSSimon Schubert } 13895796c8dcSSimon Schubert } 13905796c8dcSSimon Schubert 13915796c8dcSSimon Schubert return val; 13925796c8dcSSimon Schubert } 1393*c50c785cSJohn Marino 1394*c50c785cSJohn Marino /* Performs a binary operation on two vector operands by calling scalar_binop 1395*c50c785cSJohn Marino for each pair of vector components. */ 1396*c50c785cSJohn Marino 1397*c50c785cSJohn Marino static struct value * 1398*c50c785cSJohn Marino vector_binop (struct value *val1, struct value *val2, enum exp_opcode op) 1399*c50c785cSJohn Marino { 1400*c50c785cSJohn Marino struct value *val, *tmp, *mark; 1401*c50c785cSJohn Marino struct type *type1, *type2, *eltype1, *eltype2, *result_type; 1402*c50c785cSJohn Marino int t1_is_vec, t2_is_vec, elsize, i; 1403*c50c785cSJohn Marino LONGEST low_bound1, high_bound1, low_bound2, high_bound2; 1404*c50c785cSJohn Marino 1405*c50c785cSJohn Marino type1 = check_typedef (value_type (val1)); 1406*c50c785cSJohn Marino type2 = check_typedef (value_type (val2)); 1407*c50c785cSJohn Marino 1408*c50c785cSJohn Marino t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY 1409*c50c785cSJohn Marino && TYPE_VECTOR (type1)) ? 1 : 0; 1410*c50c785cSJohn Marino t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY 1411*c50c785cSJohn Marino && TYPE_VECTOR (type2)) ? 1 : 0; 1412*c50c785cSJohn Marino 1413*c50c785cSJohn Marino if (!t1_is_vec || !t2_is_vec) 1414*c50c785cSJohn Marino error (_("Vector operations are only supported among vectors")); 1415*c50c785cSJohn Marino 1416*c50c785cSJohn Marino if (!get_array_bounds (type1, &low_bound1, &high_bound1) 1417*c50c785cSJohn Marino || !get_array_bounds (type2, &low_bound2, &high_bound2)) 1418*c50c785cSJohn Marino error (_("Could not determine the vector bounds")); 1419*c50c785cSJohn Marino 1420*c50c785cSJohn Marino eltype1 = check_typedef (TYPE_TARGET_TYPE (type1)); 1421*c50c785cSJohn Marino eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)); 1422*c50c785cSJohn Marino elsize = TYPE_LENGTH (eltype1); 1423*c50c785cSJohn Marino 1424*c50c785cSJohn Marino if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2) 1425*c50c785cSJohn Marino || elsize != TYPE_LENGTH (eltype2) 1426*c50c785cSJohn Marino || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2) 1427*c50c785cSJohn Marino || low_bound1 != low_bound2 || high_bound1 != high_bound2) 1428*c50c785cSJohn Marino error (_("Cannot perform operation on vectors with different types")); 1429*c50c785cSJohn Marino 1430*c50c785cSJohn Marino val = allocate_value (type1); 1431*c50c785cSJohn Marino mark = value_mark (); 1432*c50c785cSJohn Marino for (i = 0; i < high_bound1 - low_bound1 + 1; i++) 1433*c50c785cSJohn Marino { 1434*c50c785cSJohn Marino tmp = value_binop (value_subscript (val1, i), 1435*c50c785cSJohn Marino value_subscript (val2, i), op); 1436*c50c785cSJohn Marino memcpy (value_contents_writeable (val) + i * elsize, 1437*c50c785cSJohn Marino value_contents_all (tmp), 1438*c50c785cSJohn Marino elsize); 1439*c50c785cSJohn Marino } 1440*c50c785cSJohn Marino value_free_to_mark (mark); 1441*c50c785cSJohn Marino 1442*c50c785cSJohn Marino return val; 1443*c50c785cSJohn Marino } 1444*c50c785cSJohn Marino 1445*c50c785cSJohn Marino /* Perform a binary operation on two operands. */ 1446*c50c785cSJohn Marino 1447*c50c785cSJohn Marino struct value * 1448*c50c785cSJohn Marino value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op) 1449*c50c785cSJohn Marino { 1450*c50c785cSJohn Marino struct value *val; 1451*c50c785cSJohn Marino struct type *type1 = check_typedef (value_type (arg1)); 1452*c50c785cSJohn Marino struct type *type2 = check_typedef (value_type (arg2)); 1453*c50c785cSJohn Marino int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY 1454*c50c785cSJohn Marino && TYPE_VECTOR (type1)); 1455*c50c785cSJohn Marino int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY 1456*c50c785cSJohn Marino && TYPE_VECTOR (type2)); 1457*c50c785cSJohn Marino 1458*c50c785cSJohn Marino if (!t1_is_vec && !t2_is_vec) 1459*c50c785cSJohn Marino val = scalar_binop (arg1, arg2, op); 1460*c50c785cSJohn Marino else if (t1_is_vec && t2_is_vec) 1461*c50c785cSJohn Marino val = vector_binop (arg1, arg2, op); 1462*c50c785cSJohn Marino else 1463*c50c785cSJohn Marino { 1464*c50c785cSJohn Marino /* Widen the scalar operand to a vector. */ 1465*c50c785cSJohn Marino struct value **v = t1_is_vec ? &arg2 : &arg1; 1466*c50c785cSJohn Marino struct type *t = t1_is_vec ? type2 : type1; 1467*c50c785cSJohn Marino 1468*c50c785cSJohn Marino if (TYPE_CODE (t) != TYPE_CODE_FLT 1469*c50c785cSJohn Marino && TYPE_CODE (t) != TYPE_CODE_DECFLOAT 1470*c50c785cSJohn Marino && !is_integral_type (t)) 1471*c50c785cSJohn Marino error (_("Argument to operation not a number or boolean.")); 1472*c50c785cSJohn Marino 1473*c50c785cSJohn Marino *v = value_cast (t1_is_vec ? type1 : type2, *v); 1474*c50c785cSJohn Marino val = vector_binop (arg1, arg2, op); 1475*c50c785cSJohn Marino } 1476*c50c785cSJohn Marino 1477*c50c785cSJohn Marino return val; 1478*c50c785cSJohn Marino } 14795796c8dcSSimon Schubert 14805796c8dcSSimon Schubert /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ 14815796c8dcSSimon Schubert 14825796c8dcSSimon Schubert int 14835796c8dcSSimon Schubert value_logical_not (struct value *arg1) 14845796c8dcSSimon Schubert { 14855796c8dcSSimon Schubert int len; 14865796c8dcSSimon Schubert const gdb_byte *p; 14875796c8dcSSimon Schubert struct type *type1; 14885796c8dcSSimon Schubert 14895796c8dcSSimon Schubert arg1 = coerce_array (arg1); 14905796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 14915796c8dcSSimon Schubert 14925796c8dcSSimon Schubert if (TYPE_CODE (type1) == TYPE_CODE_FLT) 14935796c8dcSSimon Schubert return 0 == value_as_double (arg1); 14945796c8dcSSimon Schubert else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT) 14955796c8dcSSimon Schubert return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1), 14965796c8dcSSimon Schubert gdbarch_byte_order (get_type_arch (type1))); 14975796c8dcSSimon Schubert 14985796c8dcSSimon Schubert len = TYPE_LENGTH (type1); 14995796c8dcSSimon Schubert p = value_contents (arg1); 15005796c8dcSSimon Schubert 15015796c8dcSSimon Schubert while (--len >= 0) 15025796c8dcSSimon Schubert { 15035796c8dcSSimon Schubert if (*p++) 15045796c8dcSSimon Schubert break; 15055796c8dcSSimon Schubert } 15065796c8dcSSimon Schubert 15075796c8dcSSimon Schubert return len < 0; 15085796c8dcSSimon Schubert } 15095796c8dcSSimon Schubert 15105796c8dcSSimon Schubert /* Perform a comparison on two string values (whose content are not 1511*c50c785cSJohn Marino necessarily null terminated) based on their length. */ 15125796c8dcSSimon Schubert 15135796c8dcSSimon Schubert static int 15145796c8dcSSimon Schubert value_strcmp (struct value *arg1, struct value *arg2) 15155796c8dcSSimon Schubert { 15165796c8dcSSimon Schubert int len1 = TYPE_LENGTH (value_type (arg1)); 15175796c8dcSSimon Schubert int len2 = TYPE_LENGTH (value_type (arg2)); 15185796c8dcSSimon Schubert const gdb_byte *s1 = value_contents (arg1); 15195796c8dcSSimon Schubert const gdb_byte *s2 = value_contents (arg2); 15205796c8dcSSimon Schubert int i, len = len1 < len2 ? len1 : len2; 15215796c8dcSSimon Schubert 15225796c8dcSSimon Schubert for (i = 0; i < len; i++) 15235796c8dcSSimon Schubert { 15245796c8dcSSimon Schubert if (s1[i] < s2[i]) 15255796c8dcSSimon Schubert return -1; 15265796c8dcSSimon Schubert else if (s1[i] > s2[i]) 15275796c8dcSSimon Schubert return 1; 15285796c8dcSSimon Schubert else 15295796c8dcSSimon Schubert continue; 15305796c8dcSSimon Schubert } 15315796c8dcSSimon Schubert 15325796c8dcSSimon Schubert if (len1 < len2) 15335796c8dcSSimon Schubert return -1; 15345796c8dcSSimon Schubert else if (len1 > len2) 15355796c8dcSSimon Schubert return 1; 15365796c8dcSSimon Schubert else 15375796c8dcSSimon Schubert return 0; 15385796c8dcSSimon Schubert } 15395796c8dcSSimon Schubert 15405796c8dcSSimon Schubert /* Simulate the C operator == by returning a 1 15415796c8dcSSimon Schubert iff ARG1 and ARG2 have equal contents. */ 15425796c8dcSSimon Schubert 15435796c8dcSSimon Schubert int 15445796c8dcSSimon Schubert value_equal (struct value *arg1, struct value *arg2) 15455796c8dcSSimon Schubert { 15465796c8dcSSimon Schubert int len; 15475796c8dcSSimon Schubert const gdb_byte *p1; 15485796c8dcSSimon Schubert const gdb_byte *p2; 15495796c8dcSSimon Schubert struct type *type1, *type2; 15505796c8dcSSimon Schubert enum type_code code1; 15515796c8dcSSimon Schubert enum type_code code2; 15525796c8dcSSimon Schubert int is_int1, is_int2; 15535796c8dcSSimon Schubert 15545796c8dcSSimon Schubert arg1 = coerce_array (arg1); 15555796c8dcSSimon Schubert arg2 = coerce_array (arg2); 15565796c8dcSSimon Schubert 15575796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 15585796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 15595796c8dcSSimon Schubert code1 = TYPE_CODE (type1); 15605796c8dcSSimon Schubert code2 = TYPE_CODE (type2); 15615796c8dcSSimon Schubert is_int1 = is_integral_type (type1); 15625796c8dcSSimon Schubert is_int2 = is_integral_type (type2); 15635796c8dcSSimon Schubert 15645796c8dcSSimon Schubert if (is_int1 && is_int2) 15655796c8dcSSimon Schubert return longest_to_int (value_as_long (value_binop (arg1, arg2, 15665796c8dcSSimon Schubert BINOP_EQUAL))); 15675796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_FLT || is_int1) 15685796c8dcSSimon Schubert && (code2 == TYPE_CODE_FLT || is_int2)) 15695796c8dcSSimon Schubert { 15705796c8dcSSimon Schubert /* NOTE: kettenis/20050816: Avoid compiler bug on systems where 15715796c8dcSSimon Schubert `long double' values are returned in static storage (m68k). */ 15725796c8dcSSimon Schubert DOUBLEST d = value_as_double (arg1); 1573cf7f2e2dSJohn Marino 15745796c8dcSSimon Schubert return d == value_as_double (arg2); 15755796c8dcSSimon Schubert } 15765796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) 15775796c8dcSSimon Schubert && (code2 == TYPE_CODE_DECFLOAT || is_int2)) 15785796c8dcSSimon Schubert { 15795796c8dcSSimon Schubert gdb_byte v1[16], v2[16]; 15805796c8dcSSimon Schubert int len_v1, len_v2; 15815796c8dcSSimon Schubert enum bfd_endian byte_order_v1, byte_order_v2; 15825796c8dcSSimon Schubert 15835796c8dcSSimon Schubert value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, 15845796c8dcSSimon Schubert v2, &len_v2, &byte_order_v2); 15855796c8dcSSimon Schubert 15865796c8dcSSimon Schubert return decimal_compare (v1, len_v1, byte_order_v1, 15875796c8dcSSimon Schubert v2, len_v2, byte_order_v2) == 0; 15885796c8dcSSimon Schubert } 15895796c8dcSSimon Schubert 15905796c8dcSSimon Schubert /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever 15915796c8dcSSimon Schubert is bigger. */ 15925796c8dcSSimon Schubert else if (code1 == TYPE_CODE_PTR && is_int2) 15935796c8dcSSimon Schubert return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2); 15945796c8dcSSimon Schubert else if (code2 == TYPE_CODE_PTR && is_int1) 15955796c8dcSSimon Schubert return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2); 15965796c8dcSSimon Schubert 15975796c8dcSSimon Schubert else if (code1 == code2 15985796c8dcSSimon Schubert && ((len = (int) TYPE_LENGTH (type1)) 15995796c8dcSSimon Schubert == (int) TYPE_LENGTH (type2))) 16005796c8dcSSimon Schubert { 16015796c8dcSSimon Schubert p1 = value_contents (arg1); 16025796c8dcSSimon Schubert p2 = value_contents (arg2); 16035796c8dcSSimon Schubert while (--len >= 0) 16045796c8dcSSimon Schubert { 16055796c8dcSSimon Schubert if (*p1++ != *p2++) 16065796c8dcSSimon Schubert break; 16075796c8dcSSimon Schubert } 16085796c8dcSSimon Schubert return len < 0; 16095796c8dcSSimon Schubert } 16105796c8dcSSimon Schubert else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) 16115796c8dcSSimon Schubert { 16125796c8dcSSimon Schubert return value_strcmp (arg1, arg2) == 0; 16135796c8dcSSimon Schubert } 16145796c8dcSSimon Schubert else 16155796c8dcSSimon Schubert { 16165796c8dcSSimon Schubert error (_("Invalid type combination in equality test.")); 1617*c50c785cSJohn Marino return 0; /* For lint -- never reached. */ 16185796c8dcSSimon Schubert } 16195796c8dcSSimon Schubert } 16205796c8dcSSimon Schubert 1621cf7f2e2dSJohn Marino /* Compare values based on their raw contents. Useful for arrays since 1622cf7f2e2dSJohn Marino value_equal coerces them to pointers, thus comparing just the address 1623cf7f2e2dSJohn Marino of the array instead of its contents. */ 1624cf7f2e2dSJohn Marino 1625cf7f2e2dSJohn Marino int 1626cf7f2e2dSJohn Marino value_equal_contents (struct value *arg1, struct value *arg2) 1627cf7f2e2dSJohn Marino { 1628cf7f2e2dSJohn Marino struct type *type1, *type2; 1629cf7f2e2dSJohn Marino 1630cf7f2e2dSJohn Marino type1 = check_typedef (value_type (arg1)); 1631cf7f2e2dSJohn Marino type2 = check_typedef (value_type (arg2)); 1632cf7f2e2dSJohn Marino 1633cf7f2e2dSJohn Marino return (TYPE_CODE (type1) == TYPE_CODE (type2) 1634cf7f2e2dSJohn Marino && TYPE_LENGTH (type1) == TYPE_LENGTH (type2) 1635cf7f2e2dSJohn Marino && memcmp (value_contents (arg1), value_contents (arg2), 1636cf7f2e2dSJohn Marino TYPE_LENGTH (type1)) == 0); 1637cf7f2e2dSJohn Marino } 1638cf7f2e2dSJohn Marino 16395796c8dcSSimon Schubert /* Simulate the C operator < by returning 1 16405796c8dcSSimon Schubert iff ARG1's contents are less than ARG2's. */ 16415796c8dcSSimon Schubert 16425796c8dcSSimon Schubert int 16435796c8dcSSimon Schubert value_less (struct value *arg1, struct value *arg2) 16445796c8dcSSimon Schubert { 16455796c8dcSSimon Schubert enum type_code code1; 16465796c8dcSSimon Schubert enum type_code code2; 16475796c8dcSSimon Schubert struct type *type1, *type2; 16485796c8dcSSimon Schubert int is_int1, is_int2; 16495796c8dcSSimon Schubert 16505796c8dcSSimon Schubert arg1 = coerce_array (arg1); 16515796c8dcSSimon Schubert arg2 = coerce_array (arg2); 16525796c8dcSSimon Schubert 16535796c8dcSSimon Schubert type1 = check_typedef (value_type (arg1)); 16545796c8dcSSimon Schubert type2 = check_typedef (value_type (arg2)); 16555796c8dcSSimon Schubert code1 = TYPE_CODE (type1); 16565796c8dcSSimon Schubert code2 = TYPE_CODE (type2); 16575796c8dcSSimon Schubert is_int1 = is_integral_type (type1); 16585796c8dcSSimon Schubert is_int2 = is_integral_type (type2); 16595796c8dcSSimon Schubert 16605796c8dcSSimon Schubert if (is_int1 && is_int2) 16615796c8dcSSimon Schubert return longest_to_int (value_as_long (value_binop (arg1, arg2, 16625796c8dcSSimon Schubert BINOP_LESS))); 16635796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_FLT || is_int1) 16645796c8dcSSimon Schubert && (code2 == TYPE_CODE_FLT || is_int2)) 16655796c8dcSSimon Schubert { 16665796c8dcSSimon Schubert /* NOTE: kettenis/20050816: Avoid compiler bug on systems where 16675796c8dcSSimon Schubert `long double' values are returned in static storage (m68k). */ 16685796c8dcSSimon Schubert DOUBLEST d = value_as_double (arg1); 1669cf7f2e2dSJohn Marino 16705796c8dcSSimon Schubert return d < value_as_double (arg2); 16715796c8dcSSimon Schubert } 16725796c8dcSSimon Schubert else if ((code1 == TYPE_CODE_DECFLOAT || is_int1) 16735796c8dcSSimon Schubert && (code2 == TYPE_CODE_DECFLOAT || is_int2)) 16745796c8dcSSimon Schubert { 16755796c8dcSSimon Schubert gdb_byte v1[16], v2[16]; 16765796c8dcSSimon Schubert int len_v1, len_v2; 16775796c8dcSSimon Schubert enum bfd_endian byte_order_v1, byte_order_v2; 16785796c8dcSSimon Schubert 16795796c8dcSSimon Schubert value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1, 16805796c8dcSSimon Schubert v2, &len_v2, &byte_order_v2); 16815796c8dcSSimon Schubert 16825796c8dcSSimon Schubert return decimal_compare (v1, len_v1, byte_order_v1, 16835796c8dcSSimon Schubert v2, len_v2, byte_order_v2) == -1; 16845796c8dcSSimon Schubert } 16855796c8dcSSimon Schubert else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) 16865796c8dcSSimon Schubert return value_as_address (arg1) < value_as_address (arg2); 16875796c8dcSSimon Schubert 16885796c8dcSSimon Schubert /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever 16895796c8dcSSimon Schubert is bigger. */ 16905796c8dcSSimon Schubert else if (code1 == TYPE_CODE_PTR && is_int2) 16915796c8dcSSimon Schubert return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2); 16925796c8dcSSimon Schubert else if (code2 == TYPE_CODE_PTR && is_int1) 16935796c8dcSSimon Schubert return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2); 16945796c8dcSSimon Schubert else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING) 16955796c8dcSSimon Schubert return value_strcmp (arg1, arg2) < 0; 16965796c8dcSSimon Schubert else 16975796c8dcSSimon Schubert { 16985796c8dcSSimon Schubert error (_("Invalid type combination in ordering comparison.")); 16995796c8dcSSimon Schubert return 0; 17005796c8dcSSimon Schubert } 17015796c8dcSSimon Schubert } 17025796c8dcSSimon Schubert 17035796c8dcSSimon Schubert /* The unary operators +, - and ~. They free the argument ARG1. */ 17045796c8dcSSimon Schubert 17055796c8dcSSimon Schubert struct value * 17065796c8dcSSimon Schubert value_pos (struct value *arg1) 17075796c8dcSSimon Schubert { 17085796c8dcSSimon Schubert struct type *type; 17095796c8dcSSimon Schubert 17105796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 17115796c8dcSSimon Schubert type = check_typedef (value_type (arg1)); 17125796c8dcSSimon Schubert 17135796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_FLT) 17145796c8dcSSimon Schubert return value_from_double (type, value_as_double (arg1)); 17155796c8dcSSimon Schubert else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) 17165796c8dcSSimon Schubert return value_from_decfloat (type, value_contents (arg1)); 17175796c8dcSSimon Schubert else if (is_integral_type (type)) 17185796c8dcSSimon Schubert { 17195796c8dcSSimon Schubert return value_from_longest (type, value_as_long (arg1)); 17205796c8dcSSimon Schubert } 1721*c50c785cSJohn Marino else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) 1722*c50c785cSJohn Marino { 1723*c50c785cSJohn Marino struct value *val = allocate_value (type); 1724*c50c785cSJohn Marino 1725*c50c785cSJohn Marino memcpy (value_contents_raw (val), value_contents (arg1), 1726*c50c785cSJohn Marino TYPE_LENGTH (type)); 1727*c50c785cSJohn Marino return val; 1728*c50c785cSJohn Marino } 17295796c8dcSSimon Schubert else 17305796c8dcSSimon Schubert { 1731*c50c785cSJohn Marino error (_("Argument to positive operation not a number.")); 1732*c50c785cSJohn Marino return 0; /* For lint -- never reached. */ 17335796c8dcSSimon Schubert } 17345796c8dcSSimon Schubert } 17355796c8dcSSimon Schubert 17365796c8dcSSimon Schubert struct value * 17375796c8dcSSimon Schubert value_neg (struct value *arg1) 17385796c8dcSSimon Schubert { 17395796c8dcSSimon Schubert struct type *type; 17405796c8dcSSimon Schubert 17415796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 17425796c8dcSSimon Schubert type = check_typedef (value_type (arg1)); 17435796c8dcSSimon Schubert 17445796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) 17455796c8dcSSimon Schubert { 17465796c8dcSSimon Schubert struct value *val = allocate_value (type); 17475796c8dcSSimon Schubert int len = TYPE_LENGTH (type); 1748*c50c785cSJohn Marino gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */ 17495796c8dcSSimon Schubert 17505796c8dcSSimon Schubert memcpy (decbytes, value_contents (arg1), len); 17515796c8dcSSimon Schubert 17525796c8dcSSimon Schubert if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE) 17535796c8dcSSimon Schubert decbytes[len-1] = decbytes[len - 1] | 0x80; 17545796c8dcSSimon Schubert else 17555796c8dcSSimon Schubert decbytes[0] = decbytes[0] | 0x80; 17565796c8dcSSimon Schubert 17575796c8dcSSimon Schubert memcpy (value_contents_raw (val), decbytes, len); 17585796c8dcSSimon Schubert return val; 17595796c8dcSSimon Schubert } 17605796c8dcSSimon Schubert else if (TYPE_CODE (type) == TYPE_CODE_FLT) 17615796c8dcSSimon Schubert return value_from_double (type, -value_as_double (arg1)); 17625796c8dcSSimon Schubert else if (is_integral_type (type)) 17635796c8dcSSimon Schubert { 17645796c8dcSSimon Schubert return value_from_longest (type, -value_as_long (arg1)); 17655796c8dcSSimon Schubert } 1766*c50c785cSJohn Marino else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) 1767*c50c785cSJohn Marino { 1768*c50c785cSJohn Marino struct value *tmp, *val = allocate_value (type); 1769*c50c785cSJohn Marino struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type)); 1770*c50c785cSJohn Marino int i; 1771*c50c785cSJohn Marino LONGEST low_bound, high_bound; 1772*c50c785cSJohn Marino 1773*c50c785cSJohn Marino if (!get_array_bounds (type, &low_bound, &high_bound)) 1774*c50c785cSJohn Marino error (_("Could not determine the vector bounds")); 1775*c50c785cSJohn Marino 1776*c50c785cSJohn Marino for (i = 0; i < high_bound - low_bound + 1; i++) 1777*c50c785cSJohn Marino { 1778*c50c785cSJohn Marino tmp = value_neg (value_subscript (arg1, i)); 1779*c50c785cSJohn Marino memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype), 1780*c50c785cSJohn Marino value_contents_all (tmp), TYPE_LENGTH (eltype)); 1781*c50c785cSJohn Marino } 1782*c50c785cSJohn Marino return val; 1783*c50c785cSJohn Marino } 17845796c8dcSSimon Schubert else 17855796c8dcSSimon Schubert { 17865796c8dcSSimon Schubert error (_("Argument to negate operation not a number.")); 1787*c50c785cSJohn Marino return 0; /* For lint -- never reached. */ 17885796c8dcSSimon Schubert } 17895796c8dcSSimon Schubert } 17905796c8dcSSimon Schubert 17915796c8dcSSimon Schubert struct value * 17925796c8dcSSimon Schubert value_complement (struct value *arg1) 17935796c8dcSSimon Schubert { 17945796c8dcSSimon Schubert struct type *type; 1795*c50c785cSJohn Marino struct value *val; 17965796c8dcSSimon Schubert 17975796c8dcSSimon Schubert arg1 = coerce_ref (arg1); 17985796c8dcSSimon Schubert type = check_typedef (value_type (arg1)); 17995796c8dcSSimon Schubert 1800*c50c785cSJohn Marino if (is_integral_type (type)) 1801*c50c785cSJohn Marino val = value_from_longest (type, ~value_as_long (arg1)); 1802*c50c785cSJohn Marino else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)) 1803*c50c785cSJohn Marino { 1804*c50c785cSJohn Marino struct value *tmp; 1805*c50c785cSJohn Marino struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type)); 1806*c50c785cSJohn Marino int i; 1807*c50c785cSJohn Marino LONGEST low_bound, high_bound; 18085796c8dcSSimon Schubert 1809*c50c785cSJohn Marino if (!get_array_bounds (type, &low_bound, &high_bound)) 1810*c50c785cSJohn Marino error (_("Could not determine the vector bounds")); 1811*c50c785cSJohn Marino 1812*c50c785cSJohn Marino val = allocate_value (type); 1813*c50c785cSJohn Marino for (i = 0; i < high_bound - low_bound + 1; i++) 1814*c50c785cSJohn Marino { 1815*c50c785cSJohn Marino tmp = value_complement (value_subscript (arg1, i)); 1816*c50c785cSJohn Marino memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype), 1817*c50c785cSJohn Marino value_contents_all (tmp), TYPE_LENGTH (eltype)); 1818*c50c785cSJohn Marino } 1819*c50c785cSJohn Marino } 1820*c50c785cSJohn Marino else 1821*c50c785cSJohn Marino error (_("Argument to complement operation not an integer, boolean.")); 1822*c50c785cSJohn Marino 1823*c50c785cSJohn Marino return val; 18245796c8dcSSimon Schubert } 18255796c8dcSSimon Schubert 18265796c8dcSSimon Schubert /* The INDEX'th bit of SET value whose value_type is TYPE, 18275796c8dcSSimon Schubert and whose value_contents is valaddr. 18285796c8dcSSimon Schubert Return -1 if out of range, -2 other error. */ 18295796c8dcSSimon Schubert 18305796c8dcSSimon Schubert int 18315796c8dcSSimon Schubert value_bit_index (struct type *type, const gdb_byte *valaddr, int index) 18325796c8dcSSimon Schubert { 18335796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (type); 18345796c8dcSSimon Schubert LONGEST low_bound, high_bound; 18355796c8dcSSimon Schubert LONGEST word; 18365796c8dcSSimon Schubert unsigned rel_index; 18375796c8dcSSimon Schubert struct type *range = TYPE_INDEX_TYPE (type); 1838cf7f2e2dSJohn Marino 18395796c8dcSSimon Schubert if (get_discrete_bounds (range, &low_bound, &high_bound) < 0) 18405796c8dcSSimon Schubert return -2; 18415796c8dcSSimon Schubert if (index < low_bound || index > high_bound) 18425796c8dcSSimon Schubert return -1; 18435796c8dcSSimon Schubert rel_index = index - low_bound; 18445796c8dcSSimon Schubert word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1, 18455796c8dcSSimon Schubert gdbarch_byte_order (gdbarch)); 18465796c8dcSSimon Schubert rel_index %= TARGET_CHAR_BIT; 18475796c8dcSSimon Schubert if (gdbarch_bits_big_endian (gdbarch)) 18485796c8dcSSimon Schubert rel_index = TARGET_CHAR_BIT - 1 - rel_index; 18495796c8dcSSimon Schubert return (word >> rel_index) & 1; 18505796c8dcSSimon Schubert } 18515796c8dcSSimon Schubert 18525796c8dcSSimon Schubert int 18535796c8dcSSimon Schubert value_in (struct value *element, struct value *set) 18545796c8dcSSimon Schubert { 18555796c8dcSSimon Schubert int member; 18565796c8dcSSimon Schubert struct type *settype = check_typedef (value_type (set)); 18575796c8dcSSimon Schubert struct type *eltype = check_typedef (value_type (element)); 1858cf7f2e2dSJohn Marino 18595796c8dcSSimon Schubert if (TYPE_CODE (eltype) == TYPE_CODE_RANGE) 18605796c8dcSSimon Schubert eltype = TYPE_TARGET_TYPE (eltype); 18615796c8dcSSimon Schubert if (TYPE_CODE (settype) != TYPE_CODE_SET) 18625796c8dcSSimon Schubert error (_("Second argument of 'IN' has wrong type")); 18635796c8dcSSimon Schubert if (TYPE_CODE (eltype) != TYPE_CODE_INT 18645796c8dcSSimon Schubert && TYPE_CODE (eltype) != TYPE_CODE_CHAR 18655796c8dcSSimon Schubert && TYPE_CODE (eltype) != TYPE_CODE_ENUM 18665796c8dcSSimon Schubert && TYPE_CODE (eltype) != TYPE_CODE_BOOL) 18675796c8dcSSimon Schubert error (_("First argument of 'IN' has wrong type")); 18685796c8dcSSimon Schubert member = value_bit_index (settype, value_contents (set), 18695796c8dcSSimon Schubert value_as_long (element)); 18705796c8dcSSimon Schubert if (member < 0) 18715796c8dcSSimon Schubert error (_("First argument of 'IN' not in range")); 18725796c8dcSSimon Schubert return member; 18735796c8dcSSimon Schubert } 18745796c8dcSSimon Schubert 18755796c8dcSSimon Schubert void 18765796c8dcSSimon Schubert _initialize_valarith (void) 18775796c8dcSSimon Schubert { 18785796c8dcSSimon Schubert } 1879