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