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