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