PR 15231: import bare DW_TAG_lexical_block
[deliverable/binutils-gdb.git] / gdb / ax-general.c
CommitLineData
c906108c 1/* Functions for manipulating expressions designed to be executed on the agent
618f726f 2 Copyright (C) 1998-2016 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"
25#include "ax.h"
26
7a292a7a 27#include "value.h"
175ff332
HZ
28#include "user-regs.h"
29
a14ed312 30static void grow_expr (struct agent_expr *x, int n);
392a587b 31
a14ed312 32static void append_const (struct agent_expr *x, LONGEST val, int n);
392a587b 33
a14ed312 34static LONGEST read_const (struct agent_expr *x, int o, int n);
392a587b 35
a14ed312 36static void generic_ext (struct agent_expr *x, enum agent_op op, int n);
c906108c
SS
37\f
38/* Functions for building expressions. */
39
40/* Allocate a new, empty agent expression. */
41struct agent_expr *
35c9c7ba 42new_agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope)
c906108c 43{
8d749320 44 struct agent_expr *x = XNEW (struct agent_expr);
35c9c7ba 45
c5aa993b 46 x->len = 0;
c906108c
SS
47 x->size = 1; /* Change this to a larger value once
48 reallocation code is tested. */
224c3ddb 49 x->buf = (unsigned char *) xmalloc (x->size);
35c9c7ba
SS
50
51 x->gdbarch = gdbarch;
c906108c
SS
52 x->scope = scope;
53
35c9c7ba
SS
54 /* Bit vector for registers used. */
55 x->reg_mask_len = 1;
224c3ddb 56 x->reg_mask = XCNEWVEC (unsigned char, x->reg_mask_len);
35c9c7ba 57
92bc6a20
TT
58 x->tracing = 0;
59 x->trace_string = 0;
60
c906108c
SS
61 return x;
62}
63
64/* Free a agent expression. */
65void
fba45db2 66free_agent_expr (struct agent_expr *x)
c906108c 67{
b8c9b27d 68 xfree (x->buf);
35c9c7ba 69 xfree (x->reg_mask);
b8c9b27d 70 xfree (x);
c906108c
SS
71}
72
f23d52e0
AC
73static void
74do_free_agent_expr_cleanup (void *x)
75{
9a3c8263 76 free_agent_expr ((struct agent_expr *) x);
f23d52e0
AC
77}
78
79struct cleanup *
80make_cleanup_free_agent_expr (struct agent_expr *x)
81{
82 return make_cleanup (do_free_agent_expr_cleanup, x);
83}
84
c906108c
SS
85
86/* Make sure that X has room for at least N more bytes. This doesn't
87 affect the length, just the allocated size. */
88static void
fba45db2 89grow_expr (struct agent_expr *x, int n)
c906108c
SS
90{
91 if (x->len + n > x->size)
92 {
93 x->size *= 2;
94 if (x->size < x->len + n)
95 x->size = x->len + n + 10;
224c3ddb 96 x->buf = (unsigned char *) xrealloc (x->buf, x->size);
c906108c
SS
97 }
98}
99
100
101/* Append the low N bytes of VAL as an N-byte integer to the
102 expression X, in big-endian order. */
103static void
fba45db2 104append_const (struct agent_expr *x, LONGEST val, int n)
c906108c
SS
105{
106 int i;
107
108 grow_expr (x, n);
109 for (i = n - 1; i >= 0; i--)
110 {
111 x->buf[x->len + i] = val & 0xff;
112 val >>= 8;
113 }
114 x->len += n;
115}
116
117
118/* Extract an N-byte big-endian unsigned integer from expression X at
119 offset O. */
120static LONGEST
fba45db2 121read_const (struct agent_expr *x, int o, int n)
c906108c
SS
122{
123 int i;
124 LONGEST accum = 0;
125
126 /* Make sure we're not reading off the end of the expression. */
127 if (o + n > x->len)
3d263c1d 128 error (_("GDB bug: ax-general.c (read_const): incomplete constant"));
c906108c
SS
129
130 for (i = 0; i < n; i++)
131 accum = (accum << 8) | x->buf[o + i];
c5aa993b 132
c906108c
SS
133 return accum;
134}
135
70b8286a
SM
136/* See ax.h. */
137
138void
139ax_raw_byte (struct agent_expr *x, gdb_byte byte)
140{
141 grow_expr (x, 1);
142 x->buf[x->len++] = byte;
143}
c906108c
SS
144
145/* Append a simple operator OP to EXPR. */
146void
fba45db2 147ax_simple (struct agent_expr *x, enum agent_op op)
c906108c 148{
70b8286a 149 ax_raw_byte (x, op);
c906108c
SS
150}
151
c7f96d2b
TT
152/* Append a pick operator to EXPR. DEPTH is the stack item to pick,
153 with 0 being top of stack. */
2b52013f 154
c7f96d2b
TT
155void
156ax_pick (struct agent_expr *x, int depth)
157{
158 if (depth < 0 || depth > 255)
159 error (_("GDB bug: ax-general.c (ax_pick): stack depth out of range"));
160 ax_simple (x, aop_pick);
161 append_const (x, 1, depth);
162}
163
c906108c
SS
164
165/* Append a sign-extension or zero-extension instruction to EXPR, to
166 extend an N-bit value. */
167static void
fba45db2 168generic_ext (struct agent_expr *x, enum agent_op op, int n)
c906108c
SS
169{
170 /* N must fit in a byte. */
171 if (n < 0 || n > 255)
3d263c1d 172 error (_("GDB bug: ax-general.c (generic_ext): bit count out of range"));
c906108c
SS
173 /* That had better be enough range. */
174 if (sizeof (LONGEST) * 8 > 255)
3e43a32a
MS
175 error (_("GDB bug: ax-general.c (generic_ext): "
176 "opcode has inadequate range"));
c906108c
SS
177
178 grow_expr (x, 2);
179 x->buf[x->len++] = op;
180 x->buf[x->len++] = n;
181}
182
183
184/* Append a sign-extension instruction to EXPR, to extend an N-bit value. */
185void
fba45db2 186ax_ext (struct agent_expr *x, int n)
c906108c
SS
187{
188 generic_ext (x, aop_ext, n);
189}
190
191
192/* Append a zero-extension instruction to EXPR, to extend an N-bit value. */
193void
fba45db2 194ax_zero_ext (struct agent_expr *x, int n)
c906108c
SS
195{
196 generic_ext (x, aop_zero_ext, n);
197}
198
199
200/* Append a trace_quick instruction to EXPR, to record N bytes. */
201void
fba45db2 202ax_trace_quick (struct agent_expr *x, int n)
c906108c
SS
203{
204 /* N must fit in a byte. */
205 if (n < 0 || n > 255)
3e43a32a
MS
206 error (_("GDB bug: ax-general.c (ax_trace_quick): "
207 "size out of range for trace_quick"));
c906108c
SS
208
209 grow_expr (x, 2);
210 x->buf[x->len++] = aop_trace_quick;
211 x->buf[x->len++] = n;
212}
213
214
215/* Append a goto op to EXPR. OP is the actual op (must be aop_goto or
216 aop_if_goto). We assume we don't know the target offset yet,
217 because it's probably a forward branch, so we leave space in EXPR
218 for the target, and return the offset in EXPR of that space, so we
219 can backpatch it once we do know the target offset. Use ax_label
220 to do the backpatching. */
c5aa993b 221int
fba45db2 222ax_goto (struct agent_expr *x, enum agent_op op)
c906108c
SS
223{
224 grow_expr (x, 3);
225 x->buf[x->len + 0] = op;
226 x->buf[x->len + 1] = 0xff;
227 x->buf[x->len + 2] = 0xff;
228 x->len += 3;
229 return x->len - 2;
230}
231
232/* Suppose a given call to ax_goto returns some value PATCH. When you
233 know the offset TARGET that goto should jump to, call
c5aa993b 234 ax_label (EXPR, PATCH, TARGET)
c906108c
SS
235 to patch TARGET into the ax_goto instruction. */
236void
fba45db2 237ax_label (struct agent_expr *x, int patch, int target)
c906108c
SS
238{
239 /* Make sure the value is in range. Don't accept 0xffff as an
240 offset; that's our magic sentinel value for unpatched branches. */
241 if (target < 0 || target >= 0xffff)
3d263c1d 242 error (_("GDB bug: ax-general.c (ax_label): label target out of range"));
c5aa993b 243
c906108c
SS
244 x->buf[patch] = (target >> 8) & 0xff;
245 x->buf[patch + 1] = target & 0xff;
246}
247
248
249/* Assemble code to push a constant on the stack. */
250void
fba45db2 251ax_const_l (struct agent_expr *x, LONGEST l)
c906108c
SS
252{
253 static enum agent_op ops[]
c5aa993b
JM
254 =
255 {aop_const8, aop_const16, aop_const32, aop_const64};
c906108c
SS
256 int size;
257 int op;
258
259 /* How big is the number? 'op' keeps track of which opcode to use.
260 Notice that we don't really care whether the original number was
261 signed or unsigned; we always reproduce the value exactly, and
262 use the shortest representation. */
263 for (op = 0, size = 8; size < 64; size *= 2, op++)
44a81774 264 {
cf3e25ca 265 LONGEST lim = ((LONGEST) 1) << (size - 1);
44a81774
JB
266
267 if (-lim <= l && l <= lim - 1)
268 break;
269 }
c906108c 270
0e2de366 271 /* Emit the right opcode... */
c906108c
SS
272 ax_simple (x, ops[op]);
273
274 /* Emit the low SIZE bytes as an unsigned number. We know that
275 sign-extending this will yield l. */
276 append_const (x, l, size / 8);
277
278 /* Now, if it was negative, and not full-sized, sign-extend it. */
279 if (l < 0 && size < 64)
280 ax_ext (x, size);
281}
282
283
284void
fba45db2 285ax_const_d (struct agent_expr *x, LONGEST d)
c906108c
SS
286{
287 /* FIXME: floating-point support not present yet. */
3e43a32a
MS
288 error (_("GDB bug: ax-general.c (ax_const_d): "
289 "floating point not supported yet"));
c906108c
SS
290}
291
292
293/* Assemble code to push the value of register number REG on the
294 stack. */
c5aa993b 295void
fba45db2 296ax_reg (struct agent_expr *x, int reg)
c906108c 297{
175ff332
HZ
298 if (reg >= gdbarch_num_regs (x->gdbarch))
299 {
300 /* This is a pseudo-register. */
301 if (!gdbarch_ax_pseudo_register_push_stack_p (x->gdbarch))
302 error (_("'%s' is a pseudo-register; "
303 "GDB cannot yet trace its contents."),
304 user_reg_map_regnum_to_name (x->gdbarch, reg));
305 if (gdbarch_ax_pseudo_register_push_stack (x->gdbarch, x, reg))
306 error (_("Trace '%s' failed."),
307 user_reg_map_regnum_to_name (x->gdbarch, reg));
308 }
309 else
310 {
1eb7c2d8
AT
311 /* Get the remote register number. */
312 reg = gdbarch_remote_register_number (x->gdbarch, reg);
313
175ff332
HZ
314 /* Make sure the register number is in range. */
315 if (reg < 0 || reg > 0xffff)
3e43a32a
MS
316 error (_("GDB bug: ax-general.c (ax_reg): "
317 "register number out of range"));
175ff332
HZ
318 grow_expr (x, 3);
319 x->buf[x->len] = aop_reg;
320 x->buf[x->len + 1] = (reg >> 8) & 0xff;
321 x->buf[x->len + 2] = (reg) & 0xff;
322 x->len += 3;
323 }
c906108c 324}
f61e138d
SS
325
326/* Assemble code to operate on a trace state variable. */
327
328void
329ax_tsv (struct agent_expr *x, enum agent_op op, int num)
330{
331 /* Make sure the tsv number is in range. */
332 if (num < 0 || num > 0xffff)
3e43a32a
MS
333 internal_error (__FILE__, __LINE__,
334 _("ax-general.c (ax_tsv): variable "
335 "number is %d, out of range"), num);
f61e138d
SS
336
337 grow_expr (x, 3);
338 x->buf[x->len] = op;
339 x->buf[x->len + 1] = (num >> 8) & 0xff;
340 x->buf[x->len + 2] = (num) & 0xff;
341 x->len += 3;
342}
d3ce09f5
SS
343
344/* Append a string to the expression. Note that the string is going
345 into the bytecodes directly, not on the stack. As a precaution,
346 include both length as prefix, and terminate with a NUL. (The NUL
347 is counted in the length.) */
348
349void
741d92cf 350ax_string (struct agent_expr *x, const char *str, int slen)
d3ce09f5
SS
351{
352 int i;
353
354 /* Make sure the string length is reasonable. */
355 if (slen < 0 || slen > 0xffff)
356 internal_error (__FILE__, __LINE__,
357 _("ax-general.c (ax_string): string "
358 "length is %d, out of allowed range"), slen);
359
360 grow_expr (x, 2 + slen + 1);
361 x->buf[x->len++] = ((slen + 1) >> 8) & 0xff;
362 x->buf[x->len++] = (slen + 1) & 0xff;
363 for (i = 0; i < slen; ++i)
364 x->buf[x->len++] = str[i];
365 x->buf[x->len++] = '\0';
366}
c5aa993b 367\f
c906108c
SS
368
369
c906108c
SS
370/* Functions for disassembling agent expressions, and otherwise
371 debugging the expression compiler. */
372
c5aa993b
JM
373struct aop_map aop_map[] =
374{
94d5e490
TT
375 {0, 0, 0, 0, 0}
376#define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
377 , { # NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED }
378#include "ax.def"
379#undef DEFOP
c906108c
SS
380};
381
382
383/* Disassemble the expression EXPR, writing to F. */
384void
fba45db2 385ax_print (struct ui_file *f, struct agent_expr *x)
c906108c
SS
386{
387 int i;
c906108c 388
35c9c7ba
SS
389 fprintf_filtered (f, _("Scope: %s\n"), paddress (x->gdbarch, x->scope));
390 fprintf_filtered (f, _("Reg mask:"));
391 for (i = 0; i < x->reg_mask_len; ++i)
392 fprintf_filtered (f, _(" %02x"), x->reg_mask[i]);
393 fprintf_filtered (f, _("\n"));
394
c906108c
SS
395 /* Check the size of the name array against the number of entries in
396 the enum, to catch additions that people didn't sync. */
397 if ((sizeof (aop_map) / sizeof (aop_map[0]))
398 != aop_last)
3d263c1d 399 error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync"));
c5aa993b
JM
400
401 for (i = 0; i < x->len;)
c906108c 402 {
aead7601 403 enum agent_op op = (enum agent_op) x->buf[i];
c906108c
SS
404
405 if (op >= (sizeof (aop_map) / sizeof (aop_map[0]))
c5aa993b 406 || !aop_map[op].name)
c906108c 407 {
3d263c1d 408 fprintf_filtered (f, _("%3d <bad opcode %02x>\n"), i, op);
c906108c
SS
409 i++;
410 continue;
411 }
a04b0428 412 if (i + 1 + aop_map[op].op_size > x->len)
c906108c 413 {
3d263c1d 414 fprintf_filtered (f, _("%3d <incomplete opcode %s>\n"),
c906108c
SS
415 i, aop_map[op].name);
416 break;
417 }
418
419 fprintf_filtered (f, "%3d %s", i, aop_map[op].name);
a04b0428 420 if (aop_map[op].op_size > 0)
c906108c
SS
421 {
422 fputs_filtered (" ", f);
c5aa993b 423
c906108c 424 print_longest (f, 'd', 0,
a04b0428 425 read_const (x, i + 1, aop_map[op].op_size));
c906108c 426 }
d3ce09f5
SS
427 /* Handle the complicated printf arguments specially. */
428 else if (op == aop_printf)
429 {
430 int slen, nargs;
431
432 i++;
433 nargs = x->buf[i++];
434 slen = x->buf[i++];
435 slen = slen * 256 + x->buf[i++];
436 fprintf_filtered (f, _(" \"%s\", %d args"),
437 &(x->buf[i]), nargs);
438 i += slen - 1;
439 }
c906108c 440 fprintf_filtered (f, "\n");
a04b0428 441 i += 1 + aop_map[op].op_size;
c906108c
SS
442 }
443}
444
35c9c7ba
SS
445/* Add register REG to the register mask for expression AX. */
446void
447ax_reg_mask (struct agent_expr *ax, int reg)
448{
175ff332 449 if (reg >= gdbarch_num_regs (ax->gdbarch))
35c9c7ba 450 {
175ff332
HZ
451 /* This is a pseudo-register. */
452 if (!gdbarch_ax_pseudo_register_collect_p (ax->gdbarch))
453 error (_("'%s' is a pseudo-register; "
454 "GDB cannot yet trace its contents."),
455 user_reg_map_regnum_to_name (ax->gdbarch, reg));
456 if (gdbarch_ax_pseudo_register_collect (ax->gdbarch, ax, reg))
457 error (_("Trace '%s' failed."),
458 user_reg_map_regnum_to_name (ax->gdbarch, reg));
459 }
460 else
461 {
1eb7c2d8
AT
462 int byte;
463
464 /* Get the remote register number. */
465 reg = gdbarch_remote_register_number (ax->gdbarch, reg);
466 byte = reg / 8;
175ff332
HZ
467
468 /* Grow the bit mask if necessary. */
469 if (byte >= ax->reg_mask_len)
470 {
471 /* It's not appropriate to double here. This isn't a
472 string buffer. */
473 int new_len = byte + 1;
224c3ddb
SM
474 unsigned char *new_reg_mask
475 = XRESIZEVEC (unsigned char, ax->reg_mask, new_len);
476
175ff332
HZ
477 memset (new_reg_mask + ax->reg_mask_len, 0,
478 (new_len - ax->reg_mask_len) * sizeof (ax->reg_mask[0]));
479 ax->reg_mask_len = new_len;
480 ax->reg_mask = new_reg_mask;
481 }
482
483 ax->reg_mask[byte] |= 1 << (reg % 8);
35c9c7ba 484 }
35c9c7ba
SS
485}
486
487/* Given an agent expression AX, fill in requirements and other descriptive
488 bits. */
c906108c 489void
35c9c7ba 490ax_reqs (struct agent_expr *ax)
c906108c
SS
491{
492 int i;
493 int height;
494
3d269a59
JB
495 /* Jump target table. targets[i] is non-zero iff we have found a
496 jump to offset i. */
c906108c
SS
497 char *targets = (char *) alloca (ax->len * sizeof (targets[0]));
498
3d269a59
JB
499 /* Instruction boundary table. boundary[i] is non-zero iff our scan
500 has reached an instruction starting at offset i. */
c906108c
SS
501 char *boundary = (char *) alloca (ax->len * sizeof (boundary[0]));
502
3d269a59 503 /* Stack height record. If either targets[i] or boundary[i] is
c906108c
SS
504 non-zero, heights[i] is the height the stack should have before
505 executing the bytecode at that point. */
506 int *heights = (int *) alloca (ax->len * sizeof (heights[0]));
507
508 /* Pointer to a description of the present op. */
509 struct aop_map *op;
510
c906108c
SS
511 memset (targets, 0, ax->len * sizeof (targets[0]));
512 memset (boundary, 0, ax->len * sizeof (boundary[0]));
513
35c9c7ba
SS
514 ax->max_height = ax->min_height = height = 0;
515 ax->flaw = agent_flaw_none;
516 ax->max_data_size = 0;
c906108c 517
a04b0428 518 for (i = 0; i < ax->len; i += 1 + op->op_size)
c906108c
SS
519 {
520 if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0])))
521 {
35c9c7ba 522 ax->flaw = agent_flaw_bad_instruction;
c906108c
SS
523 return;
524 }
525
526 op = &aop_map[ax->buf[i]];
527
c5aa993b 528 if (!op->name)
c906108c 529 {
35c9c7ba 530 ax->flaw = agent_flaw_bad_instruction;
c906108c
SS
531 return;
532 }
c5aa993b 533
a04b0428 534 if (i + 1 + op->op_size > ax->len)
c906108c 535 {
35c9c7ba 536 ax->flaw = agent_flaw_incomplete_instruction;
c906108c
SS
537 return;
538 }
539
3d269a59
JB
540 /* If this instruction is a forward jump target, does the
541 current stack height match the stack height at the jump
542 source? */
c906108c
SS
543 if (targets[i] && (heights[i] != height))
544 {
35c9c7ba 545 ax->flaw = agent_flaw_height_mismatch;
c906108c
SS
546 return;
547 }
548
549 boundary[i] = 1;
550 heights[i] = height;
551
a04b0428 552 height -= op->consumed;
35c9c7ba
SS
553 if (height < ax->min_height)
554 ax->min_height = height;
c906108c 555 height += op->produced;
35c9c7ba
SS
556 if (height > ax->max_height)
557 ax->max_height = height;
c906108c 558
35c9c7ba
SS
559 if (op->data_size > ax->max_data_size)
560 ax->max_data_size = op->data_size;
c906108c
SS
561
562 /* For jump instructions, check that the target is a valid
c5aa993b
JM
563 offset. If it is, record the fact that that location is a
564 jump target, and record the height we expect there. */
c906108c
SS
565 if (aop_goto == op - aop_map
566 || aop_if_goto == op - aop_map)
567 {
568 int target = read_const (ax, i + 1, 2);
569 if (target < 0 || target >= ax->len)
570 {
35c9c7ba 571 ax->flaw = agent_flaw_bad_jump;
c906108c
SS
572 return;
573 }
3d269a59
JB
574
575 /* Do we have any information about what the stack height
576 should be at the target? */
577 if (targets[target] || boundary[target])
c906108c 578 {
3d269a59 579 if (heights[target] != height)
c906108c 580 {
35c9c7ba 581 ax->flaw = agent_flaw_height_mismatch;
c906108c
SS
582 return;
583 }
584 }
3d269a59
JB
585
586 /* Record the target, along with the stack height we expect. */
587 targets[target] = 1;
588 heights[target] = height;
c906108c 589 }
c5aa993b 590
c906108c
SS
591 /* For unconditional jumps with a successor, check that the
592 successor is a target, and pick up its stack height. */
593 if (aop_goto == op - aop_map
594 && i + 3 < ax->len)
595 {
c5aa993b 596 if (!targets[i + 3])
c906108c 597 {
35c9c7ba 598 ax->flaw = agent_flaw_hole;
c906108c
SS
599 return;
600 }
601
602 height = heights[i + 3];
603 }
604
605 /* For reg instructions, record the register in the bit mask. */
606 if (aop_reg == op - aop_map)
607 {
608 int reg = read_const (ax, i + 1, 2);
c906108c 609
35c9c7ba 610 ax_reg_mask (ax, reg);
c906108c
SS
611 }
612 }
613
614 /* Check that all the targets are on boundaries. */
615 for (i = 0; i < ax->len; i++)
616 if (targets[i] && !boundary[i])
617 {
35c9c7ba 618 ax->flaw = agent_flaw_bad_jump;
c906108c
SS
619 return;
620 }
621
35c9c7ba 622 ax->final_height = height;
c906108c 623}
This page took 1.769671 seconds and 4 git commands to generate.