Home | History | Annotate | Line # | Download | only in gdb
ax-gdb.c revision 1.6
      1  1.1  christos /* GDB-specific functions for operating on agent expressions.
      2  1.1  christos 
      3  1.6  christos    Copyright (C) 1998-2016 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "symtab.h"
     22  1.1  christos #include "symfile.h"
     23  1.1  christos #include "gdbtypes.h"
     24  1.1  christos #include "language.h"
     25  1.1  christos #include "value.h"
     26  1.1  christos #include "expression.h"
     27  1.1  christos #include "command.h"
     28  1.1  christos #include "gdbcmd.h"
     29  1.1  christos #include "frame.h"
     30  1.1  christos #include "target.h"
     31  1.1  christos #include "ax.h"
     32  1.1  christos #include "ax-gdb.h"
     33  1.1  christos #include "block.h"
     34  1.1  christos #include "regcache.h"
     35  1.1  christos #include "user-regs.h"
     36  1.1  christos #include "dictionary.h"
     37  1.1  christos #include "breakpoint.h"
     38  1.1  christos #include "tracepoint.h"
     39  1.1  christos #include "cp-support.h"
     40  1.1  christos #include "arch-utils.h"
     41  1.1  christos #include "cli/cli-utils.h"
     42  1.1  christos #include "linespec.h"
     43  1.6  christos #include "location.h"
     44  1.3  christos #include "objfiles.h"
     45  1.1  christos 
     46  1.1  christos #include "valprint.h"
     47  1.1  christos #include "c-lang.h"
     48  1.1  christos 
     49  1.1  christos #include "format.h"
     50  1.1  christos 
     51  1.1  christos /* To make sense of this file, you should read doc/agentexpr.texi.
     52  1.1  christos    Then look at the types and enums in ax-gdb.h.  For the code itself,
     53  1.1  christos    look at gen_expr, towards the bottom; that's the main function that
     54  1.1  christos    looks at the GDB expressions and calls everything else to generate
     55  1.1  christos    code.
     56  1.1  christos 
     57  1.1  christos    I'm beginning to wonder whether it wouldn't be nicer to internally
     58  1.1  christos    generate trees, with types, and then spit out the bytecode in
     59  1.1  christos    linear form afterwards; we could generate fewer `swap', `ext', and
     60  1.1  christos    `zero_ext' bytecodes that way; it would make good constant folding
     61  1.1  christos    easier, too.  But at the moment, I think we should be willing to
     62  1.1  christos    pay for the simplicity of this code with less-than-optimal bytecode
     63  1.1  christos    strings.
     64  1.1  christos 
     65  1.1  christos    Remember, "GBD" stands for "Great Britain, Dammit!"  So be careful.  */
     66  1.1  christos 
     67  1.1  christos 
     69  1.1  christos 
     70  1.1  christos /* Prototypes for local functions.  */
     71  1.1  christos 
     72  1.1  christos /* There's a standard order to the arguments of these functions:
     73  1.1  christos    union exp_element ** --- pointer into expression
     74  1.1  christos    struct agent_expr * --- agent expression buffer to generate code into
     75  1.1  christos    struct axs_value * --- describes value left on top of stack  */
     76  1.1  christos 
     77  1.1  christos static struct value *const_var_ref (struct symbol *var);
     78  1.1  christos static struct value *const_expr (union exp_element **pc);
     79  1.1  christos static struct value *maybe_const_expr (union exp_element **pc);
     80  1.1  christos 
     81  1.1  christos static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
     82  1.1  christos 			    struct axs_value *);
     83  1.1  christos 
     84  1.1  christos static void gen_sign_extend (struct agent_expr *, struct type *);
     85  1.1  christos static void gen_extend (struct agent_expr *, struct type *);
     86  1.1  christos static void gen_fetch (struct agent_expr *, struct type *);
     87  1.1  christos static void gen_left_shift (struct agent_expr *, int);
     88  1.1  christos 
     89  1.1  christos 
     90  1.1  christos static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
     91  1.1  christos static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
     92  1.1  christos static void gen_offset (struct agent_expr *ax, int offset);
     93  1.1  christos static void gen_sym_offset (struct agent_expr *, struct symbol *);
     94  1.1  christos static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
     95  1.1  christos 			 struct axs_value *value, struct symbol *var);
     96  1.1  christos 
     97  1.1  christos 
     98  1.1  christos static void gen_int_literal (struct agent_expr *ax,
     99  1.1  christos 			     struct axs_value *value,
    100  1.1  christos 			     LONGEST k, struct type *type);
    101  1.1  christos 
    102  1.1  christos static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
    103  1.1  christos 			     struct axs_value *value);
    104  1.1  christos static int type_wider_than (struct type *type1, struct type *type2);
    105  1.1  christos static struct type *max_type (struct type *type1, struct type *type2);
    106  1.1  christos static void gen_conversion (struct agent_expr *ax,
    107  1.1  christos 			    struct type *from, struct type *to);
    108  1.1  christos static int is_nontrivial_conversion (struct type *from, struct type *to);
    109  1.1  christos static void gen_usual_arithmetic (struct expression *exp,
    110  1.1  christos 				  struct agent_expr *ax,
    111  1.1  christos 				  struct axs_value *value1,
    112  1.1  christos 				  struct axs_value *value2);
    113  1.1  christos static void gen_integral_promotions (struct expression *exp,
    114  1.1  christos 				     struct agent_expr *ax,
    115  1.1  christos 				     struct axs_value *value);
    116  1.1  christos static void gen_cast (struct agent_expr *ax,
    117  1.1  christos 		      struct axs_value *value, struct type *type);
    118  1.1  christos static void gen_scale (struct agent_expr *ax,
    119  1.1  christos 		       enum agent_op op, struct type *type);
    120  1.1  christos static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
    121  1.1  christos 			struct axs_value *value1, struct axs_value *value2);
    122  1.1  christos static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
    123  1.1  christos 			struct axs_value *value1, struct axs_value *value2);
    124  1.1  christos static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
    125  1.1  christos 			 struct axs_value *value1, struct axs_value *value2,
    126  1.1  christos 			 struct type *result_type);
    127  1.1  christos static void gen_binop (struct agent_expr *ax,
    128  1.1  christos 		       struct axs_value *value,
    129  1.1  christos 		       struct axs_value *value1,
    130  1.1  christos 		       struct axs_value *value2,
    131  1.1  christos 		       enum agent_op op,
    132  1.1  christos 		       enum agent_op op_unsigned, int may_carry, char *name);
    133  1.1  christos static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
    134  1.1  christos 			     struct type *result_type);
    135  1.1  christos static void gen_complement (struct agent_expr *ax, struct axs_value *value);
    136  1.1  christos static void gen_deref (struct agent_expr *, struct axs_value *);
    137  1.1  christos static void gen_address_of (struct agent_expr *, struct axs_value *);
    138  1.1  christos static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
    139  1.1  christos 			      struct axs_value *value,
    140  1.1  christos 			      struct type *type, int start, int end);
    141  1.1  christos static void gen_primitive_field (struct expression *exp,
    142  1.1  christos 				 struct agent_expr *ax,
    143  1.1  christos 				 struct axs_value *value,
    144  1.1  christos 				 int offset, int fieldno, struct type *type);
    145  1.1  christos static int gen_struct_ref_recursive (struct expression *exp,
    146  1.1  christos 				     struct agent_expr *ax,
    147  1.1  christos 				     struct axs_value *value,
    148  1.1  christos 				     char *field, int offset,
    149  1.1  christos 				     struct type *type);
    150  1.1  christos static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
    151  1.1  christos 			    struct axs_value *value,
    152  1.1  christos 			    char *field,
    153  1.1  christos 			    char *operator_name, char *operand_name);
    154  1.1  christos static void gen_static_field (struct gdbarch *gdbarch,
    155  1.1  christos 			      struct agent_expr *ax, struct axs_value *value,
    156  1.1  christos 			      struct type *type, int fieldno);
    157  1.1  christos static void gen_repeat (struct expression *exp, union exp_element **pc,
    158  1.1  christos 			struct agent_expr *ax, struct axs_value *value);
    159  1.1  christos static void gen_sizeof (struct expression *exp, union exp_element **pc,
    160  1.1  christos 			struct agent_expr *ax, struct axs_value *value,
    161  1.1  christos 			struct type *size_type);
    162  1.1  christos static void gen_expr_binop_rest (struct expression *exp,
    163  1.1  christos 				 enum exp_opcode op, union exp_element **pc,
    164  1.1  christos 				 struct agent_expr *ax,
    165  1.1  christos 				 struct axs_value *value,
    166  1.1  christos 				 struct axs_value *value1,
    167  1.1  christos 				 struct axs_value *value2);
    168  1.1  christos 
    169  1.1  christos static void agent_command (char *exp, int from_tty);
    170  1.1  christos 
    171  1.1  christos 
    173  1.1  christos /* Detecting constant expressions.  */
    174  1.1  christos 
    175  1.1  christos /* If the variable reference at *PC is a constant, return its value.
    176  1.1  christos    Otherwise, return zero.
    177  1.1  christos 
    178  1.1  christos    Hey, Wally!  How can a variable reference be a constant?
    179  1.1  christos 
    180  1.1  christos    Well, Beav, this function really handles the OP_VAR_VALUE operator,
    181  1.1  christos    not specifically variable references.  GDB uses OP_VAR_VALUE to
    182  1.1  christos    refer to any kind of symbolic reference: function names, enum
    183  1.1  christos    elements, and goto labels are all handled through the OP_VAR_VALUE
    184  1.1  christos    operator, even though they're constants.  It makes sense given the
    185  1.1  christos    situation.
    186  1.1  christos 
    187  1.1  christos    Gee, Wally, don'cha wonder sometimes if data representations that
    188  1.1  christos    subvert commonly accepted definitions of terms in favor of heavily
    189  1.1  christos    context-specific interpretations are really just a tool of the
    190  1.1  christos    programming hegemony to preserve their power and exclude the
    191  1.1  christos    proletariat?  */
    192  1.1  christos 
    193  1.1  christos static struct value *
    194  1.1  christos const_var_ref (struct symbol *var)
    195  1.1  christos {
    196  1.1  christos   struct type *type = SYMBOL_TYPE (var);
    197  1.1  christos 
    198  1.1  christos   switch (SYMBOL_CLASS (var))
    199  1.1  christos     {
    200  1.1  christos     case LOC_CONST:
    201  1.1  christos       return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));
    202  1.1  christos 
    203  1.1  christos     case LOC_LABEL:
    204  1.1  christos       return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));
    205  1.1  christos 
    206  1.1  christos     default:
    207  1.1  christos       return 0;
    208  1.1  christos     }
    209  1.1  christos }
    210  1.1  christos 
    211  1.1  christos 
    212  1.1  christos /* If the expression starting at *PC has a constant value, return it.
    213  1.1  christos    Otherwise, return zero.  If we return a value, then *PC will be
    214  1.1  christos    advanced to the end of it.  If we return zero, *PC could be
    215  1.1  christos    anywhere.  */
    216  1.1  christos static struct value *
    217  1.1  christos const_expr (union exp_element **pc)
    218  1.1  christos {
    219  1.1  christos   enum exp_opcode op = (*pc)->opcode;
    220  1.1  christos   struct value *v1;
    221  1.1  christos 
    222  1.1  christos   switch (op)
    223  1.1  christos     {
    224  1.1  christos     case OP_LONG:
    225  1.1  christos       {
    226  1.1  christos 	struct type *type = (*pc)[1].type;
    227  1.1  christos 	LONGEST k = (*pc)[2].longconst;
    228  1.1  christos 
    229  1.1  christos 	(*pc) += 4;
    230  1.1  christos 	return value_from_longest (type, k);
    231  1.1  christos       }
    232  1.1  christos 
    233  1.1  christos     case OP_VAR_VALUE:
    234  1.1  christos       {
    235  1.1  christos 	struct value *v = const_var_ref ((*pc)[2].symbol);
    236  1.1  christos 
    237  1.1  christos 	(*pc) += 4;
    238  1.1  christos 	return v;
    239  1.1  christos       }
    240  1.1  christos 
    241  1.1  christos       /* We could add more operators in here.  */
    242  1.1  christos 
    243  1.1  christos     case UNOP_NEG:
    244  1.1  christos       (*pc)++;
    245  1.1  christos       v1 = const_expr (pc);
    246  1.1  christos       if (v1)
    247  1.1  christos 	return value_neg (v1);
    248  1.1  christos       else
    249  1.1  christos 	return 0;
    250  1.1  christos 
    251  1.1  christos     default:
    252  1.1  christos       return 0;
    253  1.1  christos     }
    254  1.1  christos }
    255  1.1  christos 
    256  1.1  christos 
    257  1.1  christos /* Like const_expr, but guarantee also that *PC is undisturbed if the
    258  1.1  christos    expression is not constant.  */
    259  1.1  christos static struct value *
    260  1.1  christos maybe_const_expr (union exp_element **pc)
    261  1.1  christos {
    262  1.1  christos   union exp_element *tentative_pc = *pc;
    263  1.1  christos   struct value *v = const_expr (&tentative_pc);
    264  1.1  christos 
    265  1.1  christos   /* If we got a value, then update the real PC.  */
    266  1.1  christos   if (v)
    267  1.1  christos     *pc = tentative_pc;
    268  1.1  christos 
    269  1.1  christos   return v;
    270  1.1  christos }
    271  1.1  christos 
    272  1.1  christos 
    274  1.1  christos /* Generating bytecode from GDB expressions: general assumptions */
    275  1.1  christos 
    276  1.1  christos /* Here are a few general assumptions made throughout the code; if you
    277  1.1  christos    want to make a change that contradicts one of these, then you'd
    278  1.1  christos    better scan things pretty thoroughly.
    279  1.1  christos 
    280  1.1  christos    - We assume that all values occupy one stack element.  For example,
    281  1.1  christos    sometimes we'll swap to get at the left argument to a binary
    282  1.1  christos    operator.  If we decide that void values should occupy no stack
    283  1.1  christos    elements, or that synthetic arrays (whose size is determined at
    284  1.1  christos    run time, created by the `@' operator) should occupy two stack
    285  1.1  christos    elements (address and length), then this will cause trouble.
    286  1.1  christos 
    287  1.1  christos    - We assume the stack elements are infinitely wide, and that we
    288  1.1  christos    don't have to worry what happens if the user requests an
    289  1.1  christos    operation that is wider than the actual interpreter's stack.
    290  1.1  christos    That is, it's up to the interpreter to handle directly all the
    291  1.1  christos    integer widths the user has access to.  (Woe betide the language
    292  1.1  christos    with bignums!)
    293  1.1  christos 
    294  1.1  christos    - We don't support side effects.  Thus, we don't have to worry about
    295  1.1  christos    GCC's generalized lvalues, function calls, etc.
    296  1.1  christos 
    297  1.1  christos    - We don't support floating point.  Many places where we switch on
    298  1.1  christos    some type don't bother to include cases for floating point; there
    299  1.1  christos    may be even more subtle ways this assumption exists.  For
    300  1.1  christos    example, the arguments to % must be integers.
    301  1.1  christos 
    302  1.1  christos    - We assume all subexpressions have a static, unchanging type.  If
    303  1.1  christos    we tried to support convenience variables, this would be a
    304  1.1  christos    problem.
    305  1.1  christos 
    306  1.1  christos    - All values on the stack should always be fully zero- or
    307  1.1  christos    sign-extended.
    308  1.1  christos 
    309  1.1  christos    (I wasn't sure whether to choose this or its opposite --- that
    310  1.1  christos    only addresses are assumed extended --- but it turns out that
    311  1.1  christos    neither convention completely eliminates spurious extend
    312  1.1  christos    operations (if everything is always extended, then you have to
    313  1.1  christos    extend after add, because it could overflow; if nothing is
    314  1.1  christos    extended, then you end up producing extends whenever you change
    315  1.1  christos    sizes), and this is simpler.)  */
    316  1.1  christos 
    317  1.1  christos 
    319  1.1  christos /* Scan for all static fields in the given class, including any base
    320  1.1  christos    classes, and generate tracing bytecodes for each.  */
    321  1.1  christos 
    322  1.1  christos static void
    323  1.1  christos gen_trace_static_fields (struct gdbarch *gdbarch,
    324  1.1  christos 			 struct agent_expr *ax,
    325  1.1  christos 			 struct type *type)
    326  1.6  christos {
    327  1.1  christos   int i, nbases = TYPE_N_BASECLASSES (type);
    328  1.1  christos   struct axs_value value;
    329  1.1  christos 
    330  1.1  christos   type = check_typedef (type);
    331  1.1  christos 
    332  1.1  christos   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
    333  1.1  christos     {
    334  1.1  christos       if (field_is_static (&TYPE_FIELD (type, i)))
    335  1.1  christos 	{
    336  1.1  christos 	  gen_static_field (gdbarch, ax, &value, type, i);
    337  1.1  christos 	  if (value.optimized_out)
    338  1.1  christos 	    continue;
    339  1.1  christos 	  switch (value.kind)
    340  1.1  christos 	    {
    341  1.1  christos 	    case axs_lvalue_memory:
    342  1.1  christos 	      {
    343  1.1  christos 	        /* Initialize the TYPE_LENGTH if it is a typedef.  */
    344  1.1  christos 	        check_typedef (value.type);
    345  1.1  christos 		ax_const_l (ax, TYPE_LENGTH (value.type));
    346  1.1  christos 		ax_simple (ax, aop_trace);
    347  1.1  christos 	      }
    348  1.1  christos 	      break;
    349  1.1  christos 
    350  1.1  christos 	    case axs_lvalue_register:
    351  1.1  christos 	      /* We don't actually need the register's value to be pushed,
    352  1.1  christos 		 just note that we need it to be collected.  */
    353  1.1  christos 	      ax_reg_mask (ax, value.u.reg);
    354  1.1  christos 
    355  1.1  christos 	    default:
    356  1.1  christos 	      break;
    357  1.1  christos 	    }
    358  1.1  christos 	}
    359  1.1  christos     }
    360  1.1  christos 
    361  1.1  christos   /* Now scan through base classes recursively.  */
    362  1.1  christos   for (i = 0; i < nbases; i++)
    363  1.1  christos     {
    364  1.1  christos       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
    365  1.1  christos 
    366  1.1  christos       gen_trace_static_fields (gdbarch, ax, basetype);
    367  1.1  christos     }
    368  1.1  christos }
    369  1.1  christos 
    370  1.1  christos /* Trace the lvalue on the stack, if it needs it.  In either case, pop
    371  1.1  christos    the value.  Useful on the left side of a comma, and at the end of
    372  1.1  christos    an expression being used for tracing.  */
    373  1.1  christos static void
    374  1.1  christos gen_traced_pop (struct gdbarch *gdbarch,
    375  1.1  christos 		struct agent_expr *ax, struct axs_value *value)
    376  1.1  christos {
    377  1.1  christos   int string_trace = 0;
    378  1.1  christos   if (ax->trace_string
    379  1.1  christos       && TYPE_CODE (value->type) == TYPE_CODE_PTR
    380  1.1  christos       && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
    381  1.1  christos 				 's'))
    382  1.1  christos     string_trace = 1;
    383  1.1  christos 
    384  1.1  christos   if (ax->tracing)
    385  1.1  christos     switch (value->kind)
    386  1.1  christos       {
    387  1.1  christos       case axs_rvalue:
    388  1.1  christos 	if (string_trace)
    389  1.1  christos 	  {
    390  1.1  christos 	    ax_const_l (ax, ax->trace_string);
    391  1.1  christos 	    ax_simple (ax, aop_tracenz);
    392  1.1  christos 	  }
    393  1.1  christos 	else
    394  1.1  christos 	  /* We don't trace rvalues, just the lvalues necessary to
    395  1.1  christos 	     produce them.  So just dispose of this value.  */
    396  1.1  christos 	  ax_simple (ax, aop_pop);
    397  1.1  christos 	break;
    398  1.1  christos 
    399  1.1  christos       case axs_lvalue_memory:
    400  1.1  christos 	{
    401  1.1  christos 	  /* Initialize the TYPE_LENGTH if it is a typedef.  */
    402  1.6  christos 	  check_typedef (value->type);
    403  1.1  christos 
    404  1.1  christos 	  if (string_trace)
    405  1.1  christos 	    {
    406  1.6  christos 	      gen_fetch (ax, value->type);
    407  1.6  christos 	      ax_const_l (ax, ax->trace_string);
    408  1.6  christos 	      ax_simple (ax, aop_tracenz);
    409  1.6  christos 	    }
    410  1.6  christos 	  else
    411  1.6  christos 	    {
    412  1.6  christos 	      /* There's no point in trying to use a trace_quick bytecode
    413  1.6  christos 	         here, since "trace_quick SIZE pop" is three bytes, whereas
    414  1.6  christos 	         "const8 SIZE trace" is also three bytes, does the same
    415  1.6  christos 	         thing, and the simplest code which generates that will also
    416  1.1  christos 	         work correctly for objects with large sizes.  */
    417  1.1  christos 	      ax_const_l (ax, TYPE_LENGTH (value->type));
    418  1.1  christos 	      ax_simple (ax, aop_trace);
    419  1.1  christos 	    }
    420  1.1  christos 	}
    421  1.1  christos 	break;
    422  1.1  christos 
    423  1.1  christos       case axs_lvalue_register:
    424  1.1  christos 	/* We don't actually need the register's value to be on the
    425  1.1  christos 	   stack, and the target will get heartburn if the register is
    426  1.1  christos 	   larger than will fit in a stack, so just mark it for
    427  1.1  christos 	   collection and be done with it.  */
    428  1.1  christos 	ax_reg_mask (ax, value->u.reg);
    429  1.1  christos 
    430  1.1  christos 	/* But if the register points to a string, assume the value
    431  1.1  christos 	   will fit on the stack and push it anyway.  */
    432  1.1  christos 	if (string_trace)
    433  1.1  christos 	  {
    434  1.1  christos 	    ax_reg (ax, value->u.reg);
    435  1.1  christos 	    ax_const_l (ax, ax->trace_string);
    436  1.1  christos 	    ax_simple (ax, aop_tracenz);
    437  1.1  christos 	  }
    438  1.1  christos 	break;
    439  1.1  christos       }
    440  1.1  christos   else
    441  1.1  christos     /* If we're not tracing, just pop the value.  */
    442  1.1  christos     ax_simple (ax, aop_pop);
    443  1.1  christos 
    444  1.1  christos   /* To trace C++ classes with static fields stored elsewhere.  */
    445  1.1  christos   if (ax->tracing
    446  1.1  christos       && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
    447  1.1  christos 	  || TYPE_CODE (value->type) == TYPE_CODE_UNION))
    448  1.1  christos     gen_trace_static_fields (gdbarch, ax, value->type);
    449  1.1  christos }
    450  1.1  christos 
    451  1.1  christos 
    453  1.1  christos 
    454  1.1  christos /* Generating bytecode from GDB expressions: helper functions */
    455  1.1  christos 
    456  1.1  christos /* Assume that the lower bits of the top of the stack is a value of
    457  1.1  christos    type TYPE, and the upper bits are zero.  Sign-extend if necessary.  */
    458  1.1  christos static void
    459  1.1  christos gen_sign_extend (struct agent_expr *ax, struct type *type)
    460  1.1  christos {
    461  1.1  christos   /* Do we need to sign-extend this?  */
    462  1.1  christos   if (!TYPE_UNSIGNED (type))
    463  1.1  christos     ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
    464  1.1  christos }
    465  1.1  christos 
    466  1.1  christos 
    467  1.1  christos /* Assume the lower bits of the top of the stack hold a value of type
    468  1.1  christos    TYPE, and the upper bits are garbage.  Sign-extend or truncate as
    469  1.1  christos    needed.  */
    470  1.1  christos static void
    471  1.1  christos gen_extend (struct agent_expr *ax, struct type *type)
    472  1.1  christos {
    473  1.1  christos   int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
    474  1.1  christos 
    475  1.1  christos   /* I just had to.  */
    476  1.1  christos   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
    477  1.1  christos }
    478  1.1  christos 
    479  1.1  christos 
    480  1.1  christos /* Assume that the top of the stack contains a value of type "pointer
    481  1.1  christos    to TYPE"; generate code to fetch its value.  Note that TYPE is the
    482  1.1  christos    target type, not the pointer type.  */
    483  1.1  christos static void
    484  1.1  christos gen_fetch (struct agent_expr *ax, struct type *type)
    485  1.1  christos {
    486  1.1  christos   if (ax->tracing)
    487  1.1  christos     {
    488  1.1  christos       /* Record the area of memory we're about to fetch.  */
    489  1.1  christos       ax_trace_quick (ax, TYPE_LENGTH (type));
    490  1.1  christos     }
    491  1.1  christos 
    492  1.1  christos   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
    493  1.1  christos     type = TYPE_TARGET_TYPE (type);
    494  1.1  christos 
    495  1.1  christos   switch (TYPE_CODE (type))
    496  1.1  christos     {
    497  1.1  christos     case TYPE_CODE_PTR:
    498  1.1  christos     case TYPE_CODE_REF:
    499  1.1  christos     case TYPE_CODE_ENUM:
    500  1.1  christos     case TYPE_CODE_INT:
    501  1.1  christos     case TYPE_CODE_CHAR:
    502  1.1  christos     case TYPE_CODE_BOOL:
    503  1.1  christos       /* It's a scalar value, so we know how to dereference it.  How
    504  1.1  christos          many bytes long is it?  */
    505  1.1  christos       switch (TYPE_LENGTH (type))
    506  1.1  christos 	{
    507  1.1  christos 	case 8 / TARGET_CHAR_BIT:
    508  1.1  christos 	  ax_simple (ax, aop_ref8);
    509  1.1  christos 	  break;
    510  1.1  christos 	case 16 / TARGET_CHAR_BIT:
    511  1.1  christos 	  ax_simple (ax, aop_ref16);
    512  1.1  christos 	  break;
    513  1.1  christos 	case 32 / TARGET_CHAR_BIT:
    514  1.1  christos 	  ax_simple (ax, aop_ref32);
    515  1.1  christos 	  break;
    516  1.1  christos 	case 64 / TARGET_CHAR_BIT:
    517  1.1  christos 	  ax_simple (ax, aop_ref64);
    518  1.1  christos 	  break;
    519  1.1  christos 
    520  1.1  christos 	  /* Either our caller shouldn't have asked us to dereference
    521  1.1  christos 	     that pointer (other code's fault), or we're not
    522  1.1  christos 	     implementing something we should be (this code's fault).
    523  1.1  christos 	     In any case, it's a bug the user shouldn't see.  */
    524  1.1  christos 	default:
    525  1.1  christos 	  internal_error (__FILE__, __LINE__,
    526  1.1  christos 			  _("gen_fetch: strange size"));
    527  1.1  christos 	}
    528  1.1  christos 
    529  1.1  christos       gen_sign_extend (ax, type);
    530  1.1  christos       break;
    531  1.1  christos 
    532  1.1  christos     default:
    533  1.1  christos       /* Our caller requested us to dereference a pointer from an unsupported
    534  1.1  christos 	 type.  Error out and give callers a chance to handle the failure
    535  1.1  christos 	 gracefully.  */
    536  1.1  christos       error (_("gen_fetch: Unsupported type code `%s'."),
    537  1.1  christos 	     TYPE_NAME (type));
    538  1.1  christos     }
    539  1.1  christos }
    540  1.1  christos 
    541  1.1  christos 
    542  1.1  christos /* Generate code to left shift the top of the stack by DISTANCE bits, or
    543  1.1  christos    right shift it by -DISTANCE bits if DISTANCE < 0.  This generates
    544  1.1  christos    unsigned (logical) right shifts.  */
    545  1.1  christos static void
    546  1.1  christos gen_left_shift (struct agent_expr *ax, int distance)
    547  1.1  christos {
    548  1.1  christos   if (distance > 0)
    549  1.1  christos     {
    550  1.1  christos       ax_const_l (ax, distance);
    551  1.1  christos       ax_simple (ax, aop_lsh);
    552  1.1  christos     }
    553  1.1  christos   else if (distance < 0)
    554  1.1  christos     {
    555  1.1  christos       ax_const_l (ax, -distance);
    556  1.1  christos       ax_simple (ax, aop_rsh_unsigned);
    557  1.1  christos     }
    558  1.1  christos }
    559  1.1  christos 
    560  1.1  christos 
    562  1.1  christos 
    563  1.1  christos /* Generating bytecode from GDB expressions: symbol references */
    564  1.1  christos 
    565  1.1  christos /* Generate code to push the base address of the argument portion of
    566  1.1  christos    the top stack frame.  */
    567  1.1  christos static void
    568  1.1  christos gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
    569  1.1  christos {
    570  1.1  christos   int frame_reg;
    571  1.1  christos   LONGEST frame_offset;
    572  1.1  christos 
    573  1.1  christos   gdbarch_virtual_frame_pointer (gdbarch,
    574  1.1  christos 				 ax->scope, &frame_reg, &frame_offset);
    575  1.1  christos   ax_reg (ax, frame_reg);
    576  1.1  christos   gen_offset (ax, frame_offset);
    577  1.1  christos }
    578  1.1  christos 
    579  1.1  christos 
    580  1.1  christos /* Generate code to push the base address of the locals portion of the
    581  1.1  christos    top stack frame.  */
    582  1.1  christos static void
    583  1.1  christos gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
    584  1.1  christos {
    585  1.1  christos   int frame_reg;
    586  1.1  christos   LONGEST frame_offset;
    587  1.1  christos 
    588  1.1  christos   gdbarch_virtual_frame_pointer (gdbarch,
    589  1.1  christos 				 ax->scope, &frame_reg, &frame_offset);
    590  1.1  christos   ax_reg (ax, frame_reg);
    591  1.1  christos   gen_offset (ax, frame_offset);
    592  1.1  christos }
    593  1.1  christos 
    594  1.1  christos 
    595  1.1  christos /* Generate code to add OFFSET to the top of the stack.  Try to
    596  1.1  christos    generate short and readable code.  We use this for getting to
    597  1.1  christos    variables on the stack, and structure members.  If we were
    598  1.1  christos    programming in ML, it would be clearer why these are the same
    599  1.1  christos    thing.  */
    600  1.1  christos static void
    601  1.1  christos gen_offset (struct agent_expr *ax, int offset)
    602  1.1  christos {
    603  1.1  christos   /* It would suffice to simply push the offset and add it, but this
    604  1.1  christos      makes it easier to read positive and negative offsets in the
    605  1.1  christos      bytecode.  */
    606  1.1  christos   if (offset > 0)
    607  1.1  christos     {
    608  1.1  christos       ax_const_l (ax, offset);
    609  1.1  christos       ax_simple (ax, aop_add);
    610  1.1  christos     }
    611  1.1  christos   else if (offset < 0)
    612  1.1  christos     {
    613  1.1  christos       ax_const_l (ax, -offset);
    614  1.1  christos       ax_simple (ax, aop_sub);
    615  1.1  christos     }
    616  1.1  christos }
    617  1.1  christos 
    618  1.1  christos 
    619  1.1  christos /* In many cases, a symbol's value is the offset from some other
    620  1.1  christos    address (stack frame, base register, etc.)  Generate code to add
    621  1.1  christos    VAR's value to the top of the stack.  */
    622  1.1  christos static void
    623  1.1  christos gen_sym_offset (struct agent_expr *ax, struct symbol *var)
    624  1.1  christos {
    625  1.1  christos   gen_offset (ax, SYMBOL_VALUE (var));
    626  1.1  christos }
    627  1.1  christos 
    628  1.1  christos 
    629  1.1  christos /* Generate code for a variable reference to AX.  The variable is the
    630  1.1  christos    symbol VAR.  Set VALUE to describe the result.  */
    631  1.1  christos 
    632  1.1  christos static void
    633  1.1  christos gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
    634  1.1  christos 	     struct axs_value *value, struct symbol *var)
    635  1.1  christos {
    636  1.1  christos   /* Dereference any typedefs.  */
    637  1.1  christos   value->type = check_typedef (SYMBOL_TYPE (var));
    638  1.1  christos   value->optimized_out = 0;
    639  1.1  christos 
    640  1.1  christos   if (SYMBOL_COMPUTED_OPS (var) != NULL)
    641  1.1  christos     {
    642  1.1  christos       SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
    643  1.1  christos       return;
    644  1.1  christos     }
    645  1.1  christos 
    646  1.1  christos   /* I'm imitating the code in read_var_value.  */
    647  1.1  christos   switch (SYMBOL_CLASS (var))
    648  1.1  christos     {
    649  1.1  christos     case LOC_CONST:		/* A constant, like an enum value.  */
    650  1.1  christos       ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
    651  1.1  christos       value->kind = axs_rvalue;
    652  1.1  christos       break;
    653  1.1  christos 
    654  1.1  christos     case LOC_LABEL:		/* A goto label, being used as a value.  */
    655  1.1  christos       ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
    656  1.1  christos       value->kind = axs_rvalue;
    657  1.1  christos       break;
    658  1.1  christos 
    659  1.1  christos     case LOC_CONST_BYTES:
    660  1.1  christos       internal_error (__FILE__, __LINE__,
    661  1.1  christos 		      _("gen_var_ref: LOC_CONST_BYTES "
    662  1.1  christos 			"symbols are not supported"));
    663  1.1  christos 
    664  1.1  christos       /* Variable at a fixed location in memory.  Easy.  */
    665  1.1  christos     case LOC_STATIC:
    666  1.1  christos       /* Push the address of the variable.  */
    667  1.1  christos       ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
    668  1.1  christos       value->kind = axs_lvalue_memory;
    669  1.1  christos       break;
    670  1.1  christos 
    671  1.1  christos     case LOC_ARG:		/* var lives in argument area of frame */
    672  1.1  christos       gen_frame_args_address (gdbarch, ax);
    673  1.1  christos       gen_sym_offset (ax, var);
    674  1.1  christos       value->kind = axs_lvalue_memory;
    675  1.1  christos       break;
    676  1.1  christos 
    677  1.1  christos     case LOC_REF_ARG:		/* As above, but the frame slot really
    678  1.1  christos 				   holds the address of the variable.  */
    679  1.1  christos       gen_frame_args_address (gdbarch, ax);
    680  1.1  christos       gen_sym_offset (ax, var);
    681  1.1  christos       /* Don't assume any particular pointer size.  */
    682  1.1  christos       gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
    683  1.1  christos       value->kind = axs_lvalue_memory;
    684  1.1  christos       break;
    685  1.1  christos 
    686  1.1  christos     case LOC_LOCAL:		/* var lives in locals area of frame */
    687  1.1  christos       gen_frame_locals_address (gdbarch, ax);
    688  1.1  christos       gen_sym_offset (ax, var);
    689  1.1  christos       value->kind = axs_lvalue_memory;
    690  1.1  christos       break;
    691  1.1  christos 
    692  1.1  christos     case LOC_TYPEDEF:
    693  1.1  christos       error (_("Cannot compute value of typedef `%s'."),
    694  1.1  christos 	     SYMBOL_PRINT_NAME (var));
    695  1.1  christos       break;
    696  1.1  christos 
    697  1.1  christos     case LOC_BLOCK:
    698  1.1  christos       ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
    699  1.1  christos       value->kind = axs_rvalue;
    700  1.1  christos       break;
    701  1.1  christos 
    702  1.1  christos     case LOC_REGISTER:
    703  1.1  christos       /* Don't generate any code at all; in the process of treating
    704  1.1  christos          this as an lvalue or rvalue, the caller will generate the
    705  1.1  christos          right code.  */
    706  1.1  christos       value->kind = axs_lvalue_register;
    707  1.1  christos       value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
    708  1.1  christos       break;
    709  1.1  christos 
    710  1.1  christos       /* A lot like LOC_REF_ARG, but the pointer lives directly in a
    711  1.1  christos          register, not on the stack.  Simpler than LOC_REGISTER
    712  1.1  christos          because it's just like any other case where the thing
    713  1.1  christos 	 has a real address.  */
    714  1.1  christos     case LOC_REGPARM_ADDR:
    715  1.3  christos       ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
    716  1.1  christos       value->kind = axs_lvalue_memory;
    717  1.1  christos       break;
    718  1.3  christos 
    719  1.1  christos     case LOC_UNRESOLVED:
    720  1.1  christos       {
    721  1.1  christos 	struct bound_minimal_symbol msym
    722  1.3  christos 	  = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
    723  1.1  christos 
    724  1.1  christos 	if (!msym.minsym)
    725  1.1  christos 	  error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));
    726  1.1  christos 
    727  1.1  christos 	/* Push the address of the variable.  */
    728  1.1  christos 	ax_const_l (ax, BMSYMBOL_VALUE_ADDRESS (msym));
    729  1.1  christos 	value->kind = axs_lvalue_memory;
    730  1.1  christos       }
    731  1.1  christos       break;
    732  1.1  christos 
    733  1.1  christos     case LOC_COMPUTED:
    734  1.1  christos       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
    735  1.1  christos 
    736  1.1  christos     case LOC_OPTIMIZED_OUT:
    737  1.1  christos       /* Flag this, but don't say anything; leave it up to callers to
    738  1.1  christos 	 warn the user.  */
    739  1.1  christos       value->optimized_out = 1;
    740  1.1  christos       break;
    741  1.1  christos 
    742  1.1  christos     default:
    743  1.1  christos       error (_("Cannot find value of botched symbol `%s'."),
    744  1.1  christos 	     SYMBOL_PRINT_NAME (var));
    745  1.1  christos       break;
    746  1.1  christos     }
    747  1.1  christos }
    748  1.1  christos 
    749  1.1  christos 
    751  1.1  christos 
    752  1.1  christos /* Generating bytecode from GDB expressions: literals */
    753  1.1  christos 
    754  1.1  christos static void
    755  1.1  christos gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
    756  1.1  christos 		 struct type *type)
    757  1.1  christos {
    758  1.1  christos   ax_const_l (ax, k);
    759  1.1  christos   value->kind = axs_rvalue;
    760  1.1  christos   value->type = check_typedef (type);
    761  1.1  christos }
    762  1.1  christos 
    763  1.1  christos 
    765  1.1  christos 
    766  1.1  christos /* Generating bytecode from GDB expressions: unary conversions, casts */
    767  1.1  christos 
    768  1.1  christos /* Take what's on the top of the stack (as described by VALUE), and
    769  1.1  christos    try to make an rvalue out of it.  Signal an error if we can't do
    770  1.1  christos    that.  */
    771  1.1  christos void
    772  1.1  christos require_rvalue (struct agent_expr *ax, struct axs_value *value)
    773  1.1  christos {
    774  1.1  christos   /* Only deal with scalars, structs and such may be too large
    775  1.1  christos      to fit in a stack entry.  */
    776  1.1  christos   value->type = check_typedef (value->type);
    777  1.1  christos   if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
    778  1.1  christos       || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
    779  1.1  christos       || TYPE_CODE (value->type) == TYPE_CODE_UNION
    780  1.1  christos       || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
    781  1.1  christos     error (_("Value not scalar: cannot be an rvalue."));
    782  1.1  christos 
    783  1.1  christos   switch (value->kind)
    784  1.1  christos     {
    785  1.1  christos     case axs_rvalue:
    786  1.1  christos       /* It's already an rvalue.  */
    787  1.1  christos       break;
    788  1.1  christos 
    789  1.1  christos     case axs_lvalue_memory:
    790  1.1  christos       /* The top of stack is the address of the object.  Dereference.  */
    791  1.1  christos       gen_fetch (ax, value->type);
    792  1.1  christos       break;
    793  1.1  christos 
    794  1.1  christos     case axs_lvalue_register:
    795  1.1  christos       /* There's nothing on the stack, but value->u.reg is the
    796  1.1  christos          register number containing the value.
    797  1.1  christos 
    798  1.1  christos          When we add floating-point support, this is going to have to
    799  1.1  christos          change.  What about SPARC register pairs, for example?  */
    800  1.1  christos       ax_reg (ax, value->u.reg);
    801  1.1  christos       gen_extend (ax, value->type);
    802  1.1  christos       break;
    803  1.1  christos     }
    804  1.1  christos 
    805  1.1  christos   value->kind = axs_rvalue;
    806  1.1  christos }
    807  1.1  christos 
    808  1.1  christos 
    809  1.1  christos /* Assume the top of the stack is described by VALUE, and perform the
    810  1.1  christos    usual unary conversions.  This is motivated by ANSI 6.2.2, but of
    811  1.1  christos    course GDB expressions are not ANSI; they're the mishmash union of
    812  1.1  christos    a bunch of languages.  Rah.
    813  1.1  christos 
    814  1.1  christos    NOTE!  This function promises to produce an rvalue only when the
    815  1.1  christos    incoming value is of an appropriate type.  In other words, the
    816  1.1  christos    consumer of the value this function produces may assume the value
    817  1.1  christos    is an rvalue only after checking its type.
    818  1.1  christos 
    819  1.1  christos    The immediate issue is that if the user tries to use a structure or
    820  1.1  christos    union as an operand of, say, the `+' operator, we don't want to try
    821  1.1  christos    to convert that structure to an rvalue; require_rvalue will bomb on
    822  1.1  christos    structs and unions.  Rather, we want to simply pass the struct
    823  1.1  christos    lvalue through unchanged, and let `+' raise an error.  */
    824  1.1  christos 
    825  1.1  christos static void
    826  1.1  christos gen_usual_unary (struct expression *exp, struct agent_expr *ax,
    827  1.1  christos 		 struct axs_value *value)
    828  1.1  christos {
    829  1.1  christos   /* We don't have to generate any code for the usual integral
    830  1.1  christos      conversions, since values are always represented as full-width on
    831  1.1  christos      the stack.  Should we tweak the type?  */
    832  1.1  christos 
    833  1.1  christos   /* Some types require special handling.  */
    834  1.1  christos   switch (TYPE_CODE (value->type))
    835  1.1  christos     {
    836  1.1  christos       /* Functions get converted to a pointer to the function.  */
    837  1.1  christos     case TYPE_CODE_FUNC:
    838  1.1  christos       value->type = lookup_pointer_type (value->type);
    839  1.1  christos       value->kind = axs_rvalue;	/* Should always be true, but just in case.  */
    840  1.1  christos       break;
    841  1.1  christos 
    842  1.1  christos       /* Arrays get converted to a pointer to their first element, and
    843  1.1  christos          are no longer an lvalue.  */
    844  1.1  christos     case TYPE_CODE_ARRAY:
    845  1.1  christos       {
    846  1.1  christos 	struct type *elements = TYPE_TARGET_TYPE (value->type);
    847  1.1  christos 
    848  1.1  christos 	value->type = lookup_pointer_type (elements);
    849  1.1  christos 	value->kind = axs_rvalue;
    850  1.1  christos 	/* We don't need to generate any code; the address of the array
    851  1.1  christos 	   is also the address of its first element.  */
    852  1.1  christos       }
    853  1.1  christos       break;
    854  1.1  christos 
    855  1.1  christos       /* Don't try to convert structures and unions to rvalues.  Let the
    856  1.1  christos          consumer signal an error.  */
    857  1.1  christos     case TYPE_CODE_STRUCT:
    858  1.1  christos     case TYPE_CODE_UNION:
    859  1.1  christos       return;
    860  1.1  christos     }
    861  1.1  christos 
    862  1.1  christos   /* If the value is an lvalue, dereference it.  */
    863  1.1  christos   require_rvalue (ax, value);
    864  1.1  christos }
    865  1.1  christos 
    866  1.1  christos 
    867  1.1  christos /* Return non-zero iff the type TYPE1 is considered "wider" than the
    868  1.1  christos    type TYPE2, according to the rules described in gen_usual_arithmetic.  */
    869  1.1  christos static int
    870  1.1  christos type_wider_than (struct type *type1, struct type *type2)
    871  1.1  christos {
    872  1.1  christos   return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
    873  1.1  christos 	  || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
    874  1.1  christos 	      && TYPE_UNSIGNED (type1)
    875  1.1  christos 	      && !TYPE_UNSIGNED (type2)));
    876  1.1  christos }
    877  1.1  christos 
    878  1.1  christos 
    879  1.1  christos /* Return the "wider" of the two types TYPE1 and TYPE2.  */
    880  1.1  christos static struct type *
    881  1.1  christos max_type (struct type *type1, struct type *type2)
    882  1.1  christos {
    883  1.1  christos   return type_wider_than (type1, type2) ? type1 : type2;
    884  1.1  christos }
    885  1.1  christos 
    886  1.1  christos 
    887  1.1  christos /* Generate code to convert a scalar value of type FROM to type TO.  */
    888  1.5  christos static void
    889  1.1  christos gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
    890  1.1  christos {
    891  1.1  christos   /* Perhaps there is a more graceful way to state these rules.  */
    892  1.1  christos 
    893  1.1  christos   /* If we're converting to a narrower type, then we need to clear out
    894  1.1  christos      the upper bits.  */
    895  1.1  christos   if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
    896  1.1  christos     gen_extend (ax, to);
    897  1.1  christos 
    898  1.1  christos   /* If the two values have equal width, but different signednesses,
    899  1.1  christos      then we need to extend.  */
    900  1.1  christos   else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
    901  1.1  christos     {
    902  1.1  christos       if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
    903  1.1  christos 	gen_extend (ax, to);
    904  1.1  christos     }
    905  1.1  christos 
    906  1.1  christos   /* If we're converting to a wider type, and becoming unsigned, then
    907  1.1  christos      we need to zero out any possible sign bits.  */
    908  1.1  christos   else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
    909  1.1  christos     {
    910  1.1  christos       if (TYPE_UNSIGNED (to))
    911  1.1  christos 	gen_extend (ax, to);
    912  1.1  christos     }
    913  1.1  christos }
    914  1.1  christos 
    915  1.1  christos 
    916  1.1  christos /* Return non-zero iff the type FROM will require any bytecodes to be
    917  1.1  christos    emitted to be converted to the type TO.  */
    918  1.1  christos static int
    919  1.1  christos is_nontrivial_conversion (struct type *from, struct type *to)
    920  1.1  christos {
    921  1.1  christos   struct agent_expr *ax = new_agent_expr (NULL, 0);
    922  1.1  christos   int nontrivial;
    923  1.1  christos 
    924  1.1  christos   /* Actually generate the code, and see if anything came out.  At the
    925  1.1  christos      moment, it would be trivial to replicate the code in
    926  1.1  christos      gen_conversion here, but in the future, when we're supporting
    927  1.1  christos      floating point and the like, it may not be.  Doing things this
    928  1.1  christos      way allows this function to be independent of the logic in
    929  1.1  christos      gen_conversion.  */
    930  1.1  christos   gen_conversion (ax, from, to);
    931  1.1  christos   nontrivial = ax->len > 0;
    932  1.1  christos   free_agent_expr (ax);
    933  1.1  christos   return nontrivial;
    934  1.1  christos }
    935  1.1  christos 
    936  1.1  christos 
    937  1.1  christos /* Generate code to perform the "usual arithmetic conversions" (ANSI C
    938  1.1  christos    6.2.1.5) for the two operands of an arithmetic operator.  This
    939  1.1  christos    effectively finds a "least upper bound" type for the two arguments,
    940  1.1  christos    and promotes each argument to that type.  *VALUE1 and *VALUE2
    941  1.1  christos    describe the values as they are passed in, and as they are left.  */
    942  1.1  christos static void
    943  1.1  christos gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
    944  1.1  christos 		      struct axs_value *value1, struct axs_value *value2)
    945  1.1  christos {
    946  1.1  christos   /* Do the usual binary conversions.  */
    947  1.1  christos   if (TYPE_CODE (value1->type) == TYPE_CODE_INT
    948  1.1  christos       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
    949  1.1  christos     {
    950  1.1  christos       /* The ANSI integral promotions seem to work this way: Order the
    951  1.1  christos          integer types by size, and then by signedness: an n-bit
    952  1.1  christos          unsigned type is considered "wider" than an n-bit signed
    953  1.1  christos          type.  Promote to the "wider" of the two types, and always
    954  1.1  christos          promote at least to int.  */
    955  1.1  christos       struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
    956  1.1  christos 				      max_type (value1->type, value2->type));
    957  1.1  christos 
    958  1.1  christos       /* Deal with value2, on the top of the stack.  */
    959  1.1  christos       gen_conversion (ax, value2->type, target);
    960  1.1  christos 
    961  1.1  christos       /* Deal with value1, not on the top of the stack.  Don't
    962  1.1  christos          generate the `swap' instructions if we're not actually going
    963  1.1  christos          to do anything.  */
    964  1.1  christos       if (is_nontrivial_conversion (value1->type, target))
    965  1.1  christos 	{
    966  1.1  christos 	  ax_simple (ax, aop_swap);
    967  1.1  christos 	  gen_conversion (ax, value1->type, target);
    968  1.1  christos 	  ax_simple (ax, aop_swap);
    969  1.1  christos 	}
    970  1.1  christos 
    971  1.1  christos       value1->type = value2->type = check_typedef (target);
    972  1.1  christos     }
    973  1.1  christos }
    974  1.1  christos 
    975  1.1  christos 
    976  1.1  christos /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
    977  1.1  christos    the value on the top of the stack, as described by VALUE.  Assume
    978  1.1  christos    the value has integral type.  */
    979  1.1  christos static void
    980  1.1  christos gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
    981  1.1  christos 			 struct axs_value *value)
    982  1.1  christos {
    983  1.1  christos   const struct builtin_type *builtin = builtin_type (exp->gdbarch);
    984  1.1  christos 
    985  1.1  christos   if (!type_wider_than (value->type, builtin->builtin_int))
    986  1.1  christos     {
    987  1.1  christos       gen_conversion (ax, value->type, builtin->builtin_int);
    988  1.1  christos       value->type = builtin->builtin_int;
    989  1.1  christos     }
    990  1.1  christos   else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
    991  1.1  christos     {
    992  1.1  christos       gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
    993  1.1  christos       value->type = builtin->builtin_unsigned_int;
    994  1.1  christos     }
    995  1.1  christos }
    996  1.1  christos 
    997  1.1  christos 
    998  1.1  christos /* Generate code for a cast to TYPE.  */
    999  1.1  christos static void
   1000  1.1  christos gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
   1001  1.1  christos {
   1002  1.1  christos   /* GCC does allow casts to yield lvalues, so this should be fixed
   1003  1.1  christos      before merging these changes into the trunk.  */
   1004  1.1  christos   require_rvalue (ax, value);
   1005  1.1  christos   /* Dereference typedefs.  */
   1006  1.1  christos   type = check_typedef (type);
   1007  1.1  christos 
   1008  1.1  christos   switch (TYPE_CODE (type))
   1009  1.1  christos     {
   1010  1.1  christos     case TYPE_CODE_PTR:
   1011  1.1  christos     case TYPE_CODE_REF:
   1012  1.1  christos       /* It's implementation-defined, and I'll bet this is what GCC
   1013  1.1  christos          does.  */
   1014  1.1  christos       break;
   1015  1.1  christos 
   1016  1.1  christos     case TYPE_CODE_ARRAY:
   1017  1.1  christos     case TYPE_CODE_STRUCT:
   1018  1.1  christos     case TYPE_CODE_UNION:
   1019  1.1  christos     case TYPE_CODE_FUNC:
   1020  1.1  christos       error (_("Invalid type cast: intended type must be scalar."));
   1021  1.1  christos 
   1022  1.1  christos     case TYPE_CODE_ENUM:
   1023  1.1  christos     case TYPE_CODE_BOOL:
   1024  1.1  christos       /* We don't have to worry about the size of the value, because
   1025  1.1  christos          all our integral values are fully sign-extended, and when
   1026  1.1  christos          casting pointers we can do anything we like.  Is there any
   1027  1.1  christos          way for us to know what GCC actually does with a cast like
   1028  1.1  christos          this?  */
   1029  1.1  christos       break;
   1030  1.1  christos 
   1031  1.1  christos     case TYPE_CODE_INT:
   1032  1.1  christos       gen_conversion (ax, value->type, type);
   1033  1.1  christos       break;
   1034  1.1  christos 
   1035  1.1  christos     case TYPE_CODE_VOID:
   1036  1.1  christos       /* We could pop the value, and rely on everyone else to check
   1037  1.1  christos          the type and notice that this value doesn't occupy a stack
   1038  1.1  christos          slot.  But for now, leave the value on the stack, and
   1039  1.1  christos          preserve the "value == stack element" assumption.  */
   1040  1.1  christos       break;
   1041  1.1  christos 
   1042  1.1  christos     default:
   1043  1.1  christos       error (_("Casts to requested type are not yet implemented."));
   1044  1.1  christos     }
   1045  1.1  christos 
   1046  1.1  christos   value->type = type;
   1047  1.1  christos }
   1048  1.1  christos 
   1049  1.1  christos 
   1051  1.1  christos 
   1052  1.1  christos /* Generating bytecode from GDB expressions: arithmetic */
   1053  1.1  christos 
   1054  1.1  christos /* Scale the integer on the top of the stack by the size of the target
   1055  1.1  christos    of the pointer type TYPE.  */
   1056  1.1  christos static void
   1057  1.1  christos gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
   1058  1.1  christos {
   1059  1.1  christos   struct type *element = TYPE_TARGET_TYPE (type);
   1060  1.1  christos 
   1061  1.1  christos   if (TYPE_LENGTH (element) != 1)
   1062  1.1  christos     {
   1063  1.1  christos       ax_const_l (ax, TYPE_LENGTH (element));
   1064  1.1  christos       ax_simple (ax, op);
   1065  1.1  christos     }
   1066  1.1  christos }
   1067  1.1  christos 
   1068  1.1  christos 
   1069  1.1  christos /* Generate code for pointer arithmetic PTR + INT.  */
   1070  1.1  christos static void
   1071  1.1  christos gen_ptradd (struct agent_expr *ax, struct axs_value *value,
   1072  1.1  christos 	    struct axs_value *value1, struct axs_value *value2)
   1073  1.1  christos {
   1074  1.1  christos   gdb_assert (pointer_type (value1->type));
   1075  1.1  christos   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
   1076  1.1  christos 
   1077  1.1  christos   gen_scale (ax, aop_mul, value1->type);
   1078  1.1  christos   ax_simple (ax, aop_add);
   1079  1.1  christos   gen_extend (ax, value1->type);	/* Catch overflow.  */
   1080  1.1  christos   value->type = value1->type;
   1081  1.1  christos   value->kind = axs_rvalue;
   1082  1.1  christos }
   1083  1.1  christos 
   1084  1.1  christos 
   1085  1.1  christos /* Generate code for pointer arithmetic PTR - INT.  */
   1086  1.1  christos static void
   1087  1.1  christos gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
   1088  1.1  christos 	    struct axs_value *value1, struct axs_value *value2)
   1089  1.1  christos {
   1090  1.1  christos   gdb_assert (pointer_type (value1->type));
   1091  1.1  christos   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);
   1092  1.1  christos 
   1093  1.1  christos   gen_scale (ax, aop_mul, value1->type);
   1094  1.1  christos   ax_simple (ax, aop_sub);
   1095  1.1  christos   gen_extend (ax, value1->type);	/* Catch overflow.  */
   1096  1.1  christos   value->type = value1->type;
   1097  1.1  christos   value->kind = axs_rvalue;
   1098  1.1  christos }
   1099  1.1  christos 
   1100  1.1  christos 
   1101  1.1  christos /* Generate code for pointer arithmetic PTR - PTR.  */
   1102  1.1  christos static void
   1103  1.1  christos gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
   1104  1.1  christos 	     struct axs_value *value1, struct axs_value *value2,
   1105  1.1  christos 	     struct type *result_type)
   1106  1.1  christos {
   1107  1.1  christos   gdb_assert (pointer_type (value1->type));
   1108  1.1  christos   gdb_assert (pointer_type (value2->type));
   1109  1.1  christos 
   1110  1.1  christos   if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
   1111  1.1  christos       != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
   1112  1.1  christos     error (_("\
   1113  1.1  christos First argument of `-' is a pointer, but second argument is neither\n\
   1114  1.1  christos an integer nor a pointer of the same type."));
   1115  1.1  christos 
   1116  1.1  christos   ax_simple (ax, aop_sub);
   1117  1.1  christos   gen_scale (ax, aop_div_unsigned, value1->type);
   1118  1.1  christos   value->type = result_type;
   1119  1.1  christos   value->kind = axs_rvalue;
   1120  1.1  christos }
   1121  1.1  christos 
   1122  1.1  christos static void
   1123  1.1  christos gen_equal (struct agent_expr *ax, struct axs_value *value,
   1124  1.1  christos 	   struct axs_value *value1, struct axs_value *value2,
   1125  1.1  christos 	   struct type *result_type)
   1126  1.1  christos {
   1127  1.1  christos   if (pointer_type (value1->type) || pointer_type (value2->type))
   1128  1.1  christos     ax_simple (ax, aop_equal);
   1129  1.1  christos   else
   1130  1.1  christos     gen_binop (ax, value, value1, value2,
   1131  1.1  christos 	       aop_equal, aop_equal, 0, "equal");
   1132  1.1  christos   value->type = result_type;
   1133  1.1  christos   value->kind = axs_rvalue;
   1134  1.1  christos }
   1135  1.1  christos 
   1136  1.1  christos static void
   1137  1.1  christos gen_less (struct agent_expr *ax, struct axs_value *value,
   1138  1.1  christos 	  struct axs_value *value1, struct axs_value *value2,
   1139  1.1  christos 	  struct type *result_type)
   1140  1.1  christos {
   1141  1.1  christos   if (pointer_type (value1->type) || pointer_type (value2->type))
   1142  1.1  christos     ax_simple (ax, aop_less_unsigned);
   1143  1.1  christos   else
   1144  1.1  christos     gen_binop (ax, value, value1, value2,
   1145  1.1  christos 	       aop_less_signed, aop_less_unsigned, 0, "less than");
   1146  1.1  christos   value->type = result_type;
   1147  1.1  christos   value->kind = axs_rvalue;
   1148  1.1  christos }
   1149  1.1  christos 
   1150  1.1  christos /* Generate code for a binary operator that doesn't do pointer magic.
   1151  1.1  christos    We set VALUE to describe the result value; we assume VALUE1 and
   1152  1.1  christos    VALUE2 describe the two operands, and that they've undergone the
   1153  1.1  christos    usual binary conversions.  MAY_CARRY should be non-zero iff the
   1154  1.1  christos    result needs to be extended.  NAME is the English name of the
   1155  1.1  christos    operator, used in error messages */
   1156  1.1  christos static void
   1157  1.1  christos gen_binop (struct agent_expr *ax, struct axs_value *value,
   1158  1.1  christos 	   struct axs_value *value1, struct axs_value *value2,
   1159  1.1  christos 	   enum agent_op op, enum agent_op op_unsigned,
   1160  1.1  christos 	   int may_carry, char *name)
   1161  1.1  christos {
   1162  1.1  christos   /* We only handle INT op INT.  */
   1163  1.1  christos   if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
   1164  1.1  christos       || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
   1165  1.1  christos     error (_("Invalid combination of types in %s."), name);
   1166  1.1  christos 
   1167  1.1  christos   ax_simple (ax,
   1168  1.1  christos 	     TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
   1169  1.1  christos   if (may_carry)
   1170  1.1  christos     gen_extend (ax, value1->type);	/* catch overflow */
   1171  1.1  christos   value->type = value1->type;
   1172  1.1  christos   value->kind = axs_rvalue;
   1173  1.1  christos }
   1174  1.1  christos 
   1175  1.1  christos 
   1176  1.1  christos static void
   1177  1.1  christos gen_logical_not (struct agent_expr *ax, struct axs_value *value,
   1178  1.1  christos 		 struct type *result_type)
   1179  1.1  christos {
   1180  1.1  christos   if (TYPE_CODE (value->type) != TYPE_CODE_INT
   1181  1.1  christos       && TYPE_CODE (value->type) != TYPE_CODE_PTR)
   1182  1.1  christos     error (_("Invalid type of operand to `!'."));
   1183  1.1  christos 
   1184  1.1  christos   ax_simple (ax, aop_log_not);
   1185  1.1  christos   value->type = result_type;
   1186  1.1  christos }
   1187  1.1  christos 
   1188  1.1  christos 
   1189  1.1  christos static void
   1190  1.1  christos gen_complement (struct agent_expr *ax, struct axs_value *value)
   1191  1.1  christos {
   1192  1.1  christos   if (TYPE_CODE (value->type) != TYPE_CODE_INT)
   1193  1.1  christos     error (_("Invalid type of operand to `~'."));
   1194  1.1  christos 
   1195  1.1  christos   ax_simple (ax, aop_bit_not);
   1196  1.1  christos   gen_extend (ax, value->type);
   1197  1.1  christos }
   1198  1.1  christos 
   1199  1.1  christos 
   1201  1.1  christos 
   1202  1.1  christos /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
   1203  1.1  christos 
   1204  1.1  christos /* Dereference the value on the top of the stack.  */
   1205  1.1  christos static void
   1206  1.1  christos gen_deref (struct agent_expr *ax, struct axs_value *value)
   1207  1.1  christos {
   1208  1.1  christos   /* The caller should check the type, because several operators use
   1209  1.1  christos      this, and we don't know what error message to generate.  */
   1210  1.1  christos   if (!pointer_type (value->type))
   1211  1.1  christos     internal_error (__FILE__, __LINE__,
   1212  1.1  christos 		    _("gen_deref: expected a pointer"));
   1213  1.1  christos 
   1214  1.1  christos   /* We've got an rvalue now, which is a pointer.  We want to yield an
   1215  1.1  christos      lvalue, whose address is exactly that pointer.  So we don't
   1216  1.1  christos      actually emit any code; we just change the type from "Pointer to
   1217  1.1  christos      T" to "T", and mark the value as an lvalue in memory.  Leave it
   1218  1.1  christos      to the consumer to actually dereference it.  */
   1219  1.1  christos   value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
   1220  1.1  christos   if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
   1221  1.1  christos     error (_("Attempt to dereference a generic pointer."));
   1222  1.1  christos   value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
   1223  1.1  christos 		 ? axs_rvalue : axs_lvalue_memory);
   1224  1.1  christos }
   1225  1.1  christos 
   1226  1.1  christos 
   1227  1.1  christos /* Produce the address of the lvalue on the top of the stack.  */
   1228  1.1  christos static void
   1229  1.1  christos gen_address_of (struct agent_expr *ax, struct axs_value *value)
   1230  1.1  christos {
   1231  1.1  christos   /* Special case for taking the address of a function.  The ANSI
   1232  1.1  christos      standard describes this as a special case, too, so this
   1233  1.1  christos      arrangement is not without motivation.  */
   1234  1.1  christos   if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
   1235  1.1  christos     /* The value's already an rvalue on the stack, so we just need to
   1236  1.1  christos        change the type.  */
   1237  1.1  christos     value->type = lookup_pointer_type (value->type);
   1238  1.1  christos   else
   1239  1.1  christos     switch (value->kind)
   1240  1.1  christos       {
   1241  1.1  christos       case axs_rvalue:
   1242  1.1  christos 	error (_("Operand of `&' is an rvalue, which has no address."));
   1243  1.1  christos 
   1244  1.1  christos       case axs_lvalue_register:
   1245  1.1  christos 	error (_("Operand of `&' is in a register, and has no address."));
   1246  1.1  christos 
   1247  1.1  christos       case axs_lvalue_memory:
   1248  1.1  christos 	value->kind = axs_rvalue;
   1249  1.1  christos 	value->type = lookup_pointer_type (value->type);
   1250  1.1  christos 	break;
   1251  1.1  christos       }
   1252  1.1  christos }
   1253  1.1  christos 
   1254  1.1  christos /* Generate code to push the value of a bitfield of a structure whose
   1255  1.1  christos    address is on the top of the stack.  START and END give the
   1256  1.1  christos    starting and one-past-ending *bit* numbers of the field within the
   1257  1.1  christos    structure.  */
   1258  1.1  christos static void
   1259  1.1  christos gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
   1260  1.1  christos 		  struct axs_value *value, struct type *type,
   1261  1.1  christos 		  int start, int end)
   1262  1.1  christos {
   1263  1.1  christos   /* Note that ops[i] fetches 8 << i bits.  */
   1264  1.1  christos   static enum agent_op ops[]
   1265  1.1  christos     = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
   1266  1.1  christos   static int num_ops = (sizeof (ops) / sizeof (ops[0]));
   1267  1.1  christos 
   1268  1.1  christos   /* We don't want to touch any byte that the bitfield doesn't
   1269  1.1  christos      actually occupy; we shouldn't make any accesses we're not
   1270  1.1  christos      explicitly permitted to.  We rely here on the fact that the
   1271  1.1  christos      bytecode `ref' operators work on unaligned addresses.
   1272  1.1  christos 
   1273  1.1  christos      It takes some fancy footwork to get the stack to work the way
   1274  1.1  christos      we'd like.  Say we're retrieving a bitfield that requires three
   1275  1.1  christos      fetches.  Initially, the stack just contains the address:
   1276  1.1  christos      addr
   1277  1.1  christos      For the first fetch, we duplicate the address
   1278  1.1  christos      addr addr
   1279  1.1  christos      then add the byte offset, do the fetch, and shift and mask as
   1280  1.1  christos      needed, yielding a fragment of the value, properly aligned for
   1281  1.1  christos      the final bitwise or:
   1282  1.1  christos      addr frag1
   1283  1.1  christos      then we swap, and repeat the process:
   1284  1.1  christos      frag1 addr                    --- address on top
   1285  1.1  christos      frag1 addr addr               --- duplicate it
   1286  1.1  christos      frag1 addr frag2              --- get second fragment
   1287  1.1  christos      frag1 frag2 addr              --- swap again
   1288  1.1  christos      frag1 frag2 frag3             --- get third fragment
   1289  1.1  christos      Notice that, since the third fragment is the last one, we don't
   1290  1.1  christos      bother duplicating the address this time.  Now we have all the
   1291  1.1  christos      fragments on the stack, and we can simply `or' them together,
   1292  1.1  christos      yielding the final value of the bitfield.  */
   1293  1.1  christos 
   1294  1.1  christos   /* The first and one-after-last bits in the field, but rounded down
   1295  1.1  christos      and up to byte boundaries.  */
   1296  1.1  christos   int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
   1297  1.1  christos   int bound_end = (((end + TARGET_CHAR_BIT - 1)
   1298  1.1  christos 		    / TARGET_CHAR_BIT)
   1299  1.1  christos 		   * TARGET_CHAR_BIT);
   1300  1.1  christos 
   1301  1.1  christos   /* current bit offset within the structure */
   1302  1.1  christos   int offset;
   1303  1.1  christos 
   1304  1.1  christos   /* The index in ops of the opcode we're considering.  */
   1305  1.1  christos   int op;
   1306  1.1  christos 
   1307  1.1  christos   /* The number of fragments we generated in the process.  Probably
   1308  1.1  christos      equal to the number of `one' bits in bytesize, but who cares?  */
   1309  1.1  christos   int fragment_count;
   1310  1.1  christos 
   1311  1.1  christos   /* Dereference any typedefs.  */
   1312  1.1  christos   type = check_typedef (type);
   1313  1.1  christos 
   1314  1.1  christos   /* Can we fetch the number of bits requested at all?  */
   1315  1.1  christos   if ((end - start) > ((1 << num_ops) * 8))
   1316  1.1  christos     internal_error (__FILE__, __LINE__,
   1317  1.1  christos 		    _("gen_bitfield_ref: bitfield too wide"));
   1318  1.1  christos 
   1319  1.1  christos   /* Note that we know here that we only need to try each opcode once.
   1320  1.1  christos      That may not be true on machines with weird byte sizes.  */
   1321  1.1  christos   offset = bound_start;
   1322  1.1  christos   fragment_count = 0;
   1323  1.1  christos   for (op = num_ops - 1; op >= 0; op--)
   1324  1.1  christos     {
   1325  1.1  christos       /* number of bits that ops[op] would fetch */
   1326  1.1  christos       int op_size = 8 << op;
   1327  1.1  christos 
   1328  1.1  christos       /* The stack at this point, from bottom to top, contains zero or
   1329  1.1  christos          more fragments, then the address.  */
   1330  1.1  christos 
   1331  1.1  christos       /* Does this fetch fit within the bitfield?  */
   1332  1.1  christos       if (offset + op_size <= bound_end)
   1333  1.1  christos 	{
   1334  1.1  christos 	  /* Is this the last fragment?  */
   1335  1.1  christos 	  int last_frag = (offset + op_size == bound_end);
   1336  1.1  christos 
   1337  1.1  christos 	  if (!last_frag)
   1338  1.1  christos 	    ax_simple (ax, aop_dup);	/* keep a copy of the address */
   1339  1.1  christos 
   1340  1.1  christos 	  /* Add the offset.  */
   1341  1.1  christos 	  gen_offset (ax, offset / TARGET_CHAR_BIT);
   1342  1.1  christos 
   1343  1.1  christos 	  if (ax->tracing)
   1344  1.1  christos 	    {
   1345  1.1  christos 	      /* Record the area of memory we're about to fetch.  */
   1346  1.1  christos 	      ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
   1347  1.1  christos 	    }
   1348  1.1  christos 
   1349  1.1  christos 	  /* Perform the fetch.  */
   1350  1.1  christos 	  ax_simple (ax, ops[op]);
   1351  1.1  christos 
   1352  1.1  christos 	  /* Shift the bits we have to their proper position.
   1353  1.1  christos 	     gen_left_shift will generate right shifts when the operand
   1354  1.1  christos 	     is negative.
   1355  1.1  christos 
   1356  1.1  christos 	     A big-endian field diagram to ponder:
   1357  1.1  christos 	     byte 0  byte 1  byte 2  byte 3  byte 4  byte 5  byte 6  byte 7
   1358  1.1  christos 	     +------++------++------++------++------++------++------++------+
   1359  1.1  christos 	     xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
   1360  1.1  christos 	     ^               ^               ^    ^
   1361  1.1  christos 	     bit number      16              32              48   53
   1362  1.1  christos 	     These are bit numbers as supplied by GDB.  Note that the
   1363  1.1  christos 	     bit numbers run from right to left once you've fetched the
   1364  1.1  christos 	     value!
   1365  1.1  christos 
   1366  1.1  christos 	     A little-endian field diagram to ponder:
   1367  1.1  christos 	     byte 7  byte 6  byte 5  byte 4  byte 3  byte 2  byte 1  byte 0
   1368  1.1  christos 	     +------++------++------++------++------++------++------++------+
   1369  1.1  christos 	     xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
   1370  1.1  christos 	     ^               ^               ^           ^   ^
   1371  1.1  christos 	     bit number     48              32              16          4   0
   1372  1.1  christos 
   1373  1.1  christos 	     In both cases, the most significant end is on the left
   1374  1.1  christos 	     (i.e. normal numeric writing order), which means that you
   1375  1.1  christos 	     don't go crazy thinking about `left' and `right' shifts.
   1376  1.1  christos 
   1377  1.1  christos 	     We don't have to worry about masking yet:
   1378  1.1  christos 	     - If they contain garbage off the least significant end, then we
   1379  1.1  christos 	     must be looking at the low end of the field, and the right
   1380  1.1  christos 	     shift will wipe them out.
   1381  1.1  christos 	     - If they contain garbage off the most significant end, then we
   1382  1.1  christos 	     must be looking at the most significant end of the word, and
   1383  1.1  christos 	     the sign/zero extension will wipe them out.
   1384  1.1  christos 	     - If we're in the interior of the word, then there is no garbage
   1385  1.1  christos 	     on either end, because the ref operators zero-extend.  */
   1386  1.1  christos 	  if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
   1387  1.1  christos 	    gen_left_shift (ax, end - (offset + op_size));
   1388  1.1  christos 	  else
   1389  1.1  christos 	    gen_left_shift (ax, offset - start);
   1390  1.1  christos 
   1391  1.1  christos 	  if (!last_frag)
   1392  1.1  christos 	    /* Bring the copy of the address up to the top.  */
   1393  1.1  christos 	    ax_simple (ax, aop_swap);
   1394  1.1  christos 
   1395  1.1  christos 	  offset += op_size;
   1396  1.1  christos 	  fragment_count++;
   1397  1.1  christos 	}
   1398  1.1  christos     }
   1399  1.1  christos 
   1400  1.1  christos   /* Generate enough bitwise `or' operations to combine all the
   1401  1.1  christos      fragments we left on the stack.  */
   1402  1.1  christos   while (fragment_count-- > 1)
   1403  1.1  christos     ax_simple (ax, aop_bit_or);
   1404  1.1  christos 
   1405  1.1  christos   /* Sign- or zero-extend the value as appropriate.  */
   1406  1.1  christos   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));
   1407  1.1  christos 
   1408  1.1  christos   /* This is *not* an lvalue.  Ugh.  */
   1409  1.1  christos   value->kind = axs_rvalue;
   1410  1.1  christos   value->type = type;
   1411  1.1  christos }
   1412  1.1  christos 
   1413  1.1  christos /* Generate bytecodes for field number FIELDNO of type TYPE.  OFFSET
   1414  1.1  christos    is an accumulated offset (in bytes), will be nonzero for objects
   1415  1.1  christos    embedded in other objects, like C++ base classes.  Behavior should
   1416  1.1  christos    generally follow value_primitive_field.  */
   1417  1.1  christos 
   1418  1.1  christos static void
   1419  1.1  christos gen_primitive_field (struct expression *exp,
   1420  1.1  christos 		     struct agent_expr *ax, struct axs_value *value,
   1421  1.1  christos 		     int offset, int fieldno, struct type *type)
   1422  1.1  christos {
   1423  1.1  christos   /* Is this a bitfield?  */
   1424  1.1  christos   if (TYPE_FIELD_PACKED (type, fieldno))
   1425  1.1  christos     gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
   1426  1.1  christos 		      (offset * TARGET_CHAR_BIT
   1427  1.1  christos 		       + TYPE_FIELD_BITPOS (type, fieldno)),
   1428  1.1  christos 		      (offset * TARGET_CHAR_BIT
   1429  1.1  christos 		       + TYPE_FIELD_BITPOS (type, fieldno)
   1430  1.1  christos 		       + TYPE_FIELD_BITSIZE (type, fieldno)));
   1431  1.1  christos   else
   1432  1.1  christos     {
   1433  1.1  christos       gen_offset (ax, offset
   1434  1.1  christos 		  + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
   1435  1.1  christos       value->kind = axs_lvalue_memory;
   1436  1.1  christos       value->type = TYPE_FIELD_TYPE (type, fieldno);
   1437  1.1  christos     }
   1438  1.1  christos }
   1439  1.1  christos 
   1440  1.1  christos /* Search for the given field in either the given type or one of its
   1441  1.6  christos    base classes.  Return 1 if found, 0 if not.  */
   1442  1.1  christos 
   1443  1.1  christos static int
   1444  1.1  christos gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
   1445  1.1  christos 			  struct axs_value *value,
   1446  1.1  christos 			  char *field, int offset, struct type *type)
   1447  1.1  christos {
   1448  1.1  christos   int i, rslt;
   1449  1.1  christos   int nbases = TYPE_N_BASECLASSES (type);
   1450  1.1  christos 
   1451  1.1  christos   type = check_typedef (type);
   1452  1.1  christos 
   1453  1.1  christos   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
   1454  1.1  christos     {
   1455  1.1  christos       const char *this_name = TYPE_FIELD_NAME (type, i);
   1456  1.1  christos 
   1457  1.1  christos       if (this_name)
   1458  1.1  christos 	{
   1459  1.1  christos 	  if (strcmp (field, this_name) == 0)
   1460  1.1  christos 	    {
   1461  1.1  christos 	      /* Note that bytecodes for the struct's base (aka
   1462  1.1  christos 		 "this") will have been generated already, which will
   1463  1.1  christos 		 be unnecessary but not harmful if the static field is
   1464  1.1  christos 		 being handled as a global.  */
   1465  1.1  christos 	      if (field_is_static (&TYPE_FIELD (type, i)))
   1466  1.1  christos 		{
   1467  1.1  christos 		  gen_static_field (exp->gdbarch, ax, value, type, i);
   1468  1.1  christos 		  if (value->optimized_out)
   1469  1.1  christos 		    error (_("static field `%s' has been "
   1470  1.1  christos 			     "optimized out, cannot use"),
   1471  1.1  christos 			   field);
   1472  1.1  christos 		  return 1;
   1473  1.1  christos 		}
   1474  1.1  christos 
   1475  1.1  christos 	      gen_primitive_field (exp, ax, value, offset, i, type);
   1476  1.1  christos 	      return 1;
   1477  1.1  christos 	    }
   1478  1.1  christos #if 0 /* is this right? */
   1479  1.1  christos 	  if (this_name[0] == '\0')
   1480  1.1  christos 	    internal_error (__FILE__, __LINE__,
   1481  1.1  christos 			    _("find_field: anonymous unions not supported"));
   1482  1.1  christos #endif
   1483  1.1  christos 	}
   1484  1.1  christos     }
   1485  1.1  christos 
   1486  1.1  christos   /* Now scan through base classes recursively.  */
   1487  1.1  christos   for (i = 0; i < nbases; i++)
   1488  1.1  christos     {
   1489  1.1  christos       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
   1490  1.1  christos 
   1491  1.1  christos       rslt = gen_struct_ref_recursive (exp, ax, value, field,
   1492  1.1  christos 				       offset + TYPE_BASECLASS_BITPOS (type, i)
   1493  1.1  christos 				       / TARGET_CHAR_BIT,
   1494  1.1  christos 				       basetype);
   1495  1.1  christos       if (rslt)
   1496  1.1  christos 	return 1;
   1497  1.1  christos     }
   1498  1.1  christos 
   1499  1.1  christos   /* Not found anywhere, flag so caller can complain.  */
   1500  1.1  christos   return 0;
   1501  1.1  christos }
   1502  1.1  christos 
   1503  1.1  christos /* Generate code to reference the member named FIELD of a structure or
   1504  1.1  christos    union.  The top of the stack, as described by VALUE, should have
   1505  1.1  christos    type (pointer to a)* struct/union.  OPERATOR_NAME is the name of
   1506  1.1  christos    the operator being compiled, and OPERAND_NAME is the kind of thing
   1507  1.1  christos    it operates on; we use them in error messages.  */
   1508  1.1  christos static void
   1509  1.1  christos gen_struct_ref (struct expression *exp, struct agent_expr *ax,
   1510  1.1  christos 		struct axs_value *value, char *field,
   1511  1.1  christos 		char *operator_name, char *operand_name)
   1512  1.1  christos {
   1513  1.1  christos   struct type *type;
   1514  1.1  christos   int found;
   1515  1.1  christos 
   1516  1.1  christos   /* Follow pointers until we reach a non-pointer.  These aren't the C
   1517  1.1  christos      semantics, but they're what the normal GDB evaluator does, so we
   1518  1.1  christos      should at least be consistent.  */
   1519  1.1  christos   while (pointer_type (value->type))
   1520  1.1  christos     {
   1521  1.1  christos       require_rvalue (ax, value);
   1522  1.1  christos       gen_deref (ax, value);
   1523  1.1  christos     }
   1524  1.1  christos   type = check_typedef (value->type);
   1525  1.1  christos 
   1526  1.1  christos   /* This must yield a structure or a union.  */
   1527  1.1  christos   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
   1528  1.1  christos       && TYPE_CODE (type) != TYPE_CODE_UNION)
   1529  1.1  christos     error (_("The left operand of `%s' is not a %s."),
   1530  1.1  christos 	   operator_name, operand_name);
   1531  1.1  christos 
   1532  1.1  christos   /* And it must be in memory; we don't deal with structure rvalues,
   1533  1.1  christos      or structures living in registers.  */
   1534  1.1  christos   if (value->kind != axs_lvalue_memory)
   1535  1.1  christos     error (_("Structure does not live in memory."));
   1536  1.1  christos 
   1537  1.1  christos   /* Search through fields and base classes recursively.  */
   1538  1.1  christos   found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);
   1539  1.1  christos 
   1540  1.1  christos   if (!found)
   1541  1.1  christos     error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
   1542  1.1  christos 	   field, TYPE_TAG_NAME (type));
   1543  1.1  christos }
   1544  1.1  christos 
   1545  1.1  christos static int
   1546  1.1  christos gen_namespace_elt (struct expression *exp,
   1547  1.1  christos 		   struct agent_expr *ax, struct axs_value *value,
   1548  1.1  christos 		   const struct type *curtype, char *name);
   1549  1.1  christos static int
   1550  1.1  christos gen_maybe_namespace_elt (struct expression *exp,
   1551  1.1  christos 			 struct agent_expr *ax, struct axs_value *value,
   1552  1.1  christos 			 const struct type *curtype, char *name);
   1553  1.1  christos 
   1554  1.1  christos static void
   1555  1.1  christos gen_static_field (struct gdbarch *gdbarch,
   1556  1.1  christos 		  struct agent_expr *ax, struct axs_value *value,
   1557  1.1  christos 		  struct type *type, int fieldno)
   1558  1.1  christos {
   1559  1.6  christos   if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
   1560  1.1  christos     {
   1561  1.1  christos       ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
   1562  1.1  christos       value->kind = axs_lvalue_memory;
   1563  1.1  christos       value->type = TYPE_FIELD_TYPE (type, fieldno);
   1564  1.1  christos       value->optimized_out = 0;
   1565  1.1  christos     }
   1566  1.1  christos   else
   1567  1.1  christos     {
   1568  1.1  christos       const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
   1569  1.1  christos       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0).symbol;
   1570  1.1  christos 
   1571  1.1  christos       if (sym)
   1572  1.1  christos 	{
   1573  1.1  christos 	  gen_var_ref (gdbarch, ax, value, sym);
   1574  1.1  christos 
   1575  1.1  christos 	  /* Don't error if the value was optimized out, we may be
   1576  1.1  christos 	     scanning all static fields and just want to pass over this
   1577  1.1  christos 	     and continue with the rest.  */
   1578  1.1  christos 	}
   1579  1.1  christos       else
   1580  1.1  christos 	{
   1581  1.1  christos 	  /* Silently assume this was optimized out; class printing
   1582  1.1  christos 	     will let the user know why the data is missing.  */
   1583  1.1  christos 	  value->optimized_out = 1;
   1584  1.1  christos 	}
   1585  1.1  christos     }
   1586  1.1  christos }
   1587  1.1  christos 
   1588  1.1  christos static int
   1589  1.1  christos gen_struct_elt_for_reference (struct expression *exp,
   1590  1.1  christos 			      struct agent_expr *ax, struct axs_value *value,
   1591  1.1  christos 			      struct type *type, char *fieldname)
   1592  1.1  christos {
   1593  1.1  christos   struct type *t = type;
   1594  1.1  christos   int i;
   1595  1.1  christos 
   1596  1.1  christos   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
   1597  1.1  christos       && TYPE_CODE (t) != TYPE_CODE_UNION)
   1598  1.1  christos     internal_error (__FILE__, __LINE__,
   1599  1.1  christos 		    _("non-aggregate type to gen_struct_elt_for_reference"));
   1600  1.1  christos 
   1601  1.1  christos   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
   1602  1.1  christos     {
   1603  1.1  christos       const char *t_field_name = TYPE_FIELD_NAME (t, i);
   1604  1.1  christos 
   1605  1.1  christos       if (t_field_name && strcmp (t_field_name, fieldname) == 0)
   1606  1.1  christos 	{
   1607  1.1  christos 	  if (field_is_static (&TYPE_FIELD (t, i)))
   1608  1.1  christos 	    {
   1609  1.1  christos 	      gen_static_field (exp->gdbarch, ax, value, t, i);
   1610  1.1  christos 	      if (value->optimized_out)
   1611  1.1  christos 		error (_("static field `%s' has been "
   1612  1.1  christos 			 "optimized out, cannot use"),
   1613  1.1  christos 		       fieldname);
   1614  1.1  christos 	      return 1;
   1615  1.1  christos 	    }
   1616  1.1  christos 	  if (TYPE_FIELD_PACKED (t, i))
   1617  1.1  christos 	    error (_("pointers to bitfield members not allowed"));
   1618  1.1  christos 
   1619  1.1  christos 	  /* FIXME we need a way to do "want_address" equivalent */
   1620  1.1  christos 
   1621  1.1  christos 	  error (_("Cannot reference non-static field \"%s\""), fieldname);
   1622  1.1  christos 	}
   1623  1.1  christos     }
   1624  1.1  christos 
   1625  1.1  christos   /* FIXME add other scoped-reference cases here */
   1626  1.1  christos 
   1627  1.1  christos   /* Do a last-ditch lookup.  */
   1628  1.1  christos   return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
   1629  1.1  christos }
   1630  1.1  christos 
   1631  1.1  christos /* C++: Return the member NAME of the namespace given by the type
   1632  1.1  christos    CURTYPE.  */
   1633  1.1  christos 
   1634  1.1  christos static int
   1635  1.1  christos gen_namespace_elt (struct expression *exp,
   1636  1.1  christos 		   struct agent_expr *ax, struct axs_value *value,
   1637  1.1  christos 		   const struct type *curtype, char *name)
   1638  1.1  christos {
   1639  1.1  christos   int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);
   1640  1.1  christos 
   1641  1.1  christos   if (!found)
   1642  1.1  christos     error (_("No symbol \"%s\" in namespace \"%s\"."),
   1643  1.1  christos 	   name, TYPE_TAG_NAME (curtype));
   1644  1.1  christos 
   1645  1.1  christos   return found;
   1646  1.1  christos }
   1647  1.1  christos 
   1648  1.1  christos /* A helper function used by value_namespace_elt and
   1649  1.1  christos    value_struct_elt_for_reference.  It looks up NAME inside the
   1650  1.6  christos    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
   1651  1.1  christos    is a class and NAME refers to a type in CURTYPE itself (as opposed
   1652  1.1  christos    to, say, some base class of CURTYPE).  */
   1653  1.1  christos 
   1654  1.1  christos static int
   1655  1.1  christos gen_maybe_namespace_elt (struct expression *exp,
   1656  1.6  christos 			 struct agent_expr *ax, struct axs_value *value,
   1657  1.1  christos 			 const struct type *curtype, char *name)
   1658  1.1  christos {
   1659  1.6  christos   const char *namespace_name = TYPE_TAG_NAME (curtype);
   1660  1.1  christos   struct block_symbol sym;
   1661  1.1  christos 
   1662  1.1  christos   sym = cp_lookup_symbol_namespace (namespace_name, name,
   1663  1.6  christos 				    block_for_pc (ax->scope),
   1664  1.1  christos 				    VAR_DOMAIN);
   1665  1.1  christos 
   1666  1.1  christos   if (sym.symbol == NULL)
   1667  1.1  christos     return 0;
   1668  1.1  christos 
   1669  1.1  christos   gen_var_ref (exp->gdbarch, ax, value, sym.symbol);
   1670  1.1  christos 
   1671  1.1  christos   if (value->optimized_out)
   1672  1.1  christos     error (_("`%s' has been optimized out, cannot use"),
   1673  1.1  christos 	   SYMBOL_PRINT_NAME (sym.symbol));
   1674  1.1  christos 
   1675  1.1  christos   return 1;
   1676  1.1  christos }
   1677  1.1  christos 
   1678  1.1  christos 
   1679  1.1  christos static int
   1680  1.1  christos gen_aggregate_elt_ref (struct expression *exp,
   1681  1.1  christos 		       struct agent_expr *ax, struct axs_value *value,
   1682  1.1  christos 		       struct type *type, char *field,
   1683  1.1  christos 		       char *operator_name, char *operand_name)
   1684  1.1  christos {
   1685  1.1  christos   switch (TYPE_CODE (type))
   1686  1.1  christos     {
   1687  1.1  christos     case TYPE_CODE_STRUCT:
   1688  1.1  christos     case TYPE_CODE_UNION:
   1689  1.1  christos       return gen_struct_elt_for_reference (exp, ax, value, type, field);
   1690  1.1  christos       break;
   1691  1.1  christos     case TYPE_CODE_NAMESPACE:
   1692  1.1  christos       return gen_namespace_elt (exp, ax, value, type, field);
   1693  1.1  christos       break;
   1694  1.1  christos     default:
   1695  1.1  christos       internal_error (__FILE__, __LINE__,
   1696  1.1  christos 		      _("non-aggregate type in gen_aggregate_elt_ref"));
   1697  1.1  christos     }
   1698  1.1  christos 
   1699  1.1  christos   return 0;
   1700  1.1  christos }
   1701  1.1  christos 
   1702  1.1  christos /* Generate code for GDB's magical `repeat' operator.
   1703  1.1  christos    LVALUE @ INT creates an array INT elements long, and whose elements
   1704  1.1  christos    have the same type as LVALUE, located in memory so that LVALUE is
   1705  1.1  christos    its first element.  For example, argv[0]@argc gives you the array
   1706  1.1  christos    of command-line arguments.
   1707  1.1  christos 
   1708  1.1  christos    Unfortunately, because we have to know the types before we actually
   1709  1.1  christos    have a value for the expression, we can't implement this perfectly
   1710  1.1  christos    without changing the type system, having values that occupy two
   1711  1.1  christos    stack slots, doing weird things with sizeof, etc.  So we require
   1712  1.1  christos    the right operand to be a constant expression.  */
   1713  1.1  christos static void
   1714  1.1  christos gen_repeat (struct expression *exp, union exp_element **pc,
   1715  1.1  christos 	    struct agent_expr *ax, struct axs_value *value)
   1716  1.1  christos {
   1717  1.1  christos   struct axs_value value1;
   1718  1.1  christos 
   1719  1.1  christos   /* We don't want to turn this into an rvalue, so no conversions
   1720  1.1  christos      here.  */
   1721  1.1  christos   gen_expr (exp, pc, ax, &value1);
   1722  1.1  christos   if (value1.kind != axs_lvalue_memory)
   1723  1.1  christos     error (_("Left operand of `@' must be an object in memory."));
   1724  1.1  christos 
   1725  1.1  christos   /* Evaluate the length; it had better be a constant.  */
   1726  1.1  christos   {
   1727  1.1  christos     struct value *v = const_expr (pc);
   1728  1.1  christos     int length;
   1729  1.1  christos 
   1730  1.1  christos     if (!v)
   1731  1.1  christos       error (_("Right operand of `@' must be a "
   1732  1.1  christos 	       "constant, in agent expressions."));
   1733  1.1  christos     if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
   1734  1.1  christos       error (_("Right operand of `@' must be an integer."));
   1735  1.1  christos     length = value_as_long (v);
   1736  1.1  christos     if (length <= 0)
   1737  1.1  christos       error (_("Right operand of `@' must be positive."));
   1738  1.1  christos 
   1739  1.1  christos     /* The top of the stack is already the address of the object, so
   1740  1.1  christos        all we need to do is frob the type of the lvalue.  */
   1741  1.1  christos     {
   1742  1.1  christos       /* FIXME-type-allocation: need a way to free this type when we are
   1743  1.1  christos          done with it.  */
   1744  1.1  christos       struct type *array
   1745  1.1  christos 	= lookup_array_range_type (value1.type, 0, length - 1);
   1746  1.1  christos 
   1747  1.1  christos       value->kind = axs_lvalue_memory;
   1748  1.1  christos       value->type = array;
   1749  1.1  christos     }
   1750  1.1  christos   }
   1751  1.1  christos }
   1752  1.1  christos 
   1753  1.1  christos 
   1754  1.1  christos /* Emit code for the `sizeof' operator.
   1755  1.1  christos    *PC should point at the start of the operand expression; we advance it
   1756  1.1  christos    to the first instruction after the operand.  */
   1757  1.1  christos static void
   1758  1.1  christos gen_sizeof (struct expression *exp, union exp_element **pc,
   1759  1.1  christos 	    struct agent_expr *ax, struct axs_value *value,
   1760  1.1  christos 	    struct type *size_type)
   1761  1.1  christos {
   1762  1.1  christos   /* We don't care about the value of the operand expression; we only
   1763  1.1  christos      care about its type.  However, in the current arrangement, the
   1764  1.1  christos      only way to find an expression's type is to generate code for it.
   1765  1.1  christos      So we generate code for the operand, and then throw it away,
   1766  1.1  christos      replacing it with code that simply pushes its size.  */
   1767  1.1  christos   int start = ax->len;
   1768  1.1  christos 
   1769  1.1  christos   gen_expr (exp, pc, ax, value);
   1770  1.1  christos 
   1771  1.1  christos   /* Throw away the code we just generated.  */
   1772  1.1  christos   ax->len = start;
   1773  1.1  christos 
   1774  1.1  christos   ax_const_l (ax, TYPE_LENGTH (value->type));
   1775  1.1  christos   value->kind = axs_rvalue;
   1776  1.1  christos   value->type = size_type;
   1777  1.1  christos }
   1778  1.1  christos 
   1779  1.1  christos 
   1781  1.1  christos /* Generating bytecode from GDB expressions: general recursive thingy  */
   1782  1.1  christos 
   1783  1.1  christos /* XXX: i18n */
   1784  1.1  christos /* A gen_expr function written by a Gen-X'er guy.
   1785  1.1  christos    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
   1786  1.1  christos void
   1787  1.1  christos gen_expr (struct expression *exp, union exp_element **pc,
   1788  1.1  christos 	  struct agent_expr *ax, struct axs_value *value)
   1789  1.1  christos {
   1790  1.1  christos   /* Used to hold the descriptions of operand expressions.  */
   1791  1.1  christos   struct axs_value value1, value2, value3;
   1792  1.1  christos   enum exp_opcode op = (*pc)[0].opcode, op2;
   1793  1.1  christos   int if1, go1, if2, go2, end;
   1794  1.1  christos   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
   1795  1.1  christos 
   1796  1.1  christos   /* If we're looking at a constant expression, just push its value.  */
   1797  1.1  christos   {
   1798  1.1  christos     struct value *v = maybe_const_expr (pc);
   1799  1.1  christos 
   1800  1.1  christos     if (v)
   1801  1.1  christos       {
   1802  1.1  christos 	ax_const_l (ax, value_as_long (v));
   1803  1.1  christos 	value->kind = axs_rvalue;
   1804  1.1  christos 	value->type = check_typedef (value_type (v));
   1805  1.1  christos 	return;
   1806  1.1  christos       }
   1807  1.1  christos   }
   1808  1.1  christos 
   1809  1.1  christos   /* Otherwise, go ahead and generate code for it.  */
   1810  1.1  christos   switch (op)
   1811  1.1  christos     {
   1812  1.1  christos       /* Binary arithmetic operators.  */
   1813  1.1  christos     case BINOP_ADD:
   1814  1.1  christos     case BINOP_SUB:
   1815  1.1  christos     case BINOP_MUL:
   1816  1.1  christos     case BINOP_DIV:
   1817  1.1  christos     case BINOP_REM:
   1818  1.1  christos     case BINOP_LSH:
   1819  1.1  christos     case BINOP_RSH:
   1820  1.1  christos     case BINOP_SUBSCRIPT:
   1821  1.1  christos     case BINOP_BITWISE_AND:
   1822  1.1  christos     case BINOP_BITWISE_IOR:
   1823  1.1  christos     case BINOP_BITWISE_XOR:
   1824  1.1  christos     case BINOP_EQUAL:
   1825  1.1  christos     case BINOP_NOTEQUAL:
   1826  1.1  christos     case BINOP_LESS:
   1827  1.1  christos     case BINOP_GTR:
   1828  1.1  christos     case BINOP_LEQ:
   1829  1.1  christos     case BINOP_GEQ:
   1830  1.1  christos       (*pc)++;
   1831  1.1  christos       gen_expr (exp, pc, ax, &value1);
   1832  1.1  christos       gen_usual_unary (exp, ax, &value1);
   1833  1.1  christos       gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
   1834  1.1  christos       break;
   1835  1.1  christos 
   1836  1.1  christos     case BINOP_LOGICAL_AND:
   1837  1.1  christos       (*pc)++;
   1838  1.1  christos       /* Generate the obvious sequence of tests and jumps.  */
   1839  1.1  christos       gen_expr (exp, pc, ax, &value1);
   1840  1.1  christos       gen_usual_unary (exp, ax, &value1);
   1841  1.1  christos       if1 = ax_goto (ax, aop_if_goto);
   1842  1.1  christos       go1 = ax_goto (ax, aop_goto);
   1843  1.1  christos       ax_label (ax, if1, ax->len);
   1844  1.1  christos       gen_expr (exp, pc, ax, &value2);
   1845  1.1  christos       gen_usual_unary (exp, ax, &value2);
   1846  1.1  christos       if2 = ax_goto (ax, aop_if_goto);
   1847  1.1  christos       go2 = ax_goto (ax, aop_goto);
   1848  1.1  christos       ax_label (ax, if2, ax->len);
   1849  1.1  christos       ax_const_l (ax, 1);
   1850  1.1  christos       end = ax_goto (ax, aop_goto);
   1851  1.1  christos       ax_label (ax, go1, ax->len);
   1852  1.1  christos       ax_label (ax, go2, ax->len);
   1853  1.1  christos       ax_const_l (ax, 0);
   1854  1.1  christos       ax_label (ax, end, ax->len);
   1855  1.1  christos       value->kind = axs_rvalue;
   1856  1.1  christos       value->type = int_type;
   1857  1.1  christos       break;
   1858  1.1  christos 
   1859  1.1  christos     case BINOP_LOGICAL_OR:
   1860  1.1  christos       (*pc)++;
   1861  1.1  christos       /* Generate the obvious sequence of tests and jumps.  */
   1862  1.1  christos       gen_expr (exp, pc, ax, &value1);
   1863  1.1  christos       gen_usual_unary (exp, ax, &value1);
   1864  1.1  christos       if1 = ax_goto (ax, aop_if_goto);
   1865  1.1  christos       gen_expr (exp, pc, ax, &value2);
   1866  1.1  christos       gen_usual_unary (exp, ax, &value2);
   1867  1.1  christos       if2 = ax_goto (ax, aop_if_goto);
   1868  1.1  christos       ax_const_l (ax, 0);
   1869  1.1  christos       end = ax_goto (ax, aop_goto);
   1870  1.1  christos       ax_label (ax, if1, ax->len);
   1871  1.1  christos       ax_label (ax, if2, ax->len);
   1872  1.1  christos       ax_const_l (ax, 1);
   1873  1.1  christos       ax_label (ax, end, ax->len);
   1874  1.1  christos       value->kind = axs_rvalue;
   1875  1.1  christos       value->type = int_type;
   1876  1.1  christos       break;
   1877  1.1  christos 
   1878  1.1  christos     case TERNOP_COND:
   1879  1.1  christos       (*pc)++;
   1880  1.1  christos       gen_expr (exp, pc, ax, &value1);
   1881  1.1  christos       gen_usual_unary (exp, ax, &value1);
   1882  1.1  christos       /* For (A ? B : C), it's easiest to generate subexpression
   1883  1.1  christos 	 bytecodes in order, but if_goto jumps on true, so we invert
   1884  1.1  christos 	 the sense of A.  Then we can do B by dropping through, and
   1885  1.1  christos 	 jump to do C.  */
   1886  1.1  christos       gen_logical_not (ax, &value1, int_type);
   1887  1.1  christos       if1 = ax_goto (ax, aop_if_goto);
   1888  1.1  christos       gen_expr (exp, pc, ax, &value2);
   1889  1.1  christos       gen_usual_unary (exp, ax, &value2);
   1890  1.1  christos       end = ax_goto (ax, aop_goto);
   1891  1.1  christos       ax_label (ax, if1, ax->len);
   1892  1.1  christos       gen_expr (exp, pc, ax, &value3);
   1893  1.1  christos       gen_usual_unary (exp, ax, &value3);
   1894  1.1  christos       ax_label (ax, end, ax->len);
   1895  1.1  christos       /* This is arbitary - what if B and C are incompatible types? */
   1896  1.1  christos       value->type = value2.type;
   1897  1.1  christos       value->kind = value2.kind;
   1898  1.1  christos       break;
   1899  1.1  christos 
   1900  1.1  christos     case BINOP_ASSIGN:
   1901  1.1  christos       (*pc)++;
   1902  1.1  christos       if ((*pc)[0].opcode == OP_INTERNALVAR)
   1903  1.1  christos 	{
   1904  1.1  christos 	  char *name = internalvar_name ((*pc)[1].internalvar);
   1905  1.1  christos 	  struct trace_state_variable *tsv;
   1906  1.1  christos 
   1907  1.1  christos 	  (*pc) += 3;
   1908  1.1  christos 	  gen_expr (exp, pc, ax, value);
   1909  1.1  christos 	  tsv = find_trace_state_variable (name);
   1910  1.1  christos 	  if (tsv)
   1911  1.1  christos 	    {
   1912  1.1  christos 	      ax_tsv (ax, aop_setv, tsv->number);
   1913  1.1  christos 	      if (ax->tracing)
   1914  1.1  christos 		ax_tsv (ax, aop_tracev, tsv->number);
   1915  1.1  christos 	    }
   1916  1.1  christos 	  else
   1917  1.1  christos 	    error (_("$%s is not a trace state variable, "
   1918  1.1  christos 		     "may not assign to it"), name);
   1919  1.1  christos 	}
   1920  1.1  christos       else
   1921  1.1  christos 	error (_("May only assign to trace state variables"));
   1922  1.1  christos       break;
   1923  1.1  christos 
   1924  1.1  christos     case BINOP_ASSIGN_MODIFY:
   1925  1.1  christos       (*pc)++;
   1926  1.1  christos       op2 = (*pc)[0].opcode;
   1927  1.1  christos       (*pc)++;
   1928  1.1  christos       (*pc)++;
   1929  1.1  christos       if ((*pc)[0].opcode == OP_INTERNALVAR)
   1930  1.1  christos 	{
   1931  1.1  christos 	  char *name = internalvar_name ((*pc)[1].internalvar);
   1932  1.1  christos 	  struct trace_state_variable *tsv;
   1933  1.1  christos 
   1934  1.1  christos 	  (*pc) += 3;
   1935  1.1  christos 	  tsv = find_trace_state_variable (name);
   1936  1.1  christos 	  if (tsv)
   1937  1.1  christos 	    {
   1938  1.1  christos 	      /* The tsv will be the left half of the binary operation.  */
   1939  1.1  christos 	      ax_tsv (ax, aop_getv, tsv->number);
   1940  1.1  christos 	      if (ax->tracing)
   1941  1.1  christos 		ax_tsv (ax, aop_tracev, tsv->number);
   1942  1.1  christos 	      /* Trace state variables are always 64-bit integers.  */
   1943  1.1  christos 	      value1.kind = axs_rvalue;
   1944  1.1  christos 	      value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
   1945  1.1  christos 	      /* Now do right half of expression.  */
   1946  1.1  christos 	      gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
   1947  1.1  christos 	      /* We have a result of the binary op, set the tsv.  */
   1948  1.1  christos 	      ax_tsv (ax, aop_setv, tsv->number);
   1949  1.1  christos 	      if (ax->tracing)
   1950  1.1  christos 		ax_tsv (ax, aop_tracev, tsv->number);
   1951  1.1  christos 	    }
   1952  1.1  christos 	  else
   1953  1.1  christos 	    error (_("$%s is not a trace state variable, "
   1954  1.1  christos 		     "may not assign to it"), name);
   1955  1.1  christos 	}
   1956  1.1  christos       else
   1957  1.1  christos 	error (_("May only assign to trace state variables"));
   1958  1.1  christos       break;
   1959  1.1  christos 
   1960  1.1  christos       /* Note that we need to be a little subtle about generating code
   1961  1.1  christos          for comma.  In C, we can do some optimizations here because
   1962  1.1  christos          we know the left operand is only being evaluated for effect.
   1963  1.1  christos          However, if the tracing kludge is in effect, then we always
   1964  1.1  christos          need to evaluate the left hand side fully, so that all the
   1965  1.1  christos          variables it mentions get traced.  */
   1966  1.1  christos     case BINOP_COMMA:
   1967  1.1  christos       (*pc)++;
   1968  1.1  christos       gen_expr (exp, pc, ax, &value1);
   1969  1.1  christos       /* Don't just dispose of the left operand.  We might be tracing,
   1970  1.1  christos          in which case we want to emit code to trace it if it's an
   1971  1.1  christos          lvalue.  */
   1972  1.1  christos       gen_traced_pop (exp->gdbarch, ax, &value1);
   1973  1.1  christos       gen_expr (exp, pc, ax, value);
   1974  1.1  christos       /* It's the consumer's responsibility to trace the right operand.  */
   1975  1.1  christos       break;
   1976  1.1  christos 
   1977  1.1  christos     case OP_LONG:		/* some integer constant */
   1978  1.1  christos       {
   1979  1.1  christos 	struct type *type = (*pc)[1].type;
   1980  1.1  christos 	LONGEST k = (*pc)[2].longconst;
   1981  1.1  christos 
   1982  1.1  christos 	(*pc) += 4;
   1983  1.1  christos 	gen_int_literal (ax, value, k, type);
   1984  1.1  christos       }
   1985  1.1  christos       break;
   1986  1.1  christos 
   1987  1.1  christos     case OP_VAR_VALUE:
   1988  1.1  christos       gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);
   1989  1.1  christos 
   1990  1.1  christos       if (value->optimized_out)
   1991  1.1  christos 	error (_("`%s' has been optimized out, cannot use"),
   1992  1.1  christos 	       SYMBOL_PRINT_NAME ((*pc)[2].symbol));
   1993  1.1  christos 
   1994  1.1  christos       (*pc) += 4;
   1995  1.1  christos       break;
   1996  1.1  christos 
   1997  1.1  christos     case OP_REGISTER:
   1998  1.1  christos       {
   1999  1.1  christos 	const char *name = &(*pc)[2].string;
   2000  1.1  christos 	int reg;
   2001  1.1  christos 
   2002  1.1  christos 	(*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
   2003  1.1  christos 	reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
   2004  1.1  christos 	if (reg == -1)
   2005  1.1  christos 	  internal_error (__FILE__, __LINE__,
   2006  1.1  christos 			  _("Register $%s not available"), name);
   2007  1.1  christos 	/* No support for tracing user registers yet.  */
   2008  1.1  christos 	if (reg >= gdbarch_num_regs (exp->gdbarch)
   2009  1.1  christos 	    + gdbarch_num_pseudo_regs (exp->gdbarch))
   2010  1.1  christos 	  error (_("'%s' is a user-register; "
   2011  1.1  christos 		   "GDB cannot yet trace user-register contents."),
   2012  1.1  christos 		 name);
   2013  1.1  christos 	value->kind = axs_lvalue_register;
   2014  1.1  christos 	value->u.reg = reg;
   2015  1.1  christos 	value->type = register_type (exp->gdbarch, reg);
   2016  1.1  christos       }
   2017  1.1  christos       break;
   2018  1.1  christos 
   2019  1.1  christos     case OP_INTERNALVAR:
   2020  1.1  christos       {
   2021  1.1  christos 	struct internalvar *var = (*pc)[1].internalvar;
   2022  1.1  christos 	const char *name = internalvar_name (var);
   2023  1.1  christos 	struct trace_state_variable *tsv;
   2024  1.1  christos 
   2025  1.1  christos 	(*pc) += 3;
   2026  1.1  christos 	tsv = find_trace_state_variable (name);
   2027  1.1  christos 	if (tsv)
   2028  1.1  christos 	  {
   2029  1.1  christos 	    ax_tsv (ax, aop_getv, tsv->number);
   2030  1.1  christos 	    if (ax->tracing)
   2031  1.1  christos 	      ax_tsv (ax, aop_tracev, tsv->number);
   2032  1.1  christos 	    /* Trace state variables are always 64-bit integers.  */
   2033  1.1  christos 	    value->kind = axs_rvalue;
   2034  1.1  christos 	    value->type = builtin_type (exp->gdbarch)->builtin_long_long;
   2035  1.1  christos 	  }
   2036  1.1  christos 	else if (! compile_internalvar_to_ax (var, ax, value))
   2037  1.1  christos 	  error (_("$%s is not a trace state variable; GDB agent "
   2038  1.1  christos 		   "expressions cannot use convenience variables."), name);
   2039  1.1  christos       }
   2040  1.1  christos       break;
   2041  1.1  christos 
   2042  1.1  christos       /* Weirdo operator: see comments for gen_repeat for details.  */
   2043  1.1  christos     case BINOP_REPEAT:
   2044  1.1  christos       /* Note that gen_repeat handles its own argument evaluation.  */
   2045  1.1  christos       (*pc)++;
   2046  1.1  christos       gen_repeat (exp, pc, ax, value);
   2047  1.1  christos       break;
   2048  1.1  christos 
   2049  1.1  christos     case UNOP_CAST:
   2050  1.1  christos       {
   2051  1.1  christos 	struct type *type = (*pc)[1].type;
   2052  1.1  christos 
   2053  1.1  christos 	(*pc) += 3;
   2054  1.1  christos 	gen_expr (exp, pc, ax, value);
   2055  1.1  christos 	gen_cast (ax, value, type);
   2056  1.1  christos       }
   2057  1.1  christos       break;
   2058  1.1  christos 
   2059  1.1  christos     case UNOP_CAST_TYPE:
   2060  1.1  christos       {
   2061  1.1  christos 	int offset;
   2062  1.1  christos 	struct value *val;
   2063  1.1  christos 	struct type *type;
   2064  1.1  christos 
   2065  1.1  christos 	++*pc;
   2066  1.1  christos 	offset = *pc - exp->elts;
   2067  1.1  christos 	val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
   2068  1.1  christos 	type = value_type (val);
   2069  1.1  christos 	*pc = &exp->elts[offset];
   2070  1.1  christos 
   2071  1.1  christos 	gen_expr (exp, pc, ax, value);
   2072  1.1  christos 	gen_cast (ax, value, type);
   2073  1.1  christos       }
   2074  1.1  christos       break;
   2075  1.1  christos 
   2076  1.1  christos     case UNOP_MEMVAL:
   2077  1.1  christos       {
   2078  1.1  christos 	struct type *type = check_typedef ((*pc)[1].type);
   2079  1.1  christos 
   2080  1.1  christos 	(*pc) += 3;
   2081  1.1  christos 	gen_expr (exp, pc, ax, value);
   2082  1.1  christos 
   2083  1.1  christos 	/* If we have an axs_rvalue or an axs_lvalue_memory, then we
   2084  1.1  christos 	   already have the right value on the stack.  For
   2085  1.1  christos 	   axs_lvalue_register, we must convert.  */
   2086  1.1  christos 	if (value->kind == axs_lvalue_register)
   2087  1.1  christos 	  require_rvalue (ax, value);
   2088  1.1  christos 
   2089  1.1  christos 	value->type = type;
   2090  1.1  christos 	value->kind = axs_lvalue_memory;
   2091  1.1  christos       }
   2092  1.1  christos       break;
   2093  1.1  christos 
   2094  1.1  christos     case UNOP_MEMVAL_TYPE:
   2095  1.1  christos       {
   2096  1.1  christos 	int offset;
   2097  1.1  christos 	struct value *val;
   2098  1.1  christos 	struct type *type;
   2099  1.1  christos 
   2100  1.1  christos 	++*pc;
   2101  1.1  christos 	offset = *pc - exp->elts;
   2102  1.1  christos 	val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
   2103  1.1  christos 	type = value_type (val);
   2104  1.1  christos 	*pc = &exp->elts[offset];
   2105  1.1  christos 
   2106  1.1  christos 	gen_expr (exp, pc, ax, value);
   2107  1.1  christos 
   2108  1.1  christos 	/* If we have an axs_rvalue or an axs_lvalue_memory, then we
   2109  1.1  christos 	   already have the right value on the stack.  For
   2110  1.1  christos 	   axs_lvalue_register, we must convert.  */
   2111  1.1  christos 	if (value->kind == axs_lvalue_register)
   2112  1.1  christos 	  require_rvalue (ax, value);
   2113  1.1  christos 
   2114  1.1  christos 	value->type = type;
   2115  1.1  christos 	value->kind = axs_lvalue_memory;
   2116  1.1  christos       }
   2117  1.1  christos       break;
   2118  1.1  christos 
   2119  1.1  christos     case UNOP_PLUS:
   2120  1.1  christos       (*pc)++;
   2121  1.1  christos       /* + FOO is equivalent to 0 + FOO, which can be optimized.  */
   2122  1.1  christos       gen_expr (exp, pc, ax, value);
   2123  1.1  christos       gen_usual_unary (exp, ax, value);
   2124  1.1  christos       break;
   2125  1.1  christos 
   2126  1.1  christos     case UNOP_NEG:
   2127  1.1  christos       (*pc)++;
   2128  1.1  christos       /* -FOO is equivalent to 0 - FOO.  */
   2129  1.1  christos       gen_int_literal (ax, &value1, 0,
   2130  1.1  christos 		       builtin_type (exp->gdbarch)->builtin_int);
   2131  1.1  christos       gen_usual_unary (exp, ax, &value1);	/* shouldn't do much */
   2132  1.1  christos       gen_expr (exp, pc, ax, &value2);
   2133  1.1  christos       gen_usual_unary (exp, ax, &value2);
   2134  1.1  christos       gen_usual_arithmetic (exp, ax, &value1, &value2);
   2135  1.1  christos       gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
   2136  1.1  christos       break;
   2137  1.1  christos 
   2138  1.1  christos     case UNOP_LOGICAL_NOT:
   2139  1.1  christos       (*pc)++;
   2140  1.1  christos       gen_expr (exp, pc, ax, value);
   2141  1.1  christos       gen_usual_unary (exp, ax, value);
   2142  1.1  christos       gen_logical_not (ax, value, int_type);
   2143  1.1  christos       break;
   2144  1.1  christos 
   2145  1.1  christos     case UNOP_COMPLEMENT:
   2146  1.1  christos       (*pc)++;
   2147  1.1  christos       gen_expr (exp, pc, ax, value);
   2148  1.1  christos       gen_usual_unary (exp, ax, value);
   2149  1.1  christos       gen_integral_promotions (exp, ax, value);
   2150  1.1  christos       gen_complement (ax, value);
   2151  1.1  christos       break;
   2152  1.1  christos 
   2153  1.1  christos     case UNOP_IND:
   2154  1.1  christos       (*pc)++;
   2155  1.1  christos       gen_expr (exp, pc, ax, value);
   2156  1.1  christos       gen_usual_unary (exp, ax, value);
   2157  1.1  christos       if (!pointer_type (value->type))
   2158  1.1  christos 	error (_("Argument of unary `*' is not a pointer."));
   2159  1.1  christos       gen_deref (ax, value);
   2160  1.1  christos       break;
   2161  1.1  christos 
   2162  1.1  christos     case UNOP_ADDR:
   2163  1.1  christos       (*pc)++;
   2164  1.1  christos       gen_expr (exp, pc, ax, value);
   2165  1.1  christos       gen_address_of (ax, value);
   2166  1.1  christos       break;
   2167  1.1  christos 
   2168  1.1  christos     case UNOP_SIZEOF:
   2169  1.1  christos       (*pc)++;
   2170  1.1  christos       /* Notice that gen_sizeof handles its own operand, unlike most
   2171  1.1  christos          of the other unary operator functions.  This is because we
   2172  1.1  christos          have to throw away the code we generate.  */
   2173  1.1  christos       gen_sizeof (exp, pc, ax, value,
   2174  1.1  christos 		  builtin_type (exp->gdbarch)->builtin_int);
   2175  1.1  christos       break;
   2176  1.1  christos 
   2177  1.1  christos     case STRUCTOP_STRUCT:
   2178  1.1  christos     case STRUCTOP_PTR:
   2179  1.1  christos       {
   2180  1.1  christos 	int length = (*pc)[1].longconst;
   2181  1.1  christos 	char *name = &(*pc)[2].string;
   2182  1.1  christos 
   2183  1.1  christos 	(*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
   2184  1.1  christos 	gen_expr (exp, pc, ax, value);
   2185  1.1  christos 	if (op == STRUCTOP_STRUCT)
   2186  1.1  christos 	  gen_struct_ref (exp, ax, value, name, ".", "structure or union");
   2187  1.1  christos 	else if (op == STRUCTOP_PTR)
   2188  1.1  christos 	  gen_struct_ref (exp, ax, value, name, "->",
   2189  1.1  christos 			  "pointer to a structure or union");
   2190  1.3  christos 	else
   2191  1.1  christos 	  /* If this `if' chain doesn't handle it, then the case list
   2192  1.1  christos 	     shouldn't mention it, and we shouldn't be here.  */
   2193  1.1  christos 	  internal_error (__FILE__, __LINE__,
   2194  1.1  christos 			  _("gen_expr: unhandled struct case"));
   2195  1.1  christos       }
   2196  1.1  christos       break;
   2197  1.6  christos 
   2198  1.1  christos     case OP_THIS:
   2199  1.1  christos       {
   2200  1.1  christos 	struct symbol *sym, *func;
   2201  1.1  christos 	const struct block *b;
   2202  1.1  christos 	const struct language_defn *lang;
   2203  1.1  christos 
   2204  1.1  christos 	b = block_for_pc (ax->scope);
   2205  1.1  christos 	func = block_linkage_function (b);
   2206  1.1  christos 	lang = language_def (SYMBOL_LANGUAGE (func));
   2207  1.1  christos 
   2208  1.1  christos 	sym = lookup_language_this (lang, b).symbol;
   2209  1.1  christos 	if (!sym)
   2210  1.1  christos 	  error (_("no `%s' found"), lang->la_name_of_this);
   2211  1.1  christos 
   2212  1.1  christos 	gen_var_ref (exp->gdbarch, ax, value, sym);
   2213  1.1  christos 
   2214  1.1  christos 	if (value->optimized_out)
   2215  1.1  christos 	  error (_("`%s' has been optimized out, cannot use"),
   2216  1.1  christos 		 SYMBOL_PRINT_NAME (sym));
   2217  1.1  christos 
   2218  1.1  christos 	(*pc) += 2;
   2219  1.1  christos       }
   2220  1.1  christos       break;
   2221  1.1  christos 
   2222  1.1  christos     case OP_SCOPE:
   2223  1.1  christos       {
   2224  1.1  christos 	struct type *type = (*pc)[1].type;
   2225  1.1  christos 	int length = longest_to_int ((*pc)[2].longconst);
   2226  1.1  christos 	char *name = &(*pc)[3].string;
   2227  1.1  christos 	int found;
   2228  1.1  christos 
   2229  1.1  christos 	found = gen_aggregate_elt_ref (exp, ax, value, type, name,
   2230  1.1  christos 				       "?", "??");
   2231  1.1  christos 	if (!found)
   2232  1.1  christos 	  error (_("There is no field named %s"), name);
   2233  1.1  christos 	(*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
   2234  1.1  christos       }
   2235  1.1  christos       break;
   2236  1.1  christos 
   2237  1.1  christos     case OP_TYPE:
   2238  1.1  christos     case OP_TYPEOF:
   2239  1.1  christos     case OP_DECLTYPE:
   2240  1.1  christos       error (_("Attempt to use a type name as an expression."));
   2241  1.1  christos 
   2242  1.1  christos     default:
   2243  1.1  christos       error (_("Unsupported operator %s (%d) in expression."),
   2244  1.1  christos 	     op_name (exp, op), op);
   2245  1.1  christos     }
   2246  1.1  christos }
   2247  1.1  christos 
   2248  1.1  christos /* This handles the middle-to-right-side of code generation for binary
   2249  1.1  christos    expressions, which is shared between regular binary operations and
   2250  1.1  christos    assign-modify (+= and friends) expressions.  */
   2251  1.1  christos 
   2252  1.1  christos static void
   2253  1.1  christos gen_expr_binop_rest (struct expression *exp,
   2254  1.1  christos 		     enum exp_opcode op, union exp_element **pc,
   2255  1.1  christos 		     struct agent_expr *ax, struct axs_value *value,
   2256  1.1  christos 		     struct axs_value *value1, struct axs_value *value2)
   2257  1.1  christos {
   2258  1.1  christos   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;
   2259  1.1  christos 
   2260  1.1  christos   gen_expr (exp, pc, ax, value2);
   2261  1.1  christos   gen_usual_unary (exp, ax, value2);
   2262  1.1  christos   gen_usual_arithmetic (exp, ax, value1, value2);
   2263  1.1  christos   switch (op)
   2264  1.1  christos     {
   2265  1.1  christos     case BINOP_ADD:
   2266  1.1  christos       if (TYPE_CODE (value1->type) == TYPE_CODE_INT
   2267  1.1  christos 	  && pointer_type (value2->type))
   2268  1.1  christos 	{
   2269  1.1  christos 	  /* Swap the values and proceed normally.  */
   2270  1.1  christos 	  ax_simple (ax, aop_swap);
   2271  1.1  christos 	  gen_ptradd (ax, value, value2, value1);
   2272  1.1  christos 	}
   2273  1.1  christos       else if (pointer_type (value1->type)
   2274  1.1  christos 	       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
   2275  1.1  christos 	gen_ptradd (ax, value, value1, value2);
   2276  1.1  christos       else
   2277  1.1  christos 	gen_binop (ax, value, value1, value2,
   2278  1.1  christos 		   aop_add, aop_add, 1, "addition");
   2279  1.1  christos       break;
   2280  1.1  christos     case BINOP_SUB:
   2281  1.1  christos       if (pointer_type (value1->type)
   2282  1.1  christos 	  && TYPE_CODE (value2->type) == TYPE_CODE_INT)
   2283  1.1  christos 	gen_ptrsub (ax,value, value1, value2);
   2284  1.1  christos       else if (pointer_type (value1->type)
   2285  1.1  christos 	       && pointer_type (value2->type))
   2286  1.1  christos 	/* FIXME --- result type should be ptrdiff_t */
   2287  1.1  christos 	gen_ptrdiff (ax, value, value1, value2,
   2288  1.1  christos 		     builtin_type (exp->gdbarch)->builtin_long);
   2289  1.1  christos       else
   2290  1.1  christos 	gen_binop (ax, value, value1, value2,
   2291  1.1  christos 		   aop_sub, aop_sub, 1, "subtraction");
   2292  1.1  christos       break;
   2293  1.1  christos     case BINOP_MUL:
   2294  1.1  christos       gen_binop (ax, value, value1, value2,
   2295  1.1  christos 		 aop_mul, aop_mul, 1, "multiplication");
   2296  1.1  christos       break;
   2297  1.1  christos     case BINOP_DIV:
   2298  1.1  christos       gen_binop (ax, value, value1, value2,
   2299  1.1  christos 		 aop_div_signed, aop_div_unsigned, 1, "division");
   2300  1.1  christos       break;
   2301  1.1  christos     case BINOP_REM:
   2302  1.1  christos       gen_binop (ax, value, value1, value2,
   2303  1.1  christos 		 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
   2304  1.1  christos       break;
   2305  1.1  christos     case BINOP_LSH:
   2306  1.1  christos       gen_binop (ax, value, value1, value2,
   2307  1.1  christos 		 aop_lsh, aop_lsh, 1, "left shift");
   2308  1.1  christos       break;
   2309  1.1  christos     case BINOP_RSH:
   2310  1.1  christos       gen_binop (ax, value, value1, value2,
   2311  1.1  christos 		 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
   2312  1.1  christos       break;
   2313  1.1  christos     case BINOP_SUBSCRIPT:
   2314  1.1  christos       {
   2315  1.1  christos 	struct type *type;
   2316  1.1  christos 
   2317  1.1  christos 	if (binop_types_user_defined_p (op, value1->type, value2->type))
   2318  1.1  christos 	  {
   2319  1.1  christos 	    error (_("cannot subscript requested type: "
   2320  1.1  christos 		     "cannot call user defined functions"));
   2321  1.1  christos 	  }
   2322  1.1  christos 	else
   2323  1.1  christos 	  {
   2324  1.1  christos 	    /* If the user attempts to subscript something that is not
   2325  1.1  christos 	       an array or pointer type (like a plain int variable for
   2326  1.1  christos 	       example), then report this as an error.  */
   2327  1.1  christos 	    type = check_typedef (value1->type);
   2328  1.1  christos 	    if (TYPE_CODE (type) != TYPE_CODE_ARRAY
   2329  1.1  christos 		&& TYPE_CODE (type) != TYPE_CODE_PTR)
   2330  1.1  christos 	      {
   2331  1.1  christos 		if (TYPE_NAME (type))
   2332  1.1  christos 		  error (_("cannot subscript something of type `%s'"),
   2333  1.1  christos 			 TYPE_NAME (type));
   2334  1.1  christos 		else
   2335  1.1  christos 		  error (_("cannot subscript requested type"));
   2336  1.1  christos 	      }
   2337  1.1  christos 	  }
   2338  1.1  christos 
   2339  1.1  christos 	if (!is_integral_type (value2->type))
   2340  1.1  christos 	  error (_("Argument to arithmetic operation "
   2341  1.1  christos 		   "not a number or boolean."));
   2342  1.1  christos 
   2343  1.1  christos 	gen_ptradd (ax, value, value1, value2);
   2344  1.1  christos 	gen_deref (ax, value);
   2345  1.1  christos 	break;
   2346  1.1  christos       }
   2347  1.1  christos     case BINOP_BITWISE_AND:
   2348  1.1  christos       gen_binop (ax, value, value1, value2,
   2349  1.1  christos 		 aop_bit_and, aop_bit_and, 0, "bitwise and");
   2350  1.1  christos       break;
   2351  1.1  christos 
   2352  1.1  christos     case BINOP_BITWISE_IOR:
   2353  1.1  christos       gen_binop (ax, value, value1, value2,
   2354  1.1  christos 		 aop_bit_or, aop_bit_or, 0, "bitwise or");
   2355  1.1  christos       break;
   2356  1.1  christos 
   2357  1.1  christos     case BINOP_BITWISE_XOR:
   2358  1.1  christos       gen_binop (ax, value, value1, value2,
   2359  1.1  christos 		 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
   2360  1.1  christos       break;
   2361  1.1  christos 
   2362  1.1  christos     case BINOP_EQUAL:
   2363  1.1  christos       gen_equal (ax, value, value1, value2, int_type);
   2364  1.1  christos       break;
   2365  1.1  christos 
   2366  1.1  christos     case BINOP_NOTEQUAL:
   2367  1.1  christos       gen_equal (ax, value, value1, value2, int_type);
   2368  1.1  christos       gen_logical_not (ax, value, int_type);
   2369  1.1  christos       break;
   2370  1.1  christos 
   2371  1.1  christos     case BINOP_LESS:
   2372  1.1  christos       gen_less (ax, value, value1, value2, int_type);
   2373  1.1  christos       break;
   2374  1.1  christos 
   2375  1.1  christos     case BINOP_GTR:
   2376  1.1  christos       ax_simple (ax, aop_swap);
   2377  1.1  christos       gen_less (ax, value, value1, value2, int_type);
   2378  1.1  christos       break;
   2379  1.1  christos 
   2380  1.1  christos     case BINOP_LEQ:
   2381  1.1  christos       ax_simple (ax, aop_swap);
   2382  1.1  christos       gen_less (ax, value, value1, value2, int_type);
   2383  1.1  christos       gen_logical_not (ax, value, int_type);
   2384  1.1  christos       break;
   2385  1.1  christos 
   2386  1.1  christos     case BINOP_GEQ:
   2387  1.1  christos       gen_less (ax, value, value1, value2, int_type);
   2388  1.1  christos       gen_logical_not (ax, value, int_type);
   2389  1.1  christos       break;
   2390  1.1  christos 
   2391  1.1  christos     default:
   2392  1.1  christos       /* We should only list operators in the outer case statement
   2393  1.1  christos 	 that we actually handle in the inner case statement.  */
   2394  1.1  christos       internal_error (__FILE__, __LINE__,
   2395  1.1  christos 		      _("gen_expr: op case sets don't match"));
   2396  1.1  christos     }
   2397  1.1  christos }
   2398  1.1  christos 
   2399  1.1  christos 
   2401  1.1  christos /* Given a single variable and a scope, generate bytecodes to trace
   2402  1.1  christos    its value.  This is for use in situations where we have only a
   2403  1.1  christos    variable's name, and no parsed expression; for instance, when the
   2404  1.1  christos    name comes from a list of local variables of a function.  */
   2405  1.1  christos 
   2406  1.1  christos struct agent_expr *
   2407  1.1  christos gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
   2408  1.1  christos 		   struct symbol *var, int trace_string)
   2409  1.1  christos {
   2410  1.1  christos   struct cleanup *old_chain = 0;
   2411  1.1  christos   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
   2412  1.1  christos   struct axs_value value;
   2413  1.1  christos 
   2414  1.1  christos   old_chain = make_cleanup_free_agent_expr (ax);
   2415  1.1  christos 
   2416  1.1  christos   ax->tracing = 1;
   2417  1.1  christos   ax->trace_string = trace_string;
   2418  1.1  christos   gen_var_ref (gdbarch, ax, &value, var);
   2419  1.1  christos 
   2420  1.1  christos   /* If there is no actual variable to trace, flag it by returning
   2421  1.1  christos      an empty agent expression.  */
   2422  1.1  christos   if (value.optimized_out)
   2423  1.1  christos     {
   2424  1.1  christos       do_cleanups (old_chain);
   2425  1.1  christos       return NULL;
   2426  1.1  christos     }
   2427  1.1  christos 
   2428  1.1  christos   /* Make sure we record the final object, and get rid of it.  */
   2429  1.1  christos   gen_traced_pop (gdbarch, ax, &value);
   2430  1.1  christos 
   2431  1.1  christos   /* Oh, and terminate.  */
   2432  1.1  christos   ax_simple (ax, aop_end);
   2433  1.1  christos 
   2434  1.1  christos   /* We have successfully built the agent expr, so cancel the cleanup
   2435  1.1  christos      request.  If we add more cleanups that we always want done, this
   2436  1.1  christos      will have to get more complicated.  */
   2437  1.1  christos   discard_cleanups (old_chain);
   2438  1.1  christos   return ax;
   2439  1.1  christos }
   2440  1.1  christos 
   2441  1.1  christos /* Generating bytecode from GDB expressions: driver */
   2442  1.1  christos 
   2443  1.1  christos /* Given a GDB expression EXPR, return bytecode to trace its value.
   2444  1.1  christos    The result will use the `trace' and `trace_quick' bytecodes to
   2445  1.1  christos    record the value of all memory touched by the expression.  The
   2446  1.1  christos    caller can then use the ax_reqs function to discover which
   2447  1.1  christos    registers it relies upon.  */
   2448  1.1  christos struct agent_expr *
   2449  1.1  christos gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
   2450  1.1  christos 		    int trace_string)
   2451  1.1  christos {
   2452  1.1  christos   struct cleanup *old_chain = 0;
   2453  1.1  christos   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
   2454  1.1  christos   union exp_element *pc;
   2455  1.1  christos   struct axs_value value;
   2456  1.1  christos 
   2457  1.1  christos   old_chain = make_cleanup_free_agent_expr (ax);
   2458  1.1  christos 
   2459  1.1  christos   pc = expr->elts;
   2460  1.1  christos   ax->tracing = 1;
   2461  1.1  christos   ax->trace_string = trace_string;
   2462  1.1  christos   value.optimized_out = 0;
   2463  1.1  christos   gen_expr (expr, &pc, ax, &value);
   2464  1.1  christos 
   2465  1.1  christos   /* Make sure we record the final object, and get rid of it.  */
   2466  1.1  christos   gen_traced_pop (expr->gdbarch, ax, &value);
   2467  1.1  christos 
   2468  1.1  christos   /* Oh, and terminate.  */
   2469  1.1  christos   ax_simple (ax, aop_end);
   2470  1.1  christos 
   2471  1.1  christos   /* We have successfully built the agent expr, so cancel the cleanup
   2472  1.1  christos      request.  If we add more cleanups that we always want done, this
   2473  1.1  christos      will have to get more complicated.  */
   2474  1.1  christos   discard_cleanups (old_chain);
   2475  1.1  christos   return ax;
   2476  1.1  christos }
   2477  1.1  christos 
   2478  1.1  christos /* Given a GDB expression EXPR, return a bytecode sequence that will
   2479  1.1  christos    evaluate and return a result.  The bytecodes will do a direct
   2480  1.1  christos    evaluation, using the current data on the target, rather than
   2481  1.1  christos    recording blocks of memory and registers for later use, as
   2482  1.1  christos    gen_trace_for_expr does.  The generated bytecode sequence leaves
   2483  1.1  christos    the result of expression evaluation on the top of the stack.  */
   2484  1.1  christos 
   2485  1.1  christos struct agent_expr *
   2486  1.1  christos gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
   2487  1.1  christos {
   2488  1.1  christos   struct cleanup *old_chain = 0;
   2489  1.1  christos   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
   2490  1.1  christos   union exp_element *pc;
   2491  1.1  christos   struct axs_value value;
   2492  1.1  christos 
   2493  1.1  christos   old_chain = make_cleanup_free_agent_expr (ax);
   2494  1.1  christos 
   2495  1.1  christos   pc = expr->elts;
   2496  1.1  christos   ax->tracing = 0;
   2497  1.1  christos   value.optimized_out = 0;
   2498  1.1  christos   gen_expr (expr, &pc, ax, &value);
   2499  1.1  christos 
   2500  1.1  christos   require_rvalue (ax, &value);
   2501  1.1  christos 
   2502  1.1  christos   /* Oh, and terminate.  */
   2503  1.1  christos   ax_simple (ax, aop_end);
   2504  1.1  christos 
   2505  1.1  christos   /* We have successfully built the agent expr, so cancel the cleanup
   2506  1.1  christos      request.  If we add more cleanups that we always want done, this
   2507  1.1  christos      will have to get more complicated.  */
   2508  1.1  christos   discard_cleanups (old_chain);
   2509  1.1  christos   return ax;
   2510  1.1  christos }
   2511  1.1  christos 
   2512  1.1  christos struct agent_expr *
   2513  1.1  christos gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
   2514  1.1  christos 			      int trace_string)
   2515  1.1  christos {
   2516  1.1  christos   struct cleanup *old_chain = 0;
   2517  1.1  christos   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
   2518  1.1  christos   struct axs_value value;
   2519  1.1  christos 
   2520  1.1  christos   old_chain = make_cleanup_free_agent_expr (ax);
   2521  1.1  christos 
   2522  1.1  christos   ax->tracing = 1;
   2523  1.1  christos   ax->trace_string = trace_string;
   2524  1.1  christos 
   2525  1.1  christos   gdbarch_gen_return_address (gdbarch, ax, &value, scope);
   2526  1.1  christos 
   2527  1.1  christos   /* Make sure we record the final object, and get rid of it.  */
   2528  1.1  christos   gen_traced_pop (gdbarch, ax, &value);
   2529  1.1  christos 
   2530  1.1  christos   /* Oh, and terminate.  */
   2531  1.1  christos   ax_simple (ax, aop_end);
   2532  1.1  christos 
   2533  1.1  christos   /* We have successfully built the agent expr, so cancel the cleanup
   2534  1.1  christos      request.  If we add more cleanups that we always want done, this
   2535  1.1  christos      will have to get more complicated.  */
   2536  1.1  christos   discard_cleanups (old_chain);
   2537  1.1  christos   return ax;
   2538  1.1  christos }
   2539  1.1  christos 
   2540  1.1  christos /* Given a collection of printf-style arguments, generate code to
   2541  1.1  christos    evaluate the arguments and pass everything to a special
   2542  1.1  christos    bytecode.  */
   2543  1.1  christos 
   2544  1.1  christos struct agent_expr *
   2545  1.1  christos gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
   2546  1.1  christos 	    CORE_ADDR function, LONGEST channel,
   2547  1.1  christos 	    const char *format, int fmtlen,
   2548  1.1  christos 	    struct format_piece *frags,
   2549  1.1  christos 	    int nargs, struct expression **exprs)
   2550  1.1  christos {
   2551  1.1  christos   struct cleanup *old_chain = 0;
   2552  1.1  christos   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
   2553  1.1  christos   union exp_element *pc;
   2554  1.1  christos   struct axs_value value;
   2555  1.1  christos   int tem;
   2556  1.1  christos 
   2557  1.1  christos   old_chain = make_cleanup_free_agent_expr (ax);
   2558  1.1  christos 
   2559  1.1  christos   /* We're computing values, not doing side effects.  */
   2560  1.1  christos   ax->tracing = 0;
   2561  1.1  christos 
   2562  1.1  christos   /* Evaluate and push the args on the stack in reverse order,
   2563  1.1  christos      for simplicity of collecting them on the target side.  */
   2564  1.1  christos   for (tem = nargs - 1; tem >= 0; --tem)
   2565  1.1  christos     {
   2566  1.6  christos       pc = exprs[tem]->elts;
   2567  1.1  christos       value.optimized_out = 0;
   2568  1.1  christos       gen_expr (exprs[tem], &pc, ax, &value);
   2569  1.1  christos       require_rvalue (ax, &value);
   2570  1.1  christos     }
   2571  1.1  christos 
   2572  1.1  christos   /* Push function and channel.  */
   2573  1.1  christos   ax_const_l (ax, channel);
   2574  1.1  christos   ax_const_l (ax, function);
   2575  1.1  christos 
   2576  1.1  christos   /* Issue the printf bytecode proper.  */
   2577  1.1  christos   ax_simple (ax, aop_printf);
   2578  1.1  christos   ax_raw_byte (ax, nargs);
   2579  1.1  christos   ax_string (ax, format, fmtlen);
   2580  1.1  christos 
   2581  1.1  christos   /* And terminate.  */
   2582  1.1  christos   ax_simple (ax, aop_end);
   2583  1.1  christos 
   2584  1.1  christos   /* We have successfully built the agent expr, so cancel the cleanup
   2585  1.1  christos      request.  If we add more cleanups that we always want done, this
   2586  1.1  christos      will have to get more complicated.  */
   2587  1.1  christos   discard_cleanups (old_chain);
   2588  1.1  christos 
   2589  1.1  christos   return ax;
   2590  1.1  christos }
   2591  1.1  christos 
   2592  1.1  christos static void
   2593  1.1  christos agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
   2594  1.1  christos {
   2595  1.1  christos   struct cleanup *old_chain = 0;
   2596  1.1  christos   struct expression *expr;
   2597  1.1  christos   struct agent_expr *agent;
   2598  1.1  christos   const char *arg;
   2599  1.1  christos   int trace_string = 0;
   2600  1.1  christos 
   2601  1.1  christos   if (!eval)
   2602  1.1  christos     {
   2603  1.1  christos       if (*exp == '/')
   2604  1.1  christos         exp = decode_agent_options (exp, &trace_string);
   2605  1.1  christos     }
   2606  1.1  christos 
   2607  1.1  christos   arg = exp;
   2608  1.1  christos   if (!eval && strcmp (arg, "$_ret") == 0)
   2609  1.1  christos     {
   2610  1.1  christos       agent = gen_trace_for_return_address (pc, get_current_arch (),
   2611  1.1  christos 					    trace_string);
   2612  1.1  christos       old_chain = make_cleanup_free_agent_expr (agent);
   2613  1.1  christos     }
   2614  1.1  christos   else
   2615  1.1  christos     {
   2616  1.1  christos       expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
   2617  1.1  christos       old_chain = make_cleanup (free_current_contents, &expr);
   2618  1.1  christos       if (eval)
   2619  1.1  christos 	{
   2620  1.1  christos 	  gdb_assert (trace_string == 0);
   2621  1.1  christos 	  agent = gen_eval_for_expr (pc, expr);
   2622  1.1  christos 	}
   2623  1.1  christos       else
   2624  1.1  christos 	agent = gen_trace_for_expr (pc, expr, trace_string);
   2625  1.1  christos       make_cleanup_free_agent_expr (agent);
   2626  1.1  christos     }
   2627  1.1  christos 
   2628  1.1  christos   ax_reqs (agent);
   2629  1.1  christos   ax_print (gdb_stdout, agent);
   2630  1.1  christos 
   2631  1.1  christos   /* It would be nice to call ax_reqs here to gather some general info
   2632  1.1  christos      about the expression, and then print out the result.  */
   2633  1.1  christos 
   2634  1.1  christos   do_cleanups (old_chain);
   2635  1.1  christos   dont_repeat ();
   2636  1.1  christos }
   2637  1.1  christos 
   2638  1.1  christos static void
   2639  1.1  christos agent_command_1 (char *exp, int eval)
   2640  1.1  christos {
   2641  1.1  christos   /* We don't deal with overlay debugging at the moment.  We need to
   2642  1.1  christos      think more carefully about this.  If you copy this code into
   2643  1.1  christos      another command, change the error message; the user shouldn't
   2644  1.1  christos      have to know anything about agent expressions.  */
   2645  1.6  christos   if (overlay_debugging)
   2646  1.1  christos     error (_("GDB can't do agent expression translation with overlays."));
   2647  1.1  christos 
   2648  1.1  christos   if (exp == 0)
   2649  1.6  christos     error_no_arg (_("expression to translate"));
   2650  1.6  christos 
   2651  1.6  christos   if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
   2652  1.1  christos     {
   2653  1.1  christos       struct linespec_result canonical;
   2654  1.6  christos       int ix;
   2655  1.1  christos       struct linespec_sals *iter;
   2656  1.1  christos       struct cleanup *old_chain;
   2657  1.1  christos       struct event_location *location;
   2658  1.1  christos 
   2659  1.1  christos       exp = skip_spaces (exp);
   2660  1.1  christos       init_linespec_result (&canonical);
   2661  1.1  christos       location = new_linespec_location (&exp);
   2662  1.1  christos       old_chain = make_cleanup_delete_event_location (location);
   2663  1.1  christos       decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
   2664  1.1  christos 			(struct symtab *) NULL, 0, &canonical,
   2665  1.1  christos 			NULL, NULL);
   2666  1.1  christos       make_cleanup_destroy_linespec_result (&canonical);
   2667  1.1  christos       exp = skip_spaces (exp);
   2668  1.1  christos       if (exp[0] == ',')
   2669  1.1  christos         {
   2670  1.1  christos 	  exp++;
   2671  1.1  christos 	  exp = skip_spaces (exp);
   2672  1.1  christos 	}
   2673  1.1  christos       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
   2674  1.1  christos         {
   2675  1.1  christos 	  int i;
   2676  1.1  christos 
   2677  1.1  christos 	  for (i = 0; i < iter->sals.nelts; i++)
   2678  1.1  christos 	    agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
   2679  1.1  christos         }
   2680  1.1  christos       do_cleanups (old_chain);
   2681  1.1  christos     }
   2682  1.1  christos   else
   2683  1.1  christos     agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
   2684  1.1  christos 
   2685  1.1  christos   dont_repeat ();
   2686  1.1  christos }
   2687  1.1  christos 
   2688  1.1  christos static void
   2689  1.1  christos agent_command (char *exp, int from_tty)
   2690  1.1  christos {
   2691  1.1  christos   agent_command_1 (exp, 0);
   2692  1.1  christos }
   2693  1.1  christos 
   2694  1.1  christos /* Parse the given expression, compile it into an agent expression
   2695  1.1  christos    that does direct evaluation, and display the resulting
   2696  1.1  christos    expression.  */
   2697  1.1  christos 
   2698  1.1  christos static void
   2699  1.1  christos agent_eval_command (char *exp, int from_tty)
   2700  1.1  christos {
   2701  1.1  christos   agent_command_1 (exp, 1);
   2702  1.1  christos }
   2703  1.1  christos 
   2704  1.1  christos /* Parse the given expression, compile it into an agent expression
   2705  1.1  christos    that does a printf, and display the resulting expression.  */
   2706  1.1  christos 
   2707  1.1  christos static void
   2708  1.1  christos maint_agent_printf_command (char *exp, int from_tty)
   2709  1.1  christos {
   2710  1.1  christos   struct cleanup *old_chain = 0;
   2711  1.1  christos   struct expression *expr;
   2712  1.1  christos   struct expression *argvec[100];
   2713  1.1  christos   struct agent_expr *agent;
   2714  1.1  christos   struct frame_info *fi = get_current_frame ();	/* need current scope */
   2715  1.1  christos   const char *cmdrest;
   2716  1.1  christos   const char *format_start, *format_end;
   2717  1.1  christos   struct format_piece *fpieces;
   2718  1.1  christos   int nargs;
   2719  1.1  christos 
   2720  1.1  christos   /* We don't deal with overlay debugging at the moment.  We need to
   2721  1.1  christos      think more carefully about this.  If you copy this code into
   2722  1.1  christos      another command, change the error message; the user shouldn't
   2723  1.1  christos      have to know anything about agent expressions.  */
   2724  1.1  christos   if (overlay_debugging)
   2725  1.1  christos     error (_("GDB can't do agent expression translation with overlays."));
   2726  1.1  christos 
   2727  1.1  christos   if (exp == 0)
   2728  1.1  christos     error_no_arg (_("expression to translate"));
   2729  1.1  christos 
   2730  1.1  christos   cmdrest = exp;
   2731  1.1  christos 
   2732  1.1  christos   cmdrest = skip_spaces_const (cmdrest);
   2733  1.1  christos 
   2734  1.1  christos   if (*cmdrest++ != '"')
   2735  1.1  christos     error (_("Must start with a format string."));
   2736  1.1  christos 
   2737  1.1  christos   format_start = cmdrest;
   2738  1.1  christos 
   2739  1.1  christos   fpieces = parse_format_string (&cmdrest);
   2740  1.1  christos 
   2741  1.1  christos   old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);
   2742  1.1  christos 
   2743  1.1  christos   format_end = cmdrest;
   2744  1.1  christos 
   2745  1.1  christos   if (*cmdrest++ != '"')
   2746  1.1  christos     error (_("Bad format string, non-terminated '\"'."));
   2747  1.1  christos 
   2748  1.1  christos   cmdrest = skip_spaces_const (cmdrest);
   2749  1.1  christos 
   2750  1.1  christos   if (*cmdrest != ',' && *cmdrest != 0)
   2751  1.1  christos     error (_("Invalid argument syntax"));
   2752  1.1  christos 
   2753  1.1  christos   if (*cmdrest == ',')
   2754  1.1  christos     cmdrest++;
   2755  1.1  christos   cmdrest = skip_spaces_const (cmdrest);
   2756  1.1  christos 
   2757  1.1  christos   nargs = 0;
   2758  1.1  christos   while (*cmdrest != '\0')
   2759  1.1  christos     {
   2760  1.1  christos       const char *cmd1;
   2761  1.1  christos 
   2762  1.1  christos       cmd1 = cmdrest;
   2763  1.1  christos       expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
   2764  1.1  christos       argvec[nargs] = expr;
   2765  1.1  christos       ++nargs;
   2766  1.1  christos       cmdrest = cmd1;
   2767  1.1  christos       if (*cmdrest == ',')
   2768  1.1  christos 	++cmdrest;
   2769  1.1  christos       /* else complain? */
   2770  1.1  christos     }
   2771  1.1  christos 
   2772  1.1  christos 
   2773  1.1  christos   agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
   2774  1.1  christos 		      format_start, format_end - format_start,
   2775  1.1  christos 		      fpieces, nargs, argvec);
   2776  1.1  christos   make_cleanup_free_agent_expr (agent);
   2777  1.1  christos   ax_reqs (agent);
   2778  1.1  christos   ax_print (gdb_stdout, agent);
   2779  1.1  christos 
   2780  1.1  christos   /* It would be nice to call ax_reqs here to gather some general info
   2781  1.1  christos      about the expression, and then print out the result.  */
   2782  1.1  christos 
   2783  1.1  christos   do_cleanups (old_chain);
   2784  1.1  christos   dont_repeat ();
   2785  1.1  christos }
   2786  1.1  christos 
   2787  1.1  christos 
   2789  1.1  christos /* Initialization code.  */
   2790  1.1  christos 
   2791  1.1  christos void _initialize_ax_gdb (void);
   2792  1.1  christos void
   2793  1.1  christos _initialize_ax_gdb (void)
   2794  1.1  christos {
   2795  1.1  christos   add_cmd ("agent", class_maintenance, agent_command,
   2796  1.1  christos 	   _("\
   2797  1.1  christos Translate an expression into remote agent bytecode for tracing.\n\
   2798  1.1  christos Usage: maint agent [-at location,] EXPRESSION\n\
   2799  1.1  christos If -at is given, generate remote agent bytecode for this location.\n\
   2800  1.1  christos If not, generate remote agent bytecode for current frame pc address."),
   2801  1.1  christos 	   &maintenancelist);
   2802  1.1  christos 
   2803                  add_cmd ("agent-eval", class_maintenance, agent_eval_command,
   2804                	   _("\
   2805                Translate an expression into remote agent bytecode for evaluation.\n\
   2806                Usage: maint agent-eval [-at location,] EXPRESSION\n\
   2807                If -at is given, generate remote agent bytecode for this location.\n\
   2808                If not, generate remote agent bytecode for current frame pc address."),
   2809                	   &maintenancelist);
   2810                
   2811                  add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
   2812                	   _("Translate an expression into remote "
   2813                	     "agent bytecode for evaluation and display the bytecodes."),
   2814                	   &maintenancelist);
   2815                }
   2816