* elf32-h8300.c (elf32_h8_relax_section): When checking for a
[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 */,
4c3376c8
SW
297 0 /* strict match */, &args[0], /* objp */
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
KW
1348
1349/* Performs a binary operation on two vector operands by calling scalar_binop
1350 for each pair of vector components. */
1351
1352static struct value *
1353vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1354{
1355 struct value *val, *tmp, *mark;
22e048c9 1356 struct type *type1, *type2, *eltype1, *eltype2;
dbc98a8b
KW
1357 int t1_is_vec, t2_is_vec, elsize, i;
1358 LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
7346b668
KW
1359
1360 type1 = check_typedef (value_type (val1));
1361 type2 = check_typedef (value_type (val2));
1362
1363 t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1364 && TYPE_VECTOR (type1)) ? 1 : 0;
1365 t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1366 && TYPE_VECTOR (type2)) ? 1 : 0;
1367
1368 if (!t1_is_vec || !t2_is_vec)
1369 error (_("Vector operations are only supported among vectors"));
1370
dbc98a8b
KW
1371 if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1372 || !get_array_bounds (type2, &low_bound2, &high_bound2))
1373 error (_("Could not determine the vector bounds"));
1374
7346b668
KW
1375 eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1376 eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
dbc98a8b 1377 elsize = TYPE_LENGTH (eltype1);
7346b668
KW
1378
1379 if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
dbc98a8b
KW
1380 || elsize != TYPE_LENGTH (eltype2)
1381 || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1382 || low_bound1 != low_bound2 || high_bound1 != high_bound2)
7346b668
KW
1383 error (_("Cannot perform operation on vectors with different types"));
1384
7346b668
KW
1385 val = allocate_value (type1);
1386 mark = value_mark ();
dbc98a8b 1387 for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
7346b668
KW
1388 {
1389 tmp = value_binop (value_subscript (val1, i),
1390 value_subscript (val2, i), op);
1391 memcpy (value_contents_writeable (val) + i * elsize,
1392 value_contents_all (tmp),
1393 elsize);
1394 }
1395 value_free_to_mark (mark);
1396
1397 return val;
1398}
1399
1400/* Perform a binary operation on two operands. */
1401
1402struct value *
1403value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1404{
3bdf2bbd 1405 struct value *val;
7346b668
KW
1406 struct type *type1 = check_typedef (value_type (arg1));
1407 struct type *type2 = check_typedef (value_type (arg2));
3bdf2bbd
KW
1408 int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1409 && TYPE_VECTOR (type1));
1410 int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1411 && TYPE_VECTOR (type2));
1412
1413 if (!t1_is_vec && !t2_is_vec)
1414 val = scalar_binop (arg1, arg2, op);
1415 else if (t1_is_vec && t2_is_vec)
1416 val = vector_binop (arg1, arg2, op);
7346b668 1417 else
3bdf2bbd
KW
1418 {
1419 /* Widen the scalar operand to a vector. */
1420 struct value **v = t1_is_vec ? &arg2 : &arg1;
1421 struct type *t = t1_is_vec ? type2 : type1;
1422
1423 if (TYPE_CODE (t) != TYPE_CODE_FLT
1424 && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1425 && !is_integral_type (t))
1426 error (_("Argument to operation not a number or boolean."));
1427
1428 *v = value_cast (t1_is_vec ? type1 : type2, *v);
1429 val = vector_binop (arg1, arg2, op);
1430 }
1431
1432 return val;
7346b668 1433}
c906108c
SS
1434\f
1435/* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1436
1437int
f23631e4 1438value_logical_not (struct value *arg1)
c906108c 1439{
52f0bd74 1440 int len;
fc1a4b47 1441 const gdb_byte *p;
c906108c
SS
1442 struct type *type1;
1443
0ab7ba45 1444 arg1 = coerce_array (arg1);
df407dfe 1445 type1 = check_typedef (value_type (arg1));
c906108c
SS
1446
1447 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1448 return 0 == value_as_double (arg1);
4ef30785 1449 else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
e17a4113
UW
1450 return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
1451 gdbarch_byte_order (get_type_arch (type1)));
c906108c
SS
1452
1453 len = TYPE_LENGTH (type1);
0fd88904 1454 p = value_contents (arg1);
c906108c
SS
1455
1456 while (--len >= 0)
1457 {
1458 if (*p++)
1459 break;
1460 }
1461
1462 return len < 0;
1463}
1464
c4093a6a 1465/* Perform a comparison on two string values (whose content are not
581e13c1 1466 necessarily null terminated) based on their length. */
c4093a6a
JM
1467
1468static int
f23631e4 1469value_strcmp (struct value *arg1, struct value *arg2)
c4093a6a 1470{
df407dfe
AC
1471 int len1 = TYPE_LENGTH (value_type (arg1));
1472 int len2 = TYPE_LENGTH (value_type (arg2));
fc1a4b47
AC
1473 const gdb_byte *s1 = value_contents (arg1);
1474 const gdb_byte *s2 = value_contents (arg2);
c4093a6a
JM
1475 int i, len = len1 < len2 ? len1 : len2;
1476
1477 for (i = 0; i < len; i++)
1478 {
1479 if (s1[i] < s2[i])
1480 return -1;
1481 else if (s1[i] > s2[i])
1482 return 1;
1483 else
1484 continue;
1485 }
1486
1487 if (len1 < len2)
1488 return -1;
1489 else if (len1 > len2)
1490 return 1;
1491 else
1492 return 0;
1493}
1494
c906108c
SS
1495/* Simulate the C operator == by returning a 1
1496 iff ARG1 and ARG2 have equal contents. */
1497
1498int
f23631e4 1499value_equal (struct value *arg1, struct value *arg2)
c906108c 1500{
52f0bd74 1501 int len;
fc1a4b47
AC
1502 const gdb_byte *p1;
1503 const gdb_byte *p2;
c906108c
SS
1504 struct type *type1, *type2;
1505 enum type_code code1;
1506 enum type_code code2;
2de41bce 1507 int is_int1, is_int2;
c906108c 1508
994b9211
AC
1509 arg1 = coerce_array (arg1);
1510 arg2 = coerce_array (arg2);
c906108c 1511
df407dfe
AC
1512 type1 = check_typedef (value_type (arg1));
1513 type2 = check_typedef (value_type (arg2));
c906108c
SS
1514 code1 = TYPE_CODE (type1);
1515 code2 = TYPE_CODE (type2);
2de41bce
PH
1516 is_int1 = is_integral_type (type1);
1517 is_int2 = is_integral_type (type2);
c906108c 1518
2de41bce 1519 if (is_int1 && is_int2)
c906108c
SS
1520 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1521 BINOP_EQUAL)));
2de41bce
PH
1522 else if ((code1 == TYPE_CODE_FLT || is_int1)
1523 && (code2 == TYPE_CODE_FLT || is_int2))
d067a990
MK
1524 {
1525 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1526 `long double' values are returned in static storage (m68k). */
1527 DOUBLEST d = value_as_double (arg1);
a109c7c1 1528
d067a990
MK
1529 return d == value_as_double (arg2);
1530 }
4ef30785
TJB
1531 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1532 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1533 {
1534 gdb_byte v1[16], v2[16];
1535 int len_v1, len_v2;
e17a4113 1536 enum bfd_endian byte_order_v1, byte_order_v2;
4ef30785 1537
e17a4113
UW
1538 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1539 v2, &len_v2, &byte_order_v2);
4ef30785 1540
e17a4113
UW
1541 return decimal_compare (v1, len_v1, byte_order_v1,
1542 v2, len_v2, byte_order_v2) == 0;
4ef30785 1543 }
c906108c
SS
1544
1545 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1546 is bigger. */
2de41bce 1547 else if (code1 == TYPE_CODE_PTR && is_int2)
1aa20aa8 1548 return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
2de41bce 1549 else if (code2 == TYPE_CODE_PTR && is_int1)
1aa20aa8 1550 return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
c906108c
SS
1551
1552 else if (code1 == code2
1553 && ((len = (int) TYPE_LENGTH (type1))
1554 == (int) TYPE_LENGTH (type2)))
1555 {
0fd88904
AC
1556 p1 = value_contents (arg1);
1557 p2 = value_contents (arg2);
c906108c
SS
1558 while (--len >= 0)
1559 {
1560 if (*p1++ != *p2++)
1561 break;
1562 }
1563 return len < 0;
1564 }
c4093a6a
JM
1565 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1566 {
1567 return value_strcmp (arg1, arg2) == 0;
1568 }
c906108c
SS
1569 else
1570 {
8a3fe4f8 1571 error (_("Invalid type combination in equality test."));
581e13c1 1572 return 0; /* For lint -- never reached. */
c906108c
SS
1573 }
1574}
1575
218d2fc6
TJB
1576/* Compare values based on their raw contents. Useful for arrays since
1577 value_equal coerces them to pointers, thus comparing just the address
1578 of the array instead of its contents. */
1579
1580int
1581value_equal_contents (struct value *arg1, struct value *arg2)
1582{
1583 struct type *type1, *type2;
1584
1585 type1 = check_typedef (value_type (arg1));
1586 type2 = check_typedef (value_type (arg2));
1587
1588 return (TYPE_CODE (type1) == TYPE_CODE (type2)
1589 && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1590 && memcmp (value_contents (arg1), value_contents (arg2),
1591 TYPE_LENGTH (type1)) == 0);
1592}
1593
c906108c
SS
1594/* Simulate the C operator < by returning 1
1595 iff ARG1's contents are less than ARG2's. */
1596
1597int
f23631e4 1598value_less (struct value *arg1, struct value *arg2)
c906108c 1599{
52f0bd74
AC
1600 enum type_code code1;
1601 enum type_code code2;
c906108c 1602 struct type *type1, *type2;
2de41bce 1603 int is_int1, is_int2;
c906108c 1604
994b9211
AC
1605 arg1 = coerce_array (arg1);
1606 arg2 = coerce_array (arg2);
c906108c 1607
df407dfe
AC
1608 type1 = check_typedef (value_type (arg1));
1609 type2 = check_typedef (value_type (arg2));
c906108c
SS
1610 code1 = TYPE_CODE (type1);
1611 code2 = TYPE_CODE (type2);
2de41bce
PH
1612 is_int1 = is_integral_type (type1);
1613 is_int2 = is_integral_type (type2);
c906108c 1614
2de41bce 1615 if (is_int1 && is_int2)
c906108c
SS
1616 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1617 BINOP_LESS)));
2de41bce
PH
1618 else if ((code1 == TYPE_CODE_FLT || is_int1)
1619 && (code2 == TYPE_CODE_FLT || is_int2))
d067a990
MK
1620 {
1621 /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
1622 `long double' values are returned in static storage (m68k). */
1623 DOUBLEST d = value_as_double (arg1);
a109c7c1 1624
d067a990
MK
1625 return d < value_as_double (arg2);
1626 }
4ef30785
TJB
1627 else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
1628 && (code2 == TYPE_CODE_DECFLOAT || is_int2))
1629 {
1630 gdb_byte v1[16], v2[16];
1631 int len_v1, len_v2;
e17a4113 1632 enum bfd_endian byte_order_v1, byte_order_v2;
4ef30785 1633
e17a4113
UW
1634 value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
1635 v2, &len_v2, &byte_order_v2);
4ef30785 1636
e17a4113
UW
1637 return decimal_compare (v1, len_v1, byte_order_v1,
1638 v2, len_v2, byte_order_v2) == -1;
4ef30785 1639 }
c906108c 1640 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1aa20aa8 1641 return value_as_address (arg1) < value_as_address (arg2);
c906108c
SS
1642
1643 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1644 is bigger. */
2de41bce 1645 else if (code1 == TYPE_CODE_PTR && is_int2)
1aa20aa8 1646 return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
2de41bce 1647 else if (code2 == TYPE_CODE_PTR && is_int1)
1aa20aa8 1648 return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
c4093a6a
JM
1649 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1650 return value_strcmp (arg1, arg2) < 0;
c906108c
SS
1651 else
1652 {
8a3fe4f8 1653 error (_("Invalid type combination in ordering comparison."));
c906108c
SS
1654 return 0;
1655 }
1656}
1657\f
36e9969c
NS
1658/* The unary operators +, - and ~. They free the argument ARG1. */
1659
1660struct value *
1661value_pos (struct value *arg1)
1662{
1663 struct type *type;
4066e646 1664
36e9969c 1665 arg1 = coerce_ref (arg1);
36e9969c
NS
1666 type = check_typedef (value_type (arg1));
1667
1668 if (TYPE_CODE (type) == TYPE_CODE_FLT)
4066e646 1669 return value_from_double (type, value_as_double (arg1));
4ef30785 1670 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
4066e646 1671 return value_from_decfloat (type, value_contents (arg1));
36e9969c
NS
1672 else if (is_integral_type (type))
1673 {
4066e646 1674 return value_from_longest (type, value_as_long (arg1));
36e9969c 1675 }
120bd360
KW
1676 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1677 {
1678 struct value *val = allocate_value (type);
1679
1680 memcpy (value_contents_raw (val), value_contents (arg1),
1681 TYPE_LENGTH (type));
1682 return val;
1683 }
36e9969c
NS
1684 else
1685 {
a73c6dcd 1686 error (_("Argument to positive operation not a number."));
581e13c1 1687 return 0; /* For lint -- never reached. */
36e9969c
NS
1688 }
1689}
c906108c 1690
f23631e4
AC
1691struct value *
1692value_neg (struct value *arg1)
c906108c 1693{
52f0bd74 1694 struct type *type;
4066e646 1695
994b9211 1696 arg1 = coerce_ref (arg1);
df407dfe 1697 type = check_typedef (value_type (arg1));
c906108c 1698
27bc4d80
TJB
1699 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1700 {
4066e646 1701 struct value *val = allocate_value (type);
27bc4d80 1702 int len = TYPE_LENGTH (type);
581e13c1 1703 gdb_byte decbytes[16]; /* a decfloat is at most 128 bits long. */
27bc4d80 1704
4ef30785 1705 memcpy (decbytes, value_contents (arg1), len);
27bc4d80 1706
50810684 1707 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
27bc4d80
TJB
1708 decbytes[len-1] = decbytes[len - 1] | 0x80;
1709 else
1710 decbytes[0] = decbytes[0] | 0x80;
1711
1712 memcpy (value_contents_raw (val), decbytes, len);
1713 return val;
1714 }
301f0ecf 1715 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
4066e646 1716 return value_from_double (type, -value_as_double (arg1));
2de41bce 1717 else if (is_integral_type (type))
c906108c 1718 {
4066e646 1719 return value_from_longest (type, -value_as_long (arg1));
c5aa993b 1720 }
120bd360
KW
1721 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1722 {
1723 struct value *tmp, *val = allocate_value (type);
1724 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
cfa6f054
KW
1725 int i;
1726 LONGEST low_bound, high_bound;
120bd360 1727
cfa6f054
KW
1728 if (!get_array_bounds (type, &low_bound, &high_bound))
1729 error (_("Could not determine the vector bounds"));
1730
1731 for (i = 0; i < high_bound - low_bound + 1; i++)
120bd360
KW
1732 {
1733 tmp = value_neg (value_subscript (arg1, i));
1734 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1735 value_contents_all (tmp), TYPE_LENGTH (eltype));
1736 }
1737 return val;
1738 }
c5aa993b
JM
1739 else
1740 {
8a3fe4f8 1741 error (_("Argument to negate operation not a number."));
581e13c1 1742 return 0; /* For lint -- never reached. */
c906108c 1743 }
c906108c
SS
1744}
1745
f23631e4
AC
1746struct value *
1747value_complement (struct value *arg1)
c906108c 1748{
52f0bd74 1749 struct type *type;
120bd360 1750 struct value *val;
4066e646 1751
994b9211 1752 arg1 = coerce_ref (arg1);
df407dfe 1753 type = check_typedef (value_type (arg1));
c906108c 1754
120bd360
KW
1755 if (is_integral_type (type))
1756 val = value_from_longest (type, ~value_as_long (arg1));
1757 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1758 {
1759 struct value *tmp;
1760 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
cfa6f054
KW
1761 int i;
1762 LONGEST low_bound, high_bound;
1763
1764 if (!get_array_bounds (type, &low_bound, &high_bound))
1765 error (_("Could not determine the vector bounds"));
120bd360
KW
1766
1767 val = allocate_value (type);
cfa6f054 1768 for (i = 0; i < high_bound - low_bound + 1; i++)
120bd360
KW
1769 {
1770 tmp = value_complement (value_subscript (arg1, i));
1771 memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1772 value_contents_all (tmp), TYPE_LENGTH (eltype));
1773 }
1774 }
1775 else
1776 error (_("Argument to complement operation not an integer, boolean."));
c906108c 1777
120bd360 1778 return val;
c906108c
SS
1779}
1780\f
df407dfe 1781/* The INDEX'th bit of SET value whose value_type is TYPE,
0fd88904 1782 and whose value_contents is valaddr.
581e13c1 1783 Return -1 if out of range, -2 other error. */
c906108c
SS
1784
1785int
fc1a4b47 1786value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
c906108c 1787{
50810684 1788 struct gdbarch *gdbarch = get_type_arch (type);
c906108c
SS
1789 LONGEST low_bound, high_bound;
1790 LONGEST word;
1791 unsigned rel_index;
262452ec 1792 struct type *range = TYPE_INDEX_TYPE (type);
a109c7c1 1793
c906108c
SS
1794 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1795 return -2;
1796 if (index < low_bound || index > high_bound)
1797 return -1;
1798 rel_index = index - low_bound;
e17a4113
UW
1799 word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
1800 gdbarch_byte_order (gdbarch));
c906108c 1801 rel_index %= TARGET_CHAR_BIT;
50810684 1802 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
1803 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1804 return (word >> rel_index) & 1;
1805}
1806
fbb06eb1 1807int
f23631e4 1808value_in (struct value *element, struct value *set)
c906108c
SS
1809{
1810 int member;
df407dfe
AC
1811 struct type *settype = check_typedef (value_type (set));
1812 struct type *eltype = check_typedef (value_type (element));
a109c7c1 1813
c906108c
SS
1814 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1815 eltype = TYPE_TARGET_TYPE (eltype);
1816 if (TYPE_CODE (settype) != TYPE_CODE_SET)
8a3fe4f8 1817 error (_("Second argument of 'IN' has wrong type"));
c906108c
SS
1818 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1819 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1820 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1821 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
8a3fe4f8 1822 error (_("First argument of 'IN' has wrong type"));
0fd88904 1823 member = value_bit_index (settype, value_contents (set),
c906108c
SS
1824 value_as_long (element));
1825 if (member < 0)
8a3fe4f8 1826 error (_("First argument of 'IN' not in range"));
fbb06eb1 1827 return member;
c906108c
SS
1828}
1829
1830void
fba45db2 1831_initialize_valarith (void)
c906108c
SS
1832{
1833}
This page took 1.427128 seconds and 4 git commands to generate.