Home | History | Annotate | Line # | Download | only in gdbserver
ax.cc revision 1.1.1.1
      1  1.1  christos /* Agent expression code for remote server.
      2  1.1  christos    Copyright (C) 2009-2020 Free Software Foundation, Inc.
      3  1.1  christos 
      4  1.1  christos    This file is part of GDB.
      5  1.1  christos 
      6  1.1  christos    This program is free software; you can redistribute it and/or modify
      7  1.1  christos    it under the terms of the GNU General Public License as published by
      8  1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9  1.1  christos    (at your option) any later version.
     10  1.1  christos 
     11  1.1  christos    This program is distributed in the hope that it will be useful,
     12  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1  christos    GNU General Public License for more details.
     15  1.1  christos 
     16  1.1  christos    You should have received a copy of the GNU General Public License
     17  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18  1.1  christos 
     19  1.1  christos #include "server.h"
     20  1.1  christos #include "ax.h"
     21  1.1  christos #include "gdbsupport/format.h"
     22  1.1  christos #include "tracepoint.h"
     23  1.1  christos #include "gdbsupport/rsp-low.h"
     24  1.1  christos 
     25  1.1  christos static void ax_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
     26  1.1  christos 
     27  1.1  christos #ifdef IN_PROCESS_AGENT
     28  1.1  christos bool debug_agent = 0;
     29  1.1  christos #endif
     30  1.1  christos 
     31  1.1  christos static void
     32  1.1  christos ax_vdebug (const char *fmt, ...)
     33  1.1  christos {
     34  1.1  christos   char buf[1024];
     35  1.1  christos   va_list ap;
     36  1.1  christos 
     37  1.1  christos   va_start (ap, fmt);
     38  1.1  christos   vsprintf (buf, fmt, ap);
     39  1.1  christos #ifdef IN_PROCESS_AGENT
     40  1.1  christos   fprintf (stderr, PROG "/ax: %s\n", buf);
     41  1.1  christos #else
     42  1.1  christos   debug_printf (PROG "/ax: %s\n", buf);
     43  1.1  christos #endif
     44  1.1  christos   va_end (ap);
     45  1.1  christos }
     46  1.1  christos 
     47  1.1  christos #define ax_debug_1(level, fmt, args...)	\
     48  1.1  christos   do {						\
     49  1.1  christos     if (level <= debug_threads)			\
     50  1.1  christos       ax_vdebug ((fmt), ##args);		\
     51  1.1  christos   } while (0)
     52  1.1  christos 
     53  1.1  christos #define ax_debug(FMT, args...)		\
     54  1.1  christos   ax_debug_1 (1, FMT, ##args)
     55  1.1  christos 
     56  1.1  christos /* This enum must exactly match what is documented in
     57  1.1  christos    gdb/doc/agentexpr.texi, including all the numerical values.  */
     58  1.1  christos 
     59  1.1  christos enum gdb_agent_op
     60  1.1  christos   {
     61  1.1  christos #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  \
     62  1.1  christos     gdb_agent_op_ ## NAME = VALUE,
     63  1.1  christos #include "gdbsupport/ax.def"
     64  1.1  christos #undef DEFOP
     65  1.1  christos     gdb_agent_op_last
     66  1.1  christos   };
     67  1.1  christos 
     68  1.1  christos static const char *gdb_agent_op_names [gdb_agent_op_last] =
     69  1.1  christos   {
     70  1.1  christos     "?undef?"
     71  1.1  christos #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , # NAME
     72  1.1  christos #include "gdbsupport/ax.def"
     73  1.1  christos #undef DEFOP
     74  1.1  christos   };
     75  1.1  christos 
     76  1.1  christos #ifndef IN_PROCESS_AGENT
     77  1.1  christos static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
     78  1.1  christos   {
     79  1.1  christos     0
     80  1.1  christos #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE)  , SIZE
     81  1.1  christos #include "gdbsupport/ax.def"
     82  1.1  christos #undef DEFOP
     83  1.1  christos   };
     84  1.1  christos #endif
     85  1.1  christos 
     86  1.1  christos /* A wrapper for gdb_agent_op_names that does some bounds-checking.  */
     87  1.1  christos 
     88  1.1  christos static const char *
     89  1.1  christos gdb_agent_op_name (int op)
     90  1.1  christos {
     91  1.1  christos   if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
     92  1.1  christos     return "?undef?";
     93  1.1  christos   return gdb_agent_op_names[op];
     94  1.1  christos }
     95  1.1  christos 
     96  1.1  christos #ifndef IN_PROCESS_AGENT
     97  1.1  christos 
     98  1.1  christos /* The packet form of an agent expression consists of an 'X', number
     99  1.1  christos    of bytes in expression, a comma, and then the bytes.  */
    100  1.1  christos 
    101  1.1  christos struct agent_expr *
    102  1.1  christos gdb_parse_agent_expr (const char **actparm)
    103  1.1  christos {
    104  1.1  christos   const char *act = *actparm;
    105  1.1  christos   ULONGEST xlen;
    106  1.1  christos   struct agent_expr *aexpr;
    107  1.1  christos 
    108  1.1  christos   ++act;  /* skip the X */
    109  1.1  christos   act = unpack_varlen_hex (act, &xlen);
    110  1.1  christos   ++act;  /* skip a comma */
    111  1.1  christos   aexpr = XNEW (struct agent_expr);
    112  1.1  christos   aexpr->length = xlen;
    113  1.1  christos   aexpr->bytes = (unsigned char *) xmalloc (xlen);
    114  1.1  christos   hex2bin (act, aexpr->bytes, xlen);
    115  1.1  christos   *actparm = act + (xlen * 2);
    116  1.1  christos   return aexpr;
    117  1.1  christos }
    118  1.1  christos 
    119  1.1  christos void
    120  1.1  christos gdb_free_agent_expr (struct agent_expr *aexpr)
    121  1.1  christos {
    122  1.1  christos   if (aexpr != NULL)
    123  1.1  christos     {
    124  1.1  christos       free (aexpr->bytes);
    125  1.1  christos       free (aexpr);
    126  1.1  christos     }
    127  1.1  christos }
    128  1.1  christos 
    129  1.1  christos /* Convert the bytes of an agent expression back into hex digits, so
    130  1.1  christos    they can be printed or uploaded.  This allocates the buffer,
    131  1.1  christos    callers should free when they are done with it.  */
    132  1.1  christos 
    133  1.1  christos char *
    134  1.1  christos gdb_unparse_agent_expr (struct agent_expr *aexpr)
    135  1.1  christos {
    136  1.1  christos   char *rslt;
    137  1.1  christos 
    138  1.1  christos   rslt = (char *) xmalloc (2 * aexpr->length + 1);
    139  1.1  christos   bin2hex (aexpr->bytes, rslt, aexpr->length);
    140  1.1  christos   return rslt;
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos /* Bytecode compilation.  */
    144  1.1  christos 
    145  1.1  christos CORE_ADDR current_insn_ptr;
    146  1.1  christos 
    147  1.1  christos int emit_error;
    148  1.1  christos 
    149  1.1  christos struct bytecode_address
    150  1.1  christos {
    151  1.1  christos   int pc;
    152  1.1  christos   CORE_ADDR address;
    153  1.1  christos   int goto_pc;
    154  1.1  christos   /* Offset and size of field to be modified in the goto block.  */
    155  1.1  christos   int from_offset, from_size;
    156  1.1  christos   struct bytecode_address *next;
    157  1.1  christos } *bytecode_address_table;
    158  1.1  christos 
    159  1.1  christos void
    160  1.1  christos emit_prologue (void)
    161  1.1  christos {
    162  1.1  christos   target_emit_ops ()->emit_prologue ();
    163  1.1  christos }
    164  1.1  christos 
    165  1.1  christos void
    166  1.1  christos emit_epilogue (void)
    167  1.1  christos {
    168  1.1  christos   target_emit_ops ()->emit_epilogue ();
    169  1.1  christos }
    170  1.1  christos 
    171  1.1  christos static void
    172  1.1  christos emit_add (void)
    173  1.1  christos {
    174  1.1  christos   target_emit_ops ()->emit_add ();
    175  1.1  christos }
    176  1.1  christos 
    177  1.1  christos static void
    178  1.1  christos emit_sub (void)
    179  1.1  christos {
    180  1.1  christos   target_emit_ops ()->emit_sub ();
    181  1.1  christos }
    182  1.1  christos 
    183  1.1  christos static void
    184  1.1  christos emit_mul (void)
    185  1.1  christos {
    186  1.1  christos   target_emit_ops ()->emit_mul ();
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos static void
    190  1.1  christos emit_lsh (void)
    191  1.1  christos {
    192  1.1  christos   target_emit_ops ()->emit_lsh ();
    193  1.1  christos }
    194  1.1  christos 
    195  1.1  christos static void
    196  1.1  christos emit_rsh_signed (void)
    197  1.1  christos {
    198  1.1  christos   target_emit_ops ()->emit_rsh_signed ();
    199  1.1  christos }
    200  1.1  christos 
    201  1.1  christos static void
    202  1.1  christos emit_rsh_unsigned (void)
    203  1.1  christos {
    204  1.1  christos   target_emit_ops ()->emit_rsh_unsigned ();
    205  1.1  christos }
    206  1.1  christos 
    207  1.1  christos static void
    208  1.1  christos emit_ext (int arg)
    209  1.1  christos {
    210  1.1  christos   target_emit_ops ()->emit_ext (arg);
    211  1.1  christos }
    212  1.1  christos 
    213  1.1  christos static void
    214  1.1  christos emit_log_not (void)
    215  1.1  christos {
    216  1.1  christos   target_emit_ops ()->emit_log_not ();
    217  1.1  christos }
    218  1.1  christos 
    219  1.1  christos static void
    220  1.1  christos emit_bit_and (void)
    221  1.1  christos {
    222  1.1  christos   target_emit_ops ()->emit_bit_and ();
    223  1.1  christos }
    224  1.1  christos 
    225  1.1  christos static void
    226  1.1  christos emit_bit_or (void)
    227  1.1  christos {
    228  1.1  christos   target_emit_ops ()->emit_bit_or ();
    229  1.1  christos }
    230  1.1  christos 
    231  1.1  christos static void
    232  1.1  christos emit_bit_xor (void)
    233  1.1  christos {
    234  1.1  christos   target_emit_ops ()->emit_bit_xor ();
    235  1.1  christos }
    236  1.1  christos 
    237  1.1  christos static void
    238  1.1  christos emit_bit_not (void)
    239  1.1  christos {
    240  1.1  christos   target_emit_ops ()->emit_bit_not ();
    241  1.1  christos }
    242  1.1  christos 
    243  1.1  christos static void
    244  1.1  christos emit_equal (void)
    245  1.1  christos {
    246  1.1  christos   target_emit_ops ()->emit_equal ();
    247  1.1  christos }
    248  1.1  christos 
    249  1.1  christos static void
    250  1.1  christos emit_less_signed (void)
    251  1.1  christos {
    252  1.1  christos   target_emit_ops ()->emit_less_signed ();
    253  1.1  christos }
    254  1.1  christos 
    255  1.1  christos static void
    256  1.1  christos emit_less_unsigned (void)
    257  1.1  christos {
    258  1.1  christos   target_emit_ops ()->emit_less_unsigned ();
    259  1.1  christos }
    260  1.1  christos 
    261  1.1  christos static void
    262  1.1  christos emit_ref (int size)
    263  1.1  christos {
    264  1.1  christos   target_emit_ops ()->emit_ref (size);
    265  1.1  christos }
    266  1.1  christos 
    267  1.1  christos static void
    268  1.1  christos emit_if_goto (int *offset_p, int *size_p)
    269  1.1  christos {
    270  1.1  christos   target_emit_ops ()->emit_if_goto (offset_p, size_p);
    271  1.1  christos }
    272  1.1  christos 
    273  1.1  christos static void
    274  1.1  christos emit_goto (int *offset_p, int *size_p)
    275  1.1  christos {
    276  1.1  christos   target_emit_ops ()->emit_goto (offset_p, size_p);
    277  1.1  christos }
    278  1.1  christos 
    279  1.1  christos static void
    280  1.1  christos write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
    281  1.1  christos {
    282  1.1  christos   target_emit_ops ()->write_goto_address (from, to, size);
    283  1.1  christos }
    284  1.1  christos 
    285  1.1  christos static void
    286  1.1  christos emit_const (LONGEST num)
    287  1.1  christos {
    288  1.1  christos   target_emit_ops ()->emit_const (num);
    289  1.1  christos }
    290  1.1  christos 
    291  1.1  christos static void
    292  1.1  christos emit_reg (int reg)
    293  1.1  christos {
    294  1.1  christos   target_emit_ops ()->emit_reg (reg);
    295  1.1  christos }
    296  1.1  christos 
    297  1.1  christos static void
    298  1.1  christos emit_pop (void)
    299  1.1  christos {
    300  1.1  christos   target_emit_ops ()->emit_pop ();
    301  1.1  christos }
    302  1.1  christos 
    303  1.1  christos static void
    304  1.1  christos emit_stack_flush (void)
    305  1.1  christos {
    306  1.1  christos   target_emit_ops ()->emit_stack_flush ();
    307  1.1  christos }
    308  1.1  christos 
    309  1.1  christos static void
    310  1.1  christos emit_zero_ext (int arg)
    311  1.1  christos {
    312  1.1  christos   target_emit_ops ()->emit_zero_ext (arg);
    313  1.1  christos }
    314  1.1  christos 
    315  1.1  christos static void
    316  1.1  christos emit_swap (void)
    317  1.1  christos {
    318  1.1  christos   target_emit_ops ()->emit_swap ();
    319  1.1  christos }
    320  1.1  christos 
    321  1.1  christos static void
    322  1.1  christos emit_stack_adjust (int n)
    323  1.1  christos {
    324  1.1  christos   target_emit_ops ()->emit_stack_adjust (n);
    325  1.1  christos }
    326  1.1  christos 
    327  1.1  christos /* FN's prototype is `LONGEST(*fn)(int)'.  */
    328  1.1  christos 
    329  1.1  christos static void
    330  1.1  christos emit_int_call_1 (CORE_ADDR fn, int arg1)
    331  1.1  christos {
    332  1.1  christos   target_emit_ops ()->emit_int_call_1 (fn, arg1);
    333  1.1  christos }
    334  1.1  christos 
    335  1.1  christos /* FN's prototype is `void(*fn)(int,LONGEST)'.  */
    336  1.1  christos 
    337  1.1  christos static void
    338  1.1  christos emit_void_call_2 (CORE_ADDR fn, int arg1)
    339  1.1  christos {
    340  1.1  christos   target_emit_ops ()->emit_void_call_2 (fn, arg1);
    341  1.1  christos }
    342  1.1  christos 
    343  1.1  christos static void
    344  1.1  christos emit_eq_goto (int *offset_p, int *size_p)
    345  1.1  christos {
    346  1.1  christos   target_emit_ops ()->emit_eq_goto (offset_p, size_p);
    347  1.1  christos }
    348  1.1  christos 
    349  1.1  christos static void
    350  1.1  christos emit_ne_goto (int *offset_p, int *size_p)
    351  1.1  christos {
    352  1.1  christos   target_emit_ops ()->emit_ne_goto (offset_p, size_p);
    353  1.1  christos }
    354  1.1  christos 
    355  1.1  christos static void
    356  1.1  christos emit_lt_goto (int *offset_p, int *size_p)
    357  1.1  christos {
    358  1.1  christos   target_emit_ops ()->emit_lt_goto (offset_p, size_p);
    359  1.1  christos }
    360  1.1  christos 
    361  1.1  christos static void
    362  1.1  christos emit_ge_goto (int *offset_p, int *size_p)
    363  1.1  christos {
    364  1.1  christos   target_emit_ops ()->emit_ge_goto (offset_p, size_p);
    365  1.1  christos }
    366  1.1  christos 
    367  1.1  christos static void
    368  1.1  christos emit_gt_goto (int *offset_p, int *size_p)
    369  1.1  christos {
    370  1.1  christos   target_emit_ops ()->emit_gt_goto (offset_p, size_p);
    371  1.1  christos }
    372  1.1  christos 
    373  1.1  christos static void
    374  1.1  christos emit_le_goto (int *offset_p, int *size_p)
    375  1.1  christos {
    376  1.1  christos   target_emit_ops ()->emit_le_goto (offset_p, size_p);
    377  1.1  christos }
    378  1.1  christos 
    379  1.1  christos /* Scan an agent expression for any evidence that the given PC is the
    380  1.1  christos    target of a jump bytecode in the expression.  */
    381  1.1  christos 
    382  1.1  christos static int
    383  1.1  christos is_goto_target (struct agent_expr *aexpr, int pc)
    384  1.1  christos {
    385  1.1  christos   int i;
    386  1.1  christos   unsigned char op;
    387  1.1  christos 
    388  1.1  christos   for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
    389  1.1  christos     {
    390  1.1  christos       op = aexpr->bytes[i];
    391  1.1  christos 
    392  1.1  christos       if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
    393  1.1  christos 	{
    394  1.1  christos 	  int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
    395  1.1  christos 	  if (target == pc)
    396  1.1  christos 	    return 1;
    397  1.1  christos 	}
    398  1.1  christos     }
    399  1.1  christos 
    400  1.1  christos   return 0;
    401  1.1  christos }
    402  1.1  christos 
    403  1.1  christos /* Given an agent expression, turn it into native code.  */
    404  1.1  christos 
    405  1.1  christos enum eval_result_type
    406  1.1  christos compile_bytecodes (struct agent_expr *aexpr)
    407  1.1  christos {
    408  1.1  christos   int pc = 0;
    409  1.1  christos   int done = 0;
    410  1.1  christos   unsigned char op, next_op;
    411  1.1  christos   int arg;
    412  1.1  christos   /* This is only used to build 64-bit value for constants.  */
    413  1.1  christos   ULONGEST top;
    414  1.1  christos   struct bytecode_address *aentry, *aentry2;
    415  1.1  christos 
    416  1.1  christos #define UNHANDLED					\
    417  1.1  christos   do							\
    418  1.1  christos     {							\
    419  1.1  christos       ax_debug ("Cannot compile op 0x%x\n", op);	\
    420  1.1  christos       return expr_eval_unhandled_opcode;		\
    421  1.1  christos     } while (0)
    422  1.1  christos 
    423  1.1  christos   if (aexpr->length == 0)
    424  1.1  christos     {
    425  1.1  christos       ax_debug ("empty agent expression\n");
    426  1.1  christos       return expr_eval_empty_expression;
    427  1.1  christos     }
    428  1.1  christos 
    429  1.1  christos   bytecode_address_table = NULL;
    430  1.1  christos 
    431  1.1  christos   while (!done)
    432  1.1  christos     {
    433  1.1  christos       op = aexpr->bytes[pc];
    434  1.1  christos 
    435  1.1  christos       ax_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
    436  1.1  christos 
    437  1.1  christos       /* Record the compiled-code address of the bytecode, for use by
    438  1.1  christos 	 jump instructions.  */
    439  1.1  christos       aentry = XNEW (struct bytecode_address);
    440  1.1  christos       aentry->pc = pc;
    441  1.1  christos       aentry->address = current_insn_ptr;
    442  1.1  christos       aentry->goto_pc = -1;
    443  1.1  christos       aentry->from_offset = aentry->from_size = 0;
    444  1.1  christos       aentry->next = bytecode_address_table;
    445  1.1  christos       bytecode_address_table = aentry;
    446  1.1  christos 
    447  1.1  christos       ++pc;
    448  1.1  christos 
    449  1.1  christos       emit_error = 0;
    450  1.1  christos 
    451  1.1  christos       switch (op)
    452  1.1  christos 	{
    453  1.1  christos 	case gdb_agent_op_add:
    454  1.1  christos 	  emit_add ();
    455  1.1  christos 	  break;
    456  1.1  christos 
    457  1.1  christos 	case gdb_agent_op_sub:
    458  1.1  christos 	  emit_sub ();
    459  1.1  christos 	  break;
    460  1.1  christos 
    461  1.1  christos 	case gdb_agent_op_mul:
    462  1.1  christos 	  emit_mul ();
    463  1.1  christos 	  break;
    464  1.1  christos 
    465  1.1  christos 	case gdb_agent_op_div_signed:
    466  1.1  christos 	  UNHANDLED;
    467  1.1  christos 	  break;
    468  1.1  christos 
    469  1.1  christos 	case gdb_agent_op_div_unsigned:
    470  1.1  christos 	  UNHANDLED;
    471  1.1  christos 	  break;
    472  1.1  christos 
    473  1.1  christos 	case gdb_agent_op_rem_signed:
    474  1.1  christos 	  UNHANDLED;
    475  1.1  christos 	  break;
    476  1.1  christos 
    477  1.1  christos 	case gdb_agent_op_rem_unsigned:
    478  1.1  christos 	  UNHANDLED;
    479  1.1  christos 	  break;
    480  1.1  christos 
    481  1.1  christos 	case gdb_agent_op_lsh:
    482  1.1  christos 	  emit_lsh ();
    483  1.1  christos 	  break;
    484  1.1  christos 
    485  1.1  christos 	case gdb_agent_op_rsh_signed:
    486  1.1  christos 	  emit_rsh_signed ();
    487  1.1  christos 	  break;
    488  1.1  christos 
    489  1.1  christos 	case gdb_agent_op_rsh_unsigned:
    490  1.1  christos 	  emit_rsh_unsigned ();
    491  1.1  christos 	  break;
    492  1.1  christos 
    493  1.1  christos 	case gdb_agent_op_trace:
    494  1.1  christos 	  UNHANDLED;
    495  1.1  christos 	  break;
    496  1.1  christos 
    497  1.1  christos 	case gdb_agent_op_trace_quick:
    498  1.1  christos 	  UNHANDLED;
    499  1.1  christos 	  break;
    500  1.1  christos 
    501  1.1  christos 	case gdb_agent_op_log_not:
    502  1.1  christos 	  emit_log_not ();
    503  1.1  christos 	  break;
    504  1.1  christos 
    505  1.1  christos 	case gdb_agent_op_bit_and:
    506  1.1  christos 	  emit_bit_and ();
    507  1.1  christos 	  break;
    508  1.1  christos 
    509  1.1  christos 	case gdb_agent_op_bit_or:
    510  1.1  christos 	  emit_bit_or ();
    511  1.1  christos 	  break;
    512  1.1  christos 
    513  1.1  christos 	case gdb_agent_op_bit_xor:
    514  1.1  christos 	  emit_bit_xor ();
    515  1.1  christos 	  break;
    516  1.1  christos 
    517  1.1  christos 	case gdb_agent_op_bit_not:
    518  1.1  christos 	  emit_bit_not ();
    519  1.1  christos 	  break;
    520  1.1  christos 
    521  1.1  christos 	case gdb_agent_op_equal:
    522  1.1  christos 	  next_op = aexpr->bytes[pc];
    523  1.1  christos 	  if (next_op == gdb_agent_op_if_goto
    524  1.1  christos 	      && !is_goto_target (aexpr, pc)
    525  1.1  christos 	      && target_emit_ops ()->emit_eq_goto)
    526  1.1  christos 	    {
    527  1.1  christos 	      ax_debug ("Combining equal & if_goto");
    528  1.1  christos 	      pc += 1;
    529  1.1  christos 	      aentry->pc = pc;
    530  1.1  christos 	      arg = aexpr->bytes[pc++];
    531  1.1  christos 	      arg = (arg << 8) + aexpr->bytes[pc++];
    532  1.1  christos 	      aentry->goto_pc = arg;
    533  1.1  christos 	      emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
    534  1.1  christos 	    }
    535  1.1  christos 	  else if (next_op == gdb_agent_op_log_not
    536  1.1  christos 		   && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
    537  1.1  christos 		   && !is_goto_target (aexpr, pc + 1)
    538  1.1  christos 		   && target_emit_ops ()->emit_ne_goto)
    539  1.1  christos 	    {
    540  1.1  christos 	      ax_debug ("Combining equal & log_not & if_goto");
    541  1.1  christos 	      pc += 2;
    542  1.1  christos 	      aentry->pc = pc;
    543  1.1  christos 	      arg = aexpr->bytes[pc++];
    544  1.1  christos 	      arg = (arg << 8) + aexpr->bytes[pc++];
    545  1.1  christos 	      aentry->goto_pc = arg;
    546  1.1  christos 	      emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
    547  1.1  christos 	    }
    548  1.1  christos 	  else
    549  1.1  christos 	    emit_equal ();
    550  1.1  christos 	  break;
    551  1.1  christos 
    552  1.1  christos 	case gdb_agent_op_less_signed:
    553  1.1  christos 	  next_op = aexpr->bytes[pc];
    554  1.1  christos 	  if (next_op == gdb_agent_op_if_goto
    555  1.1  christos 	      && !is_goto_target (aexpr, pc))
    556  1.1  christos 	    {
    557  1.1  christos 	      ax_debug ("Combining less_signed & if_goto");
    558  1.1  christos 	      pc += 1;
    559  1.1  christos 	      aentry->pc = pc;
    560  1.1  christos 	      arg = aexpr->bytes[pc++];
    561  1.1  christos 	      arg = (arg << 8) + aexpr->bytes[pc++];
    562  1.1  christos 	      aentry->goto_pc = arg;
    563  1.1  christos 	      emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
    564  1.1  christos 	    }
    565  1.1  christos 	  else if (next_op == gdb_agent_op_log_not
    566  1.1  christos 		   && !is_goto_target (aexpr, pc)
    567  1.1  christos 		   && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
    568  1.1  christos 		   && !is_goto_target (aexpr, pc + 1))
    569  1.1  christos 	    {
    570  1.1  christos 	      ax_debug ("Combining less_signed & log_not & if_goto");
    571  1.1  christos 	      pc += 2;
    572  1.1  christos 	      aentry->pc = pc;
    573  1.1  christos 	      arg = aexpr->bytes[pc++];
    574  1.1  christos 	      arg = (arg << 8) + aexpr->bytes[pc++];
    575  1.1  christos 	      aentry->goto_pc = arg;
    576  1.1  christos 	      emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
    577  1.1  christos 	    }
    578  1.1  christos 	  else
    579  1.1  christos 	    emit_less_signed ();
    580  1.1  christos 	  break;
    581  1.1  christos 
    582  1.1  christos 	case gdb_agent_op_less_unsigned:
    583  1.1  christos 	  emit_less_unsigned ();
    584  1.1  christos 	  break;
    585  1.1  christos 
    586  1.1  christos 	case gdb_agent_op_ext:
    587  1.1  christos 	  arg = aexpr->bytes[pc++];
    588  1.1  christos 	  if (arg < (sizeof (LONGEST) * 8))
    589  1.1  christos 	    emit_ext (arg);
    590  1.1  christos 	  break;
    591  1.1  christos 
    592  1.1  christos 	case gdb_agent_op_ref8:
    593  1.1  christos 	  emit_ref (1);
    594  1.1  christos 	  break;
    595  1.1  christos 
    596  1.1  christos 	case gdb_agent_op_ref16:
    597  1.1  christos 	  emit_ref (2);
    598  1.1  christos 	  break;
    599  1.1  christos 
    600  1.1  christos 	case gdb_agent_op_ref32:
    601  1.1  christos 	  emit_ref (4);
    602  1.1  christos 	  break;
    603  1.1  christos 
    604  1.1  christos 	case gdb_agent_op_ref64:
    605  1.1  christos 	  emit_ref (8);
    606  1.1  christos 	  break;
    607  1.1  christos 
    608  1.1  christos 	case gdb_agent_op_if_goto:
    609  1.1  christos 	  arg = aexpr->bytes[pc++];
    610  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
    611  1.1  christos 	  aentry->goto_pc = arg;
    612  1.1  christos 	  emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
    613  1.1  christos 	  break;
    614  1.1  christos 
    615  1.1  christos 	case gdb_agent_op_goto:
    616  1.1  christos 	  arg = aexpr->bytes[pc++];
    617  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
    618  1.1  christos 	  aentry->goto_pc = arg;
    619  1.1  christos 	  emit_goto (&(aentry->from_offset), &(aentry->from_size));
    620  1.1  christos 	  break;
    621  1.1  christos 
    622  1.1  christos 	case gdb_agent_op_const8:
    623  1.1  christos 	  emit_stack_flush ();
    624  1.1  christos 	  top = aexpr->bytes[pc++];
    625  1.1  christos 	  emit_const (top);
    626  1.1  christos 	  break;
    627  1.1  christos 
    628  1.1  christos 	case gdb_agent_op_const16:
    629  1.1  christos 	  emit_stack_flush ();
    630  1.1  christos 	  top = aexpr->bytes[pc++];
    631  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    632  1.1  christos 	  emit_const (top);
    633  1.1  christos 	  break;
    634  1.1  christos 
    635  1.1  christos 	case gdb_agent_op_const32:
    636  1.1  christos 	  emit_stack_flush ();
    637  1.1  christos 	  top = aexpr->bytes[pc++];
    638  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    639  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    640  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    641  1.1  christos 	  emit_const (top);
    642  1.1  christos 	  break;
    643  1.1  christos 
    644  1.1  christos 	case gdb_agent_op_const64:
    645  1.1  christos 	  emit_stack_flush ();
    646  1.1  christos 	  top = aexpr->bytes[pc++];
    647  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    648  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    649  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    650  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    651  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    652  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    653  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
    654  1.1  christos 	  emit_const (top);
    655  1.1  christos 	  break;
    656  1.1  christos 
    657  1.1  christos 	case gdb_agent_op_reg:
    658  1.1  christos 	  emit_stack_flush ();
    659  1.1  christos 	  arg = aexpr->bytes[pc++];
    660  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
    661  1.1  christos 	  emit_reg (arg);
    662  1.1  christos 	  break;
    663  1.1  christos 
    664  1.1  christos 	case gdb_agent_op_end:
    665  1.1  christos 	  ax_debug ("At end of expression\n");
    666  1.1  christos 
    667  1.1  christos 	  /* Assume there is one stack element left, and that it is
    668  1.1  christos 	     cached in "top" where emit_epilogue can get to it.  */
    669  1.1  christos 	  emit_stack_adjust (1);
    670  1.1  christos 
    671  1.1  christos 	  done = 1;
    672  1.1  christos 	  break;
    673  1.1  christos 
    674  1.1  christos 	case gdb_agent_op_dup:
    675  1.1  christos 	  /* In our design, dup is equivalent to stack flushing.  */
    676  1.1  christos 	  emit_stack_flush ();
    677  1.1  christos 	  break;
    678  1.1  christos 
    679  1.1  christos 	case gdb_agent_op_pop:
    680  1.1  christos 	  emit_pop ();
    681  1.1  christos 	  break;
    682  1.1  christos 
    683  1.1  christos 	case gdb_agent_op_zero_ext:
    684  1.1  christos 	  arg = aexpr->bytes[pc++];
    685  1.1  christos 	  if (arg < (sizeof (LONGEST) * 8))
    686  1.1  christos 	    emit_zero_ext (arg);
    687  1.1  christos 	  break;
    688  1.1  christos 
    689  1.1  christos 	case gdb_agent_op_swap:
    690  1.1  christos 	  next_op = aexpr->bytes[pc];
    691  1.1  christos 	  /* Detect greater-than comparison sequences.  */
    692  1.1  christos 	  if (next_op == gdb_agent_op_less_signed
    693  1.1  christos 	      && !is_goto_target (aexpr, pc)
    694  1.1  christos 	      && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
    695  1.1  christos 	      && !is_goto_target (aexpr, pc + 1))
    696  1.1  christos 	    {
    697  1.1  christos 	      ax_debug ("Combining swap & less_signed & if_goto");
    698  1.1  christos 	      pc += 2;
    699  1.1  christos 	      aentry->pc = pc;
    700  1.1  christos 	      arg = aexpr->bytes[pc++];
    701  1.1  christos 	      arg = (arg << 8) + aexpr->bytes[pc++];
    702  1.1  christos 	      aentry->goto_pc = arg;
    703  1.1  christos 	      emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
    704  1.1  christos 	    }
    705  1.1  christos 	  else if (next_op == gdb_agent_op_less_signed
    706  1.1  christos 		   && !is_goto_target (aexpr, pc)
    707  1.1  christos 		   && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
    708  1.1  christos 		   && !is_goto_target (aexpr, pc + 1)
    709  1.1  christos 		   && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
    710  1.1  christos 		   && !is_goto_target (aexpr, pc + 2))
    711  1.1  christos 	    {
    712  1.1  christos 	      ax_debug ("Combining swap & less_signed & log_not & if_goto");
    713  1.1  christos 	      pc += 3;
    714  1.1  christos 	      aentry->pc = pc;
    715  1.1  christos 	      arg = aexpr->bytes[pc++];
    716  1.1  christos 	      arg = (arg << 8) + aexpr->bytes[pc++];
    717  1.1  christos 	      aentry->goto_pc = arg;
    718  1.1  christos 	      emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
    719  1.1  christos 	    }
    720  1.1  christos 	  else
    721  1.1  christos 	    emit_swap ();
    722  1.1  christos 	  break;
    723  1.1  christos 
    724  1.1  christos 	case gdb_agent_op_getv:
    725  1.1  christos 	  emit_stack_flush ();
    726  1.1  christos 	  arg = aexpr->bytes[pc++];
    727  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
    728  1.1  christos 	  emit_int_call_1 (get_get_tsv_func_addr (),
    729  1.1  christos 			   arg);
    730  1.1  christos 	  break;
    731  1.1  christos 
    732  1.1  christos 	case gdb_agent_op_setv:
    733  1.1  christos 	  arg = aexpr->bytes[pc++];
    734  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
    735  1.1  christos 	  emit_void_call_2 (get_set_tsv_func_addr (),
    736  1.1  christos 			    arg);
    737  1.1  christos 	  break;
    738  1.1  christos 
    739  1.1  christos 	case gdb_agent_op_tracev:
    740  1.1  christos 	  UNHANDLED;
    741  1.1  christos 	  break;
    742  1.1  christos 
    743  1.1  christos 	  /* GDB never (currently) generates any of these ops.  */
    744  1.1  christos 	case gdb_agent_op_float:
    745  1.1  christos 	case gdb_agent_op_ref_float:
    746  1.1  christos 	case gdb_agent_op_ref_double:
    747  1.1  christos 	case gdb_agent_op_ref_long_double:
    748  1.1  christos 	case gdb_agent_op_l_to_d:
    749  1.1  christos 	case gdb_agent_op_d_to_l:
    750  1.1  christos 	case gdb_agent_op_trace16:
    751  1.1  christos 	  UNHANDLED;
    752  1.1  christos 	  break;
    753  1.1  christos 
    754  1.1  christos 	default:
    755  1.1  christos 	  ax_debug ("Agent expression op 0x%x not recognized\n", op);
    756  1.1  christos 	  /* Don't struggle on, things will just get worse.  */
    757  1.1  christos 	  return expr_eval_unrecognized_opcode;
    758  1.1  christos 	}
    759  1.1  christos 
    760  1.1  christos       /* This catches errors that occur in target-specific code
    761  1.1  christos 	 emission.  */
    762  1.1  christos       if (emit_error)
    763  1.1  christos 	{
    764  1.1  christos 	  ax_debug ("Error %d while emitting code for %s\n",
    765  1.1  christos 		    emit_error, gdb_agent_op_name (op));
    766  1.1  christos 	  return expr_eval_unhandled_opcode;
    767  1.1  christos 	}
    768  1.1  christos 
    769  1.1  christos       ax_debug ("Op %s compiled\n", gdb_agent_op_name (op));
    770  1.1  christos     }
    771  1.1  christos 
    772  1.1  christos   /* Now fill in real addresses as goto destinations.  */
    773  1.1  christos   for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
    774  1.1  christos     {
    775  1.1  christos       int written = 0;
    776  1.1  christos 
    777  1.1  christos       if (aentry->goto_pc < 0)
    778  1.1  christos 	continue;
    779  1.1  christos 
    780  1.1  christos       /* Find the location that we are going to, and call back into
    781  1.1  christos 	 target-specific code to write the actual address or
    782  1.1  christos 	 displacement.  */
    783  1.1  christos       for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
    784  1.1  christos 	{
    785  1.1  christos 	  if (aentry2->pc == aentry->goto_pc)
    786  1.1  christos 	    {
    787  1.1  christos 	      ax_debug ("Want to jump from %s to %s\n",
    788  1.1  christos 			paddress (aentry->address),
    789  1.1  christos 			paddress (aentry2->address));
    790  1.1  christos 	      write_goto_address (aentry->address + aentry->from_offset,
    791  1.1  christos 				  aentry2->address, aentry->from_size);
    792  1.1  christos 	      written = 1;
    793  1.1  christos 	      break;
    794  1.1  christos 	    }
    795  1.1  christos 	}
    796  1.1  christos 
    797  1.1  christos       /* Error out if we didn't find a destination.  */
    798  1.1  christos       if (!written)
    799  1.1  christos 	{
    800  1.1  christos 	  ax_debug ("Destination of goto %d not found\n",
    801  1.1  christos 		    aentry->goto_pc);
    802  1.1  christos 	  return expr_eval_invalid_goto;
    803  1.1  christos 	}
    804  1.1  christos     }
    805  1.1  christos 
    806  1.1  christos   return expr_eval_no_error;
    807  1.1  christos }
    808  1.1  christos 
    809  1.1  christos #endif
    810  1.1  christos 
    811  1.1  christos /* Make printf-type calls using arguments supplied from the host.  We
    812  1.1  christos    need to parse the format string ourselves, and call the formatting
    813  1.1  christos    function with one argument at a time, partly because there is no
    814  1.1  christos    safe portable way to construct a varargs call, and partly to serve
    815  1.1  christos    as a security barrier against bad format strings that might get
    816  1.1  christos    in.  */
    817  1.1  christos 
    818  1.1  christos static void
    819  1.1  christos ax_printf (CORE_ADDR fn, CORE_ADDR chan, const char *format,
    820  1.1  christos 	   int nargs, ULONGEST *args)
    821  1.1  christos {
    822  1.1  christos   const char *f = format;
    823  1.1  christos   int i;
    824  1.1  christos   const char *current_substring;
    825  1.1  christos   int nargs_wanted;
    826  1.1  christos 
    827  1.1  christos   ax_debug ("Printf of \"%s\" with %d args", format, nargs);
    828  1.1  christos 
    829  1.1  christos   format_pieces fpieces (&f);
    830  1.1  christos 
    831  1.1  christos   nargs_wanted = 0;
    832  1.1  christos   for (auto &&piece : fpieces)
    833  1.1  christos     if (piece.argclass != literal_piece)
    834  1.1  christos       ++nargs_wanted;
    835  1.1  christos 
    836  1.1  christos   if (nargs != nargs_wanted)
    837  1.1  christos     error (_("Wrong number of arguments for specified format-string"));
    838  1.1  christos 
    839  1.1  christos   i = 0;
    840  1.1  christos   for (auto &&piece : fpieces)
    841  1.1  christos     {
    842  1.1  christos       current_substring = piece.string;
    843  1.1  christos       ax_debug ("current substring is '%s', class is %d",
    844  1.1  christos 		current_substring, piece.argclass);
    845  1.1  christos       switch (piece.argclass)
    846  1.1  christos 	{
    847  1.1  christos 	case string_arg:
    848  1.1  christos 	  {
    849  1.1  christos 	    gdb_byte *str;
    850  1.1  christos 	    CORE_ADDR tem;
    851  1.1  christos 	    int j;
    852  1.1  christos 
    853  1.1  christos 	    tem = args[i];
    854  1.1  christos 	    if (tem == 0)
    855  1.1  christos 	      {
    856  1.1  christos 		printf (current_substring, "(null)");
    857  1.1  christos 		break;
    858  1.1  christos 	      }
    859  1.1  christos 
    860  1.1  christos 	    /* This is a %s argument.  Find the length of the string.  */
    861  1.1  christos 	    for (j = 0;; j++)
    862  1.1  christos 	      {
    863  1.1  christos 		gdb_byte c;
    864  1.1  christos 
    865  1.1  christos 		read_inferior_memory (tem + j, &c, 1);
    866  1.1  christos 		if (c == 0)
    867  1.1  christos 		  break;
    868  1.1  christos 	      }
    869  1.1  christos 
    870  1.1  christos 	      /* Copy the string contents into a string inside GDB.  */
    871  1.1  christos 	      str = (gdb_byte *) alloca (j + 1);
    872  1.1  christos 	      if (j != 0)
    873  1.1  christos 		read_inferior_memory (tem, str, j);
    874  1.1  christos 	      str[j] = 0;
    875  1.1  christos 
    876  1.1  christos               printf (current_substring, (char *) str);
    877  1.1  christos 	    }
    878  1.1  christos 	    break;
    879  1.1  christos 
    880  1.1  christos 	  case long_long_arg:
    881  1.1  christos #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
    882  1.1  christos 	    {
    883  1.1  christos 	      long long val = args[i];
    884  1.1  christos 
    885  1.1  christos               printf (current_substring, val);
    886  1.1  christos 	      break;
    887  1.1  christos 	    }
    888  1.1  christos #else
    889  1.1  christos 	    error (_("long long not supported in agent printf"));
    890  1.1  christos #endif
    891  1.1  christos 	case int_arg:
    892  1.1  christos 	  {
    893  1.1  christos 	    int val = args[i];
    894  1.1  christos 
    895  1.1  christos 	    printf (current_substring, val);
    896  1.1  christos 	    break;
    897  1.1  christos 	  }
    898  1.1  christos 
    899  1.1  christos 	case long_arg:
    900  1.1  christos 	  {
    901  1.1  christos 	    long val = args[i];
    902  1.1  christos 
    903  1.1  christos 	    printf (current_substring, val);
    904  1.1  christos 	    break;
    905  1.1  christos 	  }
    906  1.1  christos 
    907  1.1  christos 	case size_t_arg:
    908  1.1  christos 	  {
    909  1.1  christos 	    size_t val = args[i];
    910  1.1  christos 
    911  1.1  christos 	    printf (current_substring, val);
    912  1.1  christos 	    break;
    913  1.1  christos 	  }
    914  1.1  christos 
    915  1.1  christos 	case literal_piece:
    916  1.1  christos 	  /* Print a portion of the format string that has no
    917  1.1  christos 	     directives.  Note that this will not include any
    918  1.1  christos 	     ordinary %-specs, but it might include "%%".  That is
    919  1.1  christos 	     why we use printf_filtered and not puts_filtered here.
    920  1.1  christos 	     Also, we pass a dummy argument because some platforms
    921  1.1  christos 	     have modified GCC to include -Wformat-security by
    922  1.1  christos 	     default, which will warn here if there is no
    923  1.1  christos 	     argument.  */
    924  1.1  christos 	  printf (current_substring, 0);
    925  1.1  christos 	  break;
    926  1.1  christos 
    927  1.1  christos 	default:
    928  1.1  christos 	  error (_("Format directive in '%s' not supported in agent printf"),
    929  1.1  christos 		 current_substring);
    930  1.1  christos 	}
    931  1.1  christos 
    932  1.1  christos       /* Maybe advance to the next argument.  */
    933  1.1  christos       if (piece.argclass != literal_piece)
    934  1.1  christos 	++i;
    935  1.1  christos     }
    936  1.1  christos 
    937  1.1  christos   fflush (stdout);
    938  1.1  christos }
    939  1.1  christos 
    940  1.1  christos /* The agent expression evaluator, as specified by the GDB docs. It
    941  1.1  christos    returns 0 if everything went OK, and a nonzero error code
    942  1.1  christos    otherwise.  */
    943  1.1  christos 
    944  1.1  christos enum eval_result_type
    945  1.1  christos gdb_eval_agent_expr (struct eval_agent_expr_context *ctx,
    946  1.1  christos 		     struct agent_expr *aexpr,
    947  1.1  christos 		     ULONGEST *rslt)
    948  1.1  christos {
    949  1.1  christos   int pc = 0;
    950  1.1  christos #define STACK_MAX 100
    951  1.1  christos   ULONGEST stack[STACK_MAX], top;
    952  1.1  christos   int sp = 0;
    953  1.1  christos   unsigned char op;
    954  1.1  christos   int arg;
    955  1.1  christos 
    956  1.1  christos   /* This union is a convenient way to convert representations.  For
    957  1.1  christos      now, assume a standard architecture where the hardware integer
    958  1.1  christos      types have 8, 16, 32, 64 bit types.  A more robust solution would
    959  1.1  christos      be to import stdint.h from gnulib.  */
    960  1.1  christos   union
    961  1.1  christos   {
    962  1.1  christos     union
    963  1.1  christos     {
    964  1.1  christos       unsigned char bytes[1];
    965  1.1  christos       unsigned char val;
    966  1.1  christos     } u8;
    967  1.1  christos     union
    968  1.1  christos     {
    969  1.1  christos       unsigned char bytes[2];
    970  1.1  christos       unsigned short val;
    971  1.1  christos     } u16;
    972  1.1  christos     union
    973  1.1  christos     {
    974  1.1  christos       unsigned char bytes[4];
    975  1.1  christos       unsigned int val;
    976  1.1  christos     } u32;
    977  1.1  christos     union
    978  1.1  christos     {
    979  1.1  christos       unsigned char bytes[8];
    980  1.1  christos       ULONGEST val;
    981  1.1  christos     } u64;
    982  1.1  christos   } cnv;
    983  1.1  christos 
    984  1.1  christos   if (aexpr->length == 0)
    985  1.1  christos     {
    986  1.1  christos       ax_debug ("empty agent expression");
    987  1.1  christos       return expr_eval_empty_expression;
    988  1.1  christos     }
    989  1.1  christos 
    990  1.1  christos   /* Cache the stack top in its own variable. Much of the time we can
    991  1.1  christos      operate on this variable, rather than dinking with the stack. It
    992  1.1  christos      needs to be copied to the stack when sp changes.  */
    993  1.1  christos   top = 0;
    994  1.1  christos 
    995  1.1  christos   while (1)
    996  1.1  christos     {
    997  1.1  christos       op = aexpr->bytes[pc++];
    998  1.1  christos 
    999  1.1  christos       ax_debug ("About to interpret byte 0x%x", op);
   1000  1.1  christos 
   1001  1.1  christos       switch (op)
   1002  1.1  christos 	{
   1003  1.1  christos 	case gdb_agent_op_add:
   1004  1.1  christos 	  top += stack[--sp];
   1005  1.1  christos 	  break;
   1006  1.1  christos 
   1007  1.1  christos 	case gdb_agent_op_sub:
   1008  1.1  christos 	  top = stack[--sp] - top;
   1009  1.1  christos 	  break;
   1010  1.1  christos 
   1011  1.1  christos 	case gdb_agent_op_mul:
   1012  1.1  christos 	  top *= stack[--sp];
   1013  1.1  christos 	  break;
   1014  1.1  christos 
   1015  1.1  christos 	case gdb_agent_op_div_signed:
   1016  1.1  christos 	  if (top == 0)
   1017  1.1  christos 	    {
   1018  1.1  christos 	      ax_debug ("Attempted to divide by zero");
   1019  1.1  christos 	      return expr_eval_divide_by_zero;
   1020  1.1  christos 	    }
   1021  1.1  christos 	  top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
   1022  1.1  christos 	  break;
   1023  1.1  christos 
   1024  1.1  christos 	case gdb_agent_op_div_unsigned:
   1025  1.1  christos 	  if (top == 0)
   1026  1.1  christos 	    {
   1027  1.1  christos 	      ax_debug ("Attempted to divide by zero");
   1028  1.1  christos 	      return expr_eval_divide_by_zero;
   1029  1.1  christos 	    }
   1030  1.1  christos 	  top = stack[--sp] / top;
   1031  1.1  christos 	  break;
   1032  1.1  christos 
   1033  1.1  christos 	case gdb_agent_op_rem_signed:
   1034  1.1  christos 	  if (top == 0)
   1035  1.1  christos 	    {
   1036  1.1  christos 	      ax_debug ("Attempted to divide by zero");
   1037  1.1  christos 	      return expr_eval_divide_by_zero;
   1038  1.1  christos 	    }
   1039  1.1  christos 	  top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
   1040  1.1  christos 	  break;
   1041  1.1  christos 
   1042  1.1  christos 	case gdb_agent_op_rem_unsigned:
   1043  1.1  christos 	  if (top == 0)
   1044  1.1  christos 	    {
   1045  1.1  christos 	      ax_debug ("Attempted to divide by zero");
   1046  1.1  christos 	      return expr_eval_divide_by_zero;
   1047  1.1  christos 	    }
   1048  1.1  christos 	  top = stack[--sp] % top;
   1049  1.1  christos 	  break;
   1050  1.1  christos 
   1051  1.1  christos 	case gdb_agent_op_lsh:
   1052  1.1  christos 	  top = stack[--sp] << top;
   1053  1.1  christos 	  break;
   1054  1.1  christos 
   1055  1.1  christos 	case gdb_agent_op_rsh_signed:
   1056  1.1  christos 	  top = ((LONGEST) stack[--sp]) >> top;
   1057  1.1  christos 	  break;
   1058  1.1  christos 
   1059  1.1  christos 	case gdb_agent_op_rsh_unsigned:
   1060  1.1  christos 	  top = stack[--sp] >> top;
   1061  1.1  christos 	  break;
   1062  1.1  christos 
   1063  1.1  christos 	case gdb_agent_op_trace:
   1064  1.1  christos 	  agent_mem_read (ctx, NULL, (CORE_ADDR) stack[--sp],
   1065  1.1  christos 			  (ULONGEST) top);
   1066  1.1  christos 	  if (--sp >= 0)
   1067  1.1  christos 	    top = stack[sp];
   1068  1.1  christos 	  break;
   1069  1.1  christos 
   1070  1.1  christos 	case gdb_agent_op_trace_quick:
   1071  1.1  christos 	  arg = aexpr->bytes[pc++];
   1072  1.1  christos 	  agent_mem_read (ctx, NULL, (CORE_ADDR) top, (ULONGEST) arg);
   1073  1.1  christos 	  break;
   1074  1.1  christos 
   1075  1.1  christos 	case gdb_agent_op_log_not:
   1076  1.1  christos 	  top = !top;
   1077  1.1  christos 	  break;
   1078  1.1  christos 
   1079  1.1  christos 	case gdb_agent_op_bit_and:
   1080  1.1  christos 	  top &= stack[--sp];
   1081  1.1  christos 	  break;
   1082  1.1  christos 
   1083  1.1  christos 	case gdb_agent_op_bit_or:
   1084  1.1  christos 	  top |= stack[--sp];
   1085  1.1  christos 	  break;
   1086  1.1  christos 
   1087  1.1  christos 	case gdb_agent_op_bit_xor:
   1088  1.1  christos 	  top ^= stack[--sp];
   1089  1.1  christos 	  break;
   1090  1.1  christos 
   1091  1.1  christos 	case gdb_agent_op_bit_not:
   1092  1.1  christos 	  top = ~top;
   1093  1.1  christos 	  break;
   1094  1.1  christos 
   1095  1.1  christos 	case gdb_agent_op_equal:
   1096  1.1  christos 	  top = (stack[--sp] == top);
   1097  1.1  christos 	  break;
   1098  1.1  christos 
   1099  1.1  christos 	case gdb_agent_op_less_signed:
   1100  1.1  christos 	  top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
   1101  1.1  christos 	  break;
   1102  1.1  christos 
   1103  1.1  christos 	case gdb_agent_op_less_unsigned:
   1104  1.1  christos 	  top = (stack[--sp] < top);
   1105  1.1  christos 	  break;
   1106  1.1  christos 
   1107  1.1  christos 	case gdb_agent_op_ext:
   1108  1.1  christos 	  arg = aexpr->bytes[pc++];
   1109  1.1  christos 	  if (arg < (sizeof (LONGEST) * 8))
   1110  1.1  christos 	    {
   1111  1.1  christos 	      LONGEST mask = 1 << (arg - 1);
   1112  1.1  christos 	      top &= ((LONGEST) 1 << arg) - 1;
   1113  1.1  christos 	      top = (top ^ mask) - mask;
   1114  1.1  christos 	    }
   1115  1.1  christos 	  break;
   1116  1.1  christos 
   1117  1.1  christos 	case gdb_agent_op_ref8:
   1118  1.1  christos 	  agent_mem_read (ctx, cnv.u8.bytes, (CORE_ADDR) top, 1);
   1119  1.1  christos 	  top = cnv.u8.val;
   1120  1.1  christos 	  break;
   1121  1.1  christos 
   1122  1.1  christos 	case gdb_agent_op_ref16:
   1123  1.1  christos 	  agent_mem_read (ctx, cnv.u16.bytes, (CORE_ADDR) top, 2);
   1124  1.1  christos 	  top = cnv.u16.val;
   1125  1.1  christos 	  break;
   1126  1.1  christos 
   1127  1.1  christos 	case gdb_agent_op_ref32:
   1128  1.1  christos 	  agent_mem_read (ctx, cnv.u32.bytes, (CORE_ADDR) top, 4);
   1129  1.1  christos 	  top = cnv.u32.val;
   1130  1.1  christos 	  break;
   1131  1.1  christos 
   1132  1.1  christos 	case gdb_agent_op_ref64:
   1133  1.1  christos 	  agent_mem_read (ctx, cnv.u64.bytes, (CORE_ADDR) top, 8);
   1134  1.1  christos 	  top = cnv.u64.val;
   1135  1.1  christos 	  break;
   1136  1.1  christos 
   1137  1.1  christos 	case gdb_agent_op_if_goto:
   1138  1.1  christos 	  if (top)
   1139  1.1  christos 	    pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
   1140  1.1  christos 	  else
   1141  1.1  christos 	    pc += 2;
   1142  1.1  christos 	  if (--sp >= 0)
   1143  1.1  christos 	    top = stack[sp];
   1144  1.1  christos 	  break;
   1145  1.1  christos 
   1146  1.1  christos 	case gdb_agent_op_goto:
   1147  1.1  christos 	  pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
   1148  1.1  christos 	  break;
   1149  1.1  christos 
   1150  1.1  christos 	case gdb_agent_op_const8:
   1151  1.1  christos 	  /* Flush the cached stack top.  */
   1152  1.1  christos 	  stack[sp++] = top;
   1153  1.1  christos 	  top = aexpr->bytes[pc++];
   1154  1.1  christos 	  break;
   1155  1.1  christos 
   1156  1.1  christos 	case gdb_agent_op_const16:
   1157  1.1  christos 	  /* Flush the cached stack top.  */
   1158  1.1  christos 	  stack[sp++] = top;
   1159  1.1  christos 	  top = aexpr->bytes[pc++];
   1160  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1161  1.1  christos 	  break;
   1162  1.1  christos 
   1163  1.1  christos 	case gdb_agent_op_const32:
   1164  1.1  christos 	  /* Flush the cached stack top.  */
   1165  1.1  christos 	  stack[sp++] = top;
   1166  1.1  christos 	  top = aexpr->bytes[pc++];
   1167  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1168  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1169  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1170  1.1  christos 	  break;
   1171  1.1  christos 
   1172  1.1  christos 	case gdb_agent_op_const64:
   1173  1.1  christos 	  /* Flush the cached stack top.  */
   1174  1.1  christos 	  stack[sp++] = top;
   1175  1.1  christos 	  top = aexpr->bytes[pc++];
   1176  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1177  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1178  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1179  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1180  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1181  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1182  1.1  christos 	  top = (top << 8) + aexpr->bytes[pc++];
   1183  1.1  christos 	  break;
   1184  1.1  christos 
   1185  1.1  christos 	case gdb_agent_op_reg:
   1186  1.1  christos 	  /* Flush the cached stack top.  */
   1187  1.1  christos 	  stack[sp++] = top;
   1188  1.1  christos 	  arg = aexpr->bytes[pc++];
   1189  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
   1190  1.1  christos 	  {
   1191  1.1  christos 	    int regnum = arg;
   1192  1.1  christos 	    struct regcache *regcache = ctx->regcache;
   1193  1.1  christos 
   1194  1.1  christos 	    switch (register_size (regcache->tdesc, regnum))
   1195  1.1  christos 	      {
   1196  1.1  christos 	      case 8:
   1197  1.1  christos 		collect_register (regcache, regnum, cnv.u64.bytes);
   1198  1.1  christos 		top = cnv.u64.val;
   1199  1.1  christos 		break;
   1200  1.1  christos 	      case 4:
   1201  1.1  christos 		collect_register (regcache, regnum, cnv.u32.bytes);
   1202  1.1  christos 		top = cnv.u32.val;
   1203  1.1  christos 		break;
   1204  1.1  christos 	      case 2:
   1205  1.1  christos 		collect_register (regcache, regnum, cnv.u16.bytes);
   1206  1.1  christos 		top = cnv.u16.val;
   1207  1.1  christos 		break;
   1208  1.1  christos 	      case 1:
   1209  1.1  christos 		collect_register (regcache, regnum, cnv.u8.bytes);
   1210  1.1  christos 		top = cnv.u8.val;
   1211  1.1  christos 		break;
   1212  1.1  christos 	      default:
   1213  1.1  christos 		internal_error (__FILE__, __LINE__,
   1214  1.1  christos 				"unhandled register size");
   1215  1.1  christos 	      }
   1216  1.1  christos 	  }
   1217  1.1  christos 	  break;
   1218  1.1  christos 
   1219  1.1  christos 	case gdb_agent_op_end:
   1220  1.1  christos 	  ax_debug ("At end of expression, sp=%d, stack top cache=0x%s",
   1221  1.1  christos 		    sp, pulongest (top));
   1222  1.1  christos 	  if (rslt)
   1223  1.1  christos 	    {
   1224  1.1  christos 	      if (sp <= 0)
   1225  1.1  christos 		{
   1226  1.1  christos 		  /* This should be an error */
   1227  1.1  christos 		  ax_debug ("Stack is empty, nothing to return");
   1228  1.1  christos 		  return expr_eval_empty_stack;
   1229  1.1  christos 		}
   1230  1.1  christos 	      *rslt = top;
   1231  1.1  christos 	    }
   1232  1.1  christos 	  return expr_eval_no_error;
   1233  1.1  christos 
   1234  1.1  christos 	case gdb_agent_op_dup:
   1235  1.1  christos 	  stack[sp++] = top;
   1236  1.1  christos 	  break;
   1237  1.1  christos 
   1238  1.1  christos 	case gdb_agent_op_pop:
   1239  1.1  christos 	  if (--sp >= 0)
   1240  1.1  christos 	    top = stack[sp];
   1241  1.1  christos 	  break;
   1242  1.1  christos 
   1243  1.1  christos 	case gdb_agent_op_pick:
   1244  1.1  christos 	  arg = aexpr->bytes[pc++];
   1245  1.1  christos 	  stack[sp] = top;
   1246  1.1  christos 	  top = stack[sp - arg];
   1247  1.1  christos 	  ++sp;
   1248  1.1  christos 	  break;
   1249  1.1  christos 
   1250  1.1  christos 	case gdb_agent_op_rot:
   1251  1.1  christos 	  {
   1252  1.1  christos 	    ULONGEST tem = stack[sp - 1];
   1253  1.1  christos 
   1254  1.1  christos 	    stack[sp - 1] = stack[sp - 2];
   1255  1.1  christos 	    stack[sp - 2] = top;
   1256  1.1  christos 	    top = tem;
   1257  1.1  christos 	  }
   1258  1.1  christos 	  break;
   1259  1.1  christos 
   1260  1.1  christos 	case gdb_agent_op_zero_ext:
   1261  1.1  christos 	  arg = aexpr->bytes[pc++];
   1262  1.1  christos 	  if (arg < (sizeof (LONGEST) * 8))
   1263  1.1  christos 	    top &= ((LONGEST) 1 << arg) - 1;
   1264  1.1  christos 	  break;
   1265  1.1  christos 
   1266  1.1  christos 	case gdb_agent_op_swap:
   1267  1.1  christos 	  /* Interchange top two stack elements, making sure top gets
   1268  1.1  christos 	     copied back onto stack.  */
   1269  1.1  christos 	  stack[sp] = top;
   1270  1.1  christos 	  top = stack[sp - 1];
   1271  1.1  christos 	  stack[sp - 1] = stack[sp];
   1272  1.1  christos 	  break;
   1273  1.1  christos 
   1274  1.1  christos 	case gdb_agent_op_getv:
   1275  1.1  christos 	  /* Flush the cached stack top.  */
   1276  1.1  christos 	  stack[sp++] = top;
   1277  1.1  christos 	  arg = aexpr->bytes[pc++];
   1278  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
   1279  1.1  christos 	  top = agent_get_trace_state_variable_value (arg);
   1280  1.1  christos 	  break;
   1281  1.1  christos 
   1282  1.1  christos 	case gdb_agent_op_setv:
   1283  1.1  christos 	  arg = aexpr->bytes[pc++];
   1284  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
   1285  1.1  christos 	  agent_set_trace_state_variable_value (arg, top);
   1286  1.1  christos 	  /* Note that we leave the value on the stack, for the
   1287  1.1  christos 	     benefit of later/enclosing expressions.  */
   1288  1.1  christos 	  break;
   1289  1.1  christos 
   1290  1.1  christos 	case gdb_agent_op_tracev:
   1291  1.1  christos 	  arg = aexpr->bytes[pc++];
   1292  1.1  christos 	  arg = (arg << 8) + aexpr->bytes[pc++];
   1293  1.1  christos 	  agent_tsv_read (ctx, arg);
   1294  1.1  christos 	  break;
   1295  1.1  christos 
   1296  1.1  christos 	case gdb_agent_op_tracenz:
   1297  1.1  christos 	  agent_mem_read_string (ctx, NULL, (CORE_ADDR) stack[--sp],
   1298  1.1  christos 				 (ULONGEST) top);
   1299  1.1  christos 	  if (--sp >= 0)
   1300  1.1  christos 	    top = stack[sp];
   1301  1.1  christos 	  break;
   1302  1.1  christos 
   1303  1.1  christos 	case gdb_agent_op_printf:
   1304  1.1  christos 	  {
   1305  1.1  christos 	    int nargs, slen, i;
   1306  1.1  christos 	    CORE_ADDR fn = 0, chan = 0;
   1307  1.1  christos 	    /* Can't have more args than the entire size of the stack.  */
   1308  1.1  christos 	    ULONGEST args[STACK_MAX];
   1309  1.1  christos 	    char *format;
   1310  1.1  christos 
   1311  1.1  christos 	    nargs = aexpr->bytes[pc++];
   1312  1.1  christos 	    slen = aexpr->bytes[pc++];
   1313  1.1  christos 	    slen = (slen << 8) + aexpr->bytes[pc++];
   1314  1.1  christos 	    format = (char *) &(aexpr->bytes[pc]);
   1315  1.1  christos 	    pc += slen;
   1316  1.1  christos 	    /* Pop function and channel.  */
   1317  1.1  christos 	    fn = top;
   1318  1.1  christos 	    if (--sp >= 0)
   1319  1.1  christos 	      top = stack[sp];
   1320  1.1  christos 	    chan = top;
   1321  1.1  christos 	    if (--sp >= 0)
   1322  1.1  christos 	      top = stack[sp];
   1323  1.1  christos 	    /* Pop arguments into a dedicated array.  */
   1324  1.1  christos 	    for (i = 0; i < nargs; ++i)
   1325  1.1  christos 	      {
   1326  1.1  christos 		args[i] = top;
   1327  1.1  christos 		if (--sp >= 0)
   1328  1.1  christos 		  top = stack[sp];
   1329  1.1  christos 	      }
   1330  1.1  christos 
   1331  1.1  christos 	    /* A bad format string means something is very wrong; give
   1332  1.1  christos 	       up immediately.  */
   1333  1.1  christos 	    if (format[slen - 1] != '\0')
   1334  1.1  christos 	      error (_("Unterminated format string in printf bytecode"));
   1335  1.1  christos 
   1336  1.1  christos 	    ax_printf (fn, chan, format, nargs, args);
   1337  1.1  christos 	  }
   1338  1.1  christos 	  break;
   1339  1.1  christos 
   1340  1.1  christos 	  /* GDB never (currently) generates any of these ops.  */
   1341  1.1  christos 	case gdb_agent_op_float:
   1342  1.1  christos 	case gdb_agent_op_ref_float:
   1343  1.1  christos 	case gdb_agent_op_ref_double:
   1344  1.1  christos 	case gdb_agent_op_ref_long_double:
   1345  1.1  christos 	case gdb_agent_op_l_to_d:
   1346  1.1  christos 	case gdb_agent_op_d_to_l:
   1347  1.1  christos 	case gdb_agent_op_trace16:
   1348  1.1  christos 	  ax_debug ("Agent expression op 0x%x valid, but not handled",
   1349  1.1  christos 		    op);
   1350  1.1  christos 	  /* If ever GDB generates any of these, we don't have the
   1351  1.1  christos 	     option of ignoring.  */
   1352  1.1  christos 	  return expr_eval_unhandled_opcode;
   1353  1.1  christos 
   1354  1.1  christos 	default:
   1355  1.1  christos 	  ax_debug ("Agent expression op 0x%x not recognized", op);
   1356  1.1  christos 	  /* Don't struggle on, things will just get worse.  */
   1357  1.1  christos 	  return expr_eval_unrecognized_opcode;
   1358  1.1  christos 	}
   1359  1.1  christos 
   1360  1.1  christos       /* Check for stack badness.  */
   1361  1.1  christos       if (sp >= (STACK_MAX - 1))
   1362  1.1  christos 	{
   1363  1.1  christos 	  ax_debug ("Expression stack overflow");
   1364  1.1  christos 	  return expr_eval_stack_overflow;
   1365  1.1  christos 	}
   1366  1.1  christos 
   1367  1.1  christos       if (sp < 0)
   1368  1.1  christos 	{
   1369  1.1  christos 	  ax_debug ("Expression stack underflow");
   1370  1.1  christos 	  return expr_eval_stack_underflow;
   1371  1.1  christos 	}
   1372  1.1  christos 
   1373  1.1  christos       ax_debug ("Op %s -> sp=%d, top=0x%s",
   1374  1.1  christos 		gdb_agent_op_name (op), sp, phex_nz (top, 0));
   1375  1.1  christos     }
   1376  1.1  christos }
   1377