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