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