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