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