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