* corelow.c, exec.c, inftarg.c, m3-nat.c, op50-rom.c, procfs.c,
[deliverable/binutils-gdb.git] / gdb / valarith.c
CommitLineData
bd5635a1 1/* Perform arithmetic and other operations on values, for GDB.
e17960fb 2 Copyright 1986, 1989, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
088c3a0b 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
088c3a0b
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
088c3a0b 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
088c3a0b
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
bd5635a1 20#include "defs.h"
bd5635a1 21#include "value.h"
088c3a0b 22#include "symtab.h"
51b57ded 23#include "gdbtypes.h"
bd5635a1
RP
24#include "expression.h"
25#include "target.h"
2fcc38b8 26#include "language.h"
eade0c6c 27#include "demangle.h"
bd5635a1
RP
28#include <string.h>
29
2fcc38b8
FF
30/* Define whether or not the C operator '/' truncates towards zero for
31 differently signed operands (truncation direction is undefined in C). */
32
33#ifndef TRUNCATION_TOWARDS_ZERO
34#define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
35#endif
36
088c3a0b
JG
37static value
38value_subscripted_rvalue PARAMS ((value, value));
bd5635a1 39
088c3a0b 40\f
bd5635a1
RP
41value
42value_add (arg1, arg2)
43 value arg1, arg2;
44{
088c3a0b 45 register value valint, valptr;
bd5635a1
RP
46 register int len;
47
48 COERCE_ARRAY (arg1);
49 COERCE_ARRAY (arg2);
50
51 if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
52 || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR)
53 &&
54 (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT
55 || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT))
56 /* Exactly one argument is a pointer, and one is an integer. */
57 {
58 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
59 {
60 valptr = arg1;
61 valint = arg2;
62 }
63 else
64 {
65 valptr = arg2;
66 valint = arg1;
67 }
68 len = TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr)));
69 if (len == 0) len = 1; /* For (void *) */
088c3a0b
JG
70 return value_from_longest (VALUE_TYPE (valptr),
71 value_as_long (valptr)
72 + (len * value_as_long (valint)));
bd5635a1
RP
73 }
74
75 return value_binop (arg1, arg2, BINOP_ADD);
76}
77
78value
79value_sub (arg1, arg2)
80 value arg1, arg2;
81{
bd5635a1
RP
82
83 COERCE_ARRAY (arg1);
84 COERCE_ARRAY (arg2);
85
86 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
87 {
88 if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
89 {
90 /* pointer - integer. */
088c3a0b
JG
91 return value_from_longest
92 (VALUE_TYPE (arg1),
bd5635a1
RP
93 value_as_long (arg1)
94 - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)))
95 * value_as_long (arg2)));
bd5635a1
RP
96 }
97 else if (VALUE_TYPE (arg1) == VALUE_TYPE (arg2))
98 {
99 /* pointer to <type x> - pointer to <type x>. */
088c3a0b
JG
100 return value_from_longest
101 (builtin_type_long, /* FIXME -- should be ptrdiff_t */
bd5635a1
RP
102 (value_as_long (arg1) - value_as_long (arg2))
103 / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))));
bd5635a1
RP
104 }
105 else
106 {
107 error ("\
108First argument of `-' is a pointer and second argument is neither\n\
109an integer nor a pointer of the same type.");
110 }
111 }
112
113 return value_binop (arg1, arg2, BINOP_SUB);
114}
115
fb6e675f
FF
116/* Return the value of ARRAY[IDX].
117 See comments in value_coerce_array() for rationale for reason for
118 doing lower bounds adjustment here rather than there.
119 FIXME: Perhaps we should validate that the index is valid and if
120 verbosity is set, warn about invalid indices (but still use them). */
bd5635a1
RP
121
122value
123value_subscript (array, idx)
124 value array, idx;
125{
fb6e675f
FF
126 int lowerbound;
127 value bound;
128 struct type *range_type;
129
eade0c6c
JK
130 COERCE_REF (array);
131
132 if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
133 || TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_STRING)
fb6e675f
FF
134 {
135 range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
136 lowerbound = TYPE_FIELD_BITPOS (range_type, 0);
137 if (lowerbound != 0)
138 {
139 bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
140 idx = value_sub (idx, bound);
141 }
142 if (VALUE_LVAL (array) != lval_memory)
143 {
144 return value_subscripted_rvalue (array, idx);
145 }
eade0c6c 146 array = value_coerce_array (array);
fb6e675f
FF
147 }
148 return value_ind (value_add (array, idx));
bd5635a1
RP
149}
150
151/* Return the value of EXPR[IDX], expr an aggregate rvalue
152 (eg, a vector register). This routine used to promote floats
153 to doubles, but no longer does. */
154
088c3a0b 155static value
bd5635a1
RP
156value_subscripted_rvalue (array, idx)
157 value array, idx;
158{
159 struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
160 int elt_size = TYPE_LENGTH (elt_type);
088c3a0b 161 int elt_offs = elt_size * longest_to_int (value_as_long (idx));
bd5635a1
RP
162 value v;
163
164 if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array)))
165 error ("no such vector element");
166
167 v = allocate_value (elt_type);
e58de8a2 168 memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
bd5635a1
RP
169
170 if (VALUE_LVAL (array) == lval_internalvar)
171 VALUE_LVAL (v) = lval_internalvar_component;
172 else
173 VALUE_LVAL (v) = not_lval;
174 VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
175 VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
176 VALUE_BITSIZE (v) = elt_size * 8;
177 return v;
178}
179\f
180/* Check to see if either argument is a structure. This is called so
181 we know whether to go ahead with the normal binop or look for a
182 user defined function instead.
183
184 For now, we do not overload the `=' operator. */
185
186int
187binop_user_defined_p (op, arg1, arg2)
188 enum exp_opcode op;
189 value arg1, arg2;
190{
191 if (op == BINOP_ASSIGN)
192 return 0;
193 return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
194 || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
195 || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
196 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
197 || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
198 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
199}
200
201/* Check to see if argument is a structure. This is called so
202 we know whether to go ahead with the normal unop or look for a
203 user defined function instead.
204
205 For now, we do not overload the `&' operator. */
206
207int unop_user_defined_p (op, arg1)
208 enum exp_opcode op;
209 value arg1;
210{
211 if (op == UNOP_ADDR)
212 return 0;
213 return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
214 || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
215 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
216}
217
218/* We know either arg1 or arg2 is a structure, so try to find the right
219 user defined function. Create an argument vector that calls
220 arg1.operator @ (arg1,arg2) and return that value (where '@' is any
088c3a0b
JG
221 binary operator which is legal for GNU C++).
222
223 OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
224 is the opcode saying how to modify it. Otherwise, OTHEROP is
225 unused. */
bd5635a1
RP
226
227value
228value_x_binop (arg1, arg2, op, otherop)
229 value arg1, arg2;
230 enum exp_opcode op, otherop;
231{
232 value * argvec;
eade0c6c
JK
233 char *ptr, *mangle_ptr;
234 char tstr[13], mangle_tstr[13];
bd5635a1
RP
235 int static_memfuncp;
236
088c3a0b
JG
237 COERCE_REF (arg1);
238 COERCE_REF (arg2);
bd5635a1
RP
239 COERCE_ENUM (arg1);
240 COERCE_ENUM (arg2);
241
242 /* now we know that what we have to do is construct our
243 arg vector and find the right function to call it with. */
244
245 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
246 error ("Can't do that binary op on that type"); /* FIXME be explicit */
247
248 argvec = (value *) alloca (sizeof (value) * 4);
249 argvec[1] = value_addr (arg1);
250 argvec[2] = arg2;
251 argvec[3] = 0;
252
253 /* make the right function name up */
254 strcpy(tstr, "operator__");
255 ptr = tstr+8;
256 switch (op)
257 {
e58de8a2
FF
258 case BINOP_ADD: strcpy(ptr,"+"); break;
259 case BINOP_SUB: strcpy(ptr,"-"); break;
260 case BINOP_MUL: strcpy(ptr,"*"); break;
261 case BINOP_DIV: strcpy(ptr,"/"); break;
262 case BINOP_REM: strcpy(ptr,"%"); break;
263 case BINOP_LSH: strcpy(ptr,"<<"); break;
264 case BINOP_RSH: strcpy(ptr,">>"); break;
265 case BINOP_BITWISE_AND: strcpy(ptr,"&"); break;
266 case BINOP_BITWISE_IOR: strcpy(ptr,"|"); break;
267 case BINOP_BITWISE_XOR: strcpy(ptr,"^"); break;
268 case BINOP_LOGICAL_AND: strcpy(ptr,"&&"); break;
269 case BINOP_LOGICAL_OR: strcpy(ptr,"||"); break;
270 case BINOP_MIN: strcpy(ptr,"<?"); break;
271 case BINOP_MAX: strcpy(ptr,">?"); break;
272 case BINOP_ASSIGN: strcpy(ptr,"="); break;
bd5635a1
RP
273 case BINOP_ASSIGN_MODIFY:
274 switch (otherop)
275 {
e58de8a2
FF
276 case BINOP_ADD: strcpy(ptr,"+="); break;
277 case BINOP_SUB: strcpy(ptr,"-="); break;
278 case BINOP_MUL: strcpy(ptr,"*="); break;
279 case BINOP_DIV: strcpy(ptr,"/="); break;
280 case BINOP_REM: strcpy(ptr,"%="); break;
281 case BINOP_BITWISE_AND: strcpy(ptr,"&="); break;
282 case BINOP_BITWISE_IOR: strcpy(ptr,"|="); break;
283 case BINOP_BITWISE_XOR: strcpy(ptr,"^="); break;
2fcc38b8 284 case BINOP_MOD: /* invalid */
bd5635a1
RP
285 default:
286 error ("Invalid binary operation specified.");
287 }
288 break;
289 case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
290 case BINOP_EQUAL: strcpy(ptr,"=="); break;
291 case BINOP_NOTEQUAL: strcpy(ptr,"!="); break;
292 case BINOP_LESS: strcpy(ptr,"<"); break;
293 case BINOP_GTR: strcpy(ptr,">"); break;
294 case BINOP_GEQ: strcpy(ptr,">="); break;
295 case BINOP_LEQ: strcpy(ptr,"<="); break;
2fcc38b8 296 case BINOP_MOD: /* invalid */
bd5635a1
RP
297 default:
298 error ("Invalid binary operation specified.");
299 }
eade0c6c 300
bd5635a1 301 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
eade0c6c 302
bd5635a1
RP
303 if (argvec[0])
304 {
305 if (static_memfuncp)
306 {
307 argvec[1] = argvec[0];
308 argvec++;
309 }
e17960fb 310 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
bd5635a1
RP
311 }
312 error ("member function %s not found", tstr);
313#ifdef lint
e17960fb 314 return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
bd5635a1
RP
315#endif
316}
317
318/* We know that arg1 is a structure, so try to find a unary user
319 defined operator that matches the operator in question.
320 Create an argument vector that calls arg1.operator @ (arg1)
321 and return that value (where '@' is (almost) any unary operator which
322 is legal for GNU C++). */
323
324value
325value_x_unop (arg1, op)
326 value arg1;
327 enum exp_opcode op;
328{
329 value * argvec;
eade0c6c
JK
330 char *ptr, *mangle_ptr;
331 char tstr[13], mangle_tstr[13];
bd5635a1
RP
332 int static_memfuncp;
333
334 COERCE_ENUM (arg1);
335
336 /* now we know that what we have to do is construct our
337 arg vector and find the right function to call it with. */
338
339 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
340 error ("Can't do that unary op on that type"); /* FIXME be explicit */
341
342 argvec = (value *) alloca (sizeof (value) * 3);
343 argvec[1] = value_addr (arg1);
344 argvec[2] = 0;
345
346 /* make the right function name up */
347 strcpy(tstr,"operator__");
348 ptr = tstr+8;
eade0c6c
JK
349 strcpy(mangle_tstr, "__");
350 mangle_ptr = mangle_tstr+2;
bd5635a1
RP
351 switch (op)
352 {
353 case UNOP_PREINCREMENT: strcpy(ptr,"++"); break;
354 case UNOP_PREDECREMENT: strcpy(ptr,"++"); break;
355 case UNOP_POSTINCREMENT: strcpy(ptr,"++"); break;
356 case UNOP_POSTDECREMENT: strcpy(ptr,"++"); break;
e58de8a2
FF
357 case UNOP_LOGICAL_NOT: strcpy(ptr,"!"); break;
358 case UNOP_COMPLEMENT: strcpy(ptr,"~"); break;
359 case UNOP_NEG: strcpy(ptr,"-"); break;
bd5635a1
RP
360 default:
361 error ("Invalid binary operation specified.");
362 }
eade0c6c 363
bd5635a1 364 argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
eade0c6c 365
bd5635a1
RP
366 if (argvec[0])
367 {
368 if (static_memfuncp)
369 {
370 argvec[1] = argvec[0];
371 argvec++;
372 }
e17960fb 373 return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
bd5635a1
RP
374 }
375 error ("member function %s not found", tstr);
376 return 0; /* For lint -- never reached */
377}
2fcc38b8
FF
378
379\f
380/* Concatenate two values with the following conditions:
381
382 (1) Both values must be either bitstring values or character string
383 values and the resulting value consists of the concatenation of
384 ARG1 followed by ARG2.
385
386 or
387
388 One value must be an integer value and the other value must be
389 either a bitstring value or character string value, which is
390 to be repeated by the number of times specified by the integer
391 value.
392
393
394 (2) Boolean values are also allowed and are treated as bit string
395 values of length 1.
396
397 (3) Character values are also allowed and are treated as character
398 string values of length 1.
399*/
400
401value
402value_concat (arg1, arg2)
403 value arg1, arg2;
404{
405 register value inval1, inval2, outval;
406 int inval1len, inval2len;
407 int count, idx;
408 char *ptr;
409 char inchar;
410
411 /* First figure out if we are dealing with two values to be concatenated
412 or a repeat count and a value to be repeated. INVAL1 is set to the
413 first of two concatenated values, or the repeat count. INVAL2 is set
414 to the second of the two concatenated values or the value to be
415 repeated. */
416
417 if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
418 {
419 inval1 = arg2;
420 inval2 = arg1;
421 }
422 else
423 {
424 inval1 = arg1;
425 inval2 = arg2;
426 }
427
428 /* Now process the input values. */
429
430 if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_INT)
431 {
432 /* We have a repeat count. Validate the second value and then
433 construct a value repeated that many times. */
434 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_STRING
435 || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
436 {
437 count = longest_to_int (value_as_long (inval1));
438 inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
439 ptr = (char *) alloca (count * inval2len);
440 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
441 {
442 inchar = (char) unpack_long (VALUE_TYPE (inval2),
443 VALUE_CONTENTS (inval2));
444 for (idx = 0; idx < count; idx++)
445 {
446 *(ptr + idx) = inchar;
447 }
448 }
449 else
450 {
451 for (idx = 0; idx < count; idx++)
452 {
453 memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
454 inval2len);
455 }
456 }
457 outval = value_string (ptr, count * inval2len);
458 }
459 else if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BITSTRING
460 || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BOOL)
461 {
462 error ("unimplemented support for bitstring/boolean repeats");
463 }
464 else
465 {
466 error ("can't repeat values of that type");
467 }
468 }
469 else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_STRING
470 || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
471 {
472 /* We have two character strings to concatenate. */
473 if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_STRING
474 && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_CHAR)
475 {
476 error ("Strings can only be concatenated with other strings.");
477 }
478 inval1len = TYPE_LENGTH (VALUE_TYPE (inval1));
479 inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
480 ptr = (char *) alloca (inval1len + inval2len);
481 if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
482 {
483 *ptr = (char) unpack_long (VALUE_TYPE (inval1), VALUE_CONTENTS (inval1));
484 }
485 else
486 {
487 memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
488 }
489 if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
490 {
491 *(ptr + inval1len) =
492 (char) unpack_long (VALUE_TYPE (inval2), VALUE_CONTENTS (inval2));
493 }
494 else
495 {
496 memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
497 }
498 outval = value_string (ptr, inval1len + inval2len);
499 }
500 else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BITSTRING
501 || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BOOL)
502 {
503 /* We have two bitstrings to concatenate. */
504 if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BITSTRING
505 && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BOOL)
506 {
507 error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
508 }
509 error ("unimplemented support for bitstring/boolean concatenation.");
510 }
511 else
512 {
513 /* We don't know how to concatenate these operands. */
514 error ("illegal operands for concatenation.");
515 }
516 return (outval);
517}
518
bd5635a1 519\f
eade0c6c
JK
520/* The type we give to value_binop results. This is a kludge to get around
521 the fact that we don't know how to determine the result type from
522 the types of the operands. (I'm not really sure how much we feel
523 the need to duplicate the exact rules of the current language.
524 They can get really hairy. But not to do so makes it hard to document
525 just what we *do* do). */
526static struct type *signed_operation_result;
527static struct type *unsigned_operation_result;
528
2fcc38b8
FF
529/* Perform a binary operation on two operands which have reasonable
530 representations as integers or floats. This includes booleans,
531 characters, integers, or floats.
bd5635a1
RP
532 Does not support addition and subtraction on pointers;
533 use value_add or value_sub if you want to handle those possibilities. */
534
535value
536value_binop (arg1, arg2, op)
537 value arg1, arg2;
088c3a0b 538 enum exp_opcode op;
bd5635a1
RP
539{
540 register value val;
541
542 COERCE_ENUM (arg1);
543 COERCE_ENUM (arg2);
544
545 if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT
2fcc38b8
FF
546 &&
547 TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_CHAR
bd5635a1 548 &&
e58de8a2
FF
549 TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT
550 &&
551 TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL)
bd5635a1
RP
552 ||
553 (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT
2fcc38b8
FF
554 &&
555 TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_CHAR
bd5635a1 556 &&
e58de8a2
FF
557 TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT
558 &&
559 TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL))
560 error ("Argument to arithmetic operation not a number or boolean.");
bd5635a1
RP
561
562 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT
563 ||
564 TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT)
565 {
eade0c6c
JK
566 /* FIXME-if-picky-about-floating-accuracy: Should be doing this
567 in target format. real.c in GCC probably has the necessary
568 code. */
bd5635a1
RP
569 double v1, v2, v;
570 v1 = value_as_double (arg1);
571 v2 = value_as_double (arg2);
572 switch (op)
573 {
574 case BINOP_ADD:
575 v = v1 + v2;
576 break;
577
578 case BINOP_SUB:
579 v = v1 - v2;
580 break;
581
582 case BINOP_MUL:
583 v = v1 * v2;
584 break;
585
586 case BINOP_DIV:
587 v = v1 / v2;
588 break;
589
590 default:
591 error ("Integer-only operation on floating point number.");
592 }
593
594 val = allocate_value (builtin_type_double);
eade0c6c
JK
595 store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
596 v);
bd5635a1 597 }
e58de8a2
FF
598 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL
599 &&
600 TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL)
601 {
602 LONGEST v1, v2, v;
603 v1 = value_as_long (arg1);
604 v2 = value_as_long (arg2);
605
606 switch (op)
607 {
608 case BINOP_BITWISE_AND:
609 v = v1 & v2;
610 break;
611
612 case BINOP_BITWISE_IOR:
613 v = v1 | v2;
614 break;
615
616 case BINOP_BITWISE_XOR:
617 v = v1 ^ v2;
618 break;
619
620 default:
621 error ("Invalid operation on booleans.");
622 }
623
624 val = allocate_value (builtin_type_chill_bool);
eade0c6c
JK
625 store_signed_integer (VALUE_CONTENTS_RAW (val),
626 TYPE_LENGTH (VALUE_TYPE (val)),
627 v);
e58de8a2 628 }
bd5635a1
RP
629 else
630 /* Integral operations here. */
e58de8a2 631 /* FIXME: Also mixed integral/booleans, with result an integer. */
bd5635a1
RP
632 {
633 /* Should we promote to unsigned longest? */
634 if ((TYPE_UNSIGNED (VALUE_TYPE (arg1))
635 || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
636 && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST)
eade0c6c 637 || TYPE_LENGTH (VALUE_TYPE (arg2)) >= sizeof (unsigned LONGEST)))
bd5635a1
RP
638 {
639 unsigned LONGEST v1, v2, v;
640 v1 = (unsigned LONGEST) value_as_long (arg1);
641 v2 = (unsigned LONGEST) value_as_long (arg2);
642
643 switch (op)
644 {
645 case BINOP_ADD:
646 v = v1 + v2;
647 break;
648
649 case BINOP_SUB:
650 v = v1 - v2;
651 break;
652
653 case BINOP_MUL:
654 v = v1 * v2;
655 break;
656
657 case BINOP_DIV:
658 v = v1 / v2;
659 break;
660
661 case BINOP_REM:
662 v = v1 % v2;
663 break;
664
2fcc38b8
FF
665 case BINOP_MOD:
666 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
667 v1 mod 0 has a defined value, v1. */
2fcc38b8
FF
668 /* Chill specifies that v2 must be > 0, so check for that. */
669 if (current_language -> la_language == language_chill
670 && value_as_long (arg2) <= 0)
671 {
672 error ("Second operand of MOD must be greater than zero.");
673 }
2fcc38b8
FF
674 if (v2 == 0)
675 {
676 v = v1;
677 }
678 else
679 {
680 v = v1/v2;
681 /* Note floor(v1/v2) == v1/v2 for unsigned. */
682 v = v1 - (v2 * v);
683 }
684 break;
685
bd5635a1
RP
686 case BINOP_LSH:
687 v = v1 << v2;
688 break;
689
690 case BINOP_RSH:
691 v = v1 >> v2;
692 break;
693
e58de8a2 694 case BINOP_BITWISE_AND:
bd5635a1
RP
695 v = v1 & v2;
696 break;
697
e58de8a2 698 case BINOP_BITWISE_IOR:
bd5635a1
RP
699 v = v1 | v2;
700 break;
701
e58de8a2 702 case BINOP_BITWISE_XOR:
bd5635a1
RP
703 v = v1 ^ v2;
704 break;
705
e58de8a2 706 case BINOP_LOGICAL_AND:
bd5635a1
RP
707 v = v1 && v2;
708 break;
709
e58de8a2 710 case BINOP_LOGICAL_OR:
bd5635a1
RP
711 v = v1 || v2;
712 break;
713
714 case BINOP_MIN:
715 v = v1 < v2 ? v1 : v2;
716 break;
717
718 case BINOP_MAX:
719 v = v1 > v2 ? v1 : v2;
720 break;
721
722 default:
723 error ("Invalid binary operation on numbers.");
724 }
725
eade0c6c
JK
726 val = allocate_value (unsigned_operation_result);
727 store_unsigned_integer (VALUE_CONTENTS_RAW (val),
728 TYPE_LENGTH (VALUE_TYPE (val)),
729 v);
bd5635a1
RP
730 }
731 else
732 {
733 LONGEST v1, v2, v;
734 v1 = value_as_long (arg1);
735 v2 = value_as_long (arg2);
736
737 switch (op)
738 {
739 case BINOP_ADD:
740 v = v1 + v2;
741 break;
742
743 case BINOP_SUB:
744 v = v1 - v2;
745 break;
746
747 case BINOP_MUL:
748 v = v1 * v2;
749 break;
750
751 case BINOP_DIV:
752 v = v1 / v2;
753 break;
754
755 case BINOP_REM:
756 v = v1 % v2;
757 break;
758
2fcc38b8
FF
759 case BINOP_MOD:
760 /* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
761 X mod 0 has a defined value, X. */
2fcc38b8
FF
762 /* Chill specifies that v2 must be > 0, so check for that. */
763 if (current_language -> la_language == language_chill
764 && v2 <= 0)
765 {
766 error ("Second operand of MOD must be greater than zero.");
767 }
2fcc38b8
FF
768 if (v2 == 0)
769 {
770 v = v1;
771 }
772 else
773 {
774 v = v1/v2;
775 /* Compute floor. */
776 if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
777 {
778 v--;
779 }
780 v = v1 - (v2 * v);
781 }
782 break;
783
bd5635a1
RP
784 case BINOP_LSH:
785 v = v1 << v2;
786 break;
787
788 case BINOP_RSH:
789 v = v1 >> v2;
790 break;
791
e58de8a2 792 case BINOP_BITWISE_AND:
bd5635a1
RP
793 v = v1 & v2;
794 break;
795
e58de8a2 796 case BINOP_BITWISE_IOR:
bd5635a1
RP
797 v = v1 | v2;
798 break;
799
e58de8a2 800 case BINOP_BITWISE_XOR:
bd5635a1
RP
801 v = v1 ^ v2;
802 break;
803
e58de8a2 804 case BINOP_LOGICAL_AND:
bd5635a1
RP
805 v = v1 && v2;
806 break;
807
e58de8a2 808 case BINOP_LOGICAL_OR:
bd5635a1
RP
809 v = v1 || v2;
810 break;
811
812 case BINOP_MIN:
813 v = v1 < v2 ? v1 : v2;
814 break;
815
816 case BINOP_MAX:
817 v = v1 > v2 ? v1 : v2;
818 break;
819
820 default:
821 error ("Invalid binary operation on numbers.");
822 }
823
eade0c6c
JK
824 val = allocate_value (signed_operation_result);
825 store_signed_integer (VALUE_CONTENTS_RAW (val),
826 TYPE_LENGTH (VALUE_TYPE (val)),
827 v);
bd5635a1
RP
828 }
829 }
830
831 return val;
832}
833\f
51b57ded 834/* Simulate the C operator ! -- return 1 if ARG1 contains zero. */
bd5635a1
RP
835
836int
e58de8a2 837value_logical_not (arg1)
bd5635a1
RP
838 value arg1;
839{
840 register int len;
841 register char *p;
842
843 COERCE_ARRAY (arg1);
844
51b57ded
FF
845 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT)
846 return 0 == value_as_double (arg1);
847
bd5635a1
RP
848 len = TYPE_LENGTH (VALUE_TYPE (arg1));
849 p = VALUE_CONTENTS (arg1);
850
851 while (--len >= 0)
852 {
853 if (*p++)
854 break;
855 }
856
857 return len < 0;
858}
859
860/* Simulate the C operator == by returning a 1
861 iff ARG1 and ARG2 have equal contents. */
862
863int
864value_equal (arg1, arg2)
865 register value arg1, arg2;
866
867{
868 register int len;
869 register char *p1, *p2;
870 enum type_code code1;
871 enum type_code code2;
872
873 COERCE_ARRAY (arg1);
874 COERCE_ARRAY (arg2);
875
876 code1 = TYPE_CODE (VALUE_TYPE (arg1));
877 code2 = TYPE_CODE (VALUE_TYPE (arg2));
878
879 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
880 return value_as_long (arg1) == value_as_long (arg2);
881 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
882 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
883 return value_as_double (arg1) == value_as_double (arg2);
088c3a0b
JG
884
885 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
886 is bigger. */
887 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
888 return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
889 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
890 return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
891
bd5635a1
RP
892 else if (code1 == code2
893 && ((len = TYPE_LENGTH (VALUE_TYPE (arg1)))
894 == TYPE_LENGTH (VALUE_TYPE (arg2))))
895 {
896 p1 = VALUE_CONTENTS (arg1);
897 p2 = VALUE_CONTENTS (arg2);
898 while (--len >= 0)
899 {
900 if (*p1++ != *p2++)
901 break;
902 }
903 return len < 0;
904 }
905 else
906 {
907 error ("Invalid type combination in equality test.");
908 return 0; /* For lint -- never reached */
909 }
910}
911
912/* Simulate the C operator < by returning 1
913 iff ARG1's contents are less than ARG2's. */
914
915int
916value_less (arg1, arg2)
917 register value arg1, arg2;
918{
919 register enum type_code code1;
920 register enum type_code code2;
921
922 COERCE_ARRAY (arg1);
923 COERCE_ARRAY (arg2);
924
925 code1 = TYPE_CODE (VALUE_TYPE (arg1));
926 code2 = TYPE_CODE (VALUE_TYPE (arg2));
927
928 if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
929 {
930 if (TYPE_UNSIGNED (VALUE_TYPE (arg1))
931 || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
088c3a0b
JG
932 return ((unsigned LONGEST) value_as_long (arg1)
933 < (unsigned LONGEST) value_as_long (arg2));
bd5635a1
RP
934 else
935 return value_as_long (arg1) < value_as_long (arg2);
936 }
937 else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
938 && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
939 return value_as_double (arg1) < value_as_double (arg2);
088c3a0b
JG
940 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
941 return value_as_pointer (arg1) < value_as_pointer (arg2);
942
943 /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
944 is bigger. */
945 else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
946 return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
947 else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
948 return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
949
bd5635a1
RP
950 else
951 {
952 error ("Invalid type combination in ordering comparison.");
953 return 0;
954 }
955}
956\f
957/* The unary operators - and ~. Both free the argument ARG1. */
958
959value
960value_neg (arg1)
961 register value arg1;
962{
963 register struct type *type;
964
965 COERCE_ENUM (arg1);
966
967 type = VALUE_TYPE (arg1);
968
969 if (TYPE_CODE (type) == TYPE_CODE_FLT)
970 return value_from_double (type, - value_as_double (arg1));
971 else if (TYPE_CODE (type) == TYPE_CODE_INT)
088c3a0b 972 return value_from_longest (type, - value_as_long (arg1));
bd5635a1
RP
973 else {
974 error ("Argument to negate operation not a number.");
975 return 0; /* For lint -- never reached */
976 }
977}
978
979value
e58de8a2 980value_complement (arg1)
bd5635a1
RP
981 register value arg1;
982{
983 COERCE_ENUM (arg1);
984
985 if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
986 error ("Argument to complement operation not an integer.");
987
088c3a0b 988 return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1));
bd5635a1
RP
989}
990\f
eade0c6c
JK
991/* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
992 and whose VALUE_CONTENTS is valaddr.
993 Return -1 if out of range, -2 other error. */
994
995int
996value_bit_index (type, valaddr, index)
997 struct type *type;
998 char *valaddr;
999 int index;
1000{
1001 struct type *range;
1002 int low_bound, high_bound, bit_length;
1003 LONGEST word;
1004 range = TYPE_FIELD_TYPE (type, 0);
1005 if (TYPE_CODE (range) != TYPE_CODE_RANGE)
1006 return -2;
1007 low_bound = TYPE_LOW_BOUND (range);
1008 high_bound = TYPE_HIGH_BOUND (range);
1009 if (index < low_bound || index > high_bound)
1010 return -1;
1011 bit_length = high_bound - low_bound + 1;
1012 index -= low_bound;
1013 if (bit_length <= TARGET_CHAR_BIT)
1014 word = unpack_long (builtin_type_unsigned_char, valaddr);
1015 else if (bit_length <= TARGET_SHORT_BIT)
1016 word = unpack_long (builtin_type_unsigned_short, valaddr);
1017 else
1018 {
1019 int word_start_index = (index / TARGET_INT_BIT) * TARGET_INT_BIT;
1020 index -= word_start_index;
1021 word = unpack_long (builtin_type_unsigned_int,
1022 valaddr + (word_start_index / HOST_CHAR_BIT));
1023 }
1024#if BITS_BIG_ENDIAN
1025 if (bit_length <= TARGET_CHAR_BIT)
1026 index = TARGET_CHAR_BIT - 1 - index;
1027 else if (bit_length <= TARGET_SHORT_BIT)
1028 index = TARGET_SHORT_BIT - 1 - index;
1029 else
1030 index = TARGET_INT_BIT - 1 - index;
1031#endif
1032 return (word >> index) & 1;
1033}
1034
1035value
1036value_in (element, set)
1037 value element, set;
1038{
1039 int member;
1040 if (TYPE_CODE (VALUE_TYPE (set)) != TYPE_CODE_SET)
1041 error ("Second argument of 'IN' has wrong type");
1042 if (TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_INT
1043 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_CHAR
1044 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_ENUM
1045 && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_BOOL)
1046 error ("First argument of 'IN' has wrong type");
1047 member = value_bit_index (VALUE_TYPE (set), VALUE_CONTENTS (set),
1048 value_as_long (element));
1049 if (member < 0)
1050 error ("First argument of 'IN' not in range");
1051 return value_from_longest (builtin_type_int, member);
1052}
1053
1054void
1055_initialize_valarith ()
1056{
1057 /* Can't just call init_type because we wouldn't know what names to give
1058 them. */
1059 if (sizeof (LONGEST) > TARGET_LONG_BIT / HOST_CHAR_BIT)
1060 {
1061 unsigned_operation_result = builtin_type_unsigned_long_long;
1062 signed_operation_result = builtin_type_long_long;
1063 }
1064 else
1065 {
1066 unsigned_operation_result = builtin_type_unsigned_long;
1067 signed_operation_result = builtin_type_long;
1068 }
1069}
This page took 0.257546 seconds and 4 git commands to generate.