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