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