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