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