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