15796c8dcSSimon Schubert /* Functions for manipulating expressions designed to be executed on the agent 2*cf7f2e2dSJohn Marino Copyright (C) 1998, 1999, 2000, 2007, 2008, 2009, 2010 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 315796c8dcSSimon Schubert static void grow_expr (struct agent_expr *x, int n); 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert static void append_const (struct agent_expr *x, LONGEST val, int n); 345796c8dcSSimon Schubert 355796c8dcSSimon Schubert static LONGEST read_const (struct agent_expr *x, int o, int n); 365796c8dcSSimon Schubert 375796c8dcSSimon Schubert static void generic_ext (struct agent_expr *x, enum agent_op op, int n); 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert /* Functions for building expressions. */ 405796c8dcSSimon Schubert 415796c8dcSSimon Schubert /* Allocate a new, empty agent expression. */ 425796c8dcSSimon Schubert struct agent_expr * 43*cf7f2e2dSJohn Marino new_agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope) 445796c8dcSSimon Schubert { 455796c8dcSSimon Schubert struct agent_expr *x = xmalloc (sizeof (*x)); 46*cf7f2e2dSJohn Marino 475796c8dcSSimon Schubert x->len = 0; 485796c8dcSSimon Schubert x->size = 1; /* Change this to a larger value once 495796c8dcSSimon Schubert reallocation code is tested. */ 505796c8dcSSimon Schubert x->buf = xmalloc (x->size); 51*cf7f2e2dSJohn Marino 52*cf7f2e2dSJohn Marino x->gdbarch = gdbarch; 535796c8dcSSimon Schubert x->scope = scope; 545796c8dcSSimon Schubert 55*cf7f2e2dSJohn Marino /* Bit vector for registers used. */ 56*cf7f2e2dSJohn Marino x->reg_mask_len = 1; 57*cf7f2e2dSJohn Marino x->reg_mask = xmalloc (x->reg_mask_len * sizeof (x->reg_mask[0])); 58*cf7f2e2dSJohn Marino memset (x->reg_mask, 0, x->reg_mask_len * sizeof (x->reg_mask[0])); 59*cf7f2e2dSJohn Marino 605796c8dcSSimon Schubert return x; 615796c8dcSSimon Schubert } 625796c8dcSSimon Schubert 635796c8dcSSimon Schubert /* Free a agent expression. */ 645796c8dcSSimon Schubert void 655796c8dcSSimon Schubert free_agent_expr (struct agent_expr *x) 665796c8dcSSimon Schubert { 675796c8dcSSimon Schubert xfree (x->buf); 68*cf7f2e2dSJohn Marino xfree (x->reg_mask); 695796c8dcSSimon Schubert xfree (x); 705796c8dcSSimon Schubert } 715796c8dcSSimon Schubert 725796c8dcSSimon Schubert static void 735796c8dcSSimon Schubert do_free_agent_expr_cleanup (void *x) 745796c8dcSSimon Schubert { 755796c8dcSSimon Schubert free_agent_expr (x); 765796c8dcSSimon Schubert } 775796c8dcSSimon Schubert 785796c8dcSSimon Schubert struct cleanup * 795796c8dcSSimon Schubert make_cleanup_free_agent_expr (struct agent_expr *x) 805796c8dcSSimon Schubert { 815796c8dcSSimon Schubert return make_cleanup (do_free_agent_expr_cleanup, x); 825796c8dcSSimon Schubert } 835796c8dcSSimon Schubert 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert /* Make sure that X has room for at least N more bytes. This doesn't 865796c8dcSSimon Schubert affect the length, just the allocated size. */ 875796c8dcSSimon Schubert static void 885796c8dcSSimon Schubert grow_expr (struct agent_expr *x, int n) 895796c8dcSSimon Schubert { 905796c8dcSSimon Schubert if (x->len + n > x->size) 915796c8dcSSimon Schubert { 925796c8dcSSimon Schubert x->size *= 2; 935796c8dcSSimon Schubert if (x->size < x->len + n) 945796c8dcSSimon Schubert x->size = x->len + n + 10; 955796c8dcSSimon Schubert x->buf = xrealloc (x->buf, x->size); 965796c8dcSSimon Schubert } 975796c8dcSSimon Schubert } 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert 1005796c8dcSSimon Schubert /* Append the low N bytes of VAL as an N-byte integer to the 1015796c8dcSSimon Schubert expression X, in big-endian order. */ 1025796c8dcSSimon Schubert static void 1035796c8dcSSimon Schubert append_const (struct agent_expr *x, LONGEST val, int n) 1045796c8dcSSimon Schubert { 1055796c8dcSSimon Schubert int i; 1065796c8dcSSimon Schubert 1075796c8dcSSimon Schubert grow_expr (x, n); 1085796c8dcSSimon Schubert for (i = n - 1; i >= 0; i--) 1095796c8dcSSimon Schubert { 1105796c8dcSSimon Schubert x->buf[x->len + i] = val & 0xff; 1115796c8dcSSimon Schubert val >>= 8; 1125796c8dcSSimon Schubert } 1135796c8dcSSimon Schubert x->len += n; 1145796c8dcSSimon Schubert } 1155796c8dcSSimon Schubert 1165796c8dcSSimon Schubert 1175796c8dcSSimon Schubert /* Extract an N-byte big-endian unsigned integer from expression X at 1185796c8dcSSimon Schubert offset O. */ 1195796c8dcSSimon Schubert static LONGEST 1205796c8dcSSimon Schubert read_const (struct agent_expr *x, int o, int n) 1215796c8dcSSimon Schubert { 1225796c8dcSSimon Schubert int i; 1235796c8dcSSimon Schubert LONGEST accum = 0; 1245796c8dcSSimon Schubert 1255796c8dcSSimon Schubert /* Make sure we're not reading off the end of the expression. */ 1265796c8dcSSimon Schubert if (o + n > x->len) 1275796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (read_const): incomplete constant")); 1285796c8dcSSimon Schubert 1295796c8dcSSimon Schubert for (i = 0; i < n; i++) 1305796c8dcSSimon Schubert accum = (accum << 8) | x->buf[o + i]; 1315796c8dcSSimon Schubert 1325796c8dcSSimon Schubert return accum; 1335796c8dcSSimon Schubert } 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert 1365796c8dcSSimon Schubert /* Append a simple operator OP to EXPR. */ 1375796c8dcSSimon Schubert void 1385796c8dcSSimon Schubert ax_simple (struct agent_expr *x, enum agent_op op) 1395796c8dcSSimon Schubert { 1405796c8dcSSimon Schubert grow_expr (x, 1); 1415796c8dcSSimon Schubert x->buf[x->len++] = op; 1425796c8dcSSimon Schubert } 1435796c8dcSSimon Schubert 1445796c8dcSSimon Schubert 1455796c8dcSSimon Schubert /* Append a sign-extension or zero-extension instruction to EXPR, to 1465796c8dcSSimon Schubert extend an N-bit value. */ 1475796c8dcSSimon Schubert static void 1485796c8dcSSimon Schubert generic_ext (struct agent_expr *x, enum agent_op op, int n) 1495796c8dcSSimon Schubert { 1505796c8dcSSimon Schubert /* N must fit in a byte. */ 1515796c8dcSSimon Schubert if (n < 0 || n > 255) 1525796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (generic_ext): bit count out of range")); 1535796c8dcSSimon Schubert /* That had better be enough range. */ 1545796c8dcSSimon Schubert if (sizeof (LONGEST) * 8 > 255) 1555796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (generic_ext): opcode has inadequate range")); 1565796c8dcSSimon Schubert 1575796c8dcSSimon Schubert grow_expr (x, 2); 1585796c8dcSSimon Schubert x->buf[x->len++] = op; 1595796c8dcSSimon Schubert x->buf[x->len++] = n; 1605796c8dcSSimon Schubert } 1615796c8dcSSimon Schubert 1625796c8dcSSimon Schubert 1635796c8dcSSimon Schubert /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */ 1645796c8dcSSimon Schubert void 1655796c8dcSSimon Schubert ax_ext (struct agent_expr *x, int n) 1665796c8dcSSimon Schubert { 1675796c8dcSSimon Schubert generic_ext (x, aop_ext, n); 1685796c8dcSSimon Schubert } 1695796c8dcSSimon Schubert 1705796c8dcSSimon Schubert 1715796c8dcSSimon Schubert /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */ 1725796c8dcSSimon Schubert void 1735796c8dcSSimon Schubert ax_zero_ext (struct agent_expr *x, int n) 1745796c8dcSSimon Schubert { 1755796c8dcSSimon Schubert generic_ext (x, aop_zero_ext, n); 1765796c8dcSSimon Schubert } 1775796c8dcSSimon Schubert 1785796c8dcSSimon Schubert 1795796c8dcSSimon Schubert /* Append a trace_quick instruction to EXPR, to record N bytes. */ 1805796c8dcSSimon Schubert void 1815796c8dcSSimon Schubert ax_trace_quick (struct agent_expr *x, int n) 1825796c8dcSSimon Schubert { 1835796c8dcSSimon Schubert /* N must fit in a byte. */ 1845796c8dcSSimon Schubert if (n < 0 || n > 255) 1855796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (ax_trace_quick): size out of range for trace_quick")); 1865796c8dcSSimon Schubert 1875796c8dcSSimon Schubert grow_expr (x, 2); 1885796c8dcSSimon Schubert x->buf[x->len++] = aop_trace_quick; 1895796c8dcSSimon Schubert x->buf[x->len++] = n; 1905796c8dcSSimon Schubert } 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert 1935796c8dcSSimon Schubert /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or 1945796c8dcSSimon Schubert aop_if_goto). We assume we don't know the target offset yet, 1955796c8dcSSimon Schubert because it's probably a forward branch, so we leave space in EXPR 1965796c8dcSSimon Schubert for the target, and return the offset in EXPR of that space, so we 1975796c8dcSSimon Schubert can backpatch it once we do know the target offset. Use ax_label 1985796c8dcSSimon Schubert to do the backpatching. */ 1995796c8dcSSimon Schubert int 2005796c8dcSSimon Schubert ax_goto (struct agent_expr *x, enum agent_op op) 2015796c8dcSSimon Schubert { 2025796c8dcSSimon Schubert grow_expr (x, 3); 2035796c8dcSSimon Schubert x->buf[x->len + 0] = op; 2045796c8dcSSimon Schubert x->buf[x->len + 1] = 0xff; 2055796c8dcSSimon Schubert x->buf[x->len + 2] = 0xff; 2065796c8dcSSimon Schubert x->len += 3; 2075796c8dcSSimon Schubert return x->len - 2; 2085796c8dcSSimon Schubert } 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert /* Suppose a given call to ax_goto returns some value PATCH. When you 2115796c8dcSSimon Schubert know the offset TARGET that goto should jump to, call 2125796c8dcSSimon Schubert ax_label (EXPR, PATCH, TARGET) 2135796c8dcSSimon Schubert to patch TARGET into the ax_goto instruction. */ 2145796c8dcSSimon Schubert void 2155796c8dcSSimon Schubert ax_label (struct agent_expr *x, int patch, int target) 2165796c8dcSSimon Schubert { 2175796c8dcSSimon Schubert /* Make sure the value is in range. Don't accept 0xffff as an 2185796c8dcSSimon Schubert offset; that's our magic sentinel value for unpatched branches. */ 2195796c8dcSSimon Schubert if (target < 0 || target >= 0xffff) 2205796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (ax_label): label target out of range")); 2215796c8dcSSimon Schubert 2225796c8dcSSimon Schubert x->buf[patch] = (target >> 8) & 0xff; 2235796c8dcSSimon Schubert x->buf[patch + 1] = target & 0xff; 2245796c8dcSSimon Schubert } 2255796c8dcSSimon Schubert 2265796c8dcSSimon Schubert 2275796c8dcSSimon Schubert /* Assemble code to push a constant on the stack. */ 2285796c8dcSSimon Schubert void 2295796c8dcSSimon Schubert ax_const_l (struct agent_expr *x, LONGEST l) 2305796c8dcSSimon Schubert { 2315796c8dcSSimon Schubert static enum agent_op ops[] 2325796c8dcSSimon Schubert = 2335796c8dcSSimon Schubert {aop_const8, aop_const16, aop_const32, aop_const64}; 2345796c8dcSSimon Schubert int size; 2355796c8dcSSimon Schubert int op; 2365796c8dcSSimon Schubert 2375796c8dcSSimon Schubert /* How big is the number? 'op' keeps track of which opcode to use. 2385796c8dcSSimon Schubert Notice that we don't really care whether the original number was 2395796c8dcSSimon Schubert signed or unsigned; we always reproduce the value exactly, and 2405796c8dcSSimon Schubert use the shortest representation. */ 2415796c8dcSSimon Schubert for (op = 0, size = 8; size < 64; size *= 2, op++) 2425796c8dcSSimon Schubert { 243*cf7f2e2dSJohn Marino LONGEST lim = ((LONGEST) 1) << (size - 1); 2445796c8dcSSimon Schubert 2455796c8dcSSimon Schubert if (-lim <= l && l <= lim - 1) 2465796c8dcSSimon Schubert break; 2475796c8dcSSimon Schubert } 2485796c8dcSSimon Schubert 2495796c8dcSSimon Schubert /* Emit the right opcode... */ 2505796c8dcSSimon Schubert ax_simple (x, ops[op]); 2515796c8dcSSimon Schubert 2525796c8dcSSimon Schubert /* Emit the low SIZE bytes as an unsigned number. We know that 2535796c8dcSSimon Schubert sign-extending this will yield l. */ 2545796c8dcSSimon Schubert append_const (x, l, size / 8); 2555796c8dcSSimon Schubert 2565796c8dcSSimon Schubert /* Now, if it was negative, and not full-sized, sign-extend it. */ 2575796c8dcSSimon Schubert if (l < 0 && size < 64) 2585796c8dcSSimon Schubert ax_ext (x, size); 2595796c8dcSSimon Schubert } 2605796c8dcSSimon Schubert 2615796c8dcSSimon Schubert 2625796c8dcSSimon Schubert void 2635796c8dcSSimon Schubert ax_const_d (struct agent_expr *x, LONGEST d) 2645796c8dcSSimon Schubert { 2655796c8dcSSimon Schubert /* FIXME: floating-point support not present yet. */ 2665796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (ax_const_d): floating point not supported yet")); 2675796c8dcSSimon Schubert } 2685796c8dcSSimon Schubert 2695796c8dcSSimon Schubert 2705796c8dcSSimon Schubert /* Assemble code to push the value of register number REG on the 2715796c8dcSSimon Schubert stack. */ 2725796c8dcSSimon Schubert void 2735796c8dcSSimon Schubert ax_reg (struct agent_expr *x, int reg) 2745796c8dcSSimon Schubert { 2755796c8dcSSimon Schubert /* Make sure the register number is in range. */ 2765796c8dcSSimon Schubert if (reg < 0 || reg > 0xffff) 2775796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (ax_reg): register number out of range")); 2785796c8dcSSimon Schubert grow_expr (x, 3); 2795796c8dcSSimon Schubert x->buf[x->len] = aop_reg; 2805796c8dcSSimon Schubert x->buf[x->len + 1] = (reg >> 8) & 0xff; 2815796c8dcSSimon Schubert x->buf[x->len + 2] = (reg) & 0xff; 2825796c8dcSSimon Schubert x->len += 3; 2835796c8dcSSimon Schubert } 284*cf7f2e2dSJohn Marino 285*cf7f2e2dSJohn Marino /* Assemble code to operate on a trace state variable. */ 286*cf7f2e2dSJohn Marino 287*cf7f2e2dSJohn Marino void 288*cf7f2e2dSJohn Marino ax_tsv (struct agent_expr *x, enum agent_op op, int num) 289*cf7f2e2dSJohn Marino { 290*cf7f2e2dSJohn Marino /* Make sure the tsv number is in range. */ 291*cf7f2e2dSJohn Marino if (num < 0 || num > 0xffff) 292*cf7f2e2dSJohn Marino internal_error (__FILE__, __LINE__, _("ax-general.c (ax_tsv): variable number is %d, out of range"), num); 293*cf7f2e2dSJohn Marino 294*cf7f2e2dSJohn Marino grow_expr (x, 3); 295*cf7f2e2dSJohn Marino x->buf[x->len] = op; 296*cf7f2e2dSJohn Marino x->buf[x->len + 1] = (num >> 8) & 0xff; 297*cf7f2e2dSJohn Marino x->buf[x->len + 2] = (num) & 0xff; 298*cf7f2e2dSJohn Marino x->len += 3; 299*cf7f2e2dSJohn Marino } 3005796c8dcSSimon Schubert 3015796c8dcSSimon Schubert 3025796c8dcSSimon Schubert 3035796c8dcSSimon Schubert /* Functions for disassembling agent expressions, and otherwise 3045796c8dcSSimon Schubert debugging the expression compiler. */ 3055796c8dcSSimon Schubert 3065796c8dcSSimon Schubert struct aop_map aop_map[] = 3075796c8dcSSimon Schubert { 3085796c8dcSSimon Schubert {0, 0, 0, 0, 0}, 3095796c8dcSSimon Schubert {"float", 0, 0, 0, 0}, /* 0x01 */ 3105796c8dcSSimon Schubert {"add", 0, 0, 2, 1}, /* 0x02 */ 3115796c8dcSSimon Schubert {"sub", 0, 0, 2, 1}, /* 0x03 */ 3125796c8dcSSimon Schubert {"mul", 0, 0, 2, 1}, /* 0x04 */ 3135796c8dcSSimon Schubert {"div_signed", 0, 0, 2, 1}, /* 0x05 */ 3145796c8dcSSimon Schubert {"div_unsigned", 0, 0, 2, 1}, /* 0x06 */ 3155796c8dcSSimon Schubert {"rem_signed", 0, 0, 2, 1}, /* 0x07 */ 3165796c8dcSSimon Schubert {"rem_unsigned", 0, 0, 2, 1}, /* 0x08 */ 3175796c8dcSSimon Schubert {"lsh", 0, 0, 2, 1}, /* 0x09 */ 3185796c8dcSSimon Schubert {"rsh_signed", 0, 0, 2, 1}, /* 0x0a */ 3195796c8dcSSimon Schubert {"rsh_unsigned", 0, 0, 2, 1}, /* 0x0b */ 3205796c8dcSSimon Schubert {"trace", 0, 0, 2, 0}, /* 0x0c */ 3215796c8dcSSimon Schubert {"trace_quick", 1, 0, 1, 1}, /* 0x0d */ 3225796c8dcSSimon Schubert {"log_not", 0, 0, 1, 1}, /* 0x0e */ 3235796c8dcSSimon Schubert {"bit_and", 0, 0, 2, 1}, /* 0x0f */ 3245796c8dcSSimon Schubert {"bit_or", 0, 0, 2, 1}, /* 0x10 */ 3255796c8dcSSimon Schubert {"bit_xor", 0, 0, 2, 1}, /* 0x11 */ 3265796c8dcSSimon Schubert {"bit_not", 0, 0, 1, 1}, /* 0x12 */ 3275796c8dcSSimon Schubert {"equal", 0, 0, 2, 1}, /* 0x13 */ 3285796c8dcSSimon Schubert {"less_signed", 0, 0, 2, 1}, /* 0x14 */ 3295796c8dcSSimon Schubert {"less_unsigned", 0, 0, 2, 1}, /* 0x15 */ 3305796c8dcSSimon Schubert {"ext", 1, 0, 1, 1}, /* 0x16 */ 3315796c8dcSSimon Schubert {"ref8", 0, 8, 1, 1}, /* 0x17 */ 3325796c8dcSSimon Schubert {"ref16", 0, 16, 1, 1}, /* 0x18 */ 3335796c8dcSSimon Schubert {"ref32", 0, 32, 1, 1}, /* 0x19 */ 3345796c8dcSSimon Schubert {"ref64", 0, 64, 1, 1}, /* 0x1a */ 3355796c8dcSSimon Schubert {"ref_float", 0, 0, 1, 1}, /* 0x1b */ 3365796c8dcSSimon Schubert {"ref_double", 0, 0, 1, 1}, /* 0x1c */ 3375796c8dcSSimon Schubert {"ref_long_double", 0, 0, 1, 1}, /* 0x1d */ 3385796c8dcSSimon Schubert {"l_to_d", 0, 0, 1, 1}, /* 0x1e */ 3395796c8dcSSimon Schubert {"d_to_l", 0, 0, 1, 1}, /* 0x1f */ 3405796c8dcSSimon Schubert {"if_goto", 2, 0, 1, 0}, /* 0x20 */ 3415796c8dcSSimon Schubert {"goto", 2, 0, 0, 0}, /* 0x21 */ 3425796c8dcSSimon Schubert {"const8", 1, 8, 0, 1}, /* 0x22 */ 3435796c8dcSSimon Schubert {"const16", 2, 16, 0, 1}, /* 0x23 */ 3445796c8dcSSimon Schubert {"const32", 4, 32, 0, 1}, /* 0x24 */ 3455796c8dcSSimon Schubert {"const64", 8, 64, 0, 1}, /* 0x25 */ 3465796c8dcSSimon Schubert {"reg", 2, 0, 0, 1}, /* 0x26 */ 3475796c8dcSSimon Schubert {"end", 0, 0, 0, 0}, /* 0x27 */ 3485796c8dcSSimon Schubert {"dup", 0, 0, 1, 2}, /* 0x28 */ 3495796c8dcSSimon Schubert {"pop", 0, 0, 1, 0}, /* 0x29 */ 3505796c8dcSSimon Schubert {"zero_ext", 1, 0, 1, 1}, /* 0x2a */ 3515796c8dcSSimon Schubert {"swap", 0, 0, 2, 2}, /* 0x2b */ 352*cf7f2e2dSJohn Marino {"getv", 2, 0, 0, 1}, /* 0x2c */ 353*cf7f2e2dSJohn Marino {"setv", 2, 0, 0, 1}, /* 0x2d */ 354*cf7f2e2dSJohn Marino {"tracev", 2, 0, 0, 1}, /* 0x2e */ 3555796c8dcSSimon Schubert {0, 0, 0, 0, 0}, /* 0x2f */ 3565796c8dcSSimon Schubert {"trace16", 2, 0, 1, 1}, /* 0x30 */ 3575796c8dcSSimon Schubert }; 3585796c8dcSSimon Schubert 3595796c8dcSSimon Schubert 3605796c8dcSSimon Schubert /* Disassemble the expression EXPR, writing to F. */ 3615796c8dcSSimon Schubert void 3625796c8dcSSimon Schubert ax_print (struct ui_file *f, struct agent_expr *x) 3635796c8dcSSimon Schubert { 3645796c8dcSSimon Schubert int i; 3655796c8dcSSimon Schubert int is_float = 0; 3665796c8dcSSimon Schubert 367*cf7f2e2dSJohn Marino fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope)); 368*cf7f2e2dSJohn Marino fprintf_filtered (f, _("Reg mask:")); 369*cf7f2e2dSJohn Marino for (i = 0; i < x->reg_mask_len; ++i) 370*cf7f2e2dSJohn Marino fprintf_filtered (f, _(" %02x"), x->reg_mask[i]); 371*cf7f2e2dSJohn Marino fprintf_filtered (f, _("\n")); 372*cf7f2e2dSJohn Marino 3735796c8dcSSimon Schubert /* Check the size of the name array against the number of entries in 3745796c8dcSSimon Schubert the enum, to catch additions that people didn't sync. */ 3755796c8dcSSimon Schubert if ((sizeof (aop_map) / sizeof (aop_map[0])) 3765796c8dcSSimon Schubert != aop_last) 3775796c8dcSSimon Schubert error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync")); 3785796c8dcSSimon Schubert 3795796c8dcSSimon Schubert for (i = 0; i < x->len;) 3805796c8dcSSimon Schubert { 3815796c8dcSSimon Schubert enum agent_op op = x->buf[i]; 3825796c8dcSSimon Schubert 3835796c8dcSSimon Schubert if (op >= (sizeof (aop_map) / sizeof (aop_map[0])) 3845796c8dcSSimon Schubert || !aop_map[op].name) 3855796c8dcSSimon Schubert { 3865796c8dcSSimon Schubert fprintf_filtered (f, _("%3d <bad opcode %02x>\n"), i, op); 3875796c8dcSSimon Schubert i++; 3885796c8dcSSimon Schubert continue; 3895796c8dcSSimon Schubert } 3905796c8dcSSimon Schubert if (i + 1 + aop_map[op].op_size > x->len) 3915796c8dcSSimon Schubert { 3925796c8dcSSimon Schubert fprintf_filtered (f, _("%3d <incomplete opcode %s>\n"), 3935796c8dcSSimon Schubert i, aop_map[op].name); 3945796c8dcSSimon Schubert break; 3955796c8dcSSimon Schubert } 3965796c8dcSSimon Schubert 3975796c8dcSSimon Schubert fprintf_filtered (f, "%3d %s", i, aop_map[op].name); 3985796c8dcSSimon Schubert if (aop_map[op].op_size > 0) 3995796c8dcSSimon Schubert { 4005796c8dcSSimon Schubert fputs_filtered (" ", f); 4015796c8dcSSimon Schubert 4025796c8dcSSimon Schubert print_longest (f, 'd', 0, 4035796c8dcSSimon Schubert read_const (x, i + 1, aop_map[op].op_size)); 4045796c8dcSSimon Schubert } 4055796c8dcSSimon Schubert fprintf_filtered (f, "\n"); 4065796c8dcSSimon Schubert i += 1 + aop_map[op].op_size; 4075796c8dcSSimon Schubert 4085796c8dcSSimon Schubert is_float = (op == aop_float); 4095796c8dcSSimon Schubert } 4105796c8dcSSimon Schubert } 4115796c8dcSSimon Schubert 412*cf7f2e2dSJohn Marino /* Add register REG to the register mask for expression AX. */ 4135796c8dcSSimon Schubert void 414*cf7f2e2dSJohn Marino ax_reg_mask (struct agent_expr *ax, int reg) 415*cf7f2e2dSJohn Marino { 416*cf7f2e2dSJohn Marino int byte = reg / 8; 417*cf7f2e2dSJohn Marino 418*cf7f2e2dSJohn Marino /* Grow the bit mask if necessary. */ 419*cf7f2e2dSJohn Marino if (byte >= ax->reg_mask_len) 420*cf7f2e2dSJohn Marino { 421*cf7f2e2dSJohn Marino /* It's not appropriate to double here. This isn't a 422*cf7f2e2dSJohn Marino string buffer. */ 423*cf7f2e2dSJohn Marino int new_len = byte + 1; 424*cf7f2e2dSJohn Marino unsigned char *new_reg_mask = xrealloc (ax->reg_mask, 425*cf7f2e2dSJohn Marino new_len * sizeof (ax->reg_mask[0])); 426*cf7f2e2dSJohn Marino memset (new_reg_mask + ax->reg_mask_len, 0, 427*cf7f2e2dSJohn Marino (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0])); 428*cf7f2e2dSJohn Marino ax->reg_mask_len = new_len; 429*cf7f2e2dSJohn Marino ax->reg_mask = new_reg_mask; 430*cf7f2e2dSJohn Marino } 431*cf7f2e2dSJohn Marino 432*cf7f2e2dSJohn Marino ax->reg_mask[byte] |= 1 << (reg % 8); 433*cf7f2e2dSJohn Marino } 434*cf7f2e2dSJohn Marino 435*cf7f2e2dSJohn Marino /* Given an agent expression AX, fill in requirements and other descriptive 436*cf7f2e2dSJohn Marino bits. */ 437*cf7f2e2dSJohn Marino void 438*cf7f2e2dSJohn Marino ax_reqs (struct agent_expr *ax) 4395796c8dcSSimon Schubert { 4405796c8dcSSimon Schubert int i; 4415796c8dcSSimon Schubert int height; 4425796c8dcSSimon Schubert 4435796c8dcSSimon Schubert /* Jump target table. targets[i] is non-zero iff we have found a 4445796c8dcSSimon Schubert jump to offset i. */ 4455796c8dcSSimon Schubert char *targets = (char *) alloca (ax->len * sizeof (targets[0])); 4465796c8dcSSimon Schubert 4475796c8dcSSimon Schubert /* Instruction boundary table. boundary[i] is non-zero iff our scan 4485796c8dcSSimon Schubert has reached an instruction starting at offset i. */ 4495796c8dcSSimon Schubert char *boundary = (char *) alloca (ax->len * sizeof (boundary[0])); 4505796c8dcSSimon Schubert 4515796c8dcSSimon Schubert /* Stack height record. If either targets[i] or boundary[i] is 4525796c8dcSSimon Schubert non-zero, heights[i] is the height the stack should have before 4535796c8dcSSimon Schubert executing the bytecode at that point. */ 4545796c8dcSSimon Schubert int *heights = (int *) alloca (ax->len * sizeof (heights[0])); 4555796c8dcSSimon Schubert 4565796c8dcSSimon Schubert /* Pointer to a description of the present op. */ 4575796c8dcSSimon Schubert struct aop_map *op; 4585796c8dcSSimon Schubert 4595796c8dcSSimon Schubert memset (targets, 0, ax->len * sizeof (targets[0])); 4605796c8dcSSimon Schubert memset (boundary, 0, ax->len * sizeof (boundary[0])); 4615796c8dcSSimon Schubert 462*cf7f2e2dSJohn Marino ax->max_height = ax->min_height = height = 0; 463*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_none; 464*cf7f2e2dSJohn Marino ax->max_data_size = 0; 4655796c8dcSSimon Schubert 4665796c8dcSSimon Schubert for (i = 0; i < ax->len; i += 1 + op->op_size) 4675796c8dcSSimon Schubert { 4685796c8dcSSimon Schubert if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0]))) 4695796c8dcSSimon Schubert { 470*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_bad_instruction; 4715796c8dcSSimon Schubert return; 4725796c8dcSSimon Schubert } 4735796c8dcSSimon Schubert 4745796c8dcSSimon Schubert op = &aop_map[ax->buf[i]]; 4755796c8dcSSimon Schubert 4765796c8dcSSimon Schubert if (!op->name) 4775796c8dcSSimon Schubert { 478*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_bad_instruction; 4795796c8dcSSimon Schubert return; 4805796c8dcSSimon Schubert } 4815796c8dcSSimon Schubert 4825796c8dcSSimon Schubert if (i + 1 + op->op_size > ax->len) 4835796c8dcSSimon Schubert { 484*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_incomplete_instruction; 4855796c8dcSSimon Schubert return; 4865796c8dcSSimon Schubert } 4875796c8dcSSimon Schubert 4885796c8dcSSimon Schubert /* If this instruction is a forward jump target, does the 4895796c8dcSSimon Schubert current stack height match the stack height at the jump 4905796c8dcSSimon Schubert source? */ 4915796c8dcSSimon Schubert if (targets[i] && (heights[i] != height)) 4925796c8dcSSimon Schubert { 493*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_height_mismatch; 4945796c8dcSSimon Schubert return; 4955796c8dcSSimon Schubert } 4965796c8dcSSimon Schubert 4975796c8dcSSimon Schubert boundary[i] = 1; 4985796c8dcSSimon Schubert heights[i] = height; 4995796c8dcSSimon Schubert 5005796c8dcSSimon Schubert height -= op->consumed; 501*cf7f2e2dSJohn Marino if (height < ax->min_height) 502*cf7f2e2dSJohn Marino ax->min_height = height; 5035796c8dcSSimon Schubert height += op->produced; 504*cf7f2e2dSJohn Marino if (height > ax->max_height) 505*cf7f2e2dSJohn Marino ax->max_height = height; 5065796c8dcSSimon Schubert 507*cf7f2e2dSJohn Marino if (op->data_size > ax->max_data_size) 508*cf7f2e2dSJohn Marino ax->max_data_size = op->data_size; 5095796c8dcSSimon Schubert 5105796c8dcSSimon Schubert /* For jump instructions, check that the target is a valid 5115796c8dcSSimon Schubert offset. If it is, record the fact that that location is a 5125796c8dcSSimon Schubert jump target, and record the height we expect there. */ 5135796c8dcSSimon Schubert if (aop_goto == op - aop_map 5145796c8dcSSimon Schubert || aop_if_goto == op - aop_map) 5155796c8dcSSimon Schubert { 5165796c8dcSSimon Schubert int target = read_const (ax, i + 1, 2); 5175796c8dcSSimon Schubert if (target < 0 || target >= ax->len) 5185796c8dcSSimon Schubert { 519*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_bad_jump; 5205796c8dcSSimon Schubert return; 5215796c8dcSSimon Schubert } 5225796c8dcSSimon Schubert 5235796c8dcSSimon Schubert /* Do we have any information about what the stack height 5245796c8dcSSimon Schubert should be at the target? */ 5255796c8dcSSimon Schubert if (targets[target] || boundary[target]) 5265796c8dcSSimon Schubert { 5275796c8dcSSimon Schubert if (heights[target] != height) 5285796c8dcSSimon Schubert { 529*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_height_mismatch; 5305796c8dcSSimon Schubert return; 5315796c8dcSSimon Schubert } 5325796c8dcSSimon Schubert } 5335796c8dcSSimon Schubert 5345796c8dcSSimon Schubert /* Record the target, along with the stack height we expect. */ 5355796c8dcSSimon Schubert targets[target] = 1; 5365796c8dcSSimon Schubert heights[target] = height; 5375796c8dcSSimon Schubert } 5385796c8dcSSimon Schubert 5395796c8dcSSimon Schubert /* For unconditional jumps with a successor, check that the 5405796c8dcSSimon Schubert successor is a target, and pick up its stack height. */ 5415796c8dcSSimon Schubert if (aop_goto == op - aop_map 5425796c8dcSSimon Schubert && i + 3 < ax->len) 5435796c8dcSSimon Schubert { 5445796c8dcSSimon Schubert if (!targets[i + 3]) 5455796c8dcSSimon Schubert { 546*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_hole; 5475796c8dcSSimon Schubert return; 5485796c8dcSSimon Schubert } 5495796c8dcSSimon Schubert 5505796c8dcSSimon Schubert height = heights[i + 3]; 5515796c8dcSSimon Schubert } 5525796c8dcSSimon Schubert 5535796c8dcSSimon Schubert /* For reg instructions, record the register in the bit mask. */ 5545796c8dcSSimon Schubert if (aop_reg == op - aop_map) 5555796c8dcSSimon Schubert { 5565796c8dcSSimon Schubert int reg = read_const (ax, i + 1, 2); 5575796c8dcSSimon Schubert 558*cf7f2e2dSJohn Marino ax_reg_mask (ax, reg); 5595796c8dcSSimon Schubert } 5605796c8dcSSimon Schubert } 5615796c8dcSSimon Schubert 5625796c8dcSSimon Schubert /* Check that all the targets are on boundaries. */ 5635796c8dcSSimon Schubert for (i = 0; i < ax->len; i++) 5645796c8dcSSimon Schubert if (targets[i] && !boundary[i]) 5655796c8dcSSimon Schubert { 566*cf7f2e2dSJohn Marino ax->flaw = agent_flaw_bad_jump; 5675796c8dcSSimon Schubert return; 5685796c8dcSSimon Schubert } 5695796c8dcSSimon Schubert 570*cf7f2e2dSJohn Marino ax->final_height = height; 5715796c8dcSSimon Schubert } 572