| 1 | /* Perform arithmetic and other operations on values, for GDB. |
| 2 | Copyright 1986, 1989, 1991, 1992, 1993, 1994 |
| 3 | 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 2 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, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "value.h" |
| 23 | #include "symtab.h" |
| 24 | #include "gdbtypes.h" |
| 25 | #include "expression.h" |
| 26 | #include "target.h" |
| 27 | #include "language.h" |
| 28 | #include "demangle.h" |
| 29 | #include <string.h> |
| 30 | |
| 31 | /* Define whether or not the C operator '/' truncates towards zero for |
| 32 | differently signed operands (truncation direction is undefined in C). */ |
| 33 | |
| 34 | #ifndef TRUNCATION_TOWARDS_ZERO |
| 35 | #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2) |
| 36 | #endif |
| 37 | |
| 38 | static value_ptr value_subscripted_rvalue PARAMS ((value_ptr, value_ptr)); |
| 39 | |
| 40 | \f |
| 41 | value_ptr |
| 42 | value_add (arg1, arg2) |
| 43 | value_ptr arg1, arg2; |
| 44 | { |
| 45 | register value_ptr valint, valptr; |
| 46 | register int len; |
| 47 | |
| 48 | COERCE_ARRAY (arg1); |
| 49 | COERCE_ARRAY (arg2); |
| 50 | |
| 51 | if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR |
| 52 | || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR) |
| 53 | && |
| 54 | (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT |
| 55 | || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)) |
| 56 | /* Exactly one argument is a pointer, and one is an integer. */ |
| 57 | { |
| 58 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) |
| 59 | { |
| 60 | valptr = arg1; |
| 61 | valint = arg2; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | valptr = arg2; |
| 66 | valint = arg1; |
| 67 | } |
| 68 | len = TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr))); |
| 69 | if (len == 0) len = 1; /* For (void *) */ |
| 70 | return value_from_longest (VALUE_TYPE (valptr), |
| 71 | value_as_long (valptr) |
| 72 | + (len * value_as_long (valint))); |
| 73 | } |
| 74 | |
| 75 | return value_binop (arg1, arg2, BINOP_ADD); |
| 76 | } |
| 77 | |
| 78 | value_ptr |
| 79 | value_sub (arg1, arg2) |
| 80 | value_ptr arg1, arg2; |
| 81 | { |
| 82 | |
| 83 | COERCE_ARRAY (arg1); |
| 84 | COERCE_ARRAY (arg2); |
| 85 | |
| 86 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR) |
| 87 | { |
| 88 | if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT) |
| 89 | { |
| 90 | /* pointer - integer. */ |
| 91 | return value_from_longest |
| 92 | (VALUE_TYPE (arg1), |
| 93 | value_as_long (arg1) |
| 94 | - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) |
| 95 | * value_as_long (arg2))); |
| 96 | } |
| 97 | else if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR |
| 98 | && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) |
| 99 | == TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))) |
| 100 | { |
| 101 | /* pointer to <type x> - pointer to <type x>. */ |
| 102 | return value_from_longest |
| 103 | (builtin_type_long, /* FIXME -- should be ptrdiff_t */ |
| 104 | (value_as_long (arg1) - value_as_long (arg2)) |
| 105 | / (LONGEST) (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))))); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | error ("\ |
| 110 | First argument of `-' is a pointer and second argument is neither\n\ |
| 111 | an integer nor a pointer of the same type."); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return value_binop (arg1, arg2, BINOP_SUB); |
| 116 | } |
| 117 | |
| 118 | /* Return the value of ARRAY[IDX]. |
| 119 | See comments in value_coerce_array() for rationale for reason for |
| 120 | doing lower bounds adjustment here rather than there. |
| 121 | FIXME: Perhaps we should validate that the index is valid and if |
| 122 | verbosity is set, warn about invalid indices (but still use them). */ |
| 123 | |
| 124 | value_ptr |
| 125 | value_subscript (array, idx) |
| 126 | value_ptr array, idx; |
| 127 | { |
| 128 | int lowerbound; |
| 129 | value_ptr bound; |
| 130 | struct type *range_type; |
| 131 | |
| 132 | COERCE_REF (array); |
| 133 | |
| 134 | if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY |
| 135 | || TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_STRING) |
| 136 | { |
| 137 | range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0); |
| 138 | lowerbound = TYPE_FIELD_BITPOS (range_type, 0); |
| 139 | if (lowerbound != 0) |
| 140 | { |
| 141 | bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound); |
| 142 | idx = value_sub (idx, bound); |
| 143 | } |
| 144 | if (VALUE_LVAL (array) != lval_memory) |
| 145 | { |
| 146 | return value_subscripted_rvalue (array, idx); |
| 147 | } |
| 148 | array = value_coerce_array (array); |
| 149 | } |
| 150 | return value_ind (value_add (array, idx)); |
| 151 | } |
| 152 | |
| 153 | /* Return the value of EXPR[IDX], expr an aggregate rvalue |
| 154 | (eg, a vector register). This routine used to promote floats |
| 155 | to doubles, but no longer does. */ |
| 156 | |
| 157 | static value_ptr |
| 158 | value_subscripted_rvalue (array, idx) |
| 159 | value_ptr array, idx; |
| 160 | { |
| 161 | struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array)); |
| 162 | int elt_size = TYPE_LENGTH (elt_type); |
| 163 | int elt_offs = elt_size * longest_to_int (value_as_long (idx)); |
| 164 | value_ptr v; |
| 165 | |
| 166 | if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array))) |
| 167 | error ("no such vector element"); |
| 168 | |
| 169 | v = allocate_value (elt_type); |
| 170 | memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size); |
| 171 | |
| 172 | if (VALUE_LVAL (array) == lval_internalvar) |
| 173 | VALUE_LVAL (v) = lval_internalvar_component; |
| 174 | else |
| 175 | VALUE_LVAL (v) = not_lval; |
| 176 | VALUE_ADDRESS (v) = VALUE_ADDRESS (array); |
| 177 | VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs; |
| 178 | VALUE_BITSIZE (v) = elt_size * 8; |
| 179 | return v; |
| 180 | } |
| 181 | \f |
| 182 | /* Check to see if either argument is a structure. This is called so |
| 183 | we know whether to go ahead with the normal binop or look for a |
| 184 | user defined function instead. |
| 185 | |
| 186 | For now, we do not overload the `=' operator. */ |
| 187 | |
| 188 | int |
| 189 | binop_user_defined_p (op, arg1, arg2) |
| 190 | enum exp_opcode op; |
| 191 | value_ptr arg1, arg2; |
| 192 | { |
| 193 | if (op == BINOP_ASSIGN) |
| 194 | return 0; |
| 195 | return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT |
| 196 | || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT |
| 197 | || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF |
| 198 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT) |
| 199 | || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF |
| 200 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT)); |
| 201 | } |
| 202 | |
| 203 | /* Check to see if argument is a structure. This is called so |
| 204 | we know whether to go ahead with the normal unop or look for a |
| 205 | user defined function instead. |
| 206 | |
| 207 | For now, we do not overload the `&' operator. */ |
| 208 | |
| 209 | int unop_user_defined_p (op, arg1) |
| 210 | enum exp_opcode op; |
| 211 | value_ptr arg1; |
| 212 | { |
| 213 | if (op == UNOP_ADDR) |
| 214 | return 0; |
| 215 | return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT |
| 216 | || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF |
| 217 | && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)); |
| 218 | } |
| 219 | |
| 220 | /* We know either arg1 or arg2 is a structure, so try to find the right |
| 221 | user defined function. Create an argument vector that calls |
| 222 | arg1.operator @ (arg1,arg2) and return that value (where '@' is any |
| 223 | binary operator which is legal for GNU C++). |
| 224 | |
| 225 | OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP |
| 226 | is the opcode saying how to modify it. Otherwise, OTHEROP is |
| 227 | unused. */ |
| 228 | |
| 229 | value_ptr |
| 230 | value_x_binop (arg1, arg2, op, otherop) |
| 231 | value_ptr arg1, arg2; |
| 232 | enum exp_opcode op, otherop; |
| 233 | { |
| 234 | value_ptr * argvec; |
| 235 | char *ptr; |
| 236 | char tstr[13]; |
| 237 | int static_memfuncp; |
| 238 | |
| 239 | COERCE_REF (arg1); |
| 240 | COERCE_REF (arg2); |
| 241 | COERCE_ENUM (arg1); |
| 242 | COERCE_ENUM (arg2); |
| 243 | |
| 244 | /* now we know that what we have to do is construct our |
| 245 | arg vector and find the right function to call it with. */ |
| 246 | |
| 247 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT) |
| 248 | error ("Can't do that binary op on that type"); /* FIXME be explicit */ |
| 249 | |
| 250 | argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4); |
| 251 | argvec[1] = value_addr (arg1); |
| 252 | argvec[2] = arg2; |
| 253 | argvec[3] = 0; |
| 254 | |
| 255 | /* make the right function name up */ |
| 256 | strcpy(tstr, "operator__"); |
| 257 | ptr = tstr+8; |
| 258 | switch (op) |
| 259 | { |
| 260 | case BINOP_ADD: strcpy(ptr,"+"); break; |
| 261 | case BINOP_SUB: strcpy(ptr,"-"); break; |
| 262 | case BINOP_MUL: strcpy(ptr,"*"); break; |
| 263 | case BINOP_DIV: strcpy(ptr,"/"); break; |
| 264 | case BINOP_REM: strcpy(ptr,"%"); break; |
| 265 | case BINOP_LSH: strcpy(ptr,"<<"); break; |
| 266 | case BINOP_RSH: strcpy(ptr,">>"); break; |
| 267 | case BINOP_BITWISE_AND: strcpy(ptr,"&"); break; |
| 268 | case BINOP_BITWISE_IOR: strcpy(ptr,"|"); break; |
| 269 | case BINOP_BITWISE_XOR: strcpy(ptr,"^"); break; |
| 270 | case BINOP_LOGICAL_AND: strcpy(ptr,"&&"); break; |
| 271 | case BINOP_LOGICAL_OR: strcpy(ptr,"||"); break; |
| 272 | case BINOP_MIN: strcpy(ptr,"<?"); break; |
| 273 | case BINOP_MAX: strcpy(ptr,">?"); break; |
| 274 | case BINOP_ASSIGN: strcpy(ptr,"="); break; |
| 275 | case BINOP_ASSIGN_MODIFY: |
| 276 | switch (otherop) |
| 277 | { |
| 278 | case BINOP_ADD: strcpy(ptr,"+="); break; |
| 279 | case BINOP_SUB: strcpy(ptr,"-="); break; |
| 280 | case BINOP_MUL: strcpy(ptr,"*="); break; |
| 281 | case BINOP_DIV: strcpy(ptr,"/="); break; |
| 282 | case BINOP_REM: strcpy(ptr,"%="); break; |
| 283 | case BINOP_BITWISE_AND: strcpy(ptr,"&="); break; |
| 284 | case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break; |
| 285 | case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break; |
| 286 | case BINOP_MOD: /* invalid */ |
| 287 | default: |
| 288 | error ("Invalid binary operation specified."); |
| 289 | } |
| 290 | break; |
| 291 | case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break; |
| 292 | case BINOP_EQUAL: strcpy(ptr,"=="); break; |
| 293 | case BINOP_NOTEQUAL: strcpy(ptr,"!="); break; |
| 294 | case BINOP_LESS: strcpy(ptr,"<"); break; |
| 295 | case BINOP_GTR: strcpy(ptr,">"); break; |
| 296 | case BINOP_GEQ: strcpy(ptr,">="); break; |
| 297 | case BINOP_LEQ: strcpy(ptr,"<="); break; |
| 298 | case BINOP_MOD: /* invalid */ |
| 299 | default: |
| 300 | error ("Invalid binary operation specified."); |
| 301 | } |
| 302 | |
| 303 | argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure"); |
| 304 | |
| 305 | if (argvec[0]) |
| 306 | { |
| 307 | if (static_memfuncp) |
| 308 | { |
| 309 | argvec[1] = argvec[0]; |
| 310 | argvec++; |
| 311 | } |
| 312 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); |
| 313 | } |
| 314 | error ("member function %s not found", tstr); |
| 315 | #ifdef lint |
| 316 | return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1); |
| 317 | #endif |
| 318 | } |
| 319 | |
| 320 | /* We know that arg1 is a structure, so try to find a unary user |
| 321 | defined operator that matches the operator in question. |
| 322 | Create an argument vector that calls arg1.operator @ (arg1) |
| 323 | and return that value (where '@' is (almost) any unary operator which |
| 324 | is legal for GNU C++). */ |
| 325 | |
| 326 | value_ptr |
| 327 | value_x_unop (arg1, op) |
| 328 | value_ptr arg1; |
| 329 | enum exp_opcode op; |
| 330 | { |
| 331 | value_ptr * argvec; |
| 332 | char *ptr, *mangle_ptr; |
| 333 | char tstr[13], mangle_tstr[13]; |
| 334 | int static_memfuncp; |
| 335 | |
| 336 | COERCE_ENUM (arg1); |
| 337 | |
| 338 | /* now we know that what we have to do is construct our |
| 339 | arg vector and find the right function to call it with. */ |
| 340 | |
| 341 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT) |
| 342 | error ("Can't do that unary op on that type"); /* FIXME be explicit */ |
| 343 | |
| 344 | argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3); |
| 345 | argvec[1] = value_addr (arg1); |
| 346 | argvec[2] = 0; |
| 347 | |
| 348 | /* make the right function name up */ |
| 349 | strcpy(tstr,"operator__"); |
| 350 | ptr = tstr+8; |
| 351 | strcpy(mangle_tstr, "__"); |
| 352 | mangle_ptr = mangle_tstr+2; |
| 353 | switch (op) |
| 354 | { |
| 355 | case UNOP_PREINCREMENT: strcpy(ptr,"++"); break; |
| 356 | case UNOP_PREDECREMENT: strcpy(ptr,"++"); break; |
| 357 | case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break; |
| 358 | case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break; |
| 359 | case UNOP_LOGICAL_NOT: strcpy(ptr,"!"); break; |
| 360 | case UNOP_COMPLEMENT: strcpy(ptr,"~"); break; |
| 361 | case UNOP_NEG: strcpy(ptr,"-"); break; |
| 362 | default: |
| 363 | error ("Invalid binary operation specified."); |
| 364 | } |
| 365 | |
| 366 | argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure"); |
| 367 | |
| 368 | if (argvec[0]) |
| 369 | { |
| 370 | if (static_memfuncp) |
| 371 | { |
| 372 | argvec[1] = argvec[0]; |
| 373 | argvec++; |
| 374 | } |
| 375 | return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1); |
| 376 | } |
| 377 | error ("member function %s not found", tstr); |
| 378 | return 0; /* For lint -- never reached */ |
| 379 | } |
| 380 | |
| 381 | \f |
| 382 | /* Concatenate two values with the following conditions: |
| 383 | |
| 384 | (1) Both values must be either bitstring values or character string |
| 385 | values and the resulting value consists of the concatenation of |
| 386 | ARG1 followed by ARG2. |
| 387 | |
| 388 | or |
| 389 | |
| 390 | One value must be an integer value and the other value must be |
| 391 | either a bitstring value or character string value, which is |
| 392 | to be repeated by the number of times specified by the integer |
| 393 | value. |
| 394 | |
| 395 | |
| 396 | (2) Boolean values are also allowed and are treated as bit string |
| 397 | values of length 1. |
| 398 | |
| 399 | (3) Character values are also allowed and are treated as character |
| 400 | string values of length 1. |
| 401 | */ |
| 402 | |
| 403 | value_ptr |
| 404 | value_concat (arg1, arg2) |
| 405 | value_ptr arg1, arg2; |
| 406 | { |
| 407 | register value_ptr inval1, inval2, outval; |
| 408 | int inval1len, inval2len; |
| 409 | int count, idx; |
| 410 | char *ptr; |
| 411 | char inchar; |
| 412 | |
| 413 | /* First figure out if we are dealing with two values to be concatenated |
| 414 | or a repeat count and a value to be repeated. INVAL1 is set to the |
| 415 | first of two concatenated values, or the repeat count. INVAL2 is set |
| 416 | to the second of the two concatenated values or the value to be |
| 417 | repeated. */ |
| 418 | |
| 419 | if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT) |
| 420 | { |
| 421 | inval1 = arg2; |
| 422 | inval2 = arg1; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | inval1 = arg1; |
| 427 | inval2 = arg2; |
| 428 | } |
| 429 | |
| 430 | /* Now process the input values. */ |
| 431 | |
| 432 | if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_INT) |
| 433 | { |
| 434 | /* We have a repeat count. Validate the second value and then |
| 435 | construct a value repeated that many times. */ |
| 436 | if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_STRING |
| 437 | || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR) |
| 438 | { |
| 439 | count = longest_to_int (value_as_long (inval1)); |
| 440 | inval2len = TYPE_LENGTH (VALUE_TYPE (inval2)); |
| 441 | ptr = (char *) alloca (count * inval2len); |
| 442 | if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR) |
| 443 | { |
| 444 | inchar = (char) unpack_long (VALUE_TYPE (inval2), |
| 445 | VALUE_CONTENTS (inval2)); |
| 446 | for (idx = 0; idx < count; idx++) |
| 447 | { |
| 448 | *(ptr + idx) = inchar; |
| 449 | } |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | for (idx = 0; idx < count; idx++) |
| 454 | { |
| 455 | memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2), |
| 456 | inval2len); |
| 457 | } |
| 458 | } |
| 459 | outval = value_string (ptr, count * inval2len); |
| 460 | } |
| 461 | else if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BITSTRING |
| 462 | || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BOOL) |
| 463 | { |
| 464 | error ("unimplemented support for bitstring/boolean repeats"); |
| 465 | } |
| 466 | else |
| 467 | { |
| 468 | error ("can't repeat values of that type"); |
| 469 | } |
| 470 | } |
| 471 | else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_STRING |
| 472 | || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR) |
| 473 | { |
| 474 | /* We have two character strings to concatenate. */ |
| 475 | if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_STRING |
| 476 | && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_CHAR) |
| 477 | { |
| 478 | error ("Strings can only be concatenated with other strings."); |
| 479 | } |
| 480 | inval1len = TYPE_LENGTH (VALUE_TYPE (inval1)); |
| 481 | inval2len = TYPE_LENGTH (VALUE_TYPE (inval2)); |
| 482 | ptr = (char *) alloca (inval1len + inval2len); |
| 483 | if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR) |
| 484 | { |
| 485 | *ptr = (char) unpack_long (VALUE_TYPE (inval1), VALUE_CONTENTS (inval1)); |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | memcpy (ptr, VALUE_CONTENTS (inval1), inval1len); |
| 490 | } |
| 491 | if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR) |
| 492 | { |
| 493 | *(ptr + inval1len) = |
| 494 | (char) unpack_long (VALUE_TYPE (inval2), VALUE_CONTENTS (inval2)); |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len); |
| 499 | } |
| 500 | outval = value_string (ptr, inval1len + inval2len); |
| 501 | } |
| 502 | else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BITSTRING |
| 503 | || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BOOL) |
| 504 | { |
| 505 | /* We have two bitstrings to concatenate. */ |
| 506 | if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BITSTRING |
| 507 | && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BOOL) |
| 508 | { |
| 509 | error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans."); |
| 510 | } |
| 511 | error ("unimplemented support for bitstring/boolean concatenation."); |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | /* We don't know how to concatenate these operands. */ |
| 516 | error ("illegal operands for concatenation."); |
| 517 | } |
| 518 | return (outval); |
| 519 | } |
| 520 | |
| 521 | \f |
| 522 | |
| 523 | /* Perform a binary operation on two operands which have reasonable |
| 524 | representations as integers or floats. This includes booleans, |
| 525 | characters, integers, or floats. |
| 526 | Does not support addition and subtraction on pointers; |
| 527 | use value_add or value_sub if you want to handle those possibilities. */ |
| 528 | |
| 529 | value_ptr |
| 530 | value_binop (arg1, arg2, op) |
| 531 | value_ptr arg1, arg2; |
| 532 | enum exp_opcode op; |
| 533 | { |
| 534 | register value_ptr val; |
| 535 | |
| 536 | COERCE_ENUM (arg1); |
| 537 | COERCE_ENUM (arg2); |
| 538 | |
| 539 | if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT |
| 540 | && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_CHAR |
| 541 | && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT |
| 542 | && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL |
| 543 | && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_RANGE) |
| 544 | || |
| 545 | (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT |
| 546 | && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_CHAR |
| 547 | && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT |
| 548 | && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL |
| 549 | && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_RANGE)) |
| 550 | error ("Argument to arithmetic operation not a number or boolean."); |
| 551 | |
| 552 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT |
| 553 | || |
| 554 | TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT) |
| 555 | { |
| 556 | /* FIXME-if-picky-about-floating-accuracy: Should be doing this |
| 557 | in target format. real.c in GCC probably has the necessary |
| 558 | code. */ |
| 559 | double v1, v2, v; |
| 560 | v1 = value_as_double (arg1); |
| 561 | v2 = value_as_double (arg2); |
| 562 | switch (op) |
| 563 | { |
| 564 | case BINOP_ADD: |
| 565 | v = v1 + v2; |
| 566 | break; |
| 567 | |
| 568 | case BINOP_SUB: |
| 569 | v = v1 - v2; |
| 570 | break; |
| 571 | |
| 572 | case BINOP_MUL: |
| 573 | v = v1 * v2; |
| 574 | break; |
| 575 | |
| 576 | case BINOP_DIV: |
| 577 | v = v1 / v2; |
| 578 | break; |
| 579 | |
| 580 | default: |
| 581 | error ("Integer-only operation on floating point number."); |
| 582 | } |
| 583 | |
| 584 | val = allocate_value (builtin_type_double); |
| 585 | store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)), |
| 586 | v); |
| 587 | } |
| 588 | else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL |
| 589 | && |
| 590 | TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL) |
| 591 | { |
| 592 | LONGEST v1, v2, v; |
| 593 | v1 = value_as_long (arg1); |
| 594 | v2 = value_as_long (arg2); |
| 595 | |
| 596 | switch (op) |
| 597 | { |
| 598 | case BINOP_BITWISE_AND: |
| 599 | v = v1 & v2; |
| 600 | break; |
| 601 | |
| 602 | case BINOP_BITWISE_IOR: |
| 603 | v = v1 | v2; |
| 604 | break; |
| 605 | |
| 606 | case BINOP_BITWISE_XOR: |
| 607 | v = v1 ^ v2; |
| 608 | break; |
| 609 | |
| 610 | default: |
| 611 | error ("Invalid operation on booleans."); |
| 612 | } |
| 613 | |
| 614 | val = allocate_value (builtin_type_chill_bool); |
| 615 | store_signed_integer (VALUE_CONTENTS_RAW (val), |
| 616 | TYPE_LENGTH (VALUE_TYPE (val)), |
| 617 | v); |
| 618 | } |
| 619 | else |
| 620 | /* Integral operations here. */ |
| 621 | /* FIXME: Also mixed integral/booleans, with result an integer. */ |
| 622 | /* FIXME: This implements ANSI C rules (also correct for C++). |
| 623 | What about FORTRAN and chill? */ |
| 624 | { |
| 625 | struct type *type1 = VALUE_TYPE (arg1); |
| 626 | struct type *type2 = VALUE_TYPE (arg2); |
| 627 | int promoted_len1 = TYPE_LENGTH (type1); |
| 628 | int promoted_len2 = TYPE_LENGTH (type2); |
| 629 | int is_unsigned1 = TYPE_UNSIGNED (type1); |
| 630 | int is_unsigned2 = TYPE_UNSIGNED (type2); |
| 631 | int result_len; |
| 632 | int unsigned_operation; |
| 633 | |
| 634 | /* Determine type length and signedness after promotion for |
| 635 | both operands. */ |
| 636 | if (promoted_len1 < TYPE_LENGTH (builtin_type_int)) |
| 637 | { |
| 638 | is_unsigned1 = 0; |
| 639 | promoted_len1 = TYPE_LENGTH (builtin_type_int); |
| 640 | } |
| 641 | if (promoted_len2 < TYPE_LENGTH (builtin_type_int)) |
| 642 | { |
| 643 | is_unsigned2 = 0; |
| 644 | promoted_len2 = TYPE_LENGTH (builtin_type_int); |
| 645 | } |
| 646 | |
| 647 | /* Determine type length of the result, and if the operation should |
| 648 | be done unsigned. |
| 649 | Use the signedness of the operand with the greater length. |
| 650 | If both operands are of equal length, use unsigned operation |
| 651 | if one of the operands is unsigned. */ |
| 652 | if (promoted_len1 > promoted_len2) |
| 653 | { |
| 654 | unsigned_operation = is_unsigned1; |
| 655 | result_len = promoted_len1; |
| 656 | } |
| 657 | else if (promoted_len2 > promoted_len1) |
| 658 | { |
| 659 | unsigned_operation = is_unsigned2; |
| 660 | result_len = promoted_len2; |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | unsigned_operation = is_unsigned1 || is_unsigned2; |
| 665 | result_len = promoted_len1; |
| 666 | } |
| 667 | |
| 668 | if (unsigned_operation) |
| 669 | { |
| 670 | unsigned LONGEST v1, v2, v; |
| 671 | v1 = (unsigned LONGEST) value_as_long (arg1); |
| 672 | v2 = (unsigned LONGEST) value_as_long (arg2); |
| 673 | |
| 674 | /* Truncate values to the type length of the result. */ |
| 675 | if (result_len < sizeof (unsigned LONGEST)) |
| 676 | { |
| 677 | v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; |
| 678 | v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1; |
| 679 | } |
| 680 | |
| 681 | switch (op) |
| 682 | { |
| 683 | case BINOP_ADD: |
| 684 | v = v1 + v2; |
| 685 | break; |
| 686 | |
| 687 | case BINOP_SUB: |
| 688 | v = v1 - v2; |
| 689 | break; |
| 690 | |
| 691 | case BINOP_MUL: |
| 692 | v = v1 * v2; |
| 693 | break; |
| 694 | |
| 695 | case BINOP_DIV: |
| 696 | v = v1 / v2; |
| 697 | break; |
| 698 | |
| 699 | case BINOP_REM: |
| 700 | v = v1 % v2; |
| 701 | break; |
| 702 | |
| 703 | case BINOP_MOD: |
| 704 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, |
| 705 | v1 mod 0 has a defined value, v1. */ |
| 706 | /* Chill specifies that v2 must be > 0, so check for that. */ |
| 707 | if (current_language -> la_language == language_chill |
| 708 | && value_as_long (arg2) <= 0) |
| 709 | { |
| 710 | error ("Second operand of MOD must be greater than zero."); |
| 711 | } |
| 712 | if (v2 == 0) |
| 713 | { |
| 714 | v = v1; |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | v = v1/v2; |
| 719 | /* Note floor(v1/v2) == v1/v2 for unsigned. */ |
| 720 | v = v1 - (v2 * v); |
| 721 | } |
| 722 | break; |
| 723 | |
| 724 | case BINOP_LSH: |
| 725 | v = v1 << v2; |
| 726 | break; |
| 727 | |
| 728 | case BINOP_RSH: |
| 729 | v = v1 >> v2; |
| 730 | break; |
| 731 | |
| 732 | case BINOP_BITWISE_AND: |
| 733 | v = v1 & v2; |
| 734 | break; |
| 735 | |
| 736 | case BINOP_BITWISE_IOR: |
| 737 | v = v1 | v2; |
| 738 | break; |
| 739 | |
| 740 | case BINOP_BITWISE_XOR: |
| 741 | v = v1 ^ v2; |
| 742 | break; |
| 743 | |
| 744 | case BINOP_LOGICAL_AND: |
| 745 | v = v1 && v2; |
| 746 | break; |
| 747 | |
| 748 | case BINOP_LOGICAL_OR: |
| 749 | v = v1 || v2; |
| 750 | break; |
| 751 | |
| 752 | case BINOP_MIN: |
| 753 | v = v1 < v2 ? v1 : v2; |
| 754 | break; |
| 755 | |
| 756 | case BINOP_MAX: |
| 757 | v = v1 > v2 ? v1 : v2; |
| 758 | break; |
| 759 | |
| 760 | case BINOP_EQUAL: |
| 761 | v = v1 == v2; |
| 762 | break; |
| 763 | |
| 764 | case BINOP_LESS: |
| 765 | v = v1 < v2; |
| 766 | break; |
| 767 | |
| 768 | default: |
| 769 | error ("Invalid binary operation on numbers."); |
| 770 | } |
| 771 | |
| 772 | /* This is a kludge to get around the fact that we don't |
| 773 | know how to determine the result type from the types of |
| 774 | the operands. (I'm not really sure how much we feel the |
| 775 | need to duplicate the exact rules of the current |
| 776 | language. They can get really hairy. But not to do so |
| 777 | makes it hard to document just what we *do* do). */ |
| 778 | |
| 779 | /* Can't just call init_type because we wouldn't know what |
| 780 | name to give the type. */ |
| 781 | val = allocate_value |
| 782 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT |
| 783 | ? builtin_type_unsigned_long_long |
| 784 | : builtin_type_unsigned_long); |
| 785 | store_unsigned_integer (VALUE_CONTENTS_RAW (val), |
| 786 | TYPE_LENGTH (VALUE_TYPE (val)), |
| 787 | v); |
| 788 | } |
| 789 | else |
| 790 | { |
| 791 | LONGEST v1, v2, v; |
| 792 | v1 = value_as_long (arg1); |
| 793 | v2 = value_as_long (arg2); |
| 794 | |
| 795 | switch (op) |
| 796 | { |
| 797 | case BINOP_ADD: |
| 798 | v = v1 + v2; |
| 799 | break; |
| 800 | |
| 801 | case BINOP_SUB: |
| 802 | v = v1 - v2; |
| 803 | break; |
| 804 | |
| 805 | case BINOP_MUL: |
| 806 | v = v1 * v2; |
| 807 | break; |
| 808 | |
| 809 | case BINOP_DIV: |
| 810 | v = v1 / v2; |
| 811 | break; |
| 812 | |
| 813 | case BINOP_REM: |
| 814 | v = v1 % v2; |
| 815 | break; |
| 816 | |
| 817 | case BINOP_MOD: |
| 818 | /* Knuth 1.2.4, integer only. Note that unlike the C '%' op, |
| 819 | X mod 0 has a defined value, X. */ |
| 820 | /* Chill specifies that v2 must be > 0, so check for that. */ |
| 821 | if (current_language -> la_language == language_chill |
| 822 | && v2 <= 0) |
| 823 | { |
| 824 | error ("Second operand of MOD must be greater than zero."); |
| 825 | } |
| 826 | if (v2 == 0) |
| 827 | { |
| 828 | v = v1; |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | v = v1/v2; |
| 833 | /* Compute floor. */ |
| 834 | if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0)) |
| 835 | { |
| 836 | v--; |
| 837 | } |
| 838 | v = v1 - (v2 * v); |
| 839 | } |
| 840 | break; |
| 841 | |
| 842 | case BINOP_LSH: |
| 843 | v = v1 << v2; |
| 844 | break; |
| 845 | |
| 846 | case BINOP_RSH: |
| 847 | v = v1 >> v2; |
| 848 | break; |
| 849 | |
| 850 | case BINOP_BITWISE_AND: |
| 851 | v = v1 & v2; |
| 852 | break; |
| 853 | |
| 854 | case BINOP_BITWISE_IOR: |
| 855 | v = v1 | v2; |
| 856 | break; |
| 857 | |
| 858 | case BINOP_BITWISE_XOR: |
| 859 | v = v1 ^ v2; |
| 860 | break; |
| 861 | |
| 862 | case BINOP_LOGICAL_AND: |
| 863 | v = v1 && v2; |
| 864 | break; |
| 865 | |
| 866 | case BINOP_LOGICAL_OR: |
| 867 | v = v1 || v2; |
| 868 | break; |
| 869 | |
| 870 | case BINOP_MIN: |
| 871 | v = v1 < v2 ? v1 : v2; |
| 872 | break; |
| 873 | |
| 874 | case BINOP_MAX: |
| 875 | v = v1 > v2 ? v1 : v2; |
| 876 | break; |
| 877 | |
| 878 | case BINOP_EQUAL: |
| 879 | v = v1 == v2; |
| 880 | break; |
| 881 | |
| 882 | case BINOP_LESS: |
| 883 | v = v1 < v2; |
| 884 | break; |
| 885 | |
| 886 | default: |
| 887 | error ("Invalid binary operation on numbers."); |
| 888 | } |
| 889 | |
| 890 | /* This is a kludge to get around the fact that we don't |
| 891 | know how to determine the result type from the types of |
| 892 | the operands. (I'm not really sure how much we feel the |
| 893 | need to duplicate the exact rules of the current |
| 894 | language. They can get really hairy. But not to do so |
| 895 | makes it hard to document just what we *do* do). */ |
| 896 | |
| 897 | /* Can't just call init_type because we wouldn't know what |
| 898 | name to give the type. */ |
| 899 | val = allocate_value |
| 900 | (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT |
| 901 | ? builtin_type_long_long |
| 902 | : builtin_type_long); |
| 903 | store_signed_integer (VALUE_CONTENTS_RAW (val), |
| 904 | TYPE_LENGTH (VALUE_TYPE (val)), |
| 905 | v); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return val; |
| 910 | } |
| 911 | \f |
| 912 | /* Simulate the C operator ! -- return 1 if ARG1 contains zero. */ |
| 913 | |
| 914 | int |
| 915 | value_logical_not (arg1) |
| 916 | value_ptr arg1; |
| 917 | { |
| 918 | register int len; |
| 919 | register char *p; |
| 920 | |
| 921 | COERCE_ARRAY (arg1); |
| 922 | |
| 923 | if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT) |
| 924 | return 0 == value_as_double (arg1); |
| 925 | |
| 926 | len = TYPE_LENGTH (VALUE_TYPE (arg1)); |
| 927 | p = VALUE_CONTENTS (arg1); |
| 928 | |
| 929 | while (--len >= 0) |
| 930 | { |
| 931 | if (*p++) |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | return len < 0; |
| 936 | } |
| 937 | |
| 938 | /* Simulate the C operator == by returning a 1 |
| 939 | iff ARG1 and ARG2 have equal contents. */ |
| 940 | |
| 941 | int |
| 942 | value_equal (arg1, arg2) |
| 943 | register value_ptr arg1, arg2; |
| 944 | |
| 945 | { |
| 946 | register int len; |
| 947 | register char *p1, *p2; |
| 948 | enum type_code code1; |
| 949 | enum type_code code2; |
| 950 | |
| 951 | COERCE_ARRAY (arg1); |
| 952 | COERCE_ARRAY (arg2); |
| 953 | |
| 954 | code1 = TYPE_CODE (VALUE_TYPE (arg1)); |
| 955 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); |
| 956 | |
| 957 | if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT) |
| 958 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
| 959 | BINOP_EQUAL))); |
| 960 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT) |
| 961 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT)) |
| 962 | return value_as_double (arg1) == value_as_double (arg2); |
| 963 | |
| 964 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever |
| 965 | is bigger. */ |
| 966 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT) |
| 967 | return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2); |
| 968 | else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT) |
| 969 | return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2); |
| 970 | |
| 971 | else if (code1 == code2 |
| 972 | && ((len = TYPE_LENGTH (VALUE_TYPE (arg1))) |
| 973 | == TYPE_LENGTH (VALUE_TYPE (arg2)))) |
| 974 | { |
| 975 | p1 = VALUE_CONTENTS (arg1); |
| 976 | p2 = VALUE_CONTENTS (arg2); |
| 977 | while (--len >= 0) |
| 978 | { |
| 979 | if (*p1++ != *p2++) |
| 980 | break; |
| 981 | } |
| 982 | return len < 0; |
| 983 | } |
| 984 | else |
| 985 | { |
| 986 | error ("Invalid type combination in equality test."); |
| 987 | return 0; /* For lint -- never reached */ |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | /* Simulate the C operator < by returning 1 |
| 992 | iff ARG1's contents are less than ARG2's. */ |
| 993 | |
| 994 | int |
| 995 | value_less (arg1, arg2) |
| 996 | register value_ptr arg1, arg2; |
| 997 | { |
| 998 | register enum type_code code1; |
| 999 | register enum type_code code2; |
| 1000 | |
| 1001 | COERCE_ARRAY (arg1); |
| 1002 | COERCE_ARRAY (arg2); |
| 1003 | |
| 1004 | code1 = TYPE_CODE (VALUE_TYPE (arg1)); |
| 1005 | code2 = TYPE_CODE (VALUE_TYPE (arg2)); |
| 1006 | |
| 1007 | if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT) |
| 1008 | return longest_to_int (value_as_long (value_binop (arg1, arg2, |
| 1009 | BINOP_LESS))); |
| 1010 | else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT) |
| 1011 | && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT)) |
| 1012 | return value_as_double (arg1) < value_as_double (arg2); |
| 1013 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) |
| 1014 | return value_as_pointer (arg1) < value_as_pointer (arg2); |
| 1015 | |
| 1016 | /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever |
| 1017 | is bigger. */ |
| 1018 | else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT) |
| 1019 | return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2); |
| 1020 | else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT) |
| 1021 | return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2); |
| 1022 | |
| 1023 | else |
| 1024 | { |
| 1025 | error ("Invalid type combination in ordering comparison."); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | } |
| 1029 | \f |
| 1030 | /* The unary operators - and ~. Both free the argument ARG1. */ |
| 1031 | |
| 1032 | value_ptr |
| 1033 | value_neg (arg1) |
| 1034 | register value_ptr arg1; |
| 1035 | { |
| 1036 | register struct type *type; |
| 1037 | |
| 1038 | COERCE_ENUM (arg1); |
| 1039 | |
| 1040 | type = VALUE_TYPE (arg1); |
| 1041 | |
| 1042 | if (TYPE_CODE (type) == TYPE_CODE_FLT) |
| 1043 | return value_from_double (type, - value_as_double (arg1)); |
| 1044 | else if (TYPE_CODE (type) == TYPE_CODE_INT) |
| 1045 | return value_from_longest (type, - value_as_long (arg1)); |
| 1046 | else { |
| 1047 | error ("Argument to negate operation not a number."); |
| 1048 | return 0; /* For lint -- never reached */ |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | value_ptr |
| 1053 | value_complement (arg1) |
| 1054 | register value_ptr arg1; |
| 1055 | { |
| 1056 | COERCE_ENUM (arg1); |
| 1057 | |
| 1058 | if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT) |
| 1059 | error ("Argument to complement operation not an integer."); |
| 1060 | |
| 1061 | return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1)); |
| 1062 | } |
| 1063 | \f |
| 1064 | /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE, |
| 1065 | and whose VALUE_CONTENTS is valaddr. |
| 1066 | Return -1 if out of range, -2 other error. */ |
| 1067 | |
| 1068 | int |
| 1069 | value_bit_index (type, valaddr, index) |
| 1070 | struct type *type; |
| 1071 | char *valaddr; |
| 1072 | int index; |
| 1073 | { |
| 1074 | struct type *range; |
| 1075 | int low_bound, high_bound; |
| 1076 | LONGEST word; |
| 1077 | unsigned rel_index; |
| 1078 | range = TYPE_FIELD_TYPE (type, 0); |
| 1079 | if (TYPE_CODE (range) != TYPE_CODE_RANGE) |
| 1080 | return -2; |
| 1081 | low_bound = TYPE_LOW_BOUND (range); |
| 1082 | high_bound = TYPE_HIGH_BOUND (range); |
| 1083 | if (index < low_bound || index > high_bound) |
| 1084 | return -1; |
| 1085 | rel_index = index - low_bound; |
| 1086 | word = unpack_long (builtin_type_unsigned_char, |
| 1087 | valaddr + (rel_index / TARGET_CHAR_BIT)); |
| 1088 | rel_index %= TARGET_CHAR_BIT; |
| 1089 | if (BITS_BIG_ENDIAN) |
| 1090 | rel_index = TARGET_CHAR_BIT - 1 - rel_index; |
| 1091 | return (word >> rel_index) & 1; |
| 1092 | } |
| 1093 | |
| 1094 | value_ptr |
| 1095 | value_in (element, set) |
| 1096 | value_ptr element, set; |
| 1097 | { |
| 1098 | int member; |
| 1099 | if (TYPE_CODE (VALUE_TYPE (set)) != TYPE_CODE_SET) |
| 1100 | error ("Second argument of 'IN' has wrong type"); |
| 1101 | if (TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_INT |
| 1102 | && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_CHAR |
| 1103 | && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_ENUM |
| 1104 | && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_BOOL) |
| 1105 | error ("First argument of 'IN' has wrong type"); |
| 1106 | member = value_bit_index (VALUE_TYPE (set), VALUE_CONTENTS (set), |
| 1107 | value_as_long (element)); |
| 1108 | if (member < 0) |
| 1109 | error ("First argument of 'IN' not in range"); |
| 1110 | return value_from_longest (builtin_type_int, member); |
| 1111 | } |
| 1112 | |
| 1113 | void |
| 1114 | _initialize_valarith () |
| 1115 | { |
| 1116 | } |