]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/valarith.c
* Makefile.in: Sort header definitions.
[thirdparty/binutils-gdb.git] / gdb / valarith.c
CommitLineData
c906108c 1/* Perform arithmetic and other operations on values, for GDB.
b6ba6518
KB
2 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1997, 1998, 1999, 2000
c906108c
SS
4 Free Software Foundation, Inc.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "value.h"
25#include "symtab.h"
26#include "gdbtypes.h"
27#include "expression.h"
28#include "target.h"
29#include "language.h"
c906108c 30#include "gdb_string.h"
c4093a6a 31#include <math.h>
c906108c
SS
32
33/* Define whether or not the C operator '/' truncates towards zero for
34 differently signed operands (truncation direction is undefined in C). */
35
36#ifndef TRUNCATION_TOWARDS_ZERO
37#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
38#endif
39
a14ed312 40static value_ptr value_subscripted_rvalue (value_ptr, value_ptr, int);
c906108c 41
a14ed312 42void _initialize_valarith (void);
c906108c 43\f
c5aa993b 44
c906108c 45value_ptr
fba45db2 46value_add (value_ptr arg1, value_ptr arg2)
c906108c
SS
47{
48 register value_ptr valint, valptr;
49 register int len;
50 struct type *type1, *type2, *valptrtype;
51
52 COERCE_NUMBER (arg1);
53 COERCE_NUMBER (arg2);
54 type1 = check_typedef (VALUE_TYPE (arg1));
55 type2 = check_typedef (VALUE_TYPE (arg2));
56
57 if ((TYPE_CODE (type1) == TYPE_CODE_PTR
58 || TYPE_CODE (type2) == TYPE_CODE_PTR)
59 &&
60 (TYPE_CODE (type1) == TYPE_CODE_INT
61 || TYPE_CODE (type2) == TYPE_CODE_INT))
62 /* Exactly one argument is a pointer, and one is an integer. */
63 {
64 value_ptr retval;
65
66 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
67 {
68 valptr = arg1;
69 valint = arg2;
70 valptrtype = type1;
71 }
72 else
73 {
74 valptr = arg2;
75 valint = arg1;
76 valptrtype = type2;
77 }
78 len = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (valptrtype)));
79 if (len == 0)
80 len = 1; /* For (void *) */
4478b372
JB
81 retval = value_from_pointer (valptrtype,
82 value_as_pointer (valptr)
c906108c
SS
83 + (len * value_as_long (valint)));
84 VALUE_BFD_SECTION (retval) = VALUE_BFD_SECTION (valptr);
85 return retval;
86 }
87
88 return value_binop (arg1, arg2, BINOP_ADD);
89}
90
91value_ptr
fba45db2 92value_sub (value_ptr arg1, value_ptr arg2)
c906108c
SS
93{
94 struct type *type1, *type2;
95 COERCE_NUMBER (arg1);
96 COERCE_NUMBER (arg2);
97 type1 = check_typedef (VALUE_TYPE (arg1));
98 type2 = check_typedef (VALUE_TYPE (arg2));
99
100 if (TYPE_CODE (type1) == TYPE_CODE_PTR)
101 {
102 if (TYPE_CODE (type2) == TYPE_CODE_INT)
103 {
104 /* pointer - integer. */
105 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
4478b372
JB
106 return value_from_pointer (VALUE_TYPE (arg1),
107 (value_as_pointer (arg1)
108 - (sz * value_as_long (arg2))));
c906108c
SS
109 }
110 else if (TYPE_CODE (type2) == TYPE_CODE_PTR
3dd3139b
MS
111 && TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
112 == TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
c906108c
SS
113 {
114 /* pointer to <type x> - pointer to <type x>. */
115 LONGEST sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
116 return value_from_longest
c5aa993b 117 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
c906108c
SS
118 (value_as_long (arg1) - value_as_long (arg2)) / sz);
119 }
120 else
121 {
122 error ("\
123First argument of `-' is a pointer and second argument is neither\n\
124an integer nor a pointer of the same type.");
125 }
126 }
127
128 return value_binop (arg1, arg2, BINOP_SUB);
129}
130
131/* Return the value of ARRAY[IDX].
132 See comments in value_coerce_array() for rationale for reason for
133 doing lower bounds adjustment here rather than there.
134 FIXME: Perhaps we should validate that the index is valid and if
135 verbosity is set, warn about invalid indices (but still use them). */
136
137value_ptr
fba45db2 138value_subscript (value_ptr array, value_ptr idx)
c906108c
SS
139{
140 value_ptr bound;
141 int c_style = current_language->c_style_arrays;
142 struct type *tarray;
143
144 COERCE_REF (array);
145 tarray = check_typedef (VALUE_TYPE (array));
146 COERCE_VARYING_ARRAY (array, tarray);
147
148 if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
149 || TYPE_CODE (tarray) == TYPE_CODE_STRING)
150 {
151 struct type *range_type = TYPE_INDEX_TYPE (tarray);
152 LONGEST lowerbound, upperbound;
153 get_discrete_bounds (range_type, &lowerbound, &upperbound);
154
155 if (VALUE_LVAL (array) != lval_memory)
156 return value_subscripted_rvalue (array, idx, lowerbound);
157
158 if (c_style == 0)
159 {
160 LONGEST index = value_as_long (idx);
161 if (index >= lowerbound && index <= upperbound)
162 return value_subscripted_rvalue (array, idx, lowerbound);
163 warning ("array or string index out of range");
164 /* fall doing C stuff */
165 c_style = 1;
166 }
167
168 if (lowerbound != 0)
169 {
170 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
171 idx = value_sub (idx, bound);
172 }
173
174 array = value_coerce_array (array);
175 }
176
177 if (TYPE_CODE (tarray) == TYPE_CODE_BITSTRING)
178 {
179 struct type *range_type = TYPE_INDEX_TYPE (tarray);
180 LONGEST index = value_as_long (idx);
181 value_ptr v;
182 int offset, byte, bit_index;
183 LONGEST lowerbound, upperbound;
184 get_discrete_bounds (range_type, &lowerbound, &upperbound);
185 if (index < lowerbound || index > upperbound)
186 error ("bitstring index out of range");
187 index -= lowerbound;
188 offset = index / TARGET_CHAR_BIT;
c5aa993b 189 byte = *((char *) VALUE_CONTENTS (array) + offset);
c906108c
SS
190 bit_index = index % TARGET_CHAR_BIT;
191 byte >>= (BITS_BIG_ENDIAN ? TARGET_CHAR_BIT - 1 - bit_index : bit_index);
192 v = value_from_longest (LA_BOOL_TYPE, byte & 1);
193 VALUE_BITPOS (v) = bit_index;
194 VALUE_BITSIZE (v) = 1;
195 VALUE_LVAL (v) = VALUE_LVAL (array);
196 if (VALUE_LVAL (array) == lval_internalvar)
197 VALUE_LVAL (v) = lval_internalvar_component;
198 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
199 VALUE_OFFSET (v) = offset + VALUE_OFFSET (array);
200 return v;
201 }
202
203 if (c_style)
204 return value_ind (value_add (array, idx));
205 else
206 error ("not an array or string");
207}
208
209/* Return the value of EXPR[IDX], expr an aggregate rvalue
210 (eg, a vector register). This routine used to promote floats
211 to doubles, but no longer does. */
212
213static value_ptr
fba45db2 214value_subscripted_rvalue (value_ptr array, value_ptr idx, int lowerbound)
c906108c
SS
215{
216 struct type *array_type = check_typedef (VALUE_TYPE (array));
217 struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
218 unsigned int elt_size = TYPE_LENGTH (elt_type);
219 LONGEST index = value_as_long (idx);
220 unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
221 value_ptr v;
222
223 if (index < lowerbound || elt_offs >= TYPE_LENGTH (array_type))
224 error ("no such vector element");
225
226 v = allocate_value (elt_type);
227 if (VALUE_LAZY (array))
228 VALUE_LAZY (v) = 1;
229 else
230 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
231
232 if (VALUE_LVAL (array) == lval_internalvar)
233 VALUE_LVAL (v) = lval_internalvar_component;
234 else
235 VALUE_LVAL (v) = VALUE_LVAL (array);
236 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
237 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
238 return v;
239}
240\f
241/* Check to see if either argument is a structure. This is called so
242 we know whether to go ahead with the normal binop or look for a
243 user defined function instead.
244
245 For now, we do not overload the `=' operator. */
246
247int
fba45db2 248binop_user_defined_p (enum exp_opcode op, value_ptr arg1, value_ptr arg2)
c906108c
SS
249{
250 struct type *type1, *type2;
251 if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
252 return 0;
253 type1 = check_typedef (VALUE_TYPE (arg1));
254 type2 = check_typedef (VALUE_TYPE (arg2));
255 return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
256 || TYPE_CODE (type2) == TYPE_CODE_STRUCT
257 || (TYPE_CODE (type1) == TYPE_CODE_REF
258 && TYPE_CODE (TYPE_TARGET_TYPE (type1)) == TYPE_CODE_STRUCT)
259 || (TYPE_CODE (type2) == TYPE_CODE_REF
260 && TYPE_CODE (TYPE_TARGET_TYPE (type2)) == TYPE_CODE_STRUCT));
261}
262
263/* Check to see if argument is a structure. This is called so
264 we know whether to go ahead with the normal unop or look for a
265 user defined function instead.
266
267 For now, we do not overload the `&' operator. */
268
c5aa993b 269int
fba45db2 270unop_user_defined_p (enum exp_opcode op, value_ptr arg1)
c906108c
SS
271{
272 struct type *type1;
273 if (op == UNOP_ADDR)
274 return 0;
275 type1 = check_typedef (VALUE_TYPE (arg1));
276 for (;;)
277 {
278 if (TYPE_CODE (type1) == TYPE_CODE_STRUCT)
279 return 1;
280 else if (TYPE_CODE (type1) == TYPE_CODE_REF)
281 type1 = TYPE_TARGET_TYPE (type1);
282 else
283 return 0;
284 }
285}
286
287/* We know either arg1 or arg2 is a structure, so try to find the right
288 user defined function. Create an argument vector that calls
289 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
290 binary operator which is legal for GNU C++).
291
292 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
293 is the opcode saying how to modify it. Otherwise, OTHEROP is
294 unused. */
295
296value_ptr
fba45db2
KB
297value_x_binop (value_ptr arg1, value_ptr arg2, enum exp_opcode op,
298 enum exp_opcode otherop, enum noside noside)
c906108c 299{
c5aa993b 300 value_ptr *argvec;
c906108c
SS
301 char *ptr;
302 char tstr[13];
303 int static_memfuncp;
304
305 COERCE_REF (arg1);
306 COERCE_REF (arg2);
307 COERCE_ENUM (arg1);
308 COERCE_ENUM (arg2);
309
310 /* now we know that what we have to do is construct our
311 arg vector and find the right function to call it with. */
312
313 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
c5aa993b 314 error ("Can't do that binary op on that type"); /* FIXME be explicit */
c906108c
SS
315
316 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4);
317 argvec[1] = value_addr (arg1);
318 argvec[2] = arg2;
319 argvec[3] = 0;
320
c5aa993b
JM
321 /* make the right function name up */
322 strcpy (tstr, "operator__");
323 ptr = tstr + 8;
c906108c
SS
324 switch (op)
325 {
c5aa993b
JM
326 case BINOP_ADD:
327 strcpy (ptr, "+");
328 break;
329 case BINOP_SUB:
330 strcpy (ptr, "-");
331 break;
332 case BINOP_MUL:
333 strcpy (ptr, "*");
334 break;
335 case BINOP_DIV:
336 strcpy (ptr, "/");
337 break;
338 case BINOP_REM:
339 strcpy (ptr, "%");
340 break;
341 case BINOP_LSH:
342 strcpy (ptr, "<<");
343 break;
344 case BINOP_RSH:
345 strcpy (ptr, ">>");
346 break;
347 case BINOP_BITWISE_AND:
348 strcpy (ptr, "&");
349 break;
350 case BINOP_BITWISE_IOR:
351 strcpy (ptr, "|");
352 break;
353 case BINOP_BITWISE_XOR:
354 strcpy (ptr, "^");
355 break;
356 case BINOP_LOGICAL_AND:
357 strcpy (ptr, "&&");
358 break;
359 case BINOP_LOGICAL_OR:
360 strcpy (ptr, "||");
361 break;
362 case BINOP_MIN:
363 strcpy (ptr, "<?");
364 break;
365 case BINOP_MAX:
366 strcpy (ptr, ">?");
367 break;
368 case BINOP_ASSIGN:
369 strcpy (ptr, "=");
370 break;
371 case BINOP_ASSIGN_MODIFY:
c906108c
SS
372 switch (otherop)
373 {
c5aa993b
JM
374 case BINOP_ADD:
375 strcpy (ptr, "+=");
376 break;
377 case BINOP_SUB:
378 strcpy (ptr, "-=");
379 break;
380 case BINOP_MUL:
381 strcpy (ptr, "*=");
382 break;
383 case BINOP_DIV:
384 strcpy (ptr, "/=");
385 break;
386 case BINOP_REM:
387 strcpy (ptr, "%=");
388 break;
389 case BINOP_BITWISE_AND:
390 strcpy (ptr, "&=");
391 break;
392 case BINOP_BITWISE_IOR:
393 strcpy (ptr, "|=");
394 break;
395 case BINOP_BITWISE_XOR:
396 strcpy (ptr, "^=");
397 break;
398 case BINOP_MOD: /* invalid */
c906108c
SS
399 default:
400 error ("Invalid binary operation specified.");
401 }
402 break;
c5aa993b
JM
403 case BINOP_SUBSCRIPT:
404 strcpy (ptr, "[]");
405 break;
406 case BINOP_EQUAL:
407 strcpy (ptr, "==");
408 break;
409 case BINOP_NOTEQUAL:
410 strcpy (ptr, "!=");
411 break;
412 case BINOP_LESS:
413 strcpy (ptr, "<");
414 break;
415 case BINOP_GTR:
416 strcpy (ptr, ">");
417 break;
418 case BINOP_GEQ:
419 strcpy (ptr, ">=");
420 break;
421 case BINOP_LEQ:
422 strcpy (ptr, "<=");
423 break;
424 case BINOP_MOD: /* invalid */
c906108c
SS
425 default:
426 error ("Invalid binary operation specified.");
427 }
428
c5aa993b
JM
429 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
430
c906108c
SS
431 if (argvec[0])
432 {
433 if (static_memfuncp)
434 {
435 argvec[1] = argvec[0];
436 argvec++;
437 }
438 if (noside == EVAL_AVOID_SIDE_EFFECTS)
439 {
440 struct type *return_type;
441 return_type
442 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
443 return value_zero (return_type, VALUE_LVAL (arg1));
444 }
445 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
446 }
447 error ("member function %s not found", tstr);
448#ifdef lint
449 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
450#endif
451}
452
453/* We know that arg1 is a structure, so try to find a unary user
454 defined operator that matches the operator in question.
455 Create an argument vector that calls arg1.operator @ (arg1)
456 and return that value (where '@' is (almost) any unary operator which
457 is legal for GNU C++). */
458
459value_ptr
fba45db2 460value_x_unop (value_ptr arg1, enum exp_opcode op, enum noside noside)
c906108c 461{
c5aa993b 462 value_ptr *argvec;
c906108c
SS
463 char *ptr, *mangle_ptr;
464 char tstr[13], mangle_tstr[13];
465 int static_memfuncp;
466
467 COERCE_REF (arg1);
468 COERCE_ENUM (arg1);
469
470 /* now we know that what we have to do is construct our
471 arg vector and find the right function to call it with. */
472
473 if (TYPE_CODE (check_typedef (VALUE_TYPE (arg1))) != TYPE_CODE_STRUCT)
c5aa993b 474 error ("Can't do that unary op on that type"); /* FIXME be explicit */
c906108c
SS
475
476 argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3);
477 argvec[1] = value_addr (arg1);
478 argvec[2] = 0;
479
c5aa993b
JM
480 /* make the right function name up */
481 strcpy (tstr, "operator__");
482 ptr = tstr + 8;
483 strcpy (mangle_tstr, "__");
484 mangle_ptr = mangle_tstr + 2;
c906108c
SS
485 switch (op)
486 {
c5aa993b
JM
487 case UNOP_PREINCREMENT:
488 strcpy (ptr, "++");
489 break;
490 case UNOP_PREDECREMENT:
491 strcpy (ptr, "++");
492 break;
493 case UNOP_POSTINCREMENT:
494 strcpy (ptr, "++");
495 break;
496 case UNOP_POSTDECREMENT:
497 strcpy (ptr, "++");
498 break;
499 case UNOP_LOGICAL_NOT:
500 strcpy (ptr, "!");
501 break;
502 case UNOP_COMPLEMENT:
503 strcpy (ptr, "~");
504 break;
505 case UNOP_NEG:
506 strcpy (ptr, "-");
507 break;
508 case UNOP_IND:
509 strcpy (ptr, "*");
510 break;
c906108c
SS
511 default:
512 error ("Invalid unary operation specified.");
513 }
514
c5aa993b 515 argvec[0] = value_struct_elt (&arg1, argvec + 1, tstr, &static_memfuncp, "structure");
c906108c
SS
516
517 if (argvec[0])
518 {
519 if (static_memfuncp)
520 {
521 argvec[1] = argvec[0];
522 argvec++;
523 }
524 if (noside == EVAL_AVOID_SIDE_EFFECTS)
525 {
526 struct type *return_type;
527 return_type
528 = TYPE_TARGET_TYPE (check_typedef (VALUE_TYPE (argvec[0])));
529 return value_zero (return_type, VALUE_LVAL (arg1));
530 }
531 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
532 }
533 error ("member function %s not found", tstr);
c5aa993b 534 return 0; /* For lint -- never reached */
c906108c 535}
c906108c 536\f
c5aa993b 537
c906108c
SS
538/* Concatenate two values with the following conditions:
539
c5aa993b
JM
540 (1) Both values must be either bitstring values or character string
541 values and the resulting value consists of the concatenation of
542 ARG1 followed by ARG2.
c906108c 543
c5aa993b 544 or
c906108c 545
c5aa993b
JM
546 One value must be an integer value and the other value must be
547 either a bitstring value or character string value, which is
548 to be repeated by the number of times specified by the integer
549 value.
c906108c
SS
550
551
c5aa993b
JM
552 (2) Boolean values are also allowed and are treated as bit string
553 values of length 1.
c906108c 554
c5aa993b
JM
555 (3) Character values are also allowed and are treated as character
556 string values of length 1.
557 */
c906108c
SS
558
559value_ptr
fba45db2 560value_concat (value_ptr arg1, value_ptr arg2)
c906108c 561{
c4093a6a 562 register value_ptr inval1, inval2, outval = NULL;
c906108c
SS
563 int inval1len, inval2len;
564 int count, idx;
565 char *ptr;
566 char inchar;
567 struct type *type1 = check_typedef (VALUE_TYPE (arg1));
568 struct type *type2 = check_typedef (VALUE_TYPE (arg2));
569
570 COERCE_VARYING_ARRAY (arg1, type1);
571 COERCE_VARYING_ARRAY (arg2, type2);
572
573 /* First figure out if we are dealing with two values to be concatenated
574 or a repeat count and a value to be repeated. INVAL1 is set to the
575 first of two concatenated values, or the repeat count. INVAL2 is set
576 to the second of the two concatenated values or the value to be
577 repeated. */
578
579 if (TYPE_CODE (type2) == TYPE_CODE_INT)
580 {
581 struct type *tmp = type1;
582 type1 = tmp;
583 tmp = type2;
584 inval1 = arg2;
585 inval2 = arg1;
586 }
587 else
588 {
589 inval1 = arg1;
590 inval2 = arg2;
591 }
592
593 /* Now process the input values. */
594
595 if (TYPE_CODE (type1) == TYPE_CODE_INT)
596 {
597 /* We have a repeat count. Validate the second value and then
c5aa993b 598 construct a value repeated that many times. */
c906108c
SS
599 if (TYPE_CODE (type2) == TYPE_CODE_STRING
600 || TYPE_CODE (type2) == TYPE_CODE_CHAR)
601 {
602 count = longest_to_int (value_as_long (inval1));
603 inval2len = TYPE_LENGTH (type2);
604 ptr = (char *) alloca (count * inval2len);
605 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
606 {
607 inchar = (char) unpack_long (type2,
608 VALUE_CONTENTS (inval2));
609 for (idx = 0; idx < count; idx++)
610 {
611 *(ptr + idx) = inchar;
612 }
613 }
614 else
615 {
616 for (idx = 0; idx < count; idx++)
617 {
618 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
619 inval2len);
620 }
621 }
622 outval = value_string (ptr, count * inval2len);
623 }
624 else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
625 || TYPE_CODE (type2) == TYPE_CODE_BOOL)
626 {
627 error ("unimplemented support for bitstring/boolean repeats");
628 }
629 else
630 {
631 error ("can't repeat values of that type");
632 }
633 }
634 else if (TYPE_CODE (type1) == TYPE_CODE_STRING
c5aa993b 635 || TYPE_CODE (type1) == TYPE_CODE_CHAR)
c906108c
SS
636 {
637 /* We have two character strings to concatenate. */
638 if (TYPE_CODE (type2) != TYPE_CODE_STRING
639 && TYPE_CODE (type2) != TYPE_CODE_CHAR)
640 {
641 error ("Strings can only be concatenated with other strings.");
642 }
643 inval1len = TYPE_LENGTH (type1);
644 inval2len = TYPE_LENGTH (type2);
645 ptr = (char *) alloca (inval1len + inval2len);
646 if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
647 {
648 *ptr = (char) unpack_long (type1, VALUE_CONTENTS (inval1));
649 }
650 else
651 {
652 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
653 }
654 if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
655 {
c5aa993b 656 *(ptr + inval1len) =
c906108c
SS
657 (char) unpack_long (type2, VALUE_CONTENTS (inval2));
658 }
659 else
660 {
661 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
662 }
663 outval = value_string (ptr, inval1len + inval2len);
664 }
665 else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
666 || TYPE_CODE (type1) == TYPE_CODE_BOOL)
667 {
668 /* We have two bitstrings to concatenate. */
669 if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
670 && TYPE_CODE (type2) != TYPE_CODE_BOOL)
671 {
672 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
673 }
674 error ("unimplemented support for bitstring/boolean concatenation.");
c5aa993b 675 }
c906108c
SS
676 else
677 {
678 /* We don't know how to concatenate these operands. */
679 error ("illegal operands for concatenation.");
680 }
681 return (outval);
682}
c906108c
SS
683\f
684
c5aa993b 685
c906108c
SS
686/* Perform a binary operation on two operands which have reasonable
687 representations as integers or floats. This includes booleans,
688 characters, integers, or floats.
689 Does not support addition and subtraction on pointers;
690 use value_add or value_sub if you want to handle those possibilities. */
691
692value_ptr
fba45db2 693value_binop (value_ptr arg1, value_ptr arg2, enum exp_opcode op)
c906108c
SS
694{
695 register value_ptr val;
696 struct type *type1, *type2;
697
698 COERCE_REF (arg1);
699 COERCE_REF (arg2);
700 COERCE_ENUM (arg1);
701 COERCE_ENUM (arg2);
702 type1 = check_typedef (VALUE_TYPE (arg1));
703 type2 = check_typedef (VALUE_TYPE (arg2));
704
705 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
706 && TYPE_CODE (type1) != TYPE_CODE_CHAR
707 && TYPE_CODE (type1) != TYPE_CODE_INT
708 && TYPE_CODE (type1) != TYPE_CODE_BOOL
709 && TYPE_CODE (type1) != TYPE_CODE_RANGE)
710 ||
711 (TYPE_CODE (type2) != TYPE_CODE_FLT
712 && TYPE_CODE (type2) != TYPE_CODE_CHAR
713 && TYPE_CODE (type2) != TYPE_CODE_INT
714 && TYPE_CODE (type2) != TYPE_CODE_BOOL
715 && TYPE_CODE (type2) != TYPE_CODE_RANGE))
716 error ("Argument to arithmetic operation not a number or boolean.");
717
718 if (TYPE_CODE (type1) == TYPE_CODE_FLT
719 ||
720 TYPE_CODE (type2) == TYPE_CODE_FLT)
721 {
722 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
c5aa993b
JM
723 in target format. real.c in GCC probably has the necessary
724 code. */
c4093a6a 725 DOUBLEST v1, v2, v = 0;
c906108c
SS
726 v1 = value_as_double (arg1);
727 v2 = value_as_double (arg2);
728 switch (op)
729 {
730 case BINOP_ADD:
731 v = v1 + v2;
732 break;
733
734 case BINOP_SUB:
735 v = v1 - v2;
736 break;
737
738 case BINOP_MUL:
739 v = v1 * v2;
740 break;
741
742 case BINOP_DIV:
743 v = v1 / v2;
744 break;
745
c4093a6a
JM
746 case BINOP_EXP:
747 v = pow (v1, v2);
748 if (errno)
749 error ("Cannot perform exponentiation: %s", strerror (errno));
750 break;
751
c906108c
SS
752 default:
753 error ("Integer-only operation on floating point number.");
754 }
755
756 /* If either arg was long double, make sure that value is also long
c5aa993b 757 double. */
c906108c 758
c5aa993b
JM
759 if (TYPE_LENGTH (type1) * 8 > TARGET_DOUBLE_BIT
760 || TYPE_LENGTH (type2) * 8 > TARGET_DOUBLE_BIT)
c906108c
SS
761 val = allocate_value (builtin_type_long_double);
762 else
763 val = allocate_value (builtin_type_double);
764
765 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
766 v);
767 }
768 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
769 &&
770 TYPE_CODE (type2) == TYPE_CODE_BOOL)
c5aa993b 771 {
c4093a6a 772 LONGEST v1, v2, v = 0;
c5aa993b
JM
773 v1 = value_as_long (arg1);
774 v2 = value_as_long (arg2);
775
776 switch (op)
777 {
778 case BINOP_BITWISE_AND:
779 v = v1 & v2;
780 break;
781
782 case BINOP_BITWISE_IOR:
783 v = v1 | v2;
784 break;
785
786 case BINOP_BITWISE_XOR:
787 v = v1 ^ v2;
c4093a6a
JM
788 break;
789
790 case BINOP_EQUAL:
791 v = v1 == v2;
792 break;
793
794 case BINOP_NOTEQUAL:
795 v = v1 != v2;
c5aa993b
JM
796 break;
797
798 default:
799 error ("Invalid operation on booleans.");
800 }
801
802 val = allocate_value (type1);
803 store_signed_integer (VALUE_CONTENTS_RAW (val),
804 TYPE_LENGTH (type1),
805 v);
806 }
c906108c
SS
807 else
808 /* Integral operations here. */
809 /* FIXME: Also mixed integral/booleans, with result an integer. */
810 /* FIXME: This implements ANSI C rules (also correct for C++).
811 What about FORTRAN and chill? */
812 {
813 unsigned int promoted_len1 = TYPE_LENGTH (type1);
814 unsigned int promoted_len2 = TYPE_LENGTH (type2);
815 int is_unsigned1 = TYPE_UNSIGNED (type1);
816 int is_unsigned2 = TYPE_UNSIGNED (type2);
817 unsigned int result_len;
818 int unsigned_operation;
819
820 /* Determine type length and signedness after promotion for
c5aa993b 821 both operands. */
c906108c
SS
822 if (promoted_len1 < TYPE_LENGTH (builtin_type_int))
823 {
824 is_unsigned1 = 0;
825 promoted_len1 = TYPE_LENGTH (builtin_type_int);
826 }
827 if (promoted_len2 < TYPE_LENGTH (builtin_type_int))
828 {
829 is_unsigned2 = 0;
830 promoted_len2 = TYPE_LENGTH (builtin_type_int);
831 }
832
833 /* Determine type length of the result, and if the operation should
c5aa993b
JM
834 be done unsigned.
835 Use the signedness of the operand with the greater length.
836 If both operands are of equal length, use unsigned operation
837 if one of the operands is unsigned. */
c906108c
SS
838 if (promoted_len1 > promoted_len2)
839 {
840 unsigned_operation = is_unsigned1;
841 result_len = promoted_len1;
842 }
843 else if (promoted_len2 > promoted_len1)
844 {
845 unsigned_operation = is_unsigned2;
846 result_len = promoted_len2;
847 }
848 else
849 {
850 unsigned_operation = is_unsigned1 || is_unsigned2;
851 result_len = promoted_len1;
852 }
853
854 if (unsigned_operation)
855 {
c4093a6a 856 ULONGEST v1, v2, v = 0;
c906108c
SS
857 v1 = (ULONGEST) value_as_long (arg1);
858 v2 = (ULONGEST) value_as_long (arg2);
859
860 /* Truncate values to the type length of the result. */
861 if (result_len < sizeof (ULONGEST))
862 {
863 v1 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
864 v2 &= ((LONGEST) 1 << HOST_CHAR_BIT * result_len) - 1;
865 }
c5aa993b 866
c906108c
SS
867 switch (op)
868 {
869 case BINOP_ADD:
870 v = v1 + v2;
871 break;
c5aa993b 872
c906108c
SS
873 case BINOP_SUB:
874 v = v1 - v2;
875 break;
c5aa993b 876
c906108c
SS
877 case BINOP_MUL:
878 v = v1 * v2;
879 break;
c5aa993b 880
c906108c
SS
881 case BINOP_DIV:
882 v = v1 / v2;
883 break;
c5aa993b 884
c4093a6a
JM
885 case BINOP_EXP:
886 v = pow (v1, v2);
887 if (errno)
888 error ("Cannot perform exponentiation: %s", strerror (errno));
889 break;
890
c906108c
SS
891 case BINOP_REM:
892 v = v1 % v2;
893 break;
c5aa993b 894
c906108c
SS
895 case BINOP_MOD:
896 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
897 v1 mod 0 has a defined value, v1. */
898 /* Chill specifies that v2 must be > 0, so check for that. */
c5aa993b 899 if (current_language->la_language == language_chill
c906108c
SS
900 && value_as_long (arg2) <= 0)
901 {
902 error ("Second operand of MOD must be greater than zero.");
903 }
904 if (v2 == 0)
905 {
906 v = v1;
907 }
908 else
909 {
c5aa993b 910 v = v1 / v2;
c906108c
SS
911 /* Note floor(v1/v2) == v1/v2 for unsigned. */
912 v = v1 - (v2 * v);
913 }
914 break;
c5aa993b 915
c906108c
SS
916 case BINOP_LSH:
917 v = v1 << v2;
918 break;
c5aa993b 919
c906108c
SS
920 case BINOP_RSH:
921 v = v1 >> v2;
922 break;
c5aa993b 923
c906108c
SS
924 case BINOP_BITWISE_AND:
925 v = v1 & v2;
926 break;
c5aa993b 927
c906108c
SS
928 case BINOP_BITWISE_IOR:
929 v = v1 | v2;
930 break;
c5aa993b 931
c906108c
SS
932 case BINOP_BITWISE_XOR:
933 v = v1 ^ v2;
934 break;
c5aa993b 935
c906108c
SS
936 case BINOP_LOGICAL_AND:
937 v = v1 && v2;
938 break;
c5aa993b 939
c906108c
SS
940 case BINOP_LOGICAL_OR:
941 v = v1 || v2;
942 break;
c5aa993b 943
c906108c
SS
944 case BINOP_MIN:
945 v = v1 < v2 ? v1 : v2;
946 break;
c5aa993b 947
c906108c
SS
948 case BINOP_MAX:
949 v = v1 > v2 ? v1 : v2;
950 break;
951
952 case BINOP_EQUAL:
953 v = v1 == v2;
954 break;
955
c4093a6a
JM
956 case BINOP_NOTEQUAL:
957 v = v1 != v2;
958 break;
959
c906108c
SS
960 case BINOP_LESS:
961 v = v1 < v2;
962 break;
c5aa993b 963
c906108c
SS
964 default:
965 error ("Invalid binary operation on numbers.");
966 }
967
968 /* This is a kludge to get around the fact that we don't
969 know how to determine the result type from the types of
970 the operands. (I'm not really sure how much we feel the
971 need to duplicate the exact rules of the current
972 language. They can get really hairy. But not to do so
973 makes it hard to document just what we *do* do). */
974
975 /* Can't just call init_type because we wouldn't know what
976 name to give the type. */
977 val = allocate_value
978 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
979 ? builtin_type_unsigned_long_long
980 : builtin_type_unsigned_long);
981 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
982 TYPE_LENGTH (VALUE_TYPE (val)),
983 v);
984 }
985 else
986 {
c4093a6a 987 LONGEST v1, v2, v = 0;
c906108c
SS
988 v1 = value_as_long (arg1);
989 v2 = value_as_long (arg2);
c5aa993b 990
c906108c
SS
991 switch (op)
992 {
993 case BINOP_ADD:
994 v = v1 + v2;
995 break;
c5aa993b 996
c906108c
SS
997 case BINOP_SUB:
998 v = v1 - v2;
999 break;
c5aa993b 1000
c906108c
SS
1001 case BINOP_MUL:
1002 v = v1 * v2;
1003 break;
c5aa993b 1004
c906108c
SS
1005 case BINOP_DIV:
1006 v = v1 / v2;
c4093a6a
JM
1007 break;
1008
1009 case BINOP_EXP:
1010 v = pow (v1, v2);
1011 if (errno)
1012 error ("Cannot perform exponentiation: %s", strerror (errno));
c906108c 1013 break;
c5aa993b 1014
c906108c
SS
1015 case BINOP_REM:
1016 v = v1 % v2;
1017 break;
c5aa993b 1018
c906108c
SS
1019 case BINOP_MOD:
1020 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
1021 X mod 0 has a defined value, X. */
1022 /* Chill specifies that v2 must be > 0, so check for that. */
c5aa993b 1023 if (current_language->la_language == language_chill
c906108c
SS
1024 && v2 <= 0)
1025 {
1026 error ("Second operand of MOD must be greater than zero.");
1027 }
1028 if (v2 == 0)
1029 {
1030 v = v1;
1031 }
1032 else
1033 {
c5aa993b 1034 v = v1 / v2;
c906108c
SS
1035 /* Compute floor. */
1036 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
1037 {
1038 v--;
1039 }
1040 v = v1 - (v2 * v);
1041 }
1042 break;
c5aa993b 1043
c906108c
SS
1044 case BINOP_LSH:
1045 v = v1 << v2;
1046 break;
c5aa993b 1047
c906108c
SS
1048 case BINOP_RSH:
1049 v = v1 >> v2;
1050 break;
c5aa993b 1051
c906108c
SS
1052 case BINOP_BITWISE_AND:
1053 v = v1 & v2;
1054 break;
c5aa993b 1055
c906108c
SS
1056 case BINOP_BITWISE_IOR:
1057 v = v1 | v2;
1058 break;
c5aa993b 1059
c906108c
SS
1060 case BINOP_BITWISE_XOR:
1061 v = v1 ^ v2;
1062 break;
c5aa993b 1063
c906108c
SS
1064 case BINOP_LOGICAL_AND:
1065 v = v1 && v2;
1066 break;
c5aa993b 1067
c906108c
SS
1068 case BINOP_LOGICAL_OR:
1069 v = v1 || v2;
1070 break;
c5aa993b 1071
c906108c
SS
1072 case BINOP_MIN:
1073 v = v1 < v2 ? v1 : v2;
1074 break;
c5aa993b 1075
c906108c
SS
1076 case BINOP_MAX:
1077 v = v1 > v2 ? v1 : v2;
1078 break;
1079
1080 case BINOP_EQUAL:
1081 v = v1 == v2;
1082 break;
1083
1084 case BINOP_LESS:
1085 v = v1 < v2;
1086 break;
c5aa993b 1087
c906108c
SS
1088 default:
1089 error ("Invalid binary operation on numbers.");
1090 }
1091
1092 /* This is a kludge to get around the fact that we don't
1093 know how to determine the result type from the types of
1094 the operands. (I'm not really sure how much we feel the
1095 need to duplicate the exact rules of the current
1096 language. They can get really hairy. But not to do so
1097 makes it hard to document just what we *do* do). */
1098
1099 /* Can't just call init_type because we wouldn't know what
1100 name to give the type. */
1101 val = allocate_value
1102 (result_len > TARGET_LONG_BIT / HOST_CHAR_BIT
1103 ? builtin_type_long_long
1104 : builtin_type_long);
1105 store_signed_integer (VALUE_CONTENTS_RAW (val),
1106 TYPE_LENGTH (VALUE_TYPE (val)),
1107 v);
1108 }
1109 }
1110
1111 return val;
1112}
1113\f
1114/* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
1115
1116int
fba45db2 1117value_logical_not (value_ptr arg1)
c906108c
SS
1118{
1119 register int len;
1120 register char *p;
1121 struct type *type1;
1122
1123 COERCE_NUMBER (arg1);
1124 type1 = check_typedef (VALUE_TYPE (arg1));
1125
1126 if (TYPE_CODE (type1) == TYPE_CODE_FLT)
1127 return 0 == value_as_double (arg1);
1128
1129 len = TYPE_LENGTH (type1);
1130 p = VALUE_CONTENTS (arg1);
1131
1132 while (--len >= 0)
1133 {
1134 if (*p++)
1135 break;
1136 }
1137
1138 return len < 0;
1139}
1140
c4093a6a
JM
1141/* Perform a comparison on two string values (whose content are not
1142 necessarily null terminated) based on their length */
1143
1144static int
fba45db2 1145value_strcmp (register value_ptr arg1, register value_ptr arg2)
c4093a6a
JM
1146{
1147 int len1 = TYPE_LENGTH (VALUE_TYPE (arg1));
1148 int len2 = TYPE_LENGTH (VALUE_TYPE (arg2));
1149 char *s1 = VALUE_CONTENTS (arg1);
1150 char *s2 = VALUE_CONTENTS (arg2);
1151 int i, len = len1 < len2 ? len1 : len2;
1152
1153 for (i = 0; i < len; i++)
1154 {
1155 if (s1[i] < s2[i])
1156 return -1;
1157 else if (s1[i] > s2[i])
1158 return 1;
1159 else
1160 continue;
1161 }
1162
1163 if (len1 < len2)
1164 return -1;
1165 else if (len1 > len2)
1166 return 1;
1167 else
1168 return 0;
1169}
1170
c906108c
SS
1171/* Simulate the C operator == by returning a 1
1172 iff ARG1 and ARG2 have equal contents. */
1173
1174int
fba45db2 1175value_equal (register value_ptr arg1, register value_ptr arg2)
c906108c
SS
1176{
1177 register int len;
1178 register char *p1, *p2;
1179 struct type *type1, *type2;
1180 enum type_code code1;
1181 enum type_code code2;
1182
1183 COERCE_NUMBER (arg1);
1184 COERCE_NUMBER (arg2);
1185
1186 type1 = check_typedef (VALUE_TYPE (arg1));
1187 type2 = check_typedef (VALUE_TYPE (arg2));
1188 code1 = TYPE_CODE (type1);
1189 code2 = TYPE_CODE (type2);
1190
1191 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1192 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1193 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1194 BINOP_EQUAL)));
1195 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1196 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1197 return value_as_double (arg1) == value_as_double (arg2);
1198
1199 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1200 is bigger. */
1201 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1202 return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
1203 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1204 return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
1205
1206 else if (code1 == code2
1207 && ((len = (int) TYPE_LENGTH (type1))
1208 == (int) TYPE_LENGTH (type2)))
1209 {
1210 p1 = VALUE_CONTENTS (arg1);
1211 p2 = VALUE_CONTENTS (arg2);
1212 while (--len >= 0)
1213 {
1214 if (*p1++ != *p2++)
1215 break;
1216 }
1217 return len < 0;
1218 }
c4093a6a
JM
1219 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1220 {
1221 return value_strcmp (arg1, arg2) == 0;
1222 }
c906108c
SS
1223 else
1224 {
1225 error ("Invalid type combination in equality test.");
c5aa993b 1226 return 0; /* For lint -- never reached */
c906108c
SS
1227 }
1228}
1229
1230/* Simulate the C operator < by returning 1
1231 iff ARG1's contents are less than ARG2's. */
1232
1233int
fba45db2 1234value_less (register value_ptr arg1, register value_ptr arg2)
c906108c
SS
1235{
1236 register enum type_code code1;
1237 register enum type_code code2;
1238 struct type *type1, *type2;
1239
1240 COERCE_NUMBER (arg1);
1241 COERCE_NUMBER (arg2);
1242
1243 type1 = check_typedef (VALUE_TYPE (arg1));
1244 type2 = check_typedef (VALUE_TYPE (arg2));
1245 code1 = TYPE_CODE (type1);
1246 code2 = TYPE_CODE (type2);
1247
1248 if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL) &&
1249 (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1250 return longest_to_int (value_as_long (value_binop (arg1, arg2,
1251 BINOP_LESS)));
1252 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL)
1253 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1254 return value_as_double (arg1) < value_as_double (arg2);
1255 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
1256 return value_as_pointer (arg1) < value_as_pointer (arg2);
1257
1258 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
1259 is bigger. */
1260 else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_BOOL))
1261 return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
1262 else if (code2 == TYPE_CODE_PTR && (code1 == TYPE_CODE_INT || code1 == TYPE_CODE_BOOL))
1263 return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
c4093a6a
JM
1264 else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
1265 return value_strcmp (arg1, arg2) < 0;
c906108c
SS
1266 else
1267 {
1268 error ("Invalid type combination in ordering comparison.");
1269 return 0;
1270 }
1271}
1272\f
1273/* The unary operators - and ~. Both free the argument ARG1. */
1274
1275value_ptr
fba45db2 1276value_neg (register value_ptr arg1)
c906108c
SS
1277{
1278 register struct type *type;
1279 register struct type *result_type = VALUE_TYPE (arg1);
1280
1281 COERCE_REF (arg1);
1282 COERCE_ENUM (arg1);
1283
1284 type = check_typedef (VALUE_TYPE (arg1));
1285
1286 if (TYPE_CODE (type) == TYPE_CODE_FLT)
c5aa993b 1287 return value_from_double (result_type, -value_as_double (arg1));
c906108c
SS
1288 else if (TYPE_CODE (type) == TYPE_CODE_INT || TYPE_CODE (type) == TYPE_CODE_BOOL)
1289 {
1290 /* Perform integral promotion for ANSI C/C++.
c5aa993b 1291 FIXME: What about FORTRAN and chill ? */
c906108c
SS
1292 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1293 result_type = builtin_type_int;
1294
c5aa993b
JM
1295 return value_from_longest (result_type, -value_as_long (arg1));
1296 }
1297 else
1298 {
1299 error ("Argument to negate operation not a number.");
1300 return 0; /* For lint -- never reached */
c906108c 1301 }
c906108c
SS
1302}
1303
1304value_ptr
fba45db2 1305value_complement (register value_ptr arg1)
c906108c
SS
1306{
1307 register struct type *type;
1308 register struct type *result_type = VALUE_TYPE (arg1);
c5aa993b 1309 int typecode;
c906108c
SS
1310
1311 COERCE_REF (arg1);
1312 COERCE_ENUM (arg1);
1313
1314 type = check_typedef (VALUE_TYPE (arg1));
1315
1316 typecode = TYPE_CODE (type);
1317 if ((typecode != TYPE_CODE_INT) && (typecode != TYPE_CODE_BOOL))
1318 error ("Argument to complement operation not an integer or boolean.");
1319
1320 /* Perform integral promotion for ANSI C/C++.
1321 FIXME: What about FORTRAN ? */
1322 if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
1323 result_type = builtin_type_int;
1324
c5aa993b 1325 return value_from_longest (result_type, ~value_as_long (arg1));
c906108c
SS
1326}
1327\f
1328/* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
1329 and whose VALUE_CONTENTS is valaddr.
1330 Return -1 if out of range, -2 other error. */
1331
1332int
fba45db2 1333value_bit_index (struct type *type, char *valaddr, int index)
c906108c
SS
1334{
1335 LONGEST low_bound, high_bound;
1336 LONGEST word;
1337 unsigned rel_index;
1338 struct type *range = TYPE_FIELD_TYPE (type, 0);
1339 if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
1340 return -2;
1341 if (index < low_bound || index > high_bound)
1342 return -1;
1343 rel_index = index - low_bound;
1344 word = unpack_long (builtin_type_unsigned_char,
1345 valaddr + (rel_index / TARGET_CHAR_BIT));
1346 rel_index %= TARGET_CHAR_BIT;
1347 if (BITS_BIG_ENDIAN)
1348 rel_index = TARGET_CHAR_BIT - 1 - rel_index;
1349 return (word >> rel_index) & 1;
1350}
1351
1352value_ptr
fba45db2 1353value_in (value_ptr element, value_ptr set)
c906108c
SS
1354{
1355 int member;
1356 struct type *settype = check_typedef (VALUE_TYPE (set));
1357 struct type *eltype = check_typedef (VALUE_TYPE (element));
1358 if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
1359 eltype = TYPE_TARGET_TYPE (eltype);
1360 if (TYPE_CODE (settype) != TYPE_CODE_SET)
1361 error ("Second argument of 'IN' has wrong type");
1362 if (TYPE_CODE (eltype) != TYPE_CODE_INT
1363 && TYPE_CODE (eltype) != TYPE_CODE_CHAR
1364 && TYPE_CODE (eltype) != TYPE_CODE_ENUM
1365 && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
1366 error ("First argument of 'IN' has wrong type");
1367 member = value_bit_index (settype, VALUE_CONTENTS (set),
1368 value_as_long (element));
1369 if (member < 0)
1370 error ("First argument of 'IN' not in range");
1371 return value_from_longest (LA_BOOL_TYPE, member);
1372}
1373
1374void
fba45db2 1375_initialize_valarith (void)
c906108c
SS
1376{
1377}