xref: /dflybsd-src/contrib/gdb-7/gdb/ax-general.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Functions for manipulating expressions designed to be executed on the agent
2*ef5ccd6cSJohn Marino    Copyright (C) 1998-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert 
45796c8dcSSimon Schubert    This file is part of GDB.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert    (at your option) any later version.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145796c8dcSSimon Schubert    GNU General Public License for more details.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert /* Despite what the above comment says about this file being part of
205796c8dcSSimon Schubert    GDB, we would like to keep these functions free of GDB
215796c8dcSSimon Schubert    dependencies, since we want to be able to use them in contexts
225796c8dcSSimon Schubert    outside of GDB (test suites, the stub, etc.)  */
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert #include "defs.h"
255796c8dcSSimon Schubert #include "ax.h"
265796c8dcSSimon Schubert 
275796c8dcSSimon Schubert #include "value.h"
285796c8dcSSimon Schubert #include "gdb_string.h"
295796c8dcSSimon Schubert 
30c50c785cSJohn Marino #include "user-regs.h"
31c50c785cSJohn Marino 
325796c8dcSSimon Schubert static void grow_expr (struct agent_expr *x, int n);
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert static void append_const (struct agent_expr *x, LONGEST val, int n);
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert static LONGEST read_const (struct agent_expr *x, int o, int n);
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert /* Functions for building expressions.  */
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert /* Allocate a new, empty agent expression.  */
435796c8dcSSimon Schubert struct agent_expr *
new_agent_expr(struct gdbarch * gdbarch,CORE_ADDR scope)44cf7f2e2dSJohn Marino new_agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope)
455796c8dcSSimon Schubert {
465796c8dcSSimon Schubert   struct agent_expr *x = xmalloc (sizeof (*x));
47cf7f2e2dSJohn Marino 
485796c8dcSSimon Schubert   x->len = 0;
495796c8dcSSimon Schubert   x->size = 1;			/* Change this to a larger value once
505796c8dcSSimon Schubert 				   reallocation code is tested.  */
515796c8dcSSimon Schubert   x->buf = xmalloc (x->size);
52cf7f2e2dSJohn Marino 
53cf7f2e2dSJohn Marino   x->gdbarch = gdbarch;
545796c8dcSSimon Schubert   x->scope = scope;
555796c8dcSSimon Schubert 
56cf7f2e2dSJohn Marino   /* Bit vector for registers used.  */
57cf7f2e2dSJohn Marino   x->reg_mask_len = 1;
58cf7f2e2dSJohn Marino   x->reg_mask = xmalloc (x->reg_mask_len * sizeof (x->reg_mask[0]));
59cf7f2e2dSJohn Marino   memset (x->reg_mask, 0, x->reg_mask_len * sizeof (x->reg_mask[0]));
60cf7f2e2dSJohn Marino 
615796c8dcSSimon Schubert   return x;
625796c8dcSSimon Schubert }
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert /* Free a agent expression.  */
655796c8dcSSimon Schubert void
free_agent_expr(struct agent_expr * x)665796c8dcSSimon Schubert free_agent_expr (struct agent_expr *x)
675796c8dcSSimon Schubert {
685796c8dcSSimon Schubert   xfree (x->buf);
69cf7f2e2dSJohn Marino   xfree (x->reg_mask);
705796c8dcSSimon Schubert   xfree (x);
715796c8dcSSimon Schubert }
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert static void
do_free_agent_expr_cleanup(void * x)745796c8dcSSimon Schubert do_free_agent_expr_cleanup (void *x)
755796c8dcSSimon Schubert {
765796c8dcSSimon Schubert   free_agent_expr (x);
775796c8dcSSimon Schubert }
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert struct cleanup *
make_cleanup_free_agent_expr(struct agent_expr * x)805796c8dcSSimon Schubert make_cleanup_free_agent_expr (struct agent_expr *x)
815796c8dcSSimon Schubert {
825796c8dcSSimon Schubert   return make_cleanup (do_free_agent_expr_cleanup, x);
835796c8dcSSimon Schubert }
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert /* Make sure that X has room for at least N more bytes.  This doesn't
875796c8dcSSimon Schubert    affect the length, just the allocated size.  */
885796c8dcSSimon Schubert static void
grow_expr(struct agent_expr * x,int n)895796c8dcSSimon Schubert grow_expr (struct agent_expr *x, int n)
905796c8dcSSimon Schubert {
915796c8dcSSimon Schubert   if (x->len + n > x->size)
925796c8dcSSimon Schubert     {
935796c8dcSSimon Schubert       x->size *= 2;
945796c8dcSSimon Schubert       if (x->size < x->len + n)
955796c8dcSSimon Schubert 	x->size = x->len + n + 10;
965796c8dcSSimon Schubert       x->buf = xrealloc (x->buf, x->size);
975796c8dcSSimon Schubert     }
985796c8dcSSimon Schubert }
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert /* Append the low N bytes of VAL as an N-byte integer to the
1025796c8dcSSimon Schubert    expression X, in big-endian order.  */
1035796c8dcSSimon Schubert static void
append_const(struct agent_expr * x,LONGEST val,int n)1045796c8dcSSimon Schubert append_const (struct agent_expr *x, LONGEST val, int n)
1055796c8dcSSimon Schubert {
1065796c8dcSSimon Schubert   int i;
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert   grow_expr (x, n);
1095796c8dcSSimon Schubert   for (i = n - 1; i >= 0; i--)
1105796c8dcSSimon Schubert     {
1115796c8dcSSimon Schubert       x->buf[x->len + i] = val & 0xff;
1125796c8dcSSimon Schubert       val >>= 8;
1135796c8dcSSimon Schubert     }
1145796c8dcSSimon Schubert   x->len += n;
1155796c8dcSSimon Schubert }
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert /* Extract an N-byte big-endian unsigned integer from expression X at
1195796c8dcSSimon Schubert    offset O.  */
1205796c8dcSSimon Schubert static LONGEST
read_const(struct agent_expr * x,int o,int n)1215796c8dcSSimon Schubert read_const (struct agent_expr *x, int o, int n)
1225796c8dcSSimon Schubert {
1235796c8dcSSimon Schubert   int i;
1245796c8dcSSimon Schubert   LONGEST accum = 0;
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert   /* Make sure we're not reading off the end of the expression.  */
1275796c8dcSSimon Schubert   if (o + n > x->len)
1285796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
1295796c8dcSSimon Schubert 
1305796c8dcSSimon Schubert   for (i = 0; i < n; i++)
1315796c8dcSSimon Schubert     accum = (accum << 8) | x->buf[o + i];
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert   return accum;
1345796c8dcSSimon Schubert }
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert /* Append a simple operator OP to EXPR.  */
1385796c8dcSSimon Schubert void
ax_simple(struct agent_expr * x,enum agent_op op)1395796c8dcSSimon Schubert ax_simple (struct agent_expr *x, enum agent_op op)
1405796c8dcSSimon Schubert {
1415796c8dcSSimon Schubert   grow_expr (x, 1);
1425796c8dcSSimon Schubert   x->buf[x->len++] = op;
1435796c8dcSSimon Schubert }
1445796c8dcSSimon Schubert 
145c50c785cSJohn Marino /* Append a pick operator to EXPR.  DEPTH is the stack item to pick,
146c50c785cSJohn Marino    with 0 being top of stack.  */
147c50c785cSJohn Marino 
148c50c785cSJohn Marino void
ax_pick(struct agent_expr * x,int depth)149c50c785cSJohn Marino ax_pick (struct agent_expr *x, int depth)
150c50c785cSJohn Marino {
151c50c785cSJohn Marino   if (depth < 0 || depth > 255)
152c50c785cSJohn Marino     error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
153c50c785cSJohn Marino   ax_simple (x, aop_pick);
154c50c785cSJohn Marino   append_const (x, 1, depth);
155c50c785cSJohn Marino }
156c50c785cSJohn Marino 
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert /* Append a sign-extension or zero-extension instruction to EXPR, to
1595796c8dcSSimon Schubert    extend an N-bit value.  */
1605796c8dcSSimon Schubert static void
generic_ext(struct agent_expr * x,enum agent_op op,int n)1615796c8dcSSimon Schubert generic_ext (struct agent_expr *x, enum agent_op op, int n)
1625796c8dcSSimon Schubert {
1635796c8dcSSimon Schubert   /* N must fit in a byte.  */
1645796c8dcSSimon Schubert   if (n < 0 || n > 255)
1655796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
1665796c8dcSSimon Schubert   /* That had better be enough range.  */
1675796c8dcSSimon Schubert   if (sizeof (LONGEST) * 8 > 255)
168c50c785cSJohn Marino     error (_("GDB bug: ax-general.c (generic_ext): "
169c50c785cSJohn Marino 	     "opcode has inadequate range"));
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert   grow_expr (x, 2);
1725796c8dcSSimon Schubert   x->buf[x->len++] = op;
1735796c8dcSSimon Schubert   x->buf[x->len++] = n;
1745796c8dcSSimon Schubert }
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert /* Append a sign-extension instruction to EXPR, to extend an N-bit value.  */
1785796c8dcSSimon Schubert void
ax_ext(struct agent_expr * x,int n)1795796c8dcSSimon Schubert ax_ext (struct agent_expr *x, int n)
1805796c8dcSSimon Schubert {
1815796c8dcSSimon Schubert   generic_ext (x, aop_ext, n);
1825796c8dcSSimon Schubert }
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert /* Append a zero-extension instruction to EXPR, to extend an N-bit value.  */
1865796c8dcSSimon Schubert void
ax_zero_ext(struct agent_expr * x,int n)1875796c8dcSSimon Schubert ax_zero_ext (struct agent_expr *x, int n)
1885796c8dcSSimon Schubert {
1895796c8dcSSimon Schubert   generic_ext (x, aop_zero_ext, n);
1905796c8dcSSimon Schubert }
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert /* Append a trace_quick instruction to EXPR, to record N bytes.  */
1945796c8dcSSimon Schubert void
ax_trace_quick(struct agent_expr * x,int n)1955796c8dcSSimon Schubert ax_trace_quick (struct agent_expr *x, int n)
1965796c8dcSSimon Schubert {
1975796c8dcSSimon Schubert   /* N must fit in a byte.  */
1985796c8dcSSimon Schubert   if (n < 0 || n > 255)
199c50c785cSJohn Marino     error (_("GDB bug: ax-general.c (ax_trace_quick): "
200c50c785cSJohn Marino 	     "size out of range for trace_quick"));
2015796c8dcSSimon Schubert 
2025796c8dcSSimon Schubert   grow_expr (x, 2);
2035796c8dcSSimon Schubert   x->buf[x->len++] = aop_trace_quick;
2045796c8dcSSimon Schubert   x->buf[x->len++] = n;
2055796c8dcSSimon Schubert }
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert /* Append a goto op to EXPR.  OP is the actual op (must be aop_goto or
2095796c8dcSSimon Schubert    aop_if_goto).  We assume we don't know the target offset yet,
2105796c8dcSSimon Schubert    because it's probably a forward branch, so we leave space in EXPR
2115796c8dcSSimon Schubert    for the target, and return the offset in EXPR of that space, so we
2125796c8dcSSimon Schubert    can backpatch it once we do know the target offset.  Use ax_label
2135796c8dcSSimon Schubert    to do the backpatching.  */
2145796c8dcSSimon Schubert int
ax_goto(struct agent_expr * x,enum agent_op op)2155796c8dcSSimon Schubert ax_goto (struct agent_expr *x, enum agent_op op)
2165796c8dcSSimon Schubert {
2175796c8dcSSimon Schubert   grow_expr (x, 3);
2185796c8dcSSimon Schubert   x->buf[x->len + 0] = op;
2195796c8dcSSimon Schubert   x->buf[x->len + 1] = 0xff;
2205796c8dcSSimon Schubert   x->buf[x->len + 2] = 0xff;
2215796c8dcSSimon Schubert   x->len += 3;
2225796c8dcSSimon Schubert   return x->len - 2;
2235796c8dcSSimon Schubert }
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert /* Suppose a given call to ax_goto returns some value PATCH.  When you
2265796c8dcSSimon Schubert    know the offset TARGET that goto should jump to, call
2275796c8dcSSimon Schubert    ax_label (EXPR, PATCH, TARGET)
2285796c8dcSSimon Schubert    to patch TARGET into the ax_goto instruction.  */
2295796c8dcSSimon Schubert void
ax_label(struct agent_expr * x,int patch,int target)2305796c8dcSSimon Schubert ax_label (struct agent_expr *x, int patch, int target)
2315796c8dcSSimon Schubert {
2325796c8dcSSimon Schubert   /* Make sure the value is in range.  Don't accept 0xffff as an
2335796c8dcSSimon Schubert      offset; that's our magic sentinel value for unpatched branches.  */
2345796c8dcSSimon Schubert   if (target < 0 || target >= 0xffff)
2355796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert   x->buf[patch] = (target >> 8) & 0xff;
2385796c8dcSSimon Schubert   x->buf[patch + 1] = target & 0xff;
2395796c8dcSSimon Schubert }
2405796c8dcSSimon Schubert 
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert /* Assemble code to push a constant on the stack.  */
2435796c8dcSSimon Schubert void
ax_const_l(struct agent_expr * x,LONGEST l)2445796c8dcSSimon Schubert ax_const_l (struct agent_expr *x, LONGEST l)
2455796c8dcSSimon Schubert {
2465796c8dcSSimon Schubert   static enum agent_op ops[]
2475796c8dcSSimon Schubert   =
2485796c8dcSSimon Schubert   {aop_const8, aop_const16, aop_const32, aop_const64};
2495796c8dcSSimon Schubert   int size;
2505796c8dcSSimon Schubert   int op;
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert   /* How big is the number?  'op' keeps track of which opcode to use.
2535796c8dcSSimon Schubert      Notice that we don't really care whether the original number was
2545796c8dcSSimon Schubert      signed or unsigned; we always reproduce the value exactly, and
2555796c8dcSSimon Schubert      use the shortest representation.  */
2565796c8dcSSimon Schubert   for (op = 0, size = 8; size < 64; size *= 2, op++)
2575796c8dcSSimon Schubert     {
258cf7f2e2dSJohn Marino       LONGEST lim = ((LONGEST) 1) << (size - 1);
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert       if (-lim <= l && l <= lim - 1)
2615796c8dcSSimon Schubert         break;
2625796c8dcSSimon Schubert     }
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert   /* Emit the right opcode...  */
2655796c8dcSSimon Schubert   ax_simple (x, ops[op]);
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert   /* Emit the low SIZE bytes as an unsigned number.  We know that
2685796c8dcSSimon Schubert      sign-extending this will yield l.  */
2695796c8dcSSimon Schubert   append_const (x, l, size / 8);
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert   /* Now, if it was negative, and not full-sized, sign-extend it.  */
2725796c8dcSSimon Schubert   if (l < 0 && size < 64)
2735796c8dcSSimon Schubert     ax_ext (x, size);
2745796c8dcSSimon Schubert }
2755796c8dcSSimon Schubert 
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert void
ax_const_d(struct agent_expr * x,LONGEST d)2785796c8dcSSimon Schubert ax_const_d (struct agent_expr *x, LONGEST d)
2795796c8dcSSimon Schubert {
2805796c8dcSSimon Schubert   /* FIXME: floating-point support not present yet.  */
281c50c785cSJohn Marino   error (_("GDB bug: ax-general.c (ax_const_d): "
282c50c785cSJohn Marino 	   "floating point not supported yet"));
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert /* Assemble code to push the value of register number REG on the
2875796c8dcSSimon Schubert    stack.  */
2885796c8dcSSimon Schubert void
ax_reg(struct agent_expr * x,int reg)2895796c8dcSSimon Schubert ax_reg (struct agent_expr *x, int reg)
2905796c8dcSSimon Schubert {
291c50c785cSJohn Marino   if (reg >= gdbarch_num_regs (x->gdbarch))
292c50c785cSJohn Marino     {
293c50c785cSJohn Marino       /* This is a pseudo-register.  */
294c50c785cSJohn Marino       if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch))
295c50c785cSJohn Marino 	error (_("'%s' is a pseudo-register; "
296c50c785cSJohn Marino 		 "GDB cannot yet trace its contents."),
297c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (x->gdbarch, reg));
298c50c785cSJohn Marino       if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg))
299c50c785cSJohn Marino 	error (_("Trace '%s' failed."),
300c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (x->gdbarch, reg));
301c50c785cSJohn Marino     }
302c50c785cSJohn Marino   else
303c50c785cSJohn Marino     {
3045796c8dcSSimon Schubert       /* Make sure the register number is in range.  */
3055796c8dcSSimon Schubert       if (reg < 0 || reg > 0xffff)
306c50c785cSJohn Marino         error (_("GDB bug: ax-general.c (ax_reg): "
307c50c785cSJohn Marino 		 "register number out of range"));
3085796c8dcSSimon Schubert       grow_expr (x, 3);
3095796c8dcSSimon Schubert       x->buf[x->len] = aop_reg;
3105796c8dcSSimon Schubert       x->buf[x->len + 1] = (reg >> 8) & 0xff;
3115796c8dcSSimon Schubert       x->buf[x->len + 2] = (reg) & 0xff;
3125796c8dcSSimon Schubert       x->len += 3;
3135796c8dcSSimon Schubert     }
314c50c785cSJohn Marino }
315cf7f2e2dSJohn Marino 
316cf7f2e2dSJohn Marino /* Assemble code to operate on a trace state variable.  */
317cf7f2e2dSJohn Marino 
318cf7f2e2dSJohn Marino void
ax_tsv(struct agent_expr * x,enum agent_op op,int num)319cf7f2e2dSJohn Marino ax_tsv (struct agent_expr *x, enum agent_op op, int num)
320cf7f2e2dSJohn Marino {
321cf7f2e2dSJohn Marino   /* Make sure the tsv number is in range.  */
322cf7f2e2dSJohn Marino   if (num < 0 || num > 0xffff)
323c50c785cSJohn Marino     internal_error (__FILE__, __LINE__,
324c50c785cSJohn Marino 		    _("ax-general.c (ax_tsv): variable "
325c50c785cSJohn Marino 		      "number is %d, out of range"), num);
326cf7f2e2dSJohn Marino 
327cf7f2e2dSJohn Marino   grow_expr (x, 3);
328cf7f2e2dSJohn Marino   x->buf[x->len] = op;
329cf7f2e2dSJohn Marino   x->buf[x->len + 1] = (num >> 8) & 0xff;
330cf7f2e2dSJohn Marino   x->buf[x->len + 2] = (num) & 0xff;
331cf7f2e2dSJohn Marino   x->len += 3;
332cf7f2e2dSJohn Marino }
333*ef5ccd6cSJohn Marino 
334*ef5ccd6cSJohn Marino /* Append a string to the expression.  Note that the string is going
335*ef5ccd6cSJohn Marino    into the bytecodes directly, not on the stack.  As a precaution,
336*ef5ccd6cSJohn Marino    include both length as prefix, and terminate with a NUL.  (The NUL
337*ef5ccd6cSJohn Marino    is counted in the length.)  */
338*ef5ccd6cSJohn Marino 
339*ef5ccd6cSJohn Marino void
ax_string(struct agent_expr * x,const char * str,int slen)340*ef5ccd6cSJohn Marino ax_string (struct agent_expr *x, const char *str, int slen)
341*ef5ccd6cSJohn Marino {
342*ef5ccd6cSJohn Marino   int i;
343*ef5ccd6cSJohn Marino 
344*ef5ccd6cSJohn Marino   /* Make sure the string length is reasonable.  */
345*ef5ccd6cSJohn Marino   if (slen < 0 || slen > 0xffff)
346*ef5ccd6cSJohn Marino     internal_error (__FILE__, __LINE__,
347*ef5ccd6cSJohn Marino 		    _("ax-general.c (ax_string): string "
348*ef5ccd6cSJohn Marino 		      "length is %d, out of allowed range"), slen);
349*ef5ccd6cSJohn Marino 
350*ef5ccd6cSJohn Marino   grow_expr (x, 2 + slen + 1);
351*ef5ccd6cSJohn Marino   x->buf[x->len++] = ((slen + 1) >> 8) & 0xff;
352*ef5ccd6cSJohn Marino   x->buf[x->len++] = (slen + 1) & 0xff;
353*ef5ccd6cSJohn Marino   for (i = 0; i < slen; ++i)
354*ef5ccd6cSJohn Marino     x->buf[x->len++] = str[i];
355*ef5ccd6cSJohn Marino   x->buf[x->len++] = '\0';
356*ef5ccd6cSJohn Marino }
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert 
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert /* Functions for disassembling agent expressions, and otherwise
3615796c8dcSSimon Schubert    debugging the expression compiler.  */
3625796c8dcSSimon Schubert 
3635796c8dcSSimon Schubert struct aop_map aop_map[] =
3645796c8dcSSimon Schubert {
365c50c785cSJohn Marino   {0, 0, 0, 0, 0}
366c50c785cSJohn Marino #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
367c50c785cSJohn Marino   , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
368c50c785cSJohn Marino #include "ax.def"
369c50c785cSJohn Marino #undef DEFOP
3705796c8dcSSimon Schubert };
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert /* Disassemble the expression EXPR, writing to F.  */
3745796c8dcSSimon Schubert void
ax_print(struct ui_file * f,struct agent_expr * x)3755796c8dcSSimon Schubert ax_print (struct ui_file *f, struct agent_expr *x)
3765796c8dcSSimon Schubert {
3775796c8dcSSimon Schubert   int i;
3785796c8dcSSimon Schubert 
379cf7f2e2dSJohn Marino   fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
380cf7f2e2dSJohn Marino   fprintf_filtered (f, _("Reg mask:"));
381cf7f2e2dSJohn Marino   for (i = 0; i < x->reg_mask_len; ++i)
382cf7f2e2dSJohn Marino     fprintf_filtered (f, _(" %02x"), x->reg_mask[i]);
383cf7f2e2dSJohn Marino   fprintf_filtered (f, _("\n"));
384cf7f2e2dSJohn Marino 
3855796c8dcSSimon Schubert   /* Check the size of the name array against the number of entries in
3865796c8dcSSimon Schubert      the enum, to catch additions that people didn't sync.  */
3875796c8dcSSimon Schubert   if ((sizeof (aop_map) / sizeof (aop_map[0]))
3885796c8dcSSimon Schubert       != aop_last)
3895796c8dcSSimon Schubert     error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));
3905796c8dcSSimon Schubert 
3915796c8dcSSimon Schubert   for (i = 0; i < x->len;)
3925796c8dcSSimon Schubert     {
3935796c8dcSSimon Schubert       enum agent_op op = x->buf[i];
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert       if (op >= (sizeof (aop_map) / sizeof (aop_map[0]))
3965796c8dcSSimon Schubert 	  || !aop_map[op].name)
3975796c8dcSSimon Schubert 	{
3985796c8dcSSimon Schubert 	  fprintf_filtered (f, _("%3d  <bad opcode %02x>\n"), i, op);
3995796c8dcSSimon Schubert 	  i++;
4005796c8dcSSimon Schubert 	  continue;
4015796c8dcSSimon Schubert 	}
4025796c8dcSSimon Schubert       if (i + 1 + aop_map[op].op_size > x->len)
4035796c8dcSSimon Schubert 	{
4045796c8dcSSimon Schubert 	  fprintf_filtered (f, _("%3d  <incomplete opcode %s>\n"),
4055796c8dcSSimon Schubert 			    i, aop_map[op].name);
4065796c8dcSSimon Schubert 	  break;
4075796c8dcSSimon Schubert 	}
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert       fprintf_filtered (f, "%3d  %s", i, aop_map[op].name);
4105796c8dcSSimon Schubert       if (aop_map[op].op_size > 0)
4115796c8dcSSimon Schubert 	{
4125796c8dcSSimon Schubert 	  fputs_filtered (" ", f);
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert 	  print_longest (f, 'd', 0,
4155796c8dcSSimon Schubert 			 read_const (x, i + 1, aop_map[op].op_size));
4165796c8dcSSimon Schubert 	}
417*ef5ccd6cSJohn Marino       /* Handle the complicated printf arguments specially.  */
418*ef5ccd6cSJohn Marino       else if (op == aop_printf)
419*ef5ccd6cSJohn Marino 	{
420*ef5ccd6cSJohn Marino 	  int slen, nargs;
421*ef5ccd6cSJohn Marino 
422*ef5ccd6cSJohn Marino 	  i++;
423*ef5ccd6cSJohn Marino 	  nargs = x->buf[i++];
424*ef5ccd6cSJohn Marino 	  slen = x->buf[i++];
425*ef5ccd6cSJohn Marino 	  slen = slen * 256 + x->buf[i++];
426*ef5ccd6cSJohn Marino 	  fprintf_filtered (f, _(" \"%s\", %d args"),
427*ef5ccd6cSJohn Marino 			    &(x->buf[i]), nargs);
428*ef5ccd6cSJohn Marino 	  i += slen - 1;
429*ef5ccd6cSJohn Marino 	}
4305796c8dcSSimon Schubert       fprintf_filtered (f, "\n");
4315796c8dcSSimon Schubert       i += 1 + aop_map[op].op_size;
4325796c8dcSSimon Schubert     }
4335796c8dcSSimon Schubert }
4345796c8dcSSimon Schubert 
435cf7f2e2dSJohn Marino /* Add register REG to the register mask for expression AX.  */
4365796c8dcSSimon Schubert void
ax_reg_mask(struct agent_expr * ax,int reg)437cf7f2e2dSJohn Marino ax_reg_mask (struct agent_expr *ax, int reg)
438cf7f2e2dSJohn Marino {
439c50c785cSJohn Marino   if (reg >= gdbarch_num_regs (ax->gdbarch))
440c50c785cSJohn Marino     {
441c50c785cSJohn Marino       /* This is a pseudo-register.  */
442c50c785cSJohn Marino       if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
443c50c785cSJohn Marino 	error (_("'%s' is a pseudo-register; "
444c50c785cSJohn Marino 		 "GDB cannot yet trace its contents."),
445c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (ax->gdbarch, reg));
446c50c785cSJohn Marino       if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
447c50c785cSJohn Marino 	error (_("Trace '%s' failed."),
448c50c785cSJohn Marino 	       user_reg_map_regnum_to_name (ax->gdbarch, reg));
449c50c785cSJohn Marino     }
450c50c785cSJohn Marino   else
451c50c785cSJohn Marino     {
452cf7f2e2dSJohn Marino       int byte = reg / 8;
453cf7f2e2dSJohn Marino 
454cf7f2e2dSJohn Marino       /* Grow the bit mask if necessary.  */
455cf7f2e2dSJohn Marino       if (byte >= ax->reg_mask_len)
456cf7f2e2dSJohn Marino         {
457cf7f2e2dSJohn Marino           /* It's not appropriate to double here.  This isn't a
458cf7f2e2dSJohn Marino 	     string buffer.  */
459cf7f2e2dSJohn Marino           int new_len = byte + 1;
460cf7f2e2dSJohn Marino           unsigned char *new_reg_mask = xrealloc (ax->reg_mask,
461c50c785cSJohn Marino 					          new_len
462c50c785cSJohn Marino 					          * sizeof (ax->reg_mask[0]));
463cf7f2e2dSJohn Marino           memset (new_reg_mask + ax->reg_mask_len, 0,
464cf7f2e2dSJohn Marino 	          (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0]));
465cf7f2e2dSJohn Marino           ax->reg_mask_len = new_len;
466cf7f2e2dSJohn Marino           ax->reg_mask = new_reg_mask;
467cf7f2e2dSJohn Marino         }
468cf7f2e2dSJohn Marino 
469cf7f2e2dSJohn Marino       ax->reg_mask[byte] |= 1 << (reg % 8);
470cf7f2e2dSJohn Marino     }
471c50c785cSJohn Marino }
472cf7f2e2dSJohn Marino 
473cf7f2e2dSJohn Marino /* Given an agent expression AX, fill in requirements and other descriptive
474cf7f2e2dSJohn Marino    bits.  */
475cf7f2e2dSJohn Marino void
ax_reqs(struct agent_expr * ax)476cf7f2e2dSJohn Marino ax_reqs (struct agent_expr *ax)
4775796c8dcSSimon Schubert {
4785796c8dcSSimon Schubert   int i;
4795796c8dcSSimon Schubert   int height;
4805796c8dcSSimon Schubert 
4815796c8dcSSimon Schubert   /* Jump target table.  targets[i] is non-zero iff we have found a
4825796c8dcSSimon Schubert      jump to offset i.  */
4835796c8dcSSimon Schubert   char *targets = (char *) alloca (ax->len * sizeof (targets[0]));
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert   /* Instruction boundary table.  boundary[i] is non-zero iff our scan
4865796c8dcSSimon Schubert      has reached an instruction starting at offset i.  */
4875796c8dcSSimon Schubert   char *boundary = (char *) alloca (ax->len * sizeof (boundary[0]));
4885796c8dcSSimon Schubert 
4895796c8dcSSimon Schubert   /* Stack height record.  If either targets[i] or boundary[i] is
4905796c8dcSSimon Schubert      non-zero, heights[i] is the height the stack should have before
4915796c8dcSSimon Schubert      executing the bytecode at that point.  */
4925796c8dcSSimon Schubert   int *heights = (int *) alloca (ax->len * sizeof (heights[0]));
4935796c8dcSSimon Schubert 
4945796c8dcSSimon Schubert   /* Pointer to a description of the present op.  */
4955796c8dcSSimon Schubert   struct aop_map *op;
4965796c8dcSSimon Schubert 
4975796c8dcSSimon Schubert   memset (targets, 0, ax->len * sizeof (targets[0]));
4985796c8dcSSimon Schubert   memset (boundary, 0, ax->len * sizeof (boundary[0]));
4995796c8dcSSimon Schubert 
500cf7f2e2dSJohn Marino   ax->max_height = ax->min_height = height = 0;
501cf7f2e2dSJohn Marino   ax->flaw = agent_flaw_none;
502cf7f2e2dSJohn Marino   ax->max_data_size = 0;
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert   for (i = 0; i < ax->len; i += 1 + op->op_size)
5055796c8dcSSimon Schubert     {
5065796c8dcSSimon Schubert       if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0])))
5075796c8dcSSimon Schubert 	{
508cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_bad_instruction;
5095796c8dcSSimon Schubert 	  return;
5105796c8dcSSimon Schubert 	}
5115796c8dcSSimon Schubert 
5125796c8dcSSimon Schubert       op = &aop_map[ax->buf[i]];
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert       if (!op->name)
5155796c8dcSSimon Schubert 	{
516cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_bad_instruction;
5175796c8dcSSimon Schubert 	  return;
5185796c8dcSSimon Schubert 	}
5195796c8dcSSimon Schubert 
5205796c8dcSSimon Schubert       if (i + 1 + op->op_size > ax->len)
5215796c8dcSSimon Schubert 	{
522cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_incomplete_instruction;
5235796c8dcSSimon Schubert 	  return;
5245796c8dcSSimon Schubert 	}
5255796c8dcSSimon Schubert 
5265796c8dcSSimon Schubert       /* If this instruction is a forward jump target, does the
5275796c8dcSSimon Schubert          current stack height match the stack height at the jump
5285796c8dcSSimon Schubert          source?  */
5295796c8dcSSimon Schubert       if (targets[i] && (heights[i] != height))
5305796c8dcSSimon Schubert 	{
531cf7f2e2dSJohn Marino 	  ax->flaw = agent_flaw_height_mismatch;
5325796c8dcSSimon Schubert 	  return;
5335796c8dcSSimon Schubert 	}
5345796c8dcSSimon Schubert 
5355796c8dcSSimon Schubert       boundary[i] = 1;
5365796c8dcSSimon Schubert       heights[i] = height;
5375796c8dcSSimon Schubert 
5385796c8dcSSimon Schubert       height -= op->consumed;
539cf7f2e2dSJohn Marino       if (height < ax->min_height)
540cf7f2e2dSJohn Marino 	ax->min_height = height;
5415796c8dcSSimon Schubert       height += op->produced;
542cf7f2e2dSJohn Marino       if (height > ax->max_height)
543cf7f2e2dSJohn Marino 	ax->max_height = height;
5445796c8dcSSimon Schubert 
545cf7f2e2dSJohn Marino       if (op->data_size > ax->max_data_size)
546cf7f2e2dSJohn Marino 	ax->max_data_size = op->data_size;
5475796c8dcSSimon Schubert 
5485796c8dcSSimon Schubert       /* For jump instructions, check that the target is a valid
5495796c8dcSSimon Schubert          offset.  If it is, record the fact that that location is a
5505796c8dcSSimon Schubert          jump target, and record the height we expect there.  */
5515796c8dcSSimon Schubert       if (aop_goto == op - aop_map
5525796c8dcSSimon Schubert 	  || aop_if_goto == op - aop_map)
5535796c8dcSSimon Schubert 	{
5545796c8dcSSimon Schubert 	  int target = read_const (ax, i + 1, 2);
5555796c8dcSSimon Schubert 	  if (target < 0 || target >= ax->len)
5565796c8dcSSimon Schubert 	    {
557cf7f2e2dSJohn Marino 	      ax->flaw = agent_flaw_bad_jump;
5585796c8dcSSimon Schubert 	      return;
5595796c8dcSSimon Schubert 	    }
5605796c8dcSSimon Schubert 
5615796c8dcSSimon Schubert 	  /* Do we have any information about what the stack height
5625796c8dcSSimon Schubert              should be at the target?  */
5635796c8dcSSimon Schubert 	  if (targets[target] || boundary[target])
5645796c8dcSSimon Schubert 	    {
5655796c8dcSSimon Schubert 	      if (heights[target] != height)
5665796c8dcSSimon Schubert 		{
567cf7f2e2dSJohn Marino 		  ax->flaw = agent_flaw_height_mismatch;
5685796c8dcSSimon Schubert 		  return;
5695796c8dcSSimon Schubert 		}
5705796c8dcSSimon Schubert 	    }
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert           /* Record the target, along with the stack height we expect.  */
5735796c8dcSSimon Schubert           targets[target] = 1;
5745796c8dcSSimon Schubert           heights[target] = height;
5755796c8dcSSimon Schubert 	}
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert       /* For unconditional jumps with a successor, check that the
5785796c8dcSSimon Schubert          successor is a target, and pick up its stack height.  */
5795796c8dcSSimon Schubert       if (aop_goto == op - aop_map
5805796c8dcSSimon Schubert 	  && i + 3 < ax->len)
5815796c8dcSSimon Schubert 	{
5825796c8dcSSimon Schubert 	  if (!targets[i + 3])
5835796c8dcSSimon Schubert 	    {
584cf7f2e2dSJohn Marino 	      ax->flaw = agent_flaw_hole;
5855796c8dcSSimon Schubert 	      return;
5865796c8dcSSimon Schubert 	    }
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert 	  height = heights[i + 3];
5895796c8dcSSimon Schubert 	}
5905796c8dcSSimon Schubert 
5915796c8dcSSimon Schubert       /* For reg instructions, record the register in the bit mask.  */
5925796c8dcSSimon Schubert       if (aop_reg == op - aop_map)
5935796c8dcSSimon Schubert 	{
5945796c8dcSSimon Schubert 	  int reg = read_const (ax, i + 1, 2);
5955796c8dcSSimon Schubert 
596cf7f2e2dSJohn Marino 	  ax_reg_mask (ax, reg);
5975796c8dcSSimon Schubert 	}
5985796c8dcSSimon Schubert     }
5995796c8dcSSimon Schubert 
6005796c8dcSSimon Schubert   /* Check that all the targets are on boundaries.  */
6015796c8dcSSimon Schubert   for (i = 0; i < ax->len; i++)
6025796c8dcSSimon Schubert     if (targets[i] && !boundary[i])
6035796c8dcSSimon Schubert       {
604cf7f2e2dSJohn Marino 	ax->flaw = agent_flaw_bad_jump;
6055796c8dcSSimon Schubert 	return;
6065796c8dcSSimon Schubert       }
6075796c8dcSSimon Schubert 
608cf7f2e2dSJohn Marino   ax->final_height = height;
6095796c8dcSSimon Schubert }
610