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