xref: /dflybsd-src/contrib/gdb-7/gdb/ax-general.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* Functions for manipulating expressions designed to be executed on the agent
2*c50c785cSJohn Marino    Copyright (C) 1998, 1999, 2000, 2007, 2008, 2009, 2010, 2011
35796c8dcSSimon Schubert    Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert /* Despite what the above comment says about this file being part of
215796c8dcSSimon Schubert    GDB, we would like to keep these functions free of GDB
225796c8dcSSimon Schubert    dependencies, since we want to be able to use them in contexts
235796c8dcSSimon Schubert    outside of GDB (test suites, the stub, etc.)  */
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert #include "defs.h"
265796c8dcSSimon Schubert #include "ax.h"
275796c8dcSSimon Schubert 
285796c8dcSSimon Schubert #include "value.h"
295796c8dcSSimon Schubert #include "gdb_string.h"
305796c8dcSSimon Schubert 
31*c50c785cSJohn Marino #include "user-regs.h"
32*c50c785cSJohn Marino 
335796c8dcSSimon Schubert static void grow_expr (struct agent_expr *x, int n);
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert static void append_const (struct agent_expr *x, LONGEST val, int n);
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert static LONGEST read_const (struct agent_expr *x, int o, int n);
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
405796c8dcSSimon Schubert 
415796c8dcSSimon Schubert /* Functions for building expressions.  */
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert /* Allocate a new, empty agent expression.  */
445796c8dcSSimon Schubert struct agent_expr *
45cf7f2e2dSJohn Marino new_agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope)
465796c8dcSSimon Schubert {
475796c8dcSSimon Schubert   struct agent_expr *x = xmalloc (sizeof (*x));
48cf7f2e2dSJohn Marino 
495796c8dcSSimon Schubert   x->len = 0;
505796c8dcSSimon Schubert   x->size = 1;			/* Change this to a larger value once
515796c8dcSSimon Schubert 				   reallocation code is tested.  */
525796c8dcSSimon Schubert   x->buf = xmalloc (x->size);
53cf7f2e2dSJohn Marino 
54cf7f2e2dSJohn Marino   x->gdbarch = gdbarch;
555796c8dcSSimon Schubert   x->scope = scope;
565796c8dcSSimon Schubert 
57cf7f2e2dSJohn Marino   /* Bit vector for registers used.  */
58cf7f2e2dSJohn Marino   x->reg_mask_len = 1;
59cf7f2e2dSJohn Marino   x->reg_mask = xmalloc (x->reg_mask_len * sizeof (x->reg_mask[0]));
60cf7f2e2dSJohn Marino   memset (x->reg_mask, 0, x->reg_mask_len * sizeof (x->reg_mask[0]));
61cf7f2e2dSJohn Marino 
625796c8dcSSimon Schubert   return x;
635796c8dcSSimon Schubert }
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert /* Free a agent expression.  */
665796c8dcSSimon Schubert void
675796c8dcSSimon Schubert free_agent_expr (struct agent_expr *x)
685796c8dcSSimon Schubert {
695796c8dcSSimon Schubert   xfree (x->buf);
70cf7f2e2dSJohn Marino   xfree (x->reg_mask);
715796c8dcSSimon Schubert   xfree (x);
725796c8dcSSimon Schubert }
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert static void
755796c8dcSSimon Schubert do_free_agent_expr_cleanup (void *x)
765796c8dcSSimon Schubert {
775796c8dcSSimon Schubert   free_agent_expr (x);
785796c8dcSSimon Schubert }
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert struct cleanup *
815796c8dcSSimon Schubert make_cleanup_free_agent_expr (struct agent_expr *x)
825796c8dcSSimon Schubert {
835796c8dcSSimon Schubert   return make_cleanup (do_free_agent_expr_cleanup, x);
845796c8dcSSimon Schubert }
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert /* Make sure that X has room for at least N more bytes.  This doesn't
885796c8dcSSimon Schubert    affect the length, just the allocated size.  */
895796c8dcSSimon Schubert static void
905796c8dcSSimon Schubert grow_expr (struct agent_expr *x, int n)
915796c8dcSSimon Schubert {
925796c8dcSSimon Schubert   if (x->len + n > x->size)
935796c8dcSSimon Schubert     {
945796c8dcSSimon Schubert       x->size *= 2;
955796c8dcSSimon Schubert       if (x->size < x->len + n)
965796c8dcSSimon Schubert 	x->size = x->len + n + 10;
975796c8dcSSimon Schubert       x->buf = xrealloc (x->buf, x->size);
985796c8dcSSimon Schubert     }
995796c8dcSSimon Schubert }
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert /* Append the low N bytes of VAL as an N-byte integer to the
1035796c8dcSSimon Schubert    expression X, in big-endian order.  */
1045796c8dcSSimon Schubert static void
1055796c8dcSSimon Schubert append_const (struct agent_expr *x, LONGEST val, int n)
1065796c8dcSSimon Schubert {
1075796c8dcSSimon Schubert   int i;
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert   grow_expr (x, n);
1105796c8dcSSimon Schubert   for (i = n - 1; i >= 0; i--)
1115796c8dcSSimon Schubert     {
1125796c8dcSSimon Schubert       x->buf[x->len + i] = val & 0xff;
1135796c8dcSSimon Schubert       val >>= 8;
1145796c8dcSSimon Schubert     }
1155796c8dcSSimon Schubert   x->len += n;
1165796c8dcSSimon Schubert }
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert /* Extract an N-byte big-endian unsigned integer from expression X at
1205796c8dcSSimon Schubert    offset O.  */
1215796c8dcSSimon Schubert static LONGEST
1225796c8dcSSimon Schubert read_const (struct agent_expr *x, int o, int n)
1235796c8dcSSimon Schubert {
1245796c8dcSSimon Schubert   int i;
1255796c8dcSSimon Schubert   LONGEST accum = 0;
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert   /* Make sure we're not reading off the end of the expression.  */
1285796c8dcSSimon Schubert   if (o + n > x->len)
1295796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert   for (i = 0; i < n; i++)
1325796c8dcSSimon Schubert     accum = (accum << 8) | x->buf[o + i];
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert   return accum;
1355796c8dcSSimon Schubert }
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert /* Append a simple operator OP to EXPR.  */
1395796c8dcSSimon Schubert void
1405796c8dcSSimon Schubert ax_simple (struct agent_expr *x, enum agent_op op)
1415796c8dcSSimon Schubert {
1425796c8dcSSimon Schubert   grow_expr (x, 1);
1435796c8dcSSimon Schubert   x->buf[x->len++] = op;
1445796c8dcSSimon Schubert }
1455796c8dcSSimon Schubert 
146*c50c785cSJohn Marino /* Append a pick operator to EXPR.  DEPTH is the stack item to pick,
147*c50c785cSJohn Marino    with 0 being top of stack.  */
148*c50c785cSJohn Marino 
149*c50c785cSJohn Marino void
150*c50c785cSJohn Marino ax_pick (struct agent_expr *x, int depth)
151*c50c785cSJohn Marino {
152*c50c785cSJohn Marino   if (depth < 0 || depth > 255)
153*c50c785cSJohn Marino     error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
154*c50c785cSJohn Marino   ax_simple (x, aop_pick);
155*c50c785cSJohn Marino   append_const (x, 1, depth);
156*c50c785cSJohn Marino }
157*c50c785cSJohn Marino 
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert /* Append a sign-extension or zero-extension instruction to EXPR, to
1605796c8dcSSimon Schubert    extend an N-bit value.  */
1615796c8dcSSimon Schubert static void
1625796c8dcSSimon Schubert generic_ext (struct agent_expr *x, enum agent_op op, int n)
1635796c8dcSSimon Schubert {
1645796c8dcSSimon Schubert   /* N must fit in a byte.  */
1655796c8dcSSimon Schubert   if (n < 0 || n > 255)
1665796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
1675796c8dcSSimon Schubert   /* That had better be enough range.  */
1685796c8dcSSimon Schubert   if (sizeof (LONGEST) * 8 > 255)
169*c50c785cSJohn Marino     error (_("GDB bug: ax-general.c (generic_ext): "
170*c50c785cSJohn Marino 	     "opcode has inadequate range"));
1715796c8dcSSimon Schubert 
1725796c8dcSSimon Schubert   grow_expr (x, 2);
1735796c8dcSSimon Schubert   x->buf[x->len++] = op;
1745796c8dcSSimon Schubert   x->buf[x->len++] = n;
1755796c8dcSSimon Schubert }
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert /* Append a sign-extension instruction to EXPR, to extend an N-bit value.  */
1795796c8dcSSimon Schubert void
1805796c8dcSSimon Schubert ax_ext (struct agent_expr *x, int n)
1815796c8dcSSimon Schubert {
1825796c8dcSSimon Schubert   generic_ext (x, aop_ext, n);
1835796c8dcSSimon Schubert }
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert /* Append a zero-extension instruction to EXPR, to extend an N-bit value.  */
1875796c8dcSSimon Schubert void
1885796c8dcSSimon Schubert ax_zero_ext (struct agent_expr *x, int n)
1895796c8dcSSimon Schubert {
1905796c8dcSSimon Schubert   generic_ext (x, aop_zero_ext, n);
1915796c8dcSSimon Schubert }
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert /* Append a trace_quick instruction to EXPR, to record N bytes.  */
1955796c8dcSSimon Schubert void
1965796c8dcSSimon Schubert ax_trace_quick (struct agent_expr *x, int n)
1975796c8dcSSimon Schubert {
1985796c8dcSSimon Schubert   /* N must fit in a byte.  */
1995796c8dcSSimon Schubert   if (n < 0 || n > 255)
200*c50c785cSJohn Marino     error (_("GDB bug: ax-general.c (ax_trace_quick): "
201*c50c785cSJohn Marino 	     "size out of range for trace_quick"));
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert   grow_expr (x, 2);
2045796c8dcSSimon Schubert   x->buf[x->len++] = aop_trace_quick;
2055796c8dcSSimon Schubert   x->buf[x->len++] = n;
2065796c8dcSSimon Schubert }
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert /* Append a goto op to EXPR.  OP is the actual op (must be aop_goto or
2105796c8dcSSimon Schubert    aop_if_goto).  We assume we don't know the target offset yet,
2115796c8dcSSimon Schubert    because it's probably a forward branch, so we leave space in EXPR
2125796c8dcSSimon Schubert    for the target, and return the offset in EXPR of that space, so we
2135796c8dcSSimon Schubert    can backpatch it once we do know the target offset.  Use ax_label
2145796c8dcSSimon Schubert    to do the backpatching.  */
2155796c8dcSSimon Schubert int
2165796c8dcSSimon Schubert ax_goto (struct agent_expr *x, enum agent_op op)
2175796c8dcSSimon Schubert {
2185796c8dcSSimon Schubert   grow_expr (x, 3);
2195796c8dcSSimon Schubert   x->buf[x->len + 0] = op;
2205796c8dcSSimon Schubert   x->buf[x->len + 1] = 0xff;
2215796c8dcSSimon Schubert   x->buf[x->len + 2] = 0xff;
2225796c8dcSSimon Schubert   x->len += 3;
2235796c8dcSSimon Schubert   return x->len - 2;
2245796c8dcSSimon Schubert }
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert /* Suppose a given call to ax_goto returns some value PATCH.  When you
2275796c8dcSSimon Schubert    know the offset TARGET that goto should jump to, call
2285796c8dcSSimon Schubert    ax_label (EXPR, PATCH, TARGET)
2295796c8dcSSimon Schubert    to patch TARGET into the ax_goto instruction.  */
2305796c8dcSSimon Schubert void
2315796c8dcSSimon Schubert ax_label (struct agent_expr *x, int patch, int target)
2325796c8dcSSimon Schubert {
2335796c8dcSSimon Schubert   /* Make sure the value is in range.  Don't accept 0xffff as an
2345796c8dcSSimon Schubert      offset; that's our magic sentinel value for unpatched branches.  */
2355796c8dcSSimon Schubert   if (target < 0 || target >= 0xffff)
2365796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
2375796c8dcSSimon Schubert 
2385796c8dcSSimon Schubert   x->buf[patch] = (target >> 8) & 0xff;
2395796c8dcSSimon Schubert   x->buf[patch + 1] = target & 0xff;
2405796c8dcSSimon Schubert }
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert 
2435796c8dcSSimon Schubert /* Assemble code to push a constant on the stack.  */
2445796c8dcSSimon Schubert void
2455796c8dcSSimon Schubert ax_const_l (struct agent_expr *x, LONGEST l)
2465796c8dcSSimon Schubert {
2475796c8dcSSimon Schubert   static enum agent_op ops[]
2485796c8dcSSimon Schubert   =
2495796c8dcSSimon Schubert   {aop_const8, aop_const16, aop_const32, aop_const64};
2505796c8dcSSimon Schubert   int size;
2515796c8dcSSimon Schubert   int op;
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert   /* How big is the number?  'op' keeps track of which opcode to use.
2545796c8dcSSimon Schubert      Notice that we don't really care whether the original number was
2555796c8dcSSimon Schubert      signed or unsigned; we always reproduce the value exactly, and
2565796c8dcSSimon Schubert      use the shortest representation.  */
2575796c8dcSSimon Schubert   for (op = 0, size = 8; size < 64; size *= 2, op++)
2585796c8dcSSimon Schubert     {
259cf7f2e2dSJohn Marino       LONGEST lim = ((LONGEST) 1) << (size - 1);
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert       if (-lim <= l && l <= lim - 1)
2625796c8dcSSimon Schubert         break;
2635796c8dcSSimon Schubert     }
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert   /* Emit the right opcode...  */
2665796c8dcSSimon Schubert   ax_simple (x, ops[op]);
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert   /* Emit the low SIZE bytes as an unsigned number.  We know that
2695796c8dcSSimon Schubert      sign-extending this will yield l.  */
2705796c8dcSSimon Schubert   append_const (x, l, size / 8);
2715796c8dcSSimon Schubert 
2725796c8dcSSimon Schubert   /* Now, if it was negative, and not full-sized, sign-extend it.  */
2735796c8dcSSimon Schubert   if (l < 0 && size < 64)
2745796c8dcSSimon Schubert     ax_ext (x, size);
2755796c8dcSSimon Schubert }
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert void
2795796c8dcSSimon Schubert ax_const_d (struct agent_expr *x, LONGEST d)
2805796c8dcSSimon Schubert {
2815796c8dcSSimon Schubert   /* FIXME: floating-point support not present yet.  */
282*c50c785cSJohn Marino   error (_("GDB bug: ax-general.c (ax_const_d): "
283*c50c785cSJohn Marino 	   "floating point not supported yet"));
2845796c8dcSSimon Schubert }
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert 
2875796c8dcSSimon Schubert /* Assemble code to push the value of register number REG on the
2885796c8dcSSimon Schubert    stack.  */
2895796c8dcSSimon Schubert void
2905796c8dcSSimon Schubert ax_reg (struct agent_expr *x, int reg)
2915796c8dcSSimon Schubert {
292*c50c785cSJohn Marino   if (reg >= gdbarch_num_regs (x->gdbarch))
293*c50c785cSJohn Marino     {
294*c50c785cSJohn Marino       /* This is a pseudo-register.  */
295*c50c785cSJohn Marino       if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch))
296*c50c785cSJohn Marino 	error (_("'%s' is a pseudo-register; "
297*c50c785cSJohn Marino 		 "GDB cannot yet trace its contents."),
298*c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (x->gdbarch, reg));
299*c50c785cSJohn Marino       if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg))
300*c50c785cSJohn Marino 	error (_("Trace '%s' failed."),
301*c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (x->gdbarch, reg));
302*c50c785cSJohn Marino     }
303*c50c785cSJohn Marino   else
304*c50c785cSJohn Marino     {
3055796c8dcSSimon Schubert       /* Make sure the register number is in range.  */
3065796c8dcSSimon Schubert       if (reg < 0 || reg > 0xffff)
307*c50c785cSJohn Marino         error (_("GDB bug: ax-general.c (ax_reg): "
308*c50c785cSJohn Marino 		 "register number out of range"));
3095796c8dcSSimon Schubert       grow_expr (x, 3);
3105796c8dcSSimon Schubert       x->buf[x->len] = aop_reg;
3115796c8dcSSimon Schubert       x->buf[x->len + 1] = (reg >> 8) & 0xff;
3125796c8dcSSimon Schubert       x->buf[x->len + 2] = (reg) & 0xff;
3135796c8dcSSimon Schubert       x->len += 3;
3145796c8dcSSimon Schubert     }
315*c50c785cSJohn Marino }
316cf7f2e2dSJohn Marino 
317cf7f2e2dSJohn Marino /* Assemble code to operate on a trace state variable.  */
318cf7f2e2dSJohn Marino 
319cf7f2e2dSJohn Marino void
320cf7f2e2dSJohn Marino ax_tsv (struct agent_expr *x, enum agent_op op, int num)
321cf7f2e2dSJohn Marino {
322cf7f2e2dSJohn Marino   /* Make sure the tsv number is in range.  */
323cf7f2e2dSJohn Marino   if (num < 0 || num > 0xffff)
324*c50c785cSJohn Marino     internal_error (__FILE__, __LINE__,
325*c50c785cSJohn Marino 		    _("ax-general.c (ax_tsv): variable "
326*c50c785cSJohn Marino 		      "number is %d, out of range"), num);
327cf7f2e2dSJohn Marino 
328cf7f2e2dSJohn Marino   grow_expr (x, 3);
329cf7f2e2dSJohn Marino   x->buf[x->len] = op;
330cf7f2e2dSJohn Marino   x->buf[x->len + 1] = (num >> 8) & 0xff;
331cf7f2e2dSJohn Marino   x->buf[x->len + 2] = (num) & 0xff;
332cf7f2e2dSJohn Marino   x->len += 3;
333cf7f2e2dSJohn Marino }
3345796c8dcSSimon Schubert 
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert 
3375796c8dcSSimon Schubert /* Functions for disassembling agent expressions, and otherwise
3385796c8dcSSimon Schubert    debugging the expression compiler.  */
3395796c8dcSSimon Schubert 
3405796c8dcSSimon Schubert struct aop_map aop_map[] =
3415796c8dcSSimon Schubert {
342*c50c785cSJohn Marino   {0, 0, 0, 0, 0}
343*c50c785cSJohn Marino #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
344*c50c785cSJohn Marino   , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
345*c50c785cSJohn Marino #include "ax.def"
346*c50c785cSJohn Marino #undef DEFOP
3475796c8dcSSimon Schubert };
3485796c8dcSSimon Schubert 
3495796c8dcSSimon Schubert 
3505796c8dcSSimon Schubert /* Disassemble the expression EXPR, writing to F.  */
3515796c8dcSSimon Schubert void
3525796c8dcSSimon Schubert ax_print (struct ui_file *f, struct agent_expr *x)
3535796c8dcSSimon Schubert {
3545796c8dcSSimon Schubert   int i;
3555796c8dcSSimon Schubert   int is_float = 0;
3565796c8dcSSimon Schubert 
357cf7f2e2dSJohn Marino   fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
358cf7f2e2dSJohn Marino   fprintf_filtered (f, _("Reg mask:"));
359cf7f2e2dSJohn Marino   for (i = 0; i < x->reg_mask_len; ++i)
360cf7f2e2dSJohn Marino     fprintf_filtered (f, _(" %02x"), x->reg_mask[i]);
361cf7f2e2dSJohn Marino   fprintf_filtered (f, _("\n"));
362cf7f2e2dSJohn Marino 
3635796c8dcSSimon Schubert   /* Check the size of the name array against the number of entries in
3645796c8dcSSimon Schubert      the enum, to catch additions that people didn't sync.  */
3655796c8dcSSimon Schubert   if ((sizeof (aop_map) / sizeof (aop_map[0]))
3665796c8dcSSimon Schubert       != aop_last)
3675796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));
3685796c8dcSSimon Schubert 
3695796c8dcSSimon Schubert   for (i = 0; i < x->len;)
3705796c8dcSSimon Schubert     {
3715796c8dcSSimon Schubert       enum agent_op op = x->buf[i];
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert       if (op >= (sizeof (aop_map) / sizeof (aop_map[0]))
3745796c8dcSSimon Schubert 	  || !aop_map[op].name)
3755796c8dcSSimon Schubert 	{
3765796c8dcSSimon Schubert 	  fprintf_filtered (f, _("%3d  <bad opcode %02x>\n"), i, op);
3775796c8dcSSimon Schubert 	  i++;
3785796c8dcSSimon Schubert 	  continue;
3795796c8dcSSimon Schubert 	}
3805796c8dcSSimon Schubert       if (i + 1 + aop_map[op].op_size > x->len)
3815796c8dcSSimon Schubert 	{
3825796c8dcSSimon Schubert 	  fprintf_filtered (f, _("%3d  <incomplete opcode %s>\n"),
3835796c8dcSSimon Schubert 			    i, aop_map[op].name);
3845796c8dcSSimon Schubert 	  break;
3855796c8dcSSimon Schubert 	}
3865796c8dcSSimon Schubert 
3875796c8dcSSimon Schubert       fprintf_filtered (f, "%3d  %s", i, aop_map[op].name);
3885796c8dcSSimon Schubert       if (aop_map[op].op_size > 0)
3895796c8dcSSimon Schubert 	{
3905796c8dcSSimon Schubert 	  fputs_filtered (" ", f);
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert 	  print_longest (f, 'd', 0,
3935796c8dcSSimon Schubert 			 read_const (x, i + 1, aop_map[op].op_size));
3945796c8dcSSimon Schubert 	}
3955796c8dcSSimon Schubert       fprintf_filtered (f, "\n");
3965796c8dcSSimon Schubert       i += 1 + aop_map[op].op_size;
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert       is_float = (op == aop_float);
3995796c8dcSSimon Schubert     }
4005796c8dcSSimon Schubert }
4015796c8dcSSimon Schubert 
402cf7f2e2dSJohn Marino /* Add register REG to the register mask for expression AX.  */
4035796c8dcSSimon Schubert void
404cf7f2e2dSJohn Marino ax_reg_mask (struct agent_expr *ax, int reg)
405cf7f2e2dSJohn Marino {
406*c50c785cSJohn Marino   if (reg >= gdbarch_num_regs (ax->gdbarch))
407*c50c785cSJohn Marino     {
408*c50c785cSJohn Marino       /* This is a pseudo-register.  */
409*c50c785cSJohn Marino       if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
410*c50c785cSJohn Marino 	error (_("'%s' is a pseudo-register; "
411*c50c785cSJohn Marino 		 "GDB cannot yet trace its contents."),
412*c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (ax->gdbarch, reg));
413*c50c785cSJohn Marino       if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
414*c50c785cSJohn Marino 	error (_("Trace '%s' failed."),
415*c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (ax->gdbarch, reg));
416*c50c785cSJohn Marino     }
417*c50c785cSJohn Marino   else
418*c50c785cSJohn Marino     {
419cf7f2e2dSJohn Marino       int byte = reg / 8;
420cf7f2e2dSJohn Marino 
421cf7f2e2dSJohn Marino       /* Grow the bit mask if necessary.  */
422cf7f2e2dSJohn Marino       if (byte >= ax->reg_mask_len)
423cf7f2e2dSJohn Marino         {
424cf7f2e2dSJohn Marino           /* It's not appropriate to double here.  This isn't a
425cf7f2e2dSJohn Marino 	     string buffer.  */
426cf7f2e2dSJohn Marino           int new_len = byte + 1;
427cf7f2e2dSJohn Marino           unsigned char *new_reg_mask = xrealloc (ax->reg_mask,
428*c50c785cSJohn Marino 					          new_len
429*c50c785cSJohn Marino 					          * sizeof (ax->reg_mask[0]));
430cf7f2e2dSJohn Marino           memset (new_reg_mask + ax->reg_mask_len, 0,
431cf7f2e2dSJohn Marino 	          (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0]));
432cf7f2e2dSJohn Marino           ax->reg_mask_len = new_len;
433cf7f2e2dSJohn Marino           ax->reg_mask = new_reg_mask;
434cf7f2e2dSJohn Marino         }
435cf7f2e2dSJohn Marino 
436cf7f2e2dSJohn Marino       ax->reg_mask[byte] |= 1 << (reg % 8);
437cf7f2e2dSJohn Marino     }
438*c50c785cSJohn Marino }
439cf7f2e2dSJohn Marino 
440cf7f2e2dSJohn Marino /* Given an agent expression AX, fill in requirements and other descriptive
441cf7f2e2dSJohn Marino    bits.  */
442cf7f2e2dSJohn Marino void
443cf7f2e2dSJohn Marino ax_reqs (struct agent_expr *ax)
4445796c8dcSSimon Schubert {
4455796c8dcSSimon Schubert   int i;
4465796c8dcSSimon Schubert   int height;
4475796c8dcSSimon Schubert 
4485796c8dcSSimon Schubert   /* Jump target table.  targets[i] is non-zero iff we have found a
4495796c8dcSSimon Schubert      jump to offset i.  */
4505796c8dcSSimon Schubert   char *targets = (char *) alloca (ax->len * sizeof (targets[0]));
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert   /* Instruction boundary table.  boundary[i] is non-zero iff our scan
4535796c8dcSSimon Schubert      has reached an instruction starting at offset i.  */
4545796c8dcSSimon Schubert   char *boundary = (char *) alloca (ax->len * sizeof (boundary[0]));
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert   /* Stack height record.  If either targets[i] or boundary[i] is
4575796c8dcSSimon Schubert      non-zero, heights[i] is the height the stack should have before
4585796c8dcSSimon Schubert      executing the bytecode at that point.  */
4595796c8dcSSimon Schubert   int *heights = (int *) alloca (ax->len * sizeof (heights[0]));
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert   /* Pointer to a description of the present op.  */
4625796c8dcSSimon Schubert   struct aop_map *op;
4635796c8dcSSimon Schubert 
4645796c8dcSSimon Schubert   memset (targets, 0, ax->len * sizeof (targets[0]));
4655796c8dcSSimon Schubert   memset (boundary, 0, ax->len * sizeof (boundary[0]));
4665796c8dcSSimon Schubert 
467cf7f2e2dSJohn Marino   ax->max_height = ax->min_height = height = 0;
468cf7f2e2dSJohn Marino   ax->flaw = agent_flaw_none;
469cf7f2e2dSJohn Marino   ax->max_data_size = 0;
4705796c8dcSSimon Schubert 
4715796c8dcSSimon Schubert   for (i = 0; i < ax->len; i += 1 + op->op_size)
4725796c8dcSSimon Schubert     {
4735796c8dcSSimon Schubert       if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0])))
4745796c8dcSSimon Schubert 	{
475cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_bad_instruction;
4765796c8dcSSimon Schubert 	  return;
4775796c8dcSSimon Schubert 	}
4785796c8dcSSimon Schubert 
4795796c8dcSSimon Schubert       op = &aop_map[ax->buf[i]];
4805796c8dcSSimon Schubert 
4815796c8dcSSimon Schubert       if (!op->name)
4825796c8dcSSimon Schubert 	{
483cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_bad_instruction;
4845796c8dcSSimon Schubert 	  return;
4855796c8dcSSimon Schubert 	}
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert       if (i + 1 + op->op_size > ax->len)
4885796c8dcSSimon Schubert 	{
489cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_incomplete_instruction;
4905796c8dcSSimon Schubert 	  return;
4915796c8dcSSimon Schubert 	}
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert       /* If this instruction is a forward jump target, does the
4945796c8dcSSimon Schubert          current stack height match the stack height at the jump
4955796c8dcSSimon Schubert          source?  */
4965796c8dcSSimon Schubert       if (targets[i] && (heights[i] != height))
4975796c8dcSSimon Schubert 	{
498cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_height_mismatch;
4995796c8dcSSimon Schubert 	  return;
5005796c8dcSSimon Schubert 	}
5015796c8dcSSimon Schubert 
5025796c8dcSSimon Schubert       boundary[i] = 1;
5035796c8dcSSimon Schubert       heights[i] = height;
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert       height -= op->consumed;
506cf7f2e2dSJohn Marino       if (height < ax->min_height)
507cf7f2e2dSJohn Marino 	ax->min_height = height;
5085796c8dcSSimon Schubert       height += op->produced;
509cf7f2e2dSJohn Marino       if (height > ax->max_height)
510cf7f2e2dSJohn Marino 	ax->max_height = height;
5115796c8dcSSimon Schubert 
512cf7f2e2dSJohn Marino       if (op->data_size > ax->max_data_size)
513cf7f2e2dSJohn Marino 	ax->max_data_size = op->data_size;
5145796c8dcSSimon Schubert 
5155796c8dcSSimon Schubert       /* For jump instructions, check that the target is a valid
5165796c8dcSSimon Schubert          offset.  If it is, record the fact that that location is a
5175796c8dcSSimon Schubert          jump target, and record the height we expect there.  */
5185796c8dcSSimon Schubert       if (aop_goto == op - aop_map
5195796c8dcSSimon Schubert 	  || aop_if_goto == op - aop_map)
5205796c8dcSSimon Schubert 	{
5215796c8dcSSimon Schubert 	  int target = read_const (ax, i + 1, 2);
5225796c8dcSSimon Schubert 	  if (target < 0 || target >= ax->len)
5235796c8dcSSimon Schubert 	    {
524cf7f2e2dSJohn Marino 	      ax->flaw = agent_flaw_bad_jump;
5255796c8dcSSimon Schubert 	      return;
5265796c8dcSSimon Schubert 	    }
5275796c8dcSSimon Schubert 
5285796c8dcSSimon Schubert 	  /* Do we have any information about what the stack height
5295796c8dcSSimon Schubert              should be at the target?  */
5305796c8dcSSimon Schubert 	  if (targets[target] || boundary[target])
5315796c8dcSSimon Schubert 	    {
5325796c8dcSSimon Schubert 	      if (heights[target] != height)
5335796c8dcSSimon Schubert 		{
534cf7f2e2dSJohn Marino 		  ax->flaw = agent_flaw_height_mismatch;
5355796c8dcSSimon Schubert 		  return;
5365796c8dcSSimon Schubert 		}
5375796c8dcSSimon Schubert 	    }
5385796c8dcSSimon Schubert 
5395796c8dcSSimon Schubert           /* Record the target, along with the stack height we expect.  */
5405796c8dcSSimon Schubert           targets[target] = 1;
5415796c8dcSSimon Schubert           heights[target] = height;
5425796c8dcSSimon Schubert 	}
5435796c8dcSSimon Schubert 
5445796c8dcSSimon Schubert       /* For unconditional jumps with a successor, check that the
5455796c8dcSSimon Schubert          successor is a target, and pick up its stack height.  */
5465796c8dcSSimon Schubert       if (aop_goto == op - aop_map
5475796c8dcSSimon Schubert 	  && i + 3 < ax->len)
5485796c8dcSSimon Schubert 	{
5495796c8dcSSimon Schubert 	  if (!targets[i + 3])
5505796c8dcSSimon Schubert 	    {
551cf7f2e2dSJohn Marino 	      ax->flaw = agent_flaw_hole;
5525796c8dcSSimon Schubert 	      return;
5535796c8dcSSimon Schubert 	    }
5545796c8dcSSimon Schubert 
5555796c8dcSSimon Schubert 	  height = heights[i + 3];
5565796c8dcSSimon Schubert 	}
5575796c8dcSSimon Schubert 
5585796c8dcSSimon Schubert       /* For reg instructions, record the register in the bit mask.  */
5595796c8dcSSimon Schubert       if (aop_reg == op - aop_map)
5605796c8dcSSimon Schubert 	{
5615796c8dcSSimon Schubert 	  int reg = read_const (ax, i + 1, 2);
5625796c8dcSSimon Schubert 
563cf7f2e2dSJohn Marino 	  ax_reg_mask (ax, reg);
5645796c8dcSSimon Schubert 	}
5655796c8dcSSimon Schubert     }
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert   /* Check that all the targets are on boundaries.  */
5685796c8dcSSimon Schubert   for (i = 0; i < ax->len; i++)
5695796c8dcSSimon Schubert     if (targets[i] && !boundary[i])
5705796c8dcSSimon Schubert       {
571cf7f2e2dSJohn Marino 	ax->flaw = agent_flaw_bad_jump;
5725796c8dcSSimon Schubert 	return;
5735796c8dcSSimon Schubert       }
5745796c8dcSSimon Schubert 
575cf7f2e2dSJohn Marino   ax->final_height = height;
5765796c8dcSSimon Schubert }
577