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