[gdb/tdep] Fix reverse execution of LDR(immediate) T4
[binutils-gdb.git] / gdb / ax-general.c
blob19c684c04e01e6d1319ab93c01bc397e908c666c
1 /* Functions for manipulating expressions designed to be executed on the agent
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Despite what the above comment says about this file being part of
20 GDB, we would like to keep these functions free of GDB
21 dependencies, since we want to be able to use them in contexts
22 outside of GDB (test suites, the stub, etc.) */
24 #include "defs.h"
25 #include "ax.h"
26 #include "gdbarch.h"
28 #include "value.h"
29 #include "user-regs.h"
31 static void append_const (struct agent_expr *x, LONGEST val, int n);
33 static LONGEST read_const (struct agent_expr *x, int o, int n);
35 static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
37 /* Functions for building expressions. */
39 /* Append the low N bytes of VAL as an N-byte integer to the
40 expression X, in big-endian order. */
41 static void
42 append_const (struct agent_expr *x, LONGEST val, int n)
44 size_t len = x->buf.size ();
45 x->buf.resize (len + n);
46 for (int i = n - 1; i >= 0; i--)
48 x->buf[len + i] = val & 0xff;
49 val >>= 8;
54 /* Extract an N-byte big-endian unsigned integer from expression X at
55 offset O. */
56 static LONGEST
57 read_const (struct agent_expr *x, int o, int n)
59 int i;
60 LONGEST accum = 0;
62 /* Make sure we're not reading off the end of the expression. */
63 if (o + n > x->buf.size ())
64 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
66 for (i = 0; i < n; i++)
67 accum = (accum << 8) | x->buf[o + i];
69 return accum;
72 /* See ax.h. */
74 void
75 ax_raw_byte (struct agent_expr *x, gdb_byte byte)
77 x->buf.push_back (byte);
80 /* Append a simple operator OP to EXPR. */
81 void
82 ax_simple (struct agent_expr *x, enum agent_op op)
84 ax_raw_byte (x, op);
87 /* Append a pick operator to EXPR. DEPTH is the stack item to pick,
88 with 0 being top of stack. */
90 void
91 ax_pick (struct agent_expr *x, int depth)
93 if (depth < 0 || depth > 255)
94 error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
95 ax_simple (x, aop_pick);
96 append_const (x, 1, depth);
100 /* Append a sign-extension or zero-extension instruction to EXPR, to
101 extend an N-bit value. */
102 static void
103 generic_ext (struct agent_expr *x, enum agent_op op, int n)
105 /* N must fit in a byte. */
106 if (n < 0 || n > 255)
107 error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
108 /* That had better be enough range. */
109 if (sizeof (LONGEST) * 8 > 255)
110 error (_("GDB bug: ax-general.c (generic_ext): "
111 "opcode has inadequate range"));
113 x->buf.push_back (op);
114 x->buf.push_back (n);
118 /* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
119 void
120 ax_ext (struct agent_expr *x, int n)
122 generic_ext (x, aop_ext, n);
126 /* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
127 void
128 ax_zero_ext (struct agent_expr *x, int n)
130 generic_ext (x, aop_zero_ext, n);
134 /* Append a trace_quick instruction to EXPR, to record N bytes. */
135 void
136 ax_trace_quick (struct agent_expr *x, int n)
138 /* N must fit in a byte. */
139 if (n < 0 || n > 255)
140 error (_("GDB bug: ax-general.c (ax_trace_quick): "
141 "size out of range for trace_quick"));
143 x->buf.push_back (aop_trace_quick);
144 x->buf.push_back (n);
148 /* Append a goto op to EXPR. OP is the actual op (must be aop_goto or
149 aop_if_goto). We assume we don't know the target offset yet,
150 because it's probably a forward branch, so we leave space in EXPR
151 for the target, and return the offset in EXPR of that space, so we
152 can backpatch it once we do know the target offset. Use ax_label
153 to do the backpatching. */
155 ax_goto (struct agent_expr *x, enum agent_op op)
157 x->buf.push_back (op);
158 x->buf.push_back (0xff);
159 x->buf.push_back (0xff);
160 return x->buf.size () - 2;
163 /* Suppose a given call to ax_goto returns some value PATCH. When you
164 know the offset TARGET that goto should jump to, call
165 ax_label (EXPR, PATCH, TARGET)
166 to patch TARGET into the ax_goto instruction. */
167 void
168 ax_label (struct agent_expr *x, int patch, int target)
170 /* Make sure the value is in range. Don't accept 0xffff as an
171 offset; that's our magic sentinel value for unpatched branches. */
172 if (target < 0 || target >= 0xffff)
173 error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
175 x->buf[patch] = (target >> 8) & 0xff;
176 x->buf[patch + 1] = target & 0xff;
180 /* Assemble code to push a constant on the stack. */
181 void
182 ax_const_l (struct agent_expr *x, LONGEST l)
184 static enum agent_op ops[]
186 {aop_const8, aop_const16, aop_const32, aop_const64};
187 int size;
188 int op;
190 /* How big is the number? 'op' keeps track of which opcode to use.
191 Notice that we don't really care whether the original number was
192 signed or unsigned; we always reproduce the value exactly, and
193 use the shortest representation. */
194 for (op = 0, size = 8; size < 64; size *= 2, op++)
196 LONGEST lim = ((LONGEST) 1) << (size - 1);
198 if (-lim <= l && l <= lim - 1)
199 break;
202 /* Emit the right opcode... */
203 ax_simple (x, ops[op]);
205 /* Emit the low SIZE bytes as an unsigned number. We know that
206 sign-extending this will yield l. */
207 append_const (x, l, size / 8);
209 /* Now, if it was negative, and not full-sized, sign-extend it. */
210 if (l < 0 && size < 64)
211 ax_ext (x, size);
215 void
216 ax_const_d (struct agent_expr *x, LONGEST d)
218 /* FIXME: floating-point support not present yet. */
219 error (_("GDB bug: ax-general.c (ax_const_d): "
220 "floating point not supported yet"));
224 /* Assemble code to push the value of register number REG on the
225 stack. */
226 void
227 ax_reg (struct agent_expr *x, int reg)
229 if (reg >= gdbarch_num_regs (x->gdbarch))
231 /* This is a pseudo-register. */
232 if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch))
233 error (_("'%s' is a pseudo-register; "
234 "GDB cannot yet trace its contents."),
235 user_reg_map_regnum_to_name (x->gdbarch, reg));
236 if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg))
237 error (_("Trace '%s' failed."),
238 user_reg_map_regnum_to_name (x->gdbarch, reg));
240 else
242 /* Get the remote register number. */
243 reg = gdbarch_remote_register_number (x->gdbarch, reg);
245 /* Make sure the register number is in range. */
246 if (reg < 0 || reg > 0xffff)
247 error (_("GDB bug: ax-general.c (ax_reg): "
248 "register number out of range"));
249 x->buf.push_back (aop_reg);
250 x->buf.push_back ((reg >> 8) & 0xff);
251 x->buf.push_back ((reg) & 0xff);
255 /* Assemble code to operate on a trace state variable. */
257 void
258 ax_tsv (struct agent_expr *x, enum agent_op op, int num)
260 /* Make sure the tsv number is in range. */
261 if (num < 0 || num > 0xffff)
262 internal_error (_("ax-general.c (ax_tsv): variable "
263 "number is %d, out of range"), num);
265 x->buf.push_back (op);
266 x->buf.push_back ((num >> 8) & 0xff);
267 x->buf.push_back ((num) & 0xff);
270 /* Append a string to the expression. Note that the string is going
271 into the bytecodes directly, not on the stack. As a precaution,
272 include both length as prefix, and terminate with a NUL. (The NUL
273 is counted in the length.) */
275 void
276 ax_string (struct agent_expr *x, const char *str, int slen)
278 int i;
280 /* Make sure the string length is reasonable. */
281 if (slen < 0 || slen > 0xffff)
282 internal_error (_("ax-general.c (ax_string): string "
283 "length is %d, out of allowed range"), slen);
285 x->buf.push_back (((slen + 1) >> 8) & 0xff);
286 x->buf.push_back ((slen + 1) & 0xff);
287 for (i = 0; i < slen; ++i)
288 x->buf.push_back (str[i]);
289 x->buf.push_back ('\0');
294 /* Functions for disassembling agent expressions, and otherwise
295 debugging the expression compiler. */
297 /* An entry in the opcode map. */
298 struct aop_map
301 /* The name of the opcode. Null means that this entry is not a
302 valid opcode --- a hole in the opcode space. */
303 const char *name;
305 /* All opcodes take no operands from the bytecode stream, or take
306 unsigned integers of various sizes. If this is a positive number
307 n, then the opcode is followed by an n-byte operand, which should
308 be printed as an unsigned integer. If this is zero, then the
309 opcode takes no operands from the bytecode stream.
311 If we get more complicated opcodes in the future, don't add other
312 magic values of this; that's a crock. Add an `enum encoding'
313 field to this, or something like that. */
314 int op_size;
316 /* The size of the data operated upon, in bits, for bytecodes that
317 care about that (ref and const). Zero for all others. */
318 int data_size;
320 /* Number of stack elements consumed, and number produced. */
321 int consumed, produced;
324 /* Map of the bytecodes, indexed by bytecode number. */
326 static struct aop_map aop_map[] =
328 {0, 0, 0, 0, 0}
329 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
330 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
331 #include "gdbsupport/ax.def"
332 #undef DEFOP
336 /* Disassemble the expression EXPR, writing to F. */
337 void
338 ax_print (struct ui_file *f, struct agent_expr *x)
340 int i;
342 gdb_printf (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
343 gdb_printf (f, _("Reg mask:"));
344 for (i = 0; i < x->reg_mask.size (); ++i)
346 if ((i % 8) == 0)
347 gdb_printf (f, " ");
348 gdb_printf (f, _("%d"), (int) x->reg_mask[i]);
350 gdb_printf (f, _("\n"));
352 for (i = 0; i < x->buf.size ();)
354 enum agent_op op = (enum agent_op) x->buf[i];
356 if (op >= ARRAY_SIZE (aop_map) || aop_map[op].name == nullptr)
358 gdb_printf (f, _("%3d <bad opcode %02x>\n"), i, op);
359 i++;
360 continue;
362 if (i + 1 + aop_map[op].op_size > x->buf.size ())
364 gdb_printf (f, _("%3d <incomplete opcode %s>\n"),
365 i, aop_map[op].name);
366 break;
369 gdb_printf (f, "%3d %s", i, aop_map[op].name);
370 if (aop_map[op].op_size > 0)
372 gdb_puts (" ", f);
374 print_longest (f, 'd', 0,
375 read_const (x, i + 1, aop_map[op].op_size));
377 /* Handle the complicated printf arguments specially. */
378 else if (op == aop_printf)
380 int slen, nargs;
382 i++;
383 nargs = x->buf[i++];
384 slen = x->buf[i++];
385 slen = slen * 256 + x->buf[i++];
386 gdb_printf (f, _(" \"%s\", %d args"),
387 &(x->buf[i]), nargs);
388 i += slen - 1;
390 gdb_printf (f, "\n");
391 i += 1 + aop_map[op].op_size;
395 /* Add register REG to the register mask for expression AX. */
396 void
397 ax_reg_mask (struct agent_expr *ax, int reg)
399 if (reg >= gdbarch_num_regs (ax->gdbarch))
401 /* This is a pseudo-register. */
402 if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
403 error (_("'%s' is a pseudo-register; "
404 "GDB cannot yet trace its contents."),
405 user_reg_map_regnum_to_name (ax->gdbarch, reg));
406 if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
407 error (_("Trace '%s' failed."),
408 user_reg_map_regnum_to_name (ax->gdbarch, reg));
410 else
412 /* Get the remote register number. */
413 reg = gdbarch_remote_register_number (ax->gdbarch, reg);
415 /* Grow the bit mask if necessary. */
416 if (reg >= ax->reg_mask.size ())
417 ax->reg_mask.resize (reg + 1);
419 ax->reg_mask[reg] = true;
423 /* Given an agent expression AX, fill in requirements and other descriptive
424 bits. */
425 void
426 ax_reqs (struct agent_expr *ax)
428 int i;
429 int height;
431 /* Jump target table. targets[i] is non-zero iff we have found a
432 jump to offset i. */
433 char *targets = (char *) alloca (ax->buf.size () * sizeof (targets[0]));
435 /* Instruction boundary table. boundary[i] is non-zero iff our scan
436 has reached an instruction starting at offset i. */
437 char *boundary = (char *) alloca (ax->buf.size () * sizeof (boundary[0]));
439 /* Stack height record. If either targets[i] or boundary[i] is
440 non-zero, heights[i] is the height the stack should have before
441 executing the bytecode at that point. */
442 int *heights = (int *) alloca (ax->buf.size () * sizeof (heights[0]));
444 /* Pointer to a description of the present op. */
445 struct aop_map *op;
447 memset (targets, 0, ax->buf.size () * sizeof (targets[0]));
448 memset (boundary, 0, ax->buf.size () * sizeof (boundary[0]));
450 ax->max_height = ax->min_height = height = 0;
451 ax->flaw = agent_flaw_none;
452 ax->max_data_size = 0;
454 for (i = 0; i < ax->buf.size (); i += 1 + op->op_size)
456 if (ax->buf[i] >= ARRAY_SIZE (aop_map))
458 ax->flaw = agent_flaw_bad_instruction;
459 return;
462 op = &aop_map[ax->buf[i]];
464 if (!op->name)
466 ax->flaw = agent_flaw_bad_instruction;
467 return;
470 if (i + 1 + op->op_size > ax->buf.size ())
472 ax->flaw = agent_flaw_incomplete_instruction;
473 return;
476 /* If this instruction is a forward jump target, does the
477 current stack height match the stack height at the jump
478 source? */
479 if (targets[i] && (heights[i] != height))
481 ax->flaw = agent_flaw_height_mismatch;
482 return;
485 boundary[i] = 1;
486 heights[i] = height;
488 height -= op->consumed;
489 if (height < ax->min_height)
490 ax->min_height = height;
491 height += op->produced;
492 if (height > ax->max_height)
493 ax->max_height = height;
495 if (op->data_size > ax->max_data_size)
496 ax->max_data_size = op->data_size;
498 /* For jump instructions, check that the target is a valid
499 offset. If it is, record the fact that that location is a
500 jump target, and record the height we expect there. */
501 if (aop_goto == op - aop_map
502 || aop_if_goto == op - aop_map)
504 int target = read_const (ax, i + 1, 2);
505 if (target < 0 || target >= ax->buf.size ())
507 ax->flaw = agent_flaw_bad_jump;
508 return;
511 /* Do we have any information about what the stack height
512 should be at the target? */
513 if (targets[target] || boundary[target])
515 if (heights[target] != height)
517 ax->flaw = agent_flaw_height_mismatch;
518 return;
522 /* Record the target, along with the stack height we expect. */
523 targets[target] = 1;
524 heights[target] = height;
527 /* For unconditional jumps with a successor, check that the
528 successor is a target, and pick up its stack height. */
529 if (aop_goto == op - aop_map
530 && i + 3 < ax->buf.size ())
532 if (!targets[i + 3])
534 ax->flaw = agent_flaw_hole;
535 return;
538 height = heights[i + 3];
541 /* For reg instructions, record the register in the bit mask. */
542 if (aop_reg == op - aop_map)
544 int reg = read_const (ax, i + 1, 2);
546 ax_reg_mask (ax, reg);
550 /* Check that all the targets are on boundaries. */
551 for (i = 0; i < ax->buf.size (); i++)
552 if (targets[i] && !boundary[i])
554 ax->flaw = agent_flaw_bad_jump;
555 return;
558 ax->final_height = height;