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