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