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