xref: /dflybsd-src/contrib/gdb-7/gdb/valarith.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Perform arithmetic and other operations on values, for GDB.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 1986, 1988-2005, 2007-2012 Free Software Foundation,
4*a45ae5f8SJohn Marino    Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This file is part of GDB.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert    (at your option) any later version.
125796c8dcSSimon Schubert 
135796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165796c8dcSSimon Schubert    GNU General Public License for more details.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "value.h"
235796c8dcSSimon Schubert #include "symtab.h"
245796c8dcSSimon Schubert #include "gdbtypes.h"
255796c8dcSSimon Schubert #include "expression.h"
265796c8dcSSimon Schubert #include "target.h"
275796c8dcSSimon Schubert #include "language.h"
285796c8dcSSimon Schubert #include "gdb_string.h"
295796c8dcSSimon Schubert #include "doublest.h"
305796c8dcSSimon Schubert #include "dfp.h"
315796c8dcSSimon Schubert #include <math.h>
325796c8dcSSimon Schubert #include "infcall.h"
33cf7f2e2dSJohn Marino #include "exceptions.h"
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert /* Define whether or not the C operator '/' truncates towards zero for
365796c8dcSSimon Schubert    differently signed operands (truncation direction is undefined in C).  */
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert #ifndef TRUNCATION_TOWARDS_ZERO
395796c8dcSSimon Schubert #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
405796c8dcSSimon Schubert #endif
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert void _initialize_valarith (void);
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert /* Given a pointer, return the size of its target.
465796c8dcSSimon Schubert    If the pointer type is void *, then return 1.
475796c8dcSSimon Schubert    If the target type is incomplete, then error out.
485796c8dcSSimon Schubert    This isn't a general purpose function, but just a
49c50c785cSJohn Marino    helper for value_ptradd.  */
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert static LONGEST
525796c8dcSSimon Schubert find_size_for_pointer_math (struct type *ptr_type)
535796c8dcSSimon Schubert {
545796c8dcSSimon Schubert   LONGEST sz = -1;
555796c8dcSSimon Schubert   struct type *ptr_target;
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
585796c8dcSSimon Schubert   ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert   sz = TYPE_LENGTH (ptr_target);
615796c8dcSSimon Schubert   if (sz == 0)
625796c8dcSSimon Schubert     {
635796c8dcSSimon Schubert       if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
645796c8dcSSimon Schubert 	sz = 1;
655796c8dcSSimon Schubert       else
665796c8dcSSimon Schubert 	{
675796c8dcSSimon Schubert 	  char *name;
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert 	  name = TYPE_NAME (ptr_target);
705796c8dcSSimon Schubert 	  if (name == NULL)
715796c8dcSSimon Schubert 	    name = TYPE_TAG_NAME (ptr_target);
725796c8dcSSimon Schubert 	  if (name == NULL)
735796c8dcSSimon Schubert 	    error (_("Cannot perform pointer math on incomplete types, "
745796c8dcSSimon Schubert 		   "try casting to a known type, or void *."));
755796c8dcSSimon Schubert 	  else
765796c8dcSSimon Schubert 	    error (_("Cannot perform pointer math on incomplete type \"%s\", "
775796c8dcSSimon Schubert 		   "try casting to a known type, or void *."), name);
785796c8dcSSimon Schubert 	}
795796c8dcSSimon Schubert     }
805796c8dcSSimon Schubert   return sz;
815796c8dcSSimon Schubert }
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert /* Given a pointer ARG1 and an integral value ARG2, return the
845796c8dcSSimon Schubert    result of C-style pointer arithmetic ARG1 + ARG2.  */
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert struct value *
875796c8dcSSimon Schubert value_ptradd (struct value *arg1, LONGEST arg2)
885796c8dcSSimon Schubert {
895796c8dcSSimon Schubert   struct type *valptrtype;
905796c8dcSSimon Schubert   LONGEST sz;
91c50c785cSJohn Marino   struct value *result;
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
945796c8dcSSimon Schubert   valptrtype = check_typedef (value_type (arg1));
955796c8dcSSimon Schubert   sz = find_size_for_pointer_math (valptrtype);
965796c8dcSSimon Schubert 
97c50c785cSJohn Marino   result = value_from_pointer (valptrtype,
985796c8dcSSimon Schubert 			       value_as_address (arg1) + sz * arg2);
99c50c785cSJohn Marino   if (VALUE_LVAL (result) != lval_internalvar)
100c50c785cSJohn Marino     set_value_component_location (result, arg1);
101c50c785cSJohn Marino   return result;
1025796c8dcSSimon Schubert }
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert /* Given two compatible pointer values ARG1 and ARG2, return the
1055796c8dcSSimon Schubert    result of C-style pointer arithmetic ARG1 - ARG2.  */
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert LONGEST
1085796c8dcSSimon Schubert value_ptrdiff (struct value *arg1, struct value *arg2)
1095796c8dcSSimon Schubert {
1105796c8dcSSimon Schubert   struct type *type1, *type2;
1115796c8dcSSimon Schubert   LONGEST sz;
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
1145796c8dcSSimon Schubert   arg2 = coerce_array (arg2);
1155796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
1165796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
1195796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert   if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
1225796c8dcSSimon Schubert       != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
123c50c785cSJohn Marino     error (_("First argument of `-' is a pointer and "
124c50c785cSJohn Marino 	     "second argument is neither\n"
125c50c785cSJohn Marino 	     "an integer nor a pointer of the same type."));
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert   sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
128cf7f2e2dSJohn Marino   if (sz == 0)
129cf7f2e2dSJohn Marino     {
130cf7f2e2dSJohn Marino       warning (_("Type size unknown, assuming 1. "
131cf7f2e2dSJohn Marino                "Try casting to a known type, or void *."));
132cf7f2e2dSJohn Marino       sz = 1;
133cf7f2e2dSJohn Marino     }
134cf7f2e2dSJohn Marino 
1355796c8dcSSimon Schubert   return (value_as_long (arg1) - value_as_long (arg2)) / sz;
1365796c8dcSSimon Schubert }
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert /* Return the value of ARRAY[IDX].
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert    ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING.  If the
1415796c8dcSSimon Schubert    current language supports C-style arrays, it may also be TYPE_CODE_PTR.
1425796c8dcSSimon Schubert    To access TYPE_CODE_BITSTRING values, use value_bitstring_subscript.
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert    See comments in value_coerce_array() for rationale for reason for
1455796c8dcSSimon Schubert    doing lower bounds adjustment here rather than there.
1465796c8dcSSimon Schubert    FIXME:  Perhaps we should validate that the index is valid and if
1475796c8dcSSimon Schubert    verbosity is set, warn about invalid indices (but still use them).  */
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert struct value *
1505796c8dcSSimon Schubert value_subscript (struct value *array, LONGEST index)
1515796c8dcSSimon Schubert {
1525796c8dcSSimon Schubert   int c_style = current_language->c_style_arrays;
1535796c8dcSSimon Schubert   struct type *tarray;
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert   array = coerce_ref (array);
1565796c8dcSSimon Schubert   tarray = check_typedef (value_type (array));
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert   if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
1595796c8dcSSimon Schubert       || TYPE_CODE (tarray) == TYPE_CODE_STRING)
1605796c8dcSSimon Schubert     {
1615796c8dcSSimon Schubert       struct type *range_type = TYPE_INDEX_TYPE (tarray);
1625796c8dcSSimon Schubert       LONGEST lowerbound, upperbound;
1635796c8dcSSimon Schubert 
164cf7f2e2dSJohn Marino       get_discrete_bounds (range_type, &lowerbound, &upperbound);
1655796c8dcSSimon Schubert       if (VALUE_LVAL (array) != lval_memory)
1665796c8dcSSimon Schubert 	return value_subscripted_rvalue (array, index, lowerbound);
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert       if (c_style == 0)
1695796c8dcSSimon Schubert 	{
1705796c8dcSSimon Schubert 	  if (index >= lowerbound && index <= upperbound)
1715796c8dcSSimon Schubert 	    return value_subscripted_rvalue (array, index, lowerbound);
1725796c8dcSSimon Schubert 	  /* Emit warning unless we have an array of unknown size.
1735796c8dcSSimon Schubert 	     An array of unknown size has lowerbound 0 and upperbound -1.  */
1745796c8dcSSimon Schubert 	  if (upperbound > -1)
1755796c8dcSSimon Schubert 	    warning (_("array or string index out of range"));
1765796c8dcSSimon Schubert 	  /* fall doing C stuff */
1775796c8dcSSimon Schubert 	  c_style = 1;
1785796c8dcSSimon Schubert 	}
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert       index -= lowerbound;
1815796c8dcSSimon Schubert       array = value_coerce_array (array);
1825796c8dcSSimon Schubert     }
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert   if (c_style)
1855796c8dcSSimon Schubert     return value_ind (value_ptradd (array, index));
1865796c8dcSSimon Schubert   else
1875796c8dcSSimon Schubert     error (_("not an array or string"));
1885796c8dcSSimon Schubert }
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert /* Return the value of EXPR[IDX], expr an aggregate rvalue
1915796c8dcSSimon Schubert    (eg, a vector register).  This routine used to promote floats
1925796c8dcSSimon Schubert    to doubles, but no longer does.  */
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert struct value *
1955796c8dcSSimon Schubert value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
1965796c8dcSSimon Schubert {
1975796c8dcSSimon Schubert   struct type *array_type = check_typedef (value_type (array));
1985796c8dcSSimon Schubert   struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
1995796c8dcSSimon Schubert   unsigned int elt_size = TYPE_LENGTH (elt_type);
2005796c8dcSSimon Schubert   unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
2015796c8dcSSimon Schubert   struct value *v;
2025796c8dcSSimon Schubert 
203cf7f2e2dSJohn Marino   if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
204cf7f2e2dSJohn Marino 			     && elt_offs >= TYPE_LENGTH (array_type)))
2055796c8dcSSimon Schubert     error (_("no such vector element"));
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
208c50c785cSJohn Marino     v = allocate_value_lazy (elt_type);
2095796c8dcSSimon Schubert   else
210c50c785cSJohn Marino     {
211c50c785cSJohn Marino       v = allocate_value (elt_type);
212c50c785cSJohn Marino       value_contents_copy (v, value_embedded_offset (v),
213c50c785cSJohn Marino 			   array, value_embedded_offset (array) + elt_offs,
214c50c785cSJohn Marino 			   elt_size);
215c50c785cSJohn Marino     }
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert   set_value_component_location (v, array);
2185796c8dcSSimon Schubert   VALUE_REGNUM (v) = VALUE_REGNUM (array);
2195796c8dcSSimon Schubert   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
2205796c8dcSSimon Schubert   set_value_offset (v, value_offset (array) + elt_offs);
2215796c8dcSSimon Schubert   return v;
2225796c8dcSSimon Schubert }
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert /* Return the value of BITSTRING[IDX] as (boolean) type TYPE.  */
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert struct value *
2275796c8dcSSimon Schubert value_bitstring_subscript (struct type *type,
2285796c8dcSSimon Schubert 			   struct value *bitstring, LONGEST index)
2295796c8dcSSimon Schubert {
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert   struct type *bitstring_type, *range_type;
2325796c8dcSSimon Schubert   struct value *v;
2335796c8dcSSimon Schubert   int offset, byte, bit_index;
2345796c8dcSSimon Schubert   LONGEST lowerbound, upperbound;
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert   bitstring_type = check_typedef (value_type (bitstring));
2375796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (bitstring_type) == TYPE_CODE_BITSTRING);
2385796c8dcSSimon Schubert 
2395796c8dcSSimon Schubert   range_type = TYPE_INDEX_TYPE (bitstring_type);
2405796c8dcSSimon Schubert   get_discrete_bounds (range_type, &lowerbound, &upperbound);
2415796c8dcSSimon Schubert   if (index < lowerbound || index > upperbound)
2425796c8dcSSimon Schubert     error (_("bitstring index out of range"));
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert   index -= lowerbound;
2455796c8dcSSimon Schubert   offset = index / TARGET_CHAR_BIT;
2465796c8dcSSimon Schubert   byte = *((char *) value_contents (bitstring) + offset);
2475796c8dcSSimon Schubert 
2485796c8dcSSimon Schubert   bit_index = index % TARGET_CHAR_BIT;
2495796c8dcSSimon Schubert   byte >>= (gdbarch_bits_big_endian (get_type_arch (bitstring_type)) ?
2505796c8dcSSimon Schubert 	    TARGET_CHAR_BIT - 1 - bit_index : bit_index);
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   v = value_from_longest (type, byte & 1);
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert   set_value_bitpos (v, bit_index);
2555796c8dcSSimon Schubert   set_value_bitsize (v, 1);
2565796c8dcSSimon Schubert   set_value_component_location (v, bitstring);
2575796c8dcSSimon Schubert   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (bitstring);
2585796c8dcSSimon Schubert 
2595796c8dcSSimon Schubert   set_value_offset (v, offset + value_offset (bitstring));
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert   return v;
2625796c8dcSSimon Schubert }
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert /* Check to see if either argument is a structure, or a reference to
2665796c8dcSSimon Schubert    one.  This is called so we know whether to go ahead with the normal
2675796c8dcSSimon Schubert    binop or look for a user defined function instead.
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert    For now, we do not overload the `=' operator.  */
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert int
272cf7f2e2dSJohn Marino binop_types_user_defined_p (enum exp_opcode op,
273cf7f2e2dSJohn Marino 			    struct type *type1, struct type *type2)
2745796c8dcSSimon Schubert {
2755796c8dcSSimon Schubert   if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
2765796c8dcSSimon Schubert     return 0;
2775796c8dcSSimon Schubert 
278cf7f2e2dSJohn Marino   type1 = check_typedef (type1);
2795796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_REF)
2805796c8dcSSimon Schubert     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
2815796c8dcSSimon Schubert 
282cf7f2e2dSJohn Marino   type2 = check_typedef (type1);
2835796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_REF)
2845796c8dcSSimon Schubert     type2 = check_typedef (TYPE_TARGET_TYPE (type2));
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert   return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
2875796c8dcSSimon Schubert 	  || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
2885796c8dcSSimon Schubert }
2895796c8dcSSimon Schubert 
290cf7f2e2dSJohn Marino /* Check to see if either argument is a structure, or a reference to
291cf7f2e2dSJohn Marino    one.  This is called so we know whether to go ahead with the normal
292cf7f2e2dSJohn Marino    binop or look for a user defined function instead.
293cf7f2e2dSJohn Marino 
294cf7f2e2dSJohn Marino    For now, we do not overload the `=' operator.  */
295cf7f2e2dSJohn Marino 
296cf7f2e2dSJohn Marino int
297cf7f2e2dSJohn Marino binop_user_defined_p (enum exp_opcode op,
298cf7f2e2dSJohn Marino 		      struct value *arg1, struct value *arg2)
299cf7f2e2dSJohn Marino {
300cf7f2e2dSJohn Marino   return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
301cf7f2e2dSJohn Marino }
302cf7f2e2dSJohn Marino 
3035796c8dcSSimon Schubert /* Check to see if argument is a structure.  This is called so
3045796c8dcSSimon Schubert    we know whether to go ahead with the normal unop or look for a
3055796c8dcSSimon Schubert    user defined function instead.
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert    For now, we do not overload the `&' operator.  */
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert int
3105796c8dcSSimon Schubert unop_user_defined_p (enum exp_opcode op, struct value *arg1)
3115796c8dcSSimon Schubert {
3125796c8dcSSimon Schubert   struct type *type1;
313cf7f2e2dSJohn Marino 
3145796c8dcSSimon Schubert   if (op == UNOP_ADDR)
3155796c8dcSSimon Schubert     return 0;
3165796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
317c50c785cSJohn Marino   if (TYPE_CODE (type1) == TYPE_CODE_REF)
318c50c785cSJohn Marino     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
319c50c785cSJohn Marino   return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
3205796c8dcSSimon Schubert }
3215796c8dcSSimon Schubert 
322cf7f2e2dSJohn Marino /* Try to find an operator named OPERATOR which takes NARGS arguments
323cf7f2e2dSJohn Marino    specified in ARGS.  If the operator found is a static member operator
324cf7f2e2dSJohn Marino    *STATIC_MEMFUNP will be set to 1, and otherwise 0.
325cf7f2e2dSJohn Marino    The search if performed through find_overload_match which will handle
326cf7f2e2dSJohn Marino    member operators, non member operators, operators imported implicitly or
327cf7f2e2dSJohn Marino    explicitly, and perform correct overload resolution in all of the above
328cf7f2e2dSJohn Marino    situations or combinations thereof.  */
329cf7f2e2dSJohn Marino 
330cf7f2e2dSJohn Marino static struct value *
331cf7f2e2dSJohn Marino value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
332cf7f2e2dSJohn Marino                            int *static_memfuncp)
333cf7f2e2dSJohn Marino {
334cf7f2e2dSJohn Marino 
335cf7f2e2dSJohn Marino   struct symbol *symp = NULL;
336cf7f2e2dSJohn Marino   struct value *valp = NULL;
337cf7f2e2dSJohn Marino 
338*a45ae5f8SJohn Marino   find_overload_match (args, nargs, operator, BOTH /* could be method */,
339cf7f2e2dSJohn Marino                        0 /* strict match */, &args[0], /* objp */
340cf7f2e2dSJohn Marino                        NULL /* pass NULL symbol since symbol is unknown */,
341cf7f2e2dSJohn Marino                        &valp, &symp, static_memfuncp, 0);
342cf7f2e2dSJohn Marino 
343cf7f2e2dSJohn Marino   if (valp)
344cf7f2e2dSJohn Marino     return valp;
345cf7f2e2dSJohn Marino 
346cf7f2e2dSJohn Marino   if (symp)
347cf7f2e2dSJohn Marino     {
348cf7f2e2dSJohn Marino       /* This is a non member function and does not
349cf7f2e2dSJohn Marino          expect a reference as its first argument
350cf7f2e2dSJohn Marino          rather the explicit structure.  */
351cf7f2e2dSJohn Marino       args[0] = value_ind (args[0]);
352cf7f2e2dSJohn Marino       return value_of_variable (symp, 0);
353cf7f2e2dSJohn Marino     }
354cf7f2e2dSJohn Marino 
355cf7f2e2dSJohn Marino   error (_("Could not find %s."), operator);
356cf7f2e2dSJohn Marino }
357cf7f2e2dSJohn Marino 
358cf7f2e2dSJohn Marino /* Lookup user defined operator NAME.  Return a value representing the
359cf7f2e2dSJohn Marino    function, otherwise return NULL.  */
360cf7f2e2dSJohn Marino 
361cf7f2e2dSJohn Marino static struct value *
362cf7f2e2dSJohn Marino value_user_defined_op (struct value **argp, struct value **args, char *name,
363cf7f2e2dSJohn Marino                        int *static_memfuncp, int nargs)
364cf7f2e2dSJohn Marino {
365cf7f2e2dSJohn Marino   struct value *result = NULL;
366cf7f2e2dSJohn Marino 
367cf7f2e2dSJohn Marino   if (current_language->la_language == language_cplus)
368cf7f2e2dSJohn Marino     result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp);
369cf7f2e2dSJohn Marino   else
370cf7f2e2dSJohn Marino     result = value_struct_elt (argp, args, name, static_memfuncp,
371cf7f2e2dSJohn Marino                                "structure");
372cf7f2e2dSJohn Marino 
373cf7f2e2dSJohn Marino   return result;
374cf7f2e2dSJohn Marino }
375cf7f2e2dSJohn Marino 
3765796c8dcSSimon Schubert /* We know either arg1 or arg2 is a structure, so try to find the right
3775796c8dcSSimon Schubert    user defined function.  Create an argument vector that calls
3785796c8dcSSimon Schubert    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
3795796c8dcSSimon Schubert    binary operator which is legal for GNU C++).
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
3825796c8dcSSimon Schubert    is the opcode saying how to modify it.  Otherwise, OTHEROP is
3835796c8dcSSimon Schubert    unused.  */
3845796c8dcSSimon Schubert 
3855796c8dcSSimon Schubert struct value *
3865796c8dcSSimon Schubert value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
3875796c8dcSSimon Schubert 	       enum exp_opcode otherop, enum noside noside)
3885796c8dcSSimon Schubert {
3895796c8dcSSimon Schubert   struct value **argvec;
3905796c8dcSSimon Schubert   char *ptr;
3915796c8dcSSimon Schubert   char tstr[13];
3925796c8dcSSimon Schubert   int static_memfuncp;
3935796c8dcSSimon Schubert 
3945796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
3955796c8dcSSimon Schubert   arg2 = coerce_ref (arg2);
3965796c8dcSSimon Schubert 
3975796c8dcSSimon Schubert   /* now we know that what we have to do is construct our
3985796c8dcSSimon Schubert      arg vector and find the right function to call it with.  */
3995796c8dcSSimon Schubert 
4005796c8dcSSimon Schubert   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
4015796c8dcSSimon Schubert     error (_("Can't do that binary op on that type"));	/* FIXME be explicit */
4025796c8dcSSimon Schubert 
4035796c8dcSSimon Schubert   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
4045796c8dcSSimon Schubert   argvec[1] = value_addr (arg1);
4055796c8dcSSimon Schubert   argvec[2] = arg2;
4065796c8dcSSimon Schubert   argvec[3] = 0;
4075796c8dcSSimon Schubert 
408c50c785cSJohn Marino   /* Make the right function name up.  */
4095796c8dcSSimon Schubert   strcpy (tstr, "operator__");
4105796c8dcSSimon Schubert   ptr = tstr + 8;
4115796c8dcSSimon Schubert   switch (op)
4125796c8dcSSimon Schubert     {
4135796c8dcSSimon Schubert     case BINOP_ADD:
4145796c8dcSSimon Schubert       strcpy (ptr, "+");
4155796c8dcSSimon Schubert       break;
4165796c8dcSSimon Schubert     case BINOP_SUB:
4175796c8dcSSimon Schubert       strcpy (ptr, "-");
4185796c8dcSSimon Schubert       break;
4195796c8dcSSimon Schubert     case BINOP_MUL:
4205796c8dcSSimon Schubert       strcpy (ptr, "*");
4215796c8dcSSimon Schubert       break;
4225796c8dcSSimon Schubert     case BINOP_DIV:
4235796c8dcSSimon Schubert       strcpy (ptr, "/");
4245796c8dcSSimon Schubert       break;
4255796c8dcSSimon Schubert     case BINOP_REM:
4265796c8dcSSimon Schubert       strcpy (ptr, "%");
4275796c8dcSSimon Schubert       break;
4285796c8dcSSimon Schubert     case BINOP_LSH:
4295796c8dcSSimon Schubert       strcpy (ptr, "<<");
4305796c8dcSSimon Schubert       break;
4315796c8dcSSimon Schubert     case BINOP_RSH:
4325796c8dcSSimon Schubert       strcpy (ptr, ">>");
4335796c8dcSSimon Schubert       break;
4345796c8dcSSimon Schubert     case BINOP_BITWISE_AND:
4355796c8dcSSimon Schubert       strcpy (ptr, "&");
4365796c8dcSSimon Schubert       break;
4375796c8dcSSimon Schubert     case BINOP_BITWISE_IOR:
4385796c8dcSSimon Schubert       strcpy (ptr, "|");
4395796c8dcSSimon Schubert       break;
4405796c8dcSSimon Schubert     case BINOP_BITWISE_XOR:
4415796c8dcSSimon Schubert       strcpy (ptr, "^");
4425796c8dcSSimon Schubert       break;
4435796c8dcSSimon Schubert     case BINOP_LOGICAL_AND:
4445796c8dcSSimon Schubert       strcpy (ptr, "&&");
4455796c8dcSSimon Schubert       break;
4465796c8dcSSimon Schubert     case BINOP_LOGICAL_OR:
4475796c8dcSSimon Schubert       strcpy (ptr, "||");
4485796c8dcSSimon Schubert       break;
4495796c8dcSSimon Schubert     case BINOP_MIN:
4505796c8dcSSimon Schubert       strcpy (ptr, "<?");
4515796c8dcSSimon Schubert       break;
4525796c8dcSSimon Schubert     case BINOP_MAX:
4535796c8dcSSimon Schubert       strcpy (ptr, ">?");
4545796c8dcSSimon Schubert       break;
4555796c8dcSSimon Schubert     case BINOP_ASSIGN:
4565796c8dcSSimon Schubert       strcpy (ptr, "=");
4575796c8dcSSimon Schubert       break;
4585796c8dcSSimon Schubert     case BINOP_ASSIGN_MODIFY:
4595796c8dcSSimon Schubert       switch (otherop)
4605796c8dcSSimon Schubert 	{
4615796c8dcSSimon Schubert 	case BINOP_ADD:
4625796c8dcSSimon Schubert 	  strcpy (ptr, "+=");
4635796c8dcSSimon Schubert 	  break;
4645796c8dcSSimon Schubert 	case BINOP_SUB:
4655796c8dcSSimon Schubert 	  strcpy (ptr, "-=");
4665796c8dcSSimon Schubert 	  break;
4675796c8dcSSimon Schubert 	case BINOP_MUL:
4685796c8dcSSimon Schubert 	  strcpy (ptr, "*=");
4695796c8dcSSimon Schubert 	  break;
4705796c8dcSSimon Schubert 	case BINOP_DIV:
4715796c8dcSSimon Schubert 	  strcpy (ptr, "/=");
4725796c8dcSSimon Schubert 	  break;
4735796c8dcSSimon Schubert 	case BINOP_REM:
4745796c8dcSSimon Schubert 	  strcpy (ptr, "%=");
4755796c8dcSSimon Schubert 	  break;
4765796c8dcSSimon Schubert 	case BINOP_BITWISE_AND:
4775796c8dcSSimon Schubert 	  strcpy (ptr, "&=");
4785796c8dcSSimon Schubert 	  break;
4795796c8dcSSimon Schubert 	case BINOP_BITWISE_IOR:
4805796c8dcSSimon Schubert 	  strcpy (ptr, "|=");
4815796c8dcSSimon Schubert 	  break;
4825796c8dcSSimon Schubert 	case BINOP_BITWISE_XOR:
4835796c8dcSSimon Schubert 	  strcpy (ptr, "^=");
4845796c8dcSSimon Schubert 	  break;
4855796c8dcSSimon Schubert 	case BINOP_MOD:	/* invalid */
4865796c8dcSSimon Schubert 	default:
4875796c8dcSSimon Schubert 	  error (_("Invalid binary operation specified."));
4885796c8dcSSimon Schubert 	}
4895796c8dcSSimon Schubert       break;
4905796c8dcSSimon Schubert     case BINOP_SUBSCRIPT:
4915796c8dcSSimon Schubert       strcpy (ptr, "[]");
4925796c8dcSSimon Schubert       break;
4935796c8dcSSimon Schubert     case BINOP_EQUAL:
4945796c8dcSSimon Schubert       strcpy (ptr, "==");
4955796c8dcSSimon Schubert       break;
4965796c8dcSSimon Schubert     case BINOP_NOTEQUAL:
4975796c8dcSSimon Schubert       strcpy (ptr, "!=");
4985796c8dcSSimon Schubert       break;
4995796c8dcSSimon Schubert     case BINOP_LESS:
5005796c8dcSSimon Schubert       strcpy (ptr, "<");
5015796c8dcSSimon Schubert       break;
5025796c8dcSSimon Schubert     case BINOP_GTR:
5035796c8dcSSimon Schubert       strcpy (ptr, ">");
5045796c8dcSSimon Schubert       break;
5055796c8dcSSimon Schubert     case BINOP_GEQ:
5065796c8dcSSimon Schubert       strcpy (ptr, ">=");
5075796c8dcSSimon Schubert       break;
5085796c8dcSSimon Schubert     case BINOP_LEQ:
5095796c8dcSSimon Schubert       strcpy (ptr, "<=");
5105796c8dcSSimon Schubert       break;
5115796c8dcSSimon Schubert     case BINOP_MOD:		/* invalid */
5125796c8dcSSimon Schubert     default:
5135796c8dcSSimon Schubert       error (_("Invalid binary operation specified."));
5145796c8dcSSimon Schubert     }
5155796c8dcSSimon Schubert 
516cf7f2e2dSJohn Marino   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
517cf7f2e2dSJohn Marino                                      &static_memfuncp, 2);
5185796c8dcSSimon Schubert 
5195796c8dcSSimon Schubert   if (argvec[0])
5205796c8dcSSimon Schubert     {
5215796c8dcSSimon Schubert       if (static_memfuncp)
5225796c8dcSSimon Schubert 	{
5235796c8dcSSimon Schubert 	  argvec[1] = argvec[0];
5245796c8dcSSimon Schubert 	  argvec++;
5255796c8dcSSimon Schubert 	}
5265796c8dcSSimon Schubert       if (noside == EVAL_AVOID_SIDE_EFFECTS)
5275796c8dcSSimon Schubert 	{
5285796c8dcSSimon Schubert 	  struct type *return_type;
529cf7f2e2dSJohn Marino 
5305796c8dcSSimon Schubert 	  return_type
5315796c8dcSSimon Schubert 	    = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
5325796c8dcSSimon Schubert 	  return value_zero (return_type, VALUE_LVAL (arg1));
5335796c8dcSSimon Schubert 	}
534c50c785cSJohn Marino       return call_function_by_hand (argvec[0], 2 - static_memfuncp,
535c50c785cSJohn Marino 				    argvec + 1);
5365796c8dcSSimon Schubert     }
537c50c785cSJohn Marino   throw_error (NOT_FOUND_ERROR,
538c50c785cSJohn Marino                _("member function %s not found"), tstr);
5395796c8dcSSimon Schubert #ifdef lint
5405796c8dcSSimon Schubert   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
5415796c8dcSSimon Schubert #endif
5425796c8dcSSimon Schubert }
5435796c8dcSSimon Schubert 
5445796c8dcSSimon Schubert /* We know that arg1 is a structure, so try to find a unary user
5455796c8dcSSimon Schubert    defined operator that matches the operator in question.
5465796c8dcSSimon Schubert    Create an argument vector that calls arg1.operator @ (arg1)
5475796c8dcSSimon Schubert    and return that value (where '@' is (almost) any unary operator which
5485796c8dcSSimon Schubert    is legal for GNU C++).  */
5495796c8dcSSimon Schubert 
5505796c8dcSSimon Schubert struct value *
5515796c8dcSSimon Schubert value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
5525796c8dcSSimon Schubert {
5535796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
5545796c8dcSSimon Schubert   struct value **argvec;
5555796c8dcSSimon Schubert   char *ptr, *mangle_ptr;
5565796c8dcSSimon Schubert   char tstr[13], mangle_tstr[13];
5575796c8dcSSimon Schubert   int static_memfuncp, nargs;
5585796c8dcSSimon Schubert 
5595796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
5605796c8dcSSimon Schubert 
5615796c8dcSSimon Schubert   /* now we know that what we have to do is construct our
5625796c8dcSSimon Schubert      arg vector and find the right function to call it with.  */
5635796c8dcSSimon Schubert 
5645796c8dcSSimon Schubert   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
5655796c8dcSSimon Schubert     error (_("Can't do that unary op on that type"));	/* FIXME be explicit */
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
5685796c8dcSSimon Schubert   argvec[1] = value_addr (arg1);
5695796c8dcSSimon Schubert   argvec[2] = 0;
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert   nargs = 1;
5725796c8dcSSimon Schubert 
573c50c785cSJohn Marino   /* Make the right function name up.  */
5745796c8dcSSimon Schubert   strcpy (tstr, "operator__");
5755796c8dcSSimon Schubert   ptr = tstr + 8;
5765796c8dcSSimon Schubert   strcpy (mangle_tstr, "__");
5775796c8dcSSimon Schubert   mangle_ptr = mangle_tstr + 2;
5785796c8dcSSimon Schubert   switch (op)
5795796c8dcSSimon Schubert     {
5805796c8dcSSimon Schubert     case UNOP_PREINCREMENT:
5815796c8dcSSimon Schubert       strcpy (ptr, "++");
5825796c8dcSSimon Schubert       break;
5835796c8dcSSimon Schubert     case UNOP_PREDECREMENT:
5845796c8dcSSimon Schubert       strcpy (ptr, "--");
5855796c8dcSSimon Schubert       break;
5865796c8dcSSimon Schubert     case UNOP_POSTINCREMENT:
5875796c8dcSSimon Schubert       strcpy (ptr, "++");
5885796c8dcSSimon Schubert       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
5895796c8dcSSimon Schubert       argvec[3] = 0;
5905796c8dcSSimon Schubert       nargs ++;
5915796c8dcSSimon Schubert       break;
5925796c8dcSSimon Schubert     case UNOP_POSTDECREMENT:
5935796c8dcSSimon Schubert       strcpy (ptr, "--");
5945796c8dcSSimon Schubert       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
5955796c8dcSSimon Schubert       argvec[3] = 0;
5965796c8dcSSimon Schubert       nargs ++;
5975796c8dcSSimon Schubert       break;
5985796c8dcSSimon Schubert     case UNOP_LOGICAL_NOT:
5995796c8dcSSimon Schubert       strcpy (ptr, "!");
6005796c8dcSSimon Schubert       break;
6015796c8dcSSimon Schubert     case UNOP_COMPLEMENT:
6025796c8dcSSimon Schubert       strcpy (ptr, "~");
6035796c8dcSSimon Schubert       break;
6045796c8dcSSimon Schubert     case UNOP_NEG:
6055796c8dcSSimon Schubert       strcpy (ptr, "-");
6065796c8dcSSimon Schubert       break;
6075796c8dcSSimon Schubert     case UNOP_PLUS:
6085796c8dcSSimon Schubert       strcpy (ptr, "+");
6095796c8dcSSimon Schubert       break;
6105796c8dcSSimon Schubert     case UNOP_IND:
6115796c8dcSSimon Schubert       strcpy (ptr, "*");
6125796c8dcSSimon Schubert       break;
613c50c785cSJohn Marino     case STRUCTOP_PTR:
614c50c785cSJohn Marino       strcpy (ptr, "->");
615c50c785cSJohn Marino       break;
6165796c8dcSSimon Schubert     default:
6175796c8dcSSimon Schubert       error (_("Invalid unary operation specified."));
6185796c8dcSSimon Schubert     }
6195796c8dcSSimon Schubert 
620cf7f2e2dSJohn Marino   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
621cf7f2e2dSJohn Marino                                      &static_memfuncp, nargs);
6225796c8dcSSimon Schubert 
6235796c8dcSSimon Schubert   if (argvec[0])
6245796c8dcSSimon Schubert     {
6255796c8dcSSimon Schubert       if (static_memfuncp)
6265796c8dcSSimon Schubert 	{
6275796c8dcSSimon Schubert 	  argvec[1] = argvec[0];
6285796c8dcSSimon Schubert 	  nargs --;
6295796c8dcSSimon Schubert 	  argvec++;
6305796c8dcSSimon Schubert 	}
6315796c8dcSSimon Schubert       if (noside == EVAL_AVOID_SIDE_EFFECTS)
6325796c8dcSSimon Schubert 	{
6335796c8dcSSimon Schubert 	  struct type *return_type;
634cf7f2e2dSJohn Marino 
6355796c8dcSSimon Schubert 	  return_type
6365796c8dcSSimon Schubert 	    = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
6375796c8dcSSimon Schubert 	  return value_zero (return_type, VALUE_LVAL (arg1));
6385796c8dcSSimon Schubert 	}
6395796c8dcSSimon Schubert       return call_function_by_hand (argvec[0], nargs, argvec + 1);
6405796c8dcSSimon Schubert     }
641c50c785cSJohn Marino   throw_error (NOT_FOUND_ERROR,
642c50c785cSJohn Marino                _("member function %s not found"), tstr);
643c50c785cSJohn Marino 
6445796c8dcSSimon Schubert   return 0;			/* For lint -- never reached */
6455796c8dcSSimon Schubert }
6465796c8dcSSimon Schubert 
6475796c8dcSSimon Schubert 
6485796c8dcSSimon Schubert /* Concatenate two values with the following conditions:
6495796c8dcSSimon Schubert 
6505796c8dcSSimon Schubert    (1)  Both values must be either bitstring values or character string
6515796c8dcSSimon Schubert    values and the resulting value consists of the concatenation of
6525796c8dcSSimon Schubert    ARG1 followed by ARG2.
6535796c8dcSSimon Schubert 
6545796c8dcSSimon Schubert    or
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert    One value must be an integer value and the other value must be
6575796c8dcSSimon Schubert    either a bitstring value or character string value, which is
6585796c8dcSSimon Schubert    to be repeated by the number of times specified by the integer
6595796c8dcSSimon Schubert    value.
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert 
6625796c8dcSSimon Schubert    (2)  Boolean values are also allowed and are treated as bit string
6635796c8dcSSimon Schubert    values of length 1.
6645796c8dcSSimon Schubert 
6655796c8dcSSimon Schubert    (3)  Character values are also allowed and are treated as character
666c50c785cSJohn Marino    string values of length 1.  */
6675796c8dcSSimon Schubert 
6685796c8dcSSimon Schubert struct value *
6695796c8dcSSimon Schubert value_concat (struct value *arg1, struct value *arg2)
6705796c8dcSSimon Schubert {
6715796c8dcSSimon Schubert   struct value *inval1;
6725796c8dcSSimon Schubert   struct value *inval2;
6735796c8dcSSimon Schubert   struct value *outval = NULL;
6745796c8dcSSimon Schubert   int inval1len, inval2len;
6755796c8dcSSimon Schubert   int count, idx;
6765796c8dcSSimon Schubert   char *ptr;
6775796c8dcSSimon Schubert   char inchar;
6785796c8dcSSimon Schubert   struct type *type1 = check_typedef (value_type (arg1));
6795796c8dcSSimon Schubert   struct type *type2 = check_typedef (value_type (arg2));
6805796c8dcSSimon Schubert   struct type *char_type;
6815796c8dcSSimon Schubert 
6825796c8dcSSimon Schubert   /* First figure out if we are dealing with two values to be concatenated
6835796c8dcSSimon Schubert      or a repeat count and a value to be repeated.  INVAL1 is set to the
6845796c8dcSSimon Schubert      first of two concatenated values, or the repeat count.  INVAL2 is set
6855796c8dcSSimon Schubert      to the second of the two concatenated values or the value to be
6865796c8dcSSimon Schubert      repeated.  */
6875796c8dcSSimon Schubert 
6885796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_INT)
6895796c8dcSSimon Schubert     {
6905796c8dcSSimon Schubert       struct type *tmp = type1;
691cf7f2e2dSJohn Marino 
6925796c8dcSSimon Schubert       type1 = tmp;
6935796c8dcSSimon Schubert       tmp = type2;
6945796c8dcSSimon Schubert       inval1 = arg2;
6955796c8dcSSimon Schubert       inval2 = arg1;
6965796c8dcSSimon Schubert     }
6975796c8dcSSimon Schubert   else
6985796c8dcSSimon Schubert     {
6995796c8dcSSimon Schubert       inval1 = arg1;
7005796c8dcSSimon Schubert       inval2 = arg2;
7015796c8dcSSimon Schubert     }
7025796c8dcSSimon Schubert 
7035796c8dcSSimon Schubert   /* Now process the input values.  */
7045796c8dcSSimon Schubert 
7055796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_INT)
7065796c8dcSSimon Schubert     {
7075796c8dcSSimon Schubert       /* We have a repeat count.  Validate the second value and then
7085796c8dcSSimon Schubert          construct a value repeated that many times.  */
7095796c8dcSSimon Schubert       if (TYPE_CODE (type2) == TYPE_CODE_STRING
7105796c8dcSSimon Schubert 	  || TYPE_CODE (type2) == TYPE_CODE_CHAR)
7115796c8dcSSimon Schubert 	{
7125796c8dcSSimon Schubert 	  count = longest_to_int (value_as_long (inval1));
7135796c8dcSSimon Schubert 	  inval2len = TYPE_LENGTH (type2);
7145796c8dcSSimon Schubert 	  ptr = (char *) alloca (count * inval2len);
7155796c8dcSSimon Schubert 	  if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
7165796c8dcSSimon Schubert 	    {
7175796c8dcSSimon Schubert 	      char_type = type2;
718cf7f2e2dSJohn Marino 
7195796c8dcSSimon Schubert 	      inchar = (char) unpack_long (type2,
7205796c8dcSSimon Schubert 					   value_contents (inval2));
7215796c8dcSSimon Schubert 	      for (idx = 0; idx < count; idx++)
7225796c8dcSSimon Schubert 		{
7235796c8dcSSimon Schubert 		  *(ptr + idx) = inchar;
7245796c8dcSSimon Schubert 		}
7255796c8dcSSimon Schubert 	    }
7265796c8dcSSimon Schubert 	  else
7275796c8dcSSimon Schubert 	    {
7285796c8dcSSimon Schubert 	      char_type = TYPE_TARGET_TYPE (type2);
729cf7f2e2dSJohn Marino 
7305796c8dcSSimon Schubert 	      for (idx = 0; idx < count; idx++)
7315796c8dcSSimon Schubert 		{
7325796c8dcSSimon Schubert 		  memcpy (ptr + (idx * inval2len), value_contents (inval2),
7335796c8dcSSimon Schubert 			  inval2len);
7345796c8dcSSimon Schubert 		}
7355796c8dcSSimon Schubert 	    }
7365796c8dcSSimon Schubert 	  outval = value_string (ptr, count * inval2len, char_type);
7375796c8dcSSimon Schubert 	}
7385796c8dcSSimon Schubert       else if (TYPE_CODE (type2) == TYPE_CODE_BITSTRING
7395796c8dcSSimon Schubert 	       || TYPE_CODE (type2) == TYPE_CODE_BOOL)
7405796c8dcSSimon Schubert 	{
7415796c8dcSSimon Schubert 	  error (_("unimplemented support for bitstring/boolean repeats"));
7425796c8dcSSimon Schubert 	}
7435796c8dcSSimon Schubert       else
7445796c8dcSSimon Schubert 	{
7455796c8dcSSimon Schubert 	  error (_("can't repeat values of that type"));
7465796c8dcSSimon Schubert 	}
7475796c8dcSSimon Schubert     }
7485796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
7495796c8dcSSimon Schubert 	   || TYPE_CODE (type1) == TYPE_CODE_CHAR)
7505796c8dcSSimon Schubert     {
7515796c8dcSSimon Schubert       /* We have two character strings to concatenate.  */
7525796c8dcSSimon Schubert       if (TYPE_CODE (type2) != TYPE_CODE_STRING
7535796c8dcSSimon Schubert 	  && TYPE_CODE (type2) != TYPE_CODE_CHAR)
7545796c8dcSSimon Schubert 	{
7555796c8dcSSimon Schubert 	  error (_("Strings can only be concatenated with other strings."));
7565796c8dcSSimon Schubert 	}
7575796c8dcSSimon Schubert       inval1len = TYPE_LENGTH (type1);
7585796c8dcSSimon Schubert       inval2len = TYPE_LENGTH (type2);
7595796c8dcSSimon Schubert       ptr = (char *) alloca (inval1len + inval2len);
7605796c8dcSSimon Schubert       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
7615796c8dcSSimon Schubert 	{
7625796c8dcSSimon Schubert 	  char_type = type1;
763cf7f2e2dSJohn Marino 
7645796c8dcSSimon Schubert 	  *ptr = (char) unpack_long (type1, value_contents (inval1));
7655796c8dcSSimon Schubert 	}
7665796c8dcSSimon Schubert       else
7675796c8dcSSimon Schubert 	{
7685796c8dcSSimon Schubert 	  char_type = TYPE_TARGET_TYPE (type1);
769cf7f2e2dSJohn Marino 
7705796c8dcSSimon Schubert 	  memcpy (ptr, value_contents (inval1), inval1len);
7715796c8dcSSimon Schubert 	}
7725796c8dcSSimon Schubert       if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
7735796c8dcSSimon Schubert 	{
7745796c8dcSSimon Schubert 	  *(ptr + inval1len) =
7755796c8dcSSimon Schubert 	    (char) unpack_long (type2, value_contents (inval2));
7765796c8dcSSimon Schubert 	}
7775796c8dcSSimon Schubert       else
7785796c8dcSSimon Schubert 	{
7795796c8dcSSimon Schubert 	  memcpy (ptr + inval1len, value_contents (inval2), inval2len);
7805796c8dcSSimon Schubert 	}
7815796c8dcSSimon Schubert       outval = value_string (ptr, inval1len + inval2len, char_type);
7825796c8dcSSimon Schubert     }
7835796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_BITSTRING
7845796c8dcSSimon Schubert 	   || TYPE_CODE (type1) == TYPE_CODE_BOOL)
7855796c8dcSSimon Schubert     {
7865796c8dcSSimon Schubert       /* We have two bitstrings to concatenate.  */
7875796c8dcSSimon Schubert       if (TYPE_CODE (type2) != TYPE_CODE_BITSTRING
7885796c8dcSSimon Schubert 	  && TYPE_CODE (type2) != TYPE_CODE_BOOL)
7895796c8dcSSimon Schubert 	{
790c50c785cSJohn Marino 	  error (_("Bitstrings or booleans can only be concatenated "
791c50c785cSJohn Marino 		   "with other bitstrings or booleans."));
7925796c8dcSSimon Schubert 	}
7935796c8dcSSimon Schubert       error (_("unimplemented support for bitstring/boolean concatenation."));
7945796c8dcSSimon Schubert     }
7955796c8dcSSimon Schubert   else
7965796c8dcSSimon Schubert     {
7975796c8dcSSimon Schubert       /* We don't know how to concatenate these operands.  */
7985796c8dcSSimon Schubert       error (_("illegal operands for concatenation."));
7995796c8dcSSimon Schubert     }
8005796c8dcSSimon Schubert   return (outval);
8015796c8dcSSimon Schubert }
8025796c8dcSSimon Schubert 
8035796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are
8045796c8dcSSimon Schubert    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
805c50c785cSJohn Marino 
8065796c8dcSSimon Schubert static LONGEST
8075796c8dcSSimon Schubert integer_pow (LONGEST v1, LONGEST v2)
8085796c8dcSSimon Schubert {
8095796c8dcSSimon Schubert   if (v2 < 0)
8105796c8dcSSimon Schubert     {
8115796c8dcSSimon Schubert       if (v1 == 0)
8125796c8dcSSimon Schubert 	error (_("Attempt to raise 0 to negative power."));
8135796c8dcSSimon Schubert       else
8145796c8dcSSimon Schubert 	return 0;
8155796c8dcSSimon Schubert     }
8165796c8dcSSimon Schubert   else
8175796c8dcSSimon Schubert     {
818c50c785cSJohn Marino       /* The Russian Peasant's Algorithm.  */
8195796c8dcSSimon Schubert       LONGEST v;
8205796c8dcSSimon Schubert 
8215796c8dcSSimon Schubert       v = 1;
8225796c8dcSSimon Schubert       for (;;)
8235796c8dcSSimon Schubert 	{
8245796c8dcSSimon Schubert 	  if (v2 & 1L)
8255796c8dcSSimon Schubert 	    v *= v1;
8265796c8dcSSimon Schubert 	  v2 >>= 1;
8275796c8dcSSimon Schubert 	  if (v2 == 0)
8285796c8dcSSimon Schubert 	    return v;
8295796c8dcSSimon Schubert 	  v1 *= v1;
8305796c8dcSSimon Schubert 	}
8315796c8dcSSimon Schubert     }
8325796c8dcSSimon Schubert }
8335796c8dcSSimon Schubert 
8345796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are
8355796c8dcSSimon Schubert    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
836c50c785cSJohn Marino 
8375796c8dcSSimon Schubert static ULONGEST
8385796c8dcSSimon Schubert uinteger_pow (ULONGEST v1, LONGEST v2)
8395796c8dcSSimon Schubert {
8405796c8dcSSimon Schubert   if (v2 < 0)
8415796c8dcSSimon Schubert     {
8425796c8dcSSimon Schubert       if (v1 == 0)
8435796c8dcSSimon Schubert 	error (_("Attempt to raise 0 to negative power."));
8445796c8dcSSimon Schubert       else
8455796c8dcSSimon Schubert 	return 0;
8465796c8dcSSimon Schubert     }
8475796c8dcSSimon Schubert   else
8485796c8dcSSimon Schubert     {
849c50c785cSJohn Marino       /* The Russian Peasant's Algorithm.  */
8505796c8dcSSimon Schubert       ULONGEST v;
8515796c8dcSSimon Schubert 
8525796c8dcSSimon Schubert       v = 1;
8535796c8dcSSimon Schubert       for (;;)
8545796c8dcSSimon Schubert 	{
8555796c8dcSSimon Schubert 	  if (v2 & 1L)
8565796c8dcSSimon Schubert 	    v *= v1;
8575796c8dcSSimon Schubert 	  v2 >>= 1;
8585796c8dcSSimon Schubert 	  if (v2 == 0)
8595796c8dcSSimon Schubert 	    return v;
8605796c8dcSSimon Schubert 	  v1 *= v1;
8615796c8dcSSimon Schubert 	}
8625796c8dcSSimon Schubert     }
8635796c8dcSSimon Schubert }
8645796c8dcSSimon Schubert 
8655796c8dcSSimon Schubert /* Obtain decimal value of arguments for binary operation, converting from
8665796c8dcSSimon Schubert    other types if one of them is not decimal floating point.  */
8675796c8dcSSimon Schubert static void
8685796c8dcSSimon Schubert value_args_as_decimal (struct value *arg1, struct value *arg2,
8695796c8dcSSimon Schubert 		       gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
8705796c8dcSSimon Schubert 		       gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
8715796c8dcSSimon Schubert {
8725796c8dcSSimon Schubert   struct type *type1, *type2;
8735796c8dcSSimon Schubert 
8745796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
8755796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
8765796c8dcSSimon Schubert 
8775796c8dcSSimon Schubert   /* At least one of the arguments must be of decimal float type.  */
8785796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
8795796c8dcSSimon Schubert 	      || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
8805796c8dcSSimon Schubert 
8815796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_FLT
8825796c8dcSSimon Schubert       || TYPE_CODE (type2) == TYPE_CODE_FLT)
8835796c8dcSSimon Schubert     /* The DFP extension to the C language does not allow mixing of
8845796c8dcSSimon Schubert      * decimal float types with other float types in expressions
8855796c8dcSSimon Schubert      * (see WDTR 24732, page 12).  */
886c50c785cSJohn Marino     error (_("Mixing decimal floating types with "
887c50c785cSJohn Marino 	     "other floating types is not allowed."));
8885796c8dcSSimon Schubert 
8895796c8dcSSimon Schubert   /* Obtain decimal value of arg1, converting from other types
8905796c8dcSSimon Schubert      if necessary.  */
8915796c8dcSSimon Schubert 
8925796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
8935796c8dcSSimon Schubert     {
8945796c8dcSSimon Schubert       *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
8955796c8dcSSimon Schubert       *len_x = TYPE_LENGTH (type1);
8965796c8dcSSimon Schubert       memcpy (x, value_contents (arg1), *len_x);
8975796c8dcSSimon Schubert     }
8985796c8dcSSimon Schubert   else if (is_integral_type (type1))
8995796c8dcSSimon Schubert     {
9005796c8dcSSimon Schubert       *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
9015796c8dcSSimon Schubert       *len_x = TYPE_LENGTH (type2);
9025796c8dcSSimon Schubert       decimal_from_integral (arg1, x, *len_x, *byte_order_x);
9035796c8dcSSimon Schubert     }
9045796c8dcSSimon Schubert   else
9055796c8dcSSimon Schubert     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
9065796c8dcSSimon Schubert 	     TYPE_NAME (type2));
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert   /* Obtain decimal value of arg2, converting from other types
9095796c8dcSSimon Schubert      if necessary.  */
9105796c8dcSSimon Schubert 
9115796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
9125796c8dcSSimon Schubert     {
9135796c8dcSSimon Schubert       *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
9145796c8dcSSimon Schubert       *len_y = TYPE_LENGTH (type2);
9155796c8dcSSimon Schubert       memcpy (y, value_contents (arg2), *len_y);
9165796c8dcSSimon Schubert     }
9175796c8dcSSimon Schubert   else if (is_integral_type (type2))
9185796c8dcSSimon Schubert     {
9195796c8dcSSimon Schubert       *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
9205796c8dcSSimon Schubert       *len_y = TYPE_LENGTH (type1);
9215796c8dcSSimon Schubert       decimal_from_integral (arg2, y, *len_y, *byte_order_y);
9225796c8dcSSimon Schubert     }
9235796c8dcSSimon Schubert   else
9245796c8dcSSimon Schubert     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
9255796c8dcSSimon Schubert 	     TYPE_NAME (type2));
9265796c8dcSSimon Schubert }
9275796c8dcSSimon Schubert 
9285796c8dcSSimon Schubert /* Perform a binary operation on two operands which have reasonable
9295796c8dcSSimon Schubert    representations as integers or floats.  This includes booleans,
9305796c8dcSSimon Schubert    characters, integers, or floats.
9315796c8dcSSimon Schubert    Does not support addition and subtraction on pointers;
9325796c8dcSSimon Schubert    use value_ptradd, value_ptrsub or value_ptrdiff for those operations.  */
9335796c8dcSSimon Schubert 
934c50c785cSJohn Marino static struct value *
935c50c785cSJohn Marino scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
9365796c8dcSSimon Schubert {
9375796c8dcSSimon Schubert   struct value *val;
9385796c8dcSSimon Schubert   struct type *type1, *type2, *result_type;
9395796c8dcSSimon Schubert 
9405796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
9415796c8dcSSimon Schubert   arg2 = coerce_ref (arg2);
9425796c8dcSSimon Schubert 
9435796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
9445796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
9455796c8dcSSimon Schubert 
9465796c8dcSSimon Schubert   if ((TYPE_CODE (type1) != TYPE_CODE_FLT
9475796c8dcSSimon Schubert        && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
9485796c8dcSSimon Schubert        && !is_integral_type (type1))
9495796c8dcSSimon Schubert       || (TYPE_CODE (type2) != TYPE_CODE_FLT
9505796c8dcSSimon Schubert 	  && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
9515796c8dcSSimon Schubert 	  && !is_integral_type (type2)))
9525796c8dcSSimon Schubert     error (_("Argument to arithmetic operation not a number or boolean."));
9535796c8dcSSimon Schubert 
9545796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
9555796c8dcSSimon Schubert       || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
9565796c8dcSSimon Schubert     {
9575796c8dcSSimon Schubert       int len_v1, len_v2, len_v;
9585796c8dcSSimon Schubert       enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
9595796c8dcSSimon Schubert       gdb_byte v1[16], v2[16];
9605796c8dcSSimon Schubert       gdb_byte v[16];
9615796c8dcSSimon Schubert 
9625796c8dcSSimon Schubert       /* If only one type is decimal float, use its type.
9635796c8dcSSimon Schubert 	 Otherwise use the bigger type.  */
9645796c8dcSSimon Schubert       if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
9655796c8dcSSimon Schubert 	result_type = type2;
9665796c8dcSSimon Schubert       else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
9675796c8dcSSimon Schubert 	result_type = type1;
9685796c8dcSSimon Schubert       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
9695796c8dcSSimon Schubert 	result_type = type2;
9705796c8dcSSimon Schubert       else
9715796c8dcSSimon Schubert 	result_type = type1;
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert       len_v = TYPE_LENGTH (result_type);
9745796c8dcSSimon Schubert       byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
9775796c8dcSSimon Schubert 					 v2, &len_v2, &byte_order_v2);
9785796c8dcSSimon Schubert 
9795796c8dcSSimon Schubert       switch (op)
9805796c8dcSSimon Schubert 	{
9815796c8dcSSimon Schubert 	case BINOP_ADD:
9825796c8dcSSimon Schubert 	case BINOP_SUB:
9835796c8dcSSimon Schubert 	case BINOP_MUL:
9845796c8dcSSimon Schubert 	case BINOP_DIV:
9855796c8dcSSimon Schubert 	case BINOP_EXP:
9865796c8dcSSimon Schubert 	  decimal_binop (op, v1, len_v1, byte_order_v1,
9875796c8dcSSimon Schubert 			     v2, len_v2, byte_order_v2,
9885796c8dcSSimon Schubert 			     v, len_v, byte_order_v);
9895796c8dcSSimon Schubert 	  break;
9905796c8dcSSimon Schubert 
9915796c8dcSSimon Schubert 	default:
9925796c8dcSSimon Schubert 	  error (_("Operation not valid for decimal floating point number."));
9935796c8dcSSimon Schubert 	}
9945796c8dcSSimon Schubert 
9955796c8dcSSimon Schubert       val = value_from_decfloat (result_type, v);
9965796c8dcSSimon Schubert     }
9975796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_FLT
9985796c8dcSSimon Schubert 	   || TYPE_CODE (type2) == TYPE_CODE_FLT)
9995796c8dcSSimon Schubert     {
10005796c8dcSSimon Schubert       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
10015796c8dcSSimon Schubert          in target format.  real.c in GCC probably has the necessary
10025796c8dcSSimon Schubert          code.  */
10035796c8dcSSimon Schubert       DOUBLEST v1, v2, v = 0;
1004cf7f2e2dSJohn Marino 
10055796c8dcSSimon Schubert       v1 = value_as_double (arg1);
10065796c8dcSSimon Schubert       v2 = value_as_double (arg2);
10075796c8dcSSimon Schubert 
10085796c8dcSSimon Schubert       switch (op)
10095796c8dcSSimon Schubert 	{
10105796c8dcSSimon Schubert 	case BINOP_ADD:
10115796c8dcSSimon Schubert 	  v = v1 + v2;
10125796c8dcSSimon Schubert 	  break;
10135796c8dcSSimon Schubert 
10145796c8dcSSimon Schubert 	case BINOP_SUB:
10155796c8dcSSimon Schubert 	  v = v1 - v2;
10165796c8dcSSimon Schubert 	  break;
10175796c8dcSSimon Schubert 
10185796c8dcSSimon Schubert 	case BINOP_MUL:
10195796c8dcSSimon Schubert 	  v = v1 * v2;
10205796c8dcSSimon Schubert 	  break;
10215796c8dcSSimon Schubert 
10225796c8dcSSimon Schubert 	case BINOP_DIV:
10235796c8dcSSimon Schubert 	  v = v1 / v2;
10245796c8dcSSimon Schubert 	  break;
10255796c8dcSSimon Schubert 
10265796c8dcSSimon Schubert 	case BINOP_EXP:
10275796c8dcSSimon Schubert 	  errno = 0;
10285796c8dcSSimon Schubert 	  v = pow (v1, v2);
10295796c8dcSSimon Schubert 	  if (errno)
1030c50c785cSJohn Marino 	    error (_("Cannot perform exponentiation: %s"),
1031c50c785cSJohn Marino 		   safe_strerror (errno));
10325796c8dcSSimon Schubert 	  break;
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert 	case BINOP_MIN:
10355796c8dcSSimon Schubert 	  v = v1 < v2 ? v1 : v2;
10365796c8dcSSimon Schubert 	  break;
10375796c8dcSSimon Schubert 
10385796c8dcSSimon Schubert 	case BINOP_MAX:
10395796c8dcSSimon Schubert 	  v = v1 > v2 ? v1 : v2;
10405796c8dcSSimon Schubert 	  break;
10415796c8dcSSimon Schubert 
10425796c8dcSSimon Schubert 	default:
10435796c8dcSSimon Schubert 	  error (_("Integer-only operation on floating point number."));
10445796c8dcSSimon Schubert 	}
10455796c8dcSSimon Schubert 
10465796c8dcSSimon Schubert       /* If only one type is float, use its type.
10475796c8dcSSimon Schubert 	 Otherwise use the bigger type.  */
10485796c8dcSSimon Schubert       if (TYPE_CODE (type1) != TYPE_CODE_FLT)
10495796c8dcSSimon Schubert 	result_type = type2;
10505796c8dcSSimon Schubert       else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
10515796c8dcSSimon Schubert 	result_type = type1;
10525796c8dcSSimon Schubert       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
10535796c8dcSSimon Schubert 	result_type = type2;
10545796c8dcSSimon Schubert       else
10555796c8dcSSimon Schubert 	result_type = type1;
10565796c8dcSSimon Schubert 
10575796c8dcSSimon Schubert       val = allocate_value (result_type);
10585796c8dcSSimon Schubert       store_typed_floating (value_contents_raw (val), value_type (val), v);
10595796c8dcSSimon Schubert     }
10605796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
10615796c8dcSSimon Schubert 	   || TYPE_CODE (type2) == TYPE_CODE_BOOL)
10625796c8dcSSimon Schubert     {
10635796c8dcSSimon Schubert       LONGEST v1, v2, v = 0;
1064cf7f2e2dSJohn Marino 
10655796c8dcSSimon Schubert       v1 = value_as_long (arg1);
10665796c8dcSSimon Schubert       v2 = value_as_long (arg2);
10675796c8dcSSimon Schubert 
10685796c8dcSSimon Schubert       switch (op)
10695796c8dcSSimon Schubert 	{
10705796c8dcSSimon Schubert 	case BINOP_BITWISE_AND:
10715796c8dcSSimon Schubert 	  v = v1 & v2;
10725796c8dcSSimon Schubert 	  break;
10735796c8dcSSimon Schubert 
10745796c8dcSSimon Schubert 	case BINOP_BITWISE_IOR:
10755796c8dcSSimon Schubert 	  v = v1 | v2;
10765796c8dcSSimon Schubert 	  break;
10775796c8dcSSimon Schubert 
10785796c8dcSSimon Schubert 	case BINOP_BITWISE_XOR:
10795796c8dcSSimon Schubert 	  v = v1 ^ v2;
10805796c8dcSSimon Schubert           break;
10815796c8dcSSimon Schubert 
10825796c8dcSSimon Schubert         case BINOP_EQUAL:
10835796c8dcSSimon Schubert           v = v1 == v2;
10845796c8dcSSimon Schubert           break;
10855796c8dcSSimon Schubert 
10865796c8dcSSimon Schubert         case BINOP_NOTEQUAL:
10875796c8dcSSimon Schubert           v = v1 != v2;
10885796c8dcSSimon Schubert 	  break;
10895796c8dcSSimon Schubert 
10905796c8dcSSimon Schubert 	default:
10915796c8dcSSimon Schubert 	  error (_("Invalid operation on booleans."));
10925796c8dcSSimon Schubert 	}
10935796c8dcSSimon Schubert 
10945796c8dcSSimon Schubert       result_type = type1;
10955796c8dcSSimon Schubert 
10965796c8dcSSimon Schubert       val = allocate_value (result_type);
10975796c8dcSSimon Schubert       store_signed_integer (value_contents_raw (val),
10985796c8dcSSimon Schubert 			    TYPE_LENGTH (result_type),
10995796c8dcSSimon Schubert 			    gdbarch_byte_order (get_type_arch (result_type)),
11005796c8dcSSimon Schubert 			    v);
11015796c8dcSSimon Schubert     }
11025796c8dcSSimon Schubert   else
11035796c8dcSSimon Schubert     /* Integral operations here.  */
11045796c8dcSSimon Schubert     {
11055796c8dcSSimon Schubert       /* Determine type length of the result, and if the operation should
11065796c8dcSSimon Schubert 	 be done unsigned.  For exponentiation and shift operators,
11075796c8dcSSimon Schubert 	 use the length and type of the left operand.  Otherwise,
11085796c8dcSSimon Schubert 	 use the signedness of the operand with the greater length.
11095796c8dcSSimon Schubert 	 If both operands are of equal length, use unsigned operation
11105796c8dcSSimon Schubert 	 if one of the operands is unsigned.  */
11115796c8dcSSimon Schubert       if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
11125796c8dcSSimon Schubert 	result_type = type1;
11135796c8dcSSimon Schubert       else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
11145796c8dcSSimon Schubert 	result_type = type1;
11155796c8dcSSimon Schubert       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
11165796c8dcSSimon Schubert 	result_type = type2;
11175796c8dcSSimon Schubert       else if (TYPE_UNSIGNED (type1))
11185796c8dcSSimon Schubert 	result_type = type1;
11195796c8dcSSimon Schubert       else if (TYPE_UNSIGNED (type2))
11205796c8dcSSimon Schubert 	result_type = type2;
11215796c8dcSSimon Schubert       else
11225796c8dcSSimon Schubert 	result_type = type1;
11235796c8dcSSimon Schubert 
11245796c8dcSSimon Schubert       if (TYPE_UNSIGNED (result_type))
11255796c8dcSSimon Schubert 	{
11265796c8dcSSimon Schubert 	  LONGEST v2_signed = value_as_long (arg2);
11275796c8dcSSimon Schubert 	  ULONGEST v1, v2, v = 0;
1128cf7f2e2dSJohn Marino 
11295796c8dcSSimon Schubert 	  v1 = (ULONGEST) value_as_long (arg1);
11305796c8dcSSimon Schubert 	  v2 = (ULONGEST) v2_signed;
11315796c8dcSSimon Schubert 
11325796c8dcSSimon Schubert 	  switch (op)
11335796c8dcSSimon Schubert 	    {
11345796c8dcSSimon Schubert 	    case BINOP_ADD:
11355796c8dcSSimon Schubert 	      v = v1 + v2;
11365796c8dcSSimon Schubert 	      break;
11375796c8dcSSimon Schubert 
11385796c8dcSSimon Schubert 	    case BINOP_SUB:
11395796c8dcSSimon Schubert 	      v = v1 - v2;
11405796c8dcSSimon Schubert 	      break;
11415796c8dcSSimon Schubert 
11425796c8dcSSimon Schubert 	    case BINOP_MUL:
11435796c8dcSSimon Schubert 	      v = v1 * v2;
11445796c8dcSSimon Schubert 	      break;
11455796c8dcSSimon Schubert 
11465796c8dcSSimon Schubert 	    case BINOP_DIV:
11475796c8dcSSimon Schubert 	    case BINOP_INTDIV:
11485796c8dcSSimon Schubert 	      if (v2 != 0)
11495796c8dcSSimon Schubert 		v = v1 / v2;
11505796c8dcSSimon Schubert 	      else
11515796c8dcSSimon Schubert 		error (_("Division by zero"));
11525796c8dcSSimon Schubert 	      break;
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert 	    case BINOP_EXP:
11555796c8dcSSimon Schubert               v = uinteger_pow (v1, v2_signed);
11565796c8dcSSimon Schubert 	      break;
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert 	    case BINOP_REM:
11595796c8dcSSimon Schubert 	      if (v2 != 0)
11605796c8dcSSimon Schubert 		v = v1 % v2;
11615796c8dcSSimon Schubert 	      else
11625796c8dcSSimon Schubert 		error (_("Division by zero"));
11635796c8dcSSimon Schubert 	      break;
11645796c8dcSSimon Schubert 
11655796c8dcSSimon Schubert 	    case BINOP_MOD:
11665796c8dcSSimon Schubert 	      /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
11675796c8dcSSimon Schubert 	         v1 mod 0 has a defined value, v1.  */
11685796c8dcSSimon Schubert 	      if (v2 == 0)
11695796c8dcSSimon Schubert 		{
11705796c8dcSSimon Schubert 		  v = v1;
11715796c8dcSSimon Schubert 		}
11725796c8dcSSimon Schubert 	      else
11735796c8dcSSimon Schubert 		{
11745796c8dcSSimon Schubert 		  v = v1 / v2;
11755796c8dcSSimon Schubert 		  /* Note floor(v1/v2) == v1/v2 for unsigned.  */
11765796c8dcSSimon Schubert 		  v = v1 - (v2 * v);
11775796c8dcSSimon Schubert 		}
11785796c8dcSSimon Schubert 	      break;
11795796c8dcSSimon Schubert 
11805796c8dcSSimon Schubert 	    case BINOP_LSH:
11815796c8dcSSimon Schubert 	      v = v1 << v2;
11825796c8dcSSimon Schubert 	      break;
11835796c8dcSSimon Schubert 
11845796c8dcSSimon Schubert 	    case BINOP_RSH:
11855796c8dcSSimon Schubert 	      v = v1 >> v2;
11865796c8dcSSimon Schubert 	      break;
11875796c8dcSSimon Schubert 
11885796c8dcSSimon Schubert 	    case BINOP_BITWISE_AND:
11895796c8dcSSimon Schubert 	      v = v1 & v2;
11905796c8dcSSimon Schubert 	      break;
11915796c8dcSSimon Schubert 
11925796c8dcSSimon Schubert 	    case BINOP_BITWISE_IOR:
11935796c8dcSSimon Schubert 	      v = v1 | v2;
11945796c8dcSSimon Schubert 	      break;
11955796c8dcSSimon Schubert 
11965796c8dcSSimon Schubert 	    case BINOP_BITWISE_XOR:
11975796c8dcSSimon Schubert 	      v = v1 ^ v2;
11985796c8dcSSimon Schubert 	      break;
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert 	    case BINOP_LOGICAL_AND:
12015796c8dcSSimon Schubert 	      v = v1 && v2;
12025796c8dcSSimon Schubert 	      break;
12035796c8dcSSimon Schubert 
12045796c8dcSSimon Schubert 	    case BINOP_LOGICAL_OR:
12055796c8dcSSimon Schubert 	      v = v1 || v2;
12065796c8dcSSimon Schubert 	      break;
12075796c8dcSSimon Schubert 
12085796c8dcSSimon Schubert 	    case BINOP_MIN:
12095796c8dcSSimon Schubert 	      v = v1 < v2 ? v1 : v2;
12105796c8dcSSimon Schubert 	      break;
12115796c8dcSSimon Schubert 
12125796c8dcSSimon Schubert 	    case BINOP_MAX:
12135796c8dcSSimon Schubert 	      v = v1 > v2 ? v1 : v2;
12145796c8dcSSimon Schubert 	      break;
12155796c8dcSSimon Schubert 
12165796c8dcSSimon Schubert 	    case BINOP_EQUAL:
12175796c8dcSSimon Schubert 	      v = v1 == v2;
12185796c8dcSSimon Schubert 	      break;
12195796c8dcSSimon Schubert 
12205796c8dcSSimon Schubert             case BINOP_NOTEQUAL:
12215796c8dcSSimon Schubert               v = v1 != v2;
12225796c8dcSSimon Schubert               break;
12235796c8dcSSimon Schubert 
12245796c8dcSSimon Schubert 	    case BINOP_LESS:
12255796c8dcSSimon Schubert 	      v = v1 < v2;
12265796c8dcSSimon Schubert 	      break;
12275796c8dcSSimon Schubert 
1228cf7f2e2dSJohn Marino 	    case BINOP_GTR:
1229cf7f2e2dSJohn Marino 	      v = v1 > v2;
1230cf7f2e2dSJohn Marino 	      break;
1231cf7f2e2dSJohn Marino 
1232cf7f2e2dSJohn Marino 	    case BINOP_LEQ:
1233cf7f2e2dSJohn Marino 	      v = v1 <= v2;
1234cf7f2e2dSJohn Marino 	      break;
1235cf7f2e2dSJohn Marino 
1236cf7f2e2dSJohn Marino 	    case BINOP_GEQ:
1237cf7f2e2dSJohn Marino 	      v = v1 >= v2;
1238cf7f2e2dSJohn Marino 	      break;
1239cf7f2e2dSJohn Marino 
12405796c8dcSSimon Schubert 	    default:
12415796c8dcSSimon Schubert 	      error (_("Invalid binary operation on numbers."));
12425796c8dcSSimon Schubert 	    }
12435796c8dcSSimon Schubert 
12445796c8dcSSimon Schubert 	  val = allocate_value (result_type);
12455796c8dcSSimon Schubert 	  store_unsigned_integer (value_contents_raw (val),
12465796c8dcSSimon Schubert 				  TYPE_LENGTH (value_type (val)),
12475796c8dcSSimon Schubert 				  gdbarch_byte_order
12485796c8dcSSimon Schubert 				    (get_type_arch (result_type)),
12495796c8dcSSimon Schubert 				  v);
12505796c8dcSSimon Schubert 	}
12515796c8dcSSimon Schubert       else
12525796c8dcSSimon Schubert 	{
12535796c8dcSSimon Schubert 	  LONGEST v1, v2, v = 0;
1254cf7f2e2dSJohn Marino 
12555796c8dcSSimon Schubert 	  v1 = value_as_long (arg1);
12565796c8dcSSimon Schubert 	  v2 = value_as_long (arg2);
12575796c8dcSSimon Schubert 
12585796c8dcSSimon Schubert 	  switch (op)
12595796c8dcSSimon Schubert 	    {
12605796c8dcSSimon Schubert 	    case BINOP_ADD:
12615796c8dcSSimon Schubert 	      v = v1 + v2;
12625796c8dcSSimon Schubert 	      break;
12635796c8dcSSimon Schubert 
12645796c8dcSSimon Schubert 	    case BINOP_SUB:
12655796c8dcSSimon Schubert 	      v = v1 - v2;
12665796c8dcSSimon Schubert 	      break;
12675796c8dcSSimon Schubert 
12685796c8dcSSimon Schubert 	    case BINOP_MUL:
12695796c8dcSSimon Schubert 	      v = v1 * v2;
12705796c8dcSSimon Schubert 	      break;
12715796c8dcSSimon Schubert 
12725796c8dcSSimon Schubert 	    case BINOP_DIV:
12735796c8dcSSimon Schubert 	    case BINOP_INTDIV:
12745796c8dcSSimon Schubert 	      if (v2 != 0)
12755796c8dcSSimon Schubert 		v = v1 / v2;
12765796c8dcSSimon Schubert 	      else
12775796c8dcSSimon Schubert 		error (_("Division by zero"));
12785796c8dcSSimon Schubert               break;
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert 	    case BINOP_EXP:
12815796c8dcSSimon Schubert               v = integer_pow (v1, v2);
12825796c8dcSSimon Schubert 	      break;
12835796c8dcSSimon Schubert 
12845796c8dcSSimon Schubert 	    case BINOP_REM:
12855796c8dcSSimon Schubert 	      if (v2 != 0)
12865796c8dcSSimon Schubert 		v = v1 % v2;
12875796c8dcSSimon Schubert 	      else
12885796c8dcSSimon Schubert 		error (_("Division by zero"));
12895796c8dcSSimon Schubert 	      break;
12905796c8dcSSimon Schubert 
12915796c8dcSSimon Schubert 	    case BINOP_MOD:
12925796c8dcSSimon Schubert 	      /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
12935796c8dcSSimon Schubert 	         X mod 0 has a defined value, X.  */
12945796c8dcSSimon Schubert 	      if (v2 == 0)
12955796c8dcSSimon Schubert 		{
12965796c8dcSSimon Schubert 		  v = v1;
12975796c8dcSSimon Schubert 		}
12985796c8dcSSimon Schubert 	      else
12995796c8dcSSimon Schubert 		{
13005796c8dcSSimon Schubert 		  v = v1 / v2;
13015796c8dcSSimon Schubert 		  /* Compute floor.  */
13025796c8dcSSimon Schubert 		  if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
13035796c8dcSSimon Schubert 		    {
13045796c8dcSSimon Schubert 		      v--;
13055796c8dcSSimon Schubert 		    }
13065796c8dcSSimon Schubert 		  v = v1 - (v2 * v);
13075796c8dcSSimon Schubert 		}
13085796c8dcSSimon Schubert 	      break;
13095796c8dcSSimon Schubert 
13105796c8dcSSimon Schubert 	    case BINOP_LSH:
13115796c8dcSSimon Schubert 	      v = v1 << v2;
13125796c8dcSSimon Schubert 	      break;
13135796c8dcSSimon Schubert 
13145796c8dcSSimon Schubert 	    case BINOP_RSH:
13155796c8dcSSimon Schubert 	      v = v1 >> v2;
13165796c8dcSSimon Schubert 	      break;
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert 	    case BINOP_BITWISE_AND:
13195796c8dcSSimon Schubert 	      v = v1 & v2;
13205796c8dcSSimon Schubert 	      break;
13215796c8dcSSimon Schubert 
13225796c8dcSSimon Schubert 	    case BINOP_BITWISE_IOR:
13235796c8dcSSimon Schubert 	      v = v1 | v2;
13245796c8dcSSimon Schubert 	      break;
13255796c8dcSSimon Schubert 
13265796c8dcSSimon Schubert 	    case BINOP_BITWISE_XOR:
13275796c8dcSSimon Schubert 	      v = v1 ^ v2;
13285796c8dcSSimon Schubert 	      break;
13295796c8dcSSimon Schubert 
13305796c8dcSSimon Schubert 	    case BINOP_LOGICAL_AND:
13315796c8dcSSimon Schubert 	      v = v1 && v2;
13325796c8dcSSimon Schubert 	      break;
13335796c8dcSSimon Schubert 
13345796c8dcSSimon Schubert 	    case BINOP_LOGICAL_OR:
13355796c8dcSSimon Schubert 	      v = v1 || v2;
13365796c8dcSSimon Schubert 	      break;
13375796c8dcSSimon Schubert 
13385796c8dcSSimon Schubert 	    case BINOP_MIN:
13395796c8dcSSimon Schubert 	      v = v1 < v2 ? v1 : v2;
13405796c8dcSSimon Schubert 	      break;
13415796c8dcSSimon Schubert 
13425796c8dcSSimon Schubert 	    case BINOP_MAX:
13435796c8dcSSimon Schubert 	      v = v1 > v2 ? v1 : v2;
13445796c8dcSSimon Schubert 	      break;
13455796c8dcSSimon Schubert 
13465796c8dcSSimon Schubert 	    case BINOP_EQUAL:
13475796c8dcSSimon Schubert 	      v = v1 == v2;
13485796c8dcSSimon Schubert 	      break;
13495796c8dcSSimon Schubert 
1350cf7f2e2dSJohn Marino             case BINOP_NOTEQUAL:
1351cf7f2e2dSJohn Marino               v = v1 != v2;
1352cf7f2e2dSJohn Marino               break;
1353cf7f2e2dSJohn Marino 
13545796c8dcSSimon Schubert 	    case BINOP_LESS:
13555796c8dcSSimon Schubert 	      v = v1 < v2;
13565796c8dcSSimon Schubert 	      break;
13575796c8dcSSimon Schubert 
1358cf7f2e2dSJohn Marino 	    case BINOP_GTR:
1359cf7f2e2dSJohn Marino 	      v = v1 > v2;
1360cf7f2e2dSJohn Marino 	      break;
1361cf7f2e2dSJohn Marino 
1362cf7f2e2dSJohn Marino 	    case BINOP_LEQ:
1363cf7f2e2dSJohn Marino 	      v = v1 <= v2;
1364cf7f2e2dSJohn Marino 	      break;
1365cf7f2e2dSJohn Marino 
1366cf7f2e2dSJohn Marino 	    case BINOP_GEQ:
1367cf7f2e2dSJohn Marino 	      v = v1 >= v2;
1368cf7f2e2dSJohn Marino 	      break;
1369cf7f2e2dSJohn Marino 
13705796c8dcSSimon Schubert 	    default:
13715796c8dcSSimon Schubert 	      error (_("Invalid binary operation on numbers."));
13725796c8dcSSimon Schubert 	    }
13735796c8dcSSimon Schubert 
13745796c8dcSSimon Schubert 	  val = allocate_value (result_type);
13755796c8dcSSimon Schubert 	  store_signed_integer (value_contents_raw (val),
13765796c8dcSSimon Schubert 				TYPE_LENGTH (value_type (val)),
13775796c8dcSSimon Schubert 				gdbarch_byte_order
13785796c8dcSSimon Schubert 				  (get_type_arch (result_type)),
13795796c8dcSSimon Schubert 				v);
13805796c8dcSSimon Schubert 	}
13815796c8dcSSimon Schubert     }
13825796c8dcSSimon Schubert 
13835796c8dcSSimon Schubert   return val;
13845796c8dcSSimon Schubert }
1385c50c785cSJohn Marino 
1386c50c785cSJohn Marino /* Performs a binary operation on two vector operands by calling scalar_binop
1387c50c785cSJohn Marino    for each pair of vector components.  */
1388c50c785cSJohn Marino 
1389c50c785cSJohn Marino static struct value *
1390c50c785cSJohn Marino vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1391c50c785cSJohn Marino {
1392c50c785cSJohn Marino   struct value *val, *tmp, *mark;
1393c50c785cSJohn Marino   struct type *type1, *type2, *eltype1, *eltype2, *result_type;
1394c50c785cSJohn Marino   int t1_is_vec, t2_is_vec, elsize, i;
1395c50c785cSJohn Marino   LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1396c50c785cSJohn Marino 
1397c50c785cSJohn Marino   type1 = check_typedef (value_type (val1));
1398c50c785cSJohn Marino   type2 = check_typedef (value_type (val2));
1399c50c785cSJohn Marino 
1400c50c785cSJohn Marino   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1401c50c785cSJohn Marino 	       && TYPE_VECTOR (type1)) ? 1 : 0;
1402c50c785cSJohn Marino   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1403c50c785cSJohn Marino 	       && TYPE_VECTOR (type2)) ? 1 : 0;
1404c50c785cSJohn Marino 
1405c50c785cSJohn Marino   if (!t1_is_vec || !t2_is_vec)
1406c50c785cSJohn Marino     error (_("Vector operations are only supported among vectors"));
1407c50c785cSJohn Marino 
1408c50c785cSJohn Marino   if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1409c50c785cSJohn Marino       || !get_array_bounds (type2, &low_bound2, &high_bound2))
1410c50c785cSJohn Marino     error (_("Could not determine the vector bounds"));
1411c50c785cSJohn Marino 
1412c50c785cSJohn Marino   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1413c50c785cSJohn Marino   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1414c50c785cSJohn Marino   elsize = TYPE_LENGTH (eltype1);
1415c50c785cSJohn Marino 
1416c50c785cSJohn Marino   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1417c50c785cSJohn Marino       || elsize != TYPE_LENGTH (eltype2)
1418c50c785cSJohn Marino       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1419c50c785cSJohn Marino       || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1420c50c785cSJohn Marino     error (_("Cannot perform operation on vectors with different types"));
1421c50c785cSJohn Marino 
1422c50c785cSJohn Marino   val = allocate_value (type1);
1423c50c785cSJohn Marino   mark = value_mark ();
1424c50c785cSJohn Marino   for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1425c50c785cSJohn Marino     {
1426c50c785cSJohn Marino       tmp = value_binop (value_subscript (val1, i),
1427c50c785cSJohn Marino 			 value_subscript (val2, i), op);
1428c50c785cSJohn Marino       memcpy (value_contents_writeable (val) + i * elsize,
1429c50c785cSJohn Marino 	      value_contents_all (tmp),
1430c50c785cSJohn Marino 	      elsize);
1431c50c785cSJohn Marino      }
1432c50c785cSJohn Marino   value_free_to_mark (mark);
1433c50c785cSJohn Marino 
1434c50c785cSJohn Marino   return val;
1435c50c785cSJohn Marino }
1436c50c785cSJohn Marino 
1437c50c785cSJohn Marino /* Perform a binary operation on two operands.  */
1438c50c785cSJohn Marino 
1439c50c785cSJohn Marino struct value *
1440c50c785cSJohn Marino value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1441c50c785cSJohn Marino {
1442c50c785cSJohn Marino   struct value *val;
1443c50c785cSJohn Marino   struct type *type1 = check_typedef (value_type (arg1));
1444c50c785cSJohn Marino   struct type *type2 = check_typedef (value_type (arg2));
1445c50c785cSJohn Marino   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1446c50c785cSJohn Marino 		   && TYPE_VECTOR (type1));
1447c50c785cSJohn Marino   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1448c50c785cSJohn Marino 		   && TYPE_VECTOR (type2));
1449c50c785cSJohn Marino 
1450c50c785cSJohn Marino   if (!t1_is_vec && !t2_is_vec)
1451c50c785cSJohn Marino     val = scalar_binop (arg1, arg2, op);
1452c50c785cSJohn Marino   else if (t1_is_vec && t2_is_vec)
1453c50c785cSJohn Marino     val = vector_binop (arg1, arg2, op);
1454c50c785cSJohn Marino   else
1455c50c785cSJohn Marino     {
1456c50c785cSJohn Marino       /* Widen the scalar operand to a vector.  */
1457c50c785cSJohn Marino       struct value **v = t1_is_vec ? &arg2 : &arg1;
1458c50c785cSJohn Marino       struct type *t = t1_is_vec ? type2 : type1;
1459c50c785cSJohn Marino 
1460c50c785cSJohn Marino       if (TYPE_CODE (t) != TYPE_CODE_FLT
1461c50c785cSJohn Marino 	  && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1462c50c785cSJohn Marino 	  && !is_integral_type (t))
1463c50c785cSJohn Marino 	error (_("Argument to operation not a number or boolean."));
1464c50c785cSJohn Marino 
1465c50c785cSJohn Marino       *v = value_cast (t1_is_vec ? type1 : type2, *v);
1466c50c785cSJohn Marino       val = vector_binop (arg1, arg2, op);
1467c50c785cSJohn Marino     }
1468c50c785cSJohn Marino 
1469c50c785cSJohn Marino   return val;
1470c50c785cSJohn Marino }
14715796c8dcSSimon Schubert 
14725796c8dcSSimon Schubert /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
14735796c8dcSSimon Schubert 
14745796c8dcSSimon Schubert int
14755796c8dcSSimon Schubert value_logical_not (struct value *arg1)
14765796c8dcSSimon Schubert {
14775796c8dcSSimon Schubert   int len;
14785796c8dcSSimon Schubert   const gdb_byte *p;
14795796c8dcSSimon Schubert   struct type *type1;
14805796c8dcSSimon Schubert 
14815796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
14825796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
14835796c8dcSSimon Schubert 
14845796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_FLT)
14855796c8dcSSimon Schubert     return 0 == value_as_double (arg1);
14865796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
14875796c8dcSSimon Schubert     return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
14885796c8dcSSimon Schubert 			    gdbarch_byte_order (get_type_arch (type1)));
14895796c8dcSSimon Schubert 
14905796c8dcSSimon Schubert   len = TYPE_LENGTH (type1);
14915796c8dcSSimon Schubert   p = value_contents (arg1);
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert   while (--len >= 0)
14945796c8dcSSimon Schubert     {
14955796c8dcSSimon Schubert       if (*p++)
14965796c8dcSSimon Schubert 	break;
14975796c8dcSSimon Schubert     }
14985796c8dcSSimon Schubert 
14995796c8dcSSimon Schubert   return len < 0;
15005796c8dcSSimon Schubert }
15015796c8dcSSimon Schubert 
15025796c8dcSSimon Schubert /* Perform a comparison on two string values (whose content are not
1503c50c785cSJohn Marino    necessarily null terminated) based on their length.  */
15045796c8dcSSimon Schubert 
15055796c8dcSSimon Schubert static int
15065796c8dcSSimon Schubert value_strcmp (struct value *arg1, struct value *arg2)
15075796c8dcSSimon Schubert {
15085796c8dcSSimon Schubert   int len1 = TYPE_LENGTH (value_type (arg1));
15095796c8dcSSimon Schubert   int len2 = TYPE_LENGTH (value_type (arg2));
15105796c8dcSSimon Schubert   const gdb_byte *s1 = value_contents (arg1);
15115796c8dcSSimon Schubert   const gdb_byte *s2 = value_contents (arg2);
15125796c8dcSSimon Schubert   int i, len = len1 < len2 ? len1 : len2;
15135796c8dcSSimon Schubert 
15145796c8dcSSimon Schubert   for (i = 0; i < len; i++)
15155796c8dcSSimon Schubert     {
15165796c8dcSSimon Schubert       if (s1[i] < s2[i])
15175796c8dcSSimon Schubert         return -1;
15185796c8dcSSimon Schubert       else if (s1[i] > s2[i])
15195796c8dcSSimon Schubert         return 1;
15205796c8dcSSimon Schubert       else
15215796c8dcSSimon Schubert         continue;
15225796c8dcSSimon Schubert     }
15235796c8dcSSimon Schubert 
15245796c8dcSSimon Schubert   if (len1 < len2)
15255796c8dcSSimon Schubert     return -1;
15265796c8dcSSimon Schubert   else if (len1 > len2)
15275796c8dcSSimon Schubert     return 1;
15285796c8dcSSimon Schubert   else
15295796c8dcSSimon Schubert     return 0;
15305796c8dcSSimon Schubert }
15315796c8dcSSimon Schubert 
15325796c8dcSSimon Schubert /* Simulate the C operator == by returning a 1
15335796c8dcSSimon Schubert    iff ARG1 and ARG2 have equal contents.  */
15345796c8dcSSimon Schubert 
15355796c8dcSSimon Schubert int
15365796c8dcSSimon Schubert value_equal (struct value *arg1, struct value *arg2)
15375796c8dcSSimon Schubert {
15385796c8dcSSimon Schubert   int len;
15395796c8dcSSimon Schubert   const gdb_byte *p1;
15405796c8dcSSimon Schubert   const gdb_byte *p2;
15415796c8dcSSimon Schubert   struct type *type1, *type2;
15425796c8dcSSimon Schubert   enum type_code code1;
15435796c8dcSSimon Schubert   enum type_code code2;
15445796c8dcSSimon Schubert   int is_int1, is_int2;
15455796c8dcSSimon Schubert 
15465796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
15475796c8dcSSimon Schubert   arg2 = coerce_array (arg2);
15485796c8dcSSimon Schubert 
15495796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
15505796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
15515796c8dcSSimon Schubert   code1 = TYPE_CODE (type1);
15525796c8dcSSimon Schubert   code2 = TYPE_CODE (type2);
15535796c8dcSSimon Schubert   is_int1 = is_integral_type (type1);
15545796c8dcSSimon Schubert   is_int2 = is_integral_type (type2);
15555796c8dcSSimon Schubert 
15565796c8dcSSimon Schubert   if (is_int1 && is_int2)
15575796c8dcSSimon Schubert     return longest_to_int (value_as_long (value_binop (arg1, arg2,
15585796c8dcSSimon Schubert 						       BINOP_EQUAL)));
15595796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_FLT || is_int1)
15605796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_FLT || is_int2))
15615796c8dcSSimon Schubert     {
15625796c8dcSSimon Schubert       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
15635796c8dcSSimon Schubert 	 `long double' values are returned in static storage (m68k).  */
15645796c8dcSSimon Schubert       DOUBLEST d = value_as_double (arg1);
1565cf7f2e2dSJohn Marino 
15665796c8dcSSimon Schubert       return d == value_as_double (arg2);
15675796c8dcSSimon Schubert     }
15685796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
15695796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_DECFLOAT || is_int2))
15705796c8dcSSimon Schubert     {
15715796c8dcSSimon Schubert       gdb_byte v1[16], v2[16];
15725796c8dcSSimon Schubert       int len_v1, len_v2;
15735796c8dcSSimon Schubert       enum bfd_endian byte_order_v1, byte_order_v2;
15745796c8dcSSimon Schubert 
15755796c8dcSSimon Schubert       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
15765796c8dcSSimon Schubert 					 v2, &len_v2, &byte_order_v2);
15775796c8dcSSimon Schubert 
15785796c8dcSSimon Schubert       return decimal_compare (v1, len_v1, byte_order_v1,
15795796c8dcSSimon Schubert 			      v2, len_v2, byte_order_v2) == 0;
15805796c8dcSSimon Schubert     }
15815796c8dcSSimon Schubert 
15825796c8dcSSimon Schubert   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
15835796c8dcSSimon Schubert      is bigger.  */
15845796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && is_int2)
15855796c8dcSSimon Schubert     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
15865796c8dcSSimon Schubert   else if (code2 == TYPE_CODE_PTR && is_int1)
15875796c8dcSSimon Schubert     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
15885796c8dcSSimon Schubert 
15895796c8dcSSimon Schubert   else if (code1 == code2
15905796c8dcSSimon Schubert 	   && ((len = (int) TYPE_LENGTH (type1))
15915796c8dcSSimon Schubert 	       == (int) TYPE_LENGTH (type2)))
15925796c8dcSSimon Schubert     {
15935796c8dcSSimon Schubert       p1 = value_contents (arg1);
15945796c8dcSSimon Schubert       p2 = value_contents (arg2);
15955796c8dcSSimon Schubert       while (--len >= 0)
15965796c8dcSSimon Schubert 	{
15975796c8dcSSimon Schubert 	  if (*p1++ != *p2++)
15985796c8dcSSimon Schubert 	    break;
15995796c8dcSSimon Schubert 	}
16005796c8dcSSimon Schubert       return len < 0;
16015796c8dcSSimon Schubert     }
16025796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
16035796c8dcSSimon Schubert     {
16045796c8dcSSimon Schubert       return value_strcmp (arg1, arg2) == 0;
16055796c8dcSSimon Schubert     }
16065796c8dcSSimon Schubert   else
16075796c8dcSSimon Schubert     {
16085796c8dcSSimon Schubert       error (_("Invalid type combination in equality test."));
1609c50c785cSJohn Marino       return 0;			/* For lint -- never reached.  */
16105796c8dcSSimon Schubert     }
16115796c8dcSSimon Schubert }
16125796c8dcSSimon Schubert 
1613cf7f2e2dSJohn Marino /* Compare values based on their raw contents.  Useful for arrays since
1614cf7f2e2dSJohn Marino    value_equal coerces them to pointers, thus comparing just the address
1615cf7f2e2dSJohn Marino    of the array instead of its contents.  */
1616cf7f2e2dSJohn Marino 
1617cf7f2e2dSJohn Marino int
1618cf7f2e2dSJohn Marino value_equal_contents (struct value *arg1, struct value *arg2)
1619cf7f2e2dSJohn Marino {
1620cf7f2e2dSJohn Marino   struct type *type1, *type2;
1621cf7f2e2dSJohn Marino 
1622cf7f2e2dSJohn Marino   type1 = check_typedef (value_type (arg1));
1623cf7f2e2dSJohn Marino   type2 = check_typedef (value_type (arg2));
1624cf7f2e2dSJohn Marino 
1625cf7f2e2dSJohn Marino   return (TYPE_CODE (type1) == TYPE_CODE (type2)
1626cf7f2e2dSJohn Marino 	  && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1627cf7f2e2dSJohn Marino 	  && memcmp (value_contents (arg1), value_contents (arg2),
1628cf7f2e2dSJohn Marino 		     TYPE_LENGTH (type1)) == 0);
1629cf7f2e2dSJohn Marino }
1630cf7f2e2dSJohn Marino 
16315796c8dcSSimon Schubert /* Simulate the C operator < by returning 1
16325796c8dcSSimon Schubert    iff ARG1's contents are less than ARG2's.  */
16335796c8dcSSimon Schubert 
16345796c8dcSSimon Schubert int
16355796c8dcSSimon Schubert value_less (struct value *arg1, struct value *arg2)
16365796c8dcSSimon Schubert {
16375796c8dcSSimon Schubert   enum type_code code1;
16385796c8dcSSimon Schubert   enum type_code code2;
16395796c8dcSSimon Schubert   struct type *type1, *type2;
16405796c8dcSSimon Schubert   int is_int1, is_int2;
16415796c8dcSSimon Schubert 
16425796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
16435796c8dcSSimon Schubert   arg2 = coerce_array (arg2);
16445796c8dcSSimon Schubert 
16455796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
16465796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
16475796c8dcSSimon Schubert   code1 = TYPE_CODE (type1);
16485796c8dcSSimon Schubert   code2 = TYPE_CODE (type2);
16495796c8dcSSimon Schubert   is_int1 = is_integral_type (type1);
16505796c8dcSSimon Schubert   is_int2 = is_integral_type (type2);
16515796c8dcSSimon Schubert 
16525796c8dcSSimon Schubert   if (is_int1 && is_int2)
16535796c8dcSSimon Schubert     return longest_to_int (value_as_long (value_binop (arg1, arg2,
16545796c8dcSSimon Schubert 						       BINOP_LESS)));
16555796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_FLT || is_int1)
16565796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_FLT || is_int2))
16575796c8dcSSimon Schubert     {
16585796c8dcSSimon Schubert       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
16595796c8dcSSimon Schubert 	 `long double' values are returned in static storage (m68k).  */
16605796c8dcSSimon Schubert       DOUBLEST d = value_as_double (arg1);
1661cf7f2e2dSJohn Marino 
16625796c8dcSSimon Schubert       return d < value_as_double (arg2);
16635796c8dcSSimon Schubert     }
16645796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
16655796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_DECFLOAT || is_int2))
16665796c8dcSSimon Schubert     {
16675796c8dcSSimon Schubert       gdb_byte v1[16], v2[16];
16685796c8dcSSimon Schubert       int len_v1, len_v2;
16695796c8dcSSimon Schubert       enum bfd_endian byte_order_v1, byte_order_v2;
16705796c8dcSSimon Schubert 
16715796c8dcSSimon Schubert       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
16725796c8dcSSimon Schubert 					 v2, &len_v2, &byte_order_v2);
16735796c8dcSSimon Schubert 
16745796c8dcSSimon Schubert       return decimal_compare (v1, len_v1, byte_order_v1,
16755796c8dcSSimon Schubert 			      v2, len_v2, byte_order_v2) == -1;
16765796c8dcSSimon Schubert     }
16775796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
16785796c8dcSSimon Schubert     return value_as_address (arg1) < value_as_address (arg2);
16795796c8dcSSimon Schubert 
16805796c8dcSSimon Schubert   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
16815796c8dcSSimon Schubert      is bigger.  */
16825796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && is_int2)
16835796c8dcSSimon Schubert     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
16845796c8dcSSimon Schubert   else if (code2 == TYPE_CODE_PTR && is_int1)
16855796c8dcSSimon Schubert     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
16865796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
16875796c8dcSSimon Schubert     return value_strcmp (arg1, arg2) < 0;
16885796c8dcSSimon Schubert   else
16895796c8dcSSimon Schubert     {
16905796c8dcSSimon Schubert       error (_("Invalid type combination in ordering comparison."));
16915796c8dcSSimon Schubert       return 0;
16925796c8dcSSimon Schubert     }
16935796c8dcSSimon Schubert }
16945796c8dcSSimon Schubert 
16955796c8dcSSimon Schubert /* The unary operators +, - and ~.  They free the argument ARG1.  */
16965796c8dcSSimon Schubert 
16975796c8dcSSimon Schubert struct value *
16985796c8dcSSimon Schubert value_pos (struct value *arg1)
16995796c8dcSSimon Schubert {
17005796c8dcSSimon Schubert   struct type *type;
17015796c8dcSSimon Schubert 
17025796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
17035796c8dcSSimon Schubert   type = check_typedef (value_type (arg1));
17045796c8dcSSimon Schubert 
17055796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_FLT)
17065796c8dcSSimon Schubert     return value_from_double (type, value_as_double (arg1));
17075796c8dcSSimon Schubert   else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
17085796c8dcSSimon Schubert     return value_from_decfloat (type, value_contents (arg1));
17095796c8dcSSimon Schubert   else if (is_integral_type (type))
17105796c8dcSSimon Schubert     {
17115796c8dcSSimon Schubert       return value_from_longest (type, value_as_long (arg1));
17125796c8dcSSimon Schubert     }
1713c50c785cSJohn Marino   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1714c50c785cSJohn Marino     {
1715c50c785cSJohn Marino       struct value *val = allocate_value (type);
1716c50c785cSJohn Marino 
1717c50c785cSJohn Marino       memcpy (value_contents_raw (val), value_contents (arg1),
1718c50c785cSJohn Marino               TYPE_LENGTH (type));
1719c50c785cSJohn Marino       return val;
1720c50c785cSJohn Marino     }
17215796c8dcSSimon Schubert   else
17225796c8dcSSimon Schubert     {
1723c50c785cSJohn Marino       error (_("Argument to positive operation not a number."));
1724c50c785cSJohn Marino       return 0;			/* For lint -- never reached.  */
17255796c8dcSSimon Schubert     }
17265796c8dcSSimon Schubert }
17275796c8dcSSimon Schubert 
17285796c8dcSSimon Schubert struct value *
17295796c8dcSSimon Schubert value_neg (struct value *arg1)
17305796c8dcSSimon Schubert {
17315796c8dcSSimon Schubert   struct type *type;
17325796c8dcSSimon Schubert 
17335796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
17345796c8dcSSimon Schubert   type = check_typedef (value_type (arg1));
17355796c8dcSSimon Schubert 
17365796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
17375796c8dcSSimon Schubert     {
17385796c8dcSSimon Schubert       struct value *val = allocate_value (type);
17395796c8dcSSimon Schubert       int len = TYPE_LENGTH (type);
1740c50c785cSJohn Marino       gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long.  */
17415796c8dcSSimon Schubert 
17425796c8dcSSimon Schubert       memcpy (decbytes, value_contents (arg1), len);
17435796c8dcSSimon Schubert 
17445796c8dcSSimon Schubert       if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
17455796c8dcSSimon Schubert 	decbytes[len-1] = decbytes[len - 1] | 0x80;
17465796c8dcSSimon Schubert       else
17475796c8dcSSimon Schubert 	decbytes[0] = decbytes[0] | 0x80;
17485796c8dcSSimon Schubert 
17495796c8dcSSimon Schubert       memcpy (value_contents_raw (val), decbytes, len);
17505796c8dcSSimon Schubert       return val;
17515796c8dcSSimon Schubert     }
17525796c8dcSSimon Schubert   else if (TYPE_CODE (type) == TYPE_CODE_FLT)
17535796c8dcSSimon Schubert     return value_from_double (type, -value_as_double (arg1));
17545796c8dcSSimon Schubert   else if (is_integral_type (type))
17555796c8dcSSimon Schubert     {
17565796c8dcSSimon Schubert       return value_from_longest (type, -value_as_long (arg1));
17575796c8dcSSimon Schubert     }
1758c50c785cSJohn Marino   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1759c50c785cSJohn Marino     {
1760c50c785cSJohn Marino       struct value *tmp, *val = allocate_value (type);
1761c50c785cSJohn Marino       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1762c50c785cSJohn Marino       int i;
1763c50c785cSJohn Marino       LONGEST low_bound, high_bound;
1764c50c785cSJohn Marino 
1765c50c785cSJohn Marino       if (!get_array_bounds (type, &low_bound, &high_bound))
1766c50c785cSJohn Marino 	error (_("Could not determine the vector bounds"));
1767c50c785cSJohn Marino 
1768c50c785cSJohn Marino       for (i = 0; i < high_bound - low_bound + 1; i++)
1769c50c785cSJohn Marino 	{
1770c50c785cSJohn Marino 	  tmp = value_neg (value_subscript (arg1, i));
1771c50c785cSJohn Marino 	  memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1772c50c785cSJohn Marino 		  value_contents_all (tmp), TYPE_LENGTH (eltype));
1773c50c785cSJohn Marino 	}
1774c50c785cSJohn Marino       return val;
1775c50c785cSJohn Marino     }
17765796c8dcSSimon Schubert   else
17775796c8dcSSimon Schubert     {
17785796c8dcSSimon Schubert       error (_("Argument to negate operation not a number."));
1779c50c785cSJohn Marino       return 0;			/* For lint -- never reached.  */
17805796c8dcSSimon Schubert     }
17815796c8dcSSimon Schubert }
17825796c8dcSSimon Schubert 
17835796c8dcSSimon Schubert struct value *
17845796c8dcSSimon Schubert value_complement (struct value *arg1)
17855796c8dcSSimon Schubert {
17865796c8dcSSimon Schubert   struct type *type;
1787c50c785cSJohn Marino   struct value *val;
17885796c8dcSSimon Schubert 
17895796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
17905796c8dcSSimon Schubert   type = check_typedef (value_type (arg1));
17915796c8dcSSimon Schubert 
1792c50c785cSJohn Marino   if (is_integral_type (type))
1793c50c785cSJohn Marino     val = value_from_longest (type, ~value_as_long (arg1));
1794c50c785cSJohn Marino   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1795c50c785cSJohn Marino     {
1796c50c785cSJohn Marino       struct value *tmp;
1797c50c785cSJohn Marino       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1798c50c785cSJohn Marino       int i;
1799c50c785cSJohn Marino       LONGEST low_bound, high_bound;
18005796c8dcSSimon Schubert 
1801c50c785cSJohn Marino       if (!get_array_bounds (type, &low_bound, &high_bound))
1802c50c785cSJohn Marino 	error (_("Could not determine the vector bounds"));
1803c50c785cSJohn Marino 
1804c50c785cSJohn Marino       val = allocate_value (type);
1805c50c785cSJohn Marino       for (i = 0; i < high_bound - low_bound + 1; i++)
1806c50c785cSJohn Marino         {
1807c50c785cSJohn Marino           tmp = value_complement (value_subscript (arg1, i));
1808c50c785cSJohn Marino           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1809c50c785cSJohn Marino                   value_contents_all (tmp), TYPE_LENGTH (eltype));
1810c50c785cSJohn Marino         }
1811c50c785cSJohn Marino     }
1812c50c785cSJohn Marino   else
1813c50c785cSJohn Marino     error (_("Argument to complement operation not an integer, boolean."));
1814c50c785cSJohn Marino 
1815c50c785cSJohn Marino   return val;
18165796c8dcSSimon Schubert }
18175796c8dcSSimon Schubert 
18185796c8dcSSimon Schubert /* The INDEX'th bit of SET value whose value_type is TYPE,
18195796c8dcSSimon Schubert    and whose value_contents is valaddr.
18205796c8dcSSimon Schubert    Return -1 if out of range, -2 other error.  */
18215796c8dcSSimon Schubert 
18225796c8dcSSimon Schubert int
18235796c8dcSSimon Schubert value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
18245796c8dcSSimon Schubert {
18255796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (type);
18265796c8dcSSimon Schubert   LONGEST low_bound, high_bound;
18275796c8dcSSimon Schubert   LONGEST word;
18285796c8dcSSimon Schubert   unsigned rel_index;
18295796c8dcSSimon Schubert   struct type *range = TYPE_INDEX_TYPE (type);
1830cf7f2e2dSJohn Marino 
18315796c8dcSSimon Schubert   if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
18325796c8dcSSimon Schubert     return -2;
18335796c8dcSSimon Schubert   if (index < low_bound || index > high_bound)
18345796c8dcSSimon Schubert     return -1;
18355796c8dcSSimon Schubert   rel_index = index - low_bound;
18365796c8dcSSimon Schubert   word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
18375796c8dcSSimon Schubert 				   gdbarch_byte_order (gdbarch));
18385796c8dcSSimon Schubert   rel_index %= TARGET_CHAR_BIT;
18395796c8dcSSimon Schubert   if (gdbarch_bits_big_endian (gdbarch))
18405796c8dcSSimon Schubert     rel_index = TARGET_CHAR_BIT - 1 - rel_index;
18415796c8dcSSimon Schubert   return (word >> rel_index) & 1;
18425796c8dcSSimon Schubert }
18435796c8dcSSimon Schubert 
18445796c8dcSSimon Schubert int
18455796c8dcSSimon Schubert value_in (struct value *element, struct value *set)
18465796c8dcSSimon Schubert {
18475796c8dcSSimon Schubert   int member;
18485796c8dcSSimon Schubert   struct type *settype = check_typedef (value_type (set));
18495796c8dcSSimon Schubert   struct type *eltype = check_typedef (value_type (element));
1850cf7f2e2dSJohn Marino 
18515796c8dcSSimon Schubert   if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
18525796c8dcSSimon Schubert     eltype = TYPE_TARGET_TYPE (eltype);
18535796c8dcSSimon Schubert   if (TYPE_CODE (settype) != TYPE_CODE_SET)
18545796c8dcSSimon Schubert     error (_("Second argument of 'IN' has wrong type"));
18555796c8dcSSimon Schubert   if (TYPE_CODE (eltype) != TYPE_CODE_INT
18565796c8dcSSimon Schubert       && TYPE_CODE (eltype) != TYPE_CODE_CHAR
18575796c8dcSSimon Schubert       && TYPE_CODE (eltype) != TYPE_CODE_ENUM
18585796c8dcSSimon Schubert       && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
18595796c8dcSSimon Schubert     error (_("First argument of 'IN' has wrong type"));
18605796c8dcSSimon Schubert   member = value_bit_index (settype, value_contents (set),
18615796c8dcSSimon Schubert 			    value_as_long (element));
18625796c8dcSSimon Schubert   if (member < 0)
18635796c8dcSSimon Schubert     error (_("First argument of 'IN' not in range"));
18645796c8dcSSimon Schubert   return member;
18655796c8dcSSimon Schubert }
18665796c8dcSSimon Schubert 
18675796c8dcSSimon Schubert void
18685796c8dcSSimon Schubert _initialize_valarith (void)
18695796c8dcSSimon Schubert {
18705796c8dcSSimon Schubert }
1871