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