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