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