home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / valarith.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-13  |  29.2 KB  |  1,069 lines

  1. /* Perform arithmetic and other operations on values, for GDB.
  2.    Copyright 1986, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "value.h"
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "expression.h"
  25. #include "target.h"
  26. #include "language.h"
  27. #include "demangle.h"
  28. #include <string.h>
  29.  
  30. /* Define whether or not the C operator '/' truncates towards zero for
  31.    differently signed operands (truncation direction is undefined in C). */
  32.  
  33. #ifndef TRUNCATION_TOWARDS_ZERO
  34. #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
  35. #endif
  36.  
  37. static value_ptr value_subscripted_rvalue PARAMS ((value_ptr, value_ptr));
  38.  
  39.  
  40. value_ptr
  41. value_add (arg1, arg2)
  42.      value_ptr arg1, arg2;
  43. {
  44.   register value_ptr valint, valptr;
  45.   register int len;
  46.  
  47.   COERCE_ARRAY (arg1);
  48.   COERCE_ARRAY (arg2);
  49.  
  50.   if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
  51.        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR)
  52.       &&
  53.       (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT
  54.        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT))
  55.     /* Exactly one argument is a pointer, and one is an integer.  */
  56.     {
  57.       if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  58.     {
  59.       valptr = arg1;
  60.       valint = arg2;
  61.     }
  62.       else
  63.     {
  64.       valptr = arg2;
  65.       valint = arg1;
  66.     }
  67.       len = TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr)));
  68.       if (len == 0) len = 1;    /* For (void *) */
  69.       return value_from_longest (VALUE_TYPE (valptr),
  70.                   value_as_long (valptr)
  71.                   + (len * value_as_long (valint)));
  72.     }
  73.  
  74.   return value_binop (arg1, arg2, BINOP_ADD);
  75. }
  76.  
  77. value_ptr
  78. value_sub (arg1, arg2)
  79.      value_ptr arg1, arg2;
  80. {
  81.  
  82.   COERCE_ARRAY (arg1);
  83.   COERCE_ARRAY (arg2);
  84.  
  85.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  86.     {
  87.       if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
  88.     {
  89.       /* pointer - integer.  */
  90.       return value_from_longest
  91.         (VALUE_TYPE (arg1),
  92.          value_as_long (arg1)
  93.          - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)))
  94.         * value_as_long (arg2)));
  95.     }
  96.       else if (VALUE_TYPE (arg1) == VALUE_TYPE (arg2))
  97.     {
  98.       /* pointer to <type x> - pointer to <type x>.  */
  99.       return value_from_longest
  100.         (builtin_type_long,        /* FIXME -- should be ptrdiff_t */
  101.          (value_as_long (arg1) - value_as_long (arg2))
  102.          / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))));
  103.     }
  104.       else
  105.     {
  106.       error ("\
  107. First argument of `-' is a pointer and second argument is neither\n\
  108. an integer nor a pointer of the same type.");
  109.     }
  110.     }
  111.  
  112.   return value_binop (arg1, arg2, BINOP_SUB);
  113. }
  114.  
  115. /* Return the value of ARRAY[IDX].
  116.    See comments in value_coerce_array() for rationale for reason for
  117.    doing lower bounds adjustment here rather than there.
  118.    FIXME:  Perhaps we should validate that the index is valid and if
  119.    verbosity is set, warn about invalid indices (but still use them). */
  120.  
  121. value_ptr
  122. value_subscript (array, idx)
  123.      value_ptr array, idx;
  124. {
  125.   int lowerbound;
  126.   value_ptr bound;
  127.   struct type *range_type;
  128.  
  129.   COERCE_REF (array);
  130.  
  131.   if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
  132.       || TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_STRING)
  133.     {
  134.       range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
  135.       lowerbound = TYPE_FIELD_BITPOS (range_type, 0);
  136.       if (lowerbound != 0)
  137.     {
  138.       bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
  139.       idx = value_sub (idx, bound);
  140.     }
  141.       if (VALUE_LVAL (array) != lval_memory)
  142.     {
  143.       return value_subscripted_rvalue (array, idx);
  144.     }
  145.       array = value_coerce_array (array);
  146.     }
  147.   return value_ind (value_add (array, idx));
  148. }
  149.  
  150. /* Return the value of EXPR[IDX], expr an aggregate rvalue
  151.    (eg, a vector register).  This routine used to promote floats
  152.    to doubles, but no longer does.  */
  153.  
  154. static value_ptr
  155. value_subscripted_rvalue (array, idx)
  156.      value_ptr array, idx;
  157. {
  158.   struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
  159.   int elt_size = TYPE_LENGTH (elt_type);
  160.   int elt_offs = elt_size * longest_to_int (value_as_long (idx));
  161.   value_ptr v;
  162.  
  163.   if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array)))
  164.     error ("no such vector element");
  165.  
  166.   v = allocate_value (elt_type);
  167.   memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
  168.  
  169.   if (VALUE_LVAL (array) == lval_internalvar)
  170.     VALUE_LVAL (v) = lval_internalvar_component;
  171.   else
  172.     VALUE_LVAL (v) = not_lval;
  173.   VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
  174.   VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
  175.   VALUE_BITSIZE (v) = elt_size * 8;
  176.   return v;
  177. }
  178.  
  179. /* Check to see if either argument is a structure.  This is called so
  180.    we know whether to go ahead with the normal binop or look for a 
  181.    user defined function instead.
  182.  
  183.    For now, we do not overload the `=' operator.  */
  184.  
  185. int
  186. binop_user_defined_p (op, arg1, arg2)
  187.      enum exp_opcode op;
  188.      value_ptr arg1, arg2;
  189. {
  190.   if (op == BINOP_ASSIGN)
  191.     return 0;
  192.   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
  193.       || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
  194.       || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
  195.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
  196.       || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
  197.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
  198. }
  199.  
  200. /* Check to see if argument is a structure.  This is called so
  201.    we know whether to go ahead with the normal unop or look for a 
  202.    user defined function instead.
  203.  
  204.    For now, we do not overload the `&' operator.  */
  205.  
  206. int unop_user_defined_p (op, arg1)
  207.      enum exp_opcode op;
  208.      value_ptr arg1;
  209. {
  210.   if (op == UNOP_ADDR)
  211.     return 0;
  212.   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
  213.       || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
  214.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
  215. }
  216.  
  217. /* We know either arg1 or arg2 is a structure, so try to find the right
  218.    user defined function.  Create an argument vector that calls 
  219.    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
  220.    binary operator which is legal for GNU C++).
  221.  
  222.    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
  223.    is the opcode saying how to modify it.  Otherwise, OTHEROP is
  224.    unused.  */
  225.  
  226. value_ptr
  227. value_x_binop (arg1, arg2, op, otherop)
  228.      value_ptr arg1, arg2;
  229.      enum exp_opcode op, otherop;
  230. {
  231.   value_ptr * argvec;
  232.   char *ptr;
  233.   char tstr[13];
  234.   int static_memfuncp;
  235.  
  236.   COERCE_REF (arg1);
  237.   COERCE_REF (arg2);
  238.   COERCE_ENUM (arg1);
  239.   COERCE_ENUM (arg2);
  240.  
  241.   /* now we know that what we have to do is construct our
  242.      arg vector and find the right function to call it with.  */
  243.  
  244.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
  245.     error ("Can't do that binary op on that type");  /* FIXME be explicit */
  246.  
  247.   argvec = (value_ptr *) alloca (sizeof (value_ptr) * 4);
  248.   argvec[1] = value_addr (arg1);
  249.   argvec[2] = arg2;
  250.   argvec[3] = 0;
  251.  
  252.   /* make the right function name up */  
  253.   strcpy(tstr, "operator__");
  254.   ptr = tstr+8;
  255.   switch (op)
  256.     {
  257.     case BINOP_ADD:        strcpy(ptr,"+"); break;
  258.     case BINOP_SUB:        strcpy(ptr,"-"); break;
  259.     case BINOP_MUL:        strcpy(ptr,"*"); break;
  260.     case BINOP_DIV:        strcpy(ptr,"/"); break;
  261.     case BINOP_REM:        strcpy(ptr,"%"); break;
  262.     case BINOP_LSH:        strcpy(ptr,"<<"); break;
  263.     case BINOP_RSH:        strcpy(ptr,">>"); break;
  264.     case BINOP_BITWISE_AND:    strcpy(ptr,"&"); break;
  265.     case BINOP_BITWISE_IOR:    strcpy(ptr,"|"); break;
  266.     case BINOP_BITWISE_XOR:    strcpy(ptr,"^"); break;
  267.     case BINOP_LOGICAL_AND:    strcpy(ptr,"&&"); break;
  268.     case BINOP_LOGICAL_OR:    strcpy(ptr,"||"); break;
  269.     case BINOP_MIN:        strcpy(ptr,"<?"); break;
  270.     case BINOP_MAX:        strcpy(ptr,">?"); break;
  271.     case BINOP_ASSIGN:        strcpy(ptr,"="); break;
  272.     case BINOP_ASSIGN_MODIFY:    
  273.       switch (otherop)
  274.     {
  275.     case BINOP_ADD:        strcpy(ptr,"+="); break;
  276.     case BINOP_SUB:        strcpy(ptr,"-="); break;
  277.     case BINOP_MUL:        strcpy(ptr,"*="); break;
  278.     case BINOP_DIV:        strcpy(ptr,"/="); break;
  279.     case BINOP_REM:        strcpy(ptr,"%="); break;
  280.     case BINOP_BITWISE_AND:    strcpy(ptr,"&="); break;
  281.     case BINOP_BITWISE_IOR:    strcpy(ptr,"|="); break;
  282.     case BINOP_BITWISE_XOR:    strcpy(ptr,"^="); break;
  283.     case BINOP_MOD:        /* invalid */
  284.     default:
  285.       error ("Invalid binary operation specified.");
  286.     }
  287.       break;
  288.     case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
  289.     case BINOP_EQUAL:      strcpy(ptr,"=="); break;
  290.     case BINOP_NOTEQUAL:  strcpy(ptr,"!="); break;
  291.     case BINOP_LESS:      strcpy(ptr,"<"); break;
  292.     case BINOP_GTR:       strcpy(ptr,">"); break;
  293.     case BINOP_GEQ:       strcpy(ptr,">="); break;
  294.     case BINOP_LEQ:       strcpy(ptr,"<="); break;
  295.     case BINOP_MOD:      /* invalid */
  296.     default:
  297.       error ("Invalid binary operation specified.");
  298.     }
  299.  
  300.   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
  301.   
  302.   if (argvec[0])
  303.     {
  304.       if (static_memfuncp)
  305.     {
  306.       argvec[1] = argvec[0];
  307.       argvec++;
  308.     }
  309.       return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
  310.     }
  311.   error ("member function %s not found", tstr);
  312. #ifdef lint
  313.   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
  314. #endif
  315. }
  316.  
  317. /* We know that arg1 is a structure, so try to find a unary user
  318.    defined operator that matches the operator in question.  
  319.    Create an argument vector that calls arg1.operator @ (arg1)
  320.    and return that value (where '@' is (almost) any unary operator which
  321.    is legal for GNU C++).  */
  322.  
  323. value_ptr
  324. value_x_unop (arg1, op)
  325.      value_ptr arg1;
  326.      enum exp_opcode op;
  327. {
  328.   value_ptr * argvec;
  329.   char *ptr, *mangle_ptr;
  330.   char tstr[13], mangle_tstr[13];
  331.   int static_memfuncp;
  332.  
  333.   COERCE_ENUM (arg1);
  334.  
  335.   /* now we know that what we have to do is construct our
  336.      arg vector and find the right function to call it with.  */
  337.  
  338.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
  339.     error ("Can't do that unary op on that type");  /* FIXME be explicit */
  340.  
  341.   argvec = (value_ptr *) alloca (sizeof (value_ptr) * 3);
  342.   argvec[1] = value_addr (arg1);
  343.   argvec[2] = 0;
  344.  
  345.   /* make the right function name up */  
  346.   strcpy(tstr,"operator__");
  347.   ptr = tstr+8;
  348.   strcpy(mangle_tstr, "__");
  349.   mangle_ptr = mangle_tstr+2;
  350.   switch (op)
  351.     {
  352.     case UNOP_PREINCREMENT:    strcpy(ptr,"++"); break;
  353.     case UNOP_PREDECREMENT:    strcpy(ptr,"++"); break;
  354.     case UNOP_POSTINCREMENT:    strcpy(ptr,"++"); break;
  355.     case UNOP_POSTDECREMENT:    strcpy(ptr,"++"); break;
  356.     case UNOP_LOGICAL_NOT:    strcpy(ptr,"!"); break;
  357.     case UNOP_COMPLEMENT:    strcpy(ptr,"~"); break;
  358.     case UNOP_NEG:        strcpy(ptr,"-"); break;
  359.     default:
  360.       error ("Invalid binary operation specified.");
  361.     }
  362.  
  363.   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
  364.  
  365.   if (argvec[0])
  366.     {
  367.       if (static_memfuncp)
  368.     {
  369.       argvec[1] = argvec[0];
  370.       argvec++;
  371.     }
  372.       return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
  373.     }
  374.   error ("member function %s not found", tstr);
  375.   return 0;  /* For lint -- never reached */
  376. }
  377.  
  378.  
  379. /* Concatenate two values with the following conditions:
  380.  
  381.    (1)    Both values must be either bitstring values or character string
  382.     values and the resulting value consists of the concatenation of
  383.     ARG1 followed by ARG2.
  384.  
  385.     or
  386.  
  387.     One value must be an integer value and the other value must be
  388.     either a bitstring value or character string value, which is
  389.     to be repeated by the number of times specified by the integer
  390.     value.
  391.  
  392.  
  393.     (2)    Boolean values are also allowed and are treated as bit string
  394.         values of length 1.
  395.  
  396.     (3)    Character values are also allowed and are treated as character
  397.         string values of length 1.
  398. */
  399.  
  400. value_ptr
  401. value_concat (arg1, arg2)
  402.      value_ptr arg1, arg2;
  403. {
  404.   register value_ptr inval1, inval2, outval;
  405.   int inval1len, inval2len;
  406.   int count, idx;
  407.   char *ptr;
  408.   char inchar;
  409.  
  410.   /* First figure out if we are dealing with two values to be concatenated
  411.      or a repeat count and a value to be repeated.  INVAL1 is set to the
  412.      first of two concatenated values, or the repeat count.  INVAL2 is set
  413.      to the second of the two concatenated values or the value to be 
  414.      repeated. */
  415.  
  416.   if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
  417.     {
  418.       inval1 = arg2;
  419.       inval2 = arg1;
  420.     }
  421.   else
  422.     {
  423.       inval1 = arg1;
  424.       inval2 = arg2;
  425.     }
  426.  
  427.   /* Now process the input values. */
  428.  
  429.   if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_INT)
  430.     {
  431.       /* We have a repeat count.  Validate the second value and then
  432.      construct a value repeated that many times. */
  433.       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_STRING
  434.       || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
  435.     {
  436.       count = longest_to_int (value_as_long (inval1));
  437.       inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
  438.       ptr = (char *) alloca (count * inval2len);
  439.       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
  440.         {
  441.           inchar = (char) unpack_long (VALUE_TYPE (inval2),
  442.                        VALUE_CONTENTS (inval2));
  443.           for (idx = 0; idx < count; idx++)
  444.         {
  445.           *(ptr + idx) = inchar;
  446.         }
  447.         }
  448.       else
  449.         {
  450.           for (idx = 0; idx < count; idx++)
  451.         {
  452.           memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
  453.               inval2len);
  454.         }
  455.         }
  456.       outval = value_string (ptr, count * inval2len);
  457.     }
  458.       else if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BITSTRING
  459.            || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BOOL)
  460.     {
  461.       error ("unimplemented support for bitstring/boolean repeats");
  462.     }
  463.       else
  464.     {
  465.       error ("can't repeat values of that type");
  466.     }
  467.     }
  468.   else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_STRING
  469.       || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
  470.     {
  471.       /* We have two character strings to concatenate. */
  472.       if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_STRING
  473.       && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_CHAR)
  474.     {
  475.       error ("Strings can only be concatenated with other strings.");
  476.     }
  477.       inval1len = TYPE_LENGTH (VALUE_TYPE (inval1));
  478.       inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
  479.       ptr = (char *) alloca (inval1len + inval2len);
  480.       if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
  481.     {
  482.       *ptr = (char) unpack_long (VALUE_TYPE (inval1), VALUE_CONTENTS (inval1));
  483.     }
  484.       else
  485.     {
  486.       memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
  487.     }
  488.       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
  489.     {
  490.       *(ptr + inval1len) = 
  491.         (char) unpack_long (VALUE_TYPE (inval2), VALUE_CONTENTS (inval2));
  492.     }
  493.       else
  494.     {
  495.       memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
  496.     }
  497.       outval = value_string (ptr, inval1len + inval2len);
  498.     }
  499.   else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BITSTRING
  500.        || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BOOL)
  501.     {
  502.       /* We have two bitstrings to concatenate. */
  503.       if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BITSTRING
  504.       && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BOOL)
  505.     {
  506.       error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
  507.     }
  508.       error ("unimplemented support for bitstring/boolean concatenation.");
  509.     }      
  510.   else
  511.     {
  512.       /* We don't know how to concatenate these operands. */
  513.       error ("illegal operands for concatenation.");
  514.     }
  515.   return (outval);
  516. }
  517.  
  518.  
  519.  
  520. /* Perform a binary operation on two operands which have reasonable
  521.    representations as integers or floats.  This includes booleans,
  522.    characters, integers, or floats.
  523.    Does not support addition and subtraction on pointers;
  524.    use value_add or value_sub if you want to handle those possibilities.  */
  525.  
  526. value_ptr
  527. value_binop (arg1, arg2, op)
  528.      value_ptr arg1, arg2;
  529.      enum exp_opcode op;
  530. {
  531.   register value_ptr val;
  532.  
  533.   COERCE_ENUM (arg1);
  534.   COERCE_ENUM (arg2);
  535.  
  536.   if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT
  537.        && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_CHAR
  538.        && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT
  539.        && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL
  540.        && TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_RANGE)
  541.       ||
  542.       (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT
  543.        && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_CHAR
  544.        && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT
  545.        && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL
  546.        && TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_RANGE))
  547.     error ("Argument to arithmetic operation not a number or boolean.");
  548.  
  549.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT
  550.       ||
  551.       TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT)
  552.     {
  553.       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
  554.      in target format.  real.c in GCC probably has the necessary
  555.      code.  */
  556.       double v1, v2, v;
  557.       v1 = value_as_double (arg1);
  558.       v2 = value_as_double (arg2);
  559.       switch (op)
  560.     {
  561.     case BINOP_ADD:
  562.       v = v1 + v2;
  563.       break;
  564.  
  565.     case BINOP_SUB:
  566.       v = v1 - v2;
  567.       break;
  568.  
  569.     case BINOP_MUL:
  570.       v = v1 * v2;
  571.       break;
  572.  
  573.     case BINOP_DIV:
  574.       v = v1 / v2;
  575.       break;
  576.  
  577.     default:
  578.       error ("Integer-only operation on floating point number.");
  579.     }
  580.  
  581.       val = allocate_value (builtin_type_double);
  582.       store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
  583.               v);
  584.     }
  585.   else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL
  586.        &&
  587.        TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL)
  588.       {
  589.       LONGEST v1, v2, v;
  590.       v1 = value_as_long (arg1);
  591.       v2 = value_as_long (arg2);
  592.       
  593.       switch (op)
  594.         {
  595.         case BINOP_BITWISE_AND:
  596.           v = v1 & v2;
  597.           break;
  598.           
  599.         case BINOP_BITWISE_IOR:
  600.           v = v1 | v2;
  601.           break;
  602.           
  603.         case BINOP_BITWISE_XOR:
  604.           v = v1 ^ v2;
  605.           break;
  606.           
  607.         default:
  608.           error ("Invalid operation on booleans.");
  609.         }
  610.       
  611.       val = allocate_value (builtin_type_chill_bool);
  612.       store_signed_integer (VALUE_CONTENTS_RAW (val),
  613.                 TYPE_LENGTH (VALUE_TYPE (val)),
  614.                 v);
  615.       }
  616.   else
  617.     /* Integral operations here.  */
  618.     /* FIXME:  Also mixed integral/booleans, with result an integer. */
  619.     {
  620.       /* Should we promote to unsigned longest?  */
  621.       if ((TYPE_UNSIGNED (VALUE_TYPE (arg1))
  622.        || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
  623.       && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST)
  624.           || TYPE_LENGTH (VALUE_TYPE (arg2)) >= sizeof (unsigned LONGEST)))
  625.     {
  626.       unsigned LONGEST v1, v2, v;
  627.       v1 = (unsigned LONGEST) value_as_long (arg1);
  628.       v2 = (unsigned LONGEST) value_as_long (arg2);
  629.       
  630.       switch (op)
  631.         {
  632.         case BINOP_ADD:
  633.           v = v1 + v2;
  634.           break;
  635.           
  636.         case BINOP_SUB:
  637.           v = v1 - v2;
  638.           break;
  639.           
  640.         case BINOP_MUL:
  641.           v = v1 * v2;
  642.           break;
  643.           
  644.         case BINOP_DIV:
  645.           v = v1 / v2;
  646.           break;
  647.           
  648.         case BINOP_REM:
  649.           v = v1 % v2;
  650.           break;
  651.           
  652.         case BINOP_MOD:
  653.           /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
  654.              v1 mod 0 has a defined value, v1. */
  655.           /* Chill specifies that v2 must be > 0, so check for that. */
  656.           if (current_language -> la_language == language_chill
  657.           && value_as_long (arg2) <= 0)
  658.         {
  659.           error ("Second operand of MOD must be greater than zero.");
  660.         }
  661.           if (v2 == 0)
  662.         {
  663.           v = v1;
  664.         }
  665.           else
  666.         {
  667.           v = v1/v2;
  668.           /* Note floor(v1/v2) == v1/v2 for unsigned. */
  669.           v = v1 - (v2 * v);
  670.         }
  671.           break;
  672.           
  673.         case BINOP_LSH:
  674.           v = v1 << v2;
  675.           break;
  676.           
  677.         case BINOP_RSH:
  678.           v = v1 >> v2;
  679.           break;
  680.           
  681.         case BINOP_BITWISE_AND:
  682.           v = v1 & v2;
  683.           break;
  684.           
  685.         case BINOP_BITWISE_IOR:
  686.           v = v1 | v2;
  687.           break;
  688.           
  689.         case BINOP_BITWISE_XOR:
  690.           v = v1 ^ v2;
  691.           break;
  692.           
  693.         case BINOP_LOGICAL_AND:
  694.           v = v1 && v2;
  695.           break;
  696.           
  697.         case BINOP_LOGICAL_OR:
  698.           v = v1 || v2;
  699.           break;
  700.           
  701.         case BINOP_MIN:
  702.           v = v1 < v2 ? v1 : v2;
  703.           break;
  704.           
  705.         case BINOP_MAX:
  706.           v = v1 > v2 ? v1 : v2;
  707.           break;
  708.           
  709.         default:
  710.           error ("Invalid binary operation on numbers.");
  711.         }
  712.  
  713.       /* This is a kludge to get around the fact that we don't
  714.          know how to determine the result type from the types of
  715.          the operands.  (I'm not really sure how much we feel the
  716.          need to duplicate the exact rules of the current
  717.          language.  They can get really hairy.  But not to do so
  718.          makes it hard to document just what we *do* do).  */
  719.  
  720.       /* Can't just call init_type because we wouldn't know what
  721.          name to give the type.  */
  722.       val = allocate_value
  723.         (sizeof (LONGEST) > TARGET_LONG_BIT / HOST_CHAR_BIT
  724.          ? builtin_type_unsigned_long_long
  725.          : builtin_type_unsigned_long);
  726.       store_unsigned_integer (VALUE_CONTENTS_RAW (val),
  727.                   TYPE_LENGTH (VALUE_TYPE (val)),
  728.                   v);
  729.     }
  730.       else
  731.     {
  732.       LONGEST v1, v2, v;
  733.       v1 = value_as_long (arg1);
  734.       v2 = value_as_long (arg2);
  735.       
  736.       switch (op)
  737.         {
  738.         case BINOP_ADD:
  739.           v = v1 + v2;
  740.           break;
  741.           
  742.         case BINOP_SUB:
  743.           v = v1 - v2;
  744.           break;
  745.           
  746.         case BINOP_MUL:
  747.           v = v1 * v2;
  748.           break;
  749.           
  750.         case BINOP_DIV:
  751.           v = v1 / v2;
  752.           break;
  753.           
  754.         case BINOP_REM:
  755.           v = v1 % v2;
  756.           break;
  757.           
  758.         case BINOP_MOD:
  759.           /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
  760.              X mod 0 has a defined value, X. */
  761.           /* Chill specifies that v2 must be > 0, so check for that. */
  762.           if (current_language -> la_language == language_chill
  763.           && v2 <= 0)
  764.         {
  765.           error ("Second operand of MOD must be greater than zero.");
  766.         }
  767.           if (v2 == 0)
  768.         {
  769.           v = v1;
  770.         }
  771.           else
  772.         {
  773.           v = v1/v2;
  774.           /* Compute floor. */
  775.           if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
  776.             {
  777.               v--;
  778.             }
  779.           v = v1 - (v2 * v);
  780.         }
  781.           break;
  782.           
  783.         case BINOP_LSH:
  784.           v = v1 << v2;
  785.           break;
  786.           
  787.         case BINOP_RSH:
  788.           v = v1 >> v2;
  789.           break;
  790.           
  791.         case BINOP_BITWISE_AND:
  792.           v = v1 & v2;
  793.           break;
  794.           
  795.         case BINOP_BITWISE_IOR:
  796.           v = v1 | v2;
  797.           break;
  798.           
  799.         case BINOP_BITWISE_XOR:
  800.           v = v1 ^ v2;
  801.           break;
  802.           
  803.         case BINOP_LOGICAL_AND:
  804.           v = v1 && v2;
  805.           break;
  806.           
  807.         case BINOP_LOGICAL_OR:
  808.           v = v1 || v2;
  809.           break;
  810.           
  811.         case BINOP_MIN:
  812.           v = v1 < v2 ? v1 : v2;
  813.           break;
  814.           
  815.         case BINOP_MAX:
  816.           v = v1 > v2 ? v1 : v2;
  817.           break;
  818.           
  819.         default:
  820.           error ("Invalid binary operation on numbers.");
  821.         }
  822.  
  823.       /* This is a kludge to get around the fact that we don't
  824.          know how to determine the result type from the types of
  825.          the operands.  (I'm not really sure how much we feel the
  826.          need to duplicate the exact rules of the current
  827.          language.  They can get really hairy.  But not to do so
  828.          makes it hard to document just what we *do* do).  */
  829.  
  830.       /* Can't just call init_type because we wouldn't know what
  831.          name to give the type.  */
  832.       val = allocate_value
  833.         (sizeof (LONGEST) > TARGET_LONG_BIT / HOST_CHAR_BIT
  834.          ? builtin_type_long_long
  835.          : builtin_type_long);
  836.       store_signed_integer (VALUE_CONTENTS_RAW (val),
  837.                 TYPE_LENGTH (VALUE_TYPE (val)),
  838.                 v);
  839.     }
  840.     }
  841.  
  842.   return val;
  843. }
  844.  
  845. /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
  846.  
  847. int
  848. value_logical_not (arg1)
  849.      value_ptr arg1;
  850. {
  851.   register int len;
  852.   register char *p;
  853.  
  854.   COERCE_ARRAY (arg1);
  855.  
  856.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT)
  857.     return 0 == value_as_double (arg1);
  858.  
  859.   len = TYPE_LENGTH (VALUE_TYPE (arg1));
  860.   p = VALUE_CONTENTS (arg1);
  861.  
  862.   while (--len >= 0)
  863.     {
  864.       if (*p++)
  865.     break;
  866.     }
  867.  
  868.   return len < 0;
  869. }
  870.  
  871. /* Simulate the C operator == by returning a 1
  872.    iff ARG1 and ARG2 have equal contents.  */
  873.  
  874. int
  875. value_equal (arg1, arg2)
  876.      register value_ptr arg1, arg2;
  877.  
  878. {
  879.   register int len;
  880.   register char *p1, *p2;
  881.   enum type_code code1;
  882.   enum type_code code2;
  883.  
  884.   COERCE_ARRAY (arg1);
  885.   COERCE_ARRAY (arg2);
  886.  
  887.   code1 = TYPE_CODE (VALUE_TYPE (arg1));
  888.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  889.  
  890.   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
  891.     return value_as_long (arg1) == value_as_long (arg2);
  892.   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
  893.        && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
  894.     return value_as_double (arg1) == value_as_double (arg2);
  895.  
  896.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  897.      is bigger.  */
  898.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
  899.     return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
  900.   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
  901.     return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
  902.  
  903.   else if (code1 == code2
  904.        && ((len = TYPE_LENGTH (VALUE_TYPE (arg1)))
  905.            == TYPE_LENGTH (VALUE_TYPE (arg2))))
  906.     {
  907.       p1 = VALUE_CONTENTS (arg1);
  908.       p2 = VALUE_CONTENTS (arg2);
  909.       while (--len >= 0)
  910.     {
  911.       if (*p1++ != *p2++)
  912.         break;
  913.     }
  914.       return len < 0;
  915.     }
  916.   else
  917.     {
  918.       error ("Invalid type combination in equality test.");
  919.       return 0;  /* For lint -- never reached */
  920.     }
  921. }
  922.  
  923. /* Simulate the C operator < by returning 1
  924.    iff ARG1's contents are less than ARG2's.  */
  925.  
  926. int
  927. value_less (arg1, arg2)
  928.      register value_ptr arg1, arg2;
  929. {
  930.   register enum type_code code1;
  931.   register enum type_code code2;
  932.  
  933.   COERCE_ARRAY (arg1);
  934.   COERCE_ARRAY (arg2);
  935.  
  936.   code1 = TYPE_CODE (VALUE_TYPE (arg1));
  937.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  938.  
  939.   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
  940.     {
  941.       if (TYPE_UNSIGNED (VALUE_TYPE (arg1))
  942.        || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
  943.     return ((unsigned LONGEST) value_as_long (arg1)
  944.         < (unsigned LONGEST) value_as_long (arg2));
  945.       else
  946.     return value_as_long (arg1) < value_as_long (arg2);
  947.     }
  948.   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
  949.        && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
  950.     return value_as_double (arg1) < value_as_double (arg2);
  951.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
  952.     return value_as_pointer (arg1) < value_as_pointer (arg2);
  953.  
  954.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  955.      is bigger.  */
  956.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
  957.     return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
  958.   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
  959.     return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
  960.  
  961.   else
  962.     {
  963.       error ("Invalid type combination in ordering comparison.");
  964.       return 0;
  965.     }
  966. }
  967.  
  968. /* The unary operators - and ~.  Both free the argument ARG1.  */
  969.  
  970. value_ptr
  971. value_neg (arg1)
  972.      register value_ptr arg1;
  973. {
  974.   register struct type *type;
  975.  
  976.   COERCE_ENUM (arg1);
  977.  
  978.   type = VALUE_TYPE (arg1);
  979.  
  980.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  981.     return value_from_double (type, - value_as_double (arg1));
  982.   else if (TYPE_CODE (type) == TYPE_CODE_INT)
  983.     return value_from_longest (type, - value_as_long (arg1));
  984.   else {
  985.     error ("Argument to negate operation not a number.");
  986.     return 0;  /* For lint -- never reached */
  987.   }
  988. }
  989.  
  990. value_ptr
  991. value_complement (arg1)
  992.      register value_ptr arg1;
  993. {
  994.   COERCE_ENUM (arg1);
  995.  
  996.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
  997.     error ("Argument to complement operation not an integer.");
  998.  
  999.   return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1));
  1000. }
  1001.  
  1002. /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
  1003.    and whose VALUE_CONTENTS is valaddr.
  1004.    Return -1 if out of range, -2 other error. */
  1005.  
  1006. int
  1007. value_bit_index (type, valaddr, index)
  1008.      struct type *type;
  1009.      char *valaddr;
  1010.      int index;
  1011. {
  1012.   struct type *range;
  1013.   int low_bound, high_bound, bit_length;
  1014.   LONGEST word;
  1015.   range = TYPE_FIELD_TYPE (type, 0);
  1016.   if (TYPE_CODE (range) != TYPE_CODE_RANGE)
  1017.     return -2;
  1018.   low_bound = TYPE_LOW_BOUND (range);
  1019.   high_bound = TYPE_HIGH_BOUND (range);
  1020.   if (index < low_bound || index > high_bound)
  1021.     return -1;
  1022.   bit_length = high_bound - low_bound + 1;
  1023.   index -= low_bound;
  1024.   if (bit_length <= TARGET_CHAR_BIT)
  1025.     word = unpack_long (builtin_type_unsigned_char, valaddr);
  1026.   else if (bit_length <= TARGET_SHORT_BIT)
  1027.     word = unpack_long (builtin_type_unsigned_short, valaddr);
  1028.   else
  1029.     {
  1030.       int word_start_index = (index / TARGET_INT_BIT) * TARGET_INT_BIT;
  1031.       index -= word_start_index;
  1032.       word = unpack_long (builtin_type_unsigned_int,
  1033.               valaddr + (word_start_index / HOST_CHAR_BIT));
  1034.     }
  1035. #if BITS_BIG_ENDIAN
  1036.   if (bit_length <= TARGET_CHAR_BIT)
  1037.     index = TARGET_CHAR_BIT - 1 - index;
  1038.   else if (bit_length <= TARGET_SHORT_BIT)
  1039.     index = TARGET_SHORT_BIT - 1 - index;
  1040.   else
  1041.     index = TARGET_INT_BIT - 1 - index;
  1042. #endif
  1043.   return (word >> index) & 1;
  1044. }
  1045.  
  1046. value_ptr
  1047. value_in (element, set)
  1048.      value_ptr element, set;
  1049. {
  1050.   int member;
  1051.   if (TYPE_CODE (VALUE_TYPE (set)) != TYPE_CODE_SET)
  1052.     error ("Second argument of 'IN' has wrong type");
  1053.   if (TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_INT
  1054.       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_CHAR
  1055.       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_ENUM
  1056.       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_BOOL)
  1057.     error ("First argument of 'IN' has wrong type");
  1058.   member = value_bit_index (VALUE_TYPE (set), VALUE_CONTENTS (set),
  1059.                 value_as_long (element));
  1060.   if (member < 0)
  1061.     error ("First argument of 'IN' not in range");
  1062.   return value_from_longest (builtin_type_int, member);
  1063. }
  1064.  
  1065. void
  1066. _initialize_valarith ()
  1067. {
  1068. }
  1069.