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