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