1 /* GDB-specific functions for operating on agent expressions.
3 Copyright (C) 1998-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 #include "expression.h"
27 #include "cli/cli-cmds.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"
44 #include "typeprint.h"
49 #include "gdbsupport/format.h"
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
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
65 Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
69 /* Prototypes for local functions. */
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 */
75 static void gen_traced_pop (struct agent_expr
*, struct axs_value
*);
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);
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
,
91 static void gen_int_literal (struct agent_expr
*ax
,
92 struct axs_value
*value
,
93 LONGEST k
, struct type
*type
);
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
,
122 enum agent_op op_unsigned
, int may_carry
,
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
,
138 static void gen_struct_ref (struct agent_expr
*ax
,
139 struct axs_value
*value
,
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
,
147 struct agent_expr
*ax
,
148 struct axs_value
*value
,
149 struct axs_value
*value1
,
150 struct axs_value
*value2
);
154 /* Generating bytecode from GDB expressions: general assumptions */
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.
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.
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
174 - We don't support side effects. Thus, we don't have to worry about
175 GCC's generalized lvalues, function calls, etc.
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.
182 - We assume all subexpressions have a static, unchanging type. If
183 we tried to support convenience variables, this would be a
186 - All values on the stack should always be fully zero- or
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.) */
198 /* Scan for all static fields in the given class, including any base
199 classes, and generate tracing bytecodes for each. */
202 gen_trace_static_fields (struct agent_expr
*ax
,
205 int i
, nbases
= TYPE_N_BASECLASSES (type
);
206 struct axs_value value
;
208 type
= check_typedef (type
);
210 for (i
= type
->num_fields () - 1; i
>= nbases
; i
--)
212 if (type
->field (i
).is_static ())
214 gen_static_field (ax
, &value
, type
, i
);
215 if (value
.optimized_out
)
219 case axs_lvalue_memory
:
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
);
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
);
239 /* Now scan through base classes recursively. */
240 for (i
= 0; i
< nbases
; i
++)
242 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
244 gen_trace_static_fields (ax
, basetype
);
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. */
252 gen_traced_pop (struct agent_expr
*ax
, struct axs_value
*value
)
254 int string_trace
= 0;
256 && value
->type
->code () == TYPE_CODE_PTR
257 && c_textual_element_type (check_typedef (value
->type
->target_type ()),
267 ax_const_l (ax
, ax
->trace_string
);
268 ax_simple (ax
, aop_tracenz
);
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
);
276 case axs_lvalue_memory
:
278 /* Initialize the TYPE_LENGTH if it is a typedef. */
279 check_typedef (value
->type
);
283 gen_fetch (ax
, value
->type
);
284 ax_const_l (ax
, ax
->trace_string
);
285 ax_simple (ax
, aop_tracenz
);
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
);
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
);
307 /* But if the register points to a string, assume the value
308 will fit on the stack and push it anyway. */
311 ax_reg (ax
, value
->u
.reg
);
312 ax_const_l (ax
, ax
->trace_string
);
313 ax_simple (ax
, aop_tracenz
);
318 /* If we're not tracing, just pop the value. */
319 ax_simple (ax
, aop_pop
);
321 /* To trace C++ classes with static fields stored elsewhere. */
323 && (value
->type
->code () == TYPE_CODE_STRUCT
324 || value
->type
->code () == TYPE_CODE_UNION
))
325 gen_trace_static_fields (ax
, value
->type
);
330 /* Generating bytecode from GDB expressions: helper functions */
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. */
335 gen_sign_extend (struct agent_expr
*ax
, struct type
*type
)
337 /* Do we need to sign-extend this? */
338 if (!type
->is_unsigned ())
339 ax_ext (ax
, type
->length () * TARGET_CHAR_BIT
);
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
347 gen_extend (struct agent_expr
*ax
, struct type
*type
)
349 int bits
= type
->length () * TARGET_CHAR_BIT
;
352 ((type
->is_unsigned () ? ax_zero_ext
: ax_ext
) (ax
, bits
));
355 /* A helper that returns the target type if TYPE is a range type, or
356 otherwise just returns TYPE. */
359 strip_range_type (struct type
*type
)
361 if (type
->code () == TYPE_CODE_RANGE
)
362 return type
->target_type ();
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. */
370 gen_fetch (struct agent_expr
*ax
, struct type
*type
)
374 /* Record the area of memory we're about to fetch. */
375 ax_trace_quick (ax
, type
->length ());
378 type
= strip_range_type (type
);
380 switch (type
->code ())
384 case TYPE_CODE_RVALUE_REF
:
389 /* It's a scalar value, so we know how to dereference it. How
390 many bytes long is it? */
391 switch (type
->length ())
393 case 8 / TARGET_CHAR_BIT
:
394 ax_simple (ax
, aop_ref8
);
396 case 16 / TARGET_CHAR_BIT
:
397 ax_simple (ax
, aop_ref16
);
399 case 32 / TARGET_CHAR_BIT
:
400 ax_simple (ax
, aop_ref32
);
402 case 64 / TARGET_CHAR_BIT
:
403 ax_simple (ax
, aop_ref64
);
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. */
411 internal_error (_("gen_fetch: strange size"));
414 gen_sign_extend (ax
, type
);
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
421 error (_("gen_fetch: Unsupported type code `%s'."),
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. */
431 gen_left_shift (struct agent_expr
*ax
, int distance
)
435 ax_const_l (ax
, distance
);
436 ax_simple (ax
, aop_lsh
);
438 else if (distance
< 0)
440 ax_const_l (ax
, -distance
);
441 ax_simple (ax
, aop_rsh_unsigned
);
447 /* Generating bytecode from GDB expressions: symbol references */
449 /* Generate code to push the base address of the argument portion of
450 the top stack frame. */
452 gen_frame_args_address (struct agent_expr
*ax
)
455 LONGEST frame_offset
;
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
);
464 /* Generate code to push the base address of the locals portion of the
467 gen_frame_locals_address (struct agent_expr
*ax
)
470 LONGEST frame_offset
;
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
);
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
485 gen_offset (struct agent_expr
*ax
, int offset
)
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
492 ax_const_l (ax
, offset
);
493 ax_simple (ax
, aop_add
);
497 ax_const_l (ax
, -offset
);
498 ax_simple (ax
, aop_sub
);
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. */
507 gen_sym_offset (struct agent_expr
*ax
, struct symbol
*var
)
509 gen_offset (ax
, var
->value_longest ());
513 /* Generate code for a variable reference to AX. The variable is the
514 symbol VAR. Set VALUE to describe the result. */
517 gen_var_ref (struct agent_expr
*ax
, struct axs_value
*value
, struct symbol
*var
)
519 /* Dereference any typedefs. */
520 value
->type
= check_typedef (var
->type ());
521 value
->optimized_out
= 0;
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
);
527 /* I'm imitating the code in read_var_value. */
528 switch (var
->aclass ())
530 case LOC_CONST
: /* A constant, like an enum value. */
531 ax_const_l (ax
, (LONGEST
) var
->value_longest ());
532 value
->kind
= axs_rvalue
;
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
;
540 case LOC_CONST_BYTES
:
541 internal_error (_("gen_var_ref: LOC_CONST_BYTES "
542 "symbols are not supported"));
544 /* Variable at a fixed location in memory. Easy. */
546 /* Push the address of the variable. */
547 ax_const_l (ax
, var
->value_address ());
548 value
->kind
= axs_lvalue_memory
;
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
;
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
;
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
;
573 error (_("Cannot compute value of typedef `%s'."),
578 ax_const_l (ax
, var
->value_block ()->entry_pc ());
579 value
->kind
= axs_rvalue
;
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
586 value
->kind
= axs_lvalue_register
;
587 value
->u
.reg
= var
->register_ops ()->register_number (var
, ax
->gdbarch
);
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
;
601 bound_minimal_symbol msym
602 = lookup_minimal_symbol (current_program_space
,
603 var
->linkage_name ());
605 error (_("Couldn't resolve symbol `%s'."), var
->print_name ());
607 /* Push the address of the variable. */
608 ax_const_l (ax
, msym
.value_address ());
609 value
->kind
= axs_lvalue_memory
;
614 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
616 case LOC_OPTIMIZED_OUT
:
617 /* Flag this, but don't say anything; leave it up to callers to
619 value
->optimized_out
= 1;
623 error (_("Cannot find value of botched symbol `%s'."),
629 /* Generate code for a minimal symbol variable reference to AX. The
630 variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
634 gen_msym_var_ref (agent_expr
*ax
, axs_value
*value
,
635 minimal_symbol
*msymbol
, objfile
*objf
)
638 type
*t
= find_minsym_type_and_address (msymbol
, objf
, &address
);
640 value
->optimized_out
= false;
641 ax_const_l (ax
, address
);
642 value
->kind
= axs_lvalue_memory
;
648 /* Generating bytecode from GDB expressions: literals */
651 gen_int_literal (struct agent_expr
*ax
, struct axs_value
*value
, LONGEST k
,
655 value
->kind
= axs_rvalue
;
656 value
->type
= check_typedef (type
);
661 /* Generating bytecode from GDB expressions: unary conversions, casts */
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
667 require_rvalue (struct agent_expr
*ax
, struct axs_value
*value
)
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."));
681 /* It's already an rvalue. */
684 case axs_lvalue_memory
:
685 /* The top of stack is the address of the object. Dereference. */
686 gen_fetch (ax
, value
->type
);
689 case axs_lvalue_register
:
690 /* There's nothing on the stack, but value->u.reg is the
691 register number containing the value.
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
);
700 value
->kind
= axs_rvalue
;
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.
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.
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. */
721 gen_usual_unary (struct agent_expr
*ax
, struct axs_value
*value
)
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? */
727 /* Some types require special handling. */
728 switch (value
->type
->code ())
730 /* Functions get converted to a pointer to the function. */
732 value
->type
= lookup_pointer_type (value
->type
);
733 value
->kind
= axs_rvalue
; /* Should always be true, but just in case. */
736 /* Arrays get converted to a pointer to their first element, and
737 are no longer an lvalue. */
738 case TYPE_CODE_ARRAY
:
740 struct type
*elements
= value
->type
->target_type ();
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. */
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
:
756 /* If the value is an lvalue, dereference it. */
757 require_rvalue (ax
, value
);
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. */
764 type_wider_than (struct type
*type1
, struct type
*type2
)
766 return (type1
->length () > type2
->length ()
767 || (type1
->length () == type2
->length ()
768 && type1
->is_unsigned ()
769 && !type2
->is_unsigned ()));
773 /* Return the "wider" of the two types TYPE1 and TYPE2. */
775 max_type (struct type
*type1
, struct type
*type2
)
777 return type_wider_than (type1
, type2
) ? type1
: type2
;
781 /* Generate code to convert a scalar value of type FROM to type TO. */
783 gen_conversion (struct agent_expr
*ax
, struct type
*from
, struct type
*to
)
785 /* Perhaps there is a more graceful way to state these rules. */
787 /* If we're converting to a narrower type, then we need to clear out
789 if (to
->length () < from
->length ())
792 /* If the two values have equal width, but different signednesses,
793 then we need to extend. */
794 else if (to
->length () == from
->length ())
796 if (from
->is_unsigned () != to
->is_unsigned ())
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 ())
804 if (to
->is_unsigned ())
810 /* Return non-zero iff the type FROM will require any bytecodes to be
811 emitted to be converted to the type TO. */
813 is_nontrivial_conversion (struct type
*from
, struct type
*to
)
815 agent_expr_up
ax (new agent_expr (NULL
, 0));
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
823 gen_conversion (ax
.get (), from
, to
);
824 return !ax
->buf
.empty ();
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. */
834 gen_usual_arithmetic (struct agent_expr
*ax
, struct axs_value
*value1
,
835 struct axs_value
*value2
)
837 struct type
*type1
= strip_range_type (value1
->type
);
838 struct type
*type2
= strip_range_type (value2
->type
);
840 /* Do the usual binary conversions. */
841 if (type1
->code () == TYPE_CODE_INT
842 && type2
->code () == TYPE_CODE_INT
)
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
));
852 /* Deal with value2, on the top of the stack. */
853 gen_conversion (ax
, type2
, target
);
855 /* Deal with value1, not on the top of the stack. Don't
856 generate the `swap' instructions if we're not actually going
858 if (is_nontrivial_conversion (type1
, target
))
860 ax_simple (ax
, aop_swap
);
861 gen_conversion (ax
, type1
, target
);
862 ax_simple (ax
, aop_swap
);
865 value1
->type
= value2
->type
= check_typedef (target
);
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. */
874 gen_integral_promotions (struct agent_expr
*ax
, struct axs_value
*value
)
876 const struct builtin_type
*builtin
= builtin_type (ax
->gdbarch
);
878 if (!type_wider_than (value
->type
, builtin
->builtin_int
))
880 gen_conversion (ax
, value
->type
, builtin
->builtin_int
);
881 value
->type
= builtin
->builtin_int
;
883 else if (!type_wider_than (value
->type
, builtin
->builtin_unsigned_int
))
885 gen_conversion (ax
, value
->type
, builtin
->builtin_unsigned_int
);
886 value
->type
= builtin
->builtin_unsigned_int
;
891 /* Generate code for a cast to TYPE. */
893 gen_cast (struct agent_expr
*ax
, struct axs_value
*value
, struct type
*type
)
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
);
902 switch (type
->code ())
906 case TYPE_CODE_RVALUE_REF
:
907 /* It's implementation-defined, and I'll bet this is what GCC
911 case TYPE_CODE_ARRAY
:
912 case TYPE_CODE_STRUCT
:
913 case TYPE_CODE_UNION
:
915 error (_("Invalid type cast: intended type must be scalar."));
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
927 gen_conversion (ax
, value
->type
, type
);
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. */
938 error (_("Casts to requested type are not yet implemented."));
946 /* Generating bytecode from GDB expressions: arithmetic */
948 /* Scale the integer on the top of the stack by the size of the target
949 of the pointer type TYPE. */
951 gen_scale (struct agent_expr
*ax
, enum agent_op op
, struct type
*type
)
953 struct type
*element
= type
->target_type ();
955 if (element
->length () != 1)
957 ax_const_l (ax
, element
->length ());
963 /* Generate code for pointer arithmetic PTR + INT. */
965 gen_ptradd (struct agent_expr
*ax
, struct axs_value
*value
,
966 struct axs_value
*value1
, struct axs_value
*value2
)
968 gdb_assert (value1
->type
->is_pointer_or_reference ());
969 gdb_assert (strip_range_type (value2
->type
)->code () == TYPE_CODE_INT
);
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
;
979 /* Generate code for pointer arithmetic PTR - INT. */
981 gen_ptrsub (struct agent_expr
*ax
, struct axs_value
*value
,
982 struct axs_value
*value1
, struct axs_value
*value2
)
984 gdb_assert (value1
->type
->is_pointer_or_reference ());
985 gdb_assert (strip_range_type (value2
->type
)->code () == TYPE_CODE_INT
);
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
;
995 /* Generate code for pointer arithmetic PTR - PTR. */
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
)
1001 gdb_assert (value1
->type
->is_pointer_or_reference ());
1002 gdb_assert (value2
->type
->is_pointer_or_reference ());
1004 if (value1
->type
->target_type ()->length ()
1005 != value2
->type
->target_type ()->length ())
1007 First argument of `-' is a pointer, but second argument is neither\n\
1008 an integer nor a pointer of the same type."));
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
;
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
)
1021 if (value1
->type
->is_pointer_or_reference () || value2
->type
->is_pointer_or_reference ())
1022 ax_simple (ax
, aop_equal
);
1024 gen_binop (ax
, value
, value1
, value2
,
1025 aop_equal
, aop_equal
, 0, "equal");
1026 value
->type
= result_type
;
1027 value
->kind
= axs_rvalue
;
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
)
1035 if (value1
->type
->is_pointer_or_reference () || value2
->type
->is_pointer_or_reference ())
1036 ax_simple (ax
, aop_less_unsigned
);
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
;
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 */
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
)
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
);
1062 ax_simple (ax
, type1
->is_unsigned () ? op_unsigned
: op
);
1064 gen_extend (ax
, type1
); /* catch overflow */
1065 value
->type
= type1
;
1066 value
->kind
= axs_rvalue
;
1071 gen_logical_not (struct agent_expr
*ax
, struct axs_value
*value
,
1072 struct type
*result_type
)
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 `!'."));
1079 ax_simple (ax
, aop_log_not
);
1080 value
->type
= result_type
;
1085 gen_complement (struct agent_expr
*ax
, struct axs_value
*value
)
1087 struct type
*type
= strip_range_type (value
->type
);
1088 if (type
->code () != TYPE_CODE_INT
)
1089 error (_("Invalid type of operand to `~'."));
1091 ax_simple (ax
, aop_bit_not
);
1092 gen_extend (ax
, type
);
1097 /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
1099 /* Dereference the value on the top of the stack. */
1101 gen_deref (struct axs_value
*value
)
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"));
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
);
1121 /* Produce the address of the lvalue on the top of the stack. */
1123 gen_address_of (struct axs_value
*value
)
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
1131 value
->type
= lookup_pointer_type (value
->type
);
1133 switch (value
->kind
)
1136 error (_("Operand of `&' is an rvalue, which has no address."));
1138 case axs_lvalue_register
:
1139 error (_("Operand of `&' is in a register, and has no address."));
1141 case axs_lvalue_memory
:
1142 value
->kind
= axs_rvalue
;
1143 value
->type
= lookup_pointer_type (value
->type
);
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
1153 gen_bitfield_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1154 struct type
*type
, int start
, int end
)
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]));
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.
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:
1170 For the first fetch, we duplicate the address
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:
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. */
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)
1194 /* current bit offset within the structure */
1197 /* The index in ops of the opcode we're considering. */
1200 /* The number of fragments we generated in the process. Probably
1201 equal to the number of `one' bits in bytesize, but who cares? */
1204 /* Dereference any typedefs. */
1205 type
= check_typedef (type
);
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"));
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
;
1215 for (op
= num_ops
- 1; op
>= 0; op
--)
1217 /* number of bits that ops[op] would fetch */
1218 int op_size
= 8 << op
;
1220 /* The stack at this point, from bottom to top, contains zero or
1221 more fragments, then the address. */
1223 /* Does this fetch fit within the bitfield? */
1224 if (offset
+ op_size
<= bound_end
)
1226 /* Is this the last fragment? */
1227 int last_frag
= (offset
+ op_size
== bound_end
);
1230 ax_simple (ax
, aop_dup
); /* keep a copy of the address */
1232 /* Add the offset. */
1233 gen_offset (ax
, offset
/ TARGET_CHAR_BIT
);
1237 /* Record the area of memory we're about to fetch. */
1238 ax_trace_quick (ax
, op_size
/ TARGET_CHAR_BIT
);
1241 /* Perform the fetch. */
1242 ax_simple (ax
, ops
[op
]);
1244 /* Shift the bits we have to their proper position.
1245 gen_left_shift will generate right shifts when the operand
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
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
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
1263 bit number 48 32 16 4 0
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.
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
));
1281 gen_left_shift (ax
, offset
- start
);
1284 /* Bring the copy of the address up to the top. */
1285 ax_simple (ax
, aop_swap
);
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
);
1297 /* Sign- or zero-extend the value as appropriate. */
1298 ((type
->is_unsigned () ? ax_zero_ext
: ax_ext
) (ax
, end
- start
));
1300 /* This is *not* an lvalue. Ugh. */
1301 value
->kind
= axs_rvalue
;
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. */
1311 gen_primitive_field (struct agent_expr
*ax
, struct axs_value
*value
,
1312 int offset
, int fieldno
, struct type
*type
)
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 ()));
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 ();
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. */
1335 gen_struct_ref_recursive (struct agent_expr
*ax
, struct axs_value
*value
,
1336 const char *field
, int offset
, struct type
*type
)
1339 int nbases
= TYPE_N_BASECLASSES (type
);
1341 type
= check_typedef (type
);
1343 for (i
= type
->num_fields () - 1; i
>= nbases
; i
--)
1345 const char *this_name
= type
->field (i
).name ();
1349 if (strcmp (field
, this_name
) == 0)
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 ())
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"),
1365 gen_primitive_field (ax
, value
, offset
, i
, type
);
1368 #if 0 /* is this right? */
1369 if (this_name
[0] == '\0')
1370 internal_error (_("find_field: anonymous unions not supported"));
1375 /* Now scan through base classes recursively. */
1376 for (i
= 0; i
< nbases
; i
++)
1378 struct type
*basetype
= check_typedef (TYPE_BASECLASS (type
, i
));
1380 rslt
= gen_struct_ref_recursive (ax
, value
, field
,
1381 offset
+ TYPE_BASECLASS_BITPOS (type
, i
)
1388 /* Not found anywhere, flag so caller can complain. */
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. */
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
)
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 ())
1410 require_rvalue (ax
, value
);
1413 type
= check_typedef (value
->type
);
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
);
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."));
1426 /* Search through fields and base classes recursively. */
1427 found
= gen_struct_ref_recursive (ax
, value
, field
, 0, type
);
1430 error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
1431 field
, type
->name ());
1435 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1436 const struct type
*curtype
, const char *name
);
1438 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1439 const struct type
*curtype
, const char *name
);
1442 gen_static_field (struct agent_expr
*ax
, struct axs_value
*value
,
1443 struct type
*type
, int fieldno
)
1445 if (type
->field (fieldno
).loc_kind () == FIELD_LOC_KIND_PHYSADDR
)
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;
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
;
1460 gen_var_ref (ax
, value
, sym
);
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. */
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;
1476 gen_struct_elt_for_reference (struct agent_expr
*ax
, struct axs_value
*value
,
1477 struct type
*type
, const char *fieldname
)
1479 struct type
*t
= type
;
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"));
1486 for (i
= t
->num_fields () - 1; i
>= TYPE_N_BASECLASSES (t
); i
--)
1488 const char *t_field_name
= t
->field (i
).name ();
1490 if (t_field_name
&& strcmp (t_field_name
, fieldname
) == 0)
1492 if (t
->field (i
).is_static ())
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"),
1501 if (t
->field (i
).is_packed ())
1502 error (_("pointers to bitfield members not allowed"));
1504 /* FIXME we need a way to do "want_address" equivalent */
1506 error (_("Cannot reference non-static field \"%s\""), fieldname
);
1510 /* FIXME add other scoped-reference cases here */
1512 /* Do a last-ditch lookup. */
1513 return gen_maybe_namespace_elt (ax
, value
, type
, fieldname
);
1516 /* C++: Return the member NAME of the namespace given by the type
1520 gen_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1521 const struct type
*curtype
, const char *name
)
1523 int found
= gen_maybe_namespace_elt (ax
, value
, curtype
, name
);
1526 error (_("No symbol \"%s\" in namespace \"%s\"."),
1527 name
, curtype
->name ());
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). */
1539 gen_maybe_namespace_elt (struct agent_expr
*ax
, struct axs_value
*value
,
1540 const struct type
*curtype
, const char *name
)
1542 const char *namespace_name
= curtype
->name ();
1543 struct block_symbol sym
;
1545 sym
= cp_lookup_symbol_namespace (namespace_name
, name
,
1546 block_for_pc (ax
->scope
),
1549 if (sym
.symbol
== NULL
)
1552 gen_var_ref (ax
, value
, sym
.symbol
);
1554 if (value
->optimized_out
)
1555 error (_("`%s' has been optimized out, cannot use"),
1556 sym
.symbol
->print_name ());
1563 gen_aggregate_elt_ref (struct agent_expr
*ax
, struct axs_value
*value
,
1564 struct type
*type
, const char *field
)
1566 switch (type
->code ())
1568 case TYPE_CODE_STRUCT
:
1569 case TYPE_CODE_UNION
:
1570 return gen_struct_elt_for_reference (ax
, value
, type
, field
);
1572 case TYPE_CODE_NAMESPACE
:
1573 return gen_namespace_elt (ax
, value
, type
, field
);
1576 internal_error (_("non-aggregate type in gen_aggregate_elt_ref"));
1588 operation::generate_ax (struct expression
*exp
,
1589 struct agent_expr
*ax
,
1590 struct axs_value
*value
,
1591 struct type
*cast_type
)
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 ());
1602 do_generate_ax (exp
, ax
, value
, cast_type
);
1603 if (cast_type
!= nullptr)
1604 gen_cast (ax
, value
, 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
)
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 ());
1618 error (_("There is no field named %s"), name
.c_str ());
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
)
1627 LONGEST val
= as_longest ();
1628 gen_int_literal (ax
, value
, val
, std::get
<0> (m_storage
));
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
)
1637 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
1638 gen_msym_var_ref (ax
, value
, b
.minsym
, b
.objfile
);
1640 if (value
->type
->code () == TYPE_CODE_ERROR
)
1642 if (cast_type
== nullptr)
1643 error_unknown_type (b
.minsym
->linkage_name ());
1644 value
->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
)
1654 const char *name
= std::get
<0> (m_storage
).c_str ();
1655 int len
= std::get
<0> (m_storage
).size ();
1658 reg
= user_reg_map_name_to_regnum (ax
->gdbarch
, name
, len
);
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."),
1666 value
->kind
= axs_lvalue_register
;
1668 value
->type
= register_type (ax
->gdbarch
, reg
);
1672 internalvar_operation::do_generate_ax (struct expression
*exp
,
1673 struct agent_expr
*ax
,
1674 struct axs_value
*value
,
1675 struct type
*cast_type
)
1677 struct internalvar
*var
= std::get
<0> (m_storage
);
1678 const char *name
= internalvar_name (var
);
1679 struct trace_state_variable
*tsv
;
1681 tsv
= find_trace_state_variable (name
);
1684 ax_tsv (ax
, aop_getv
, tsv
->number
);
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
;
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
);
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
)
1702 struct axs_value value1
, value2
, value3
;
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
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
;
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.
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. */
1737 repeat_operation::do_generate_ax (struct expression
*exp
,
1738 struct agent_expr
*ax
,
1739 struct axs_value
*value
,
1740 struct type
*cast_type
)
1742 struct axs_value value1
;
1744 /* We don't want to turn this into an rvalue, so no conversions
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."));
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."));
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
);
1762 error (_("Right operand of `@' must be positive."));
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
1769 = lookup_array_range_type (value1
.type
, 0, length
- 1);
1771 value
->kind
= axs_lvalue_memory
;
1772 value
->type
= array
;
1776 comma_operation::do_generate_ax (struct expression
*exp
,
1777 struct agent_expr
*ax
,
1778 struct axs_value
*value
,
1779 struct type
*cast_type
)
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
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. */
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
)
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 ();
1810 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1812 /* Throw away the code we just generated. */
1813 ax
->buf
.resize (start
);
1815 ax_const_l (ax
, value
->type
->length ());
1816 value
->kind
= axs_rvalue
;
1817 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
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
)
1826 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
,
1827 std::get
<1> (m_storage
));
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
)
1836 std::get
<0> (m_storage
)->generate_ax (exp
, ax
, value
);
1838 struct type
*to_type
= get_type ();
1840 if (!is_scalar_type (to_type
))
1841 error (_("can't generate agent expression to extract non-scalar type"));
1843 if (to_type
->is_unsigned ())
1844 gen_extend (ax
, to_type
);
1846 gen_sign_extend (ax
, to_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
)
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
);
1862 value
->type
= std::get
<1> (m_storage
);
1863 value
->kind
= axs_lvalue_memory
;
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
)
1873 = std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1874 EVAL_AVOID_SIDE_EFFECTS
);
1875 struct type
*type
= val
->type ();
1877 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
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
);
1886 value
->kind
= axs_lvalue_memory
;
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
)
1895 struct symbol
*sym
, *func
;
1896 const struct block
*b
;
1897 const struct language_defn
*lang
;
1899 b
= block_for_pc (ax
->scope
);
1900 func
= b
->linkage_function ();
1901 lang
= language_def (func
->language ());
1903 sym
= lookup_language_this (lang
, b
).symbol
;
1905 error (_("no `%s' found"), lang
->name_of_this ());
1907 gen_var_ref (ax
, value
, sym
);
1909 if (value
->optimized_out
)
1910 error (_("`%s' has been optimized out, cannot use"),
1911 sym
->print_name ());
1915 assign_operation::do_generate_ax (struct expression
*exp
,
1916 struct agent_expr
*ax
,
1917 struct axs_value
*value
,
1918 struct type
*cast_type
)
1920 operation
*subop
= std::get
<0> (m_storage
).get ();
1921 if (subop
->opcode () != OP_INTERNALVAR
)
1922 error (_("May only assign to trace state variables"));
1924 internalvar_operation
*ivarop
1925 = gdb::checked_static_cast
<internalvar_operation
*> (subop
);
1927 const char *name
= internalvar_name (ivarop
->get_internalvar ());
1928 struct trace_state_variable
*tsv
;
1930 std::get
<1> (m_storage
)->generate_ax (exp
, ax
, value
);
1931 tsv
= find_trace_state_variable (name
);
1934 ax_tsv (ax
, aop_setv
, tsv
->number
);
1936 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1939 error (_("$%s is not a trace state variable, "
1940 "may not assign to it"), name
);
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
)
1949 operation
*subop
= std::get
<1> (m_storage
).get ();
1950 if (subop
->opcode () != OP_INTERNALVAR
)
1951 error (_("May only assign to trace state variables"));
1953 internalvar_operation
*ivarop
1954 = gdb::checked_static_cast
<internalvar_operation
*> (subop
);
1956 const char *name
= internalvar_name (ivarop
->get_internalvar ());
1957 struct trace_state_variable
*tsv
;
1959 tsv
= find_trace_state_variable (name
);
1962 /* The tsv will be the left half of the binary operation. */
1963 ax_tsv (ax
, aop_getv
, tsv
->number
);
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
);
1977 ax_tsv (ax
, aop_tracev
, tsv
->number
);
1980 error (_("$%s is not a trace state variable, "
1981 "may not assign to it"), name
);
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
)
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 ());
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
)
2002 gen_var_ref (ax
, value
, std::get
<0> (m_storage
).symbol
);
2004 if (value
->optimized_out
)
2005 error (_("`%s' has been optimized out, cannot use"),
2006 std::get
<0> (m_storage
).symbol
->print_name ());
2008 if (value
->type
->code () == TYPE_CODE_ERROR
)
2010 if (cast_type
== nullptr)
2011 error_unknown_type (std::get
<0> (m_storage
).symbol
->print_name ());
2012 value
->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
)
2022 struct axs_value value1
, value2
;
2023 int if1
, go1
, if2
, go2
, end
;
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 ());
2037 end
= ax_goto (ax
, aop_goto
);
2038 ax_label (ax
, go1
, ax
->buf
.size ());
2039 ax_label (ax
, go2
, ax
->buf
.size ());
2041 ax_label (ax
, end
, ax
->buf
.size ());
2042 value
->kind
= axs_rvalue
;
2043 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
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
)
2052 struct axs_value value1
, value2
;
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
);
2063 end
= ax_goto (ax
, aop_goto
);
2064 ax_label (ax
, if1
, ax
->buf
.size ());
2065 ax_label (ax
, if2
, ax
->buf
.size ());
2067 ax_label (ax
, end
, ax
->buf
.size ());
2068 value
->kind
= axs_rvalue
;
2069 value
->type
= builtin_type (ax
->gdbarch
)->builtin_int
;
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. */
2079 gen_expr_binop_rest (struct expression
*exp
,
2081 struct agent_expr
*ax
, struct axs_value
*value
,
2082 struct axs_value
*value1
, struct axs_value
*value2
)
2084 struct type
*int_type
= builtin_type (ax
->gdbarch
)->builtin_int
;
2086 gen_usual_unary (ax
, value2
);
2087 gen_usual_arithmetic (ax
, value1
, value2
);
2091 if (strip_range_type (value1
->type
)->code () == TYPE_CODE_INT
2092 && value2
->type
->is_pointer_or_reference ())
2094 /* Swap the values and proceed normally. */
2095 ax_simple (ax
, aop_swap
);
2096 gen_ptradd (ax
, value
, value2
, value1
);
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
);
2102 gen_binop (ax
, value
, value1
, value2
,
2103 aop_add
, aop_add
, 1, "addition");
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
);
2115 gen_binop (ax
, value
, value1
, value2
,
2116 aop_sub
, aop_sub
, 1, "subtraction");
2119 gen_binop (ax
, value
, value1
, value2
,
2120 aop_mul
, aop_mul
, 1, "multiplication");
2123 gen_binop (ax
, value
, value1
, value2
,
2124 aop_div_signed
, aop_div_unsigned
, 1, "division");
2127 gen_binop (ax
, value
, value1
, value2
,
2128 aop_rem_signed
, aop_rem_unsigned
, 1, "remainder");
2131 gen_binop (ax
, value
, value1
, value2
,
2132 aop_lsh
, aop_lsh
, 1, "left shift");
2135 gen_binop (ax
, value
, value1
, value2
,
2136 aop_rsh_signed
, aop_rsh_unsigned
, 1, "right shift");
2138 case BINOP_SUBSCRIPT
:
2142 if (binop_types_user_defined_p (op
, value1
->type
, value2
->type
))
2144 error (_("cannot subscript requested type: "
2145 "cannot call user defined functions"));
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
)
2157 error (_("cannot subscript something of type `%s'"),
2160 error (_("cannot subscript requested type"));
2164 if (!is_integral_type (value2
->type
))
2165 error (_("Argument to arithmetic operation "
2166 "not a number or boolean."));
2168 gen_ptradd (ax
, value
, value1
, value2
);
2172 case BINOP_BITWISE_AND
:
2173 gen_binop (ax
, value
, value1
, value2
,
2174 aop_bit_and
, aop_bit_and
, 0, "bitwise and");
2177 case BINOP_BITWISE_IOR
:
2178 gen_binop (ax
, value
, value1
, value2
,
2179 aop_bit_or
, aop_bit_or
, 0, "bitwise or");
2182 case BINOP_BITWISE_XOR
:
2183 gen_binop (ax
, value
, value1
, value2
,
2184 aop_bit_xor
, aop_bit_xor
, 0, "bitwise exclusive-or");
2188 gen_equal (ax
, value
, value1
, value2
, int_type
);
2191 case BINOP_NOTEQUAL
:
2192 gen_equal (ax
, value
, value1
, value2
, int_type
);
2193 gen_logical_not (ax
, value
, int_type
);
2197 gen_less (ax
, value
, value1
, value2
, int_type
);
2201 ax_simple (ax
, aop_swap
);
2202 gen_less (ax
, value
, value1
, value2
, int_type
);
2206 ax_simple (ax
, aop_swap
);
2207 gen_less (ax
, value
, value1
, value2
, int_type
);
2208 gen_logical_not (ax
, value
, int_type
);
2212 gen_less (ax
, value
, value1
, value2
, int_type
);
2213 gen_logical_not (ax
, value
, int_type
);
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"));
2223 /* A helper function that emits a binop based on two operations. */
2226 gen_expr_binop (struct expression
*exp
,
2228 expr::operation
*lhs
, expr::operation
*rhs
,
2229 struct agent_expr
*ax
, struct axs_value
*value
)
2231 struct axs_value value1
, value2
;
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
);
2239 /* A helper function that emits a structop based on an operation and a
2243 gen_expr_structop (struct expression
*exp
,
2245 expr::operation
*lhs
,
2247 struct agent_expr
*ax
, struct axs_value
*value
)
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");
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"));
2261 /* A helper function that emits a unary operation. */
2264 gen_expr_unop (struct expression
*exp
,
2266 expr::operation
*lhs
,
2267 struct agent_expr
*ax
, struct axs_value
*value
)
2269 struct axs_value value1
, value2
;
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");
2284 /* + FOO is equivalent to 0 + FOO, which can be optimized. */
2285 lhs
->generate_ax (exp
, ax
, value
);
2286 gen_usual_unary (ax
, value
);
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
);
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
);
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."));
2311 lhs
->generate_ax (exp
, ax
, value
);
2312 gen_address_of (value
);
2316 gdb_assert_not_reached ("invalid case in gen_expr_unop");
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. */
2328 gen_trace_for_var (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2329 struct symbol
*var
, int trace_string
)
2331 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2332 struct axs_value value
;
2335 ax
->trace_string
= trace_string
;
2336 gen_var_ref (ax
.get (), &value
, var
);
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 ();
2343 /* Make sure we record the final object, and get rid of it. */
2344 gen_traced_pop (ax
.get (), &value
);
2346 /* Oh, and terminate. */
2347 ax_simple (ax
.get (), aop_end
);
2352 /* Generating bytecode from GDB expressions: driver */
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. */
2361 gen_trace_for_expr (CORE_ADDR scope
, struct expression
*expr
,
2364 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2365 struct axs_value value
;
2368 ax
->trace_string
= trace_string
;
2369 value
.optimized_out
= 0;
2370 expr
->op
->generate_ax (expr
, ax
.get (), &value
);
2372 /* Make sure we record the final object, and get rid of it. */
2373 gen_traced_pop (ax
.get (), &value
);
2375 /* Oh, and terminate. */
2376 ax_simple (ax
.get (), aop_end
);
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. */
2389 gen_eval_for_expr (CORE_ADDR scope
, struct expression
*expr
)
2391 agent_expr_up
ax (new agent_expr (expr
->gdbarch
, scope
));
2392 struct axs_value value
;
2394 ax
->tracing
= false;
2395 value
.optimized_out
= 0;
2396 expr
->op
->generate_ax (expr
, ax
.get (), &value
);
2398 require_rvalue (ax
.get (), &value
);
2400 /* Oh, and terminate. */
2401 ax_simple (ax
.get (), aop_end
);
2407 gen_trace_for_return_address (CORE_ADDR scope
, struct gdbarch
*gdbarch
,
2410 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2411 struct axs_value value
;
2414 ax
->trace_string
= trace_string
;
2416 gdbarch_gen_return_address (gdbarch
, ax
.get (), &value
, scope
);
2418 /* Make sure we record the final object, and get rid of it. */
2419 gen_traced_pop (ax
.get (), &value
);
2421 /* Oh, and terminate. */
2422 ax_simple (ax
.get (), aop_end
);
2427 /* Given a collection of printf-style arguments, generate code to
2428 evaluate the arguments and pass everything to a special
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
)
2437 agent_expr_up
ax (new agent_expr (gdbarch
, scope
));
2438 struct axs_value value
;
2441 /* We're computing values, not doing side effects. */
2442 ax
->tracing
= false;
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
)
2448 value
.optimized_out
= 0;
2449 exprs
[tem
]->op
->generate_ax (exprs
[tem
], ax
.get (), &value
);
2450 require_rvalue (ax
.get (), &value
);
2453 /* Push function and channel. */
2454 ax_const_l (ax
.get (), channel
);
2455 ax_const_l (ax
.get (), function
);
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
);
2462 /* And terminate. */
2463 ax_simple (ax
.get (), aop_end
);
2469 agent_eval_command_one (const char *exp
, int eval
, CORE_ADDR pc
)
2472 int trace_string
= 0;
2477 exp
= decode_agent_options (exp
, &trace_string
);
2480 agent_expr_up agent
;
2483 if (!eval
&& strcmp (arg
, "$_ret") == 0)
2485 agent
= gen_trace_for_return_address (pc
, get_current_arch (),
2490 expression_up expr
= parse_exp_1 (&arg
, pc
, block_for_pc (pc
), 0);
2494 gdb_assert (trace_string
== 0);
2495 agent
= gen_eval_for_expr (pc
, expr
.get ());
2498 agent
= gen_trace_for_expr (pc
, expr
.get (), trace_string
);
2501 ax_reqs (agent
.get ());
2502 ax_print (gdb_stdout
, agent
.get ());
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. */
2511 maint_agent_command_1 (const char *exp
, int eval
)
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."));
2521 error_no_arg (_("expression to translate"));
2523 if (check_for_argument (&exp
, "-at", sizeof ("-at") - 1))
2525 struct linespec_result canonical
;
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
,
2532 exp
= skip_spaces (exp
);
2536 exp
= skip_spaces (exp
);
2538 for (const auto &lsal
: canonical
.lsals
)
2539 for (const auto &sal
: lsal
.sals
)
2540 agent_eval_command_one (exp
, eval
, sal
.pc
);
2543 agent_eval_command_one (exp
, eval
, get_frame_pc (get_current_frame ()));
2549 maint_agent_command (const char *exp
, int from_tty
)
2551 maint_agent_command_1 (exp
, 0);
2554 /* Parse the given expression, compile it into an agent expression
2555 that does direct evaluation, and display the resulting
2559 maint_agent_eval_command (const char *exp
, int from_tty
)
2561 maint_agent_command_1 (exp
, 1);
2564 /* Parse the given expression, compile it into an agent expression
2565 that does a printf, and display the resulting expression. */
2568 maint_agent_printf_command (const char *cmdrest
, int from_tty
)
2570 frame_info_ptr fi
= get_current_frame (); /* need current scope */
2571 const char *format_start
, *format_end
;
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."));
2581 error_no_arg (_("expression to translate"));
2583 cmdrest
= skip_spaces (cmdrest
);
2585 if (*cmdrest
++ != '"')
2586 error (_("Must start with a format string."));
2588 format_start
= cmdrest
;
2590 format_pieces
fpieces (&cmdrest
);
2592 format_end
= cmdrest
;
2594 if (*cmdrest
++ != '"')
2595 error (_("Bad format string, non-terminated '\"'."));
2597 cmdrest
= skip_spaces (cmdrest
);
2599 if (*cmdrest
!= ',' && *cmdrest
!= 0)
2600 error (_("Invalid argument syntax"));
2602 if (*cmdrest
== ',')
2604 cmdrest
= skip_spaces (cmdrest
);
2606 std::vector
<struct expression
*> argvec
;
2607 while (*cmdrest
!= '\0')
2612 expression_up expr
= parse_exp_1 (&cmd1
, 0, (struct block
*) 0,
2613 PARSER_COMMA_TERMINATES
);
2614 argvec
.push_back (expr
.release ());
2616 if (*cmdrest
== ',')
2618 /* else complain? */
2622 agent_expr_up agent
= gen_printf (get_frame_pc (fi
), get_current_arch (),
2624 format_start
, format_end
- format_start
,
2625 argvec
.size (), argvec
.data ());
2626 ax_reqs (agent
.get ());
2627 ax_print (gdb_stdout
, agent
.get ());
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. */
2635 /* Initialization code. */
2637 void _initialize_ax_gdb ();
2639 _initialize_ax_gdb ()
2641 add_cmd ("agent", class_maintenance
, maint_agent_command
,
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."),
2649 add_cmd ("agent-eval", class_maintenance
, maint_agent_eval_command
,
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."),
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."),