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