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