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