xref: /dragonfly/contrib/gdb-7/gdb/valarith.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Perform arithmetic and other operations on values, for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "value.h"
225796c8dcSSimon Schubert #include "symtab.h"
235796c8dcSSimon Schubert #include "gdbtypes.h"
245796c8dcSSimon Schubert #include "expression.h"
255796c8dcSSimon Schubert #include "target.h"
265796c8dcSSimon Schubert #include "language.h"
275796c8dcSSimon Schubert #include "gdb_string.h"
285796c8dcSSimon Schubert #include "doublest.h"
295796c8dcSSimon Schubert #include "dfp.h"
305796c8dcSSimon Schubert #include <math.h>
315796c8dcSSimon Schubert #include "infcall.h"
32cf7f2e2dSJohn Marino #include "exceptions.h"
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert /* Define whether or not the C operator '/' truncates towards zero for
355796c8dcSSimon Schubert    differently signed operands (truncation direction is undefined in C).  */
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert #ifndef TRUNCATION_TOWARDS_ZERO
385796c8dcSSimon Schubert #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
395796c8dcSSimon Schubert #endif
405796c8dcSSimon Schubert 
415796c8dcSSimon Schubert void _initialize_valarith (void);
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert /* Given a pointer, return the size of its target.
455796c8dcSSimon Schubert    If the pointer type is void *, then return 1.
465796c8dcSSimon Schubert    If the target type is incomplete, then error out.
475796c8dcSSimon Schubert    This isn't a general purpose function, but just a
48c50c785cSJohn Marino    helper for value_ptradd.  */
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert static LONGEST
find_size_for_pointer_math(struct type * ptr_type)515796c8dcSSimon Schubert find_size_for_pointer_math (struct type *ptr_type)
525796c8dcSSimon Schubert {
535796c8dcSSimon Schubert   LONGEST sz = -1;
545796c8dcSSimon Schubert   struct type *ptr_target;
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
575796c8dcSSimon Schubert   ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert   sz = TYPE_LENGTH (ptr_target);
605796c8dcSSimon Schubert   if (sz == 0)
615796c8dcSSimon Schubert     {
625796c8dcSSimon Schubert       if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
635796c8dcSSimon Schubert 	sz = 1;
645796c8dcSSimon Schubert       else
655796c8dcSSimon Schubert 	{
66*ef5ccd6cSJohn Marino 	  const char *name;
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert 	  name = TYPE_NAME (ptr_target);
695796c8dcSSimon Schubert 	  if (name == NULL)
705796c8dcSSimon Schubert 	    name = TYPE_TAG_NAME (ptr_target);
715796c8dcSSimon Schubert 	  if (name == NULL)
725796c8dcSSimon Schubert 	    error (_("Cannot perform pointer math on incomplete types, "
735796c8dcSSimon Schubert 		   "try casting to a known type, or void *."));
745796c8dcSSimon Schubert 	  else
755796c8dcSSimon Schubert 	    error (_("Cannot perform pointer math on incomplete type \"%s\", "
765796c8dcSSimon Schubert 		   "try casting to a known type, or void *."), name);
775796c8dcSSimon Schubert 	}
785796c8dcSSimon Schubert     }
795796c8dcSSimon Schubert   return sz;
805796c8dcSSimon Schubert }
815796c8dcSSimon Schubert 
825796c8dcSSimon Schubert /* Given a pointer ARG1 and an integral value ARG2, return the
835796c8dcSSimon Schubert    result of C-style pointer arithmetic ARG1 + ARG2.  */
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert struct value *
value_ptradd(struct value * arg1,LONGEST arg2)865796c8dcSSimon Schubert value_ptradd (struct value *arg1, LONGEST arg2)
875796c8dcSSimon Schubert {
885796c8dcSSimon Schubert   struct type *valptrtype;
895796c8dcSSimon Schubert   LONGEST sz;
90c50c785cSJohn Marino   struct value *result;
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
935796c8dcSSimon Schubert   valptrtype = check_typedef (value_type (arg1));
945796c8dcSSimon Schubert   sz = find_size_for_pointer_math (valptrtype);
955796c8dcSSimon Schubert 
96c50c785cSJohn Marino   result = value_from_pointer (valptrtype,
975796c8dcSSimon Schubert 			       value_as_address (arg1) + sz * arg2);
98c50c785cSJohn Marino   if (VALUE_LVAL (result) != lval_internalvar)
99c50c785cSJohn Marino     set_value_component_location (result, arg1);
100c50c785cSJohn Marino   return result;
1015796c8dcSSimon Schubert }
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert /* Given two compatible pointer values ARG1 and ARG2, return the
1045796c8dcSSimon Schubert    result of C-style pointer arithmetic ARG1 - ARG2.  */
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert LONGEST
value_ptrdiff(struct value * arg1,struct value * arg2)1075796c8dcSSimon Schubert value_ptrdiff (struct value *arg1, struct value *arg2)
1085796c8dcSSimon Schubert {
1095796c8dcSSimon Schubert   struct type *type1, *type2;
1105796c8dcSSimon Schubert   LONGEST sz;
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
1135796c8dcSSimon Schubert   arg2 = coerce_array (arg2);
1145796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
1155796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
1185796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert   if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
1215796c8dcSSimon Schubert       != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
122c50c785cSJohn Marino     error (_("First argument of `-' is a pointer and "
123c50c785cSJohn Marino 	     "second argument is neither\n"
124c50c785cSJohn Marino 	     "an integer nor a pointer of the same type."));
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
127cf7f2e2dSJohn Marino   if (sz == 0)
128cf7f2e2dSJohn Marino     {
129cf7f2e2dSJohn Marino       warning (_("Type size unknown, assuming 1. "
130cf7f2e2dSJohn Marino                "Try casting to a known type, or void *."));
131cf7f2e2dSJohn Marino       sz = 1;
132cf7f2e2dSJohn Marino     }
133cf7f2e2dSJohn Marino 
1345796c8dcSSimon Schubert   return (value_as_long (arg1) - value_as_long (arg2)) / sz;
1355796c8dcSSimon Schubert }
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert /* Return the value of ARRAY[IDX].
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert    ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING.  If the
1405796c8dcSSimon Schubert    current language supports C-style arrays, it may also be TYPE_CODE_PTR.
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert    See comments in value_coerce_array() for rationale for reason for
1435796c8dcSSimon Schubert    doing lower bounds adjustment here rather than there.
1445796c8dcSSimon Schubert    FIXME:  Perhaps we should validate that the index is valid and if
1455796c8dcSSimon Schubert    verbosity is set, warn about invalid indices (but still use them).  */
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert struct value *
value_subscript(struct value * array,LONGEST index)1485796c8dcSSimon Schubert value_subscript (struct value *array, LONGEST index)
1495796c8dcSSimon Schubert {
1505796c8dcSSimon Schubert   int c_style = current_language->c_style_arrays;
1515796c8dcSSimon Schubert   struct type *tarray;
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   array = coerce_ref (array);
1545796c8dcSSimon Schubert   tarray = check_typedef (value_type (array));
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert   if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
1575796c8dcSSimon Schubert       || TYPE_CODE (tarray) == TYPE_CODE_STRING)
1585796c8dcSSimon Schubert     {
1595796c8dcSSimon Schubert       struct type *range_type = TYPE_INDEX_TYPE (tarray);
1605796c8dcSSimon Schubert       LONGEST lowerbound, upperbound;
1615796c8dcSSimon Schubert 
162cf7f2e2dSJohn Marino       get_discrete_bounds (range_type, &lowerbound, &upperbound);
1635796c8dcSSimon Schubert       if (VALUE_LVAL (array) != lval_memory)
1645796c8dcSSimon Schubert 	return value_subscripted_rvalue (array, index, lowerbound);
1655796c8dcSSimon Schubert 
1665796c8dcSSimon Schubert       if (c_style == 0)
1675796c8dcSSimon Schubert 	{
1685796c8dcSSimon Schubert 	  if (index >= lowerbound && index <= upperbound)
1695796c8dcSSimon Schubert 	    return value_subscripted_rvalue (array, index, lowerbound);
1705796c8dcSSimon Schubert 	  /* Emit warning unless we have an array of unknown size.
1715796c8dcSSimon Schubert 	     An array of unknown size has lowerbound 0 and upperbound -1.  */
1725796c8dcSSimon Schubert 	  if (upperbound > -1)
1735796c8dcSSimon Schubert 	    warning (_("array or string index out of range"));
1745796c8dcSSimon Schubert 	  /* fall doing C stuff */
1755796c8dcSSimon Schubert 	  c_style = 1;
1765796c8dcSSimon Schubert 	}
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert       index -= lowerbound;
1795796c8dcSSimon Schubert       array = value_coerce_array (array);
1805796c8dcSSimon Schubert     }
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert   if (c_style)
1835796c8dcSSimon Schubert     return value_ind (value_ptradd (array, index));
1845796c8dcSSimon Schubert   else
1855796c8dcSSimon Schubert     error (_("not an array or string"));
1865796c8dcSSimon Schubert }
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert /* Return the value of EXPR[IDX], expr an aggregate rvalue
1895796c8dcSSimon Schubert    (eg, a vector register).  This routine used to promote floats
1905796c8dcSSimon Schubert    to doubles, but no longer does.  */
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert struct value *
value_subscripted_rvalue(struct value * array,LONGEST index,int lowerbound)1935796c8dcSSimon Schubert value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
1945796c8dcSSimon Schubert {
1955796c8dcSSimon Schubert   struct type *array_type = check_typedef (value_type (array));
1965796c8dcSSimon Schubert   struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
1975796c8dcSSimon Schubert   unsigned int elt_size = TYPE_LENGTH (elt_type);
1985796c8dcSSimon Schubert   unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
1995796c8dcSSimon Schubert   struct value *v;
2005796c8dcSSimon Schubert 
201cf7f2e2dSJohn Marino   if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
202cf7f2e2dSJohn Marino 			     && elt_offs >= TYPE_LENGTH (array_type)))
2035796c8dcSSimon Schubert     error (_("no such vector element"));
2045796c8dcSSimon Schubert 
2055796c8dcSSimon Schubert   if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
206c50c785cSJohn Marino     v = allocate_value_lazy (elt_type);
2075796c8dcSSimon Schubert   else
208c50c785cSJohn Marino     {
209c50c785cSJohn Marino       v = allocate_value (elt_type);
210c50c785cSJohn Marino       value_contents_copy (v, value_embedded_offset (v),
211c50c785cSJohn Marino 			   array, value_embedded_offset (array) + elt_offs,
212c50c785cSJohn Marino 			   elt_size);
213c50c785cSJohn Marino     }
2145796c8dcSSimon Schubert 
2155796c8dcSSimon Schubert   set_value_component_location (v, array);
2165796c8dcSSimon Schubert   VALUE_REGNUM (v) = VALUE_REGNUM (array);
2175796c8dcSSimon Schubert   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
2185796c8dcSSimon Schubert   set_value_offset (v, value_offset (array) + elt_offs);
2195796c8dcSSimon Schubert   return v;
2205796c8dcSSimon Schubert }
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert 
2235796c8dcSSimon Schubert /* Check to see if either argument is a structure, or a reference to
2245796c8dcSSimon Schubert    one.  This is called so we know whether to go ahead with the normal
2255796c8dcSSimon Schubert    binop or look for a user defined function instead.
2265796c8dcSSimon Schubert 
2275796c8dcSSimon Schubert    For now, we do not overload the `=' operator.  */
2285796c8dcSSimon Schubert 
2295796c8dcSSimon Schubert int
binop_types_user_defined_p(enum exp_opcode op,struct type * type1,struct type * type2)230cf7f2e2dSJohn Marino binop_types_user_defined_p (enum exp_opcode op,
231cf7f2e2dSJohn Marino 			    struct type *type1, struct type *type2)
2325796c8dcSSimon Schubert {
2335796c8dcSSimon Schubert   if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
2345796c8dcSSimon Schubert     return 0;
2355796c8dcSSimon Schubert 
236cf7f2e2dSJohn Marino   type1 = check_typedef (type1);
2375796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_REF)
2385796c8dcSSimon Schubert     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
2395796c8dcSSimon Schubert 
240*ef5ccd6cSJohn Marino   type2 = check_typedef (type2);
2415796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_REF)
2425796c8dcSSimon Schubert     type2 = check_typedef (TYPE_TARGET_TYPE (type2));
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert   return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
2455796c8dcSSimon Schubert 	  || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
2465796c8dcSSimon Schubert }
2475796c8dcSSimon Schubert 
248cf7f2e2dSJohn Marino /* Check to see if either argument is a structure, or a reference to
249cf7f2e2dSJohn Marino    one.  This is called so we know whether to go ahead with the normal
250cf7f2e2dSJohn Marino    binop or look for a user defined function instead.
251cf7f2e2dSJohn Marino 
252cf7f2e2dSJohn Marino    For now, we do not overload the `=' operator.  */
253cf7f2e2dSJohn Marino 
254cf7f2e2dSJohn Marino int
binop_user_defined_p(enum exp_opcode op,struct value * arg1,struct value * arg2)255cf7f2e2dSJohn Marino binop_user_defined_p (enum exp_opcode op,
256cf7f2e2dSJohn Marino 		      struct value *arg1, struct value *arg2)
257cf7f2e2dSJohn Marino {
258cf7f2e2dSJohn Marino   return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
259cf7f2e2dSJohn Marino }
260cf7f2e2dSJohn Marino 
2615796c8dcSSimon Schubert /* Check to see if argument is a structure.  This is called so
2625796c8dcSSimon Schubert    we know whether to go ahead with the normal unop or look for a
2635796c8dcSSimon Schubert    user defined function instead.
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert    For now, we do not overload the `&' operator.  */
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert int
unop_user_defined_p(enum exp_opcode op,struct value * arg1)2685796c8dcSSimon Schubert unop_user_defined_p (enum exp_opcode op, struct value *arg1)
2695796c8dcSSimon Schubert {
2705796c8dcSSimon Schubert   struct type *type1;
271cf7f2e2dSJohn Marino 
2725796c8dcSSimon Schubert   if (op == UNOP_ADDR)
2735796c8dcSSimon Schubert     return 0;
2745796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
275c50c785cSJohn Marino   if (TYPE_CODE (type1) == TYPE_CODE_REF)
276c50c785cSJohn Marino     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
277c50c785cSJohn Marino   return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
2785796c8dcSSimon Schubert }
2795796c8dcSSimon Schubert 
280cf7f2e2dSJohn Marino /* Try to find an operator named OPERATOR which takes NARGS arguments
281cf7f2e2dSJohn Marino    specified in ARGS.  If the operator found is a static member operator
282cf7f2e2dSJohn Marino    *STATIC_MEMFUNP will be set to 1, and otherwise 0.
283cf7f2e2dSJohn Marino    The search if performed through find_overload_match which will handle
284cf7f2e2dSJohn Marino    member operators, non member operators, operators imported implicitly or
285cf7f2e2dSJohn Marino    explicitly, and perform correct overload resolution in all of the above
286cf7f2e2dSJohn Marino    situations or combinations thereof.  */
287cf7f2e2dSJohn Marino 
288cf7f2e2dSJohn Marino static struct value *
value_user_defined_cpp_op(struct value ** args,int nargs,char * operator,int * static_memfuncp)289cf7f2e2dSJohn Marino value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
290cf7f2e2dSJohn Marino                            int *static_memfuncp)
291cf7f2e2dSJohn Marino {
292cf7f2e2dSJohn Marino 
293cf7f2e2dSJohn Marino   struct symbol *symp = NULL;
294cf7f2e2dSJohn Marino   struct value *valp = NULL;
295cf7f2e2dSJohn Marino 
296a45ae5f8SJohn Marino   find_overload_match (args, nargs, operator, BOTH /* could be method */,
297*ef5ccd6cSJohn Marino                        &args[0] /* objp */,
298cf7f2e2dSJohn Marino                        NULL /* pass NULL symbol since symbol is unknown */,
299cf7f2e2dSJohn Marino                        &valp, &symp, static_memfuncp, 0);
300cf7f2e2dSJohn Marino 
301cf7f2e2dSJohn Marino   if (valp)
302cf7f2e2dSJohn Marino     return valp;
303cf7f2e2dSJohn Marino 
304cf7f2e2dSJohn Marino   if (symp)
305cf7f2e2dSJohn Marino     {
306cf7f2e2dSJohn Marino       /* This is a non member function and does not
307cf7f2e2dSJohn Marino          expect a reference as its first argument
308cf7f2e2dSJohn Marino          rather the explicit structure.  */
309cf7f2e2dSJohn Marino       args[0] = value_ind (args[0]);
310cf7f2e2dSJohn Marino       return value_of_variable (symp, 0);
311cf7f2e2dSJohn Marino     }
312cf7f2e2dSJohn Marino 
313cf7f2e2dSJohn Marino   error (_("Could not find %s."), operator);
314cf7f2e2dSJohn Marino }
315cf7f2e2dSJohn Marino 
316cf7f2e2dSJohn Marino /* Lookup user defined operator NAME.  Return a value representing the
317cf7f2e2dSJohn Marino    function, otherwise return NULL.  */
318cf7f2e2dSJohn Marino 
319cf7f2e2dSJohn Marino static struct value *
value_user_defined_op(struct value ** argp,struct value ** args,char * name,int * static_memfuncp,int nargs)320cf7f2e2dSJohn Marino value_user_defined_op (struct value **argp, struct value **args, char *name,
321cf7f2e2dSJohn Marino                        int *static_memfuncp, int nargs)
322cf7f2e2dSJohn Marino {
323cf7f2e2dSJohn Marino   struct value *result = NULL;
324cf7f2e2dSJohn Marino 
325cf7f2e2dSJohn Marino   if (current_language->la_language == language_cplus)
326cf7f2e2dSJohn Marino     result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp);
327cf7f2e2dSJohn Marino   else
328cf7f2e2dSJohn Marino     result = value_struct_elt (argp, args, name, static_memfuncp,
329cf7f2e2dSJohn Marino                                "structure");
330cf7f2e2dSJohn Marino 
331cf7f2e2dSJohn Marino   return result;
332cf7f2e2dSJohn Marino }
333cf7f2e2dSJohn Marino 
3345796c8dcSSimon Schubert /* We know either arg1 or arg2 is a structure, so try to find the right
3355796c8dcSSimon Schubert    user defined function.  Create an argument vector that calls
3365796c8dcSSimon Schubert    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
3375796c8dcSSimon Schubert    binary operator which is legal for GNU C++).
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
3405796c8dcSSimon Schubert    is the opcode saying how to modify it.  Otherwise, OTHEROP is
3415796c8dcSSimon Schubert    unused.  */
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert struct value *
value_x_binop(struct value * arg1,struct value * arg2,enum exp_opcode op,enum exp_opcode otherop,enum noside noside)3445796c8dcSSimon Schubert value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
3455796c8dcSSimon Schubert 	       enum exp_opcode otherop, enum noside noside)
3465796c8dcSSimon Schubert {
3475796c8dcSSimon Schubert   struct value **argvec;
3485796c8dcSSimon Schubert   char *ptr;
3495796c8dcSSimon Schubert   char tstr[13];
3505796c8dcSSimon Schubert   int static_memfuncp;
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
3535796c8dcSSimon Schubert   arg2 = coerce_ref (arg2);
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert   /* now we know that what we have to do is construct our
3565796c8dcSSimon Schubert      arg vector and find the right function to call it with.  */
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
3595796c8dcSSimon Schubert     error (_("Can't do that binary op on that type"));	/* FIXME be explicit */
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
3625796c8dcSSimon Schubert   argvec[1] = value_addr (arg1);
3635796c8dcSSimon Schubert   argvec[2] = arg2;
3645796c8dcSSimon Schubert   argvec[3] = 0;
3655796c8dcSSimon Schubert 
366c50c785cSJohn Marino   /* Make the right function name up.  */
3675796c8dcSSimon Schubert   strcpy (tstr, "operator__");
3685796c8dcSSimon Schubert   ptr = tstr + 8;
3695796c8dcSSimon Schubert   switch (op)
3705796c8dcSSimon Schubert     {
3715796c8dcSSimon Schubert     case BINOP_ADD:
3725796c8dcSSimon Schubert       strcpy (ptr, "+");
3735796c8dcSSimon Schubert       break;
3745796c8dcSSimon Schubert     case BINOP_SUB:
3755796c8dcSSimon Schubert       strcpy (ptr, "-");
3765796c8dcSSimon Schubert       break;
3775796c8dcSSimon Schubert     case BINOP_MUL:
3785796c8dcSSimon Schubert       strcpy (ptr, "*");
3795796c8dcSSimon Schubert       break;
3805796c8dcSSimon Schubert     case BINOP_DIV:
3815796c8dcSSimon Schubert       strcpy (ptr, "/");
3825796c8dcSSimon Schubert       break;
3835796c8dcSSimon Schubert     case BINOP_REM:
3845796c8dcSSimon Schubert       strcpy (ptr, "%");
3855796c8dcSSimon Schubert       break;
3865796c8dcSSimon Schubert     case BINOP_LSH:
3875796c8dcSSimon Schubert       strcpy (ptr, "<<");
3885796c8dcSSimon Schubert       break;
3895796c8dcSSimon Schubert     case BINOP_RSH:
3905796c8dcSSimon Schubert       strcpy (ptr, ">>");
3915796c8dcSSimon Schubert       break;
3925796c8dcSSimon Schubert     case BINOP_BITWISE_AND:
3935796c8dcSSimon Schubert       strcpy (ptr, "&");
3945796c8dcSSimon Schubert       break;
3955796c8dcSSimon Schubert     case BINOP_BITWISE_IOR:
3965796c8dcSSimon Schubert       strcpy (ptr, "|");
3975796c8dcSSimon Schubert       break;
3985796c8dcSSimon Schubert     case BINOP_BITWISE_XOR:
3995796c8dcSSimon Schubert       strcpy (ptr, "^");
4005796c8dcSSimon Schubert       break;
4015796c8dcSSimon Schubert     case BINOP_LOGICAL_AND:
4025796c8dcSSimon Schubert       strcpy (ptr, "&&");
4035796c8dcSSimon Schubert       break;
4045796c8dcSSimon Schubert     case BINOP_LOGICAL_OR:
4055796c8dcSSimon Schubert       strcpy (ptr, "||");
4065796c8dcSSimon Schubert       break;
4075796c8dcSSimon Schubert     case BINOP_MIN:
4085796c8dcSSimon Schubert       strcpy (ptr, "<?");
4095796c8dcSSimon Schubert       break;
4105796c8dcSSimon Schubert     case BINOP_MAX:
4115796c8dcSSimon Schubert       strcpy (ptr, ">?");
4125796c8dcSSimon Schubert       break;
4135796c8dcSSimon Schubert     case BINOP_ASSIGN:
4145796c8dcSSimon Schubert       strcpy (ptr, "=");
4155796c8dcSSimon Schubert       break;
4165796c8dcSSimon Schubert     case BINOP_ASSIGN_MODIFY:
4175796c8dcSSimon Schubert       switch (otherop)
4185796c8dcSSimon Schubert 	{
4195796c8dcSSimon Schubert 	case BINOP_ADD:
4205796c8dcSSimon Schubert 	  strcpy (ptr, "+=");
4215796c8dcSSimon Schubert 	  break;
4225796c8dcSSimon Schubert 	case BINOP_SUB:
4235796c8dcSSimon Schubert 	  strcpy (ptr, "-=");
4245796c8dcSSimon Schubert 	  break;
4255796c8dcSSimon Schubert 	case BINOP_MUL:
4265796c8dcSSimon Schubert 	  strcpy (ptr, "*=");
4275796c8dcSSimon Schubert 	  break;
4285796c8dcSSimon Schubert 	case BINOP_DIV:
4295796c8dcSSimon Schubert 	  strcpy (ptr, "/=");
4305796c8dcSSimon Schubert 	  break;
4315796c8dcSSimon Schubert 	case BINOP_REM:
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_MOD:	/* invalid */
4445796c8dcSSimon Schubert 	default:
4455796c8dcSSimon Schubert 	  error (_("Invalid binary operation specified."));
4465796c8dcSSimon Schubert 	}
4475796c8dcSSimon Schubert       break;
4485796c8dcSSimon Schubert     case BINOP_SUBSCRIPT:
4495796c8dcSSimon Schubert       strcpy (ptr, "[]");
4505796c8dcSSimon Schubert       break;
4515796c8dcSSimon Schubert     case BINOP_EQUAL:
4525796c8dcSSimon Schubert       strcpy (ptr, "==");
4535796c8dcSSimon Schubert       break;
4545796c8dcSSimon Schubert     case BINOP_NOTEQUAL:
4555796c8dcSSimon Schubert       strcpy (ptr, "!=");
4565796c8dcSSimon Schubert       break;
4575796c8dcSSimon Schubert     case BINOP_LESS:
4585796c8dcSSimon Schubert       strcpy (ptr, "<");
4595796c8dcSSimon Schubert       break;
4605796c8dcSSimon Schubert     case BINOP_GTR:
4615796c8dcSSimon Schubert       strcpy (ptr, ">");
4625796c8dcSSimon Schubert       break;
4635796c8dcSSimon Schubert     case BINOP_GEQ:
4645796c8dcSSimon Schubert       strcpy (ptr, ">=");
4655796c8dcSSimon Schubert       break;
4665796c8dcSSimon Schubert     case BINOP_LEQ:
4675796c8dcSSimon Schubert       strcpy (ptr, "<=");
4685796c8dcSSimon Schubert       break;
4695796c8dcSSimon Schubert     case BINOP_MOD:		/* invalid */
4705796c8dcSSimon Schubert     default:
4715796c8dcSSimon Schubert       error (_("Invalid binary operation specified."));
4725796c8dcSSimon Schubert     }
4735796c8dcSSimon Schubert 
474cf7f2e2dSJohn Marino   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
475cf7f2e2dSJohn Marino                                      &static_memfuncp, 2);
4765796c8dcSSimon Schubert 
4775796c8dcSSimon Schubert   if (argvec[0])
4785796c8dcSSimon Schubert     {
4795796c8dcSSimon Schubert       if (static_memfuncp)
4805796c8dcSSimon Schubert 	{
4815796c8dcSSimon Schubert 	  argvec[1] = argvec[0];
4825796c8dcSSimon Schubert 	  argvec++;
4835796c8dcSSimon Schubert 	}
4845796c8dcSSimon Schubert       if (noside == EVAL_AVOID_SIDE_EFFECTS)
4855796c8dcSSimon Schubert 	{
4865796c8dcSSimon Schubert 	  struct type *return_type;
487cf7f2e2dSJohn Marino 
4885796c8dcSSimon Schubert 	  return_type
4895796c8dcSSimon Schubert 	    = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
4905796c8dcSSimon Schubert 	  return value_zero (return_type, VALUE_LVAL (arg1));
4915796c8dcSSimon Schubert 	}
492c50c785cSJohn Marino       return call_function_by_hand (argvec[0], 2 - static_memfuncp,
493c50c785cSJohn Marino 				    argvec + 1);
4945796c8dcSSimon Schubert     }
495c50c785cSJohn Marino   throw_error (NOT_FOUND_ERROR,
496c50c785cSJohn Marino                _("member function %s not found"), tstr);
4975796c8dcSSimon Schubert #ifdef lint
4985796c8dcSSimon Schubert   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
4995796c8dcSSimon Schubert #endif
5005796c8dcSSimon Schubert }
5015796c8dcSSimon Schubert 
5025796c8dcSSimon Schubert /* We know that arg1 is a structure, so try to find a unary user
5035796c8dcSSimon Schubert    defined operator that matches the operator in question.
5045796c8dcSSimon Schubert    Create an argument vector that calls arg1.operator @ (arg1)
5055796c8dcSSimon Schubert    and return that value (where '@' is (almost) any unary operator which
5065796c8dcSSimon Schubert    is legal for GNU C++).  */
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert struct value *
value_x_unop(struct value * arg1,enum exp_opcode op,enum noside noside)5095796c8dcSSimon Schubert value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
5105796c8dcSSimon Schubert {
5115796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
5125796c8dcSSimon Schubert   struct value **argvec;
513*ef5ccd6cSJohn Marino   char *ptr;
5145796c8dcSSimon Schubert   char tstr[13], mangle_tstr[13];
5155796c8dcSSimon Schubert   int static_memfuncp, nargs;
5165796c8dcSSimon Schubert 
5175796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
5185796c8dcSSimon Schubert 
5195796c8dcSSimon Schubert   /* now we know that what we have to do is construct our
5205796c8dcSSimon Schubert      arg vector and find the right function to call it with.  */
5215796c8dcSSimon Schubert 
5225796c8dcSSimon Schubert   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
5235796c8dcSSimon Schubert     error (_("Can't do that unary op on that type"));	/* FIXME be explicit */
5245796c8dcSSimon Schubert 
5255796c8dcSSimon Schubert   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
5265796c8dcSSimon Schubert   argvec[1] = value_addr (arg1);
5275796c8dcSSimon Schubert   argvec[2] = 0;
5285796c8dcSSimon Schubert 
5295796c8dcSSimon Schubert   nargs = 1;
5305796c8dcSSimon Schubert 
531c50c785cSJohn Marino   /* Make the right function name up.  */
5325796c8dcSSimon Schubert   strcpy (tstr, "operator__");
5335796c8dcSSimon Schubert   ptr = tstr + 8;
5345796c8dcSSimon Schubert   strcpy (mangle_tstr, "__");
5355796c8dcSSimon Schubert   switch (op)
5365796c8dcSSimon Schubert     {
5375796c8dcSSimon Schubert     case UNOP_PREINCREMENT:
5385796c8dcSSimon Schubert       strcpy (ptr, "++");
5395796c8dcSSimon Schubert       break;
5405796c8dcSSimon Schubert     case UNOP_PREDECREMENT:
5415796c8dcSSimon Schubert       strcpy (ptr, "--");
5425796c8dcSSimon Schubert       break;
5435796c8dcSSimon Schubert     case UNOP_POSTINCREMENT:
5445796c8dcSSimon Schubert       strcpy (ptr, "++");
5455796c8dcSSimon Schubert       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
5465796c8dcSSimon Schubert       argvec[3] = 0;
5475796c8dcSSimon Schubert       nargs ++;
5485796c8dcSSimon Schubert       break;
5495796c8dcSSimon Schubert     case UNOP_POSTDECREMENT:
5505796c8dcSSimon Schubert       strcpy (ptr, "--");
5515796c8dcSSimon Schubert       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
5525796c8dcSSimon Schubert       argvec[3] = 0;
5535796c8dcSSimon Schubert       nargs ++;
5545796c8dcSSimon Schubert       break;
5555796c8dcSSimon Schubert     case UNOP_LOGICAL_NOT:
5565796c8dcSSimon Schubert       strcpy (ptr, "!");
5575796c8dcSSimon Schubert       break;
5585796c8dcSSimon Schubert     case UNOP_COMPLEMENT:
5595796c8dcSSimon Schubert       strcpy (ptr, "~");
5605796c8dcSSimon Schubert       break;
5615796c8dcSSimon Schubert     case UNOP_NEG:
5625796c8dcSSimon Schubert       strcpy (ptr, "-");
5635796c8dcSSimon Schubert       break;
5645796c8dcSSimon Schubert     case UNOP_PLUS:
5655796c8dcSSimon Schubert       strcpy (ptr, "+");
5665796c8dcSSimon Schubert       break;
5675796c8dcSSimon Schubert     case UNOP_IND:
5685796c8dcSSimon Schubert       strcpy (ptr, "*");
5695796c8dcSSimon Schubert       break;
570c50c785cSJohn Marino     case STRUCTOP_PTR:
571c50c785cSJohn Marino       strcpy (ptr, "->");
572c50c785cSJohn Marino       break;
5735796c8dcSSimon Schubert     default:
5745796c8dcSSimon Schubert       error (_("Invalid unary operation specified."));
5755796c8dcSSimon Schubert     }
5765796c8dcSSimon Schubert 
577cf7f2e2dSJohn Marino   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
578cf7f2e2dSJohn Marino                                      &static_memfuncp, nargs);
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert   if (argvec[0])
5815796c8dcSSimon Schubert     {
5825796c8dcSSimon Schubert       if (static_memfuncp)
5835796c8dcSSimon Schubert 	{
5845796c8dcSSimon Schubert 	  argvec[1] = argvec[0];
5855796c8dcSSimon Schubert 	  nargs --;
5865796c8dcSSimon Schubert 	  argvec++;
5875796c8dcSSimon Schubert 	}
5885796c8dcSSimon Schubert       if (noside == EVAL_AVOID_SIDE_EFFECTS)
5895796c8dcSSimon Schubert 	{
5905796c8dcSSimon Schubert 	  struct type *return_type;
591cf7f2e2dSJohn Marino 
5925796c8dcSSimon Schubert 	  return_type
5935796c8dcSSimon Schubert 	    = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
5945796c8dcSSimon Schubert 	  return value_zero (return_type, VALUE_LVAL (arg1));
5955796c8dcSSimon Schubert 	}
5965796c8dcSSimon Schubert       return call_function_by_hand (argvec[0], nargs, argvec + 1);
5975796c8dcSSimon Schubert     }
598c50c785cSJohn Marino   throw_error (NOT_FOUND_ERROR,
599c50c785cSJohn Marino                _("member function %s not found"), tstr);
600c50c785cSJohn Marino 
6015796c8dcSSimon Schubert   return 0;			/* For lint -- never reached */
6025796c8dcSSimon Schubert }
6035796c8dcSSimon Schubert 
6045796c8dcSSimon Schubert 
6055796c8dcSSimon Schubert /* Concatenate two values with the following conditions:
6065796c8dcSSimon Schubert 
6075796c8dcSSimon Schubert    (1)  Both values must be either bitstring values or character string
6085796c8dcSSimon Schubert    values and the resulting value consists of the concatenation of
6095796c8dcSSimon Schubert    ARG1 followed by ARG2.
6105796c8dcSSimon Schubert 
6115796c8dcSSimon Schubert    or
6125796c8dcSSimon Schubert 
6135796c8dcSSimon Schubert    One value must be an integer value and the other value must be
6145796c8dcSSimon Schubert    either a bitstring value or character string value, which is
6155796c8dcSSimon Schubert    to be repeated by the number of times specified by the integer
6165796c8dcSSimon Schubert    value.
6175796c8dcSSimon Schubert 
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert    (2)  Boolean values are also allowed and are treated as bit string
6205796c8dcSSimon Schubert    values of length 1.
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert    (3)  Character values are also allowed and are treated as character
623c50c785cSJohn Marino    string values of length 1.  */
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert struct value *
value_concat(struct value * arg1,struct value * arg2)6265796c8dcSSimon Schubert value_concat (struct value *arg1, struct value *arg2)
6275796c8dcSSimon Schubert {
6285796c8dcSSimon Schubert   struct value *inval1;
6295796c8dcSSimon Schubert   struct value *inval2;
6305796c8dcSSimon Schubert   struct value *outval = NULL;
6315796c8dcSSimon Schubert   int inval1len, inval2len;
6325796c8dcSSimon Schubert   int count, idx;
6335796c8dcSSimon Schubert   char *ptr;
6345796c8dcSSimon Schubert   char inchar;
6355796c8dcSSimon Schubert   struct type *type1 = check_typedef (value_type (arg1));
6365796c8dcSSimon Schubert   struct type *type2 = check_typedef (value_type (arg2));
6375796c8dcSSimon Schubert   struct type *char_type;
6385796c8dcSSimon Schubert 
6395796c8dcSSimon Schubert   /* First figure out if we are dealing with two values to be concatenated
6405796c8dcSSimon Schubert      or a repeat count and a value to be repeated.  INVAL1 is set to the
6415796c8dcSSimon Schubert      first of two concatenated values, or the repeat count.  INVAL2 is set
6425796c8dcSSimon Schubert      to the second of the two concatenated values or the value to be
6435796c8dcSSimon Schubert      repeated.  */
6445796c8dcSSimon Schubert 
6455796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_INT)
6465796c8dcSSimon Schubert     {
6475796c8dcSSimon Schubert       struct type *tmp = type1;
648cf7f2e2dSJohn Marino 
6495796c8dcSSimon Schubert       type1 = tmp;
6505796c8dcSSimon Schubert       tmp = type2;
6515796c8dcSSimon Schubert       inval1 = arg2;
6525796c8dcSSimon Schubert       inval2 = arg1;
6535796c8dcSSimon Schubert     }
6545796c8dcSSimon Schubert   else
6555796c8dcSSimon Schubert     {
6565796c8dcSSimon Schubert       inval1 = arg1;
6575796c8dcSSimon Schubert       inval2 = arg2;
6585796c8dcSSimon Schubert     }
6595796c8dcSSimon Schubert 
6605796c8dcSSimon Schubert   /* Now process the input values.  */
6615796c8dcSSimon Schubert 
6625796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_INT)
6635796c8dcSSimon Schubert     {
6645796c8dcSSimon Schubert       /* We have a repeat count.  Validate the second value and then
6655796c8dcSSimon Schubert          construct a value repeated that many times.  */
6665796c8dcSSimon Schubert       if (TYPE_CODE (type2) == TYPE_CODE_STRING
6675796c8dcSSimon Schubert 	  || TYPE_CODE (type2) == TYPE_CODE_CHAR)
6685796c8dcSSimon Schubert 	{
669*ef5ccd6cSJohn Marino 	  struct cleanup *back_to;
670*ef5ccd6cSJohn Marino 
6715796c8dcSSimon Schubert 	  count = longest_to_int (value_as_long (inval1));
6725796c8dcSSimon Schubert 	  inval2len = TYPE_LENGTH (type2);
673*ef5ccd6cSJohn Marino 	  ptr = (char *) xmalloc (count * inval2len);
674*ef5ccd6cSJohn Marino 	  back_to = make_cleanup (xfree, ptr);
6755796c8dcSSimon Schubert 	  if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
6765796c8dcSSimon Schubert 	    {
6775796c8dcSSimon Schubert 	      char_type = type2;
678cf7f2e2dSJohn Marino 
6795796c8dcSSimon Schubert 	      inchar = (char) unpack_long (type2,
6805796c8dcSSimon Schubert 					   value_contents (inval2));
6815796c8dcSSimon Schubert 	      for (idx = 0; idx < count; idx++)
6825796c8dcSSimon Schubert 		{
6835796c8dcSSimon Schubert 		  *(ptr + idx) = inchar;
6845796c8dcSSimon Schubert 		}
6855796c8dcSSimon Schubert 	    }
6865796c8dcSSimon Schubert 	  else
6875796c8dcSSimon Schubert 	    {
6885796c8dcSSimon Schubert 	      char_type = TYPE_TARGET_TYPE (type2);
689cf7f2e2dSJohn Marino 
6905796c8dcSSimon Schubert 	      for (idx = 0; idx < count; idx++)
6915796c8dcSSimon Schubert 		{
6925796c8dcSSimon Schubert 		  memcpy (ptr + (idx * inval2len), value_contents (inval2),
6935796c8dcSSimon Schubert 			  inval2len);
6945796c8dcSSimon Schubert 		}
6955796c8dcSSimon Schubert 	    }
6965796c8dcSSimon Schubert 	  outval = value_string (ptr, count * inval2len, char_type);
697*ef5ccd6cSJohn Marino 	  do_cleanups (back_to);
6985796c8dcSSimon Schubert 	}
699*ef5ccd6cSJohn Marino       else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
7005796c8dcSSimon Schubert 	{
701*ef5ccd6cSJohn Marino 	  error (_("unimplemented support for boolean repeats"));
7025796c8dcSSimon Schubert 	}
7035796c8dcSSimon Schubert       else
7045796c8dcSSimon Schubert 	{
7055796c8dcSSimon Schubert 	  error (_("can't repeat values of that type"));
7065796c8dcSSimon Schubert 	}
7075796c8dcSSimon Schubert     }
7085796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
7095796c8dcSSimon Schubert 	   || TYPE_CODE (type1) == TYPE_CODE_CHAR)
7105796c8dcSSimon Schubert     {
711*ef5ccd6cSJohn Marino       struct cleanup *back_to;
712*ef5ccd6cSJohn Marino 
7135796c8dcSSimon Schubert       /* We have two character strings to concatenate.  */
7145796c8dcSSimon Schubert       if (TYPE_CODE (type2) != TYPE_CODE_STRING
7155796c8dcSSimon Schubert 	  && TYPE_CODE (type2) != TYPE_CODE_CHAR)
7165796c8dcSSimon Schubert 	{
7175796c8dcSSimon Schubert 	  error (_("Strings can only be concatenated with other strings."));
7185796c8dcSSimon Schubert 	}
7195796c8dcSSimon Schubert       inval1len = TYPE_LENGTH (type1);
7205796c8dcSSimon Schubert       inval2len = TYPE_LENGTH (type2);
721*ef5ccd6cSJohn Marino       ptr = (char *) xmalloc (inval1len + inval2len);
722*ef5ccd6cSJohn Marino       back_to = make_cleanup (xfree, ptr);
7235796c8dcSSimon Schubert       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
7245796c8dcSSimon Schubert 	{
7255796c8dcSSimon Schubert 	  char_type = type1;
726cf7f2e2dSJohn Marino 
7275796c8dcSSimon Schubert 	  *ptr = (char) unpack_long (type1, value_contents (inval1));
7285796c8dcSSimon Schubert 	}
7295796c8dcSSimon Schubert       else
7305796c8dcSSimon Schubert 	{
7315796c8dcSSimon Schubert 	  char_type = TYPE_TARGET_TYPE (type1);
732cf7f2e2dSJohn Marino 
7335796c8dcSSimon Schubert 	  memcpy (ptr, value_contents (inval1), inval1len);
7345796c8dcSSimon Schubert 	}
7355796c8dcSSimon Schubert       if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
7365796c8dcSSimon Schubert 	{
7375796c8dcSSimon Schubert 	  *(ptr + inval1len) =
7385796c8dcSSimon Schubert 	    (char) unpack_long (type2, value_contents (inval2));
7395796c8dcSSimon Schubert 	}
7405796c8dcSSimon Schubert       else
7415796c8dcSSimon Schubert 	{
7425796c8dcSSimon Schubert 	  memcpy (ptr + inval1len, value_contents (inval2), inval2len);
7435796c8dcSSimon Schubert 	}
7445796c8dcSSimon Schubert       outval = value_string (ptr, inval1len + inval2len, char_type);
745*ef5ccd6cSJohn Marino       do_cleanups (back_to);
7465796c8dcSSimon Schubert     }
747*ef5ccd6cSJohn Marino   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
7485796c8dcSSimon Schubert     {
7495796c8dcSSimon Schubert       /* We have two bitstrings to concatenate.  */
750*ef5ccd6cSJohn Marino       if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
7515796c8dcSSimon Schubert 	{
752*ef5ccd6cSJohn Marino 	  error (_("Booleans can only be concatenated "
753c50c785cSJohn Marino 		   "with other bitstrings or booleans."));
7545796c8dcSSimon Schubert 	}
755*ef5ccd6cSJohn Marino       error (_("unimplemented support for boolean concatenation."));
7565796c8dcSSimon Schubert     }
7575796c8dcSSimon Schubert   else
7585796c8dcSSimon Schubert     {
7595796c8dcSSimon Schubert       /* We don't know how to concatenate these operands.  */
7605796c8dcSSimon Schubert       error (_("illegal operands for concatenation."));
7615796c8dcSSimon Schubert     }
7625796c8dcSSimon Schubert   return (outval);
7635796c8dcSSimon Schubert }
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are
7665796c8dcSSimon Schubert    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
767c50c785cSJohn Marino 
7685796c8dcSSimon Schubert static LONGEST
integer_pow(LONGEST v1,LONGEST v2)7695796c8dcSSimon Schubert integer_pow (LONGEST v1, LONGEST v2)
7705796c8dcSSimon Schubert {
7715796c8dcSSimon Schubert   if (v2 < 0)
7725796c8dcSSimon Schubert     {
7735796c8dcSSimon Schubert       if (v1 == 0)
7745796c8dcSSimon Schubert 	error (_("Attempt to raise 0 to negative power."));
7755796c8dcSSimon Schubert       else
7765796c8dcSSimon Schubert 	return 0;
7775796c8dcSSimon Schubert     }
7785796c8dcSSimon Schubert   else
7795796c8dcSSimon Schubert     {
780c50c785cSJohn Marino       /* The Russian Peasant's Algorithm.  */
7815796c8dcSSimon Schubert       LONGEST v;
7825796c8dcSSimon Schubert 
7835796c8dcSSimon Schubert       v = 1;
7845796c8dcSSimon Schubert       for (;;)
7855796c8dcSSimon Schubert 	{
7865796c8dcSSimon Schubert 	  if (v2 & 1L)
7875796c8dcSSimon Schubert 	    v *= v1;
7885796c8dcSSimon Schubert 	  v2 >>= 1;
7895796c8dcSSimon Schubert 	  if (v2 == 0)
7905796c8dcSSimon Schubert 	    return v;
7915796c8dcSSimon Schubert 	  v1 *= v1;
7925796c8dcSSimon Schubert 	}
7935796c8dcSSimon Schubert     }
7945796c8dcSSimon Schubert }
7955796c8dcSSimon Schubert 
7965796c8dcSSimon Schubert /* Integer exponentiation: V1**V2, where both arguments are
7975796c8dcSSimon Schubert    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
798c50c785cSJohn Marino 
7995796c8dcSSimon Schubert static ULONGEST
uinteger_pow(ULONGEST v1,LONGEST v2)8005796c8dcSSimon Schubert uinteger_pow (ULONGEST v1, LONGEST v2)
8015796c8dcSSimon Schubert {
8025796c8dcSSimon Schubert   if (v2 < 0)
8035796c8dcSSimon Schubert     {
8045796c8dcSSimon Schubert       if (v1 == 0)
8055796c8dcSSimon Schubert 	error (_("Attempt to raise 0 to negative power."));
8065796c8dcSSimon Schubert       else
8075796c8dcSSimon Schubert 	return 0;
8085796c8dcSSimon Schubert     }
8095796c8dcSSimon Schubert   else
8105796c8dcSSimon Schubert     {
811c50c785cSJohn Marino       /* The Russian Peasant's Algorithm.  */
8125796c8dcSSimon Schubert       ULONGEST v;
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert       v = 1;
8155796c8dcSSimon Schubert       for (;;)
8165796c8dcSSimon Schubert 	{
8175796c8dcSSimon Schubert 	  if (v2 & 1L)
8185796c8dcSSimon Schubert 	    v *= v1;
8195796c8dcSSimon Schubert 	  v2 >>= 1;
8205796c8dcSSimon Schubert 	  if (v2 == 0)
8215796c8dcSSimon Schubert 	    return v;
8225796c8dcSSimon Schubert 	  v1 *= v1;
8235796c8dcSSimon Schubert 	}
8245796c8dcSSimon Schubert     }
8255796c8dcSSimon Schubert }
8265796c8dcSSimon Schubert 
8275796c8dcSSimon Schubert /* Obtain decimal value of arguments for binary operation, converting from
8285796c8dcSSimon Schubert    other types if one of them is not decimal floating point.  */
8295796c8dcSSimon Schubert static void
value_args_as_decimal(struct value * arg1,struct value * arg2,gdb_byte * x,int * len_x,enum bfd_endian * byte_order_x,gdb_byte * y,int * len_y,enum bfd_endian * byte_order_y)8305796c8dcSSimon Schubert value_args_as_decimal (struct value *arg1, struct value *arg2,
8315796c8dcSSimon Schubert 		       gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
8325796c8dcSSimon Schubert 		       gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
8335796c8dcSSimon Schubert {
8345796c8dcSSimon Schubert   struct type *type1, *type2;
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
8375796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
8385796c8dcSSimon Schubert 
8395796c8dcSSimon Schubert   /* At least one of the arguments must be of decimal float type.  */
8405796c8dcSSimon Schubert   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
8415796c8dcSSimon Schubert 	      || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
8425796c8dcSSimon Schubert 
8435796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_FLT
8445796c8dcSSimon Schubert       || TYPE_CODE (type2) == TYPE_CODE_FLT)
8455796c8dcSSimon Schubert     /* The DFP extension to the C language does not allow mixing of
8465796c8dcSSimon Schubert      * decimal float types with other float types in expressions
8475796c8dcSSimon Schubert      * (see WDTR 24732, page 12).  */
848c50c785cSJohn Marino     error (_("Mixing decimal floating types with "
849c50c785cSJohn Marino 	     "other floating types is not allowed."));
8505796c8dcSSimon Schubert 
8515796c8dcSSimon Schubert   /* Obtain decimal value of arg1, converting from other types
8525796c8dcSSimon Schubert      if necessary.  */
8535796c8dcSSimon Schubert 
8545796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
8555796c8dcSSimon Schubert     {
8565796c8dcSSimon Schubert       *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
8575796c8dcSSimon Schubert       *len_x = TYPE_LENGTH (type1);
8585796c8dcSSimon Schubert       memcpy (x, value_contents (arg1), *len_x);
8595796c8dcSSimon Schubert     }
8605796c8dcSSimon Schubert   else if (is_integral_type (type1))
8615796c8dcSSimon Schubert     {
8625796c8dcSSimon Schubert       *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
8635796c8dcSSimon Schubert       *len_x = TYPE_LENGTH (type2);
8645796c8dcSSimon Schubert       decimal_from_integral (arg1, x, *len_x, *byte_order_x);
8655796c8dcSSimon Schubert     }
8665796c8dcSSimon Schubert   else
8675796c8dcSSimon Schubert     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
8685796c8dcSSimon Schubert 	     TYPE_NAME (type2));
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert   /* Obtain decimal value of arg2, converting from other types
8715796c8dcSSimon Schubert      if necessary.  */
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
8745796c8dcSSimon Schubert     {
8755796c8dcSSimon Schubert       *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
8765796c8dcSSimon Schubert       *len_y = TYPE_LENGTH (type2);
8775796c8dcSSimon Schubert       memcpy (y, value_contents (arg2), *len_y);
8785796c8dcSSimon Schubert     }
8795796c8dcSSimon Schubert   else if (is_integral_type (type2))
8805796c8dcSSimon Schubert     {
8815796c8dcSSimon Schubert       *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
8825796c8dcSSimon Schubert       *len_y = TYPE_LENGTH (type1);
8835796c8dcSSimon Schubert       decimal_from_integral (arg2, y, *len_y, *byte_order_y);
8845796c8dcSSimon Schubert     }
8855796c8dcSSimon Schubert   else
8865796c8dcSSimon Schubert     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
8875796c8dcSSimon Schubert 	     TYPE_NAME (type2));
8885796c8dcSSimon Schubert }
8895796c8dcSSimon Schubert 
8905796c8dcSSimon Schubert /* Perform a binary operation on two operands which have reasonable
8915796c8dcSSimon Schubert    representations as integers or floats.  This includes booleans,
8925796c8dcSSimon Schubert    characters, integers, or floats.
8935796c8dcSSimon Schubert    Does not support addition and subtraction on pointers;
8945796c8dcSSimon Schubert    use value_ptradd, value_ptrsub or value_ptrdiff for those operations.  */
8955796c8dcSSimon Schubert 
896c50c785cSJohn Marino static struct value *
scalar_binop(struct value * arg1,struct value * arg2,enum exp_opcode op)897c50c785cSJohn Marino scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
8985796c8dcSSimon Schubert {
8995796c8dcSSimon Schubert   struct value *val;
9005796c8dcSSimon Schubert   struct type *type1, *type2, *result_type;
9015796c8dcSSimon Schubert 
9025796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
9035796c8dcSSimon Schubert   arg2 = coerce_ref (arg2);
9045796c8dcSSimon Schubert 
9055796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
9065796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert   if ((TYPE_CODE (type1) != TYPE_CODE_FLT
9095796c8dcSSimon Schubert        && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
9105796c8dcSSimon Schubert        && !is_integral_type (type1))
9115796c8dcSSimon Schubert       || (TYPE_CODE (type2) != TYPE_CODE_FLT
9125796c8dcSSimon Schubert 	  && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
9135796c8dcSSimon Schubert 	  && !is_integral_type (type2)))
9145796c8dcSSimon Schubert     error (_("Argument to arithmetic operation not a number or boolean."));
9155796c8dcSSimon Schubert 
9165796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
9175796c8dcSSimon Schubert       || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
9185796c8dcSSimon Schubert     {
9195796c8dcSSimon Schubert       int len_v1, len_v2, len_v;
9205796c8dcSSimon Schubert       enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
9215796c8dcSSimon Schubert       gdb_byte v1[16], v2[16];
9225796c8dcSSimon Schubert       gdb_byte v[16];
9235796c8dcSSimon Schubert 
9245796c8dcSSimon Schubert       /* If only one type is decimal float, use its type.
9255796c8dcSSimon Schubert 	 Otherwise use the bigger type.  */
9265796c8dcSSimon Schubert       if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
9275796c8dcSSimon Schubert 	result_type = type2;
9285796c8dcSSimon Schubert       else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
9295796c8dcSSimon Schubert 	result_type = type1;
9305796c8dcSSimon Schubert       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
9315796c8dcSSimon Schubert 	result_type = type2;
9325796c8dcSSimon Schubert       else
9335796c8dcSSimon Schubert 	result_type = type1;
9345796c8dcSSimon Schubert 
9355796c8dcSSimon Schubert       len_v = TYPE_LENGTH (result_type);
9365796c8dcSSimon Schubert       byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
9375796c8dcSSimon Schubert 
9385796c8dcSSimon Schubert       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
9395796c8dcSSimon Schubert 					 v2, &len_v2, &byte_order_v2);
9405796c8dcSSimon Schubert 
9415796c8dcSSimon Schubert       switch (op)
9425796c8dcSSimon Schubert 	{
9435796c8dcSSimon Schubert 	case BINOP_ADD:
9445796c8dcSSimon Schubert 	case BINOP_SUB:
9455796c8dcSSimon Schubert 	case BINOP_MUL:
9465796c8dcSSimon Schubert 	case BINOP_DIV:
9475796c8dcSSimon Schubert 	case BINOP_EXP:
9485796c8dcSSimon Schubert 	  decimal_binop (op, v1, len_v1, byte_order_v1,
9495796c8dcSSimon Schubert 			     v2, len_v2, byte_order_v2,
9505796c8dcSSimon Schubert 			     v, len_v, byte_order_v);
9515796c8dcSSimon Schubert 	  break;
9525796c8dcSSimon Schubert 
9535796c8dcSSimon Schubert 	default:
9545796c8dcSSimon Schubert 	  error (_("Operation not valid for decimal floating point number."));
9555796c8dcSSimon Schubert 	}
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert       val = value_from_decfloat (result_type, v);
9585796c8dcSSimon Schubert     }
9595796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_FLT
9605796c8dcSSimon Schubert 	   || TYPE_CODE (type2) == TYPE_CODE_FLT)
9615796c8dcSSimon Schubert     {
9625796c8dcSSimon Schubert       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
9635796c8dcSSimon Schubert          in target format.  real.c in GCC probably has the necessary
9645796c8dcSSimon Schubert          code.  */
9655796c8dcSSimon Schubert       DOUBLEST v1, v2, v = 0;
966cf7f2e2dSJohn Marino 
9675796c8dcSSimon Schubert       v1 = value_as_double (arg1);
9685796c8dcSSimon Schubert       v2 = value_as_double (arg2);
9695796c8dcSSimon Schubert 
9705796c8dcSSimon Schubert       switch (op)
9715796c8dcSSimon Schubert 	{
9725796c8dcSSimon Schubert 	case BINOP_ADD:
9735796c8dcSSimon Schubert 	  v = v1 + v2;
9745796c8dcSSimon Schubert 	  break;
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert 	case BINOP_SUB:
9775796c8dcSSimon Schubert 	  v = v1 - v2;
9785796c8dcSSimon Schubert 	  break;
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert 	case BINOP_MUL:
9815796c8dcSSimon Schubert 	  v = v1 * v2;
9825796c8dcSSimon Schubert 	  break;
9835796c8dcSSimon Schubert 
9845796c8dcSSimon Schubert 	case BINOP_DIV:
9855796c8dcSSimon Schubert 	  v = v1 / v2;
9865796c8dcSSimon Schubert 	  break;
9875796c8dcSSimon Schubert 
9885796c8dcSSimon Schubert 	case BINOP_EXP:
9895796c8dcSSimon Schubert 	  errno = 0;
9905796c8dcSSimon Schubert 	  v = pow (v1, v2);
9915796c8dcSSimon Schubert 	  if (errno)
992c50c785cSJohn Marino 	    error (_("Cannot perform exponentiation: %s"),
993c50c785cSJohn Marino 		   safe_strerror (errno));
9945796c8dcSSimon Schubert 	  break;
9955796c8dcSSimon Schubert 
9965796c8dcSSimon Schubert 	case BINOP_MIN:
9975796c8dcSSimon Schubert 	  v = v1 < v2 ? v1 : v2;
9985796c8dcSSimon Schubert 	  break;
9995796c8dcSSimon Schubert 
10005796c8dcSSimon Schubert 	case BINOP_MAX:
10015796c8dcSSimon Schubert 	  v = v1 > v2 ? v1 : v2;
10025796c8dcSSimon Schubert 	  break;
10035796c8dcSSimon Schubert 
10045796c8dcSSimon Schubert 	default:
10055796c8dcSSimon Schubert 	  error (_("Integer-only operation on floating point number."));
10065796c8dcSSimon Schubert 	}
10075796c8dcSSimon Schubert 
10085796c8dcSSimon Schubert       /* If only one type is float, use its type.
10095796c8dcSSimon Schubert 	 Otherwise use the bigger type.  */
10105796c8dcSSimon Schubert       if (TYPE_CODE (type1) != TYPE_CODE_FLT)
10115796c8dcSSimon Schubert 	result_type = type2;
10125796c8dcSSimon Schubert       else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
10135796c8dcSSimon Schubert 	result_type = type1;
10145796c8dcSSimon Schubert       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
10155796c8dcSSimon Schubert 	result_type = type2;
10165796c8dcSSimon Schubert       else
10175796c8dcSSimon Schubert 	result_type = type1;
10185796c8dcSSimon Schubert 
10195796c8dcSSimon Schubert       val = allocate_value (result_type);
10205796c8dcSSimon Schubert       store_typed_floating (value_contents_raw (val), value_type (val), v);
10215796c8dcSSimon Schubert     }
10225796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
10235796c8dcSSimon Schubert 	   || TYPE_CODE (type2) == TYPE_CODE_BOOL)
10245796c8dcSSimon Schubert     {
10255796c8dcSSimon Schubert       LONGEST v1, v2, v = 0;
1026cf7f2e2dSJohn Marino 
10275796c8dcSSimon Schubert       v1 = value_as_long (arg1);
10285796c8dcSSimon Schubert       v2 = value_as_long (arg2);
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert       switch (op)
10315796c8dcSSimon Schubert 	{
10325796c8dcSSimon Schubert 	case BINOP_BITWISE_AND:
10335796c8dcSSimon Schubert 	  v = v1 & v2;
10345796c8dcSSimon Schubert 	  break;
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert 	case BINOP_BITWISE_IOR:
10375796c8dcSSimon Schubert 	  v = v1 | v2;
10385796c8dcSSimon Schubert 	  break;
10395796c8dcSSimon Schubert 
10405796c8dcSSimon Schubert 	case BINOP_BITWISE_XOR:
10415796c8dcSSimon Schubert 	  v = v1 ^ v2;
10425796c8dcSSimon Schubert           break;
10435796c8dcSSimon Schubert 
10445796c8dcSSimon Schubert         case BINOP_EQUAL:
10455796c8dcSSimon Schubert           v = v1 == v2;
10465796c8dcSSimon Schubert           break;
10475796c8dcSSimon Schubert 
10485796c8dcSSimon Schubert         case BINOP_NOTEQUAL:
10495796c8dcSSimon Schubert           v = v1 != v2;
10505796c8dcSSimon Schubert 	  break;
10515796c8dcSSimon Schubert 
10525796c8dcSSimon Schubert 	default:
10535796c8dcSSimon Schubert 	  error (_("Invalid operation on booleans."));
10545796c8dcSSimon Schubert 	}
10555796c8dcSSimon Schubert 
10565796c8dcSSimon Schubert       result_type = type1;
10575796c8dcSSimon Schubert 
10585796c8dcSSimon Schubert       val = allocate_value (result_type);
10595796c8dcSSimon Schubert       store_signed_integer (value_contents_raw (val),
10605796c8dcSSimon Schubert 			    TYPE_LENGTH (result_type),
10615796c8dcSSimon Schubert 			    gdbarch_byte_order (get_type_arch (result_type)),
10625796c8dcSSimon Schubert 			    v);
10635796c8dcSSimon Schubert     }
10645796c8dcSSimon Schubert   else
10655796c8dcSSimon Schubert     /* Integral operations here.  */
10665796c8dcSSimon Schubert     {
10675796c8dcSSimon Schubert       /* Determine type length of the result, and if the operation should
10685796c8dcSSimon Schubert 	 be done unsigned.  For exponentiation and shift operators,
10695796c8dcSSimon Schubert 	 use the length and type of the left operand.  Otherwise,
10705796c8dcSSimon Schubert 	 use the signedness of the operand with the greater length.
10715796c8dcSSimon Schubert 	 If both operands are of equal length, use unsigned operation
10725796c8dcSSimon Schubert 	 if one of the operands is unsigned.  */
10735796c8dcSSimon Schubert       if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
10745796c8dcSSimon Schubert 	result_type = type1;
10755796c8dcSSimon Schubert       else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
10765796c8dcSSimon Schubert 	result_type = type1;
10775796c8dcSSimon Schubert       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
10785796c8dcSSimon Schubert 	result_type = type2;
10795796c8dcSSimon Schubert       else if (TYPE_UNSIGNED (type1))
10805796c8dcSSimon Schubert 	result_type = type1;
10815796c8dcSSimon Schubert       else if (TYPE_UNSIGNED (type2))
10825796c8dcSSimon Schubert 	result_type = type2;
10835796c8dcSSimon Schubert       else
10845796c8dcSSimon Schubert 	result_type = type1;
10855796c8dcSSimon Schubert 
10865796c8dcSSimon Schubert       if (TYPE_UNSIGNED (result_type))
10875796c8dcSSimon Schubert 	{
10885796c8dcSSimon Schubert 	  LONGEST v2_signed = value_as_long (arg2);
10895796c8dcSSimon Schubert 	  ULONGEST v1, v2, v = 0;
1090cf7f2e2dSJohn Marino 
10915796c8dcSSimon Schubert 	  v1 = (ULONGEST) value_as_long (arg1);
10925796c8dcSSimon Schubert 	  v2 = (ULONGEST) v2_signed;
10935796c8dcSSimon Schubert 
10945796c8dcSSimon Schubert 	  switch (op)
10955796c8dcSSimon Schubert 	    {
10965796c8dcSSimon Schubert 	    case BINOP_ADD:
10975796c8dcSSimon Schubert 	      v = v1 + v2;
10985796c8dcSSimon Schubert 	      break;
10995796c8dcSSimon Schubert 
11005796c8dcSSimon Schubert 	    case BINOP_SUB:
11015796c8dcSSimon Schubert 	      v = v1 - v2;
11025796c8dcSSimon Schubert 	      break;
11035796c8dcSSimon Schubert 
11045796c8dcSSimon Schubert 	    case BINOP_MUL:
11055796c8dcSSimon Schubert 	      v = v1 * v2;
11065796c8dcSSimon Schubert 	      break;
11075796c8dcSSimon Schubert 
11085796c8dcSSimon Schubert 	    case BINOP_DIV:
11095796c8dcSSimon Schubert 	    case BINOP_INTDIV:
11105796c8dcSSimon Schubert 	      if (v2 != 0)
11115796c8dcSSimon Schubert 		v = v1 / v2;
11125796c8dcSSimon Schubert 	      else
11135796c8dcSSimon Schubert 		error (_("Division by zero"));
11145796c8dcSSimon Schubert 	      break;
11155796c8dcSSimon Schubert 
11165796c8dcSSimon Schubert 	    case BINOP_EXP:
11175796c8dcSSimon Schubert               v = uinteger_pow (v1, v2_signed);
11185796c8dcSSimon Schubert 	      break;
11195796c8dcSSimon Schubert 
11205796c8dcSSimon Schubert 	    case BINOP_REM:
11215796c8dcSSimon Schubert 	      if (v2 != 0)
11225796c8dcSSimon Schubert 		v = v1 % v2;
11235796c8dcSSimon Schubert 	      else
11245796c8dcSSimon Schubert 		error (_("Division by zero"));
11255796c8dcSSimon Schubert 	      break;
11265796c8dcSSimon Schubert 
11275796c8dcSSimon Schubert 	    case BINOP_MOD:
11285796c8dcSSimon Schubert 	      /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
11295796c8dcSSimon Schubert 	         v1 mod 0 has a defined value, v1.  */
11305796c8dcSSimon Schubert 	      if (v2 == 0)
11315796c8dcSSimon Schubert 		{
11325796c8dcSSimon Schubert 		  v = v1;
11335796c8dcSSimon Schubert 		}
11345796c8dcSSimon Schubert 	      else
11355796c8dcSSimon Schubert 		{
11365796c8dcSSimon Schubert 		  v = v1 / v2;
11375796c8dcSSimon Schubert 		  /* Note floor(v1/v2) == v1/v2 for unsigned.  */
11385796c8dcSSimon Schubert 		  v = v1 - (v2 * v);
11395796c8dcSSimon Schubert 		}
11405796c8dcSSimon Schubert 	      break;
11415796c8dcSSimon Schubert 
11425796c8dcSSimon Schubert 	    case BINOP_LSH:
11435796c8dcSSimon Schubert 	      v = v1 << v2;
11445796c8dcSSimon Schubert 	      break;
11455796c8dcSSimon Schubert 
11465796c8dcSSimon Schubert 	    case BINOP_RSH:
11475796c8dcSSimon Schubert 	      v = v1 >> v2;
11485796c8dcSSimon Schubert 	      break;
11495796c8dcSSimon Schubert 
11505796c8dcSSimon Schubert 	    case BINOP_BITWISE_AND:
11515796c8dcSSimon Schubert 	      v = v1 & v2;
11525796c8dcSSimon Schubert 	      break;
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert 	    case BINOP_BITWISE_IOR:
11555796c8dcSSimon Schubert 	      v = v1 | v2;
11565796c8dcSSimon Schubert 	      break;
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert 	    case BINOP_BITWISE_XOR:
11595796c8dcSSimon Schubert 	      v = v1 ^ v2;
11605796c8dcSSimon Schubert 	      break;
11615796c8dcSSimon Schubert 
11625796c8dcSSimon Schubert 	    case BINOP_LOGICAL_AND:
11635796c8dcSSimon Schubert 	      v = v1 && v2;
11645796c8dcSSimon Schubert 	      break;
11655796c8dcSSimon Schubert 
11665796c8dcSSimon Schubert 	    case BINOP_LOGICAL_OR:
11675796c8dcSSimon Schubert 	      v = v1 || v2;
11685796c8dcSSimon Schubert 	      break;
11695796c8dcSSimon Schubert 
11705796c8dcSSimon Schubert 	    case BINOP_MIN:
11715796c8dcSSimon Schubert 	      v = v1 < v2 ? v1 : v2;
11725796c8dcSSimon Schubert 	      break;
11735796c8dcSSimon Schubert 
11745796c8dcSSimon Schubert 	    case BINOP_MAX:
11755796c8dcSSimon Schubert 	      v = v1 > v2 ? v1 : v2;
11765796c8dcSSimon Schubert 	      break;
11775796c8dcSSimon Schubert 
11785796c8dcSSimon Schubert 	    case BINOP_EQUAL:
11795796c8dcSSimon Schubert 	      v = v1 == v2;
11805796c8dcSSimon Schubert 	      break;
11815796c8dcSSimon Schubert 
11825796c8dcSSimon Schubert             case BINOP_NOTEQUAL:
11835796c8dcSSimon Schubert               v = v1 != v2;
11845796c8dcSSimon Schubert               break;
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert 	    case BINOP_LESS:
11875796c8dcSSimon Schubert 	      v = v1 < v2;
11885796c8dcSSimon Schubert 	      break;
11895796c8dcSSimon Schubert 
1190cf7f2e2dSJohn Marino 	    case BINOP_GTR:
1191cf7f2e2dSJohn Marino 	      v = v1 > v2;
1192cf7f2e2dSJohn Marino 	      break;
1193cf7f2e2dSJohn Marino 
1194cf7f2e2dSJohn Marino 	    case BINOP_LEQ:
1195cf7f2e2dSJohn Marino 	      v = v1 <= v2;
1196cf7f2e2dSJohn Marino 	      break;
1197cf7f2e2dSJohn Marino 
1198cf7f2e2dSJohn Marino 	    case BINOP_GEQ:
1199cf7f2e2dSJohn Marino 	      v = v1 >= v2;
1200cf7f2e2dSJohn Marino 	      break;
1201cf7f2e2dSJohn Marino 
12025796c8dcSSimon Schubert 	    default:
12035796c8dcSSimon Schubert 	      error (_("Invalid binary operation on numbers."));
12045796c8dcSSimon Schubert 	    }
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert 	  val = allocate_value (result_type);
12075796c8dcSSimon Schubert 	  store_unsigned_integer (value_contents_raw (val),
12085796c8dcSSimon Schubert 				  TYPE_LENGTH (value_type (val)),
12095796c8dcSSimon Schubert 				  gdbarch_byte_order
12105796c8dcSSimon Schubert 				    (get_type_arch (result_type)),
12115796c8dcSSimon Schubert 				  v);
12125796c8dcSSimon Schubert 	}
12135796c8dcSSimon Schubert       else
12145796c8dcSSimon Schubert 	{
12155796c8dcSSimon Schubert 	  LONGEST v1, v2, v = 0;
1216cf7f2e2dSJohn Marino 
12175796c8dcSSimon Schubert 	  v1 = value_as_long (arg1);
12185796c8dcSSimon Schubert 	  v2 = value_as_long (arg2);
12195796c8dcSSimon Schubert 
12205796c8dcSSimon Schubert 	  switch (op)
12215796c8dcSSimon Schubert 	    {
12225796c8dcSSimon Schubert 	    case BINOP_ADD:
12235796c8dcSSimon Schubert 	      v = v1 + v2;
12245796c8dcSSimon Schubert 	      break;
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert 	    case BINOP_SUB:
12275796c8dcSSimon Schubert 	      v = v1 - v2;
12285796c8dcSSimon Schubert 	      break;
12295796c8dcSSimon Schubert 
12305796c8dcSSimon Schubert 	    case BINOP_MUL:
12315796c8dcSSimon Schubert 	      v = v1 * v2;
12325796c8dcSSimon Schubert 	      break;
12335796c8dcSSimon Schubert 
12345796c8dcSSimon Schubert 	    case BINOP_DIV:
12355796c8dcSSimon Schubert 	    case BINOP_INTDIV:
12365796c8dcSSimon Schubert 	      if (v2 != 0)
12375796c8dcSSimon Schubert 		v = v1 / v2;
12385796c8dcSSimon Schubert 	      else
12395796c8dcSSimon Schubert 		error (_("Division by zero"));
12405796c8dcSSimon Schubert               break;
12415796c8dcSSimon Schubert 
12425796c8dcSSimon Schubert 	    case BINOP_EXP:
12435796c8dcSSimon Schubert               v = integer_pow (v1, v2);
12445796c8dcSSimon Schubert 	      break;
12455796c8dcSSimon Schubert 
12465796c8dcSSimon Schubert 	    case BINOP_REM:
12475796c8dcSSimon Schubert 	      if (v2 != 0)
12485796c8dcSSimon Schubert 		v = v1 % v2;
12495796c8dcSSimon Schubert 	      else
12505796c8dcSSimon Schubert 		error (_("Division by zero"));
12515796c8dcSSimon Schubert 	      break;
12525796c8dcSSimon Schubert 
12535796c8dcSSimon Schubert 	    case BINOP_MOD:
12545796c8dcSSimon Schubert 	      /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
12555796c8dcSSimon Schubert 	         X mod 0 has a defined value, X.  */
12565796c8dcSSimon Schubert 	      if (v2 == 0)
12575796c8dcSSimon Schubert 		{
12585796c8dcSSimon Schubert 		  v = v1;
12595796c8dcSSimon Schubert 		}
12605796c8dcSSimon Schubert 	      else
12615796c8dcSSimon Schubert 		{
12625796c8dcSSimon Schubert 		  v = v1 / v2;
12635796c8dcSSimon Schubert 		  /* Compute floor.  */
12645796c8dcSSimon Schubert 		  if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
12655796c8dcSSimon Schubert 		    {
12665796c8dcSSimon Schubert 		      v--;
12675796c8dcSSimon Schubert 		    }
12685796c8dcSSimon Schubert 		  v = v1 - (v2 * v);
12695796c8dcSSimon Schubert 		}
12705796c8dcSSimon Schubert 	      break;
12715796c8dcSSimon Schubert 
12725796c8dcSSimon Schubert 	    case BINOP_LSH:
12735796c8dcSSimon Schubert 	      v = v1 << v2;
12745796c8dcSSimon Schubert 	      break;
12755796c8dcSSimon Schubert 
12765796c8dcSSimon Schubert 	    case BINOP_RSH:
12775796c8dcSSimon Schubert 	      v = v1 >> v2;
12785796c8dcSSimon Schubert 	      break;
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert 	    case BINOP_BITWISE_AND:
12815796c8dcSSimon Schubert 	      v = v1 & v2;
12825796c8dcSSimon Schubert 	      break;
12835796c8dcSSimon Schubert 
12845796c8dcSSimon Schubert 	    case BINOP_BITWISE_IOR:
12855796c8dcSSimon Schubert 	      v = v1 | v2;
12865796c8dcSSimon Schubert 	      break;
12875796c8dcSSimon Schubert 
12885796c8dcSSimon Schubert 	    case BINOP_BITWISE_XOR:
12895796c8dcSSimon Schubert 	      v = v1 ^ v2;
12905796c8dcSSimon Schubert 	      break;
12915796c8dcSSimon Schubert 
12925796c8dcSSimon Schubert 	    case BINOP_LOGICAL_AND:
12935796c8dcSSimon Schubert 	      v = v1 && v2;
12945796c8dcSSimon Schubert 	      break;
12955796c8dcSSimon Schubert 
12965796c8dcSSimon Schubert 	    case BINOP_LOGICAL_OR:
12975796c8dcSSimon Schubert 	      v = v1 || v2;
12985796c8dcSSimon Schubert 	      break;
12995796c8dcSSimon Schubert 
13005796c8dcSSimon Schubert 	    case BINOP_MIN:
13015796c8dcSSimon Schubert 	      v = v1 < v2 ? v1 : v2;
13025796c8dcSSimon Schubert 	      break;
13035796c8dcSSimon Schubert 
13045796c8dcSSimon Schubert 	    case BINOP_MAX:
13055796c8dcSSimon Schubert 	      v = v1 > v2 ? v1 : v2;
13065796c8dcSSimon Schubert 	      break;
13075796c8dcSSimon Schubert 
13085796c8dcSSimon Schubert 	    case BINOP_EQUAL:
13095796c8dcSSimon Schubert 	      v = v1 == v2;
13105796c8dcSSimon Schubert 	      break;
13115796c8dcSSimon Schubert 
1312cf7f2e2dSJohn Marino             case BINOP_NOTEQUAL:
1313cf7f2e2dSJohn Marino               v = v1 != v2;
1314cf7f2e2dSJohn Marino               break;
1315cf7f2e2dSJohn Marino 
13165796c8dcSSimon Schubert 	    case BINOP_LESS:
13175796c8dcSSimon Schubert 	      v = v1 < v2;
13185796c8dcSSimon Schubert 	      break;
13195796c8dcSSimon Schubert 
1320cf7f2e2dSJohn Marino 	    case BINOP_GTR:
1321cf7f2e2dSJohn Marino 	      v = v1 > v2;
1322cf7f2e2dSJohn Marino 	      break;
1323cf7f2e2dSJohn Marino 
1324cf7f2e2dSJohn Marino 	    case BINOP_LEQ:
1325cf7f2e2dSJohn Marino 	      v = v1 <= v2;
1326cf7f2e2dSJohn Marino 	      break;
1327cf7f2e2dSJohn Marino 
1328cf7f2e2dSJohn Marino 	    case BINOP_GEQ:
1329cf7f2e2dSJohn Marino 	      v = v1 >= v2;
1330cf7f2e2dSJohn Marino 	      break;
1331cf7f2e2dSJohn Marino 
13325796c8dcSSimon Schubert 	    default:
13335796c8dcSSimon Schubert 	      error (_("Invalid binary operation on numbers."));
13345796c8dcSSimon Schubert 	    }
13355796c8dcSSimon Schubert 
13365796c8dcSSimon Schubert 	  val = allocate_value (result_type);
13375796c8dcSSimon Schubert 	  store_signed_integer (value_contents_raw (val),
13385796c8dcSSimon Schubert 				TYPE_LENGTH (value_type (val)),
13395796c8dcSSimon Schubert 				gdbarch_byte_order
13405796c8dcSSimon Schubert 				  (get_type_arch (result_type)),
13415796c8dcSSimon Schubert 				v);
13425796c8dcSSimon Schubert 	}
13435796c8dcSSimon Schubert     }
13445796c8dcSSimon Schubert 
13455796c8dcSSimon Schubert   return val;
13465796c8dcSSimon Schubert }
1347c50c785cSJohn Marino 
1348*ef5ccd6cSJohn Marino /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
1349*ef5ccd6cSJohn Marino    replicating SCALAR_VALUE for each element of the vector.  Only scalar
1350*ef5ccd6cSJohn Marino    types that can be cast to the type of one element of the vector are
1351*ef5ccd6cSJohn Marino    acceptable.  The newly created vector value is returned upon success,
1352*ef5ccd6cSJohn Marino    otherwise an error is thrown.  */
1353*ef5ccd6cSJohn Marino 
1354*ef5ccd6cSJohn Marino struct value *
value_vector_widen(struct value * scalar_value,struct type * vector_type)1355*ef5ccd6cSJohn Marino value_vector_widen (struct value *scalar_value, struct type *vector_type)
1356*ef5ccd6cSJohn Marino {
1357*ef5ccd6cSJohn Marino   /* Widen the scalar to a vector.  */
1358*ef5ccd6cSJohn Marino   struct type *eltype, *scalar_type;
1359*ef5ccd6cSJohn Marino   struct value *val, *elval;
1360*ef5ccd6cSJohn Marino   LONGEST low_bound, high_bound;
1361*ef5ccd6cSJohn Marino   int i;
1362*ef5ccd6cSJohn Marino 
1363*ef5ccd6cSJohn Marino   CHECK_TYPEDEF (vector_type);
1364*ef5ccd6cSJohn Marino 
1365*ef5ccd6cSJohn Marino   gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
1366*ef5ccd6cSJohn Marino 	      && TYPE_VECTOR (vector_type));
1367*ef5ccd6cSJohn Marino 
1368*ef5ccd6cSJohn Marino   if (!get_array_bounds (vector_type, &low_bound, &high_bound))
1369*ef5ccd6cSJohn Marino     error (_("Could not determine the vector bounds"));
1370*ef5ccd6cSJohn Marino 
1371*ef5ccd6cSJohn Marino   eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
1372*ef5ccd6cSJohn Marino   elval = value_cast (eltype, scalar_value);
1373*ef5ccd6cSJohn Marino 
1374*ef5ccd6cSJohn Marino   scalar_type = check_typedef (value_type (scalar_value));
1375*ef5ccd6cSJohn Marino 
1376*ef5ccd6cSJohn Marino   /* If we reduced the length of the scalar then check we didn't loose any
1377*ef5ccd6cSJohn Marino      important bits.  */
1378*ef5ccd6cSJohn Marino   if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
1379*ef5ccd6cSJohn Marino       && !value_equal (elval, scalar_value))
1380*ef5ccd6cSJohn Marino     error (_("conversion of scalar to vector involves truncation"));
1381*ef5ccd6cSJohn Marino 
1382*ef5ccd6cSJohn Marino   val = allocate_value (vector_type);
1383*ef5ccd6cSJohn Marino   for (i = 0; i < high_bound - low_bound + 1; i++)
1384*ef5ccd6cSJohn Marino     /* Duplicate the contents of elval into the destination vector.  */
1385*ef5ccd6cSJohn Marino     memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
1386*ef5ccd6cSJohn Marino 	    value_contents_all (elval), TYPE_LENGTH (eltype));
1387*ef5ccd6cSJohn Marino 
1388*ef5ccd6cSJohn Marino   return val;
1389*ef5ccd6cSJohn Marino }
1390*ef5ccd6cSJohn Marino 
1391c50c785cSJohn Marino /* Performs a binary operation on two vector operands by calling scalar_binop
1392c50c785cSJohn Marino    for each pair of vector components.  */
1393c50c785cSJohn Marino 
1394c50c785cSJohn Marino static struct value *
vector_binop(struct value * val1,struct value * val2,enum exp_opcode op)1395c50c785cSJohn Marino vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
1396c50c785cSJohn Marino {
1397c50c785cSJohn Marino   struct value *val, *tmp, *mark;
1398*ef5ccd6cSJohn Marino   struct type *type1, *type2, *eltype1, *eltype2;
1399c50c785cSJohn Marino   int t1_is_vec, t2_is_vec, elsize, i;
1400c50c785cSJohn Marino   LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
1401c50c785cSJohn Marino 
1402c50c785cSJohn Marino   type1 = check_typedef (value_type (val1));
1403c50c785cSJohn Marino   type2 = check_typedef (value_type (val2));
1404c50c785cSJohn Marino 
1405c50c785cSJohn Marino   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1406c50c785cSJohn Marino 	       && TYPE_VECTOR (type1)) ? 1 : 0;
1407c50c785cSJohn Marino   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1408c50c785cSJohn Marino 	       && TYPE_VECTOR (type2)) ? 1 : 0;
1409c50c785cSJohn Marino 
1410c50c785cSJohn Marino   if (!t1_is_vec || !t2_is_vec)
1411c50c785cSJohn Marino     error (_("Vector operations are only supported among vectors"));
1412c50c785cSJohn Marino 
1413c50c785cSJohn Marino   if (!get_array_bounds (type1, &low_bound1, &high_bound1)
1414c50c785cSJohn Marino       || !get_array_bounds (type2, &low_bound2, &high_bound2))
1415c50c785cSJohn Marino     error (_("Could not determine the vector bounds"));
1416c50c785cSJohn Marino 
1417c50c785cSJohn Marino   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
1418c50c785cSJohn Marino   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
1419c50c785cSJohn Marino   elsize = TYPE_LENGTH (eltype1);
1420c50c785cSJohn Marino 
1421c50c785cSJohn Marino   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
1422c50c785cSJohn Marino       || elsize != TYPE_LENGTH (eltype2)
1423c50c785cSJohn Marino       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
1424c50c785cSJohn Marino       || low_bound1 != low_bound2 || high_bound1 != high_bound2)
1425c50c785cSJohn Marino     error (_("Cannot perform operation on vectors with different types"));
1426c50c785cSJohn Marino 
1427c50c785cSJohn Marino   val = allocate_value (type1);
1428c50c785cSJohn Marino   mark = value_mark ();
1429c50c785cSJohn Marino   for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
1430c50c785cSJohn Marino     {
1431c50c785cSJohn Marino       tmp = value_binop (value_subscript (val1, i),
1432c50c785cSJohn Marino 			 value_subscript (val2, i), op);
1433c50c785cSJohn Marino       memcpy (value_contents_writeable (val) + i * elsize,
1434c50c785cSJohn Marino 	      value_contents_all (tmp),
1435c50c785cSJohn Marino 	      elsize);
1436c50c785cSJohn Marino      }
1437c50c785cSJohn Marino   value_free_to_mark (mark);
1438c50c785cSJohn Marino 
1439c50c785cSJohn Marino   return val;
1440c50c785cSJohn Marino }
1441c50c785cSJohn Marino 
1442c50c785cSJohn Marino /* Perform a binary operation on two operands.  */
1443c50c785cSJohn Marino 
1444c50c785cSJohn Marino struct value *
value_binop(struct value * arg1,struct value * arg2,enum exp_opcode op)1445c50c785cSJohn Marino value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
1446c50c785cSJohn Marino {
1447c50c785cSJohn Marino   struct value *val;
1448c50c785cSJohn Marino   struct type *type1 = check_typedef (value_type (arg1));
1449c50c785cSJohn Marino   struct type *type2 = check_typedef (value_type (arg2));
1450c50c785cSJohn Marino   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
1451c50c785cSJohn Marino 		   && TYPE_VECTOR (type1));
1452c50c785cSJohn Marino   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
1453c50c785cSJohn Marino 		   && TYPE_VECTOR (type2));
1454c50c785cSJohn Marino 
1455c50c785cSJohn Marino   if (!t1_is_vec && !t2_is_vec)
1456c50c785cSJohn Marino     val = scalar_binop (arg1, arg2, op);
1457c50c785cSJohn Marino   else if (t1_is_vec && t2_is_vec)
1458c50c785cSJohn Marino     val = vector_binop (arg1, arg2, op);
1459c50c785cSJohn Marino   else
1460c50c785cSJohn Marino     {
1461c50c785cSJohn Marino       /* Widen the scalar operand to a vector.  */
1462c50c785cSJohn Marino       struct value **v = t1_is_vec ? &arg2 : &arg1;
1463c50c785cSJohn Marino       struct type *t = t1_is_vec ? type2 : type1;
1464c50c785cSJohn Marino 
1465c50c785cSJohn Marino       if (TYPE_CODE (t) != TYPE_CODE_FLT
1466c50c785cSJohn Marino 	  && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
1467c50c785cSJohn Marino 	  && !is_integral_type (t))
1468c50c785cSJohn Marino 	error (_("Argument to operation not a number or boolean."));
1469c50c785cSJohn Marino 
1470*ef5ccd6cSJohn Marino       /* Replicate the scalar value to make a vector value.  */
1471*ef5ccd6cSJohn Marino       *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
1472*ef5ccd6cSJohn Marino 
1473c50c785cSJohn Marino       val = vector_binop (arg1, arg2, op);
1474c50c785cSJohn Marino     }
1475c50c785cSJohn Marino 
1476c50c785cSJohn Marino   return val;
1477c50c785cSJohn Marino }
14785796c8dcSSimon Schubert 
14795796c8dcSSimon Schubert /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
14805796c8dcSSimon Schubert 
14815796c8dcSSimon Schubert int
value_logical_not(struct value * arg1)14825796c8dcSSimon Schubert value_logical_not (struct value *arg1)
14835796c8dcSSimon Schubert {
14845796c8dcSSimon Schubert   int len;
14855796c8dcSSimon Schubert   const gdb_byte *p;
14865796c8dcSSimon Schubert   struct type *type1;
14875796c8dcSSimon Schubert 
14885796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
14895796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
14905796c8dcSSimon Schubert 
14915796c8dcSSimon Schubert   if (TYPE_CODE (type1) == TYPE_CODE_FLT)
14925796c8dcSSimon Schubert     return 0 == value_as_double (arg1);
14935796c8dcSSimon Schubert   else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
14945796c8dcSSimon Schubert     return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
14955796c8dcSSimon Schubert 			    gdbarch_byte_order (get_type_arch (type1)));
14965796c8dcSSimon Schubert 
14975796c8dcSSimon Schubert   len = TYPE_LENGTH (type1);
14985796c8dcSSimon Schubert   p = value_contents (arg1);
14995796c8dcSSimon Schubert 
15005796c8dcSSimon Schubert   while (--len >= 0)
15015796c8dcSSimon Schubert     {
15025796c8dcSSimon Schubert       if (*p++)
15035796c8dcSSimon Schubert 	break;
15045796c8dcSSimon Schubert     }
15055796c8dcSSimon Schubert 
15065796c8dcSSimon Schubert   return len < 0;
15075796c8dcSSimon Schubert }
15085796c8dcSSimon Schubert 
15095796c8dcSSimon Schubert /* Perform a comparison on two string values (whose content are not
1510c50c785cSJohn Marino    necessarily null terminated) based on their length.  */
15115796c8dcSSimon Schubert 
15125796c8dcSSimon Schubert static int
value_strcmp(struct value * arg1,struct value * arg2)15135796c8dcSSimon Schubert value_strcmp (struct value *arg1, struct value *arg2)
15145796c8dcSSimon Schubert {
15155796c8dcSSimon Schubert   int len1 = TYPE_LENGTH (value_type (arg1));
15165796c8dcSSimon Schubert   int len2 = TYPE_LENGTH (value_type (arg2));
15175796c8dcSSimon Schubert   const gdb_byte *s1 = value_contents (arg1);
15185796c8dcSSimon Schubert   const gdb_byte *s2 = value_contents (arg2);
15195796c8dcSSimon Schubert   int i, len = len1 < len2 ? len1 : len2;
15205796c8dcSSimon Schubert 
15215796c8dcSSimon Schubert   for (i = 0; i < len; i++)
15225796c8dcSSimon Schubert     {
15235796c8dcSSimon Schubert       if (s1[i] < s2[i])
15245796c8dcSSimon Schubert         return -1;
15255796c8dcSSimon Schubert       else if (s1[i] > s2[i])
15265796c8dcSSimon Schubert         return 1;
15275796c8dcSSimon Schubert       else
15285796c8dcSSimon Schubert         continue;
15295796c8dcSSimon Schubert     }
15305796c8dcSSimon Schubert 
15315796c8dcSSimon Schubert   if (len1 < len2)
15325796c8dcSSimon Schubert     return -1;
15335796c8dcSSimon Schubert   else if (len1 > len2)
15345796c8dcSSimon Schubert     return 1;
15355796c8dcSSimon Schubert   else
15365796c8dcSSimon Schubert     return 0;
15375796c8dcSSimon Schubert }
15385796c8dcSSimon Schubert 
15395796c8dcSSimon Schubert /* Simulate the C operator == by returning a 1
15405796c8dcSSimon Schubert    iff ARG1 and ARG2 have equal contents.  */
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert int
value_equal(struct value * arg1,struct value * arg2)15435796c8dcSSimon Schubert value_equal (struct value *arg1, struct value *arg2)
15445796c8dcSSimon Schubert {
15455796c8dcSSimon Schubert   int len;
15465796c8dcSSimon Schubert   const gdb_byte *p1;
15475796c8dcSSimon Schubert   const gdb_byte *p2;
15485796c8dcSSimon Schubert   struct type *type1, *type2;
15495796c8dcSSimon Schubert   enum type_code code1;
15505796c8dcSSimon Schubert   enum type_code code2;
15515796c8dcSSimon Schubert   int is_int1, is_int2;
15525796c8dcSSimon Schubert 
15535796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
15545796c8dcSSimon Schubert   arg2 = coerce_array (arg2);
15555796c8dcSSimon Schubert 
15565796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
15575796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
15585796c8dcSSimon Schubert   code1 = TYPE_CODE (type1);
15595796c8dcSSimon Schubert   code2 = TYPE_CODE (type2);
15605796c8dcSSimon Schubert   is_int1 = is_integral_type (type1);
15615796c8dcSSimon Schubert   is_int2 = is_integral_type (type2);
15625796c8dcSSimon Schubert 
15635796c8dcSSimon Schubert   if (is_int1 && is_int2)
15645796c8dcSSimon Schubert     return longest_to_int (value_as_long (value_binop (arg1, arg2,
15655796c8dcSSimon Schubert 						       BINOP_EQUAL)));
15665796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_FLT || is_int1)
15675796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_FLT || is_int2))
15685796c8dcSSimon Schubert     {
15695796c8dcSSimon Schubert       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
15705796c8dcSSimon Schubert 	 `long double' values are returned in static storage (m68k).  */
15715796c8dcSSimon Schubert       DOUBLEST d = value_as_double (arg1);
1572cf7f2e2dSJohn Marino 
15735796c8dcSSimon Schubert       return d == value_as_double (arg2);
15745796c8dcSSimon Schubert     }
15755796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
15765796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_DECFLOAT || is_int2))
15775796c8dcSSimon Schubert     {
15785796c8dcSSimon Schubert       gdb_byte v1[16], v2[16];
15795796c8dcSSimon Schubert       int len_v1, len_v2;
15805796c8dcSSimon Schubert       enum bfd_endian byte_order_v1, byte_order_v2;
15815796c8dcSSimon Schubert 
15825796c8dcSSimon Schubert       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
15835796c8dcSSimon Schubert 					 v2, &len_v2, &byte_order_v2);
15845796c8dcSSimon Schubert 
15855796c8dcSSimon Schubert       return decimal_compare (v1, len_v1, byte_order_v1,
15865796c8dcSSimon Schubert 			      v2, len_v2, byte_order_v2) == 0;
15875796c8dcSSimon Schubert     }
15885796c8dcSSimon Schubert 
15895796c8dcSSimon Schubert   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
15905796c8dcSSimon Schubert      is bigger.  */
15915796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && is_int2)
15925796c8dcSSimon Schubert     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
15935796c8dcSSimon Schubert   else if (code2 == TYPE_CODE_PTR && is_int1)
15945796c8dcSSimon Schubert     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
15955796c8dcSSimon Schubert 
15965796c8dcSSimon Schubert   else if (code1 == code2
15975796c8dcSSimon Schubert 	   && ((len = (int) TYPE_LENGTH (type1))
15985796c8dcSSimon Schubert 	       == (int) TYPE_LENGTH (type2)))
15995796c8dcSSimon Schubert     {
16005796c8dcSSimon Schubert       p1 = value_contents (arg1);
16015796c8dcSSimon Schubert       p2 = value_contents (arg2);
16025796c8dcSSimon Schubert       while (--len >= 0)
16035796c8dcSSimon Schubert 	{
16045796c8dcSSimon Schubert 	  if (*p1++ != *p2++)
16055796c8dcSSimon Schubert 	    break;
16065796c8dcSSimon Schubert 	}
16075796c8dcSSimon Schubert       return len < 0;
16085796c8dcSSimon Schubert     }
16095796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
16105796c8dcSSimon Schubert     {
16115796c8dcSSimon Schubert       return value_strcmp (arg1, arg2) == 0;
16125796c8dcSSimon Schubert     }
16135796c8dcSSimon Schubert   else
16145796c8dcSSimon Schubert     {
16155796c8dcSSimon Schubert       error (_("Invalid type combination in equality test."));
1616c50c785cSJohn Marino       return 0;			/* For lint -- never reached.  */
16175796c8dcSSimon Schubert     }
16185796c8dcSSimon Schubert }
16195796c8dcSSimon Schubert 
1620cf7f2e2dSJohn Marino /* Compare values based on their raw contents.  Useful for arrays since
1621cf7f2e2dSJohn Marino    value_equal coerces them to pointers, thus comparing just the address
1622cf7f2e2dSJohn Marino    of the array instead of its contents.  */
1623cf7f2e2dSJohn Marino 
1624cf7f2e2dSJohn Marino int
value_equal_contents(struct value * arg1,struct value * arg2)1625cf7f2e2dSJohn Marino value_equal_contents (struct value *arg1, struct value *arg2)
1626cf7f2e2dSJohn Marino {
1627cf7f2e2dSJohn Marino   struct type *type1, *type2;
1628cf7f2e2dSJohn Marino 
1629cf7f2e2dSJohn Marino   type1 = check_typedef (value_type (arg1));
1630cf7f2e2dSJohn Marino   type2 = check_typedef (value_type (arg2));
1631cf7f2e2dSJohn Marino 
1632cf7f2e2dSJohn Marino   return (TYPE_CODE (type1) == TYPE_CODE (type2)
1633cf7f2e2dSJohn Marino 	  && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
1634cf7f2e2dSJohn Marino 	  && memcmp (value_contents (arg1), value_contents (arg2),
1635cf7f2e2dSJohn Marino 		     TYPE_LENGTH (type1)) == 0);
1636cf7f2e2dSJohn Marino }
1637cf7f2e2dSJohn Marino 
16385796c8dcSSimon Schubert /* Simulate the C operator < by returning 1
16395796c8dcSSimon Schubert    iff ARG1's contents are less than ARG2's.  */
16405796c8dcSSimon Schubert 
16415796c8dcSSimon Schubert int
value_less(struct value * arg1,struct value * arg2)16425796c8dcSSimon Schubert value_less (struct value *arg1, struct value *arg2)
16435796c8dcSSimon Schubert {
16445796c8dcSSimon Schubert   enum type_code code1;
16455796c8dcSSimon Schubert   enum type_code code2;
16465796c8dcSSimon Schubert   struct type *type1, *type2;
16475796c8dcSSimon Schubert   int is_int1, is_int2;
16485796c8dcSSimon Schubert 
16495796c8dcSSimon Schubert   arg1 = coerce_array (arg1);
16505796c8dcSSimon Schubert   arg2 = coerce_array (arg2);
16515796c8dcSSimon Schubert 
16525796c8dcSSimon Schubert   type1 = check_typedef (value_type (arg1));
16535796c8dcSSimon Schubert   type2 = check_typedef (value_type (arg2));
16545796c8dcSSimon Schubert   code1 = TYPE_CODE (type1);
16555796c8dcSSimon Schubert   code2 = TYPE_CODE (type2);
16565796c8dcSSimon Schubert   is_int1 = is_integral_type (type1);
16575796c8dcSSimon Schubert   is_int2 = is_integral_type (type2);
16585796c8dcSSimon Schubert 
16595796c8dcSSimon Schubert   if (is_int1 && is_int2)
16605796c8dcSSimon Schubert     return longest_to_int (value_as_long (value_binop (arg1, arg2,
16615796c8dcSSimon Schubert 						       BINOP_LESS)));
16625796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_FLT || is_int1)
16635796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_FLT || is_int2))
16645796c8dcSSimon Schubert     {
16655796c8dcSSimon Schubert       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
16665796c8dcSSimon Schubert 	 `long double' values are returned in static storage (m68k).  */
16675796c8dcSSimon Schubert       DOUBLEST d = value_as_double (arg1);
1668cf7f2e2dSJohn Marino 
16695796c8dcSSimon Schubert       return d < value_as_double (arg2);
16705796c8dcSSimon Schubert     }
16715796c8dcSSimon Schubert   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
16725796c8dcSSimon Schubert 	   && (code2 == TYPE_CODE_DECFLOAT || is_int2))
16735796c8dcSSimon Schubert     {
16745796c8dcSSimon Schubert       gdb_byte v1[16], v2[16];
16755796c8dcSSimon Schubert       int len_v1, len_v2;
16765796c8dcSSimon Schubert       enum bfd_endian byte_order_v1, byte_order_v2;
16775796c8dcSSimon Schubert 
16785796c8dcSSimon Schubert       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
16795796c8dcSSimon Schubert 					 v2, &len_v2, &byte_order_v2);
16805796c8dcSSimon Schubert 
16815796c8dcSSimon Schubert       return decimal_compare (v1, len_v1, byte_order_v1,
16825796c8dcSSimon Schubert 			      v2, len_v2, byte_order_v2) == -1;
16835796c8dcSSimon Schubert     }
16845796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
16855796c8dcSSimon Schubert     return value_as_address (arg1) < value_as_address (arg2);
16865796c8dcSSimon Schubert 
16875796c8dcSSimon Schubert   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
16885796c8dcSSimon Schubert      is bigger.  */
16895796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_PTR && is_int2)
16905796c8dcSSimon Schubert     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
16915796c8dcSSimon Schubert   else if (code2 == TYPE_CODE_PTR && is_int1)
16925796c8dcSSimon Schubert     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
16935796c8dcSSimon Schubert   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
16945796c8dcSSimon Schubert     return value_strcmp (arg1, arg2) < 0;
16955796c8dcSSimon Schubert   else
16965796c8dcSSimon Schubert     {
16975796c8dcSSimon Schubert       error (_("Invalid type combination in ordering comparison."));
16985796c8dcSSimon Schubert       return 0;
16995796c8dcSSimon Schubert     }
17005796c8dcSSimon Schubert }
17015796c8dcSSimon Schubert 
17025796c8dcSSimon Schubert /* The unary operators +, - and ~.  They free the argument ARG1.  */
17035796c8dcSSimon Schubert 
17045796c8dcSSimon Schubert struct value *
value_pos(struct value * arg1)17055796c8dcSSimon Schubert value_pos (struct value *arg1)
17065796c8dcSSimon Schubert {
17075796c8dcSSimon Schubert   struct type *type;
17085796c8dcSSimon Schubert 
17095796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
17105796c8dcSSimon Schubert   type = check_typedef (value_type (arg1));
17115796c8dcSSimon Schubert 
17125796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_FLT)
17135796c8dcSSimon Schubert     return value_from_double (type, value_as_double (arg1));
17145796c8dcSSimon Schubert   else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
17155796c8dcSSimon Schubert     return value_from_decfloat (type, value_contents (arg1));
17165796c8dcSSimon Schubert   else if (is_integral_type (type))
17175796c8dcSSimon Schubert     {
17185796c8dcSSimon Schubert       return value_from_longest (type, value_as_long (arg1));
17195796c8dcSSimon Schubert     }
1720c50c785cSJohn Marino   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1721c50c785cSJohn Marino     {
1722c50c785cSJohn Marino       struct value *val = allocate_value (type);
1723c50c785cSJohn Marino 
1724c50c785cSJohn Marino       memcpy (value_contents_raw (val), value_contents (arg1),
1725c50c785cSJohn Marino               TYPE_LENGTH (type));
1726c50c785cSJohn Marino       return val;
1727c50c785cSJohn Marino     }
17285796c8dcSSimon Schubert   else
17295796c8dcSSimon Schubert     {
1730c50c785cSJohn Marino       error (_("Argument to positive operation not a number."));
1731c50c785cSJohn Marino       return 0;			/* For lint -- never reached.  */
17325796c8dcSSimon Schubert     }
17335796c8dcSSimon Schubert }
17345796c8dcSSimon Schubert 
17355796c8dcSSimon Schubert struct value *
value_neg(struct value * arg1)17365796c8dcSSimon Schubert value_neg (struct value *arg1)
17375796c8dcSSimon Schubert {
17385796c8dcSSimon Schubert   struct type *type;
17395796c8dcSSimon Schubert 
17405796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
17415796c8dcSSimon Schubert   type = check_typedef (value_type (arg1));
17425796c8dcSSimon Schubert 
17435796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
17445796c8dcSSimon Schubert     {
17455796c8dcSSimon Schubert       struct value *val = allocate_value (type);
17465796c8dcSSimon Schubert       int len = TYPE_LENGTH (type);
1747c50c785cSJohn Marino       gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long.  */
17485796c8dcSSimon Schubert 
17495796c8dcSSimon Schubert       memcpy (decbytes, value_contents (arg1), len);
17505796c8dcSSimon Schubert 
17515796c8dcSSimon Schubert       if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
17525796c8dcSSimon Schubert 	decbytes[len-1] = decbytes[len - 1] | 0x80;
17535796c8dcSSimon Schubert       else
17545796c8dcSSimon Schubert 	decbytes[0] = decbytes[0] | 0x80;
17555796c8dcSSimon Schubert 
17565796c8dcSSimon Schubert       memcpy (value_contents_raw (val), decbytes, len);
17575796c8dcSSimon Schubert       return val;
17585796c8dcSSimon Schubert     }
17595796c8dcSSimon Schubert   else if (TYPE_CODE (type) == TYPE_CODE_FLT)
17605796c8dcSSimon Schubert     return value_from_double (type, -value_as_double (arg1));
17615796c8dcSSimon Schubert   else if (is_integral_type (type))
17625796c8dcSSimon Schubert     {
17635796c8dcSSimon Schubert       return value_from_longest (type, -value_as_long (arg1));
17645796c8dcSSimon Schubert     }
1765c50c785cSJohn Marino   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1766c50c785cSJohn Marino     {
1767c50c785cSJohn Marino       struct value *tmp, *val = allocate_value (type);
1768c50c785cSJohn Marino       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1769c50c785cSJohn Marino       int i;
1770c50c785cSJohn Marino       LONGEST low_bound, high_bound;
1771c50c785cSJohn Marino 
1772c50c785cSJohn Marino       if (!get_array_bounds (type, &low_bound, &high_bound))
1773c50c785cSJohn Marino 	error (_("Could not determine the vector bounds"));
1774c50c785cSJohn Marino 
1775c50c785cSJohn Marino       for (i = 0; i < high_bound - low_bound + 1; i++)
1776c50c785cSJohn Marino 	{
1777c50c785cSJohn Marino 	  tmp = value_neg (value_subscript (arg1, i));
1778c50c785cSJohn Marino 	  memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1779c50c785cSJohn Marino 		  value_contents_all (tmp), TYPE_LENGTH (eltype));
1780c50c785cSJohn Marino 	}
1781c50c785cSJohn Marino       return val;
1782c50c785cSJohn Marino     }
17835796c8dcSSimon Schubert   else
17845796c8dcSSimon Schubert     {
17855796c8dcSSimon Schubert       error (_("Argument to negate operation not a number."));
1786c50c785cSJohn Marino       return 0;			/* For lint -- never reached.  */
17875796c8dcSSimon Schubert     }
17885796c8dcSSimon Schubert }
17895796c8dcSSimon Schubert 
17905796c8dcSSimon Schubert struct value *
value_complement(struct value * arg1)17915796c8dcSSimon Schubert value_complement (struct value *arg1)
17925796c8dcSSimon Schubert {
17935796c8dcSSimon Schubert   struct type *type;
1794c50c785cSJohn Marino   struct value *val;
17955796c8dcSSimon Schubert 
17965796c8dcSSimon Schubert   arg1 = coerce_ref (arg1);
17975796c8dcSSimon Schubert   type = check_typedef (value_type (arg1));
17985796c8dcSSimon Schubert 
1799c50c785cSJohn Marino   if (is_integral_type (type))
1800c50c785cSJohn Marino     val = value_from_longest (type, ~value_as_long (arg1));
1801c50c785cSJohn Marino   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
1802c50c785cSJohn Marino     {
1803c50c785cSJohn Marino       struct value *tmp;
1804c50c785cSJohn Marino       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1805c50c785cSJohn Marino       int i;
1806c50c785cSJohn Marino       LONGEST low_bound, high_bound;
18075796c8dcSSimon Schubert 
1808c50c785cSJohn Marino       if (!get_array_bounds (type, &low_bound, &high_bound))
1809c50c785cSJohn Marino 	error (_("Could not determine the vector bounds"));
1810c50c785cSJohn Marino 
1811c50c785cSJohn Marino       val = allocate_value (type);
1812c50c785cSJohn Marino       for (i = 0; i < high_bound - low_bound + 1; i++)
1813c50c785cSJohn Marino         {
1814c50c785cSJohn Marino           tmp = value_complement (value_subscript (arg1, i));
1815c50c785cSJohn Marino           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
1816c50c785cSJohn Marino                   value_contents_all (tmp), TYPE_LENGTH (eltype));
1817c50c785cSJohn Marino         }
1818c50c785cSJohn Marino     }
1819c50c785cSJohn Marino   else
1820c50c785cSJohn Marino     error (_("Argument to complement operation not an integer, boolean."));
1821c50c785cSJohn Marino 
1822c50c785cSJohn Marino   return val;
18235796c8dcSSimon Schubert }
18245796c8dcSSimon Schubert 
18255796c8dcSSimon Schubert /* The INDEX'th bit of SET value whose value_type is TYPE,
18265796c8dcSSimon Schubert    and whose value_contents is valaddr.
18275796c8dcSSimon Schubert    Return -1 if out of range, -2 other error.  */
18285796c8dcSSimon Schubert 
18295796c8dcSSimon Schubert int
value_bit_index(struct type * type,const gdb_byte * valaddr,int index)18305796c8dcSSimon Schubert value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
18315796c8dcSSimon Schubert {
18325796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (type);
18335796c8dcSSimon Schubert   LONGEST low_bound, high_bound;
18345796c8dcSSimon Schubert   LONGEST word;
18355796c8dcSSimon Schubert   unsigned rel_index;
18365796c8dcSSimon Schubert   struct type *range = TYPE_INDEX_TYPE (type);
1837cf7f2e2dSJohn Marino 
18385796c8dcSSimon Schubert   if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
18395796c8dcSSimon Schubert     return -2;
18405796c8dcSSimon Schubert   if (index < low_bound || index > high_bound)
18415796c8dcSSimon Schubert     return -1;
18425796c8dcSSimon Schubert   rel_index = index - low_bound;
18435796c8dcSSimon Schubert   word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
18445796c8dcSSimon Schubert 				   gdbarch_byte_order (gdbarch));
18455796c8dcSSimon Schubert   rel_index %= TARGET_CHAR_BIT;
18465796c8dcSSimon Schubert   if (gdbarch_bits_big_endian (gdbarch))
18475796c8dcSSimon Schubert     rel_index = TARGET_CHAR_BIT - 1 - rel_index;
18485796c8dcSSimon Schubert   return (word >> rel_index) & 1;
18495796c8dcSSimon Schubert }
18505796c8dcSSimon Schubert 
18515796c8dcSSimon Schubert int
value_in(struct value * element,struct value * set)18525796c8dcSSimon Schubert value_in (struct value *element, struct value *set)
18535796c8dcSSimon Schubert {
18545796c8dcSSimon Schubert   int member;
18555796c8dcSSimon Schubert   struct type *settype = check_typedef (value_type (set));
18565796c8dcSSimon Schubert   struct type *eltype = check_typedef (value_type (element));
1857cf7f2e2dSJohn Marino 
18585796c8dcSSimon Schubert   if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
18595796c8dcSSimon Schubert     eltype = TYPE_TARGET_TYPE (eltype);
18605796c8dcSSimon Schubert   if (TYPE_CODE (settype) != TYPE_CODE_SET)
18615796c8dcSSimon Schubert     error (_("Second argument of 'IN' has wrong type"));
18625796c8dcSSimon Schubert   if (TYPE_CODE (eltype) != TYPE_CODE_INT
18635796c8dcSSimon Schubert       && TYPE_CODE (eltype) != TYPE_CODE_CHAR
18645796c8dcSSimon Schubert       && TYPE_CODE (eltype) != TYPE_CODE_ENUM
18655796c8dcSSimon Schubert       && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
18665796c8dcSSimon Schubert     error (_("First argument of 'IN' has wrong type"));
18675796c8dcSSimon Schubert   member = value_bit_index (settype, value_contents (set),
18685796c8dcSSimon Schubert 			    value_as_long (element));
18695796c8dcSSimon Schubert   if (member < 0)
18705796c8dcSSimon Schubert     error (_("First argument of 'IN' not in range"));
18715796c8dcSSimon Schubert   return member;
18725796c8dcSSimon Schubert }
18735796c8dcSSimon Schubert 
18745796c8dcSSimon Schubert void
_initialize_valarith(void)18755796c8dcSSimon Schubert _initialize_valarith (void)
18765796c8dcSSimon Schubert {
18775796c8dcSSimon Schubert }
1878