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