]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ax-general.c
Make aop_map 'static'
[thirdparty/binutils-gdb.git] / gdb / ax-general.c
CommitLineData
c906108c 1/* Functions for manipulating expressions designed to be executed on the agent
213516ef 2 Copyright (C) 1998-2023 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
c5aa993b 9 (at your option) any later version.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b 16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 18
c906108c
SS
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.) */
23
24#include "defs.h"
d55e5aa6 25#include "ax.h"
0d12e84c 26#include "gdbarch.h"
4de283e4 27
d55e5aa6 28#include "value.h"
4de283e4 29#include "user-regs.h"
175ff332 30
a14ed312 31static void append_const (struct agent_expr *x, LONGEST val, int n);
392a587b 32
a14ed312 33static LONGEST read_const (struct agent_expr *x, int o, int n);
392a587b 34
a14ed312 35static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
c906108c
SS
36\f
37/* Functions for building expressions. */
38
c906108c
SS
39/* Append the low N bytes of VAL as an N-byte integer to the
40 expression X, in big-endian order. */
41static void
fba45db2 42append_const (struct agent_expr *x, LONGEST val, int n)
c906108c
SS
43{
44 int i;
45
c906108c
SS
46 for (i = n - 1; i >= 0; i--)
47 {
6f96f485 48 x->buf.push_back (val & 0xff);
c906108c
SS
49 val >>= 8;
50 }
c906108c
SS
51}
52
53
54/* Extract an N-byte big-endian unsigned integer from expression X at
55 offset O. */
56static LONGEST
fba45db2 57read_const (struct agent_expr *x, int o, int n)
c906108c
SS
58{
59 int i;
60 LONGEST accum = 0;
61
62 /* Make sure we're not reading off the end of the expression. */
6f96f485 63 if (o + n > x->buf.size ())
3d263c1d 64 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
c906108c
SS
65
66 for (i = 0; i < n; i++)
67 accum = (accum << 8) | x->buf[o + i];
c5aa993b 68
c906108c
SS
69 return accum;
70}
71
70b8286a
SM
72/* See ax.h. */
73
74void
75ax_raw_byte (struct agent_expr *x, gdb_byte byte)
76{
6f96f485 77 x->buf.push_back (byte);
70b8286a 78}
c906108c
SS
79
80/* Append a simple operator OP to EXPR. */
81void
fba45db2 82ax_simple (struct agent_expr *x, enum agent_op op)
c906108c 83{
70b8286a 84 ax_raw_byte (x, op);
c906108c
SS
85}
86
c7f96d2b
TT
87/* Append a pick operator to EXPR. DEPTH is the stack item to pick,
88 with 0 being top of stack. */
2b52013f 89
c7f96d2b
TT
90void
91ax_pick (struct agent_expr *x, int depth)
92{
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);
97}
98
c906108c
SS
99
100/* Append a sign-extension or zero-extension instruction to EXPR, to
101 extend an N-bit value. */
102static void
fba45db2 103generic_ext (struct agent_expr *x, enum agent_op op, int n)
c906108c
SS
104{
105 /* N must fit in a byte. */
106 if (n < 0 || n > 255)
3d263c1d 107 error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
c906108c
SS
108 /* That had better be enough range. */
109 if (sizeof (LONGEST) * 8 > 255)
3e43a32a
MS
110 error (_("GDB bug: ax-general.c (generic_ext): "
111 "opcode has inadequate range"));
c906108c 112
6f96f485
TT
113 x->buf.push_back (op);
114 x->buf.push_back (n);
c906108c
SS
115}
116
117
118/* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
119void
fba45db2 120ax_ext (struct agent_expr *x, int n)
c906108c
SS
121{
122 generic_ext (x, aop_ext, n);
123}
124
125
126/* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
127void
fba45db2 128ax_zero_ext (struct agent_expr *x, int n)
c906108c
SS
129{
130 generic_ext (x, aop_zero_ext, n);
131}
132
133
134/* Append a trace_quick instruction to EXPR, to record N bytes. */
135void
fba45db2 136ax_trace_quick (struct agent_expr *x, int n)
c906108c
SS
137{
138 /* N must fit in a byte. */
139 if (n < 0 || n > 255)
3e43a32a
MS
140 error (_("GDB bug: ax-general.c (ax_trace_quick): "
141 "size out of range for trace_quick"));
c906108c 142
6f96f485
TT
143 x->buf.push_back (aop_trace_quick);
144 x->buf.push_back (n);
c906108c
SS
145}
146
147
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. */
c5aa993b 154int
fba45db2 155ax_goto (struct agent_expr *x, enum agent_op op)
c906108c 156{
6f96f485
TT
157 x->buf.push_back (op);
158 x->buf.push_back (0xff);
159 x->buf.push_back (0xff);
160 return x->buf.size () - 2;
c906108c
SS
161}
162
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
c5aa993b 165 ax_label (EXPR, PATCH, TARGET)
c906108c
SS
166 to patch TARGET into the ax_goto instruction. */
167void
fba45db2 168ax_label (struct agent_expr *x, int patch, int target)
c906108c
SS
169{
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)
3d263c1d 173 error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
c5aa993b 174
c906108c
SS
175 x->buf[patch] = (target >> 8) & 0xff;
176 x->buf[patch + 1] = target & 0xff;
177}
178
179
180/* Assemble code to push a constant on the stack. */
181void
fba45db2 182ax_const_l (struct agent_expr *x, LONGEST l)
c906108c
SS
183{
184 static enum agent_op ops[]
c5aa993b
JM
185 =
186 {aop_const8, aop_const16, aop_const32, aop_const64};
c906108c
SS
187 int size;
188 int op;
189
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++)
44a81774 195 {
cf3e25ca 196 LONGEST lim = ((LONGEST) 1) << (size - 1);
44a81774
JB
197
198 if (-lim <= l && l <= lim - 1)
dda83cd7 199 break;
44a81774 200 }
c906108c 201
0e2de366 202 /* Emit the right opcode... */
c906108c
SS
203 ax_simple (x, ops[op]);
204
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);
208
209 /* Now, if it was negative, and not full-sized, sign-extend it. */
210 if (l < 0 && size < 64)
211 ax_ext (x, size);
212}
213
214
215void
fba45db2 216ax_const_d (struct agent_expr *x, LONGEST d)
c906108c
SS
217{
218 /* FIXME: floating-point support not present yet. */
3e43a32a
MS
219 error (_("GDB bug: ax-general.c (ax_const_d): "
220 "floating point not supported yet"));
c906108c
SS
221}
222
223
224/* Assemble code to push the value of register number REG on the
225 stack. */
c5aa993b 226void
fba45db2 227ax_reg (struct agent_expr *x, int reg)
c906108c 228{
175ff332
HZ
229 if (reg >= gdbarch_num_regs (x->gdbarch))
230 {
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));
239 }
240 else
241 {
1eb7c2d8
AT
242 /* Get the remote register number. */
243 reg = gdbarch_remote_register_number (x->gdbarch, reg);
244
175ff332
HZ
245 /* Make sure the register number is in range. */
246 if (reg < 0 || reg > 0xffff)
dda83cd7 247 error (_("GDB bug: ax-general.c (ax_reg): "
3e43a32a 248 "register number out of range"));
6f96f485
TT
249 x->buf.push_back (aop_reg);
250 x->buf.push_back ((reg >> 8) & 0xff);
251 x->buf.push_back ((reg) & 0xff);
175ff332 252 }
c906108c 253}
f61e138d
SS
254
255/* Assemble code to operate on a trace state variable. */
256
257void
258ax_tsv (struct agent_expr *x, enum agent_op op, int num)
259{
260 /* Make sure the tsv number is in range. */
261 if (num < 0 || num > 0xffff)
f34652de 262 internal_error (_("ax-general.c (ax_tsv): variable "
3e43a32a 263 "number is %d, out of range"), num);
f61e138d 264
6f96f485
TT
265 x->buf.push_back (op);
266 x->buf.push_back ((num >> 8) & 0xff);
267 x->buf.push_back ((num) & 0xff);
f61e138d 268}
d3ce09f5
SS
269
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.) */
274
275void
741d92cf 276ax_string (struct agent_expr *x, const char *str, int slen)
d3ce09f5
SS
277{
278 int i;
279
280 /* Make sure the string length is reasonable. */
281 if (slen < 0 || slen > 0xffff)
f34652de 282 internal_error (_("ax-general.c (ax_string): string "
d3ce09f5
SS
283 "length is %d, out of allowed range"), slen);
284
6f96f485
TT
285 x->buf.push_back (((slen + 1) >> 8) & 0xff);
286 x->buf.push_back ((slen + 1) & 0xff);
d3ce09f5 287 for (i = 0; i < slen; ++i)
6f96f485
TT
288 x->buf.push_back (str[i]);
289 x->buf.push_back ('\0');
d3ce09f5 290}
c5aa993b 291\f
c906108c
SS
292
293
c906108c
SS
294/* Functions for disassembling agent expressions, and otherwise
295 debugging the expression compiler. */
296
1e0e3ecd
TT
297/* An entry in the opcode map. */
298struct aop_map
299 {
300
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;
304
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.
310
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;
315
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;
319
320 /* Number of stack elements consumed, and number produced. */
321 int consumed, produced;
322 };
323
324/* Map of the bytecodes, indexed by bytecode number. */
325
326static struct aop_map aop_map[] =
c5aa993b 327{
94d5e490
TT
328 {0, 0, 0, 0, 0}
329#define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
330 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
268a13a5 331#include "gdbsupport/ax.def"
94d5e490 332#undef DEFOP
c906108c
SS
333};
334
335
336/* Disassemble the expression EXPR, writing to F. */
337void
fba45db2 338ax_print (struct ui_file *f, struct agent_expr *x)
c906108c
SS
339{
340 int i;
c906108c 341
6cb06a8c
TT
342 gdb_printf (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
343 gdb_printf (f, _("Reg mask:"));
a2bbca9f
TT
344 for (i = 0; i < x->reg_mask.size (); ++i)
345 {
346 if ((i % 8) == 0)
347 gdb_printf (f, " ");
348 gdb_printf (f, _("%d"), (int) x->reg_mask[i]);
349 }
6cb06a8c 350 gdb_printf (f, _("\n"));
35c9c7ba 351
c906108c
SS
352 /* Check the size of the name array against the number of entries in
353 the enum, to catch additions that people didn't sync. */
354 if ((sizeof (aop_map) / sizeof (aop_map[0]))
355 != aop_last)
3d263c1d 356 error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));
c5aa993b 357
6f96f485 358 for (i = 0; i < x->buf.size ();)
c906108c 359 {
aead7601 360 enum agent_op op = (enum agent_op) x->buf[i];
c906108c
SS
361
362 if (op >= (sizeof (aop_map) / sizeof (aop_map[0]))
c5aa993b 363 || !aop_map[op].name)
c906108c 364 {
6cb06a8c 365 gdb_printf (f, _("%3d <bad opcode %02x>\n"), i, op);
c906108c
SS
366 i++;
367 continue;
368 }
6f96f485 369 if (i + 1 + aop_map[op].op_size > x->buf.size ())
c906108c 370 {
6cb06a8c
TT
371 gdb_printf (f, _("%3d <incomplete opcode %s>\n"),
372 i, aop_map[op].name);
c906108c
SS
373 break;
374 }
375
6cb06a8c 376 gdb_printf (f, "%3d %s", i, aop_map[op].name);
a04b0428 377 if (aop_map[op].op_size > 0)
c906108c 378 {
0426ad51 379 gdb_puts (" ", f);
c5aa993b 380
c906108c 381 print_longest (f, 'd', 0,
a04b0428 382 read_const (x, i + 1, aop_map[op].op_size));
c906108c 383 }
d3ce09f5
SS
384 /* Handle the complicated printf arguments specially. */
385 else if (op == aop_printf)
386 {
387 int slen, nargs;
388
389 i++;
390 nargs = x->buf[i++];
391 slen = x->buf[i++];
392 slen = slen * 256 + x->buf[i++];
6cb06a8c
TT
393 gdb_printf (f, _(" \"%s\", %d args"),
394 &(x->buf[i]), nargs);
d3ce09f5
SS
395 i += slen - 1;
396 }
6cb06a8c 397 gdb_printf (f, "\n");
a04b0428 398 i += 1 + aop_map[op].op_size;
c906108c
SS
399 }
400}
401
35c9c7ba
SS
402/* Add register REG to the register mask for expression AX. */
403void
404ax_reg_mask (struct agent_expr *ax, int reg)
405{
175ff332 406 if (reg >= gdbarch_num_regs (ax->gdbarch))
35c9c7ba 407 {
175ff332
HZ
408 /* This is a pseudo-register. */
409 if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
410 error (_("'%s' is a pseudo-register; "
411 "GDB cannot yet trace its contents."),
412 user_reg_map_regnum_to_name (ax->gdbarch, reg));
413 if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
414 error (_("Trace '%s' failed."),
415 user_reg_map_regnum_to_name (ax->gdbarch, reg));
416 }
417 else
418 {
1eb7c2d8
AT
419 /* Get the remote register number. */
420 reg = gdbarch_remote_register_number (ax->gdbarch, reg);
175ff332
HZ
421
422 /* Grow the bit mask if necessary. */
a2bbca9f
TT
423 if (reg >= ax->reg_mask.size ())
424 ax->reg_mask.resize (reg);
175ff332 425
a2bbca9f 426 ax->reg_mask[reg] = true;
35c9c7ba 427 }
35c9c7ba
SS
428}
429
430/* Given an agent expression AX, fill in requirements and other descriptive
431 bits. */
c906108c 432void
35c9c7ba 433ax_reqs (struct agent_expr *ax)
c906108c
SS
434{
435 int i;
436 int height;
437
3d269a59
JB
438 /* Jump target table. targets[i] is non-zero iff we have found a
439 jump to offset i. */
6f96f485 440 char *targets = (char *) alloca (ax->buf.size () * sizeof (targets[0]));
c906108c 441
3d269a59
JB
442 /* Instruction boundary table. boundary[i] is non-zero iff our scan
443 has reached an instruction starting at offset i. */
6f96f485 444 char *boundary = (char *) alloca (ax->buf.size () * sizeof (boundary[0]));
c906108c 445
3d269a59 446 /* Stack height record. If either targets[i] or boundary[i] is
c906108c
SS
447 non-zero, heights[i] is the height the stack should have before
448 executing the bytecode at that point. */
6f96f485 449 int *heights = (int *) alloca (ax->buf.size () * sizeof (heights[0]));
c906108c
SS
450
451 /* Pointer to a description of the present op. */
452 struct aop_map *op;
453
6f96f485
TT
454 memset (targets, 0, ax->buf.size () * sizeof (targets[0]));
455 memset (boundary, 0, ax->buf.size () * sizeof (boundary[0]));
c906108c 456
35c9c7ba
SS
457 ax->max_height = ax->min_height = height = 0;
458 ax->flaw = agent_flaw_none;
459 ax->max_data_size = 0;
c906108c 460
6f96f485 461 for (i = 0; i < ax->buf.size (); i += 1 + op->op_size)
c906108c
SS
462 {
463 if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0])))
464 {
35c9c7ba 465 ax->flaw = agent_flaw_bad_instruction;
c906108c
SS
466 return;
467 }
468
469 op = &aop_map[ax->buf[i]];
470
c5aa993b 471 if (!op->name)
c906108c 472 {
35c9c7ba 473 ax->flaw = agent_flaw_bad_instruction;
c906108c
SS
474 return;
475 }
c5aa993b 476
6f96f485 477 if (i + 1 + op->op_size > ax->buf.size ())
c906108c 478 {
35c9c7ba 479 ax->flaw = agent_flaw_incomplete_instruction;
c906108c
SS
480 return;
481 }
482
3d269a59 483 /* If this instruction is a forward jump target, does the
dda83cd7
SM
484 current stack height match the stack height at the jump
485 source? */
c906108c
SS
486 if (targets[i] && (heights[i] != height))
487 {
35c9c7ba 488 ax->flaw = agent_flaw_height_mismatch;
c906108c
SS
489 return;
490 }
491
492 boundary[i] = 1;
493 heights[i] = height;
494
a04b0428 495 height -= op->consumed;
35c9c7ba
SS
496 if (height < ax->min_height)
497 ax->min_height = height;
c906108c 498 height += op->produced;
35c9c7ba
SS
499 if (height > ax->max_height)
500 ax->max_height = height;
c906108c 501
35c9c7ba
SS
502 if (op->data_size > ax->max_data_size)
503 ax->max_data_size = op->data_size;
c906108c
SS
504
505 /* For jump instructions, check that the target is a valid
dda83cd7
SM
506 offset. If it is, record the fact that that location is a
507 jump target, and record the height we expect there. */
c906108c
SS
508 if (aop_goto == op - aop_map
509 || aop_if_goto == op - aop_map)
510 {
511 int target = read_const (ax, i + 1, 2);
6f96f485 512 if (target < 0 || target >= ax->buf.size ())
c906108c 513 {
35c9c7ba 514 ax->flaw = agent_flaw_bad_jump;
c906108c
SS
515 return;
516 }
3d269a59
JB
517
518 /* Do we have any information about what the stack height
dda83cd7 519 should be at the target? */
3d269a59 520 if (targets[target] || boundary[target])
c906108c 521 {
3d269a59 522 if (heights[target] != height)
c906108c 523 {
35c9c7ba 524 ax->flaw = agent_flaw_height_mismatch;
c906108c
SS
525 return;
526 }
527 }
3d269a59 528
dda83cd7
SM
529 /* Record the target, along with the stack height we expect. */
530 targets[target] = 1;
531 heights[target] = height;
c906108c 532 }
c5aa993b 533
c906108c 534 /* For unconditional jumps with a successor, check that the
dda83cd7 535 successor is a target, and pick up its stack height. */
c906108c 536 if (aop_goto == op - aop_map
6f96f485 537 && i + 3 < ax->buf.size ())
c906108c 538 {
c5aa993b 539 if (!targets[i + 3])
c906108c 540 {
35c9c7ba 541 ax->flaw = agent_flaw_hole;
c906108c
SS
542 return;
543 }
544
545 height = heights[i + 3];
546 }
547
548 /* For reg instructions, record the register in the bit mask. */
549 if (aop_reg == op - aop_map)
550 {
551 int reg = read_const (ax, i + 1, 2);
c906108c 552
35c9c7ba 553 ax_reg_mask (ax, reg);
c906108c
SS
554 }
555 }
556
557 /* Check that all the targets are on boundaries. */
6f96f485 558 for (i = 0; i < ax->buf.size (); i++)
c906108c
SS
559 if (targets[i] && !boundary[i])
560 {
35c9c7ba 561 ax->flaw = agent_flaw_bad_jump;
c906108c
SS
562 return;
563 }
564
35c9c7ba 565 ax->final_height = height;
c906108c 566}