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