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