home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / valarith.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  31.0 KB  |  1,138 lines

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