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