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