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