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