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