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