1 /* GDB-specific functions for operating on agent expressions.
2
3 Copyright (C) 1998-2024 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "symtab.h"
21 #include "symfile.h"
22 #include "gdbtypes.h"
23 #include "language.h"
24 #include "value.h"
25 #include "expression.h"
26 #include "command.h"
27 #include "cli/cli-cmds.h"
28 #include "frame.h"
29 #include "target.h"
30 #include "ax.h"
31 #include "ax-gdb.h"
32 #include "block.h"
33 #include "regcache.h"
34 #include "user-regs.h"
35 #include "dictionary.h"
36 #include "breakpoint.h"
37 #include "tracepoint.h"
38 #include "cp-support.h"
39 #include "arch-utils.h"
40 #include "cli/cli-utils.h"
41 #include "linespec.h"
42 #include "location.h"
43 #include "objfiles.h"
44 #include "typeprint.h"
45 #include "valprint.h"
46 #include "c-lang.h"
47 #include "expop.h"
48
49 #include "gdbsupport/format.h"
50
51 /* To make sense of this file, you should read doc/agentexpr.texi.
52 Then look at the types and enums in ax-gdb.h. For the code itself,
53 look at gen_expr, towards the bottom; that's the main function that
54 looks at the GDB expressions and calls everything else to generate
55 code.
56
57 I'm beginning to wonder whether it wouldn't be nicer to internally
58 generate trees, with types, and then spit out the bytecode in
59 linear form afterwards; we could generate fewer `swap', `ext', and
60 `zero_ext' bytecodes that way; it would make good constant folding
61 easier, too. But at the moment, I think we should be willing to
62 pay for the simplicity of this code with less-than-optimal bytecode
63 strings.
64
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
66
67
68
69 /* Prototypes for local functions. */
70
71 /* There's a standard order to the arguments of these functions:
72 struct agent_expr * --- agent expression buffer to generate code into
73 struct axs_value * --- describes value left on top of stack */
74
75 static void gen_traced_pop (struct agent_expr *, struct axs_value *);
76
77 static void gen_sign_extend (struct agent_expr *, struct type *);
78 static void gen_extend (struct agent_expr *, struct type *);
79 static void gen_fetch (struct agent_expr *, struct type *);
80 static void gen_left_shift (struct agent_expr *, int);
81
82
83 static void gen_frame_args_address (struct agent_expr *);
84 static void gen_frame_locals_address (struct agent_expr *);
85 static void gen_offset (struct agent_expr *ax, int offset);
86 static void gen_sym_offset (struct agent_expr *, struct symbol *);
87 static void gen_var_ref (struct agent_expr *ax, struct axs_value *value,
88 struct symbol *var);
89
90
91 static void gen_int_literal (struct agent_expr *ax,
92 struct axs_value *value,
93 LONGEST k, struct type *type);
94
95 static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
96 static int type_wider_than (struct type *type1, struct type *type2);
97 static struct type *max_type (struct type *type1, struct type *type2);
98 static void gen_conversion (struct agent_expr *ax,
99 struct type *from, struct type *to);
100 static int is_nontrivial_conversion (struct type *from, struct type *to);
101 static void gen_usual_arithmetic (struct agent_expr *ax,
102 struct axs_value *value1,
103 struct axs_value *value2);
104 static void gen_integral_promotions (struct agent_expr *ax,
105 struct axs_value *value);
106 static void gen_cast (struct agent_expr *ax,
107 struct axs_value *value, struct type *type);
108 static void gen_scale (struct agent_expr *ax,
109 enum agent_op op, struct type *type);
110 static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
111 struct axs_value *value1, struct axs_value *value2);
112 static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
113 struct axs_value *value1, struct axs_value *value2);
114 static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
115 struct axs_value *value1, struct axs_value *value2,
116 struct type *result_type);
117 static void gen_binop (struct agent_expr *ax,
118 struct axs_value *value,
119 struct axs_value *value1,
120 struct axs_value *value2,
121 enum agent_op op,
122 enum agent_op op_unsigned, int may_carry,
123 const char *name);
124 static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
125 struct type *result_type);
126 static void gen_complement (struct agent_expr *ax, struct axs_value *value);
127 static void gen_deref (struct axs_value *);
128 static void gen_address_of (struct axs_value *);
129 static void gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
130 struct type *type, int start, int end);
131 static void gen_primitive_field (struct agent_expr *ax,
132 struct axs_value *value,
133 int offset, int fieldno, struct type *type);
134 static int gen_struct_ref_recursive (struct agent_expr *ax,
135 struct axs_value *value,
136 const char *field, int offset,
137 struct type *type);
138 static void gen_struct_ref (struct agent_expr *ax,
139 struct axs_value *value,
140 const char *field,
141 const char *operator_name,
142 const char *operand_name);
143 static void gen_static_field (struct agent_expr *ax, struct axs_value *value,
144 struct type *type, int fieldno);
145 static void gen_expr_binop_rest (struct expression *exp,
146 enum exp_opcode op,
147 struct agent_expr *ax,
148 struct axs_value *value,
149 struct axs_value *value1,
150 struct axs_value *value2);
151
152
153
154 /* Generating bytecode from GDB expressions: general assumptions */
155
156 /* Here are a few general assumptions made throughout the code; if you
157 want to make a change that contradicts one of these, then you'd
158 better scan things pretty thoroughly.
159
160 - We assume that all values occupy one stack element. For example,
161 sometimes we'll swap to get at the left argument to a binary
162 operator. If we decide that void values should occupy no stack
163 elements, or that synthetic arrays (whose size is determined at
164 run time, created by the `@' operator) should occupy two stack
165 elements (address and length), then this will cause trouble.
166
167 - We assume the stack elements are infinitely wide, and that we
168 don't have to worry what happens if the user requests an
169 operation that is wider than the actual interpreter's stack.
170 That is, it's up to the interpreter to handle directly all the
171 integer widths the user has access to. (Woe betide the language
172 with bignums!)
173
174 - We don't support side effects. Thus, we don't have to worry about
175 GCC's generalized lvalues, function calls, etc.
176
177 - We don't support floating point. Many places where we switch on
178 some type don't bother to include cases for floating point; there
179 may be even more subtle ways this assumption exists. For
180 example, the arguments to % must be integers.
181
182 - We assume all subexpressions have a static, unchanging type. If
183 we tried to support convenience variables, this would be a
184 problem.
185
186 - All values on the stack should always be fully zero- or
187 sign-extended.
188
189 (I wasn't sure whether to choose this or its opposite --- that
190 only addresses are assumed extended --- but it turns out that
191 neither convention completely eliminates spurious extend
192 operations (if everything is always extended, then you have to
193 extend after add, because it could overflow; if nothing is
194 extended, then you end up producing extends whenever you change
195 sizes), and this is simpler.) */
196
197
198 /* Scan for all static fields in the given class, including any base
199 classes, and generate tracing bytecodes for each. */
200
201 static void
gen_trace_static_fields(struct agent_expr * ax,struct type * type)202 gen_trace_static_fields (struct agent_expr *ax,
203 struct type *type)
204 {
205 int i, nbases = TYPE_N_BASECLASSES (type);
206 struct axs_value value;
207
208 type = check_typedef (type);
209
210 for (i = type->num_fields () - 1; i >= nbases; i--)
211 {
212 if (type->field (i).is_static ())
213 {
214 gen_static_field (ax, &value, type, i);
215 if (value.optimized_out)
216 continue;
217 switch (value.kind)
218 {
219 case axs_lvalue_memory:
220 {
221 /* Initialize the TYPE_LENGTH if it is a typedef. */
222 check_typedef (value.type);
223 ax_const_l (ax, value.type->length ());
224 ax_simple (ax, aop_trace);
225 }
226 break;
227
228 case axs_lvalue_register:
229 /* We don't actually need the register's value to be pushed,
230 just note that we need it to be collected. */
231 ax_reg_mask (ax, value.u.reg);
232
233 default:
234 break;
235 }
236 }
237 }
238
239 /* Now scan through base classes recursively. */
240 for (i = 0; i < nbases; i++)
241 {
242 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
243
244 gen_trace_static_fields (ax, basetype);
245 }
246 }
247
248 /* Trace the lvalue on the stack, if it needs it. In either case, pop
249 the value. Useful on the left side of a comma, and at the end of
250 an expression being used for tracing. */
251 static void
gen_traced_pop(struct agent_expr * ax,struct axs_value * value)252 gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
253 {
254 int string_trace = 0;
255 if (ax->trace_string
256 && value->type->code () == TYPE_CODE_PTR
257 && c_textual_element_type (check_typedef (value->type->target_type ()),
258 's'))
259 string_trace = 1;
260
261 if (ax->tracing)
262 switch (value->kind)
263 {
264 case axs_rvalue:
265 if (string_trace)
266 {
267 ax_const_l (ax, ax->trace_string);
268 ax_simple (ax, aop_tracenz);
269 }
270 else
271 /* We don't trace rvalues, just the lvalues necessary to
272 produce them. So just dispose of this value. */
273 ax_simple (ax, aop_pop);
274 break;
275
276 case axs_lvalue_memory:
277 {
278 /* Initialize the TYPE_LENGTH if it is a typedef. */
279 check_typedef (value->type);
280
281 if (string_trace)
282 {
283 gen_fetch (ax, value->type);
284 ax_const_l (ax, ax->trace_string);
285 ax_simple (ax, aop_tracenz);
286 }
287 else
288 {
289 /* There's no point in trying to use a trace_quick bytecode
290 here, since "trace_quick SIZE pop" is three bytes, whereas
291 "const8 SIZE trace" is also three bytes, does the same
292 thing, and the simplest code which generates that will also
293 work correctly for objects with large sizes. */
294 ax_const_l (ax, value->type->length ());
295 ax_simple (ax, aop_trace);
296 }
297 }
298 break;
299
300 case axs_lvalue_register:
301 /* We don't actually need the register's value to be on the
302 stack, and the target will get heartburn if the register is
303 larger than will fit in a stack, so just mark it for
304 collection and be done with it. */
305 ax_reg_mask (ax, value->u.reg);
306
307 /* But if the register points to a string, assume the value
308 will fit on the stack and push it anyway. */
309 if (string_trace)
310 {
311 ax_reg (ax, value->u.reg);
312 ax_const_l (ax, ax->trace_string);
313 ax_simple (ax, aop_tracenz);
314 }
315 break;
316 }
317 else
318 /* If we're not tracing, just pop the value. */
319 ax_simple (ax, aop_pop);
320
321 /* To trace C++ classes with static fields stored elsewhere. */
322 if (ax->tracing
323 && (value->type->code () == TYPE_CODE_STRUCT
324 || value->type->code () == TYPE_CODE_UNION))
325 gen_trace_static_fields (ax, value->type);
326 }
327
328
329
330 /* Generating bytecode from GDB expressions: helper functions */
331
332 /* Assume that the lower bits of the top of the stack is a value of
333 type TYPE, and the upper bits are zero. Sign-extend if necessary. */
334 static void
gen_sign_extend(struct agent_expr * ax,struct type * type)335 gen_sign_extend (struct agent_expr *ax, struct type *type)
336 {
337 /* Do we need to sign-extend this? */
338 if (!type->is_unsigned ())
339 ax_ext (ax, type->length () * TARGET_CHAR_BIT);
340 }
341
342
343 /* Assume the lower bits of the top of the stack hold a value of type
344 TYPE, and the upper bits are garbage. Sign-extend or truncate as
345 needed. */
346 static void
gen_extend(struct agent_expr * ax,struct type * type)347 gen_extend (struct agent_expr *ax, struct type *type)
348 {
349 int bits = type->length () * TARGET_CHAR_BIT;
350
351 /* I just had to. */
352 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, bits));
353 }
354
355 /* A helper that returns the target type if TYPE is a range type, or
356 otherwise just returns TYPE. */
357
358 static struct type *
strip_range_type(struct type * type)359 strip_range_type (struct type *type)
360 {
361 if (type->code () == TYPE_CODE_RANGE)
362 return type->target_type ();
363 return type;
364 }
365
366 /* Assume that the top of the stack contains a value of type "pointer
367 to TYPE"; generate code to fetch its value. Note that TYPE is the
368 target type, not the pointer type. */
369 static void
gen_fetch(struct agent_expr * ax,struct type * type)370 gen_fetch (struct agent_expr *ax, struct type *type)
371 {
372 if (ax->tracing)
373 {
374 /* Record the area of memory we're about to fetch. */
375 ax_trace_quick (ax, type->length ());
376 }
377
378 type = strip_range_type (type);
379
380 switch (type->code ())
381 {
382 case TYPE_CODE_PTR:
383 case TYPE_CODE_REF:
384 case TYPE_CODE_RVALUE_REF:
385 case TYPE_CODE_ENUM:
386 case TYPE_CODE_INT:
387 case TYPE_CODE_CHAR:
388 case TYPE_CODE_BOOL:
389 /* It's a scalar value, so we know how to dereference it. How
390 many bytes long is it? */
391 switch (type->length ())
392 {
393 case 8 / TARGET_CHAR_BIT:
394 ax_simple (ax, aop_ref8);
395 break;
396 case 16 / TARGET_CHAR_BIT:
397 ax_simple (ax, aop_ref16);
398 break;
399 case 32 / TARGET_CHAR_BIT:
400 ax_simple (ax, aop_ref32);
401 break;
402 case 64 / TARGET_CHAR_BIT:
403 ax_simple (ax, aop_ref64);
404 break;
405
406 /* Either our caller shouldn't have asked us to dereference
407 that pointer (other code's fault), or we're not
408 implementing something we should be (this code's fault).
409 In any case, it's a bug the user shouldn't see. */
410 default:
411 internal_error (_("gen_fetch: strange size"));
412 }
413
414 gen_sign_extend (ax, type);
415 break;
416
417 default:
418 /* Our caller requested us to dereference a pointer from an unsupported
419 type. Error out and give callers a chance to handle the failure
420 gracefully. */
421 error (_("gen_fetch: Unsupported type code `%s'."),
422 type->name ());
423 }
424 }
425
426
427 /* Generate code to left shift the top of the stack by DISTANCE bits, or
428 right shift it by -DISTANCE bits if DISTANCE < 0. This generates
429 unsigned (logical) right shifts. */
430 static void
gen_left_shift(struct agent_expr * ax,int distance)431 gen_left_shift (struct agent_expr *ax, int distance)
432 {
433 if (distance > 0)
434 {
435 ax_const_l (ax, distance);
436 ax_simple (ax, aop_lsh);
437 }
438 else if (distance < 0)
439 {
440 ax_const_l (ax, -distance);
441 ax_simple (ax, aop_rsh_unsigned);
442 }
443 }
444
445
446
447 /* Generating bytecode from GDB expressions: symbol references */
448
449 /* Generate code to push the base address of the argument portion of
450 the top stack frame. */
451 static void
gen_frame_args_address(struct agent_expr * ax)452 gen_frame_args_address (struct agent_expr *ax)
453 {
454 int frame_reg;
455 LONGEST frame_offset;
456
457 gdbarch_virtual_frame_pointer (ax->gdbarch,
458 ax->scope, &frame_reg, &frame_offset);
459 ax_reg (ax, frame_reg);
460 gen_offset (ax, frame_offset);
461 }
462
463
464 /* Generate code to push the base address of the locals portion of the
465 top stack frame. */
466 static void
gen_frame_locals_address(struct agent_expr * ax)467 gen_frame_locals_address (struct agent_expr *ax)
468 {
469 int frame_reg;
470 LONGEST frame_offset;
471
472 gdbarch_virtual_frame_pointer (ax->gdbarch,
473 ax->scope, &frame_reg, &frame_offset);
474 ax_reg (ax, frame_reg);
475 gen_offset (ax, frame_offset);
476 }
477
478
479 /* Generate code to add OFFSET to the top of the stack. Try to
480 generate short and readable code. We use this for getting to
481 variables on the stack, and structure members. If we were
482 programming in ML, it would be clearer why these are the same
483 thing. */
484 static void
gen_offset(struct agent_expr * ax,int offset)485 gen_offset (struct agent_expr *ax, int offset)
486 {
487 /* It would suffice to simply push the offset and add it, but this
488 makes it easier to read positive and negative offsets in the
489 bytecode. */
490 if (offset > 0)
491 {
492 ax_const_l (ax, offset);
493 ax_simple (ax, aop_add);
494 }
495 else if (offset < 0)
496 {
497 ax_const_l (ax, -offset);
498 ax_simple (ax, aop_sub);
499 }
500 }
501
502
503 /* In many cases, a symbol's value is the offset from some other
504 address (stack frame, base register, etc.) Generate code to add
505 VAR's value to the top of the stack. */
506 static void
gen_sym_offset(struct agent_expr * ax,struct symbol * var)507 gen_sym_offset (struct agent_expr *ax, struct symbol *var)
508 {
509 gen_offset (ax, var->value_longest ());
510 }
511
512
513 /* Generate code for a variable reference to AX. The variable is the
514 symbol VAR. Set VALUE to describe the result. */
515
516 static void
gen_var_ref(struct agent_expr * ax,struct axs_value * value,struct symbol * var)517 gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
518 {
519 /* Dereference any typedefs. */
520 value->type = check_typedef (var->type ());
521 value->optimized_out = 0;
522
523 if (const symbol_computed_ops *computed_ops = var->computed_ops ();
524 computed_ops != nullptr)
525 return computed_ops->tracepoint_var_ref (var, ax, value);
526
527 /* I'm imitating the code in read_var_value. */
528 switch (var->aclass ())
529 {
530 case LOC_CONST: /* A constant, like an enum value. */
531 ax_const_l (ax, (LONGEST) var->value_longest ());
532 value->kind = axs_rvalue;
533 break;
534
535 case LOC_LABEL: /* A goto label, being used as a value. */
536 ax_const_l (ax, (LONGEST) var->value_address ());
537 value->kind = axs_rvalue;
538 break;
539
540 case LOC_CONST_BYTES:
541 internal_error (_("gen_var_ref: LOC_CONST_BYTES "
542 "symbols are not supported"));
543
544 /* Variable at a fixed location in memory. Easy. */
545 case LOC_STATIC:
546 /* Push the address of the variable. */
547 ax_const_l (ax, var->value_address ());
548 value->kind = axs_lvalue_memory;
549 break;
550
551 case LOC_ARG: /* var lives in argument area of frame */
552 gen_frame_args_address (ax);
553 gen_sym_offset (ax, var);
554 value->kind = axs_lvalue_memory;
555 break;
556
557 case LOC_REF_ARG: /* As above, but the frame slot really
558 holds the address of the variable. */
559 gen_frame_args_address (ax);
560 gen_sym_offset (ax, var);
561 /* Don't assume any particular pointer size. */
562 gen_fetch (ax, builtin_type (ax->gdbarch)->builtin_data_ptr);
563 value->kind = axs_lvalue_memory;
564 break;
565
566 case LOC_LOCAL: /* var lives in locals area of frame */
567 gen_frame_locals_address (ax);
568 gen_sym_offset (ax, var);
569 value->kind = axs_lvalue_memory;
570 break;
571
572 case LOC_TYPEDEF:
573 error (_("Cannot compute value of typedef `%s'."),
574 var->print_name ());
575 break;
576
577 case LOC_BLOCK:
578 ax_const_l (ax, var->value_block ()->entry_pc ());
579 value->kind = axs_rvalue;
580 break;
581
582 case LOC_REGISTER:
583 /* Don't generate any code at all; in the process of treating
584 this as an lvalue or rvalue, the caller will generate the
585 right code. */
586 value->kind = axs_lvalue_register;
587 value->u.reg = var->register_ops ()->register_number (var, ax->gdbarch);
588 break;
589
590 /* A lot like LOC_REF_ARG, but the pointer lives directly in a
591 register, not on the stack. Simpler than LOC_REGISTER
592 because it's just like any other case where the thing
593 has a real address. */
594 case LOC_REGPARM_ADDR:
595 ax_reg (ax, var->register_ops ()->register_number (var, ax->gdbarch));
596 value->kind = axs_lvalue_memory;
597 break;
598
599 case LOC_UNRESOLVED:
600 {
601 struct bound_minimal_symbol msym
602 = lookup_minimal_symbol (var->linkage_name (), NULL, NULL);
603
604 if (!msym.minsym)
605 error (_("Couldn't resolve symbol `%s'."), var->print_name ());
606
607 /* Push the address of the variable. */
608 ax_const_l (ax, msym.value_address ());
609 value->kind = axs_lvalue_memory;
610 }
611 break;
612
613 case LOC_COMPUTED:
614 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
615
616 case LOC_OPTIMIZED_OUT:
617 /* Flag this, but don't say anything; leave it up to callers to
618 warn the user. */
619 value->optimized_out = 1;
620 break;
621
622 default:
623 error (_("Cannot find value of botched symbol `%s'."),
624 var->print_name ());
625 break;
626 }
627 }
628
629 /* Generate code for a minimal symbol variable reference to AX. The
630 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
631 the result. */
632
633 static void
gen_msym_var_ref(agent_expr * ax,axs_value * value,minimal_symbol * msymbol,objfile * objf)634 gen_msym_var_ref (agent_expr *ax, axs_value *value,
635 minimal_symbol *msymbol, objfile *objf)
636 {
637 CORE_ADDR address;
638 type *t = find_minsym_type_and_address (msymbol, objf, &address);
639 value->type = t;
640 value->optimized_out = false;
641 ax_const_l (ax, address);
642 value->kind = axs_lvalue_memory;
643 }
644
645
646
647
648 /* Generating bytecode from GDB expressions: literals */
649
650 static void
gen_int_literal(struct agent_expr * ax,struct axs_value * value,LONGEST k,struct type * type)651 gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
652 struct type *type)
653 {
654 ax_const_l (ax, k);
655 value->kind = axs_rvalue;
656 value->type = check_typedef (type);
657 }
658
659
660
661 /* Generating bytecode from GDB expressions: unary conversions, casts */
662
663 /* Take what's on the top of the stack (as described by VALUE), and
664 try to make an rvalue out of it. Signal an error if we can't do
665 that. */
666 void
require_rvalue(struct agent_expr * ax,struct axs_value * value)667 require_rvalue (struct agent_expr *ax, struct axs_value *value)
668 {
669 /* Only deal with scalars, structs and such may be too large
670 to fit in a stack entry. */
671 value->type = check_typedef (value->type);
672 if (value->type->code () == TYPE_CODE_ARRAY
673 || value->type->code () == TYPE_CODE_STRUCT
674 || value->type->code () == TYPE_CODE_UNION
675 || value->type->code () == TYPE_CODE_FUNC)
676 error (_("Value not scalar: cannot be an rvalue."));
677
678 switch (value->kind)
679 {
680 case axs_rvalue:
681 /* It's already an rvalue. */
682 break;
683
684 case axs_lvalue_memory:
685 /* The top of stack is the address of the object. Dereference. */
686 gen_fetch (ax, value->type);
687 break;
688
689 case axs_lvalue_register:
690 /* There's nothing on the stack, but value->u.reg is the
691 register number containing the value.
692
693 When we add floating-point support, this is going to have to
694 change. What about SPARC register pairs, for example? */
695 ax_reg (ax, value->u.reg);
696 gen_extend (ax, value->type);
697 break;
698 }
699
700 value->kind = axs_rvalue;
701 }
702
703
704 /* Assume the top of the stack is described by VALUE, and perform the
705 usual unary conversions. This is motivated by ANSI 6.2.2, but of
706 course GDB expressions are not ANSI; they're the mishmash union of
707 a bunch of languages. Rah.
708
709 NOTE! This function promises to produce an rvalue only when the
710 incoming value is of an appropriate type. In other words, the
711 consumer of the value this function produces may assume the value
712 is an rvalue only after checking its type.
713
714 The immediate issue is that if the user tries to use a structure or
715 union as an operand of, say, the `+' operator, we don't want to try
716 to convert that structure to an rvalue; require_rvalue will bomb on
717 structs and unions. Rather, we want to simply pass the struct
718 lvalue through unchanged, and let `+' raise an error. */
719
720 static void
gen_usual_unary(struct agent_expr * ax,struct axs_value * value)721 gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
722 {
723 /* We don't have to generate any code for the usual integral
724 conversions, since values are always represented as full-width on
725 the stack. Should we tweak the type? */
726
727 /* Some types require special handling. */
728 switch (value->type->code ())
729 {
730 /* Functions get converted to a pointer to the function. */
731 case TYPE_CODE_FUNC:
732 value->type = lookup_pointer_type (value->type);
733 value->kind = axs_rvalue; /* Should always be true, but just in case. */
734 break;
735
736 /* Arrays get converted to a pointer to their first element, and
737 are no longer an lvalue. */
738 case TYPE_CODE_ARRAY:
739 {
740 struct type *elements = value->type->target_type ();
741
742 value->type = lookup_pointer_type (elements);
743 value->kind = axs_rvalue;
744 /* We don't need to generate any code; the address of the array
745 is also the address of its first element. */
746 }
747 break;
748
749 /* Don't try to convert structures and unions to rvalues. Let the
750 consumer signal an error. */
751 case TYPE_CODE_STRUCT:
752 case TYPE_CODE_UNION:
753 return;
754 }
755
756 /* If the value is an lvalue, dereference it. */
757 require_rvalue (ax, value);
758 }
759
760
761 /* Return non-zero iff the type TYPE1 is considered "wider" than the
762 type TYPE2, according to the rules described in gen_usual_arithmetic. */
763 static int
type_wider_than(struct type * type1,struct type * type2)764 type_wider_than (struct type *type1, struct type *type2)
765 {
766 return (type1->length () > type2->length ()
767 || (type1->length () == type2->length ()
768 && type1->is_unsigned ()
769 && !type2->is_unsigned ()));
770 }
771
772
773 /* Return the "wider" of the two types TYPE1 and TYPE2. */
774 static struct type *
max_type(struct type * type1,struct type * type2)775 max_type (struct type *type1, struct type *type2)
776 {
777 return type_wider_than (type1, type2) ? type1 : type2;
778 }
779
780
781 /* Generate code to convert a scalar value of type FROM to type TO. */
782 static void
gen_conversion(struct agent_expr * ax,struct type * from,struct type * to)783 gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
784 {
785 /* Perhaps there is a more graceful way to state these rules. */
786
787 /* If we're converting to a narrower type, then we need to clear out
788 the upper bits. */
789 if (to->length () < from->length ())
790 gen_extend (ax, to);
791
792 /* If the two values have equal width, but different signednesses,
793 then we need to extend. */
794 else if (to->length () == from->length ())
795 {
796 if (from->is_unsigned () != to->is_unsigned ())
797 gen_extend (ax, to);
798 }
799
800 /* If we're converting to a wider type, and becoming unsigned, then
801 we need to zero out any possible sign bits. */
802 else if (to->length () > from->length ())
803 {
804 if (to->is_unsigned ())
805 gen_extend (ax, to);
806 }
807 }
808
809
810 /* Return non-zero iff the type FROM will require any bytecodes to be
811 emitted to be converted to the type TO. */
812 static int
is_nontrivial_conversion(struct type * from,struct type * to)813 is_nontrivial_conversion (struct type *from, struct type *to)
814 {
815 agent_expr_up ax (new agent_expr (NULL, 0));
816
817 /* Actually generate the code, and see if anything came out. At the
818 moment, it would be trivial to replicate the code in
819 gen_conversion here, but in the future, when we're supporting
820 floating point and the like, it may not be. Doing things this
821 way allows this function to be independent of the logic in
822 gen_conversion. */
823 gen_conversion (ax.get (), from, to);
824 return !ax->buf.empty ();
825 }
826
827
828 /* Generate code to perform the "usual arithmetic conversions" (ANSI C
829 6.2.1.5) for the two operands of an arithmetic operator. This
830 effectively finds a "least upper bound" type for the two arguments,
831 and promotes each argument to that type. *VALUE1 and *VALUE2
832 describe the values as they are passed in, and as they are left. */
833 static void
gen_usual_arithmetic(struct agent_expr * ax,struct axs_value * value1,struct axs_value * value2)834 gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
835 struct axs_value *value2)
836 {
837 struct type *type1 = strip_range_type (value1->type);
838 struct type *type2 = strip_range_type (value2->type);
839
840 /* Do the usual binary conversions. */
841 if (type1->code () == TYPE_CODE_INT
842 && type2->code () == TYPE_CODE_INT)
843 {
844 /* The ANSI integral promotions seem to work this way: Order the
845 integer types by size, and then by signedness: an n-bit
846 unsigned type is considered "wider" than an n-bit signed
847 type. Promote to the "wider" of the two types, and always
848 promote at least to int. */
849 struct type *target = max_type (builtin_type (ax->gdbarch)->builtin_int,
850 max_type (type1, type2));
851
852 /* Deal with value2, on the top of the stack. */
853 gen_conversion (ax, type2, target);
854
855 /* Deal with value1, not on the top of the stack. Don't
856 generate the `swap' instructions if we're not actually going
857 to do anything. */
858 if (is_nontrivial_conversion (type1, target))
859 {
860 ax_simple (ax, aop_swap);
861 gen_conversion (ax, type1, target);
862 ax_simple (ax, aop_swap);
863 }
864
865 value1->type = value2->type = check_typedef (target);
866 }
867 }
868
869
870 /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
871 the value on the top of the stack, as described by VALUE. Assume
872 the value has integral type. */
873 static void
gen_integral_promotions(struct agent_expr * ax,struct axs_value * value)874 gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
875 {
876 const struct builtin_type *builtin = builtin_type (ax->gdbarch);
877
878 if (!type_wider_than (value->type, builtin->builtin_int))
879 {
880 gen_conversion (ax, value->type, builtin->builtin_int);
881 value->type = builtin->builtin_int;
882 }
883 else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
884 {
885 gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
886 value->type = builtin->builtin_unsigned_int;
887 }
888 }
889
890
891 /* Generate code for a cast to TYPE. */
892 static void
gen_cast(struct agent_expr * ax,struct axs_value * value,struct type * type)893 gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
894 {
895 /* GCC does allow casts to yield lvalues, so this should be fixed
896 before merging these changes into the trunk. */
897 require_rvalue (ax, value);
898 /* Dereference typedefs. */
899 type = check_typedef (type);
900 type = strip_range_type (type);
901
902 switch (type->code ())
903 {
904 case TYPE_CODE_PTR:
905 case TYPE_CODE_REF:
906 case TYPE_CODE_RVALUE_REF:
907 /* It's implementation-defined, and I'll bet this is what GCC
908 does. */
909 break;
910
911 case TYPE_CODE_ARRAY:
912 case TYPE_CODE_STRUCT:
913 case TYPE_CODE_UNION:
914 case TYPE_CODE_FUNC:
915 error (_("Invalid type cast: intended type must be scalar."));
916
917 case TYPE_CODE_ENUM:
918 case TYPE_CODE_BOOL:
919 /* We don't have to worry about the size of the value, because
920 all our integral values are fully sign-extended, and when
921 casting pointers we can do anything we like. Is there any
922 way for us to know what GCC actually does with a cast like
923 this? */
924 break;
925
926 case TYPE_CODE_INT:
927 gen_conversion (ax, value->type, type);
928 break;
929
930 case TYPE_CODE_VOID:
931 /* We could pop the value, and rely on everyone else to check
932 the type and notice that this value doesn't occupy a stack
933 slot. But for now, leave the value on the stack, and
934 preserve the "value == stack element" assumption. */
935 break;
936
937 default:
938 error (_("Casts to requested type are not yet implemented."));
939 }
940
941 value->type = type;
942 }
943
944
945
946 /* Generating bytecode from GDB expressions: arithmetic */
947
948 /* Scale the integer on the top of the stack by the size of the target
949 of the pointer type TYPE. */
950 static void
gen_scale(struct agent_expr * ax,enum agent_op op,struct type * type)951 gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
952 {
953 struct type *element = type->target_type ();
954
955 if (element->length () != 1)
956 {
957 ax_const_l (ax, element->length ());
958 ax_simple (ax, op);
959 }
960 }
961
962
963 /* Generate code for pointer arithmetic PTR + INT. */
964 static void
gen_ptradd(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2)965 gen_ptradd (struct agent_expr *ax, struct axs_value *value,
966 struct axs_value *value1, struct axs_value *value2)
967 {
968 gdb_assert (value1->type->is_pointer_or_reference ());
969 gdb_assert (strip_range_type (value2->type)->code () == TYPE_CODE_INT);
970
971 gen_scale (ax, aop_mul, value1->type);
972 ax_simple (ax, aop_add);
973 gen_extend (ax, value1->type); /* Catch overflow. */
974 value->type = value1->type;
975 value->kind = axs_rvalue;
976 }
977
978
979 /* Generate code for pointer arithmetic PTR - INT. */
980 static void
gen_ptrsub(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2)981 gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
982 struct axs_value *value1, struct axs_value *value2)
983 {
984 gdb_assert (value1->type->is_pointer_or_reference ());
985 gdb_assert (strip_range_type (value2->type)->code () == TYPE_CODE_INT);
986
987 gen_scale (ax, aop_mul, value1->type);
988 ax_simple (ax, aop_sub);
989 gen_extend (ax, value1->type); /* Catch overflow. */
990 value->type = value1->type;
991 value->kind = axs_rvalue;
992 }
993
994
995 /* Generate code for pointer arithmetic PTR - PTR. */
996 static void
gen_ptrdiff(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,struct type * result_type)997 gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
998 struct axs_value *value1, struct axs_value *value2,
999 struct type *result_type)
1000 {
1001 gdb_assert (value1->type->is_pointer_or_reference ());
1002 gdb_assert (value2->type->is_pointer_or_reference ());
1003
1004 if (value1->type->target_type ()->length ()
1005 != value2->type->target_type ()->length ())
1006 error (_("\
1007 First argument of `-' is a pointer, but second argument is neither\n\
1008 an integer nor a pointer of the same type."));
1009
1010 ax_simple (ax, aop_sub);
1011 gen_scale (ax, aop_div_unsigned, value1->type);
1012 value->type = result_type;
1013 value->kind = axs_rvalue;
1014 }
1015
1016 static void
gen_equal(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,struct type * result_type)1017 gen_equal (struct agent_expr *ax, struct axs_value *value,
1018 struct axs_value *value1, struct axs_value *value2,
1019 struct type *result_type)
1020 {
1021 if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
1022 ax_simple (ax, aop_equal);
1023 else
1024 gen_binop (ax, value, value1, value2,
1025 aop_equal, aop_equal, 0, "equal");
1026 value->type = result_type;
1027 value->kind = axs_rvalue;
1028 }
1029
1030 static void
gen_less(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,struct type * result_type)1031 gen_less (struct agent_expr *ax, struct axs_value *value,
1032 struct axs_value *value1, struct axs_value *value2,
1033 struct type *result_type)
1034 {
1035 if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
1036 ax_simple (ax, aop_less_unsigned);
1037 else
1038 gen_binop (ax, value, value1, value2,
1039 aop_less_signed, aop_less_unsigned, 0, "less than");
1040 value->type = result_type;
1041 value->kind = axs_rvalue;
1042 }
1043
1044 /* Generate code for a binary operator that doesn't do pointer magic.
1045 We set VALUE to describe the result value; we assume VALUE1 and
1046 VALUE2 describe the two operands, and that they've undergone the
1047 usual binary conversions. MAY_CARRY should be non-zero iff the
1048 result needs to be extended. NAME is the English name of the
1049 operator, used in error messages */
1050 static void
gen_binop(struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2,enum agent_op op,enum agent_op op_unsigned,int may_carry,const char * name)1051 gen_binop (struct agent_expr *ax, struct axs_value *value,
1052 struct axs_value *value1, struct axs_value *value2,
1053 enum agent_op op, enum agent_op op_unsigned,
1054 int may_carry, const char *name)
1055 {
1056 /* We only handle INT op INT. */
1057 struct type *type1 = strip_range_type (value1->type);
1058 if ((type1->code () != TYPE_CODE_INT)
1059 || (strip_range_type (value2->type)->code () != TYPE_CODE_INT))
1060 error (_("Invalid combination of types in %s."), name);
1061
1062 ax_simple (ax, type1->is_unsigned () ? op_unsigned : op);
1063 if (may_carry)
1064 gen_extend (ax, type1); /* catch overflow */
1065 value->type = type1;
1066 value->kind = axs_rvalue;
1067 }
1068
1069
1070 static void
gen_logical_not(struct agent_expr * ax,struct axs_value * value,struct type * result_type)1071 gen_logical_not (struct agent_expr *ax, struct axs_value *value,
1072 struct type *result_type)
1073 {
1074 struct type *type = strip_range_type (value->type);
1075 if (type->code () != TYPE_CODE_INT
1076 && type->code () != TYPE_CODE_PTR)
1077 error (_("Invalid type of operand to `!'."));
1078
1079 ax_simple (ax, aop_log_not);
1080 value->type = result_type;
1081 }
1082
1083
1084 static void
gen_complement(struct agent_expr * ax,struct axs_value * value)1085 gen_complement (struct agent_expr *ax, struct axs_value *value)
1086 {
1087 struct type *type = strip_range_type (value->type);
1088 if (type->code () != TYPE_CODE_INT)
1089 error (_("Invalid type of operand to `~'."));
1090
1091 ax_simple (ax, aop_bit_not);
1092 gen_extend (ax, type);
1093 }
1094
1095
1096
1097 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1098
1099 /* Dereference the value on the top of the stack. */
1100 static void
gen_deref(struct axs_value * value)1101 gen_deref (struct axs_value *value)
1102 {
1103 /* The caller should check the type, because several operators use
1104 this, and we don't know what error message to generate. */
1105 if (!value->type->is_pointer_or_reference ())
1106 internal_error (_("gen_deref: expected a pointer"));
1107
1108 /* We've got an rvalue now, which is a pointer. We want to yield an
1109 lvalue, whose address is exactly that pointer. So we don't
1110 actually emit any code; we just change the type from "Pointer to
1111 T" to "T", and mark the value as an lvalue in memory. Leave it
1112 to the consumer to actually dereference it. */
1113 value->type = check_typedef (value->type->target_type ());
1114 if (value->type->code () == TYPE_CODE_VOID)
1115 error (_("Attempt to dereference a generic pointer."));
1116 value->kind = ((value->type->code () == TYPE_CODE_FUNC)
1117 ? axs_rvalue : axs_lvalue_memory);
1118 }
1119
1120
1121 /* Produce the address of the lvalue on the top of the stack. */
1122 static void
gen_address_of(struct axs_value * value)1123 gen_address_of (struct axs_value *value)
1124 {
1125 /* Special case for taking the address of a function. The ANSI
1126 standard describes this as a special case, too, so this
1127 arrangement is not without motivation. */
1128 if (value->type->code () == TYPE_CODE_FUNC)
1129 /* The value's already an rvalue on the stack, so we just need to
1130 change the type. */
1131 value->type = lookup_pointer_type (value->type);
1132 else
1133 switch (value->kind)
1134 {
1135 case axs_rvalue:
1136 error (_("Operand of `&' is an rvalue, which has no address."));
1137
1138 case axs_lvalue_register:
1139 error (_("Operand of `&' is in a register, and has no address."));
1140
1141 case axs_lvalue_memory:
1142 value->kind = axs_rvalue;
1143 value->type = lookup_pointer_type (value->type);
1144 break;
1145 }
1146 }
1147
1148 /* Generate code to push the value of a bitfield of a structure whose
1149 address is on the top of the stack. START and END give the
1150 starting and one-past-ending *bit* numbers of the field within the
1151 structure. */
1152 static void
gen_bitfield_ref(struct agent_expr * ax,struct axs_value * value,struct type * type,int start,int end)1153 gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
1154 struct type *type, int start, int end)
1155 {
1156 /* Note that ops[i] fetches 8 << i bits. */
1157 static enum agent_op ops[]
1158 = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
1159 static int num_ops = (sizeof (ops) / sizeof (ops[0]));
1160
1161 /* We don't want to touch any byte that the bitfield doesn't
1162 actually occupy; we shouldn't make any accesses we're not
1163 explicitly permitted to. We rely here on the fact that the
1164 bytecode `ref' operators work on unaligned addresses.
1165
1166 It takes some fancy footwork to get the stack to work the way
1167 we'd like. Say we're retrieving a bitfield that requires three
1168 fetches. Initially, the stack just contains the address:
1169 addr
1170 For the first fetch, we duplicate the address
1171 addr addr
1172 then add the byte offset, do the fetch, and shift and mask as
1173 needed, yielding a fragment of the value, properly aligned for
1174 the final bitwise or:
1175 addr frag1
1176 then we swap, and repeat the process:
1177 frag1 addr --- address on top
1178 frag1 addr addr --- duplicate it
1179 frag1 addr frag2 --- get second fragment
1180 frag1 frag2 addr --- swap again
1181 frag1 frag2 frag3 --- get third fragment
1182 Notice that, since the third fragment is the last one, we don't
1183 bother duplicating the address this time. Now we have all the
1184 fragments on the stack, and we can simply `or' them together,
1185 yielding the final value of the bitfield. */
1186
1187 /* The first and one-after-last bits in the field, but rounded down
1188 and up to byte boundaries. */
1189 int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
1190 int bound_end = (((end + TARGET_CHAR_BIT - 1)
1191 / TARGET_CHAR_BIT)
1192 * TARGET_CHAR_BIT);
1193
1194 /* current bit offset within the structure */
1195 int offset;
1196
1197 /* The index in ops of the opcode we're considering. */
1198 int op;
1199
1200 /* The number of fragments we generated in the process. Probably
1201 equal to the number of `one' bits in bytesize, but who cares? */
1202 int fragment_count;
1203
1204 /* Dereference any typedefs. */
1205 type = check_typedef (type);
1206
1207 /* Can we fetch the number of bits requested at all? */
1208 if ((end - start) > ((1 << num_ops) * 8))
1209 internal_error (_("gen_bitfield_ref: bitfield too wide"));
1210
1211 /* Note that we know here that we only need to try each opcode once.
1212 That may not be true on machines with weird byte sizes. */
1213 offset = bound_start;
1214 fragment_count = 0;
1215 for (op = num_ops - 1; op >= 0; op--)
1216 {
1217 /* number of bits that ops[op] would fetch */
1218 int op_size = 8 << op;
1219
1220 /* The stack at this point, from bottom to top, contains zero or
1221 more fragments, then the address. */
1222
1223 /* Does this fetch fit within the bitfield? */
1224 if (offset + op_size <= bound_end)
1225 {
1226 /* Is this the last fragment? */
1227 int last_frag = (offset + op_size == bound_end);
1228
1229 if (!last_frag)
1230 ax_simple (ax, aop_dup); /* keep a copy of the address */
1231
1232 /* Add the offset. */
1233 gen_offset (ax, offset / TARGET_CHAR_BIT);
1234
1235 if (ax->tracing)
1236 {
1237 /* Record the area of memory we're about to fetch. */
1238 ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
1239 }
1240
1241 /* Perform the fetch. */
1242 ax_simple (ax, ops[op]);
1243
1244 /* Shift the bits we have to their proper position.
1245 gen_left_shift will generate right shifts when the operand
1246 is negative.
1247
1248 A big-endian field diagram to ponder:
1249 byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
1250 +------++------++------++------++------++------++------++------+
1251 xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
1252 ^ ^ ^ ^
1253 bit number 16 32 48 53
1254 These are bit numbers as supplied by GDB. Note that the
1255 bit numbers run from right to left once you've fetched the
1256 value!
1257
1258 A little-endian field diagram to ponder:
1259 byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
1260 +------++------++------++------++------++------++------++------+
1261 xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
1262 ^ ^ ^ ^ ^
1263 bit number 48 32 16 4 0
1264
1265 In both cases, the most significant end is on the left
1266 (i.e. normal numeric writing order), which means that you
1267 don't go crazy thinking about `left' and `right' shifts.
1268
1269 We don't have to worry about masking yet:
1270 - If they contain garbage off the least significant end, then we
1271 must be looking at the low end of the field, and the right
1272 shift will wipe them out.
1273 - If they contain garbage off the most significant end, then we
1274 must be looking at the most significant end of the word, and
1275 the sign/zero extension will wipe them out.
1276 - If we're in the interior of the word, then there is no garbage
1277 on either end, because the ref operators zero-extend. */
1278 if (gdbarch_byte_order (ax->gdbarch) == BFD_ENDIAN_BIG)
1279 gen_left_shift (ax, end - (offset + op_size));
1280 else
1281 gen_left_shift (ax, offset - start);
1282
1283 if (!last_frag)
1284 /* Bring the copy of the address up to the top. */
1285 ax_simple (ax, aop_swap);
1286
1287 offset += op_size;
1288 fragment_count++;
1289 }
1290 }
1291
1292 /* Generate enough bitwise `or' operations to combine all the
1293 fragments we left on the stack. */
1294 while (fragment_count-- > 1)
1295 ax_simple (ax, aop_bit_or);
1296
1297 /* Sign- or zero-extend the value as appropriate. */
1298 ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, end - start));
1299
1300 /* This is *not* an lvalue. Ugh. */
1301 value->kind = axs_rvalue;
1302 value->type = type;
1303 }
1304
1305 /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
1306 is an accumulated offset (in bytes), will be nonzero for objects
1307 embedded in other objects, like C++ base classes. Behavior should
1308 generally follow value_primitive_field. */
1309
1310 static void
gen_primitive_field(struct agent_expr * ax,struct axs_value * value,int offset,int fieldno,struct type * type)1311 gen_primitive_field (struct agent_expr *ax, struct axs_value *value,
1312 int offset, int fieldno, struct type *type)
1313 {
1314 /* Is this a bitfield? */
1315 if (type->field (fieldno).is_packed ())
1316 gen_bitfield_ref (ax, value, type->field (fieldno).type (),
1317 (offset * TARGET_CHAR_BIT
1318 + type->field (fieldno).loc_bitpos ()),
1319 (offset * TARGET_CHAR_BIT
1320 + type->field (fieldno).loc_bitpos ()
1321 + type->field (fieldno).bitsize ()));
1322 else
1323 {
1324 gen_offset (ax, offset
1325 + type->field (fieldno).loc_bitpos () / TARGET_CHAR_BIT);
1326 value->kind = axs_lvalue_memory;
1327 value->type = type->field (fieldno).type ();
1328 }
1329 }
1330
1331 /* Search for the given field in either the given type or one of its
1332 base classes. Return 1 if found, 0 if not. */
1333
1334 static int
gen_struct_ref_recursive(struct agent_expr * ax,struct axs_value * value,const char * field,int offset,struct type * type)1335 gen_struct_ref_recursive (struct agent_expr *ax, struct axs_value *value,
1336 const char *field, int offset, struct type *type)
1337 {
1338 int i, rslt;
1339 int nbases = TYPE_N_BASECLASSES (type);
1340
1341 type = check_typedef (type);
1342
1343 for (i = type->num_fields () - 1; i >= nbases; i--)
1344 {
1345 const char *this_name = type->field (i).name ();
1346
1347 if (this_name)
1348 {
1349 if (strcmp (field, this_name) == 0)
1350 {
1351 /* Note that bytecodes for the struct's base (aka
1352 "this") will have been generated already, which will
1353 be unnecessary but not harmful if the static field is
1354 being handled as a global. */
1355 if (type->field (i).is_static ())
1356 {
1357 gen_static_field (ax, value, type, i);
1358 if (value->optimized_out)
1359 error (_("static field `%s' has been "
1360 "optimized out, cannot use"),
1361 field);
1362 return 1;
1363 }
1364
1365 gen_primitive_field (ax, value, offset, i, type);
1366 return 1;
1367 }
1368 #if 0 /* is this right? */
1369 if (this_name[0] == '\0')
1370 internal_error (_("find_field: anonymous unions not supported"));
1371 #endif
1372 }
1373 }
1374
1375 /* Now scan through base classes recursively. */
1376 for (i = 0; i < nbases; i++)
1377 {
1378 struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1379
1380 rslt = gen_struct_ref_recursive (ax, value, field,
1381 offset + TYPE_BASECLASS_BITPOS (type, i)
1382 / TARGET_CHAR_BIT,
1383 basetype);
1384 if (rslt)
1385 return 1;
1386 }
1387
1388 /* Not found anywhere, flag so caller can complain. */
1389 return 0;
1390 }
1391
1392 /* Generate code to reference the member named FIELD of a structure or
1393 union. The top of the stack, as described by VALUE, should have
1394 type (pointer to a)* struct/union. OPERATOR_NAME is the name of
1395 the operator being compiled, and OPERAND_NAME is the kind of thing
1396 it operates on; we use them in error messages. */
1397 static void
gen_struct_ref(struct agent_expr * ax,struct axs_value * value,const char * field,const char * operator_name,const char * operand_name)1398 gen_struct_ref (struct agent_expr *ax, struct axs_value *value,
1399 const char *field, const char *operator_name,
1400 const char *operand_name)
1401 {
1402 struct type *type;
1403 int found;
1404
1405 /* Follow pointers until we reach a non-pointer. These aren't the C
1406 semantics, but they're what the normal GDB evaluator does, so we
1407 should at least be consistent. */
1408 while (value->type->is_pointer_or_reference ())
1409 {
1410 require_rvalue (ax, value);
1411 gen_deref (value);
1412 }
1413 type = check_typedef (value->type);
1414
1415 /* This must yield a structure or a union. */
1416 if (type->code () != TYPE_CODE_STRUCT
1417 && type->code () != TYPE_CODE_UNION)
1418 error (_("The left operand of `%s' is not a %s."),
1419 operator_name, operand_name);
1420
1421 /* And it must be in memory; we don't deal with structure rvalues,
1422 or structures living in registers. */
1423 if (value->kind != axs_lvalue_memory)
1424 error (_("Structure does not live in memory."));
1425
1426 /* Search through fields and base classes recursively. */
1427 found = gen_struct_ref_recursive (ax, value, field, 0, type);
1428
1429 if (!found)
1430 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1431 field, type->name ());
1432 }
1433
1434 static int
1435 gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1436 const struct type *curtype, const char *name);
1437 static int
1438 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1439 const struct type *curtype, const char *name);
1440
1441 static void
gen_static_field(struct agent_expr * ax,struct axs_value * value,struct type * type,int fieldno)1442 gen_static_field (struct agent_expr *ax, struct axs_value *value,
1443 struct type *type, int fieldno)
1444 {
1445 if (type->field (fieldno).loc_kind () == FIELD_LOC_KIND_PHYSADDR)
1446 {
1447 ax_const_l (ax, type->field (fieldno).loc_physaddr ());
1448 value->kind = axs_lvalue_memory;
1449 value->type = type->field (fieldno).type ();
1450 value->optimized_out = 0;
1451 }
1452 else
1453 {
1454 const char *phys_name = type->field (fieldno).loc_physname ();
1455 struct symbol *sym = lookup_symbol (phys_name, 0,
1456 SEARCH_VAR_DOMAIN, 0).symbol;
1457
1458 if (sym)
1459 {
1460 gen_var_ref (ax, value, sym);
1461
1462 /* Don't error if the value was optimized out, we may be
1463 scanning all static fields and just want to pass over this
1464 and continue with the rest. */
1465 }
1466 else
1467 {
1468 /* Silently assume this was optimized out; class printing
1469 will let the user know why the data is missing. */
1470 value->optimized_out = 1;
1471 }
1472 }
1473 }
1474
1475 static int
gen_struct_elt_for_reference(struct agent_expr * ax,struct axs_value * value,struct type * type,const char * fieldname)1476 gen_struct_elt_for_reference (struct agent_expr *ax, struct axs_value *value,
1477 struct type *type, const char *fieldname)
1478 {
1479 struct type *t = type;
1480 int i;
1481
1482 if (t->code () != TYPE_CODE_STRUCT
1483 && t->code () != TYPE_CODE_UNION)
1484 internal_error (_("non-aggregate type to gen_struct_elt_for_reference"));
1485
1486 for (i = t->num_fields () - 1; i >= TYPE_N_BASECLASSES (t); i--)
1487 {
1488 const char *t_field_name = t->field (i).name ();
1489
1490 if (t_field_name && strcmp (t_field_name, fieldname) == 0)
1491 {
1492 if (t->field (i).is_static ())
1493 {
1494 gen_static_field (ax, value, t, i);
1495 if (value->optimized_out)
1496 error (_("static field `%s' has been "
1497 "optimized out, cannot use"),
1498 fieldname);
1499 return 1;
1500 }
1501 if (t->field (i).is_packed ())
1502 error (_("pointers to bitfield members not allowed"));
1503
1504 /* FIXME we need a way to do "want_address" equivalent */
1505
1506 error (_("Cannot reference non-static field \"%s\""), fieldname);
1507 }
1508 }
1509
1510 /* FIXME add other scoped-reference cases here */
1511
1512 /* Do a last-ditch lookup. */
1513 return gen_maybe_namespace_elt (ax, value, type, fieldname);
1514 }
1515
1516 /* C++: Return the member NAME of the namespace given by the type
1517 CURTYPE. */
1518
1519 static int
gen_namespace_elt(struct agent_expr * ax,struct axs_value * value,const struct type * curtype,const char * name)1520 gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1521 const struct type *curtype, const char *name)
1522 {
1523 int found = gen_maybe_namespace_elt (ax, value, curtype, name);
1524
1525 if (!found)
1526 error (_("No symbol \"%s\" in namespace \"%s\"."),
1527 name, curtype->name ());
1528
1529 return found;
1530 }
1531
1532 /* A helper function used by value_namespace_elt and
1533 value_struct_elt_for_reference. It looks up NAME inside the
1534 context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
1535 is a class and NAME refers to a type in CURTYPE itself (as opposed
1536 to, say, some base class of CURTYPE). */
1537
1538 static int
gen_maybe_namespace_elt(struct agent_expr * ax,struct axs_value * value,const struct type * curtype,const char * name)1539 gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
1540 const struct type *curtype, const char *name)
1541 {
1542 const char *namespace_name = curtype->name ();
1543 struct block_symbol sym;
1544
1545 sym = cp_lookup_symbol_namespace (namespace_name, name,
1546 block_for_pc (ax->scope),
1547 SEARCH_VAR_DOMAIN);
1548
1549 if (sym.symbol == NULL)
1550 return 0;
1551
1552 gen_var_ref (ax, value, sym.symbol);
1553
1554 if (value->optimized_out)
1555 error (_("`%s' has been optimized out, cannot use"),
1556 sym.symbol->print_name ());
1557
1558 return 1;
1559 }
1560
1561
1562 static int
gen_aggregate_elt_ref(struct agent_expr * ax,struct axs_value * value,struct type * type,const char * field)1563 gen_aggregate_elt_ref (struct agent_expr *ax, struct axs_value *value,
1564 struct type *type, const char *field)
1565 {
1566 switch (type->code ())
1567 {
1568 case TYPE_CODE_STRUCT:
1569 case TYPE_CODE_UNION:
1570 return gen_struct_elt_for_reference (ax, value, type, field);
1571 break;
1572 case TYPE_CODE_NAMESPACE:
1573 return gen_namespace_elt (ax, value, type, field);
1574 break;
1575 default:
1576 internal_error (_("non-aggregate type in gen_aggregate_elt_ref"));
1577 }
1578
1579 return 0;
1580 }
1581
1582
1583
1584 namespace expr
1585 {
1586
1587 void
generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1588 operation::generate_ax (struct expression *exp,
1589 struct agent_expr *ax,
1590 struct axs_value *value,
1591 struct type *cast_type)
1592 {
1593 if (constant_p ())
1594 {
1595 struct value *v = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
1596 ax_const_l (ax, value_as_long (v));
1597 value->kind = axs_rvalue;
1598 value->type = check_typedef (v->type ());
1599 }
1600 else
1601 {
1602 do_generate_ax (exp, ax, value, cast_type);
1603 if (cast_type != nullptr)
1604 gen_cast (ax, value, cast_type);
1605 }
1606 }
1607
1608 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1609 scope_operation::do_generate_ax (struct expression *exp,
1610 struct agent_expr *ax,
1611 struct axs_value *value,
1612 struct type *cast_type)
1613 {
1614 struct type *type = std::get<0> (m_storage);
1615 const std::string &name = std::get<1> (m_storage);
1616 int found = gen_aggregate_elt_ref (ax, value, type, name.c_str ());
1617 if (!found)
1618 error (_("There is no field named %s"), name.c_str ());
1619 }
1620
1621 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1622 long_const_operation::do_generate_ax (struct expression *exp,
1623 struct agent_expr *ax,
1624 struct axs_value *value,
1625 struct type *cast_type)
1626 {
1627 LONGEST val = as_longest ();
1628 gen_int_literal (ax, value, val, std::get<0> (m_storage));
1629 }
1630
1631 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1632 var_msym_value_operation::do_generate_ax (struct expression *exp,
1633 struct agent_expr *ax,
1634 struct axs_value *value,
1635 struct type *cast_type)
1636 {
1637 const bound_minimal_symbol &b = std::get<0> (m_storage);
1638 gen_msym_var_ref (ax, value, b.minsym, b.objfile);
1639
1640 if (value->type->code () == TYPE_CODE_ERROR)
1641 {
1642 if (cast_type == nullptr)
1643 error_unknown_type (b.minsym->linkage_name ());
1644 value->type = cast_type;
1645 }
1646 }
1647
1648 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1649 register_operation::do_generate_ax (struct expression *exp,
1650 struct agent_expr *ax,
1651 struct axs_value *value,
1652 struct type *cast_type)
1653 {
1654 const char *name = std::get<0> (m_storage).c_str ();
1655 int len = std::get<0> (m_storage).size ();
1656 int reg;
1657
1658 reg = user_reg_map_name_to_regnum (ax->gdbarch, name, len);
1659 if (reg == -1)
1660 internal_error (_("Register $%s not available"), name);
1661 /* No support for tracing user registers yet. */
1662 if (reg >= gdbarch_num_cooked_regs (ax->gdbarch))
1663 error (_("'%s' is a user-register; "
1664 "GDB cannot yet trace user-register contents."),
1665 name);
1666 value->kind = axs_lvalue_register;
1667 value->u.reg = reg;
1668 value->type = register_type (ax->gdbarch, reg);
1669 }
1670
1671 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1672 internalvar_operation::do_generate_ax (struct expression *exp,
1673 struct agent_expr *ax,
1674 struct axs_value *value,
1675 struct type *cast_type)
1676 {
1677 struct internalvar *var = std::get<0> (m_storage);
1678 const char *name = internalvar_name (var);
1679 struct trace_state_variable *tsv;
1680
1681 tsv = find_trace_state_variable (name);
1682 if (tsv)
1683 {
1684 ax_tsv (ax, aop_getv, tsv->number);
1685 if (ax->tracing)
1686 ax_tsv (ax, aop_tracev, tsv->number);
1687 /* Trace state variables are always 64-bit integers. */
1688 value->kind = axs_rvalue;
1689 value->type = builtin_type (ax->gdbarch)->builtin_long_long;
1690 }
1691 else if (! compile_internalvar_to_ax (var, ax, value))
1692 error (_("$%s is not a trace state variable; GDB agent "
1693 "expressions cannot use convenience variables."), name);
1694 }
1695
1696 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1697 ternop_cond_operation::do_generate_ax (struct expression *exp,
1698 struct agent_expr *ax,
1699 struct axs_value *value,
1700 struct type *cast_type)
1701 {
1702 struct axs_value value1, value2, value3;
1703 int if1, end;
1704
1705 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
1706 gen_usual_unary (ax, &value1);
1707 /* For (A ? B : C), it's easiest to generate subexpression
1708 bytecodes in order, but if_goto jumps on true, so we invert
1709 the sense of A. Then we can do B by dropping through, and
1710 jump to do C. */
1711 gen_logical_not (ax, &value1, builtin_type (ax->gdbarch)->builtin_int);
1712 if1 = ax_goto (ax, aop_if_goto);
1713 std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
1714 gen_usual_unary (ax, &value2);
1715 end = ax_goto (ax, aop_goto);
1716 ax_label (ax, if1, ax->buf.size ());
1717 std::get<2> (m_storage)->generate_ax (exp, ax, &value3);
1718 gen_usual_unary (ax, &value3);
1719 ax_label (ax, end, ax->buf.size ());
1720 /* This is arbitrary - what if B and C are incompatible types? */
1721 value->type = value2.type;
1722 value->kind = value2.kind;
1723 }
1724
1725 /* Generate code for GDB's magical `repeat' operator.
1726 LVALUE @ INT creates an array INT elements long, and whose elements
1727 have the same type as LVALUE, located in memory so that LVALUE is
1728 its first element. For example, argv[0]@argc gives you the array
1729 of command-line arguments.
1730
1731 Unfortunately, because we have to know the types before we actually
1732 have a value for the expression, we can't implement this perfectly
1733 without changing the type system, having values that occupy two
1734 stack slots, doing weird things with sizeof, etc. So we require
1735 the right operand to be a constant expression. */
1736 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1737 repeat_operation::do_generate_ax (struct expression *exp,
1738 struct agent_expr *ax,
1739 struct axs_value *value,
1740 struct type *cast_type)
1741 {
1742 struct axs_value value1;
1743
1744 /* We don't want to turn this into an rvalue, so no conversions
1745 here. */
1746 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
1747 if (value1.kind != axs_lvalue_memory)
1748 error (_("Left operand of `@' must be an object in memory."));
1749
1750 /* Evaluate the length; it had better be a constant. */
1751 if (!std::get<1> (m_storage)->constant_p ())
1752 error (_("Right operand of `@' must be a "
1753 "constant, in agent expressions."));
1754
1755 struct value *v
1756 = std::get<1> (m_storage)->evaluate (nullptr, exp,
1757 EVAL_AVOID_SIDE_EFFECTS);
1758 if (v->type ()->code () != TYPE_CODE_INT)
1759 error (_("Right operand of `@' must be an integer."));
1760 int length = value_as_long (v);
1761 if (length <= 0)
1762 error (_("Right operand of `@' must be positive."));
1763
1764 /* The top of the stack is already the address of the object, so
1765 all we need to do is frob the type of the lvalue. */
1766 /* FIXME-type-allocation: need a way to free this type when we are
1767 done with it. */
1768 struct type *array
1769 = lookup_array_range_type (value1.type, 0, length - 1);
1770
1771 value->kind = axs_lvalue_memory;
1772 value->type = array;
1773 }
1774
1775 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1776 comma_operation::do_generate_ax (struct expression *exp,
1777 struct agent_expr *ax,
1778 struct axs_value *value,
1779 struct type *cast_type)
1780 {
1781 /* Note that we need to be a little subtle about generating code
1782 for comma. In C, we can do some optimizations here because
1783 we know the left operand is only being evaluated for effect.
1784 However, if the tracing kludge is in effect, then we always
1785 need to evaluate the left hand side fully, so that all the
1786 variables it mentions get traced. */
1787 struct axs_value value1;
1788 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
1789 /* Don't just dispose of the left operand. We might be tracing,
1790 in which case we want to emit code to trace it if it's an
1791 lvalue. */
1792 gen_traced_pop (ax, &value1);
1793 std::get<1> (m_storage)->generate_ax (exp, ax, value);
1794 /* It's the consumer's responsibility to trace the right operand. */
1795 }
1796
1797 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1798 unop_sizeof_operation::do_generate_ax (struct expression *exp,
1799 struct agent_expr *ax,
1800 struct axs_value *value,
1801 struct type *cast_type)
1802 {
1803 /* We don't care about the value of the operand expression; we only
1804 care about its type. However, in the current arrangement, the
1805 only way to find an expression's type is to generate code for it.
1806 So we generate code for the operand, and then throw it away,
1807 replacing it with code that simply pushes its size. */
1808 int start = ax->buf.size ();
1809
1810 std::get<0> (m_storage)->generate_ax (exp, ax, value);
1811
1812 /* Throw away the code we just generated. */
1813 ax->buf.resize (start);
1814
1815 ax_const_l (ax, value->type->length ());
1816 value->kind = axs_rvalue;
1817 value->type = builtin_type (ax->gdbarch)->builtin_int;
1818 }
1819
1820 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1821 unop_cast_operation::do_generate_ax (struct expression *exp,
1822 struct agent_expr *ax,
1823 struct axs_value *value,
1824 struct type *cast_type)
1825 {
1826 std::get<0> (m_storage)->generate_ax (exp, ax, value,
1827 std::get<1> (m_storage));
1828 }
1829
1830 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1831 unop_extract_operation::do_generate_ax (struct expression *exp,
1832 struct agent_expr *ax,
1833 struct axs_value *value,
1834 struct type *cast_type)
1835 {
1836 std::get<0> (m_storage)->generate_ax (exp, ax, value);
1837
1838 struct type *to_type = get_type ();
1839
1840 if (!is_scalar_type (to_type))
1841 error (_("can't generate agent expression to extract non-scalar type"));
1842
1843 if (to_type->is_unsigned ())
1844 gen_extend (ax, to_type);
1845 else
1846 gen_sign_extend (ax, to_type);
1847 }
1848
1849 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1850 unop_memval_operation::do_generate_ax (struct expression *exp,
1851 struct agent_expr *ax,
1852 struct axs_value *value,
1853 struct type *cast_type)
1854 {
1855 std::get<0> (m_storage)->generate_ax (exp, ax, value);
1856 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
1857 already have the right value on the stack. For
1858 axs_lvalue_register, we must convert. */
1859 if (value->kind == axs_lvalue_register)
1860 require_rvalue (ax, value);
1861
1862 value->type = std::get<1> (m_storage);
1863 value->kind = axs_lvalue_memory;
1864 }
1865
1866 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1867 unop_memval_type_operation::do_generate_ax (struct expression *exp,
1868 struct agent_expr *ax,
1869 struct axs_value *value,
1870 struct type *cast_type)
1871 {
1872 struct value *val
1873 = std::get<0> (m_storage)->evaluate (nullptr, exp,
1874 EVAL_AVOID_SIDE_EFFECTS);
1875 struct type *type = val->type ();
1876
1877 std::get<1> (m_storage)->generate_ax (exp, ax, value);
1878
1879 /* If we have an axs_rvalue or an axs_lvalue_memory, then we
1880 already have the right value on the stack. For
1881 axs_lvalue_register, we must convert. */
1882 if (value->kind == axs_lvalue_register)
1883 require_rvalue (ax, value);
1884
1885 value->type = type;
1886 value->kind = axs_lvalue_memory;
1887 }
1888
1889 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1890 op_this_operation::do_generate_ax (struct expression *exp,
1891 struct agent_expr *ax,
1892 struct axs_value *value,
1893 struct type *cast_type)
1894 {
1895 struct symbol *sym, *func;
1896 const struct block *b;
1897 const struct language_defn *lang;
1898
1899 b = block_for_pc (ax->scope);
1900 func = b->linkage_function ();
1901 lang = language_def (func->language ());
1902
1903 sym = lookup_language_this (lang, b).symbol;
1904 if (!sym)
1905 error (_("no `%s' found"), lang->name_of_this ());
1906
1907 gen_var_ref (ax, value, sym);
1908
1909 if (value->optimized_out)
1910 error (_("`%s' has been optimized out, cannot use"),
1911 sym->print_name ());
1912 }
1913
1914 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1915 assign_operation::do_generate_ax (struct expression *exp,
1916 struct agent_expr *ax,
1917 struct axs_value *value,
1918 struct type *cast_type)
1919 {
1920 operation *subop = std::get<0> (m_storage).get ();
1921 if (subop->opcode () != OP_INTERNALVAR)
1922 error (_("May only assign to trace state variables"));
1923
1924 internalvar_operation *ivarop
1925 = gdb::checked_static_cast<internalvar_operation *> (subop);
1926
1927 const char *name = internalvar_name (ivarop->get_internalvar ());
1928 struct trace_state_variable *tsv;
1929
1930 std::get<1> (m_storage)->generate_ax (exp, ax, value);
1931 tsv = find_trace_state_variable (name);
1932 if (tsv)
1933 {
1934 ax_tsv (ax, aop_setv, tsv->number);
1935 if (ax->tracing)
1936 ax_tsv (ax, aop_tracev, tsv->number);
1937 }
1938 else
1939 error (_("$%s is not a trace state variable, "
1940 "may not assign to it"), name);
1941 }
1942
1943 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1944 assign_modify_operation::do_generate_ax (struct expression *exp,
1945 struct agent_expr *ax,
1946 struct axs_value *value,
1947 struct type *cast_type)
1948 {
1949 operation *subop = std::get<1> (m_storage).get ();
1950 if (subop->opcode () != OP_INTERNALVAR)
1951 error (_("May only assign to trace state variables"));
1952
1953 internalvar_operation *ivarop
1954 = gdb::checked_static_cast<internalvar_operation *> (subop);
1955
1956 const char *name = internalvar_name (ivarop->get_internalvar ());
1957 struct trace_state_variable *tsv;
1958
1959 tsv = find_trace_state_variable (name);
1960 if (tsv)
1961 {
1962 /* The tsv will be the left half of the binary operation. */
1963 ax_tsv (ax, aop_getv, tsv->number);
1964 if (ax->tracing)
1965 ax_tsv (ax, aop_tracev, tsv->number);
1966 /* Trace state variables are always 64-bit integers. */
1967 struct axs_value value1, value2;
1968 value1.kind = axs_rvalue;
1969 value1.type = builtin_type (ax->gdbarch)->builtin_long_long;
1970 /* Now do right half of expression. */
1971 std::get<2> (m_storage)->generate_ax (exp, ax, &value2);
1972 gen_expr_binop_rest (exp, std::get<0> (m_storage), ax,
1973 value, &value1, &value2);
1974 /* We have a result of the binary op, set the tsv. */
1975 ax_tsv (ax, aop_setv, tsv->number);
1976 if (ax->tracing)
1977 ax_tsv (ax, aop_tracev, tsv->number);
1978 }
1979 else
1980 error (_("$%s is not a trace state variable, "
1981 "may not assign to it"), name);
1982 }
1983
1984 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1985 unop_cast_type_operation::do_generate_ax (struct expression *exp,
1986 struct agent_expr *ax,
1987 struct axs_value *value,
1988 struct type *cast_type)
1989 {
1990 struct value *val
1991 = std::get<0> (m_storage)->evaluate (nullptr, exp,
1992 EVAL_AVOID_SIDE_EFFECTS);
1993 std::get<1> (m_storage)->generate_ax (exp, ax, value, val->type ());
1994 }
1995
1996 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)1997 var_value_operation::do_generate_ax (struct expression *exp,
1998 struct agent_expr *ax,
1999 struct axs_value *value,
2000 struct type *cast_type)
2001 {
2002 gen_var_ref (ax, value, std::get<0> (m_storage).symbol);
2003
2004 if (value->optimized_out)
2005 error (_("`%s' has been optimized out, cannot use"),
2006 std::get<0> (m_storage).symbol->print_name ());
2007
2008 if (value->type->code () == TYPE_CODE_ERROR)
2009 {
2010 if (cast_type == nullptr)
2011 error_unknown_type (std::get<0> (m_storage).symbol->print_name ());
2012 value->type = cast_type;
2013 }
2014 }
2015
2016 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)2017 logical_and_operation::do_generate_ax (struct expression *exp,
2018 struct agent_expr *ax,
2019 struct axs_value *value,
2020 struct type *cast_type)
2021 {
2022 struct axs_value value1, value2;
2023 int if1, go1, if2, go2, end;
2024
2025 /* Generate the obvious sequence of tests and jumps. */
2026 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
2027 gen_usual_unary (ax, &value1);
2028 if1 = ax_goto (ax, aop_if_goto);
2029 go1 = ax_goto (ax, aop_goto);
2030 ax_label (ax, if1, ax->buf.size ());
2031 std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
2032 gen_usual_unary (ax, &value2);
2033 if2 = ax_goto (ax, aop_if_goto);
2034 go2 = ax_goto (ax, aop_goto);
2035 ax_label (ax, if2, ax->buf.size ());
2036 ax_const_l (ax, 1);
2037 end = ax_goto (ax, aop_goto);
2038 ax_label (ax, go1, ax->buf.size ());
2039 ax_label (ax, go2, ax->buf.size ());
2040 ax_const_l (ax, 0);
2041 ax_label (ax, end, ax->buf.size ());
2042 value->kind = axs_rvalue;
2043 value->type = builtin_type (ax->gdbarch)->builtin_int;
2044 }
2045
2046 void
do_generate_ax(struct expression * exp,struct agent_expr * ax,struct axs_value * value,struct type * cast_type)2047 logical_or_operation::do_generate_ax (struct expression *exp,
2048 struct agent_expr *ax,
2049 struct axs_value *value,
2050 struct type *cast_type)
2051 {
2052 struct axs_value value1, value2;
2053 int if1, if2, end;
2054
2055 /* Generate the obvious sequence of tests and jumps. */
2056 std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
2057 gen_usual_unary (ax, &value1);
2058 if1 = ax_goto (ax, aop_if_goto);
2059 std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
2060 gen_usual_unary (ax, &value2);
2061 if2 = ax_goto (ax, aop_if_goto);
2062 ax_const_l (ax, 0);
2063 end = ax_goto (ax, aop_goto);
2064 ax_label (ax, if1, ax->buf.size ());
2065 ax_label (ax, if2, ax->buf.size ());
2066 ax_const_l (ax, 1);
2067 ax_label (ax, end, ax->buf.size ());
2068 value->kind = axs_rvalue;
2069 value->type = builtin_type (ax->gdbarch)->builtin_int;
2070 }
2071
2072 }
2073
2074 /* This handles the middle-to-right-side of code generation for binary
2075 expressions, which is shared between regular binary operations and
2076 assign-modify (+= and friends) expressions. */
2077
2078 static void
gen_expr_binop_rest(struct expression * exp,enum exp_opcode op,struct agent_expr * ax,struct axs_value * value,struct axs_value * value1,struct axs_value * value2)2079 gen_expr_binop_rest (struct expression *exp,
2080 enum exp_opcode op,
2081 struct agent_expr *ax, struct axs_value *value,
2082 struct axs_value *value1, struct axs_value *value2)
2083 {
2084 struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
2085
2086 gen_usual_unary (ax, value2);
2087 gen_usual_arithmetic (ax, value1, value2);
2088 switch (op)
2089 {
2090 case BINOP_ADD:
2091 if (strip_range_type (value1->type)->code () == TYPE_CODE_INT
2092 && value2->type->is_pointer_or_reference ())
2093 {
2094 /* Swap the values and proceed normally. */
2095 ax_simple (ax, aop_swap);
2096 gen_ptradd (ax, value, value2, value1);
2097 }
2098 else if (value1->type->is_pointer_or_reference ()
2099 && strip_range_type (value2->type)->code () == TYPE_CODE_INT)
2100 gen_ptradd (ax, value, value1, value2);
2101 else
2102 gen_binop (ax, value, value1, value2,
2103 aop_add, aop_add, 1, "addition");
2104 break;
2105 case BINOP_SUB:
2106 if (value1->type->is_pointer_or_reference ()
2107 && strip_range_type (value2->type)->code () == TYPE_CODE_INT)
2108 gen_ptrsub (ax,value, value1, value2);
2109 else if (value1->type->is_pointer_or_reference ()
2110 && value2->type->is_pointer_or_reference ())
2111 /* FIXME --- result type should be ptrdiff_t */
2112 gen_ptrdiff (ax, value, value1, value2,
2113 builtin_type (ax->gdbarch)->builtin_long);
2114 else
2115 gen_binop (ax, value, value1, value2,
2116 aop_sub, aop_sub, 1, "subtraction");
2117 break;
2118 case BINOP_MUL:
2119 gen_binop (ax, value, value1, value2,
2120 aop_mul, aop_mul, 1, "multiplication");
2121 break;
2122 case BINOP_DIV:
2123 gen_binop (ax, value, value1, value2,
2124 aop_div_signed, aop_div_unsigned, 1, "division");
2125 break;
2126 case BINOP_REM:
2127 gen_binop (ax, value, value1, value2,
2128 aop_rem_signed, aop_rem_unsigned, 1, "remainder");
2129 break;
2130 case BINOP_LSH:
2131 gen_binop (ax, value, value1, value2,
2132 aop_lsh, aop_lsh, 1, "left shift");
2133 break;
2134 case BINOP_RSH:
2135 gen_binop (ax, value, value1, value2,
2136 aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
2137 break;
2138 case BINOP_SUBSCRIPT:
2139 {
2140 struct type *type;
2141
2142 if (binop_types_user_defined_p (op, value1->type, value2->type))
2143 {
2144 error (_("cannot subscript requested type: "
2145 "cannot call user defined functions"));
2146 }
2147 else
2148 {
2149 /* If the user attempts to subscript something that is not
2150 an array or pointer type (like a plain int variable for
2151 example), then report this as an error. */
2152 type = check_typedef (value1->type);
2153 if (type->code () != TYPE_CODE_ARRAY
2154 && type->code () != TYPE_CODE_PTR)
2155 {
2156 if (type->name ())
2157 error (_("cannot subscript something of type `%s'"),
2158 type->name ());
2159 else
2160 error (_("cannot subscript requested type"));
2161 }
2162 }
2163
2164 if (!is_integral_type (value2->type))
2165 error (_("Argument to arithmetic operation "
2166 "not a number or boolean."));
2167
2168 gen_ptradd (ax, value, value1, value2);
2169 gen_deref (value);
2170 break;
2171 }
2172 case BINOP_BITWISE_AND:
2173 gen_binop (ax, value, value1, value2,
2174 aop_bit_and, aop_bit_and, 0, "bitwise and");
2175 break;
2176
2177 case BINOP_BITWISE_IOR:
2178 gen_binop (ax, value, value1, value2,
2179 aop_bit_or, aop_bit_or, 0, "bitwise or");
2180 break;
2181
2182 case BINOP_BITWISE_XOR:
2183 gen_binop (ax, value, value1, value2,
2184 aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
2185 break;
2186
2187 case BINOP_EQUAL:
2188 gen_equal (ax, value, value1, value2, int_type);
2189 break;
2190
2191 case BINOP_NOTEQUAL:
2192 gen_equal (ax, value, value1, value2, int_type);
2193 gen_logical_not (ax, value, int_type);
2194 break;
2195
2196 case BINOP_LESS:
2197 gen_less (ax, value, value1, value2, int_type);
2198 break;
2199
2200 case BINOP_GTR:
2201 ax_simple (ax, aop_swap);
2202 gen_less (ax, value, value1, value2, int_type);
2203 break;
2204
2205 case BINOP_LEQ:
2206 ax_simple (ax, aop_swap);
2207 gen_less (ax, value, value1, value2, int_type);
2208 gen_logical_not (ax, value, int_type);
2209 break;
2210
2211 case BINOP_GEQ:
2212 gen_less (ax, value, value1, value2, int_type);
2213 gen_logical_not (ax, value, int_type);
2214 break;
2215
2216 default:
2217 /* We should only list operators in the outer case statement
2218 that we actually handle in the inner case statement. */
2219 internal_error (_("gen_expr: op case sets don't match"));
2220 }
2221 }
2222
2223 /* A helper function that emits a binop based on two operations. */
2224
2225 void
gen_expr_binop(struct expression * exp,enum exp_opcode op,expr::operation * lhs,expr::operation * rhs,struct agent_expr * ax,struct axs_value * value)2226 gen_expr_binop (struct expression *exp,
2227 enum exp_opcode op,
2228 expr::operation *lhs, expr::operation *rhs,
2229 struct agent_expr *ax, struct axs_value *value)
2230 {
2231 struct axs_value value1, value2;
2232
2233 lhs->generate_ax (exp, ax, &value1);
2234 gen_usual_unary (ax, &value1);
2235 rhs->generate_ax (exp, ax, &value2);
2236 gen_expr_binop_rest (exp, op, ax, value, &value1, &value2);
2237 }
2238
2239 /* A helper function that emits a structop based on an operation and a
2240 member name. */
2241
2242 void
gen_expr_structop(struct expression * exp,enum exp_opcode op,expr::operation * lhs,const char * name,struct agent_expr * ax,struct axs_value * value)2243 gen_expr_structop (struct expression *exp,
2244 enum exp_opcode op,
2245 expr::operation *lhs,
2246 const char *name,
2247 struct agent_expr *ax, struct axs_value *value)
2248 {
2249 lhs->generate_ax (exp, ax, value);
2250 if (op == STRUCTOP_STRUCT)
2251 gen_struct_ref (ax, value, name, ".", "structure or union");
2252 else if (op == STRUCTOP_PTR)
2253 gen_struct_ref (ax, value, name, "->",
2254 "pointer to a structure or union");
2255 else
2256 /* If this `if' chain doesn't handle it, then the case list
2257 shouldn't mention it, and we shouldn't be here. */
2258 internal_error (_("gen_expr: unhandled struct case"));
2259 }
2260
2261 /* A helper function that emits a unary operation. */
2262
2263 void
gen_expr_unop(struct expression * exp,enum exp_opcode op,expr::operation * lhs,struct agent_expr * ax,struct axs_value * value)2264 gen_expr_unop (struct expression *exp,
2265 enum exp_opcode op,
2266 expr::operation *lhs,
2267 struct agent_expr *ax, struct axs_value *value)
2268 {
2269 struct axs_value value1, value2;
2270
2271 switch (op)
2272 {
2273 case UNOP_NEG:
2274 gen_int_literal (ax, &value1, 0,
2275 builtin_type (ax->gdbarch)->builtin_int);
2276 gen_usual_unary (ax, &value1); /* shouldn't do much */
2277 lhs->generate_ax (exp, ax, &value2);
2278 gen_usual_unary (ax, &value2);
2279 gen_usual_arithmetic (ax, &value1, &value2);
2280 gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
2281 break;
2282
2283 case UNOP_PLUS:
2284 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2285 lhs->generate_ax (exp, ax, value);
2286 gen_usual_unary (ax, value);
2287 break;
2288
2289 case UNOP_LOGICAL_NOT:
2290 lhs->generate_ax (exp, ax, value);
2291 gen_usual_unary (ax, value);
2292 gen_logical_not (ax, value, builtin_type (ax->gdbarch)->builtin_int);
2293 break;
2294
2295 case UNOP_COMPLEMENT:
2296 lhs->generate_ax (exp, ax, value);
2297 gen_usual_unary (ax, value);
2298 gen_integral_promotions (ax, value);
2299 gen_complement (ax, value);
2300 break;
2301
2302 case UNOP_IND:
2303 lhs->generate_ax (exp, ax, value);
2304 gen_usual_unary (ax, value);
2305 if (!value->type->is_pointer_or_reference ())
2306 error (_("Argument of unary `*' is not a pointer."));
2307 gen_deref (value);
2308 break;
2309
2310 case UNOP_ADDR:
2311 lhs->generate_ax (exp, ax, value);
2312 gen_address_of (value);
2313 break;
2314
2315 default:
2316 gdb_assert_not_reached ("invalid case in gen_expr_unop");
2317 }
2318 }
2319
2320
2321
2322 /* Given a single variable and a scope, generate bytecodes to trace
2323 its value. This is for use in situations where we have only a
2324 variable's name, and no parsed expression; for instance, when the
2325 name comes from a list of local variables of a function. */
2326
2327 agent_expr_up
gen_trace_for_var(CORE_ADDR scope,struct gdbarch * gdbarch,struct symbol * var,int trace_string)2328 gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
2329 struct symbol *var, int trace_string)
2330 {
2331 agent_expr_up ax (new agent_expr (gdbarch, scope));
2332 struct axs_value value;
2333
2334 ax->tracing = true;
2335 ax->trace_string = trace_string;
2336 gen_var_ref (ax.get (), &value, var);
2337
2338 /* If there is no actual variable to trace, flag it by returning
2339 an empty agent expression. */
2340 if (value.optimized_out)
2341 return agent_expr_up ();
2342
2343 /* Make sure we record the final object, and get rid of it. */
2344 gen_traced_pop (ax.get (), &value);
2345
2346 /* Oh, and terminate. */
2347 ax_simple (ax.get (), aop_end);
2348
2349 return ax;
2350 }
2351
2352 /* Generating bytecode from GDB expressions: driver */
2353
2354 /* Given a GDB expression EXPR, return bytecode to trace its value.
2355 The result will use the `trace' and `trace_quick' bytecodes to
2356 record the value of all memory touched by the expression. The
2357 caller can then use the ax_reqs function to discover which
2358 registers it relies upon. */
2359
2360 agent_expr_up
gen_trace_for_expr(CORE_ADDR scope,struct expression * expr,int trace_string)2361 gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
2362 int trace_string)
2363 {
2364 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2365 struct axs_value value;
2366
2367 ax->tracing = true;
2368 ax->trace_string = trace_string;
2369 value.optimized_out = 0;
2370 expr->op->generate_ax (expr, ax.get (), &value);
2371
2372 /* Make sure we record the final object, and get rid of it. */
2373 gen_traced_pop (ax.get (), &value);
2374
2375 /* Oh, and terminate. */
2376 ax_simple (ax.get (), aop_end);
2377
2378 return ax;
2379 }
2380
2381 /* Given a GDB expression EXPR, return a bytecode sequence that will
2382 evaluate and return a result. The bytecodes will do a direct
2383 evaluation, using the current data on the target, rather than
2384 recording blocks of memory and registers for later use, as
2385 gen_trace_for_expr does. The generated bytecode sequence leaves
2386 the result of expression evaluation on the top of the stack. */
2387
2388 agent_expr_up
gen_eval_for_expr(CORE_ADDR scope,struct expression * expr)2389 gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
2390 {
2391 agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
2392 struct axs_value value;
2393
2394 ax->tracing = false;
2395 value.optimized_out = 0;
2396 expr->op->generate_ax (expr, ax.get (), &value);
2397
2398 require_rvalue (ax.get (), &value);
2399
2400 /* Oh, and terminate. */
2401 ax_simple (ax.get (), aop_end);
2402
2403 return ax;
2404 }
2405
2406 agent_expr_up
gen_trace_for_return_address(CORE_ADDR scope,struct gdbarch * gdbarch,int trace_string)2407 gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
2408 int trace_string)
2409 {
2410 agent_expr_up ax (new agent_expr (gdbarch, scope));
2411 struct axs_value value;
2412
2413 ax->tracing = true;
2414 ax->trace_string = trace_string;
2415
2416 gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
2417
2418 /* Make sure we record the final object, and get rid of it. */
2419 gen_traced_pop (ax.get (), &value);
2420
2421 /* Oh, and terminate. */
2422 ax_simple (ax.get (), aop_end);
2423
2424 return ax;
2425 }
2426
2427 /* Given a collection of printf-style arguments, generate code to
2428 evaluate the arguments and pass everything to a special
2429 bytecode. */
2430
2431 agent_expr_up
gen_printf(CORE_ADDR scope,struct gdbarch * gdbarch,CORE_ADDR function,LONGEST channel,const char * format,int fmtlen,int nargs,struct expression ** exprs)2432 gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
2433 CORE_ADDR function, LONGEST channel,
2434 const char *format, int fmtlen,
2435 int nargs, struct expression **exprs)
2436 {
2437 agent_expr_up ax (new agent_expr (gdbarch, scope));
2438 struct axs_value value;
2439 int tem;
2440
2441 /* We're computing values, not doing side effects. */
2442 ax->tracing = false;
2443
2444 /* Evaluate and push the args on the stack in reverse order,
2445 for simplicity of collecting them on the target side. */
2446 for (tem = nargs - 1; tem >= 0; --tem)
2447 {
2448 value.optimized_out = 0;
2449 exprs[tem]->op->generate_ax (exprs[tem], ax.get (), &value);
2450 require_rvalue (ax.get (), &value);
2451 }
2452
2453 /* Push function and channel. */
2454 ax_const_l (ax.get (), channel);
2455 ax_const_l (ax.get (), function);
2456
2457 /* Issue the printf bytecode proper. */
2458 ax_simple (ax.get (), aop_printf);
2459 ax_raw_byte (ax.get (), nargs);
2460 ax_string (ax.get (), format, fmtlen);
2461
2462 /* And terminate. */
2463 ax_simple (ax.get (), aop_end);
2464
2465 return ax;
2466 }
2467
2468 static void
agent_eval_command_one(const char * exp,int eval,CORE_ADDR pc)2469 agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
2470 {
2471 const char *arg;
2472 int trace_string = 0;
2473
2474 if (!eval)
2475 {
2476 if (*exp == '/')
2477 exp = decode_agent_options (exp, &trace_string);
2478 }
2479
2480 agent_expr_up agent;
2481
2482 arg = exp;
2483 if (!eval && strcmp (arg, "$_ret") == 0)
2484 {
2485 agent = gen_trace_for_return_address (pc, get_current_arch (),
2486 trace_string);
2487 }
2488 else
2489 {
2490 expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
2491
2492 if (eval)
2493 {
2494 gdb_assert (trace_string == 0);
2495 agent = gen_eval_for_expr (pc, expr.get ());
2496 }
2497 else
2498 agent = gen_trace_for_expr (pc, expr.get (), trace_string);
2499 }
2500
2501 ax_reqs (agent.get ());
2502 ax_print (gdb_stdout, agent.get ());
2503
2504 /* It would be nice to call ax_reqs here to gather some general info
2505 about the expression, and then print out the result. */
2506
2507 dont_repeat ();
2508 }
2509
2510 static void
maint_agent_command_1(const char * exp,int eval)2511 maint_agent_command_1 (const char *exp, int eval)
2512 {
2513 /* We don't deal with overlay debugging at the moment. We need to
2514 think more carefully about this. If you copy this code into
2515 another command, change the error message; the user shouldn't
2516 have to know anything about agent expressions. */
2517 if (overlay_debugging)
2518 error (_("GDB can't do agent expression translation with overlays."));
2519
2520 if (exp == 0)
2521 error_no_arg (_("expression to translate"));
2522
2523 if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
2524 {
2525 struct linespec_result canonical;
2526
2527 location_spec_up locspec
2528 = new_linespec_location_spec (&exp, symbol_name_match_type::WILD);
2529 decode_line_full (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
2530 NULL, 0, &canonical,
2531 NULL, NULL);
2532 exp = skip_spaces (exp);
2533 if (exp[0] == ',')
2534 {
2535 exp++;
2536 exp = skip_spaces (exp);
2537 }
2538 for (const auto &lsal : canonical.lsals)
2539 for (const auto &sal : lsal.sals)
2540 agent_eval_command_one (exp, eval, sal.pc);
2541 }
2542 else
2543 agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
2544
2545 dont_repeat ();
2546 }
2547
2548 static void
maint_agent_command(const char * exp,int from_tty)2549 maint_agent_command (const char *exp, int from_tty)
2550 {
2551 maint_agent_command_1 (exp, 0);
2552 }
2553
2554 /* Parse the given expression, compile it into an agent expression
2555 that does direct evaluation, and display the resulting
2556 expression. */
2557
2558 static void
maint_agent_eval_command(const char * exp,int from_tty)2559 maint_agent_eval_command (const char *exp, int from_tty)
2560 {
2561 maint_agent_command_1 (exp, 1);
2562 }
2563
2564 /* Parse the given expression, compile it into an agent expression
2565 that does a printf, and display the resulting expression. */
2566
2567 static void
maint_agent_printf_command(const char * cmdrest,int from_tty)2568 maint_agent_printf_command (const char *cmdrest, int from_tty)
2569 {
2570 frame_info_ptr fi = get_current_frame (); /* need current scope */
2571 const char *format_start, *format_end;
2572
2573 /* We don't deal with overlay debugging at the moment. We need to
2574 think more carefully about this. If you copy this code into
2575 another command, change the error message; the user shouldn't
2576 have to know anything about agent expressions. */
2577 if (overlay_debugging)
2578 error (_("GDB can't do agent expression translation with overlays."));
2579
2580 if (cmdrest == 0)
2581 error_no_arg (_("expression to translate"));
2582
2583 cmdrest = skip_spaces (cmdrest);
2584
2585 if (*cmdrest++ != '"')
2586 error (_("Must start with a format string."));
2587
2588 format_start = cmdrest;
2589
2590 format_pieces fpieces (&cmdrest);
2591
2592 format_end = cmdrest;
2593
2594 if (*cmdrest++ != '"')
2595 error (_("Bad format string, non-terminated '\"'."));
2596
2597 cmdrest = skip_spaces (cmdrest);
2598
2599 if (*cmdrest != ',' && *cmdrest != 0)
2600 error (_("Invalid argument syntax"));
2601
2602 if (*cmdrest == ',')
2603 cmdrest++;
2604 cmdrest = skip_spaces (cmdrest);
2605
2606 std::vector<struct expression *> argvec;
2607 while (*cmdrest != '\0')
2608 {
2609 const char *cmd1;
2610
2611 cmd1 = cmdrest;
2612 expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0,
2613 PARSER_COMMA_TERMINATES);
2614 argvec.push_back (expr.release ());
2615 cmdrest = cmd1;
2616 if (*cmdrest == ',')
2617 ++cmdrest;
2618 /* else complain? */
2619 }
2620
2621
2622 agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
2623 0, 0,
2624 format_start, format_end - format_start,
2625 argvec.size (), argvec.data ());
2626 ax_reqs (agent.get ());
2627 ax_print (gdb_stdout, agent.get ());
2628
2629 /* It would be nice to call ax_reqs here to gather some general info
2630 about the expression, and then print out the result. */
2631
2632 dont_repeat ();
2633 }
2634
2635 /* Initialization code. */
2636
2637 void _initialize_ax_gdb ();
2638 void
_initialize_ax_gdb()2639 _initialize_ax_gdb ()
2640 {
2641 add_cmd ("agent", class_maintenance, maint_agent_command,
2642 _("\
2643 Translate an expression into remote agent bytecode for tracing.\n\
2644 Usage: maint agent [-at LOCATION,] EXPRESSION\n\
2645 If -at is given, generate remote agent bytecode for this location.\n\
2646 If not, generate remote agent bytecode for current frame pc address."),
2647 &maintenancelist);
2648
2649 add_cmd ("agent-eval", class_maintenance, maint_agent_eval_command,
2650 _("\
2651 Translate an expression into remote agent bytecode for evaluation.\n\
2652 Usage: maint agent-eval [-at LOCATION,] EXPRESSION\n\
2653 If -at is given, generate remote agent bytecode for this location.\n\
2654 If not, generate remote agent bytecode for current frame pc address."),
2655 &maintenancelist);
2656
2657 add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
2658 _("Translate an expression into remote "
2659 "agent bytecode for evaluation and display the bytecodes."),
2660 &maintenancelist);
2661 }
2662