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