gdb/ax-gdb.c - gdb

Functions defined

Source code

  1. /* GDB-specific functions for operating on agent expressions.

  2.    Copyright (C) 1998-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "symfile.h"
  17. #include "gdbtypes.h"
  18. #include "language.h"
  19. #include "value.h"
  20. #include "expression.h"
  21. #include "command.h"
  22. #include "gdbcmd.h"
  23. #include "frame.h"
  24. #include "target.h"
  25. #include "ax.h"
  26. #include "ax-gdb.h"
  27. #include "block.h"
  28. #include "regcache.h"
  29. #include "user-regs.h"
  30. #include "dictionary.h"
  31. #include "breakpoint.h"
  32. #include "tracepoint.h"
  33. #include "cp-support.h"
  34. #include "arch-utils.h"
  35. #include "cli/cli-utils.h"
  36. #include "linespec.h"
  37. #include "objfiles.h"

  38. #include "valprint.h"
  39. #include "c-lang.h"

  40. #include "format.h"

  41. /* To make sense of this file, you should read doc/agentexpr.texi.
  42.    Then look at the types and enums in ax-gdb.h.  For the code itself,
  43.    look at gen_expr, towards the bottom; that's the main function that
  44.    looks at the GDB expressions and calls everything else to generate
  45.    code.

  46.    I'm beginning to wonder whether it wouldn't be nicer to internally
  47.    generate trees, with types, and then spit out the bytecode in
  48.    linear form afterwards; we could generate fewer `swap', `ext', and
  49.    `zero_ext' bytecodes that way; it would make good constant folding
  50.    easier, too.  But at the moment, I think we should be willing to
  51.    pay for the simplicity of this code with less-than-optimal bytecode
  52.    strings.

  53.    Remember, "GBD" stands for "Great Britain, Dammit!"  So be careful.  */



  54. /* Prototypes for local functions.  */

  55. /* There's a standard order to the arguments of these functions:
  56.    union exp_element ** --- pointer into expression
  57.    struct agent_expr * --- agent expression buffer to generate code into
  58.    struct axs_value * --- describes value left on top of stack  */

  59. static struct value *const_var_ref (struct symbol *var);
  60. static struct value *const_expr (union exp_element **pc);
  61. static struct value *maybe_const_expr (union exp_element **pc);

  62. static void gen_traced_pop (struct gdbarch *, struct agent_expr *,
  63.                             struct axs_value *);

  64. static void gen_sign_extend (struct agent_expr *, struct type *);
  65. static void gen_extend (struct agent_expr *, struct type *);
  66. static void gen_fetch (struct agent_expr *, struct type *);
  67. static void gen_left_shift (struct agent_expr *, int);


  68. static void gen_frame_args_address (struct gdbarch *, struct agent_expr *);
  69. static void gen_frame_locals_address (struct gdbarch *, struct agent_expr *);
  70. static void gen_offset (struct agent_expr *ax, int offset);
  71. static void gen_sym_offset (struct agent_expr *, struct symbol *);
  72. static void gen_var_ref (struct gdbarch *, struct agent_expr *ax,
  73.                          struct axs_value *value, struct symbol *var);


  74. static void gen_int_literal (struct agent_expr *ax,
  75.                              struct axs_value *value,
  76.                              LONGEST k, struct type *type);

  77. static void gen_usual_unary (struct expression *exp, struct agent_expr *ax,
  78.                              struct axs_value *value);
  79. static int type_wider_than (struct type *type1, struct type *type2);
  80. static struct type *max_type (struct type *type1, struct type *type2);
  81. static void gen_conversion (struct agent_expr *ax,
  82.                             struct type *from, struct type *to);
  83. static int is_nontrivial_conversion (struct type *from, struct type *to);
  84. static void gen_usual_arithmetic (struct expression *exp,
  85.                                   struct agent_expr *ax,
  86.                                   struct axs_value *value1,
  87.                                   struct axs_value *value2);
  88. static void gen_integral_promotions (struct expression *exp,
  89.                                      struct agent_expr *ax,
  90.                                      struct axs_value *value);
  91. static void gen_cast (struct agent_expr *ax,
  92.                       struct axs_value *value, struct type *type);
  93. static void gen_scale (struct agent_expr *ax,
  94.                        enum agent_op op, struct type *type);
  95. static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
  96.                         struct axs_value *value1, struct axs_value *value2);
  97. static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
  98.                         struct axs_value *value1, struct axs_value *value2);
  99. static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
  100.                          struct axs_value *value1, struct axs_value *value2,
  101.                          struct type *result_type);
  102. static void gen_binop (struct agent_expr *ax,
  103.                        struct axs_value *value,
  104.                        struct axs_value *value1,
  105.                        struct axs_value *value2,
  106.                        enum agent_op op,
  107.                        enum agent_op op_unsigned, int may_carry, char *name);
  108. static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
  109.                              struct type *result_type);
  110. static void gen_complement (struct agent_expr *ax, struct axs_value *value);
  111. static void gen_deref (struct agent_expr *, struct axs_value *);
  112. static void gen_address_of (struct agent_expr *, struct axs_value *);
  113. static void gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
  114.                               struct axs_value *value,
  115.                               struct type *type, int start, int end);
  116. static void gen_primitive_field (struct expression *exp,
  117.                                  struct agent_expr *ax,
  118.                                  struct axs_value *value,
  119.                                  int offset, int fieldno, struct type *type);
  120. static int gen_struct_ref_recursive (struct expression *exp,
  121.                                      struct agent_expr *ax,
  122.                                      struct axs_value *value,
  123.                                      char *field, int offset,
  124.                                      struct type *type);
  125. static void gen_struct_ref (struct expression *exp, struct agent_expr *ax,
  126.                             struct axs_value *value,
  127.                             char *field,
  128.                             char *operator_name, char *operand_name);
  129. static void gen_static_field (struct gdbarch *gdbarch,
  130.                               struct agent_expr *ax, struct axs_value *value,
  131.                               struct type *type, int fieldno);
  132. static void gen_repeat (struct expression *exp, union exp_element **pc,
  133.                         struct agent_expr *ax, struct axs_value *value);
  134. static void gen_sizeof (struct expression *exp, union exp_element **pc,
  135.                         struct agent_expr *ax, struct axs_value *value,
  136.                         struct type *size_type);
  137. static void gen_expr_binop_rest (struct expression *exp,
  138.                                  enum exp_opcode op, union exp_element **pc,
  139.                                  struct agent_expr *ax,
  140.                                  struct axs_value *value,
  141.                                  struct axs_value *value1,
  142.                                  struct axs_value *value2);

  143. static void agent_command (char *exp, int from_tty);


  144. /* Detecting constant expressions.  */

  145. /* If the variable reference at *PC is a constant, return its value.
  146.    Otherwise, return zero.

  147.    Hey, Wally!  How can a variable reference be a constant?

  148.    Well, Beav, this function really handles the OP_VAR_VALUE operator,
  149.    not specifically variable references.  GDB uses OP_VAR_VALUE to
  150.    refer to any kind of symbolic reference: function names, enum
  151.    elements, and goto labels are all handled through the OP_VAR_VALUE
  152.    operator, even though they're constants.  It makes sense given the
  153.    situation.

  154.    Gee, Wally, don'cha wonder sometimes if data representations that
  155.    subvert commonly accepted definitions of terms in favor of heavily
  156.    context-specific interpretations are really just a tool of the
  157.    programming hegemony to preserve their power and exclude the
  158.    proletariat?  */

  159. static struct value *
  160. const_var_ref (struct symbol *var)
  161. {
  162.   struct type *type = SYMBOL_TYPE (var);

  163.   switch (SYMBOL_CLASS (var))
  164.     {
  165.     case LOC_CONST:
  166.       return value_from_longest (type, (LONGEST) SYMBOL_VALUE (var));

  167.     case LOC_LABEL:
  168.       return value_from_pointer (type, (CORE_ADDR) SYMBOL_VALUE_ADDRESS (var));

  169.     default:
  170.       return 0;
  171.     }
  172. }


  173. /* If the expression starting at *PC has a constant value, return it.
  174.    Otherwise, return zero.  If we return a value, then *PC will be
  175.    advanced to the end of it.  If we return zero, *PC could be
  176.    anywhere.  */
  177. static struct value *
  178. const_expr (union exp_element **pc)
  179. {
  180.   enum exp_opcode op = (*pc)->opcode;
  181.   struct value *v1;

  182.   switch (op)
  183.     {
  184.     case OP_LONG:
  185.       {
  186.         struct type *type = (*pc)[1].type;
  187.         LONGEST k = (*pc)[2].longconst;

  188.         (*pc) += 4;
  189.         return value_from_longest (type, k);
  190.       }

  191.     case OP_VAR_VALUE:
  192.       {
  193.         struct value *v = const_var_ref ((*pc)[2].symbol);

  194.         (*pc) += 4;
  195.         return v;
  196.       }

  197.       /* We could add more operators in here.  */

  198.     case UNOP_NEG:
  199.       (*pc)++;
  200.       v1 = const_expr (pc);
  201.       if (v1)
  202.         return value_neg (v1);
  203.       else
  204.         return 0;

  205.     default:
  206.       return 0;
  207.     }
  208. }


  209. /* Like const_expr, but guarantee also that *PC is undisturbed if the
  210.    expression is not constant.  */
  211. static struct value *
  212. maybe_const_expr (union exp_element **pc)
  213. {
  214.   union exp_element *tentative_pc = *pc;
  215.   struct value *v = const_expr (&tentative_pc);

  216.   /* If we got a value, then update the real PC.  */
  217.   if (v)
  218.     *pc = tentative_pc;

  219.   return v;
  220. }


  221. /* Generating bytecode from GDB expressions: general assumptions */

  222. /* Here are a few general assumptions made throughout the code; if you
  223.    want to make a change that contradicts one of these, then you'd
  224.    better scan things pretty thoroughly.

  225.    - We assume that all values occupy one stack element.  For example,
  226.    sometimes we'll swap to get at the left argument to a binary
  227.    operator.  If we decide that void values should occupy no stack
  228.    elements, or that synthetic arrays (whose size is determined at
  229.    run time, created by the `@' operator) should occupy two stack
  230.    elements (address and length), then this will cause trouble.

  231.    - We assume the stack elements are infinitely wide, and that we
  232.    don't have to worry what happens if the user requests an
  233.    operation that is wider than the actual interpreter's stack.
  234.    That is, it's up to the interpreter to handle directly all the
  235.    integer widths the user has access to.  (Woe betide the language
  236.    with bignums!)

  237.    - We don't support side effects.  Thus, we don't have to worry about
  238.    GCC's generalized lvalues, function calls, etc.

  239.    - We don't support floating point.  Many places where we switch on
  240.    some type don't bother to include cases for floating point; there
  241.    may be even more subtle ways this assumption exists.  For
  242.    example, the arguments to % must be integers.

  243.    - We assume all subexpressions have a static, unchanging type.  If
  244.    we tried to support convenience variables, this would be a
  245.    problem.

  246.    - All values on the stack should always be fully zero- or
  247.    sign-extended.

  248.    (I wasn't sure whether to choose this or its opposite --- that
  249.    only addresses are assumed extended --- but it turns out that
  250.    neither convention completely eliminates spurious extend
  251.    operations (if everything is always extended, then you have to
  252.    extend after add, because it could overflow; if nothing is
  253.    extended, then you end up producing extends whenever you change
  254.    sizes), and this is simpler.)  */


  255. /* Scan for all static fields in the given class, including any base
  256.    classes, and generate tracing bytecodes for each.  */

  257. static void
  258. gen_trace_static_fields (struct gdbarch *gdbarch,
  259.                          struct agent_expr *ax,
  260.                          struct type *type)
  261. {
  262.   int i, nbases = TYPE_N_BASECLASSES (type);
  263.   struct axs_value value;

  264.   CHECK_TYPEDEF (type);

  265.   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
  266.     {
  267.       if (field_is_static (&TYPE_FIELD (type, i)))
  268.         {
  269.           gen_static_field (gdbarch, ax, &value, type, i);
  270.           if (value.optimized_out)
  271.             continue;
  272.           switch (value.kind)
  273.             {
  274.             case axs_lvalue_memory:
  275.               {
  276.                 /* Initialize the TYPE_LENGTH if it is a typedef.  */
  277.                 check_typedef (value.type);
  278.                 ax_const_l (ax, TYPE_LENGTH (value.type));
  279.                 ax_simple (ax, aop_trace);
  280.               }
  281.               break;

  282.             case axs_lvalue_register:
  283.               /* We don't actually need the register's value to be pushed,
  284.                  just note that we need it to be collected.  */
  285.               ax_reg_mask (ax, value.u.reg);

  286.             default:
  287.               break;
  288.             }
  289.         }
  290.     }

  291.   /* Now scan through base classes recursively.  */
  292.   for (i = 0; i < nbases; i++)
  293.     {
  294.       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));

  295.       gen_trace_static_fields (gdbarch, ax, basetype);
  296.     }
  297. }

  298. /* Trace the lvalue on the stack, if it needs it.  In either case, pop
  299.    the value.  Useful on the left side of a comma, and at the end of
  300.    an expression being used for tracing.  */
  301. static void
  302. gen_traced_pop (struct gdbarch *gdbarch,
  303.                 struct agent_expr *ax, struct axs_value *value)
  304. {
  305.   int string_trace = 0;
  306.   if (ax->trace_string
  307.       && TYPE_CODE (value->type) == TYPE_CODE_PTR
  308.       && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
  309.                                  's'))
  310.     string_trace = 1;

  311.   if (ax->tracing)
  312.     switch (value->kind)
  313.       {
  314.       case axs_rvalue:
  315.         if (string_trace)
  316.           {
  317.             ax_const_l (ax, ax->trace_string);
  318.             ax_simple (ax, aop_tracenz);
  319.           }
  320.         else
  321.           /* We don't trace rvalues, just the lvalues necessary to
  322.              produce them.  So just dispose of this value.  */
  323.           ax_simple (ax, aop_pop);
  324.         break;

  325.       case axs_lvalue_memory:
  326.         {
  327.           if (string_trace)
  328.             ax_simple (ax, aop_dup);

  329.           /* Initialize the TYPE_LENGTH if it is a typedef.  */
  330.           check_typedef (value->type);

  331.           /* There's no point in trying to use a trace_quick bytecode
  332.              here, since "trace_quick SIZE pop" is three bytes, whereas
  333.              "const8 SIZE trace" is also three bytes, does the same
  334.              thing, and the simplest code which generates that will also
  335.              work correctly for objects with large sizes.  */
  336.           ax_const_l (ax, TYPE_LENGTH (value->type));
  337.           ax_simple (ax, aop_trace);

  338.           if (string_trace)
  339.             {
  340.               ax_simple (ax, aop_ref32);
  341.               ax_const_l (ax, ax->trace_string);
  342.               ax_simple (ax, aop_tracenz);
  343.             }
  344.         }
  345.         break;

  346.       case axs_lvalue_register:
  347.         /* We don't actually need the register's value to be on the
  348.            stack, and the target will get heartburn if the register is
  349.            larger than will fit in a stack, so just mark it for
  350.            collection and be done with it.  */
  351.         ax_reg_mask (ax, value->u.reg);

  352.         /* But if the register points to a string, assume the value
  353.            will fit on the stack and push it anyway.  */
  354.         if (string_trace)
  355.           {
  356.             ax_reg (ax, value->u.reg);
  357.             ax_const_l (ax, ax->trace_string);
  358.             ax_simple (ax, aop_tracenz);
  359.           }
  360.         break;
  361.       }
  362.   else
  363.     /* If we're not tracing, just pop the value.  */
  364.     ax_simple (ax, aop_pop);

  365.   /* To trace C++ classes with static fields stored elsewhere.  */
  366.   if (ax->tracing
  367.       && (TYPE_CODE (value->type) == TYPE_CODE_STRUCT
  368.           || TYPE_CODE (value->type) == TYPE_CODE_UNION))
  369.     gen_trace_static_fields (gdbarch, ax, value->type);
  370. }



  371. /* Generating bytecode from GDB expressions: helper functions */

  372. /* Assume that the lower bits of the top of the stack is a value of
  373.    type TYPE, and the upper bits are zero.  Sign-extend if necessary.  */
  374. static void
  375. gen_sign_extend (struct agent_expr *ax, struct type *type)
  376. {
  377.   /* Do we need to sign-extend this?  */
  378.   if (!TYPE_UNSIGNED (type))
  379.     ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
  380. }


  381. /* Assume the lower bits of the top of the stack hold a value of type
  382.    TYPE, and the upper bits are garbage.  Sign-extend or truncate as
  383.    needed.  */
  384. static void
  385. gen_extend (struct agent_expr *ax, struct type *type)
  386. {
  387.   int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;

  388.   /* I just had to.  */
  389.   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, bits));
  390. }


  391. /* Assume that the top of the stack contains a value of type "pointer
  392.    to TYPE"; generate code to fetch its value.  Note that TYPE is the
  393.    target type, not the pointer type.  */
  394. static void
  395. gen_fetch (struct agent_expr *ax, struct type *type)
  396. {
  397.   if (ax->tracing)
  398.     {
  399.       /* Record the area of memory we're about to fetch.  */
  400.       ax_trace_quick (ax, TYPE_LENGTH (type));
  401.     }

  402.   if (TYPE_CODE (type) == TYPE_CODE_RANGE)
  403.     type = TYPE_TARGET_TYPE (type);

  404.   switch (TYPE_CODE (type))
  405.     {
  406.     case TYPE_CODE_PTR:
  407.     case TYPE_CODE_REF:
  408.     case TYPE_CODE_ENUM:
  409.     case TYPE_CODE_INT:
  410.     case TYPE_CODE_CHAR:
  411.     case TYPE_CODE_BOOL:
  412.       /* It's a scalar value, so we know how to dereference it.  How
  413.          many bytes long is it?  */
  414.       switch (TYPE_LENGTH (type))
  415.         {
  416.         case 8 / TARGET_CHAR_BIT:
  417.           ax_simple (ax, aop_ref8);
  418.           break;
  419.         case 16 / TARGET_CHAR_BIT:
  420.           ax_simple (ax, aop_ref16);
  421.           break;
  422.         case 32 / TARGET_CHAR_BIT:
  423.           ax_simple (ax, aop_ref32);
  424.           break;
  425.         case 64 / TARGET_CHAR_BIT:
  426.           ax_simple (ax, aop_ref64);
  427.           break;

  428.           /* Either our caller shouldn't have asked us to dereference
  429.              that pointer (other code's fault), or we're not
  430.              implementing something we should be (this code's fault).
  431.              In any case, it's a bug the user shouldn't see.  */
  432.         default:
  433.           internal_error (__FILE__, __LINE__,
  434.                           _("gen_fetch: strange size"));
  435.         }

  436.       gen_sign_extend (ax, type);
  437.       break;

  438.     default:
  439.       /* Our caller requested us to dereference a pointer from an unsupported
  440.          type.  Error out and give callers a chance to handle the failure
  441.          gracefully.  */
  442.       error (_("gen_fetch: Unsupported type code `%s'."),
  443.              TYPE_NAME (type));
  444.     }
  445. }


  446. /* Generate code to left shift the top of the stack by DISTANCE bits, or
  447.    right shift it by -DISTANCE bits if DISTANCE < 0.  This generates
  448.    unsigned (logical) right shifts.  */
  449. static void
  450. gen_left_shift (struct agent_expr *ax, int distance)
  451. {
  452.   if (distance > 0)
  453.     {
  454.       ax_const_l (ax, distance);
  455.       ax_simple (ax, aop_lsh);
  456.     }
  457.   else if (distance < 0)
  458.     {
  459.       ax_const_l (ax, -distance);
  460.       ax_simple (ax, aop_rsh_unsigned);
  461.     }
  462. }



  463. /* Generating bytecode from GDB expressions: symbol references */

  464. /* Generate code to push the base address of the argument portion of
  465.    the top stack frame.  */
  466. static void
  467. gen_frame_args_address (struct gdbarch *gdbarch, struct agent_expr *ax)
  468. {
  469.   int frame_reg;
  470.   LONGEST frame_offset;

  471.   gdbarch_virtual_frame_pointer (gdbarch,
  472.                                  ax->scope, &frame_reg, &frame_offset);
  473.   ax_reg (ax, frame_reg);
  474.   gen_offset (ax, frame_offset);
  475. }


  476. /* Generate code to push the base address of the locals portion of the
  477.    top stack frame.  */
  478. static void
  479. gen_frame_locals_address (struct gdbarch *gdbarch, struct agent_expr *ax)
  480. {
  481.   int frame_reg;
  482.   LONGEST frame_offset;

  483.   gdbarch_virtual_frame_pointer (gdbarch,
  484.                                  ax->scope, &frame_reg, &frame_offset);
  485.   ax_reg (ax, frame_reg);
  486.   gen_offset (ax, frame_offset);
  487. }


  488. /* Generate code to add OFFSET to the top of the stack.  Try to
  489.    generate short and readable code.  We use this for getting to
  490.    variables on the stack, and structure members.  If we were
  491.    programming in ML, it would be clearer why these are the same
  492.    thing.  */
  493. static void
  494. gen_offset (struct agent_expr *ax, int offset)
  495. {
  496.   /* It would suffice to simply push the offset and add it, but this
  497.      makes it easier to read positive and negative offsets in the
  498.      bytecode.  */
  499.   if (offset > 0)
  500.     {
  501.       ax_const_l (ax, offset);
  502.       ax_simple (ax, aop_add);
  503.     }
  504.   else if (offset < 0)
  505.     {
  506.       ax_const_l (ax, -offset);
  507.       ax_simple (ax, aop_sub);
  508.     }
  509. }


  510. /* In many cases, a symbol's value is the offset from some other
  511.    address (stack frame, base register, etc.)  Generate code to add
  512.    VAR's value to the top of the stack.  */
  513. static void
  514. gen_sym_offset (struct agent_expr *ax, struct symbol *var)
  515. {
  516.   gen_offset (ax, SYMBOL_VALUE (var));
  517. }


  518. /* Generate code for a variable reference to AX.  The variable is the
  519.    symbol VAR.  Set VALUE to describe the result.  */

  520. static void
  521. gen_var_ref (struct gdbarch *gdbarch, struct agent_expr *ax,
  522.              struct axs_value *value, struct symbol *var)
  523. {
  524.   /* Dereference any typedefs.  */
  525.   value->type = check_typedef (SYMBOL_TYPE (var));
  526.   value->optimized_out = 0;

  527.   if (SYMBOL_COMPUTED_OPS (var) != NULL)
  528.     {
  529.       SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, gdbarch, ax, value);
  530.       return;
  531.     }

  532.   /* I'm imitating the code in read_var_value.  */
  533.   switch (SYMBOL_CLASS (var))
  534.     {
  535.     case LOC_CONST:                /* A constant, like an enum value.  */
  536.       ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
  537.       value->kind = axs_rvalue;
  538.       break;

  539.     case LOC_LABEL:                /* A goto label, being used as a value.  */
  540.       ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
  541.       value->kind = axs_rvalue;
  542.       break;

  543.     case LOC_CONST_BYTES:
  544.       internal_error (__FILE__, __LINE__,
  545.                       _("gen_var_ref: LOC_CONST_BYTES "
  546.                         "symbols are not supported"));

  547.       /* Variable at a fixed location in memory.  Easy.  */
  548.     case LOC_STATIC:
  549.       /* Push the address of the variable.  */
  550.       ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
  551.       value->kind = axs_lvalue_memory;
  552.       break;

  553.     case LOC_ARG:                /* var lives in argument area of frame */
  554.       gen_frame_args_address (gdbarch, ax);
  555.       gen_sym_offset (ax, var);
  556.       value->kind = axs_lvalue_memory;
  557.       break;

  558.     case LOC_REF_ARG:                /* As above, but the frame slot really
  559.                                    holds the address of the variable.  */
  560.       gen_frame_args_address (gdbarch, ax);
  561.       gen_sym_offset (ax, var);
  562.       /* Don't assume any particular pointer size.  */
  563.       gen_fetch (ax, builtin_type (gdbarch)->builtin_data_ptr);
  564.       value->kind = axs_lvalue_memory;
  565.       break;

  566.     case LOC_LOCAL:                /* var lives in locals area of frame */
  567.       gen_frame_locals_address (gdbarch, ax);
  568.       gen_sym_offset (ax, var);
  569.       value->kind = axs_lvalue_memory;
  570.       break;

  571.     case LOC_TYPEDEF:
  572.       error (_("Cannot compute value of typedef `%s'."),
  573.              SYMBOL_PRINT_NAME (var));
  574.       break;

  575.     case LOC_BLOCK:
  576.       ax_const_l (ax, BLOCK_START (SYMBOL_BLOCK_VALUE (var)));
  577.       value->kind = axs_rvalue;
  578.       break;

  579.     case LOC_REGISTER:
  580.       /* Don't generate any code at all; in the process of treating
  581.          this as an lvalue or rvalue, the caller will generate the
  582.          right code.  */
  583.       value->kind = axs_lvalue_register;
  584.       value->u.reg = SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch);
  585.       break;

  586.       /* A lot like LOC_REF_ARG, but the pointer lives directly in a
  587.          register, not on the stack.  Simpler than LOC_REGISTER
  588.          because it's just like any other case where the thing
  589.          has a real address.  */
  590.     case LOC_REGPARM_ADDR:
  591.       ax_reg (ax, SYMBOL_REGISTER_OPS (var)->register_number (var, gdbarch));
  592.       value->kind = axs_lvalue_memory;
  593.       break;

  594.     case LOC_UNRESOLVED:
  595.       {
  596.         struct bound_minimal_symbol msym
  597.           = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);

  598.         if (!msym.minsym)
  599.           error (_("Couldn't resolve symbol `%s'."), SYMBOL_PRINT_NAME (var));

  600.         /* Push the address of the variable.  */
  601.         ax_const_l (ax, BMSYMBOL_VALUE_ADDRESS (msym));
  602.         value->kind = axs_lvalue_memory;
  603.       }
  604.       break;

  605.     case LOC_COMPUTED:
  606.       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));

  607.     case LOC_OPTIMIZED_OUT:
  608.       /* Flag this, but don't say anything; leave it up to callers to
  609.          warn the user.  */
  610.       value->optimized_out = 1;
  611.       break;

  612.     default:
  613.       error (_("Cannot find value of botched symbol `%s'."),
  614.              SYMBOL_PRINT_NAME (var));
  615.       break;
  616.     }
  617. }



  618. /* Generating bytecode from GDB expressions: literals */

  619. static void
  620. gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
  621.                  struct type *type)
  622. {
  623.   ax_const_l (ax, k);
  624.   value->kind = axs_rvalue;
  625.   value->type = check_typedef (type);
  626. }



  627. /* Generating bytecode from GDB expressions: unary conversions, casts */

  628. /* Take what's on the top of the stack (as described by VALUE), and
  629.    try to make an rvalue out of it.  Signal an error if we can't do
  630.    that.  */
  631. void
  632. require_rvalue (struct agent_expr *ax, struct axs_value *value)
  633. {
  634.   /* Only deal with scalars, structs and such may be too large
  635.      to fit in a stack entry.  */
  636.   value->type = check_typedef (value->type);
  637.   if (TYPE_CODE (value->type) == TYPE_CODE_ARRAY
  638.       || TYPE_CODE (value->type) == TYPE_CODE_STRUCT
  639.       || TYPE_CODE (value->type) == TYPE_CODE_UNION
  640.       || TYPE_CODE (value->type) == TYPE_CODE_FUNC)
  641.     error (_("Value not scalar: cannot be an rvalue."));

  642.   switch (value->kind)
  643.     {
  644.     case axs_rvalue:
  645.       /* It's already an rvalue.  */
  646.       break;

  647.     case axs_lvalue_memory:
  648.       /* The top of stack is the address of the object.  Dereference.  */
  649.       gen_fetch (ax, value->type);
  650.       break;

  651.     case axs_lvalue_register:
  652.       /* There's nothing on the stack, but value->u.reg is the
  653.          register number containing the value.

  654.          When we add floating-point support, this is going to have to
  655.          change.  What about SPARC register pairs, for example?  */
  656.       ax_reg (ax, value->u.reg);
  657.       gen_extend (ax, value->type);
  658.       break;
  659.     }

  660.   value->kind = axs_rvalue;
  661. }


  662. /* Assume the top of the stack is described by VALUE, and perform the
  663.    usual unary conversions.  This is motivated by ANSI 6.2.2, but of
  664.    course GDB expressions are not ANSI; they're the mishmash union of
  665.    a bunch of languages.  Rah.

  666.    NOTE!  This function promises to produce an rvalue only when the
  667.    incoming value is of an appropriate type.  In other words, the
  668.    consumer of the value this function produces may assume the value
  669.    is an rvalue only after checking its type.

  670.    The immediate issue is that if the user tries to use a structure or
  671.    union as an operand of, say, the `+' operator, we don't want to try
  672.    to convert that structure to an rvalue; require_rvalue will bomb on
  673.    structs and unions.  Rather, we want to simply pass the struct
  674.    lvalue through unchanged, and let `+' raise an error.  */

  675. static void
  676. gen_usual_unary (struct expression *exp, struct agent_expr *ax,
  677.                  struct axs_value *value)
  678. {
  679.   /* We don't have to generate any code for the usual integral
  680.      conversions, since values are always represented as full-width on
  681.      the stack.  Should we tweak the type?  */

  682.   /* Some types require special handling.  */
  683.   switch (TYPE_CODE (value->type))
  684.     {
  685.       /* Functions get converted to a pointer to the function.  */
  686.     case TYPE_CODE_FUNC:
  687.       value->type = lookup_pointer_type (value->type);
  688.       value->kind = axs_rvalue;        /* Should always be true, but just in case.  */
  689.       break;

  690.       /* Arrays get converted to a pointer to their first element, and
  691.          are no longer an lvalue.  */
  692.     case TYPE_CODE_ARRAY:
  693.       {
  694.         struct type *elements = TYPE_TARGET_TYPE (value->type);

  695.         value->type = lookup_pointer_type (elements);
  696.         value->kind = axs_rvalue;
  697.         /* We don't need to generate any code; the address of the array
  698.            is also the address of its first element.  */
  699.       }
  700.       break;

  701.       /* Don't try to convert structures and unions to rvalues.  Let the
  702.          consumer signal an error.  */
  703.     case TYPE_CODE_STRUCT:
  704.     case TYPE_CODE_UNION:
  705.       return;
  706.     }

  707.   /* If the value is an lvalue, dereference it.  */
  708.   require_rvalue (ax, value);
  709. }


  710. /* Return non-zero iff the type TYPE1 is considered "wider" than the
  711.    type TYPE2, according to the rules described in gen_usual_arithmetic.  */
  712. static int
  713. type_wider_than (struct type *type1, struct type *type2)
  714. {
  715.   return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
  716.           || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
  717.               && TYPE_UNSIGNED (type1)
  718.               && !TYPE_UNSIGNED (type2)));
  719. }


  720. /* Return the "wider" of the two types TYPE1 and TYPE2.  */
  721. static struct type *
  722. max_type (struct type *type1, struct type *type2)
  723. {
  724.   return type_wider_than (type1, type2) ? type1 : type2;
  725. }


  726. /* Generate code to convert a scalar value of type FROM to type TO.  */
  727. static void
  728. gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
  729. {
  730.   /* Perhaps there is a more graceful way to state these rules.  */

  731.   /* If we're converting to a narrower type, then we need to clear out
  732.      the upper bits.  */
  733.   if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
  734.     gen_extend (ax, from);

  735.   /* If the two values have equal width, but different signednesses,
  736.      then we need to extend.  */
  737.   else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
  738.     {
  739.       if (TYPE_UNSIGNED (from) != TYPE_UNSIGNED (to))
  740.         gen_extend (ax, to);
  741.     }

  742.   /* If we're converting to a wider type, and becoming unsigned, then
  743.      we need to zero out any possible sign bits.  */
  744.   else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
  745.     {
  746.       if (TYPE_UNSIGNED (to))
  747.         gen_extend (ax, to);
  748.     }
  749. }


  750. /* Return non-zero iff the type FROM will require any bytecodes to be
  751.    emitted to be converted to the type TO.  */
  752. static int
  753. is_nontrivial_conversion (struct type *from, struct type *to)
  754. {
  755.   struct agent_expr *ax = new_agent_expr (NULL, 0);
  756.   int nontrivial;

  757.   /* Actually generate the code, and see if anything came out.  At the
  758.      moment, it would be trivial to replicate the code in
  759.      gen_conversion here, but in the future, when we're supporting
  760.      floating point and the like, it may not be.  Doing things this
  761.      way allows this function to be independent of the logic in
  762.      gen_conversion.  */
  763.   gen_conversion (ax, from, to);
  764.   nontrivial = ax->len > 0;
  765.   free_agent_expr (ax);
  766.   return nontrivial;
  767. }


  768. /* Generate code to perform the "usual arithmetic conversions" (ANSI C
  769.    6.2.1.5) for the two operands of an arithmetic operator.  This
  770.    effectively finds a "least upper bound" type for the two arguments,
  771.    and promotes each argument to that type.  *VALUE1 and *VALUE2
  772.    describe the values as they are passed in, and as they are left.  */
  773. static void
  774. gen_usual_arithmetic (struct expression *exp, struct agent_expr *ax,
  775.                       struct axs_value *value1, struct axs_value *value2)
  776. {
  777.   /* Do the usual binary conversions.  */
  778.   if (TYPE_CODE (value1->type) == TYPE_CODE_INT
  779.       && TYPE_CODE (value2->type) == TYPE_CODE_INT)
  780.     {
  781.       /* The ANSI integral promotions seem to work this way: Order the
  782.          integer types by size, and then by signedness: an n-bit
  783.          unsigned type is considered "wider" than an n-bit signed
  784.          type.  Promote to the "wider" of the two types, and always
  785.          promote at least to int.  */
  786.       struct type *target = max_type (builtin_type (exp->gdbarch)->builtin_int,
  787.                                       max_type (value1->type, value2->type));

  788.       /* Deal with value2, on the top of the stack.  */
  789.       gen_conversion (ax, value2->type, target);

  790.       /* Deal with value1, not on the top of the stack.  Don't
  791.          generate the `swap' instructions if we're not actually going
  792.          to do anything.  */
  793.       if (is_nontrivial_conversion (value1->type, target))
  794.         {
  795.           ax_simple (ax, aop_swap);
  796.           gen_conversion (ax, value1->type, target);
  797.           ax_simple (ax, aop_swap);
  798.         }

  799.       value1->type = value2->type = check_typedef (target);
  800.     }
  801. }


  802. /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
  803.    the value on the top of the stack, as described by VALUE.  Assume
  804.    the value has integral type.  */
  805. static void
  806. gen_integral_promotions (struct expression *exp, struct agent_expr *ax,
  807.                          struct axs_value *value)
  808. {
  809.   const struct builtin_type *builtin = builtin_type (exp->gdbarch);

  810.   if (!type_wider_than (value->type, builtin->builtin_int))
  811.     {
  812.       gen_conversion (ax, value->type, builtin->builtin_int);
  813.       value->type = builtin->builtin_int;
  814.     }
  815.   else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
  816.     {
  817.       gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
  818.       value->type = builtin->builtin_unsigned_int;
  819.     }
  820. }


  821. /* Generate code for a cast to TYPE.  */
  822. static void
  823. gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
  824. {
  825.   /* GCC does allow casts to yield lvalues, so this should be fixed
  826.      before merging these changes into the trunk.  */
  827.   require_rvalue (ax, value);
  828.   /* Dereference typedefs.  */
  829.   type = check_typedef (type);

  830.   switch (TYPE_CODE (type))
  831.     {
  832.     case TYPE_CODE_PTR:
  833.     case TYPE_CODE_REF:
  834.       /* It's implementation-defined, and I'll bet this is what GCC
  835.          does.  */
  836.       break;

  837.     case TYPE_CODE_ARRAY:
  838.     case TYPE_CODE_STRUCT:
  839.     case TYPE_CODE_UNION:
  840.     case TYPE_CODE_FUNC:
  841.       error (_("Invalid type cast: intended type must be scalar."));

  842.     case TYPE_CODE_ENUM:
  843.     case TYPE_CODE_BOOL:
  844.       /* We don't have to worry about the size of the value, because
  845.          all our integral values are fully sign-extended, and when
  846.          casting pointers we can do anything we like.  Is there any
  847.          way for us to know what GCC actually does with a cast like
  848.          this?  */
  849.       break;

  850.     case TYPE_CODE_INT:
  851.       gen_conversion (ax, value->type, type);
  852.       break;

  853.     case TYPE_CODE_VOID:
  854.       /* We could pop the value, and rely on everyone else to check
  855.          the type and notice that this value doesn't occupy a stack
  856.          slot.  But for now, leave the value on the stack, and
  857.          preserve the "value == stack element" assumption.  */
  858.       break;

  859.     default:
  860.       error (_("Casts to requested type are not yet implemented."));
  861.     }

  862.   value->type = type;
  863. }



  864. /* Generating bytecode from GDB expressions: arithmetic */

  865. /* Scale the integer on the top of the stack by the size of the target
  866.    of the pointer type TYPE.  */
  867. static void
  868. gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
  869. {
  870.   struct type *element = TYPE_TARGET_TYPE (type);

  871.   if (TYPE_LENGTH (element) != 1)
  872.     {
  873.       ax_const_l (ax, TYPE_LENGTH (element));
  874.       ax_simple (ax, op);
  875.     }
  876. }


  877. /* Generate code for pointer arithmetic PTR + INT.  */
  878. static void
  879. gen_ptradd (struct agent_expr *ax, struct axs_value *value,
  880.             struct axs_value *value1, struct axs_value *value2)
  881. {
  882.   gdb_assert (pointer_type (value1->type));
  883.   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);

  884.   gen_scale (ax, aop_mul, value1->type);
  885.   ax_simple (ax, aop_add);
  886.   gen_extend (ax, value1->type);        /* Catch overflow.  */
  887.   value->type = value1->type;
  888.   value->kind = axs_rvalue;
  889. }


  890. /* Generate code for pointer arithmetic PTR - INT.  */
  891. static void
  892. gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
  893.             struct axs_value *value1, struct axs_value *value2)
  894. {
  895.   gdb_assert (pointer_type (value1->type));
  896.   gdb_assert (TYPE_CODE (value2->type) == TYPE_CODE_INT);

  897.   gen_scale (ax, aop_mul, value1->type);
  898.   ax_simple (ax, aop_sub);
  899.   gen_extend (ax, value1->type);        /* Catch overflow.  */
  900.   value->type = value1->type;
  901.   value->kind = axs_rvalue;
  902. }


  903. /* Generate code for pointer arithmetic PTR - PTR.  */
  904. static void
  905. gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
  906.              struct axs_value *value1, struct axs_value *value2,
  907.              struct type *result_type)
  908. {
  909.   gdb_assert (pointer_type (value1->type));
  910.   gdb_assert (pointer_type (value2->type));

  911.   if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
  912.       != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
  913.     error (_("\
  914. First argument of `-' is a pointer, but second argument is neither\n\
  915. an integer nor a pointer of the same type."));

  916.   ax_simple (ax, aop_sub);
  917.   gen_scale (ax, aop_div_unsigned, value1->type);
  918.   value->type = result_type;
  919.   value->kind = axs_rvalue;
  920. }

  921. static void
  922. gen_equal (struct agent_expr *ax, struct axs_value *value,
  923.            struct axs_value *value1, struct axs_value *value2,
  924.            struct type *result_type)
  925. {
  926.   if (pointer_type (value1->type) || pointer_type (value2->type))
  927.     ax_simple (ax, aop_equal);
  928.   else
  929.     gen_binop (ax, value, value1, value2,
  930.                aop_equal, aop_equal, 0, "equal");
  931.   value->type = result_type;
  932.   value->kind = axs_rvalue;
  933. }

  934. static void
  935. gen_less (struct agent_expr *ax, struct axs_value *value,
  936.           struct axs_value *value1, struct axs_value *value2,
  937.           struct type *result_type)
  938. {
  939.   if (pointer_type (value1->type) || pointer_type (value2->type))
  940.     ax_simple (ax, aop_less_unsigned);
  941.   else
  942.     gen_binop (ax, value, value1, value2,
  943.                aop_less_signed, aop_less_unsigned, 0, "less than");
  944.   value->type = result_type;
  945.   value->kind = axs_rvalue;
  946. }

  947. /* Generate code for a binary operator that doesn't do pointer magic.
  948.    We set VALUE to describe the result value; we assume VALUE1 and
  949.    VALUE2 describe the two operands, and that they've undergone the
  950.    usual binary conversions.  MAY_CARRY should be non-zero iff the
  951.    result needs to be extended.  NAME is the English name of the
  952.    operator, used in error messages */
  953. static void
  954. gen_binop (struct agent_expr *ax, struct axs_value *value,
  955.            struct axs_value *value1, struct axs_value *value2,
  956.            enum agent_op op, enum agent_op op_unsigned,
  957.            int may_carry, char *name)
  958. {
  959.   /* We only handle INT op INT.  */
  960.   if ((TYPE_CODE (value1->type) != TYPE_CODE_INT)
  961.       || (TYPE_CODE (value2->type) != TYPE_CODE_INT))
  962.     error (_("Invalid combination of types in %s."), name);

  963.   ax_simple (ax,
  964.              TYPE_UNSIGNED (value1->type) ? op_unsigned : op);
  965.   if (may_carry)
  966.     gen_extend (ax, value1->type);        /* catch overflow */
  967.   value->type = value1->type;
  968.   value->kind = axs_rvalue;
  969. }


  970. static void
  971. gen_logical_not (struct agent_expr *ax, struct axs_value *value,
  972.                  struct type *result_type)
  973. {
  974.   if (TYPE_CODE (value->type) != TYPE_CODE_INT
  975.       && TYPE_CODE (value->type) != TYPE_CODE_PTR)
  976.     error (_("Invalid type of operand to `!'."));

  977.   ax_simple (ax, aop_log_not);
  978.   value->type = result_type;
  979. }


  980. static void
  981. gen_complement (struct agent_expr *ax, struct axs_value *value)
  982. {
  983.   if (TYPE_CODE (value->type) != TYPE_CODE_INT)
  984.     error (_("Invalid type of operand to `~'."));

  985.   ax_simple (ax, aop_bit_not);
  986.   gen_extend (ax, value->type);
  987. }



  988. /* Generating bytecode from GDB expressions: * & . -> @ sizeof */

  989. /* Dereference the value on the top of the stack.  */
  990. static void
  991. gen_deref (struct agent_expr *ax, struct axs_value *value)
  992. {
  993.   /* The caller should check the type, because several operators use
  994.      this, and we don't know what error message to generate.  */
  995.   if (!pointer_type (value->type))
  996.     internal_error (__FILE__, __LINE__,
  997.                     _("gen_deref: expected a pointer"));

  998.   /* We've got an rvalue now, which is a pointer.  We want to yield an
  999.      lvalue, whose address is exactly that pointer.  So we don't
  1000.      actually emit any code; we just change the type from "Pointer to
  1001.      T" to "T", and mark the value as an lvalue in memory.  Leave it
  1002.      to the consumer to actually dereference it.  */
  1003.   value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
  1004.   if (TYPE_CODE (value->type) == TYPE_CODE_VOID)
  1005.     error (_("Attempt to dereference a generic pointer."));
  1006.   value->kind = ((TYPE_CODE (value->type) == TYPE_CODE_FUNC)
  1007.                  ? axs_rvalue : axs_lvalue_memory);
  1008. }


  1009. /* Produce the address of the lvalue on the top of the stack.  */
  1010. static void
  1011. gen_address_of (struct agent_expr *ax, struct axs_value *value)
  1012. {
  1013.   /* Special case for taking the address of a function.  The ANSI
  1014.      standard describes this as a special case, too, so this
  1015.      arrangement is not without motivation.  */
  1016.   if (TYPE_CODE (value->type) == TYPE_CODE_FUNC)
  1017.     /* The value's already an rvalue on the stack, so we just need to
  1018.        change the type.  */
  1019.     value->type = lookup_pointer_type (value->type);
  1020.   else
  1021.     switch (value->kind)
  1022.       {
  1023.       case axs_rvalue:
  1024.         error (_("Operand of `&' is an rvalue, which has no address."));

  1025.       case axs_lvalue_register:
  1026.         error (_("Operand of `&' is in a register, and has no address."));

  1027.       case axs_lvalue_memory:
  1028.         value->kind = axs_rvalue;
  1029.         value->type = lookup_pointer_type (value->type);
  1030.         break;
  1031.       }
  1032. }

  1033. /* Generate code to push the value of a bitfield of a structure whose
  1034.    address is on the top of the stack.  START and END give the
  1035.    starting and one-past-ending *bit* numbers of the field within the
  1036.    structure.  */
  1037. static void
  1038. gen_bitfield_ref (struct expression *exp, struct agent_expr *ax,
  1039.                   struct axs_value *value, struct type *type,
  1040.                   int start, int end)
  1041. {
  1042.   /* Note that ops[i] fetches 8 << i bits.  */
  1043.   static enum agent_op ops[]
  1044.     = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
  1045.   static int num_ops = (sizeof (ops) / sizeof (ops[0]));

  1046.   /* We don't want to touch any byte that the bitfield doesn't
  1047.      actually occupy; we shouldn't make any accesses we're not
  1048.      explicitly permitted to.  We rely here on the fact that the
  1049.      bytecode `ref' operators work on unaligned addresses.

  1050.      It takes some fancy footwork to get the stack to work the way
  1051.      we'd like.  Say we're retrieving a bitfield that requires three
  1052.      fetches.  Initially, the stack just contains the address:
  1053.      addr
  1054.      For the first fetch, we duplicate the address
  1055.      addr addr
  1056.      then add the byte offset, do the fetch, and shift and mask as
  1057.      needed, yielding a fragment of the value, properly aligned for
  1058.      the final bitwise or:
  1059.      addr frag1
  1060.      then we swap, and repeat the process:
  1061.      frag1 addr                    --- address on top
  1062.      frag1 addr addr               --- duplicate it
  1063.      frag1 addr frag2              --- get second fragment
  1064.      frag1 frag2 addr              --- swap again
  1065.      frag1 frag2 frag3             --- get third fragment
  1066.      Notice that, since the third fragment is the last one, we don't
  1067.      bother duplicating the address this time.  Now we have all the
  1068.      fragments on the stack, and we can simply `or' them together,
  1069.      yielding the final value of the bitfield.  */

  1070.   /* The first and one-after-last bits in the field, but rounded down
  1071.      and up to byte boundaries.  */
  1072.   int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
  1073.   int bound_end = (((end + TARGET_CHAR_BIT - 1)
  1074.                     / TARGET_CHAR_BIT)
  1075.                    * TARGET_CHAR_BIT);

  1076.   /* current bit offset within the structure */
  1077.   int offset;

  1078.   /* The index in ops of the opcode we're considering.  */
  1079.   int op;

  1080.   /* The number of fragments we generated in the process.  Probably
  1081.      equal to the number of `one' bits in bytesize, but who cares?  */
  1082.   int fragment_count;

  1083.   /* Dereference any typedefs.  */
  1084.   type = check_typedef (type);

  1085.   /* Can we fetch the number of bits requested at all?  */
  1086.   if ((end - start) > ((1 << num_ops) * 8))
  1087.     internal_error (__FILE__, __LINE__,
  1088.                     _("gen_bitfield_ref: bitfield too wide"));

  1089.   /* Note that we know here that we only need to try each opcode once.
  1090.      That may not be true on machines with weird byte sizes.  */
  1091.   offset = bound_start;
  1092.   fragment_count = 0;
  1093.   for (op = num_ops - 1; op >= 0; op--)
  1094.     {
  1095.       /* number of bits that ops[op] would fetch */
  1096.       int op_size = 8 << op;

  1097.       /* The stack at this point, from bottom to top, contains zero or
  1098.          more fragments, then the address.  */

  1099.       /* Does this fetch fit within the bitfield?  */
  1100.       if (offset + op_size <= bound_end)
  1101.         {
  1102.           /* Is this the last fragment?  */
  1103.           int last_frag = (offset + op_size == bound_end);

  1104.           if (!last_frag)
  1105.             ax_simple (ax, aop_dup);        /* keep a copy of the address */

  1106.           /* Add the offset.  */
  1107.           gen_offset (ax, offset / TARGET_CHAR_BIT);

  1108.           if (ax->tracing)
  1109.             {
  1110.               /* Record the area of memory we're about to fetch.  */
  1111.               ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
  1112.             }

  1113.           /* Perform the fetch.  */
  1114.           ax_simple (ax, ops[op]);

  1115.           /* Shift the bits we have to their proper position.
  1116.              gen_left_shift will generate right shifts when the operand
  1117.              is negative.

  1118.              A big-endian field diagram to ponder:
  1119.              byte 0  byte 1  byte 2  byte 3  byte 4  byte 5  byte 6  byte 7
  1120.              +------++------++------++------++------++------++------++------+
  1121.              xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
  1122.              ^               ^               ^    ^
  1123.              bit number      16              32              48   53
  1124.              These are bit numbers as supplied by GDB.  Note that the
  1125.              bit numbers run from right to left once you've fetched the
  1126.              value!

  1127.              A little-endian field diagram to ponder:
  1128.              byte 7  byte 6  byte 5  byte 4  byte 3  byte 2  byte 1  byte 0
  1129.              +------++------++------++------++------++------++------++------+
  1130.              xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
  1131.              ^               ^               ^           ^   ^
  1132.              bit number     48              32              16          4   0

  1133.              In both cases, the most significant end is on the left
  1134.              (i.e. normal numeric writing order), which means that you
  1135.              don't go crazy thinking about `left' and `right' shifts.

  1136.              We don't have to worry about masking yet:
  1137.              - If they contain garbage off the least significant end, then we
  1138.              must be looking at the low end of the field, and the right
  1139.              shift will wipe them out.
  1140.              - If they contain garbage off the most significant end, then we
  1141.              must be looking at the most significant end of the word, and
  1142.              the sign/zero extension will wipe them out.
  1143.              - If we're in the interior of the word, then there is no garbage
  1144.              on either end, because the ref operators zero-extend.  */
  1145.           if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
  1146.             gen_left_shift (ax, end - (offset + op_size));
  1147.           else
  1148.             gen_left_shift (ax, offset - start);

  1149.           if (!last_frag)
  1150.             /* Bring the copy of the address up to the top.  */
  1151.             ax_simple (ax, aop_swap);

  1152.           offset += op_size;
  1153.           fragment_count++;
  1154.         }
  1155.     }

  1156.   /* Generate enough bitwise `or' operations to combine all the
  1157.      fragments we left on the stack.  */
  1158.   while (fragment_count-- > 1)
  1159.     ax_simple (ax, aop_bit_or);

  1160.   /* Sign- or zero-extend the value as appropriate.  */
  1161.   ((TYPE_UNSIGNED (type) ? ax_zero_ext : ax_ext) (ax, end - start));

  1162.   /* This is *not* an lvalue.  Ugh.  */
  1163.   value->kind = axs_rvalue;
  1164.   value->type = type;
  1165. }

  1166. /* Generate bytecodes for field number FIELDNO of type TYPE.  OFFSET
  1167.    is an accumulated offset (in bytes), will be nonzero for objects
  1168.    embedded in other objects, like C++ base classes.  Behavior should
  1169.    generally follow value_primitive_field.  */

  1170. static void
  1171. gen_primitive_field (struct expression *exp,
  1172.                      struct agent_expr *ax, struct axs_value *value,
  1173.                      int offset, int fieldno, struct type *type)
  1174. {
  1175.   /* Is this a bitfield?  */
  1176.   if (TYPE_FIELD_PACKED (type, fieldno))
  1177.     gen_bitfield_ref (exp, ax, value, TYPE_FIELD_TYPE (type, fieldno),
  1178.                       (offset * TARGET_CHAR_BIT
  1179.                        + TYPE_FIELD_BITPOS (type, fieldno)),
  1180.                       (offset * TARGET_CHAR_BIT
  1181.                        + TYPE_FIELD_BITPOS (type, fieldno)
  1182.                        + TYPE_FIELD_BITSIZE (type, fieldno)));
  1183.   else
  1184.     {
  1185.       gen_offset (ax, offset
  1186.                   + TYPE_FIELD_BITPOS (type, fieldno) / TARGET_CHAR_BIT);
  1187.       value->kind = axs_lvalue_memory;
  1188.       value->type = TYPE_FIELD_TYPE (type, fieldno);
  1189.     }
  1190. }

  1191. /* Search for the given field in either the given type or one of its
  1192.    base classes.  Return 1 if found, 0 if not.  */

  1193. static int
  1194. gen_struct_ref_recursive (struct expression *exp, struct agent_expr *ax,
  1195.                           struct axs_value *value,
  1196.                           char *field, int offset, struct type *type)
  1197. {
  1198.   int i, rslt;
  1199.   int nbases = TYPE_N_BASECLASSES (type);

  1200.   CHECK_TYPEDEF (type);

  1201.   for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
  1202.     {
  1203.       const char *this_name = TYPE_FIELD_NAME (type, i);

  1204.       if (this_name)
  1205.         {
  1206.           if (strcmp (field, this_name) == 0)
  1207.             {
  1208.               /* Note that bytecodes for the struct's base (aka
  1209.                  "this") will have been generated already, which will
  1210.                  be unnecessary but not harmful if the static field is
  1211.                  being handled as a global.  */
  1212.               if (field_is_static (&TYPE_FIELD (type, i)))
  1213.                 {
  1214.                   gen_static_field (exp->gdbarch, ax, value, type, i);
  1215.                   if (value->optimized_out)
  1216.                     error (_("static field `%s' has been "
  1217.                              "optimized out, cannot use"),
  1218.                            field);
  1219.                   return 1;
  1220.                 }

  1221.               gen_primitive_field (exp, ax, value, offset, i, type);
  1222.               return 1;
  1223.             }
  1224. #if 0 /* is this right? */
  1225.           if (this_name[0] == '\0')
  1226.             internal_error (__FILE__, __LINE__,
  1227.                             _("find_field: anonymous unions not supported"));
  1228. #endif
  1229.         }
  1230.     }

  1231.   /* Now scan through base classes recursively.  */
  1232.   for (i = 0; i < nbases; i++)
  1233.     {
  1234.       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));

  1235.       rslt = gen_struct_ref_recursive (exp, ax, value, field,
  1236.                                        offset + TYPE_BASECLASS_BITPOS (type, i)
  1237.                                        / TARGET_CHAR_BIT,
  1238.                                        basetype);
  1239.       if (rslt)
  1240.         return 1;
  1241.     }

  1242.   /* Not found anywhere, flag so caller can complain.  */
  1243.   return 0;
  1244. }

  1245. /* Generate code to reference the member named FIELD of a structure or
  1246.    union.  The top of the stack, as described by VALUE, should have
  1247.    type (pointer to a)* struct/union.  OPERATOR_NAME is the name of
  1248.    the operator being compiled, and OPERAND_NAME is the kind of thing
  1249.    it operates on; we use them in error messages.  */
  1250. static void
  1251. gen_struct_ref (struct expression *exp, struct agent_expr *ax,
  1252.                 struct axs_value *value, char *field,
  1253.                 char *operator_name, char *operand_name)
  1254. {
  1255.   struct type *type;
  1256.   int found;

  1257.   /* Follow pointers until we reach a non-pointer.  These aren't the C
  1258.      semantics, but they're what the normal GDB evaluator does, so we
  1259.      should at least be consistent.  */
  1260.   while (pointer_type (value->type))
  1261.     {
  1262.       require_rvalue (ax, value);
  1263.       gen_deref (ax, value);
  1264.     }
  1265.   type = check_typedef (value->type);

  1266.   /* This must yield a structure or a union.  */
  1267.   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  1268.       && TYPE_CODE (type) != TYPE_CODE_UNION)
  1269.     error (_("The left operand of `%s' is not a %s."),
  1270.            operator_name, operand_name);

  1271.   /* And it must be in memory; we don't deal with structure rvalues,
  1272.      or structures living in registers.  */
  1273.   if (value->kind != axs_lvalue_memory)
  1274.     error (_("Structure does not live in memory."));

  1275.   /* Search through fields and base classes recursively.  */
  1276.   found = gen_struct_ref_recursive (exp, ax, value, field, 0, type);

  1277.   if (!found)
  1278.     error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
  1279.            field, TYPE_TAG_NAME (type));
  1280. }

  1281. static int
  1282. gen_namespace_elt (struct expression *exp,
  1283.                    struct agent_expr *ax, struct axs_value *value,
  1284.                    const struct type *curtype, char *name);
  1285. static int
  1286. gen_maybe_namespace_elt (struct expression *exp,
  1287.                          struct agent_expr *ax, struct axs_value *value,
  1288.                          const struct type *curtype, char *name);

  1289. static void
  1290. gen_static_field (struct gdbarch *gdbarch,
  1291.                   struct agent_expr *ax, struct axs_value *value,
  1292.                   struct type *type, int fieldno)
  1293. {
  1294.   if (TYPE_FIELD_LOC_KIND (type, fieldno) == FIELD_LOC_KIND_PHYSADDR)
  1295.     {
  1296.       ax_const_l (ax, TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
  1297.       value->kind = axs_lvalue_memory;
  1298.       value->type = TYPE_FIELD_TYPE (type, fieldno);
  1299.       value->optimized_out = 0;
  1300.     }
  1301.   else
  1302.     {
  1303.       const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
  1304.       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);

  1305.       if (sym)
  1306.         {
  1307.           gen_var_ref (gdbarch, ax, value, sym);

  1308.           /* Don't error if the value was optimized out, we may be
  1309.              scanning all static fields and just want to pass over this
  1310.              and continue with the rest.  */
  1311.         }
  1312.       else
  1313.         {
  1314.           /* Silently assume this was optimized out; class printing
  1315.              will let the user know why the data is missing.  */
  1316.           value->optimized_out = 1;
  1317.         }
  1318.     }
  1319. }

  1320. static int
  1321. gen_struct_elt_for_reference (struct expression *exp,
  1322.                               struct agent_expr *ax, struct axs_value *value,
  1323.                               struct type *type, char *fieldname)
  1324. {
  1325.   struct type *t = type;
  1326.   int i;

  1327.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  1328.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1329.     internal_error (__FILE__, __LINE__,
  1330.                     _("non-aggregate type to gen_struct_elt_for_reference"));

  1331.   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
  1332.     {
  1333.       const char *t_field_name = TYPE_FIELD_NAME (t, i);

  1334.       if (t_field_name && strcmp (t_field_name, fieldname) == 0)
  1335.         {
  1336.           if (field_is_static (&TYPE_FIELD (t, i)))
  1337.             {
  1338.               gen_static_field (exp->gdbarch, ax, value, t, i);
  1339.               if (value->optimized_out)
  1340.                 error (_("static field `%s' has been "
  1341.                          "optimized out, cannot use"),
  1342.                        fieldname);
  1343.               return 1;
  1344.             }
  1345.           if (TYPE_FIELD_PACKED (t, i))
  1346.             error (_("pointers to bitfield members not allowed"));

  1347.           /* FIXME we need a way to do "want_address" equivalent */

  1348.           error (_("Cannot reference non-static field \"%s\""), fieldname);
  1349.         }
  1350.     }

  1351.   /* FIXME add other scoped-reference cases here */

  1352.   /* Do a last-ditch lookup.  */
  1353.   return gen_maybe_namespace_elt (exp, ax, value, type, fieldname);
  1354. }

  1355. /* C++: Return the member NAME of the namespace given by the type
  1356.    CURTYPE.  */

  1357. static int
  1358. gen_namespace_elt (struct expression *exp,
  1359.                    struct agent_expr *ax, struct axs_value *value,
  1360.                    const struct type *curtype, char *name)
  1361. {
  1362.   int found = gen_maybe_namespace_elt (exp, ax, value, curtype, name);

  1363.   if (!found)
  1364.     error (_("No symbol \"%s\" in namespace \"%s\"."),
  1365.            name, TYPE_TAG_NAME (curtype));

  1366.   return found;
  1367. }

  1368. /* A helper function used by value_namespace_elt and
  1369.    value_struct_elt_for_reference.  It looks up NAME inside the
  1370.    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
  1371.    is a class and NAME refers to a type in CURTYPE itself (as opposed
  1372.    to, say, some base class of CURTYPE).  */

  1373. static int
  1374. gen_maybe_namespace_elt (struct expression *exp,
  1375.                          struct agent_expr *ax, struct axs_value *value,
  1376.                          const struct type *curtype, char *name)
  1377. {
  1378.   const char *namespace_name = TYPE_TAG_NAME (curtype);
  1379.   struct symbol *sym;

  1380.   sym = cp_lookup_symbol_namespace (namespace_name, name,
  1381.                                     block_for_pc (ax->scope),
  1382.                                     VAR_DOMAIN);

  1383.   if (sym == NULL)
  1384.     return 0;

  1385.   gen_var_ref (exp->gdbarch, ax, value, sym);

  1386.   if (value->optimized_out)
  1387.     error (_("`%s' has been optimized out, cannot use"),
  1388.            SYMBOL_PRINT_NAME (sym));

  1389.   return 1;
  1390. }


  1391. static int
  1392. gen_aggregate_elt_ref (struct expression *exp,
  1393.                        struct agent_expr *ax, struct axs_value *value,
  1394.                        struct type *type, char *field,
  1395.                        char *operator_name, char *operand_name)
  1396. {
  1397.   switch (TYPE_CODE (type))
  1398.     {
  1399.     case TYPE_CODE_STRUCT:
  1400.     case TYPE_CODE_UNION:
  1401.       return gen_struct_elt_for_reference (exp, ax, value, type, field);
  1402.       break;
  1403.     case TYPE_CODE_NAMESPACE:
  1404.       return gen_namespace_elt (exp, ax, value, type, field);
  1405.       break;
  1406.     default:
  1407.       internal_error (__FILE__, __LINE__,
  1408.                       _("non-aggregate type in gen_aggregate_elt_ref"));
  1409.     }

  1410.   return 0;
  1411. }

  1412. /* Generate code for GDB's magical `repeat' operator.
  1413.    LVALUE @ INT creates an array INT elements long, and whose elements
  1414.    have the same type as LVALUE, located in memory so that LVALUE is
  1415.    its first element.  For example, argv[0]@argc gives you the array
  1416.    of command-line arguments.

  1417.    Unfortunately, because we have to know the types before we actually
  1418.    have a value for the expression, we can't implement this perfectly
  1419.    without changing the type system, having values that occupy two
  1420.    stack slots, doing weird things with sizeof, etc.  So we require
  1421.    the right operand to be a constant expression.  */
  1422. static void
  1423. gen_repeat (struct expression *exp, union exp_element **pc,
  1424.             struct agent_expr *ax, struct axs_value *value)
  1425. {
  1426.   struct axs_value value1;

  1427.   /* We don't want to turn this into an rvalue, so no conversions
  1428.      here.  */
  1429.   gen_expr (exp, pc, ax, &value1);
  1430.   if (value1.kind != axs_lvalue_memory)
  1431.     error (_("Left operand of `@' must be an object in memory."));

  1432.   /* Evaluate the length; it had better be a constant.  */
  1433.   {
  1434.     struct value *v = const_expr (pc);
  1435.     int length;

  1436.     if (!v)
  1437.       error (_("Right operand of `@' must be a "
  1438.                "constant, in agent expressions."));
  1439.     if (TYPE_CODE (value_type (v)) != TYPE_CODE_INT)
  1440.       error (_("Right operand of `@' must be an integer."));
  1441.     length = value_as_long (v);
  1442.     if (length <= 0)
  1443.       error (_("Right operand of `@' must be positive."));

  1444.     /* The top of the stack is already the address of the object, so
  1445.        all we need to do is frob the type of the lvalue.  */
  1446.     {
  1447.       /* FIXME-type-allocation: need a way to free this type when we are
  1448.          done with it.  */
  1449.       struct type *array
  1450.         = lookup_array_range_type (value1.type, 0, length - 1);

  1451.       value->kind = axs_lvalue_memory;
  1452.       value->type = array;
  1453.     }
  1454.   }
  1455. }


  1456. /* Emit code for the `sizeof' operator.
  1457.    *PC should point at the start of the operand expression; we advance it
  1458.    to the first instruction after the operand.  */
  1459. static void
  1460. gen_sizeof (struct expression *exp, union exp_element **pc,
  1461.             struct agent_expr *ax, struct axs_value *value,
  1462.             struct type *size_type)
  1463. {
  1464.   /* We don't care about the value of the operand expression; we only
  1465.      care about its type.  However, in the current arrangement, the
  1466.      only way to find an expression's type is to generate code for it.
  1467.      So we generate code for the operand, and then throw it away,
  1468.      replacing it with code that simply pushes its size.  */
  1469.   int start = ax->len;

  1470.   gen_expr (exp, pc, ax, value);

  1471.   /* Throw away the code we just generated.  */
  1472.   ax->len = start;

  1473.   ax_const_l (ax, TYPE_LENGTH (value->type));
  1474.   value->kind = axs_rvalue;
  1475.   value->type = size_type;
  1476. }


  1477. /* Generating bytecode from GDB expressions: general recursive thingy  */

  1478. /* XXX: i18n */
  1479. /* A gen_expr function written by a Gen-X'er guy.
  1480.    Append code for the subexpression of EXPR starting at *POS_P to AX.  */
  1481. void
  1482. gen_expr (struct expression *exp, union exp_element **pc,
  1483.           struct agent_expr *ax, struct axs_value *value)
  1484. {
  1485.   /* Used to hold the descriptions of operand expressions.  */
  1486.   struct axs_value value1, value2, value3;
  1487.   enum exp_opcode op = (*pc)[0].opcode, op2;
  1488.   int if1, go1, if2, go2, end;
  1489.   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;

  1490.   /* If we're looking at a constant expression, just push its value.  */
  1491.   {
  1492.     struct value *v = maybe_const_expr (pc);

  1493.     if (v)
  1494.       {
  1495.         ax_const_l (ax, value_as_long (v));
  1496.         value->kind = axs_rvalue;
  1497.         value->type = check_typedef (value_type (v));
  1498.         return;
  1499.       }
  1500.   }

  1501.   /* Otherwise, go ahead and generate code for it.  */
  1502.   switch (op)
  1503.     {
  1504.       /* Binary arithmetic operators.  */
  1505.     case BINOP_ADD:
  1506.     case BINOP_SUB:
  1507.     case BINOP_MUL:
  1508.     case BINOP_DIV:
  1509.     case BINOP_REM:
  1510.     case BINOP_LSH:
  1511.     case BINOP_RSH:
  1512.     case BINOP_SUBSCRIPT:
  1513.     case BINOP_BITWISE_AND:
  1514.     case BINOP_BITWISE_IOR:
  1515.     case BINOP_BITWISE_XOR:
  1516.     case BINOP_EQUAL:
  1517.     case BINOP_NOTEQUAL:
  1518.     case BINOP_LESS:
  1519.     case BINOP_GTR:
  1520.     case BINOP_LEQ:
  1521.     case BINOP_GEQ:
  1522.       (*pc)++;
  1523.       gen_expr (exp, pc, ax, &value1);
  1524.       gen_usual_unary (exp, ax, &value1);
  1525.       gen_expr_binop_rest (exp, op, pc, ax, value, &value1, &value2);
  1526.       break;

  1527.     case BINOP_LOGICAL_AND:
  1528.       (*pc)++;
  1529.       /* Generate the obvious sequence of tests and jumps.  */
  1530.       gen_expr (exp, pc, ax, &value1);
  1531.       gen_usual_unary (exp, ax, &value1);
  1532.       if1 = ax_goto (ax, aop_if_goto);
  1533.       go1 = ax_goto (ax, aop_goto);
  1534.       ax_label (ax, if1, ax->len);
  1535.       gen_expr (exp, pc, ax, &value2);
  1536.       gen_usual_unary (exp, ax, &value2);
  1537.       if2 = ax_goto (ax, aop_if_goto);
  1538.       go2 = ax_goto (ax, aop_goto);
  1539.       ax_label (ax, if2, ax->len);
  1540.       ax_const_l (ax, 1);
  1541.       end = ax_goto (ax, aop_goto);
  1542.       ax_label (ax, go1, ax->len);
  1543.       ax_label (ax, go2, ax->len);
  1544.       ax_const_l (ax, 0);
  1545.       ax_label (ax, end, ax->len);
  1546.       value->kind = axs_rvalue;
  1547.       value->type = int_type;
  1548.       break;

  1549.     case BINOP_LOGICAL_OR:
  1550.       (*pc)++;
  1551.       /* Generate the obvious sequence of tests and jumps.  */
  1552.       gen_expr (exp, pc, ax, &value1);
  1553.       gen_usual_unary (exp, ax, &value1);
  1554.       if1 = ax_goto (ax, aop_if_goto);
  1555.       gen_expr (exp, pc, ax, &value2);
  1556.       gen_usual_unary (exp, ax, &value2);
  1557.       if2 = ax_goto (ax, aop_if_goto);
  1558.       ax_const_l (ax, 0);
  1559.       end = ax_goto (ax, aop_goto);
  1560.       ax_label (ax, if1, ax->len);
  1561.       ax_label (ax, if2, ax->len);
  1562.       ax_const_l (ax, 1);
  1563.       ax_label (ax, end, ax->len);
  1564.       value->kind = axs_rvalue;
  1565.       value->type = int_type;
  1566.       break;

  1567.     case TERNOP_COND:
  1568.       (*pc)++;
  1569.       gen_expr (exp, pc, ax, &value1);
  1570.       gen_usual_unary (exp, ax, &value1);
  1571.       /* For (A ? B : C), it's easiest to generate subexpression
  1572.          bytecodes in order, but if_goto jumps on true, so we invert
  1573.          the sense of A.  Then we can do B by dropping through, and
  1574.          jump to do C.  */
  1575.       gen_logical_not (ax, &value1, int_type);
  1576.       if1 = ax_goto (ax, aop_if_goto);
  1577.       gen_expr (exp, pc, ax, &value2);
  1578.       gen_usual_unary (exp, ax, &value2);
  1579.       end = ax_goto (ax, aop_goto);
  1580.       ax_label (ax, if1, ax->len);
  1581.       gen_expr (exp, pc, ax, &value3);
  1582.       gen_usual_unary (exp, ax, &value3);
  1583.       ax_label (ax, end, ax->len);
  1584.       /* This is arbitary - what if B and C are incompatible types? */
  1585.       value->type = value2.type;
  1586.       value->kind = value2.kind;
  1587.       break;

  1588.     case BINOP_ASSIGN:
  1589.       (*pc)++;
  1590.       if ((*pc)[0].opcode == OP_INTERNALVAR)
  1591.         {
  1592.           char *name = internalvar_name ((*pc)[1].internalvar);
  1593.           struct trace_state_variable *tsv;

  1594.           (*pc) += 3;
  1595.           gen_expr (exp, pc, ax, value);
  1596.           tsv = find_trace_state_variable (name);
  1597.           if (tsv)
  1598.             {
  1599.               ax_tsv (ax, aop_setv, tsv->number);
  1600.               if (ax->tracing)
  1601.                 ax_tsv (ax, aop_tracev, tsv->number);
  1602.             }
  1603.           else
  1604.             error (_("$%s is not a trace state variable, "
  1605.                      "may not assign to it"), name);
  1606.         }
  1607.       else
  1608.         error (_("May only assign to trace state variables"));
  1609.       break;

  1610.     case BINOP_ASSIGN_MODIFY:
  1611.       (*pc)++;
  1612.       op2 = (*pc)[0].opcode;
  1613.       (*pc)++;
  1614.       (*pc)++;
  1615.       if ((*pc)[0].opcode == OP_INTERNALVAR)
  1616.         {
  1617.           char *name = internalvar_name ((*pc)[1].internalvar);
  1618.           struct trace_state_variable *tsv;

  1619.           (*pc) += 3;
  1620.           tsv = find_trace_state_variable (name);
  1621.           if (tsv)
  1622.             {
  1623.               /* The tsv will be the left half of the binary operation.  */
  1624.               ax_tsv (ax, aop_getv, tsv->number);
  1625.               if (ax->tracing)
  1626.                 ax_tsv (ax, aop_tracev, tsv->number);
  1627.               /* Trace state variables are always 64-bit integers.  */
  1628.               value1.kind = axs_rvalue;
  1629.               value1.type = builtin_type (exp->gdbarch)->builtin_long_long;
  1630.               /* Now do right half of expression.  */
  1631.               gen_expr_binop_rest (exp, op2, pc, ax, value, &value1, &value2);
  1632.               /* We have a result of the binary op, set the tsv.  */
  1633.               ax_tsv (ax, aop_setv, tsv->number);
  1634.               if (ax->tracing)
  1635.                 ax_tsv (ax, aop_tracev, tsv->number);
  1636.             }
  1637.           else
  1638.             error (_("$%s is not a trace state variable, "
  1639.                      "may not assign to it"), name);
  1640.         }
  1641.       else
  1642.         error (_("May only assign to trace state variables"));
  1643.       break;

  1644.       /* Note that we need to be a little subtle about generating code
  1645.          for comma.  In C, we can do some optimizations here because
  1646.          we know the left operand is only being evaluated for effect.
  1647.          However, if the tracing kludge is in effect, then we always
  1648.          need to evaluate the left hand side fully, so that all the
  1649.          variables it mentions get traced.  */
  1650.     case BINOP_COMMA:
  1651.       (*pc)++;
  1652.       gen_expr (exp, pc, ax, &value1);
  1653.       /* Don't just dispose of the left operand.  We might be tracing,
  1654.          in which case we want to emit code to trace it if it's an
  1655.          lvalue.  */
  1656.       gen_traced_pop (exp->gdbarch, ax, &value1);
  1657.       gen_expr (exp, pc, ax, value);
  1658.       /* It's the consumer's responsibility to trace the right operand.  */
  1659.       break;

  1660.     case OP_LONG:                /* some integer constant */
  1661.       {
  1662.         struct type *type = (*pc)[1].type;
  1663.         LONGEST k = (*pc)[2].longconst;

  1664.         (*pc) += 4;
  1665.         gen_int_literal (ax, value, k, type);
  1666.       }
  1667.       break;

  1668.     case OP_VAR_VALUE:
  1669.       gen_var_ref (exp->gdbarch, ax, value, (*pc)[2].symbol);

  1670.       if (value->optimized_out)
  1671.         error (_("`%s' has been optimized out, cannot use"),
  1672.                SYMBOL_PRINT_NAME ((*pc)[2].symbol));

  1673.       (*pc) += 4;
  1674.       break;

  1675.     case OP_REGISTER:
  1676.       {
  1677.         const char *name = &(*pc)[2].string;
  1678.         int reg;

  1679.         (*pc) += 4 + BYTES_TO_EXP_ELEM ((*pc)[1].longconst + 1);
  1680.         reg = user_reg_map_name_to_regnum (exp->gdbarch, name, strlen (name));
  1681.         if (reg == -1)
  1682.           internal_error (__FILE__, __LINE__,
  1683.                           _("Register $%s not available"), name);
  1684.         /* No support for tracing user registers yet.  */
  1685.         if (reg >= gdbarch_num_regs (exp->gdbarch)
  1686.             + gdbarch_num_pseudo_regs (exp->gdbarch))
  1687.           error (_("'%s' is a user-register; "
  1688.                    "GDB cannot yet trace user-register contents."),
  1689.                  name);
  1690.         value->kind = axs_lvalue_register;
  1691.         value->u.reg = reg;
  1692.         value->type = register_type (exp->gdbarch, reg);
  1693.       }
  1694.       break;

  1695.     case OP_INTERNALVAR:
  1696.       {
  1697.         struct internalvar *var = (*pc)[1].internalvar;
  1698.         const char *name = internalvar_name (var);
  1699.         struct trace_state_variable *tsv;

  1700.         (*pc) += 3;
  1701.         tsv = find_trace_state_variable (name);
  1702.         if (tsv)
  1703.           {
  1704.             ax_tsv (ax, aop_getv, tsv->number);
  1705.             if (ax->tracing)
  1706.               ax_tsv (ax, aop_tracev, tsv->number);
  1707.             /* Trace state variables are always 64-bit integers.  */
  1708.             value->kind = axs_rvalue;
  1709.             value->type = builtin_type (exp->gdbarch)->builtin_long_long;
  1710.           }
  1711.         else if (! compile_internalvar_to_ax (var, ax, value))
  1712.           error (_("$%s is not a trace state variable; GDB agent "
  1713.                    "expressions cannot use convenience variables."), name);
  1714.       }
  1715.       break;

  1716.       /* Weirdo operator: see comments for gen_repeat for details.  */
  1717.     case BINOP_REPEAT:
  1718.       /* Note that gen_repeat handles its own argument evaluation.  */
  1719.       (*pc)++;
  1720.       gen_repeat (exp, pc, ax, value);
  1721.       break;

  1722.     case UNOP_CAST:
  1723.       {
  1724.         struct type *type = (*pc)[1].type;

  1725.         (*pc) += 3;
  1726.         gen_expr (exp, pc, ax, value);
  1727.         gen_cast (ax, value, type);
  1728.       }
  1729.       break;

  1730.     case UNOP_CAST_TYPE:
  1731.       {
  1732.         int offset;
  1733.         struct value *val;
  1734.         struct type *type;

  1735.         ++*pc;
  1736.         offset = *pc - exp->elts;
  1737.         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
  1738.         type = value_type (val);
  1739.         *pc = &exp->elts[offset];

  1740.         gen_expr (exp, pc, ax, value);
  1741.         gen_cast (ax, value, type);
  1742.       }
  1743.       break;

  1744.     case UNOP_MEMVAL:
  1745.       {
  1746.         struct type *type = check_typedef ((*pc)[1].type);

  1747.         (*pc) += 3;
  1748.         gen_expr (exp, pc, ax, value);

  1749.         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
  1750.            already have the right value on the stack.  For
  1751.            axs_lvalue_register, we must convert.  */
  1752.         if (value->kind == axs_lvalue_register)
  1753.           require_rvalue (ax, value);

  1754.         value->type = type;
  1755.         value->kind = axs_lvalue_memory;
  1756.       }
  1757.       break;

  1758.     case UNOP_MEMVAL_TYPE:
  1759.       {
  1760.         int offset;
  1761.         struct value *val;
  1762.         struct type *type;

  1763.         ++*pc;
  1764.         offset = *pc - exp->elts;
  1765.         val = evaluate_subexp (NULL, exp, &offset, EVAL_AVOID_SIDE_EFFECTS);
  1766.         type = value_type (val);
  1767.         *pc = &exp->elts[offset];

  1768.         gen_expr (exp, pc, ax, value);

  1769.         /* If we have an axs_rvalue or an axs_lvalue_memory, then we
  1770.            already have the right value on the stack.  For
  1771.            axs_lvalue_register, we must convert.  */
  1772.         if (value->kind == axs_lvalue_register)
  1773.           require_rvalue (ax, value);

  1774.         value->type = type;
  1775.         value->kind = axs_lvalue_memory;
  1776.       }
  1777.       break;

  1778.     case UNOP_PLUS:
  1779.       (*pc)++;
  1780.       /* + FOO is equivalent to 0 + FOO, which can be optimized.  */
  1781.       gen_expr (exp, pc, ax, value);
  1782.       gen_usual_unary (exp, ax, value);
  1783.       break;

  1784.     case UNOP_NEG:
  1785.       (*pc)++;
  1786.       /* -FOO is equivalent to 0 - FOO.  */
  1787.       gen_int_literal (ax, &value1, 0,
  1788.                        builtin_type (exp->gdbarch)->builtin_int);
  1789.       gen_usual_unary (exp, ax, &value1);        /* shouldn't do much */
  1790.       gen_expr (exp, pc, ax, &value2);
  1791.       gen_usual_unary (exp, ax, &value2);
  1792.       gen_usual_arithmetic (exp, ax, &value1, &value2);
  1793.       gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
  1794.       break;

  1795.     case UNOP_LOGICAL_NOT:
  1796.       (*pc)++;
  1797.       gen_expr (exp, pc, ax, value);
  1798.       gen_usual_unary (exp, ax, value);
  1799.       gen_logical_not (ax, value, int_type);
  1800.       break;

  1801.     case UNOP_COMPLEMENT:
  1802.       (*pc)++;
  1803.       gen_expr (exp, pc, ax, value);
  1804.       gen_usual_unary (exp, ax, value);
  1805.       gen_integral_promotions (exp, ax, value);
  1806.       gen_complement (ax, value);
  1807.       break;

  1808.     case UNOP_IND:
  1809.       (*pc)++;
  1810.       gen_expr (exp, pc, ax, value);
  1811.       gen_usual_unary (exp, ax, value);
  1812.       if (!pointer_type (value->type))
  1813.         error (_("Argument of unary `*' is not a pointer."));
  1814.       gen_deref (ax, value);
  1815.       break;

  1816.     case UNOP_ADDR:
  1817.       (*pc)++;
  1818.       gen_expr (exp, pc, ax, value);
  1819.       gen_address_of (ax, value);
  1820.       break;

  1821.     case UNOP_SIZEOF:
  1822.       (*pc)++;
  1823.       /* Notice that gen_sizeof handles its own operand, unlike most
  1824.          of the other unary operator functions.  This is because we
  1825.          have to throw away the code we generate.  */
  1826.       gen_sizeof (exp, pc, ax, value,
  1827.                   builtin_type (exp->gdbarch)->builtin_int);
  1828.       break;

  1829.     case STRUCTOP_STRUCT:
  1830.     case STRUCTOP_PTR:
  1831.       {
  1832.         int length = (*pc)[1].longconst;
  1833.         char *name = &(*pc)[2].string;

  1834.         (*pc) += 4 + BYTES_TO_EXP_ELEM (length + 1);
  1835.         gen_expr (exp, pc, ax, value);
  1836.         if (op == STRUCTOP_STRUCT)
  1837.           gen_struct_ref (exp, ax, value, name, ".", "structure or union");
  1838.         else if (op == STRUCTOP_PTR)
  1839.           gen_struct_ref (exp, ax, value, name, "->",
  1840.                           "pointer to a structure or union");
  1841.         else
  1842.           /* If this `if' chain doesn't handle it, then the case list
  1843.              shouldn't mention it, and we shouldn't be here.  */
  1844.           internal_error (__FILE__, __LINE__,
  1845.                           _("gen_expr: unhandled struct case"));
  1846.       }
  1847.       break;

  1848.     case OP_THIS:
  1849.       {
  1850.         struct symbol *sym, *func;
  1851.         const struct block *b;
  1852.         const struct language_defn *lang;

  1853.         b = block_for_pc (ax->scope);
  1854.         func = block_linkage_function (b);
  1855.         lang = language_def (SYMBOL_LANGUAGE (func));

  1856.         sym = lookup_language_this (lang, b);
  1857.         if (!sym)
  1858.           error (_("no `%s' found"), lang->la_name_of_this);

  1859.         gen_var_ref (exp->gdbarch, ax, value, sym);

  1860.         if (value->optimized_out)
  1861.           error (_("`%s' has been optimized out, cannot use"),
  1862.                  SYMBOL_PRINT_NAME (sym));

  1863.         (*pc) += 2;
  1864.       }
  1865.       break;

  1866.     case OP_SCOPE:
  1867.       {
  1868.         struct type *type = (*pc)[1].type;
  1869.         int length = longest_to_int ((*pc)[2].longconst);
  1870.         char *name = &(*pc)[3].string;
  1871.         int found;

  1872.         found = gen_aggregate_elt_ref (exp, ax, value, type, name,
  1873.                                        "?", "??");
  1874.         if (!found)
  1875.           error (_("There is no field named %s"), name);
  1876.         (*pc) += 5 + BYTES_TO_EXP_ELEM (length + 1);
  1877.       }
  1878.       break;

  1879.     case OP_TYPE:
  1880.     case OP_TYPEOF:
  1881.     case OP_DECLTYPE:
  1882.       error (_("Attempt to use a type name as an expression."));

  1883.     default:
  1884.       error (_("Unsupported operator %s (%d) in expression."),
  1885.              op_name (exp, op), op);
  1886.     }
  1887. }

  1888. /* This handles the middle-to-right-side of code generation for binary
  1889.    expressions, which is shared between regular binary operations and
  1890.    assign-modify (+= and friends) expressions.  */

  1891. static void
  1892. gen_expr_binop_rest (struct expression *exp,
  1893.                      enum exp_opcode op, union exp_element **pc,
  1894.                      struct agent_expr *ax, struct axs_value *value,
  1895.                      struct axs_value *value1, struct axs_value *value2)
  1896. {
  1897.   struct type *int_type = builtin_type (exp->gdbarch)->builtin_int;

  1898.   gen_expr (exp, pc, ax, value2);
  1899.   gen_usual_unary (exp, ax, value2);
  1900.   gen_usual_arithmetic (exp, ax, value1, value2);
  1901.   switch (op)
  1902.     {
  1903.     case BINOP_ADD:
  1904.       if (TYPE_CODE (value1->type) == TYPE_CODE_INT
  1905.           && pointer_type (value2->type))
  1906.         {
  1907.           /* Swap the values and proceed normally.  */
  1908.           ax_simple (ax, aop_swap);
  1909.           gen_ptradd (ax, value, value2, value1);
  1910.         }
  1911.       else if (pointer_type (value1->type)
  1912.                && TYPE_CODE (value2->type) == TYPE_CODE_INT)
  1913.         gen_ptradd (ax, value, value1, value2);
  1914.       else
  1915.         gen_binop (ax, value, value1, value2,
  1916.                    aop_add, aop_add, 1, "addition");
  1917.       break;
  1918.     case BINOP_SUB:
  1919.       if (pointer_type (value1->type)
  1920.           && TYPE_CODE (value2->type) == TYPE_CODE_INT)
  1921.         gen_ptrsub (ax,value, value1, value2);
  1922.       else if (pointer_type (value1->type)
  1923.                && pointer_type (value2->type))
  1924.         /* FIXME --- result type should be ptrdiff_t */
  1925.         gen_ptrdiff (ax, value, value1, value2,
  1926.                      builtin_type (exp->gdbarch)->builtin_long);
  1927.       else
  1928.         gen_binop (ax, value, value1, value2,
  1929.                    aop_sub, aop_sub, 1, "subtraction");
  1930.       break;
  1931.     case BINOP_MUL:
  1932.       gen_binop (ax, value, value1, value2,
  1933.                  aop_mul, aop_mul, 1, "multiplication");
  1934.       break;
  1935.     case BINOP_DIV:
  1936.       gen_binop (ax, value, value1, value2,
  1937.                  aop_div_signed, aop_div_unsigned, 1, "division");
  1938.       break;
  1939.     case BINOP_REM:
  1940.       gen_binop (ax, value, value1, value2,
  1941.                  aop_rem_signed, aop_rem_unsigned, 1, "remainder");
  1942.       break;
  1943.     case BINOP_LSH:
  1944.       gen_binop (ax, value, value1, value2,
  1945.                  aop_lsh, aop_lsh, 1, "left shift");
  1946.       break;
  1947.     case BINOP_RSH:
  1948.       gen_binop (ax, value, value1, value2,
  1949.                  aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
  1950.       break;
  1951.     case BINOP_SUBSCRIPT:
  1952.       {
  1953.         struct type *type;

  1954.         if (binop_types_user_defined_p (op, value1->type, value2->type))
  1955.           {
  1956.             error (_("cannot subscript requested type: "
  1957.                      "cannot call user defined functions"));
  1958.           }
  1959.         else
  1960.           {
  1961.             /* If the user attempts to subscript something that is not
  1962.                an array or pointer type (like a plain int variable for
  1963.                example), then report this as an error.  */
  1964.             type = check_typedef (value1->type);
  1965.             if (TYPE_CODE (type) != TYPE_CODE_ARRAY
  1966.                 && TYPE_CODE (type) != TYPE_CODE_PTR)
  1967.               {
  1968.                 if (TYPE_NAME (type))
  1969.                   error (_("cannot subscript something of type `%s'"),
  1970.                          TYPE_NAME (type));
  1971.                 else
  1972.                   error (_("cannot subscript requested type"));
  1973.               }
  1974.           }

  1975.         if (!is_integral_type (value2->type))
  1976.           error (_("Argument to arithmetic operation "
  1977.                    "not a number or boolean."));

  1978.         gen_ptradd (ax, value, value1, value2);
  1979.         gen_deref (ax, value);
  1980.         break;
  1981.       }
  1982.     case BINOP_BITWISE_AND:
  1983.       gen_binop (ax, value, value1, value2,
  1984.                  aop_bit_and, aop_bit_and, 0, "bitwise and");
  1985.       break;

  1986.     case BINOP_BITWISE_IOR:
  1987.       gen_binop (ax, value, value1, value2,
  1988.                  aop_bit_or, aop_bit_or, 0, "bitwise or");
  1989.       break;

  1990.     case BINOP_BITWISE_XOR:
  1991.       gen_binop (ax, value, value1, value2,
  1992.                  aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
  1993.       break;

  1994.     case BINOP_EQUAL:
  1995.       gen_equal (ax, value, value1, value2, int_type);
  1996.       break;

  1997.     case BINOP_NOTEQUAL:
  1998.       gen_equal (ax, value, value1, value2, int_type);
  1999.       gen_logical_not (ax, value, int_type);
  2000.       break;

  2001.     case BINOP_LESS:
  2002.       gen_less (ax, value, value1, value2, int_type);
  2003.       break;

  2004.     case BINOP_GTR:
  2005.       ax_simple (ax, aop_swap);
  2006.       gen_less (ax, value, value1, value2, int_type);
  2007.       break;

  2008.     case BINOP_LEQ:
  2009.       ax_simple (ax, aop_swap);
  2010.       gen_less (ax, value, value1, value2, int_type);
  2011.       gen_logical_not (ax, value, int_type);
  2012.       break;

  2013.     case BINOP_GEQ:
  2014.       gen_less (ax, value, value1, value2, int_type);
  2015.       gen_logical_not (ax, value, int_type);
  2016.       break;

  2017.     default:
  2018.       /* We should only list operators in the outer case statement
  2019.          that we actually handle in the inner case statement.  */
  2020.       internal_error (__FILE__, __LINE__,
  2021.                       _("gen_expr: op case sets don't match"));
  2022.     }
  2023. }


  2024. /* Given a single variable and a scope, generate bytecodes to trace
  2025.    its value.  This is for use in situations where we have only a
  2026.    variable's name, and no parsed expression; for instance, when the
  2027.    name comes from a list of local variables of a function.  */

  2028. struct agent_expr *
  2029. gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
  2030.                    struct symbol *var, int trace_string)
  2031. {
  2032.   struct cleanup *old_chain = 0;
  2033.   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
  2034.   struct axs_value value;

  2035.   old_chain = make_cleanup_free_agent_expr (ax);

  2036.   ax->tracing = 1;
  2037.   ax->trace_string = trace_string;
  2038.   gen_var_ref (gdbarch, ax, &value, var);

  2039.   /* If there is no actual variable to trace, flag it by returning
  2040.      an empty agent expression.  */
  2041.   if (value.optimized_out)
  2042.     {
  2043.       do_cleanups (old_chain);
  2044.       return NULL;
  2045.     }

  2046.   /* Make sure we record the final object, and get rid of it.  */
  2047.   gen_traced_pop (gdbarch, ax, &value);

  2048.   /* Oh, and terminate.  */
  2049.   ax_simple (ax, aop_end);

  2050.   /* We have successfully built the agent expr, so cancel the cleanup
  2051.      request.  If we add more cleanups that we always want done, this
  2052.      will have to get more complicated.  */
  2053.   discard_cleanups (old_chain);
  2054.   return ax;
  2055. }

  2056. /* Generating bytecode from GDB expressions: driver */

  2057. /* Given a GDB expression EXPR, return bytecode to trace its value.
  2058.    The result will use the `trace' and `trace_quick' bytecodes to
  2059.    record the value of all memory touched by the expression.  The
  2060.    caller can then use the ax_reqs function to discover which
  2061.    registers it relies upon.  */
  2062. struct agent_expr *
  2063. gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
  2064.                     int trace_string)
  2065. {
  2066.   struct cleanup *old_chain = 0;
  2067.   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
  2068.   union exp_element *pc;
  2069.   struct axs_value value;

  2070.   old_chain = make_cleanup_free_agent_expr (ax);

  2071.   pc = expr->elts;
  2072.   ax->tracing = 1;
  2073.   ax->trace_string = trace_string;
  2074.   value.optimized_out = 0;
  2075.   gen_expr (expr, &pc, ax, &value);

  2076.   /* Make sure we record the final object, and get rid of it.  */
  2077.   gen_traced_pop (expr->gdbarch, ax, &value);

  2078.   /* Oh, and terminate.  */
  2079.   ax_simple (ax, aop_end);

  2080.   /* We have successfully built the agent expr, so cancel the cleanup
  2081.      request.  If we add more cleanups that we always want done, this
  2082.      will have to get more complicated.  */
  2083.   discard_cleanups (old_chain);
  2084.   return ax;
  2085. }

  2086. /* Given a GDB expression EXPR, return a bytecode sequence that will
  2087.    evaluate and return a result.  The bytecodes will do a direct
  2088.    evaluation, using the current data on the target, rather than
  2089.    recording blocks of memory and registers for later use, as
  2090.    gen_trace_for_expr does.  The generated bytecode sequence leaves
  2091.    the result of expression evaluation on the top of the stack.  */

  2092. struct agent_expr *
  2093. gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
  2094. {
  2095.   struct cleanup *old_chain = 0;
  2096.   struct agent_expr *ax = new_agent_expr (expr->gdbarch, scope);
  2097.   union exp_element *pc;
  2098.   struct axs_value value;

  2099.   old_chain = make_cleanup_free_agent_expr (ax);

  2100.   pc = expr->elts;
  2101.   ax->tracing = 0;
  2102.   value.optimized_out = 0;
  2103.   gen_expr (expr, &pc, ax, &value);

  2104.   require_rvalue (ax, &value);

  2105.   /* Oh, and terminate.  */
  2106.   ax_simple (ax, aop_end);

  2107.   /* We have successfully built the agent expr, so cancel the cleanup
  2108.      request.  If we add more cleanups that we always want done, this
  2109.      will have to get more complicated.  */
  2110.   discard_cleanups (old_chain);
  2111.   return ax;
  2112. }

  2113. struct agent_expr *
  2114. gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
  2115.                               int trace_string)
  2116. {
  2117.   struct cleanup *old_chain = 0;
  2118.   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
  2119.   struct axs_value value;

  2120.   old_chain = make_cleanup_free_agent_expr (ax);

  2121.   ax->tracing = 1;
  2122.   ax->trace_string = trace_string;

  2123.   gdbarch_gen_return_address (gdbarch, ax, &value, scope);

  2124.   /* Make sure we record the final object, and get rid of it.  */
  2125.   gen_traced_pop (gdbarch, ax, &value);

  2126.   /* Oh, and terminate.  */
  2127.   ax_simple (ax, aop_end);

  2128.   /* We have successfully built the agent expr, so cancel the cleanup
  2129.      request.  If we add more cleanups that we always want done, this
  2130.      will have to get more complicated.  */
  2131.   discard_cleanups (old_chain);
  2132.   return ax;
  2133. }

  2134. /* Given a collection of printf-style arguments, generate code to
  2135.    evaluate the arguments and pass everything to a special
  2136.    bytecode.  */

  2137. struct agent_expr *
  2138. gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
  2139.             CORE_ADDR function, LONGEST channel,
  2140.             const char *format, int fmtlen,
  2141.             struct format_piece *frags,
  2142.             int nargs, struct expression **exprs)
  2143. {
  2144.   struct cleanup *old_chain = 0;
  2145.   struct agent_expr *ax = new_agent_expr (gdbarch, scope);
  2146.   union exp_element *pc;
  2147.   struct axs_value value;
  2148.   int tem;

  2149.   old_chain = make_cleanup_free_agent_expr (ax);

  2150.   /* We're computing values, not doing side effects.  */
  2151.   ax->tracing = 0;

  2152.   /* Evaluate and push the args on the stack in reverse order,
  2153.      for simplicity of collecting them on the target side.  */
  2154.   for (tem = nargs - 1; tem >= 0; --tem)
  2155.     {
  2156.       pc = exprs[tem]->elts;
  2157.       value.optimized_out = 0;
  2158.       gen_expr (exprs[tem], &pc, ax, &value);
  2159.       require_rvalue (ax, &value);
  2160.     }

  2161.   /* Push function and channel.  */
  2162.   ax_const_l (ax, channel);
  2163.   ax_const_l (ax, function);

  2164.   /* Issue the printf bytecode proper.  */
  2165.   ax_simple (ax, aop_printf);
  2166.   ax_simple (ax, nargs);
  2167.   ax_string (ax, format, fmtlen);

  2168.   /* And terminate.  */
  2169.   ax_simple (ax, aop_end);

  2170.   /* We have successfully built the agent expr, so cancel the cleanup
  2171.      request.  If we add more cleanups that we always want done, this
  2172.      will have to get more complicated.  */
  2173.   discard_cleanups (old_chain);

  2174.   return ax;
  2175. }

  2176. static void
  2177. agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
  2178. {
  2179.   struct cleanup *old_chain = 0;
  2180.   struct expression *expr;
  2181.   struct agent_expr *agent;
  2182.   const char *arg;
  2183.   int trace_string = 0;

  2184.   if (!eval)
  2185.     {
  2186.       if (*exp == '/')
  2187.         exp = decode_agent_options (exp, &trace_string);
  2188.     }

  2189.   arg = exp;
  2190.   if (!eval && strcmp (arg, "$_ret") == 0)
  2191.     {
  2192.       agent = gen_trace_for_return_address (pc, get_current_arch (),
  2193.                                             trace_string);
  2194.       old_chain = make_cleanup_free_agent_expr (agent);
  2195.     }
  2196.   else
  2197.     {
  2198.       expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
  2199.       old_chain = make_cleanup (free_current_contents, &expr);
  2200.       if (eval)
  2201.         {
  2202.           gdb_assert (trace_string == 0);
  2203.           agent = gen_eval_for_expr (pc, expr);
  2204.         }
  2205.       else
  2206.         agent = gen_trace_for_expr (pc, expr, trace_string);
  2207.       make_cleanup_free_agent_expr (agent);
  2208.     }

  2209.   ax_reqs (agent);
  2210.   ax_print (gdb_stdout, agent);

  2211.   /* It would be nice to call ax_reqs here to gather some general info
  2212.      about the expression, and then print out the result.  */

  2213.   do_cleanups (old_chain);
  2214.   dont_repeat ();
  2215. }

  2216. static void
  2217. agent_command_1 (char *exp, int eval)
  2218. {
  2219.   /* We don't deal with overlay debugging at the moment.  We need to
  2220.      think more carefully about this.  If you copy this code into
  2221.      another command, change the error message; the user shouldn't
  2222.      have to know anything about agent expressions.  */
  2223.   if (overlay_debugging)
  2224.     error (_("GDB can't do agent expression translation with overlays."));

  2225.   if (exp == 0)
  2226.     error_no_arg (_("expression to translate"));

  2227.   if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
  2228.     {
  2229.       struct linespec_result canonical;
  2230.       int ix;
  2231.       struct linespec_sals *iter;
  2232.       struct cleanup *old_chain;

  2233.       exp = skip_spaces (exp);
  2234.       init_linespec_result (&canonical);
  2235.       decode_line_full (&exp, DECODE_LINE_FUNFIRSTLINE,
  2236.                         (struct symtab *) NULL, 0, &canonical,
  2237.                         NULL, NULL);
  2238.       old_chain = make_cleanup_destroy_linespec_result (&canonical);
  2239.       exp = skip_spaces (exp);
  2240.       if (exp[0] == ',')
  2241.         {
  2242.           exp++;
  2243.           exp = skip_spaces (exp);
  2244.         }
  2245.       for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
  2246.         {
  2247.           int i;

  2248.           for (i = 0; i < iter->sals.nelts; i++)
  2249.             agent_eval_command_one (exp, eval, iter->sals.sals[i].pc);
  2250.         }
  2251.       do_cleanups (old_chain);
  2252.     }
  2253.   else
  2254.     agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));

  2255.   dont_repeat ();
  2256. }

  2257. static void
  2258. agent_command (char *exp, int from_tty)
  2259. {
  2260.   agent_command_1 (exp, 0);
  2261. }

  2262. /* Parse the given expression, compile it into an agent expression
  2263.    that does direct evaluation, and display the resulting
  2264.    expression.  */

  2265. static void
  2266. agent_eval_command (char *exp, int from_tty)
  2267. {
  2268.   agent_command_1 (exp, 1);
  2269. }

  2270. /* Parse the given expression, compile it into an agent expression
  2271.    that does a printf, and display the resulting expression.  */

  2272. static void
  2273. maint_agent_printf_command (char *exp, int from_tty)
  2274. {
  2275.   struct cleanup *old_chain = 0;
  2276.   struct expression *expr;
  2277.   struct expression *argvec[100];
  2278.   struct agent_expr *agent;
  2279.   struct frame_info *fi = get_current_frame ();        /* need current scope */
  2280.   const char *cmdrest;
  2281.   const char *format_start, *format_end;
  2282.   struct format_piece *fpieces;
  2283.   int nargs;

  2284.   /* We don't deal with overlay debugging at the moment.  We need to
  2285.      think more carefully about this.  If you copy this code into
  2286.      another command, change the error message; the user shouldn't
  2287.      have to know anything about agent expressions.  */
  2288.   if (overlay_debugging)
  2289.     error (_("GDB can't do agent expression translation with overlays."));

  2290.   if (exp == 0)
  2291.     error_no_arg (_("expression to translate"));

  2292.   cmdrest = exp;

  2293.   cmdrest = skip_spaces_const (cmdrest);

  2294.   if (*cmdrest++ != '"')
  2295.     error (_("Must start with a format string."));

  2296.   format_start = cmdrest;

  2297.   fpieces = parse_format_string (&cmdrest);

  2298.   old_chain = make_cleanup (free_format_pieces_cleanup, &fpieces);

  2299.   format_end = cmdrest;

  2300.   if (*cmdrest++ != '"')
  2301.     error (_("Bad format string, non-terminated '\"'."));

  2302.   cmdrest = skip_spaces_const (cmdrest);

  2303.   if (*cmdrest != ',' && *cmdrest != 0)
  2304.     error (_("Invalid argument syntax"));

  2305.   if (*cmdrest == ',')
  2306.     cmdrest++;
  2307.   cmdrest = skip_spaces_const (cmdrest);

  2308.   nargs = 0;
  2309.   while (*cmdrest != '\0')
  2310.     {
  2311.       const char *cmd1;

  2312.       cmd1 = cmdrest;
  2313.       expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
  2314.       argvec[nargs] = expr;
  2315.       ++nargs;
  2316.       cmdrest = cmd1;
  2317.       if (*cmdrest == ',')
  2318.         ++cmdrest;
  2319.       /* else complain? */
  2320.     }


  2321.   agent = gen_printf (get_frame_pc (fi), get_current_arch (), 0, 0,
  2322.                       format_start, format_end - format_start,
  2323.                       fpieces, nargs, argvec);
  2324.   make_cleanup_free_agent_expr (agent);
  2325.   ax_reqs (agent);
  2326.   ax_print (gdb_stdout, agent);

  2327.   /* It would be nice to call ax_reqs here to gather some general info
  2328.      about the expression, and then print out the result.  */

  2329.   do_cleanups (old_chain);
  2330.   dont_repeat ();
  2331. }


  2332. /* Initialization code.  */

  2333. void _initialize_ax_gdb (void);
  2334. void
  2335. _initialize_ax_gdb (void)
  2336. {
  2337.   add_cmd ("agent", class_maintenance, agent_command,
  2338.            _("\
  2339. Translate an expression into remote agent bytecode for tracing.\n\
  2340. Usage: maint agent [-at location,] EXPRESSION\n\
  2341. If -at is given, generate remote agent bytecode for this location.\n\
  2342. If not, generate remote agent bytecode for current frame pc address."),
  2343.            &maintenancelist);

  2344.   add_cmd ("agent-eval", class_maintenance, agent_eval_command,
  2345.            _("\
  2346. Translate an expression into remote agent bytecode for evaluation.\n\
  2347. Usage: maint agent-eval [-at location,] EXPRESSION\n\
  2348. If -at is given, generate remote agent bytecode for this location.\n\
  2349. If not, generate remote agent bytecode for current frame pc address."),
  2350.            &maintenancelist);

  2351.   add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
  2352.            _("Translate an expression into remote "
  2353.              "agent bytecode for evaluation and display the bytecodes."),
  2354.            &maintenancelist);
  2355. }