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