gdb/valarith.c - gdb

Functions defined

Macros defined

Source code

  1. /* Perform arithmetic and other operations on values, for GDB.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "value.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "expression.h"
  19. #include "target.h"
  20. #include "language.h"
  21. #include "doublest.h"
  22. #include "dfp.h"
  23. #include <math.h>
  24. #include "infcall.h"

  25. /* Define whether or not the C operator '/' truncates towards zero for
  26.    differently signed operands (truncation direction is undefined in C).  */

  27. #ifndef TRUNCATION_TOWARDS_ZERO
  28. #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
  29. #endif

  30. void _initialize_valarith (void);


  31. /* Given a pointer, return the size of its target.
  32.    If the pointer type is void *, then return 1.
  33.    If the target type is incomplete, then error out.
  34.    This isn't a general purpose function, but just a
  35.    helper for value_ptradd.  */

  36. static LONGEST
  37. find_size_for_pointer_math (struct type *ptr_type)
  38. {
  39.   LONGEST sz = -1;
  40.   struct type *ptr_target;

  41.   gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
  42.   ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));

  43.   sz = TYPE_LENGTH (ptr_target);
  44.   if (sz == 0)
  45.     {
  46.       if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
  47.         sz = 1;
  48.       else
  49.         {
  50.           const char *name;

  51.           name = TYPE_NAME (ptr_target);
  52.           if (name == NULL)
  53.             name = TYPE_TAG_NAME (ptr_target);
  54.           if (name == NULL)
  55.             error (_("Cannot perform pointer math on incomplete types, "
  56.                    "try casting to a known type, or void *."));
  57.           else
  58.             error (_("Cannot perform pointer math on incomplete type \"%s\", "
  59.                    "try casting to a known type, or void *."), name);
  60.         }
  61.     }
  62.   return sz;
  63. }

  64. /* Given a pointer ARG1 and an integral value ARG2, return the
  65.    result of C-style pointer arithmetic ARG1 + ARG2.  */

  66. struct value *
  67. value_ptradd (struct value *arg1, LONGEST arg2)
  68. {
  69.   struct type *valptrtype;
  70.   LONGEST sz;
  71.   struct value *result;

  72.   arg1 = coerce_array (arg1);
  73.   valptrtype = check_typedef (value_type (arg1));
  74.   sz = find_size_for_pointer_math (valptrtype);

  75.   result = value_from_pointer (valptrtype,
  76.                                value_as_address (arg1) + sz * arg2);
  77.   if (VALUE_LVAL (result) != lval_internalvar)
  78.     set_value_component_location (result, arg1);
  79.   return result;
  80. }

  81. /* Given two compatible pointer values ARG1 and ARG2, return the
  82.    result of C-style pointer arithmetic ARG1 - ARG2.  */

  83. LONGEST
  84. value_ptrdiff (struct value *arg1, struct value *arg2)
  85. {
  86.   struct type *type1, *type2;
  87.   LONGEST sz;

  88.   arg1 = coerce_array (arg1);
  89.   arg2 = coerce_array (arg2);
  90.   type1 = check_typedef (value_type (arg1));
  91.   type2 = check_typedef (value_type (arg2));

  92.   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
  93.   gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);

  94.   if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
  95.       != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
  96.     error (_("First argument of `-' is a pointer and "
  97.              "second argument is neither\n"
  98.              "an integer nor a pointer of the same type."));

  99.   sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
  100.   if (sz == 0)
  101.     {
  102.       warning (_("Type size unknown, assuming 1. "
  103.                "Try casting to a known type, or void *."));
  104.       sz = 1;
  105.     }

  106.   return (value_as_long (arg1) - value_as_long (arg2)) / sz;
  107. }

  108. /* Return the value of ARRAY[IDX].

  109.    ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING.  If the
  110.    current language supports C-style arrays, it may also be TYPE_CODE_PTR.

  111.    See comments in value_coerce_array() for rationale for reason for
  112.    doing lower bounds adjustment here rather than there.
  113.    FIXME:  Perhaps we should validate that the index is valid and if
  114.    verbosity is set, warn about invalid indices (but still use them).  */

  115. struct value *
  116. value_subscript (struct value *array, LONGEST index)
  117. {
  118.   int c_style = current_language->c_style_arrays;
  119.   struct type *tarray;

  120.   array = coerce_ref (array);
  121.   tarray = check_typedef (value_type (array));

  122.   if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
  123.       || TYPE_CODE (tarray) == TYPE_CODE_STRING)
  124.     {
  125.       struct type *range_type = TYPE_INDEX_TYPE (tarray);
  126.       LONGEST lowerbound, upperbound;

  127.       get_discrete_bounds (range_type, &lowerbound, &upperbound);
  128.       if (VALUE_LVAL (array) != lval_memory)
  129.         return value_subscripted_rvalue (array, index, lowerbound);

  130.       if (c_style == 0)
  131.         {
  132.           if (index >= lowerbound && index <= upperbound)
  133.             return value_subscripted_rvalue (array, index, lowerbound);
  134.           /* Emit warning unless we have an array of unknown size.
  135.              An array of unknown size has lowerbound 0 and upperbound -1.  */
  136.           if (upperbound > -1)
  137.             warning (_("array or string index out of range"));
  138.           /* fall doing C stuff */
  139.           c_style = 1;
  140.         }

  141.       index -= lowerbound;
  142.       array = value_coerce_array (array);
  143.     }

  144.   if (c_style)
  145.     return value_ind (value_ptradd (array, index));
  146.   else
  147.     error (_("not an array or string"));
  148. }

  149. /* Return the value of EXPR[IDX], expr an aggregate rvalue
  150.    (eg, a vector register).  This routine used to promote floats
  151.    to doubles, but no longer does.  */

  152. struct value *
  153. value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
  154. {
  155.   struct type *array_type = check_typedef (value_type (array));
  156.   struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
  157.   unsigned int elt_size = TYPE_LENGTH (elt_type);
  158.   unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
  159.   struct value *v;

  160.   if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
  161.                              && elt_offs >= TYPE_LENGTH (array_type)))
  162.     error (_("no such vector element"));

  163.   if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
  164.     v = allocate_value_lazy (elt_type);
  165.   else
  166.     {
  167.       v = allocate_value (elt_type);
  168.       value_contents_copy (v, value_embedded_offset (v),
  169.                            array, value_embedded_offset (array) + elt_offs,
  170.                            elt_size);
  171.     }

  172.   set_value_component_location (v, array);
  173.   VALUE_REGNUM (v) = VALUE_REGNUM (array);
  174.   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
  175.   set_value_offset (v, value_offset (array) + elt_offs);
  176.   return v;
  177. }


  178. /* Check to see if either argument is a structure, or a reference to
  179.    one.  This is called so we know whether to go ahead with the normal
  180.    binop or look for a user defined function instead.

  181.    For now, we do not overload the `=' operator.  */

  182. int
  183. binop_types_user_defined_p (enum exp_opcode op,
  184.                             struct type *type1, struct type *type2)
  185. {
  186.   if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
  187.     return 0;

  188.   type1 = check_typedef (type1);
  189.   if (TYPE_CODE (type1) == TYPE_CODE_REF)
  190.     type1 = check_typedef (TYPE_TARGET_TYPE (type1));

  191.   type2 = check_typedef (type2);
  192.   if (TYPE_CODE (type2) == TYPE_CODE_REF)
  193.     type2 = check_typedef (TYPE_TARGET_TYPE (type2));

  194.   return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
  195.           || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
  196. }

  197. /* Check to see if either argument is a structure, or a reference to
  198.    one.  This is called so we know whether to go ahead with the normal
  199.    binop or look for a user defined function instead.

  200.    For now, we do not overload the `=' operator.  */

  201. int
  202. binop_user_defined_p (enum exp_opcode op,
  203.                       struct value *arg1, struct value *arg2)
  204. {
  205.   return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
  206. }

  207. /* Check to see if argument is a structure.  This is called so
  208.    we know whether to go ahead with the normal unop or look for a
  209.    user defined function instead.

  210.    For now, we do not overload the `&' operator.  */

  211. int
  212. unop_user_defined_p (enum exp_opcode op, struct value *arg1)
  213. {
  214.   struct type *type1;

  215.   if (op == UNOP_ADDR)
  216.     return 0;
  217.   type1 = check_typedef (value_type (arg1));
  218.   if (TYPE_CODE (type1) == TYPE_CODE_REF)
  219.     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
  220.   return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
  221. }

  222. /* Try to find an operator named OPERATOR which takes NARGS arguments
  223.    specified in ARGS.  If the operator found is a static member operator
  224.    *STATIC_MEMFUNP will be set to 1, and otherwise 0.
  225.    The search if performed through find_overload_match which will handle
  226.    member operators, non member operators, operators imported implicitly or
  227.    explicitly, and perform correct overload resolution in all of the above
  228.    situations or combinations thereof.  */

  229. static struct value *
  230. value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
  231.                            int *static_memfuncp, enum noside noside)
  232. {

  233.   struct symbol *symp = NULL;
  234.   struct value *valp = NULL;

  235.   find_overload_match (args, nargs, operator, BOTH /* could be method */,
  236.                        &args[0] /* objp */,
  237.                        NULL /* pass NULL symbol since symbol is unknown */,
  238.                        &valp, &symp, static_memfuncp, 0, noside);

  239.   if (valp)
  240.     return valp;

  241.   if (symp)
  242.     {
  243.       /* This is a non member function and does not
  244.          expect a reference as its first argument
  245.          rather the explicit structure.  */
  246.       args[0] = value_ind (args[0]);
  247.       return value_of_variable (symp, 0);
  248.     }

  249.   error (_("Could not find %s."), operator);
  250. }

  251. /* Lookup user defined operator NAME.  Return a value representing the
  252.    function, otherwise return NULL.  */

  253. static struct value *
  254. value_user_defined_op (struct value **argp, struct value **args, char *name,
  255.                        int *static_memfuncp, int nargs, enum noside noside)
  256. {
  257.   struct value *result = NULL;

  258.   if (current_language->la_language == language_cplus)
  259.     {
  260.       result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp,
  261.                                           noside);
  262.     }
  263.   else
  264.     result = value_struct_elt (argp, args, name, static_memfuncp,
  265.                                "structure");

  266.   return result;
  267. }

  268. /* We know either arg1 or arg2 is a structure, so try to find the right
  269.    user defined function.  Create an argument vector that calls
  270.    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
  271.    binary operator which is legal for GNU C++).

  272.    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
  273.    is the opcode saying how to modify it.  Otherwise, OTHEROP is
  274.    unused.  */

  275. struct value *
  276. value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
  277.                enum exp_opcode otherop, enum noside noside)
  278. {
  279.   struct value **argvec;
  280.   char *ptr;
  281.   char tstr[13];
  282.   int static_memfuncp;

  283.   arg1 = coerce_ref (arg1);
  284.   arg2 = coerce_ref (arg2);

  285.   /* now we know that what we have to do is construct our
  286.      arg vector and find the right function to call it with.  */

  287.   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
  288.     error (_("Can't do that binary op on that type"));        /* FIXME be explicit */

  289.   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
  290.   argvec[1] = value_addr (arg1);
  291.   argvec[2] = arg2;
  292.   argvec[3] = 0;

  293.   /* Make the right function name up.  */
  294.   strcpy (tstr, "operator__");
  295.   ptr = tstr + 8;
  296.   switch (op)
  297.     {
  298.     case BINOP_ADD:
  299.       strcpy (ptr, "+");
  300.       break;
  301.     case BINOP_SUB:
  302.       strcpy (ptr, "-");
  303.       break;
  304.     case BINOP_MUL:
  305.       strcpy (ptr, "*");
  306.       break;
  307.     case BINOP_DIV:
  308.       strcpy (ptr, "/");
  309.       break;
  310.     case BINOP_REM:
  311.       strcpy (ptr, "%");
  312.       break;
  313.     case BINOP_LSH:
  314.       strcpy (ptr, "<<");
  315.       break;
  316.     case BINOP_RSH:
  317.       strcpy (ptr, ">>");
  318.       break;
  319.     case BINOP_BITWISE_AND:
  320.       strcpy (ptr, "&");
  321.       break;
  322.     case BINOP_BITWISE_IOR:
  323.       strcpy (ptr, "|");
  324.       break;
  325.     case BINOP_BITWISE_XOR:
  326.       strcpy (ptr, "^");
  327.       break;
  328.     case BINOP_LOGICAL_AND:
  329.       strcpy (ptr, "&&");
  330.       break;
  331.     case BINOP_LOGICAL_OR:
  332.       strcpy (ptr, "||");
  333.       break;
  334.     case BINOP_MIN:
  335.       strcpy (ptr, "<?");
  336.       break;
  337.     case BINOP_MAX:
  338.       strcpy (ptr, ">?");
  339.       break;
  340.     case BINOP_ASSIGN:
  341.       strcpy (ptr, "=");
  342.       break;
  343.     case BINOP_ASSIGN_MODIFY:
  344.       switch (otherop)
  345.         {
  346.         case BINOP_ADD:
  347.           strcpy (ptr, "+=");
  348.           break;
  349.         case BINOP_SUB:
  350.           strcpy (ptr, "-=");
  351.           break;
  352.         case BINOP_MUL:
  353.           strcpy (ptr, "*=");
  354.           break;
  355.         case BINOP_DIV:
  356.           strcpy (ptr, "/=");
  357.           break;
  358.         case BINOP_REM:
  359.           strcpy (ptr, "%=");
  360.           break;
  361.         case BINOP_BITWISE_AND:
  362.           strcpy (ptr, "&=");
  363.           break;
  364.         case BINOP_BITWISE_IOR:
  365.           strcpy (ptr, "|=");
  366.           break;
  367.         case BINOP_BITWISE_XOR:
  368.           strcpy (ptr, "^=");
  369.           break;
  370.         case BINOP_MOD:        /* invalid */
  371.         default:
  372.           error (_("Invalid binary operation specified."));
  373.         }
  374.       break;
  375.     case BINOP_SUBSCRIPT:
  376.       strcpy (ptr, "[]");
  377.       break;
  378.     case BINOP_EQUAL:
  379.       strcpy (ptr, "==");
  380.       break;
  381.     case BINOP_NOTEQUAL:
  382.       strcpy (ptr, "!=");
  383.       break;
  384.     case BINOP_LESS:
  385.       strcpy (ptr, "<");
  386.       break;
  387.     case BINOP_GTR:
  388.       strcpy (ptr, ">");
  389.       break;
  390.     case BINOP_GEQ:
  391.       strcpy (ptr, ">=");
  392.       break;
  393.     case BINOP_LEQ:
  394.       strcpy (ptr, "<=");
  395.       break;
  396.     case BINOP_MOD:                /* invalid */
  397.     default:
  398.       error (_("Invalid binary operation specified."));
  399.     }

  400.   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
  401.                                      &static_memfuncp, 2, noside);

  402.   if (argvec[0])
  403.     {
  404.       if (static_memfuncp)
  405.         {
  406.           argvec[1] = argvec[0];
  407.           argvec++;
  408.         }
  409.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  410.         {
  411.           struct type *return_type;

  412.           return_type
  413.             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
  414.           return value_zero (return_type, VALUE_LVAL (arg1));
  415.         }

  416.       if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
  417.         {
  418.           /* Static xmethods are not supported yet.  */
  419.           gdb_assert (static_memfuncp == 0);
  420.           return call_xmethod (argvec[0], 2, argvec + 1);
  421.         }
  422.       else
  423.         return call_function_by_hand (argvec[0], 2 - static_memfuncp,
  424.                                       argvec + 1);
  425.     }
  426.   throw_error (NOT_FOUND_ERROR,
  427.                _("member function %s not found"), tstr);
  428. #ifdef lint
  429.   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
  430. #endif
  431. }

  432. /* We know that arg1 is a structure, so try to find a unary user
  433.    defined operator that matches the operator in question.
  434.    Create an argument vector that calls arg1.operator @ (arg1)
  435.    and return that value (where '@' is (almost) any unary operator which
  436.    is legal for GNU C++).  */

  437. struct value *
  438. value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
  439. {
  440.   struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
  441.   struct value **argvec;
  442.   char *ptr;
  443.   char tstr[13], mangle_tstr[13];
  444.   int static_memfuncp, nargs;

  445.   arg1 = coerce_ref (arg1);

  446.   /* now we know that what we have to do is construct our
  447.      arg vector and find the right function to call it with.  */

  448.   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
  449.     error (_("Can't do that unary op on that type"));        /* FIXME be explicit */

  450.   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
  451.   argvec[1] = value_addr (arg1);
  452.   argvec[2] = 0;

  453.   nargs = 1;

  454.   /* Make the right function name up.  */
  455.   strcpy (tstr, "operator__");
  456.   ptr = tstr + 8;
  457.   strcpy (mangle_tstr, "__");
  458.   switch (op)
  459.     {
  460.     case UNOP_PREINCREMENT:
  461.       strcpy (ptr, "++");
  462.       break;
  463.     case UNOP_PREDECREMENT:
  464.       strcpy (ptr, "--");
  465.       break;
  466.     case UNOP_POSTINCREMENT:
  467.       strcpy (ptr, "++");
  468.       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
  469.       argvec[3] = 0;
  470.       nargs ++;
  471.       break;
  472.     case UNOP_POSTDECREMENT:
  473.       strcpy (ptr, "--");
  474.       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
  475.       argvec[3] = 0;
  476.       nargs ++;
  477.       break;
  478.     case UNOP_LOGICAL_NOT:
  479.       strcpy (ptr, "!");
  480.       break;
  481.     case UNOP_COMPLEMENT:
  482.       strcpy (ptr, "~");
  483.       break;
  484.     case UNOP_NEG:
  485.       strcpy (ptr, "-");
  486.       break;
  487.     case UNOP_PLUS:
  488.       strcpy (ptr, "+");
  489.       break;
  490.     case UNOP_IND:
  491.       strcpy (ptr, "*");
  492.       break;
  493.     case STRUCTOP_PTR:
  494.       strcpy (ptr, "->");
  495.       break;
  496.     default:
  497.       error (_("Invalid unary operation specified."));
  498.     }

  499.   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
  500.                                      &static_memfuncp, nargs, noside);

  501.   if (argvec[0])
  502.     {
  503.       if (static_memfuncp)
  504.         {
  505.           argvec[1] = argvec[0];
  506.           nargs --;
  507.           argvec++;
  508.         }
  509.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  510.         {
  511.           struct type *return_type;

  512.           return_type
  513.             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
  514.           return value_zero (return_type, VALUE_LVAL (arg1));
  515.         }
  516.       if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_XMETHOD)
  517.         {
  518.           /* Static xmethods are not supported yet.  */
  519.           gdb_assert (static_memfuncp == 0);
  520.           return call_xmethod (argvec[0], 1, argvec + 1);
  521.         }
  522.       else
  523.         return call_function_by_hand (argvec[0], nargs, argvec + 1);
  524.     }
  525.   throw_error (NOT_FOUND_ERROR,
  526.                _("member function %s not found"), tstr);

  527.   return 0;                        /* For lint -- never reached */
  528. }


  529. /* Concatenate two values with the following conditions:

  530.    (1)  Both values must be either bitstring values or character string
  531.    values and the resulting value consists of the concatenation of
  532.    ARG1 followed by ARG2.

  533.    or

  534.    One value must be an integer value and the other value must be
  535.    either a bitstring value or character string value, which is
  536.    to be repeated by the number of times specified by the integer
  537.    value.


  538.    (2)  Boolean values are also allowed and are treated as bit string
  539.    values of length 1.

  540.    (3)  Character values are also allowed and are treated as character
  541.    string values of length 1.  */

  542. struct value *
  543. value_concat (struct value *arg1, struct value *arg2)
  544. {
  545.   struct value *inval1;
  546.   struct value *inval2;
  547.   struct value *outval = NULL;
  548.   int inval1len, inval2len;
  549.   int count, idx;
  550.   char *ptr;
  551.   char inchar;
  552.   struct type *type1 = check_typedef (value_type (arg1));
  553.   struct type *type2 = check_typedef (value_type (arg2));
  554.   struct type *char_type;

  555.   /* First figure out if we are dealing with two values to be concatenated
  556.      or a repeat count and a value to be repeated.  INVAL1 is set to the
  557.      first of two concatenated values, or the repeat count.  INVAL2 is set
  558.      to the second of the two concatenated values or the value to be
  559.      repeated.  */

  560.   if (TYPE_CODE (type2) == TYPE_CODE_INT)
  561.     {
  562.       struct type *tmp = type1;

  563.       type1 = tmp;
  564.       tmp = type2;
  565.       inval1 = arg2;
  566.       inval2 = arg1;
  567.     }
  568.   else
  569.     {
  570.       inval1 = arg1;
  571.       inval2 = arg2;
  572.     }

  573.   /* Now process the input values.  */

  574.   if (TYPE_CODE (type1) == TYPE_CODE_INT)
  575.     {
  576.       /* We have a repeat count.  Validate the second value and then
  577.          construct a value repeated that many times.  */
  578.       if (TYPE_CODE (type2) == TYPE_CODE_STRING
  579.           || TYPE_CODE (type2) == TYPE_CODE_CHAR)
  580.         {
  581.           struct cleanup *back_to;

  582.           count = longest_to_int (value_as_long (inval1));
  583.           inval2len = TYPE_LENGTH (type2);
  584.           ptr = (char *) xmalloc (count * inval2len);
  585.           back_to = make_cleanup (xfree, ptr);
  586.           if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
  587.             {
  588.               char_type = type2;

  589.               inchar = (char) unpack_long (type2,
  590.                                            value_contents (inval2));
  591.               for (idx = 0; idx < count; idx++)
  592.                 {
  593.                   *(ptr + idx) = inchar;
  594.                 }
  595.             }
  596.           else
  597.             {
  598.               char_type = TYPE_TARGET_TYPE (type2);

  599.               for (idx = 0; idx < count; idx++)
  600.                 {
  601.                   memcpy (ptr + (idx * inval2len), value_contents (inval2),
  602.                           inval2len);
  603.                 }
  604.             }
  605.           outval = value_string (ptr, count * inval2len, char_type);
  606.           do_cleanups (back_to);
  607.         }
  608.       else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
  609.         {
  610.           error (_("unimplemented support for boolean repeats"));
  611.         }
  612.       else
  613.         {
  614.           error (_("can't repeat values of that type"));
  615.         }
  616.     }
  617.   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
  618.            || TYPE_CODE (type1) == TYPE_CODE_CHAR)
  619.     {
  620.       struct cleanup *back_to;

  621.       /* We have two character strings to concatenate.  */
  622.       if (TYPE_CODE (type2) != TYPE_CODE_STRING
  623.           && TYPE_CODE (type2) != TYPE_CODE_CHAR)
  624.         {
  625.           error (_("Strings can only be concatenated with other strings."));
  626.         }
  627.       inval1len = TYPE_LENGTH (type1);
  628.       inval2len = TYPE_LENGTH (type2);
  629.       ptr = (char *) xmalloc (inval1len + inval2len);
  630.       back_to = make_cleanup (xfree, ptr);
  631.       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
  632.         {
  633.           char_type = type1;

  634.           *ptr = (char) unpack_long (type1, value_contents (inval1));
  635.         }
  636.       else
  637.         {
  638.           char_type = TYPE_TARGET_TYPE (type1);

  639.           memcpy (ptr, value_contents (inval1), inval1len);
  640.         }
  641.       if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
  642.         {
  643.           *(ptr + inval1len) =
  644.             (char) unpack_long (type2, value_contents (inval2));
  645.         }
  646.       else
  647.         {
  648.           memcpy (ptr + inval1len, value_contents (inval2), inval2len);
  649.         }
  650.       outval = value_string (ptr, inval1len + inval2len, char_type);
  651.       do_cleanups (back_to);
  652.     }
  653.   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
  654.     {
  655.       /* We have two bitstrings to concatenate.  */
  656.       if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
  657.         {
  658.           error (_("Booleans can only be concatenated "
  659.                    "with other bitstrings or booleans."));
  660.         }
  661.       error (_("unimplemented support for boolean concatenation."));
  662.     }
  663.   else
  664.     {
  665.       /* We don't know how to concatenate these operands.  */
  666.       error (_("illegal operands for concatenation."));
  667.     }
  668.   return (outval);
  669. }

  670. /* Integer exponentiation: V1**V2, where both arguments are
  671.    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */

  672. static LONGEST
  673. integer_pow (LONGEST v1, LONGEST v2)
  674. {
  675.   if (v2 < 0)
  676.     {
  677.       if (v1 == 0)
  678.         error (_("Attempt to raise 0 to negative power."));
  679.       else
  680.         return 0;
  681.     }
  682.   else
  683.     {
  684.       /* The Russian Peasant's Algorithm.  */
  685.       LONGEST v;

  686.       v = 1;
  687.       for (;;)
  688.         {
  689.           if (v2 & 1L)
  690.             v *= v1;
  691.           v2 >>= 1;
  692.           if (v2 == 0)
  693.             return v;
  694.           v1 *= v1;
  695.         }
  696.     }
  697. }

  698. /* Integer exponentiation: V1**V2, where both arguments are
  699.    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */

  700. static ULONGEST
  701. uinteger_pow (ULONGEST v1, LONGEST v2)
  702. {
  703.   if (v2 < 0)
  704.     {
  705.       if (v1 == 0)
  706.         error (_("Attempt to raise 0 to negative power."));
  707.       else
  708.         return 0;
  709.     }
  710.   else
  711.     {
  712.       /* The Russian Peasant's Algorithm.  */
  713.       ULONGEST v;

  714.       v = 1;
  715.       for (;;)
  716.         {
  717.           if (v2 & 1L)
  718.             v *= v1;
  719.           v2 >>= 1;
  720.           if (v2 == 0)
  721.             return v;
  722.           v1 *= v1;
  723.         }
  724.     }
  725. }

  726. /* Obtain decimal value of arguments for binary operation, converting from
  727.    other types if one of them is not decimal floating point.  */
  728. static void
  729. value_args_as_decimal (struct value *arg1, struct value *arg2,
  730.                        gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
  731.                        gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
  732. {
  733.   struct type *type1, *type2;

  734.   type1 = check_typedef (value_type (arg1));
  735.   type2 = check_typedef (value_type (arg2));

  736.   /* At least one of the arguments must be of decimal float type.  */
  737.   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
  738.               || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);

  739.   if (TYPE_CODE (type1) == TYPE_CODE_FLT
  740.       || TYPE_CODE (type2) == TYPE_CODE_FLT)
  741.     /* The DFP extension to the C language does not allow mixing of
  742.      * decimal float types with other float types in expressions
  743.      * (see WDTR 24732, page 12).  */
  744.     error (_("Mixing decimal floating types with "
  745.              "other floating types is not allowed."));

  746.   /* Obtain decimal value of arg1, converting from other types
  747.      if necessary.  */

  748.   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
  749.     {
  750.       *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
  751.       *len_x = TYPE_LENGTH (type1);
  752.       memcpy (x, value_contents (arg1), *len_x);
  753.     }
  754.   else if (is_integral_type (type1))
  755.     {
  756.       *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
  757.       *len_x = TYPE_LENGTH (type2);
  758.       decimal_from_integral (arg1, x, *len_x, *byte_order_x);
  759.     }
  760.   else
  761.     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
  762.              TYPE_NAME (type2));

  763.   /* Obtain decimal value of arg2, converting from other types
  764.      if necessary.  */

  765.   if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
  766.     {
  767.       *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
  768.       *len_y = TYPE_LENGTH (type2);
  769.       memcpy (y, value_contents (arg2), *len_y);
  770.     }
  771.   else if (is_integral_type (type2))
  772.     {
  773.       *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
  774.       *len_y = TYPE_LENGTH (type1);
  775.       decimal_from_integral (arg2, y, *len_y, *byte_order_y);
  776.     }
  777.   else
  778.     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
  779.              TYPE_NAME (type2));
  780. }

  781. /* Perform a binary operation on two operands which have reasonable
  782.    representations as integers or floats.  This includes booleans,
  783.    characters, integers, or floats.
  784.    Does not support addition and subtraction on pointers;
  785.    use value_ptradd, value_ptrsub or value_ptrdiff for those operations.  */

  786. static struct value *
  787. scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
  788. {
  789.   struct value *val;
  790.   struct type *type1, *type2, *result_type;

  791.   arg1 = coerce_ref (arg1);
  792.   arg2 = coerce_ref (arg2);

  793.   type1 = check_typedef (value_type (arg1));
  794.   type2 = check_typedef (value_type (arg2));

  795.   if ((TYPE_CODE (type1) != TYPE_CODE_FLT
  796.        && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
  797.        && !is_integral_type (type1))
  798.       || (TYPE_CODE (type2) != TYPE_CODE_FLT
  799.           && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
  800.           && !is_integral_type (type2)))
  801.     error (_("Argument to arithmetic operation not a number or boolean."));

  802.   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
  803.       || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
  804.     {
  805.       int len_v1, len_v2, len_v;
  806.       enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
  807.       gdb_byte v1[16], v2[16];
  808.       gdb_byte v[16];

  809.       /* If only one type is decimal float, use its type.
  810.          Otherwise use the bigger type.  */
  811.       if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
  812.         result_type = type2;
  813.       else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
  814.         result_type = type1;
  815.       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
  816.         result_type = type2;
  817.       else
  818.         result_type = type1;

  819.       len_v = TYPE_LENGTH (result_type);
  820.       byte_order_v = gdbarch_byte_order (get_type_arch (result_type));

  821.       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
  822.                                          v2, &len_v2, &byte_order_v2);

  823.       switch (op)
  824.         {
  825.         case BINOP_ADD:
  826.         case BINOP_SUB:
  827.         case BINOP_MUL:
  828.         case BINOP_DIV:
  829.         case BINOP_EXP:
  830.           decimal_binop (op, v1, len_v1, byte_order_v1,
  831.                              v2, len_v2, byte_order_v2,
  832.                              v, len_v, byte_order_v);
  833.           break;

  834.         default:
  835.           error (_("Operation not valid for decimal floating point number."));
  836.         }

  837.       val = value_from_decfloat (result_type, v);
  838.     }
  839.   else if (TYPE_CODE (type1) == TYPE_CODE_FLT
  840.            || TYPE_CODE (type2) == TYPE_CODE_FLT)
  841.     {
  842.       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
  843.          in target format.  real.c in GCC probably has the necessary
  844.          code.  */
  845.       DOUBLEST v1, v2, v = 0;

  846.       v1 = value_as_double (arg1);
  847.       v2 = value_as_double (arg2);

  848.       switch (op)
  849.         {
  850.         case BINOP_ADD:
  851.           v = v1 + v2;
  852.           break;

  853.         case BINOP_SUB:
  854.           v = v1 - v2;
  855.           break;

  856.         case BINOP_MUL:
  857.           v = v1 * v2;
  858.           break;

  859.         case BINOP_DIV:
  860.           v = v1 / v2;
  861.           break;

  862.         case BINOP_EXP:
  863.           errno = 0;
  864.           v = pow (v1, v2);
  865.           if (errno)
  866.             error (_("Cannot perform exponentiation: %s"),
  867.                    safe_strerror (errno));
  868.           break;

  869.         case BINOP_MIN:
  870.           v = v1 < v2 ? v1 : v2;
  871.           break;

  872.         case BINOP_MAX:
  873.           v = v1 > v2 ? v1 : v2;
  874.           break;

  875.         default:
  876.           error (_("Integer-only operation on floating point number."));
  877.         }

  878.       /* If only one type is float, use its type.
  879.          Otherwise use the bigger type.  */
  880.       if (TYPE_CODE (type1) != TYPE_CODE_FLT)
  881.         result_type = type2;
  882.       else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
  883.         result_type = type1;
  884.       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
  885.         result_type = type2;
  886.       else
  887.         result_type = type1;

  888.       val = allocate_value (result_type);
  889.       store_typed_floating (value_contents_raw (val), value_type (val), v);
  890.     }
  891.   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
  892.            || TYPE_CODE (type2) == TYPE_CODE_BOOL)
  893.     {
  894.       LONGEST v1, v2, v = 0;

  895.       v1 = value_as_long (arg1);
  896.       v2 = value_as_long (arg2);

  897.       switch (op)
  898.         {
  899.         case BINOP_BITWISE_AND:
  900.           v = v1 & v2;
  901.           break;

  902.         case BINOP_BITWISE_IOR:
  903.           v = v1 | v2;
  904.           break;

  905.         case BINOP_BITWISE_XOR:
  906.           v = v1 ^ v2;
  907.           break;

  908.         case BINOP_EQUAL:
  909.           v = v1 == v2;
  910.           break;

  911.         case BINOP_NOTEQUAL:
  912.           v = v1 != v2;
  913.           break;

  914.         default:
  915.           error (_("Invalid operation on booleans."));
  916.         }

  917.       result_type = type1;

  918.       val = allocate_value (result_type);
  919.       store_signed_integer (value_contents_raw (val),
  920.                             TYPE_LENGTH (result_type),
  921.                             gdbarch_byte_order (get_type_arch (result_type)),
  922.                             v);
  923.     }
  924.   else
  925.     /* Integral operations here.  */
  926.     {
  927.       /* Determine type length of the result, and if the operation should
  928.          be done unsigned.  For exponentiation and shift operators,
  929.          use the length and type of the left operand.  Otherwise,
  930.          use the signedness of the operand with the greater length.
  931.          If both operands are of equal length, use unsigned operation
  932.          if one of the operands is unsigned.  */
  933.       if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
  934.         result_type = type1;
  935.       else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
  936.         result_type = type1;
  937.       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
  938.         result_type = type2;
  939.       else if (TYPE_UNSIGNED (type1))
  940.         result_type = type1;
  941.       else if (TYPE_UNSIGNED (type2))
  942.         result_type = type2;
  943.       else
  944.         result_type = type1;

  945.       if (TYPE_UNSIGNED (result_type))
  946.         {
  947.           LONGEST v2_signed = value_as_long (arg2);
  948.           ULONGEST v1, v2, v = 0;

  949.           v1 = (ULONGEST) value_as_long (arg1);
  950.           v2 = (ULONGEST) v2_signed;

  951.           switch (op)
  952.             {
  953.             case BINOP_ADD:
  954.               v = v1 + v2;
  955.               break;

  956.             case BINOP_SUB:
  957.               v = v1 - v2;
  958.               break;

  959.             case BINOP_MUL:
  960.               v = v1 * v2;
  961.               break;

  962.             case BINOP_DIV:
  963.             case BINOP_INTDIV:
  964.               if (v2 != 0)
  965.                 v = v1 / v2;
  966.               else
  967.                 error (_("Division by zero"));
  968.               break;

  969.             case BINOP_EXP:
  970.               v = uinteger_pow (v1, v2_signed);
  971.               break;

  972.             case BINOP_REM:
  973.               if (v2 != 0)
  974.                 v = v1 % v2;
  975.               else
  976.                 error (_("Division by zero"));
  977.               break;

  978.             case BINOP_MOD:
  979.               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
  980.                  v1 mod 0 has a defined value, v1.  */
  981.               if (v2 == 0)
  982.                 {
  983.                   v = v1;
  984.                 }
  985.               else
  986.                 {
  987.                   v = v1 / v2;
  988.                   /* Note floor(v1/v2) == v1/v2 for unsigned.  */
  989.                   v = v1 - (v2 * v);
  990.                 }
  991.               break;

  992.             case BINOP_LSH:
  993.               v = v1 << v2;
  994.               break;

  995.             case BINOP_RSH:
  996.               v = v1 >> v2;
  997.               break;

  998.             case BINOP_BITWISE_AND:
  999.               v = v1 & v2;
  1000.               break;

  1001.             case BINOP_BITWISE_IOR:
  1002.               v = v1 | v2;
  1003.               break;

  1004.             case BINOP_BITWISE_XOR:
  1005.               v = v1 ^ v2;
  1006.               break;

  1007.             case BINOP_LOGICAL_AND:
  1008.               v = v1 && v2;
  1009.               break;

  1010.             case BINOP_LOGICAL_OR:
  1011.               v = v1 || v2;
  1012.               break;

  1013.             case BINOP_MIN:
  1014.               v = v1 < v2 ? v1 : v2;
  1015.               break;

  1016.             case BINOP_MAX:
  1017.               v = v1 > v2 ? v1 : v2;
  1018.               break;

  1019.             case BINOP_EQUAL:
  1020.               v = v1 == v2;
  1021.               break;

  1022.             case BINOP_NOTEQUAL:
  1023.               v = v1 != v2;
  1024.               break;

  1025.             case BINOP_LESS:
  1026.               v = v1 < v2;
  1027.               break;

  1028.             case BINOP_GTR:
  1029.               v = v1 > v2;
  1030.               break;

  1031.             case BINOP_LEQ:
  1032.               v = v1 <= v2;
  1033.               break;

  1034.             case BINOP_GEQ:
  1035.               v = v1 >= v2;
  1036.               break;

  1037.             default:
  1038.               error (_("Invalid binary operation on numbers."));
  1039.             }

  1040.           val = allocate_value (result_type);
  1041.           store_unsigned_integer (value_contents_raw (val),
  1042.                                   TYPE_LENGTH (value_type (val)),
  1043.                                   gdbarch_byte_order
  1044.                                     (get_type_arch (result_type)),
  1045.                                   v);
  1046.         }
  1047.       else
  1048.         {
  1049.           LONGEST v1, v2, v = 0;

  1050.           v1 = value_as_long (arg1);
  1051.           v2 = value_as_long (arg2);

  1052.           switch (op)
  1053.             {
  1054.             case BINOP_ADD:
  1055.               v = v1 + v2;
  1056.               break;

  1057.             case BINOP_SUB:
  1058.               v = v1 - v2;
  1059.               break;

  1060.             case BINOP_MUL:
  1061.               v = v1 * v2;
  1062.               break;

  1063.             case BINOP_DIV:
  1064.             case BINOP_INTDIV:
  1065.               if (v2 != 0)
  1066.                 v = v1 / v2;
  1067.               else
  1068.                 error (_("Division by zero"));
  1069.               break;

  1070.             case BINOP_EXP:
  1071.               v = integer_pow (v1, v2);
  1072.               break;

  1073.             case BINOP_REM:
  1074.               if (v2 != 0)
  1075.                 v = v1 % v2;
  1076.               else
  1077.                 error (_("Division by zero"));
  1078.               break;

  1079.             case BINOP_MOD:
  1080.               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
  1081.                  X mod 0 has a defined value, X.  */
  1082.               if (v2 == 0)
  1083.                 {
  1084.                   v = v1;
  1085.                 }
  1086.               else
  1087.                 {
  1088.                   v = v1 / v2;
  1089.                   /* Compute floor.  */
  1090.                   if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
  1091.                     {
  1092.                       v--;
  1093.                     }
  1094.                   v = v1 - (v2 * v);
  1095.                 }
  1096.               break;

  1097.             case BINOP_LSH:
  1098.               v = v1 << v2;
  1099.               break;

  1100.             case BINOP_RSH:
  1101.               v = v1 >> v2;
  1102.               break;

  1103.             case BINOP_BITWISE_AND:
  1104.               v = v1 & v2;
  1105.               break;

  1106.             case BINOP_BITWISE_IOR:
  1107.               v = v1 | v2;
  1108.               break;

  1109.             case BINOP_BITWISE_XOR:
  1110.               v = v1 ^ v2;
  1111.               break;

  1112.             case BINOP_LOGICAL_AND:
  1113.               v = v1 && v2;
  1114.               break;

  1115.             case BINOP_LOGICAL_OR:
  1116.               v = v1 || v2;
  1117.               break;

  1118.             case BINOP_MIN:
  1119.               v = v1 < v2 ? v1 : v2;
  1120.               break;

  1121.             case BINOP_MAX:
  1122.               v = v1 > v2 ? v1 : v2;
  1123.               break;

  1124.             case BINOP_EQUAL:
  1125.               v = v1 == v2;
  1126.               break;

  1127.             case BINOP_NOTEQUAL:
  1128.               v = v1 != v2;
  1129.               break;

  1130.             case BINOP_LESS:
  1131.               v = v1 < v2;
  1132.               break;

  1133.             case BINOP_GTR:
  1134.               v = v1 > v2;
  1135.               break;

  1136.             case BINOP_LEQ:
  1137.               v = v1 <= v2;
  1138.               break;

  1139.             case BINOP_GEQ:
  1140.               v = v1 >= v2;
  1141.               break;

  1142.             default:
  1143.               error (_("Invalid binary operation on numbers."));
  1144.             }

  1145.           val = allocate_value (result_type);
  1146.           store_signed_integer (value_contents_raw (val),
  1147.                                 TYPE_LENGTH (value_type (val)),
  1148.                                 gdbarch_byte_order
  1149.                                   (get_type_arch (result_type)),
  1150.                                 v);
  1151.         }
  1152.     }

  1153.   return val;
  1154. }

  1155. /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
  1156.    replicating SCALAR_VALUE for each element of the vector.  Only scalar
  1157.    types that can be cast to the type of one element of the vector are
  1158.    acceptable.  The newly created vector value is returned upon success,
  1159.    otherwise an error is thrown.  */

  1160. struct value *
  1161. value_vector_widen (struct value *scalar_value, struct type *vector_type)
  1162. {
  1163.   /* Widen the scalar to a vector.  */
  1164.   struct type *eltype, *scalar_type;
  1165.   struct value *val, *elval;
  1166.   LONGEST low_bound, high_bound;
  1167.   int i;

  1168.   CHECK_TYPEDEF (vector_type);

  1169.   gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
  1170.               && TYPE_VECTOR (vector_type));

  1171.   if (!get_array_bounds (vector_type, &low_bound, &high_bound))
  1172.     error (_("Could not determine the vector bounds"));

  1173.   eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
  1174.   elval = value_cast (eltype, scalar_value);

  1175.   scalar_type = check_typedef (value_type (scalar_value));

  1176.   /* If we reduced the length of the scalar then check we didn't loose any
  1177.      important bits.  */
  1178.   if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
  1179.       && !value_equal (elval, scalar_value))
  1180.     error (_("conversion of scalar to vector involves truncation"));

  1181.   val = allocate_value (vector_type);
  1182.   for (i = 0; i < high_bound - low_bound + 1; i++)
  1183.     /* Duplicate the contents of elval into the destination vector.  */
  1184.     memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
  1185.             value_contents_all (elval), TYPE_LENGTH (eltype));

  1186.   return val;
  1187. }

  1188. /* Performs a binary operation on two vector operands by calling scalar_binop
  1189.    for each pair of vector components.  */

  1190. static struct value *
  1191. vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
  1192. {
  1193.   struct value *val, *tmp, *mark;
  1194.   struct type *type1, *type2, *eltype1, *eltype2;
  1195.   int t1_is_vec, t2_is_vec, elsize, i;
  1196.   LONGEST low_bound1, high_bound1, low_bound2, high_bound2;

  1197.   type1 = check_typedef (value_type (val1));
  1198.   type2 = check_typedef (value_type (val2));

  1199.   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
  1200.                && TYPE_VECTOR (type1)) ? 1 : 0;
  1201.   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
  1202.                && TYPE_VECTOR (type2)) ? 1 : 0;

  1203.   if (!t1_is_vec || !t2_is_vec)
  1204.     error (_("Vector operations are only supported among vectors"));

  1205.   if (!get_array_bounds (type1, &low_bound1, &high_bound1)
  1206.       || !get_array_bounds (type2, &low_bound2, &high_bound2))
  1207.     error (_("Could not determine the vector bounds"));

  1208.   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
  1209.   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
  1210.   elsize = TYPE_LENGTH (eltype1);

  1211.   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
  1212.       || elsize != TYPE_LENGTH (eltype2)
  1213.       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
  1214.       || low_bound1 != low_bound2 || high_bound1 != high_bound2)
  1215.     error (_("Cannot perform operation on vectors with different types"));

  1216.   val = allocate_value (type1);
  1217.   mark = value_mark ();
  1218.   for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
  1219.     {
  1220.       tmp = value_binop (value_subscript (val1, i),
  1221.                          value_subscript (val2, i), op);
  1222.       memcpy (value_contents_writeable (val) + i * elsize,
  1223.               value_contents_all (tmp),
  1224.               elsize);
  1225.      }
  1226.   value_free_to_mark (mark);

  1227.   return val;
  1228. }

  1229. /* Perform a binary operation on two operands.  */

  1230. struct value *
  1231. value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
  1232. {
  1233.   struct value *val;
  1234.   struct type *type1 = check_typedef (value_type (arg1));
  1235.   struct type *type2 = check_typedef (value_type (arg2));
  1236.   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
  1237.                    && TYPE_VECTOR (type1));
  1238.   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
  1239.                    && TYPE_VECTOR (type2));

  1240.   if (!t1_is_vec && !t2_is_vec)
  1241.     val = scalar_binop (arg1, arg2, op);
  1242.   else if (t1_is_vec && t2_is_vec)
  1243.     val = vector_binop (arg1, arg2, op);
  1244.   else
  1245.     {
  1246.       /* Widen the scalar operand to a vector.  */
  1247.       struct value **v = t1_is_vec ? &arg2 : &arg1;
  1248.       struct type *t = t1_is_vec ? type2 : type1;

  1249.       if (TYPE_CODE (t) != TYPE_CODE_FLT
  1250.           && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
  1251.           && !is_integral_type (t))
  1252.         error (_("Argument to operation not a number or boolean."));

  1253.       /* Replicate the scalar value to make a vector value.  */
  1254.       *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);

  1255.       val = vector_binop (arg1, arg2, op);
  1256.     }

  1257.   return val;
  1258. }

  1259. /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */

  1260. int
  1261. value_logical_not (struct value *arg1)
  1262. {
  1263.   int len;
  1264.   const gdb_byte *p;
  1265.   struct type *type1;

  1266.   arg1 = coerce_array (arg1);
  1267.   type1 = check_typedef (value_type (arg1));

  1268.   if (TYPE_CODE (type1) == TYPE_CODE_FLT)
  1269.     return 0 == value_as_double (arg1);
  1270.   else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
  1271.     return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
  1272.                             gdbarch_byte_order (get_type_arch (type1)));

  1273.   len = TYPE_LENGTH (type1);
  1274.   p = value_contents (arg1);

  1275.   while (--len >= 0)
  1276.     {
  1277.       if (*p++)
  1278.         break;
  1279.     }

  1280.   return len < 0;
  1281. }

  1282. /* Perform a comparison on two string values (whose content are not
  1283.    necessarily null terminated) based on their length.  */

  1284. static int
  1285. value_strcmp (struct value *arg1, struct value *arg2)
  1286. {
  1287.   int len1 = TYPE_LENGTH (value_type (arg1));
  1288.   int len2 = TYPE_LENGTH (value_type (arg2));
  1289.   const gdb_byte *s1 = value_contents (arg1);
  1290.   const gdb_byte *s2 = value_contents (arg2);
  1291.   int i, len = len1 < len2 ? len1 : len2;

  1292.   for (i = 0; i < len; i++)
  1293.     {
  1294.       if (s1[i] < s2[i])
  1295.         return -1;
  1296.       else if (s1[i] > s2[i])
  1297.         return 1;
  1298.       else
  1299.         continue;
  1300.     }

  1301.   if (len1 < len2)
  1302.     return -1;
  1303.   else if (len1 > len2)
  1304.     return 1;
  1305.   else
  1306.     return 0;
  1307. }

  1308. /* Simulate the C operator == by returning a 1
  1309.    iff ARG1 and ARG2 have equal contents.  */

  1310. int
  1311. value_equal (struct value *arg1, struct value *arg2)
  1312. {
  1313.   int len;
  1314.   const gdb_byte *p1;
  1315.   const gdb_byte *p2;
  1316.   struct type *type1, *type2;
  1317.   enum type_code code1;
  1318.   enum type_code code2;
  1319.   int is_int1, is_int2;

  1320.   arg1 = coerce_array (arg1);
  1321.   arg2 = coerce_array (arg2);

  1322.   type1 = check_typedef (value_type (arg1));
  1323.   type2 = check_typedef (value_type (arg2));
  1324.   code1 = TYPE_CODE (type1);
  1325.   code2 = TYPE_CODE (type2);
  1326.   is_int1 = is_integral_type (type1);
  1327.   is_int2 = is_integral_type (type2);

  1328.   if (is_int1 && is_int2)
  1329.     return longest_to_int (value_as_long (value_binop (arg1, arg2,
  1330.                                                        BINOP_EQUAL)));
  1331.   else if ((code1 == TYPE_CODE_FLT || is_int1)
  1332.            && (code2 == TYPE_CODE_FLT || is_int2))
  1333.     {
  1334.       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
  1335.          `long double' values are returned in static storage (m68k).  */
  1336.       DOUBLEST d = value_as_double (arg1);

  1337.       return d == value_as_double (arg2);
  1338.     }
  1339.   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
  1340.            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
  1341.     {
  1342.       gdb_byte v1[16], v2[16];
  1343.       int len_v1, len_v2;
  1344.       enum bfd_endian byte_order_v1, byte_order_v2;

  1345.       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
  1346.                                          v2, &len_v2, &byte_order_v2);

  1347.       return decimal_compare (v1, len_v1, byte_order_v1,
  1348.                               v2, len_v2, byte_order_v2) == 0;
  1349.     }

  1350.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  1351.      is bigger.  */
  1352.   else if (code1 == TYPE_CODE_PTR && is_int2)
  1353.     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
  1354.   else if (code2 == TYPE_CODE_PTR && is_int1)
  1355.     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);

  1356.   else if (code1 == code2
  1357.            && ((len = (int) TYPE_LENGTH (type1))
  1358.                == (int) TYPE_LENGTH (type2)))
  1359.     {
  1360.       p1 = value_contents (arg1);
  1361.       p2 = value_contents (arg2);
  1362.       while (--len >= 0)
  1363.         {
  1364.           if (*p1++ != *p2++)
  1365.             break;
  1366.         }
  1367.       return len < 0;
  1368.     }
  1369.   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
  1370.     {
  1371.       return value_strcmp (arg1, arg2) == 0;
  1372.     }
  1373.   else
  1374.     {
  1375.       error (_("Invalid type combination in equality test."));
  1376.       return 0;                        /* For lint -- never reached.  */
  1377.     }
  1378. }

  1379. /* Compare values based on their raw contents.  Useful for arrays since
  1380.    value_equal coerces them to pointers, thus comparing just the address
  1381.    of the array instead of its contents.  */

  1382. int
  1383. value_equal_contents (struct value *arg1, struct value *arg2)
  1384. {
  1385.   struct type *type1, *type2;

  1386.   type1 = check_typedef (value_type (arg1));
  1387.   type2 = check_typedef (value_type (arg2));

  1388.   return (TYPE_CODE (type1) == TYPE_CODE (type2)
  1389.           && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
  1390.           && memcmp (value_contents (arg1), value_contents (arg2),
  1391.                      TYPE_LENGTH (type1)) == 0);
  1392. }

  1393. /* Simulate the C operator < by returning 1
  1394.    iff ARG1's contents are less than ARG2's.  */

  1395. int
  1396. value_less (struct value *arg1, struct value *arg2)
  1397. {
  1398.   enum type_code code1;
  1399.   enum type_code code2;
  1400.   struct type *type1, *type2;
  1401.   int is_int1, is_int2;

  1402.   arg1 = coerce_array (arg1);
  1403.   arg2 = coerce_array (arg2);

  1404.   type1 = check_typedef (value_type (arg1));
  1405.   type2 = check_typedef (value_type (arg2));
  1406.   code1 = TYPE_CODE (type1);
  1407.   code2 = TYPE_CODE (type2);
  1408.   is_int1 = is_integral_type (type1);
  1409.   is_int2 = is_integral_type (type2);

  1410.   if (is_int1 && is_int2)
  1411.     return longest_to_int (value_as_long (value_binop (arg1, arg2,
  1412.                                                        BINOP_LESS)));
  1413.   else if ((code1 == TYPE_CODE_FLT || is_int1)
  1414.            && (code2 == TYPE_CODE_FLT || is_int2))
  1415.     {
  1416.       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
  1417.          `long double' values are returned in static storage (m68k).  */
  1418.       DOUBLEST d = value_as_double (arg1);

  1419.       return d < value_as_double (arg2);
  1420.     }
  1421.   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
  1422.            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
  1423.     {
  1424.       gdb_byte v1[16], v2[16];
  1425.       int len_v1, len_v2;
  1426.       enum bfd_endian byte_order_v1, byte_order_v2;

  1427.       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
  1428.                                          v2, &len_v2, &byte_order_v2);

  1429.       return decimal_compare (v1, len_v1, byte_order_v1,
  1430.                               v2, len_v2, byte_order_v2) == -1;
  1431.     }
  1432.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
  1433.     return value_as_address (arg1) < value_as_address (arg2);

  1434.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  1435.      is bigger.  */
  1436.   else if (code1 == TYPE_CODE_PTR && is_int2)
  1437.     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
  1438.   else if (code2 == TYPE_CODE_PTR && is_int1)
  1439.     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
  1440.   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
  1441.     return value_strcmp (arg1, arg2) < 0;
  1442.   else
  1443.     {
  1444.       error (_("Invalid type combination in ordering comparison."));
  1445.       return 0;
  1446.     }
  1447. }

  1448. /* The unary operators +, - and ~.  They free the argument ARG1.  */

  1449. struct value *
  1450. value_pos (struct value *arg1)
  1451. {
  1452.   struct type *type;

  1453.   arg1 = coerce_ref (arg1);
  1454.   type = check_typedef (value_type (arg1));

  1455.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  1456.     return value_from_double (type, value_as_double (arg1));
  1457.   else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  1458.     return value_from_decfloat (type, value_contents (arg1));
  1459.   else if (is_integral_type (type))
  1460.     {
  1461.       return value_from_longest (type, value_as_long (arg1));
  1462.     }
  1463.   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
  1464.     {
  1465.       struct value *val = allocate_value (type);

  1466.       memcpy (value_contents_raw (val), value_contents (arg1),
  1467.               TYPE_LENGTH (type));
  1468.       return val;
  1469.     }
  1470.   else
  1471.     {
  1472.       error (_("Argument to positive operation not a number."));
  1473.       return 0;                        /* For lint -- never reached.  */
  1474.     }
  1475. }

  1476. struct value *
  1477. value_neg (struct value *arg1)
  1478. {
  1479.   struct type *type;

  1480.   arg1 = coerce_ref (arg1);
  1481.   type = check_typedef (value_type (arg1));

  1482.   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  1483.     {
  1484.       struct value *val = allocate_value (type);
  1485.       int len = TYPE_LENGTH (type);
  1486.       gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long.  */

  1487.       memcpy (decbytes, value_contents (arg1), len);

  1488.       if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
  1489.         decbytes[len-1] = decbytes[len - 1] | 0x80;
  1490.       else
  1491.         decbytes[0] = decbytes[0] | 0x80;

  1492.       memcpy (value_contents_raw (val), decbytes, len);
  1493.       return val;
  1494.     }
  1495.   else if (TYPE_CODE (type) == TYPE_CODE_FLT)
  1496.     return value_from_double (type, -value_as_double (arg1));
  1497.   else if (is_integral_type (type))
  1498.     {
  1499.       return value_from_longest (type, -value_as_long (arg1));
  1500.     }
  1501.   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
  1502.     {
  1503.       struct value *tmp, *val = allocate_value (type);
  1504.       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
  1505.       int i;
  1506.       LONGEST low_bound, high_bound;

  1507.       if (!get_array_bounds (type, &low_bound, &high_bound))
  1508.         error (_("Could not determine the vector bounds"));

  1509.       for (i = 0; i < high_bound - low_bound + 1; i++)
  1510.         {
  1511.           tmp = value_neg (value_subscript (arg1, i));
  1512.           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
  1513.                   value_contents_all (tmp), TYPE_LENGTH (eltype));
  1514.         }
  1515.       return val;
  1516.     }
  1517.   else
  1518.     {
  1519.       error (_("Argument to negate operation not a number."));
  1520.       return 0;                        /* For lint -- never reached.  */
  1521.     }
  1522. }

  1523. struct value *
  1524. value_complement (struct value *arg1)
  1525. {
  1526.   struct type *type;
  1527.   struct value *val;

  1528.   arg1 = coerce_ref (arg1);
  1529.   type = check_typedef (value_type (arg1));

  1530.   if (is_integral_type (type))
  1531.     val = value_from_longest (type, ~value_as_long (arg1));
  1532.   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
  1533.     {
  1534.       struct value *tmp;
  1535.       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
  1536.       int i;
  1537.       LONGEST low_bound, high_bound;

  1538.       if (!get_array_bounds (type, &low_bound, &high_bound))
  1539.         error (_("Could not determine the vector bounds"));

  1540.       val = allocate_value (type);
  1541.       for (i = 0; i < high_bound - low_bound + 1; i++)
  1542.         {
  1543.           tmp = value_complement (value_subscript (arg1, i));
  1544.           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
  1545.                   value_contents_all (tmp), TYPE_LENGTH (eltype));
  1546.         }
  1547.     }
  1548.   else
  1549.     error (_("Argument to complement operation not an integer, boolean."));

  1550.   return val;
  1551. }

  1552. /* The INDEX'th bit of SET value whose value_type is TYPE,
  1553.    and whose value_contents is valaddr.
  1554.    Return -1 if out of range, -2 other error.  */

  1555. int
  1556. value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
  1557. {
  1558.   struct gdbarch *gdbarch = get_type_arch (type);
  1559.   LONGEST low_bound, high_bound;
  1560.   LONGEST word;
  1561.   unsigned rel_index;
  1562.   struct type *range = TYPE_INDEX_TYPE (type);

  1563.   if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
  1564.     return -2;
  1565.   if (index < low_bound || index > high_bound)
  1566.     return -1;
  1567.   rel_index = index - low_bound;
  1568.   word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
  1569.                                    gdbarch_byte_order (gdbarch));
  1570.   rel_index %= TARGET_CHAR_BIT;
  1571.   if (gdbarch_bits_big_endian (gdbarch))
  1572.     rel_index = TARGET_CHAR_BIT - 1 - rel_index;
  1573.   return (word >> rel_index) & 1;
  1574. }

  1575. int
  1576. value_in (struct value *element, struct value *set)
  1577. {
  1578.   int member;
  1579.   struct type *settype = check_typedef (value_type (set));
  1580.   struct type *eltype = check_typedef (value_type (element));

  1581.   if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
  1582.     eltype = TYPE_TARGET_TYPE (eltype);
  1583.   if (TYPE_CODE (settype) != TYPE_CODE_SET)
  1584.     error (_("Second argument of 'IN' has wrong type"));
  1585.   if (TYPE_CODE (eltype) != TYPE_CODE_INT
  1586.       && TYPE_CODE (eltype) != TYPE_CODE_CHAR
  1587.       && TYPE_CODE (eltype) != TYPE_CODE_ENUM
  1588.       && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
  1589.     error (_("First argument of 'IN' has wrong type"));
  1590.   member = value_bit_index (settype, value_contents (set),
  1591.                             value_as_long (element));
  1592.   if (member < 0)
  1593.     error (_("First argument of 'IN' not in range"));
  1594.   return member;
  1595. }

  1596. void
  1597. _initialize_valarith (void)
  1598. {
  1599. }