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