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