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