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