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