GDB (API)
/home/stan/gdb/src/gdb/valarith.c
Go to the documentation of this file.
00001 /* Perform arithmetic and other operations on values, for GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "value.h"
00022 #include "symtab.h"
00023 #include "gdbtypes.h"
00024 #include "expression.h"
00025 #include "target.h"
00026 #include "language.h"
00027 #include "gdb_string.h"
00028 #include "doublest.h"
00029 #include "dfp.h"
00030 #include <math.h>
00031 #include "infcall.h"
00032 #include "exceptions.h"
00033 
00034 /* Define whether or not the C operator '/' truncates towards zero for
00035    differently signed operands (truncation direction is undefined in C).  */
00036 
00037 #ifndef TRUNCATION_TOWARDS_ZERO
00038 #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
00039 #endif
00040 
00041 void _initialize_valarith (void);
00042 
00043 
00044 /* Given a pointer, return the size of its target.
00045    If the pointer type is void *, then return 1.
00046    If the target type is incomplete, then error out.
00047    This isn't a general purpose function, but just a 
00048    helper for value_ptradd.  */
00049 
00050 static LONGEST
00051 find_size_for_pointer_math (struct type *ptr_type)
00052 {
00053   LONGEST sz = -1;
00054   struct type *ptr_target;
00055 
00056   gdb_assert (TYPE_CODE (ptr_type) == TYPE_CODE_PTR);
00057   ptr_target = check_typedef (TYPE_TARGET_TYPE (ptr_type));
00058 
00059   sz = TYPE_LENGTH (ptr_target);
00060   if (sz == 0)
00061     {
00062       if (TYPE_CODE (ptr_type) == TYPE_CODE_VOID)
00063         sz = 1;
00064       else
00065         {
00066           const char *name;
00067           
00068           name = TYPE_NAME (ptr_target);
00069           if (name == NULL)
00070             name = TYPE_TAG_NAME (ptr_target);
00071           if (name == NULL)
00072             error (_("Cannot perform pointer math on incomplete types, "
00073                    "try casting to a known type, or void *."));
00074           else
00075             error (_("Cannot perform pointer math on incomplete type \"%s\", "
00076                    "try casting to a known type, or void *."), name);
00077         }
00078     }
00079   return sz;
00080 }
00081 
00082 /* Given a pointer ARG1 and an integral value ARG2, return the
00083    result of C-style pointer arithmetic ARG1 + ARG2.  */
00084 
00085 struct value *
00086 value_ptradd (struct value *arg1, LONGEST arg2)
00087 {
00088   struct type *valptrtype;
00089   LONGEST sz;
00090   struct value *result;
00091 
00092   arg1 = coerce_array (arg1);
00093   valptrtype = check_typedef (value_type (arg1));
00094   sz = find_size_for_pointer_math (valptrtype);
00095 
00096   result = value_from_pointer (valptrtype,
00097                                value_as_address (arg1) + sz * arg2);
00098   if (VALUE_LVAL (result) != lval_internalvar)
00099     set_value_component_location (result, arg1);
00100   return result;
00101 }
00102 
00103 /* Given two compatible pointer values ARG1 and ARG2, return the
00104    result of C-style pointer arithmetic ARG1 - ARG2.  */
00105 
00106 LONGEST
00107 value_ptrdiff (struct value *arg1, struct value *arg2)
00108 {
00109   struct type *type1, *type2;
00110   LONGEST sz;
00111 
00112   arg1 = coerce_array (arg1);
00113   arg2 = coerce_array (arg2);
00114   type1 = check_typedef (value_type (arg1));
00115   type2 = check_typedef (value_type (arg2));
00116 
00117   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_PTR);
00118   gdb_assert (TYPE_CODE (type2) == TYPE_CODE_PTR);
00119 
00120   if (TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)))
00121       != TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type2))))
00122     error (_("First argument of `-' is a pointer and "
00123              "second argument is neither\n"
00124              "an integer nor a pointer of the same type."));
00125 
00126   sz = TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type1)));
00127   if (sz == 0) 
00128     {
00129       warning (_("Type size unknown, assuming 1. "
00130                "Try casting to a known type, or void *."));
00131       sz = 1;
00132     }
00133 
00134   return (value_as_long (arg1) - value_as_long (arg2)) / sz;
00135 }
00136 
00137 /* Return the value of ARRAY[IDX].
00138 
00139    ARRAY may be of type TYPE_CODE_ARRAY or TYPE_CODE_STRING.  If the
00140    current language supports C-style arrays, it may also be TYPE_CODE_PTR.
00141 
00142    See comments in value_coerce_array() for rationale for reason for
00143    doing lower bounds adjustment here rather than there.
00144    FIXME:  Perhaps we should validate that the index is valid and if
00145    verbosity is set, warn about invalid indices (but still use them).  */
00146 
00147 struct value *
00148 value_subscript (struct value *array, LONGEST index)
00149 {
00150   int c_style = current_language->c_style_arrays;
00151   struct type *tarray;
00152 
00153   array = coerce_ref (array);
00154   tarray = check_typedef (value_type (array));
00155 
00156   if (TYPE_CODE (tarray) == TYPE_CODE_ARRAY
00157       || TYPE_CODE (tarray) == TYPE_CODE_STRING)
00158     {
00159       struct type *range_type = TYPE_INDEX_TYPE (tarray);
00160       LONGEST lowerbound, upperbound;
00161 
00162       get_discrete_bounds (range_type, &lowerbound, &upperbound);
00163       if (VALUE_LVAL (array) != lval_memory)
00164         return value_subscripted_rvalue (array, index, lowerbound);
00165 
00166       if (c_style == 0)
00167         {
00168           if (index >= lowerbound && index <= upperbound)
00169             return value_subscripted_rvalue (array, index, lowerbound);
00170           /* Emit warning unless we have an array of unknown size.
00171              An array of unknown size has lowerbound 0 and upperbound -1.  */
00172           if (upperbound > -1)
00173             warning (_("array or string index out of range"));
00174           /* fall doing C stuff */
00175           c_style = 1;
00176         }
00177 
00178       index -= lowerbound;
00179       array = value_coerce_array (array);
00180     }
00181 
00182   if (c_style)
00183     return value_ind (value_ptradd (array, index));
00184   else
00185     error (_("not an array or string"));
00186 }
00187 
00188 /* Return the value of EXPR[IDX], expr an aggregate rvalue
00189    (eg, a vector register).  This routine used to promote floats
00190    to doubles, but no longer does.  */
00191 
00192 struct value *
00193 value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound)
00194 {
00195   struct type *array_type = check_typedef (value_type (array));
00196   struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (array_type));
00197   unsigned int elt_size = TYPE_LENGTH (elt_type);
00198   unsigned int elt_offs = elt_size * longest_to_int (index - lowerbound);
00199   struct value *v;
00200 
00201   if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type)
00202                              && elt_offs >= TYPE_LENGTH (array_type)))
00203     error (_("no such vector element"));
00204 
00205   if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
00206     v = allocate_value_lazy (elt_type);
00207   else
00208     {
00209       v = allocate_value (elt_type);
00210       value_contents_copy (v, value_embedded_offset (v),
00211                            array, value_embedded_offset (array) + elt_offs,
00212                            elt_size);
00213     }
00214 
00215   set_value_component_location (v, array);
00216   VALUE_REGNUM (v) = VALUE_REGNUM (array);
00217   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (array);
00218   set_value_offset (v, value_offset (array) + elt_offs);
00219   return v;
00220 }
00221 
00222 
00223 /* Check to see if either argument is a structure, or a reference to
00224    one.  This is called so we know whether to go ahead with the normal
00225    binop or look for a user defined function instead.
00226 
00227    For now, we do not overload the `=' operator.  */
00228 
00229 int
00230 binop_types_user_defined_p (enum exp_opcode op,
00231                             struct type *type1, struct type *type2)
00232 {
00233   if (op == BINOP_ASSIGN || op == BINOP_CONCAT)
00234     return 0;
00235 
00236   type1 = check_typedef (type1);
00237   if (TYPE_CODE (type1) == TYPE_CODE_REF)
00238     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
00239 
00240   type2 = check_typedef (type2);
00241   if (TYPE_CODE (type2) == TYPE_CODE_REF)
00242     type2 = check_typedef (TYPE_TARGET_TYPE (type2));
00243 
00244   return (TYPE_CODE (type1) == TYPE_CODE_STRUCT
00245           || TYPE_CODE (type2) == TYPE_CODE_STRUCT);
00246 }
00247 
00248 /* Check to see if either argument is a structure, or a reference to
00249    one.  This is called so we know whether to go ahead with the normal
00250    binop or look for a user defined function instead.
00251 
00252    For now, we do not overload the `=' operator.  */
00253 
00254 int
00255 binop_user_defined_p (enum exp_opcode op,
00256                       struct value *arg1, struct value *arg2)
00257 {
00258   return binop_types_user_defined_p (op, value_type (arg1), value_type (arg2));
00259 }
00260 
00261 /* Check to see if argument is a structure.  This is called so
00262    we know whether to go ahead with the normal unop or look for a 
00263    user defined function instead.
00264 
00265    For now, we do not overload the `&' operator.  */
00266 
00267 int
00268 unop_user_defined_p (enum exp_opcode op, struct value *arg1)
00269 {
00270   struct type *type1;
00271 
00272   if (op == UNOP_ADDR)
00273     return 0;
00274   type1 = check_typedef (value_type (arg1));
00275   if (TYPE_CODE (type1) == TYPE_CODE_REF)
00276     type1 = check_typedef (TYPE_TARGET_TYPE (type1));
00277   return TYPE_CODE (type1) == TYPE_CODE_STRUCT;
00278 }
00279 
00280 /* Try to find an operator named OPERATOR which takes NARGS arguments
00281    specified in ARGS.  If the operator found is a static member operator
00282    *STATIC_MEMFUNP will be set to 1, and otherwise 0.
00283    The search if performed through find_overload_match which will handle
00284    member operators, non member operators, operators imported implicitly or
00285    explicitly, and perform correct overload resolution in all of the above
00286    situations or combinations thereof.  */
00287 
00288 static struct value *
00289 value_user_defined_cpp_op (struct value **args, int nargs, char *operator,
00290                            int *static_memfuncp)
00291 {
00292 
00293   struct symbol *symp = NULL;
00294   struct value *valp = NULL;
00295 
00296   find_overload_match (args, nargs, operator, BOTH /* could be method */,
00297                        &args[0] /* objp */,
00298                        NULL /* pass NULL symbol since symbol is unknown */,
00299                        &valp, &symp, static_memfuncp, 0);
00300 
00301   if (valp)
00302     return valp;
00303 
00304   if (symp)
00305     {
00306       /* This is a non member function and does not
00307          expect a reference as its first argument
00308          rather the explicit structure.  */
00309       args[0] = value_ind (args[0]);
00310       return value_of_variable (symp, 0);
00311     }
00312 
00313   error (_("Could not find %s."), operator);
00314 }
00315 
00316 /* Lookup user defined operator NAME.  Return a value representing the
00317    function, otherwise return NULL.  */
00318 
00319 static struct value *
00320 value_user_defined_op (struct value **argp, struct value **args, char *name,
00321                        int *static_memfuncp, int nargs)
00322 {
00323   struct value *result = NULL;
00324 
00325   if (current_language->la_language == language_cplus)
00326     result = value_user_defined_cpp_op (args, nargs, name, static_memfuncp);
00327   else
00328     result = value_struct_elt (argp, args, name, static_memfuncp,
00329                                "structure");
00330 
00331   return result;
00332 }
00333 
00334 /* We know either arg1 or arg2 is a structure, so try to find the right
00335    user defined function.  Create an argument vector that calls 
00336    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
00337    binary operator which is legal for GNU C++).
00338 
00339    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
00340    is the opcode saying how to modify it.  Otherwise, OTHEROP is
00341    unused.  */
00342 
00343 struct value *
00344 value_x_binop (struct value *arg1, struct value *arg2, enum exp_opcode op,
00345                enum exp_opcode otherop, enum noside noside)
00346 {
00347   struct value **argvec;
00348   char *ptr;
00349   char tstr[13];
00350   int static_memfuncp;
00351 
00352   arg1 = coerce_ref (arg1);
00353   arg2 = coerce_ref (arg2);
00354 
00355   /* now we know that what we have to do is construct our
00356      arg vector and find the right function to call it with.  */
00357 
00358   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
00359     error (_("Can't do that binary op on that type"));  /* FIXME be explicit */
00360 
00361   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
00362   argvec[1] = value_addr (arg1);
00363   argvec[2] = arg2;
00364   argvec[3] = 0;
00365 
00366   /* Make the right function name up.  */
00367   strcpy (tstr, "operator__");
00368   ptr = tstr + 8;
00369   switch (op)
00370     {
00371     case BINOP_ADD:
00372       strcpy (ptr, "+");
00373       break;
00374     case BINOP_SUB:
00375       strcpy (ptr, "-");
00376       break;
00377     case BINOP_MUL:
00378       strcpy (ptr, "*");
00379       break;
00380     case BINOP_DIV:
00381       strcpy (ptr, "/");
00382       break;
00383     case BINOP_REM:
00384       strcpy (ptr, "%");
00385       break;
00386     case BINOP_LSH:
00387       strcpy (ptr, "<<");
00388       break;
00389     case BINOP_RSH:
00390       strcpy (ptr, ">>");
00391       break;
00392     case BINOP_BITWISE_AND:
00393       strcpy (ptr, "&");
00394       break;
00395     case BINOP_BITWISE_IOR:
00396       strcpy (ptr, "|");
00397       break;
00398     case BINOP_BITWISE_XOR:
00399       strcpy (ptr, "^");
00400       break;
00401     case BINOP_LOGICAL_AND:
00402       strcpy (ptr, "&&");
00403       break;
00404     case BINOP_LOGICAL_OR:
00405       strcpy (ptr, "||");
00406       break;
00407     case BINOP_MIN:
00408       strcpy (ptr, "<?");
00409       break;
00410     case BINOP_MAX:
00411       strcpy (ptr, ">?");
00412       break;
00413     case BINOP_ASSIGN:
00414       strcpy (ptr, "=");
00415       break;
00416     case BINOP_ASSIGN_MODIFY:
00417       switch (otherop)
00418         {
00419         case BINOP_ADD:
00420           strcpy (ptr, "+=");
00421           break;
00422         case BINOP_SUB:
00423           strcpy (ptr, "-=");
00424           break;
00425         case BINOP_MUL:
00426           strcpy (ptr, "*=");
00427           break;
00428         case BINOP_DIV:
00429           strcpy (ptr, "/=");
00430           break;
00431         case BINOP_REM:
00432           strcpy (ptr, "%=");
00433           break;
00434         case BINOP_BITWISE_AND:
00435           strcpy (ptr, "&=");
00436           break;
00437         case BINOP_BITWISE_IOR:
00438           strcpy (ptr, "|=");
00439           break;
00440         case BINOP_BITWISE_XOR:
00441           strcpy (ptr, "^=");
00442           break;
00443         case BINOP_MOD: /* invalid */
00444         default:
00445           error (_("Invalid binary operation specified."));
00446         }
00447       break;
00448     case BINOP_SUBSCRIPT:
00449       strcpy (ptr, "[]");
00450       break;
00451     case BINOP_EQUAL:
00452       strcpy (ptr, "==");
00453       break;
00454     case BINOP_NOTEQUAL:
00455       strcpy (ptr, "!=");
00456       break;
00457     case BINOP_LESS:
00458       strcpy (ptr, "<");
00459       break;
00460     case BINOP_GTR:
00461       strcpy (ptr, ">");
00462       break;
00463     case BINOP_GEQ:
00464       strcpy (ptr, ">=");
00465       break;
00466     case BINOP_LEQ:
00467       strcpy (ptr, "<=");
00468       break;
00469     case BINOP_MOD:             /* invalid */
00470     default:
00471       error (_("Invalid binary operation specified."));
00472     }
00473 
00474   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
00475                                      &static_memfuncp, 2);
00476 
00477   if (argvec[0])
00478     {
00479       if (static_memfuncp)
00480         {
00481           argvec[1] = argvec[0];
00482           argvec++;
00483         }
00484       if (noside == EVAL_AVOID_SIDE_EFFECTS)
00485         {
00486           struct type *return_type;
00487 
00488           return_type
00489             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
00490           return value_zero (return_type, VALUE_LVAL (arg1));
00491         }
00492       return call_function_by_hand (argvec[0], 2 - static_memfuncp,
00493                                     argvec + 1);
00494     }
00495   throw_error (NOT_FOUND_ERROR,
00496                _("member function %s not found"), tstr);
00497 #ifdef lint
00498   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
00499 #endif
00500 }
00501 
00502 /* We know that arg1 is a structure, so try to find a unary user
00503    defined operator that matches the operator in question.
00504    Create an argument vector that calls arg1.operator @ (arg1)
00505    and return that value (where '@' is (almost) any unary operator which
00506    is legal for GNU C++).  */
00507 
00508 struct value *
00509 value_x_unop (struct value *arg1, enum exp_opcode op, enum noside noside)
00510 {
00511   struct gdbarch *gdbarch = get_type_arch (value_type (arg1));
00512   struct value **argvec;
00513   char *ptr;
00514   char tstr[13], mangle_tstr[13];
00515   int static_memfuncp, nargs;
00516 
00517   arg1 = coerce_ref (arg1);
00518 
00519   /* now we know that what we have to do is construct our
00520      arg vector and find the right function to call it with.  */
00521 
00522   if (TYPE_CODE (check_typedef (value_type (arg1))) != TYPE_CODE_STRUCT)
00523     error (_("Can't do that unary op on that type"));   /* FIXME be explicit */
00524 
00525   argvec = (struct value **) alloca (sizeof (struct value *) * 4);
00526   argvec[1] = value_addr (arg1);
00527   argvec[2] = 0;
00528 
00529   nargs = 1;
00530 
00531   /* Make the right function name up.  */
00532   strcpy (tstr, "operator__");
00533   ptr = tstr + 8;
00534   strcpy (mangle_tstr, "__");
00535   switch (op)
00536     {
00537     case UNOP_PREINCREMENT:
00538       strcpy (ptr, "++");
00539       break;
00540     case UNOP_PREDECREMENT:
00541       strcpy (ptr, "--");
00542       break;
00543     case UNOP_POSTINCREMENT:
00544       strcpy (ptr, "++");
00545       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
00546       argvec[3] = 0;
00547       nargs ++;
00548       break;
00549     case UNOP_POSTDECREMENT:
00550       strcpy (ptr, "--");
00551       argvec[2] = value_from_longest (builtin_type (gdbarch)->builtin_int, 0);
00552       argvec[3] = 0;
00553       nargs ++;
00554       break;
00555     case UNOP_LOGICAL_NOT:
00556       strcpy (ptr, "!");
00557       break;
00558     case UNOP_COMPLEMENT:
00559       strcpy (ptr, "~");
00560       break;
00561     case UNOP_NEG:
00562       strcpy (ptr, "-");
00563       break;
00564     case UNOP_PLUS:
00565       strcpy (ptr, "+");
00566       break;
00567     case UNOP_IND:
00568       strcpy (ptr, "*");
00569       break;
00570     case STRUCTOP_PTR:
00571       strcpy (ptr, "->");
00572       break;
00573     default:
00574       error (_("Invalid unary operation specified."));
00575     }
00576 
00577   argvec[0] = value_user_defined_op (&arg1, argvec + 1, tstr,
00578                                      &static_memfuncp, nargs);
00579 
00580   if (argvec[0])
00581     {
00582       if (static_memfuncp)
00583         {
00584           argvec[1] = argvec[0];
00585           nargs --;
00586           argvec++;
00587         }
00588       if (noside == EVAL_AVOID_SIDE_EFFECTS)
00589         {
00590           struct type *return_type;
00591 
00592           return_type
00593             = TYPE_TARGET_TYPE (check_typedef (value_type (argvec[0])));
00594           return value_zero (return_type, VALUE_LVAL (arg1));
00595         }
00596       return call_function_by_hand (argvec[0], nargs, argvec + 1);
00597     }
00598   throw_error (NOT_FOUND_ERROR,
00599                _("member function %s not found"), tstr);
00600 
00601   return 0;                     /* For lint -- never reached */
00602 }
00603 
00604 
00605 /* Concatenate two values with the following conditions:
00606 
00607    (1)  Both values must be either bitstring values or character string
00608    values and the resulting value consists of the concatenation of
00609    ARG1 followed by ARG2.
00610 
00611    or
00612 
00613    One value must be an integer value and the other value must be
00614    either a bitstring value or character string value, which is
00615    to be repeated by the number of times specified by the integer
00616    value.
00617 
00618 
00619    (2)  Boolean values are also allowed and are treated as bit string
00620    values of length 1.
00621 
00622    (3)  Character values are also allowed and are treated as character
00623    string values of length 1.  */
00624 
00625 struct value *
00626 value_concat (struct value *arg1, struct value *arg2)
00627 {
00628   struct value *inval1;
00629   struct value *inval2;
00630   struct value *outval = NULL;
00631   int inval1len, inval2len;
00632   int count, idx;
00633   char *ptr;
00634   char inchar;
00635   struct type *type1 = check_typedef (value_type (arg1));
00636   struct type *type2 = check_typedef (value_type (arg2));
00637   struct type *char_type;
00638 
00639   /* First figure out if we are dealing with two values to be concatenated
00640      or a repeat count and a value to be repeated.  INVAL1 is set to the
00641      first of two concatenated values, or the repeat count.  INVAL2 is set
00642      to the second of the two concatenated values or the value to be 
00643      repeated.  */
00644 
00645   if (TYPE_CODE (type2) == TYPE_CODE_INT)
00646     {
00647       struct type *tmp = type1;
00648 
00649       type1 = tmp;
00650       tmp = type2;
00651       inval1 = arg2;
00652       inval2 = arg1;
00653     }
00654   else
00655     {
00656       inval1 = arg1;
00657       inval2 = arg2;
00658     }
00659 
00660   /* Now process the input values.  */
00661 
00662   if (TYPE_CODE (type1) == TYPE_CODE_INT)
00663     {
00664       /* We have a repeat count.  Validate the second value and then
00665          construct a value repeated that many times.  */
00666       if (TYPE_CODE (type2) == TYPE_CODE_STRING
00667           || TYPE_CODE (type2) == TYPE_CODE_CHAR)
00668         {
00669           struct cleanup *back_to;
00670 
00671           count = longest_to_int (value_as_long (inval1));
00672           inval2len = TYPE_LENGTH (type2);
00673           ptr = (char *) xmalloc (count * inval2len);
00674           back_to = make_cleanup (xfree, ptr);
00675           if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
00676             {
00677               char_type = type2;
00678 
00679               inchar = (char) unpack_long (type2,
00680                                            value_contents (inval2));
00681               for (idx = 0; idx < count; idx++)
00682                 {
00683                   *(ptr + idx) = inchar;
00684                 }
00685             }
00686           else
00687             {
00688               char_type = TYPE_TARGET_TYPE (type2);
00689 
00690               for (idx = 0; idx < count; idx++)
00691                 {
00692                   memcpy (ptr + (idx * inval2len), value_contents (inval2),
00693                           inval2len);
00694                 }
00695             }
00696           outval = value_string (ptr, count * inval2len, char_type);
00697           do_cleanups (back_to);
00698         }
00699       else if (TYPE_CODE (type2) == TYPE_CODE_BOOL)
00700         {
00701           error (_("unimplemented support for boolean repeats"));
00702         }
00703       else
00704         {
00705           error (_("can't repeat values of that type"));
00706         }
00707     }
00708   else if (TYPE_CODE (type1) == TYPE_CODE_STRING
00709            || TYPE_CODE (type1) == TYPE_CODE_CHAR)
00710     {
00711       struct cleanup *back_to;
00712 
00713       /* We have two character strings to concatenate.  */
00714       if (TYPE_CODE (type2) != TYPE_CODE_STRING
00715           && TYPE_CODE (type2) != TYPE_CODE_CHAR)
00716         {
00717           error (_("Strings can only be concatenated with other strings."));
00718         }
00719       inval1len = TYPE_LENGTH (type1);
00720       inval2len = TYPE_LENGTH (type2);
00721       ptr = (char *) xmalloc (inval1len + inval2len);
00722       back_to = make_cleanup (xfree, ptr);
00723       if (TYPE_CODE (type1) == TYPE_CODE_CHAR)
00724         {
00725           char_type = type1;
00726 
00727           *ptr = (char) unpack_long (type1, value_contents (inval1));
00728         }
00729       else
00730         {
00731           char_type = TYPE_TARGET_TYPE (type1);
00732 
00733           memcpy (ptr, value_contents (inval1), inval1len);
00734         }
00735       if (TYPE_CODE (type2) == TYPE_CODE_CHAR)
00736         {
00737           *(ptr + inval1len) =
00738             (char) unpack_long (type2, value_contents (inval2));
00739         }
00740       else
00741         {
00742           memcpy (ptr + inval1len, value_contents (inval2), inval2len);
00743         }
00744       outval = value_string (ptr, inval1len + inval2len, char_type);
00745       do_cleanups (back_to);
00746     }
00747   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL)
00748     {
00749       /* We have two bitstrings to concatenate.  */
00750       if (TYPE_CODE (type2) != TYPE_CODE_BOOL)
00751         {
00752           error (_("Booleans can only be concatenated "
00753                    "with other bitstrings or booleans."));
00754         }
00755       error (_("unimplemented support for boolean concatenation."));
00756     }
00757   else
00758     {
00759       /* We don't know how to concatenate these operands.  */
00760       error (_("illegal operands for concatenation."));
00761     }
00762   return (outval);
00763 }
00764 
00765 /* Integer exponentiation: V1**V2, where both arguments are
00766    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
00767 
00768 static LONGEST
00769 integer_pow (LONGEST v1, LONGEST v2)
00770 {
00771   if (v2 < 0)
00772     {
00773       if (v1 == 0)
00774         error (_("Attempt to raise 0 to negative power."));
00775       else
00776         return 0;
00777     }
00778   else 
00779     {
00780       /* The Russian Peasant's Algorithm.  */
00781       LONGEST v;
00782       
00783       v = 1;
00784       for (;;)
00785         {
00786           if (v2 & 1L) 
00787             v *= v1;
00788           v2 >>= 1;
00789           if (v2 == 0)
00790             return v;
00791           v1 *= v1;
00792         }
00793     }
00794 }
00795 
00796 /* Integer exponentiation: V1**V2, where both arguments are
00797    integers.  Requires V1 != 0 if V2 < 0.  Returns 1 for 0 ** 0.  */
00798 
00799 static ULONGEST
00800 uinteger_pow (ULONGEST v1, LONGEST v2)
00801 {
00802   if (v2 < 0)
00803     {
00804       if (v1 == 0)
00805         error (_("Attempt to raise 0 to negative power."));
00806       else
00807         return 0;
00808     }
00809   else 
00810     {
00811       /* The Russian Peasant's Algorithm.  */
00812       ULONGEST v;
00813       
00814       v = 1;
00815       for (;;)
00816         {
00817           if (v2 & 1L) 
00818             v *= v1;
00819           v2 >>= 1;
00820           if (v2 == 0)
00821             return v;
00822           v1 *= v1;
00823         }
00824     }
00825 }
00826 
00827 /* Obtain decimal value of arguments for binary operation, converting from
00828    other types if one of them is not decimal floating point.  */
00829 static void
00830 value_args_as_decimal (struct value *arg1, struct value *arg2,
00831                        gdb_byte *x, int *len_x, enum bfd_endian *byte_order_x,
00832                        gdb_byte *y, int *len_y, enum bfd_endian *byte_order_y)
00833 {
00834   struct type *type1, *type2;
00835 
00836   type1 = check_typedef (value_type (arg1));
00837   type2 = check_typedef (value_type (arg2));
00838 
00839   /* At least one of the arguments must be of decimal float type.  */
00840   gdb_assert (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
00841               || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT);
00842 
00843   if (TYPE_CODE (type1) == TYPE_CODE_FLT
00844       || TYPE_CODE (type2) == TYPE_CODE_FLT)
00845     /* The DFP extension to the C language does not allow mixing of
00846      * decimal float types with other float types in expressions
00847      * (see WDTR 24732, page 12).  */
00848     error (_("Mixing decimal floating types with "
00849              "other floating types is not allowed."));
00850 
00851   /* Obtain decimal value of arg1, converting from other types
00852      if necessary.  */
00853 
00854   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
00855     {
00856       *byte_order_x = gdbarch_byte_order (get_type_arch (type1));
00857       *len_x = TYPE_LENGTH (type1);
00858       memcpy (x, value_contents (arg1), *len_x);
00859     }
00860   else if (is_integral_type (type1))
00861     {
00862       *byte_order_x = gdbarch_byte_order (get_type_arch (type2));
00863       *len_x = TYPE_LENGTH (type2);
00864       decimal_from_integral (arg1, x, *len_x, *byte_order_x);
00865     }
00866   else
00867     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
00868              TYPE_NAME (type2));
00869 
00870   /* Obtain decimal value of arg2, converting from other types
00871      if necessary.  */
00872 
00873   if (TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
00874     {
00875       *byte_order_y = gdbarch_byte_order (get_type_arch (type2));
00876       *len_y = TYPE_LENGTH (type2);
00877       memcpy (y, value_contents (arg2), *len_y);
00878     }
00879   else if (is_integral_type (type2))
00880     {
00881       *byte_order_y = gdbarch_byte_order (get_type_arch (type1));
00882       *len_y = TYPE_LENGTH (type1);
00883       decimal_from_integral (arg2, y, *len_y, *byte_order_y);
00884     }
00885   else
00886     error (_("Don't know how to convert from %s to %s."), TYPE_NAME (type1),
00887              TYPE_NAME (type2));
00888 }
00889 
00890 /* Perform a binary operation on two operands which have reasonable
00891    representations as integers or floats.  This includes booleans,
00892    characters, integers, or floats.
00893    Does not support addition and subtraction on pointers;
00894    use value_ptradd, value_ptrsub or value_ptrdiff for those operations.  */
00895 
00896 static struct value *
00897 scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
00898 {
00899   struct value *val;
00900   struct type *type1, *type2, *result_type;
00901 
00902   arg1 = coerce_ref (arg1);
00903   arg2 = coerce_ref (arg2);
00904 
00905   type1 = check_typedef (value_type (arg1));
00906   type2 = check_typedef (value_type (arg2));
00907 
00908   if ((TYPE_CODE (type1) != TYPE_CODE_FLT
00909        && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
00910        && !is_integral_type (type1))
00911       || (TYPE_CODE (type2) != TYPE_CODE_FLT
00912           && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
00913           && !is_integral_type (type2)))
00914     error (_("Argument to arithmetic operation not a number or boolean."));
00915 
00916   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
00917       || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
00918     {
00919       int len_v1, len_v2, len_v;
00920       enum bfd_endian byte_order_v1, byte_order_v2, byte_order_v;
00921       gdb_byte v1[16], v2[16];
00922       gdb_byte v[16];
00923 
00924       /* If only one type is decimal float, use its type.
00925          Otherwise use the bigger type.  */
00926       if (TYPE_CODE (type1) != TYPE_CODE_DECFLOAT)
00927         result_type = type2;
00928       else if (TYPE_CODE (type2) != TYPE_CODE_DECFLOAT)
00929         result_type = type1;
00930       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
00931         result_type = type2;
00932       else
00933         result_type = type1;
00934 
00935       len_v = TYPE_LENGTH (result_type);
00936       byte_order_v = gdbarch_byte_order (get_type_arch (result_type));
00937 
00938       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
00939                                          v2, &len_v2, &byte_order_v2);
00940 
00941       switch (op)
00942         {
00943         case BINOP_ADD:
00944         case BINOP_SUB:
00945         case BINOP_MUL:
00946         case BINOP_DIV:
00947         case BINOP_EXP:
00948           decimal_binop (op, v1, len_v1, byte_order_v1,
00949                              v2, len_v2, byte_order_v2,
00950                              v, len_v, byte_order_v);
00951           break;
00952 
00953         default:
00954           error (_("Operation not valid for decimal floating point number."));
00955         }
00956 
00957       val = value_from_decfloat (result_type, v);
00958     }
00959   else if (TYPE_CODE (type1) == TYPE_CODE_FLT
00960            || TYPE_CODE (type2) == TYPE_CODE_FLT)
00961     {
00962       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
00963          in target format.  real.c in GCC probably has the necessary
00964          code.  */
00965       DOUBLEST v1, v2, v = 0;
00966 
00967       v1 = value_as_double (arg1);
00968       v2 = value_as_double (arg2);
00969 
00970       switch (op)
00971         {
00972         case BINOP_ADD:
00973           v = v1 + v2;
00974           break;
00975 
00976         case BINOP_SUB:
00977           v = v1 - v2;
00978           break;
00979 
00980         case BINOP_MUL:
00981           v = v1 * v2;
00982           break;
00983 
00984         case BINOP_DIV:
00985           v = v1 / v2;
00986           break;
00987 
00988         case BINOP_EXP:
00989           errno = 0;
00990           v = pow (v1, v2);
00991           if (errno)
00992             error (_("Cannot perform exponentiation: %s"),
00993                    safe_strerror (errno));
00994           break;
00995 
00996         case BINOP_MIN:
00997           v = v1 < v2 ? v1 : v2;
00998           break;
00999               
01000         case BINOP_MAX:
01001           v = v1 > v2 ? v1 : v2;
01002           break;
01003 
01004         default:
01005           error (_("Integer-only operation on floating point number."));
01006         }
01007 
01008       /* If only one type is float, use its type.
01009          Otherwise use the bigger type.  */
01010       if (TYPE_CODE (type1) != TYPE_CODE_FLT)
01011         result_type = type2;
01012       else if (TYPE_CODE (type2) != TYPE_CODE_FLT)
01013         result_type = type1;
01014       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
01015         result_type = type2;
01016       else
01017         result_type = type1;
01018 
01019       val = allocate_value (result_type);
01020       store_typed_floating (value_contents_raw (val), value_type (val), v);
01021     }
01022   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
01023            || TYPE_CODE (type2) == TYPE_CODE_BOOL)
01024     {
01025       LONGEST v1, v2, v = 0;
01026 
01027       v1 = value_as_long (arg1);
01028       v2 = value_as_long (arg2);
01029 
01030       switch (op)
01031         {
01032         case BINOP_BITWISE_AND:
01033           v = v1 & v2;
01034           break;
01035 
01036         case BINOP_BITWISE_IOR:
01037           v = v1 | v2;
01038           break;
01039 
01040         case BINOP_BITWISE_XOR:
01041           v = v1 ^ v2;
01042           break;
01043               
01044         case BINOP_EQUAL:
01045           v = v1 == v2;
01046           break;
01047           
01048         case BINOP_NOTEQUAL:
01049           v = v1 != v2;
01050           break;
01051 
01052         default:
01053           error (_("Invalid operation on booleans."));
01054         }
01055 
01056       result_type = type1;
01057 
01058       val = allocate_value (result_type);
01059       store_signed_integer (value_contents_raw (val),
01060                             TYPE_LENGTH (result_type),
01061                             gdbarch_byte_order (get_type_arch (result_type)),
01062                             v);
01063     }
01064   else
01065     /* Integral operations here.  */
01066     {
01067       /* Determine type length of the result, and if the operation should
01068          be done unsigned.  For exponentiation and shift operators,
01069          use the length and type of the left operand.  Otherwise,
01070          use the signedness of the operand with the greater length.
01071          If both operands are of equal length, use unsigned operation
01072          if one of the operands is unsigned.  */
01073       if (op == BINOP_RSH || op == BINOP_LSH || op == BINOP_EXP)
01074         result_type = type1;
01075       else if (TYPE_LENGTH (type1) > TYPE_LENGTH (type2))
01076         result_type = type1;
01077       else if (TYPE_LENGTH (type2) > TYPE_LENGTH (type1))
01078         result_type = type2;
01079       else if (TYPE_UNSIGNED (type1))
01080         result_type = type1;
01081       else if (TYPE_UNSIGNED (type2))
01082         result_type = type2;
01083       else
01084         result_type = type1;
01085 
01086       if (TYPE_UNSIGNED (result_type))
01087         {
01088           LONGEST v2_signed = value_as_long (arg2);
01089           ULONGEST v1, v2, v = 0;
01090 
01091           v1 = (ULONGEST) value_as_long (arg1);
01092           v2 = (ULONGEST) v2_signed;
01093 
01094           switch (op)
01095             {
01096             case BINOP_ADD:
01097               v = v1 + v2;
01098               break;
01099 
01100             case BINOP_SUB:
01101               v = v1 - v2;
01102               break;
01103 
01104             case BINOP_MUL:
01105               v = v1 * v2;
01106               break;
01107 
01108             case BINOP_DIV:
01109             case BINOP_INTDIV:
01110               if (v2 != 0)
01111                 v = v1 / v2;
01112               else
01113                 error (_("Division by zero"));
01114               break;
01115 
01116             case BINOP_EXP:
01117               v = uinteger_pow (v1, v2_signed);
01118               break;
01119 
01120             case BINOP_REM:
01121               if (v2 != 0)
01122                 v = v1 % v2;
01123               else
01124                 error (_("Division by zero"));
01125               break;
01126 
01127             case BINOP_MOD:
01128               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
01129                  v1 mod 0 has a defined value, v1.  */
01130               if (v2 == 0)
01131                 {
01132                   v = v1;
01133                 }
01134               else
01135                 {
01136                   v = v1 / v2;
01137                   /* Note floor(v1/v2) == v1/v2 for unsigned.  */
01138                   v = v1 - (v2 * v);
01139                 }
01140               break;
01141 
01142             case BINOP_LSH:
01143               v = v1 << v2;
01144               break;
01145 
01146             case BINOP_RSH:
01147               v = v1 >> v2;
01148               break;
01149 
01150             case BINOP_BITWISE_AND:
01151               v = v1 & v2;
01152               break;
01153 
01154             case BINOP_BITWISE_IOR:
01155               v = v1 | v2;
01156               break;
01157 
01158             case BINOP_BITWISE_XOR:
01159               v = v1 ^ v2;
01160               break;
01161 
01162             case BINOP_LOGICAL_AND:
01163               v = v1 && v2;
01164               break;
01165 
01166             case BINOP_LOGICAL_OR:
01167               v = v1 || v2;
01168               break;
01169 
01170             case BINOP_MIN:
01171               v = v1 < v2 ? v1 : v2;
01172               break;
01173 
01174             case BINOP_MAX:
01175               v = v1 > v2 ? v1 : v2;
01176               break;
01177 
01178             case BINOP_EQUAL:
01179               v = v1 == v2;
01180               break;
01181 
01182             case BINOP_NOTEQUAL:
01183               v = v1 != v2;
01184               break;
01185 
01186             case BINOP_LESS:
01187               v = v1 < v2;
01188               break;
01189 
01190             case BINOP_GTR:
01191               v = v1 > v2;
01192               break;
01193 
01194             case BINOP_LEQ:
01195               v = v1 <= v2;
01196               break;
01197 
01198             case BINOP_GEQ:
01199               v = v1 >= v2;
01200               break;
01201 
01202             default:
01203               error (_("Invalid binary operation on numbers."));
01204             }
01205 
01206           val = allocate_value (result_type);
01207           store_unsigned_integer (value_contents_raw (val),
01208                                   TYPE_LENGTH (value_type (val)),
01209                                   gdbarch_byte_order
01210                                     (get_type_arch (result_type)),
01211                                   v);
01212         }
01213       else
01214         {
01215           LONGEST v1, v2, v = 0;
01216 
01217           v1 = value_as_long (arg1);
01218           v2 = value_as_long (arg2);
01219 
01220           switch (op)
01221             {
01222             case BINOP_ADD:
01223               v = v1 + v2;
01224               break;
01225 
01226             case BINOP_SUB:
01227               v = v1 - v2;
01228               break;
01229 
01230             case BINOP_MUL:
01231               v = v1 * v2;
01232               break;
01233 
01234             case BINOP_DIV:
01235             case BINOP_INTDIV:
01236               if (v2 != 0)
01237                 v = v1 / v2;
01238               else
01239                 error (_("Division by zero"));
01240               break;
01241 
01242             case BINOP_EXP:
01243               v = integer_pow (v1, v2);
01244               break;
01245 
01246             case BINOP_REM:
01247               if (v2 != 0)
01248                 v = v1 % v2;
01249               else
01250                 error (_("Division by zero"));
01251               break;
01252 
01253             case BINOP_MOD:
01254               /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
01255                  X mod 0 has a defined value, X.  */
01256               if (v2 == 0)
01257                 {
01258                   v = v1;
01259                 }
01260               else
01261                 {
01262                   v = v1 / v2;
01263                   /* Compute floor.  */
01264                   if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
01265                     {
01266                       v--;
01267                     }
01268                   v = v1 - (v2 * v);
01269                 }
01270               break;
01271 
01272             case BINOP_LSH:
01273               v = v1 << v2;
01274               break;
01275 
01276             case BINOP_RSH:
01277               v = v1 >> v2;
01278               break;
01279 
01280             case BINOP_BITWISE_AND:
01281               v = v1 & v2;
01282               break;
01283 
01284             case BINOP_BITWISE_IOR:
01285               v = v1 | v2;
01286               break;
01287 
01288             case BINOP_BITWISE_XOR:
01289               v = v1 ^ v2;
01290               break;
01291 
01292             case BINOP_LOGICAL_AND:
01293               v = v1 && v2;
01294               break;
01295 
01296             case BINOP_LOGICAL_OR:
01297               v = v1 || v2;
01298               break;
01299 
01300             case BINOP_MIN:
01301               v = v1 < v2 ? v1 : v2;
01302               break;
01303 
01304             case BINOP_MAX:
01305               v = v1 > v2 ? v1 : v2;
01306               break;
01307 
01308             case BINOP_EQUAL:
01309               v = v1 == v2;
01310               break;
01311 
01312             case BINOP_NOTEQUAL:
01313               v = v1 != v2;
01314               break;
01315 
01316             case BINOP_LESS:
01317               v = v1 < v2;
01318               break;
01319 
01320             case BINOP_GTR:
01321               v = v1 > v2;
01322               break;
01323 
01324             case BINOP_LEQ:
01325               v = v1 <= v2;
01326               break;
01327 
01328             case BINOP_GEQ:
01329               v = v1 >= v2;
01330               break;
01331 
01332             default:
01333               error (_("Invalid binary operation on numbers."));
01334             }
01335 
01336           val = allocate_value (result_type);
01337           store_signed_integer (value_contents_raw (val),
01338                                 TYPE_LENGTH (value_type (val)),
01339                                 gdbarch_byte_order
01340                                   (get_type_arch (result_type)),
01341                                 v);
01342         }
01343     }
01344 
01345   return val;
01346 }
01347 
01348 /* Widen a scalar value SCALAR_VALUE to vector type VECTOR_TYPE by
01349    replicating SCALAR_VALUE for each element of the vector.  Only scalar
01350    types that can be cast to the type of one element of the vector are
01351    acceptable.  The newly created vector value is returned upon success,
01352    otherwise an error is thrown.  */
01353 
01354 struct value *
01355 value_vector_widen (struct value *scalar_value, struct type *vector_type)
01356 {
01357   /* Widen the scalar to a vector.  */
01358   struct type *eltype, *scalar_type;
01359   struct value *val, *elval;
01360   LONGEST low_bound, high_bound;
01361   int i;
01362 
01363   CHECK_TYPEDEF (vector_type);
01364 
01365   gdb_assert (TYPE_CODE (vector_type) == TYPE_CODE_ARRAY
01366               && TYPE_VECTOR (vector_type));
01367 
01368   if (!get_array_bounds (vector_type, &low_bound, &high_bound))
01369     error (_("Could not determine the vector bounds"));
01370 
01371   eltype = check_typedef (TYPE_TARGET_TYPE (vector_type));
01372   elval = value_cast (eltype, scalar_value);
01373 
01374   scalar_type = check_typedef (value_type (scalar_value));
01375 
01376   /* If we reduced the length of the scalar then check we didn't loose any
01377      important bits.  */
01378   if (TYPE_LENGTH (eltype) < TYPE_LENGTH (scalar_type)
01379       && !value_equal (elval, scalar_value))
01380     error (_("conversion of scalar to vector involves truncation"));
01381 
01382   val = allocate_value (vector_type);
01383   for (i = 0; i < high_bound - low_bound + 1; i++)
01384     /* Duplicate the contents of elval into the destination vector.  */
01385     memcpy (value_contents_writeable (val) + (i * TYPE_LENGTH (eltype)),
01386             value_contents_all (elval), TYPE_LENGTH (eltype));
01387 
01388   return val;
01389 }
01390 
01391 /* Performs a binary operation on two vector operands by calling scalar_binop
01392    for each pair of vector components.  */
01393 
01394 static struct value *
01395 vector_binop (struct value *val1, struct value *val2, enum exp_opcode op)
01396 {
01397   struct value *val, *tmp, *mark;
01398   struct type *type1, *type2, *eltype1, *eltype2;
01399   int t1_is_vec, t2_is_vec, elsize, i;
01400   LONGEST low_bound1, high_bound1, low_bound2, high_bound2;
01401 
01402   type1 = check_typedef (value_type (val1));
01403   type2 = check_typedef (value_type (val2));
01404 
01405   t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
01406                && TYPE_VECTOR (type1)) ? 1 : 0;
01407   t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
01408                && TYPE_VECTOR (type2)) ? 1 : 0;
01409 
01410   if (!t1_is_vec || !t2_is_vec)
01411     error (_("Vector operations are only supported among vectors"));
01412 
01413   if (!get_array_bounds (type1, &low_bound1, &high_bound1)
01414       || !get_array_bounds (type2, &low_bound2, &high_bound2))
01415     error (_("Could not determine the vector bounds"));
01416 
01417   eltype1 = check_typedef (TYPE_TARGET_TYPE (type1));
01418   eltype2 = check_typedef (TYPE_TARGET_TYPE (type2));
01419   elsize = TYPE_LENGTH (eltype1);
01420 
01421   if (TYPE_CODE (eltype1) != TYPE_CODE (eltype2)
01422       || elsize != TYPE_LENGTH (eltype2)
01423       || TYPE_UNSIGNED (eltype1) != TYPE_UNSIGNED (eltype2)
01424       || low_bound1 != low_bound2 || high_bound1 != high_bound2)
01425     error (_("Cannot perform operation on vectors with different types"));
01426 
01427   val = allocate_value (type1);
01428   mark = value_mark ();
01429   for (i = 0; i < high_bound1 - low_bound1 + 1; i++)
01430     {
01431       tmp = value_binop (value_subscript (val1, i),
01432                          value_subscript (val2, i), op);
01433       memcpy (value_contents_writeable (val) + i * elsize,
01434               value_contents_all (tmp),
01435               elsize);
01436      }
01437   value_free_to_mark (mark);
01438 
01439   return val;
01440 }
01441 
01442 /* Perform a binary operation on two operands.  */
01443 
01444 struct value *
01445 value_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
01446 {
01447   struct value *val;
01448   struct type *type1 = check_typedef (value_type (arg1));
01449   struct type *type2 = check_typedef (value_type (arg2));
01450   int t1_is_vec = (TYPE_CODE (type1) == TYPE_CODE_ARRAY
01451                    && TYPE_VECTOR (type1));
01452   int t2_is_vec = (TYPE_CODE (type2) == TYPE_CODE_ARRAY
01453                    && TYPE_VECTOR (type2));
01454 
01455   if (!t1_is_vec && !t2_is_vec)
01456     val = scalar_binop (arg1, arg2, op);
01457   else if (t1_is_vec && t2_is_vec)
01458     val = vector_binop (arg1, arg2, op);
01459   else
01460     {
01461       /* Widen the scalar operand to a vector.  */
01462       struct value **v = t1_is_vec ? &arg2 : &arg1;
01463       struct type *t = t1_is_vec ? type2 : type1;
01464       
01465       if (TYPE_CODE (t) != TYPE_CODE_FLT
01466           && TYPE_CODE (t) != TYPE_CODE_DECFLOAT
01467           && !is_integral_type (t))
01468         error (_("Argument to operation not a number or boolean."));
01469 
01470       /* Replicate the scalar value to make a vector value.  */
01471       *v = value_vector_widen (*v, t1_is_vec ? type1 : type2);
01472 
01473       val = vector_binop (arg1, arg2, op);
01474     }
01475 
01476   return val;
01477 }
01478 
01479 /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
01480 
01481 int
01482 value_logical_not (struct value *arg1)
01483 {
01484   int len;
01485   const gdb_byte *p;
01486   struct type *type1;
01487 
01488   arg1 = coerce_array (arg1);
01489   type1 = check_typedef (value_type (arg1));
01490 
01491   if (TYPE_CODE (type1) == TYPE_CODE_FLT)
01492     return 0 == value_as_double (arg1);
01493   else if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
01494     return decimal_is_zero (value_contents (arg1), TYPE_LENGTH (type1),
01495                             gdbarch_byte_order (get_type_arch (type1)));
01496 
01497   len = TYPE_LENGTH (type1);
01498   p = value_contents (arg1);
01499 
01500   while (--len >= 0)
01501     {
01502       if (*p++)
01503         break;
01504     }
01505 
01506   return len < 0;
01507 }
01508 
01509 /* Perform a comparison on two string values (whose content are not
01510    necessarily null terminated) based on their length.  */
01511 
01512 static int
01513 value_strcmp (struct value *arg1, struct value *arg2)
01514 {
01515   int len1 = TYPE_LENGTH (value_type (arg1));
01516   int len2 = TYPE_LENGTH (value_type (arg2));
01517   const gdb_byte *s1 = value_contents (arg1);
01518   const gdb_byte *s2 = value_contents (arg2);
01519   int i, len = len1 < len2 ? len1 : len2;
01520 
01521   for (i = 0; i < len; i++)
01522     {
01523       if (s1[i] < s2[i])
01524         return -1;
01525       else if (s1[i] > s2[i])
01526         return 1;
01527       else
01528         continue;
01529     }
01530 
01531   if (len1 < len2)
01532     return -1;
01533   else if (len1 > len2)
01534     return 1;
01535   else
01536     return 0;
01537 }
01538 
01539 /* Simulate the C operator == by returning a 1
01540    iff ARG1 and ARG2 have equal contents.  */
01541 
01542 int
01543 value_equal (struct value *arg1, struct value *arg2)
01544 {
01545   int len;
01546   const gdb_byte *p1;
01547   const gdb_byte *p2;
01548   struct type *type1, *type2;
01549   enum type_code code1;
01550   enum type_code code2;
01551   int is_int1, is_int2;
01552 
01553   arg1 = coerce_array (arg1);
01554   arg2 = coerce_array (arg2);
01555 
01556   type1 = check_typedef (value_type (arg1));
01557   type2 = check_typedef (value_type (arg2));
01558   code1 = TYPE_CODE (type1);
01559   code2 = TYPE_CODE (type2);
01560   is_int1 = is_integral_type (type1);
01561   is_int2 = is_integral_type (type2);
01562 
01563   if (is_int1 && is_int2)
01564     return longest_to_int (value_as_long (value_binop (arg1, arg2,
01565                                                        BINOP_EQUAL)));
01566   else if ((code1 == TYPE_CODE_FLT || is_int1)
01567            && (code2 == TYPE_CODE_FLT || is_int2))
01568     {
01569       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
01570          `long double' values are returned in static storage (m68k).  */
01571       DOUBLEST d = value_as_double (arg1);
01572 
01573       return d == value_as_double (arg2);
01574     }
01575   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
01576            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
01577     {
01578       gdb_byte v1[16], v2[16];
01579       int len_v1, len_v2;
01580       enum bfd_endian byte_order_v1, byte_order_v2;
01581 
01582       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
01583                                          v2, &len_v2, &byte_order_v2);
01584 
01585       return decimal_compare (v1, len_v1, byte_order_v1,
01586                               v2, len_v2, byte_order_v2) == 0;
01587     }
01588 
01589   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
01590      is bigger.  */
01591   else if (code1 == TYPE_CODE_PTR && is_int2)
01592     return value_as_address (arg1) == (CORE_ADDR) value_as_long (arg2);
01593   else if (code2 == TYPE_CODE_PTR && is_int1)
01594     return (CORE_ADDR) value_as_long (arg1) == value_as_address (arg2);
01595 
01596   else if (code1 == code2
01597            && ((len = (int) TYPE_LENGTH (type1))
01598                == (int) TYPE_LENGTH (type2)))
01599     {
01600       p1 = value_contents (arg1);
01601       p2 = value_contents (arg2);
01602       while (--len >= 0)
01603         {
01604           if (*p1++ != *p2++)
01605             break;
01606         }
01607       return len < 0;
01608     }
01609   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
01610     {
01611       return value_strcmp (arg1, arg2) == 0;
01612     }
01613   else
01614     {
01615       error (_("Invalid type combination in equality test."));
01616       return 0;                 /* For lint -- never reached.  */
01617     }
01618 }
01619 
01620 /* Compare values based on their raw contents.  Useful for arrays since
01621    value_equal coerces them to pointers, thus comparing just the address
01622    of the array instead of its contents.  */
01623 
01624 int
01625 value_equal_contents (struct value *arg1, struct value *arg2)
01626 {
01627   struct type *type1, *type2;
01628 
01629   type1 = check_typedef (value_type (arg1));
01630   type2 = check_typedef (value_type (arg2));
01631 
01632   return (TYPE_CODE (type1) == TYPE_CODE (type2)
01633           && TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
01634           && memcmp (value_contents (arg1), value_contents (arg2),
01635                      TYPE_LENGTH (type1)) == 0);
01636 }
01637 
01638 /* Simulate the C operator < by returning 1
01639    iff ARG1's contents are less than ARG2's.  */
01640 
01641 int
01642 value_less (struct value *arg1, struct value *arg2)
01643 {
01644   enum type_code code1;
01645   enum type_code code2;
01646   struct type *type1, *type2;
01647   int is_int1, is_int2;
01648 
01649   arg1 = coerce_array (arg1);
01650   arg2 = coerce_array (arg2);
01651 
01652   type1 = check_typedef (value_type (arg1));
01653   type2 = check_typedef (value_type (arg2));
01654   code1 = TYPE_CODE (type1);
01655   code2 = TYPE_CODE (type2);
01656   is_int1 = is_integral_type (type1);
01657   is_int2 = is_integral_type (type2);
01658 
01659   if (is_int1 && is_int2)
01660     return longest_to_int (value_as_long (value_binop (arg1, arg2,
01661                                                        BINOP_LESS)));
01662   else if ((code1 == TYPE_CODE_FLT || is_int1)
01663            && (code2 == TYPE_CODE_FLT || is_int2))
01664     {
01665       /* NOTE: kettenis/20050816: Avoid compiler bug on systems where
01666          `long double' values are returned in static storage (m68k).  */
01667       DOUBLEST d = value_as_double (arg1);
01668 
01669       return d < value_as_double (arg2);
01670     }
01671   else if ((code1 == TYPE_CODE_DECFLOAT || is_int1)
01672            && (code2 == TYPE_CODE_DECFLOAT || is_int2))
01673     {
01674       gdb_byte v1[16], v2[16];
01675       int len_v1, len_v2;
01676       enum bfd_endian byte_order_v1, byte_order_v2;
01677 
01678       value_args_as_decimal (arg1, arg2, v1, &len_v1, &byte_order_v1,
01679                                          v2, &len_v2, &byte_order_v2);
01680 
01681       return decimal_compare (v1, len_v1, byte_order_v1,
01682                               v2, len_v2, byte_order_v2) == -1;
01683     }
01684   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
01685     return value_as_address (arg1) < value_as_address (arg2);
01686 
01687   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
01688      is bigger.  */
01689   else if (code1 == TYPE_CODE_PTR && is_int2)
01690     return value_as_address (arg1) < (CORE_ADDR) value_as_long (arg2);
01691   else if (code2 == TYPE_CODE_PTR && is_int1)
01692     return (CORE_ADDR) value_as_long (arg1) < value_as_address (arg2);
01693   else if (code1 == TYPE_CODE_STRING && code2 == TYPE_CODE_STRING)
01694     return value_strcmp (arg1, arg2) < 0;
01695   else
01696     {
01697       error (_("Invalid type combination in ordering comparison."));
01698       return 0;
01699     }
01700 }
01701 
01702 /* The unary operators +, - and ~.  They free the argument ARG1.  */
01703 
01704 struct value *
01705 value_pos (struct value *arg1)
01706 {
01707   struct type *type;
01708 
01709   arg1 = coerce_ref (arg1);
01710   type = check_typedef (value_type (arg1));
01711 
01712   if (TYPE_CODE (type) == TYPE_CODE_FLT)
01713     return value_from_double (type, value_as_double (arg1));
01714   else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
01715     return value_from_decfloat (type, value_contents (arg1));
01716   else if (is_integral_type (type))
01717     {
01718       return value_from_longest (type, value_as_long (arg1));
01719     }
01720   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
01721     {
01722       struct value *val = allocate_value (type);
01723 
01724       memcpy (value_contents_raw (val), value_contents (arg1),
01725               TYPE_LENGTH (type));
01726       return val;
01727     }
01728   else
01729     {
01730       error (_("Argument to positive operation not a number."));
01731       return 0;                 /* For lint -- never reached.  */
01732     }
01733 }
01734 
01735 struct value *
01736 value_neg (struct value *arg1)
01737 {
01738   struct type *type;
01739 
01740   arg1 = coerce_ref (arg1);
01741   type = check_typedef (value_type (arg1));
01742 
01743   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
01744     {
01745       struct value *val = allocate_value (type);
01746       int len = TYPE_LENGTH (type);
01747       gdb_byte decbytes[16];  /* a decfloat is at most 128 bits long.  */
01748 
01749       memcpy (decbytes, value_contents (arg1), len);
01750 
01751       if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_LITTLE)
01752         decbytes[len-1] = decbytes[len - 1] | 0x80;
01753       else
01754         decbytes[0] = decbytes[0] | 0x80;
01755 
01756       memcpy (value_contents_raw (val), decbytes, len);
01757       return val;
01758     }
01759   else if (TYPE_CODE (type) == TYPE_CODE_FLT)
01760     return value_from_double (type, -value_as_double (arg1));
01761   else if (is_integral_type (type))
01762     {
01763       return value_from_longest (type, -value_as_long (arg1));
01764     }
01765   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
01766     {
01767       struct value *tmp, *val = allocate_value (type);
01768       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
01769       int i;
01770       LONGEST low_bound, high_bound;
01771 
01772       if (!get_array_bounds (type, &low_bound, &high_bound))
01773         error (_("Could not determine the vector bounds"));
01774 
01775       for (i = 0; i < high_bound - low_bound + 1; i++)
01776         {
01777           tmp = value_neg (value_subscript (arg1, i));
01778           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
01779                   value_contents_all (tmp), TYPE_LENGTH (eltype));
01780         }
01781       return val;
01782     }
01783   else
01784     {
01785       error (_("Argument to negate operation not a number."));
01786       return 0;                 /* For lint -- never reached.  */
01787     }
01788 }
01789 
01790 struct value *
01791 value_complement (struct value *arg1)
01792 {
01793   struct type *type;
01794   struct value *val;
01795 
01796   arg1 = coerce_ref (arg1);
01797   type = check_typedef (value_type (arg1));
01798 
01799   if (is_integral_type (type))
01800     val = value_from_longest (type, ~value_as_long (arg1));
01801   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
01802     {
01803       struct value *tmp;
01804       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
01805       int i;
01806       LONGEST low_bound, high_bound;
01807 
01808       if (!get_array_bounds (type, &low_bound, &high_bound))
01809         error (_("Could not determine the vector bounds"));
01810 
01811       val = allocate_value (type);
01812       for (i = 0; i < high_bound - low_bound + 1; i++)
01813         {
01814           tmp = value_complement (value_subscript (arg1, i));
01815           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
01816                   value_contents_all (tmp), TYPE_LENGTH (eltype));
01817         }
01818     }
01819   else
01820     error (_("Argument to complement operation not an integer, boolean."));
01821 
01822   return val;
01823 }
01824 
01825 /* The INDEX'th bit of SET value whose value_type is TYPE,
01826    and whose value_contents is valaddr.
01827    Return -1 if out of range, -2 other error.  */
01828 
01829 int
01830 value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
01831 {
01832   struct gdbarch *gdbarch = get_type_arch (type);
01833   LONGEST low_bound, high_bound;
01834   LONGEST word;
01835   unsigned rel_index;
01836   struct type *range = TYPE_INDEX_TYPE (type);
01837 
01838   if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
01839     return -2;
01840   if (index < low_bound || index > high_bound)
01841     return -1;
01842   rel_index = index - low_bound;
01843   word = extract_unsigned_integer (valaddr + (rel_index / TARGET_CHAR_BIT), 1,
01844                                    gdbarch_byte_order (gdbarch));
01845   rel_index %= TARGET_CHAR_BIT;
01846   if (gdbarch_bits_big_endian (gdbarch))
01847     rel_index = TARGET_CHAR_BIT - 1 - rel_index;
01848   return (word >> rel_index) & 1;
01849 }
01850 
01851 int
01852 value_in (struct value *element, struct value *set)
01853 {
01854   int member;
01855   struct type *settype = check_typedef (value_type (set));
01856   struct type *eltype = check_typedef (value_type (element));
01857 
01858   if (TYPE_CODE (eltype) == TYPE_CODE_RANGE)
01859     eltype = TYPE_TARGET_TYPE (eltype);
01860   if (TYPE_CODE (settype) != TYPE_CODE_SET)
01861     error (_("Second argument of 'IN' has wrong type"));
01862   if (TYPE_CODE (eltype) != TYPE_CODE_INT
01863       && TYPE_CODE (eltype) != TYPE_CODE_CHAR
01864       && TYPE_CODE (eltype) != TYPE_CODE_ENUM
01865       && TYPE_CODE (eltype) != TYPE_CODE_BOOL)
01866     error (_("First argument of 'IN' has wrong type"));
01867   member = value_bit_index (settype, value_contents (set),
01868                             value_as_long (element));
01869   if (member < 0)
01870     error (_("First argument of 'IN' not in range"));
01871   return member;
01872 }
01873 
01874 void
01875 _initialize_valarith (void)
01876 {
01877 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines