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