home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  30.3 KB  |  1,058 lines

  1. /* Evaluate expressions for GDB.
  2.    Copyright (C) 1986, 1987, 1989 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 <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "symtab.h"
  24. #include "value.h"
  25. #include "expression.h"
  26. #include "target.h"
  27. #include "frame.h"
  28.  
  29. #define    NULL_TYPE    ((struct type *)0)
  30.  
  31.  
  32. /* Parse the string EXP as a C expression, evaluate it,
  33.    and return the result as a number.  */
  34.  
  35. CORE_ADDR
  36. parse_and_eval_address (exp)
  37.      char *exp;
  38. {
  39.   struct expression *expr = parse_c_expression (exp);
  40.   register CORE_ADDR addr;
  41.   register struct cleanup *old_chain
  42.     = make_cleanup (free_current_contents, &expr);
  43.  
  44.   addr = value_as_pointer (evaluate_expression (expr));
  45.   do_cleanups (old_chain);
  46.   return addr;
  47. }
  48.  
  49. /* Like parse_and_eval_address but takes a pointer to a char * variable
  50.    and advanced that variable across the characters parsed.  */
  51.  
  52. CORE_ADDR
  53. parse_and_eval_address_1 (expptr)
  54.      char **expptr;
  55. {
  56.   struct expression *expr = parse_c_1 (expptr, (struct block *)0, 0);
  57.   register CORE_ADDR addr;
  58.   register struct cleanup *old_chain
  59.     = make_cleanup (free_current_contents, &expr);
  60.  
  61.   addr = value_as_pointer (evaluate_expression (expr));
  62.   do_cleanups (old_chain);
  63.   return addr;
  64. }
  65.  
  66. value
  67. parse_and_eval (exp)
  68.      char *exp;
  69. {
  70.   struct expression *expr = parse_c_expression (exp);
  71.   register value val;
  72.   register struct cleanup *old_chain
  73.     = make_cleanup (free_current_contents, &expr);
  74.  
  75.   val = evaluate_expression (expr);
  76.   do_cleanups (old_chain);
  77.   return val;
  78. }
  79.  
  80. /* Parse up to a comma (or to a closeparen)
  81.    in the string EXPP as an expression, evaluate it, and return the value.
  82.    EXPP is advanced to point to the comma.  */
  83.  
  84. value
  85. parse_to_comma_and_eval (expp)
  86.      char **expp;
  87. {
  88.   struct expression *expr = parse_c_1 (expp, (struct block *) 0, 1);
  89.   register value val;
  90.   register struct cleanup *old_chain
  91.     = make_cleanup (free_current_contents, &expr);
  92.  
  93.   val = evaluate_expression (expr);
  94.   do_cleanups (old_chain);
  95.   return val;
  96. }
  97.  
  98. /* Evaluate an expression in internal prefix form
  99.    such as is constructed by expread.y.
  100.  
  101.    See expression.h for info on the format of an expression.  */
  102.  
  103. static value evaluate_subexp ();
  104. static value evaluate_subexp_for_address ();
  105. static value evaluate_subexp_for_sizeof ();
  106. static value evaluate_subexp_with_coercion ();
  107.  
  108. /* Values of NOSIDE argument to eval_subexp.  */
  109. enum noside
  110. { EVAL_NORMAL,
  111.   EVAL_SKIP,            /* Only effect is to increment pos.  */
  112.   EVAL_AVOID_SIDE_EFFECTS,    /* Don't modify any variables or
  113.                    call any functions.  The value
  114.                    returned will have the correct
  115.                    type, and will have an
  116.                    approximately correct lvalue
  117.                    type (inaccuracy: anything that is
  118.                    listed as being in a register in
  119.                    the function in which it was
  120.                    declared will be lval_register).  */
  121. };
  122.  
  123. value
  124. evaluate_expression (exp)
  125.      struct expression *exp;
  126. {
  127.   int pc = 0;
  128.   return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
  129. }
  130.  
  131. /* Evaluate an expression, avoiding all memory references
  132.    and getting a value whose type alone is correct.  */
  133.  
  134. value
  135. evaluate_type (exp)
  136.      struct expression *exp;
  137. {
  138.   int pc = 0;
  139.   return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
  140. }
  141.  
  142. static value
  143. evaluate_subexp (expect_type, exp, pos, noside)
  144.      struct type *expect_type;
  145.      register struct expression *exp;
  146.      register int *pos;
  147.      enum noside noside;
  148. {
  149.   enum exp_opcode op;
  150.   int tem;
  151.   register int pc, pc2, oldpos;
  152.   register value arg1, arg2, arg3;
  153.   int nargs;
  154.   value *argvec;
  155.  
  156.   pc = (*pos)++;
  157.   op = exp->elts[pc].opcode;
  158.  
  159.   switch (op)
  160.     {
  161.     case OP_SCOPE:
  162.       tem = strlen (&exp->elts[pc + 2].string);
  163.       (*pos) += 3 + ((tem + sizeof (union exp_element))
  164.              / sizeof (union exp_element));
  165.       arg1 = value_static_field (exp->elts[pc + 1].type,
  166.                  &exp->elts[pc + 2].string, -1);
  167.       if (arg1 == NULL)
  168.     error ("There is no field named %s", &exp->elts[pc + 2].string);
  169.       return arg1;
  170.  
  171.     case OP_LONG:
  172.       (*pos) += 3;
  173.       return value_from_long (exp->elts[pc + 1].type,
  174.                   exp->elts[pc + 2].longconst);
  175.  
  176.     case OP_DOUBLE:
  177.       (*pos) += 3;
  178.       return value_from_double (exp->elts[pc + 1].type,
  179.                 exp->elts[pc + 2].doubleconst);
  180.  
  181.     case OP_VAR_VALUE:
  182.       (*pos) += 2;
  183.       if (noside == EVAL_SKIP)
  184.     goto nosideret;
  185.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  186.     {
  187.       struct symbol * sym = exp->elts[pc + 1].symbol;
  188.       enum lval_type lv;
  189.  
  190.       switch (SYMBOL_CLASS (sym))
  191.         {
  192.         case LOC_CONST:
  193.         case LOC_LABEL:
  194.         case LOC_CONST_BYTES:
  195.           lv = not_lval;
  196.           break;
  197.  
  198.         case LOC_REGISTER:
  199.         case LOC_REGPARM:
  200.           lv = lval_register;
  201.           break;
  202.  
  203.         default:
  204.           lv = lval_memory;
  205.           break;
  206.         }
  207.  
  208.       return value_zero (SYMBOL_TYPE (sym), lv);
  209.     }
  210.       else
  211.     return value_of_variable (exp->elts[pc + 1].symbol);
  212.  
  213.     case OP_LAST:
  214.       (*pos) += 2;
  215.       return
  216.     access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
  217.  
  218.     case OP_REGISTER:
  219.       (*pos) += 2;
  220.       return value_of_register (longest_to_int (exp->elts[pc + 1].longconst));
  221.  
  222.     case OP_INTERNALVAR:
  223.       (*pos) += 2;
  224.       return value_of_internalvar (exp->elts[pc + 1].internalvar);
  225.  
  226.     case OP_STRING:
  227.       tem = strlen (&exp->elts[pc + 1].string);
  228.       (*pos) += 2 + ((tem + sizeof (union exp_element))
  229.              / sizeof (union exp_element));
  230.       if (noside == EVAL_SKIP)
  231.     goto nosideret;
  232.       return value_string (&exp->elts[pc + 1].string, tem);
  233.  
  234.     case TERNOP_COND:
  235.       /* Skip third and second args to evaluate the first one.  */
  236.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  237.       if (value_zerop (arg1))
  238.     {
  239.       evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
  240.       return evaluate_subexp (NULL_TYPE, exp, pos, noside);
  241.     }
  242.       else
  243.     {
  244.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  245.       evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
  246.       return arg2;
  247.     }
  248.  
  249.     case OP_FUNCALL:
  250.       (*pos) += 2;
  251.       op = exp->elts[*pos].opcode;
  252.       if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
  253.     {
  254.       int fnptr;
  255.  
  256.       nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
  257.       /* First, evaluate the structure into arg2 */
  258.       pc2 = (*pos)++;
  259.  
  260.       if (noside == EVAL_SKIP)
  261.         goto nosideret;
  262.  
  263.       if (op == STRUCTOP_MEMBER)
  264.         {
  265.           arg2 = evaluate_subexp_for_address (exp, pos, noside);
  266.         }
  267.       else
  268.         {
  269.           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  270.         }
  271.  
  272.       /* If the function is a virtual function, then the
  273.          aggregate value (providing the structure) plays
  274.          its part by providing the vtable.  Otherwise,
  275.          it is just along for the ride: call the function
  276.          directly.  */
  277.  
  278.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  279.  
  280.       fnptr = longest_to_int (value_as_long (arg1));
  281.       /* FIXME-tiemann: this is way obsolete.  */
  282.       if (fnptr < 128)
  283.         {
  284.           struct type *basetype;
  285.           int i, j;
  286.           basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
  287.           basetype = TYPE_VPTR_BASETYPE (basetype);
  288.           for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
  289.         {
  290.           struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
  291.           /* If one is virtual, then all are virtual.  */
  292.           if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
  293.             for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
  294.               if (TYPE_FN_FIELD_VOFFSET (f, j) == fnptr)
  295.             {
  296.               value vtbl;
  297.               value base = value_ind (arg2);
  298.               struct type *fntype = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  299.  
  300.               if (TYPE_VPTR_FIELDNO (basetype) < 0)
  301.                 fill_in_vptr_fieldno (basetype);
  302.  
  303.               VALUE_TYPE (base) = basetype;
  304.               vtbl = value_field (base, TYPE_VPTR_FIELDNO (basetype));
  305.               VALUE_TYPE (vtbl) = lookup_pointer_type (fntype);
  306.               VALUE_TYPE (arg1) = builtin_type_int;
  307.               arg1 = value_subscript (vtbl, arg1);
  308.               VALUE_TYPE (arg1) = fntype;
  309.               goto got_it;
  310.             }
  311.         }
  312.           if (i < 0)
  313.         error ("virtual function at index %d not found", fnptr);
  314.         }
  315.       else
  316.         {
  317.           VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
  318.         }
  319.     got_it:
  320.  
  321.       /* Now, say which argument to start evaluating from */
  322.       tem = 2;
  323.     }
  324.       else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
  325.     {
  326.       /* Hair for method invocations */
  327.       int tem2;
  328.  
  329.       nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
  330.       /* First, evaluate the structure into arg2 */
  331.       pc2 = (*pos)++;
  332.       tem2 = strlen (&exp->elts[pc2 + 1].string);
  333.       *pos += 2 + (tem2 + sizeof (union exp_element)) / sizeof (union exp_element);
  334.       if (noside == EVAL_SKIP)
  335.         goto nosideret;
  336.  
  337.       if (op == STRUCTOP_STRUCT)
  338.         {
  339.           arg2 = evaluate_subexp_for_address (exp, pos, noside);
  340.         }
  341.       else
  342.         {
  343.           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  344.         }
  345.       /* Now, say which argument to start evaluating from */
  346.       tem = 2;
  347.     }
  348.       else
  349.     {
  350.       nargs = longest_to_int (exp->elts[pc + 1].longconst);
  351.       tem = 0;
  352.     }
  353.       argvec = (value *) alloca (sizeof (value) * (nargs + 2));
  354.       for (; tem <= nargs; tem++)
  355.     /* Ensure that array expressions are coerced into pointer objects. */
  356.     argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
  357.  
  358.       /* signal end of arglist */
  359.       argvec[tem] = 0;
  360.  
  361.       if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
  362.     {
  363.       int static_memfuncp;
  364.       value temp = arg2;
  365.  
  366.       argvec[1] = arg2;
  367.       argvec[0] =
  368.         value_struct_elt (&temp, argvec+1, &exp->elts[pc2 + 1].string,
  369.                   &static_memfuncp,
  370.                   op == STRUCTOP_STRUCT
  371.                   ? "structure" : "structure pointer");
  372.       if (VALUE_OFFSET (temp))
  373.         {
  374.           arg2 = value_from_long (builtin_type_long,
  375.                       value_as_long (arg2)+VALUE_OFFSET (temp));
  376.           VALUE_TYPE (arg2) = lookup_pointer_type (VALUE_TYPE (temp));
  377.           argvec[1] = arg2;
  378.         }
  379.       if (static_memfuncp)
  380.         {
  381.           argvec[1] = argvec[0];
  382.           nargs--;
  383.           argvec++;
  384.         }
  385.     }
  386.       else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
  387.     {
  388.       argvec[1] = arg2;
  389.       argvec[0] = arg1;
  390.     }
  391.  
  392.       if (noside == EVAL_SKIP)
  393.     goto nosideret;
  394.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  395.     {
  396.       /* If the return type doesn't look like a function type, call an
  397.          error.  This can happen if somebody tries to turn a variable into
  398.          a function call. This is here because people often want to
  399.          call, eg, strcmp, which gdb doesn't know is a function.  If
  400.          gdb isn't asked for it's opinion (ie. through "whatis"),
  401.          it won't offer it. */
  402.  
  403.       struct type *ftype =
  404.         TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
  405.  
  406.       if (ftype)
  407.         return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
  408.       else
  409.         error ("Expression of type other than \"Function returning ...\" used as function");
  410.     }
  411.       return target_call_function (argvec[0], nargs, argvec + 1);
  412.  
  413.     case STRUCTOP_STRUCT:
  414.       tem = strlen (&exp->elts[pc + 1].string);
  415.       (*pos) += 2 + ((tem + sizeof (union exp_element))
  416.              / sizeof (union exp_element));
  417.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  418.       if (noside == EVAL_SKIP)
  419.     goto nosideret;
  420.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  421.     return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
  422.                            &exp->elts[pc + 1].string,
  423.                            1),
  424.                lval_memory);
  425.       else
  426.     {
  427.       value temp = arg1;
  428.       return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 1].string,
  429.                    (int *) 0, "structure");
  430.     }
  431.  
  432.     case STRUCTOP_PTR:
  433.       tem = strlen (&exp->elts[pc + 1].string);
  434.       (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
  435.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  436.       if (noside == EVAL_SKIP)
  437.     goto nosideret;
  438.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  439.     return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
  440.                            (VALUE_TYPE (arg1)),
  441.                            &exp->elts[pc + 1].string,
  442.                            1),
  443.                lval_memory);
  444.       else
  445.     {
  446.       value temp = arg1;
  447.       return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 1].string,
  448.                    (int *) 0, "structure pointer");
  449.     }
  450.  
  451.     case STRUCTOP_MEMBER:
  452.       arg1 = evaluate_subexp_for_address (exp, pos, noside);
  453.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  454.       if (noside == EVAL_SKIP)
  455.     goto nosideret;
  456.       /* Now, convert these values to an address.  */
  457.       if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR
  458.       || ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))
  459.            != TYPE_CODE_MEMBER)
  460.           && (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))
  461.           != TYPE_CODE_METHOD)))
  462.     error ("non-pointer-to-member value used in pointer-to-member construct");
  463.       arg3 = value_from_long (builtin_type_long,
  464.                   value_as_long (arg1) + value_as_long (arg2));
  465.       VALUE_TYPE (arg3) =
  466.     lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))));
  467.       return value_ind (arg3);
  468.  
  469.     case STRUCTOP_MPTR:
  470.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  471.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  472.       if (noside == EVAL_SKIP)
  473.     goto nosideret;
  474.       /* Now, convert these values to an address.  */
  475.       if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR
  476.       || (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_MEMBER
  477.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_METHOD))
  478.     error ("non-pointer-to-member value used in pointer-to-member construct");
  479.       arg3 = value_from_long (builtin_type_long,
  480.                   value_as_long (arg1) + value_as_long (arg2));
  481.       VALUE_TYPE (arg3) =
  482.     lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))));
  483.       return value_ind (arg3);
  484.  
  485.     case BINOP_ASSIGN:
  486.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  487.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  488.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  489.     return arg1;
  490.       if (binop_user_defined_p (op, arg1, arg2))
  491.     return value_x_binop (arg1, arg2, op, OP_NULL);
  492.       else
  493.     return value_assign (arg1, arg2);
  494.  
  495.     case BINOP_ASSIGN_MODIFY:
  496.       (*pos) += 2;
  497.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  498.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  499.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  500.     return arg1;
  501.       op = exp->elts[pc + 1].opcode;
  502.       if (binop_user_defined_p (op, arg1, arg2))
  503.     return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
  504.       else if (op == BINOP_ADD)
  505.     arg2 = value_add (arg1, arg2);
  506.       else if (op == BINOP_SUB)
  507.     arg2 = value_sub (arg1, arg2);
  508.       else
  509.     arg2 = value_binop (arg1, arg2, op);
  510.       return value_assign (arg1, arg2);
  511.  
  512.     case BINOP_ADD:
  513.       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
  514.       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
  515.       if (noside == EVAL_SKIP)
  516.     goto nosideret;
  517.       if (binop_user_defined_p (op, arg1, arg2))
  518.     return value_x_binop (arg1, arg2, op, OP_NULL);
  519.       else
  520.     return value_add (arg1, arg2);
  521.  
  522.     case BINOP_SUB:
  523.       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
  524.       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
  525.       if (noside == EVAL_SKIP)
  526.     goto nosideret;
  527.       if (binop_user_defined_p (op, arg1, arg2))
  528.     return value_x_binop (arg1, arg2, op, OP_NULL);
  529.       else
  530.     return value_sub (arg1, arg2);
  531.  
  532.     case BINOP_MUL:
  533.     case BINOP_DIV:
  534.     case BINOP_REM:
  535.     case BINOP_LSH:
  536.     case BINOP_RSH:
  537.     case BINOP_LOGAND:
  538.     case BINOP_LOGIOR:
  539.     case BINOP_LOGXOR:
  540.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  541.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  542.       if (noside == EVAL_SKIP)
  543.     goto nosideret;
  544.       if (binop_user_defined_p (op, arg1, arg2))
  545.     return value_x_binop (arg1, arg2, op, OP_NULL);
  546.       else
  547.     if (noside == EVAL_AVOID_SIDE_EFFECTS
  548.         && op == BINOP_DIV)
  549.       return value_zero (VALUE_TYPE (arg1), not_lval);
  550.       else
  551.     return value_binop (arg1, arg2, op);
  552.  
  553.     case BINOP_SUBSCRIPT:
  554.       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
  555.       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
  556.       if (noside == EVAL_SKIP)
  557.     goto nosideret;
  558.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  559.     return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
  560.                VALUE_LVAL (arg1));
  561.                
  562.       if (binop_user_defined_p (op, arg1, arg2))
  563.     return value_x_binop (arg1, arg2, op, OP_NULL);
  564.       else
  565.     return value_subscript (arg1, arg2);
  566.       
  567.     case BINOP_AND:
  568.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  569.       if (noside == EVAL_SKIP)
  570.     {
  571.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  572.       goto nosideret;
  573.     }
  574.       
  575.       oldpos = *pos;
  576.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
  577.       *pos = oldpos;
  578.       
  579.       if (binop_user_defined_p (op, arg1, arg2)) 
  580.     {
  581.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  582.       return value_x_binop (arg1, arg2, op, OP_NULL);
  583.     }
  584.       else
  585.     {
  586.       tem = value_zerop (arg1);
  587.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
  588.                   (tem ? EVAL_SKIP : noside));
  589.       return value_from_long (builtin_type_int,
  590.                   (LONGEST) (!tem && !value_zerop (arg2)));
  591.     }
  592.  
  593.     case BINOP_OR:
  594.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  595.       if (noside == EVAL_SKIP)
  596.     {
  597.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  598.       goto nosideret;
  599.     }
  600.       
  601.       oldpos = *pos;
  602.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
  603.       *pos = oldpos;
  604.       
  605.       if (binop_user_defined_p (op, arg1, arg2)) 
  606.     {
  607.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  608.       return value_x_binop (arg1, arg2, op, OP_NULL);
  609.     }
  610.       else
  611.     {
  612.       tem = value_zerop (arg1);
  613.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
  614.                   (!tem ? EVAL_SKIP : noside));
  615.       return value_from_long (builtin_type_int,
  616.                   (LONGEST) (!tem || !value_zerop (arg2)));
  617.     }
  618.  
  619.     case BINOP_EQUAL:
  620.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  621.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  622.       if (noside == EVAL_SKIP)
  623.     goto nosideret;
  624.       if (binop_user_defined_p (op, arg1, arg2))
  625.     {
  626.       return value_x_binop (arg1, arg2, op, OP_NULL);
  627.     }
  628.       else
  629.     {
  630.       tem = value_equal (arg1, arg2);
  631.       return value_from_long (builtin_type_int, (LONGEST) tem);
  632.     }
  633.  
  634.     case BINOP_NOTEQUAL:
  635.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  636.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  637.       if (noside == EVAL_SKIP)
  638.     goto nosideret;
  639.       if (binop_user_defined_p (op, arg1, arg2))
  640.     {
  641.       return value_x_binop (arg1, arg2, op, OP_NULL);
  642.     }
  643.       else
  644.     {
  645.       tem = value_equal (arg1, arg2);
  646.       return value_from_long (builtin_type_int, (LONGEST) ! tem);
  647.     }
  648.  
  649.     case BINOP_LESS:
  650.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  651.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  652.       if (noside == EVAL_SKIP)
  653.     goto nosideret;
  654.       if (binop_user_defined_p (op, arg1, arg2))
  655.     {
  656.       return value_x_binop (arg1, arg2, op, OP_NULL);
  657.     }
  658.       else
  659.     {
  660.       tem = value_less (arg1, arg2);
  661.       return value_from_long (builtin_type_int, (LONGEST) tem);
  662.     }
  663.  
  664.     case BINOP_GTR:
  665.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  666.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  667.       if (noside == EVAL_SKIP)
  668.     goto nosideret;
  669.       if (binop_user_defined_p (op, arg1, arg2))
  670.     {
  671.       return value_x_binop (arg1, arg2, op, OP_NULL);
  672.     }
  673.       else
  674.     {
  675.       tem = value_less (arg2, arg1);
  676.       return value_from_long (builtin_type_int, (LONGEST) tem);
  677.     }
  678.  
  679.     case BINOP_GEQ:
  680.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  681.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  682.       if (noside == EVAL_SKIP)
  683.     goto nosideret;
  684.       if (binop_user_defined_p (op, arg1, arg2))
  685.     {
  686.       return value_x_binop (arg1, arg2, op, OP_NULL);
  687.     }
  688.       else
  689.     {
  690.       tem = value_less (arg1, arg2);
  691.       return value_from_long (builtin_type_int, (LONGEST) ! tem);
  692.     }
  693.  
  694.     case BINOP_LEQ:
  695.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  696.       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
  697.       if (noside == EVAL_SKIP)
  698.     goto nosideret;
  699.       if (binop_user_defined_p (op, arg1, arg2))
  700.     {
  701.       return value_x_binop (arg1, arg2, op, OP_NULL);
  702.     }
  703.       else 
  704.     {
  705.       tem = value_less (arg2, arg1);
  706.       return value_from_long (builtin_type_int, (LONGEST) ! tem);
  707.     }
  708.  
  709.     case BINOP_REPEAT:
  710.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  711.       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  712.       if (noside == EVAL_SKIP)
  713.     goto nosideret;
  714.       if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
  715.     error ("Non-integral right operand for \"@\" operator.");
  716.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  717.     return allocate_repeat_value (VALUE_TYPE (arg1),
  718.                       longest_to_int (value_as_long (arg2)));
  719.       else
  720.     return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
  721.  
  722.     case BINOP_COMMA:
  723.       evaluate_subexp (NULL_TYPE, exp, pos, noside);
  724.       return evaluate_subexp (NULL_TYPE, exp, pos, noside);
  725.  
  726.     case UNOP_NEG:
  727.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  728.       if (noside == EVAL_SKIP)
  729.     goto nosideret;
  730.       if (unop_user_defined_p (op, arg1))
  731.     return value_x_unop (arg1, op);
  732.       else
  733.     return value_neg (arg1);
  734.  
  735.     case UNOP_LOGNOT:
  736.       /* C++: check for and handle destructor names.  */
  737.       op = exp->elts[*pos].opcode;
  738.  
  739.       /* FIXME-tiemann: this is a cop-out.  */
  740.       if (op == OP_SCOPE)
  741.     error ("destructor in eval");
  742.  
  743.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  744.       if (noside == EVAL_SKIP)
  745.     goto nosideret;
  746.       if (unop_user_defined_p (UNOP_LOGNOT, arg1))
  747.     return value_x_unop (arg1, UNOP_LOGNOT);
  748.       else
  749.     return value_lognot (arg1);
  750.  
  751.     case UNOP_ZEROP:
  752.       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  753.       if (noside == EVAL_SKIP)
  754.     goto nosideret;
  755.       if (unop_user_defined_p (op, arg1))
  756.     return value_x_unop (arg1, op);
  757.       else
  758.     return value_from_long (builtin_type_int,
  759.                 (LONGEST) value_zerop (arg1));
  760.  
  761.     case UNOP_IND:
  762.       if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
  763.         expect_type = TYPE_TARGET_TYPE (expect_type);
  764.       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
  765.       if (noside == EVAL_SKIP)
  766.     goto nosideret;
  767.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  768.     {
  769.       if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
  770.           || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
  771.           /* In C you can dereference an array to get the 1st elt.  */
  772.           || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
  773.           )
  774.         return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
  775.                    lval_memory);
  776.       else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
  777.         /* GDB allows dereferencing an int.  */
  778.         return value_zero (builtin_type_int, lval_memory);
  779.       else
  780.         error ("Attempt to take contents of a non-pointer value.");
  781.     }
  782.       return value_ind (arg1);
  783.  
  784.     case UNOP_ADDR:
  785.       /* C++: check for and handle pointer to members.  */
  786.       
  787.       op = exp->elts[*pos].opcode;
  788.  
  789.       if (noside == EVAL_SKIP)
  790.     {
  791.       if (op == OP_SCOPE)
  792.         {
  793.           char *name = &exp->elts[pc+3].string;
  794.           int temm = strlen (name);
  795.           (*pos) += 2 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
  796.         }
  797.       else
  798.         evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
  799.       goto nosideret;
  800.     }
  801.  
  802.       if (op == OP_SCOPE)
  803.     {
  804.       char *name = &exp->elts[pc+3].string;
  805.       int temm = strlen (name);
  806.       struct type *domain = exp->elts[pc+2].type;
  807.       (*pos) += 2 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
  808.       arg1 = value_struct_elt_for_address (domain, expect_type, name);
  809.       if (arg1)
  810.         return arg1;
  811.       error ("no field `%s' in structure", name);
  812.     }
  813.       else
  814.     return evaluate_subexp_for_address (exp, pos, noside);
  815.  
  816.     case UNOP_SIZEOF:
  817.       if (noside == EVAL_SKIP)
  818.     {
  819.       evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
  820.       goto nosideret;
  821.     }
  822.       return evaluate_subexp_for_sizeof (exp, pos);
  823.  
  824.     case UNOP_CAST:
  825.       (*pos) += 2;
  826.       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
  827.       if (noside == EVAL_SKIP)
  828.     goto nosideret;
  829.       return value_cast (exp->elts[pc + 1].type, arg1);
  830.  
  831.     case UNOP_MEMVAL:
  832.       (*pos) += 2;
  833.       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
  834.       if (noside == EVAL_SKIP)
  835.     goto nosideret;
  836.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  837.     return value_zero (exp->elts[pc + 1].type, lval_memory);
  838.       else
  839.     return value_at_lazy (exp->elts[pc + 1].type,
  840.                   value_as_pointer (arg1));
  841.  
  842.     case UNOP_PREINCREMENT:
  843.       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
  844.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  845.     return arg1;
  846.       else if (unop_user_defined_p (op, arg1))
  847.     {
  848.       return value_x_unop (arg1, op);
  849.     }
  850.       else
  851.     {
  852.       arg2 = value_add (arg1, value_from_long (builtin_type_char, 
  853.                            (LONGEST) 1));
  854.       return value_assign (arg1, arg2);
  855.     }
  856.  
  857.     case UNOP_PREDECREMENT:
  858.       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
  859.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  860.     return arg1;
  861.       else if (unop_user_defined_p (op, arg1))
  862.     {
  863.       return value_x_unop (arg1, op);
  864.     }
  865.       else
  866.     {
  867.       arg2 = value_sub (arg1, value_from_long (builtin_type_char, 
  868.                            (LONGEST) 1));
  869.       return value_assign (arg1, arg2);
  870.     }
  871.  
  872.     case UNOP_POSTINCREMENT:
  873.       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
  874.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  875.     return arg1;
  876.       else if (unop_user_defined_p (op, arg1))
  877.     {
  878.       return value_x_unop (arg1, op);
  879.     }
  880.       else
  881.     {
  882.       arg2 = value_add (arg1, value_from_long (builtin_type_char, 
  883.                            (LONGEST) 1));
  884.       value_assign (arg1, arg2);
  885.       return arg1;
  886.     }
  887.  
  888.     case UNOP_POSTDECREMENT:
  889.       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
  890.       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
  891.     return arg1;
  892.       else if (unop_user_defined_p (op, arg1))
  893.     {
  894.       return value_x_unop (arg1, op);
  895.     }
  896.       else
  897.     {
  898.       arg2 = value_sub (arg1, value_from_long (builtin_type_char, 
  899.                            (LONGEST) 1));
  900.       value_assign (arg1, arg2);
  901.       return arg1;
  902.     }
  903.     
  904.     case OP_THIS:
  905.       (*pos) += 1;
  906.       return value_of_this (1);
  907.  
  908.     default:
  909.       error ("internal error: I do not know how to evaluate what you gave me");
  910.     }
  911.  
  912.  nosideret:
  913.   return value_from_long (builtin_type_long, (LONGEST) 1);
  914. }
  915.  
  916. /* Evaluate a subexpression of EXP, at index *POS,
  917.    and return the address of that subexpression.
  918.    Advance *POS over the subexpression.
  919.    If the subexpression isn't an lvalue, get an error.
  920.    NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
  921.    then only the type of the result need be correct.  */
  922.  
  923. static value
  924. evaluate_subexp_for_address (exp, pos, noside)
  925.      register struct expression *exp;
  926.      register int *pos;
  927.      enum noside noside;
  928. {
  929.   enum exp_opcode op;
  930.   register int pc;
  931.  
  932.   pc = (*pos);
  933.   op = exp->elts[pc].opcode;
  934.  
  935.   switch (op)
  936.     {
  937.     case UNOP_IND:
  938.       (*pos)++;
  939.       return evaluate_subexp (NULL_TYPE, exp, pos, noside);
  940.  
  941.     case UNOP_MEMVAL:
  942.       (*pos) += 3;
  943.       return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
  944.              evaluate_subexp (NULL_TYPE, exp, pos, noside));
  945.  
  946.     case OP_VAR_VALUE:
  947.       (*pos) += 3;
  948.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  949.     {
  950.       struct type *type =
  951.         lookup_pointer_type (SYMBOL_TYPE (exp->elts[pc + 1].symbol));
  952.       enum address_class sym_class =
  953.         SYMBOL_CLASS (exp->elts[pc + 1].symbol);
  954.  
  955.       if (sym_class == LOC_CONST
  956.           || sym_class == LOC_CONST_BYTES
  957.           || sym_class == LOC_REGISTER
  958.           || sym_class == LOC_REGPARM)
  959.         error ("Attempt to take address of register or constant.");
  960.  
  961.     return
  962.       value_zero (type, not_lval);
  963.     }
  964.       else
  965.     return locate_var_value (exp->elts[pc + 1].symbol, (FRAME) 0);
  966.  
  967.     default:
  968.       if (noside == EVAL_AVOID_SIDE_EFFECTS)
  969.     {
  970.       value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
  971.       if (VALUE_LVAL (x) == lval_memory)
  972.         return value_zero (TYPE_POINTER_TYPE (VALUE_TYPE (x)),
  973.                    not_lval);
  974.       else
  975.         error ("Attempt to take address of non-lval");
  976.     }
  977.       return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
  978.     }
  979. }
  980.  
  981. /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
  982.    When used in contexts where arrays will be coerced anyway,
  983.    this is equivalent to `evaluate_subexp'
  984.    but much faster because it avoids actually fetching array contents.  */
  985.  
  986. static value
  987. evaluate_subexp_with_coercion (exp, pos, noside)
  988.      register struct expression *exp;
  989.      register int *pos;
  990.      enum noside noside;
  991. {
  992.   register enum exp_opcode op;
  993.   register int pc;
  994.   register value val;
  995.  
  996.   pc = (*pos);
  997.   op = exp->elts[pc].opcode;
  998.  
  999.   switch (op)
  1000.     {
  1001.     case OP_VAR_VALUE:
  1002.       if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
  1003.     {
  1004.       (*pos) += 3;
  1005.       val = locate_var_value (exp->elts[pc + 1].symbol, (FRAME) 0);
  1006.       return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp->elts[pc + 1].symbol))),
  1007.                  val);
  1008.     }
  1009.       default:
  1010.     return evaluate_subexp (NULL_TYPE, exp, pos, noside);
  1011.     }
  1012. }
  1013.  
  1014. /* Evaluate a subexpression of EXP, at index *POS,
  1015.    and return a value for the size of that subexpression.
  1016.    Advance *POS over the subexpression.  */
  1017.  
  1018. static value
  1019. evaluate_subexp_for_sizeof (exp, pos)
  1020.      register struct expression *exp;
  1021.      register int *pos;
  1022. {
  1023.   enum exp_opcode op;
  1024.   register int pc;
  1025.   value val;
  1026.  
  1027.   pc = (*pos);
  1028.   op = exp->elts[pc].opcode;
  1029.  
  1030.   switch (op)
  1031.     {
  1032.       /* This case is handled specially
  1033.      so that we avoid creating a value for the result type.
  1034.      If the result type is very big, it's desirable not to
  1035.      create a value unnecessarily.  */
  1036.     case UNOP_IND:
  1037.       (*pos)++;
  1038.       val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
  1039.       return value_from_long (builtin_type_int, (LONGEST)
  1040.               TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
  1041.  
  1042.     case UNOP_MEMVAL:
  1043.       (*pos) += 3;
  1044.       return value_from_long (builtin_type_int, 
  1045.                   (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
  1046.  
  1047.     case OP_VAR_VALUE:
  1048.       (*pos) += 3;
  1049.       return value_from_long (builtin_type_int,
  1050.      (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
  1051.  
  1052.     default:
  1053.       val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
  1054.       return value_from_long (builtin_type_int,
  1055.                   (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
  1056.     }
  1057. }
  1058.