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